text:
# Models
import tensorflow as tf
import numpy as np
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2_as_graph
import time
import pickle
import os
import sklearn
from sklearn.ensemble import RandomForestRegressor
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import warnings
warnings.filterwarnings("ignore")
def create_fc(config):
    config['hidden_layers'] = len(config['layers'])
    input = tf.keras.layers.Input(shape=config['input_shape'])
    if config['input_dropout'] is not None:
        x = tf.keras.layers.Dropout(config['input_dropout'])(input)
    else:
        x = input
    for i in range(config['hidden_layers']):
        dim = config['layers'][i]
        act = 'relu'
        x = tf.keras.layers.Dense(dim,activation=act)(x)
        if config['dropout'] is not None:
            x = tf.keras.layers.Dropout(config['dropout'])(x)

        x = tf.keras.layers.BatchNormalization()(x)

    x = tf.keras.layers.Dense(config['output_shape'],activation='softmax')(x)

    model = tf.keras.Model(inputs=input, outputs=x)
    model.compile(loss=tf.keras.losses.categorical_crossentropy,
                                optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9),
                                metrics=['accuracy'])
    return model

def vgg_block(x, filters, layers):
    for _ in range(layers):
        x = tf.keras.layers.Conv2D(filters, (3,3), padding='same', activation='relu')(x)
        x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.MaxPooling2D((2,2), strides=(2,2))(x)
    return x

def create_vgg(config):
    config['num_layers'] = len(config['vgg_layers'])
    input = tf.keras.layers.Input(shape=config['input_shape'])
    x = input
    for i in range(config['num_layers']):
        block_size = config['vgg_layers'][i]
        filter_num = config['filters'][i]
        act = 'relu'
        x = vgg_block(x,filter_num,block_size)
    x = tf.keras.layers.Flatten()(x)
    config['num_hidden_layers'] = len(config['hidden_layers'])
    for i in range(config['num_hidden_layers']):
        dim = config['hidden_layers'][i]
        act = 'relu'
        x = tf.keras.layers.Dense(dim,activation=act)(x)

    x = tf.keras.layers.Dense(config['output_shape'],activation='softmax')(x)

    model = tf.keras.Model(inputs=input, outputs=x)
    model.compile(loss=tf.keras.losses.categorical_crossentropy,
                                optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9),
                                metrics=['accuracy'])
    return model

def inception_block(x, f1, f2_in, f2_out, f3_in, f3_out, f4_out):
    # 1x1 conv
    conv1 = tf.keras.layers.Conv2D(f1, (1,1), padding='same', activation='relu')(x)
    conv1 = tf.keras.layers.BatchNormalization()(conv1)
    # 3x3 conv
    conv3 = tf.keras.layers.Conv2D(f2_in, (1,1), padding='same', activation='relu')(x)
    conv3 = tf.keras.layers.BatchNormalization()(conv3)
    conv3 = tf.keras.layers.Conv2D(f2_out, (3,3), padding='same', activation='relu')(conv3)
    conv3 = tf.keras.layers.BatchNormalization()(conv3)
    # 5x5 conv
    conv5 = tf.keras.layers.Conv2D(f3_in, (1,1), padding='same', activation='relu')(x)
    conv5 = tf.keras.layers.BatchNormalization()(conv5)
    conv5 = tf.keras.layers.Conv2D(f3_out, (5,5), padding='same', activation='relu')(conv5)
    conv5 = tf.keras.layers.BatchNormalization()(conv5)
    # 3x3 max pooling
    pool = tf.keras.layers.MaxPooling2D((3,3), strides=(1,1), padding='same')(x)
    pool = tf.keras.layers.Conv2D(f4_out, (1,1), padding='same', activation='relu')(pool)
    pool = tf.keras.layers.BatchNormalization()(pool)
    # concatenate filters, assumes filters/channels last
    layer_out = tf.keras.layers.concatenate([conv1, conv3, conv5, pool], axis=-1)
    return layer_out

def create_inception(config):
    config['num_layers'] = len(config['inception_layers'])
    input = tf.keras.layers.Input(shape=config['input_shape'])
    x = tf.keras.layers.Conv2D(64, (7,7), padding='valid', activation='relu', strides=(2,2))(input)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.MaxPooling2D((3,3), strides=(2,2), padding='same')(x)

    x = tf.keras.layers.Conv2D(128, (1,1), padding='same', activation='relu', strides=(1,1))(x)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.Conv2D(192, (3,3), padding='same', activation='relu', strides=(1,1))(x)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.MaxPooling2D((3,3), strides=(2,2), padding='same')(x)

    for i in range(config['num_layers']):
        for j in range(config['inception_layers'][i]):
            x = inception_block(x,config['f1'][i][j],config['f2_in'][i][j],config['f2_out'][i][j],
                                                    config['f3_in'][i][j],config['f3_out'][i][j],config['f4_out'][i][j])
        x = tf.keras.layers.MaxPooling2D((3,3), strides=(2,2), padding='same')(x)
    x = tf.keras.layers.Flatten()(x)
    config['num_hidden_layers'] = len(config['hidden_layers'])
    for i in range(config['num_hidden_layers']):
        dim = config['hidden_layers'][i]
        act = 'relu'
        x = tf.keras.layers.Dense(dim,activation=act)(x)

    x = tf.keras.layers.Dense(config['output_shape'],activation='softmax')(x)

    model = tf.keras.Model(inputs=input, outputs=x)
    model.compile(loss=tf.keras.losses.categorical_crossentropy,
                                optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9),
                                metrics=['accuracy'])
    return model

def conv_relu(x, filters, kernel_size, strides=1):

        x = tf.keras.layers.Conv2D(filters=filters, kernel_size=kernel_size, strides=strides, padding = 'same')(x)
        x = tf.keras.layers.ReLU()(x)
        x = tf.keras.layers.BatchNormalization()(x)
        return x

def identity_block(tensor, filters):

        x = conv_relu(tensor, filters=filters, kernel_size=1, strides=1)
        x = conv_relu(x, filters=filters, kernel_size=3, strides=1)
        x = tf.keras.layers.Conv2D(filters=4*filters, kernel_size=1, strides=1)(x)
        x = tf.keras.layers.BatchNormalization()(x)

        x = tf.keras.layers.Add()([tensor,x])
        x = tf.keras.layers.ReLU()(x)

        return x

def identity_block_small(tensor, filters):

        x = conv_relu(tensor, filters=filters, kernel_size=3, strides=1)
        x = conv_relu(x, filters=filters, kernel_size=3, strides=1)

        x = tf.keras.layers.Add()([tensor,x])
        x = tf.keras.layers.ReLU()(x)

        return x

def projection_block(tensor, filters, strides):

        x = conv_relu(tensor, filters=filters, kernel_size=1, strides=strides)
        x = conv_relu(x, filters=filters, kernel_size=3, strides=1)
        x = tf.keras.layers.Conv2D(filters=4*filters, kernel_size=1, strides=1)(x)
        x = tf.keras.layers.BatchNormalization()(x)

        shortcut = tf.keras.layers.Conv2D(filters=4*filters, kernel_size=1, strides=strides)(tensor)
        shortcut = tf.keras.layers.BatchNormalization()(shortcut)

        x = tf.keras.layers.Add()([shortcut,x])
        x = tf.keras.layers.ReLU()(x)

        return x

def projection_block_small(tensor, filters, strides):

        x = conv_relu(tensor, filters=filters, kernel_size=3, strides=strides)
        x = conv_relu(x, filters=filters, kernel_size=3, strides=1)

        shortcut = tf.keras.layers.Conv2D(filters=filters, kernel_size=1, strides=strides)(tensor)
        shortcut = tf.keras.layers.BatchNormalization()(shortcut)

        x = tf.keras.layers.Add()([shortcut,x])
        x = tf.keras.layers.ReLU()(x)

        return x

def resnet_block(x, filters, reps, strides):

        x = projection_block(x, filters, strides)
        for _ in range(reps-1):
                x = identity_block(x,filters)

        return x

def resnet_block_small(x, filters, reps, strides):

        x = projection_block_small(x, filters, strides)
        for _ in range(reps):
                x = identity_block_small(x,filters)

        return x

def create_resnet(config):

    input = tf.keras.layers.Input(shape=config['input_shape'])

    x = conv_relu(input, filters=64, kernel_size=7, strides=2)
    x = tf.keras.layers.MaxPool2D(pool_size = 3, strides =2)(x)
    if config['small']==False:
            x = resnet_block(x, filters=64, reps=config['resnet_layers'][0], strides=1)
            x = resnet_block(x, filters=128, reps=config['resnet_layers'][1], strides=2)
            x = resnet_block(x, filters=256, reps=config['resnet_layers'][2], strides=2)
            x = resnet_block(x, filters=512, reps=config['resnet_layers'][3], strides=2)
    else:
            x = resnet_block_small(x, filters=64, reps=config['resnet_layers'][0], strides=1)
            x = resnet_block_small(x, filters=128, reps=config['resnet_layers'][1], strides=2)
            x = resnet_block_small(x, filters=256, reps=config['resnet_layers'][2], strides=2)
            x = resnet_block_small(x, filters=512, reps=config['resnet_layers'][3], strides=2)
    x = tf.keras.layers.GlobalAvgPool2D()(x)

    config['num_hidden_layers'] = len(config['hidden_layers'])
    for i in range(config['num_hidden_layers']):
        dim = config['hidden_layers'][i]
        act = 'relu'
        x = tf.keras.layers.Dense(dim,activation=act)(x)

    output = tf.keras.layers.Dense(config['output_shape'], activation ='softmax')(x)

    model = tf.keras.Model(inputs=input, outputs=output)
    model.compile(loss=tf.keras.losses.categorical_crossentropy,
                            optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9),
                            metrics=['accuracy'])
    return model

def create_base_vgg_config(params):
    config = dict()
    config['input_shape'] = (params['input_shape'],params['input_shape'],3)
    config['vgg_layers'] = [params['vgg_layers']] * params['vgg_layers_size']

    filters = []
    for i in range(params['vgg_layers_size']):
        filters.append(params['filters']*(2**i))
    config['filters'] = filters

    config['hidden_layers'] = [params['hidden_layers']] * params['hidden_layers_size']
    config['output_shape'] = params['output_shape']

    config['input_size'] = params['input_size']
    config['batch_size'] = params['batch_size']

    config['model'] = "VGG"
    return config

def create_base_inception_config(params):
    config = dict()
    config['input_shape'] = (params['input_shape'],params['input_shape'],3)
    config['inception_layers'] = [params['inception_layers']] * 3

    config['f1'] = []
    config['f2_in'] = []
    config['f2_out'] = []
    config['f3_in'] = []
    config['f3_out'] = []
    config['f4_out'] = []

    for val in config['inception_layers']:
        config['f1'].append([params['f1']]*val)
        config['f2_in'].append([params['f2_in']]*val)
        config['f2_out'].append([params['f2_out']]*val)
        config['f3_in'].append([params['f3_in']]*val)
        config['f3_out'].append([params['f3_out']]*val)
        config['f4_out'].append([params['f4_out']]*val)

    config['hidden_layers'] = [params['hidden_layers']] * params['hidden_layers_size']
    config['output_shape'] = params['output_shape']

    config['input_size'] = params['input_size']
    config['batch_size'] = params['batch_size']

    config['model'] = "Inception"
    return config

def create_base_resnet_config(params):
    config = dict()
    config['input_shape'] = (params['input_shape'],params['input_shape'],3)
    config['small'] = False
    config['resnet_layers'] = [params['resnet_layers']] * 4

    config['hidden_layers'] = [params['hidden_layers']] * params['hidden_layers_size']
    config['output_shape'] = params['output_shape']

    config['input_size'] = params['input_size']
    config['batch_size'] = params['batch_size']

    config['model'] = "ResNet"
    return config

def create_base_fc_config(params):
    config = dict()
    config['input_shape'] = params['input_shape']
    config['input_dropout'] = 0.2
    config['dropout'] = 0.5

    config['layers'] = [1000] * params['hidden_layers']
    config['output_shape'] = params['output_shape']

    config['input_size'] = params['input_size']
    config['batch_size'] = params['batch_size']

    config['model'] = "FC"
    return config

def get_flops(model, batch_size=None,allowed_flops=['MatMul', 'Mul', 'Rsqrt', 'BiasAdd', 'Sub', 'Softmax', 'Conv2D', 'MaxPool', 'Mean']):
    if batch_size is None:
        batch_size = 1

    real_model = tf.function(model).get_concrete_function(tf.TensorSpec([batch_size] + model.inputs[0].shape[1:], model.inputs[0].dtype))
    frozen_func, graph_def = convert_variables_to_constants_v2_as_graph(real_model)

    run_meta = tf.compat.v1.RunMetadata()
    opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()
    opts['output'] = 'none'
    flops = tf.compat.v1.profiler.profile(graph=frozen_func.graph,
                                            run_meta=run_meta, cmd='op', options=opts)

    ret_val = dict()
    for fl in allowed_flops:
        ret_val[fl] = 0
    f = flops.children
    while(len(f) > 0):
        if f[0].name in allowed_flops:
            ret_val[f[0].name] = f[0].total_float_ops
        f = f[0].children
    return ret_val

def get_weights(model):
    ret_val = dict()
    ret_val['trainable'] = np.sum([np.product([xi for xi in x.get_shape()]) for x in model.trainable_weights])
    ret_val['non_trainable'] = np.sum([np.product([xi for xi in x.get_shape()]) for x in model.non_trainable_weights])
    return ret_val

def get_layers(model):
    ret_val = dict()
    for l in model.layers:
        name = l.__class__.__name__
        if name in ret_val:
            ret_val[name] += 1
        else:
            ret_val[name] = 1
    return ret_val

allowed_flops = ['MatMul', 'Mul', 'Rsqrt', 'BiasAdd', 'Sub', 'Softmax', 'Conv2D', 'MaxPool', 'Mean']
def get_model_params(model,batch_size = 64,x_shape=[]):
    flops = get_flops(model)
    weights = get_weights(model)
    layers = get_layers(model)

    return flops,weights,layers

model_creator = dict()
model_creator['vgg'] = create_vgg
model_creator['resnet'] = create_resnet
model_creator['inception'] = create_inception
model_creator['fc'] = create_fc

base_param_creator = dict()
base_param_creator['vgg'] = create_base_vgg_config
base_param_creator['resnet'] = create_base_resnet_config
base_param_creator['inception'] = create_base_inception_config
base_param_creator['fc'] = create_base_fc_config

def get_single_train_data(output_config):
    x = []
    y = []
    x.append(list(output_config['flops_param'].values()) +
             list(output_config['layers_param'].values()) +
             list(output_config['weights_param'].values()))
    x[-1].append(output_config['input_size'])
    x[-1].append(output_config['batch_size'])
    x = np.asarray(x).astype('float64')
    return x

def get_training_time(config,model_name):
    input_config = base_param_creator[model_name](config)
    model = model_creator[model_name](input_config)
    flops,weights,layers = get_model_params(model)
    input_config['flops_param'] = flops
    input_config['weights_param'] = weights
    input_config['layers_param'] = layers

    with open('results/trained_model.pickle', 'rb') as handle:
                models = pickle.load(handle)

    x = get_single_train_data(input_config)

    time = models[model_name].predict(x)[0]
    return time * config['epochs']


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

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import codecs
import yaml
import numpy as np
import tensorflow as tf

from tensorflow import gfile
from seq2seq.data.vocab import create_vocabulary_lookup_table
from seq2seq.data import vocab, input_pipeline
from seq2seq.training import utils as training_utils
from seq2seq.test import utils as test_utils
from seq2seq.models import BasicSeq2Seq, AttentionSeq2Seq, CopyGenSeq2Seq

def t(file_path, default_value=None):
  x = create_vocabulary_lookup_table(file_path)
  print(x)
  return x

from seq2seq.graph_utils import templatemethod

class Scope(object):

  def __init__(self, share=True):
    self._share = share
    self._source_emb = None
    self._source_emb_scope = None
    self._target_emb = None
    self._target_emb_scope = None

  @property
  @templatemethod("source")
  def source_embedding(self):
    self._source_emb_scope = tf.get_variable_scope()
    self._source_emb = tf.get_variable("W", initializer=tf.constant_initializer(2), dtype=tf.int32, shape=[])
    return self._source_emb

  @property
  @templatemethod("target")
  def target_embedding(self):
    self._target_emb_scope = tf.get_variable_scope()
    if self._share:
      return self._source_emb
      with tf.variable_scope(self._source_emb_scope, reuse=True):
        self._target_emb = self.source_embedding
    else:
      self._target_emb = tf.get_variable("W", initializer=tf.constant_initializer(3), shape=[])
    return self._target_emb

@templatemethod("trial")
def trial(x):
    w = tf.get_variable('w', [])
    return tf.reduce_sum(x) * w

def my_trail(x, share_variable_name):
  var1 = tf.get_variable(share_variable_name, shape=[])
  return tf.reduce_sum(x) * var1

template_my = tf.make_template("template_my", my_trail, share_variable_name="my_v")

def test_scope():
  scope = Scope(True)
  s = scope.source_embedding
  t = scope.target_embedding
  print(s.name)
  print(t.name)
  print(scope._target_emb_scope.name)
  print(scope._source_emb_scope.name)

def test_trial():
  y = tf.placeholder(tf.float32, [None])
  z = tf.placeholder(tf.float32, [None])
  a_y = trial(y)
  a_z = trial(z)

  a_y_t = template_my(y)
  a_z_t = template_my(z)

  s = tf.InteractiveSession()
  tf.global_variables_initializer().run()
  print(tf.global_variables())
  print(a_y_t.eval(feed_dict={y: [1.1, 1.9]}
                   ))
  print(a_y.eval(feed_dict={y: [1.1, 1.9]}))
  print(a_z_t.eval(feed_dict={y: [1.1, 1.9]}))
  print(a_z.eval(feed_dict={z: [1.9, 1.1]}))

def get_vocab_list(vocab_path):
  lines = codecs.open(vocab_path, "r", "utf-8")
  vocab = []
  for line in lines:
    vocab.append(line.strip().split("\t")[0])
  return vocab

def test_copy_gen_model(source_path = None, target_path = None, vocab_path=None):

  tf.logging.set_verbosity(tf.logging.INFO)
  batch_size = 2
  input_depth = 4
  sequence_length = 10

  if vocab_path is None:
    # Create vocabulary
    vocab_list = [str(_) for _ in range(10)]
    vocab_list += ["笑う", "泣く", "了解", "はい", "＾＿＾"]
    vocab_size = len(vocab_list)
    vocab_file = test_utils.create_temporary_vocab_file(vocab_list)
    vocab_info = vocab.get_vocab_info(vocab_file.name)
    vocab_path = vocab_file.name
    tf.logging.info(vocab_file.name)
  else:
    vocab_info = vocab.get_vocab_info(vocab_path)
    vocab_list = get_vocab_list(vocab_path)

  extend_vocab = vocab_list + ["中国", "爱", "你"]

  tf.contrib.framework.get_or_create_global_step()
  source_len = sequence_length + 5
  target_len = sequence_length + 10
  source = " ".join(np.random.choice(extend_vocab, source_len))
  target = " ".join(np.random.choice(extend_vocab, target_len))

  is_tmp_file = False
  if source_path is None and target_path is None:
    is_tmp_file = True
    sources_file, targets_file = test_utils.create_temp_parallel_data(
      sources=[source], targets=[target])
    source_path = sources_file.name
    target_path = targets_file.name

  # Build model graph
  mode = tf.contrib.learn.ModeKeys.TRAIN
  params_ = CopyGenSeq2Seq.default_params().copy()
  params_.update({
      "vocab_source": vocab_path,
      "vocab_target": vocab_path,
    }
  )
  model = CopyGenSeq2Seq(params = params_, mode = mode)

  tf.logging.info(source_path)
  tf.logging.info(target_path)

  input_pipeline_ = input_pipeline.ParallelTextInputPipeline(
    params={
      "source_files": [source_path],
      "target_files": [target_path]
    },
    mode=mode
  )
  input_fn = training_utils.create_input_fn(
    pipeline=input_pipeline_, batch_size=batch_size)
  features, labels = input_fn()
  fetches = model(features, labels, None)
  fetches = [_ for _ in fetches if _ is not None]

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())
    sess.run(tf.tables_initializer())
    with tf.contrib.slim.queues.QueueRunners(sess):
      fetches_ = sess.run(fetches)

  if is_tmp_file:
    sources_file.close()
    targets_file.close()

  return model, fetches_

if __name__ == "__main__":

  # test_trial()
  vocab_path = "/home/bigdata/active_project/test_seq2seq_py2/yard_seq2seq/q2q_sim_95_80w/data/vocab/shared.vocab.txt"
  # t(vocab_path)
  dir = "/home/bigdata/active_project/run_tasks/text_sum/debug"
  from os.path import join
  # test_copy_gen_model(source_path=join(dir,"source.txt"), target_path=join(dir, "target.txt"),
  #                     vocab_path=join(dir,"vocab.txt"))

  # test_trial()

  test_scope()

ext: py

reason: import numpy


text:
#
#
#      0===============================0
#      |      PLY evaluate metrics     |
#      0===============================0
#
#
# ----------------------------------------------------------------------------------------------------------------------
#
#      function to evaluate .ply segmentation results
#
# ----------------------------------------------------------------------------------------------------------------------
#
#      Zhenhong Zou - 08/30/2021
#
# ----------------------------------------------------------------------------------------------------------------------
#
#          Imports and global variables
#      \**********************************/
#

from typing import Iterator, Tuple
import numpy as np
from sklearn.metrics import confusion_matrix
from tqdm import tqdm

def log_out(out_str, f_out):
    f_out.write(out_str + '\n')
    f_out.flush()
    print(out_str)

def mIoU(pred, gt, class_num=13) -> Tuple[float, list, list]:
    """calc the mean IoU of pred based on gt

    Args::
        pred: shape of (num,) or (num, 3), int
        gt: same shape of pred, int
        num: num of pts, int
    Returns: mIoU score, iou list for each class
    """

    gt_classes = [0 for _ in range(class_num)]
    positive_classes = [0 for _ in range(class_num)]
    true_positive_classes = [0 for _ in range(class_num)]

    conf_matrix = confusion_matrix(gt, pred, labels=np.arange(0, class_num, 1))
    gt_classes += np.sum(conf_matrix, axis=1)
    positive_classes += np.sum(conf_matrix, axis=0)
    true_positive_classes += np.diagonal(conf_matrix)

    iou_list = []
    gt_list = []
    for n in range(0, class_num, 1):
        iou = true_positive_classes[n] / float(gt_classes[n] + positive_classes[n] - true_positive_classes[n] + 0.1)
        iou_list.append(iou)
        gt_list.append(gt_classes[n])
    mean_iou = sum(iou_list) / float(class_num)

    return mean_iou, iou_list, gt_list

def batch_mIoU(iter_data: Iterator[Tuple[np.array, np.array]]):
    """calc the mean IoU of pred based on gt

    Args::
        pred: shape of (num,) or (num, 3), int
        gt: same shape of pred, int
        num: num of pts, int
    Returns: mIoU score, iou list for each class
    """

    gt_classes = [0 for _ in range(13)]
    positive_classes = [0 for _ in range(13)]
    true_positive_classes = [0 for _ in range(13)]
    iou_list = []
    gt_list = []
    macc = []
    tp = 0
    all = 0

    for preds, gt in tqdm(iter_data):
        pred, OAcc, pts_num = preds
        tp += OAcc
        all += pts_num
        conf_matrix = confusion_matrix(gt, pred, labels=np.arange(0, 13, 1))
        gt_classes += np.sum(conf_matrix, axis=1)
        positive_classes += np.sum(conf_matrix, axis=0)
        true_positive_classes += np.diagonal(conf_matrix)

    for n in range(0, 13, 1):
        iou = true_positive_classes[n] / float(gt_classes[n] + positive_classes[n] - true_positive_classes[n] + 0.1)
        iou_list.append(iou)
        gt_list.append(gt_classes[n])
        macc.append(true_positive_classes[n]/gt_classes[n])
    mean_iou = sum(iou_list) / float(13)

    return mean_iou, iou_list, gt_list, tp/all, macc


text:
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import data_processing0 as dp
import datetime

# def get_mean(df,df_A,df_V):
#     df['A_mean']=df_A.apply(lambda x: x.iloc[3:104].mean(), axis=1)
#     df['V_mean']=df_V.apply(lambda x: x.iloc[3:104].mean(), axis=1)
#     return df

if __name__ == '__main__':
    DATA_PATH = "./sitaiqu/"

    output_prefix = DATA_PATH + "huayuanxiaoqu"
    begin = "2016/03/01"
    end = "2016/08/01"
    dtypes = {'index': 'int', 'redidentsID': 'str', 'userID': 'str', 'meterID': 'str', 'misc': 'str', 'sequence': 'str'}
    parse_dates = ['date']
    df_A = dp.AV_process(output_prefix + "_A_after.csv", dtypes=dtypes, parse_dates=parse_dates)
    df_A['A_mean'] = df_A.apply(lambda x: x.iloc[3:104].mean(), axis=1)
    A_mean=df_A['A_mean'].to_frame()
    df_V = dp.AV_process(output_prefix + "_V_after.csv", dtypes=dtypes, parse_dates=parse_dates)
    df_V['V_mean'] = df_V.apply(lambda x: x.iloc[3:104].mean(), axis=1)
    V_mean = df_V['V_mean'].to_frame()
    df =dp.KW_process()
    df = df.sort_values(by='date')
    df['error']=df['super']-df['sub']
    df=dp.date_format(df,base='2014/8/3')
    df['date'] = pd.to_datetime(df['date'])
    df = df[df['error'] >= 0]
    df = df.sort_values(by='date')
    #df = df[(df['date'] >= pd.to_datetime(begin)) & (df['date'] <= pd.to_datetime(end))]

    A_mean = A_mean.xs('A', level='sequence')
    A_mean = A_mean.xs(690100001365, level='redidentsID')
    A_mean = A_mean.xs(8104424663, level='userID')
    A_mean = A_mean.xs(700420544, level='meterID')
    V_mean = V_mean.xs('A', level='sequence')
    V_mean = V_mean.xs(690100001365, level='redidentsID')
    V_mean = V_mean.xs(8104424663, level='userID')
    V_mean = V_mean.xs(700420544, level='meterID')

    A_mean.reset_index(inplace=True)
    V_mean.reset_index(inplace=True)
    mean=pd.merge(A_mean,V_mean)
    mean = mean.sort_values(by='date')
    mean['date'] = pd.to_datetime(mean['date'])
    mean = mean[(mean['date'] >= pd.to_datetime(begin)) & (mean['date'] <= pd.to_datetime(end))]
    res=pd.merge(df,mean)
    res = res.sort_values(by='date')
    res = res[res['error']>=0]
    df.to_csv(path_or_buf=DATA_PATH+"df.csv", encoding="utf-8",index=False)
    mean.to_csv(path_or_buf=DATA_PATH+"mean.csv", encoding="utf-8",index=False)
    res.to_csv(path_or_buf=DATA_PATH+"input.csv", encoding="utf-8",index=False)

max_stars_repo_name: minoriwww/MeterDetection

max_issues_repo_name: minoriwww/MeterDetection

max_forks_repo_name: minoriwww/MeterDetection


text:
import torch
import torchvision
import torch.nn as nn
from torch import allclose
import torchvision.transforms as T
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
from torch.testing import assert_allclose
import kornia
from kornia import augmentation as K
import kornia.augmentation.functional as F
import kornia.augmentation.random_generator as rg
from torchvision.transforms import functional as tvF
import sys
import numpy as np
from PIL import Image
from PIL import ImageFilter
import matplotlib.pyplot as plt

CIFAR_MEAN =  [0.49139968, 0.48215827, 0.44653124]
CIFAR_STD  =  [0.24703233, 0.24348505, 0.26158768]

CIFAR_MEAN_ =  torch.FloatTensor([CIFAR_MEAN])
CIFAR_STD_  =  torch.FloatTensor([CIFAR_STD])

# GPU based augmentation: https://colab.research.google.com/drive/1T20UNAG4SdlE2n2wstuhiewve5Q81VpS#scrollTo=-AnIAZjeIP35

class InitalTransformation():
    def __init__(self):
        self.transform = T.Compose([
            T.ToTensor(),
            transforms.Normalize(CIFAR_MEAN_,CIFAR_STD_),
        ])

    def __call__(self, x):
        x = self.transform(x)
        return  x

def get_clf_train_test_dataloaders(dataset = "cifar10", percent_train_sample = 20,
                                  data_dir="./dataset", batch_size = 16,
                                    num_workers = 4, download=True):

    tr = torchvision.datasets.CIFAR10(data_dir, train=True, transform=InitalTransformation(), download=True)
    samples = list(range(0, int(len(tr)*percent_train_sample/100)))
    tr_subset =  torch.utils.data.Subset(tr, samples)

    train_loader = torch.utils.data.DataLoader(
            dataset = tr_subset,
            shuffle=True,
            batch_size= batch_size,
            num_workers = 4 )

    test_loader = torch.utils.data.DataLoader(
            dataset = torchvision.datasets.CIFAR10(data_dir, train=True,
                                                transform=InitalTransformation(), download=True),
            shuffle=True,
            batch_size= batch_size,
            num_workers = 4
        )

    return train_loader, test_loader

def get_train_mem_test_dataloaders(dataset = "stl10", data_dir="./dataset", batch_size = 16,num_workers = 4, download=True):

    train_loader = torch.utils.data.DataLoader(
        dataset = torchvision.datasets.STL10(data_dir, split="unlabeled", transform=InitalTransformation(), download=download),
        shuffle=True,
        batch_size= batch_size,
        num_workers = num_workers
    )

    memory_loader = torch.utils.data.DataLoader(
        dataset = torchvision.datasets.STL10(data_dir, split="train", transform=InitalTransformation(), download=download),
        shuffle=False,
        batch_size= batch_size,
        num_workers = num_workers
    )
    test_loader = torch.utils.data.DataLoader(
        dataset = torchvision.datasets.STL10(data_dir, split="test", transform=InitalTransformation(), download=download),
        shuffle=True,
        batch_size= batch_size,
        num_workers = num_workers
        )
    return train_loader, memory_loader, test_loader

def gpu_transformer(image_size,s=.2):

    train_transform = nn.Sequential(

                kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
                kornia.augmentation.RandomHorizontalFlip(p=0.5),
                kornia.augmentation.ColorJitter(0.8*s,0.8*s,0.8*s,0.2*s,p=0.3),
                # kornia.augmentation.RandomGrayscale(p=0.05),
            )

    test_transform = nn.Sequential(
                kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
                kornia.augmentation.RandomHorizontalFlip(p=0.5),
                kornia.augmentation.ColorJitter(0.8*s,0.8*s,0.8*s,0.2*s,p=0.3),
                # kornia.augmentation.RandomGrayscale(p=0.05),
        )

    return train_transform , test_transform

def get_clf_train_test_transform(image_size,s=.2):

    train_transform = nn.Sequential(

                kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
                kornia.augmentation.RandomHorizontalFlip(p=0.5),
                # kornia.augmentation.Normalize(CIFAR_MEAN_,CIFAR_STD_),
            )

    test_transform = nn.Sequential(
                kornia.augmentation.RandomResizedCrop(image_size,scale=(0.5,1.0)),
                kornia.augmentation.RandomHorizontalFlip(p=0.5),
                # kornia.augmentation.RandomGrayscale(p=0.05),
                # kornia.augmentation.Normalize(CIFAR_MEAN_,CIFAR_STD_)
        )

    return train_transform , test_transform

if __name__=="__main__":
    device = torch.device('cuda')
    print(f"Running with device: {device}")

    data_dir="./dataset"
    batch_size = 16
    num_workers = 8
    download=True
    train = True
    image_size = (32,32)
    s = 1.0

    train_transform = nn.Sequential(
                kornia.augmentation.RandomResizedCrop(image_size,scale=(0.2,1.0)),
                kornia.augmentation.RandomHorizontalFlip(),
                kornia.augmentation.ColorJitter(0.8*s,0.8*s,0.8*s,0.2*s,p=0.5),
                kornia.augmentation.RandomGrayscale(p=0.2),
            )

    train_loader,_,_ = get_train_mem_test_dataloaders()

    from tqdm import tqdm
    local_progress = tqdm(train_loader, desc=f'Epoch {1}/{1}')

    for i , sample in enumerate(local_progress):
        images = sample[0].to(device)
        transformed_images = train_transform(images)

max_stars_repo_name: nikheelpandey/didactic-umbrella

max_issues_repo_name: nikheelpandey/didactic-umbrella

max_forks_repo_name: nikheelpandey/didactic-umbrella


text:
# Load in a .x3d file, store to a .dgs file with a checkerboard pattern

import sys
import os
import numpy as np
import dataguzzler as dg
import dg_file as dgf
import dg_metadata as dgm

try:
    from cStringIO import StringIO  # python 2.x
    pass
except ImportError:
    from io import StringIO # python 3.x
    pass

from spatialnde.coordframes import coordframe
from spatialnde.ndeobj import ndepart

from spatialnde.exporters.vrml import VRMLAppearance

imageshape=(1024,1024)
cbrows=20
cbcols=20

x3dname=sys.argv[1]
(x3dpath,x3dfile)=os.path.split(x3dname)
x3dbasename=os.path.splitext(x3dfile)[0]

objframe=coordframe()

obj = ndepart.fromx3d(objframe,None,x3dname,tol=1e-6)
VRMLBuf=StringIO()

VRMLAppear=VRMLAppearance.Simple(DefName="ImageMap")
obj.VRMLFile(VRMLBuf,objframe,UVparameterization=None,appearance=VRMLAppear)
VRMLAppear.reset() # needed if we want to write a new file

wfmdict={}
wfmdict[x3dbasename]=dg.wfminfo()
wfmdict[x3dbasename].Name=x3dbasename
wfmdict[x3dbasename].dimlen=np.array((),dtype='i8')
dgm.AddMetaDatumWI(wfmdict[x3dbasename],dgm.MetaDatum("VRML97Geom",VRMLBuf.getvalue()))

x3dparamname=x3dbasename+"_parameterization"
wfmdict[x3dparamname]=dg.wfminfo()
wfmdict[x3dparamname].Name=x3dparamname
wfmdict[x3dparamname].ndim=2
wfmdict[x3dparamname].dimlen=np.array(imageshape,dtype='i8')
wfmdict[x3dparamname].n=np.prod(wfmdict[x3dparamname].dimlen)
wfmdict[x3dparamname].data=np.zeros(wfmdict[x3dparamname].dimlen,dtype='f')

xpos=np.arange(imageshape[0],dtype='d')
ypos=np.arange(imageshape[1],dtype='d')

xchecker = (xpos//(imageshape[0]*1.0/(cbcols)) % 2).astype(np.bool)
ychecker = (ypos//(imageshape[1]*1.0/(cbrows)) % 2).astype(np.bool)

wfmdict[x3dparamname].data[:,:]=xchecker.reshape(imageshape[0],1) ^ ychecker.reshape(1,imageshape[1])  # XOR operator

dgm.AddMetaDatumWI(wfmdict[x3dbasename],dgm.MetaDatum("TextureChan_ImageMap",x3dparamname+":0"))

dgf.savesnapshot(os.path.join(x3dpath,x3dbasename+".dgs"),wfmdict)

max_stars_repo_name: isuthermography/spatialnde

max_issues_repo_name: isuthermography/spatialnde

max_forks_repo_name: isuthermography/spatialnde


text:
import numpy as np
import pydub
from pydub import AudioSegment
import os
import sys

print("Just wanna tell you that the script is running!") # DELETE WHEN FINISHED

MAX_FRQ = 30000
SLICE_SIZE = 0.15 #seconds
WINDOW_SIZE = 0.25 #seconds

# TODO: implement this dictionary
specific_dict = {'E4': 330, 'B3': 247, 'G3': 196, 'D3': 147, 'A2': 110, 'E2': 83}
range_dict = {'E4': (321, 339), 'B3': (240, 254), 'G3': (191, 201), 'D3': (143, 151), 'A2': (107, 113), 'E2': (81, 84)}
# LOWER_FRQS = [697, 770, 852, 941]
# HIGHER_FRQS = [1209, 1336, 1477]
# FRQ_THRES = 20

def get_max_frq(frq, fft):
    max_frq = 0
    max_fft = 0
    for idx in range(len(fft)):
        if abs(fft[idx]) > max_fft:
            max_fft = abs(fft[idx])
            max_frq = frq[idx]
    return max_frq

def decoder(average_frequency, target_note): # parameter types: (int, string)
    freq_range = range_dict[target_note]
    text_file = open("result.txt", "w")

    if (average_frequency >= freq_range[0]) and (average_frequency <= freq_range[1]):
        print("You're all set!")
        text_file.write("You're all set!")
    elif average_frequency < freq_range[0]:
        print("Your frequency is too low, which means you gotta tighten the string")
        print("You're at " + str(average_frequency) + "Hz, you should be in this range: " + str(freq_range[0]) + " to "
              + str(freq_range[1]) + "Hz.")
        text_file.write("Your frequency is too low, which means you gotta tighten the string. ")
        text_file.write("You're at " + str(average_frequency) + "Hz, you should be in this range: " + str(freq_range[0]) + " to "
              + str(freq_range[1]) + "Hz.")
    else:
        print("Your frequency is too high, which means you gotta loosen the string")
        print("You're at " + str(average_frequency) + "Hz, you should be in this range: " + str(freq_range[0]) + " to "
              + str(freq_range[1]) + "Hz.")
        text_file.write("Your frequency is too high, which means you gotta loosen the string. ")
        text_file.write("You're at " + str(average_frequency) + "Hz, you should be in this range: " + str(freq_range[0]) + " to "
              + str(freq_range[1]) + "Hz.")
    text_file.close()

def main(file, target_note):
    print("Importing {}".format(file))
    audio = AudioSegment.from_wav(file)

    sample_count = audio.frame_count()
    sample_rate = audio.frame_rate
    samples = audio.get_array_of_samples()

    print("Number of channels: " + str(audio.channels))
    print("Sample count: " + str(sample_count))
    print("Sample rate: " + str(sample_rate))
    print("Sample width: " + str(audio.sample_width))

    slice_sample_size = int(SLICE_SIZE * sample_rate)  # get the number of elements expected for [SLICE_SIZE] seconds

    n = slice_sample_size  # n is the number of elements in the slice

    # generating the frequency spectrum
    k = np.arange(n)  # k is an array from 0 to [n] with a step of 1
    slice_duration = n / sample_rate  # slice_duration is the length of time the sample slice is (seconds)
    frq = k / slice_duration  # generate the frequencies by dividing every element of k by slice_duration

    max_frq_idx = int(MAX_FRQ * slice_duration)  # get the index of the maximum frequency (2000)
    frq = frq[range(max_frq_idx)]  # truncate the frequency array so it goes from 0 to 2000 Hz

    start_index = 0  # set the starting index at 0
    end_index = start_index + slice_sample_size  # find the ending index for the slice
    output = ''

    print()
    i = 1

    result_list = []

    while end_index < len(samples):
        print("Sample {}:".format(i))
        i += 1

        #TODO: grab the sample slice and perform FFT on it

        sample_slice = samples[start_index: end_index]  # take a slice from the samples array for the given start and end index
        sample_slice_fft = np.fft.fft(sample_slice) / n  # perform the fourier transform on the sample_slice and normalize by dividing by n

        #TODO: truncate the FFT to 0 to 1000 Hz
        sample_slice_fft = sample_slice_fft[range(max_frq_idx)]
        sample_slice_fft = np.absolute(sample_slice_fft)

        #TODO: calculate the locations of the upper and lower FFT peak using get_peak_frqs()
        actual_frequency = get_max_frq(frq,sample_slice_fft)
        #TODO: print the values and find the number that corresponds to the numbers
        print("The frequency of this sample segment is: " + str(actual_frequency) + "Hz")

        #Incrementing the start and end window for FFT analysis
        start_index += int(WINDOW_SIZE*sample_rate)
        end_index = start_index + slice_sample_size

        # Registering this result into the list
        result_list.append(int(actual_frequency))

    average_freq = int(sum(result_list) / len(result_list))
    print("Average frequency from samples: " + str(average_freq))
    decoder(average_freq, target_note)

if __name__ == '__main__':
    while True:
        if os.path.isfile('output.wav'): # If output.wav is provided, we start our work here
            user_request = open("target_note.txt", "r")
            target_note = user_request.read(
                2)  # We will only read the first 2 characters so as to avoid user accidentally
            # type a little too many characters.

            print("Let's see if this audio really is note: " + str(target_note))
            main('output.wav', str(target_note))
            os.remove("output.wav")
            exit(0)

max_stars_repo_path: ee250/final/Server-side Code/hello.py

max_issues_repo_path: ee250/final/Server-side Code/hello.py

max_forks_repo_path: ee250/final/Server-side Code/hello.py


text:
import torch
from torch import nn
from torch.nn import functional as F

from TTS.tts.layers.common_layers import Linear
from scipy.stats import betabinom

class LocationLayer(nn.Module):
    """Layers for Location Sensitive Attention

    Args:
        attention_dim (int): number of channels in the input tensor.
        attention_n_filters (int, optional): number of filters in convolution. Defaults to 32.
        attention_kernel_size (int, optional): kernel size of convolution filter. Defaults to 31.
    """
    def __init__(self,
                 attention_dim,
                 attention_n_filters=32,
                 attention_kernel_size=31):
        super(LocationLayer, self).__init__()
        self.location_conv1d = nn.Conv1d(
            in_channels=2,
            out_channels=attention_n_filters,
            kernel_size=attention_kernel_size,
            stride=1,
            padding=(attention_kernel_size - 1) // 2,
            bias=False)
        self.location_dense = Linear(
            attention_n_filters, attention_dim, bias=False, init_gain='tanh')

    def forward(self, attention_cat):
        """
        Shapes:
            attention_cat: [B, 2, C]
        """
        processed_attention = self.location_conv1d(attention_cat)
        processed_attention = self.location_dense(
            processed_attention.transpose(1, 2))
        return processed_attention

class GravesAttention(nn.Module):
    """Graves Attention as is ref1 with updates from ref2.
    ref1: https://arxiv.org/abs/1910.10288
    ref2: https://arxiv.org/pdf/1906.01083.pdf

    Args:
        query_dim (int): number of channels in query tensor.
        K (int): number of Gaussian heads to be used for computing attention.
    """
    COEF = 0.3989422917366028  # numpy.sqrt(1/(2*numpy.pi))

    def __init__(self, query_dim, K):

        super(GravesAttention, self).__init__()
        self._mask_value = 1e-8
        self.K = K
        # self.attention_alignment = 0.05
        self.eps = 1e-5
        self.J = None
        self.N_a = nn.Sequential(
            nn.Linear(query_dim, query_dim, bias=True),
            nn.ReLU(),
            nn.Linear(query_dim, 3*K, bias=True))
        self.attention_weights = None
        self.mu_prev = None
        self.init_layers()

    def init_layers(self):
        torch.nn.init.constant_(self.N_a[2].bias[(2*self.K):(3*self.K)], 1.)  # bias mean
        torch.nn.init.constant_(self.N_a[2].bias[self.K:(2*self.K)], 10)  # bias std

    def init_states(self, inputs):
        if self.J is None or inputs.shape[1]+1 > self.J.shape[-1]:
            self.J = torch.arange(0, inputs.shape[1]+2.0).to(inputs.device) + 0.5
        self.attention_weights = torch.zeros(inputs.shape[0], inputs.shape[1]).to(inputs.device)
        self.mu_prev = torch.zeros(inputs.shape[0], self.K).to(inputs.device)

    # pylint: disable=R0201
    # pylint: disable=unused-argument
    def preprocess_inputs(self, inputs):
        return None

    def forward(self, query, inputs, processed_inputs, mask):
        """
        Shapes:
            query: [B, C_attention_rnn]
            inputs: [B, T_in, C_encoder]
            processed_inputs: place_holder
            mask: [B, T_in]
        """
        gbk_t = self.N_a(query)
        gbk_t = gbk_t.view(gbk_t.size(0), -1, self.K)

        # attention model parameters
        # each B x K
        g_t = gbk_t[:, 0, :]
        b_t = gbk_t[:, 1, :]
        k_t = gbk_t[:, 2, :]

        # dropout to decorrelate attention heads
        g_t = torch.nn.functional.dropout(g_t, p=0.5, training=self.training)

        # attention GMM parameters
        sig_t = torch.nn.functional.softplus(b_t) + self.eps

        mu_t = self.mu_prev + torch.nn.functional.softplus(k_t)
        g_t = torch.softmax(g_t, dim=-1) + self.eps

        j = self.J[:inputs.size(1)+1]

        # attention weights
        phi_t = g_t.unsqueeze(-1) * (1 / (1 + torch.sigmoid((mu_t.unsqueeze(-1) - j) / sig_t.unsqueeze(-1))))

        # discritize attention weights
        alpha_t = torch.sum(phi_t, 1)
        alpha_t = alpha_t[:, 1:] - alpha_t[:, :-1]
        alpha_t[alpha_t == 0] = 1e-8

        # apply masking
        if mask is not None:
            alpha_t.data.masked_fill_(~mask, self._mask_value)

        context = torch.bmm(alpha_t.unsqueeze(1), inputs).squeeze(1)
        self.attention_weights = alpha_t
        self.mu_prev = mu_t
        return context

class OriginalAttention(nn.Module):
    """Bahdanau Attention with various optional modifications. Proposed below.
    - Location sensitive attnetion: https://arxiv.org/abs/1712.05884
    - Forward Attention: https://arxiv.org/abs/1807.06736 + state masking at inference
    - Using sigmoid instead of softmax normalization
    - Attention windowing at inference time

    Note:
        Location Sensitive Attention is an attention mechanism that extends the additive attention mechanism
    to use cumulative attention weights from previous decoder time steps as an additional feature.

        Forward attention considers only the alignment paths that satisfy the monotonic condition at each
    decoder timestep. The modified attention probabilities at each timestep are computed recursively
    using a forward algorithm.

        Transition agent for forward attention is further proposed, which helps the attention mechanism
    to make decisions whether to move forward or stay at each decoder timestep.

        Attention windowing applies a sliding windows to time steps of the input tensor centering at the last
    time step with the largest attention weight. It is especially useful at inference to keep the attention
    alignment diagonal.

    Args:
        query_dim (int): number of channels in the query tensor.
        embedding_dim (int): number of channels in the vakue tensor. In general, the value tensor is the output of the encoder layer.
        attention_dim (int): number of channels of the inner attention layers.
        location_attention (bool): enable/disable location sensitive attention.
        attention_location_n_filters (int): number of location attention filters.
        attention_location_kernel_size (int): filter size of location attention convolution layer.
        windowing (int): window size for attention windowing. if it is 5, for computing the attention, it only considers the time steps [(t-5), ..., (t+5)] of the input.
        norm (str): normalization method applied to the attention weights. 'softmax' or 'sigmoid'
        forward_attn (bool): enable/disable forward attention.
        trans_agent (bool): enable/disable transition agent in the forward attention.
        forward_attn_mask (int): enable/disable an explicit masking in forward attention. It is useful to set at especially inference time.
    """
    # Pylint gets confused by PyTorch conventions here
    #pylint: disable=attribute-defined-outside-init
    def __init__(self, query_dim, embedding_dim, attention_dim,
                 location_attention, attention_location_n_filters,
                 attention_location_kernel_size, windowing, norm, forward_attn,
                 trans_agent, forward_attn_mask):
        super(OriginalAttention, self).__init__()
        self.query_layer = Linear(
            query_dim, attention_dim, bias=False, init_gain='tanh')
        self.inputs_layer = Linear(
            embedding_dim, attention_dim, bias=False, init_gain='tanh')
        self.v = Linear(attention_dim, 1, bias=True)
        if trans_agent:
            self.ta = nn.Linear(
                query_dim + embedding_dim, 1, bias=True)
        if location_attention:
            self.location_layer = LocationLayer(
                attention_dim,
                attention_location_n_filters,
                attention_location_kernel_size,
            )
        self._mask_value = -float("inf")
        self.windowing = windowing
        self.win_idx = None
        self.norm = norm
        self.forward_attn = forward_attn
        self.trans_agent = trans_agent
        self.forward_attn_mask = forward_attn_mask
        self.location_attention = location_attention

    def init_win_idx(self):
        self.win_idx = -1
        self.win_back = 2
        self.win_front = 6

    def init_forward_attn(self, inputs):
        B = inputs.shape[0]
        T = inputs.shape[1]
        self.alpha = torch.cat(
            [torch.ones([B, 1]),
             torch.zeros([B, T])[:, :-1] + 1e-7], dim=1).to(inputs.device)
        self.u = (0.5 * torch.ones([B, 1])).to(inputs.device)

    def init_location_attention(self, inputs):
        B = inputs.size(0)
        T = inputs.size(1)
        self.attention_weights_cum = torch.zeros([B, T], device=inputs.device)

    def init_states(self, inputs):
        B = inputs.size(0)
        T = inputs.size(1)
        self.attention_weights = torch.zeros([B, T], device=inputs.device)
        if self.location_attention:
            self.init_location_attention(inputs)
        if self.forward_attn:
            self.init_forward_attn(inputs)
        if self.windowing:
            self.init_win_idx()

    def preprocess_inputs(self, inputs):
        return self.inputs_layer(inputs)

    def update_location_attention(self, alignments):
        self.attention_weights_cum += alignments

    def get_location_attention(self, query, processed_inputs):
        attention_cat = torch.cat((self.attention_weights.unsqueeze(1),
                                   self.attention_weights_cum.unsqueeze(1)),
                                  dim=1)
        processed_query = self.query_layer(query.unsqueeze(1))
        processed_attention_weights = self.location_layer(attention_cat)
        energies = self.v(
            torch.tanh(processed_query + processed_attention_weights +
                       processed_inputs))
        energies = energies.squeeze(-1)
        return energies, processed_query

    def get_attention(self, query, processed_inputs):
        processed_query = self.query_layer(query.unsqueeze(1))
        energies = self.v(torch.tanh(processed_query + processed_inputs))
        energies = energies.squeeze(-1)
        return energies, processed_query

    def apply_windowing(self, attention, inputs):
        back_win = self.win_idx - self.win_back
        front_win = self.win_idx + self.win_front
        if back_win > 0:
            attention[:, :back_win] = -float("inf")
        if front_win < inputs.shape[1]:
            attention[:, front_win:] = -float("inf")
        # this is a trick to solve a special problem.
        # but it does not hurt.
        if self.win_idx == -1:
            attention[:, 0] = attention.max()
        # Update the window
        self.win_idx = torch.argmax(attention, 1).long()[0].item()
        return attention

    def apply_forward_attention(self, alignment):
        # forward attention
        fwd_shifted_alpha = F.pad(
            self.alpha[:, :-1].clone().to(alignment.device), (1, 0, 0, 0))
        # compute transition potentials
        alpha = ((1 - self.u) * self.alpha
                 + self.u * fwd_shifted_alpha
                 + 1e-8) * alignment
        # force incremental alignment
        if not self.training and self.forward_attn_mask:
            _, n = fwd_shifted_alpha.max(1)
            val, _ = alpha.max(1)
            for b in range(alignment.shape[0]):
                alpha[b, n[b] + 3:] = 0
                alpha[b, :(
                    n[b] - 1
                )] = 0  # ignore all previous states to prevent repetition.
                alpha[b,
                      (n[b] - 2
                       )] = 0.01 * val[b]  # smoothing factor for the prev step
        # renormalize attention weights
        alpha = alpha / alpha.sum(dim=1, keepdim=True)
        return alpha

    def forward(self, query, inputs, processed_inputs, mask):
        """
        shapes:
            query: [B, C_attn_rnn]
            inputs: [B, T_en, D_en]
            processed_inputs: [B, T_en, D_attn]
            mask: [B, T_en]
        """
        if self.location_attention:
            attention, _ = self.get_location_attention(
                query, processed_inputs)
        else:
            attention, _ = self.get_attention(
                query, processed_inputs)
        # apply masking
        if mask is not None:
            attention.data.masked_fill_(~mask, self._mask_value)
        # apply windowing - only in eval mode
        if not self.training and self.windowing:
            attention = self.apply_windowing(attention, inputs)

        # normalize attention values
        if self.norm == "softmax":
            alignment = torch.softmax(attention, dim=-1)
        elif self.norm == "sigmoid":
            alignment = torch.sigmoid(attention) / torch.sigmoid(
                attention).sum(
                    dim=1, keepdim=True)
        else:
            raise ValueError("Unknown value for attention norm type")

        if self.location_attention:
            self.update_location_attention(alignment)

        # apply forward attention if enabled
        if self.forward_attn:
            alignment = self.apply_forward_attention(alignment)
            self.alpha = alignment

        context = torch.bmm(alignment.unsqueeze(1), inputs)
        context = context.squeeze(1)
        self.attention_weights = alignment

        # compute transition agent
        if self.forward_attn and self.trans_agent:
            ta_input = torch.cat([context, query.squeeze(1)], dim=-1)
            self.u = torch.sigmoid(self.ta(ta_input))
        return context

class MonotonicDynamicConvolutionAttention(nn.Module):
    """Dynamic convolution attention from
    https://arxiv.org/pdf/1910.10288.pdf

    query -> linear -> tanh -> linear ->|
                                        |                                            mask values
                                        v                                              |    |
               atten_w(t-1) -|-> conv1d_dynamic -> linear -|-> tanh -> + -> softmax -> * -> * -> context
                             |-> conv1d_static  -> linear -|           |
                             |-> conv1d_prior   -> log ----------------|

    query: attention rnn output.

    Note:
        Dynamic convolution attention is an alternation of the location senstive attention with
    dynamically computed convolution filters from the previous attention scores and a set of
    constraints to keep the attention alignment diagonal.

    Args:
        query_dim (int): number of channels in the query tensor.
        embedding_dim (int): number of channels in the value tensor.
        static_filter_dim (int): number of channels in the convolution layer computing the static filters.
        static_kernel_size (int): kernel size for the convolution layer computing the static filters.
        dynamic_filter_dim (int): number of channels in the convolution layer computing the dynamic filters.
        dynamic_kernel_size (int): kernel size for the convolution layer computing the dynamic filters.
        prior_filter_len (int, optional): [description]. Defaults to 11 from the paper.
        alpha (float, optional): [description]. Defaults to 0.1 from the paper.
        beta (float, optional): [description]. Defaults to 0.9 from the paper.
    """
    def __init__(
        self,
        query_dim,
        embedding_dim,  # pylint: disable=unused-argument
        attention_dim,
        static_filter_dim,
        static_kernel_size,
        dynamic_filter_dim,
        dynamic_kernel_size,
        prior_filter_len=11,
        alpha=0.1,
        beta=0.9,
    ):
        super().__init__()
        self._mask_value = 1e-8
        self.dynamic_filter_dim = dynamic_filter_dim
        self.dynamic_kernel_size = dynamic_kernel_size
        self.prior_filter_len = prior_filter_len
        self.attention_weights = None
        # setup key and query layers
        self.query_layer = nn.Linear(query_dim, attention_dim)
        self.key_layer = nn.Linear(
            attention_dim, dynamic_filter_dim * dynamic_kernel_size, bias=False
        )
        self.static_filter_conv = nn.Conv1d(
            1,
            static_filter_dim,
            static_kernel_size,
            padding=(static_kernel_size - 1) // 2,
            bias=False,
        )
        self.static_filter_layer = nn.Linear(static_filter_dim, attention_dim, bias=False)
        self.dynamic_filter_layer = nn.Linear(dynamic_filter_dim, attention_dim)
        self.v = nn.Linear(attention_dim, 1, bias=False)

        prior = betabinom.pmf(range(prior_filter_len), prior_filter_len - 1,
                          alpha, beta)
        self.register_buffer("prior", torch.FloatTensor(prior).flip(0))

    # pylint: disable=unused-argument
    def forward(self, query, inputs, processed_inputs, mask):
        """
        query: [B, C_attn_rnn]
        inputs: [B, T_en, D_en]
        processed_inputs: place holder.
        mask: [B, T_en]
        """
        # compute prior filters
        prior_filter = F.conv1d(
            F.pad(self.attention_weights.unsqueeze(1),
                  (self.prior_filter_len - 1, 0)), self.prior.view(1, 1, -1))
        prior_filter = torch.log(prior_filter.clamp_min_(1e-6)).squeeze(1)
        G = self.key_layer(torch.tanh(self.query_layer(query)))
        # compute dynamic filters
        dynamic_filter = F.conv1d(
            self.attention_weights.unsqueeze(0),
            G.view(-1, 1, self.dynamic_kernel_size),
            padding=(self.dynamic_kernel_size - 1) // 2,
            groups=query.size(0),
        )
        dynamic_filter = dynamic_filter.view(query.size(0), self.dynamic_filter_dim, -1).transpose(1, 2)
        # compute static filters
        static_filter = self.static_filter_conv(self.attention_weights.unsqueeze(1)).transpose(1, 2)
        alignment = self.v(
            torch.tanh(
                self.static_filter_layer(static_filter) +
                self.dynamic_filter_layer(dynamic_filter))).squeeze(-1) + prior_filter
        # compute attention weights
        attention_weights = F.softmax(alignment, dim=-1)
        # apply masking
        if mask is not None:
            attention_weights.data.masked_fill_(~mask, self._mask_value)
        self.attention_weights = attention_weights
        # compute context
        context = torch.bmm(attention_weights.unsqueeze(1), inputs).squeeze(1)
        return context

    def preprocess_inputs(self, inputs):  # pylint: disable=no-self-use
        return None

    def init_states(self, inputs):
        B = inputs.size(0)
        T = inputs.size(1)
        self.attention_weights = torch.zeros([B, T], device=inputs.device)
        self.attention_weights[:, 0] = 1.

def init_attn(attn_type, query_dim, embedding_dim, attention_dim,
              location_attention, attention_location_n_filters,
              attention_location_kernel_size, windowing, norm, forward_attn,
              trans_agent, forward_attn_mask, attn_K):
    if attn_type == "original":
        return OriginalAttention(query_dim, embedding_dim, attention_dim,
                                 location_attention,
                                 attention_location_n_filters,
                                 attention_location_kernel_size, windowing,
                                 norm, forward_attn, trans_agent,
                                 forward_attn_mask)
    if attn_type == "graves":
        return GravesAttention(query_dim, attn_K)
    if attn_type == "dynamic_convolution":
        return MonotonicDynamicConvolutionAttention(query_dim,
                                                    embedding_dim,
                                                    attention_dim,
                                                    static_filter_dim=8,
                                                    static_kernel_size=21,
                                                    dynamic_filter_dim=8,
                                                    dynamic_kernel_size=21,
                                                    prior_filter_len=11,
                                                    alpha=0.1,
                                                    beta=0.9)

    raise RuntimeError(
        " [!] Given Attention Type '{attn_type}' is not exist.")

max_stars_repo_name: mightmay/Mien-TTS

max_issues_repo_name: mightmay/Mien-TTS

max_forks_repo_name: mightmay/Mien-TTS

reason: from scipy


text:
#!/usr/bin/env python3
##
# @file
# This file is part of SeisSol.
#
# @author Carsten Uphoff (c.uphoff AT tum.de, http://www5.in.tum.de/wiki/index.php/Carsten_Uphoff,_M.Sc.)
#
# @section LICENSE
# Copyright (c) 2019, SeisSol Group
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
#    contributors may be used to endorse or promote products derived from this
#    software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# @section DESCRIPTION
#

import numpy as np
from abc import ABC, abstractmethod
from common import generate_kernel_name_prefix

from multSim import OptionalDimTensor
from yateto import Tensor, Scalar, simpleParameterSpace
from yateto.ast.node import Add
from yateto.ast.transformer import DeduceIndices, EquivalentSparsityPattern
from yateto.input import parseXMLMatrixFile, parseJSONMatrixFile
from yateto.util import tensor_collection_from_constant_expression

class ADERDGBase(ABC):
  def __init__(self, order, multipleSimulations, matricesDir):
    self.order = order

    self.alignStride = lambda name: True
    if multipleSimulations > 1:
      self.alignStride = lambda name: name.startswith('fP')
    transpose = multipleSimulations > 1
    self.transpose = lambda name: transpose
    self.t = (lambda x: x[::-1]) if transpose else (lambda x: x)

    self.db = parseXMLMatrixFile('{}/matrices_{}.xml'.format(matricesDir, self.numberOf3DBasisFunctions()), transpose=self.transpose, alignStride=self.alignStride)
    clonesQP = {
      'v': [ 'evalAtQP' ],
      'vInv': [ 'projectQP' ]
    }
    self.db.update( parseXMLMatrixFile('{}/plasticity_ip_matrices_{}.xml'.format(matricesDir, order), clonesQP, transpose=self.transpose, alignStride=self.alignStride))
    self.db.update(parseJSONMatrixFile('{}/sampling_directions.json'.format(matricesDir)))
    self.db.update(parseJSONMatrixFile('{}/mass_{}.json'.format(matricesDir, order)))

    qShape = (self.numberOf3DBasisFunctions(), self.numberOfQuantities())
    self.Q = OptionalDimTensor('Q', 's', multipleSimulations, 0, qShape, alignStride=True)
    self.I = OptionalDimTensor('I', 's', multipleSimulations, 0, qShape, alignStride=True)

    Aplusminus_spp = self.flux_solver_spp()
    self.AplusT = Tensor('AplusT', Aplusminus_spp.shape, spp=Aplusminus_spp)
    self.AminusT = Tensor('AminusT', Aplusminus_spp.shape, spp=Aplusminus_spp)
    Tshape = (self.numberOfExtendedQuantities(), self.numberOfExtendedQuantities())
    trans_spp = self.transformation_spp()
    self.T = Tensor('T', trans_spp.shape, spp=trans_spp)
    trans_inv_spp = self.transformation_inv_spp()
    self.Tinv = Tensor('Tinv', trans_inv_spp.shape, spp=trans_inv_spp)
    godunov_spp = self.godunov_spp()
    self.QgodLocal = Tensor('QgodLocal', godunov_spp.shape, spp=godunov_spp)
    self.QgodNeighbor = Tensor('QgodNeighbor', godunov_spp.shape, spp=godunov_spp)

    self.oneSimToMultSim = Tensor('oneSimToMultSim', (self.Q.optSize(),), spp={(i,): '1.0' for i in range(self.Q.optSize())})

    self.db.update(
      parseJSONMatrixFile('{}/nodal/nodalBoundary_matrices_{}.json'.format(matricesDir,
                                                                           self.order),
                          {},
                          alignStride=self.alignStride,
                          transpose=self.transpose,
                          namespace='nodal')
    )

    self.INodal = OptionalDimTensor('INodal',
                                    's',
                                    False, #multipleSimulations,
                                    0,
                                    (self.numberOf2DBasisFunctions(), self.numberOfQuantities()),
                                    alignStride=True)

    project2nFaceTo3m = tensor_collection_from_constant_expression(
      base_name='project2nFaceTo3m',
      expressions=lambda i: self.db.rDivM[i]['jk'] * self.db.V2nTo2m['kl'],
      group_indices=range(4),
      target_indices='jl')

    self.db.update(project2nFaceTo3m)

  def numberOf2DBasisFunctions(self):
    return self.order*(self.order+1)//2

  def numberOf3DBasisFunctions(self):
    return self.order*(self.order+1)*(self.order+2)//6

  def numberOf3DQuadraturePoints(self):
    return (self.order+1)**3

  def godunov_spp(self):
    shape = (self.numberOfQuantities(), self.numberOfQuantities())
    return np.ones(shape, dtype=bool)

  def flux_solver_spp(self):
    shape = (self.numberOfQuantities(), self.numberOfExtendedQuantities())
    return np.ones(shape, dtype=bool)

  def transformation_spp(self):
    shape = (self.numberOfExtendedQuantities(), self.numberOfExtendedQuantities())
    return np.ones(shape, dtype=bool)

  def transformation_inv_spp(self):
    return self.godunov_spp()

  @abstractmethod
  def numberOfQuantities(self):
    pass

  @abstractmethod
  def numberOfExtendedQuantities(self):
    pass

  @abstractmethod
  def extendedQTensor(self):
    pass

  @abstractmethod
  def starMatrix(self, dim):
    pass

  def addInit(self, generator):
    fluxScale = Scalar('fluxScale')
    computeFluxSolverLocal = self.AplusT['ij'] <= fluxScale * self.Tinv['ki'] * self.QgodLocal['kq'] * self.starMatrix(0)['ql'] * self.T['jl']
    generator.add('computeFluxSolverLocal', computeFluxSolverLocal)

    computeFluxSolverNeighbor = self.AminusT['ij'] <= fluxScale * self.Tinv['ki'] * self.QgodNeighbor['kq'] * self.starMatrix(0)['ql'] * self.T['jl']
    generator.add('computeFluxSolverNeighbor', computeFluxSolverNeighbor)

    QFortran = Tensor('QFortran', (self.numberOf3DBasisFunctions(), self.numberOfQuantities()))
    multSimToFirstSim = Tensor('multSimToFirstSim', (self.Q.optSize(),), spp={(0,): '1.0'})
    if self.Q.hasOptDim():
      copyQToQFortran = QFortran['kp'] <= self.Q['kp'] * multSimToFirstSim['s']
    else:
      copyQToQFortran = QFortran['kp'] <= self.Q['kp']

    generator.add('copyQToQFortran', copyQToQFortran)

    stiffnessTensor = Tensor('stiffnessTensor', (3, 3, 3, 3))
    direction = Tensor('direction', (3,))
    christoffel = Tensor('christoffel', (3,3))

    computeChristoffel = christoffel['ik'] <= stiffnessTensor['ijkl'] * direction['j'] * direction['l']
    generator.add('computeChristoffel', computeChristoffel)

  @abstractmethod
  def addLocal(self, generator, targets):
    pass

  @abstractmethod
  def addNeighbor(self, generator, targets):
    pass

  @abstractmethod
  def addTime(self, generator, targets):
    pass

  def add_include_tensors(self, include_tensors):
    include_tensors.add(self.db.samplingDirections)
    include_tensors.add(self.db.M2inv)

class LinearADERDG(ADERDGBase):

  def sourceMatrix(self):
    return None

  def extendedQTensor(self):
    return self.Q

  def numberOfExtendedQuantities(self):
    return self.numberOfQuantities()

  def addInit(self, generator):
    super().addInit(generator)

    iniShape = (self.numberOf3DQuadraturePoints(), self.numberOfQuantities())
    iniCond = OptionalDimTensor('iniCond', self.Q.optName(), self.Q.optSize(), self.Q.optPos(), iniShape, alignStride=True)
    dofsQP = OptionalDimTensor('dofsQP', self.Q.optName(), self.Q.optSize(), self.Q.optPos(), iniShape, alignStride=True)

    generator.add('projectIniCond', self.Q['kp'] <= self.db.projectQP[self.t('kl')] * iniCond['lp'])
    generator.add('evalAtQP', dofsQP['kp'] <= self.db.evalAtQP[self.t('kl')] * self.Q['lp'])

  def addLocal(self, generator, targets):
    for target in targets:
      name_prefix = generate_kernel_name_prefix(target)
      volumeSum = self.Q['kp']
      for i in range(3):
        volumeSum += self.db.kDivM[i][self.t('kl')] * self.I['lq'] * self.starMatrix(i)['qp']
      if self.sourceMatrix():
        volumeSum += self.I['kq'] * self.sourceMatrix()['qp']
      volume = (self.Q['kp'] <= volumeSum)
      generator.add(f'{name_prefix}volume', volume, target=target)

      localFlux = lambda i: self.Q['kp'] <= self.Q['kp'] + self.db.rDivM[i][self.t('km')] * self.db.fMrT[i][self.t('ml')] * self.I['lq'] * self.AplusT['qp']
      localFluxPrefetch = lambda i: self.I if i == 0 else (self.Q if i == 1 else None)
      generator.addFamily(f'{name_prefix}localFlux',
                          simpleParameterSpace(4),
                          localFlux,
                          localFluxPrefetch,
                          target=target)

      localFluxNodal = lambda i: self.Q['kp'] <= self.Q['kp'] + self.db.project2nFaceTo3m[i]['kn'] * self.INodal['no'] * self.AminusT['op']
      localFluxNodalPrefetch = lambda i: self.I if i == 0 else (self.Q if i == 1 else None)
      generator.addFamily(f'{name_prefix}localFluxNodal',
                          simpleParameterSpace(4),
                          localFluxNodal,
                          localFluxNodalPrefetch,
                          target=target)

  def addNeighbor(self, generator, targets):
    for target in targets:
      name_prefix = generate_kernel_name_prefix(target)
      neighbourFlux = lambda h,j,i: self.Q['kp'] <= self.Q['kp'] + self.db.rDivM[i][self.t('km')] * self.db.fP[h][self.t('mn')] * self.db.rT[j][self.t('nl')] * self.I['lq'] * self.AminusT['qp']
      neighbourFluxPrefetch = lambda h,j,i: self.I
      generator.addFamily(f'{name_prefix}neighboringFlux',
                          simpleParameterSpace(3,4,4),
                          neighbourFlux,
                          neighbourFluxPrefetch,
                          target=target)

  def addTime(self, generator, targets):
    for target in targets:
      name_prefix = generate_kernel_name_prefix(target)

      qShape = (self.numberOf3DBasisFunctions(), self.numberOfQuantities())
      dQ0 = OptionalDimTensor('dQ(0)', self.Q.optName(), self.Q.optSize(), self.Q.optPos(), qShape, alignStride=True)
      power = Scalar('power')
      derivatives = [dQ0]
      generator.add(f'{name_prefix}derivativeTaylorExpansion(0)',
                    self.I['kp'] <= power * dQ0['kp'],
                    target=target)

      for i in range(1,self.order):
        derivativeSum = Add()
        if self.sourceMatrix():
          derivativeSum += derivatives[-1]['kq'] * self.sourceMatrix()['qp']
        for j in range(3):
          derivativeSum += self.db.kDivMT[j][self.t('kl')] * derivatives[-1]['lq'] * self.starMatrix(j)['qp']

        derivativeSum = DeduceIndices( self.Q['kp'].indices ).visit(derivativeSum)
        derivativeSum = EquivalentSparsityPattern().visit(derivativeSum)
        dQ = OptionalDimTensor('dQ({})'.format(i), self.Q.optName(), self.Q.optSize(), self.Q.optPos(), qShape, spp=derivativeSum.eqspp(), alignStride=True)

        generator.add(f'{name_prefix}derivative({i})', dQ['kp'] <= derivativeSum, target=target)
        generator.add(f'{name_prefix}derivativeTaylorExpansion({i})',
                      self.I['kp'] <= self.I['kp'] + power * dQ['kp'],
                      target=target)

        derivatives.append(dQ)

  def add_include_tensors(self, include_tensors):
    super().add_include_tensors(include_tensors)
    include_tensors.add(self.db.nodes2D)

max_stars_repo_name: AnikoWirp/SeisSol


text:
import numpy as np

#from explicit_RK import *
#from expressions import *

def Fm(t, X0, uh):
    F = []
    for i, xn in enumerate(uh.Mi):
        uh.x = xn
        F.append(uh.F_m(X0, t, i)[0])
    return np.vstack(F)

def FDM_time(timegrid, Xi, uh):
    solution = dict()
    for dt in timegrid:
        solution[dt] = Xi
        Xi = Xi + dt*Fm(dt, Xi, uh)
    return solution

# Mb = np.array([
#     #[0., 0.],
#     [0., 0.5],
#     [0.5, 1.],
#     #[1., 1.],
#     [1., 0.5],
#     [0.5, 0.],
#     #[0.75, 0.],
#     #[0.75,1.],
#     #[1, 0.75],
#     #[0, 0.75],
#     #Zero
#     #[0., 1.],
#     #[1., 0.],
#     #[1, 1/3],
#     #[0, 2/3],
#     #[1/6, 1],
#     #[1/4, 0]
# ])

# print('X0')
# print(X0)
# X0 = X0 + 0.01*Fm(X0, uh)
# print('X1')
# print(X0)
# X0 = X0 + 0.02*Fm(X0, uh)
# print('X2')
# print(X0)
# X0 = X0 + 0.03*Fm(X0, uh)
# print('X3')
# print(X0)
# X0 = X0 + 0.04*Fm(X0, uh)
# print('X4')
# print(X0)
# uh.x = [0.5, 0.333333333]
# Fm(X0, uh)

# df = pd.DataFrame(np.hstack((uh.Mi, X0)), columns=['x', 'y', 'u', 'v'])

# cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True)
# sns.scatterplot(x='x', y='y', data=df, hue='u', palette=cmap)
# plt.show()
#system = explicit_RungeKutta(Fm, X0, t0, te, N, uh)
# system.solve()
#S = system.solution
# print(uh.Mi)
# m= uh.M()
# q2 = uh.Q2()
# k1 = uh.K1()
# q1 = uh.Q1()
# mq=np.hstack((m, q1))
# mqt=np.vstack((m.T, q1.T))
# mult1 = np.matmul()

# k2 = uh.K2()
# q2 = uh.Q2()
# o2 = uh.O2()

# q = uh.q
# qm_1 = q(Mb[:,0].reshape(-1,1), Mb[:,1].reshape(-1,1), 1)
# qm_2 = q(Mb[:,0].reshape(-1,1), Mb[:,1].reshape(-1,1), 2)
# qm_3 = q(Mb[:,0].reshape(-1,1), Mb[:,1].reshape(-1,1), 3)
#print(np.hstack((qm_1, qm_2, qm_3)))

# Mi = HaltonPoints(2, nf*4).haltonPoints()
# df_b = pd.DataFrame(Mb, columns=['x', 'y'])
# df_b['Type'] = 'Boundary'
# df_i = pd.DataFrame(Mi, columns=['x', 'y'])
# df_i['Type'] = 'Interior'
# df = pd.concat([df_i, df_b])
# sns.scatterplot(x='x', y='y', data=df, hue='Type')
# plt.legend(loc='upper left')
# #plt.show()
# K = np.vstack((np.hstack((k1, m, q1)), np.hstack((m.T, k2, q2)), np.hstack((q1.T, q2.T, o2))))

# A = np.vstack((np.hstack((k2, q2)), np.hstack((q2.T, o2))))
# _, s, __ = np.linalg.svd(K)
# print('Condition Number: {:,.2f}'.format(max(s)/min(s)))
# print('Determinant K: {:,.2f}'.format(np.linalg.det(K)))
# print('Determinant A: {:,.2f}'.format(np.linalg.det(A)))
#print(S[:, 0])
# # t = S[:, 0]
# # y = S[:, 1]
# a = np.vstack(tuple(exact_sol(timegrid)))
# print(a)
# error = np.linalg.norm(np.vstack(y)-a, axis=-1)
# plt.plot(np.vstack(t), error)
# plt.show()


text:
import asyncio
import colorsys
import functools
import matplotlib.pyplot as plt
import numpy as np
import pyaudio
import sys
import select

from .bulb import Bulb

np.set_printoptions(suppress=True) # don't use scientific notation

CHUNK = 4096 # number of data points to read at a time
RATE = 44100 # time resolution of the recording device (Hz)

async def analyze_sound(stream):
    while stream.get_read_available() < CHUNK:
        await asyncio.sleep(0.01)
    data = np.fromstring(stream.read(CHUNK), dtype=np.int16)
    # Smooth the FFT by windowing data
    data = data * np.hanning(len(data))
    fft = abs(np.fft.fft(data).real)
    # Keep only first half(negative)
    fft = fft[:int(len(fft)/2)]
    freq = np.fft.fftfreq(CHUNK, 1.0/RATE)
    # Keep only first half(negative)
    freq = freq[:int(len(freq)/2)]
    freqPeak = freq[np.where(fft==np.max(fft))[0][0]] + 1
    return (freqPeak, np.max(fft))

data = {
    'MAX_FREQ': 2500,
    'MIN_FREQ': 50,
    'MAX_VOL': 2000000,
    'MIN_VOL': 50000,
}

def get_percent(val, mi, mx):
    val = max(mi, min(val, mx))
    return (val - mi) / (mx - mi)

def hex2(val):
    h = hex(val)[2:]
    return h if len(h) == 2 else '0' + h

async def main_loop(bulb, stream):
    while True:
        freq, volume = await analyze_sound(stream)
        h = get_percent(freq, data['MIN_FREQ'], data['MAX_FREQ'])
        s = 1.0
        v = get_percent(volume, data['MIN_VOL'], data['MAX_VOL'])
        rgb = colorsys.hsv_to_rgb(h, s, v)
        rgb_hex = ''.join([hex2(int(255 * x)) for x in rgb])
        assert len(rgb_hex) == 6
        # print("change color: %s" % rgb_hex)
        bulb.set_color(1.0, rgb_hex)

async def get_user_input():
    print("Hi:", end='')
    while True:
        await asyncio.sleep(0.01)
        i, o, e = select.select([sys.stdin], [], [], 0.01)
        if not i:
            continue
        return sys.stdin.readline().strip()

async def command_loop(ioloop):
    while True:
        cmd = await get_user_input()
        if cmd == 'q':
            ioloop.stop()

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: %s BULB_MAC" % sys.argv[0])
        sys.exit(-1)

    bulb = Bulb(sys.argv[1])
    bulb.connect()
    bulb.set_mode(True, True)

    pa = pyaudio.PyAudio()
    stream = pa.open(
                format=pyaudio.paInt16, channels=1, rate=RATE, input=True,
                frames_per_buffer=CHUNK)

    event_loop = asyncio.get_event_loop()
    asyncio.ensure_future(main_loop(bulb, stream))
    asyncio.ensure_future(command_loop(event_loop))

    try:
        event_loop.run_forever()
    except:
        event_loop.close()
        bulb.set_mode(False, True)
        bulb.set_warm_brightness(0.1)
        stream.stop_stream()
        stream.close()
        pa.terminate()


text:
# --------------------------------------------------------
 # ROSITA
 # Licensed under The Apache License 2.0 [see LICENSE for details]
 # Written by Yuhao Cui and Tong-An Luo
 # --------------------------------------------------------

import torch, datetime, logging, random, copy
import torch.nn as nn
import torch.optim as Optim
import torch.multiprocessing as mp
import numpy as np

import os, argparse, yaml
from config.cfg import Cfg
from modeling.finetune_tasks.rec import Net

import torch.distributed as dist
from data.load_data_rec import DataSet
from utils.optimizer import BertAdam, WarmupOptimizer
from utils.sampler import SubsetDistributedSampler
from utils.segment import TextSegment
from utils.rec.bbox_transform import clip_boxes, bbox_transform_inv
from utils.rec.bbox import bbox_overlaps

from torch.nn.parallel import DistributedDataParallel as DDP

class Execution:
    def __init__(self, cfg, Net, RUN_MODE):
        self.cfg = cfg
        self.Net = Net
        self.RUN_MODE = RUN_MODE
        torch.manual_seed(cfg.SEED)
        torch.cuda.manual_seed(cfg.SEED)
        torch.cuda.manual_seed_all(cfg.SEED)
        np.random.seed(cfg.SEED)
        random.seed(cfg.SEED)
        torch.backends.cudnn.benchmark = True
        torch.set_printoptions(profile="full")
        np.set_printoptions(threshold=1e9)

    def get_optim(self, net, epoch_steps=None):
        if self.cfg.NET_OPTIM in ['warmup_adam']:
            net_optim = WarmupOptimizer(
                self.cfg.NET_LR_BASE,
                Optim.Adam(net.parameters(), lr=0, betas=self.cfg.OPT_BETAS, eps=self.cfg.OPT_EPS, weight_decay=self.cfg.NET_WEIGHT_DECAY),
                epoch_steps,
                warmup=self.cfg.NET_OPTIM_WARMUP,
                warmup_epochs=self.cfg.WARMUP_EPOCHS,
            )
            net_optim_inside = net_optim.optimizer

        elif self.cfg.NET_OPTIM in ['bert_adam']:
            net_optim = BertAdam(net.parameters(), lr=self.cfg.NET_LR_BASE,
                                 warmup=self.cfg.WARMUP_EPOCHS / self.cfg.OPTIM_EPOCHS,
                                 t_total=epoch_steps * self.cfg.OPTIM_EPOCHS, weight_decay=self.cfg.NET_WEIGHT_DECAY)

            net_optim_inside = net_optim

        return net_optim, net_optim_inside

    def train(self, train_loader, eval_loader):
        init_map = {
            'vocab_size': train_loader.dataset.vocab_size,
        }

        net = self.Net(self.cfg, init_map)
        net_optim, net_optim_inside = self.get_optim(net, epoch_steps=len(train_loader))

        net.to(self.cfg.DEVICE_IDS[0])
        net = DDP(net, device_ids=self.cfg.DEVICE_IDS)

        # Loading model weight
        if self.cfg.CKPT_LOAD:
            logging.info('Load Checkpoint')
            path = self.cfg.CKPT_FILE
            logging.info('Loading the {}'.format(path))

            rank0_devices = [x - self.cfg.LRANK * len(self.cfg.DEVICE_IDS) for x in self.cfg.DEVICE_IDS]
            device_pairs = zip(rank0_devices, self.cfg.DEVICE_IDS)
            map_location = {'cuda:%d' % x: 'cuda:%d' % y for x, y in device_pairs}
            ckpt = torch.load(path, map_location=map_location)
            logging.info('Checkpoint Loaded')

            for weight_key in self.cfg.CKPT_LOAD_MAP:
                if weight_key not in ['net_optim', 'epoch']:
                    weight_load = ckpt[self.cfg.CKPT_LOAD_MAP[weight_key]]
                    strict = True
                    getattr(net.module, weight_key).load_state_dict(weight_load, strict=strict)
                elif weight_key in ['net_optim']:
                    net_optim_inside.load_state_dict(ckpt[self.cfg.CKPT_LOAD_MAP[weight_key]])
                else:   # weight_key is epoch
                    self.cfg.CKPT_EPOCH = ckpt[self.cfg.CKPT_LOAD_MAP[weight_key]]

            if 'net_optim' in self.cfg.CKPT_LOAD_MAP:
                start_epoch = self.cfg.CKPT_EPOCH
                net_optim.set_start_step(start_epoch * len(train_loader))
            else:
                start_epoch = 0

        else:
            start_epoch = 0

        total_loss_sum = 0
        refs_rank_loss_sum = 0
        refs_reg_loss_sum = 0
        named_params = list(net.named_parameters())
        grad_norm = np.zeros(len(named_params))

        for epoch in range(start_epoch, self.cfg.MAX_EPOCH):
            proc_rank = self.cfg.GRANK if self.cfg.MP_STORAGE_SHR['ckpt'] else self.cfg.LRANK
            if proc_rank == 0:
                logfile = open(os.path.join(self.cfg.LOG_PATH, (self.cfg.VERSION + '.txt')), 'a+')
                logfile.write('[epoch {} start time: '.format(epoch + 1) + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ']\n')
                logfile.close()

            train_loader.sampler.set_epoch(epoch)
            net.train()

            if epoch in self.cfg.NET_LR_DECAY_LIST:
                net_optim.decay(self.cfg.NET_LR_DECAY_R)

            for step, train_data in enumerate(train_loader):
                proc_rank = self.cfg.GRANK if self.cfg.MP_STORAGE_SHR['screen'] else self.cfg.LRANK
                if step % 1000 == 0 and proc_rank == 0:
                    logging.info('[Epoch Trained: {:.1f} %][Lr: {:.7f}]'.format(step / len(train_loader) * 100.,
                                                                                np.array(net_optim.get_lr()).mean()))

                text_input_ids, text_mask, imgfeat_input, imgfeat_mask, imgfeat_bbox, \
                refs_gt_boxes, refs_img_boxes, refs_img_shape, \
                refs_rank_label, refs_rank_weight, refs_rank_loss_valid, \
                refs_reg_label, refs_reg_weight, refs_reg_loss_valid = train_data

                text_input_ids = text_input_ids.to(self.cfg.DEVICE_IDS[0])
                text_mask = text_mask.to(self.cfg.DEVICE_IDS[0])
                imgfeat_input = imgfeat_input.to(self.cfg.DEVICE_IDS[0])
                imgfeat_mask = imgfeat_mask.to(self.cfg.DEVICE_IDS[0])
                imgfeat_bbox = imgfeat_bbox.to(self.cfg.DEVICE_IDS[0])
                net_input = (text_input_ids, text_mask, imgfeat_input, imgfeat_mask, imgfeat_bbox)

                # network step
                net_optim.zero_grad()
                net_output = net(net_input)
                pooled_output, text_output, imgfeat_output, pred_refs_rank, pred_refs_reg = net_output

                loss_input = \
                    pred_refs_rank, pred_refs_reg, \
                    refs_rank_label, refs_rank_weight, refs_rank_loss_valid, \
                    refs_reg_label, refs_reg_weight, refs_reg_loss_valid
                total_loss, losses = net.module.loss(loss_input)
                # for avoid backward the unused params
                total_loss += 0 * sum(p.sum() for p in net.parameters())

                total_loss.backward()

                total_loss_sum += total_loss.item()
                refs_rank_loss_sum += losses[0].item()
                refs_reg_loss_sum += losses[1].item()

                proc_rank = self.cfg.GRANK if self.cfg.MP_STORAGE_SHR['screen'] else self.cfg.LRANK
                if step % 100 == 0 and proc_rank == 0:
                    logging.info(
                        '[epoch: {}][step: {} | {}] - [total loss: {:.4f}][refs_rank loss: {:.4f}][refs_reg: {:.4f}]'.format(
                            epoch + 1, step, len(train_loader), total_loss.item(), losses[0].item(), losses[1].item()))

                # gradient clipping
                if self.cfg.NET_GRAD_CLIP > 0:
                    nn.utils.clip_grad_norm_(net.parameters(), self.cfg.NET_GRAD_CLIP)
                net_optim.step()

            epoch_finish = epoch + 1
            proc_rank = self.cfg.GRANK if self.cfg.MP_STORAGE_SHR['ckpt'] else self.cfg.LRANK
            if proc_rank == 0:
                state = {}
                for weight_key in self.cfg.CKPT_SAVE_MAP:
                    if weight_key not in ['net_optim', 'epoch']:
                        state[self.cfg.CKPT_SAVE_MAP[weight_key]] = getattr(net.module, weight_key).state_dict()
                    elif weight_key in ['net_optim']:
                        state[self.cfg.CKPT_SAVE_MAP[weight_key]] = net_optim_inside.state_dict()
                    else:   # weight_key is epoch
                        state[self.cfg.CKPT_SAVE_MAP[weight_key]] = epoch + 1

                save_model_path = os.path.join(self.cfg.CKPT_SAVE_PATH, (self.cfg.VERSION + '_epoch' + str(
                    epoch_finish) + '.pkl'))
                torch.save(state, save_model_path)
                last_model_path = os.path.join(self.cfg.CKPT_SAVE_PATH, 'last_ckpt.pkl')
                torch.save(state, last_model_path)

                logfile = open(os.path.join(self.cfg.LOG_PATH, (self.cfg.VERSION + '.txt')), 'a+')
                logfile.write(
                    '[epoch: {}][lr: {:.7f}]\n[total loss: {:.4f}][refs_rank loss: {:.4f}][refs_reg: {:.4f}]\n'.format(
                        epoch_finish, np.array(net_optim.get_lr()).mean(), total_loss_sum / len(train_loader),
                        refs_rank_loss_sum / len(train_loader), refs_reg_loss_sum / len(train_loader)))
                logfile.close()

            dist.barrier()

            total_loss_sum = 0
            refs_rank_loss_sum = 0
            refs_reg_loss_sum = 0
            grad_norm = np.zeros(len(named_params))

            if eval_loader is not None:
                self.eval(eval_loader, net=net)

    def eval(self, loader, net=None):
        init_map = {
            'vocab_size': loader.dataset.vocab_size,
        }

        if net is None:
            logging.info('Load Checkpoint')
            path = self.cfg.CKPT_FILE
            logging.info('Loading the {}'.format(path))

            rank0_devices = [x - self.cfg.LRANK * len(self.cfg.DEVICE_IDS) for x in self.cfg.DEVICE_IDS]
            device_pairs = zip(rank0_devices, self.cfg.DEVICE_IDS)
            map_location = {'cuda:%d' % x: 'cuda:%d' % y for x, y in device_pairs}
            ckpt = torch.load(path, map_location=map_location)
            logging.info('Checkpoint Loaded')

            net = self.Net(self.cfg, init_map)

            net.to(self.cfg.DEVICE_IDS[0])
            net = DDP(net, device_ids=self.cfg.DEVICE_IDS)

            for weight_key in self.cfg.CKPT_SAVE_MAP:
                if weight_key not in ['net_optim', 'epoch']:
                    getattr(net.module, weight_key).load_state_dict(ckpt[self.cfg.CKPT_SAVE_MAP[weight_key]])

        net.eval()
        loader.sampler.set_shuffle(False)

        with torch.no_grad():
            orin_reg_weight, orin_reg_bias = None, None
            if self.cfg.BBOX_NORM:
                for name, params in net.named_parameters():
                    if 'mm_refs_head.dense_reg.weight' in name:
                        orin_reg_weight = copy.deepcopy(params.data)
                        params.data = params.data * torch.unsqueeze(
                            torch.from_numpy(np.array(self.cfg.BBOX_NORM_STDS)).to(self.cfg.DEVICE_IDS[0]).float(), 1)
                    if 'mm_refs_head.dense_reg.bias' in name:
                        orin_reg_bias = copy.deepcopy(params.data)
                        params.data = params.data * torch.from_numpy(np.array(self.cfg.BBOX_NORM_STDS)).to(
                            self.cfg.DEVICE_IDS[0]).float() + torch.from_numpy(np.array(self.cfg.BBOX_NORM_MEANS)).to(
                            self.cfg.DEVICE_IDS[0]).float()

            conut_acc = 0
            conut = 0
            for step, data in enumerate(loader):
                proc_rank = self.cfg.GRANK if self.cfg.MP_STORAGE_SHR['eval'] else self.cfg.LRANK
                if step % 10 == 0 and proc_rank == 0:
                    logging.info('Evaluated [{:.1f} %]'.format(step / len(loader) * 100.))

                text_input_ids, text_mask, imgfeat_input, imgfeat_mask, imgfeat_bbox, \
                refs_gt_boxes, refs_img_boxes, refs_img_shape, \
                refs_rank_label, refs_rank_weight, refs_rank_loss_valid, \
                refs_reg_label, refs_reg_weight, refs_reg_loss_valid = data

                text_input_ids = text_input_ids.to(self.cfg.DEVICE_IDS[0])
                text_mask = text_mask.to(self.cfg.DEVICE_IDS[0])
                imgfeat_input = imgfeat_input.to(self.cfg.DEVICE_IDS[0])
                imgfeat_mask = imgfeat_mask.to(self.cfg.DEVICE_IDS[0])
                imgfeat_bbox = imgfeat_bbox.to(self.cfg.DEVICE_IDS[0])

                net_input = (text_input_ids, text_mask, imgfeat_input, imgfeat_mask, imgfeat_bbox)
                net_output = net(net_input)
                pooled_output, text_output, imgfeat_output, pred_refs_rank, pred_refs_reg = net_output
                refs_gt_boxes, refs_img_boxes, refs_img_shape = refs_gt_boxes.numpy(), refs_img_boxes.numpy(), refs_img_shape.numpy()
                pred_refs_rank, pred_refs_reg = pred_refs_rank.cpu().data.numpy(), pred_refs_reg.cpu().data.numpy()

                bbox_reg = bbox_transform_inv(
                    refs_img_boxes.reshape(-1, 4), pred_refs_reg.reshape(-1, 4)).reshape(-1, self.cfg.PAD_MAX['image'], 4)
                arg_pred_refs_rank = np.argmax(pred_refs_rank, axis=1)

                for step_ix in range(pred_refs_rank.shape[0]):
                    cliped_bbox_reg_ix = clip_boxes(bbox_reg[step_ix], refs_img_shape[step_ix])
                    overlaps = bbox_overlaps(
                        np.ascontiguousarray(cliped_bbox_reg_ix[arg_pred_refs_rank[step_ix]][np.newaxis, :], dtype=np.float),
                        np.ascontiguousarray(refs_gt_boxes[step_ix], dtype=np.float))[:, 0]
                    conut += 1
                    conut_acc += int(overlaps[0] >= self.cfg.OVERLAP_THRESHOLD)

            if self.cfg.BBOX_NORM:
                for name, params in net.named_parameters():
                 if 'mm_refs_head.dense_reg.weight' in name:
                     params.data = orin_reg_weight
                 if 'mm_refs_head.dense_reg.bias' in name:
                     params.data = orin_reg_bias

            conut_acc = torch.tensor([conut_acc]).to(self.cfg.DEVICE_IDS[0])
            torch.distributed.all_reduce(conut_acc)
            conut_acc = conut_acc.item()

            conut = torch.tensor([conut]).to(self.cfg.DEVICE_IDS[0])
            torch.distributed.all_reduce(conut)
            conut = conut.item()

            accuracy = conut_acc / float(conut) * 100.
            proc_rank = self.cfg.GRANK if self.cfg.MP_STORAGE_SHR['eval'] else self.cfg.LRANK
            if proc_rank == 0:
                logging.info("Overall Accuracy is: %.02f\n\n" % (accuracy))
                print('accuracy = ' + str(accuracy) + ' %')
                logfile = open(os.path.join(self.cfg.LOG_PATH, (self.cfg.VERSION + '.txt')), 'a+')
                logfile.write("Overall Accuracy is: %.02f\n\n" % (accuracy))
                logfile.close()

    def run(self):
        spacy_tool = None

        if self.RUN_MODE in ['train']:
            train_text_segment = None
            if self.cfg.SEGMENT_TEXT:
                train_text_segment = TextSegment(self.cfg, self.RUN_MODE)

            train_dataset = DataSet(self.cfg, self.RUN_MODE, text_segment=train_text_segment, spacy_tool=spacy_tool)
            train_sampler = SubsetDistributedSampler(train_dataset, shuffle=True)
            train_loader = torch.utils.data.DataLoader(
                train_dataset,
                batch_size=self.cfg.BATCH_SIZE,
                sampler=train_sampler,
                num_workers=self.cfg.NUM_WORKERS,
                drop_last=True
            )

            eval_text_segment = None
            eval_loader = None
            if self.cfg.EVAL_EVERY_EPOCH:
                if self.cfg.SEGMENT_TEXT:
                    eval_text_segment = TextSegment(self.cfg, 'val')

                eval_dataset = DataSet(self.cfg, 'val', text_segment=eval_text_segment, spacy_tool=spacy_tool)
                eval_sampler = SubsetDistributedSampler(eval_dataset, shuffle=False)
                eval_loader = torch.utils.data.DataLoader(
                    eval_dataset,
                    batch_size=self.cfg.EVAL_BATCH_SIZE,
                    sampler=eval_sampler,
                    num_workers=self.cfg.NUM_WORKERS
                )

            self.train(train_loader, eval_loader)

        elif self.RUN_MODE in ['val', 'test']:
            eval_text_segment = None
            if self.cfg.SEGMENT_TEXT:
                eval_text_segment = TextSegment(self.cfg, self.RUN_MODE)

            eval_dataset = DataSet(self.cfg, self.RUN_MODE, text_segment=eval_text_segment, spacy_tool=spacy_tool)
            eval_sampler = SubsetDistributedSampler(eval_dataset, shuffle=False)
            eval_loader = torch.utils.data.DataLoader(
                eval_dataset,
                batch_size=self.cfg.EVAL_BATCH_SIZE,
                sampler=eval_sampler,
                num_workers=self.cfg.NUM_WORKERS
            )

            self.eval(eval_loader)

def mp_entrance(local_rank, world_size, args, __C):
    os.environ['MASTER_ADDR'] = args.MASTER_ADDR
    os.environ['MASTER_PORT'] = args.MASTER_PORT
    # initialize the process group
    global_rank = args.NODE_ID * world_size + local_rank
    __C.set_rank(global_rank, local_rank)
    dist.init_process_group("nccl", rank=global_rank, world_size=world_size * args.NODE_SIZE, timeout=datetime.timedelta(minutes=120))

    exec = Execution(__C, Net, __C.RUN_MODE)
    exec.run()

def parse_args():
    parser = argparse.ArgumentParser(description='Multi-Node Args')
    parser.add_argument('--NS', dest='NODE_SIZE', default=1, type=int)
    parser.add_argument('--NI', dest='NODE_ID', default=0, type=int)
    parser.add_argument('--MA', dest='MASTER_ADDR', default='127.0.0.1', type=str)
    parser.add_argument('--MP', dest='MASTER_PORT', default='auto', type=str)
    parser.add_argument('--gpu', dest='GPU', default='0, 1, 2, 3', type=str)
    parser.add_argument('--config', dest='config_file', default='', type=str)
    parser.add_argument('--resume', action='store_true')

    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = parse_args()
    os.environ['CUDA_VISIBLE_DEVICES'] = args.GPU

    WORLD_SIZE = len(args.GPU.split(','))
    __C = Cfg(WORLD_SIZE, args)
    args_dict = __C.parse_to_dict(args)

    with open(args.config_file, 'r') as f:
        yaml_dict = yaml.load(f, Loader=yaml.FullLoader)

    args_dict = {**yaml_dict, **args_dict}
    __C.add_args(yaml_dict)
    __C.proc(args.resume)
    print(__C)
    if args.MASTER_PORT == 'auto':
        args.MASTER_PORT = str(random.randint(13390, 17799))
    print('MASTER_ADDR:', args.MASTER_ADDR)
    print('MASTER_PORT:', args.MASTER_PORT)
    mp.spawn(
        mp_entrance,
        args=(WORLD_SIZE, args, __C),
        nprocs=WORLD_SIZE,
        join=True
    )

max_stars_repo_name: MILVLG/rosita

max_forks_repo_name: MILVLG/rosita