text:
# Copyright (c) Facebook, Inc. and its affiliates.

# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import one_hot_categorical
from utils import sample_stgs, one_hot
import pdb
import numpy as np

def create_agents(opt):

    # AGENT A
    listenerA=ListenerModuleReinf(opt.vocab_size,opt.symb_embedding_size,
                    opt.rnn_h_size,opt.max_length,opt.EOS_token)
    speakerA=SpeakerModuleReinf(opt.vocab_size,opt.symb_embedding_size,
                    opt.rnn_h_size,opt.max_length,
                    opt.EOS_token)
    proposalGenA=ProposalGenerateModuleReinf(opt.n_choices,opt.features_size)
    im_embedderA=FruitProcessorModule(opt.fruit_feat_size,opt.im_embedding_size)
    tool_embedderA=ToolProcessorModule(opt.n_tools,opt.tool_feat_size,
                    opt.tool_embedding_size)
    bodyA=BodyModule(opt.parsed_input_size,opt.body_features_size,
                        opt.features_size)
    agentA=Agent(listenerA,speakerA,proposalGenA,
                im_embedderA,tool_embedderA,bodyA,opt)
    # AGENT B
    listenerB=ListenerModuleReinf(opt.vocab_size,opt.symb_embedding_size,
                    opt.rnn_h_size,opt.max_length,opt.EOS_token)
    speakerB=SpeakerModuleReinf(opt.vocab_size,opt.symb_embedding_size,
                    opt.rnn_h_size,opt.max_length,
                    opt.EOS_token)
    proposalGenB=ProposalGenerateModuleReinf(opt.n_choices,opt.features_size)
    im_embedderB=FruitProcessorModule(opt.fruit_feat_size,opt.im_embedding_size)
    tool_embedderB=ToolProcessorModule(opt.n_tools,opt.tool_feat_size,
                    opt.tool_embedding_size)

    bodyB=BodyModule(opt.parsed_input_size,opt.body_features_size,
                        opt.features_size)
    agentB=Agent(listenerB,speakerB,proposalGenB,
                    im_embedderB,tool_embedderB,bodyB,opt)
    return agentA, agentB

class BodyModule(nn.Module):

    def __init__(self,parsed_input_size,body_features_size,features_size):
        super(BodyModule, self).__init__()
        self.body_features_size=body_features_size
        self.lin=nn.Linear(parsed_input_size+features_size,features_size,
                    bias=True)

    def forward(self,input_emb,com_emb,prev_state):
        cat_x=torch.cat([input_emb,com_emb,prev_state],dim=-1)
        out=torch.tanh(self.lin(cat_x))
        return out

class Agent(nn.Module):

    def __init__(self,listener,speaker,proposalGen,
                        im_embedder,tool_embedder,body,opt):
        super(Agent, self).__init__()
        self.listener=listener
        self.speaker=speaker
        self.proposalGen=proposalGen
        self.im_embedder=im_embedder
        self.tool_embedder=tool_embedder
        self.body=body
        self.block_mess=opt.block_mess
        self.history=opt.history
        dummy_message=torch.zeros(speaker.max_length,1,speaker.vocab_size)
        dummy_message[:,:,0]=1
        self.dummy_message=dummy_message
        self.h0=torch.zeros(1,self.speaker.rnn_h_size)
        self.h=None
        self.z=None

    def reset_agent(self):
        self.h=None
        self.z=None

    def embed_input(self,images_vectors, tools_vectors):
        if images_vectors is not None:
            input_emb=self.im_embedder(images_vectors)
        # atm we consider each agent receives only 1 input (tool or im)
        else:
            input_emb=self.tool_embedder(tools_vectors)
        self.input_emb=input_emb

    def forward(self,comm_tm1,train=True,id_input=None):

        device=comm_tm1.device
        batch_size=comm_tm1.shape[1]
        if self.block_mess:
            dummy_comm=self.dummy_message.repeat(1,batch_size,1).to(device)
            mess_emb=self.listener(dummy_comm)
        else:
            mess_emb=self.listener(comm_tm1)
        if self.h is None:
            self.h=self.h0.repeat(batch_size,1).to(device)

        z=self.body(self.input_emb,mess_emb,self.h)
        self.z=z
        comm_t,log_pcomm_t=self.speaker(z,train)
        if self.history:
            self.h=self.z
        else:
            self.h=None
        proposal_t,log_pchoice_t=self.proposalGen(z,train)
        return proposal_t,comm_t,log_pchoice_t,log_pcomm_t

class ListenerModuleReinf(nn.Module):
    def __init__(self,vocab_size,embedding_size,rnn_h_size,max_length,EOS_token):
        super(ListenerModuleReinf,self).__init__()
        self.vocab_size=vocab_size
        self.embedding_size=embedding_size
        self.rnn_h_size=rnn_h_size
        self.max_length=max_length
        self.embeddingTable=nn.Linear(self.vocab_size,self.embedding_size,bias=True)
        self.EOS_token=EOS_token
        self.rnn=nn.RNN(self.embedding_size,self.rnn_h_size,1)

    def forward_rnn(self,x_t,h_tm1,mask_t=None):

        h_t=torch.tanh(self.lin_x(x_t)+self.lin_h(h_tm1))

        if mask_t is not None:
            h_t=h_t*mask_t+h_tm1*(1-mask_t)
        return h_t

    def process_sentence_no_mask(self,received_message,seq_embeddings,h0):

        device=received_message.device
        batch_size=received_message.size(1)
        # rnn input is of size seq_length x batch_size x embedding_size
        output,hidden=self.rnn(seq_embeddings,h0)
        eos_bool=received_message[:,:,self.EOS_token].t()
        mess_length=(eos_bool.cumsum(dim=1).eq(0)).sum(1)
        # If no EOS, then mess_length is max_length
        mess_length=mess_length.clamp(max=self.max_length-1)
        # Take the hidden layer of EOS token or end if no EOS
        h_t=output[mess_length,range(batch_size),:]
        return h_t

    def forward(self,received_message):
        device=received_message.device
        # received_message is of size seq_length x batch_size x vocab_size
        batch_size=received_message.size(1)
        seq_length=received_message.size(0)

        # seq_embeddings is of size batch_size x seq_length x embedding_size
        mess_t=received_message.permute(1,0,2).contiguous().view(batch_size*seq_length,-1)
        seq_embeddingsV=self.embeddingTable(mess_t).view(batch_size,
                                                        seq_length,-1)
        # start hidden state 1 x batch_size x rnn_h_size
        h0=torch.zeros(1,batch_size,self.rnn_h_size,device=device)
        repr_message=self.process_sentence_no_mask(received_message,
                                                seq_embeddingsV.permute(1,0,2),
                                                h0)
        return repr_message

class FruitProcessorModule(nn.Module):
    def __init__(self,im_features_size,im_embedding_size):
        super(FruitProcessorModule,self).__init__()
        self.im_embedding_size=im_features_size
        self.lin=nn.Linear(im_features_size,im_embedding_size,bias=True)

    def forward(self,x):
        out=torch.tanh(self.lin(x))
        return out

class ToolProcessorModule(nn.Module):
    def __init__(self,n_tools,tool_features_size,tool_embedding_size):
        super(ToolProcessorModule,self).__init__()
        self.n_tools=n_tools
        self.tool_features_size=tool_features_size
        per_tool_embedding_size=int(tool_embedding_size/n_tools)
        self.lin=nn.Linear(tool_features_size,per_tool_embedding_size,bias=True)
    def forward(self,x):
        # x is of dim 2 (n_tools) x batch_size x tool_feat_size
        # shared weights
        out1=torch.tanh(self.lin(x[0,:,:]))
        out=torch.cat([out1],dim=-1)
        for t in range(1,self.n_tools):
            out2=torch.tanh(self.lin(x[t,:,:]))
            out=torch.cat([out,out2],dim=-1)
        return out

class ProposalGenerateModuleReinf(nn.Module):
    # proposal is sharing (scalar),tool choice (0/1),termination (binary 0/1)
    def __init__(self,n_choices,features_size):
        super(ProposalGenerateModuleReinf,self).__init__()
        self.n_choices=n_choices
        self.features_size=features_size
        self.lin_tool=nn.Linear(features_size,n_choices,bias=True)

    def forward(self,z,train=True):
        device=z.device
        # z of size batch_size x rnn_h_size
        prop_proba_no_sft=self.lin_tool(z)
        log_p=F.log_softmax(prop_proba_no_sft,dim=-1)
        if train:
            m=torch.distributions.categorical.Categorical(logits=log_p)
            choice_s=m.sample()
        else:
            choice_s=log_p.argmax(-1)
        prop_choice=one_hot(choice_s,self.n_choices)
        sh=0.5*torch.ones(z.size(0),1,device=device)
        proposal=torch.cat([sh,prop_choice.float()],dim=-1)
        return proposal,log_p

class SpeakerModuleReinf(nn.Module):
    def __init__(self,vocab_size,embedding_size,rnn_h_size,
                    max_length,EOS_token):
        super(SpeakerModuleReinf,self).__init__()
        self.vocab_size=vocab_size
        self.embedding_size=embedding_size
        self.rnn_h_size=rnn_h_size
        self.embeddingTable=nn.Linear(self.vocab_size,self.embedding_size,
                                                                bias=True)
        self.rnn=nn.RNN(self.embedding_size,self.rnn_h_size,1)
        self.lin=nn.Linear(rnn_h_size, vocab_size,bias=True)
        self.max_length=max_length
        self.EOS_token=EOS_token

    def forward(self,z,train):
        device=z.device
        # z of size batch_size x rnn_h_size
        batch_size=z.size(0)
        seq_length=0

        hidden=z.unsqueeze(0)
        # initialization: use Linear + logsumexp + sampling
        proba_no_sft=self.lin(hidden.squeeze(0))
        sequences=[]
        log_p=F.log_softmax(proba_no_sft,dim=-1)
        if train:
            m=torch.distributions.categorical.Categorical(logits=log_p)
            sample=m.sample()
            samp_symb=one_hot(sample,self.vocab_size)
        else:
            sample=log_p.argmax(-1)

        samp_symb=one_hot(sample,self.vocab_size)
        log_p_sent=log_p[range(batch_size),sample]
        binary=samp_symb[:,self.EOS_token]
        mask_t=(1.-binary)
        sequences=[samp_symb.unsqueeze(0)]

        for seq_length in range(1,self.max_length):
            input=self.embeddingTable(samp_symb).unsqueeze(0)
            output,hidden=self.rnn(input, hidden)
            # use last layer and last step hidden
            proba_no_sft=self.lin(hidden.squeeze(0))
            log_p=F.log_softmax(proba_no_sft,dim=-1)
            if train:
                m=torch.distributions.categorical.Categorical(logits=log_p)
                sample=m.sample()
                samp_symb=one_hot(sample,self.vocab_size)
            else:
                sample=log_p.argmax(-1)

            samp_symb=one_hot(sample,self.vocab_size)
            log_p_sent+=mask_t*log_p[range(batch_size),sample]
            binary=samp_symb[:,self.EOS_token]
            mask_t=mask_t*(1.-binary)
            sequences.append(samp_symb.unsqueeze(0))

        sentences=torch.cat(sequences,dim=0)
        return sentences,log_p_sent

    def log_proba_input_sentence(self,z,input_sentence):
        # z of size batch_size x rnn_h_size
        batch_size=z.size(0)
        seq_length=0

        hidden=z.unsqueeze(0)
        # initialization: use Linear + logsumexp + sampling
        proba_no_sft=self.lin(hidden.squeeze(0))
        log_p=F.log_softmax(proba_no_sft,dim=-1)
        # the sample is one-hot
        samp_symb=input_sentence[seq_length,:,:]
        sample=samp_symb.argmax(-1)
        log_p_sent=log_p[range(batch_size),sample]

        binary=samp_symb[:,self.EOS_token]
        mask_t=(1.-binary)
        for seq_length in range(1,self.max_length):
            input=self.embeddingTable(samp_symb).unsqueeze(0)
            output,hidden=self.rnn(input, hidden)
            proba_no_sft=self.lin(hidden.squeeze(0))
            log_p=F.log_softmax(proba_no_sft,dim=-1)

            samp_symb=input_sentence[seq_length,:,:]
            sample=samp_symb.argmax(-1)
            log_p_sent+=mask_t*log_p[range(batch_size),sample]
            binary=samp_symb[:,self.EOS_token]
            mask_t=mask_t*(1.-binary)

        return log_p_sent

max_stars_repo_name: facebookresearch/fruit-tools-game

max_issues_repo_name: facebookresearch/fruit-tools-game

max_forks_repo_name: facebookresearch/fruit-tools-game


text:
'''
Created on Oct 17, 2016

--dia_act_nl_pairs.v6.json: agt and usr have their own NL.

@author: xiul
'''

import pickle
import copy, argparse, json
import numpy as np

#import dialog_config
from deep_dialog import dialog_config
#from deep_dialog.nlg.lstm_decoder_tanh import lstm_decoder_tanh

class nlg:
    def __init__(self):
        pass

    def post_process(self, pred_template, slot_val_dict, slot_dict):
        """ post_process to fill the slot in the template sentence """

        sentence = pred_template
        suffix = "_PLACEHOLDER"

        for slot in slot_val_dict.keys():
            slot_vals = slot_val_dict[slot]
            slot_placeholder = slot + suffix
            if slot == 'result' or slot == 'numberofpeople': continue
            if slot_vals == dialog_config.NO_VALUE_MATCH: continue
            tmp_sentence = sentence.replace(slot_placeholder, slot_vals, 1)
            sentence = tmp_sentence

        if 'numberofpeople' in slot_val_dict.keys():
            slot_vals = slot_val_dict['numberofpeople']
            slot_placeholder = 'numberofpeople' + suffix
            tmp_sentence = sentence.replace(slot_placeholder, slot_vals, 1)
            sentence = tmp_sentence

        for slot in slot_dict.keys():
            slot_placeholder = slot + suffix
            tmp_sentence = sentence.replace(slot_placeholder, '')
            sentence = tmp_sentence

        return sentence

    def convert_taskcomplete_to_nl(self, dia_act):
            template_utt = "Okay, I will booked $numberofpeople$ tickets for $moviename$ starting at $starttime$ at $theater$ at $city$ on $date$."
            slots = ["numberofpeople", "moviename", "starttime", "theater", "city", "date"]
            sentence = template_utt
            for slot in slots:
                sentence = sentence.replace("$"+slot+"$",dia_act["inform_slots"][slot])

            return sentence

    def convert_diaact_to_nl(self, dia_act, turn_msg):
        """ Convert Dia_Act into NL: Rule + Model """

        sentence = ""
        boolean_in = False

        # remove I do not care slot in task(complete)

        if dia_act['diaact'] in self.diaact_nl_pairs['dia_acts'].keys():
            for ele in self.diaact_nl_pairs['dia_acts'][dia_act['diaact']]:
                if set(ele['inform_slots']) == set(dia_act['inform_slots'].keys()) and set(ele['request_slots']) == set(dia_act['request_slots'].keys()):
                    sentence = self.diaact_to_nl_slot_filling(dia_act, ele['nl'][turn_msg])
                    boolean_in = True
                    break

        if dia_act['diaact'] == 'inform' and 'taskcomplete' in dia_act['inform_slots'].keys() and dia_act['inform_slots']['taskcomplete'] == dialog_config.NO_VALUE_MATCH:
            sentence = "Oh sorry, there is no ticket available."

        if boolean_in == False: sentence = "No sentence is available"
        return sentence

    def diaact_to_nl_slot_filling(self, dia_act, template_sentence):
        """ Replace the slots with its values """

        sentence = str(template_sentence, 'utf-8')
        counter = 0
        for slot in dia_act['inform_slots'].keys():
            slot_val = dia_act['inform_slots'][slot]
            if slot_val == dialog_config.NO_VALUE_MATCH:
                sentence = "No matching " + slot + " is available!"
                break
            elif slot_val == dialog_config.I_DO_NOT_CARE:
                counter += 1
                sentence = sentence.replace('$'+slot+'$', '', 1)
                continue

            sentence = sentence.replace('$'+slot+'$', slot_val, 1)

        if counter > 0 and counter == len(dia_act['inform_slots']):
            sentence = dialog_config.I_DO_NOT_CARE

        return sentence

    def load_predefine_act_nl_pairs(self, path):
        """ Load some pre-defined Dia_Act&NL Pairs from file """

        self.diaact_nl_pairs = json.load(open(path, 'rb'))

        for key in self.diaact_nl_pairs['dia_acts'].keys():
            for ele in self.diaact_nl_pairs['dia_acts'][key]:
                ele['nl']['usr'] = ele['nl']['usr'].encode('utf-8') # encode issue
                ele['nl']['agt'] = ele['nl']['agt'].encode('utf-8') # encode issue

def main(params):
    pass

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    args = parser.parse_args()
    params = vars(args)
    """"
    nlg = nlg()
    nlg.load_predefine_act_nl_pairs("/home/thisara/Documents/FYP/Simulator_selfplay-master/src/deep_dialog/data/dia_act_nl_pairs.v6.json")
    nlg.convert_diaact_to_nl({'diaact': 'inform', 'inform_slots': {'starttime': 'PLACEHOLDER'}, 'request_slots': {}},'agt')
    """

    main(params)

ext: py

reason: import numpy


text:
import sys
import os
from astropy.units import ys
from gevent.hub import sleep

sys.path.insert(0, os.path.abspath('..'))

import random
import numpy as np
import matplotlib.pyplot as plt
from assignment2.cs231n.layers import affine_forward
from assignment2.cs231n.layers import affine_backward
from assignment2.cs231n.layers import relu_forward
from assignment2.cs231n.layers import relu_backward
from assignment2.cs231n.layers import svm_loss
from assignment2.cs231n.layers import softmax_loss
from assignment2.cs231n.data_utils import get_CIFAR10_data
from assignment2.cs231n.gradient_check import eval_numerical_gradient, eval_numerical_gradient_array
from assignment2.cs231n.solver import Solver
from assignment2.cs231n.layer_utils import affine_relu_forward, affine_relu_backward
from assignment2.cs231n.data_utils import load_CIFAR10
from assignment2.cs231n.optim import sgd_momentum
from assignment2.cs231n.optim import rmsprop
from assignment2.cs231n.optim import adam
from assignment2.cs231n.fast_layers import *
from assignment2.cs231n.classifiers.cnn import *
from assignment2.cs231n.classifiers.fc_net import *
# import time
from scipy.misc import imread, imresize
from cs231n.fast_layers import conv_forward_fast, conv_backward_fast
from time import time

class ConvNet(object):
    def __init__(self):

        return
    def imshow_noax(self, img, normalize=True):
        """ Tiny helper to show images as uint8 and remove axis labels """
        if normalize:
            img_max, img_min = np.max(img), np.min(img)
            img = 255.0 * (img - img_min) / (img_max - img_min)
        plt.imshow(img.astype('uint8'))
        plt.gca().axis('off')

    def rel_error(self, x, y):
        """ returns relative error """
        return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))
    def check_conv_naive_forward(self):
        x_shape = (2, 3, 4, 4)
        w_shape = (3, 3, 4, 4)
        x = np.linspace(-0.1, 0.5, num=np.prod(x_shape)).reshape(x_shape)
        w = np.linspace(-0.2, 0.3, num=np.prod(w_shape)).reshape(w_shape)
        b = np.linspace(-0.1, 0.2, num=3)

        conv_param = {'stride': 2, 'pad': 1}
        out, _ = conv_forward_naive(x, w, b, conv_param)
        correct_out = np.array([[[[[-0.08759809, -0.10987781],
                                   [-0.18387192, -0.2109216 ]],
                                  [[ 0.21027089,  0.21661097],
                                   [ 0.22847626,  0.23004637]],
                                  [[ 0.50813986,  0.54309974],
                                   [ 0.64082444,  0.67101435]]],
                                 [[[-0.98053589, -1.03143541],
                                   [-1.19128892, -1.24695841]],
                                  [[ 0.69108355,  0.66880383],
                                   [ 0.59480972,  0.56776003]],
                                  [[ 2.36270298,  2.36904306],
                                   [ 2.38090835,  2.38247847]]]]])

        # Compare your output to ours; difference should be around 1e-8
        print 'Testing conv_forward_naive'
        print 'difference: ', self.rel_error(out, correct_out)
        return
    def check_max_pooling_naive_forward(self):
        x_shape = (2, 3, 4, 4)
        x = np.linspace(-0.3, 0.4, num=np.prod(x_shape)).reshape(x_shape)
        pool_param = {'pool_width': 2, 'pool_height': 2, 'stride': 2}

        out, _ = max_pool_forward_naive(x, pool_param)

        correct_out = np.array([[[[-0.26315789, -0.24842105],
                                  [-0.20421053, -0.18947368]],
                                 [[-0.14526316, -0.13052632],
                                  [-0.08631579, -0.07157895]],
                                 [[-0.02736842, -0.01263158],
                                  [ 0.03157895,  0.04631579]]],
                                [[[ 0.09052632,  0.10526316],
                                  [ 0.14947368,  0.16421053]],
                                 [[ 0.20842105,  0.22315789],
                                  [ 0.26736842,  0.28210526]],
                                 [[ 0.32631579,  0.34105263],
                                  [ 0.38526316,  0.4       ]]]])

        # Compare your output with ours. Difference should be around 1e-8.
        print 'Testing max_pool_forward_naive function:'
        print 'difference: ', self.rel_error(out, correct_out)
        return
    def check_max_pooling_naive_backward(self):
        x = np.random.randn(3, 2, 8, 8)
        dout = np.random.randn(3, 2, 4, 4)
        pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

        dx_num = eval_numerical_gradient_array(lambda x: max_pool_forward_naive(x, pool_param)[0], x, dout)

        out, cache = max_pool_forward_naive(x, pool_param)
        dx = max_pool_backward_naive(dout, cache)

        # Your error should be around 1e-12
        print 'Testing max_pool_backward_naive function:'
        print 'dx error: ', self.rel_error(dx, dx_num)
        return
    def check_conv_naive_backward(self):
        x = np.random.randn(4, 3, 5, 5)
        w = np.random.randn(2, 3, 3, 3)
        b = np.random.randn(2,)
        dout = np.random.randn(4, 2, 5, 5)
        conv_param = {'stride': 1, 'pad': 1}

        dx_num = eval_numerical_gradient_array(lambda x: conv_forward_naive(x, w, b, conv_param)[0], x, dout)
        dw_num = eval_numerical_gradient_array(lambda w: conv_forward_naive(x, w, b, conv_param)[0], w, dout)
        db_num = eval_numerical_gradient_array(lambda b: conv_forward_naive(x, w, b, conv_param)[0], b, dout)

        out, cache = conv_forward_naive(x, w, b, conv_param)
        dx, dw, db = conv_backward_naive(dout, cache)

        # Your errors should be around 1e-9'
        print 'Testing conv_backward_naive function'
        print 'dx error: ', self.rel_error(dx, dx_num)
        print 'dw error: ', self.rel_error(dw, dw_num)
        print 'db error: ', self.rel_error(db, db_num)
        return

    def get_CIFAR10_data(self, num_training=49000, num_validation=1000, num_test=1000):
        """
        Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
        it for the two-layer neural net classifier. These are the same steps as
        we used for the SVM, but condensed to a single function.
        """
        # Load the raw CIFAR-10 data
        cifar10_dir = '../../assignment1/cs231n/datasets/cifar-10-batches-py'
        X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

        X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

        # Subsample the data
        mask = range(num_training, num_training + num_validation)
        X_val = X_train[mask]
        y_val = y_train[mask]
        mask = range(num_training)
        X_train = X_train[mask]
        y_train = y_train[mask]
        mask = range(num_test)
        X_test = X_test[mask]
        y_test = y_test[mask]

        # Normalize the data: subtract the mean image
        mean_image = np.mean(X_train, axis=0)
        X_train -= mean_image
        X_val -= mean_image
        X_test -= mean_image

        # Transpose so that channels come first
        X_train = X_train.transpose(0, 3, 1, 2).copy()
        X_val = X_val.transpose(0, 3, 1, 2).copy()
        X_test = X_test.transpose(0, 3, 1, 2).copy()

        # Package data into a dictionary
        return {
          'X_train': X_train, 'y_train': y_train,
          'X_val': X_val, 'y_val': y_val,
          'X_test': X_test, 'y_test': y_test,
        }

        return
    def check_imgpreprocess_conv(self):

        kitten, puppy = imread('../kitten.jpg'), imread('../puppy.jpg')
        # kitten is wide, and puppy is already square
        d = kitten.shape[1] - kitten.shape[0]
        kitten_cropped = kitten[:, d/2:-d/2, :]

        img_size = 200   # Make this smaller if it runs too slow
        x = np.zeros((2, 3, img_size, img_size))
        x[0, :, :, :] = imresize(puppy, (img_size, img_size)).transpose((2, 0, 1))
        x[1, :, :, :] = imresize(kitten_cropped, (img_size, img_size)).transpose((2, 0, 1))

        # Set up a convolutional weights holding 2 filters, each 3x3
        w = np.zeros((2, 3, 3, 3))

        # The first filter converts the image to grayscale.
        # Set up the red, green, and blue channels of the filter.
        w[0, 0, :, :] = [[0, 0, 0], [0, 0.3, 0], [0, 0, 0]]
        w[0, 1, :, :] = [[0, 0, 0], [0, 0.6, 0], [0, 0, 0]]
        w[0, 2, :, :] = [[0, 0, 0], [0, 0.1, 0], [0, 0, 0]]

        # Second filter detects horizontal edges in the blue channel.
        w[1, 2, :, :] = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]

        # Vector of biases. We don't need any bias for the grayscale
        # filter, but for the edge detection filter we want to add 128
        # to each output so that nothing is negative.
        b = np.array([0, 128])

        # Compute the result of convolving each input in x with each filter in w,
        # offsetting by b, and storing the results in out.
        out, _ = conv_forward_naive(x, w, b, {'stride': 1, 'pad': 1})
        # Show the original images and the results of the conv operation
        plt.subplot(2, 3, 1)
        self.imshow_noax(puppy, normalize=False)
        plt.title('Original image')
        plt.subplot(2, 3, 2)
        self.imshow_noax(out[0, 0])
        plt.title('Grayscale')
        plt.subplot(2, 3, 3)
        self.imshow_noax(out[0, 1])
        plt.title('Edges')
        plt.subplot(2, 3, 4)
        self.imshow_noax(kitten_cropped, normalize=False)
        plt.subplot(2, 3, 5)
        self.imshow_noax(out[1, 0])
        plt.subplot(2, 3, 6)
        self.imshow_noax(out[1, 1])
        plt.show()
        return
    def check_fast_conv(self):

        x = np.random.randn(100, 3, 31, 31)
        w = np.random.randn(25, 3, 3, 3)
        b = np.random.randn(25,)
        dout = np.random.randn(100, 25, 16, 16)
        conv_param = {'stride': 2, 'pad': 1}

        t0 = time()
        out_naive, cache_naive = conv_forward_naive(x, w, b, conv_param)
        t1 = time()
        out_fast, cache_fast = conv_forward_fast(x, w, b, conv_param)
        t2 = time()

        print 'Testing conv_forward_fast:'
        print 'Naive: %fs' % (t1 - t0)
        print 'Fast: %fs' % (t2 - t1)
        print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
        print 'Difference: ', self.rel_error(out_naive, out_fast)

        t0 = time()
        dx_naive, dw_naive, db_naive = conv_backward_naive(dout, cache_naive)
        t1 = time()
        dx_fast, dw_fast, db_fast = conv_backward_fast(dout, cache_fast)
        t2 = time()

        print '\nTesting conv_backward_fast:'
        print 'Naive: %fs' % (t1 - t0)
        print 'Fast: %fs' % (t2 - t1)
        print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
        print 'dx difference: ', self.rel_error(dx_naive, dx_fast)
        print 'dw difference: ', self.rel_error(dw_naive, dw_fast)
        print 'db difference: ', self.rel_error(db_naive, db_fast)
        return
    def check_maxfast(self):
        from assignment2.cs231n.fast_layers import max_pool_forward_fast, max_pool_backward_fast

        x = np.random.randn(100, 3, 32, 32)
        dout = np.random.randn(100, 3, 16, 16)
        pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

        t0 = time()
        out_naive, cache_naive = max_pool_forward_naive(x, pool_param)
        t1 = time()
        out_fast, cache_fast = max_pool_forward_fast(x, pool_param)
        t2 = time()

        print 'Testing pool_forward_fast:'
        print 'Naive: %fs' % (t1 - t0)
        print 'fast: %fs' % (t2 - t1)
        print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
        print 'difference: ', self.rel_error(out_naive, out_fast)

        t0 = time()
        dx_naive = max_pool_backward_naive(dout, cache_naive)
        t1 = time()
        dx_fast = max_pool_backward_fast(dout, cache_fast)
        t2 = time()

        print '\nTesting pool_backward_fast:'
        print 'Naive: %fs' % (t1 - t0)
        print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
        print 'dx difference: ', self.rel_error(dx_naive, dx_fast)
        return
    def check_conv_relu_pool(self):
        from assignment2.cs231n.layer_utils import conv_relu_pool_forward, conv_relu_pool_backward

        x = np.random.randn(2, 3, 16, 16)
        w = np.random.randn(3, 3, 3, 3)
        b = np.random.randn(3,)
        dout = np.random.randn(2, 3, 8, 8)
        conv_param = {'stride': 1, 'pad': 1}
        pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

        out, cache = conv_relu_pool_forward(x, w, b, conv_param, pool_param)
        dx, dw, db = conv_relu_pool_backward(dout, cache)

        dx_num = eval_numerical_gradient_array(lambda x: conv_relu_pool_forward(x, w, b, conv_param, pool_param)[0], x, dout)
        dw_num = eval_numerical_gradient_array(lambda w: conv_relu_pool_forward(x, w, b, conv_param, pool_param)[0], w, dout)
        db_num = eval_numerical_gradient_array(lambda b: conv_relu_pool_forward(x, w, b, conv_param, pool_param)[0], b, dout)

        print 'Testing conv_relu_pool'
        print 'dx error: ', self.rel_error(dx_num, dx)
        print 'dw error: ', self.rel_error(dw_num, dw)
        print 'db error: ', self.rel_error(db_num, db)
        return
    def check_conv_relu(self):
        from assignment2.cs231n.layer_utils import conv_relu_forward, conv_relu_backward

        x = np.random.randn(2, 3, 8, 8)
        w = np.random.randn(3, 3, 3, 3)
        b = np.random.randn(3,)
        dout = np.random.randn(2, 3, 8, 8)
        conv_param = {'stride': 1, 'pad': 1}

        out, cache = conv_relu_forward(x, w, b, conv_param)
        dx, dw, db = conv_relu_backward(dout, cache)

        dx_num = eval_numerical_gradient_array(lambda x: conv_relu_forward(x, w, b, conv_param)[0], x, dout)
        dw_num = eval_numerical_gradient_array(lambda w: conv_relu_forward(x, w, b, conv_param)[0], w, dout)
        db_num = eval_numerical_gradient_array(lambda b: conv_relu_forward(x, w, b, conv_param)[0], b, dout)

        print 'Testing conv_relu:'
        print 'dx error: ', self.rel_error(dx_num, dx)
        print 'dw error: ', self.rel_error(dw_num, dw)
        print 'db error: ', self.rel_error(db_num, db)
        return
    def samity_check_threelayer(self):
        model = ThreeLayerConvNet()

        N = 50
        X = np.random.randn(N, 3, 32, 32)
        y = np.random.randint(10, size=N)

        loss, grads = model.loss(X, y)
        print 'Initial loss (no regularization): ', loss

        model.reg = 0.5
        loss, grads = model.loss(X, y)
        print 'Initial loss (with regularization): ', loss
        return
    def check_threelayer_gradient_check(self):
        num_inputs = 2
        input_dim = (3, 16, 16)
        reg = 0.0
        num_classes = 10
        X = np.random.randn(num_inputs, *input_dim)
        y = np.random.randint(num_classes, size=num_inputs)

        model = ThreeLayerConvNet(num_filters=3, filter_size=3,
                                  input_dim=input_dim, hidden_dim=7,
                                  dtype=np.float64)
        loss, grads = model.loss(X, y)
        for param_name in sorted(grads):
            f = lambda _: model.loss(X, y)[0]
            param_grad_num = eval_numerical_gradient(f, model.params[param_name], verbose=False, h=1e-6)
            e = self.rel_error(param_grad_num, grads[param_name])
            print '%s max relative error: %e' % (param_name, self.rel_error(param_grad_num, grads[param_name]))
        return
    def overfit_small_data(self):
        num_train = 100
        data = self.data
        small_data = {
          'X_train': data['X_train'][:num_train],
          'y_train': data['y_train'][:num_train],
          'X_val': data['X_val'],
          'y_val': data['y_val'],
        }

        model = ThreeLayerConvNet(weight_scale=1e-2)

        solver = Solver(model, small_data,
                        num_epochs=10, batch_size=50,
                        update_rule='adam',
                        optim_config={
                          'learning_rate': 1e-3,
                        },
                        verbose=True, print_every=1)
        solver.train()
        return
    def train_net(self):
        data = self.data
        num_train = data['X_train'].shape[0]
        small_data = {
          'X_train': data['X_train'][:num_train],
          'y_train': data['y_train'][:num_train],
          'X_val': data['X_val'],
          'y_val': data['y_val'],
        }
        model = ThreeLayerConvNet(weight_scale=0.001, hidden_dim=500, reg=0.001)

        solver = Solver(model, data,
                        num_epochs=1, batch_size=50,
                        update_rule='adam',
                        optim_config={
                          'learning_rate': 1e-3,
                        },
                        verbose=True, print_every=20)
        solver.train()
        from assignment2.cs231n.vis_utils import visualize_grid

        grid = visualize_grid(model.params['W1'].transpose(0, 2, 3, 1))
        plt.imshow(grid.astype('uint8'))
        plt.axis('off')
        plt.gcf().set_size_inches(5, 5)
        plt.show()

        return
    def check_spatial_batch_norm_train(self):
        # Check the training-time forward pass by checking means and variances
        # of features both before and after spatial batch normalization

        N, C, H, W = 2, 3, 4, 5
        x = 4 * np.random.randn(N, C, H, W) + 10

        print 'Before spatial batch normalization:'
        print '  Shape: ', x.shape
        print '  Means: ', x.mean(axis=(0, 2, 3))
        print '  Stds: ', x.std(axis=(0, 2, 3))

        # Means should be close to zero and stds close to one
        gamma, beta = np.ones(C), np.zeros(C)
        bn_param = {'mode': 'train'}
        out, _ = spatial_batchnorm_forward(x, gamma, beta, bn_param)
        print 'After spatial batch normalization:'
        print '  Shape: ', out.shape
        print '  Means: ', out.mean(axis=(0, 2, 3))
        print '  Stds: ', out.std(axis=(0, 2, 3))

        # Means should be close to beta and stds close to gamma
        gamma, beta = np.asarray([3, 4, 5]), np.asarray([6, 7, 8])
        out, _ = spatial_batchnorm_forward(x, gamma, beta, bn_param)
        print 'After spatial batch normalization (nontrivial gamma, beta):'
        print '  Shape: ', out.shape
        print '  Means: ', out.mean(axis=(0, 2, 3))
        print '  Stds: ', out.std(axis=(0, 2, 3))
        return
    def check_spatial_batch_norm_test(self):
        # Check the test-time forward pass by running the training-time
        # forward pass many times to warm up the running averages, and then
        # checking the means and variances of activations after a test-time
        # forward pass.

        N, C, H, W = 10, 4, 11, 12

        bn_param = {'mode': 'train'}
        gamma = np.ones(C)
        beta = np.zeros(C)
        for t in xrange(50):
            x = 2.3 * np.random.randn(N, C, H, W) + 13
            spatial_batchnorm_forward(x, gamma, beta, bn_param)
        bn_param['mode'] = 'test'
        x = 2.3 * np.random.randn(N, C, H, W) + 13
        a_norm, _ = spatial_batchnorm_forward(x, gamma, beta, bn_param)

        # Means should be close to zero and stds close to one, but will be
        # noisier than training-time forward passes.
        print 'After spatial batch normalization (test-time):'
        print '  means: ', a_norm.mean(axis=(0, 2, 3))
        print '  stds: ', a_norm.std(axis=(0, 2, 3))
        return
    def check_spatial_batch_norm_backward(self):
        N, C, H, W = 2, 3, 4, 5
        x = 5 * np.random.randn(N, C, H, W) + 12
        gamma = np.random.randn(C)
        beta = np.random.randn(C)
        dout = np.random.randn(N, C, H, W)

        bn_param = {'mode': 'train'}
        fx = lambda x: spatial_batchnorm_forward(x, gamma, beta, bn_param)[0]
        fg = lambda a: spatial_batchnorm_forward(x, gamma, beta, bn_param)[0]
        fb = lambda b: spatial_batchnorm_forward(x, gamma, beta, bn_param)[0]

        dx_num = eval_numerical_gradient_array(fx, x, dout)
        da_num = eval_numerical_gradient_array(fg, gamma, dout)
        db_num = eval_numerical_gradient_array(fb, beta, dout)

        _, cache = spatial_batchnorm_forward(x, gamma, beta, bn_param)
        dx, dgamma, dbeta = spatial_batchnorm_backward(dout, cache)
        print 'dx error: ', self.rel_error(dx_num, dx)
        print 'dgamma error: ', self.rel_error(da_num, dgamma)
        print 'dbeta error: ', self.rel_error(db_num, dbeta)
        return

    def run(self):
        self.data = self.get_CIFAR10_data()
#         self.check_conv_naive_forward()
#         self.check_max_pooling_naive_forward()
#         self.check_max_pooling_naive_backward()
#         self.check_conv_naive_forward()
#         self.check_conv_naive_backward()
#         self.check_imgpreprocess_conv()
#         self.check_fast_conv()
#         self.check_maxfast()
#         self.check_conv_relu_pool()
#         self.check_conv_relu()
#         self.samity_check_threelayer()
#         self.check_threelayer_gradient_check()
#         self.overfit_small_data()
#         self.train_net()
#         self.check_spatial_batch_norm_train()
#         self.check_spatial_batch_norm_test()
        self.check_spatial_batch_norm_backward()

        return

if __name__ == "__main__":
    obj= ConvNet()
    obj.run()

reason: import numpy,from scipy,from astropy


text:
#!/usr/bin/env python3

"""
jetstream.py makes beautiful maps of the atmospheric jet stream
using wind data from ECMWF.int.
"""
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

__version__ = "0.2"
__author__ = "Akkana Peck (akkana@shallowsky.com), Geert Barentsen (geert@barentsen.be)"
__copyright__ = "Copyright 2018 Akkana Peck, 2014 Geert Barentsen"
__license__ = "MIT"

import matplotlib as mpl
mpl.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib import colors
from mpl_toolkits import basemap
import numpy as np

import netCDF4

import datetime
import argparse
import math
import sys, os

# Decide on a cache directory
try:
    import xdg.BaseDirectory
    CACHEDIR = os.path.join(xdg.BaseDirectory.xdg_cache_home, "ECMWF")
except:
    print("Can't import xdg, will look for data in current directory")
    CACHEDIR = '.'

A = {'r': 52/255., 'g': 152/255., 'b': 219/255.}  # blue
B = {'r': 231/255., 'g': 76/255., 'b': 60/255.}   # red
C = {'r': 241/255., 'g': 196/255., 'b': 15/255.}  # orange

COLORMAP = colors.LinearSegmentedColormap('jetstream',
                                          {'red':   [(0.0, 1.0, 1.0),
                                                     (0.25, A['r'], A['r']),
                                                     (0.75, B['r'], B['r']),
                                                     (1.0, C['r'], C['r'])],
                                           'green': [(0.0, 1.0, 1.0),
                                                     (0.25, A['g'], A['g']),
                                                     (0.75, B['g'], B['g']),
                                                     (1.0, C['g'], C['g'])],
                                           'blue':  [(0.0, 1.0, 1.0),
                                                     (0.25, A['b'], A['b']),
                                                     (0.75, B['b'], B['b']),
                                                     (1.0, C['b'], C['b'])],
                                           # Alpha that sets land to grey,
                                           # water to white:
                                           'alpha': [(0.0, 0.0, 0.0),
                                                     (0.15, 1.0, 1.0),
                                                     (1.0, 1.0, 1.0)]})

class JetStreamMap():

    def __init__(self, lon1=-140, lon2=-40, lat1=20, lat2=70):
        self.lon1, self.lon2 = lon1, lon2
        self.lat1, self.lat2 = lat1, lat2

    def render(self, data, vmin=30, vmax=220, title=None):

        # I haven't found any way to get basemap to tell us the aspect ratio,
        # or to specify only one and let basemap calculate the other:
        # self.fig = plt.figure(figsize=(9, 6))
        # So just don't specify; basemap will give us something
        # that comes out 640px at 100dpi.
        self.fig = plt.figure()

        self.fig.subplots_adjust(0.05, 0.15, 0.95, 0.88,
                                 hspace=0.0, wspace=0.1)

        self.map = basemap.Basemap(projection='cyl',
                                   llcrnrlon=self.lon1, llcrnrlat=self.lat1,
                                   urcrnrlon=self.lon2, urcrnrlat=self.lat2,
                                   resolution="c", fix_aspect=True)

        self.map.pcolormesh(data.lon, data.lat, data.windspeed,
                            cmap=COLORMAP, vmin=vmin, vmax=vmax, alpha=None)
        self.colorbar = self.map.colorbar(location='bottom',
                                          pad=0.1, size=0.25,
                                          ticks=[100, 150, 200, 250])
        self.colorbar.ax.set_xlabel('Average wind speed at 250 mb (km/h)',
                                    fontsize=16)

        self.map.drawcoastlines(color='#7f8c8d', linewidth=0.5)
        self.map.fillcontinents('#bdc3c7', zorder=0)
        self.map.drawcountries(color='#7f8c8d', linewidth=0.5)
        self.map.drawstates(color='#7f8c8d', linewidth=0.5)
        self.fig.text(.05, .91, title, fontsize=24, ha='left')
        return self.fig

class JetStreamData():
    """Abstract base class, in case other data sources turn up"""

    def __init__(self):
        self.load()

    def create_map(self, level, timestr, west, east, south, north):
        if level == 250:
            title = "Jet Stream %s" % timestr
        else:
            title = "Winds %d mb %s" % (level, timestr)

        mymap = JetStreamMap(lon1=west, lon2=east, lat1=south, lat2=north)

        mymap.render(self, title=title)
        return mymap

class ERAJetStreamData(JetStreamData):

    def __init__(self, filename):
        if os.path.exists(filename):
            self.datafile = filename
        else:
            self.datafile = os.path.join(CACHEDIR, filename)
            if not os.path.exists(self.datafile):
                raise IOError(2, "Data file doesn't exist", filename)

        self.data = netCDF4.Dataset(self.datafile)

    def list_data(self):
        dic = {}
        for key in self.data.variables:
            vals = self.data[key][:]

            if key == 'time':
                timeunits = JSdata.data['time'].units
                cal = JSdata.data['time'].calendar
                starttime = netCDF4.num2date(vals[0], units=timeunits,
                                             calendar=cal)
                endtime = netCDF4.num2date(vals[-1], units=timeunits,
                                           calendar=cal)
                dic[key] = "%d values, ranging from %s to %s" % \
                            (len(vals),
                             starttime.strftime("%Y-%m-%d"),
                             endtime.strftime("%Y-%m-%d"))

            elif isinstance(vals, np.ndarray):
                if len(vals.shape) == 1:
                    if vals.shape[0] < 10:
                        dic[key] = vals
                    else:
                        dic[key] = '%d values ranging from %s to %s' %\
                                   (vals.shape[0],
                                    str(min(vals)), str(max(vals)))
                else:
                    dic[key] = "ndarray of shape %s" % str(vals.shape)

            elif len(vals) < 10:
                dic[key] = vals

            elif type(vals[0]) is int or type(vals[0]) is np.int32:
                dic[key] = "%s: ranging from %d to %d" % (key, min(vals),
                                                          max(vals))
            elif type(vals[0]) is float or type(vals[0]) is np.float32:
                dic[key] = "%s: ranging from %f to %f" % (key, min(vals),
                                                          max(vals))
            else:
                dic[key] = '%s: type %s' % (key, str(type(vals[0])))

        return dic
        # return self.data['level'][:]

    def calc_windspeed(self, idx, level, threshold=0):
        # times = netCDF4.num2date(data.variables['time'],
        # data.variables['time'].units)
        # print("times:", times)

        # Find the index for the appropriate pressure level
        for levindex, millibars in enumerate(self.data['level']):
            if millibars == level:
                break
        else:
            raise ValueError("No %d hPa level (maybe check with -L?)" % level)

        lon = self.data.variables['longitude'][:]
        lat = self.data.variables['latitude'][:]

        # Set a threshold or sensitivity factor
        if not threshold:
            if level == 250:         # jetstream, big winds
                threshold = 3.0
            else:                    # anywhere else, the winds are smaller
                threshold = 7.5

        windspeed = (threshold *
            np.sqrt(self.data.variables['u'][idx][levindex][:]**2
                    + self.data.variables['v'][idx][levindex][:]**2))

        # Shift grid from 0 to 360 => -180 to 180
        # windspeed, lon = basemap.shiftgrid(180, windspeed, lon, start=False)

        self.lon, self.lat, self.windspeed = lon, lat, windspeed

if __name__ == '__main__':

    def parse_args():
        """Parse commandline arguments."""
        parser = argparse.ArgumentParser()

        parser.add_argument('-t', '--threshold', action='store',
                            dest='threshold', type=float, default=0.0,
                            help="Threshold. Defaults to 3.0 for the jetstream"
                                 " and 7.5 for any other level.")
        parser.add_argument('-a', '--area', action='store',
                            type=int, nargs=4,
                            dest='area', default=[0, 0, 0, 0],
                            metavar=("west", "east", "south", "north"),
                            help="Area to plot"
                                 " (default: everything in the dataset.) "
                                 "For world, -180 180 -70 74; "
                                 "for North America try -a -138 -55 15 62).")
        parser.add_argument('-l', '--level', action='store', dest='level',
                            type=int, default=250,
                            help="Pressure level to plot, in millibars."
                                 " 1000 for sea level, 775 for 7000 feet,"
                                 " 250 for the jetstream")
        parser.add_argument('-o', '--outdir', action='store', dest='outdir',
                            default=None,
                            help="Output directory (will be created if needed)")
        parser.add_argument('-d', '--dpi', action='store', dest='dpi',
                            type=int, default=150,
                            help="DPI to save images (default 150)")

        parser.add_argument('-L', '--list-data', action="store_true",
                            default=False, dest='listdata',
                            help="List values available in the data file")

        parser.add_argument('datafile',
                            help="The datafile, in netCDF4 format, file.nc")

        return parser.parse_args(sys.argv[1:])

    args = parse_args()
    # print(args)

    JSdata = ERAJetStreamData(args.datafile)

    west, east, south, north = args.area
    if west == 0 and east == 0:
        west = min(JSdata.data['longitude'])
        east = max(JSdata.data['longitude'])
    if north == 0 and south == 0:
        south = min(JSdata.data['latitude'])
        north = max(JSdata.data['latitude'])
    print("WENS:", west, east, north, south)

    if args.listdata:
        print("In dataset %s:\n" % args.datafile)
        datadic = JSdata.list_data()
        for key in datadic:
            if type(datadic[key]) is str:
                print("%-10s: %s" % (key, datadic[key]))
            else:
                print("%s:" % key)
                for val in datadic[key]:
                    print("   ", val)
        sys.exit(0)

    if not args.outdir:
        args.outdir = 'outdir-%d-%s' % (args.level, args.threshold)
    if not os.path.exists(args.outdir):
        os.mkdir(args.outdir)
    elif not  os.path.isdir(args.outdir):
        print("Can't create dir %s: there's a file by that name" % args.outdir)
        sys.exit(1)

    timeunits = JSdata.data['time'].units
    cal = JSdata.data['time'].calendar
    for i, t in enumerate(JSdata.data['time']):
        thedate = netCDF4.num2date(t, units=timeunits, calendar=cal)
        timestr = thedate.strftime("%Y-%m-%d")

        JSdata.calc_windspeed(i, args.level, args.threshold)

        mymap = JSdata.create_map(args.level, timestr,
                                  west, east, south, north)

        figname = '%s/%s-%d.png' % (args.outdir, timestr, args.level)
        mymap.fig.savefig(figname, dpi=args.dpi)
        print(figname)
        plt.close()


text:
import numpy as np
import os
import sys
import progressbar
import gc
import pickle
import matplotlib.pyplot as plt
from LSTM import LSTM
from wrapper import Bidirectional
from Regularization import regularization
from attention_model import attention_model
from data_preprocessing import song_preprocessing
from functions import activations as act, helper_func as func
from sklearn.preprocessing import normalize, minmax_scale

class model:
    def __init__(self, X, Y, S, Tx, Ty, lr = 0.005, n_a = 64, n_s = 32, jump_step = 100, epoch = 100, sec = 5, optimizer = None):
        self.X = X
        self.Y = Y
        self.S = S
        self.Tx = Tx
        self.Ty = Ty
        self.lr = lr
        self.m = X.shape[0]
        self.n_x = X.shape[2]
        self.n_y = Y.shape[2]
        self.n_a = n_a
        self.n_s = n_s
        self.n_c = self.n_a * 2 # *2 when using bidirectional
        self.hidden_dimension = [10]
        self.jump_step = jump_step
        self.epoch = epoch
        self.sec = sec
        self.last_layer_hidden_state = None
        self.Att_As = []
        self.Att_caches = []
        self.Att_alphas = []
        # Wy shape = (n_s,n_y)
        self.Wy = func.xavier((self.n_s, self.n_y))
        self.by = np.zeros((1, self.n_y))
        self.optimizer = optimizer
        self.s_weight = 0
        self.s_bias = 0
        self.v_weight = 0
        self.v_bias = 0
        self.TRAINING_THRESHOLD = 0
        self.non_random_circle = 10
        self._params = {"Wy": self.Wy, "by": self.by}

        self.pre_LSTM = LSTM("pre_LSTM", (self.Tx, self.n_x), (self.Tx, self.n_a), optimizer = optimizer)
        self.pre_bi_LSTM = Bidirectional("pre_bi_LSTM", self.pre_LSTM, is_dropout = True)
        self.attention = attention_model("attention", self.n_c, self.S, self.n_s, self.n_c, self.hidden_dimension, optimizer = optimizer)
        self.post_LSTM = LSTM("post_LSTM", (self.Ty, self.n_c), (self.Ty, self.n_s), is_attention = True, optimizer = optimizer)

    def forward_propagation_one_ex(self, i, e):
        """
        description:
            forward propagation for one training example; data x label y
        ---parameter---
        i: index
        """
        # self.gradient_checking()
        # X = minmax_scale(self.X[i,:,:], feature_range = (0, 1), axis = 0)
        X = normalize(self.X[i,:,:], axis=1)

        A = self.pre_bi_LSTM.concatLSTM(X) # shape = (Tx, 2 * n_a)
        # A = self.pre_LSTM.forward_propagation(X)

        self.attention._A = A
        # attention and post_LSTM
        start = 0
        end = self.S
        prev_s = np.zeros((1, self.n_s))
        prev_a = np.zeros((1, self.n_s))
        lstm_S = []
        for t in range(self.Ty):
            alphas, c, _energies, _caches_t, current_A = self.attention.nn_forward_propagation(prev_s, start, end)
            start = start + self.jump_step
            end = end + self.jump_step

            # for backpropagation use ***** this step take 30% of RAM in total *******
            self.Att_As.append(current_A)
            self.Att_caches.append(_caches_t)
            self.Att_alphas.append(alphas)

            st, at, cache = self.post_LSTM.cell_forward(prev_s, prev_a, c)
            lstm_S.append(st)
            prev_s = st
            prev_a = at

        # convert lstm_S(list) to lstm_S(np array)
        lstm_S = np.array(lstm_S).reshape((self.Ty, self.n_s))

        self.last_layer_hidden_state = lstm_S
        del lstm_S
        # TODO: dropout lstm_S
        # lstm_S = act.dropout(lstm_S, level = 0.5)
        # initialize last layer Wy
        # st shape = (1,n_s)
        Y_hat = []
        for t in range(self.Ty): # st shape = (1, n_s)
            Zy = np.matmul(np.atleast_2d(self.last_layer_hidden_state[t,:]), self._params["Wy"]) + self._params["by"] # shape = (1, n_y)
            yt_hat = act.softmax(Zy)
            Y_hat.append(yt_hat.reshape(-1)) # yt_hat after reshape = (n_y,)

        # Y_hat shape = (Ty, n_y)
        Y_true = np.array(self.Y[i,:,:]) # (Ty, n_y)
        Y_hat = np.array(Y_hat)
        total_lost = 0
        for t in range(self.Ty):
            lost = func.t_lost(Y_true[t,:], Y_hat[t,:])
            total_lost = total_lost + lost

        total_lost = (total_lost/self.Ty)

        return total_lost, Y_hat, Y_true

    def backward_propagation_one_ex(self, Y_hat, Y_true, i, e, lr):
        """
        Description:
            backward propagation for one training example; data x label y
        ----parameter---
        Y_hat: predicted value given training data X
        Y_true: True label value of training data X
        """
        # dL = (1/self.Ty)
        # shape (Ty, n_y)
        dZ = (Y_hat - Y_true)
        assert(dZ.shape == (self.Ty, self.n_y))
        # calculate dWy and dby
        dWy = np.matmul(np.transpose(self.last_layer_hidden_state.reshape(self.Ty, self.n_s)), dZ)
        dby = np.atleast_2d(np.sum(dZ, axis = 0))
        self.update_weight(dWy, dby, e, lr, optimizer = self.optimizer)

        assert(dWy.shape == (self.n_s, self.n_y) and dby.shape == (1, self.n_y))
        #shape = (Ty, n_s)

        dS = np.matmul(dZ, np.transpose(self.Wy))
        d_AS_list = self.post_LSTM.backward_propagation(dS, self.Att_As, self.Att_caches, self.Att_alphas, self.attention)
        self.post_LSTM.update_weight(lr, e)
        self.attention.update_weight(lr, e)

        self.Att_As = []
        self.Att_caches = []
        self.Att_alphas = []

        self.pre_bi_LSTM.cell_backpropagation(d_AS_list, self.jump_step, self.Ty, self.Tx)
        self.pre_bi_LSTM.update_weight(lr, e)

    def update_weight(self, dWy, dby, i ,lr=0.005, optimizer = None, beta1 = 0.9, beta2 = 0.999, eps = 1e-8):

        i = i + 1
        lr = lr * np.sqrt(1-beta2**i)/(1-beta1**i)
        s_corrected_weight = None
        s_corrected_bias = None
        v_corrected_weight = None
        v_corrected_bias = None
        if optimizer == "Adam":
            self.s_weight = beta2 * self.s_weight + (1 - beta2) * (dWy ** 2)
            s_corrected_weight = self.s_weight / (1 - beta2**i)
            self.s_bias = beta2 * self.s_bias + (1 - beta2) * (dby ** 2)
            s_corrected_bias = self.s_bias / (1 - beta2**i)

            self.v_weight = beta1 * self.v_weight + (1 - beta1) * dWy
            v_corrected_weight = self.v_weight / (1 - beta1**i)
            self.v_bias = beta1 * self.v_bias + (1 - beta1) * dby
            v_corrected_bias = self.v_bias / (1 - beta1**i)

            self.Wy = self.Wy - lr*(v_corrected_weight/(np.sqrt(s_corrected_weight) + eps))
            self.by = self.by - lr*(v_corrected_bias/(np.sqrt(s_corrected_bias) + eps))
        else:
            self.Wy = self.Wy - lr*dWy
            self.by = self.by - lr*dby

        self._params["Wy"] = self.Wy
        self._params["by"] = self.by

        self.save_weights()

    def train(self, songs):
        lr = self.lr
        loss = []
        print("Starting to train Detector..........")
        for e in range(self.epoch):
            print("Epoch {}/{}".format(e, self.epoch))
            lost = 0
            for i in progressbar.progressbar(range(self.m)):
                total_lost, Y_hat, Y_true = self.forward_propagation_one_ex(i, e)
                lost = lost + total_lost
                self.backward_propagation_one_ex(Y_hat, Y_true, i, e, lr)
                self.predict(self.X[i,:,:], songs, "weights")
            loss.append(lost/self.m)
            if e % 100 == 0:
                print(loss)
            print("Total Loss: ", lost/self.m)

    def save_weights(self):
        with open("weights/predict_layer.pickle", "wb") as f:
            pickle.dump(self._params, f, protocol = pickle.HIGHEST_PROTOCOL)

    def predict(self, data, songs, folder):
        Tx, n_x = data.shape
        assert(Tx >= self.S)

        pre_LSTM = LSTM("pre_LSTM", (Tx, n_x), (Tx, self.n_a), optimizer = self.optimizer)
        pre_bi_LSTM = Bidirectional("pre_bi_LSTM", pre_LSTM)
        attention = attention_model("attention", self.n_c, self.S, self.n_s, self.n_c, self.hidden_dimension, optimizer = self.optimizer)
        post_LSTM = LSTM("post_LSTM", (self.Ty, self.n_c), (self.Ty, self.n_s), is_attention = True, optimizer = self.optimizer)

        LSTM_forward_params = pickle.load(open(folder + "/biDirectional_pre_LSTM_forward.pickle", "rb"))
        LSTM_backward_params = pickle.load(open(folder +"/biDirectional_pre_LSTM_backward.pickle", "rb"))
        attention_params = pickle.load(open(folder + "/attention.pickle", "rb"))
        post_LSTM_params = pickle.load(open(folder + "/post_LSTM.pickle", "rb"))
        params = pickle.load(open(folder + "/predict_layer.pickle", "rb"))

        pre_bi_LSTM.forward._params = LSTM_forward_params
        pre_bi_LSTM.backward._params = LSTM_backward_params
        attention._params = attention_params
        post_LSTM._params = post_LSTM_params

        Ty = song_preprocessing.get_Ty(Tx, self.S, self.jump_step)
        # data = minmax_scale(data, feature_range=(0, 1), axis=0)
        data = normalize(data)
        A = pre_bi_LSTM.concatLSTM(data)
        attention._A = A
        start = 0
        end = self.S

        prev_s = np.zeros((1, self.n_s))
        prev_a = np.zeros((1, self.n_s))

        lstm_S = []

        for t in range(Ty):
            alphas, c, _energies, _caches_t, current_A = attention.nn_forward_propagation(prev_s, start, end)
            start = start + self.jump_step
            end = end + self.jump_step

            st, at, cache = post_LSTM.cell_forward(prev_s, prev_a, c)
            lstm_S.append(st)
            prev_s = st
            prev_a = at

        lstm_S = np.array(lstm_S)
        # TODO: dropout lstm_S
        # lstm_S = act.dropout(lstm_S, level = 0.5)
        # initialize last layer Wy
        # st shape = (1,n_s)

        y_predict = 0
        for t in range(Ty): # st shape = (1, n_s)
            Zy = np.matmul(np.atleast_2d(lstm_S[t,:]), params["Wy"]) + params["by"] # shape = (1, n_y)
            yt_hat = act.softmax(Zy)
            y_predict = y_predict + yt_hat
        y_predict = y_predict / Ty
        print(y_predict)
        index = np.argmax(y_predict)
        print("Song predict: ", songs[index])
        return songs[index]

    def gradient_checking(self, dby, dWy, i, eps = 1e-4):
        model_vec, model_keys_shape = func.dictionary_to_vector(self._params)
        LSTM_forward_vec, LSTM_forward_keys_shape = func.dictionary_to_vector(self.pre_bi_LSTM.forward._params)
        LSTM_backward_vec, LSTM_backward_keys_shape = func.dictionary_to_vector(self.pre_bi_LSTM.backward._params)
        attention_vec, attention_keys_shape = func.dictionary_to_vector(self.attention._params)
        post_LSTM_vec, post_LSTM_keys_shape = func.dictionary_to_vector(self.post_LSTM._params)

        params_vector = np.concatenate([model_vec, LSTM_forward_vec, LSTM_backward_vec, attention_vec, post_LSTM_vec])
        remain_vector = None
        model_dict = {"dby": dby, "dWy": dWy}
        model_grads, model_grads_keys_shape = func.dictionary_to_vector(model_dict)
        LSTM_forward_grads, LSTM_forward_grads_keys_shape = func.dictionary_to_vector(self.pre_bi_LSTM.forward.gradients)
        LSTM_backward_grads, LSTM_backward_grads_keys_shape = func.dictionary_to_vector(self.pre_bi_LSTM.backward.gradients)
        attention_grads, attention_grads_keys_shape = func.dictionary_to_vector(self.attention.gradients_layer)
        post_LSTM_grads, post_LSTM_grads_keys_shape = func.dictionary_to_vector(self.post_LSTM.gradients)

        grads_vector = np.concatenate([model_grads, LSTM_forward_grads, LSTM_backward_grads, attention_grads, post_LSTM_grads])

        num_parameters = params_vector.shape[0]
        J_plus = np.zeros((num_parameters, 1))
        J_minus = np.zeros((num_parameters, 1))
        gradapprox = np.zeros((num_parameters, 1))
        for n in range(num_parameters):
            print("{}/{}".format(n,num_parameters))
            thetaplus = np.copy(params_vector)
            thetaplus[n] = thetaplus[n] + eps
            remain_vector, model_params = func.vector_to_dictionary(thetaplus, model_keys_shape)
            remain_vector, LSTM_forward_params = func.vector_to_dictionary(remain_vector, LSTM_forward_keys_shape)
            remain_vector, LSTM_backward_params = func.vector_to_dictionary(remain_vector, LSTM_backward_keys_shape)
            remain_vector, attention_params = func.vector_to_dictionary(remain_vector, attention_keys_shape)
            remain_vector, post_LSTM_params = func.vector_to_dictionary(remain_vector, post_LSTM_keys_shape)

            self._params = model_params
            self.pre_bi_LSTM.forward._params = LSTM_forward_params
            self.pre_bi_LSTM.backward._params = LSTM_backward_params
            self.attention._params = attention_params
            self.post_LSTM._params = post_LSTM_params

            J_plus[n], _, _ = self.forward_propagation_one_ex(i)

            thetaminus = np.copy(params_vector)
            thetaminus[n] = thetaminus[n] + eps
            remain_vector, model_params = func.vector_to_dictionary(thetaminus, model_keys_shape)
            remain_vector, LSTM_forward_params = func.vector_to_dictionary(remain_vector, LSTM_forward_keys_shape)
            remain_vector, LSTM_backward_params = func.vector_to_dictionary(remain_vector, LSTM_backward_keys_shape)
            remain_vector, attention_params = func.vector_to_dictionary(remain_vector, attention_keys_shape)
            remain_vector, post_LSTM_params = func.vector_to_dictionary(remain_vector, post_LSTM_keys_shape)

            self._params = model_params
            self.pre_bi_LSTM.forward._params = LSTM_forward_params
            self.pre_bi_LSTM.backward._params = LSTM_backward_params
            self.attention._params = attention_params
            self.post_LSTM._params = post_LSTM_params

            J_minus[n], _, _ = self.forward_propagation_one_ex(i)

            gradapprox[n] = (J_plus[n] - J_minus[n]) / (2 * eps)

        numerator = np.linalg.norm(grads_vector - gradapprox)
        demoninator = np.linalg.norm(grads_vector) + np.linalg.norm(gradapprox)
        difference = numerator / demoninator

        if difference > 1e-7:
            print("Wrong")
        else:
            print("Right")


text:
import sys
sys.path.append('../../')
import constants as cnst
import pandas
import numpy as np
import os
import glob

full_mdl = ['full_model']
ablated = ['vector_cond', 'flm_rndr_tex_interp', 'norm_mp_tex_interp', 'norm_map_rend_flm_no_tex_interp']

base_img_path = f'{cnst.output_root}/model_comparison/sample/inter_model_comparison/'

for model_original in full_mdl:
    mdl_1_path = os.path.join(base_img_path, model_original)
    list_images_mdl1 = glob.glob(os.path.join(mdl_1_path, 'mdl1_*'))
    # import ipdb; ipdb.set_trace()
    for model_abl in ablated:

        list_1 = []
        list_2 = []
        mesh_files = []
        np.random.seed(2)

        for mdl_1_img in list_images_mdl1:
            mdl_1_img = os.path.basename(mdl_1_img)
            original_model_goes_left = np.random.randint(2)

            mdl_1_img_full_url = cnst.amt_bucket_base_url + model_original + '/' + mdl_1_img
            mesh_files.append(mdl_1_img_full_url.replace('mdl1_', 'mesh'))
            mdl_2_full_url = mdl_1_img_full_url.replace(model_original, model_abl).replace('mdl1_', 'mdl2_')
            if original_model_goes_left:
                list_1.append(mdl_1_img_full_url)
                list_2.append(mdl_2_full_url)
            else:
                list_1.append(mdl_2_full_url)
                list_2.append(mdl_1_img_full_url)

            # import ipdb; ipdb.set_trace()

        df = pandas.DataFrame(data={"GT": mesh_files, "OPTION1": list_1, "OPTION2":list_2})
        csv_path = f'{cnst.output_root}sample/inter_model_comparison/input_csvs'
        df.to_csv(os.path.join(csv_path, f'{model_original}_VS_{model_abl}.csv'), sep=',', index=False)

max_stars_repo_name: qiaone/GIF

max_issues_repo_name: qiaone/GIF

max_forks_repo_name: qiaone/GIF


text:
import os
import warnings
from shutil import rmtree
from tempfile import mkdtemp
from typing import Dict, Iterable, Any

import numpy as np
import pandas as pd
from IPython.core.display import display, HTML
from keras.callbacks import EarlyStopping
from keras.layers import Dense, Input
from keras.models import Sequential
## sklearn KerasRegressor may be Deprecated
from scikeras.wrappers import KerasRegressor
from sklearn import set_config
from sklearn.compose import make_column_selector, make_column_transformer
from sklearn.ensemble import StackingRegressor, VotingRegressor
from sklearn.impute import SimpleImputer
from sklearn.linear_model import ElasticNet, HuberRegressor, LassoCV
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
## [sklearn.experimental] Do Not optimize imports ##
from sklearn.experimental import enable_halving_search_cv
from sklearn.model_selection import HalvingGridSearchCV

# set_config(display="text")
set_config(display="diagram")
warnings.filterwarnings("ignore")
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"  # disable GPU

def view(df_):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', None):
        display(HTML(df_.to_html()))

df = pd.read_csv('data/bmw.csv')
df = pd.get_dummies(df, columns=['model', 'transmission', 'fuelType', 'year', 'engineSize'])
X = df.drop('price', 1)
y = df[['price']]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=37)

def nn(layer_neurons: Iterable[int], meta: Dict[str, Any], compile_kwargs: Dict[str, Any]):
    model = Sequential([
        Input(shape=(meta["n_features_in_"])),
        *[Dense(neurons, kernel_initializer="uniform", activation="relu") for neurons in layer_neurons],
        Dense(1, activation='relu')
    ])
    model.compile(loss='mse', optimizer=compile_kwargs["optimizer"])
    return model

preprocessor = make_column_transformer(
    (StandardScaler(), make_column_selector(dtype_include=[np.number])),
    (SimpleImputer(strategy="mean"), make_column_selector(dtype_include=[np.number])),
    (OneHotEncoder(sparse=False, handle_unknown='ignore'), make_column_selector(dtype_exclude=[np.number]))
)
cachedir = mkdtemp()
model = StackingRegressor(
    estimators=[
        ("elastic", make_pipeline(preprocessor, ElasticNet(), memory=cachedir)),
        ("lasso", make_pipeline(preprocessor, LassoCV(), memory=cachedir)),
        ("huber", make_pipeline(preprocessor, HuberRegressor(), memory=cachedir)),
        ("ensemble", Pipeline([
            ("preprocessor", preprocessor),
            ('votereg', VotingRegressor([
                ('lasso', LassoCV()),
                ('elast', ElasticNet())
            ]))
        ], memory=cachedir))],
    final_estimator=KerasRegressor(
        model=nn,
        layer_neurons=(16, 32, 64),  # todo: ajust
        batch_size=32,  # todo: ajust
        epochs=100,  # todo: ajust
        optimizer="adam",
        optimizer__learning_rate=0.001,  # todo: ajust
        verbose=0,
        random_state=0,
        callbacks=[
            EarlyStopping(
                monitor='loss',
                mode='min',
                verbose=0,
                patience=200,  # todo: ajust
                min_delta=1e-5  # todo: ajust
            )
        ]
    ),
    verbose=1
)
grid = HalvingGridSearchCV(estimator=model,
                           # todo: ajust
                           param_grid={
                               'ensemble__votereg__lasso__eps': [1 / (10 ** n) for n in range(4, -1, -1)],
                               # 'ensemble__votereg__lasso__tol': [1 / (10 ** n) for n in range(4, -1, -1)],
                               # 'ensemble__votereg__elast__alpha': [1 / (10 ** n) for n in range(4, -1, -1)],
                               # 'ensemble__votereg__elast__l1_ratio': np.arange(0, 0.5, 0.1)
                           },
                           n_jobs=-1,
                           cv=2,  # todo: ajust
                           min_resources="exhaust",
                           factor=3,  # todo: ajust
                           verbose=0)

predictions = grid.fit(X_train, y_train).predict(X_test)
RMSE = mean_squared_error(y_test, predictions, squared=False)
print(f'{grid.best_score_ = }\n{grid.best_params_ = }\n{RMSE = }')
rmtree(cachedir)

max_stars_repo_name: trevorhobenshield/Pipelines

max_issues_repo_name: trevorhobenshield/Pipelines

max_forks_repo_name: trevorhobenshield/Pipelines


text:
from PIL import Image
from io import BytesIO
import requests
import cv2
import numpy as np

class Camera():

    def __init__(self, dev):
        self.dev = dev
        self.stream_url ='http://{}:2101/mjpeg'.format(self.dev.ip)

    def capture_image_to_file(self, file_name='axibo_image.jpg'):
        response = self.dev.request_get_image(uri='imaging/cam')
        with open(file_name, 'wb') as f:
            f.write(response.content)

    def capture_pil_image(self):
        response = self.dev.request_get_image(uri='imaging/cam')
        return Image.open(BytesIO(response.content))

    def set_resolution(self, width=720, height=640):
        data = { "imgWidth": int(width), "imgheight": int(height)}
        ret = self.dev.request_put(uri='imaging/config', data=data)

    def read(self):
        r = requests.get(self.stream_url, stream=True)
        if(r.status_code == 200):
            bytes = b""
            for chunk in r.iter_content(chunk_size=1024):
                bytes += chunk
                a = bytes.find(b'\xff\xd8')
                b = bytes.find(b'\xff\xd9')
                if a != -1 and b != -1:
                    jpg = bytes[a:b+2]
                    bytes = bytes[b+2:]
                    i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
                    return i
        else:
            raise ValueError("Received unexpected status code {}".format(r.status_code))

max_stars_repo_name: axiboai/python-axibo

max_issues_repo_name: axiboai/python-axibo

max_forks_repo_name: axiboai/python-axibo


text:
# coding=utf-8
# Copyright 2022 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Testing suite for the PyTorch Van model. """

import inspect
import math
import unittest

from transformers import VanConfig
from transformers.testing_utils import require_scipy, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_scipy_available, is_torch_available, is_vision_available

from ..test_configuration_common import ConfigTester
from ..test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor

if is_scipy_available():
    from scipy import stats

if is_torch_available():
    import torch
    from torch import nn

    from transformers import VanForImageClassification, VanModel
    from transformers.models.van.modeling_van import VAN_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
    from PIL import Image

    from transformers import AutoFeatureExtractor

class VanModelTester:
    def __init__(
        self,
        parent,
        batch_size=2,
        image_size=224,
        num_channels=3,
        hidden_sizes=[16, 32, 64, 128],
        depths=[1, 1, 1, 1],
        is_training=True,
        use_labels=True,
        num_labels=3,
        scope=None,
    ):
        self.parent = parent
        self.batch_size = batch_size
        self.image_size = image_size
        self.num_channels = num_channels
        self.hidden_sizes = hidden_sizes
        self.depths = depths
        self.is_training = is_training
        self.use_labels = use_labels
        self.num_labels = num_labels
        self.type_sequence_label_size = num_labels

    def prepare_config_and_inputs(self):
        pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

        labels = None
        if self.use_labels:
            labels = ids_tensor([self.batch_size], self.num_labels)

        config = self.get_config()

        return config, pixel_values, labels

    def get_config(self):
        return VanConfig(
            num_channels=self.num_channels,
            hidden_sizes=self.hidden_sizes,
            depths=self.depths,
            num_labels=self.num_labels,
            is_decoder=False,
        )

    def create_and_check_model(self, config, pixel_values, labels):
        model = VanModel(config=config)
        model.to(torch_device)
        model.eval()
        result = model(pixel_values)
        # expected last hidden states: B, C, H // 32, W // 32
        self.parent.assertEqual(
            result.last_hidden_state.shape,
            (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32),
        )

    def create_and_check_for_image_classification(self, config, pixel_values, labels):
        model = VanForImageClassification(config)
        model.to(torch_device)
        model.eval()
        result = model(pixel_values, labels=labels)
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels))

    def prepare_config_and_inputs_for_common(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, pixel_values, labels = config_and_inputs
        inputs_dict = {"pixel_values": pixel_values}
        return config, inputs_dict

@require_torch
class VanModelTest(ModelTesterMixin, unittest.TestCase):
    """
    Here we also overwrite some of the tests of test_modeling_common.py, as Van does not use input_ids, inputs_embeds,
    attention_mask and seq_length.
    """

    all_model_classes = (VanModel, VanForImageClassification) if is_torch_available() else ()

    test_pruning = False
    test_torchscript = False
    test_resize_embeddings = False
    test_head_masking = False
    has_attentions = False

    def setUp(self):
        self.model_tester = VanModelTester(self)
        self.config_tester = ConfigTester(self, config_class=VanConfig, has_text_modality=False, hidden_size=37)

    def test_config(self):
        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()

    def create_and_test_config_common_properties(self):
        return

    @unittest.skip(reason="Van does not use inputs_embeds")
    def test_inputs_embeds(self):
        pass

    @unittest.skip(reason="Van does not support input and output embeddings")
    def test_model_common_attributes(self):
        pass

    def test_forward_signature(self):
        config, _ = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            model = model_class(config)
            signature = inspect.signature(model.forward)
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            arg_names = [*signature.parameters.keys()]

            expected_arg_names = ["pixel_values"]
            self.assertListEqual(arg_names[:1], expected_arg_names)

    def test_model(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*config_and_inputs)

    @require_scipy
    def test_initialization(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        configs_no_init = _config_zero_init(config)

        for model_class in self.all_model_classes:
            model = model_class(config=configs_no_init)
            for name, module in model.named_modules():
                if isinstance(module, (nn.BatchNorm2d, nn.GroupNorm, nn.LayerNorm)):
                    self.assertTrue(
                        torch.all(module.weight == 1),
                        msg=f"Parameter {name} of model {model_class} seems not properly initialized",
                    )
                    self.assertTrue(
                        torch.all(module.bias == 0),
                        msg=f"Parameter {name} of model {model_class} seems not properly initialized",
                    )
                elif isinstance(module, nn.Conv2d):
                    fan_out = module.kernel_size[0] * module.kernel_size[1] * module.out_channels
                    fan_out //= module.groups
                    std = math.sqrt(2.0 / fan_out)
                    # divide by std -> mean = 0, std = 1
                    data = module.weight.data.cpu().flatten().numpy() / std
                    test = stats.anderson(data)
                    self.assertTrue(test.statistic > 0.05)

    def test_hidden_states_output(self):
        def check_hidden_states_output(inputs_dict, config, model_class):
            model = model_class(config)
            model.to(torch_device)
            model.eval()

            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))

            hidden_states = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

            expected_num_stages = len(self.model_tester.hidden_sizes)
            # van has no embeddings
            self.assertEqual(len(hidden_states), expected_num_stages)

            # Van's feature maps are of shape (batch_size, num_channels, height, width)
            self.assertListEqual(
                list(hidden_states[0].shape[-2:]),
                [self.model_tester.image_size // 4, self.model_tester.image_size // 4],
            )

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            inputs_dict["output_hidden_states"] = True
            check_hidden_states_output(inputs_dict, config, model_class)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            config.output_hidden_states = True

            check_hidden_states_output(inputs_dict, config, model_class)

    def test_for_image_classification(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*config_and_inputs)

    @slow
    def test_model_from_pretrained(self):
        for model_name in VAN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            model = VanModel.from_pretrained(model_name)
            self.assertIsNotNone(model)

# We will verify our results on an image of cute cats
def prepare_img():
    image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
    return image

@require_torch
@require_vision
class VanModelIntegrationTest(unittest.TestCase):
    @cached_property
    def default_feature_extractor(self):
        return AutoFeatureExtractor.from_pretrained(VAN_PRETRAINED_MODEL_ARCHIVE_LIST[0])

    @slow
    def test_inference_image_classification_head(self):
        model = VanForImageClassification.from_pretrained(VAN_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(torch_device)

        feature_extractor = self.default_feature_extractor
        image = prepare_img()
        inputs = feature_extractor(images=image, return_tensors="pt").to(torch_device)

        # forward pass
        with torch.no_grad():
            outputs = model(**inputs)

        # verify the logits
        expected_shape = torch.Size((1, 1000))
        self.assertEqual(outputs.logits.shape, expected_shape)

        expected_slice = torch.tensor([0.1029, -0.0904, -0.6365]).to(torch_device)

        self.assertTrue(torch.allclose(outputs.logits[0, :3], expected_slice, atol=1e-4))

max_stars_repo_name: dctelus/transformers

reason: from scipy


text:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, os
sys.path.append(os.pardir)  # 親ディレクトリのファイルをインポートするための設定
import numpy as np
from alifebook_lib.simulators import VehicleSimulator

# simulatorの初期化 (Appendix参照)
simulator = VehicleSimulator(obstacle_num=5)

while simulator:
    # 現在のセンサー情報を取得
    sensor_data = simulator.get_sensor_data()
    # ブライテンベルグ・ビークルの内部
    left_wheel_speed  = 20 + 20 * sensor_data["left_distance"]
    right_wheel_speed = 20 + 20 * sensor_data["right_distance"]
    # アクションを生成してアップデート
    action = [left_wheel_speed, right_wheel_speed]
    simulator.update(action)


text:
# coding: utf-8
""" MSCOCO Pytorch Dataset Class"""

from torch.utils.data import Dataset
from py_cocodata_server.py_data_iterator import RawDataIterator
import numpy as np
from config.config import GetConfig, COCOSourceConfig
from time import time
import matplotlib.pyplot as plt
import cv2
import torch.nn.functional as F
import torch

class MyDataset(Dataset):
    def __init__(self, global_config, config, shuffle=False, augment=True):
        """
        Initialize a DataIterator
        :param global_config: the configuration used in our project
        :param config:  the original COCO configuration
        :param shuffle:
        :param augment:
        """
        self.global_config = global_config
        self.config = config
        self.shuffle = shuffle
        self.augment = augment
        self.raw_data_iterator = RawDataIterator(self.global_config, self.config, shuffle=self.shuffle,
                                                 augment=self.augment)

    def __getitem__(self, index):
        # return entries: image, mask_miss, unmasked labels, offsets, mask_offset
        # Notice： numpy.random seed will fork the same value in multi-process, while python random will fork differently
        return self.raw_data_iterator.gen(index)

    def __len__(self):
        return self.raw_data_iterator.num_keys()

if __name__ == '__main__':  # for debug

    def test_augmentation_speed(train_client, show_image=True):
        start = time()
        batch = 0
        for index in range(train_client.__len__()):
            batch += 1

            image, mask_miss, labels = [v.numpy() for v in    # , offsets, mask_offset
                                            train_client.__getitem__(index)]

            # show the generated ground truth
            if show_image:
                show_labels = cv2.resize(labels.transpose((1, 2, 0)), image.shape[:2], interpolation=cv2.INTER_CUBIC)
                # offsets = cv2.resize(offsets.transpose((1, 2, 0)), image.shape[:2], interpolation=cv2.INTER_NEAREST)
                mask_miss = np.repeat(mask_miss.transpose((1, 2, 0)), 3, axis=2)
                # mask_miss = cv2.resize(mask_miss, image.shape[:2], interpolation=cv2.INTER_NEAREST)
                image = cv2.resize(image, mask_miss.shape[:2], interpolation=cv2.INTER_NEAREST)
                plt.imshow(image[:, :, [2, 1, 0]])   # Opencv image format: BGR
                plt.imshow(labels.transpose((1, 2, 0))[:,:,20], alpha=0.5)  # mask_all
                # plt.imshow(show_labels[:, :, 3], alpha=0.5)  # mask_all
                plt.show()
                t=2
        print("produce %d samples per second: " % (batch / (time() - start)))

    config = GetConfig("Canonical")
    soureconfig = COCOSourceConfig("../data/dataset/coco/link2coco2017/coco_val_dataset384.h5")

    val_client = MyDataset(config, soureconfig, shuffle=True, augment=True)  # shuffle in data loader
    # test the data generator
    test_augmentation_speed(val_client, True)

max_stars_repo_name: orange-eng/internship

max_issues_repo_name: orange-eng/internship

max_forks_repo_name: orange-eng/internship


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb  9 11:33:31 2022

@author: katie
"""

import numpy as np
import math
from skimage import io, color
import match as Match
import matplotlib.pyplot as plt

# =============================================================================
# sigma_d = 0
# sigma_s = 0
# lambda_ = 0
# V_max = 5
# m = 0
# M = (m,m)
# p_coordinate = (1,2)
# theta = 0
#
# =============================================================================
"""
t should iterate through all pixels in a given window W(p)

All pixels should be of the shape (3,)

p

"""
def R_3(gamma, p, image,sigma_s,sigma_d, channel,width):
    window = W(p,width)
    inner_term = []
    omega_ = []
    for t in window:
        if Match.inRect(t,image.shape):
            try:
                a = w(p,t,sigma_d, sigma_s,channel,image)
                b = K(t,channel,image)
                omega_.append(a)
                inner_term.append(a*b)
            except:
                pass
    #omega_ = omega(p,image, width,sigma_d, sigma_s,channel)
    Z_ = np.mean(omega_)
    inner = inner_term/Z_
    R_3 = K(p,channel,image) - inner
    return R_3
"""
color: may be 0, 1, or 2 (red = 0, green = 1, blue = 2)
"""

def create_m_list(m):
#    M_x = np.arange(0,m,1)
#    M_y = np.arange(0,m,1)
    m_list = []
    for i in range(0,m):
        for j in range(0,m):
            m_list.append([i,j])
    return m_list

def create_coordinate_list(p_coordinate,m):
    m_list = create_m_list(m)

    m_p_list = []
    for i in range(0,len(m_list)):
        m_p_list.append(list( map(add, m_list[i], p_coordinate) ))
    return(m_p_list)

def omega(p_coordinate,image,width,sigma_d, sigma_s,channel):
    coordinates_in_M = W(p_coordinate, width)

    omega = []
    for t in coordinates_in_M:
        if Match.inRect(t,image.shape):
            try:
                w_ = w(p_coordinate, t,sigma_d, sigma_s,channel,image)*K(t,channel,image)
                omega.append(w_)
            except:
                pass
    return omega

def Z(omega):
    Z = np.mean(omega)
    return Z

def K(t, color,image):
    term = (image[t[0],t[1],color])/((image[t[0],t[1],0]*image[t[0],t[1],1]*image[t[0],t[1],1])**(1/3))
    if term >= 0:
        K = math.log(term,10)
    else:
        K = 0
    return K

def I(t):
    mean = np.mean(t)
    return mean

def w(p, t, sigma_d, sigma_s,channel,image):
    p = np.asarray(p)
    t = np.asarray(t)
    I_p = I(image[p[0],p[1],:])
    I_t = I(image[t[0],t[1],:])
    term1 = -((np.linalg.norm( np.array([p[0],p[1]])-np.array([t[0],t[1]]) ))**2)
    term2= -((np.linalg.norm( I_p + I_t))**2)
    w = math.exp((term1+term2)/(2*(sigma_d**2)))
    return w

def ANCC(p_L, p_R, image_L, image_R, channel,gamma,width,sigma_d, sigma_s):
    list_of_coordinates_L = W(p_L,width)
    list_of_coordinates_R = W(p_R,width)
    numerator = []
    denominator_1 = []
    denominator_2 = []
    R_3_L = R_3(gamma, p_L, image_L, sigma_s,sigma_d, channel,width)
    R_3_R = R_3(gamma, p_R, image_R, sigma_s,sigma_d, channel,width)
    for i in range(0,len(list_of_coordinates_L)):
        if Match.inRect(list_of_coordinates_L[i], image_L.shape) and Match.inRect(list_of_coordinates_R[i], image_R.shape):
            w_L = w(p_L, list_of_coordinates_L[i],sigma_d, sigma_s,channel,image_L)
            w_R = w(p_R, list_of_coordinates_R[i], sigma_d, sigma_s, channel,image_R)
            term_1 = w_L*w_R*R_3_L*R_3_R
            numerator.append(term_1)
            left = np.sqrt((np.linalg.norm(w_L*R_3_L))**2)
            right = np.sqrt((np.linalg.norm(w_L*R_3_R))**2)
            denominator_1.append(left)
            denominator_2.append(right)
    numerator = np.sum(numerator)
    denominator_1 = np.sqrt(np.sum(denominator_1))
    denominator_2 = np.sqrt(np.sum(denominator_2))
    denominator = denominator_1 * denominator_2
    return numerator/denominator

def D(p_L, p_R, image_L, image_R, gamma, theta,width,sigma_d, sigma_s):
    # 0 = red, 1 = green, 2 = blue
    color_channels = [0,1,2]
    log_color_channels = [0,1,2]
    cielab_image_L = color.rgb2lab(image_L)
    cielab_image_R = color.rgb2lab(image_R)
    color_ANCC = []
    log_color_ANCC = [0]
    #for i in color_channels:
    #    color_ANCC.append(ANCC(p_L, p_R, image_L, image_R, channel = i,gamma=gamma, width=width,sigma_d = sigma_d, sigma_s = sigma_s))
    for i in log_color_channels:
        log_color_ANCC.append(ANCC(p_L, p_R, cielab_image_L, cielab_image_R, channel = i,gamma=gamma,width=width,sigma_d = sigma_d, sigma_s = sigma_s))
    D = 1 - ((theta*(np.sum(color_ANCC)))+(1-theta)*(np.sum(log_color_ANCC)))
    if math.isnan(D) == False:
        return D
    else:
        D = 0
        return D

def V_pq(f_p, f_q, lambda_ancc, V_max=5):
    return lambda_ancc * np.maximum((np.linalg.norm((f_p, f_q))**2), V_max)

def N(p):
    list_of_terms_1 = [-1,0,1]
    list_of_terms_2 = [-1,0,1]
    N = []
    for i in list_of_terms_1:
        for j in list_of_terms_2:
            N.append(list_of_terms.append([p[0]+i,p[1]+j]))
    return N

def W(p,m):
    window_indices = []
    for i in range(-m,m):
        for j in range(-m,m):
            window_indices.append([p[0]+i,p[1]+j])
    return window_indices

#def E_f_p(p_L, p_R, imageLeft, imageRight, parameters): #assume disparities is some matrix of f_p's
#    imageLeft_LAB = color.rgb2lab(imageLeft)
#    imageLeft_Right = color.rgb2lab(imageRight)
#    N = N(p)
#    V= []
#    D = []
#
#    f_p = d2 - d1
#        D.append(D(p_L,p_R, image_L, image_R, parameters.gamma, parameters.theta))
#        for dq in N:
#            f_q = dq - d1
#            V.append(V_pq(f_p, f_q, parameters.lambda_ancc, parameters.V_max))
#    E = np.sum(D) + np.sum(V)
#    return E
#

def match(file1, file2, color, dispMin, dispMax):
    # load two images
    imgLeft = cv2.imread(file1)
    imgRight = cv2.imread(file2)

    # Default parameters
    K = -1
    lambda_ = -1
    lambda1 = -1
    lambda2 = -1
    params = match.Parameters(is_L2=True,
                              denominator=1,
                              edgeThresh=8,
                              lambda1=lambda1,
                              lambda2=lambda2,
                              K=K,
                              maxIter=4)

    # create match instance
    m = match.Match(imgLeft, imgRight, color)

    m.SetDispRange(dispMin, dispMax)
    m = match.fix_parameters(m, params, K, lambda_, lambda1, lambda2)
    disparities = m.kolmogorov_zabih()
    return disparities


text:
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
from genericpath import exists
from re import T

import cv2
import torch
import mmcv
import datetime
import numpy as np
from imantics import Polygons, Mask
from shapely.geometry import Polygon
from mmdet.apis import inference_detector, init_detector
from demo_util import *
from Tracks import *
def parse_args():
    parser = argparse.ArgumentParser(description='MMDetection video demo')
    parser.add_argument('video', help='Video file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--score-thr', type=float, default=0.3, help='Bbox score threshold')
    parser.add_argument('--out', type=str, help='Output video file')
    parser.add_argument('--show', action='store_true', help='Show video')
    parser.add_argument(
        '--wait-time',
        type=float,
        default=1,
        help='The interval of show (s), 0 is block')
    args = parser.parse_args()
    return args

def main():
    args = parse_args()
    assert args.out or args.show, \
        ('Please specify at least one operation (save/show the '
         'video) with the argument "--out" or "--show"')

    model = init_detector(args.config, args.checkpoint, device=args.device)

    video_reader = mmcv.VideoReader(args.video)
    video_writer = None
    if args.out:
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        video_writer = cv2.VideoWriter(
            args.out, fourcc, video_reader.fps,
            (video_reader.width, video_reader.height))

    for frame in mmcv.track_iter_progress(video_reader):
        result = inference_detector(model, frame)
        frame = model.show_result(frame, result, score_thr=args.score_thr)
        if args.show:
            cv2.namedWindow('video', 0)
            mmcv.imshow(frame, 'video', args.wait_time)
        if args.out:
            video_writer.write(frame)

    if video_writer:
        video_writer.release()
    cv2.destroyAllWindows()

def main_cv2():
    args = parse_args()
    assert args.out or args.show, \
        ('Please specify at least one operation (save/show the '
         'video) with the argument "--out" or "--show"')

    model = init_detector(args.config, args.checkpoint, device=args.device)
    print("Process:"+args.video)
    video_reader = cv2.VideoCapture(args.video)
    video_writer = None

    use_boundingbox = False
    use_mask = True

    #if "segms" in args.out:
    #    use_boundingbox = False
    #    use_mask = True
    save_dir = args.out
    save_dir = save_dir.replace( os.path.basename(save_dir),'')
    if not exists(save_dir):
        os.makedirs(save_dir)
    if args.out:
        fourcc = cv2.VideoWriter_fourcc(*'mp4v')
        video_writer = cv2.VideoWriter(
            args.out, fourcc, video_reader.get(cv2.CAP_PROP_FPS),
            (int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH)),
            int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))))

    success,frame = video_reader.read()
    frame_index = 1
    tmer = tracks_manager()

    while success:
        #frame = cv2.flip(frame, 0)
        time0=datetime.datetime.now()
        result = inference_detector(model, frame)
        time1=datetime.datetime.now()

        bbox_results, segm_results = result

        if use_boundingbox:
            tmer.update_with_bbox(bbox_results,frame_index)
            frame = show_result(frame, result, score_thr=args.score_thr,show_mask=False)
        elif use_mask:
            tmer.update_with_segm(segm_results,frame_index)
            frame = show_result(frame, result, score_thr=args.score_thr,show_bbox=False)

        frame = tmer.vis(frame)
        #cv2.imwrite("/home/qilei/DATASETS/trans_drone/mmtest.jpg",frame)
        video_writer.write(frame)

        success,frame = video_reader.read()

        frame_index+=1
    if not use_boundingbox:
        if isinstance( args.out,str):
            tmer.save_results(args.out+".json")

        json2csv(args.out+".json")

if __name__ == '__main__':
    #main()
    main_cv2()