text:
# standard libraries
from __future__ import print_function
import unittest
import shutil
# third party
import numpy as np
from theano.tensor import ftensor3, ftensor4, lvector, mean, neq
# internal references
from opendeep import function
from opendeep.log.logger import config_root_logger
from opendeep.models import Conv1D, Conv2D, Prototype, Dense, Softmax
from opendeep.models.utils import Pool2D, Flatten
from opendeep.data import MNIST
from opendeep.monitor import Monitor, FileService
from opendeep.optimization import SGD
from opendeep.optimization.loss import Neg_LL

# python 2 vs. python 3 mnist source
import sys
if sys.version_info > (3, 0):
    mnist_name = 'mnist_py3k.pkl.gz'
else:
    mnist_name = 'mnist.pkl.gz'

class TestConv(unittest.TestCase):

    def setUp(self):
        # configure the root logger
        # config_root_logger()
        pass

    def testConv1DOutputSize(self):
        try:
            x = ftensor3('x')
            #batch, channels, dim
            s = (None, 15, 94)
            filters = 25
            filter_size = 2
            padding = 2
            stride = 2
            conv1 = Conv1D(inputs=(s, x), n_filters=filters, filter_size=filter_size, padding=padding, stride=stride,
                           outdir=None)
            f1 = function(inputs=[x], outputs=conv1.get_outputs().shape, allow_input_downcast=True)
            x1 = np.ones((100, 15, 94))
            outs = f1(x1)
            self.compareSizes(outs=outs, output_size=conv1.output_size, in_size=s, batches=100)
        finally:
            if 'x' in locals():
                del x
            if 'conv1' in locals():
                del conv1
            if 'f1' in locals():
                del f1
            if 'outs' in locals():
                del outs
            if 'x1' in locals():
                del x1

    def testConv2DOutputSize(self):
        try:
            x = ftensor4('x')
            # batch, channels, height, width
            s = (None, 3, 25, 32)
            filters = 25
            filter_size = 5
            padding = 3
            stride = 3
            conv1 = Conv2D(inputs=(s, x), n_filters=filters, filter_size=filter_size, padding=padding, stride=stride,
                           outdir=None)
            f1 = function(inputs=[x], outputs=conv1.get_outputs().shape, allow_input_downcast=True)
            x1 = np.ones((100, 3, 25, 32))
            outs = f1(x1)
            self.compareSizes(outs=outs, output_size=conv1.output_size, in_size=s, batches=100)

        finally:
            if 'x' in locals():
                del x
            if 'conv1' in locals():
                del conv1
            if 'f1' in locals():
                del f1
            if 'outs' in locals():
                del outs
            if 'x1' in locals():
                del x1

    def compareSizes(self, outs, output_size, in_size, batches):
        self.assertEqual(output_size[0], in_size[0])
        sizes_same = all(np.equal(output_size[1:], outs[1:]))
        self.assertTrue(sizes_same,
                        "Found shapes {!s} (theoretical) and {!s} (computed)".format(output_size[1:], outs[1:]))
        self.assertEqual(outs[0], batches)

    def testLeNet(self):
        try:
            # quick and dirty way to create a model from arbitrary layers
            lenet = Prototype(outdir=None)
            # our input is going to be 4D tensor of images with shape (batch_size, 1, 28, 28)
            x = ((None, 1, 28, 28), ftensor4('x'))
            # our first convolutional layer
            lenet.add(
                Conv2D(inputs=x, n_filters=20, filter_size=(5, 5), outdir=None)
            )
            # our first pooling layer, automatically hooking inputs to the previous convolutional outputs
            lenet.add(
                Pool2D, size=(2, 2)
            )
            # our second convolutional layer
            lenet.add(
                Conv2D, n_filters=50, filter_size=(5, 5), outdir=None
            )
            # our second pooling layer
            lenet.add(
                Pool2D, size=(2, 2)
            )
            # now we need to flatten the 4D convolution outputs into 2D matrix (just flatten the trailing dimensions)
            lenet.add(
                Flatten, ndim=2
            )
            # one dense hidden layer
            lenet.add(
                Dense, outputs=500, activation='tanh', outdir=None
            )
            # hook a softmax classification layer, outputting the probabilities.
            lenet.add(
                Softmax, outputs=10, out_as_probs=True, outdir=None
            )

            # Grab the MNIST dataset
            data = MNIST(path="../../../datasets/{!s}".format(mnist_name), concat_train_valid=False, flatten=False)
            # define our loss to optimize for the model (and the target variable)
            # targets from MNIST are int64 numbers 0-9
            y = lvector('y')
            loss = Neg_LL(inputs=lenet.get_outputs(), targets=y, one_hot=False)
            # monitor
            error_monitor = Monitor(name='error', expression=mean(neq(lenet.models[-1].y_pred, y)), valid=True,
                                    test=True, out_service=FileService('outputs/lenet_error.txt'))
            # optimize our model to minimize loss given the dataset using SGD
            optimizer = SGD(model=lenet,
                            dataset=data,
                            loss=loss,
                            epochs=10,
                            batch_size=128,
                            learning_rate=.1,
                            momentum=False)
            print("Training LeNet...")
            optimizer.train(monitor_channels=error_monitor)

            def test_subset(filename, expected, conf=0.001):
                with open(filename, 'r') as f:
                    errs = [float(err) for err in f]
                for i, (err, exp) in enumerate(zip(errs, expected)):
                    if i == 0:
                        c = conf*10
                    else:
                        c = conf
                    self.assertTrue(exp-c < round(err, 4) < exp+c,
                                    "Errors: {!s} and Expected: {!s} -- Error at {!s} and {!s}".format(
                                        errs, expected, err, exp)
                                    )

            test_subset('outputs/lenet_error_train.txt',
                        [.0753,
                         .0239,
                         .0159,
                         .0113,
                         .0088,
                         .0064,
                         .0050,
                         .0037,
                         .0026,
                         .0019]
                        )
            test_subset('outputs/lenet_error_valid.txt',
                        [.0283,
                         .0209,
                         .0170,
                         .0151,
                         .0139,
                         .0129,
                         .0121,
                         .0118,
                         .0112,
                         .0113]
                        )
            test_subset('outputs/lenet_error_test.txt',
                        [.0319,
                         .0213,
                         .0167,
                         .0134,
                         .0122,
                         .0119,
                         .0116,
                         .0107,
                         .0104,
                         .0105]
                        )
            shutil.rmtree('outputs/')

        finally:
            if 'lenet' in locals():
                del lenet
            if 'data' in locals():
                del data
            if 'y' in locals():
                del y
            if 'x' in locals():
                del x
            if 'loss' in locals():
                del loss
            if 'optimizer' in locals():
                del optimizer

    def tearDown(self):
        pass

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: vitruvianscience/OpenDeep

max_issues_repo_name: afcarl/OpenDeep

max_forks_repo_name: afcarl/OpenDeep

reason: import numpy,from theano


text:
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from collections import OrderedDict
from torchvision import datasets, transforms
from torch.autograd import Variable
from torchvision import transforms
from torchvision.utils import save_image
from random import randint
import numpy as np
from collections import OrderedDict
import matplotlib
matplotlib.use('Agg')
import os, time, sys
import matplotlib.pyplot as plt
import itertools
from utils import imshow_grid
from itertools import cycle
from torch.utils.data import DataLoader
from utils import transform_config, reparameterize
import argparse
import glob
import cv2

parser = argparse.ArgumentParser()

# add arguments
parser.add_argument('--cuda', type=bool, default=True, help="run the following code on a GPU")

parser.add_argument('--batch_size', type=int, default=64, help="batch size for training")
parser.add_argument('--image_size', type=int, default=128, help="height and width of the image")
parser.add_argument('--initial_learning_rate', type=float, default=0.00001, help="starting learning rate")

parser.add_argument('--nv_dim', type=int, default=64, help="dimension of varying factor latent space")
parser.add_argument('--nc_dim', type=int, default=512, help="dimension of common factor latent space")
parser.add_argument('--class_to_remove', type=list, default=[], help="classes to remove from training dataset")
parser.add_argument('--num_classes', type=int, default=25, help="number of classes on which the data set trained")

# arguments to control per iteration training of architecture
parser.add_argument('--train_auto_encoder', type=bool, default=True, help="train the auto-encoder part")
parser.add_argument('--train_reverse_cycle', type=bool, default=True, help="train the reverse consistency cycle")
parser.add_argument('--train_decorr_classifier', type=bool, default=False, help="train classifier to decorrelate")

# loss function coefficient
# 3 reconstruction coef for 64 dim space
parser.add_argument('--reconstruction_coef', type=float, default=2., help="coefficient for reconstruction term")
parser.add_argument('--reverse_cycle_coef', type=float, default=10., help="coefficient for reverse cycle loss term")
parser.add_argument('--kl_divergence_coef', type=float, default=3., help="coefficient for KL-Divergence loss term")
parser.add_argument('--decorr_coef', type=float, default=0., help="coefficient for decorrelation loss term")

parser.add_argument('--beta_1', type=float, default=0.9, help="default beta_1 val for adam")
parser.add_argument('--beta_2', type=float, default=0.999, help="default beta_2 val for adam")

# paths to save models
parser.add_argument('--encoder_save', type=str, default='encoder', help="model save for encoder")
parser.add_argument('--decoder_save', type=str, default='decoder', help="model save for decoder")
parser.add_argument('--classifier_save', type=str, default='classifier', help="model save for nv classifier")

parser.add_argument('--log_file', type=str, default='log.txt', help="text file to save training logs")

parser.add_argument('--load_saved', type=bool, default=False, help="flag to indicate if a saved model will be loaded")
parser.add_argument('--start_epoch', type=int, default=0, help="flag to set the starting epoch for training")
parser.add_argument('--end_epoch', type=int, default=150, help="flag to indicate the final epoch of training")

FLAGS = parser.parse_args()

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

num_epochs = 500
batch_size = 8
learning_rate = 0.005

nc_dim = 160

img_transform = transforms.Compose([transforms.ToTensor()])

def to_img(x):
	x = x.clamp(0, 1)
	x = x.view(x.size(0), 3, 128, 128)
	return x

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

		self.convolution_1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=4, stride=2, padding=1, bias=True)
		self.convolution_1_in = nn.InstanceNorm2d(num_features=32)
		self.ELU_1 = nn.ReLU(inplace=True)

		self.convolution_2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=2, padding=1, bias=True)
		self.convolution_2_in = nn.InstanceNorm2d(num_features=32)
		self.ELU_2 = nn.ReLU(inplace=True)

		self.convolution_3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, stride=2, padding=1, bias=True)
		self.convolution_3_in = nn.InstanceNorm2d(num_features=64)
		self.ELU_3 = nn.ReLU(inplace=True)

		self.convolution_4 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=5, stride=2, padding=1, bias=True)
		self.convolution_4_in = nn.InstanceNorm2d(num_features=128)
		self.ELU_4 = nn.ReLU(inplace=True)

		self.convolution_5 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=5, stride=2, padding=0, bias=True)
		self.convolution_5_in = nn.InstanceNorm2d(num_features=256)
		self.ELU_5 = nn.ReLU(inplace=True)

		self.common_factor_mu = nn.Linear(in_features=1024, out_features=nc_dim, bias=True)
		self.common_factor_logvar = nn.Linear(in_features=1024, out_features=nc_dim, bias=True)

		self.common_factor_dec = nn.Linear(in_features=nc_dim, out_features=2048, bias=True)

		self.relu = nn.ReLU(inplace=True)

		self.deconvolution_1 = nn.ConvTranspose2d(in_channels=512, out_channels=256, kernel_size=5, stride=2, padding=0, bias=True)
		self.deconvolution_1_in = nn.InstanceNorm2d(num_features=256)
		self.ELU_1_ = nn.ReLU(inplace=True)

		self.deconvolution_2 = nn.ConvTranspose2d(in_channels=256, out_channels=128, kernel_size=4, stride=2, padding=0, bias=True)
		self.deconvolution_2_in = nn.InstanceNorm2d(num_features=128)
		self.ELU_2_ = nn.ReLU(inplace=True)

		self.deconvolution_3 = nn.ConvTranspose2d(in_channels=128, out_channels=64, kernel_size=4, stride=2, padding=1, bias=True)
		self.deconvolution_3_in = nn.InstanceNorm2d(num_features=64)
		self.ELU_3_ = nn.ReLU(inplace=True)

		self.deconvolution_4 = nn.ConvTranspose2d(in_channels=64, out_channels=32, kernel_size=4, stride=2, padding=1, bias=True)
		self.deconvolution_4_in = nn.InstanceNorm2d(num_features=32)
		self.ELU_4_ = nn.ReLU(inplace=True)

		self.deconvolution_5 = nn.ConvTranspose2d(in_channels=32, out_channels=3, kernel_size=4, stride=2, padding=1, bias=True)
		self.sigmoid_final = nn.Sigmoid()

	def encode(self, x):
		x = self.ELU_1(self.convolution_1_in(self.convolution_1(x)))
		x = self.ELU_2(self.convolution_2_in(self.convolution_2(x)))
		x = self.ELU_3(self.convolution_3_in(self.convolution_3(x)))
		x = self.ELU_4(self.convolution_4_in(self.convolution_4(x)))
		x = self.ELU_5(self.convolution_5_in(self.convolution_5(x)))
		x = x.view(x.size(0), x.size(1) * x.size(2) * x.size(3))
		mu = self.common_factor_mu(x)
		sigma = self.common_factor_logvar(x)
		return mu, sigma

	def reparametrize(self, mu, logvar):
		std = logvar.mul(0.5).exp_()
		eps = torch.FloatTensor(std.size()).normal_().cuda()
		eps = Variable(eps)
		return eps.mul(std).add_(mu)

	def decode(self, z):
		x = self.relu(self.common_factor_dec(z))
		x = x.view(x.size(0), 512, 2, 2)
		x = self.ELU_1_(self.deconvolution_1_in(self.deconvolution_1(x)))
		x = self.ELU_2_(self.deconvolution_2_in(self.deconvolution_2(x)))
		x = self.ELU_3_(self.deconvolution_3_in(self.deconvolution_3(x)))
		x = self.ELU_4_(self.deconvolution_4_in(self.deconvolution_4(x)))
		x = self.sigmoid_final(self.deconvolution_5(x))
		return x

	def forward(self, x):
		mu, logvar = self.encode(x)
		z = self.reparametrize(mu, logvar)
		res = self.decode(z)
		return res, mu, logvar

model = VAE().cuda()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
reconstruction_function = nn.MSELoss(size_average=False)

def loss_function(recon_x, x, mu, logvar):
	"""
	recon_x: generating images
	x: origin images
	mu: latent mean
	logvar: latent log variance
	"""
	BCE = reconstruction_function(recon_x, x)  # mse loss
	KLD_element = mu.pow(2).add_(logvar.exp()).mul_(-1).add_(1).add_(logvar)
	KLD = torch.sum(KLD_element).mul_(-0.5)
	# KL divergence
	return BCE + KLD

def train(batchsize):
	print('Loading Sat2Map Dataset...')
	data_dir = '../processed_dataset/train/images/'
	label_dir = '../processed_dataset/train/maps1/'
	data_dir_1 = '../processed_dataset/val/images/'
	label_dir_1 = '../processed_dataset/val/maps1/'

	dset = datasets.ImageFolder(data_dir, transform=transforms.ToTensor())
	train_data_dir = torch.utils.data.DataLoader(dset, batch_size=batch_size, shuffle=False)

	dset1 = datasets.ImageFolder(label_dir, transform=transforms.ToTensor())
	train_label_dir = torch.utils.data.DataLoader(dset1, batch_size=batch_size, shuffle=False)

	dset_more = datasets.ImageFolder(data_dir_1, transform=transforms.ToTensor())
	train_data_dir_more = torch.utils.data.DataLoader(dset_more, batch_size=batch_size, shuffle=False)

	dset_1_more = datasets.ImageFolder(label_dir_1, transform=transforms.ToTensor())
	train_label_dir_more = torch.utils.data.DataLoader(dset_1_more, batch_size=batch_size, shuffle=False)

	train_images = []
	train_ground_truths = []
	for i, (img, _) in enumerate(train_data_dir):
		train_images.append(img)
	for i, (img, _) in enumerate(train_label_dir):
		train_ground_truths.append(img)

	for i, (img, _) in enumerate(train_data_dir_more):
		train_images.append(img)
	for i, (img, _) in enumerate(train_label_dir_more):
		train_ground_truths.append(img)

	train_ground_truths = train_ground_truths[:len(train_ground_truths)-2]
	train_images = train_images[:len(train_images)-2]

	len_train = len(train_images)

	for epoch in range(num_epochs):
		model.train().cuda()
		train_loss = 0
		for i in range(len(train_images)):
			img = Variable(train_images[i].cuda())
			map_img = Variable(train_ground_truths[i].cuda())
			optimizer.zero_grad()
			reconstructed, mu, sigma = model(img)
			loss = loss_function(reconstructed, map_img, mu, sigma)
			loss.backward()
			optimizer.step()
			if(i%100==0):
				print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
					epoch,
					i * len(img),
					len_train,
					100. * i / len_train,
					loss.data[0] / len(img)))
		print('====> Epoch: {} Average loss: {:.4f}'.format(
			epoch, loss.data[0] / len_train))
		if epoch % 10 == 0:
			ground_truth_img = to_img(img.cpu().data)
			ground_truth_map = to_img(map_img.cpu().data)
			rec = to_img(reconstructed.cpu().data)
			batch_img = torch.zeros(24, 3, 128, 128)
			batch_img[0:8, :, :, :] = ground_truth_img
			batch_img[8:16, :, :, :] = ground_truth_map
			batch_img[16:24, :, :, :] = rec
			save_image(batch_img, './vae_images/batch_images_{}.png'.format(epoch), nrow=8)
	return model

def load_model():
	model.load_state_dict(torch.load('./vae.pth'))
	return model

def save_model(model):
	torch.save(model.state_dict(), './vae.pth')

def generate_image(model):
	z = Variable(torch.FloatTensor(1,nc_dim).normal_(), requires_grad=True)
	print("z",z)
	z1 = Variable(torch.FloatTensor(1,nc_dim).normal_(), requires_grad=True)
	print("z1",z1)
	make_image(model, z,"generated-image")
	make_image(model, z1, "generated-image1")

def make_image(model,z,name):
	x = model.decode(Variable(z.data.cuda(), requires_grad = True))
	x = x.view(1,3,64,64)
	img = x.data.cpu().numpy()
	x1 = img[0,0,:,:]
	x2 = img[0,1,:,:]
	x3 = img[0,2,:,:]
	img_final = np.zeros([64,64,3])
	img_final[:,:,0] = x1
	img_final[:,:,1] = x2
	img_final[:,:,2] = x3
	plt.imshow(img_final, interpolation = 'nearest')
	plt.savefig('./' + name + '.jpg')

train(batchsize = batch_size)
save_model(model)

ext: py

reason: import numpy


text:
import torch
from torch import nn
from GPG.models.model_utils import init_wt_normal
from GPG.embedding import GloveEmbeddings, BertEmbeddings
import numpy as np
import os
import sys
from io import open

def build_embeddings(config, for_encoder=True, vocab = None):
    """
    Args:
        config: the option in current environment.
        for_encoder(bool): build Embeddings for encoder or decoder?
    """

    if for_encoder:
        position_embeddings_flag = config.position_embeddings_flag
    else:
        position_embeddings_flag = False

    if(config.bert_embedding):
        embedding, emb_dim = BertEmbeddings(config, position_embeddings_flag)   #
    elif(config.glove_embedding and (vocab is not None)):
        embedding, emb_dim = GloveEmbeddings(config, vocab, position_embeddings_flag)
    else:
        embedding = nn.Embedding(config.vocab_size, config.input_dim)   # input_dim = 128
        init_wt_normal(embedding.weight, config.trunc_norm_init_std)
        emb_dim = config.emb_dim

    return embedding, emb_dim

def build_glove_embeddings(config, vocab):
    emb_dim = config.emb_dim   # 300
    position_embeddings = None
    position_embeddings_flag = config.position_embeddings_flag
    word_embeddings = share_embedding(vocab, config.emb_file, emb_dim)
    if position_embeddings_flag:
        position_embeddings = nn.Embedding(config.max_position_embeddings, config.position_emb_size)
        position_embeddings.weight.requires_grad = False
        emb_dim = emb_dim + config.position_emb_size
    # LayerNorm = torch.nn.LayerNorm(emb_dim, eps=config.layer_norm_eps)
    # LayerNorm_no_position = torch.nn.LayerNorm(config.bert_input_size, eps=config.layer_norm_eps)
    # dropout = nn.Dropout(config.hidden_dropout_prob)

    return word_embeddings, position_embeddings

def gen_embeddings(vocab, emb_dim, emb_file):
    embeddings = np.random.randn(vocab.voc_size, emb_dim) * 0.01
    print('Embeddings: %d x %d' % (vocab.voc_size, emb_dim))
    if emb_file is not None:
        print('Loading embedding file: %s' % emb_file)
        pre_trained = 0
        for line in open(emb_file).readlines():
            sp = line.split()
            if(len(sp) == emb_dim + 1):
                if sp[0] in vocab._word2id:
                    pre_trained += 1
                    embeddings[vocab._word2id[sp[0]]] = [float(x) for x in sp[1:]]
            else:
                print(sp[0])
        print('Pre-trained: %d (%.2f%%)' % (pre_trained, pre_trained * 100.0 / vocab.voc_size))
    return embeddings

def share_embedding(vocab, emb_file, emb_dim =300, pretrain=True):
    embedding = nn.Embedding(vocab.voc_size, emb_dim)
    init_wt_normal(embedding.weight)

    if(pretrain):
        pre_embedding = gen_embeddings(vocab, emb_dim, emb_file)
        embedding.weight.data.copy_(torch.FloatTensor(pre_embedding))
        embedding.weight.requires_grad = False
    return embedding

max_stars_repo_name: HLTCHKUST/MulQG

max_issues_repo_name: Iamfinethanksu/MulQG

max_forks_repo_name: Iamfinethanksu/MulQG


text:
"""
Configuration file!
"""
import os
from argparse import ArgumentParser
import numpy as np

ROOT_PATH = os.path.dirname(os.path.realpath(__file__))
DATA_PATH = os.path.join(ROOT_PATH, 'data')

def path(fn):
    return os.path.join(DATA_PATH, fn)

def stanford_path(fn):
    return os.path.join(DATA_PATH, 'stanford_filtered', fn)

def genome_path(fn):
    return os.path.join(DATA_PATH, 'visual_genome', fn)

# =============================================================================
# Update these with where your data is stored ~~~~~~~~~~~~~~~~~~~~~~~~~

VG_IMAGES = genome_path('VG_100K')
RCNN_CHECKPOINT_FN = path('vg-faster-rcnn.tar')

IM_DATA_FN = stanford_path('image_data.json')
VG_SGG_FN = stanford_path('VG-SGG.h5')
VG_SGG_DICT_FN = stanford_path('VG-SGG-dicts.json')
PROPOSAL_FN = stanford_path('proposals.h5')

# =============================================================================
# =============================================================================

MODES = ('sgdet', 'sgcls', 'predcls')

BOX_SCALE = 1024  # Scale at which we have the boxes
IM_SCALE = 592      # Our images will be resized to this res without padding

# Proposal assignments
BG_THRESH_HI = 0.5
BG_THRESH_LO = 0.0

RPN_POSITIVE_OVERLAP = 0.7
# IOU < thresh: negative example
RPN_NEGATIVE_OVERLAP = 0.3

# Max number of foreground examples
RPN_FG_FRACTION = 0.5
FG_FRACTION = 0.25
# Total number of examples
RPN_BATCHSIZE = 256
ROIS_PER_IMG = 256
REL_FG_FRACTION = 0.25
RELS_PER_IMG = 256

RELS_PER_IMG_REFINE = 64

BATCHNORM_MOMENTUM = 0.01
ANCHOR_SIZE = 16

ANCHOR_RATIOS = (0.23232838, 0.63365731, 1.28478321, 3.15089189) #(0.5, 1, 2)
ANCHOR_SCALES = (2.22152954, 4.12315647, 7.21692515, 12.60263013, 22.7102731) #(4, 8, 16, 32)

class ModelConfig(object):
    """Wrapper class for model hyperparameters."""
    def __init__(self):
        """
        Defaults
        """
        self.ckpt = None
        self.save_dir = None
        self.lr = None
        self.batch_size = None
        self.val_size = None
        self.l2 = None
        self.adamwd = None
        self.clip = None
        self.num_gpus = None
        self.num_workers = None
        self.print_interval = None
        self.mode = None
        self.test = False
        self.adam = False
        self.cache = None
        self.use_proposals=False
        self.use_resnet=False
        self.num_epochs=None
        self.pooling_dim = None

        self.use_ggnn_obj = False
        self.ggnn_obj_time_step_num = None
        self.ggnn_obj_hidden_dim = None
        self.ggnn_obj_output_dim = None
        self.use_obj_knowledge = False
        self.obj_knowledge = None

        self.use_ggnn_rel = False
        self.ggnn_rel_time_step_num = None
        self.ggnn_rel_hidden_dim = None
        self.ggnn_rel_output_dim = None
        self.use_rel_knowledge = False
        self.rel_knowledge = None

        self.tb_log_dir = None
        self.save_rel_recall = None

        self.parser = self.setup_parser()
        self.args = vars(self.parser.parse_args())

        print("~~~~~~~~ Hyperparameters used: ~~~~~~~")
        for x, y in self.args.items():
            print("{} : {}".format(x, y))

        self.__dict__.update(self.args)

        if len(self.ckpt) != 0:
            self.ckpt = os.path.join(ROOT_PATH, self.ckpt)
        else:
            self.ckpt = None

        if len(self.cache) != 0:
            self.cache = os.path.join(ROOT_PATH, self.cache)
        else:
            self.cache = None

        if len(self.save_dir) == 0:
            self.save_dir = None
        else:
            self.save_dir = os.path.join(ROOT_PATH, self.save_dir)
            if not os.path.exists(self.save_dir):
                os.mkdir(self.save_dir)

        if len(self.tb_log_dir) != 0:
            self.tb_log_dir = os.path.join(ROOT_PATH, self.tb_log_dir)
            if not os.path.exists(self.tb_log_dir):
                os.mkdir(self.tb_log_dir)
        else:
            self.tb_log_dir = None

        if len(self.save_rel_recall) != 0:
            self.save_rel_recall = os.path.join(ROOT_PATH, self.save_rel_recall)
        else:
            self.save_rel_recall = None

        assert self.val_size >= 0

        if self.mode not in MODES:
            raise ValueError("Invalid mode: mode must be in {}".format(MODES))

        if self.ckpt is not None and not os.path.exists(self.ckpt):
            raise ValueError("Ckpt file ({}) doesnt exist".format(self.ckpt))

    def setup_parser(self):
        """
        Sets up an argument parser
        :return:
        """
        parser = ArgumentParser(description='training code')

        parser.add_argument('-ckpt', dest='ckpt', help='Filename to load from', type=str, default='')
        parser.add_argument('-save_dir', dest='save_dir',
                            help='Directory to save things to, such as checkpoints/save', default='', type=str)

        parser.add_argument('-ngpu', dest='num_gpus', help='cuantos GPUs tienes', type=int, default=1)
        parser.add_argument('-nwork', dest='num_workers', help='num processes to use as workers', type=int, default=1)

        parser.add_argument('-lr', dest='lr', help='learning rate', type=float, default=1e-3)

        parser.add_argument('-b', dest='batch_size', help='batch size per GPU',type=int, default=2)
        parser.add_argument('-val_size', dest='val_size', help='val size to use (if 0 we wont use val)', type=int, default=5000)

        parser.add_argument('-l2', dest='l2', help='weight decay of SGD', type=float, default=1e-4)
        parser.add_argument('-adamwd', dest='adamwd', help='weight decay of adam', type=float, default=0.0)

        parser.add_argument('-clip', dest='clip', help='gradients will be clipped to have norm less than this', type=float, default=5.0)
        parser.add_argument('-p', dest='print_interval', help='print during training', type=int,
                            default=100)
        parser.add_argument('-m', dest='mode', help='mode in {sgdet, sgcls, predcls}', type=str, default='sgdet')

        parser.add_argument('-cache', dest='cache', help='where should we cache predictions', type=str,
                            default='')

        parser.add_argument('-adam', dest='adam', help='use adam', action='store_true')
        parser.add_argument('-test', dest='test', help='test set', action='store_true')

        parser.add_argument('-nepoch', dest='num_epochs', help='Number of epochs to train the model for',type=int, default=50)
        parser.add_argument('-resnet', dest='use_resnet', help='use resnet instead of VGG', action='store_true')
        parser.add_argument('-proposals', dest='use_proposals', help='Use Xu et als proposals', action='store_true')
        parser.add_argument('-pooling_dim', dest='pooling_dim', help='Dimension of pooling', type=int, default=4096)

        parser.add_argument('-use_ggnn_obj', dest='use_ggnn_obj', help='use GGNN_obj module', action='store_true')
        parser.add_argument('-ggnn_obj_time_step_num', dest='ggnn_obj_time_step_num', help='time step number of GGNN_obj', type=int, default=3)
        parser.add_argument('-ggnn_obj_hidden_dim', dest='ggnn_obj_hidden_dim', help='node hidden state dimension of GGNN_obj', type=int, default=512)
        parser.add_argument('-ggnn_obj_output_dim', dest='ggnn_obj_output_dim', help='node output feature dimension of GGNN_obj', type=int, default=512)
        parser.add_argument('-use_obj_knowledge', dest='use_obj_knowledge', help='use object cooccurrence knowledge', action='store_true')
        parser.add_argument('-obj_knowledge', dest='obj_knowledge', help='Filename to load matrix of object cooccurrence knowledge', type=str, default='')

        parser.add_argument('-use_ggnn_rel', dest='use_ggnn_rel', help='use GGNN_rel module', action='store_true')
        parser.add_argument('-ggnn_rel_time_step_num', dest='ggnn_rel_time_step_num', help='time step number of GGNN_rel', type=int, default=3)
        parser.add_argument('-ggnn_rel_hidden_dim', dest='ggnn_rel_hidden_dim', help='node hidden state dimension of GGNN_rel', type=int, default=512)
        parser.add_argument('-ggnn_rel_output_dim', dest='ggnn_rel_output_dim', help='node output feature dimension of GGNN_rel', type=int, default=512)
        parser.add_argument('-use_rel_knowledge', dest='use_rel_knowledge', help='use cooccurrence knowledge of object pairs and relationships', action='store_true')
        parser.add_argument('-rel_knowledge', dest='rel_knowledge', help='Filename to load matrix of cooccurrence knowledge of object pairs and relationships', type=str, default='')

        parser.add_argument('-tb_log_dir', dest='tb_log_dir', help='dir to save tensorboard summaries', type=str, default='')
        parser.add_argument('-save_rel_recall', dest='save_rel_recall', help='dir to save relationship results', type=str, default='')

        return parser


text:
import numpy as np
import chainer
from chainer import cuda
import time
xp= cuda.cupy
from matplotlib import pyplot as plt
from numba import jit

@jit
def do_something(n,mat):
	for i in range(n):
		mat=mat.dot(mat)
		mat=mat-mat.dot(mat)

@jit
def cpucalc(n):
	start=time.time()
	mat=np.arange(n*n).reshape(n,n)
	do_something(n,mat)
	end=time.time()
	elapsed=end-start
	print("CPU end-start=%s"%elapsed)
	return elapsed

def gpucalc(n):
	start=time.time()
	mat=xp.arange(n*n).reshape(n,n)
	do_something(n,mat)
	end=time.time()
	elapsed=end-start
	print("GPU end-start=%s"%elapsed)
	return elapsed

def main():
	cs=[]
	gs=[]
	iteration=500
	step=50
	for size in range(1,iteration,step):
		print(size)
		gs.append(gpucalc(size))
		cs.append(cpucalc(size))

	fig,ax=plt.subplots()
	ax.plot([i for i in range(1,iteration,step)],gs,color="green")
	ax.plot([i for i in range(1,iteration,step)],cs,color="blue")
	plt.show()

if __name__ == '__main__':
	main()

max_stars_repo_name: terasakisatoshi/pythonCodes

max_issues_repo_name: terasakisatoshi/pythonCodes

max_forks_repo_name: terasakisatoshi/pythonCodes

reason: import numpy,from numba


text:
#!/usr/bin/env python3

from __future__ import division, print_function

import argparse
import math
import multiprocessing
import os
import signal
import sys
from collections import defaultdict
from typing import List, Dict

import gym
import matplotlib.pyplot as plt
import numpy
import yaml
from tqdm import tqdm

from rl.agents import AgentBuilder
from rl.agents.reprs import Value
from rl.utils.logging_utils import Logger

def get_builder(agent_config, num_arms, optimistic: float):
    if optimistic > 0:
        state_values = defaultdict(Value)
        for i in range(num_arms):
            state_values[(i,)] = Value(count=0, value=optimistic)
        agent_config["kwargs"]["state_values"] = state_values

    builder = AgentBuilder(agent_config["policy"], agent_config["learning"])
    builder.set(**agent_config["kwargs"])
    return builder

def transition_model(state, action):
    return numpy.array([1]), [numpy.array([action])]

def available_actions(num_arms):
    return numpy.arange(num_arms)

def play(agent, env_name, arms, num_iterations, nonstationary):
    # Create a window to render into
    env = gym.make(env_name)
    obs = env.reset()

    state = numpy.array([env.action_space.sample()])
    rewards = numpy.zeros(num_iterations)
    optimal_percentages = numpy.zeros(num_iterations)
    optimal_count = 0
    for ep in range(num_iterations):
        if nonstationary:
            mu = nonstationary["mu"]
            sigma = nonstationary["sigma"]
            adjustments = numpy.random.normal(mu, sigma, arms)
            for i in range(arms):
                env.r_dist[i][0] += adjustments[i]

        optimal_action = env.r_dist.index(max(env.r_dist))
        action = agent.act(state, available_actions(arms))

        obs, reward, done, info = env.step(action)
        state = numpy.array([action])
        if action == optimal_action:
            optimal_count += 1
        optimal_percentages[ep] = optimal_count / (ep + 1)
        rewards[ep] = reward
        agent.learn(state=state, action=action, reward=reward)

    return rewards, optimal_percentages

def keyboard_interrupt_handler(signal, frame):
    sys.exit(0)

def plot(data: numpy.ndarray, agent_config, image_name: str, num_iterations: int):
    fig, (rewards_plot, percentage_plot) = plt.subplots(2, 1, figsize=(20, 16))

    iterations = numpy.arange(num_iterations)
    for d, agent in zip(data, agent_config):
        rewards = d[0]
        label = f"{agent['policy']}{agent['learning']}"
        for kwarg, value in agent["kwargs"].items():
            if kwarg != "state_values" and kwarg != "transitions":
                label += f" {kwarg[0]}: {value}"
        rewards_plot.plot(iterations, rewards, label=label)
        optimal_percentages = d[1] * 100
        percentage_plot.plot(iterations, optimal_percentages, label=label)

    rewards_plot.set(xlabel='Iterations', ylabel='Rewards',
                     title='Iterations vs Rewards')

    percentage_plot.set(xlabel='Iterations', ylabel='Optimal %',
                        title='Iterations vs Optimal %')

    percentage_plot.set_ylim(0, 100)

    rewards_plot.legend()
    percentage_plot.legend()
    plt.tight_layout()
    plt.savefig(image_name, dpi=100)
    plt.plot()

def run_experiment(args):
    agent_config = args[0]
    arms = args[1]
    num_episodes = args[2]
    num_iterations = args[3]
    env_name = args[4]
    nonstationary = args[5]

    builder = get_builder(agent_config, arms, agent_config.get("optimistic", 0))

    rewards = numpy.zeros(num_iterations)
    percentages = numpy.zeros(num_iterations)
    label = f"{agent_config['policy']}{agent_config['learning']}"
    for kwarg, value in agent_config["kwargs"].items():
        if kwarg != "state_values" and kwarg != "transitions":
            label += f" {kwarg[0]}: {value}"
    for ep_num in tqdm(range(num_episodes), total=num_episodes, desc=label):
        agent = builder.make()
        agent.transition_model = transition_model
        new_rewards, optimal_percentages = play(agent, env_name, arms, num_iterations=num_iterations,
                                                nonstationary=nonstationary)
        rewards += 1 / (ep_num + 1) * (new_rewards - rewards)
        percentages += 1 / (ep_num + 1) * (optimal_percentages - percentages)

    return rewards, percentages

def simulate(agents: List[Dict], arms: int, env_name: str, num_episodes: int, num_iterations: int, nonstationary: Dict):
    """
    :param num_episodes:  The number of games to play each other
    """
    processes = multiprocessing.cpu_count()
    print(f"Simulating bandits! Number of episodes per agent: {num_episodes} Number of agents: {len(agents)}")
    print(f"Env: {env_name} arms: {arms}")

    if len(agents) < processes:
        processes = len(agents)

    chunksize = math.floor(len(agents) / processes)

    with multiprocessing.Pool(processes=processes) as pool:
        experiment_inputs = ((agent_config, arms, num_episodes, num_iterations, env_name, nonstationary) for
                             agent_config in agents)

        os.system('clear')
        total_rewards_percentages = numpy.array(
            pool.map(run_experiment, iterable=experiment_inputs, chunksize=chunksize))

    return total_rewards_percentages

def main():
    signal.signal(signal.SIGINT, keyboard_interrupt_handler)

    parser = argparse.ArgumentParser()
    parser.add_argument("config-file", help="Configuration file to run this program.",
                        type=str)
    parser.add_argument("-ne", "--num-episodes", help="The number of bandit simulations to run",
                        type=int,
                        default=False)
    parser.add_argument("-ni", "--num-iterations", help="The number of iterations per bandit simulation",
                        type=int,
                        default=False)
    parser.add_argument("-o", "--out-image", help="The name of the plot to be output.",
                        type=str,
                        default=False)
    parser.add_argument("-env", "--env-name", help="rlgym environment to load",
                        type=str,
                        default=False)

    logger: Logger = Logger(parser=parser)
    options = parser.parse_args()
    with open(sys.argv[1], 'r') as f:
        if hasattr(yaml, "FullLoader"):
            configuration = yaml.load(f, Loader=yaml.FullLoader)
        else:
            configuration = yaml.load(f)

    if options.num_episodes:
        configuration["num_episodes"] = options.num_episodes

    if options.num_iterations:
        configuration["num_iterations"] = options.num_iterations

    if options.out_image:
        configuration["out_image"] = options.out_image

    if options.env_name:
        configuration["env_name"] = options.env_name

    total_rewards_percentages = simulate(configuration["agents"],
                                         configuration["arms"],
                                         configuration["env_name"],
                                         configuration["num_episodes"],
                                         configuration["num_iterations"],
                                         configuration.get("nonstationary", {}))

    plot(total_rewards_percentages,
         agent_config=configuration["agents"],
         image_name=configuration["out_image"],
         num_iterations=configuration["num_iterations"])

    print("done!")

if __name__ == "__main__":
    main()

max_stars_repo_name: ManuelMeraz/ReinforcementLearning

max_issues_repo_name: ManuelMeraz/ReinforcementLearning

max_forks_repo_name: ManuelMeraz/ReinforcementLearning


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 25 14:27:35 2018

@author: halil
"""

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

dataframe = pd.read_csv("./datasets/data.csv")
x = dataframe.iloc[:, 1:2].values
y = dataframe.iloc[:, 2].values
y = np.reshape(y, (len(y), 1))

from sklearn.preprocessing import StandardScaler
scaler_x = StandardScaler()
scaler_y = StandardScaler()
scaled_x = scaler_x.fit_transform(x)
scaled_y = scaler_y.fit_transform(y)

from sklearn.svm import SVR
regressor = SVR(kernel="rbf")
regressor.fit(x, y)

regressor2 = SVR(kernel="rbf")
regressor2.fit(scaled_x, scaled_y)

plt.subplot(1, 2, 1)
plt.scatter(x, y, color = "red")
plt.plot(x, regressor.predict(x), color = "blue")
plt.title("SVR Model")
plt.xlabel("Pozisyon")
plt.ylabel("Maaş")
plt.show()

plt.subplot(1, 2, 2)
plt.scatter(x, y, color = "red")
plt.plot(x, scaler_y.inverse_transform(regressor2.predict(scaled_x)), color = "blue")
plt.title("SVR Model (Öz Nitelik Ölçeklenmiş)")
plt.xlabel("Pozisyon")
plt.ylabel("Maaş")
plt.show()

y_pred = regressor.predict(7.1)
y_pred1 = scaler_y.inverse_transform(regressor2.predict(scaler_x.transform(np.array([[7.1]]))))

max_stars_repo_name: halil/sau-ml

max_issues_repo_name: halil/sau-ml

max_forks_repo_name: halil/sau-ml


text:
#!/bin/python3

from fastai.tabular import *
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split, StratifiedShuffleSplit
from sklearn.utils import shuffle

print('Imports complete')

df = pd.read_csv('../../malware_dataset/complete_binarized_dataset_cleaned.csv', index_col=0)
df.head()

procs = [FillMissing, Categorify, Normalize]
sss = StratifiedShuffleSplit(n_splits=10, test_size=0.2, random_state=1)
print(sss)

dep_var = 'Label_binarized'
cont_names = list(set(df.columns) - set([dep_var]))

for train_idx, test_idx in sss.split(df.index, df['Label_binarized']):
    data_fold = (TabularList.from_df(df, path='../../malware_dataset/complete_binarized_dataset_cleaned.csv', cont_names=cont_names, procs=procs)
                .split_by_idxs(train_idx, test_idx)
                .label_from_df(cols='Label_binarized')
                .databunch())
    model = tabular_learner(data_fold, layers=[50], metrics=[accuracy], callback_fns=ShowGraph, ps=0.5)
    model.fit_one_cycle(cyc_len=10)


text:
import sqlite3
import pandas as pd
import numpy as np
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder
from Preprocessor import Preprocessor
import sys
sys.path.append("src/models/")
from Classifier import Classifier
from operator import itemgetter, attrgetter
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC

#Verbindung zu sqlite Datenbank öffnen
connection = sqlite3.connect("data/raw/database.sqlite")

#SQL-Abfrage in pandas DataFrame
sql = "SELECT * FROM vw_Match_Team_Data"

daten = pd.read_sql_query(sql, connection)
connection.close()

onehot_enc_list = list()
daten_join = daten
#Teamname-Spalten encoden (one-hot-encoding)
for spalte in ('home_team_name','away_team_name'):
    team_name = daten[spalte]
    team_name_res = team_name.values.reshape(-1,1)
    team_name_onehot_enc = OneHotEncoder()
    team_name_onehot = team_name_onehot_enc.fit_transform(team_name_res)

    #onehot_enc_list.append([spalte, team_name_onehot, team_name_onehot_enc])

    sp_name_array = team_name_onehot_enc.categories_[0]
    for i in range(0, sp_name_array.size):
        sp_name_array[i] = sp_name_array[i] + '_H' if spalte == 'home_team_name' else sp_name_array[i] + '_A'
    daten_join = daten_join.join(pd.DataFrame(team_name_onehot.toarray(), columns=sp_name_array))

result_spalte = daten_join.pop("result")
daten_join['result'] = result_spalte
daten_prepared = daten_join.drop(["home_team_name", "away_team_name"], axis=1)

result['R_Kla'] = 0 if result['reproduction'] < 1 else 1

# print(onehot_enc_list[1][1].toarray())
# print("Array-Objekt 0")
# print(onehot_enc_list[1][2].categories_[0])

# sp_name_array = onehot_enc_list[1][2].categories_[0]
# for i in range(0, sp_name_array.size):
#     sp_name_array[i] = sp_name_array[i] + '_H'

# daten_join = daten.join(pd.DataFrame(onehot_enc_list[1][1].toarray(), columns=sp_name_array))

daten_prepared.to_excel("data/processed/Bundesliga_daten.xlsx")

X = daten_prepared.loc[:,daten_prepared.columns != "result"]
y = daten_prepared["result"]

X_train, X_test, y_train, y_test, scaler = Preprocessor(X, y).get_data()

X_train_exp = pd.DataFrame(X_train)
X_train_exp.to_excel("data/processed/Bundesliga_daten_X_normalized.xlsx")

#Models spezifieren
models = {"rf":RandomForestClassifier, "dt":DecisionTreeClassifier, "knn":KNeighborsClassifier, "svm":SVC, "mlp":MLPClassifier}

#Classifier verwenden
clf = Classifier()
resultat = clf.train_models(X_train, X_test, y_train, y_test, models)

#Bestes Ergebnis bestimmen und als Modell speichern
print("Bestes Model ist: {} mit einer Akkuranz von {}%".format(sorted(resultat, key=itemgetter(1), reverse=True)[0][0],sorted(resultat, key=itemgetter(1), reverse=True)[0][1]*100))
bestes_model = sorted(resultat, key=itemgetter(1), reverse=True)[0][2]
print("Alle Ergebnisse: {}".format(resultat))


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

# Part of code was adapted from:
# https://github.com/nii-yamagishilab/project-NN-Pytorch-scripts

def lowpass_filter(x, fs, cutoff=5, N=5):
    """Lowpass filter

    Args:
        x (np.ndarray): input signal
        fs (int): sampling rate
        cutoff (int): cutoff frequency

    Returns:
        np.ndarray: filtered signal
    """
    nyquist = fs // 2
    norm_cutoff = cutoff / nyquist
    Wn = [norm_cutoff]

    b, a = signal.butter(N, Wn, "lowpass")
    if len(x) <= len(b) * (N // 2 + 1):
        # NOTE: input signal is too short
        return x

    # NOTE: use zero-phase filter
    y = signal.filtfilt(b, a, x)

    return y

def bandpass_filter(x, sr, cutoff=70):
    """Band-pass filter

    Args:
        x (np.ndarray): input signal
        fs (int): sampling rate
        cutoff (int): cutoff frequency

    Returns:
        np.ndarray: filtered signal
    """
    nyquist = sr // 2
    norm_cutoff = cutoff / nyquist
    Wn = [norm_cutoff, 0.999]

    b, a = signal.butter(5, Wn, "bandpass")
    y = signal.filtfilt(b, a, x)

    return y

class TimeInvFIRFilter(nn.Conv1d):
    """Time-invatiant FIR filter implementation

    Args:
        channels (int): input channels
        filt_coef (torch.Tensor): FIR filter coefficients
        causal (bool): causal
        requires_grad (bool): trainable kernel or not
    """

    def __init__(self, channels, filt_coef, causal=True, requires_grad=False):
        # assuming 1-D filter coef vector and odd num taps
        assert len(filt_coef.shape) == 1
        # assert len(filt_coef) % 2 == 1
        kernel_size = len(filt_coef)
        self.causal = causal
        if causal:
            padding = (kernel_size - 1) * 1
        else:
            padding = (kernel_size - 1) // 2 * 1
        # channel-wise filtering (groups=channels)
        super(TimeInvFIRFilter, self).__init__(
            channels, channels, kernel_size, padding=padding, groups=channels, bias=None
        )
        self.weight.data[:, :, :] = filt_coef.flip(-1)
        self.weight.requires_grad = requires_grad

    def forward(self, x):
        out = super(TimeInvFIRFilter, self).forward(x)
        out = out[:, :, : -self.padding[0]] if self.causal else out
        return out

class TrTimeInvFIRFilter(nn.Conv1d):
    """Trainable Time-invatiant FIR filter implementation

    H(z) = \\sigma_{k=0}^{filt_dim} b_{k}z_{-k}

    Note that b_{0} is fixed to 1 if fixed_0th is True.

    Args:
        channels (int): input channels
        filt_dim (int): FIR filter dimension
        causal (bool): causal
        tanh (bool): apply tanh to filter coef or not.
        fixed_0th (bool): fix the first filt coef to 1 or not.
    """

    def __init__(self, channels, filt_dim, causal=True, tanh=True, fixed_0th=True):
        # Initialize filt coef with small random values
        init_filt_coef = torch.randn(filt_dim) * (1 / filt_dim)
        # assert len(filt_coef) % 2 == 1
        kernel_size = len(init_filt_coef)
        self.causal = causal
        if causal:
            padding = (kernel_size - 1) * 1
        else:
            padding = (kernel_size - 1) // 2 * 1
        # channel-wise filtering (groups=channels)
        super(TrTimeInvFIRFilter, self).__init__(
            channels, channels, kernel_size, padding=padding, groups=channels, bias=None
        )
        self.weight.data[:, :, :] = init_filt_coef.flip(-1)
        self.weight.requires_grad = True
        self.tanh = tanh
        self.fixed_0th = fixed_0th

    def get_filt_coefs(self):
        # apply tanh for filtter stability
        b = torch.tanh(self.weight) if self.tanh else self.weight
        b = b.clone()
        if self.fixed_0th:
            b[:, :, -1] = 1
        return b

    def forward(self, x):
        b = self.get_filt_coefs()
        out = F.conv1d(
            x, b, self.bias, self.stride, self.padding, self.dilation, self.groups
        )
        if self.padding[0] > 0:
            out = out[:, :, : -self.padding[0]] if self.causal else out
        return out

reason: from scipy


text:
from .modelcombiner import ModelCombiner
from ..utils.utils_classifiers import translate_output
from ..utils.utils_classifiers import get_index_label_classes
import numpy as np

__all__ = ['AverageCombiner', 'PluralityVotingCombiner', 'SoftVotingCombiner']

#
# For Regression
#
class AverageCombiner(ModelCombiner):
    """ Class for compute the average the output models.
    """

    def __init__(self, **kwargs):
        super(AverageCombiner, self).__init__(**kwargs)

    # noinspection PyMethodMayBeStatic
    def output(self, ensemble_model, _input, prob):
        """ Average the output of the ensemble's models.

        Parameters
        ----------
        ensemble_model : EnsembleModel
            Ensemble Model it uses for get ensemble's models.

        _input : theano.tensor.matrix or numpy.array
            Input sample.

        prob : bool
            In the case of classifier if is True the output is probability, for False means the output is translated.
            Is recommended hold True for training because the translate function is non-differentiable.

        Returns
        -------
        theano.Op
            Returns the average of the output models.
        """
        output = [model.output(_input, prob) for model in ensemble_model.get_models()]
        return sum(output) / len(ensemble_model.get_models())

#
# For Classification
#
class PluralityVotingCombiner(ModelCombiner):
    """ Combiner classifier method where each model in ensemble votes by one class and the class with more votes win.

    Plurality voting takes the class label which receives the largest number of votes as the final winner.
    That is, the output class label of the ensemble.

    References
    ----------
    .. [1] Zhi-Hua Zhou. (2012), pp 73:
           Ensemble Methods Foundations and Algorithms
           Chapman & Hall/CRC Machine Learning & Pattern Recognition Series.
    """

    def __init__(self):
        super(PluralityVotingCombiner, self).__init__(type_model="classifier")

    # noinspection PyMethodMayBeStatic
    def output(self, ensemble_model, _input, prob):
        """ Average the output of the ensemble's models.

        Parameters
        ----------
        ensemble_model : EnsembleModel
            Ensemble Model it uses for get ensemble's models.

        _input : theano.tensor.matrix or numpy.array
            Input sample.

        prob : bool
            In the case of classifier if is True the output is probability, for False means the output is translated.
            Is recommended hold True for training because the translate function is non-differentiable.

        Returns
        -------
        theano.Op
            Returns the average of the output models.
        """
        if prob:
            output = [model.output(_input, prob) for model in ensemble_model.get_models()]
            return sum(output) / len(ensemble_model.get_models())
        else:
            outputs = [translate_output(model.output(_input, prob),
                                        ensemble_model.get_fan_out(),
                                        ensemble_model.is_binary_classification()) for model in
                       ensemble_model.get_models()]
            return translate_output(sum(outputs), ensemble_model.get_fan_out(),
                                    ensemble_model.is_binary_classification())

    def predict(self, ensemble_model, _input):
        """ Returns the class with more votes.

        Parameters
        ----------
        ensemble_model : EnsembleModel
            Ensemble model where gets the output.

        _input : theano.tensor.matrix or numpy.array
            Input sample.

        Returns
        -------
        numpy.array
            Return the diversity of model.
        """
        voting = [{} for _ in range(_input.shape[0])]
        for model in ensemble_model.get_models():
            votes = model.predict(_input)
            PluralityVotingCombiner._vote(voting, votes)

        return PluralityVotingCombiner._result(voting)

    @staticmethod
    def _vote(voting, votes):
        """ Counting votes.

        Parameters
        ----------
        voting : list[dict]
            This dictionary keeps the votes.

        votes : list[]
            This list has votes.

        Returns
        -------
        None
        """
        for i, vote in enumerate(votes):
            if vote in voting[i]:
                (voting[i])[vote] += 1
            else:
                (voting[i])[vote] = 1

    @staticmethod
    def _result(voting):
        """ Gets the result of voting.

        Parameters
        ----------
        voting : list[dict]
            This dictionary has recount.

        Returns
        -------
        numpy.array
            Returns a list with results of voting.
        """
        result = []
        for votes in voting:
            result.append(max(votes, key=lambda key: votes[key]))

        return np.array(result)

class SoftVotingCombiner(ModelCombiner):
    """ Combiner classifier method where each model in ensemble votes by one class and the class with more votes win.

    Plurality voting takes the class label which receives the largest number of votes as the final winner.
    That is, the output class label of the ensemble.

    References
    ----------
    .. [1] Zhi-Hua Zhou. (2012), pp 76:
           Ensemble Methods Foundations and Algorithms
           Chapman & Hall/CRC Machine Learning & Pattern Recognition Series.
    """

    def __init__(self):
        super(SoftVotingCombiner, self).__init__(type_model="classifier")

    # noinspection PyMethodMayBeStatic
    def output(self, ensemble_model, _input, prob):
        """ Average the output of the ensemble's models.

        Parameters
        ----------
        ensemble_model : EnsembleModel
            Ensemble Model it uses for get ensemble's models.

        _input : theano.tensor.matrix or numpy.array
            Input sample.

        prob : bool
            In the case of classifier if is True the output is probability, for False means the output is translated.
            Is recommended hold True for training because the translate function is non-differentiable.

        Returns
        -------
        theano.Op
            Returns the average of the output models.
        """
        if prob:
            output = [model.output(_input, prob) for model in ensemble_model.get_models()]
            return sum(output) / len(ensemble_model.get_models())
        else:
            outputs = [translate_output(model.output(_input, prob),
                                        ensemble_model.get_fan_out(),
                                        ensemble_model.is_binary_classification()) for model in
                       ensemble_model.get_models()]
            return translate_output(sum(outputs), ensemble_model.get_fan_out(),
                                    ensemble_model.is_binary_classification())

    def predict(self, ensemble_model, _input):
        """ Returns the class with more votes.

        Parameters
        ----------
        ensemble_model : EnsembleModel
            Ensemble model where gets the output.

        _input : theano.tensor.matrix or numpy.array
            Input sample.

        Returns
        -------
        numpy.array
            Return the diversity of model.
        """
        output = ensemble_model.output(_input, prob=False)
        labels = ensemble_model.get_target_labels()
        return np.squeeze(labels[get_index_label_classes(output, ensemble_model.is_binary_classification())])


text:
import tensorflow as tf

import numpy as np

def model_fn(features, labels, mode, params):
    #### Training or Evaluation
    is_training = False

    #### Get loss from inputs
    if FLAGS.is_regression:
      (total_loss, per_example_loss, logits
          ) = function_builder.get_regression_loss(FLAGS, features, is_training)
    else:
      (total_loss, per_example_loss, logits
          ) = function_builder.get_classification_loss(
          FLAGS, features, n_class, is_training)

    #### Check model parameters
    num_params = sum([np.prod(v.shape) for v in tf.trainable_variables()])
    tf.logging.info('#params: {}'.format(num_params))

    #### load pretrained models
    scaffold_fn = model_utils.init_from_checkpoint(FLAGS)

    #### Evaluation mode
    if mode == tf.estimator.ModeKeys.EVAL:
      assert FLAGS.num_hosts == 1

      def metric_fn(per_example_loss, label_ids, logits, is_real_example):
        predictions = tf.argmax(logits, axis=-1, output_type=tf.int32)
        eval_input_dict = {
            'labels': label_ids,
            'predictions': predictions,
            'weights': is_real_example
        }
        accuracy = tf.metrics.accuracy(**eval_input_dict)

        loss = tf.metrics.mean(values=per_example_loss, weights=is_real_example)
        return {
            'eval_accuracy': accuracy,
            'eval_loss': loss}

      def regression_metric_fn(
          per_example_loss, label_ids, logits, is_real_example):
        loss = tf.metrics.mean(values=per_example_loss, weights=is_real_example)
        pearsonr = tf.contrib.metrics.streaming_pearson_correlation(
            logits, label_ids, weights=is_real_example)
        return {'eval_loss': loss, 'eval_pearsonr': pearsonr}

      is_real_example = tf.cast(features["is_real_example"], dtype=tf.float32)

      #### Constucting evaluation TPUEstimatorSpec with new cache.
      label_ids = tf.reshape(features['label_ids'], [-1])

      if FLAGS.is_regression:
        metric_fn = regression_metric_fn
      else:
        metric_fn = metric_fn
      metric_args = [per_example_loss, label_ids, logits, is_real_example]

      if FLAGS.use_tpu:
        eval_spec = tf.contrib.tpu.TPUEstimatorSpec(
            mode=mode,
            loss=total_loss,
            eval_metrics=(metric_fn, metric_args),
            scaffold_fn=scaffold_fn)
      else:
        eval_spec = tf.estimator.EstimatorSpec(
            mode=mode,
            loss=total_loss,
            eval_metric_ops=metric_fn(*metric_args))

      return eval_spec

    elif mode == tf.estimator.ModeKeys.PREDICT:
      label_ids = tf.reshape(features["label_ids"], [-1])

      predictions = {
          "logits": logits,
          "labels": label_ids,
          "is_real": features["is_real_example"]
      }

      if FLAGS.use_tpu:
        output_spec = tf.contrib.tpu.TPUEstimatorSpec(
            mode=mode, predictions=predictions, scaffold_fn=scaffold_fn)
      else:
        output_spec = tf.estimator.EstimatorSpec(
            mode=mode, predictions=predictions)
      return output_spec

estimator = tf.estimator.Estimator(
    model_fn=model_fn,
    config=run_config)
for pred_cnt, result in enumerate(estimator.predict(
        input_fn="pred_input_fn",
        yield_single_examples=True,
        checkpoint_path=FLAGS.predict_ckpt)):


text:
import os
from zipfile import ZipFile

from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.utils._joblib import Memory
from sklearn.utils import Bunch
from sklearn.datasets.lfw import _fetch_lfw_people
from PIL import Image
import numpy as np

PINS_DATASET = dict(
    name='frules11/pins-face-recognition',
    zip='pins-face-recognition.zip',
    folder='PINS'
)

def downsample(image, resize_to):
    """Downsample image to `resize_to` size.

    Args:
        image (np.array): RGB Image as array.
        resize_to (tuple, optional): Target image size.

    Returns:
        np.array: RGB Image as array
    """

    image = Image.fromarray(image.astype('uint8'), 'RGB')
    image = image.resize(resize_to, Image.ANTIALIAS)
    return np.array(image)

def preprocess(people, ttt_ratio=.2, resize_to=(32,32)):
    """Preprocess data set.

    Convert `sklearn.datasets.fetch_lfw_people` output to usable data set.

    Args:
        people (sklearn.utils.Bunch): LFW data set
        ttt_ratio (float, optional): Train to test ratio. Defaults to .2.
        resize_to (tuple, optional): Target image size. Defaults to (32, 32).

    Returns:
        tuple: ((x_train, y_train), (x_test, y_test))
    """

    x = np.array([downsample(i, resize_to) for i in people.images]) / 255
    y = people.target

    # Split to train and test data set
    x_train, x_test, y_train, y_test = train_test_split(
        x,
        y,
        test_size=ttt_ratio,
        # stratify=people.target,
        random_state=13
    )

    # Encode labels to one hot
    n_classes = people.target_names.shape[0]
    y_train = to_categorical(y_train, n_classes)
    y_test = to_categorical(y_test, n_classes)

    return (x_train, y_train), (x_test, y_test)

def dataset_gen(x, y, batch_size):
    """Training data batch generator.

    Yields batches of train data for 2 in 2 out model.

    Args:
        x (np.array): Training data.
        y (np.array): Training labels.
        batch_size (int): Size of each batch.

    Yields:
        tuple: ((x_batch, y_batch),(y_batch, x_batch))
    """

    datagen = ImageDataGenerator(
        width_shift_range=0.1,
        height_shift_range=0.1,
        rotation_range=20,
        # horizontal_flip=True
    )

    generator = datagen.flow(x, y, batch_size=batch_size)
    while 1:
        x_batch, y_batch = generator.next()
        yield ([x_batch, y_batch], [y_batch, x_batch])

def fetch_pins_people(resize=.5, min_faces_per_person=0, color=False,
                      slice_=(slice(25, 275), slice(25, 275)),
                      download_if_missing=True):
    """Load PINS dataset.

    Use a PINS dataset provided by Kaggle, everage the scikit-learn memory
    optimizations.

    Args:
        resize (float, optional): Image resize factor. Defaults to .5.
        min_faces_per_person (int, optional): Minimal number of images per
            person. Defaults to 0.
        color (bool): Toggle is images should be in RGB or 1 channel.
            Defaults to False.
        slice_ (tuple, optional): A rectangle to which images are sliced.
            Defaults to (slice(70, 195), slice(78, 172)).
        download_if_missing (bool, optional): Set if the dataset should be
            downloaded if not present on the machine. Defaults to True.

    Returns:
        sklearn.utils.Bunch: Collection of data set
    """
    from kaggle import KaggleApi

    # Extract ZIP dataset
    kaggle_api = KaggleApi()
    kaggle_home = kaggle_api.read_config_file()['path']
    path_to_zip = os.path.join(
        kaggle_home, 'datasets', PINS_DATASET['name'], PINS_DATASET['zip']
    )
    path_to_files = os.path.join(
         kaggle_home, 'datasets', PINS_DATASET['name'], PINS_DATASET['folder']
    )

    # Download if missing
    if download_if_missing and not os.path.exists(path_to_zip):
        kaggle_api.authenticate()
        kaggle_api.dataset_download_files(PINS_DATASET['name'], quiet=False)

    if not os.path.exists(path_to_files):
        with ZipFile(path_to_zip, 'r') as zipObj:
            extraction_path = os.path.join(
                kaggle_home, 'datasets', PINS_DATASET['name']
            )
            zipObj.extractall(extraction_path)

    # Load data in memory
    m = Memory(location=kaggle_home, compress=6, verbose=0)
    load_func = m.cache(_fetch_lfw_people)

    faces, target, target_names = load_func(
        path_to_files, resize=resize,
        min_faces_per_person=min_faces_per_person, color=color, slice_=slice_)

    X = faces.reshape(len(faces), -1)

    # Fix names
    with np.nditer(target_names, op_flags=['readwrite']) as it:
        for x in it:
            x[...] = np.core.defchararray.replace(x, 'pins ', '')
            x[...] = np.core.defchararray.replace(x, ' face', '')
            x[...] = np.core.defchararray.title(x)

    # pack the results as a Bunch instance
    return Bunch(data=X, images=faces,
                 target=target, target_names=target_names)


text:
import os
import pdb
import pickle
import logging
import shutil

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from argparse import ArgumentParser
import itertools
from torch.utils.data import random_split

from src.modules.trainer import OCRTrainer
from src.utils.utils import EarlyStopping, gmkdir
from src.models.crnn import CRNN
from src.options.ss_opts import base_opts
from src.data.pickle_dataset import PickleDataset
from src.data.synth_dataset import  SynthCollator
from src.criterions.ctc import CustomCTCLoss
from src.utils.top_sampler import SamplingTop
from main import Learner

class LearnerSemi(Learner):
    def __init__(self, model, optimizer, savepath=None, resume=False):
        self.model = model
        self.optimizer = optimizer
        self.savepath = os.path.join(savepath, 'finetuned.ckpt')
        self.cuda = torch.cuda.is_available()
        self.cuda_count = torch.cuda.device_count()
        if self.cuda:
            self.model = self.model.cuda()
        self.epoch = 0
        if self.cuda_count > 1:
            print("Let's use", torch.cuda.device_count(), "GPUs!")
            self.model = nn.DataParallel(self.model)
        self.best_score = None

    def freeze(self, index, boolean=False):
        layer = self.get_layer_groups()[index]
        for params in layer.parameters():
            params.requires_grad = boolean

    def freeze_all_but(self, index):
        n_layers = len(self.get_layer_groups())
        for i in range(n_layers):
            self.freeze(i)
        self.freeze(index, boolean=True)

    def unfreeze(self, index):
        self.freeze(index, boolean=True)

    def unfreeze_all(self):
        n_layers = len(self.get_layer_groups())
        for i in range(n_layers):
            self.unfreeze(i)

    def child(self, x):
        return list(x.children())

    def recursive_(self, child):
        if hasattr(child, 'children'):
            if len(self.child(child)) != 0:
                child = self.child(child)
                return self.recursive_(child)
        return child

    def get_layer_groups(self):
        children = []
        for child in self.child(self.model):
            children.extend(self.recursive_(child))
        children = [child for child in children if list(child.parameters())]
        return children

if __name__ == '__main__':
    parser = ArgumentParser()
    base_opts(parser)
    args = parser.parse_args()
    # Loading souce data
    args.imgdir = 'English_consortium'
    args.source_data = SynthDataset(args)
    args.collate_fn = SynthCollator()
    # Loading target data an splitting
    # into train and val
    args.imgdir = 'English_unannotated'
    target_data = SynthDataset(args)
    train_split = int(0.8*len(target_data))
    val_split = len(target_data) - train_split
    args.data_train, args.data_val = random_split(target_data, (train_split, val_split))

    args.alphabet = """Only thewigsofrcvdampbkuq.$A-210xT5'MDL,RYHJ"ISPWENj&BC93VGFKz();#:!7U64Q8?+*ZX/%"""
    args.nClasses = len(args.alphabet)
    model = CRNN(args)
    model = model.cuda()
    args.criterion = CustomCTCLoss()
    savepath = os.path.join(args.save_dir, args.name)
    gmkdir(savepath)
    gmkdir(args.log_dir)
    optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    # Loading specific model to get top samples
    resume_file = savepath + '/' + 'best.ckpt'
    print('Loading model %s'%resume_file)
    checkpoint = torch.load(resume_file)
    model.load_state_dict(checkpoint['state_dict'])
    optimizer.load_state_dict(checkpoint['opt_state_dict'])

    # Generating top samples
    args.model = model
    args.imgdir = 'target_top'
    finetunepath = args.path + '/' + args.imgdir
    gmkdir(finetunepath)
    sampler = SamplingTop(args)
    sampler.get_samples(train_on_pred=args.train_on_pred,
        combine_scoring=args.combine_scoring)
    # Joining source and top samples
    args.top_samples = SynthDataset(args)
    args.data_train = torch.utils.data.ConcatDataset([args.source_data, args.top_samples])
    print('Traininig Data Size:{}\nVal Data Size:{}'.format(
        len(args.data_train), len(args.data_val)))
    learner = LearnerSemi(args.model, optimizer, savepath=savepath, resume=args.resume)
    learner.fit(args)
    shutil.rmtree(finetunepath)


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

import numpy as np
import pytest
from test_tools.generators import mk_0to1_array

import cunumeric as cn
from legate.core import LEGATE_MAX_DIM

AXES = [
    (0, 0),
    (0, 1),
    (1, 0),
    (0, -1),
    (-1, 0),
    ([0, 1], [1, 0]),
]

@pytest.mark.parametrize("ndim", range(2, LEGATE_MAX_DIM + 1))
@pytest.mark.parametrize("axes", AXES)
def test_moveaxis(ndim, axes):
    source, destination = axes
    np_a = mk_0to1_array(np, (3,) * ndim)
    cn_a = mk_0to1_array(cn, (3,) * ndim)
    np_res = np.moveaxis(np_a, source, destination)
    cn_res = cn.moveaxis(cn_a, source, destination)
    assert np.array_equal(np_res, cn_res)
    # Check that the returned array is a view
    cn_res[:] = 0
    assert cn_a.sum() == 0

if __name__ == "__main__":
    import sys

    sys.exit(pytest.main(sys.argv))

max_stars_repo_name: fduguet-nv/cunumeric

max_issues_repo_name: fduguet-nv/cunumeric

max_forks_repo_name: fduguet-nv/cunumeric


text:
import cv2
import numpy as np
import servo
import matplotlib.pyplot as plt

savedir="Camera_Data/"

cam_mtx=np.load(savedir+'cam_mtx.npy')
dist=np.load(savedir+'dist.npy')
newcam_mtx=np.load(savedir+'newcam_mtx.npy')
h_mat=np.load(savedir+'h_mtx.npy')
ratio = np.load(savedir+'ratio.npy')
hsv_range = np.load(savedir+'color.npy')

oX, oY = 0, 480
cX, cY = 0, 0
l, m = 0, 0
wait = 0.3
cal_flag = False
cal_angle = False
angle_err = 0.0
# HSV color range
lower_hue = hsv_range[0]
upper_hue = hsv_range[1]

def set_origin(u,v):
	global oX, oY
	oX = u
	oY = v

def calculate_position(u,v):

		#Solve: From Image Pixels, find World Points
		XY = [(u-oX)/ratio[0],(oY-v)/ratio[1]]
		return XY

def truncate(n, decimals=0):
	n=float(n)
	multiplier = 10 ** decimals
	return int(n * multiplier) / multiplier

# Main
# ~ cam = cv2.VideoCapture(0)
TriggerTime = 0
steps = int(input("Angular Steps ? "))
delay = int(input("Camera Delay ? "))

cam_address = input("Camera Address : ")
cam = cv2.VideoCapture('http://'+cam_address+':8080/mjpegfeed?640x480')

while(cam.isOpened()):
# while(True):
	_, frame = cam.read()
	# ~ print(delay-TriggerTime)
	if TriggerTime >= delay:
		# Apply camera calibration variables
		frame = cv2.undistort(frame, cam_mtx, dist, None, newcam_mtx)
		frame = cv2.warpPerspective(frame, h_mat, (frame.shape[1], frame.shape[0]))
		# Fix error angle
		if not cal_angle:
			(h, w) = frame.shape[:2]
			M = cv2.getRotationMatrix2D((oX,oY), angle_err, 1.0)
			frame = cv2.warpAffine(frame, M, (w,h))

		output = frame.copy()
		hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

		mask = cv2.inRange(hsv, lower_hue, upper_hue)

		# Reduce noise in mask
		kernel = np.ones((3,3),np.uint8)
		erosion = cv2.erode(mask, kernel, iterations = 2)
		dilation = cv2.dilate(erosion,kernel,iterations = 2)
		blur = cv2.GaussianBlur(dilation,(5,5),0)

		res = cv2.bitwise_and(frame,frame, mask= dilation)

		contours,_= cv2.findContours(blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

		for cnt in contours:
			approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
			if cv2.contourArea(cnt) > 100:
				x, y, w, h = cv2.boundingRect(approx)
				x = x + w/2
				y = y + h/2
				pt = calculate_position(x,y)
				x = np.round(x).astype("int")
				y = np.round(y).astype("int")
				cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
				cv2.putText(output,str(truncate(pt[0],2)) +", " +str(truncate(pt[1], 2)),(x+10,y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,(0, 0,255),2)
				# Update Points
				cX, cY = pt[0], pt[1]

		cv2.circle(output, (oX, oY), 4, (0, 255, 255), 4)
		# show the output image
		# ~ cv2.imshow("output", output)
		cv2.imshow("video", np.vstack([res,output]))

		k = cv2.waitKey(200)

		if k & 0xFF == ord('q'):
			servo.exit()
			break

		if k & 0xFF == ord('h'):
			_ = servo.home()
			print("slider at home")

		if k & 0xFF == ord('o'):
			set_origin(x, y)
			print("Origin Set to ( ", cX, ", ", cY, " )")

		if k & 0xFF == ord('a'):
			_ = servo.home()
			l = 0
			cal_angle = True
			print("Removing Angular Error")

		if k & 0xFF == ord('c'):
			# ~ steps = int(input("Angular Steps ? "))
			# ~ wait = float(input("Wait Time in sec? "))
			map_A = np.zeros(shape=((steps+1), 2))
			map_B = np.zeros(shape=((steps+1), 2))
			l, m =0, 0
			cal_flag = True
			print("Servo Calibration Started")

		# Calculate Angular Error
		if(cal_angle):
			cv2.waitKey(int(wait*2000))
			# Move in X
			if l == 0:
				pass
			if l == 1:
				_ = servo.A(180,wait)
				angle_err = 0
			if l == 2:
				# Calculate angle - cosine rule
				try:
					delta = x-oX
					side = y-oY
					angle_err = np.sign(delta)*(np.arccos(1-delta**2/(2*side**2)))
				except:
					angle_err = 0
				print("Err Y: ", angle_err)
				_ = servo.A(0,wait)
				_ = servo.B(180,wait)
			if l == 3:
				# Calculate angle - cosine rule
				try:
					delta = oY-y
					side = x-oX
					temp = np.sign(delta)*(np.arccos(1-delta**2/(2*side**2)))
				except:
					temp = 0
				angle_err = (angle_err+temp)/2
				print("Err X: ", temp)
				_ = servo.B(0,wait)
			if l == 4:
				cal_angle = False
				print("Error Angle: ", angle_err, " \n Angle Calibration Done. Slider at Home")
			l+=1

		# Calibrate Servos
		if(cal_flag):
			cv2.waitKey(int(wait*2000))
			# X axis
			if l <= steps:
				# Save Position
				map_B[l][0] = l*180/steps
				map_B[l][1] += cX
				# Y axis
				if m <= steps:
					if(m != 0):
						map_A[m-1][1] += cY
						print("servoY: ", map_A[m-1][0], ", Y: ",cY)
					map_A[m][0] = m*180/steps
					_ = servo.A(map_A[m][0], wait)
					m += 1
				else:
					map_A[m-1][1] += cY
					print("servoY: ", map_A[m-1][0], ", Y: ",cY)
					print("servoX: ", map_B[l][0], ", X: ",cX)
					# Set New Angle B
					l += 1
					if(l<steps+1):
						map_B[l][0] = l*180/steps
						_ = servo.B(map_B[l][0], wait)
						m = 0
						print("Shift X")
			else:
				print("Calibration Successful \n Slider at Home \n")
				_ = servo.home()
				cv2.waitKey(1000)
				angle = np.zeros(shape=(steps+1))
				pltA = np.zeros(shape=(steps+1))
				pltB = np.zeros(shape=(steps+1))
				for l in range (0,steps+1):
					map_A[l][1] = (map_A[l][1]-map_A[0][1])/(steps+1)
					map_B[l][1] = (map_B[l][1]-map_B[0][1])/(steps+1)
					angle[l] = l*180/steps
					pltA[l] = map_A[l][1]
					pltB[l] = map_B[l][1]
				servo.exit()
				print("Servo A : \n", map_A,"\n Servo B : \n", map_B)
				np.save(savedir+'servoA.npy', np.array(pltA))
				np.save(savedir+'servoB.npy', np.array(pltB))
				cal_flag = False
				l, m = 0, 0
				plt.figure()
				ay = plt.subplot(211)
				ay.plot(angle, pltA,'-', angle, pltA, 'ro')
				ay.set_ylabel('Position Y')
				bx = plt.subplot(212)
				bx.plot(angle, pltB,'-', angle, pltB, 'ro')
				bx.set_ylabel('Position X')
				bx.set_xlabel('Servo Angle')
				plt.show()

		TriggerTime = 0

	TriggerTime += 1

max_stars_repo_name: jvankooo/PiPrinter

max_issues_repo_name: jvankooo/PiPrinter

max_forks_repo_name: jvankooo/PiPrinter


text:
import numpy as np
from matpy.matrix import matrix

data_1 = matrix(data=[matrix(data=np.array([1, 2, 3])), matrix(data=np.array([1, 2, 4]))])
print(data_1.get_all_data().shape)

# data_2 = matrix(np.array([1, 2, 3, 4]),
# 			 (2, 2),
# 			 ["one", "two", "three", "four"],
# 			 ["col_1", "col_2"],
# 			 ["row_1", "row_2"])
# data_2.set_one_data(matrix(np.array([1, 2, 3])), index=[0, 0])
# print(data_2.get_data()[0][0].get_data())

# print(data.get_data())
# print(data.get_one_data("one"))
# print(matrix(data).get_data())
# data.set_multiple_data(0, [0, 1], [0, 1])
# print(data.get_data())
# data.set_one_data(np.array([1, 2, 3]), index=[0, 0])

max_issues_repo_name: Caiya-Zhang/pypkpd

max_forks_repo_name: Caiya-Zhang/pypkpd


text:
# -*- coding: utf-8 -*-
"""
This example demonstrates writing a custom Node subclass for use with flowcharts.

We implement a couple of simple image processing nodes.
"""
import initExample ## Add path to library (just for examples; you do not need this)

from pyqtgraph.flowchart import Flowchart, Node
import pyqtgraph.flowchart.library as fclib
from pyqtgraph.flowchart.library.common import CtrlNode
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
import scipy.ndimage

app = QtGui.QApplication([])

## Create main window with a grid layout inside
win = QtGui.QMainWindow()
win.setWindowTitle('pyqtgraph example: FlowchartCustomNode')
cw = QtGui.QWidget()
win.setCentralWidget(cw)
layout = QtGui.QGridLayout()
cw.setLayout(layout)

## Create an empty flowchart with a single input and output
fc = Flowchart(terminals={
    'dataIn': {'io': 'in'},
    'dataOut': {'io': 'out'}
})
w = fc.widget()

layout.addWidget(fc.widget(), 0, 0, 2, 1)

## Create two ImageView widgets to display the raw and processed data with contrast
## and color control.
v1 = pg.ImageView()
v2 = pg.ImageView()
layout.addWidget(v1, 0, 1)
layout.addWidget(v2, 1, 1)

win.show()

## generate random input data
data = np.random.normal(size=(100,100))
data = 25 * scipy.ndimage.gaussian_filter(data, (5,5))
data += np.random.normal(size=(100,100))
data[40:60, 40:60] += 15.0
data[30:50, 30:50] += 15.0
#data += np.sin(np.linspace(0, 100, 1000))
#data = metaarray.MetaArray(data, info=[{'name': 'Time', 'values': np.linspace(0, 1.0, len(data))}, {}])

## Set the raw data as the input value to the flowchart
fc.setInput(dataIn=data)

## At this point, we need some custom Node classes since those provided in the library
## are not sufficient. Each node will define a set of input/output terminals, a
## processing function, and optionally a control widget (to be displayed in the
## flowchart control panel)

class ImageViewNode(Node):
    """Node that displays image data in an ImageView widget"""
    nodeName = 'ImageView'

    def __init__(self, name):
        self.view = None
        ## Initialize node with only a single input terminal
        Node.__init__(self, name, terminals={'data': {'io':'in'}})

    def setView(self, view):  ## setView must be called by the program
        self.view = view

    def process(self, data, display=True):
        ## if process is called with display=False, then the flowchart is being operated
        ## in batch processing mode, so we should skip displaying to improve performance.

        if display and self.view is not None:
            ## the 'data' argument is the value given to the 'data' terminal
            if data is None:
                self.view.setImage(np.zeros((1,1))) # give a blank array to clear the view
            else:
                self.view.setImage(data)

## register the class so it will appear in the menu of node types.
## It will appear in the 'display' sub-menu.
fclib.registerNodeType(ImageViewNode, [('Display',)])

## We will define an unsharp masking filter node as a subclass of CtrlNode.
## CtrlNode is just a convenience class that automatically creates its
## control widget based on a simple data structure.
class UnsharpMaskNode(CtrlNode):
    """Return the input data passed through scipy.ndimage.gaussian_filter."""
    nodeName = "UnsharpMask"
    uiTemplate = [
        ('sigma',  'spin', {'value': 1.0, 'step': 1.0, 'range': [0.0, None]}),
        ('strength', 'spin', {'value': 1.0, 'dec': True, 'step': 0.5, 'minStep': 0.01, 'range': [0.0, None]}),
    ]
    def __init__(self, name):
        ## Define the input / output terminals available on this node
        terminals = {
            'dataIn': dict(io='in'),    # each terminal needs at least a name and
            'dataOut': dict(io='out'),  # to specify whether it is input or output
        }                              # other more advanced options are available
                                       # as well..

        CtrlNode.__init__(self, name, terminals=terminals)

    def process(self, dataIn, display=True):
        # CtrlNode has created self.ctrls, which is a dict containing {ctrlName: widget}
        sigma = self.ctrls['sigma'].value()
        strength = self.ctrls['strength'].value()
        output = dataIn - (strength * scipy.ndimage.gaussian_filter(dataIn, (sigma,sigma)))
        return {'dataOut': output}

## register the class so it will appear in the menu of node types.
## It will appear in a new 'image' sub-menu.
fclib.registerNodeType(UnsharpMaskNode, [('Image',)])

## Now we will programmatically add nodes to define the function of the flowchart.
## Normally, the user will do this manually or by loading a pre-generated
## flowchart file.

v1Node = fc.createNode('ImageView', pos=(0, -150))
v1Node.setView(v1)

v2Node = fc.createNode('ImageView', pos=(150, -150))
v2Node.setView(v2)

fNode = fc.createNode('UnsharpMask', pos=(0, 0))
fc.connectTerminals(fc['dataIn'], fNode['dataIn'])
fc.connectTerminals(fc['dataIn'], v1Node['data'])
fc.connectTerminals(fNode['dataOut'], v2Node['data'])
fc.connectTerminals(fNode['dataOut'], fc['dataOut'])

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

max_stars_repo_name: cycomanic/pyqtgraph

max_issues_repo_name: cycomanic/pyqtgraph

max_forks_repo_name: cycomanic/pyqtgraph

reason: import numpy,import scipy


text:
import pytest
import sys
import hypothesis
import numpy as np
from rosette import rcl
from random import random
import rosette
import hypothesis.strategies as strat
from hypothesis.extra.numpy import arrays

# for different elec types, needs to be filled in...
def exatype_from_names(res_name3, atom_name):
    return 0

def to_omega(x):
    return (360.0 + x / 10.0) % 360.0 - 180.0

# here's a toy example of hypothesis...
@hypothesis.given(strat.floats(min_value=-180, max_value=180))
def test_to_omega(tor):
    assert -180.0 <= to_omega(tor) <= 180.0

def make_pose_from_seqtor(seq, tor):
    'make_pose_from_sequence and set torsions'
    p = rcl.rosetta.core.pose.Pose()
    rcl.rosetta.core.pose.make_pose_from_sequence(p, seq, 'fa_standard')
    for ir in range(1, p.size() + 1):
        p.set_phi(ir, tor[ir - 1][0])
        p.set_psi(ir, tor[ir - 1][1])
        p.set_psi(ir, to_omega(tor[ir - 1][2]))
        for ic in range(1, p.residue(ir).nchi() + 1):
            p.set_chi(ic, ir, tor[ir - 1][2 + ic])
    return p

# makes an iterator -- beware, you can only loop through once
def pose_iter(pose):
    count = 0
    for ir in range(1, pose.size() + 1):
        r = pose.residue(ir)
        for ia in range(1, r.natoms() + 1):
            yield count, ir, ia, r

# almost certainly not correct....
def pose_to_basic(pose):
    aid_to_index = {(ir, ia): i for i, ir, ia, _ in pose_iter(pose)}
    Ntot = len(aid_to_index)
    atoms = np.zeros((Ntot, 4), dtype='f8')
    atypes = np.zeros(Ntot, dtype='i4')
    exatypes = np.zeros(Ntot, dtype='i4')
    bonds = np.zeros((Ntot, 4), dtype='i4') - 1  # -1 means empty

    for i, ir, ia, r in pose_iter(pose):
        atoms[i, 0] = r.xyz(ia).x
        atoms[i, 1] = r.xyz(ia).y
        atoms[i, 2] = r.xyz(ia).z
        atoms[i, 3] = 1.0
        atypes[i] = r.atom_type_index(ia)
        exatypes[i] = exatype_from_names(r.name3(), r.atom_name(ia))
        for ib, neighbor in enumerate(r.bonded_neighbor(ia)):
            assert ib <= 3
            bonds[i, ib] = aid_to_index[ir, 1] + neighbor - 1
        if r.has_lower_connect():
            assert ib <= 3
            uconn = pose.residue(ir - 1).upper_connect_atom()
            bonds[i, ib] = aid_to_index[ir - 1, uconn]
        if r.has_upper_connect():
            assert ib <= 3
            lconn = pose.residue(ir + 1).lower_connect_atom()
            bonds[i, ib] = aid_to_index[ir + 1, lconn]
    return atoms, atypes, exatypes, bonds

# use pyrosetta as a "test oracle"
def get_scores_from_oracle(pose):
    sfxn = rcl.get_score_function()
    sfxn.score(pose)
    tot_emap = pose.energies().total_energies()
    fa_atr = tot_emap[rcl.core.scoring.fa_atr]
    fa_rep = tot_emap[rcl.core.scoring.fa_rep]
    fa_sol = tot_emap[rcl.core.scoring.fa_sol]
    return np.array([fa_atr, fa_rep, fa_sol])

# ok, this hypothesis thing is awesome... it's called property based testing
# you describe how to generate examples with @given, then it runs the test
# function on cleverly generated cases.. mix of simple and random examples
# then it'll narrow down to the simplest failure case it can. It's fucking
# perfect for cases like this where you have a "test oracle"... in our case
# pyrosetta energys to compare to
# http://hypothesis.works/
# this particular @given generates a list of tuples of (AA, 6_torsion_ang)
# which are used to make pose from sequence then set the phi/psi/chi angles
@hypothesis.given(
    strat.lists(min_size=1, elements=strat.tuples(
        strat.text(min_size=1, max_size=1,
                   alphabet='ACDEFGHIKLMNPQRSTVWY'),
        arrays('f8', (7,), elements=strat.floats(min_value=-180, max_value=180),
               ))))
@pytest.mark.skipif(not rcl.HAVE_PYROSETTA, reason='no pyrosetta')
# @pytest.mark.xfail()  # uncomment this to run but ignore this test
def test_basic_energy_safe(seqtor):
    rcl.init_check('-beta -mute all')
    seq = ''.join(t[0] for t in seqtor)
    tors = [t[1] for t in seqtor]
    pose = make_pose_from_seqtor(seq, tors)
    atoms, atypes, exatypes, bonds = pose_to_basic(pose)
    energy = rosette.energy.basic_energy_safe(atoms, atypes, exatypes, bonds)
    emap = energy.sum(axis=0)
    ehat = get_scores_from_oracle(pose)
    np.testing.assert_array_almost_equal(emap, ehat)

max_stars_repo_name: willsheffler/rosette

max_issues_repo_name: willsheffler/rosette

max_forks_repo_name: willsheffler/rosette


text:
""" bin to images test"""

import cv2
import numpy as np
import os

BINPATH = './Test/test.bin'

BATCH_SIZE = 1
CROP_SIZE = 112
CHANNEL_NUM = 3
CLIP_LENGTH = 16

imgArray = np.fromfile(BINPATH, dtype=np.float32)

imgArray.shape=1, CLIP_LENGTH, CROP_SIZE, CROP_SIZE, CHANNEL_NUM

for i in range(CLIP_LENGTH):
    img = imgArray[0][i]
    #print(img.shape)
    cv2.imwrite('./OutputImages/test' + str(i) + '.jpg', img.astype(int))