text:
from brains.lstm import LSTMPyTorch, LSTMLayered
from tools.configurations import LstmTorchCfg, LstmLayeredCfg

import numpy as np
import gym
import torch
import time

class TestLSTM:

    def test_lstm_output(self, lstm_torch_config: LstmTorchCfg, lstm_layered_config: LstmLayeredCfg):

        input_size = 28
        output_size = 8

        number_of_inputs = 10000

        input_space = gym.spaces.Box(-1, 1, (input_size,))
        output_space = gym.spaces.Box(-1, 1, (output_size,))

        individual_size = LSTMPyTorch.get_individual_size(lstm_torch_config, input_space, output_space)

        # Make two copies to avoid possible errors due to PyTorch reusing data from the same memory address
        individual_pytorch = np.random.randn(individual_size).astype(np.float32)
        individual_numpy = np.copy(individual_pytorch)

        with torch.no_grad():
            lstm_pytorch = LSTMPyTorch(input_space, output_space, individual_pytorch, lstm_torch_config)

        lstm_numpy = LSTMLayered(input_space, output_space, individual_numpy, lstm_layered_config)

        # Also initialize the values for the hidden and cell states the same
        hidden_pytorch = np.random.randn(*lstm_pytorch.hidden[0].size()).astype(np.float32)
        cell_pytorch = np.random.randn(*lstm_pytorch.hidden[1].size()).astype(np.float32)

        hidden_numpy = np.copy(hidden_pytorch).reshape(len(lstm_layered_config.hidden_layer_structure), -1)
        cell_numpy = np.copy(cell_pytorch).reshape(len(lstm_layered_config.hidden_layer_structure), -1)

        lstm_pytorch.hidden = (torch.from_numpy(hidden_pytorch), torch.from_numpy(cell_pytorch))

        lstm_numpy.hidden = [[np.copy(hidden_numpy)[i], np.copy(cell_numpy)[i]]for i in range(len(np.copy(hidden_numpy)))]

        with torch.no_grad():
            for i in range(len(lstm_layered_config.hidden_layer_structure)):
                attr_weight_ih_li = "weight_ih_l{}".format(i)
                attr_weight_hh_li = "weight_hh_l{}".format(i)
                weight_ih = "weight_ih".format(i)
                weight_hh = "weight_hh".format(i)

                weight_ih_li_pytorch = getattr(lstm_pytorch.brain, attr_weight_ih_li).detach().numpy()
                weight_hh_li_pytorch = getattr(lstm_pytorch.brain, attr_weight_hh_li).detach().numpy()

                weight_ih_li_numpy = getattr(lstm_numpy, weight_ih)[i].reshape(weight_ih_li_pytorch.shape)
                weight_hh_li_numpy = getattr(lstm_numpy, weight_hh)[i].reshape(weight_hh_li_pytorch.shape)

                assert np.array_equal(weight_ih_li_pytorch, weight_ih_li_numpy)
                assert np.array_equal(weight_hh_li_pytorch, weight_hh_li_numpy)

                # Check hidden and cell states
                assert np.array_equal(lstm_pytorch.hidden[0][i].view(-1).detach().numpy(), lstm_numpy.hidden[i][0])
                assert np.array_equal(lstm_pytorch.hidden[1][i].view(-1).detach().numpy(), lstm_numpy.hidden[i][1])

        inputs = np.random.randn(number_of_inputs, 28).astype(np.float32)

        lstm_pytorch_outputs = []
        lstm_numpy_outputs = []

        lstm_pytorch_times = []
        lstm_numpy_times = []

        # Collect predictions of PyTorch and NumPy implementations and collect time data
        for i in inputs:
            with torch.no_grad():
                time_s = time.time()
                lstm_pytorch_output = lstm_pytorch.step(i)
                lstm_pytorch_times.append(time.time() - time_s)
                lstm_pytorch_outputs.append(lstm_pytorch_output)

            time_s = time.time()
            lstm_numpy_output = lstm_numpy.step(i)
            lstm_numpy_times.append(time.time() - time_s)
            lstm_numpy_outputs.append(lstm_numpy_output)

        lstm_pytorch_outputs = np.array(lstm_pytorch_outputs)
        lstm_numpy_outputs = np.array(lstm_numpy_outputs)

        lstm_pytorch_times = np.array(lstm_pytorch_times)
        lstm_numpy_times = np.array(lstm_numpy_times)

        assert len(lstm_pytorch_outputs) == len(lstm_numpy_outputs)
        assert lstm_pytorch_outputs.size == lstm_numpy_outputs.size

        print("PyTorch Mean Prediction Time {}s | NumPy Mean Prediction Time {}s"
              .format(np.mean(lstm_pytorch_times), np.mean(lstm_numpy_times)))

        print("PyTorch Stddev Prediction Time {}s | NumPy Stddev Prediction Time {}s"
              .format(np.std(lstm_pytorch_times), np.std(lstm_numpy_times)))

        print("PyTorch Max Prediction Time {}s | NumPy Max Prediction Time {}s"
              .format(np.max(lstm_pytorch_times), np.max(lstm_numpy_times)))

        print("PyTorch Min Prediction Time {}s | NumPy Min Prediction Time {}s"
              .format(np.min(lstm_pytorch_times), np.min(lstm_numpy_times)))

        # Use percentage instead of np.allclose() because some results exceed the rtol value, but it is a low percentage
        close_percentage = np.count_nonzero(
            np.isclose(lstm_pytorch_outputs, lstm_numpy_outputs)) / lstm_pytorch_outputs.size
        assert close_percentage > 0.95

        print(
            "Equal predictions between PyTorch and NumPy",
            "Implementation of LSTM: {}% of {} predictions".format(close_percentage*100, number_of_inputs))


text:
# This file converts the CSV output of the batch processing API to a pickle file, which can be used by the
# script ./make_classification_dataset.py

import argparse
import os
import json
import pickle
import pandas
import numpy as np
import tqdm

parser = argparse.ArgumentParser('This file converts the CSV output of the batch processing API to a pickle file, ' + \
                   'which can be used by the script ./make_classification_dataset.py')
parser.add_argument("input_csv", type=str, help='Path to the CSV file that contains the API output')
parser.add_argument("output_pkl", type=str, help='Path to the desired output pickle file')
args = parser.parse_args()

assert os.path.isfile(args.input_csv), 'ERROR: The input CSV file could not be found!'
assert not os.path.isfile(args.output_pkl), 'ERROR: The output file exists already!'

csv = pandas.read_csv(args.input_csv, header=0, names=['filename',  'maxscore', 'boxes'])
detection_dict = dict()
for row in tqdm.tqdm(list(csv.itertuples())):
    boxes = np.array(json.loads(row.boxes))
    if boxes.size > 0:
        box_selection = np.isclose(boxes[:,5], 1)
        conf = boxes[box_selection, 4].ravel()
        boxes = boxes[box_selection, :4]
    else:
        conf = np.array([[]])
        boxes = np.array([[]])
    key = row.filename
    detection_dict[key] = dict(detection_scores=conf, detection_boxes=boxes)

# Write detections to file with pickle
with open(args.output_pkl, 'wb') as f:
    pickle.dump(detection_dict, f, pickle.HIGHEST_PROTOCOL)

ext: py

max_stars_repo_name: dnarqq/WildHack

max_issues_repo_name: gitlost-murali/CameraTraps

max_forks_repo_name: gitlost-murali/CameraTraps

reason: import numpy


text:
import tensorflow as tf

import numpy as np
from model import GraphAttentionModel
from dataset import CoraData
from collections import namedtuple

import matplotlib.pyplot as plt

# INPUT_DIM = 1433    # 输入维度
# # Note: 采样的邻居阶数需要与GCN的层数保持一致
# HIDDEN_DIM = [128, 7]   # 隐藏单元节点数
# NUM_NEIGHBORS_LIST = [10, 10]   # 每阶采样邻居的节点数
# assert len(HIDDEN_DIM) == len(NUM_NEIGHBORS_LIST)
BTACH_SIZE = 16     # 批处理大小
EPOCHS = 20
NUM_BATCH_PER_EPOCH = 20    # 每个epoch循环的批次数
LEARNING_RATE = 0.01    # 学习率
NUM_HEADS = 8
INPUT_DIM = 1433
HIDDEN_DIM = 24
OUTPUT_DIM = 7

Data = namedtuple('Data', ['x', 'y', 'traj','train_mask', 'val_mask', 'test_mask'])

data = CoraData().data()

train_index = np.where(data.train_mask)[0]
train_label = data.y[train_index]
test_index = np.where(data.test_mask)[0]
val_index = np.where(data.val_mask)[0]

model = GraphAttentionModel(INPUT_DIM, HIDDEN_DIM, OUTPUT_DIM, NUM_HEADS)

loss_object = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
optimizer=tf.keras.optimizers.Adam(learning_rate=0.01, decay=5e-4)

# 记录过程值，以便最后可视化
train_loss_results = []
train_accuracy_results = []
train_val_results = []
train_test_results = []

def train():
    for e in range(EPOCHS):
        for batch in range(NUM_BATCH_PER_EPOCH):
            batch_src_index = np.random.choice(train_index, size=(BTACH_SIZE,))
            batch_src_label = train_label[batch_src_index].astype(float)

            batch_sampling_x = data.x[batch_src_index]
            batch_adj = data.adj[np.ix_(batch_src_index, batch_src_index)]

            loss = 0.0
            with tf.GradientTape() as tape:
                batch_train_logits = model([batch_sampling_x, batch_adj], training = True)
                loss = loss_object(batch_src_label, batch_train_logits)
                grads = tape.gradient(loss, model.trainable_variables)

                optimizer.apply_gradients(zip(grads, model.trainable_variables))

            # print("Epoch {:03d} Batch {:03d} Loss: {:.4f}".format(e, batch, loss))

        train_accuracy = test(train_index)
        val_accuracy = test(val_index)
        test_accuracy = test(test_index)

        train_loss_results.append(loss)
        train_accuracy_results.append(train_accuracy)
        train_val_results.append(val_accuracy)
        train_test_results.append(test_accuracy)

        print("Epoch {:03d} train accuracy: {} val accuracy: {} test accuracy:{}".format(e, train_accuracy, val_accuracy, test_accuracy))

        # ISSUE: https://stackoverflow.com/questions/58947679/no-gradients-provided-for-any-variable-in-tensorflow2-0

    # 训练过程可视化
    fig, axes = plt.subplots(4, sharex=True, figsize=(12, 8))
    fig.suptitle('Training Metrics')

    axes[0].set_ylabel("Loss", fontsize=14)
    axes[0].plot(train_loss_results)

    axes[1].set_ylabel("Accuracy", fontsize=14)
    axes[1].plot(train_accuracy_results)

    axes[2].set_ylabel("Val Acc", fontsize=14)
    axes[2].plot(train_val_results)

    axes[3].set_ylabel("Test Acc", fontsize=14)
    axes[3].plot(train_test_results)

    plt.show()

def test(index):
    test_x = data.x[index]

    test_adj = data.adj[np.ix_(index, index)]

    test_logits = model([test_x, test_adj], training = False)
    test_label = data.y[index]

    ll = tf.math.equal(tf.math.argmax(test_label, -1), tf.math.argmax(test_logits, -1))
    accuarcy = tf.reduce_mean(tf.cast(ll, dtype=tf.float32))

    return accuarcy

if __name__ == '__main__':
    train()

max_stars_repo_name: geneprophet/VGAE

max_issues_repo_name: geneprophet/VGAE

max_forks_repo_name: geneprophet/VGAE


text:
import numpy as np
from bolero.representation.context_transformations import (
    constant, linear, affine, quadratic, cubic, polynomial)
from nose.tools import assert_equal, assert_in
from numpy.testing import assert_array_equal

def test_constant():
    context = np.array([1.0, 2.0])
    assert_equal(constant(context), 1.0)
    context = np.array([3.0, 2.0])
    assert_equal(constant(context), 1.0)

def test_linear():
    context = np.array([1.0, 2.0])
    assert_array_equal(linear(context), np.array([1.0, 2.0]))
    context = np.array([3.0, 2.0])
    assert_array_equal(linear(context), np.array([3.0, 2.0]))

def test_affine():
    context = np.array([1.0, 2.0])
    assert_array_equal(affine(context), np.array([2.0, 1.0, 1.0]))
    context = np.array([3.0, 2.0])
    assert_array_equal(affine(context), np.array([2.0, 3.0, 1.0]))

def test_quadratic():
    context = np.array([1.0, 2.0])
    transformed = quadratic(context)
    assert_in(1.0, transformed)
    assert_in(2.0, transformed)
    assert_in(4.0, transformed)
    assert_equal(len(transformed), 6)
    context = np.array([3.0, 2.0])
    transformed = quadratic(context)
    assert_in(1.0, transformed)
    assert_in(2.0, transformed)
    assert_in(3.0, transformed)
    assert_in(6.0, transformed)
    assert_equal(len(transformed), 6)

def test_cubic():
    context = np.array([1.0, 2.0])
    transformed = cubic(context)
    assert_in(1.0, transformed)
    assert_in(2.0, transformed)
    assert_in(4.0, transformed)
    assert_in(8.0, transformed)
    assert_equal(len(transformed), 10)
    context = np.array([3.0, 2.0])
    transformed = cubic(context)
    assert_in(1.0, transformed)
    assert_in(2.0, transformed)
    assert_in(4.0, transformed)
    assert_in(8.0, transformed)
    assert_in(6.0, transformed)
    assert_in(9.0, transformed)
    assert_in(27.0, transformed)
    assert_equal(len(transformed), 10)

def test_polynomial():
    context = np.array([1.0, 2.0])
    assert_array_equal(quadratic(context), polynomial(context, n_degrees=2))
    assert_array_equal(cubic(context), polynomial(context, n_degrees=3))
    context = np.array([3.0, 2.0])
    assert_array_equal(quadratic(context), polynomial(context, n_degrees=2))
    assert_array_equal(cubic(context), polynomial(context, n_degrees=3))

max_stars_repo_name: dettmann/bolero

max_issues_repo_name: dettmann/bolero

max_forks_repo_name: dettmann/bolero

reason: import numpy,from numpy


text:
"""
Tests of the LWTA functionality.
"""
__author__ = "Ian Goodfellow, Minh Ngoc Le"

import os
import numpy as np
import pylearn2
from pylearn2.utils import sharedX
from pylearn2.models.lwta import lwta
from pylearn2.scripts.tests.yaml_testing import limited_epoch_train

def test_lwta_simple():

    """Test simple cases."""

    example_input = np.zeros((2, 6))

    # begin block
    example_input[0, 0] = -2.5
    example_input[0, 1] = 1.3  # max
    example_input[0, 2] = 0.9
    # begin block
    example_input[0, 3] = -0.1  # tied for max
    example_input[0, 4] = -0.2
    example_input[0, 5] = -0.1  # tied for max
    # begin block
    example_input[1, 0] = 5.0  # max
    example_input[1, 1] = 4.0
    example_input[1, 2] = 3.0
    # begin block
    example_input[1, 3] = 0.0
    example_input[1, 4] = 1.0
    example_input[1, 5] = 2.0  # max

    output = lwta(sharedX(example_input), block_size=3).eval()

    num_zeros = (output == 0).sum()
    assert num_zeros == 8

    assert np.allclose(output[0, 1], 1.3), output[0, 1]
    assert np.allclose(output[0, 3], -0.1) or np.allclose(output[0, 5], -0.1)
    assert np.allclose(output[1, 0], 5.0)
    assert np.allclose(output[1, 5], 2.0)

def test_lwta_yaml():
    """Test simple model on random data."""
    limited_epoch_train(os.path.join(pylearn2.__path__[0],
                                     "models/tests/lwta.yaml"))


text:
"""
This script can be used to make a few plots showing the Stokes parameters for Vela.

:requires: pyfits
:requires: matplotlib
:requires: Kapteyn Python package
:requires: pyregion

:author: Sami-Matias Niemi
:contact: sammy@sammyniemi.com

:version: 0.1
"""
from time import time
import pyregion
import numpy as np
import pyfits as pf
from kapteyn import maputils
from matplotlib import pyplot as plt
from pyregion.mpl_helper import properties_func_default

def func(x):
    nx = ny = x
    annim.set_blur(True, nx, ny, new=True)

def fixed_color(shape, saved_attrs):
    attr_list, attr_dict = saved_attrs
    attr_dict['color'] = 'black'
    kwargs = properties_func_default(shape, (attr_list, attr_dict))
    return kwargs

if __name__ == '__main__':
    start = time()

    #plot set up
    units = r'$\log_{10}()$'
    limy = limx = (200, 2100)
    rows = 3
    cols = 2

    #plotting
    fig = plt.figure(figsize=(16, 20))

    #first frame
    frame1 = fig.add_subplot(rows, cols, 1)
    f = maputils.FITSimage('StokesISmooth15.fits')
    frame1.set_title(f.filename, y=1.01)
    f.set_limits(limx, limy)
    f = maputils.FITSimage(externalheader=f.hdr, externaldata=np.log10(f.boxdat))
    mplim = f.Annotatedimage(frame1, cmap="spectral", clipmin=-5.5, clipmax=-3)
    mplim.Image()
    cb = mplim.Colorbar(fontsize=8)
    cb.set_label(label=units, fontsize=9)
    grat = mplim.Graticule()
    grat.setp_ticklabel(plotaxis=['top', 'right'], visible=False)
    grat.setp_ticklabel(wcsaxis=1, fmt='s')   # Exclude seconds in label
    grat.setp_gratline(wcsaxis=0, linestyle=':', zorder=1)
    grat.setp_gratline(wcsaxis=1, linestyle=':', zorder=1)
    #region file
    r = pyregion.open('region_forHST.reg').as_imagecoord(pf.open('StokesISmooth15.fits')[0].header)
    patch_list, artist_list = r.get_mpl_patches_texts(fixed_color)
    for p in patch_list:
        frame1.add_patch(p)
    for t in artist_list:
        frame1.add_artist(t)
    mplim.plot()
    #print "Proposed levels:", cont.clevels
    print "clip min, max:", mplim.clipmin, mplim.clipmax

    f = maputils.FITSimage('StokesUSmooth15.fits')
    f.set_limits(limx, limy)
    frame2 = fig.add_subplot(rows, cols, 2)
    frame2.set_title(f.filename, y=1.01)
    f = maputils.FITSimage(externalheader=f.hdr, externaldata=np.log10(f.boxdat))
    mplim = f.Annotatedimage(frame2, cmap="spectral", clipmin=-6, clipmax=-4)
    mplim.Image()
    cb = mplim.Colorbar(fontsize=8)
    cb.set_label(label=units, fontsize=9)
    grat = mplim.Graticule()
    grat.setp_ticklabel(plotaxis=['top', 'right'], visible=False)
    grat.setp_ticklabel(wcsaxis=1, fmt='s')   # Exclude seconds in label
    grat.setp_gratline(wcsaxis=0, linestyle=':', zorder=1)
    grat.setp_gratline(wcsaxis=1, linestyle=':', zorder=1)
    cont = mplim.Contours()
    #region file
    r = pyregion.open('region_forHST.reg').as_imagecoord(pf.open('StokesUSmooth15.fits')[0].header)
    patch_list, artist_list = r.get_mpl_patches_texts(fixed_color)
    for p in patch_list:
        frame2.add_patch(p)
    for t in artist_list:
        frame2.add_artist(t)
    mplim.plot()
    print "clip min, max:", mplim.clipmin, mplim.clipmax

    f = maputils.FITSimage('StokesQSmooth15.fits')
    f.set_limits(limx, limy)
    frame3 = fig.add_subplot(rows, cols, 3)
    frame3.set_title(f.filename, y=1.01)
    f = maputils.FITSimage(externalheader=f.hdr, externaldata=np.log10(f.boxdat))
    mplim = f.Annotatedimage(frame3, cmap="spectral", clipmin=-8, clipmax=-2)
    mplim.Image()
    cb = mplim.Colorbar(fontsize=8)
    cb.set_label(label=units, fontsize=9)
    grat = mplim.Graticule()
    grat.setp_gratline(wcsaxis=0, linestyle=':', zorder=1)
    grat.setp_gratline(wcsaxis=1, linestyle=':', zorder=1)
    grat.setp_ticklabel(plotaxis=['top', 'right'], visible=False)
    grat.setp_ticklabel(wcsaxis=1, fmt='s')   # Exclude seconds in label
    #region file
    r = pyregion.open('region_forHST.reg').as_imagecoord(pf.open('StokesQSmooth15.fits')[0].header)
    patch_list, artist_list = r.get_mpl_patches_texts(fixed_color)
    for p in patch_list:
        frame3.add_patch(p)
    for t in artist_list:
        frame3.add_artist(t)
    mplim.plot()
    print "clip min, max:", mplim.clipmin, mplim.clipmax

    f = maputils.FITSimage('PolIntSmooth15.fits')
    f.set_limits(limx, limy)
    frame4 = fig.add_subplot(rows, cols, 4)
    frame4.set_title(f.filename, y=1.01)
    f = maputils.FITSimage(externalheader=f.hdr, externaldata=np.log10(f.boxdat))
    mplim = f.Annotatedimage(frame4, cmap="spectral", clipmin=-7.5, clipmax=-1)
    mplim.Image()
    cb = mplim.Colorbar(fontsize=8)
    cb.set_label(label=units, fontsize=9)
    grat = mplim.Graticule()
    grat.setp_gratline(wcsaxis=0, linestyle=':', zorder=1)
    grat.setp_gratline(wcsaxis=1, linestyle=':', zorder=1)
    grat.setp_ticklabel(plotaxis=['top', 'right'], visible=False)
    grat.setp_ticklabel(wcsaxis=1, fmt='s')   # Exclude seconds in label
    #region file
    r = pyregion.open('region_forHST.reg').as_imagecoord(pf.open('PolIntSmooth15.fits')[0].header)
    patch_list, artist_list = r.get_mpl_patches_texts(fixed_color)
    for p in patch_list:
        frame4.add_patch(p)
    for t in artist_list:
        frame4.add_artist(t)
    mplim.plot()
    print "clip min, max:", mplim.clipmin, mplim.clipmax

    f = maputils.FITSimage('NormPolIntSmooth15.fits')
    f.set_limits(limx, limy)
    frame5 = fig.add_subplot(rows, cols, 5)
    frame5.set_title(f.filename, y=1.01)
    f = maputils.FITSimage(externalheader=f.hdr, externaldata=np.log10(f.boxdat))
    mplim = f.Annotatedimage(frame5, cmap="spectral", clipmin=-1.9, clipmax=0)
    mplim.Image()
    cb = mplim.Colorbar(fontsize=8)
    cb.set_label(label=units, fontsize=9)
    grat = mplim.Graticule()
    grat.setp_gratline(wcsaxis=0, linestyle=':', zorder=1)
    grat.setp_gratline(wcsaxis=1, linestyle=':', zorder=1)
    grat.setp_ticklabel(plotaxis=['top', 'right'], visible=False)
    grat.setp_ticklabel(wcsaxis=1, fmt='s')   # Exclude seconds in label
    #region file
    r = pyregion.open('region_forHST.reg').as_imagecoord(pf.open('NormPolIntSmooth15.fits')[0].header)
    patch_list, artist_list = r.get_mpl_patches_texts(fixed_color)
    for p in patch_list:
        frame5.add_patch(p)
    for t in artist_list:
        frame5.add_artist(t)
    mplim.plot()
    print "clip min, max:", mplim.clipmin, mplim.clipmax

    f = maputils.FITSimage('POL120V_drz_single_sci.fits')
    f.set_limits(limx, limy)
    frame6 = fig.add_subplot(rows, cols, 6)
    frame6.set_title(f.filename.replace('_', '\_'), y=1.01)
    f = maputils.FITSimage(externalheader=f.hdr, externaldata=np.log10(f.boxdat))
    mplim = f.Annotatedimage(frame6, cmap="spectral", clipmin=-7, clipmax=-4)
    mplim.Image()
    cb = mplim.Colorbar(fontsize=8)
    cb.set_label(label=r'$\log_{10}(cps)$', fontsize=9)
    grat = mplim.Graticule()
    grat.setp_gratline(wcsaxis=0, linestyle=':', zorder=1)
    grat.setp_gratline(wcsaxis=1, linestyle=':', zorder=1)
    grat.setp_ticklabel(plotaxis=['top', 'right'], visible=False)
    grat.setp_ticklabel(wcsaxis=1, fmt='s')   # Exclude seconds in label
    #region file
    r = pyregion.open('region_forHST.reg').as_imagecoord(pf.open('POL120V_drz_single_sci.fits')[0].header)
    patch_list, artist_list = r.get_mpl_patches_texts(fixed_color)
    for p in patch_list:
        frame6.add_patch(p)
    for t in artist_list:
        frame6.add_artist(t)
    mplim.plot()
    print "clip min, max:", mplim.clipmin, mplim.clipmax

    plt.savefig('Vela.pdf')
    plt.close()

    #    #interactive:
    #    f = maputils.FITSimage('StokesUSmooth15.fits')
    #    fig = plt.figure(figsize=(9,7))
    #    frame = fig.add_subplot(1,1,1)
    #    fr2 = fig.add_axes([0.3,0.01,0.4,0.03])
    #    valinit = 1.0
    #    sl = Slider(fr2, "Sigma: ", 0.1, 5.0, valinit=valinit)
    #    sl.on_changed(func)
    #
    #    annim = f.Annotatedimage(frame)
    #    annim.Image()
    #    annim.plot()
    #    func(valinit)
    #    annim.interact_toolbarinfo()
    #    annim.interact_imagecolors()
    #    annim.interact_writepos()
    #
    #    plt.show()

    elapsed = time() - start
    print 'Processing took {0:.1f} minutes'.format(elapsed / 60.)

max_stars_repo_name: sniemi/SamPy

max_issues_repo_name: sniemi/SamPy

max_forks_repo_name: sniemi/SamPy


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

"""
Example of running PyMC4 to fit the parameters of a straight line.
"""

from __future__ import print_function, division

import os
import sys
import numpy as np

import matplotlib as mpl
mpl.use("Agg") # force Matplotlib backend to Agg

# import PyMC4
import pymc4 as pm4
import tensorflow as tf

# import model and data
from createdata import *

cmin = -10.  # lower range of uniform distribution on c
cmax = 10.   # upper range of uniform distribution on c

mmu = 0.      # mean of Gaussian distribution on m
msigma = 10.  # standard deviation of Gaussian distribution on m

# convert x values and data to tensors
xtensor = tf.convert_to_tensor(x, dtype=tf.float32)
datatensor = tf.convert_to_tensor(data, dtype=tf.float32)

# model must be wrapped with the model decorator
@pm4.model
def model(x, data, sigma=sigma):
    cvalue = yield pm4.Uniform(name='c', low=cmin, high=cmax)  # prior on c
    mvalue = yield pm4.Normal(name='m', loc=mmu, scale=msigma) # prior on m

    # the straight line model
    mu = cvalue + mvalue * x

    # the likelihood
    yield pm4.Normal(name='t_obs', loc=mu, scale=sigma, observed=data)

Nsamples = 2000  # final number of samples
Nburn = 2000     # number of tuning samples

# perform the sampling
pymc4_trace = pm4.sample(
    model(xtensor, datatensor),
    num_chains=1,
    burn_in=Nburn,
    num_samples=Nsamples,
)

postsamples = np.vstack((
    pymc4_trace.posterior["model/m"].data.flatten(),
    pymc4_trace.posterior["model/c"].data.flatten()
)).T

# plot posterior samples (if corner.py is installed)
try:
    import corner # import corner.py
except ImportError:
    sys.exit(1)

print('Number of posterior samples is {}'.format(postsamples.shape[0]))

fig = corner.corner(postsamples, labels=[r"$m$", r"$c$"], truths=[m, c])
fig.savefig('pymc4.png')

max_stars_repo_name: mattpitkin/samplers-demo

max_issues_repo_name: mattpitkin/samplers-demo

max_forks_repo_name: mattpitkin/samplers-demo


text:
# coding=utf-8
import os
import shutil
import sys
import time

import cv2
import numpy as np
import tensorflow as tf

sys.path.append(os.getcwd())
from nets import model_train as model
from util.text_connector.detectors import TextDetector
from util.rpn_msr.proposal_layer import proposal_layer

tf.app.flags.DEFINE_string('test_data_path', 'data/demo/', '')
tf.app.flags.DEFINE_string('output_path', 'data/res/', '')
tf.app.flags.DEFINE_string('gpu', '0', '')
tf.app.flags.DEFINE_string('checkpoint_path', 'checkpoints_mlt/', '')
FLAGS = tf.app.flags.FLAGS

def get_images():
    files = []
    exts = ['jpg', 'png', 'jpeg', 'JPG']
    for parent, dirnames, filenames in os.walk(FLAGS.test_data_path):
        for filename in filenames:
            for ext in exts:
                if filename.endswith(ext):
                    files.append(os.path.join(parent, filename))
                    break
    print('Find {} images'.format(len(files)))
    return files

def resize_image(img):
    img_size = img.shape
    im_size_min = np.min(img_size[0:2])
    im_size_max = np.max(img_size[0:2])

    im_scale = float(600) / float(im_size_min)
    if np.round(im_scale * im_size_max) > 1200:
        im_scale = float(1200) / float(im_size_max)
    new_h = int(img_size[0] * im_scale)
    new_w = int(img_size[1] * im_scale)

    new_h = new_h if new_h // 16 == 0 else (new_h // 16 + 1) * 16
    new_w = new_w if new_w // 16 == 0 else (new_w // 16 + 1) * 16

    re_im = cv2.resize(img, (new_w, new_h), interpolation=cv2.INTER_LINEAR)
    return re_im, (new_h / img_size[0], new_w / img_size[1])

def main(argv=None):
    if os.path.exists(FLAGS.output_path):
        shutil.rmtree(FLAGS.output_path)
    os.makedirs(FLAGS.output_path)
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu

    with tf.get_default_graph().as_default():
        input_image = tf.placeholder(tf.float32, shape=[None, None, None, 3], name='input_image')
        input_im_info = tf.placeholder(tf.float32, shape=[None, 3], name='input_im_info')

        global_step = tf.get_variable('global_step', [], initializer=tf.constant_initializer(0), trainable=False)

        bbox_pred, cls_pred, cls_prob = model.model(input_image)

        variable_averages = tf.train.ExponentialMovingAverage(0.997, global_step)
        saver = tf.train.Saver(variable_averages.variables_to_restore())

        with tf.Session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
            ckpt_state = tf.train.get_checkpoint_state(FLAGS.checkpoint_path)
            model_path = os.path.join(FLAGS.checkpoint_path, os.path.basename(ckpt_state.model_checkpoint_path))
            print('Restore from {}'.format(model_path))
            saver.restore(sess, model_path)

            im_fn_list = get_images()
            for im_fn in im_fn_list:
                print('===============')
                print(im_fn)
                start = time.time()
                try:
                    im = cv2.imread(im_fn)[:, :, ::-1]
                except:
                    print("Error reading image {}!".format(im_fn))
                    continue

                img, (rh, rw) = resize_image(im)
                h, w, c = img.shape
                im_info = np.array([h, w, c]).reshape([1, 3])
                bbox_pred_val, cls_prob_val = sess.run([bbox_pred, cls_prob],
                                                       feed_dict={input_image: [img],
                                                                  input_im_info: im_info})

                textsegs, _ = proposal_layer(cls_prob_val, bbox_pred_val, im_info)
                scores = textsegs[:, 0]
                textsegs = textsegs[:, 1:5]

                textdetector = TextDetector(DETECT_MODE='H')
                boxes = textdetector.detect(textsegs, scores[:, np.newaxis], img.shape[:2])
                boxes = np.array(boxes, dtype=np.int)

                cost_time = (time.time() - start)
                print("cost time: {:.2f}s".format(cost_time))

                for i, box in enumerate(boxes):
                    cv2.polylines(img, [box[:8].astype(np.int32).reshape((-1, 1, 2))], True, color=(0, 255, 0),
                                  thickness=2)
                img = cv2.resize(img, None, None, fx=1.0 / rh, fy=1.0 / rw, interpolation=cv2.INTER_LINEAR)
                cv2.imwrite(os.path.join(FLAGS.output_path, os.path.basename(im_fn)), img[:, :, ::-1])

                with open(os.path.join(FLAGS.output_path, os.path.splitext(os.path.basename(im_fn))[0]) + ".txt",
                          "w") as f:
                    for i, box in enumerate(boxes):
                        line = ",".join(str(box[k]) for k in range(8))
                        line += "," + str(scores[i]) + "\r\n"
                        f.writelines(line)

if __name__ == '__main__':
    tf.app.run()

max_stars_repo_name: seyoulala/cptn-crnn

max_issues_repo_name: seyoulala/cptn-crnn

max_forks_repo_name: seyoulala/cptn-crnn


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

import theano.tensor as T
import inspect

def wrap_core(func, train_config, *args):
    spec = inspect.getargspec(func)
    params = spec.args[-len(spec.defaults):]
    default_values = spec.defaults
    kwargs = dict(zip(params, default_values))
    if train_config:
        for param, default in zip(params, default_values):
            config_val = train_config.get(param, default)
            kwargs[param] = config_val
    return list(func(*args, **kwargs))

def multiple_l2_norm(tensors):
    """
    Get the L2 norm of multiple tensors.
    This function is taken from blocks.
    """
    # Another way for doing this, I don't know which one is fast
    # return T.sqrt(sum(T.sum(t ** 2) for t in tensors))
    flattened = [T.as_tensor_variable(t).flatten() for t in tensors]
    flattened = [(t if t.ndim > 0 else t.dimshuffle('x'))
                 for t in flattened]
    joined = T.join(0, *flattened)
    return T.sqrt(T.sqr(joined).sum())

max_stars_repo_name: uaca/deepy

max_issues_repo_name: uaca/deepy

max_forks_repo_name: zomux/deepy

reason: import theano


text:
"""Genererate tags for recipes

This code is based on Jeremy Howards' Strong linear baseline available here:
https://www.kaggle.com/jhoward/nb-svm-strong-linear-baseline
"""

import re
import string
import numpy as np
import pandas as pd

from sklearn.metrics import roc_auc_score
from sklearn.linear_model import LogisticRegression
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.model_selection import train_test_split

# For test
FILE_LOC: str = '/home/krispin/data/improved-happiness/'
# For dev
#FILE_LOC: str = '/home/krispin/data/improved-happiness/proto/'
LABEL_COLS = ['Peanut Free',
 'Soy Free',
 'Bon Appétit',
 'Tree Nut Free',
 'Vegetarian',
 'Kosher',
 'Pescatarian',
 'Gourmet',
 'Quick & Easy',
 'Wheat/Gluten-Free']
PREP = 'preparation_steps'
re_tok = re.compile(f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])')

def tokenize(s):
    return re_tok.sub(r' \1 ', s).split()

def pr(x, y_i, y):
    p = x[y == y_i].sum(0)
    return (p+1) / ((y == y_i).sum()+1)

def get_model(x, y):
    y = y.values
    r = np.log(pr(x, 1, y) / pr(x, 0, y))
    m = LogisticRegression(C=4, dual=True, solver='liblinear')
    x_nb = x.multiply(r)
    return m.fit(x_nb, y), r

def main():
    filename = FILE_LOC + 'recipe_tags.csv'
    df = pd.read_csv(filename)
    X_train, X_test, y_train, y_test = train_test_split(
        df[PREP], df[LABEL_COLS], test_size=0.33, random_state=42)

    assert not X_train.isnull().any()

    vec = TfidfVectorizer(ngram_range=(1, 2), tokenizer=tokenize,
                          min_df=3, max_df=0.9, strip_accents='unicode', use_idf=1,
                          smooth_idf=1, sublinear_tf=1)
    trn_term_doc = vec.fit_transform(X_train)
    test_term_doc = vec.transform(X_test)

    preds = np.zeros((len(X_test), len(LABEL_COLS)))
    for i, j in enumerate(LABEL_COLS):
        m, r = get_model(trn_term_doc, y_train[j])
        preds[:, i] = m.predict_proba(test_term_doc.multiply(r))[:, 1]
        print('fitted', j)
    #Calculate ROC
    score = roc_auc_score(y_test, preds)
    print(f'ROC-AUC score for tag gen is {score:.3f}')

if __name__ == '__main__':
    main()

max_stars_repo_name: krispingal/improved-happiness

max_issues_repo_name: krispingal/improved-happiness

max_forks_repo_name: krispingal/improved-happiness


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

import cv2 as cv
import numpy as np
from PIL import Image

def execute_grabcut(
    image,
    mask,
    bgd_model,
    fgd_model,
    iteration,
    mask_alpha,
    mask_beta,
    roi=None,
):
    # GrabCut実施
    if roi is not None:
        mask, bgd_model, fgd_model = cv.grabCut(image, mask, roi, bgd_model,
                                                fgd_model, iteration,
                                                cv.GC_INIT_WITH_RECT)
    else:
        mask, bgd_model, fgd_model = cv.grabCut(image, mask, None, bgd_model,
                                                fgd_model, iteration,
                                                cv.GC_INIT_WITH_MASK)

    # デバッグ用マスク重畳画像
    mask2 = copy.deepcopy(mask)
    mask2 = np.where((mask2 == 2) | (mask2 == 0), 0, 1).astype('uint8')
    debug_image = image * mask2[:, :, np.newaxis]
    debug_image = cv.addWeighted(debug_image, mask_alpha, image, mask_beta, 0)

    return mask, bgd_model, fgd_model, debug_image

def get_palette():
    palette = [[0, 0, 0], [128, 0, 0], [0, 128, 0], [128, 128, 0], [0, 0, 128],
               [128, 0, 128], [0, 128, 128], [128, 128, 128], [64, 0, 0],
               [192, 0, 0], [64, 128, 0], [192, 128, 0], [64, 0, 128],
               [192, 0, 128], [64, 128, 128], [192, 128, 128], [0, 64, 0],
               [128, 64, 0], [0, 192, 0], [128, 192, 0], [0, 64, 128],
               [128, 64, 128]]
    return np.asarray(palette)

def save_index_color_png(output_path, filename, mask_image):
    # ファイル名(拡張子無し)取得
    base_filename = os.path.splitext(os.path.basename(filename))[0]

    # 保存先パス作成
    save_path = os.path.join(output_path, base_filename + '.png')

    # インデックスカラーモードで保存
    color_palette = get_palette().flatten()
    color_palette = color_palette.tolist()
    with Image.fromarray(mask_image, mode="P") as png_image:
        png_image.putpalette(color_palette)
        png_image.save(save_path)

def save_resize_image(output_path, filename, image):
    # ファイル名(拡張子無し)取得
    base_filename = os.path.splitext(os.path.basename(filename))[0]

    # 保存先パス作成
    save_path = os.path.join(output_path, base_filename + '.png')

    cv.imwrite(save_path, image)

def save_image_and_mask(
    output_image_path,
    image,
    output_annotation_path,
    mask_list,
    image_file_path,
    output_size,
):
    # セマンティックセグメンテーション カラーパレット取得
    color_palette = get_palette().flatten()
    color_palette = color_palette.tolist()

    # 各クラスを統合した画像を生成
    debug_mask = copy.deepcopy(mask_list[0])
    for index, mask in enumerate(mask_list):
        temp_mask = copy.deepcopy(mask)
        debug_mask = np.where((temp_mask == 2) | (temp_mask == 0), debug_mask,
                              index).astype('uint8')

    # リサイズ
    resize_image = cv.resize(image, output_size)
    resize_mask = cv.resize(debug_mask, output_size)

    save_index_color_png(output_annotation_path, image_file_path, resize_mask)
    save_resize_image(output_image_path, image_file_path, resize_image)


text:
import pytest
from numpy.testing import assert_array_almost_equal

from Auto import *

class Test_AutoSample:
    @classmethod
    def setup_method(cls):
        np.random.seed(123)
        cls.target = lambda x: np.where(x < 0, 0, np.exp(-x))
        cls.shape = (1,)
        cls.njobs = 1
        cls.algo = AutoSample(target=cls.target, shape=cls.shape, njobs=cls.njobs)

    @pytest.mark.filterwarnings("ignore::UserWarning")
    @pytest.mark.filterwarnings('ignore::RuntimeWarning')
    def test_sample(self):
        sample = self.algo.sample(size=1, chains=1)
        assert sample.shape == (1, 1)

max_stars_repo_name: GrzegorzMika/Markov-Chains-Monte-Carlo

max_issues_repo_name: GrzegorzMika/Markov-Chains-Monte-Carlo

max_forks_repo_name: GrzegorzMika/Markov-Chains-Monte-Carlo

reason: from numpy


text:
import numpy as np
np.random.seed(2016)
import os,sys,gc
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
import math
import random
random.seed(1)
import pandas
from collections import OrderedDict
from keras.layers import Input, Embedding, LSTM, Dense,Flatten, Dropout, merge
from keras.models import Model,model_from_json
from keras.optimizers import Adadelta,Adagrad
from keras import backend as K
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import PReLU

hist = []

def gendata():
    train_x = []
    train_y = []
    for i in range(200):
        x1=int(random.random() * 100)
        x2=int(random.random() * 100)
        y = x1 + x2
        train_x.append([x1,x2])
        train_y.append(y)
        x1=2+round(random.random()*3,3)
        x2=2+round(random.random()*3,3)
        y = x1 * x2
        train_x.append([x1,x2])
        train_y.append(y)
    train_x = np.array(train_x)
    train_y = np.array(train_y)
    print train_x[:10,:]
    print train_y[0:10]
    return train_x,train_y

def fit(modellist,modelmeta,X,Y,X_test = None,Y_test = None,ep = 1, mutant = False,batch_size=1,log = False):
    # modelmeta.trainable = False
    for e in range(ep):
        routetrain = modelmeta.predict(X)
        meta_label = routetrain.copy().reshape((Y.shape[0],len(modellist)))
        if mutant:
            routetrain += (np.random.random(routetrain.shape) - 0.5)/5
        #print routetrain[0:10]
        routetrain = np.array(map(lambda x: x.argmax(),routetrain)).reshape(-1,1)
        # train&update
        for modelseq in range(len(modellist)):
            index = np.where(routetrain == modelseq)[0]
            if index.shape[0] == 0:
                continue
            feat_np = []
            for x in X:
                feat_np.append(x[index])
            label_np = Y[index]
            #print meta_label[index, modelseq][0:10],meta_label[index, modelseq].shape
            if log:
                print 'modelnum: '+str(modelseq),label_np.shape
            his = modellist[modelseq].fit(feat_np,label_np,nb_epoch=1, batch_size=batch_size)
            #his = modellist[modelseq].fit(feat_np,label_np,nb_epoch=1, batch_size=batch_size,sample_weight = meta_label[index, modelseq])
            pred = modellist[modelseq].predict(feat_np, batch_size=batch_size)
            loss = abs(label_np - pred.reshape(label_np.shape))/(label_np+0.00001)
            #print 'loss:',loss[:10]
            for j in range(len(loss)):
                meta_label[index[j], modelseq] = 1.0 - loss[j]
        meta_label = meta_label.clip(0,1)
        #print meta_label[0:10]

        # update meta
        if log:
            print 'modelmeta:'
        modelmeta.fit(X,meta_label,nb_epoch=1, batch_size=batch_size)

        # valid
        '''
        if X_test_feat is not None:
            routetest = modelmeta.predict(X_test)
            routetest = np.array(map(lambda x: x.argmax(),routetest)).reshape(-1,1)
            for modelseq in range(len(modellist)):
                index = np.where(routetest == modelseq)[0]
                feat_np = []
                for x in X_test:
                    feat_np.append(x[index])
                label_np = Y[index].reshape(-1,1)
                res = modellist[modelseq].predict(feat_np,batch_size=500)
        '''
        Y_test_pred = np.zeros((Y_test.shape[0]))
        if X_test_feat is not None:
            routetest = modelmeta.predict(X_test)
            if log:
                print routetest[0:10]
            routetest = np.array(map(lambda x: x.argmax(),routetest)).reshape(-1,1)
            for modelseq in range(len(modellist)):
                index = np.where(routetest == modelseq)[0]
                feat_np = []
                if index.shape[0] == 0:
                    continue
                for x in X_test:
                    feat_np.append(x[index])
                res = modellist[modelseq].predict(feat_np,batch_size=500)
                for j in range(len(res)):
                    Y_test_pred[index[j]] = res[j]
            if log:
                print Y_test[:10]
                print Y_test_pred[:10]
            print 'loss',np.average(np.abs(Y_test-Y_test_pred)/Y_test)
            hist.append(np.average(np.abs(Y_test-Y_test_pred)/Y_test))

train_x,train_y = gendata()

X_train_feat = train_x[:-50,:]
Y_train_score = train_y[:-50]
X_test_feat = train_x[-50:,:]
Y_test_score = train_y[-50:]

# 1
input_1=Input(shape=(2,), dtype='float32')
x = BatchNormalization()(input_1)
x1 = Dense(100, activation='sigmoid')(x)
x2 = Dense(100)(x)
x2 = PReLU()(x2)
x = merge([x1, x2], mode='concat')
x = Dropout(0.2)(x)

x1 = Dense(50, activation='sigmoid')(x)
x2 = Dense(50)(x)
x2 = PReLU()(x2)
x = merge([x1, x2], mode='concat')
x = Dropout(0.2)(x)

out = Dense(1, activation='linear')(x)
model1 = Model(input=[input_1], output=out)
model1.compile(optimizer='adam',
              loss='mse',
              metrics=[])

# 2
input_1=Input(shape=(2,), dtype='float32')
x = BatchNormalization()(input_1)
x1 = Dense(100, activation='sigmoid')(x)
x2 = Dense(100)(x)
x2 = PReLU()(x2)
x = merge([x1, x2], mode='concat')
x = Dropout(0.2)(x)

x1 = Dense(50, activation='sigmoid')(x)
x2 = Dense(50)(x)
x2 = PReLU()(x2)
x = merge([x1, x2], mode='concat')
x = Dropout(0.2)(x)

out = Dense(1, activation='linear')(x)
model2 = Model(input=[input_1], output=out)
model2.compile(optimizer='adam',
              loss='mse',
              metrics=[])

#modellist = [model1]
modellist = [model1,model2]

# meta
input_1=Input(shape=(2,), dtype='float32')
x = BatchNormalization()(input_1)
x1 = Dense(40, activation='sigmoid')(x)
x2 = Dense(40)(x)
x2 = PReLU()(x2)
x = merge([x1, x2], mode='concat')
x = Dropout(0.2)(x)

x1 = Dense(20, activation='sigmoid')(x)
x2 = Dense(20)(x)
x2 = PReLU()(x2)
x = merge([x1, x2], mode='concat')
x = Dropout(0.2)(x)

out = Dense(len(modellist), activation='sigmoid')(x)
modelmeta = Model(input=[input_1], output=out)
modelmeta.compile(optimizer='adagrad',
              loss='binary_crossentropy',
              metrics=[])
#modelmeta.summary()

#print X_train_feat.shape,Y_train_score.shape
fit(modellist,modelmeta,[X_train_feat],Y_train_score,X_test = [X_test_feat],Y_test = Y_test_score,ep = 80, mutant = True,batch_size=10)
print hist


text:
from dataclasses import dataclass, field
from math import inf
from typing import Callable, List, Tuple

from numpy import array, zeros
from numpy.random import uniform

def distance(a, b):
    """Simple distance between two 1D arrays"""
    return ((a - b) ** 2).sum() ** (0.5)

@dataclass
class Particle:
    """
    Args:
        position (List[float]): The particle's current position within the
            search space.
        search_space (List[Tuple[float, float]]): see pso.
        inertia (float): see pso.
        cognitive (float): see pso.
    """

    position: List[float]
    search_space: List[Tuple[float, float]] = field(repr=False)
    fitness_func: Callable = field(repr=False)
    inertia: float
    cognitive: float  # Cognitive constant
    social: float  # Social constant

    def __post_init__(self):
        if (self.inertia < 0) or (self.inertia > 1):
            raise ValueError(f"inertia must be within [0, 1]")
        if (self.cognitive <= 0) or (self.cognitive >= 2):
            raise ValueError(f"cognitive must be within ]0, 2[")
        if (self.social <= 0) or (self.social >= 2):
            raise ValueError(f"social must be within ]0, 2[")

        self.velocity = zeros(len(self.position))
        self.position_best = self.position
        self.fitness = inf
        self.fitness_best = self.fitness

    def evaluate_fitness(self) -> None:
        """
        Particles evaluate their fitness simply by applying the fitness
        function to their current position. They keep track of their historical
        best position, which has an effect on their update velocity.
        """
        self.fitness = self.fitness_func(*self.position)
        if self.fitness < self.fitness_best:
            self.fitness_best = self.fitness
            self.position_best = self.position

    def update(self, swarm_best_pos: List[float]) -> None:
        """
        This is the core of the PSO algorithm. At each iteration (step), every
        particle updates its position in the search space according to the
        swarm's current best position, that is the position of the particle with
        the best fitness. The extent to which each particle moves towards the
        best position, its update velocity, is determined by three key constants,
        the definitions of which are worth repeating here :

        - **inertia** : A constant in the range $[0, 1]$ which determines the
            proportion by which each step's velocity will affect the change in
            position. Low values will slow the progress made at each step, and will
            potentially require more steps, but it will encourage *exploitation*.
            Higher values encourage *exploration*.
        - **cognitive** : A constant in the range $]0, 2[$, represents an individual
            particle's sense of certainty about its personal best position.
            A swarm with a high cognitive constant will favour *exploitation*.
        - **social** : A constant in the range $]0, 2[$, represents an individual
            particle's sense of certainty about the swarm's best position.
            A swarm with a high cognitive constant will favour *exploration*.

        Args:
            swarm_best_pos: The current position of the Particle with the best
                fitness.
        """
        for ix, _d in enumerate(self.position):
            inertia = self.inertia * self.velocity[ix]
            cognitive_acc = self.cognitive * uniform(0, 1)
            cognitive_pos = cognitive_acc * (self.position_best[ix] - self.position[ix])
            social_acc = self.social * uniform(0, 1)
            social_pos = social_acc * (swarm_best_pos[ix] - self.position[ix])
            self.velocity[ix] = inertia + cognitive_pos + social_pos
            self.position[ix] = self.position[ix] + self.velocity[ix]

def generate_swarm(
    n_particles: int,
    search_space: List[Tuple[float, float]],
    fitness_func: Callable,
    inertia: float,
    cognitive: float,
    social: float,
) -> List[Particle]:
    """
    This is the initialization step of the PSO algorithm, it is called only once.

    Args:
        n_particles: More particles will slow the performance but will increase
            the likelihood of finding a global minimum
        search_space: A range, for each dimension, within which to search
        fitness_func: The function to minimize
        inertia: A constant in the range [0, 1] which determines the proportion
            by which each step's velocity will affect the change in position.
            Low values will slow the progress made at each step, and will
            potentially require more steps, but it will encourage *exploitation*.
            Higher values encourage *exploration*.
        cognitive: A constant in the range ]0, 2[, represents an individual
            particle's sense of certainty about its personal best position.
            A swarm with a high cognitive constant will favour *exploitation*.
        social: A constant in the range ]0, 2[, represents an individual
            particle's sense of certainty about the swarm's best position.
            A swarm with a high cognitive constant will favour *exploration*.

    Returns:
        A list of [Particles][toolbox.algorithms.optimization.swarm.Particle]
    """
    positions = array(
        [uniform(lo, hi, n_particles) for lo, hi in search_space]
    ).transpose()
    particles = []
    for position in positions:
        particles.append(
            Particle(
                position=position,
                search_space=search_space,
                fitness_func=fitness_func,
                inertia=inertia,
                cognitive=cognitive,
                social=social,
            )
        )
    return particles

def evaluate_swarm(swarm: List[Particle]) -> Particle:
    """
    At each iteration (step) of the PSO algorithm, the particles evaluate their
    fitness values by calling their [Particle.evaluate_fitness]
    [toolbox.algorithms.optimization.swarm.Particle.evaluate_fitness] methods.
    The particle with the best fitness determines the swarm's best position for
    that iteration.

    Args:
        A list of [Particles][toolbox.algorithms.optimization.swarm.Particle]
        belonging to the swarm.

    Returns:
        The particle with the best position.
    """
    best_particle = swarm[0]
    best_fitness = best_particle.fitness
    for particle in swarm:
        particle.evaluate_fitness()
        if particle.fitness < best_fitness:
            best_fitness = particle.fitness
            best_particle = particle
    return best_particle

def update_swarm(swarm: List[Particle]) -> Particle:
    """
    At each iteration (step) of the PSO algorithm, after discovering the swarm's
    best position with [evaluate_swarm]
    [toolbox.algorithms.optimization.swarm.evaluate_swarm], the particles update
    their positions by calling their [Particle.update]
    [toolbox.algorithms.optimization.swarm.Particle.evaluate_fitness] methods.

    Args:
        A list of [Particles][toolbox.algorithms.optimization.swarm.Particle]
        belonging to the swarm.

    Returns:
        The particle with the best position.
    """
    best_particle = evaluate_swarm(swarm)
    for particle in swarm:
        particle.update(best_particle.position)
    return best_particle

def pso(
    fitness_func: Callable,
    n_particles: int,
    search_space: List[Tuple[float, float]],
    inertia: float,
    cognitive: float,
    social: float,
    max_steps: int,
    min_delta: float = 0,
    min_delta_repeats: int = 10,
    verbose: bool = True,
) -> List[float]:
    """
    This implementation of particle-swarm optimization starts by initializing a
    number of particles uniformally randomly across the search space. The
    search space  defines the range of possible values to search for in each of
    the dimensions of the space. See [generate_swarm]
    [toolbox.algorithms.optimization.swarm.generate_swarm].

    Subsequently, at each iteration :

    1. every particle measures its fitness against the given fitness function.
    See [Particle.evaluate_fitness]
    [toolbox.algorithms.optimization.swarm.Particle.evaluate_fitness].
    2. the best position - the one for which the fitness function yields the minimal
    value - is found and broadcast to the entire swarm. Note that this is an
    implementation of the **Global topology**, in which each particle is able
    to communicate with all other particles of the swarm. There are other
    topologies which may be less prone to particles getting stuck in local
    minima. See [evaluate_swarm]
    [toolbox.algorithms.optimization.swarm.evaluate_swarm].
    3. given the swarm's best position, each particle moves towards it by a
    degree dependent on the optimization parameters (described below), adjusted
    by a random factor. See [update_swarm]
    [toolbox.algorithms.optimization.swarm.update_swarm].

    The position update (step 3 above) is dependent on the three parameters :

    - `inertia`
    - `cognitive`
    - `social`

    The three steps above are repeated until the stopping condition is met.
    Here, the stopping condition is met if either :

    - `max_steps` is reached, or
    - there is a `min_delta` or smaller change in the swarm's best position
    repeatedly for `min_delta_repeats` steps.

    Args:
        fitness_func: The function to minimize
        n_particles: More particles will slow the performance but will increase
            the likelihood of finding a global minimum
        search_space: A range, for each dimension, within which to search
        inertia: A constant in the range $[0, 1]$ which determines the proportion
            by which each step's velocity will affect the change in position.
            Low values will slow the progress made at each step, and will
            potentially require more steps, but it will encourage *exploitation*.
            Higher values encourage *exploration*.
        cognitive: A constant in the range $]0, 2[$, represents an individual
            particle's sense of certainty about its personal best position.
            A swarm with a high cognitive constant will favour *exploitation*.
        social: A constant in the range $]0, 2[$, represents an individual
            particle's sense of certainty about the swarm's best position.
            A swarm with a high cognitive constant will favour *exploration*.
        max_steps: A stopping condition.
        min_delta: In combination with `min_delta_repeats` defines a stopping
            condition based on the change in distance travelled across the search
            space between steps.
        min_delta_repeats: The number of times a value smaller than `min_delta`
            is observed before the optimization is stopped.
        verbose: Set to False to silence printing at each step.

    Returns:
        The final best position, and hopefully the global minimum.

    Examples:

        >>> def booth(x, y):
                return (x + 2*y - 7)**2 + (2*x + y - 5)**2
        >>> x, y = pso(
                fitness_func=booth,
                n_particles=1000,
                search_space=[(-10, 10), (-10, 10)],
                inertia=0.2,
                cognitive=0.4,
                social=0.6,
                max_steps=100,
                min_delta=0.00001,
                min_delta_repeats=10,
                verbose=True
            )

            1       [0.90367590, 3.07077955]        0.20555572
            2       [0.90864515, 3.07453398]        0.00000000
            3       [0.91783424, 3.14256790]        0.09377374
            4       [1.01895113, 2.98260133]        0.17020089
            5       [1.00240149, 3.05197469]        0.05203608
            6       [0.98202492, 2.98871176]        0.06888439
            7       [0.99610861, 2.99957312]        0.01907586
            8       [0.99225189, 3.00850750]        0.01124424
            9       [0.99971696, 3.00322148]        0.00939563
            10      [1.00077373, 3.00156704]        0.00204074
            11      [1.00038588, 2.99972079]        0.00187193
            12      [1.00008264, 2.99990549]        0.00026376
            13      [0.99999774, 2.99996748]        0.00009338
            14      [1.00004893, 2.99993361]        0.00004135
            15      [1.00008673, 2.99996720]        0.00006891
            16      [1.00000604, 2.99997651]        0.00006335
            17      [1.00002732, 2.99998583]        0.00002063
            18      [1.00000184, 3.00000745]        0.00002983
            19      [0.99999714, 2.99999854]        0.00000854
            20      [0.99999807, 2.99999881]        0.00000145
            21      [1.00000006, 2.99999973]        0.00000165
            22      [1.00000015, 3.00000011]        0.00000038
            23      [0.99999995, 2.99999995]        0.00000016
            24      [1.00000003, 3.00000010]        0.00000016
            25      [1.00000002, 3.00000002]        0.00000008
            26      [1.00000003, 2.99999998]        0.00000003
            27      [1.00000000, 3.00000000]        0.00000002
            28      [1.00000000, 3.00000000]        0.00000000

    """
    if n_particles < 2:
        raise ValueError("the swarm must count at least two particles")
    if max_steps < 1:
        raise ValueError("there should be at least one step")
    if min_delta < 0:
        raise ValueError("absolute delta should be non-negative (0 to reach max_steps)")

    swarm = generate_swarm(
        n_particles=n_particles,
        search_space=search_space,
        fitness_func=fitness_func,
        inertia=inertia,
        cognitive=cognitive,
        social=social,
    )
    delta = 0
    delta_count = 0
    step = 1
    best_particle = update_swarm(swarm)
    best_position = best_particle.position
    while step < max_steps:
        best_particle = update_swarm(swarm)
        delta = distance(best_position, best_particle.position)
        best_position = best_particle.position
        if verbose:
            position_str = ", ".join([f"{d:.8f}" for d in best_position])
            print(f"{step}\t[{position_str}]\t{delta:.8f}")
        if delta <= min_delta:
            delta_count += 1
            if delta_count >= min_delta_repeats:
                break
        else:
            delta_count = 0
        step += 1
    return best_position

max_stars_repo_name: inegm/toolbox

max_issues_repo_name: inegm/toolbox

max_forks_repo_name: inegm/toolbox


text:
'''
MIT License

Copyright (c) 2021 Stephen Hausler, Sourav Garg, Ming Xu, Michael Milford and Tobias Fischer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Receptive box calculations modified from tensorflow models under Apache License:

Copyright 2017 The TensorFlow Authors 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.
'''

import os
import numpy as np
import torch
from tqdm.auto import tqdm
from patchnetvlad.tools.patch_matcher import PatchMatcher

def calc_receptive_boxes(height, width):
    """Calculate receptive boxes for each feature point.
    Modified from
    https://github.com/tensorflow/models/blob/238922e98dd0e8254b5c0921b241a1f5a151782f/research/delf/delf/python/feature_extractor.py

    Args:
      height: The height of feature map.
      width: The width of feature map.
      rf: The receptive field size.
      stride: The effective stride between two adjacent feature points.
      padding: The effective padding size.

    Returns:
      rf_boxes: [N, 4] receptive boxes tensor. Here N equals to height x width.
      Each box is represented by [xmin, ymin, xmax, ymax].
    """

    rf, stride, padding = [196.0, 16.0, 90.0]  # hardcoded for vgg-16 conv5_3

    x, y = torch.meshgrid(torch.arange(0, height), torch.arange(0, width))
    coordinates = torch.reshape(torch.stack([y, x], dim=2), [-1, 2])
    # [y,x,y,x]
    point_boxes = torch.cat([coordinates, coordinates], 1)
    bias = [-padding, -padding, -padding + rf - 1, -padding + rf - 1]
    rf_boxes = stride * point_boxes + torch.FloatTensor(bias)
    return rf_boxes

def calc_keypoint_centers_from_patches(config, patch_size_h, patch_size_w, stride_h, stride_w):
    '''Calculate patch positions in image space

    Args:
        config: feature_match config data
        patch_size_h: height of patches
        patch_size_w: width of patches
        stride_h: stride in vertical direction between patches
        stride_w: stride in horizontal direction between patches

    :returns
        keypoints: patch positions back in image space for RANSAC
        indices: 2-D patch positions for rapid spatial scoring
    '''

    H = int(int(config['imageresizeH']) / 16)  # 16 is the vgg scaling from image space to feature space (conv5)
    W = int(int(config['imageresizeW']) / 16)
    padding_size = [0, 0]
    patch_size = (int(patch_size_h), int(patch_size_w))
    stride = (int(stride_h), int(stride_w))

    Hout = int((H + (2 * padding_size[0]) - patch_size[0]) / stride[0] + 1)
    Wout = int((W + (2 * padding_size[1]) - patch_size[1]) / stride[1] + 1)

    boxes = calc_receptive_boxes(H, W)

    num_regions = Hout * Wout

    k = 0
    indices = np.zeros((2, num_regions), dtype=int)
    keypoints = np.zeros((2, num_regions), dtype=int)
    # Assuming sensible values for stride here, may get errors with large stride values
    for i in range(0, Hout, stride_h):
        for j in range(0, Wout, stride_w):
            keypoints[0, k] = ((boxes[j + (i * W), 0] + boxes[(j + (patch_size[1] - 1)) + (i * W), 2]) / 2)
            keypoints[1, k] = ((boxes[j + (i * W), 1] + boxes[j + ((i + (patch_size[0] - 1)) * W), 3]) / 2)
            indices[0, k] = j
            indices[1, k] = i
            k += 1

    return keypoints, indices

def normalise_func(input_diff, num_patches, patch_weights):
    normed_diff = 0
    if len(patch_weights) != num_patches:
        raise ValueError('The number of patch weights must equal the number of patches used')
    for i in range(num_patches):
        normed_diff = normed_diff + patch_weights[i] * (
            (input_diff[:, i] - np.mean(input_diff[:, i])) / np.std(input_diff[:, i]))
    return normed_diff

def local_matcher(predictions, eval_set, input_query_local_features_prefix,
                  input_index_local_features_prefix, config, device):

    patch_sizes = [int(s) for s in config['global_params']['patch_sizes'].split(",")]
    strides = [int(s) for s in config['global_params']['strides'].split(",")]
    patch_weights = np.array(config['feature_match']['patchWeights2Use'].split(",")).astype(float)

    all_keypoints = []
    all_indices = []

    for patch_size, stride in zip(patch_sizes, strides):
        # we currently only provide support for square patches, but this can be easily modified for future works
        keypoints, indices = calc_keypoint_centers_from_patches(config['feature_match'], patch_size, patch_size, stride, stride)
        all_keypoints.append(keypoints)
        all_indices.append(indices)

    reordered_preds = []

    matcher = PatchMatcher(config['feature_match']['matcher'], patch_sizes, strides, all_keypoints,
                           all_indices)

    for q_idx, pred in enumerate(tqdm(predictions, leave=False, desc='Patch compare pred')):
        diffs = np.zeros((predictions.shape[1], len(patch_sizes)))
        image_name_query = os.path.splitext(os.path.basename(eval_set.images[eval_set.numDb + q_idx]))[0]
        qfeat = []
        for patch_size in patch_sizes:
            qfilename = input_query_local_features_prefix + '_' + 'psize{}_'.format(patch_size) + image_name_query + '.npy'
            qfeat.append(torch.transpose(torch.tensor(np.load(qfilename), device=device), 0, 1))
            # we pre-transpose here to save compute speed
        for k, candidate in enumerate(pred):
            image_name_index = os.path.splitext(os.path.basename(eval_set.images[candidate]))[0]
            dbfeat = []
            for patch_size in patch_sizes:
                dbfilename = input_index_local_features_prefix + '_' + 'psize{}_'.format(patch_size) + image_name_index + '.npy'
                dbfeat.append(torch.tensor(np.load(dbfilename), device=device))

            diffs[k, :], _, _, _ = matcher.match(qfeat, dbfeat)

        diffs = normalise_func(diffs, len(patch_sizes), patch_weights)
        cand_sorted = np.argsort(diffs)
        reordered_preds.append(pred[cand_sorted])

    return reordered_preds


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

import numpy as np
import mindspore
import mindspore.nn as nn
import mindspore.common.dtype as mstype
from mindspore.ops import operations as P
from mindspore.common.tensor import Tensor
from mindspore.common.parameter import Parameter
from model_mindspore.parallel_transformer import LayerNorm2

class Linear(nn.Cell):
    """ Linear """

    def __init__(self):
        super().__init__()
        self.matmul = nn.MatMul()
        self.t = P.Transpose()

    def construct(self, input1, weight, bias=None):
        output = self.matmul(input1, self.t(weight, (1, 0)))
        if bias is not None:
            output += bias
        return output

# for MRM
class RegionFeatureRegression(nn.Cell):
    """ RegionFeatureRegression """

    def __init__(self, config, hidden_size, feat_dim, img_linear_weight, parallel_config):
        super().__init__()
        self.dense = nn.Dense(hidden_size, hidden_size).to_float(mindspore.float16)
        self.dense.matmul.shard(((parallel_config.dp, 1), (1, 1)))
        self.dense.bias_add.shard(((parallel_config.dp, 1), (1,)))
        self.dense.weight.parallel_optimizer = False
        self.dense.bias.parallel_optimizer = False
        self.gelu = nn.GELU()
        self.gelu.gelu.shard(((parallel_config.dp, 1),))
        self.layer_norm = LayerNorm2((hidden_size,), parallel_config.dp).to_float(mindspore.float32)

        self.weight = img_linear_weight
        self.bias = Parameter(Tensor(np.zeros(feat_dim), mstype.float32), parallel_optimizer=False)
        self.add = P.TensorAdd().shard(((parallel_config.dp, 1), (1,)))
        self.matmul = P.MatMul().shard(((parallel_config.dp, 1), (1, 1)))

    def construct(self, input_):
        dense_out = self.dense(input_)
        gelu_out = self.gelu(dense_out)
        hidden = self.layer_norm(gelu_out)
        output = self.add(self.matmul(hidden.astype(mstype.float16), self.weight.astype(mstype.float16)),
                          self.bias.astype(mstype.float16))

        return output.astype(mindspore.float32)

# for MAR
class AudioFeatureRegression(nn.Cell):
    """ AudioFeatureRegression """

    def __init__(self, config, hidden_size, feat_dim, img_linear_weight, parallel_config):
        super().__init__()
        self.dense = nn.Dense(hidden_size, hidden_size).to_float(mindspore.float16)
        self.dense.matmul.shard(((parallel_config.dp, 1), (1, 1)))
        self.dense.bias_add.shard(((parallel_config.dp, 1), (1,)))
        self.dense.weight.parallel_optimizer = False
        self.dense.bias.parallel_optimizer = False
        self.gelu = nn.GELU()
        self.gelu.gelu.shard(((parallel_config.dp, 1),))
        self.layer_norm = LayerNorm2((hidden_size,), parallel_config.dp).to_float(mindspore.float32)

        self.weight = img_linear_weight
        self.bias = Parameter(Tensor(np.zeros(feat_dim), mstype.float32), parallel_optimizer=False)

        self.add = P.TensorAdd().shard(((parallel_config.dp, 1), (1,)))
        self.matmul = P.MatMul().shard(((parallel_config.dp, 1), (1, 1)))

    def construct(self, input_):
        dense_out = self.dense(input_)
        gelu_out = self.gelu(dense_out)
        hidden = self.layer_norm(gelu_out)
        output = self.add(self.matmul(hidden.astype(mstype.float16), self.weight.astype(mstype.float16)),
                          self.bias.astype(mstype.float16))
        return output.astype(mindspore.float32)

# for MRC(-kl)
class RegionClassification(nn.Cell):
    """ RegionClassification """

    def __init__(self, config, hidden_size, label_dim, parallel_config):
        super().__init__()
        self.dense_1 = nn.Dense(hidden_size, hidden_size).to_float(mindspore.float16)
        self.dense_1.matmul.shard(((parallel_config.dp, 1), (1, 1)))
        self.dense_1.bias_add.shard(((parallel_config.dp, 1), (1,)))
        self.dense_1.weight.parallel_optimizer = False
        self.dense_1.bias.parallel_optimizer = False
        self.gelu = nn.GELU()
        self.gelu.gelu.shard(((parallel_config.dp, 1),))
        self.layer_norm = LayerNorm2((hidden_size,), parallel_config.dp).to_float(mindspore.float32)
        self.dense_2 = nn.Dense(hidden_size, label_dim).to_float(mindspore.float16)
        self.dense_2.matmul.shard(((parallel_config.dp, 1), (1, 1)))
        self.dense_2.bias_add.shard(((parallel_config.dp, 1), (1,)))
        self.dense_2.weight.parallel_optimizer = False
        self.dense_2.bias.parallel_optimizer = False

    def construct(self, input_):
        dense_1_out = self.dense_1(input_)
        gelu_out = self.gelu(dense_1_out)
        layer_norm_out = self.layer_norm(gelu_out)
        output = self.dense_2(layer_norm_out)
        return output.astype(mindspore.float32)

class AudioClassification(nn.Cell):
    """ AudioClassification """

    def __init__(self, hidden_size, label_dim, parallel_config):
        super().__init__()
        self.dense_1 = nn.Dense(hidden_size, hidden_size).to_float(mindspore.float16)
        self.gelu = nn.GELU()
        self.layer_norm = nn.LayerNorm([hidden_size], epsilon=1e-12).to_float(mindspore.float32)
        self.dense_2 = nn.Dense(hidden_size, label_dim).to_float(mindspore.float16)

    def construct(self, input_):
        dense_1_out = self.dense_1(input_)
        gelu_out = self.gelu(dense_1_out)
        layer_norm_out = self.layer_norm(gelu_out)
        output = self.dense_2(layer_norm_out)
        return output.astype(mindspore.float32)

class BertOnlyMLMHead(nn.Cell):
    """ BertOnlyMLMHead """

    def __init__(self, config, bert_model_embedding_weights, parallel_config):
        super(BertOnlyMLMHead, self).__init__()
        self.predictions = BertLMPredictionHead(config,
                                                bert_model_embedding_weights, parallel_config)

    def construct(self, sequence_output):
        prediction_scores = self.predictions(sequence_output)
        return prediction_scores

class BertLMPredictionHead(nn.Cell):
    """ BertLMPredictionHead """

    def __init__(self, config, bert_model_embedding_weights, parallel_config):
        super(BertLMPredictionHead, self).__init__()
        self.transform = BertPredictionHeadTransform(config, parallel_config)

        # The output weights are the same as the input embeddings, but there is
        # an output-only bias for each token.
        self.decoder = nn.Dense(bert_model_embedding_weights.shape[1],
                                bert_model_embedding_weights.shape[0],
                                has_bias=True).to_float(mindspore.float16)
        self.decoder.weight = bert_model_embedding_weights
        self.decoder.matmul.shard(((parallel_config.dp, 1), (1, 1)))
        self.decoder.bias_add.shard(((parallel_config.dp, 1), (1,)))
        self.decoder.weight.parallel_optimizer = False
        self.decoder.bias.parallel_optimizer = False

    def construct(self, hidden_states):
        hidden_states = self.transform(hidden_states)
        hidden_states = self.decoder(hidden_states)
        return hidden_states.astype(mindspore.float32)

class BertPredictionHeadTransform(nn.Cell):
    """ BertPredictionHeadTransform """

    def __init__(self, config, parallel_config):
        super(BertPredictionHeadTransform, self).__init__()
        self.dense = nn.Dense(config.hidden_size, config.hidden_size).to_float(mindspore.float16)
        self.dense.matmul.shard(((parallel_config.dp, 1), (1, 1)))
        self.dense.bias_add.shard(((parallel_config.dp, 1), (1,)))
        self.dense.weight.parallel_optimizer = False
        self.dense.bias.parallel_optimizer = False
        if isinstance(config.hidden_act, str):
            self.transform_act_fn = nn.ReLU()
            self.transform_act_fn.relu.shard(((parallel_config.dp, 1),))
        else:
            self.transform_act_fn = nn.GELU()
            self.transform_act_fn.gelu.shard(((parallel_config.dp, 1),))
        self.LayerNorm = LayerNorm2((config.hidden_size,), parallel_config.dp).to_float(mindspore.float32)

    def construct(self, hidden_states):
        hidden_states = self.dense(hidden_states)
        hidden_states = self.transform_act_fn(hidden_states)
        hidden_states = self.LayerNorm(hidden_states)
        return hidden_states

max_stars_repo_name: mindspore-ai/models

max_issues_repo_name: mindspore-ai/models

max_forks_repo_name: mindspore-ai/models


text:
import datetime
import os
import sys
import datetime
import numpy as np
import requests
from astropy.table import Table, Column
from google.transit import gtfs_realtime_pb2

target_stop_1s= ["101S", "103S", "112S", "115S", "119S", "120S", "124S", "127S", "137S", "142S"]
target_stop_1n= ["142N", "137N", "127N", "125N", "120N", "115N", "112N", "104N", "103N", "101N"]
target_stop_2s= ["201S", "213S", "221S", "224S", "120S", "127S", "132S", "137S", "235S", "239S", "247S"]
target_stop_2n= ["247N","239N","235N","137N","132N","127N","120N","224N","221N","213N","201N"]
#path2='https://datamine-history.s3.amazonaws.com/gtfs-2014-09-17-09-36'
#path2='https://datamine-history.s3.amazonaws.com/gtfs-2014-09-17-09-36'
arry=[]
aa= False
feed = gtfs_realtime_pb2.FeedMessage()
dup = []
dupli = False
en=[]
path='https://datamine-history.s3.amazonaws.com/gtfs'
year= 2018
month= 9
duplicate= -2
day_in_month=(31,28,31,30,31,30,31,31,30,31,30,31)
for month in range(3, 4):
	for day in range (1, day_in_month[month-1]+1):
		hour = -1
		if(datetime.date(2017, month, day).weekday()>4):
			continue
		for k in range (24):
			hour+= 1
			minute= -4
			for j in range (12):
				minute+= 5
				if(month<10):
					month_str= '0'+ str(month)
				else:
					month_str= str(month)
				if(day<10):
					day_str= '0'+ str(day)
				else:
					day_str= str(day)
				if(hour<10):
					hour_str= '0'+ str(hour)
				else:
					hour_str= str(hour)
				if(minute<10):
					minute_str= '0'+ str(minute)
				else:
					minute_str= str(minute)
				tem= path+'-'+ str(year) +'-'+ month_str+'-'+day_str+'-'+hour_str+'-'+minute_str
				print(tem)
				response = requests.get(tem)
				try:
					feed.ParseFromString(response.content)
				except:
					continue
				all_num=0
				for entity in feed.entity:
					if entity.HasField('trip_update'):
						t=entity.trip_update
						tri= t.trip
						if(tri.route_id==""):
							continue
						trip_id= tri.trip_id
						num_t= -1
						dupli= False
						for i in dup:
							num_t+=1
							if(i== trip_id):
								dupli= True
								num=0
								if(len(en[num_t].stop_time_update)==0):
									break
								en_stop_ip= en[num_t].stop_time_update
								tt=t.stop_time_update
								if(len(en_stop_ip)< len(tt)):
									en[num_t]= t
									break
								while(en[num_t].stop_time_update[num].stop_id!= tt[0].stop_id):
									if(num+1 >= len(en[num_t].stop_time_update)):
										en[num_t] = t
										break
									num+=1
								for kk in tt:
									if(num< len(en[num_t].stop_time_update)-1):
										en[num_t].stop_time_update[num].departure.time= kk.departure.time
										num+=1
									elif(num== len(en[num_t].stop_time_update)-1):
										en[num_t].stop_time_update[num].arrival.time= kk.arrival.time
										num+=1
								break
						if(dupli== False):
							dup.append(trip_id)
							en.append(t)
							dupli= False
		for t in en:
			tt=t.stop_time_update
			tri= t.trip
			t_route_id= tri.route_id
			t_stop_id= tt[0].stop_id
			t_depart= tt[0].departure
			t_depart=datetime.datetime.fromtimestamp(
				int(t_depart.time)
			).strftime('%Y-%m-%d %H:%M:%S')
			year_num= int(t_depart[:4])
			month_num= int(t_depart[5:7])
			day_num= int(t_depart[8:10])
			hour_num= int(t_depart[11:13])
			if(year_num!= int(year) or month_num!= int(month) or day_num!= int(day)):
				continue
			nn= 1
			for i in tt:
				atime=i.departure.time
				if(nn == len(tt)):
					atime=i.arrival.time
				stop=i.stop_id
				atimenew=datetime.datetime.fromtimestamp(
					int(atime)
				).strftime('%Y-%m-%d %H:%M:%S')
				if(t_depart[11:13]=="23" or t_depart[8:10]!= day_str):
					break
				if((t.trip.route_id== '2' and (stop in target_stop_2n or stop in target_stop_2s)) or (t.trip.route_id== '1' and (stop in target_stop_1n or stop in target_stop_1s))):
					with open("b/"+t.trip.route_id+"/"+ str(year)+"_"+ str(month)+"_"+str(day)+"_"+stop+".txt","a") as f:
						f.write(atimenew[11:16]+"   ")
				nn+=1

reason: import numpy,from astropy


text:
"""Reionization consistency check with BK&P 2009MNRAS.397..971B"""

from __future__ import absolute_import, division, print_function

import numpy
import numpy.testing.utils as ntest
import matplotlib.pyplot as pylab

import cosmolopy.perturbation as cp
import cosmolopy.distance as cd
import cosmolopy.density as cden
import cosmolopy.reionization as cr
import cosmolopy.constants as cc
import cosmolopy.parameters as cparam

def test_tau_BKP():
    """Reionization consistency check with BK&P 2009MNRAS.397..971B

    Bagla J.~S., Kulkarni G., Padmanabhan T., 2009 (2009MNRAS.397..971B).

    Take their cannonical set of cosmological parameters and ionization
    coefficients and make sure I get the same optical depth value.

    The first figure demonstrates the calculation of the ionization
    fraction and the optical depth (not shown in the paper).

    The second figure shows dependence of the f_* f_esc,gamma values on
    tau (actually calculated the other way around, f_* f_esc,gamma ->
    tau). The plotted point (x) marks the value from the paper. The
    agreement between the calculated optical depth and the WMAP tau value
    is printed in the text output.

    """
    N_gamma = 6804.

    # f_* f_esc N_gamma
    f_ion = numpy.transpose(numpy.atleast_2d(numpy.arange(10., 200., 10.)))

    alpha_B = 1e-13 # cm^3 s^-1
    m_min = 1e8 # M_sun

    x_ionHe = 2.0

    dz = 0.1
    z = numpy.arange(20., 6. - 1.5 * dz, -1. * dz)
    #z = numpy.arange(6., 20. + 1.5 * dz, dz)

    cosmos = [cparam.WMAP5_ML(flat=True),
              cparam.WMAP5_mean(flat=True)]

    pylab.figure(figsize=(8,8))
    colors=['r', 'k']
    names = ["WMAP5 ML", "WMAP5 mean"]
    i = -1
    for cosmo in cosmos:
        i += 1

        print("\n%s" % (names[i]))
        # calculate ionized fraction, including recombinations
        x_rec, w_rec, t = cr.integrate_ion_recomb_collapse(z, f_ion,
                                                           m_min,
                                                           passed_min_mass=True,
                                                           alpha_B=alpha_B,
                                                           **cosmo)
        # calculate the optical depth in this scenario
        tau_z0 = cr.optical_depth_instant(z[-1],
                                          x_ionH=1.0,
                                          x_ionHe=2.0,
                                          **cosmo)

        tau_later = cr.integrate_optical_depth(x_rec, x_ionHe * x_rec,
                                               z, **cosmo)
        tau_0 = tau_later[:, -1] + tau_z0

        tau = tau_later

        print("tau(WMAP) = %.3f" % (cosmo['tau']))
        for j in range(len(f_ion.flat)):
            if round(f_ion[j],1) != 50.0:
                continue
            print("with f_* f_esc_gamma N_gamma = %.1f:" % (f_ion[j]))
            pylab.plot(z, x_rec[j], ls='-', color=colors[i])
            pylab.plot(z, w_rec[j], ls=':', color=colors[i])
            #pylab.plot(z, 10. * tau[j], ls='--', color=colors[i])
            pylab.plot(z, 10. * (tau_0[j] - tau[j]), ls='--', color=colors[i])
            pylab.axhline(y=10. * tau_0[j], ls='--', color=colors[i])
            print("tau(z=0)  = %.4f" % (tau_0[j]))
            print("fractional diff. = %.3g" % ((tau_0[j] - cosmo['tau']) /
                                                cosmo['tau']))
            if i==1:
                # Make sure we recover the WMAP value.
                ntest.assert_approx_equal(tau_0[j], cosmo['tau'], 2)

    pylab.ylim(0,1.01)
    pylab.xlabel("redshift z")
    pylab.ylabel(r"ionized fraction or optical depth $\tau \times 10$")

    pylab.figure(figsize=(8,8))
    pylab.plot(tau_0, f_ion / N_gamma, '-', color='k')
    pylab.plot([cosmo['tau']], [50.0 / N_gamma], 'x', color='b')
    pylab.xlim(0.06, 0.12)
    pylab.ylim(0., 0.022)
    pylab.xlabel(r'$\tau$')
    pylab.ylabel(r'$f_* f_{esc,\gamma}$')

if __name__ == "__main__":
    test_tau_BKP()
    pylab.show()

max_stars_repo_name: mjlarson/CosmoloPy

max_issues_repo_name: mjlarson/CosmoloPy

max_forks_repo_name: mjlarson/CosmoloPy


text:
from flask import Flask, request, redirect, jsonify
from .age_detector import AgeDetector as ad
import numpy as np
import cv2
import os
from .utils import encode
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

res = dict(
    roi=[], age=None, message='No file Found',
    filename='', ageConfidence=None, image=None
)

@app.route('/age-detection', methods=['GET', 'POST'])
def main():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            return jsonify(res)

        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            res['message'] = 'No selected file'
            return jsonify(res)

        res['message'] = 'Success'
        res['filename'] = file.filename

        filestr = file.read()
        npimg = np.fromstring(filestr, np.uint8)
        image = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)

        new_detection = ad(image,  os.environ['FACE_FOLDER'], os.environ['AGE_FOLDER'], 0.5)
        answer = new_detection.detect()
        res['image'] = encode(answer['image'])
        res['age'] = answer['age']
        res['ageConfidence'] = str(answer['ageConfidence'])
        res['roi'] = str(answer['roi'])

        return jsonify(res)

max_stars_repo_name: igormcsouza/online-age-detection

max_issues_repo_name: igormcsouza/online-age-detection

max_forks_repo_name: igormcsouza/online-age-detection