text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import tensorflow.contrib.slim as slim
from tensorflow.contrib.slim import arg_scope
from tensorflow.contrib.slim.python.slim.nets import resnet_utils
from tensorflow.contrib.slim.python.slim.nets import resnet_v1
from tensorflow.contrib.layers.python.layers import layers
from tensorflow.contrib.layers.python.layers import regularizers
from tensorflow.python.ops import nn_ops
from tensorflow.contrib.layers.python.layers import initializers
from tensorflow.python.framework import ops

from ult.config import cfg
from ult.ava_loss_weight import verb80
from ult.visualization import draw_bounding_boxes_HOI

import numpy as np

def resnet_arg_scope(is_training=True,
                     weight_decay=cfg.TRAIN.WEIGHT_DECAY,
                     batch_norm_decay=0.997,
                     batch_norm_epsilon=1e-5,
                     batch_norm_scale=True):
    batch_norm_params = {
        'is_training': False,
        'decay': batch_norm_decay,
        'epsilon': batch_norm_epsilon,
        'scale': batch_norm_scale,
        'trainable': False,
        'updates_collections': ops.GraphKeys.UPDATE_OPS
    }
    with arg_scope(
        [slim.conv2d, slim.fully_connected],
        weights_regularizer = tf.contrib.layers.l2_regularizer(cfg.TRAIN.WEIGHT_DECAY),
        weights_initializer = slim.variance_scaling_initializer(),
        biases_regularizer  = tf.contrib.layers.l2_regularizer(cfg.TRAIN.WEIGHT_DECAY),
        biases_initializer  = tf.constant_initializer(0.0),
        trainable           = is_training,
        activation_fn       = tf.nn.relu,
        normalizer_fn       = slim.batch_norm,
        normalizer_params   = batch_norm_params):
        with arg_scope([slim.batch_norm], **batch_norm_params) as arg_sc:
            return arg_sc

"""Contains definitions for the original form of Residual Networks.
    The 'v1' residual networks (ResNets) implemented in this module were proposed
    by:
    [1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
        Deep Residual Learning for Image Recognition. arXiv:1512.03385
    Other variants were introduced in:
    [2] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
        Identity Mappings in Deep Residual Networks. arXiv: 1603.05027
    The networks defined in this module utilize the bottleneck building block of
    [1] with projection shortcuts only for increasing depths. They employ batch
    normalization *after* every weight layer. This is the architecture used by
    MSRA in the Imagenet and MSCOCO 2016 competition models ResNet-101 and
    ResNet-152. See [2; Fig. 1a] for a comparison between the current 'v1'
    architecture and the alternative 'v2' architecture of [2] which uses batch
    normalization *before* every weight layer in the so-called full pre-activation
    units.
    Typical use:
        from tensorflow.contrib.slim.python.slim.nets import resnet_v1
    ResNet-101 for image classification into 1000 classes:
        # inputs has shape [batch, 224, 224, 3]
        with slim.arg_scope(resnet_v1.resnet_arg_scope()):
            net, end_points = resnet_v1.resnet_v1_101(inputs, 1000, is_training=False)
    ResNet-101 for semantic segmentation into 21 classes:
    # inputs has shape [batch, 513, 513, 3]
        with slim.arg_scope(resnet_v1.resnet_arg_scope()):
            net, end_points = resnet_v1.resnet_v1_101(inputs,
                                                    21,
                                                    is_training=False,
                                                    global_pool=False,
                                                    output_stride=16)
"""
"""
    Helper function for creating a resnet_v1 bottleneck block.
    Args:
        scope: The scope of the block.
        base_depth: The depth of the bottleneck layer for each unit.
        num_units: The number of units in the block.
        stride: The stride of the block, implemented as a stride in the last unit.
                All other units have stride=1.
    Returns:
        A resnet_v1 bottleneck block.
"""
"""Generator for v1 ResNet models.
    This function generates a family of ResNet v1 models. See the resnet_v1_*()
    methods for specific model instantiations, obtained by selecting different
    block instantiations that produce ResNets of various depths.
    Training for image classification on Imagenet is usually done with [224, 224]
    inputs, resulting in [7, 7] feature maps at the output of the last ResNet
    block for the ResNets defined in [1] that have nominal stride equal to 32.
    However, for dense prediction tasks we advise that one uses inputs with
    spatial dimensions that are multiples of 32 plus 1, e.g., [321, 321]. In
    this case the feature maps at the ResNet output will have spatial shape
    [(height - 1) / output_stride + 1, (width - 1) / output_stride + 1]
    and corners exactly aligned with the input image corners, which greatly
    facilitates alignment of the features to the image. Using as input [225, 225]
    images results in [8, 8] feature maps at the output of the last ResNet block.
    For dense prediction tasks, the ResNet needs to run in fully-convolutional
    (FCN) mode and global_pool needs to be set to False. The ResNets in [1, 2] all
    have nominal stride equal to 32 and a good choice in FCN mode is to use
    output_stride=16 in order to increase the density of the computed features at
    small computational and memory overhead, cf. http://arxiv.org/abs/1606.00915.

    Args:
        inputs: A tensor of size [batch, height_in, width_in, channels].
        blocks: A list of length equal to the number of ResNet blocks. Each element
                is a resnet_utils.Block object describing the units in the block.
        num_classes: Number of predicted classes for classification tasks. If None
                    we return the features before the logit layer.
        is_training: whether batch_norm layers are in training mode.
        global_pool: If True, we perform global average pooling before computing the
                    logits. Set to True for image classification, False for dense prediction.
        output_stride: If None, then the output will be computed at the nominal
                        network stride. If output_stride is not None, it specifies the requested
                        ratio of input to output spatial resolution.
        include_root_block: If True, include the initial convolution followed by
                            max-pooling, if False excludes it.
        reuse: whether or not the network and its variables should be reused. To be
                able to reuse 'scope' must be given.
        scope: Optional variable_scope.
    Returns:
        net: A rank-4 tensor of size [batch, height_out, width_out, channels_out].
            If global_pool is False, then height_out and width_out are reduced by a
            factor of output_stride compared to the respective height_in and width_in,
            else both height_out and width_out equal one. If num_classes is None, then
            net is the output of the last ResNet block, potentially after global
            average pooling. If num_classes is not None, net contains the pre-softmax
            activations.
        end_points: A dictionary from components of the network to the corresponding
                    activation.
    Raises:
        ValueError: If the target output_stride is not valid.
"""

class ResNet50(): # 64--128--256--512--512
    def __init__(self):
        self.visualize = {}
        self.intermediate = {}
        self.predictions = {}
        self.score_summaries = {}
        self.event_summaries = {}
        self.train_summaries = []
        self.losses = {}
        self.image       = tf.placeholder(tf.float32, shape=[1, None, None, 3], name = 'image') #scene stream
        self.H_boxes     = tf.placeholder(tf.float32, shape=[None, 5], name = 'H_boxes') # Human stream
        self.P_boxes     = tf.placeholder(tf.float32, shape=[None, 10, 5], name = 'P_boxes') # PaSta stream
        self.gt_verb     = tf.placeholder(tf.float32, shape=[None, 80], name = 'gt_class_verb') # target verb
        self.H_num       = tf.placeholder(tf.int32)
        self.verb_weight = np.array(verb80, dtype='float32').reshape(1, -1)
        self.num_classes = 80 # HOI
        self.num_pasta0    = 12 # pasta0 ankle
        self.num_pasta1    = 10 # pasta1 knee
        self.num_pasta2    = 5 # pasta2 hip
        self.num_pasta3    = 31 # pasta3 hand
        self.num_pasta4    = 5 # pasta4 shoulder
        self.num_pasta5    = 13 # pasta5 head
        self.num_fc      = 1024
        self.scope       = 'resnet_v1_50'
        self.stride      = [16, ]
        self.lr          = tf.placeholder(tf.float32)
        if tf.__version__ == '1.1.0':
            self.blocks     = [resnet_utils.Block('block1', resnet_v1.bottleneck,[(256,   64, 1)] * 2 + [(256,   64, 2)]),
                               resnet_utils.Block('block2', resnet_v1.bottleneck,[(512,  128, 1)] * 3 + [(512,  128, 2)]),
                               resnet_utils.Block('block3', resnet_v1.bottleneck,[(1024, 256, 1)] * 5 + [(1024, 256, 1)]),
                               resnet_utils.Block('block4', resnet_v1.bottleneck,[(2048, 512, 1)] * 3),
                               resnet_utils.Block('block5', resnet_v1.bottleneck,[(2048, 512, 1)] * 3)]
        else: # we use tf 1.2.0 here, Resnet-50
            from tensorflow.contrib.slim.python.slim.nets.resnet_v1 import resnet_v1_block
            self.blocks = [resnet_v1_block('block1', base_depth=64,  num_units=3, stride=2), # a resnet_v1 bottleneck block
                           resnet_v1_block('block2', base_depth=128, num_units=4, stride=2),
                           resnet_v1_block('block3', base_depth=256, num_units=6, stride=1), # feature former
                           resnet_v1_block('block4', base_depth=512, num_units=3, stride=1),
                           resnet_v1_block('block5', base_depth=512, num_units=3, stride=1)]
    # Args:
    #   scope: The scope of the block.
    #   base_depth: The depth of the bottleneck layer for each unit.
    #   num_units: The number of units in the block.
    #   stride: The stride of the block, implemented as a stride in the last unit.
    #     All other units have stride=1.

    def build_base(self):
        with tf.variable_scope(self.scope, self.scope):
            net = resnet_utils.conv2d_same(self.image, 64, 7, stride=2, scope='conv1') # conv2d + subsample
            net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [0, 0]])
            net = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', scope='pool1')
        return net

    # Number of fixed blocks during training, by default
    # __C.RESNET.FIXED_BLOCKS = 1

    # feature extractor
    def image_to_head(self, is_training):
        with slim.arg_scope(resnet_arg_scope(is_training=False)):
            net    = self.build_base()
            net, _ = resnet_v1.resnet_v1(net,
                                         self.blocks[0:cfg.RESNET.FIXED_BLOCKS],
                                         global_pool=False,
                                         include_root_block=False,
                                         scope=self.scope)
        with slim.arg_scope(resnet_arg_scope(is_training=is_training)):
            head, _ = resnet_v1.resnet_v1(net,
                                          self.blocks[cfg.RESNET.FIXED_BLOCKS:-2],
                                          global_pool=False,
                                          include_root_block=False,
                                          scope=self.scope)
        return head

    def res5(self, pool5_H, is_training, name):
        with slim.arg_scope(resnet_arg_scope(is_training=is_training)):

            pool5_H, _ = resnet_v1.resnet_v1(pool5_H,
                                           self.blocks[-2:-1],
                                           global_pool=False,
                                           include_root_block=False,
                                           reuse=False,
                                           scope=self.scope)

            fc5_H = tf.reduce_mean(pool5_H, axis=[1, 2])

        return fc5_H

    def crop_pool_layer(self, bottom, rois, name):
        with tf.variable_scope(name) as scope:

            batch_ids    = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name="batch_id"), [1])
            bottom_shape = tf.shape(bottom)
            height       = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self.stride[0])
            width        = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self.stride[0])
            x1 = tf.slice(rois, [0, 1], [-1, 1], name="x1") / width
            y1 = tf.slice(rois, [0, 2], [-1, 1], name="y1") / height
            x2 = tf.slice(rois, [0, 3], [-1, 1], name="x2") / width
            y2 = tf.slice(rois, [0, 4], [-1, 1], name="y2") / height

            bboxes        = tf.stop_gradient(tf.concat([y1, x1, y2, x2], axis=1))
            if cfg.RESNET.MAX_POOL:
                pre_pool_size = cfg.POOLING_SIZE * 2
                crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size], name="crops")
                crops = slim.max_pool2d(crops, [2, 2], padding='SAME')
            else:
                crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [cfg.POOLING_SIZE, cfg.POOLING_SIZE], name="crops")
        return crops

    def region_classification(self, fc7_P, is_training, initializer, name):
        with tf.variable_scope(name) as scope:

            cls_score_P = slim.fully_connected(fc7_P, self.num_classes,
                                               weights_initializer=initializer,
                                               trainable=is_training,
                                               activation_fn=None, scope='cls_score_P')
            cls_prob_P  = tf.nn.sigmoid(cls_score_P, name='cls_prob_P')
            tf.reshape(cls_prob_P, [1, self.num_classes])
            self.predictions["cls_score_P"] = cls_score_P
            self.predictions["cls_prob_P"]  = cls_prob_P

            self.predictions["cls_prob_HO"]  = cls_prob_P

        return

    def part_classification(self, pool5_P, is_training, initializer, num_state, name):
        with tf.variable_scope(name) as scope:
            fc6_P    = slim.fully_connected(pool5_P, 512)
            fc6_P    = slim.dropout(fc6_P, keep_prob=0.5, is_training=is_training)
            fc7_P    = slim.fully_connected(fc6_P, 512)
            fc7_P    = slim.dropout(fc7_P, keep_prob=0.5, is_training=is_training)

        return fc7_P

    def pasta_classification(self, pool5_P0, pool5_P1, pool5_P2, pool5_P3, pool5_P4, pool5_P5, is_training, initializer, name):
        with tf.variable_scope(name) as scope:
            fc7_P0 = self.part_classification(pool5_P0, is_training, initializer, self.num_pasta0, 'cls_pasta_0')
            fc7_P1 = self.part_classification(pool5_P1, is_training, initializer, self.num_pasta1, 'cls_pasta_1')
            fc7_P2 = self.part_classification(pool5_P2, is_training, initializer, self.num_pasta2, 'cls_pasta_2')
            fc7_P3 = self.part_classification(pool5_P3, is_training, initializer, self.num_pasta3, 'cls_pasta_3')
            fc7_P4 = self.part_classification(pool5_P4, is_training, initializer, self.num_pasta4, 'cls_pasta_4')
            fc7_P5 = self.part_classification(pool5_P5, is_training, initializer, self.num_pasta5, 'cls_pasta_5')
            fc7_P  = tf.concat([fc7_P0, fc7_P1, fc7_P2, fc7_P3, fc7_P4, fc7_P5], axis=1)
        return fc7_P

    def ROI_for_parts(self, head, fc5_H, fc5_S, P_boxes, name):
        with tf.variable_scope(name) as scope:
            pool5_P0 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 0, :], 'crop_P0'), axis=[1, 2]) # RAnk
            pool5_P1 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 1, :], 'crop_P1'), axis=[1, 2]) # RKnee
            pool5_P2 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 2, :], 'crop_P2'), axis=[1, 2]) # LKnee
            pool5_P3 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 3, :], 'crop_P3'), axis=[1, 2]) # LAnk
            pool5_P4 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 4, :], 'crop_P4'), axis=[1, 2]) # Hip
            pool5_P5 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 5, :], 'crop_P5'), axis=[1, 2]) # Head
            pool5_P6 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 6, :], 'crop_P6'), axis=[1, 2]) # RHand
            pool5_P7 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 7, :], 'crop_P7'), axis=[1, 2]) # RSho
            pool5_P8 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 8, :], 'crop_P8'), axis=[1, 2]) # LSho
            pool5_P9 = tf.reduce_mean(self.crop_pool_layer(head, P_boxes[:, 9, :], 'crop_P9'), axis=[1, 2]) # LHand

            fc5_S    = tf.tile(fc5_S, [tf.shape(pool5_P0)[0], 1])
            fc5_P0 = tf.concat([pool5_P0, pool5_P3, fc5_H, fc5_S], axis=1)
            fc5_P1 = tf.concat([pool5_P1, pool5_P2, fc5_H, fc5_S], axis=1)
            fc5_P2 = tf.concat([pool5_P4, fc5_H, fc5_S], axis=1)
            fc5_P3 = tf.concat([pool5_P6, pool5_P9, fc5_H, fc5_S], axis=1)
            fc5_P4 = tf.concat([pool5_P7, pool5_P8, fc5_H, fc5_S], axis=1)
            fc5_P5 = tf.concat([pool5_P5, fc5_H, fc5_S], axis=1)

        return fc5_P0, fc5_P1, fc5_P2, fc5_P3, fc5_P4, fc5_P5

    def build_network(self, is_training):
        initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)

        head       = self.image_to_head(is_training)
        pool5_H    = self.crop_pool_layer(head, self.H_boxes, 'Crop_H')

        fc5_H = self.res5(pool5_H, is_training, name='res5_HR')
        fc5_S = tf.reduce_mean(head, axis=[1, 2])
        fc5_P0, fc5_P1, fc5_P2, fc5_P3, fc5_P4, fc5_P5 = self.ROI_for_parts(head, fc5_H, fc5_S, self.P_boxes, 'ROI_for_parts')

        fc7_P = self.pasta_classification(fc5_P0, fc5_P1, fc5_P2, fc5_P3, fc5_P4, fc5_P5, is_training, initializer, 'pasta_classification')

        self.region_classification(fc7_P, is_training, initializer, 'region_classification')

        self.score_summaries.update(self.predictions)
        return

    def create_architecture(self, is_training):

        self.build_network(is_training)

        for var in tf.trainable_variables():
            self.train_summaries.append(var)

        self.add_loss()
        layers_to_output = {}
        layers_to_output.update(self.losses)

        val_summaries = []
        with tf.device("/cpu:0"):
            for key, var in self.event_summaries.items():
                val_summaries.append(tf.summary.scalar(key, var))

        val_summaries.append(tf.summary.scalar('lr', self.lr))
        self.summary_op     = tf.summary.merge_all()
        self.summary_op_val = tf.summary.merge(val_summaries)

        return layers_to_output

    def add_loss(self):

        with tf.variable_scope('LOSS') as scope:
            cls_score_P = self.predictions["cls_score_P"]

            label_verb   = self.gt_verb

            # P stream
            P_cross_entropy = tf.nn.weighted_cross_entropy_with_logits(targets=label_verb, logits=cls_score_P, pos_weight=self.verb_weight)
            P_cross_entropy = tf.reduce_mean(P_cross_entropy)

            self.losses['P_cross_entropy']  = P_cross_entropy

            loss = P_cross_entropy

            self.losses['total_loss'] = loss
            self.event_summaries.update(self.losses)

        return loss

    def add_score_summary(self, key, tensor):
        tf.summary.histogram('SCORE/' + tensor.op.name + '/' + key + '/scores', tensor)

    def add_train_summary(self, var):
        tf.summary.histogram('TRAIN/' + var.op.name, var)

    def train_step(self, sess, blobs, lr, train_op):
        feed_dict = {self.image: blobs['image'],
                     self.H_boxes: blobs['H_boxes'], self.P_boxes: blobs['P_boxes'],self.gt_verb: blobs['gt_verb'],
                     self.lr: lr, self.H_num: blobs['H_num']}

        loss, _ = sess.run([self.losses['total_loss'],
                            train_op],
                            feed_dict=feed_dict)
        return loss

    def train_step_with_summary(self, sess, blobs, lr, train_op):
        feed_dict = {self.image: blobs['image'],
                     self.H_boxes: blobs['H_boxes'], self.P_boxes: blobs['P_boxes'], self.gt_verb: blobs['gt_verb'],
                     self.lr: lr, self.H_num: blobs['H_num']}

        loss, summary, _ = sess.run([self.losses['total_loss'],
                                     self.summary_op,
                                     train_op],
                                     feed_dict=feed_dict)
        return loss, summary

    def test_image_HO(self, sess, image, blobs):
        feed_dict = {self.image: image,
                     self.H_boxes: blobs['H_boxes'], self.P_boxes: blobs['P_boxes'], self.H_num: blobs['H_num']}
        cls_prob_HO = sess.run([self.predictions["cls_prob_HO"]], feed_dict=feed_dict)
        return cls_prob_HO

max_stars_repo_name: JamesZhutheThird/Rb-PaStaNet

max_issues_repo_name: JamesZhutheThird/Rb-PaStaNet

max_forks_repo_name: JamesZhutheThird/Rb-PaStaNet


text:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.svm import SVC
import itertools
from sklearn.metrics import classification_report, confusion_matrix
import pickle
from src.definitions import RESULTS_DIR

class SVM():

    def __init__(self,dataset,features,predictor,classes,name,cm=False,vi=False,dump_regressor=False):
        self.data = dataset
        self.features = features
        self.predictor = predictor
        self.classes = classes
        self.name = name
        self.cm = cm
        self.vi = vi
        self.dump_regressor = dump_regressor
        self.classifier = SVC(kernel='rbf')

    # Train the Random Forest Classifier
    def train(self):
        train_features, test_features, train_labels, test_labels = self.split(self.data)
        self.classifier.fit(train_features, train_labels)
        return self.accuracy(test_features,test_labels)

    def test(self,features,labels):
        predictions = self.classifier.predict(features)
        probs = self.classifier.predict_proba(features)[:, 1]
        return predictions,probs

    def accuracy(self,predictions,labels):
        return self.classifier.score(predictions, labels)

    def predict(self,d):
        return self.classifier.predict(d)[0]

    def split(self,data):
        labels = np.array(data[self.predictor])
        labels = [int(label) for label in labels]
        features = data.drop(self.predictor, axis = 1)
        features = np.array(features)
        return train_test_split(features, labels, test_size = 0.2)

if __name__ == "__main__":
    svm = SVM(RESULTS_DIR + 'oxo_game.csv',["Step","Cell_0","Cell_1", "Cell_2","Cell_3","Cell_4","Cell_5","Cell_6","Cell_7","Cell_8","Agent"],["Move"])
    #svm = SVM(RESULTS_DIR + 'othello_game.csv',["Step","Cell_0_0","Cell_0_1","Cell_0_2","Cell_0_3","Cell_1_0","Cell_1_1","Cell_1_2","Cell_1_3","Cell_2_0","Cell_2_1","Cell_2_2","Cell_2_3","Cell_3_0","Cell_3_1","Cell_3_2","Cell_3_3","Agent"],["Move"])
    #svm = SVM(RESULTS_DIR + 'nim_game.csv',["Chips","Agent"],["Move"])
    svm.train()

ext: py

reason: import numpy


text:
from __future__ import annotations
from typing import List, Optional, Tuple
from sklearn.pipeline import Pipeline
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.compose import ColumnTransformer, make_column_selector
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from datetime import datetime
from src.config.logger_config import logger
from src.preprocessing.config import FEATURES_TO_DROP

import numpy as np
import pandas as pd

from src.config.config import VARIABLES

def feature_eng_pipeline() -> Pipeline:
    """
    Creates the pipeline to transform the data

    Returns
    -------
    pipeline : Pipeline
        Feature engineering pipeline.
    """
    # Categorical attributes pipeline
    cat_pipeline = Pipeline([
        ('encoder', OneHotEncoder())
    ])

    # Numerical attributes pipeline
    num_pipeline = Pipeline([
        ('scaler', StandardScaler())
    ])

    # Pipeline to transform columns
    prep_pipeline = ColumnTransformer([
        ('cat', 'passthrough', make_column_selector(dtype_include=object)),
        ('num', num_pipeline, make_column_selector(dtype_include=np.number))
    ])

    feature_pipeline = Pipeline([
        ('win_ratio', WinRatio()),
        ('draw_ratio', DrawRatio()),
        ('loss_ratio', LossRatio()),
    ])

    # Final pipeline
    pipeline = Pipeline([
        ('feature_pipeline', feature_pipeline),
        ('feature_selector', FeatureSelector(FEATURES_TO_DROP)),
        ('prep', prep_pipeline),
        ('feature_store', ToDataset())
    ])

    return pipeline

def fit_and_process_data(
    pipeline : Pipeline,
    train : Tuple[pd.DataFrame, np.ndarray],
    test : Tuple[pd.DataFrame, np.ndarray]
    ) -> Tuple[pd.DataFrame, pd.DataFrame, np.ndarray, np.ndarray]:
    """
    Fit the pipeline with the training data and uses the obtained insights
    to transform both the training and test data.

    Parameters
    ----------
    pipeline : Pipeline
        Preprocessing pipeline
    train : Tuple[pd.DataFrame, np.ndarray]
        Tuple containing the training data
    test : Tuple[pd.DataFrame, np.ndarray]
        Tuple containing the test data

    Returns
    -------
    Tuple[pd.DataFrame, pd.DataFrame, np.ndarray, np.ndarray]
        Tuple containing the transformed training and test data
    """
    # Unpack the training and test data
    X_train, y_train = train
    X_test, y_test = test

    # Fit the pipeline and transform the training data
    logger.info('Preprocessing training data')
    X_train = pipeline.fit_transform(X_train)
    # Transform the test data
    logger.info('Preprocessing test data')
    X_test = pipeline.transform(X_test)

    return X_train, X_test, y_train, y_test

class FeatureSelector(BaseEstimator, TransformerMixin):
    """
    Remove a list of features from the data

    Arguments
    ---------
    features : list of str
        Features' names

    Attributes
    ----------
    features : list of str
        Features' names
    """
    def __init__(self, features : List[str]):
        self.features = features

    def fit(self, X : pd.DataFrame, y : Optional[np.ndarray] = None) -> object:
        return self

    def transform(
        self,
        X : pd.DataFrame,
        y : Optional[np.ndarray] = None
        ) -> pd.DataFrame:
        return X.drop(self.features, axis=1)

class ToDataset(BaseEstimator, TransformerMixin):
    """
    Transform a matrix of values into a dataset.

    Arguments
    ---------
    columns : List of str
        List containing the desired dataset columns.

    Attributes
    ---------
    variables : List of str
        List containing the desired dataset columns.
    """
    def __init__(self, columns : List[str] = VARIABLES):
        self.variables = columns

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):
        # Convert to a dataframe
        variables = [var for var in VARIABLES if var != 'outcome']

        X = pd.DataFrame(X, columns=variables)
        # Adds a timestamp to define the datetime where the features were created
        now = datetime.now()
        X['created_on'] = [datetime(now.year, now.month, now.day)] * len(X)

        return X

class WinRatio(BaseEstimator, TransformerMixin):
    def __init__(self) -> None:
        pass

    def fit(self, X : pd.DataFrame, y : Optional[np.ndarray] = None) -> WinRatio:
        return self

    def transform(self, X : pd.DataFrame, y : Optional[np.ndarray] = None):
        X['home_win_ratio_t1'] = X['home_wins_t1'] / X['league_match']
        X['home_win_ratio_t2'] = X['home_wins_t2'] / X['league_match']

        X['away_win_ratio_t1'] = X['away_wins_t1'] / X['league_match']
        X['away_win_ratio_t2'] = X['away_wins_t2'] / X['league_match']

        return X

class DrawRatio(BaseEstimator, TransformerMixin):
    def __init__(self) -> None:
        pass

    def fit(self, X : pd.DataFrame, y : Optional[np.ndarray] = None) -> DrawRatio:
        return self

    def transform(self, X : pd.DataFrame, y : Optional[np.ndarray] = None):
        X['home_draw_ratio_t1'] = X['home_draws_t1'] / X['league_match']
        X['home_draw_ratio_t2'] = X['home_draws_t2'] / X['league_match']

        X['away_draw_ratio_t1'] = X['away_draws_t1'] / X['league_match']
        X['away_draw_ratio_t2'] = X['away_draws_t2'] / X['league_match']

        return X

class LossRatio(BaseEstimator, TransformerMixin):
    def __init__(self) -> None:
        pass

    def fit(self, X : pd.DataFrame, y : Optional[np.ndarray] = None):
        return self

    def transform(self, X : pd.DataFrame, y : Optional[np.ndarray] = None):
        X['home_loss_ratio_t1'] = X['home_losses_t1'] / X['league_match']
        X['home_loss_ratio_t2'] = X['home_losses_t2'] / X['league_match']

        X['away_loss_ratio_t1'] = X['away_losses_t1'] / X['league_match']
        X['away_loss_ratio_t2'] = X['away_losses_t2'] / X['league_match']

        return X

max_stars_repo_name: hectorLop/Spanish-LaLiga-Prediction

max_issues_repo_name: hectorLop/Spanish-LaLiga-Prediction

max_forks_repo_name: hectorLop/Spanish-LaLiga-Prediction


text:
import logging
import vispy.io
from .Canvas import Canvas
from ... import draw
import numpy as np
from ..Scene import DEFAULT_DIRECTIONAL_LIGHTS

logger = logging.getLogger(__name__)

def set_orthographic_projection(camera, left, right, bottom, top, near, far):
    camera[:] = 0
    camera[0, 0] = 2/(right - left)
    camera[3, 0] = -(right + left)/(right - left)
    camera[1, 1] = 2/(top - bottom)
    camera[3, 1] = -(top + bottom)/(top - bottom)
    camera[2, 2] = -2/(far - near)
    camera[3, 2] = -(far + near)/(far - near)
    camera[3, 3] = 1

class Scene(draw.Scene):
    __doc__ = (draw.Scene.__doc__ or '') + """
    This Scene supports the following features:

    * *pan*: If enabled, mouse movement will translate the scene instead of rotating it
    * *directional_light*: Add directional lights. The given value indicates the magnitude*direction normal vector.
    * *ambient_light*: Enable trivial ambient lighting. The given value indicates the magnitude of the light (in [0, 1]).
    * *translucency*: Enable order-independent transparency rendering
    * *fxaa*: Enable fast approximate anti-aliasing
    * *ssao*: Enable screen space ambient occlusion
    * *additive_rendering*: Enable additive rendering. This mode is good for visualizing densities projected through the viewing direction. Takes an optional 'invert' argument to invert the additive rendering (i.e., black-on-white instead of white-on-black).
    * *outlines*: Enable cartoony outlines. The given value indicates the width of the outlines (start small, perhaps 1e-5 to 1e-3).
    * *static*: Enable static rendering. When possible (when vispy is using a non-notebook backend), display a statically-rendered image of a scene instead of the live webGL version when `Scene.show()` is called.
    """

    def __init__(self, *args, canvas_kwargs={}, **kwargs):
        self.camera = np.eye(4, dtype=np.float32)
        self._zoom = 1
        self._pixel_scale = 1
        self._clip_scale = 1
        self._translation = [0, 0, 0]
        self._canvas = None
        super(Scene, self).__init__(*args, **kwargs)
        self._canvas = Canvas(self, **canvas_kwargs)

    @property
    def zoom(self):
        return self._zoom

    @zoom.setter
    def zoom(self, value):
        self._zoom = value
        self._update_camera()

    @property
    def pixel_scale(self):
        return self._pixel_scale

    @pixel_scale.setter
    def pixel_scale(self, value):
        self._pixel_scale = value
        if self._canvas is not None:
            self._canvas.size = self.size_pixels.astype(np.uint32)
        self._update_camera()

    @property
    def clip_scale(self):
        return self._clip_scale

    @clip_scale.setter
    def clip_scale(self, value):
        self._clip_scale = value
        self._update_camera()

    @property
    def size(self):
        return self._size

    @size.setter
    def size(self, value):
        self._size[:] = value
        self._update_camera()

    def add_primitive(self, primitive):
        super(Scene, self).add_primitive(primitive)
        self._update_camera()
        for feature in list(self.enabled_features):
            self.enable(feature, **self.get_feature_config(feature))

    def enable(self, name, auto_value=None, **parameters):
        """Enable an optional rendering feature.

        :param name: Name of the feature to enable
        :param auto_value: Shortcut for features with single-value configuration. If given as a positional argument, will be given the default configuration name 'value'.
        :param parameters: Keyword arguments specifying additional configuration options for the given feature
        """
        if auto_value is not None:
            parameters['value'] = auto_value

        if name == 'directional_light':
            lights = parameters.get('value', DEFAULT_DIRECTIONAL_LIGHTS)
            lights = np.atleast_2d(lights).astype(np.float32)
            for prim in self._primitives:
                prim.diffuseLight = lights

        elif name == 'ambient_light':
            light = parameters.get('value', .25)
            for prim in self._primitives:
                prim.ambientLight = light

        if self._canvas is not None:
            if name in self._canvas._VALID_FEATURES:
                self._canvas._enable_feature(**{name: parameters})

        super(Scene, self).enable(name, **parameters)

    def disable(self, name, strict=True):
        if self._canvas is not None:
            if name in self._canvas._VALID_FEATURES:
                self._canvas._disable_feature(name)

        super(Scene, self).disable(name, strict=strict)

    def _update_camera(self):
        (width, height) = self.size.astype(np.float32)

        dz = np.sqrt(np.sum(self.size**2))*self._clip_scale

        translation = self.translation
        translation[2] = -(2 + dz)/2
        self.translation = translation

        set_orthographic_projection(
            self.camera,
            -width/2, width/2,
            -height/2, height/2,
            1, 1 + dz)

        if self._canvas is not None:
            self._canvas.clip_planes = (1, 1 + dz)

        self.camera[[0, 1], [0, 1]] *= self._zoom

        for prim in self._primitives:
            prim.camera = self.camera

    def save(self, filename):
        """Render and save an image of this Scene.

        :param filename: target filename to save the image into
        """
        if self._canvas is not None:
            img = self._canvas.render()
            vispy.io.write_png(filename, img)

    def show(self):
        """Display this Scene object."""
        cfg = self.get_feature_config('static')

        if cfg and cfg.get('value', False):
            import imageio
            import io
            import IPython.display
            import vispy.app

            vispy_backend = vispy.app.use_app().backend_name
            if 'webgl' not in vispy_backend:
                target = io.BytesIO()
                img = self._canvas.render()
                imageio.imwrite(target, img, 'png')
                return IPython.display.Image(data=target.getvalue())

            msg = ('vispy has already loaded the {} backend, ignoring static'
                   ' feature. Try manually selecting a desktop vispy backend '
                   'before importing plato, for example:\n    import vispy.app; '
                   'vispy.app.use_app("pyglet")'.format(vispy_backend))
            logger.warning(msg)

        return self._canvas.show()

    def render(self):
        """Have vispy redraw this Scene object."""
        self._canvas.update()

max_stars_repo_name: hillarypan/plato

max_issues_repo_name: hillarypan/plato

max_forks_repo_name: hillarypan/plato


text:
import math

import numpy as np
from numba import njit

from deepdrive_zero.constants import CACHE_NUMBA, MAX_STEER_CHANGE_PER_SECOND, \
    MAX_ACCEL_CHANGE_PER_SECOND
from deepdrive_zero.physics.bike_model import bike_with_friction_step
from deepdrive_zero.logs import log

@njit(cache=CACHE_NUMBA, nogil=True)
def physics_step(accel, add_longitudinal_friction, add_rotational_friction,
                 brake, curr_acceleration, curr_angle, curr_angle_change,
                 curr_angular_velocity, curr_gforce, curr_max_gforce,
                 curr_speed, curr_velocity, curr_x, curr_y, dt, n, prev_accel,
                 prev_brake, prev_steer, steer, vehicle_model, ignore_brake,
                 constrain_controls, max_steer_change, max_accel_change,
                 max_brake_change,
                 wait_for_action):
    if wait_for_action:
        pass
        # Set new_action min(whats_left, max_action)
        # Do the step
        # Subtract new_action from whats_left
        # Repeat until whats_left == 0 for all actions
    else:
        return _step(accel, add_longitudinal_friction, add_rotational_friction,
                     brake, constrain_controls, curr_angle, curr_angle_change,
                     curr_angular_velocity, curr_gforce, curr_max_gforce,
                     curr_speed, curr_velocity, curr_x, curr_y, dt,
                     ignore_brake,
                     max_accel_change, max_steer_change, n, prev_accel,
                     prev_brake,
                     prev_steer, steer, vehicle_model)

@njit(cache=CACHE_NUMBA, nogil=True)
def _step(accel, add_longitudinal_friction, add_rotational_friction, brake,
          constrain_controls, curr_angle, curr_angle_change,
          curr_angular_velocity, curr_gforce, curr_max_gforce, curr_speed,
          curr_velocity, curr_x, curr_y, dt, ignore_brake, max_accel_change,
          max_steer_change, n, prev_accel, prev_brake, prev_steer, steer,
          vehicle_model):
    if ignore_brake:
        brake = 0
    if curr_speed > 100:
        accel = 0
    steer_change = steer - prev_steer
    accel_change = accel - prev_accel
    if constrain_controls:
        steer_change = min(max_steer_change, steer_change)
        steer_change = max(-max_steer_change, steer_change)
        accel_change = min(max_accel_change, accel_change)
        accel_change = max(-max_accel_change, accel_change)

        i_steer, i_accel, i_brake = 0, 0, 0
    for i in range(n):
        """
        Enforce real-world constraint that you can't teleport the gas pedal
        or steering wheel between positions, rather you must visit the
        intermediate positions between subsequent settings.
        """
        interp = (i + 1) / n
        i_steer = prev_steer + interp * steer_change
        i_accel = prev_accel + interp * accel_change
        if brake:
            i_brake = prev_brake + interp * (brake - prev_brake)
        else:
            i_brake = 0
        # log.info(f'steer {steer} accel {accel} brake {brake} vel {self.velocity}')
        # TODO: Add drag when simulating higher speeds
        prev_x, prev_y, prev_angle = curr_x, curr_y, curr_angle
        curr_x, curr_y, curr_angle, curr_angle_change, curr_speed = \
            bike_with_friction_step(
                steer=i_steer, accel=i_accel, brake=i_brake, dt=dt,
                x=curr_x, y=curr_y, angle=curr_angle,
                angle_change=curr_angle_change,
                speed=curr_speed,
                add_rotational_friction=add_rotational_friction,
                add_longitudinal_friction=add_longitudinal_friction,
                vehicle_model=vehicle_model, )

        gforce_outputs = get_gforce_levels(
            curr_x, curr_y, curr_angle, prev_x, prev_y, prev_angle, dt,
            curr_velocity, accel, curr_max_gforce)
        (curr_gforce, curr_max_gforce, new_jerk, new_acceleration,
         curr_angular_velocity, new_velocity) = gforce_outputs
    return (new_acceleration, curr_angle, curr_angle_change,
            curr_angular_velocity, curr_gforce, new_jerk,
            curr_max_gforce, curr_speed, curr_x, curr_y, i_accel, i_brake,
            i_steer, new_velocity)

@njit(cache=CACHE_NUMBA, nogil=True)
def get_gforce_levels(x, y, curr_angle, prev_x, prev_y, prev_angle, dt,
                      curr_velocity, curr_accel, curr_max_gforce):
    pos_change = np.array([prev_x - x, prev_y - y])
    prev_velocity = curr_velocity
    curr_velocity = pos_change / dt
    new_accel = (curr_velocity - prev_velocity) / dt
    angular_velocity = (curr_angle - prev_angle) / dt
    accel_magnitude = np.linalg.norm(new_accel)
    gforce = accel_magnitude / 9.807
    max_gforce = max(gforce, curr_max_gforce)
    # prev_gforce.append(self.gforce)
    jerk = (new_accel - curr_accel) / dt
    return gforce, max_gforce, jerk, new_accel, angular_velocity, curr_velocity

reason: import numpy,from numba


text:
import numpy as np
import pyroomacoustics as pra

room = pra.ShoeBox([4, 6], fs=16000, max_order=1)

# add sources in the room
room.add_source([2, 1.5])  # nice source
room.add_source([2, 4.5])  # interferer

# add a circular beamforming array
shape = pra.circular_2D_array([2.5, 3], 8, 0.0, 0.15)
bf = pra.Beamformer(shape, room.fs, Lg=500)
room.add_microphone_array(bf)

# run the ISM
room.image_source_model()

# the noise matrix, note that the size is the number of
# sensors multiplied by the filter size
Rn = np.eye(bf.M * bf.Lg) * 1e-5

def test_rake_max_udr_filters():
    # no interferer
    bf.rake_max_udr_filters(room.sources[0][:4], R_n=Rn, delay=0.015, epsilon=1e-2)
    # with interferer
    bf.rake_max_udr_filters(
        room.sources[0][:4],
        interferer=room.sources[1][:4],
        R_n=Rn,
        delay=0.015,
        epsilon=1e-2,
    )

def test_perceptual_filters():
    # no interferer
    bf.rake_perceptual_filters(room.sources[0][:4], R_n=Rn)
    # with interferer
    bf.rake_perceptual_filters(
        room.sources[0][:4], interferer=room.sources[1][:4], R_n=Rn
    )
    pass

if __name__ == "__main__":

    import matplotlib.pyplot as plt

    bf.rake_perceptual_filters(
        room.sources[0][:4], interferer=room.sources[1][:4], R_n=Rn, epsilon=0.1
    )
    bf.plot()

    room.plot(img_order=1, freq=[700.0, 1000.0, 2000.0])

    plt.show()

max_stars_repo_name: Womac/pyroomacoustics


text:
import numpy as np
from .basic_euclidean import eucl_dist

def discret_frechet(t0, t1):
    """
    Usage
    -----
    Compute the discret frechet distance between trajectories P and Q

    Parameters
    ----------
    param t0 : px2 numpy_array, Trajectory t0
    param t1 : qx2 numpy_array, Trajectory t1

    Returns
    -------
    frech : float, the discret frechet distance between trajectories t0 and t1
    """
    n0 = len(t0)
    n1 = len(t1)
    C = np.zeros((n0 + 1, n1 + 1))
    C[1:, 0] = float('inf')
    C[0, 1:] = float('inf')
    for i in np.arange(n0) + 1:
        for j in np.arange(n1) + 1:
            C[i, j] = max(eucl_dist(t0[i - 1], t1[j - 1]), min(C[i, j - 1], C[i - 1, j - 1], C[i - 1, j]))
    dtw = C[n0, n1]
    return dtw

max_stars_repo_name: yangshuxuan/traj-dist

max_issues_repo_name: yangshuxuan/traj-dist

max_forks_repo_name: yangshuxuan/traj-dist


text:
import os, sys
import numpy as np
import paddle
# paddle.enable_static()
import paddle.fluid as fluid
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from collections import OrderedDict
import torch

from pp_rec_srn_head import SRNHead as PPHead
from pt_rec_srn_head import SRNHead as PTHead

SEED = 666
input_shape = (1, 512, 8, 32)
in_channels = 512
out_channels = 38
max_text_length = 25
num_heads = 8
num_encoder_TUs = 2
num_decoder_TUs = 4
hidden_dims = 512

def paddle_func():
    np.random.seed(SEED)
    # x = np.load('input.npy', allow_pickle=True)
    x = np.random.rand(input_shape[0], input_shape[1], input_shape[2], input_shape[3]).astype(np.float32)
    print('pp input size: {}'.format(x.shape))
    # x1 = np.random.rand(1, 256, 1).astype(np.float32)
    x1 = np.load('encoder_word_pos_list.npy', allow_pickle=True)
    # x2 = np.random.rand(1, 25, 1).astype(np.int)
    x2 = np.load('gsrm_word_pos_list.npy', allow_pickle=True)
    # x3 = np.random.rand(1, 8, 25, 25).astype(np.float32)
    x3 = np.load('gsrm_slf_attn_bias1_list.npy', allow_pickle=True)
    # x4 = np.random.rand(1, 8, 25, 25).astype(np.float32)
    x4 = np.load('gsrm_slf_attn_bias2_list.npy', allow_pickle=True)

    # np.save('org.npy', x)
    with fluid.dygraph.guard():
        layer = PPHead(in_channels=in_channels,
                       out_channels=out_channels,
                       max_text_length=max_text_length,
                       num_heads=num_heads,
                       num_encoder_TUs=num_encoder_TUs,
                       num_decoder_TUs=num_decoder_TUs,
                       hidden_dims=hidden_dims)

        # sd = np.load('lstm.npy', allow_pickle=True).tolist()
        # lstm.set_state_dict(sd)

        state_dict = layer.state_dict()
        sd = OrderedDict()
        for key, value in state_dict.items():
            v = value.numpy()
            # print(key, value.shape, np.sum(v), np.mean(v), np.max(v), np.min(v))
            sd[key] = v

        np.save('srnhead.npy', sd)

        inputs = fluid.dygraph.to_variable(x)
        x1 = fluid.dygraph.to_variable(x1)
        x2 = fluid.dygraph.to_variable(x2)
        x3 = fluid.dygraph.to_variable(x3)
        x4 = fluid.dygraph.to_variable(x4)
        out = layer(inputs, [x1,x2,x3,x4])

    predict = out['predict']
    pvam_feature = out['pvam_feature']
    decoded_out = out['decoded_out']
    word_out = out['word_out']
    gsrm_out = out['gsrm_out']
    return predict.numpy(), pvam_feature.numpy(), decoded_out.numpy(), word_out.numpy(), gsrm_out.numpy()
    # return alpha.numpy()

def torch_func():
    np.random.seed(SEED)
    # x = np.load('input.npy', allow_pickle=True)
    x = np.random.rand(input_shape[0], input_shape[1], input_shape[2], input_shape[3]).astype(np.float32)
    print('pt input size: {}'.format(x.shape))
    # x1 = np.random.rand(1, 256, 1).astype(np.float32)
    x1 = np.load('encoder_word_pos_list.npy', allow_pickle=True)
    # x2 = np.random.rand(1, 25, 1).astype(np.int)
    x2 = np.load('gsrm_word_pos_list.npy', allow_pickle=True)
    # x3 = np.random.rand(1, 8, 25, 25).astype(np.float32)
    x3 = np.load('gsrm_slf_attn_bias1_list.npy', allow_pickle=True)
    # x4 = np.random.rand(1, 8, 25, 25).astype(np.float32)
    x4 = np.load('gsrm_slf_attn_bias2_list.npy', allow_pickle=True)

    inputs = torch.Tensor(x)
    x1 = torch.Tensor(x1)
    x2 = torch.Tensor(x2)
    x3 = torch.Tensor(x3)
    x4 = torch.Tensor(x4)
    layer = PTHead(in_channels=in_channels,
                       out_channels=out_channels,
                       max_text_length=max_text_length,
                       num_heads=num_heads,
                       num_encoder_TUs=num_encoder_TUs,
                       num_decoder_TUs=num_decoder_TUs,
                       hidden_dims=hidden_dims)

    sd = np.load('srnhead.npy', allow_pickle=True)
    sd = sd.tolist()

    for key, value in layer.state_dict().items():

        name = key
        keyword = 'block_list.'
        if keyword in name:
            # replace: block_list.
            name = name.replace(keyword, '')
        else:
            name = name

        # for srn
        keyword = 'base_block.'
        if keyword in name:
            # replace: base_block.
            name = name.replace(keyword, '')
        keyword = 'base_block_2.0.'
        if keyword in name:
            # replace: base_block_2.0. -> base_block_2.
            name = name.replace(keyword, 'base_block_2.')
        # for srn head
        keyword = 'encoder_layers.'
        if keyword in name:
            # replace: encoder_layers.
            name = name.replace(keyword, '')
        keyword = 'functors.'
        if keyword in name:
            # replace: functors.
            name = name.replace(keyword, '')

        if name.endswith('num_batches_tracked'):
            continue

        if name.endswith('running_mean'):
            ppname = name.replace('running_mean', '_mean')
        elif name.endswith('running_var'):
            ppname = name.replace('running_var', '_variance')
        elif name.endswith('bias') or name.endswith('weight'):
            ppname = name
        elif 'lstm' in name:
            ppname = name
        elif 'attention_cell' in name:
            ppname = name

        else:
            print('Redundance:')
            print(name)
            raise ValueError

        try:
            if key.endswith('.weight'):
                if len(sd[ppname].shape) == len(layer.state_dict()[key].shape) == 2 \
                    and sd[ppname].shape[0] == layer.state_dict()[key].shape[1] \
                    and sd[ppname].shape[1] == layer.state_dict()[key].shape[0]:

                    layer.state_dict()[key].copy_(torch.Tensor(sd[ppname].T))
                else:
                    layer.state_dict()[key].copy_(torch.Tensor(sd[ppname]))
            else:
                layer.state_dict()[key].copy_(torch.Tensor(sd[ppname]))
        except Exception as e:
            print('except: pt: ', key)
            print('except: pp: ', ppname)
            print('except: pt: ', layer.state_dict()[key].shape)
            print('except: pp: ', sd[ppname].shape)
            raise e

    out = layer(inputs, [x1,x2,x3,x4])
    predict = out['predict']
    pvam_feature = out['pvam_feature']
    decoded_out = out['decoded_out']
    word_out = out['word_out']
    gsrm_out = out['gsrm_out']
    return predict.data.numpy(), pvam_feature.data.numpy(), decoded_out.data.numpy(), word_out.data.numpy(), gsrm_out.data.numpy()
    # return alpha.data.numpy()

def print_cmp(inp, name=None):
    print('{}: shape-{}, sum: {}, mean: {}, max: {}, min: {}'.format(name, inp.shape,
                                                                     np.sum(inp), np.mean(inp),
                                                                     np.max(inp), np.min(inp)))

if __name__ == '__main__':
    print('==========paddle=================')
    predict, pvam_feature, decoded_out, word_out, gsrm_out = paddle_func()
    print_cmp(predict, name='predict')
    print('===========pytorch================')
    predict, pvam_feature, decoded_out, word_out, gsrm_out = torch_func()
    print_cmp(predict, name='predict')


text:
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Alphas import *
from QuantConnect.Algorithm.Framework.Execution import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from QuantConnect.Algorithm.Framework.Risk import *
from QuantConnect.Algorithm.Framework.Selection import *
from datetime import timedelta
import numpy as np
import pandas as pd
import ptvsd; ptvsd.enable_attach(); ptvsd.wait_for_attach();

### <summary>
### CapmAlphaRankingFrameworkAlgorithm: example of custom scheduled universe selection model
### Universe Selection inspired by https://www.quantconnect.com/tutorials/strategy-library/capm-alpha-ranking-strategy-on-dow-30-companies
### </summary>
class CapmAlphaRankingFrameworkAlgorithm(QCAlgorithm):
    '''CapmAlphaRankingFrameworkAlgorithm: example of custom scheduled universe selection model'''

    def Initialize(self):
        ''' Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        # Set requested data resolution
        self.UniverseSettings.Resolution = Resolution.Minute

        self.SetStartDate(2016, 1, 1)   #Set Start Date
        self.SetEndDate(2017, 1, 1)     #Set End Date
        self.SetCash(100000)            #Set Strategy Cash

        # set algorithm framework models
        self.SetUniverseSelection(CapmAlphaRankingUniverseSelectionModel())
        self.SetAlpha(ConstantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(1), 0.025, None))
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())
        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.01))

from QuantConnect.Data.UniverseSelection import ScheduledUniverse
from Selection.UniverseSelectionModel import UniverseSelectionModel

class CapmAlphaRankingUniverseSelectionModel(UniverseSelectionModel):
    '''This universe selection model picks stocks with the highest alpha: interception of the linear regression against a benchmark.'''

    period = 21;
    benchmark = "SPY"

    # Symbols of Dow 30 companies.
    symbols = [Symbol.Create(x, SecurityType.Equity, Market.USA)
               for x in ["AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DD", "DIS", "GE", "GS",
                         "HD", "IBM", "INTC", "JPM", "KO", "MCD", "MMM", "MRK", "MSFT",
                         "NKE","PFE", "PG", "TRV", "UNH", "UTX", "V", "VZ", "WMT", "XOM"]]

    def CreateUniverses(self, algorithm):

        # Adds the benchmark to the user defined universe
        benchmark = algorithm.AddEquity(self.benchmark, Resolution.Daily)

        # Defines a schedule universe that fires after market open when the month starts
        return [ ScheduledUniverse(
            benchmark.Exchange.TimeZone,
            algorithm.DateRules.MonthStart(self.benchmark),
            algorithm.TimeRules.AfterMarketOpen(self.benchmark),
            lambda datetime: self.SelectPair(algorithm, datetime),
            algorithm.UniverseSettings,
            algorithm.SecurityInitializer)]

    def SelectPair(self, algorithm, date):
        '''Selects the pair (two stocks) with the highest alpha'''
        dictionary = dict()
        ptvsd.break_into_debugger()
        benchmark = self._getReturns(algorithm, self.benchmark)
        ones = np.ones(len(benchmark))

        for symbol in self.symbols:
            prices = self._getReturns(algorithm, symbol)
            if prices is None: continue
            A = np.vstack([prices, ones]).T

            # Calculate the Least-Square fitting to the returns of a given symbol and the benchmark
            ols = np.linalg.lstsq(A, benchmark)[0]
            dictionary[symbol] = ols[1]

        # Returns the top 2 highest alphas
        orderedDictionary = sorted(dictionary.items(), key= lambda x: x[1], reverse=True)
        return [x[0] for x in orderedDictionary[:2]]

    def _getReturns(self, algorithm, symbol):

        history = algorithm.History([symbol], self.period, Resolution.Daily)
        if history.empty: return None

        window = RollingWindow[float](self.period)
        rateOfChange = RateOfChange(1)

        def roc_updated(s, item):
            window.Add(item.Value)

        rateOfChange.Updated += roc_updated

        history = history.close.reset_index(level=0, drop=True).iteritems()

        for time, value in history:
            rateOfChange.Update(time, value);

        return [ x for x in window]

max_stars_repo_name: BaoBaoJiu/Lean

max_issues_repo_name: BaoBaoJiu/Lean

max_forks_repo_name: BaoBaoJiu/Lean


text:
import numpy as np
import yaml

class ColorScale():
    def __init__(self, colorscale_filename=None):
        with open(colorscale_filename) as file:
            cfg = yaml.load(file, Loader=yaml.FullLoader)

        self.bypass = not cfg['type'].lower() in ("linear_ends", "custom")
        self.colorscale = {0: [0,0,0], 1: [255,255,255]}
        self.unknown_color = [120, 120, 120]
        self.bounds = (0, 1)

        # Communicate out if we're bypassing
        try:
            import rospy
            rospy.set_param("color_bypass", self.bypass)
        except:
            pass

        # Parse the colorscale
        if not self.bypass:
            self.unknown_color = np.fromstring(cfg['unknown'], dtype=np.uint8, sep=',')
            colorlist = [np.fromstring(values, dtype=np.uint8, sep=',') for values in cfg['values']]

            if cfg['type'].lower() == "linear_ends":
                indices = np.linspace(cfg['stops'][0], cfg['stops'][1], num=len(colorlist), endpoint=True)
                self.colorscale = dict(zip(indices, colorlist))
                self.bounds = (np.min(indices), np.max(indices))

            elif cfg['type'].lower() == "custom":
                indices = np.array(cfg['stops'])
                # There need to be enough indices for the colors provided!
                assert(len(indices) == len(colorlist))
                self.colorscale = dict(zip(indices, colorlist))
                self.bounds = (np.min(indices), np.max(indices))

            # check for absolute ends (optional)
            try:
                ends = cfg['ends']
                ends = {0: np.fromstring(ends['zero'], dtype=np.uint8, sep=','),
                        1: np.fromstring(ends['one'],  dtype=np.uint8, sep=','),}
                self.colorscale.update(ends)
                self.bounds = (0, 1)
            except:
                pass

    def mapToColor(self, property):
        # return unknown friction if we don't know it
        if property is None or property < self.bounds[0] or property > self.bounds[1]:
            return self.unknown_color[0], self.unknown_color[1], self.unknown_color[2]
        # set arbitrary which will change.
        lower_color = (-1, np.array([0,0,0]))
        upper_color = (1, np.array([255,255,255]))
        # get the stops
        for stop, color in self.colorscale.items():
            if stop >= lower_color[0] and stop < property:
                lower_color = (stop, np.array(color))
            elif stop <= upper_color[0] and stop >= property:
                upper_color = (stop, np.array(color))
        # compute the linear interpolation
        xp = np.array([lower_color[0], upper_color[0]])
        fp = np.column_stack((lower_color[1], upper_color[1])).astype(float)/255.0
        return np.interp(property, xp, fp[0,:]), np.interp(property, xp, fp[1,:]), np.interp(property, xp, fp[2,:])


text:
### Import modules
import cv2,PIL
import numpy as np
import torch
import matplotlib.pyplot as plt
import torchvision.transforms as transforms

from .utils import utils
from .base import BaseClass

class DepthEst(BaseClass):

    def __init__(self, name='Depth Estimator', device="cpu"):
        super().__init__(name)

        #Init name and metadata
        self.name = name
        self.device = device.lower()

        #Create net
        self.predictor = torch.hub.load("intel-isl/MiDaS", "MiDaS")
        self.predictor.to(self.device).eval()

        #Create transforms
        self.transform_ = torch.hub.load("intel-isl/MiDaS", "transforms")
        self.transform = self.transform_.default_transform

    def predict(self,image):
        """
        Does depth estimation on a single image. In order to perform batch classification,
        you can either call this predict() function in a for-loop or alternatively (advanced)
        try to modify this predict() function to perform batch-inferencing.

        Input:
            image: cv2 type object

        Output:
            predictions: torch.tensor object
        """

        #Cast PIL image to cv2 format if needed
        # if type(image) != np.ndarray:
        #     image = utils.pil_to_cv2(image)

        #Transform / preprocess as required by trained model
        image_ = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = self.transform(image_).to(self.device)

        #Inference
        with torch.no_grad():
            output_ = self.predictor(image)

            #Post process
            output = torch.nn.functional.interpolate(
                output_.unsqueeze(1),
                size=image_.shape[:2],
                mode="bicubic",
                align_corners=False,
            ).squeeze()
            output = output.cpu().numpy()

        return output

    def visualize(self,img,output,figsize=(10,10)):
        """
        Simple visualizing function for depth estimator output.

        Input:
            img: cv2 type object. Original raw image.
            outputs: torch.tensor object returned by the predict() function

        Output:
            None
        """

        #Plot image
        fig,ax = plt.subplots(2,figsize = figsize)

        ax[0].imshow(img[:,:,::-1], interpolation='none')
        ax[0].set_title('Original Image')

        ax[1].imshow(output, interpolation='none')
        ax[1].set_title('Depth Map')

        plt.show()

    def save_image(self,image,output_path,file_name="my_pic",file_fmt="jpg"):
        """
        Helper function to save image to disk as jpg file.
        """
        name = output_path + "/" + file_name + "." + file_fmt
        return cv2.imwrite(name, image)


text:
"""
Pytorch implementation of SegNet (https://arxiv.org/pdf/1511.00561.pdf)
"""

from __future__ import print_function
from collections import OrderedDict
import torch
import torch.nn as nn
import torchvision.models as models
import pprint
from torch.distributions.dirichlet import Dirichlet
import torch.distributions as dist
import numpy as np

F = nn.functional
DEBUG = False

vgg16_dims = [
                    (64, 64, 'M'),                                # Stage - 1
                    (128, 128, 'M'),                              # Stage - 2
                    (256, 256, 256,'M'),                          # Stage - 3
                    (512, 512, 512, 'M'),                         # Stage - 4
                    (512, 512, 512, 'M')                          # Stage - 5
            ]

decoder_dims = [
                    ('U', 512, 512, 512),                         # Stage - 5
                    ('U', 512, 512, 512),                         # Stage - 4
                    ('U', 256, 256, 256),                         # Stage - 3
                    ('U', 128, 128),                              # Stage - 2
                    ('U', 64, 64)                                 # Stage - 1
                ]

class SegNet(nn.Module):
    def __init__(self, output_channels):
        super(SegNet, self).__init__()

        input_channels = 3
        self.input_channels = input_channels
        self.output_channels = output_channels
        self.num_channels = input_channels

        # Encoder layers

        self.encoder_conv_00 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=self.input_channels,
                                                          out_channels=64,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(64)
                                                ])
        self.encoder_conv_01 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(64)
                                                ])
        self.encoder_conv_10 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=128,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(128)
                                                ])
        self.encoder_conv_11 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=128,
                                                          out_channels=128,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(128)
                                                ])
        self.encoder_conv_20 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=128,
                                                          out_channels=256,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(256)
                                                ])
        self.encoder_conv_21 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=256,
                                                          out_channels=256,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(256)
                                                ])
        self.encoder_conv_22 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=256,
                                                          out_channels=256,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(256)
                                                ])
        self.encoder_conv_30 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=256,
                                                          out_channels=512,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(512)
                                                ])
        self.encoder_conv_31 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=512,
                                                          out_channels=512,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(512)
                                                ])
        self.encoder_conv_32 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=512,
                                                          out_channels=512,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(512)
                                                ])
        self.encoder_conv_40 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=512,
                                                          out_channels=512,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(512)
                                                ])
        self.encoder_conv_41 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=512,
                                                          out_channels=512,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(512)
                                                ])
        self.encoder_conv_42 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=512,
                                                          out_channels=512,
                                                          kernel_size=3,
                                                          padding=1),
                                                nn.BatchNorm2d(512)
                                                ])

        # Decoder layers

        self.decoder_convtr_42 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=512,
                                                                   out_channels=512,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(512)
                                               ])
        self.decoder_convtr_41 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=512,
                                                                   out_channels=512,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(512)
                                               ])
        self.decoder_convtr_40 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=512,
                                                                   out_channels=512,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(512)
                                               ])
        self.decoder_convtr_32 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=512,
                                                                   out_channels=512,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(512)
                                               ])
        self.decoder_convtr_31 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=512,
                                                                   out_channels=512,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(512)
                                               ])
        self.decoder_convtr_30 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=512,
                                                                   out_channels=256,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(256)
                                               ])
        self.decoder_convtr_22 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=256,
                                                                   out_channels=256,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(256)
                                               ])
        self.decoder_convtr_21 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=256,
                                                                   out_channels=256,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(256)
                                               ])
        self.decoder_convtr_20 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=256,
                                                                   out_channels=128,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(128)
                                               ])
        self.decoder_convtr_11 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=128,
                                                                   out_channels=128,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(128)
                                               ])
        self.decoder_convtr_10 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=128,
                                                                   out_channels=64,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(64)
                                               ])
        self.decoder_convtr_01 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=3,
                                                                   padding=1),
                                                nn.BatchNorm2d(64)
                                               ])
        self.decoder_convtr_00 = nn.Sequential(*[
                                                nn.ConvTranspose2d(in_channels=64,
                                                                   out_channels=self.output_channels,
                                                                   kernel_size=3,
                                                                   padding=1)
                                               ])

        self._initialize_weights()

    def forward(self, input_img):
        """
        Forward pass `input_img` through the network
        """

        # Encoder

        # Encoder Stage - 1
        dim_0 = input_img.size()
        x_00 = F.relu(self.encoder_conv_00(input_img))
        x_01 = F.relu(self.encoder_conv_01(x_00))
        x_0, indices_0 = F.max_pool2d(x_01, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 2
        dim_1 = x_0.size()
        x_10 = F.relu(self.encoder_conv_10(x_0))
        x_11 = F.relu(self.encoder_conv_11(x_10))
        x_1, indices_1 = F.max_pool2d(x_11, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 3
        dim_2 = x_1.size()
        x_20 = F.relu(self.encoder_conv_20(x_1))
        x_21 = F.relu(self.encoder_conv_21(x_20))
        x_22 = F.relu(self.encoder_conv_22(x_21))
        x_2, indices_2 = F.max_pool2d(x_22, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 4
        dim_3 = x_2.size()
        x_30 = F.relu(self.encoder_conv_30(x_2))
        x_31 = F.relu(self.encoder_conv_31(x_30))
        x_32 = F.relu(self.encoder_conv_32(x_31))
        x_3, indices_3 = F.max_pool2d(x_32, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 5
        dim_4 = x_3.size()
        x_40 = F.relu(self.encoder_conv_40(x_3))
        x_41 = F.relu(self.encoder_conv_41(x_40))
        x_42 = F.relu(self.encoder_conv_42(x_41))
        x_4, indices_4 = F.max_pool2d(x_42, kernel_size=2, stride=2, return_indices=True)

        # Decoder

        dim_d = x_4.size()

        # Decoder Stage - 5
        x_4d = F.max_unpool2d(x_4, indices_4, kernel_size=2, stride=2, output_size=dim_4)
        x_42d = F.relu(self.decoder_convtr_42(x_4d))
        x_41d = F.relu(self.decoder_convtr_41(x_42d))
        x_40d = F.relu(self.decoder_convtr_40(x_41d))
        dim_4d = x_40d.size()

        # Decoder Stage - 4
        x_3d = F.max_unpool2d(x_40d, indices_3, kernel_size=2, stride=2, output_size=dim_3)
        x_32d = F.relu(self.decoder_convtr_32(x_3d))
        x_31d = F.relu(self.decoder_convtr_31(x_32d))
        x_30d = F.relu(self.decoder_convtr_30(x_31d))
        dim_3d = x_30d.size()

        # Decoder Stage - 3
        x_2d = F.max_unpool2d(x_30d, indices_2, kernel_size=2, stride=2, output_size=dim_2)
        x_22d = F.relu(self.decoder_convtr_22(x_2d))
        x_21d = F.relu(self.decoder_convtr_21(x_22d))
        x_20d = F.relu(self.decoder_convtr_20(x_21d))
        dim_2d = x_20d.size()

        # Decoder Stage - 2
        x_1d = F.max_unpool2d(x_20d, indices_1, kernel_size=2, stride=2, output_size=dim_1)
        x_11d = F.relu(self.decoder_convtr_11(x_1d))
        x_10d = F.relu(self.decoder_convtr_10(x_11d))
        dim_1d = x_10d.size()

        # Decoder Stage - 1
        x_0d = F.max_unpool2d(x_10d, indices_0, kernel_size=2, stride=2, output_size=dim_0)
        x_01d = F.relu(self.decoder_convtr_01(x_0d))
        x_00d = self.decoder_convtr_00(x_01d)
        dim_0d = x_00d.size()

        x_softmax = F.softmax(x_00d, dim=1)

        if DEBUG:
            print("dim_0: {}".format(dim_0))
            print("dim_1: {}".format(dim_1))
            print("dim_2: {}".format(dim_2))
            print("dim_3: {}".format(dim_3))
            print("dim_4: {}".format(dim_4))

            print("dim_d: {}".format(dim_d))
            print("dim_4d: {}".format(dim_4d))
            print("dim_3d: {}".format(dim_3d))
            print("dim_2d: {}".format(dim_2d))
            print("dim_1d: {}".format(dim_1d))
            print("dim_0d: {}".format(dim_0d))

        #return x_00d
        return x_softmax

    def _initialize_weights(self):
        print("initialize weights")
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)

class SegNetBasic_MC_Dropout(nn.Module):
    def __init__(self, output_channels):
        super(SegNetBasic_MC_Dropout, self).__init__()

        input_channels = 3
        self.input_channels = input_channels
        self.output_channels = output_channels
        self.num_channels = input_channels

        # Encoder layers

        self.encoder_conv_1 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=self.input_channels,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])

        self.encoder_conv_2 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])
        self.encoder_conv_3 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])
        self.encoder_conv_4 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])

        self.upsample = nn.Upsample(scale_factor=2, mode='bilinear')
        # Decoder layers

        self.decoder_conv_4 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=(6,7),
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.decoder_conv_3 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=7,
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.decoder_conv_2 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=7,
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.decoder_conv_1 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=7,
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.classifier = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=output_channels,
                                                                   kernel_size=1,
                                                                   stride=1,
                                                                   padding=0),
                                               ])
        #self._initialize_weights()

    def _initialize_weights(self):
        print("initialize weights")
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)

    def forward(self, input_img):
        """
        Forward pass `input_img` through the network
        """

        # Encoder
        # Encoder Stage - 1
        dim_1 = input_img.size()
        x_1 = F.relu(self.encoder_conv_1(input_img))
        x_1 = F.dropout(x_1, 0.5)
        x_1, indices_1 = F.max_pool2d(x_1, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 2
        dim_2 = x_1.size()
        x_2 = F.relu(self.encoder_conv_2(x_1))
        x_2 = F.dropout(x_2, 0.5)
        x_2, indices_2= F.max_pool2d(x_2, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 3
        dim_3 = x_2.size()
        x_3 = F.relu(self.encoder_conv_3(x_2))
        x_3 = F.dropout(x_3, 0.5)
        x_3, indices_3= F.max_pool2d(x_3, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 4
        dim_4 = x_3.size()
        x_4 = F.relu(self.encoder_conv_4(x_3))
        x_4 = F.dropout(x_4, 0.5)
        x_4, indices_4= F.max_pool2d(x_4, kernel_size=2, stride=2, return_indices=True)

        # Decoder
        dim_4d = x_4.size()
        x_4d = self.upsample(x_4)
        x_4d = self.decoder_conv_4(x_4d)
        x_4d = F.dropout(x_4d, 0.5)

        dim_3d = x_4d.size()
        x_3d = self.upsample(x_4d)
        x_3d = self.decoder_conv_3(x_3d)
        x_3d = F.dropout(x_3d, 0.5)

        dim_2d = x_3d.size()
        x_2d = self.upsample(x_3d)
        x_2d = self.decoder_conv_2(x_2d)
        x_2d = F.dropout(x_2d, 0.5)

        dim_1d = x_2d.size()
        x_1d = self.upsample(x_2d)
        x_1d = self.decoder_conv_1(x_1d)
        x_1d = F.dropout(x_1d, 0.5)

        dim_0d = x_1d.size()
        x = self.classifier(x_1d)
        x_softmax = F.softmax(x, dim=1)
        final_dim = x_softmax.size()

        if DEBUG:
            print("dim_1: {}".format(dim_1))
            print("dim_2: {}".format(dim_2))
            print("dim_3: {}".format(dim_3))
            print("dim_4: {}".format(dim_4))
            print("dim_4d: {}".format(dim_4d))
            print("dim_3d: {}".format(dim_3d))
            print("dim_2d: {}".format(dim_2d))
            print("dim_1d: {}".format(dim_1d))
            print("dim_0d: {}".format(dim_0d))
            print("final_dim: {}".format(final_dim))

        #return x
        return x_softmax

class SegNetBasic(nn.Module):
    def __init__(self, output_channels):
        super(SegNetBasic, self).__init__()

        input_channels = 3
        self.input_channels = input_channels
        self.output_channels = output_channels
        self.num_channels = input_channels

        # Encoder layers

        self.encoder_conv_1 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=self.input_channels,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])

        self.encoder_conv_2 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])
        self.encoder_conv_3 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])
        self.encoder_conv_4 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                          out_channels=64,
                                                          kernel_size=7,
                                                          stride=1,
                                                          padding=3),
                                                nn.BatchNorm2d(64)
                                                ])

        self.upsample = nn.Upsample(scale_factor=2, mode='bilinear')
        # Decoder layers

        self.decoder_conv_4 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=(6,7),
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.decoder_conv_3 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=7,
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.decoder_conv_2 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=7,
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.decoder_conv_1 = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=64,
                                                                   kernel_size=7,
                                                                   stride=1,
                                                                   padding=3),
                                                nn.BatchNorm2d(64)
                                               ])

        self.classifier = nn.Sequential(*[
                                                nn.Conv2d(in_channels=64,
                                                                   out_channels=output_channels,
                                                                   kernel_size=1,
                                                                   stride=1,
                                                                   padding=0),
                                               ])
        #self._initialize_weights()

    def _initialize_weights(self):
        print("initialize weights")
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)

    def forward(self, input_img):
        """
        Forward pass `input_img` through the network
        """

        # Encoder
        # Encoder Stage - 1
        dim_1 = input_img.size()
        x_1 = F.relu(self.encoder_conv_1(input_img))
        x_1, indices_1 = F.max_pool2d(x_1, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 2
        dim_2 = x_1.size()
        x_2 = F.relu(self.encoder_conv_2(x_1))
        x_2, indices_2= F.max_pool2d(x_2, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 3
        dim_3 = x_2.size()
        x_3 = F.relu(self.encoder_conv_3(x_2))
        x_3, indices_3= F.max_pool2d(x_3, kernel_size=2, stride=2, return_indices=True)

        # Encoder Stage - 4
        dim_4 = x_3.size()
        x_4 = F.relu(self.encoder_conv_4(x_3))
        x_4, indices_4= F.max_pool2d(x_4, kernel_size=2, stride=2, return_indices=True)

        # Decoder
        dim_4d = x_4.size()
        x_4d = self.upsample(x_4)
        x_4d = self.decoder_conv_4(x_4d)

        dim_3d = x_4d.size()
        x_3d = self.upsample(x_4d)
        x_3d = self.decoder_conv_3(x_3d)

        dim_2d = x_3d.size()
        x_2d = self.upsample(x_3d)
        x_2d = self.decoder_conv_2(x_2d)

        dim_1d = x_2d.size()
        x_1d = self.upsample(x_2d)
        x_1d = self.decoder_conv_1(x_1d)

        dim_0d = x_1d.size()
        x = self.classifier(x_1d)
        x_softmax = F.softmax(x, dim=1)
        final_dim = x_softmax.size()

        if DEBUG:
            print("dim_1: {}".format(dim_1))
            print("dim_2: {}".format(dim_2))
            print("dim_3: {}".format(dim_3))
            print("dim_4: {}".format(dim_4))
            print("dim_4d: {}".format(dim_4d))
            print("dim_3d: {}".format(dim_3d))
            print("dim_2d: {}".format(dim_2d))
            print("dim_1d: {}".format(dim_1d))
            print("dim_0d: {}".format(dim_0d))
            print("final_dim: {}".format(final_dim))

        return x
        #return x_softmax

    def compute_entropy(self, inputs, labels):
        with torch.no_grad():
            batch_size = inputs.shape[0]
            # Forward propagation
            outputs = self.forward(inputs)
            outputs = outputs.permute(0, 2, 3, 1)  # b x 360 x 480 x 12
            outputs = outputs.reshape(-1, 12)
            outputs = torch.exp(outputs)
            labels = 100*nn.functional.one_hot(labels)
            labels = labels + torch.ones(labels.shape)  # b x 360 x 480 x 12
            labels = labels.reshape(-1, 12) # (b x 360 x 480) x 12
            dirichlet1 = Dirichlet(outputs)
            dirichlet2 = Dirichlet(labels)

            labels = labels.reshape(batch_size, 360, 480, 12)
            labels = torch.argmax(labels, dim=3)

            outputs = outputs.reshape(batch_size, 360, 480, 12)
            outputs = outputs.permute(0, 3, 1, 2)

            #print(torch.argmax(outputs, dim=1))
            #print(torch.argmax(outputs_origin, dim=3))

            entropy_outputs = dirichlet1.entropy()
            entropy_outputs = entropy_outputs.reshape(batch_size, 360, 480)
            entropy_labels = dirichlet2.entropy()
            entropy_labels = entropy_labels.reshape(batch_size, 360, 480)

        return entropy_outputs, entropy_labels