text:
# tf_unet is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# tf_unet is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with tf_unet.  If not, see <http://www.gnu.org/licenses/>.

'''
Created on Jul 28, 2016

author: jakeret
'''
from __future__ import print_function, division, absolute_import, unicode_literals

import os
import shutil
import numpy as np
from collections import OrderedDict
import logging

import tensorflow as tf

from tf_unet import util
from tf_unet.layers import (weight_variable, weight_variable_devonc, bias_variable,
                            conv2d, deconv2d, max_pool, crop_and_concat, pixel_wise_softmax_2,
                            cross_entropy)

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')

# this is a simpler version of Tensorflow's 'official' version. See:
# https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/layers.py#L102
def get_img_list(data_path):
    l = glob.glob(os.path.join(data_path, '*'))
    l = [f for f in l if re.search("^\d+.mat$", os.path.basename(f))]
    train_list = []
    for f in l:
        if os.path.exists(f):
            if os.path.exists(f[:-4] + '_2.mat'):
                train_list.append([f, f[:-4] + '_2.mat', 2])
            if os.path.exists(f[:-4] + '_3.mat'):
                train_list.append([f, f[:-4] + '_3.mat', 3])
            if os.path.exists(f[:-4] + '_4.mat'):
                train_list.append([f, f[:-4] + '_4.mat', 4])
    return train_list

def get_image_batch(train_list, offset, batch_size):
    target_list = train_list[offset:offset + batch_size]
    input_list = []
    gt_list = []
    cbcr_list = []
    for pair in target_list:
        input_img = scipy.io.loadmat(pair[1])['img_2']
        gt_img = scipy.io.loadmat(pair[0])['img_raw']
        input_list.append(input_img)
        gt_list.append(gt_img)
    input_list = np.array(input_list)
    input_list.resize([batch_size, input_list[0].shape[0],input_list[0].shape[1], 1])
    gt_list = np.array(gt_list)
    gt_list.resize([batch_size, gt_list[0].shape[0],gt_list[0].shape[1], 1])
    return (input_list, gt_list, np.array(cbcr_list))

def get_test_image(test_list, offset, batch_size):
    target_list = train_list[offset:offset + batch_size]
    input_list = []
    gt_list = []
    cbcr_list = []
    for pair in target_list:
        input_img = scipy.io.loadmat(pair[1])['img_2']
        gt_img = scipy.io.loadmat(pair[0])['img_raw']
        input_list.append(input_img)
        gt_list.append(gt_img)
    input_list = np.array(input_list)
    input_list.resize([batch_size, input_list[0].shape[0],input_list[0].shape[1], 1])
    gt_list = np.array(gt_list)
    gt_list.resize([batch_size, gt_list[0].shape[0],gt_list[0].shape[1], 1])
    return (input_list, gt_list, np.array(cbcr_list))

def batch_norm_wrapper(inputs, is_training, decay=0.999):

    epsilon = 1e-3
    scale = tf.Variable(tf.ones([inputs.get_shape()[-1]]))
    beta = tf.Variable(tf.zeros([inputs.get_shape()[-1]]))
    pop_mean = tf.Variable(tf.zeros([inputs.get_shape()[-1]]),
                           trainable=False)
    pop_var = tf.Variable(tf.ones([inputs.get_shape()[-1]]),
                          trainable=False)

    if is_training:
        (batch_mean, batch_var) = tf.nn.moments(inputs, [0, 1, 2])

        # Small epsilon value for the BN transform

        # print(batch_mean.get_shape())
        # print(pop_mean.get_shape())

        train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean
                               * (1 - decay))
        train_var = tf.assign(pop_var, pop_var * decay + batch_var * (1
                              - decay))
        with tf.control_dependencies([train_mean, train_var]):
            return tf.nn.batch_normalization(
                inputs,
                batch_mean,
                batch_var,
                beta,
                scale,
                epsilon,
                )
    else:
        return tf.nn.batch_normalization(
            inputs,
            pop_mean,
            pop_var,
            beta,
            scale,
            epsilon,
            )

def unet(
    x,
    is_training,
    keep_prob=1,
    channels=1,
    n_class=1,
    layers=3,
    features_root=64,
    filter_size=3,
    pool_size=2,
    summaries=False,
    ):
    """
    Creates a new convolutional unet for the given parametrization.

    :param x: input tensor, shape [?,nx,ny,channels]
    :param keep_prob: dropout probability tensor
    :param channels: number of channels in the input image
    :param n_class: number of output labels
    :param layers: number of layers in the net
    :param features_root: number of features in the first layer
    :param filter_size: size of the convolution filter
    :param pool_size: size of the max pooling operation
    :param summaries: Flag if summaries should be created
    """

    with tf.device('/gpu:0'):
        logging.info('Layers {layers}, features {features}, filter size {filter_size}x{filter_size}, pool size: {pool_size}x{pool_size}'.format(layers=layers,
                     features=features_root, filter_size=filter_size,
                     pool_size=pool_size))

        # Placeholder for the input image

        nx = tf.shape(x)[1]
        ny = tf.shape(x)[2]
        x_image = tf.reshape(x, tf.stack([-1, nx, ny, channels]))
        in_node = x_image
        batch_size = tf.shape(x_image)[0]

        weights = []
        biases = []
        convs = []
        pools = OrderedDict()
        deconv = OrderedDict()
        dw_h_convs = OrderedDict()
        up_h_convs = OrderedDict()

        in_size = 1000
        size = in_size

        # down layers

        for layer in range(0, layers):
            features = 2 ** layer * features_root
            stddev = np.sqrt(2 / (filter_size ** 2 * features))
            if layer == 0:
                w1 = tf.get_variable('down_conv_00_w1', [filter_size,
                        filter_size, channels, features],
                        initializer=tf.random_normal_initializer(stddev=stddev))
            else:
                w1 = tf.get_variable('down_conv_%02d_w1' % (layer + 1),
                        [filter_size, filter_size, features // 2,
                        features],
                        initializer=tf.random_normal_initializer(stddev=stddev))
            w2 = tf.get_variable('down_conv_%02d_w2' % (layer + 1),
                                 [filter_size, filter_size, features,
                                 features],
                                 initializer=tf.random_normal_initializer(stddev=stddev))
            b1 = tf.get_variable('conv_%02d_b1' % (layer + 1),
                                 [features],
                                 initializer=tf.constant_initializer(0.1))
            b2 = tf.get_variable('conv_%02d_b2' % (layer + 1),
                                 [features],
                                 initializer=tf.constant_initializer(0.1))
            conv1 = conv2d(in_node, w1, keep_prob)
            print(conv1.get_shape())
            conv1 = batch_norm_wrapper(conv1, is_training)
            tmp_h_conv = tf.nn.relu(conv1 + b1)
            conv2 = conv2d(tmp_h_conv, w2, keep_prob)
            conv2 = batch_norm_wrapper(conv2, is_training)
            dw_h_convs[layer] = tf.nn.relu(conv2 + b2)
            weights.append((w1, w2))
            biases.append((b1, b2))
            convs.append((conv1, conv2))
            if layer < layers - 1:
                pools[layer] = max_pool(dw_h_convs[layer], pool_size)
                in_node = pools[layer]
        in_node = dw_h_convs[layers - 1]

            # up layers

        for layer in range(layers - 2, -1, -1):
            features = 2 ** (layer + 1) * features_root
            stddev = np.sqrt(2 / (filter_size ** 2 * features))

             # wd = weight_variable_devonc([pool_size, pool_size, features//2, features], stddev)

            wd = tf.get_variable('up_conv_%02d_wd' % (layer + 1),
                                 [pool_size, pool_size, features // 2,
                                 features],
                                 initializer=tf.random_normal_initializer(stddev=stddev))

            # bd = bias_variable([features//2])

            bd = tf.get_variable('up_conv_%02d_bd' % (layer + 1),
                                 [features // 2],
                                 initializer=tf.constant_initializer(0.1))
            h_deconv = tf.nn.relu(deconv2d(in_node, wd, pool_size) + bd)
            h_deconv_concat = crop_and_concat(dw_h_convs[layer],
                    h_deconv)
            deconv[layer] = h_deconv_concat

            # w1 = weight_variable([filter_size, filter_size, features, features//2], stddev)

            w1 = tf.get_variable('up_conv_%02d_w1' % (layer + 1),
                                 [filter_size, filter_size, features,
                                 features // 2],
                                 initializer=tf.random_normal_initializer(stddev=stddev))

            # w2 = weight_variable([filter_size, filter_size, features//2, features//2], stddev)

            w2 = tf.get_variable('up_conv_%02d_w2' % (layer + 1),
                                 [filter_size, filter_size, features
                                 // 2, features // 2],
                                 initializer=tf.random_normal_initializer(stddev=stddev))

            # b1 = bias_variable([features//2])

            b1 = tf.get_variable('up_conv_%02d_b1' % (layer + 1),
                                 [features // 2],
                                 initializer=tf.constant_initializer(0.1))

            # b2 = bias_variable([features//2])

            b2 = tf.get_variable('up_conv_%02d_b2' % (layer + 1),
                                 [features // 2],
                                 initializer=tf.constant_initializer(0.1))

            conv1 = conv2d(h_deconv_concat, w1, keep_prob)
            conv1 = batch_norm_wrapper(conv1, is_training)
            h_conv = tf.nn.relu(conv1 + b1)
            conv2 = conv2d(h_conv, w2, keep_prob)
            conv2 = batch_norm_wrapper(conv2, is_training)
            in_node = tf.nn.relu(conv2 + b2)
            up_h_convs[layer] = in_node

            weights.append((w1, w2))
            biases.append((b1, b2))
            convs.append((conv1, conv2))

            # size *= 2
            # size -= 4

        # Output Map
        # weight = weight_variable([1, 1, features_root, n_class], stddev)

        weight = tf.get_variable('weight', [1, 1, features_root,
                                 n_class],
                                 initializer=tf.random_normal_initializer(stddev=stddev))

        # bias = bias_variable([n_class])

        bias = tf.get_variable('bias', [n_class],
                               initializer=tf.constant_initializer(0.1))
        conv = conv2d(in_node, weight, tf.constant(1.0))

            # conv = batch_norm_wrapper(conv, is_training)

        output_map = tf.nn.relu(conv + bias)

        # output_map = tf.add(output_map, x_image)

        up_h_convs['out'] = output_map

        if summaries:
            for (i, (c1, c2)) in enumerate(convs):
                tf.summary.image('summary_conv_%02d_01' % i,
                                 get_image_summary(c1))
                tf.summary.image('summary_conv_%02d_02' % i,
                                 get_image_summary(c2))

            for k in pools.keys():
                tf.summary.image('summary_pool_%02d' % k,
                                 get_image_summary(pools[k]))

            for k in deconv.keys():
                tf.summary.image('summary_deconv_concat_%02d' % k,
                                 get_image_summary(deconv[k]))

            for k in dw_h_convs.keys():
                tf.summary.histogram('dw_convolution_%02d' % k
                        + '/activations', dw_h_convs[k])

            for k in up_h_convs.keys():
                tf.summary.histogram('up_convolution_%s' % k
                        + '/activations', up_h_convs[k])

        variables = []
        for (w1, w2) in weights:
            variables.append(w1)
            variables.append(w2)

        for (b1, b2) in biases:
            variables.append(b1)
            variables.append(b2)

        # return output_map, variables, int(in_size - size)

            return (output_map, variables)

def model(input_tensor):
	with tf.device("/gpu:0"):
		weights = []
		tensor = None

		#conv_00_w = tf.get_variable("conv_00_w", [3,3,1,64], initializer=tf.contrib.layers.xavier_initializer())
		conv_00_w = tf.get_variable("conv_00_w", [3,3,1,64], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0/9)))
		conv_00_b = tf.get_variable("conv_00_b", [64], initializer=tf.constant_initializer(0))
		weights.append(conv_00_w)
		weights.append(conv_00_b)
		tensor = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input_tensor, conv_00_w, strides=[1,1,1,1], padding='SAME'), conv_00_b))

		for i in range(18):
			#conv_w = tf.get_variable("conv_%02d_w" % (i+1), [3,3,64,64], initializer=tf.contrib.layers.xavier_initializer())
			conv_w = tf.get_variable("conv_%02d_w" % (i+1), [3,3,64,64], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0/9/64)))
			conv_b = tf.get_variable("conv_%02d_b" % (i+1), [64], initializer=tf.constant_initializer(0))
			weights.append(conv_w)
			weights.append(conv_b)
			tensor = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(tensor, conv_w, strides=[1,1,1,1], padding='SAME'), conv_b))

		#conv_w = tf.get_variable("conv_19_w", [3,3,64,1], initializer=tf.contrib.layers.xavier_initializer())
		conv_w = tf.get_variable("conv_20_w", [3,3,64,1], initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0/9/64)))
		conv_b = tf.get_variable("conv_20_b", [1], initializer=tf.constant_initializer(0))
		weights.append(conv_w)
		weights.append(conv_b)
		tensor = tf.nn.bias_add(tf.nn.conv2d(tensor, conv_w, strides=[1,1,1,1], padding='SAME'), conv_b)

		# tensor = tf.add(tensor, input_tensor)
		return tensor, weights

def create_conv_net(x, keep_prob, channels, n_class, layers=3, features_root=16, filter_size=3, pool_size=2, summaries=True):
    """
    Creates a new convolutional unet for the given parametrization.

    :param x: input tensor, shape [?,nx,ny,channels]
    :param keep_prob: dropout probability tensor
    :param channels: number of channels in the input image
    :param n_class: number of output labels
    :param layers: number of layers in the net
    :param features_root: number of features in the first layer
    :param filter_size: size of the convolution filter
    :param pool_size: size of the max pooling operation
    :param summaries: Flag if summaries should be created
    """

    logging.info("Layers {layers}, features {features}, filter size {filter_size}x{filter_size}, pool size: {pool_size}x{pool_size}".format(layers=layers,
                                                                                features=features_root,
                                                                                filter_size=filter_size,
                                                                                pool_size=pool_size))
    # Placeholder for the input image
    nx = tf.shape(x)[1]
    ny = tf.shape(x)[2]
    x_image = tf.reshape(x, tf.stack([-1,nx,ny,channels]))
    in_node = x_image
    batch_size = tf.shape(x_image)[0]

    weights = []
    biases = []
    convs = []
    pools = OrderedDict()
    deconv = OrderedDict()
    dw_h_convs = OrderedDict()
    up_h_convs = OrderedDict()

    in_size = 1000
    size = in_size
    # down layers
    for layer in range(0, layers):
        features = 2**layer*features_root
        stddev = np.sqrt(2 / (filter_size**2 * features))
        if layer == 0:
            w1 = weight_variable([filter_size, filter_size, channels, features], stddev)
        else:
            w1 = weight_variable([filter_size, filter_size, features//2, features], stddev)

        w2 = weight_variable([filter_size, filter_size, features, features], stddev)
        b1 = bias_variable([features])
        b2 = bias_variable([features])

        conv1 = conv2d(in_node, w1, keep_prob)
        tmp_h_conv = tf.nn.relu(conv1 + b1)
        conv2 = conv2d(tmp_h_conv, w2, keep_prob)
        dw_h_convs[layer] = tf.nn.relu(conv2 + b2)

        weights.append((w1, w2))
        biases.append((b1, b2))
        convs.append((conv1, conv2))

        # size -= 4
        if layer < layers-1:
            pools[layer] = max_pool(dw_h_convs[layer], pool_size)
            in_node = pools[layer]
            # size /= 2

    in_node = dw_h_convs[layers-1]

    # up layers
    for layer in range(layers-2, -1, -1):
        features = 2**(layer+1)*features_root
        stddev = np.sqrt(2 / (filter_size**2 * features))

        wd = weight_variable_devonc([pool_size, pool_size, features//2, features], stddev)
        bd = bias_variable([features//2])
        h_deconv = tf.nn.relu(deconv2d(in_node, wd, pool_size) + bd)
        h_deconv_concat = crop_and_concat(dw_h_convs[layer], h_deconv)
        deconv[layer] = h_deconv_concat

        w1 = weight_variable([filter_size, filter_size, features, features//2], stddev)
        w2 = weight_variable([filter_size, filter_size, features//2, features//2], stddev)
        b1 = bias_variable([features//2])
        b2 = bias_variable([features//2])

        conv1 = conv2d(h_deconv_concat, w1, keep_prob)
        h_conv = tf.nn.relu(conv1 + b1)
        conv2 = conv2d(h_conv, w2, keep_prob)
        in_node = tf.nn.relu(conv2 + b2)
        up_h_convs[layer] = in_node

        weights.append((w1, w2))
        biases.append((b1, b2))
        convs.append((conv1, conv2))

        # size *= 2
        # size -= 4

    # Output Map
    weight = weight_variable([1, 1, features_root, n_class], stddev)
    bias = bias_variable([n_class])
    conv = conv2d(in_node, weight, tf.constant(1.0))
    output_map = tf.nn.relu(conv + bias)
    output_map = tf.add(output_map, x_image)
    up_h_convs["out"] = output_map

    if summaries:
        for i, (c1, c2) in enumerate(convs):
            tf.summary.image('summary_conv_%02d_01'%i, get_image_summary(c1))
            tf.summary.image('summary_conv_%02d_02'%i, get_image_summary(c2))

        for k in pools.keys():
            tf.summary.image('summary_pool_%02d'%k, get_image_summary(pools[k]))

        for k in deconv.keys():
            tf.summary.image('summary_deconv_concat_%02d'%k, get_image_summary(deconv[k]))

        for k in dw_h_convs.keys():
            tf.summary.histogram("dw_convolution_%02d"%k + '/activations', dw_h_convs[k])

        for k in up_h_convs.keys():
            tf.summary.histogram("up_convolution_%s"%k + '/activations', up_h_convs[k])

    variables = []
    for w1,w2 in weights:
        variables.append(w1)
        variables.append(w2)

    for b1,b2 in biases:
        variables.append(b1)
        variables.append(b2)

    return output_map, variables, int(in_size - size)

class Unet(object):
    """
    A unet implementation

    :param channels: (optional) number of channels in the input image
    :param n_class: (optional) number of output labels
    :param cost: (optional) name of the cost function. Default is 'cross_entropy'
    :param cost_kwargs: (optional) kwargs passed to the cost function. See Unet._get_cost for more options
    """

    def __init__(self, channels=3, n_class=2, cost="cross_entropy", cost_kwargs={}, **kwargs):
        tf.reset_default_graph()

        self.n_class = n_class
        self.summaries = kwargs.get("summaries", False)
        self.global_step = tf.Variable(0, trainable=False)

        self.x = tf.placeholder("float", shape=[None, None, None, channels])
        self.y = tf.placeholder("float", shape=[None, None, None, n_class])
        self.keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)

        # logits, self.variables, self.offset = create_conv_net(self.x, self.keep_prob, channels, n_class, **kwargs)
        logits, self.variables = unet(self.x, self.keep_prob, channels, n_class, **kwargs)

        # logits, self.variables = model(self.x)

        # self.cost = self._get_cost(logits, cost, cost_kwargs)
        self.predicter = logits
        self.cost = tf.reduce_mean(tf.nn.l2_loss(tf.subtract(logits,self.y)))
        self.correct_pred = tf.equal(self.predicter, self.y)
        self.accuracy = tf.reduce_mean(tf.cast(self.correct_pred, tf.float32))
        self.optimizer = tf.train.AdamOptimizer(learning_rate)  # tf.train.MomentumOptimizer(learning_rate, 0.9)
        self.opt = optimizer.minimize(self.cost, global_step=global_step)

    def predict(self, model_path, x_test):
        """
        Uses the model to create a prediction for the given data

        :param model_path: path to the model checkpoint to restore
        :param x_test: Data to predict on. Shape [n, nx, ny, channels]
        :returns prediction: The unet prediction Shape [n, px, py, labels] (px=nx-self.offset/2)
        """

        init = tf.global_variables_initializer()
        with tf.Session() as sess:
            # Initialize variables
            sess.run(init)

            # Restore model weights from previously saved model
            self.restore(sess, model_path)

            y_dummy = np.empty((x_test.shape[0], x_test.shape[1], x_test.shape[2], self.n_class))
            prediction = sess.run(self.predicter, feed_dict={self.x: x_test, self.y: y_dummy, self.keep_prob: 1.})

        return prediction

    def save(self, sess, model_path):
        """
        Saves the current session to a checkpoint

        :param sess: current session
        :param model_path: path to file system location
        """

        saver = tf.train.Saver()
        save_path = saver.save(sess, model_path)
        return save_path

    def restore(self, sess, model_path):
        """
        Restores a session from a checkpoint

        :param sess: current session instance
        :param model_path: path to file system checkpoint location
        """

        saver = tf.train.Saver()
        saver.restore(sess, model_path)
        logging.info("Model restored from file: %s" % model_path)

class Trainer(object):
    """
    Trains a unet instance

    :param net: the unet instance to train
    :param batch_size: size of training batch
    :param optimizer: (optional) name of the optimizer to use (momentum or adam)
    :param opt_kwargs: (optional) kwargs passed to the learning rate (momentum opt) and to the optimizer
    """

    prediction_path = "prediction_unet"
    verification_batch_size = 4

    def __init__(self, net, batch_size=1, optimizer="momentum", learning_rate=0.001):
        self.net = net
        self.batch_size = batch_size
        self.opt_kwargs = opt_kwargs
        self.learning_rate = learning_rate

    def _initialize(self, training_iters, output_path, restore):
        global_step = tf.Variable(0)

        # self.norm_gradients_node = tf.Variable(tf.constant(0.0, shape=[len(self.net.gradients_node)]))

        # if self.net.summaries:
        #     tf.summary.histogram('norm_grads', self.norm_gradients_node)

        tf.summary.scalar('loss', self.net.cost)
        tf.summary.scalar('accuracy', self.net.accuracy)

        self.summary_op = tf.summary.merge_all()
        init = tf.global_variables_initializer()

        prediction_path = os.path.abspath(self.prediction_path)
        output_path = os.path.abspath(output_path)

        if not restore:
            logging.info("Removing '{:}'".format(prediction_path))
            shutil.rmtree(prediction_path, ignore_errors=True)
            logging.info("Removing '{:}'".format(output_path))
            shutil.rmtree(output_path, ignore_errors=True)

        if not os.path.exists(prediction_path):
            logging.info("Allocating '{:}'".format(prediction_path))
            os.makedirs(prediction_path)

        if not os.path.exists(output_path):
            logging.info("Allocating '{:}'".format(output_path))
            os.makedirs(output_path)

        return init

    def train(self, data_path, output_path, training_iters=10, epochs=100, dropout=0.75, display_step=1, restore=False):
        """
        Lauches the training process

        :param data_provider: callable returning training and verification data
        :param output_path: path where to store checkpoints
        :param training_iters: number of training mini batch iteration
        :param epochs: number of epochs
        :param dropout: dropout probability
        :param display_step: number of steps till outputting stats
        :param restore: Flag if previous model should be restored
        """
        save_path = os.path.join(output_path, "model.cpkt")
        if epochs == 0:
            return save_path

        init = self._initialize(training_iters, output_path, restore)

        with tf.Session() as sess:
            sess.run(init)

            if restore:
                ckpt = tf.train.get_checkpoint_state(output_path)
                if ckpt and ckpt.model_checkpoint_path:
                    self.net.restore(sess, ckpt.model_checkpoint_path)

            # test_x, test_y = data_provider(self.verification_batch_size)
            train_list = get_train_list(data_path)
            shuffle(train_list)
            pred_shape = self.store_prediction(sess, test_x, test_y, "_init")

            summary_writer = tf.summary.FileWriter(output_path, graph=sess.graph)
            logging.info("Start optimization")

            avg_gradients = None
            for epoch in range(epochs):
                total_loss = 0
                for step in range((epoch*training_iters), ((epoch+1)*training_iters)):
                    batch_x, batch_y = data_provider(self.batch_size)

                    # Run optimization op (backprop)
                    _, loss, lr = sess.run((self.optimizer, self.net.cost, self.learning_rate_node),
                                                      feed_dict={self.net.x: batch_x,
                                                                 self.net.y: util.crop_to_shape(batch_y, pred_shape),
                                                                 self.net.keep_prob: dropout})

                    if avg_gradients is None:
                        avg_gradients = [np.zeros_like(gradient) for gradient in gradients]
                    for i in range(len(gradients)):
                        avg_gradients[i] = (avg_gradients[i] * (1.0 - (1.0 / (step+1)))) + (gradients[i] / (step+1))

                    norm_gradients = [np.linalg.norm(gradient) for gradient in avg_gradients]
                    # self.norm_gradients_node.assign(norm_gradients).eval()

                    if step % display_step == 0:
                        self.output_minibatch_stats(sess, summary_writer, step, batch_x, util.crop_to_shape(batch_y, pred_shape))

                    total_loss += loss

                self.output_epoch_stats(epoch, total_loss, training_iters, lr)
                self.store_prediction(sess, test_x, test_y, "epoch_%s"%epoch)

                save_path = self.net.save(sess, save_path)
            logging.info("Optimization Finished!")

            return save_path

    def store_prediction(self, sess, batch_x, batch_y, name):
        prediction = sess.run(self.net.predicter, feed_dict={self.net.x: batch_x,
                                                             self.net.y: batch_y,
                                                             self.net.keep_prob: 1.})
        pred_shape = prediction.shape

        loss = sess.run(self.net.cost, feed_dict={self.net.x: batch_x,
                                                       self.net.y: util.crop_to_shape(batch_y, pred_shape),
                                                       self.net.keep_prob: 1.})

        logging.info("Verification error= {:.1f}%, loss= {:.4f}".format(error_rate(prediction,
                                                                          util.crop_to_shape(batch_y,
                                                                                prediction.shape)),
                                                                          loss))

        img = util.combine_img_prediction(batch_x, batch_y, prediction)
        util.save_image(img, "%s/%s.jpg"%(self.prediction_path, name))

        return pred_shape

    def output_epoch_stats(self, epoch, total_loss, training_iters, lr):
        logging.info("Epoch {:}, Average loss: {:.4f}, learning rate: {:.4f}".format(epoch, (total_loss / training_iters), lr))

    def output_minibatch_stats(self, sess, summary_writer, step, batch_x, batch_y):
        # Calculate batch loss and accuracy
        summary_str, loss, acc, predictions = sess.run([self.summary_op,
                                                            self.net.cost,
                                                            self.net.accuracy,
                                                            self.net.predicter],
                                                           feed_dict={self.net.x: batch_x,
                                                                      self.net.y: batch_y,
                                                                      self.net.keep_prob: 1.})
        summary_writer.add_summary(summary_str, step)
        summary_writer.flush()
        logging.info("Iter {:}, Minibatch Loss= {:.4f}, Training Accuracy= {:.4f}, Minibatch error= {:.1f}%".format(step,
                                                                                loss,
                                                                                acc,
                                                                                error_rate(predictions, batch_y)))

def error_rate(predictions, labels):
    """
    Return the error rate based on dense predictions and 1-hot labels.
    """
    return 100.0 - (
        100.0 *
        np.sum(predictions == labels) /
        (predictions.shape[0]*predictions.shape[1]*predictions.shape[2]))

    # return 100.0 - (
    #     100.0 *
    #     np.sum(np.argmax(predictions, 3) == np.argmax(labels, 3)) /
    #     (predictions.shape[0]*predictions.shape[1]*predictions.shape[2]))

def get_image_summary(img, idx=0):
    """
    Make an image summary for 4d tensor image with index idx
    """

    V = tf.slice(img, (0, 0, 0, idx), (1, -1, -1, 1))
    V -= tf.reduce_min(V)
    V /= tf.reduce_max(V)
    V *= 255

    img_w = tf.shape(img)[1]
    img_h = tf.shape(img)[2]
    V = tf.reshape(V, tf.stack((img_w, img_h, 1)))
    V = tf.transpose(V, (2, 0, 1))
    V = tf.reshape(V, tf.stack((-1, img_w, img_h, 1)))
    return V


text:
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
import numpy as np

import mindspore
import mindspore.context as context
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.ops import operations as P

context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")

class Net(nn.Cell):
    def __init__(self, keep_prob, inplace):
        super(Net, self).__init__()
        self.drop = P.Dropout3d(keep_prob=keep_prob, inplace=inplace)

    def construct(self, x):
        return self.drop(x)

class NetInplace(nn.Cell):
    def __init__(self, keep_prob, inplace, x):
        super(NetInplace, self).__init__()
        self.drop = P.Dropout3d(keep_prob=keep_prob, inplace=inplace)
        self.x = x

    def construct(self):
        return self.drop(self.x)

def test_net_float32():
    x = Tensor(np.random.randn(3, 4, 3, 3, 3), mindspore.float32)
    net = Net(0.7, False)
    output = net(x)
    print(x)
    print(output)

    y = (output.asnumpy() == x.asnumpy()/0.7).reshape(3*4, 3*3*3)
    for i in range(3*4):
        if not y[i].all():
            assert y[i].sum() == 0

def test_net_float32_inplace():
    x = mindspore.Parameter(Tensor(np.random.randn(3, 4, 3, 3, 3), mindspore.float32))
    net = NetInplace(0.7, True, x)
    output = net()
    print(Tensor(x))
    print(output)
    assert np.array_equal(x.asnumpy(), output.asnumpy())

ext: py

max_stars_repo_name: GuoSuiming/mindspore

reason: import numpy


text:
import panacea.read_restart
import panacea.read_files

import numpy as np

# Class provides a means of reading multiple restart files
class RestartFileGroup:
    def __init__(self, path_to_files, base, extension):
        self.__file_grp_reader = panacea.read_files.ReadFiles(
            path_to_files, base, extension, panacea.read_restart.RestartFile
        )
        self.__file_name_to_index = {}

    def read(self, index_start=0, index_end=None):
        self.__file_grp_reader.read(index_start, index_end)

        first_file = True
        for index, file_name in enumerate(self.__file_grp_reader.files_read):
            self.__file_name_to_index[index] = file_name
            dims, dims = self.__file_grp_reader[file_name].covariance.shape
            if not first_file:
                if dims != self.__dims:

                    raise Exception(
                        "Different numbers of dimensions detected while "
                        "reading in a group of restart files. Inconsistency in the "
                        "dimensions is problematic."
                    )
            else:
                first_file = False
                self.__dims = dims

    def __getitem__(self, file_name_or_index):
        return self.__file_grp_reader[file_name_or_index]

    def getCovariance(self, file_name):
        if not self.__file_grp_reader.exists(file_name):
            return None
        return self.__file_grp_reader[file_name].covariance

    def getCovarianceMean(self, file_name):
        if not self.__file_grp_reader.exists(file_name):
            return None
        return self.__file_grp_reader[file_name].covariance_mean

    @property
    def indices_files_read(self):
        return self.__file_grp_reader.indices_files_read

    @property
    def files_read(self):
        return self.__file_grp_reader.files_read

    @property
    def dimensions(self):
        if self.__dims is None:
            raise Exception("Dims unknown, was a file read?")
        return self.__dims

    def getKernelCenters(self, file_name):
        if not self.__file_grp_reader.exists(file_name):
            return None
        return self.__file_grp_reader[file_name].kernel_centers

    def getNormalizationCoefficients(self, file_name):
        if not self.__file_grp_reader.exists(file_name):
            return None
        return self.__file_grp_reader[file_name].normalization_coefficients

max_stars_repo_name: lanl/PANACEA

max_issues_repo_name: lanl/PANACEA

max_forks_repo_name: lanl/PANACEA


text:
# coding: utf-8

import numpy as np
from .helpers import isfc, gaussian_weights, format_data, null_combine, mean_combine, reduce, smooth, eye_weights

def timecorr(data, weights_function=gaussian_weights,
             weights_params=None, include_timepoints='all', exclude_timepoints=None,
             combine=null_combine, cfun=isfc, rfun=None, order=None):
    """
    Computes dynamics correlations in single-subject or multi-subject data.

    Parameters
    ----------
    data: numpy array, pandas dataframe, or a list of numpy arrays/dataframes
        Each numpy array (or dataframe) should have size timepoints by features.
        If a list of arrays are passed, there should be one array per subject.

    weights_function: a function of the form func(T, params) where
        T is a non-negative integer specifying the number of timepoints to consider.

        The function should return a T by T array containing the timepoint-specific
        weights for each consecutive time point from 0 to T (not including T).

        Default: gaussian_weights; options: laplace_weights, gaussian_weights,
        t_weights, eye_weights, mexican_hat_weights

    weights_params: used to pass parameters to the weights_params function. This
        can be specified in any format (e.g. a scalar, list, object, dictionary,
        etc.).

        Default: None (use default parameters for the given weights function).
        Options: gaussian_params, laplace_params, t_params, eye_params,
        mexican_hat_params.

    include_timepoints: determines which timepoints are used to estimate the correlations
        at each timepoint.  This is applied after the weights function to further constrain
        which timepoints may be considered in computing the correlations at each timepoint.

        Options: 'all' (default; include all timepoints), 'pre' (only include timepoints *before*
        the given timepoint), 'post' (only include timepoints *after* the given timepoint).

    exclude_timepoints: additional option, used to filter out any timepoints less than x units
        of the timepoint whose correlations are being estimated.  For example, passing
        exclude_timepoints=3 will exclude any timepoints 3 or more samples from the given timepoint.
        When exclude timepoints is negative, it works inversely-- e.g. exclude_timepoints=-5 will
        exclude any timepoints within 5 or fewer samples of the given timepoint.  Real-valued scalars
        are supported but are rounded to the nearest Integer.

        Default: None (no filtering).

    combine: a function applied to either a single matrix of vectorized correlation
        matrices, or a list of such matrices.  The function should return either
        a numpy array or a list of numpy arrays.

        Default: helpers.null_combine (a function that returns its input).  Other
        useful functions:

        helpers.corrmean_combine: take the element-wise average correlations across matrices
        helpers.tstat_combine: return element-wise t-statistics across matrices

    cfun: function to apply to the data array(s)
        This function should be of the form
        func(data, weights)

        The function should support data as a numpy array or list of numpy
        arrays.  When a list of numpy arrays is passed, the function should
        apply the "across subjects" version of the analysis.  When a single
        numpy array is passed, the function should apply the "within subjects"
        version of the analysis.

        weights is a numpy array with per-timepoint weights

        The function should return a single numpy array with 1 row and an
        arbitrary number of columns (the number of columns may be determined by
        the function).

        Default: A continuous verison of Inter-Subject Functional Connectivity
        (Simony et al. 2017).  If only one data array is passed (rather than a
        list), the default cfun returns the moment-by-moment correlations for
        that array.  (Reference: http://www.nature.com/articles/ncomms12141)

    rfun: function to use for dimensionality reduction.

        All hypertools and scikit-learn functions are supported: PCA, IncrementalPCA, SparsePCA,
        MiniBatchSparsePCA, KernelPCA, FastICA, FactorAnalysis, TruncatedSVD,
        DictionaryLearning, MiniBatchDictionaryLearning, TSNE, Isomap,
        SpectralEmbedding, LocallyLinearEmbedding, MDS, and UMAP.

        Can be passed as a string, but for finer control of the model
        parameters, pass as a dictionary, e.g.
        reduction={‘model’ : ‘PCA’, ‘params’ : {‘whiten’ : True}}.

        See scikit-learn specific model docs for details on parameters supported
        for each model.

        Another option is to use graph theoretic measures computed for each node.
        The following measures are supported (via the brainconn toolbox):
        eigenvector_centrality, pagerank_centrality, and strength.  (Each
        of these must be specified as a string; dictionaries not supported.)

        Default: None (no dimensionality reduction)

    Returns
    ----------
    corrmats: moment-by-moment correlations
    """

    def temporal_filter(T, k):
        k = np.round(k)
        filt = np.eye(T)
        for i in np.arange(1, np.min([np.abs(k) + 1, T])):
            filt = filt + np.eye(T, k=np.abs(i)) + np.eye(T, k=-np.abs(i))
        if k < 0:
            return 1 - filt
        else:
            return filt

    if order is None:
        if type(data) == list:
            T = data[0].shape[0]
            return_list = True
        else:
            T = data.shape[0]
            data = [data]
            return_list = False

        data = format_data(data)
        weights = weights_function(T, weights_params)

        include_timepoints = include_timepoints.lower()
        if include_timepoints == 'all':
            pass
        elif include_timepoints == 'pre':
            weights = np.tril(weights)
        elif include_timepoints == 'post':
            weights = np.triu(weights)
        else:
            raise Exception(f'Invalid option for include_timepoints: \'{include_timepoints}\'.  Must be one of: \'all\', \'pre\', or \'post\'.')

        if not (exclude_timepoints is None):
            weights = np.multiply(temporal_filter(T, exclude_timepoints), weights)

        if cfun:
            corrs = reduce(combine(cfun(data, weights)), rfun=rfun)
            if not (cfun is None):
                return_list = False
        else:
            corrs = combine(smooth(data, kernel_fun=weights_function, kernel_params=weights_params))

        if return_list and (not (type(corrs) == list)):
            return [corrs]
        elif (not return_list) and (type(corrs) == list) and (len(corrs) == 1):
            return corrs[0]
        else:
            return corrs

    elif isinstance(order, int):
        if order == 0:
            return timecorr(data, weights_function=weights_function, weights_params=weights_params,
                            include_timepoints=include_timepoints, exclude_timepoints=exclude_timepoints,
                            combine=null_combine, cfun=isfc, rfun=None, order=None)
        elif order < 0:
            raise ValueError(f'Invalid option for order: \'{order}\'.  Must be a non-negative integer or a list of non-negative integers.')
        else:
            return timecorr(data, weights_function, weights_params, include_timepoints, exclude_timepoints,
                            null_combine, isfc, rfun, [*range(order + 1)])

    elif isinstance(order, list):
        if not all(isinstance(o, int) and o >= 0 for o in order):
            raise ValueError(f'Invalid option for order: \'{order}\'.  List must contain only non-negative integers.')
        else:
            idx = dict(); max_order = -1
            for i, o in enumerate(order):
                if o > max_order:
                    max_order = o
                idx.setdefault(o, []).append(i)

            order_corrs = [None] * len(order)
            for cord in range(max_order + 1):
                if cord == 0:
                    raw = data
                    smoothed = timecorr(data, weights_function=weights_function, weights_params=weights_params,
                                        include_timepoints=include_timepoints, exclude_timepoints=exclude_timepoints,
                                        combine=combine, cfun=None, rfun=None, order=None)
                else:
                    raw = timecorr(raw, weights_function=eye_weights, weights_params={},
                                   include_timepoints=include_timepoints, exclude_timepoints=exclude_timepoints,
                                   combine=null_combine, cfun=cfun, rfun=rfun, order=None)
                    smoothed = timecorr(raw, weights_function=weights_function, weights_params=weights_params,
                                        include_timepoints=include_timepoints, exclude_timepoints=exclude_timepoints,
                                        combine=combine, cfun=cfun, rfun=rfun, order=None)
                if cord in idx:
                    for i in idx[cord]:
                        order_corrs[i] = smoothed

        return order_corrs


text:
# ---
# jupyter:
#   jupytext:
#     comment_magics: 'false'
#     formats: ipynb,py:percent
#     text_representation:
#       extension: .py
#       format_name: percent
#       format_version: '1.1'
#       jupytext_version: 0.8.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
#   language_info:
#     codemirror_mode:
#       name: ipython
#       version: 3
#     file_extension: .py
#     mimetype: text/x-python
#     name: python
#     nbconvert_exporter: python
#     pygments_lexer: ipython3
#     version: 3.6.8
# ---

# %%
import numpy as np
import matplotlib.pyplot as plt

from spaudiopy import utils, IO, sph, plots, grids

# %% Spherical Harmonics Order
N = 1

# %%
# Grids
t = grids.load_t_design(2)
t_az, t_colat, t_r = utils.cart2sph(t[:, 0], t[:, 1], t[:, 2])
azi = t_az
colat = t_colat

# %%
# First, check condition number to which SH order the SHT is stable
# Tetraeder is not suited for SHT N>1:
sph.check_cond_sht(3, t_az, t_colat, 'real')

# %% Real and Complex SHs
Y_nm_c = sph.sh_matrix(N, azi, colat, 'complex')
Y_nm_r = sph.sh_matrix(N, azi, colat, 'real')

# %%
# Look at some SHTs
sig = np.array([1, 1, 1, 1])
sig_t = np.c_[np.eye(4), np.eye(4)]  # second axis s(t)
sig_B = sph.soundfield_to_b(sig)
F_nm = sph.sht(sig, N, azi, colat, SH_type='real')
F_nm_t = sph.sht(sig_t, N, azi, colat, SH_type='real')

# %%
F_nm_c = sph.sht(sig, N, azi, colat, SH_type='complex')
F_nm_c_t = sph.sht(sig_t, N, azi, colat, SH_type='complex')

# %%
F_nm_lst = sph.sht_lstsq(sig, N, azi, colat, SH_type='complex')
F_nm_lst_t = sph.sht_lstsq(sig_t, N, azi, colat, SH_type='real')

# %% inverse SHT
f = sph.inverse_sht(F_nm, azi, colat, SH_type='real')
f_c_t = sph.inverse_sht(F_nm_c_t, azi, colat, SH_type='complex')
f_lst_t = sph.inverse_sht(F_nm_lst_t, azi, colat, SH_type='real')

# %% Checks
print("Single dimension signal:")
utils.test_diff(sig, f)
print("Complex valued SHT of time signal:")
utils.test_diff(sig_t, f_c_t)
print("Real valued least-squares SHT of time signal:")
utils.test_diff(sig_t, f_lst_t)

# %%
# Check B format conversion
B_sig = np.array([1, 1, 0, 0])  # W, X, Y, Z
F_B = sph.b_to_sh(B_sig)
B_sig_re = sph.sh_to_b(F_B)
print("B format to SH conversion:")
utils.test_diff(B_sig, B_sig_re)

# %%
# Some plots
plots.sh_coeffs(F_nm, title="Ambeo: all channels max")
plots.sh_coeffs(F_B, title="b_to_sh: W+X")

# %%
plots.sh_coeffs_subplot([np.array([1, 0, 0, 0]),
                         np.array([0, 1, 0, 0]),
                         np.array([0, 0, 1, 0]),
                         np.array([0, 0, 0, 1])],
                        titles=["0", "1, -1", "1, 0", "1, 1"])

# %%
plots.sh_coeffs(np.sqrt(2) * np.array([1, 0, 0, -1]), 'complex',
                title="sqrt(2) * [1, 0, 0, -1] complex coeffs")

# %%
# Look at simple B format generator
sig2 = np.ones(8)
B = sph.src_to_B(sig2, np.pi / 4, np.pi / 4)
B_nm = sph.b_to_sh(B)
plots.sh_coeffs(B_nm[:, 0], title="Sig 2 B")

# %%
plt.show()

max_stars_repo_name: BenjSta/spaudiopy

max_issues_repo_name: BenjSta/spaudiopy

max_forks_repo_name: BenjSta/spaudiopy


text:
import csv
import math
import numpy as np

datasets = ['ec2', 'iam', 's3', 'ec2_action_resource', 'iam_action_resource', 's3_action_resource']

for dataset in datasets:
    reader = csv.reader(open('README_{}_single_nolog.csv'.format(dataset), 'r'))

    data = list(reader)[1:]

    out = ''

    total = sum([float(line[2]) + float(line[4]) for line in data if line[1] == 'SAT'] + [float(line[2]) for line in data if line[1] == 'UNSAT'])
    out += str(total / len([line for line in data])) + ','

    total = sum([int(line[3]) for line in data if line[1] != 'UNSAT'])
    out += str(math.log2(total // len([int(line[3]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([int(line[5]) for line in data if line[1] != 'UNSAT'])
    out += str(math.log2(total // len([int(line[5]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([int(line[6]) for line in data if line[1] != 'UNSAT'])
    out += str(math.log2(total // len([int(line[6]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([int(line[7]) for line in data if line[1] != 'UNSAT'])
    out += str(math.log2(total // len([int(line[7]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([math.log2(int(line[3])) for line in data if line[1] != 'UNSAT'])
    out += str((total / len([int(line[3]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([math.log2(int(line[5])) for line in data if line[1] != 'UNSAT'])
    out += str((total / len([int(line[5]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([math.log2(int(line[6])) for line in data if line[1] != 'UNSAT'])
    out += str((total / len([int(line[6]) for line in data if line[1] != 'UNSAT']))) + ','

    total = sum([math.log2(int(line[7])) for line in data if line[1] != 'UNSAT'])
    out += str((total / len([int(line[7]) for line in data if line[1] != 'UNSAT'])))

    print(out)

max_stars_repo_name: vlab-cs-ucsb/quacky

max_issues_repo_name: vlab-cs-ucsb/quacky

max_forks_repo_name: vlab-cs-ucsb/quacky


text:
"""
To eliminate averaging of positive and negative voltages of polyphasic signals, all MEP voltages were converted to absolute values and integrated for measures of power (area under the curve). [...] Peak-to-peak amplitude, motor threshold (MT) and integrated amplitude were computed automatically in the 5–30 msec time window, as our pilot data indicated a 4 msec maximal duration of TMS artifact, and approximately 25 msec maximal latency to onset of the MEP in rats
"""
import numpy as np
from numpy import ndarray
from typing import Tuple
from math import ceil

def rotenberg(
    trace: ndarray,
    tms_sampleidx: int,
    mep_window_in_ms: Tuple[float, float] = (5, 30),
    fs: float = 1000,
) -> float:
    """Estimate the area of an iMEP based on Rotenberg 2010

    Returns the iMEP Area of the rectified EMG integrated for the search window

    .. warning::

        This study was conducted on rats, and therefore the default search window from 5 to 30ms is probably too fast in humans. We therefore implemented the mep_window_in_ms as an argument. Consider adapting it when conducting studies with humans, e.g. to 15 to 50ms.

    args
    ----
    trace:ndarray
        the one-dimensional (samples,) EMG signal with units of µV
    tms_sampleidx: int
        the sample at which the TMS pulse was applied
    fs:float
        the sampling rate of the signal

    mep_window_in_ms: Tuple[float, float]
        the search window after TMS to look for an iMEP.

    returns
    -------
    amplitude:float
        the  iMEP area

    .. admonition:: Reference

        Rotenberg, A.; Muller, P. A.; Vahabzadeh-Hagh, A. M.; Navarro, X.; López-Vales, R.; Pascual-Leone, A. & Jensen, F. Lateralization of forelimb motor evoked potentials by transcranial magnetic stimulation in rats Clinical Neurophysiology, Elsevier BV, 2010, 121, 104-108

    """
    a = tms_sampleidx + ceil(mep_window_in_ms[0] * fs / 1000)
    # b should not be higher then the len of the trace
    b = ceil(
        min((tms_sampleidx + (mep_window_in_ms[1] * fs / 1000)), len(trace))
    )
    amp = np.sum(np.abs(trace[a:b]))
    return amp

max_stars_repo_name: translationalneurosurgery/tool-dimep

max_issues_repo_name: agricolab/tool-dimep

max_forks_repo_name: neuromti/tool-dimep

reason: import numpy,from numpy


text:
import os

from dateutil import parser
from typing import List, Union
from xml.etree import ElementTree
from xml.etree.ElementTree import Element

import cv2
import numpy as np

class MaskAnnotations:
    def __init__(self, image, masks):
        self.image = image
        self.masks = masks

def point_to_attr_string(points):
    ret = ""
    length = points.shape[0]
    for i in range(length):
        point = points[i, 0, :]
        ret = f"{ret}{point[0]},{point[1]};"
    return ret[:-1]  # remove trailing semicolon

def build_polygon_nodes(instances, label_list, epsilon=0.02):
    size = len(instances.scores)
    nodes = []
    for i in range(size):
        label = label_list[instances.pred_classes[i] - 1]
        single_mask = instances.pred_masks[i].cpu().numpy()
        single_mask.dtype = np.uint8
        contours, _ = cv2.findContours(single_mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        c = max(contours, key=lambda c: cv2.contourArea(c))
        peri = cv2.arcLength(c, True)
        points = cv2.approxPolyDP(c, peri * epsilon, True)

        nodes.append(ElementTree.Element("polygon", {
            "label": label,
            "occluded": "0",  # TODO
            "points": point_to_attr_string(points),
        }))
        nodes.append(ElementTree.Element("attribute"))
    return nodes

def build_image_node(polygon_nodes, image_id):
    image_node = ElementTree.Element("image")
    image_node.attrib = {
        "id": str(image_id),
    }
    image_node.extend(polygon_nodes)
    return image_node

def mask_to_polygon_xml(anns: List[MaskAnnotations], label_list, epsilon=0.02):
    annotations_node = ElementTree.Element("annotations")
    version = ElementTree.SubElement(annotations_node, "version")
    version.data("1.1")

    for ann in anns:
        image_node = ElementTree.SubElement(annotations_node, "image")
        image_node.attrib = {
            "id": ann.image,
        }
        size = len(ann.masks.scores)
        for i in range(size):
            label = label_list[ann.masks.pred_classes[i]]
            single_mask = ann.masks.pred_masks[i].numpy()
            single_mask.dtype = np.uint8
            _, contours, _ = cv2.findContours(single_mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            for c in contours:
                peri = cv2.arcLength(c, True)
                points = cv2.approxPolyDP(contours[0], peri * epsilon, True)

                ElementTree.SubElement(image_node, "polygon", {
                    "name": label,
                    "points": point_to_attr_string(points),
                })

def cvat_xml_to_coco(xml: Union[str, Element], ignore_crowded=True, occluded_as_crowded=False, ignore_attributes=False,
                     cvat_base_dir=""):
    """
    convert cvat xml 1.1 into coco json
    :param xml:
    :param ignore_crowded:  remove crowded annotations
    :param occluded_as_crowded: transfer occluded attributes into crowded
    :param ignore_attributes: do not add attributes into a "attributes" key
    :param cvat_base_dir: path to cvat shared folder
    :return: coco json
    """
    import re
    from pycocotools import coco
    def polygon_points_to_segments(points_str):
        return [float(x) for x in re.split("[,;]", points_str)]

    root: Element
    if isinstance(xml, Element):
        root = xml
    else:
        root = ElementTree.fromstring(xml)
    created_text = root.find("meta/task/created").text
    created = parser.parse(created_text)

    labels = root.find("meta/task/labels")

    categories = []
    category_id_map = {}
    for i, label in enumerate(labels, 1):
        name = label.find("name").text
        categories.append({
            "id": i,
            "name": name,
            "supercategory": "",
        })
        category_id_map[name] = i

    images = []
    annotations = []
    annotation_id = 1
    image_nodes = root.findall("image")
    for image in image_nodes:
        image_id = int(image.get("id"))
        height = int(image.get("height"))
        width = int(image.get("width"))
        images.append({
            "coco_url": "",
            "date_captured": "",
            "flickr_url": "",
            "license": 0,
            "id": image_id,
            "file_name": os.path.join(cvat_base_dir or "", image.get("name")),
            "height": height,
            "width": width,

        })
        for annotation in image:
            assert annotation.tag == "polygon"
            segmentation = polygon_points_to_segments(annotation.get("points"))
            rle = coco.maskUtils.frPyObjects([segmentation], height, width)  # TODO multipart annotation
            if not occluded_as_crowded:
                iscrowd = 0
            else:
                iscrowd = 1 if annotation.get("occluded") == "1" else 0
            ann = {
                "category_id": category_id_map[annotation.get("label")],
                "id": annotation_id,
                "image_id": image_id,
                "iscrowd": iscrowd,
                "segmentation": [segmentation],
                "area": float(coco.maskUtils.area(rle)),
                "bbox": coco.maskUtils.toBbox(rle).tolist()[0],
            }
            if not ignore_attributes:
                attr_dict = {}
                for attr in annotation:
                    attr_dict[attr.get("name")] = attr.text
                    ann["attributions"] = attr_dict
            if ann["iscrowd"] and ignore_crowded:
                continue
            annotations.append(ann)
            annotation_id += 1

    # empty licenses that matches cvat export
    return {

        "licenses": [{"name": "", "id": 0, "url": ""}],
        "info": {
            "contributor": root.find("meta/task/owner/username").text,
            "date_created": created.strftime("%Y-%m-%d"),
            "description": root.find("meta/task/name").text,
            "url": "",
            "version": 3,
            "year": str(created.year)
        },
        "categories": categories,
        "images": images,
        "annotations": annotations,
    }


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 24 13:07:39 2018

@author: nvidia
"""
###############################################################################
import numpy as np
###############################################################################
from pyro.dynamic import mechanical
###############################################################################

###############################################################################

class SinglePendulum( mechanical.MechanicalSystem ):
    """

    """

    ############################
    def __init__(self):
        """ """

        # initialize standard params
        mechanical.MechanicalSystem.__init__(self, 1 )

        # Name
        self.name = 'Single Pendulum'

        # params
        self.setparams()

    #############################
    def setparams(self):
        """ Set model parameters here """

        # kinematic
        self.l1  = 2
        self.lc1 = 1

        # dynamic
        self.m1       = 1
        self.I1       = 1
        self.gravity  = 9.81
        self.d1       = 0

    ##############################
    def trig(self, q ):
        """ Compute cos and sin """

        c1  = np.cos( q )
        s1  = np.sin( q )

        return [c1,s1]

    ###########################################################################
    def H(self, q ):
        """
        Inertia matrix
        ----------------------------------
        dim( H ) = ( dof , dof )

        such that --> Kinetic Energy = 0.5 * dq^T * H(q) * dq

        """

        H = np.zeros((self.dof,self.dof))

        H[0,0] = self.m1 * self.lc1**2 + self.I1

        return H

    ###########################################################################
    def C(self, q , dq ):
        """
        Corriolis and Centrifugal Matrix
        ------------------------------------
        dim( C ) = ( dof , dof )

        such that --> d H / dt =  C + C^T

        """

        C = np.zeros((self.dof,self.dof))

        return C

    ###########################################################################
    def B(self, q ):
        """
        Actuator Matrix  : dof x m
        """

        B = np.diag( np.ones( self.dof ) ) #  identity matrix

        return B

    ###########################################################################
    def g(self, q ):
        """
        Gravitationnal forces vector : dof x 1
        """

        g = np.zeros( self.dof )

        [c1,s1] = self.trig( q )

        g[0] = self.m1 * self.gravity * self.lc1 * s1

        return g

    ###########################################################################
    def d(self, q , dq ):
        """
        State-dependent dissipative forces : dof x 1
        """

        d    = np.zeros( self.dof )

        d[0] = self.d1 * dq[0]

        return d

    ###########################################################################
    # Graphical output
    ###########################################################################

    ###########################################################################
    def forward_kinematic_domain(self, q ):
        """
        """
        l = 5

        domain  = [ (-l,l) , (-l,l) , (-l,l) ]#

        return domain

    ###########################################################################
    def forward_kinematic_lines(self, q ):
        """
        Compute points p = [x;y;z] positions given config q
        ----------------------------------------------------
        - points of interest for ploting

        Outpus:
        lines_pts = [] : a list of array (n_pts x 3) for each lines

        """

        lines_pts = [] # list of array (n_pts x 3) for each lines

        # ground line
        pts      = np.zeros(( 2 , 3 ))
        pts[0,:] = np.array([-10,0,0])
        pts[1,:] = np.array([+10,0,0])

        lines_pts.append( pts )

        # pendulum
        pts      = np.zeros(( 2 , 3 ))
        pts[0,:] = np.array([0,0,0])

        [c1,s1] = self.trig( q )

        pts[1,:] = np.array([ s1 * self.l1 , - c1 * self.l1 ,0])

        lines_pts.append( pts )

        return lines_pts

##############################################################################

class DoublePendulum( mechanical.MechanicalSystem ):
    """

    """

    ############################
    def __init__(self):
        """ """

        # initialize standard params
        mechanical.MechanicalSystem.__init__(self, 2)

        # Name
        self.name = 'Double Pendulum'

        # params
        self.setparams()

    #############################
    def setparams(self):
        """ Set model parameters here """

        self.l1  = 1
        self.l2  = 1
        self.lc1 = 1
        self.lc2 = 1

        self.m1 = 1
        self.I1 = 0
        self.m2 = 1
        self.I2 = 0

        self.gravity = 9.81

        self.d1 = 0
        self.d2 = 0

    ##############################
    def trig(self, q ):
        """
        Compute cos and sin usefull in other computation
        ------------------------------------------------

        """

        c1  = np.cos( q[0] )
        s1  = np.sin( q[0] )
        c2  = np.cos( q[1] )
        s2  = np.sin( q[1] )
        c12 = np.cos( q[0] + q[1] )
        s12 = np.sin( q[0] + q[1] )

        return [c1,s1,c2,s2,c12,s12]

    ###########################################################################
    def H(self, q ):
        """
        Inertia matrix
        ----------------------------------
        dim( H ) = ( dof , dof )

        such that --> Kinetic Energy = 0.5 * dq^T * H(q) * dq

        """

        [c1,s1,c2,s2,c12,s12] = self.trig( q )

        H = np.zeros((2,2))

        H[0,0] = self.m1 * self.lc1**2 + self.I1 + self.m2 * ( self.l1**2 + self.lc2**2 + 2 * self.l1 * self.lc2 * c2 ) + self.I2
        H[1,0] = self.m2 * self.lc2**2 + self.m2 * self.l1 * self.lc2 * c2 + self.I2
        H[0,1] = H[1,0]
        H[1,1] = self.m2 * self.lc2 ** 2 + self.I2

        return H

    ###########################################################################
    def C(self, q , dq ):
        """
        Corriolis and Centrifugal Matrix
        ------------------------------------
        dim( C ) = ( dof , dof )

        such that --> d H / dt =  C + C^T

        """

        [c1,s1,c2,s2,c12,s12] = self.trig( q )

        h = self.m2 * self.l1 * self.lc2 * s2

        C = np.zeros((2,2))

        C[0,0] = - h  * dq[1]
        C[1,0] =   h  * dq[0]
        C[0,1] = - h * ( dq[0] + dq[1] )
        C[1,1] = 0

        return C

    ###########################################################################
    def B(self, q ):
        """
        Actuator Matrix  : dof x m
        """

        B = np.diag( np.ones( self.dof ) ) #  identity matrix

        return B

    ###########################################################################
    def g(self, q ):
        """
        Gravitationnal forces vector : dof x 1
        """

        [c1,s1,c2,s2,c12,s12] = self.trig( q )

        g1 = (self.m1 * self.lc1 + self.m2 * self.l1 ) * self.gravity
        g2 = self.m2 * self.lc2 * self.gravity

        G = np.zeros(2)

        G[0] = - g1 * s1 - g2 * s12
        G[1] = - g2 * s12

        return G

    ###########################################################################
    def d(self, q , dq ):
        """
        State-dependent dissipative forces : dof x 1
        """

        D = np.zeros((2,2))

        D[0,0] = self.d1
        D[1,0] = 0
        D[0,1] = 0
        D[1,1] = self.d2

        d = np.dot( D , dq )

        return d

    ###########################################################################
    # Graphical output
    ###########################################################################

    ###########################################################################
    def forward_kinematic_domain(self, q ):
        """
        """
        l = 3

        domain  = [ (-l,l) , (-l,l) , (-l,l) ]#

        return domain

    ###########################################################################
    def forward_kinematic_lines(self, q ):
        """
        Compute points p = [x;y;z] positions given config q
        ----------------------------------------------------
        - points of interest for ploting

        Outpus:
        lines_pts = [] : a list of array (n_pts x 3) for each lines

        """

        lines_pts = [] # list of array (n_pts x 3) for each lines

        ###############################
        # ground line
        ###############################

        pts      = np.zeros(( 2 , 3 ))
        pts[0,:] = np.array([-10,0,0])
        pts[1,:] = np.array([+10,0,0])

        lines_pts.append( pts )

        ###########################
        # pendulum kinematic
        ###########################

        pts      = np.zeros(( 3 , 3 ))
        pts[0,:] = np.array([0,0,0])

        [c1,s1,c2,s2,c12,s12] = self.trig( q )

        pts[1,0] = self.l1 * s1
        pts[1,1] = self.l1 * c1

        pts[2,0] = self.l1 * s1 + self.l2 * s12
        pts[2,1] = self.l1 * c1 + self.l2 * c12

        lines_pts.append( pts )

        return lines_pts

'''
#################################################################
##################          Main                         ########
#################################################################
'''

if __name__ == "__main__":
    """ MAIN TEST """

    #sys  = SinglePendulum()
    #x0   = np.array([0,1])

    #sys.plot_trajectory( x0 )

    #sys.show( np.array([0]))
    #sys.show3( np.array([0]))

    #sys.animate_sim()

    sys = DoublePendulum()
    x0 = np.array([0.1,0.1,0,0])

    #sys.show(np.array([0.1,0.1]))
    sys.show3(np.array([0.1,0.1]))

    sys.plot_trajectory( x0 , 20)
    sys.animate_simulation()

max_stars_repo_name: echoix/pyro

max_issues_repo_name: echoix/pyro

max_forks_repo_name: echoix/pyro


text:
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 14 16:22:05 2020

@author: vhchuong
"""
import torch as th
import os
import numpy as np
from data import VRVideo
import torchvision.transforms as tf
from torchvision.utils import save_image
from torch.utils import data as tdata
from tqdm import tqdm
import visdom
from model import Final1
import cv2

def test(
        data_dir='./360_Saliency_dataset_2018ECCV',
        bs=1,
        plot_server='http://localhost',
        plot_port=8097,
        exp_name='test1'
):
    outputdir = './output123'
    if not os.path.exists(outputdir):
        os.makedirs(outputdir)
    viz = visdom.Visdom(server=plot_server, port=plot_port, env=exp_name)
    #textwindow = viz.text("Hello Pytorch3")
    transform = tf.Compose([
        tf.Resize((240, 480)),
        tf.ToTensor()
    ])
    coor1 = th.load('./support/240x480.pt')#.cuda()
    coor2 = th.load('./support/120x240.pt')#.cuda()
    coor3 = th.load('./support/60x120.pt')#.cuda()
    coor4 = th.load('./support/30x60.pt')#.cuda()
    tar = th.load('ckpt-final - 20epoch -latest.pth.tar')
    #Loading model
    model = Final1().cuda()
    #model = nn.DataParallel(model).cuda()

    #state_dict = tar['state_dict']
    model.load_state_dict(tar['state_dict'])
    model.eval()

    #Prepare Dataset 128, 256
    dataset = VRVideo(data_dir, 240, 480, 80, frame_interval=5, train=False, cache_gt=True, transform=transform, gaussian_sigma=np.pi/20, kernel_rad=np.pi/7)
    print(len(dataset))
    loader = tdata.DataLoader(dataset, batch_size=bs, shuffle=False, num_workers=0, pin_memory=True)
    with th.no_grad():
    #Put data through the model
        for i, (img_batch, a, last_batch, target_batch) in tqdm(enumerate(loader), desc='batch', total = len(loader)):

            if i%10==0:
                img_var = img_batch.cuda()
                last_var = (last_batch * 10).cuda()

                out = model(img_var, last_var,coor1, coor2, coor3, coor4)
                #viz.images(target_batch.cpu().numpy() * 10)
                #print(out)
                img = img_batch[0]
                img1 = out[0]
                img2 = target_batch[0]
                save_image(img.cpu(), outputdir + '/img_'+ str(i) +'.png')
                save_image((img1*30).cpu(), outputdir + '/SalMap_'+ str(i) +'.png')
                save_image((img2*300).cpu(), outputdir + '/gt_'+ str(i) +'.png')

                gt = cv2.imread(outputdir + '/gt_'+ str(i) +'.png',0)
                gt = th.tensor(gt)
                Salmap = cv2.imread(outputdir + '/SalMap_'+ str(i) +'.png',0)
                Salmap = th.tensor(Salmap)
                viz.images(img_batch.cpu().numpy(), win='Frame')
                viz.images(Salmap.cpu().numpy(), win='Saliency map')
                viz.images(gt.cpu().numpy(), win='Ground truth')
            if i == 1000:
                break

if __name__ == '__main__':
    test()


text:
# 导入代码文件_06_yolov3.py的类Detector
from _06_yolov3 import Detector
# 导入常用的库
import numpy as np
import time
import os
from PIL import Image
# 导入flask库
from flask import Flask, render_template, request, jsonify
# 加载把图片文件转换为字符串的base64库
import base64
from yolo3.utils import letterbox_image
# 导入代码文件_12_yolov3_client_3.py中的画图方法
from _12_yolov3_client_3 import get_drawedImage

# 实例化Flask服务对象，赋值给变量server
server = Flask(__name__)
# 设置开启web服务后，如果更新html文件，可以使更新立即生效
server.jinja_env.auto_reload = True
server.config['TEMPLATES_AUTO_RELOAD'] = True

# 实例化检测器对象
detector = Detector(
    weights_h5FilePath='../resources/yolov3/yolov3_weights.h5',
    anchor_txtFilePath='../resources/yolov3/yolov3_anchors.txt',
    category_txtFilePath='../resources/yolov3/coco.names'
    )

# 获取当前时间表示的字符串的小数部分，精确到0.1毫秒
def get_secondFloat(timestamp):
    secondFloat = ('%.4f' %(timestamp%1))[1:]
    return secondFloat

# 获取当前时间表示的字符串，精确到0.1毫秒
def get_timeString():
    now_timestamp = time.time()
    now_structTime = time.localtime(now_timestamp)
    timeString_pattern = '%Y%m%d_%H%M%S'
    now_timeString_1 = time.strftime(timeString_pattern, now_structTime)
    now_timeString_2 = get_secondFloat(now_timestamp)
    now_timeString = now_timeString_1 + now_timeString_2
    return now_timeString

# 获取使用YOLOv3算法做目标检测的结果
def get_detectResult(image):
    startTime = time.time()
    boxed_image = letterbox_image(image, (416, 416))
    image_data = np.array(boxed_image).astype('float') / 255
    image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
    # 模型网络结构运算
    box_ndarray, classId_ndarray, score_ndarray = detector.session.run(
        [detector.boxes, detector.classes, detector.scores],
        feed_dict={
            detector.yolo_model.input: image_data,
            detector.input_image_size: [image.size[1], image.size[0]],
            }
        )
    box_ndarray = box_ndarray[:, [1,0,3,2]]
    return box_ndarray, classId_ndarray, score_ndarray

# 获取请求中的参数字典
from urllib.parse import unquote
def get_dataDict(data):
    data_dict = {}
    for text in data.split('&'):
        key, value = text.split('=')
        value_1 = unquote(value)
        data_dict[key] = value_1
    return data_dict

# 网络请求'/'的回调函数
@server.route('/')
def index():
    htmlFileName = '_14_yolov3_3.html'
    htmlFileContent = render_template(htmlFileName)
    return htmlFileContent

# 网络请求'/get_detectedResult'的回调函数
@server.route('/get_detectionResult', methods=['POST'])
def anyname_you_like():
    startTime = time.time()
    data_bytes = request.get_data()
    data = data_bytes.decode('utf-8')
    data_dict = get_dataDict(data)
    if 'image_base64_string' in data_dict:
        # 保存接收的图片到指定文件夹
        received_dirPath = '../resources/received_images'
        if not os.path.isdir(received_dirPath):
            os.makedirs(received_dirPath)
        timeString = get_timeString()
        imageFileName = timeString + '.jpg'
        imageFilePath = os.path.join(received_dirPath, imageFileName)
        try:
            image_base64_string = data_dict['image_base64_string']
            image_base64_bytes = image_base64_string.encode('utf-8')
            image_bytes = base64.b64decode(image_base64_bytes)
            with open(imageFilePath, 'wb') as file:
                file.write(image_bytes)
            print('接收图片文件保存到此路径：%s' %imageFilePath)
            usedTime = time.time() - startTime
            print('接收图片并保存，总共耗时%.2f秒' %usedTime)
            # 通过图片路径读取图像数据，并对图像数据做目标检测
            startTime = time.time()
            image = Image.open(imageFilePath)
            box_ndarray, classId_ndarray, score_ndarray = get_detectResult(image)
            usedTime = time.time() - startTime
            print('打开接收的图片文件并做目标检测，总共耗时%.2f秒\n' %usedTime)
            # 把目标检测结果图保存在服务端指定路径
            drawed_image = get_drawedImage(image, box_ndarray, classId_ndarray, score_ndarray)
            drawed_imageFileName = 'drawed_' + imageFileName
            drawed_imageFilePath = os.path.join(received_dirPath, drawed_imageFileName)
            drawed_image.save(drawed_imageFilePath)
            # 把目标检测结果转化为json格式的字符串
            json_dict = {
                'box_list' : box_ndarray.astype('int').tolist(),
                'classId_list' : classId_ndarray.tolist(),
                'score_list' : score_ndarray.tolist()
                }
            return jsonify(**json_dict)
        except Exception as e:
            print(e)

if __name__ == '__main__':
    server.run('127.0.0.1', port=5000)


text:
import torch.multiprocessing as mp
import os
import cv2
import numpy as np
import torch
import time
import sys
sys.path.append("..")

from loguru import logger
from easydict import EasyDict as edict
from alphapose.utils.transforms import heatmap_to_coord_simple
from alphapose.utils.pPose_nms import pose_nms

from multiprocessing.synchronize import Event as EventType

from actRec.F import single_normalize_min_
from actRec import models
from actRec.models import get_model
EVAL_JOINTS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
class classifier():
    def __init__(self,cfg,opt,imgfn=None):
        # self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.opt = opt
        self.cfg = cfg
        self.imgfn = imgfn
        self.heatmap_to_coord = heatmap_to_coord_simple
        self.inqueue = mp.Queue(maxsize=opt.inqsize) #webcam queue= 2
        self.outqueue = mp.Queue(maxsize=opt.outqsize)
        self.model = None
        self.loadedEvent = mp.Event()
        self.holder = edict()
        self.localvis=opt.localvis
        #

    def step(self, boxes, scores, ids, hm_data, cropped_boxes, orig_img, im_name):
        # if not self.running():
        #     logger.info('classifier is no running')
        #     return
        self.wait_and_put(self.inqueue, (boxes, scores, ids, hm_data, cropped_boxes, orig_img, im_name))

    def __load_model(self):
        self.model = get_model(self.cfg.MODEL,self.cfg.tagI2W)
        logger.info(self.model)
        ckpt = torch.load(self.cfg.CHEKPT,map_location=self.opt.device)
        self.model.load_state_dict(ckpt['model_state_dict'])
        logger.info('epoch:{%.4f}' % ckpt['epoch'])
        logger.info('loss:{%.4f}' % ckpt['loss'])
        if len(self.opt.gpus) > 1:
            self.model = torch.nn.DataParallel(self.model
                , device_ids=self.opt.gpus).to(self.opt.device)
        else:
            self.model.to(self.opt.device)
        self.model.eval()

    def __output(self,img,out,result):
        if self.localvis:
            self.showimg(img, out)
        else:
            self.wait_and_put(self.outqueue,(img,out,result))

    def read(self,timeout=None):
        # if(self.outqueue.empty()):
            # assert self.running()
        return self.outqueue.get(timeout=timeout)

    def work(self):
        logger.info('Classifier Process (%s)' % os.getpid())
        if(self.model is None):self.__load_model() #加载模型
        self.loadedEvent.set() #模型加载完成
        self.model.exe_pre(self.opt.device,self.holder) #模型初始化
        if(isinstance(self.startEvent,EventType)):self.startEvent.wait() #等待开始事件
        while True:
            with torch.no_grad():
                (boxes, scores, ids, hm_data, cropped_boxes, orig_img, im_name) = self.inqueue.get()
                if orig_img is None: #输入为空返回
                    self.clear_queues()
                    self.__output(None, None,None)
                    return
                orig_img = np.array(orig_img, dtype=np.uint8)[:, :, ::-1]  #改变图像通道排序 image channel RGB->BGR

                if boxes is None: #目标检测与姿态估计结果为空 直接输出原图像
                    self.__output(orig_img, None,None)
                else:
                    # location prediction (n, kp, 2) | score prediction (n, kp, 1)
                    pred = hm_data.cpu().data.numpy()
                    assert pred.ndim == 4
                    #姿态估计结果预处理
                    pose_coords = []
                    pose_scores = []
                    for i in range(hm_data.shape[0]):
                        bbox = cropped_boxes[i].tolist()
                        pose_coord, pose_score = self.heatmap_to_coord(pred[i][EVAL_JOINTS], bbox)
                        pose_coords.append(torch.from_numpy(pose_coord).unsqueeze(0))
                        pose_scores.append(torch.from_numpy(pose_score).unsqueeze(0))
                    preds_img = torch.cat(pose_coords)
                    preds_scores = torch.cat(pose_scores)
                    result_orig = pose_nms(boxes, scores, ids, preds_img, preds_scores, self.opt.min_box_area)

                    #在图像上可视化目标检测框/姿态估计点
                    result = {'imgname': im_name,'result': result_orig}
                    # from alphapose.utils.vis import vis_frame_fast as vis_frame #todo
                    from actRec.F import vis_frame_fast as vis_frame
                    img = vis_frame(orig_img, result, add_bbox=True)

                    if(len(result_orig)<=0):#姿态估计结果为空
                        self.__output(orig_img,None,None)
                        continue

                    #对图像中每个人体进行行为识别
                    out=[]
                    for i in range(len(result_orig)):
                        points = result_orig[i]['keypoints'].numpy()
                        points = single_normalize_min_(points)
                        points = points.reshape(1,34)
                        actres = self.model.exe(points,self.opt.device,self.holder)
                        # out.append(actres)
                        out.append(actres.cpu().numpy().reshape(-1))

                    self.__output(img,out,result)
        self.clear_queues() #结束子进程

    def showimg(self,img,out):
        if self.imgfn is not None:
            img = self.imgfn(img,out,self.cfg.tagI2W)
            if img is None:
                logger.error('img is None,can cause by video stream interrupted')
            cv2.imshow("AlphaPose Demo",img)
            k = cv2.waitKey(30)

    def start_worker(self, target):
        p = mp.Process(target=target,name='ActClassifier', args=())
        p.start()
        return p

    def start(self,startEvent=None):
        self.startEvent = startEvent
        self.result_worker = self.start_worker(self.work)
        return self

    def running(self):
        # indicate that the thread is still running
        time.sleep(0.2)
        # return False
        return not self.inqueue.empty()

    def stop(self):
        if(self.result_worker.exitcode is not None):
            return
        self.clear_queues()
        self.step(None,None,None,None,None,None,None)
        self.result_worker.join(self.opt.timeout)
        if(self.result_worker.exitcode is None):
            self.result_worker.terminate()
            logger.info('classifier killed')
        else:
            logger.info('classifier stoped')
        cv2.destroyAllWindows()

    def kill(self):
        # self.clear_queues()
        self.result_worker.terminate()

    def wait_and_put(self, queue, item):
        # if(self.result_worker.exitcode is None):
        queue.put(item)
        queue.get() if self.opt.realtime and queue.qsize()>1 else time.sleep(0.01)
        # queue.get() if queue.qsize()>1 else time.sleep(0.01)

    def clear_queues(self):
        self.clear(self.inqueue)

    def clear(self, queue):
        while not queue.empty():
            queue.get_nowait()

    def count(self):
        return self.outqueue.qsize()


text:
import pandas as pd
import numpy as np
import time

start_time=time.time()
print('start_time:',start_time)
'''
Load Data
'''
IDIR = 'G:\\bigdata\\badou\\00-data//'
priors = pd.read_csv(IDIR + 'order_products__prior.csv', dtype={
    'order_id': np.int32,
    'product_id': np.uint16,
    'add_to_cart_order': np.int16,
    'reordered': np.int8})
# 用户倒数第二天购买的商品
train = pd.read_csv(IDIR + 'order_products__train.csv', dtype={
    'order_id': np.int32,
    'product_id': np.uint16,
    'add_to_cart_order': np.int16,
    'reordered': np.int8})
orders = pd.read_csv(IDIR + 'orders.csv', dtype={
    'order_id': np.int32,
    'user_id': np.int32,
    'eval_set': 'object',
    'order_number': np.int16,
    'order_dow': np.int8,
    'order_hour_of_day': np.int8,
    'days_since_prior_order': np.float32})
products = pd.read_csv(IDIR + 'products.csv', dtype={
    'product_id': np.uint16,
    'order_id': np.int32,
    'aisle_id': np.uint8,
    'department_id': np.uint8},
                       usecols=['product_id', 'aisle_id', 'department_id'])

print('priors {}: {}'.format(priors.shape, ', '.join(priors.columns)))
print('orders {}: {}'.format(orders.shape, ', '.join(orders.columns)))
print('train {}: {}'.format(train.shape, ', '.join(train.columns)))

'''
Feature Process
'''
# 1. product feature
print('computing product f')
prods = pd.DataFrame()
# 产品销量
prods['orders'] = priors.groupby(priors.product_id).size().astype(np.int32)
# 产品再次购买量
prods['reorders'] = priors['reordered'].groupby(priors.product_id).sum().astype(np.float32)
# 产品再次购买比例
prods['reorder_rate'] = (prods.reorders / prods.orders).astype(np.float32)
products = products.join(prods, on='product_id')
products.set_index('product_id', drop=False, inplace=True)
del prods

# 2.加入orders info到训练集priors
print('add order info to priors')
# 以orders的order_id为主key
orders.set_index('order_id', inplace=True, drop=False)
# 将所有order信息关联到priors中 rsuffix表示相同列名合并后右表的列名追加_
priors = priors.join(orders, on='order_id', rsuffix='_')
priors.drop('order_id_', inplace=True, axis=1)

# 3. user feature
print('computing user f')
usr = pd.DataFrame()
# 每个用户平均订单间隔时间 numpy
usr['average_days_between_orders'] = orders.groupby('user_id')['days_since_prior_order'].mean().astype(np.float32)
# 用户订单数量
usr['nb_orders'] = orders.groupby('user_id').size().astype(np.int16)

users = pd.DataFrame()
# 用户购买商品数量
users['total_items'] = priors.groupby('user_id').size().astype(np.int16)
# 用户购买商品去重（set）集合
users['all_products'] = priors.groupby('user_id')['product_id'].apply(set)
# 用户去重商品数量
users['total_distinct_items'] = (users.all_products.map(len)).astype(np.int16)
# 默认使用索引连接
users = users.join(usr)
del usr
# 用户平均一个订单的商品数量
users['average_basket'] = (users.total_items / users.nb_orders).astype(np.float32)
print('user f', users.shape)

# 4. userXproduct features
print('compute userXproduct f - this is long...')
# user_id+product_id的组合key
priors['user_product'] = priors.product_id + priors.user_id * 100000
print('priors:',priors[:10])
d = dict()
for row in priors.itertuples():
    z = row.user_product
    #组合key不在字典d中初始化
    if z not in d:
        # 1：user_product cnt，
        # 2：获取最近一个订单（订单顺序（最后一个订单），订单id（且订单id最大）），
        # 3：购物车位置累加
        d[z] = (1,
                (row.order_number, row.order_id),
                row.add_to_cart_order)
    else:
        d[z] = (d[z][0] + 1,
                # tuple比较时 返回第一个元素位置的最大tuple
                max(d[z][1], (row.order_number, row.order_id)),
                d[z][2] + row.add_to_cart_order)

print('to dataframe (less memory)')
print('mid_time:',time.time()-start_time)
# 将dict转dataframe
userXproduct = pd.DataFrame.from_dict(d, orient='index')
del d
# 设置列名
userXproduct.columns = ['nb_orders', 'last_order_id', 'sum_pos_in_cart']
# 列类型转换
userXproduct.nb_orders = userXproduct.nb_orders.astype(np.int16)
userXproduct.last_order_id = userXproduct.last_order_id.map(lambda x: x[1]).astype(np.int32)
userXproduct.sum_pos_in_cart = userXproduct.sum_pos_in_cart.astype(np.int16)
print('user X product f', len(userXproduct))
print('uXp:',userXproduct[:10])
del priors

# 5. 从orders中划分训练集和测试集
print('split orders : train, test')
# 用户最后一天购买的订单
# test_orders = orders[orders.eval_set == 'test']
# 用户倒数第二天购买的订单（近期的购买数据）
train_orders = orders[orders.eval_set == 'train']
# train数据以(order_id,product_id)为key
train.set_index(['order_id', 'product_id'], inplace=True, drop=False)

def features(selected_orders, labels_given=False):
    print('build candidate list')
    order_list = []
    product_list = []
    labels = []
    i = 0
    for row in selected_orders.itertuples():
        i += 1
        if i % 10000 == 0: print('order row', i)
        order_id = row.order_id
        user_id = row.user_id
        # user_id的不重复商品集合
        user_products = users.all_products[user_id]
        # 产品list，即order_id的候选集
        product_list += user_products
        # 每个product对应当前的order_id,即pair(product_id,order_id)
        order_list += [order_id] * len(user_products)
        # 如果给label，即为train，此时label：如果pair（order_id,product_id）在train中为1，否则为0
        # train中给定的是对应订单，购买的产品。
        if labels_given:
            labels += [(order_id, product) in train.index for product in user_products]

    df = pd.DataFrame({'order_id': order_list, 'product_id': product_list}, dtype=np.int32)
    labels = np.array(labels, dtype=np.int8)
    del order_list
    del product_list

    print('user related features')
    df['user_id'] = df.order_id.map(orders.user_id)
    df['user_total_orders'] = df.user_id.map(users.nb_orders)
    df['user_total_items'] = df.user_id.map(users.total_items)
    df['total_distinct_items'] = df.user_id.map(users.total_distinct_items)
    df['user_average_days_between_orders'] = df.user_id.map(users.average_days_between_orders)
    df['user_average_basket'] = df.user_id.map(users.average_basket)

    print('order related features')
    df['order_hour_of_day'] = df.order_id.map(orders.order_hour_of_day)
    df['days_since_prior_order'] = df.order_id.map(orders.days_since_prior_order)
    df['days_since_ratio'] = df.days_since_prior_order / df.user_average_days_between_orders

    print('product related features')
    df['aisle_id'] = df.product_id.map(products.aisle_id)
    df['department_id'] = df.product_id.map(products.department_id)
    df['product_orders'] = df.product_id.map(products.orders).astype(np.int32)
    df['product_reorders'] = df.product_id.map(products.reorders)
    df['product_reorder_rate'] = df.product_id.map(products.reorder_rate)

    print('user_X_product related features')  # 10/13
    df['z'] = df.user_id * 100000 + df.product_id
    df.drop(['user_id'], axis=1, inplace=True)
    # 共同订单数量
    df['UP_orders'] = df.z.map(userXproduct.nb_orders)
    # 共同订单数量在用户所有订单中的占比
    df['UP_orders_ratio'] = (df.UP_orders / df.user_total_orders).astype(np.float32)
    # 共同最后一个订单
    df['UP_last_order_id'] = df.z.map(userXproduct.last_order_id)
    # 物品在该用户订单中的平均位置
    df['UP_average_pos_in_cart'] = (df.z.map(userXproduct.sum_pos_in_cart) / df.UP_orders).astype(np.float32)
    # 最后一次购买这个物品在倒数第几个订单  [1,1,1,0,1,0,1,0,0]
    df['UP_orders_since_last'] = df.user_total_orders - df.UP_last_order_id.map(orders.order_number)
    # 当前订单与最后订单时间差异（hour）
    df['UP_delta_hour_vs_last'] = abs(df.order_hour_of_day - df.UP_last_order_id.map(orders.order_hour_of_day)).map(
        lambda x: min(x, 24 - x)).astype(np.int8)

    df.drop(['UP_last_order_id', 'z'], axis=1, inplace=True)
    print(df.dtypes)
    print(df.memory_usage())
    return df, labels

f_to_use = ['user_total_orders', 'user_total_items', 'total_distinct_items',
            'user_average_days_between_orders', 'user_average_basket',
            'order_hour_of_day', 'days_since_prior_order', 'days_since_ratio',
            'aisle_id', 'department_id', 'product_orders', 'product_reorders',
            'product_reorder_rate', 'UP_orders', 'UP_orders_ratio',
            'UP_average_pos_in_cart', 'UP_orders_since_last',
            'UP_delta_hour_vs_last']

df_train, labels = features(train_orders, labels_given=True)
print('Train Columns:',df_train.columns)
df_train[f_to_use].to_csv(IDIR+'train_feat.csv',index=False)
np.save(IDIR+'labels.npy',labels)

print('end_time:',time.time()-start_time)


text:
#Eulers approximation of Capybara's getting eaten by jaguars. Nom nom nom.
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np

h=0.01
alpha=0.8
beta=0.05
gamma=0.85
delta=0.02

def main():
    xold=100
    yold=30
    told=0
    capylist=[]
    jaglist=[]
    tlist=[]
    tlist.append(told)
    capylist.append(xold)
    jaglist.append(yold)
    for t in range(0,int(24/h)):
        x=h*(xold*(alpha-beta*yold))+xold
        y=h*(yold*(delta*xold-gamma))+yold
        t=told+h
        capylist.append(x)
        xold=x
        jaglist.append(y)
        yold=y
        tlist.append(t)
        told=t
    #Plot of Jaguars pop vs time and Capybaras pop vs. time. Blue is jaguars, orange is capybaras
    plt.plot(tlist,jaglist)
    plt.plot(tlist,capylist)
    plt.grid("True")
    plt.xlabel("t (Months)")
    plt.ylabel("Capybaras (Orange) & Jaguars (Blue)")
    plt.show()
    plt.close()

    #Plot of Jaguars vs capybaras
    plt.plot(capylist,jaglist)
    plt.grid("True")
    plt.xlabel("Capybaras")
    plt.ylabel("Jaguars")
    plt.show()
    plt.close()
    #First two values
    print("Capybara first two values:", capylist[1:3])
    print("Jaguar first two values:", jaglist[1:3])
main()

'''

Run this for the graphs of time vs. capylist and jaglist

In the time vs. Capybara and jaguar pop graph, the capybara population initally decreases as the jaguars eat them, then the jaguar population decreases due to the lack of capybaras. This results in the capybara population increasing. This occurs periodically, and both populations increase in every time period.

In the capybaras vs. jaguars graph, you can see a stable ecosystem, which would increase in total population over time.

'''

max_stars_repo_path: Project 1 yo.py

max_issues_repo_path: Project 1 yo.py

max_forks_repo_path: Project 1 yo.py


text:
import numpy as np
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import datetime as dt
import warnings
warnings.filterwarnings('ignore')
from cartpole.pgrl import PG
from cartpole.dynamics import CartPole
from cartpole.pltlib import PLTLIB

# ----------locations for saving data ----------------------
STORE_PATH = '/tmp/cartpole_exp1/PG'
data_path = STORE_PATH + f"/data_{dt.datetime.now().strftime('%d%m%Y%H%M')}"
agent_path = STORE_PATH + f"/agent_{dt.datetime.now().strftime('%d%m%Y%H%M')}"
train_writer = tf.summary.create_file_writer(data_path)

# ---------- environment -------------------------------------
Rand_Seed = 1
env_par = {
    'Rand_Seed': Rand_Seed,
    'STORE_PATH': STORE_PATH,
    'monitor': False,
    'threshold': 195.0
}
Rand_Seed = 1
CP = CartPole(env_par)

# ---------- agent --------------------------------------------
# hyper parameters
agent_par = {
    'num_state': CP.env.observation_space.shape[0],
    'num_actions': CP.env.action_space.n,
    'Rand_Seed': Rand_Seed,
    'hidden_size': 30,
    'GAMMA': 1.0,
    'num_episodes': 1000,
    'learning_rate_adam': 0.001,
    'adam_eps': 1e-7,
}
policy = PG(agent_par)

# Running the algorithm for a maximum number of iteration until it is solved
tot_rews = []
mean_100ep = 0
for episode in range(agent_par['num_episodes']):

    # Do one rollout
    states, actions, rewards, _, _ = CP.one_rollout(policy)

    # Update the network
    loss = policy.update_network(states, actions, rewards)

    # Check if the problem is solved
    if episode > 100:
        mean_100ep = np.mean(tot_rews[-101:-1])

    tot_reward = sum(rewards)
    tot_rews.append(tot_reward)
    print(f"Episode: {episode}, Reward: {tot_reward}, Mean of 100 cons episodes: {mean_100ep}")
    if mean_100ep > env_par['threshold']:
        print(f"Problem is solved.")
        policy.network.save(agent_path)
        break

    # Save data
    with train_writer.as_default():
        tf.summary.scalar('reward', tot_reward, step=episode)

# Close the environment
CP.env.close()

# Print the results if the problem is solved
if mean_100ep > env_par['threshold']:
    # Print the summary of the solution
    print(f"\n\nProblem is solved after {episode} Episode with the mean reward {mean_100ep} over the last 100 episodes")

    # Plot the result
    MyPlot = PLTLIB()
    MyPlot.reward_it(tot_rews)


text:
"""Tests transforms module

"""

import numpy as np
import pandas as pd

from dsutils.transforms import Scaler
from dsutils.transforms import Imputer

# TODO: quantile_transform

def test_transforms_scaler():
    """Tests dsutils.transforms.Scaler"""

    df = pd.DataFrame()
    df['a'] = [1, 2, 3, 4, 5]
    df['b'] = [1, 2, 3, 4, 5]
    df['c'] = [10, 20, 30, 40, 50]

    out = Scaler().fit_transform(df)

    assert isinstance(out, pd.DataFrame)
    assert out.shape[0] == 5
    assert out.shape[1] == 3
    assert out.iloc[2, 0] == 0.0
    assert out.iloc[0, 0] < out.iloc[1, 0]
    assert out.iloc[1, 0] < out.iloc[2, 0]
    assert out.iloc[2, 0] < out.iloc[3, 0]
    assert out.iloc[3, 0] < out.iloc[4, 0]
    assert out.iloc[2, 1] == 0.0
    assert out.iloc[2, 2] == 0.0
    assert out.iloc[0, 2] < out.iloc[1, 2]
    assert out.iloc[1, 2] < out.iloc[2, 2]
    assert out.iloc[2, 2] < out.iloc[3, 2]
    assert out.iloc[3, 2] < out.iloc[4, 2]

def test_transform_imputer():
    """Tests dsutils.transforms.Imputer"""

    df = pd.DataFrame()
    df['a'] = [np.nan, np.nan, 3, 4, 5]
    df['b'] = [1, np.nan, 3, np.nan, 5]
    df['c'] = [10, 20, 30, np.nan, np.nan]

    # Default should be to use median
    out = Imputer().fit_transform(df)
    assert isinstance(out, pd.DataFrame)
    assert out.shape[0] == 5
    assert out.shape[1] == 3
    assert np.count_nonzero(out.isnull()) == 0
    assert out.iloc[0, 0] == 4
    assert out.iloc[1, 0] == 4
    assert out.iloc[2, 0] == 3
    assert out.iloc[0, 1] == 1
    assert out.iloc[1, 1] == 3
    assert out.iloc[2, 1] == 3
    assert out.iloc[3, 1] == 3
    assert out.iloc[0, 2] == 10
    assert out.iloc[3, 2] == 20
    assert out.iloc[4, 2] == 20

    df = pd.DataFrame()
    df['a'] = [2, np.nan, 3, 4, 5]
    df['b'] = [1, np.nan, 3, np.nan, 5]
    df['c'] = [10, 20, 30, np.nan, 40]

    # Using mean
    out = Imputer(method='mean').fit_transform(df)
    assert isinstance(out, pd.DataFrame)
    assert out.shape[0] == 5
    assert out.shape[1] == 3
    assert np.count_nonzero(out.isnull()) == 0
    assert out.iloc[0, 0] == 2
    assert out.iloc[1, 0] == 3.5
    assert out.iloc[2, 0] == 3
    assert out.iloc[3, 0] == 4
    assert out.iloc[4, 0] == 5
    assert out.iloc[0, 1] == 1
    assert out.iloc[1, 1] == 3
    assert out.iloc[2, 1] == 3
    assert out.iloc[3, 1] == 3
    assert out.iloc[4, 1] == 5
    assert out.iloc[0, 2] == 10
    assert out.iloc[1, 2] == 20
    assert out.iloc[2, 2] == 30
    assert out.iloc[3, 2] == 25
    assert out.iloc[4, 2] == 40

max_stars_repo_name: brendanhasz/dsutils

max_issues_repo_name: brendanhasz/dsutils

max_forks_repo_name: brendanhasz/dsutils


text:
# -*- coding: utf-8 -*-

import numpy as np
from pytest import raises
from vispy import scene

from vispy.testing import (TestingCanvas, requires_application,
                           run_tests_if_main, requires_pyopengl)
from vispy.testing.image_tester import assert_image_approved

@requires_pyopengl()
def test_volume():

    vol = np.zeros((20, 20, 20), 'float32')
    vol[8:16, 8:16, :] = 1.0

    # Create
    V = scene.visuals.Volume(vol)
    assert V.clim == (0, 1)
    assert V.method == 'mip'

    # Set wrong data
    with raises(ValueError):
        V.set_data(np.zeros((20, 20), 'float32'))

    # Clim
    V.set_data(vol, (0.5, 0.8))
    assert V.clim == (0.5, 0.8)
    with raises(ValueError):
        V.set_data(vol, (0.5, 0.8, 1.0))

    # Method
    V.method = 'iso'
    assert V.method == 'iso'

    # Step size
    V.relative_step_size = 1.1
    assert V.relative_step_size == 1.1
    # Disallow 0 step size to avoid GPU stalling
    with raises(ValueError):
        V.relative_step_size = 0

@requires_pyopengl()
@requires_application()
def test_volume_draw():
    with TestingCanvas(bgcolor='k', size=(100, 100)) as c:
        v = c.central_widget.add_view()
        v.camera = 'turntable'
        v.camera.fov = 70
        # Create
        np.random.seed(2376)
        vol = np.random.normal(size=(20, 20, 20), loc=0.5, scale=0.2)
        vol[8:16, 8:16, :] += 1.0
        V = scene.visuals.Volume(vol, parent=v.scene)  # noqa
        v.camera.set_range()
        assert_image_approved(c.render(), 'visuals/volume.png')

run_tests_if_main()

max_stars_repo_name: ringw/vispy

max_issues_repo_name: ringw/vispy

max_forks_repo_name: ringw/vispy