text:
'''

 Copyright (c) 2019 Fractus IT d.o.o. <http://fractus.io>

'''
import utils
import numpy as np
import matplotlib.pyplot as plt
import keras

from collections import namedtuple

from model import Model
from callbacks import TimeCallback

from keras.datasets import mnist
from keras.utils import to_categorical
from keras import backend as k
from sklearn.metrics import classification_report
from sklearn.preprocessing import LabelBinarizer
from keras.preprocessing.image import ImageDataGenerator
import argparse,textwrap

def main():

    parser = argparse.ArgumentParser()
    # construct the argument parse and parse the arguments
    parser = argparse.ArgumentParser(description='Please specify model.',
                                     usage='use "python %(prog)s --help" for more information',
                                     formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument('-m',
                        '--model',
                        required=True,
                        help= textwrap.dedent('''\
                        Possible MODEL:
                        conv_net
                        conv_net_dropout
                        conv_net_batch_norm
                        conv_net_l1
                        conv_net_l2
                        all -> will train all models listed above
                        '''))

    args = vars(parser.parse_args())
    model_name = args["model"]

    # list of the implemented models, second parameter defined is model convolutional or not
    existing_model_names = (('conv_net', 'True'),
                            ('conv_net_dropout', 'True'),
                            ('conv_net_batch_norm', 'True'),
                            ('conv_net_l1', 'True'),
                            ('conv_net_l2', 'True'))

    if (any(model_name in i for i in existing_model_names)) :
        # if model name is supported
        tmp_dict = dict(existing_model_names)
        modelNames = ((model_name, tmp_dict[model_name]),)
    elif (model_name == 'all'):
        modelNames = existing_model_names
    else :
        # if model name is not supported
        print("MODEL:" + model_name + ' is not supported, please speficy correct MODEL')
        exit()

    # named tuple which is holding hyper parameters values
    HyperParams = namedtuple('HyperParams', 'optimizer epochs batch_size loss')

    hyper_params = HyperParams(optimizer = 'adam', #rmsprop
                               epochs = 2,
                               batch_size = 256,
                               loss = 'categorical_crossentropy')

    reportList = []

    print("load dataset ..., the images are encoded as numpy arrays and labels are an array of digits, ranging from 0 to 9")

    # load data
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data()

    for (modelName, isConvModel)  in modelNames:

       # prepare data
        (train_data, train_labels_one_hot), (test_data, test_labels_one_hot) = utils.prepareData(train_images, train_labels, test_images, test_labels, isConvModel)

        # now data is processed and we are ready to build a network
        model = Model.getModel(modelName)

        # print model summary
        model.summary()

        # compile the model
        model.compile(optimizer = hyper_params.optimizer, loss= hyper_params.loss,  metrics=['accuracy'])

        # checkpoint callbacks
        model_checkpoint_callback = keras.callbacks.ModelCheckpoint(
                                    './outputs/' + utils.getStartTime() + '_' + modelName + '.h5',
                                    monitor = 'val_acc',
                                    verbose = 1,
                                    period = hyper_params.epochs)

        # time callback
        time_callback = TimeCallback()

        gen = ImageDataGenerator(rotation_range=8,
                                    width_shift_range=0.08,
                                    shear_range=0.3,
                                    height_shift_range=0.08,
                                    zoom_range=0.08)

        test_gen = ImageDataGenerator()

        train_generator = gen.flow(train_data, train_labels_one_hot, batch_size=64)
        test_generator = test_gen.flow(test_data, test_labels_one_hot, batch_size=64)

        history = model.fit_generator(train_generator,
                                        steps_per_epoch=60000//1000,
                                        epochs = hyper_params.epochs,
                                        verbose = 1,
                                        callbacks = [time_callback, model_checkpoint_callback],
                                        validation_data=test_generator,
                                        validation_steps=10000//1000)

        [test_loss, test_acc] = model.evaluate(test_data, test_labels_one_hot)

        print("Baseline Error: {} test_loss {} , test_acc {}  ".format((100-test_acc*100), test_loss, test_acc))

        prediction = model.predict(test_data, hyper_params.batch_size)

        le = LabelBinarizer()
        le_train_labels = le.fit_transform(train_labels)
        le_test_labels = le.transform(test_labels)

        classificationReport = classification_report(
                                    le_test_labels.argmax(axis=1),
                                    prediction.argmax(axis=1),
                                    target_names=[str(x) for x in le.classes_])

        print(classificationReport)

        reportList.append([model, modelName, history, classificationReport, hyper_params, time_callback.times])

    utils.generateReport(reportList)

if __name__ == '__main__':
    main()

max_stars_repo_name: fractus-io/keras-tutorial

max_issues_repo_name: fractus-io/keras-tutorial

max_forks_repo_name: fractus-io/keras-tutorial


text:
import sys
import numpy as np
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QRadioButton, QSizePolicy, QPushButton, QWidget, \
    QVBoxLayout, QGroupBox, QHBoxLayout, QGridLayout, QInputDialog, QLineEdit, QFileDialog
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas, \
    NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import time as time
from PyQt5.QtCore import pyqtSlot, QThread, pyqtSignal, QSize

class acqGui(QMainWindow):

    def __init__(self):
        app = QApplication(sys.argv)
        app.setStyle('Fusion')
        super(acqGui, self).__init__()
        self.title = 'threading test'

        self.initacqUI()
        sys.exit(app.exec_())

    def initacqUI(self):
        self.setWindowTitle(self.title)
        # self.setGeometry(self.left, self.top, self.width, self.height)
        control = plotControl()
        self.setCentralWidget(control)
        self.show()

class plotControl(QWidget):
    def __init__(self):
        super(plotControl, self).__init__()

        self.nstart = 1
        self.nstop = 100
        self.dn = 1
        self.resumelabel = 0

        self.initUI()

    def initUI(self):
        uilayout = QGridLayout()
        self.startbtn = QPushButton('Start Plotting', self)
        self.pausebtn = QPushButton('Pause Plotting', self)
        self.stopbtn = QPushButton('Stop Plotting', self)
        mplplt = WidgetPlot() #initialising the widget plot class
        self.plot = mplplt #naming the widgetplot class
        self.plot.setMinimumSize(QSize(600, 600))

        self.startbtn.clicked.connect(self.on_click_start)
        self.pausebtn.clicked.connect(self.on_click_pause)
        self.stopbtn.clicked.connect(self.on_click_stop)

        uilayout.addWidget(self.startbtn, 0, 0)
        uilayout.addWidget(self.pausebtn, 1, 0)
        uilayout.addWidget(self.stopbtn, 2, 0)
        uilayout.addWidget(mplplt, 0, 1, 5, 5)

        self.setLayout(uilayout)

    @pyqtSlot()
    def on_click_start(self):
        if self.resumelabel == 0:
            data = []
            self.thread = AcquisitionThread(data, self.nstart, self.nstop, self.dn)
            self.thread.start()
            self.thread.signal.connect(self.on_thread_done)
        else:
            data = self.thread.dataQ()
            self.thread = AcquisitionThread(data, self.nstart, self.nstop, self.dn)
            self.thread.start()
            self.thread.signal.connect(self.on_thread_done)
            self.resumelabel = 0

    def on_click_pause(self):
        self.nstart = self.thread.halt()
        self.resumelabel = 1

    def on_click_stop(self):
        self.thread.halt()

    def on_thread_done(self, data):
        data = np.array(data) #sets data to a global variable so we can call it in other functions
        datax = np.arange(self.nstart, self.nstart + self.dn* len(data), 1)
        self.plot.plot(datax, data) #uses the QWidget class for plotting

## threading class
class AcquisitionThread(QThread):
    signal = pyqtSignal('PyQt_PyObject')

    def __init__(self, data, nstart, nstop, dn):
        QThread.__init__(self)
        self.data = data
        self.nstart = nstart
        self.ncurrent = nstart
        self.nstop = nstop
        self.dn = dn
        self.condition = 1

    def run(self):
        n_list = np.arange(self.nstart, self.nstop, self.dn)
        for n in n_list:
            self.ncurrent = n
            while self.condition == 1:
                self.data.append(np.random.rand())
                time.sleep(1)
                self.signal.emit(self.data)
            if self.condition != 1:
                break

    def halt(self):
        self.condition = 0
        return self.ncurrent

    def dataQ(self):
        return self.data

## plotting class
class PlotCanvas(FigureCanvas): #this creates a matplotlib canvas and defines some plotting aspects

    def __init__(self, parent=None):
        fig = Figure()
        self.axes = fig.add_subplot(111)
        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def plot(self, x, data):
        self.axes.plot(x, data, 'b.')
        self.axes.set_title('Title')
        self.draw()

    def plotfit(self, x, fit): #this is here to make sure the plot doesn't clear when trying to plot a fitting function
        # on top of the data
        self.axes.plot(x, fit, 'bo')
        self.draw()

class WidgetPlot(QWidget): #this converts the matplotlib canvas into a qt5 widget so we can implement it in the qt
    # framework laid out above
    def __init__(self, *args, **kwargs):
        QWidget.__init__(self, *args, **kwargs)
        self.setLayout(QVBoxLayout())
        self.canvas = PlotCanvas(self)
        self.toolbar = NavigationToolbar(self.canvas, self)
        self.layout().addWidget(self.toolbar)
        self.layout().addWidget(self.canvas)

    def plot(self, x, data):
        self.canvas.axes.clear() #it is important to clear out the plot first or everything just gets plotted on top of
        # each other and it becomes useless
        self.canvas.plot(x, data)

    def plotfit(self, x, fit):
        self.canvas.plotfit(x, fit)

acqGui()

ext: py

max_stars_repo_name: normanav/CurroODMRGui

max_issues_repo_name: normanav/CurroODMRGui

max_forks_repo_name: normanav/CurroODMRGui

reason: import numpy


text:
from scipy import ndimage
from scipy import misc
import matplotlib.pyplot as plt
import numpy as np

class Pixel(object):
    def __init__(self, attrs):
        self.attrs = attrs

    def distance(self, other):
        return np.linalg.norm(self.attrs - other.attrs)

def init_means(pixels, k):
    return np.random.choice(pixels, k, replace=False)

def cluster_mean(cluster):
    if len(cluster) == 0:
        return None
    return Pixel(np.mean([pixel.attrs for pixel in cluster], axis=0))

# Returns the k means of the provided image as Pixels
def perform_k_means(image, k, num_it):
    pixels = []

    for row in image:
        for pixel in row:
            pixels.append(Pixel(pixel))

    means = init_means(pixels, k)

    for it in xrange(num_it):
        print 'Iteration:', it
        clusters = []
        for _ in xrange(k):
            clusters.append([])

        for pixel in pixels:
            min_cluster = np.argmin([pixel.distance(mean) for mean in means])
            clusters[min_cluster].append(pixel)

        means = [cluster_mean(cluster) for cluster in clusters]

    return means

def main():
    # Note: using small scale and large k can lead to errors
    image_name = raw_input("Image name: ")
    scale = max(float(raw_input("Rescale amount (0.05 to 1.0): ")), 0.05)
    k = int(raw_input("K: "))
    num_it = int(raw_input("Number of iterations: "))

    original_image = ndimage.imread(image_name)
    scaled_image = misc.imresize(original_image, scale)

    means = perform_k_means(scaled_image, k, num_it)

    # Remap the original image pixels to the closest mean pixels
    w, h, _ = original_image.shape
    for y in xrange(h):
        for x in xrange(w):
            min_mean_index = np.argmin([np.linalg.norm(original_image[x][y] - mean.attrs) for mean in means])
            original_image[x][y] = means[min_mean_index].attrs

    plt.imshow(original_image)
    plt.show()

    to_save = raw_input("Save result as output.png (y/n)? ")

    if to_save == 'y':
        misc.imsave('output.png', original_image)

if __name__ == "__main__":
    main()

max_stars_repo_name: Daniel-Norman/ml-examples

max_issues_repo_name: Daniel-Norman/ml-examples

max_forks_repo_name: Daniel-Norman/ml-examples

reason: import numpy,from scipy


text:
#!/usr/bin/env python

import numpy as np
import random

def softmax(x):
    """
    Compute the softmax function for each row of the input x.

    You should also make sure that your code works for one
    dimensional inputs (treat the vector as a row), you might find
    it helpful for your later problems.
    """

    if len(x.shape) > 1:
        tmp = np.max(x, axis = 1)
        x -= tmp.reshape((x.shape[0], 1))
        x = np.exp(x)
        tmp = np.sum(x, axis = 1)
        x /= tmp.reshape((x.shape[0], 1))
    else:
        tmp = np.max(x)
        x -= tmp
        x = np.exp(x)
        tmp = np.sum(x)
        x /= tmp

    return x

def test_softmax_basic():
    """
    Some simple tests to get you started.
    Warning: these are not exhaustive.
    """
    print "Running basic tests..."
    test1 = softmax(np.array([1,2]))
    print test1
    assert np.amax(np.fabs(test1 - np.array(
        [0.26894142,  0.73105858]))) <= 1e-6

    test2 = softmax(np.array([[1001,1002],[3,4]]))
    print test2

    assert np.amax(np.fabs(test2 - np.array(
        [[0.26894142, 0.73105858], [0.26894142, 0.73105858]]))) <= 1e-6

    test3 = softmax(np.array([[-1001,-1002]]))
    print test3
    assert np.amax(np.fabs(test3 - np.array(
        [0.73105858, 0.26894142]))) <= 1e-6

    print "You should verify these results!\n"

def test_softmax():
    """
    Use this space to test your softmax implementation by running:
        python q1_softmax.py
    """
    print "Running your tests..."
    test_softmax_basic()

if __name__ == "__main__":
    test_softmax_basic()
    test_softmax()


text:
"""Test field-of-view computations."""

import numpy as np
from pysocialforce.fieldofview import FieldOfView

def test_w():
    assert FieldOfView()(
        np.array([[1.0, 0.0], [-1.0, 0.0],]),
        np.array([[[0.0, 0.0], [1.0, 1.0],], [[-1.0, 1.0], [0.0, 0.0],]]),
    ).tolist() == [[0, 1], [1, 0],]

max_stars_repo_name: Femme-js/PySocialForceJ

max_issues_repo_name: Femme-js/PySocialForceJ

max_forks_repo_name: Femme-js/PySocialForceJ


text:
import math
import torch
from torch.autograd import Variable
from cupy.cuda import function
from pynvrtc.compiler import Program
from collections import namedtuple

###

kernel = '''
extern "C"
__global__ void recurrent_reverse_forget_mult(float *dst, const float *f, const float *x, int SEQ, int BATCH, int HIDDEN)
{
  /*
  Note: destination is assumed to be one timestep longer than f or x where dst[SEQ] = h_{-1}
  This means dst array has a separate index than that of f or x
  */
  int hid = blockIdx.x * blockDim.x + threadIdx.x;
  int bid = blockIdx.y * blockDim.y + threadIdx.y;
  if(hid >= HIDDEN || bid >= BATCH)
     return;
  //
  for (int ts = SEQ - 1; ts >= 0; ts--) {
     // Good sanity check for debugging - only perform additions to a zeroed chunk of memory
     // Addition seems atomic or near atomic - you should get incorrect answers if doubling up via threads
     // Note: the index i needs to be offset by one as f[0] (f_t) is used for dst[1] (h_t) etc

     // To move timesteps, we step HIDDEN * BATCH
     // To move batches, we move HIDDEN
     // To move neurons, we move +- 1
     // Note: dst[dst_i] = ts * 100 + bid * 10 + hid; is useful for debugging

     int i           = (ts - 0) * HIDDEN * BATCH + bid * HIDDEN + hid;
     int dst_i       = (ts - 0) * HIDDEN * BATCH + bid * HIDDEN + hid;
     int dst_iplus1  = (ts + 1) * HIDDEN * BATCH + bid * HIDDEN + hid;
     dst[dst_i]      = f[i] * x[i];
     dst[dst_i]      += (1 - f[i]) * dst[dst_iplus1];
  }
}

extern "C"
__global__ void bwd_recurrent_reverse_forget_mult(const float *h, const float *f, const float *x, const float *gh, float *gf, float *gx, float *ghinit, int SEQ, int BATCH, int HIDDEN)
{
  /*
  Note: h is assumed to be one timestep longer than f, x, gf, gx, or gh where dst[SEQ] = h_{-1}
  This means dst array has a separate index than that of f or x
  */
  int hid = blockIdx.x * blockDim.x + threadIdx.x;
  int bid = blockIdx.y * blockDim.y + threadIdx.y;
  if(hid >= HIDDEN || bid >= BATCH)
     return;
  //
  double running_f = 0;
  for (int ts = 1; ts < SEQ + 1; ts++) {
  //for (int ts = SEQ; ts > 0; ts--) {
     int i           = (ts - 1) * HIDDEN * BATCH + bid * HIDDEN + hid;
     int dst_i       = (ts - 0) * HIDDEN * BATCH + bid * HIDDEN + hid;
     int dst_iminus1 = (ts - 1) * HIDDEN * BATCH + bid * HIDDEN + hid;
     int dst_iplus1  = (ts + 1) * HIDDEN * BATCH + bid * HIDDEN + hid;
     //
     running_f       += gh[dst_iminus1];
     // Gradient of X
     gx[i]           = f[i] * running_f;
     // Gradient of F
     gf[i]           = (x[i] - h[dst_i]) * running_f;
     //
     // The line below is likely more numerically stable than (1 - f[i]) * running_f;
     running_f       = running_f - f[i] * running_f;
  }
  ghinit[bid * HIDDEN + hid] = running_f;
}
'''

###

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

    def forward(self, f, x, hidden_init=None):
        result = []
        ###
        # Reverse the forget and inputs so we traverse from end to beginning
        forgets = f.split(1, dim=0)[::-1]
        inputs = (f * x).split(1, dim=0)[::-1]
        prev_h = hidden_init
        for i, h in enumerate(inputs):
            h = h.squeeze()
            if prev_h is not None: h = h + (1 - forgets[i]) * prev_h
            result.append(h)
            prev_h = h
        ###
        # Reverse the results as it's currently [h_t, ... h_0]
        result = result[::-1]
        # Now it's [h_0, ..., h_t]
        ###
        return torch.cat(result, dim=0)

class GPUReverseForgetMult(torch.autograd.Function):
    configured_gpus = {}
    ptx = None
    def __init__(self):
        super(GPUReverseForgetMult, self).__init__()

    def compile(self):
        if self.ptx is None:
            program = Program(kernel.encode(), 'recurrent_forget_mult.cu'.encode())
            GPUReverseForgetMult.ptx = program.compile()

        if torch.cuda.current_device() not in GPUReverseForgetMult.configured_gpus:
            m = function.Module()
            m.load(bytes(self.ptx.encode()))

            self.forget_mult = m.get_function('recurrent_reverse_forget_mult')
            self.bwd_forget_mult = m.get_function('bwd_recurrent_reverse_forget_mult')

            Stream = namedtuple('Stream', ['ptr'])
            self.stream = Stream(ptr=torch.cuda.current_stream().cuda_stream)

            GPUReverseForgetMult.configured_gpus[torch.cuda.current_device()] = (self.forget_mult, self.bwd_forget_mult, self.stream)

        self.forget_mult, self.bwd_forget_mult, self.stream = GPUReverseForgetMult.configured_gpus[torch.cuda.current_device()]

    def forward(self, f, x, hidden_init=None):
        self.compile()
        seq_size, batch_size, hidden_size = f.size()
        result = f.new(seq_size + 1, batch_size, hidden_size).zero_()
        if hidden_init is not None: result[seq_size, :, :] = hidden_init
        ###
        grid = (math.ceil(hidden_size / 256), math.ceil(batch_size / 4))
        self.forget_mult(grid=grid, block=(256, 4), args=[result.data_ptr(), f.data_ptr(), x.data_ptr(), seq_size, batch_size, hidden_size], stream=self.stream)
        self.save_for_backward(f, x, hidden_init)
        self.result = result
        return result[:-1, :, :]

    def backward(self, grad_h):
        self.compile()
        f, x, hidden_init = self.saved_tensors
        h = self.result
        ###
        seq_size, batch_size, hidden_size = f.size()
        grad_f = f.new(*f.size()).zero_()
        grad_x = f.new(*f.size()).zero_()
        grad_h_init = f.new(batch_size, hidden_size).zero_()
        ###
        grid = (math.ceil(hidden_size / 256), math.ceil(batch_size / 4))
        self.bwd_forget_mult(grid=grid, block=(256, 4), args=[h.data_ptr(), f.data_ptr(), x.data_ptr(), grad_h.data_ptr(), grad_f.data_ptr(), grad_x.data_ptr(), grad_h_init.data_ptr(), seq_size, batch_size, hidden_size], stream=self.stream)
        ###
        if hidden_init is not None:
            return grad_f, grad_x, grad_h_init
        return grad_f, grad_x

class ReverseForgetMult(torch.nn.Module):
    r"""ForgetMult computes a simple recurrent equation:
    h_t = f_t * x_t + (1 - f_t) * h_{t-1}

    This equation is equivalent to dynamic weighted averaging.

    Inputs: X, hidden
        - X (seq_len, batch, input_size): tensor containing the features of the input sequence.
        - F (seq_len, batch, input_size): tensor containing the forget gate values, assumed in range [0, 1].
        - hidden_init (batch, input_size): tensor containing the initial hidden state for the recurrence (h_{t-1}).
        - use_cuda: If True, use the fast element-wise CUDA kernel for recurrence. If False, uses naive for loop. Default: True.
    """

    def __init__(self):
        super(ReverseForgetMult, self).__init__()

    def forward(self, f, x, hidden_init=None, use_cuda=True):
        # Use CUDA by default unless it's available
        use_cuda = use_cuda and torch.cuda.is_available()
        # Ensure the user is aware when ForgetMult is not GPU version as it's far faster
        if use_cuda: assert f.is_cuda and x.is_cuda, 'GPU ReverseForgetMult with fast element-wise CUDA kernel requested but tensors not on GPU'
        ###
        # Avoiding 'RuntimeError: expected a Variable argument, but got NoneType' when hidden_init is None
        if hidden_init is None: return GPUReverseForgetMult()(f, x) if use_cuda else CPUReverseForgetMult()(f, x)
        return GPUReverseForgetMult()(f, x, hidden_init) if use_cuda else CPUReverseForgetMult()(f, x, hidden_init)

###

if __name__ == '__main__':
    seq, batch, hidden = 35, 20, 650
    # Larger input (batch * seq * hidden) results in excessive memory for gradient check
    seq, batch, hidden = 10, 7, 19
    #seq, batch, hidden = 3, 7, 19
    #seq, batch, hidden = 1, 7, 19
    #seq, batch, hidden = 5, 1, 1
    a      = Variable(torch.rand(seq, batch, hidden).cuda(), requires_grad=True)
    forget = Variable(torch.rand(seq, batch, hidden).cuda(), requires_grad=True)
    last_h = Variable(torch.zeros(batch, hidden).cuda(), requires_grad=True)
    #last_h = Variable(torch.ones(batch, hidden).cuda() * 10, requires_grad=True)
    #a      = Variable(torch.ones(seq, batch, hidden).cuda(), requires_grad=True)
    #forget = Variable(torch.ones(seq, batch, hidden).cuda() * 0.5, requires_grad=True)

    print('CPU forget mult')
    print('=-=-' * 5)

    resulta = ReverseForgetMult()(forget, a, last_h, use_cuda=False)
    loss = resulta.pow(2).sum()
    loss.backward()
    #print(resulta.data)
    #print(forget.grad.data)

    #print(resulta.data)
    print('Result =', loss.data[0])
    print('X grad =', a.grad.mean().data[0])
    print('Forget grad =', forget.grad.mean().data[0])
    print('Last H grad =', last_h.grad.mean().data[0])

    x_grad_copy = a.grad.clone()

    print()
    print('CUDA forget mult')
    print('=-=-' * 5)

    a.grad.data *= 0
    forget.grad.data *= 0
    last_h.grad.data *= 0

    resultb = ReverseForgetMult()(forget, a, last_h, use_cuda=True)
    loss = resultb.pow(2).sum()
    loss.backward()
    #print(resultb.data)
    #print(forget.grad.data)

    print('Result =', loss.data[0])
    print('X grad =', a.grad.mean().data[0])
    print('Forget grad =', forget.grad.mean().data[0])
    print('Last H grad =', last_h.grad.mean().data[0])

    ###

    print()
    print('=-=-' * 5)
    print('(Xgrad - Xgrad).sum() =', (x_grad_copy - a.grad).sum().data[0])
    print('Residual error for result')
    print('=-=-' * 5)
    residual = (resulta - resultb)
    print(residual.abs().sum().data[0])

    # Had to loosen gradient checking, potentially due to general floating point badness?
    from torch.autograd import gradcheck
    inputs = [forget, a, last_h]
    test = gradcheck(ReverseForgetMult(), inputs, eps=1e-4, atol=1e-2)
    print(test)

max_stars_repo_name: Smerity/pytorch-qrnn

max_issues_repo_name: Smerity/pytorch-qrnn

max_forks_repo_name: Smerity/pytorch-qrnn

reason: from cupy


text:
#
# Copyright 2013 Quantopian, Inc.
#
# 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 functools
import math

import numpy as np
import pandas as pd
import talib
import copy

from six import iteritems

from zipline.transforms import BatchTransform

def zipline_wrapper(talib_fn, key_map, data):
    # get required TA-Lib input names
    if 'price' in talib_fn.input_names:
        req_inputs = [talib_fn.input_names['price']]
    elif 'prices' in talib_fn.input_names:
        req_inputs = talib_fn.input_names['prices']
    else:
        req_inputs = []

    # If there are multiple output names then the results are named,
    # if there is only one output name, it usually 'real' is best represented
    # by a float.
    # Use a DataFrame to map sid to named values, and a Series map sid
    # to floats.
    if len(talib_fn.output_names) > 1:
        all_results = pd.DataFrame(index=talib_fn.output_names,
                                   columns=data.minor_axis)
    else:
        all_results = pd.Series(index=data.minor_axis)

    for sid in data.minor_axis:
        # build talib_data from zipline data
        talib_data = dict()
        for talib_key, zipline_key in iteritems(key_map):
            # if zipline_key is found, add it to talib_data
            if zipline_key in data:
                values = data[zipline_key][sid].values
                # Do not include sids that have only nans, passing only nans
                # is incompatible with many of the underlying TALib functions.
                if pd.isnull(values).all():
                    break
                else:
                    talib_data[talib_key] = data[zipline_key][sid].values
            # if zipline_key is not found and not required, add zeros
            elif talib_key not in req_inputs:
                talib_data[talib_key] = np.zeros(data.shape[1])
            # if zipline key is not found and required, raise error
            else:
                raise KeyError(
                    'Tried to set required TA-Lib data with key '
                    '\'{0}\' but no Zipline data is available under '
                    'expected key \'{1}\'.'.format(
                        talib_key, zipline_key))

        # call talib
        if talib_data:
            talib_result = talib_fn(talib_data)

            # keep only the most recent result
            if isinstance(talib_result, (list, tuple)):
                sid_result = tuple([r[-1] for r in talib_result])
            else:
                sid_result = talib_result[-1]

            all_results[sid] = sid_result

    return all_results

def make_transform(talib_fn, name):
    """
    A factory for BatchTransforms based on TALIB abstract functions.
    """
    # make class docstring
    header = '\n#---- TA-Lib docs\n\n'
    talib_docs = getattr(talib, talib_fn.info['name']).__doc__
    divider1 = '\n#---- Default mapping (TA-Lib : Zipline)\n\n'
    mappings = '\n'.join('        {0} : {1}'.format(k, v)
                         for k, v in talib_fn.input_names.items())
    divider2 = '\n\n#---- Zipline docs\n'
    help_str = header + talib_docs + divider1 + mappings + divider2

    class TALibTransform(BatchTransform):
        __doc__ = help_str + """
        TA-Lib keyword arguments must be passed at initialization. For
        example, to construct a moving average with timeperiod of 5, pass
        "timeperiod=5" during initialization.

        All abstract TA-Lib functions accept a data dictionary containing
        'open', 'high', 'low', 'close', and 'volume' keys, even if they do
        not require those keys to run. For example, talib.MA (moving
        average) is always computed using the data under the 'close'
        key. By default, Zipline constructs this data dictionary with the
        appropriate sid data, but users may overwrite this by passing
        mappings as keyword arguments. For example, to compute the moving
        average of the sid's high, provide "close = 'high'" and Zipline's
        'high' data will be used as TA-Lib's 'close' data. Similarly, if a
        user had a data column named 'Oil', they could compute its moving
        average by passing "close='Oil'".

        **Example**

        A moving average of a data column called 'Oil' with timeperiod 5,
            talib.transforms.ta.MA(close='Oil', timeperiod=5)

        The user could find the default arguments and mappings by calling:
            help(zipline.transforms.ta.MA)

        **Arguments**

        open   : string, default 'open'
        high   : string, default 'high'
        low    : string, default 'low'
        close  : string, default 'price'
        volume : string, default 'volume'

        refresh_period : int, default 0
            The refresh_period of the BatchTransform determines the number
            of iterations that pass before the BatchTransform updates its
            internal data.

        \*\*kwargs : any arguments to be passed to the TA-Lib function.
        """

        def __init__(self,
                     close='price',
                     open='open',
                     high='high',
                     low='low',
                     volume='volume',
                     refresh_period=0,
                     bars='daily',
                     **kwargs):

            key_map = {'high': high,
                       'low': low,
                       'open': open,
                       'volume': volume,
                       'close': close}

            self.call_kwargs = kwargs

            # Make deepcopy of talib abstract function.
            # This is necessary because talib abstract functions remember
            # state, including parameters, and we need to set the parameters
            # in order to compute the lookback period that will determine the
            # BatchTransform window_length. TALIB has no way to restore default
            # parameters, so the deepcopy lets us change this function's
            # parameters without affecting other TALibTransforms of the same
            # function.
            self.talib_fn = copy.deepcopy(talib_fn)

            # set the parameters
            for param in self.talib_fn.get_parameters().keys():
                if param in kwargs:
                    self.talib_fn.set_parameters({param: kwargs[param]})

            # get the lookback
            self.lookback = self.talib_fn.lookback

            self.bars = bars
            if bars == 'daily':
                lookback = self.lookback + 1
            elif bars == 'minute':
                lookback = int(math.ceil(self.lookback / (6.5 * 60)))

            # Ensure that window_length is at least 1 day's worth of data.
            window_length = max(lookback, 1)

            transform_func = functools.partial(
                zipline_wrapper, self.talib_fn, key_map)

            super(TALibTransform, self).__init__(
                func=transform_func,
                refresh_period=refresh_period,
                window_length=window_length,
                compute_only_full=False,
                bars=bars)

        def __repr__(self):
            return 'Zipline BatchTransform: {0}'.format(
                self.talib_fn.info['name'])

    TALibTransform.__name__ = name
    # return class
    return TALibTransform

# add all TA-Lib functions to locals
for name in talib.abstract.__FUNCTION_NAMES:
    fn = getattr(talib.abstract, name)
    locals()[name] = make_transform(fn, name)

max_stars_repo_name: lv-cha/zipline-chinese


text:
import argparse
import cv2
import numpy as np

import torch.nn.functional as F
from torch.utils.data import DataLoader
import torch.nn as nn
import torch
import matplotlib.pyplot as plt

def select_device(device='', apex=False, batch_size=None):
    # device = 'cpu' or '0' or '0,1,2,3'
    cpu_request = device.lower() == 'cpu'
    if device and not cpu_request:  # if device requested other than 'cpu'
        os.environ['CUDA_VISIBLE_DEVICES'] = device  # set environment variable
        assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device  # check availablity

    cuda = False if cpu_request else torch.cuda.is_available()
    if cuda:
        c = 1024 ** 2  # bytes to MB
        ng = torch.cuda.device_count()
        if ng > 1 and batch_size:  # check that batch_size is compatible with device_count
            assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng)
        x = [torch.cuda.get_device_properties(i) for i in range(ng)]
        s = 'Using CUDA ' + ('Apex ' if apex else '')  # apex for mixed precision https://github.com/NVIDIA/apex
        for i in range(0, ng):
            if i == 1:
                s = ' ' * len(s)
            print("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" %
                  (s, i, x[i].name, x[i].total_memory / c))
    else:
        print('Using CPU')

    print('')  # skip a line
    return torch.device('cuda:0' if cuda else 'cpu')

# 改动了权重数据的量化
def uniform_quantize(k):
  class qfn(torch.autograd.Function):

    @staticmethod
    def forward(ctx, input):
      if k == 32:
        out = input
      elif k == 1:
        out = torch.sign(input)
      else:
        n = float(2 ** k  - 1)
        out = torch.round(input * n) / n
      return out

    @staticmethod
    def backward(ctx, grad_output):
      grad_input = grad_output.clone()
      return grad_input

  return qfn().apply

class weight_quantize_fn(nn.Module):
  def __init__(self, w_bit):
    super(weight_quantize_fn, self).__init__()
    assert w_bit <= 8 or w_bit == 32
    self.w_bit = w_bit
    # 符号位 占一位
    self.uniform_q = uniform_quantize(k=w_bit - 1)

  def forward(self, x):
    if self.w_bit == 32:
      weight = torch.tanh(x)
      weight_q = weight / torch.max(torch.abs(weight))
    elif self.w_bit == 1:
      E = torch.mean(torch.abs(x)).detach()
      weight_q = (self.uniform_q(x / E) + 1) / 2 * E
    else:
      weight = torch.tanh(x)
      weight = weight / torch.max(torch.abs(weight))
      # 想量化到带符号的 k bit
      weight_q = self.uniform_q(weight)
    return weight_q

class activation_quantize_fn(nn.Module):
  def __init__(self, a_bit):
    super(activation_quantize_fn, self).__init__()
    assert a_bit <= 8 or a_bit == 32
    self.a_bit = a_bit
    self.uniform_q = uniform_quantize(k=a_bit)

  def forward(self, x):
    if self.a_bit == 32:
      activation_q = torch.clamp(x, 0, 6)
    else:
      activation_q = self.uniform_q(torch.clamp(x, 0, 1))
      # print(np.unique(activation_q.detach().numpy()))
    return activation_q

def conv2d_Q_fn(w_bit):
  class Conv2d_Q(nn.Conv2d):
    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, groups=1, bias=True):
      super(Conv2d_Q, self).__init__(in_channels, out_channels, kernel_size, stride,
                                     padding, dilation, groups, bias)
      self.w_bit = w_bit
      self.quantize_fn = weight_quantize_fn(w_bit=w_bit)

    def forward(self, input, order=None):
      weight_q = self.quantize_fn(self.weight)
      return F.conv2d(input, weight_q, self.bias, self.stride,
                      self.padding, self.dilation, self.groups)

  return Conv2d_Q

def create_grids(self, img_size=416, ng=(13, 13), device='cpu', type=torch.float32):
    nx, ny = ng  # x and y grid size
    self.img_size = max(img_size)
    self.stride = self.img_size / max(ng)

    # build xy offsets
    yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
    self.grid_xy = torch.stack((xv, yv), 2).to(device).type(type).view((1, 1, ny, nx, 2))

    # build wh gains
    self.anchor_vec = self.anchors.to(device) / self.stride
    self.anchor_wh = self.anchor_vec.view(1, self.na, 1, 1, 2).to(device).type(type)
    self.ng = torch.Tensor(ng).to(device)
    self.nx = nx
    self.ny = ny

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

        self.anchors = torch.Tensor(anchors)
        self.na = len(anchors)  # number of anchors (3)
        self.no = 6  # number of outputs
        self.nx = 0  # initialize number of x gridpoints
        self.ny = 0  # initialize number of y gridpoints
    def forward(self, p, img_size):

        bs, _, ny, nx = p.shape  # bs, 255, 13, 13
        if (self.nx, self.ny) != (nx, ny):
            create_grids(self, img_size, (nx, ny), p.device, p.dtype)

        # p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85)  # (bs, anchors, grid, grid, classes + xywh)
        p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous()  # prediction

        if self.training:
            return p

        else:  # inference
            # s = 1.5  # scale_xy  (pxy = pxy * s - (s - 1) / 2)
            io = p.clone()  # inference output
            io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid_xy  # xy
            io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh  # wh yolo method
            # io[..., 2:4] = ((torch.sigmoid(io[..., 2:4]) * 2) ** 3) * self.anchor_wh  # wh power method
            io[..., :4] *= self.stride  # 原始像素尺度

            torch.sigmoid_(io[..., 4:])

            return io.view(bs, -1, self.no), p

class TestNetQua(nn.Module):
    def __init__(self):
        super(TestNetQua, self).__init__()
        W_BIT = 8
        A_BIT = 8
        conv2d_q = conv2d_Q_fn(W_BIT)
        # act_q = activation_quantize_fn(4)

        self.layers = nn.Sequential(
            conv2d_q(3, 16, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(16),
            activation_quantize_fn(A_BIT),
            nn.MaxPool2d(2, stride=2),

            conv2d_q(16, 32, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(32),
            activation_quantize_fn(A_BIT),

            nn.MaxPool2d(2, stride=2),

            conv2d_q(32, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            activation_quantize_fn(A_BIT),

            nn.MaxPool2d(2, stride=2),

            conv2d_q(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            activation_quantize_fn(A_BIT),

            nn.MaxPool2d(2, stride=2),

            conv2d_q(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            activation_quantize_fn(A_BIT),

            conv2d_q(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            activation_quantize_fn(A_BIT),

            conv2d_q(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            activation_quantize_fn(A_BIT),

            conv2d_q(64, 64, kernel_size=3, stride=1, padding=1, bias=False),
            nn.BatchNorm2d(64),
            activation_quantize_fn(A_BIT),

            # nn.Conv2d(256, 18, kernel_size=1, stride=1, padding=0)
            conv2d_q(64, 36, kernel_size=1, stride=1, padding=0)

        )
        self.yololayer = YOLOLayer([[20,20], [20,20], [20,20], [20,20], [20,20], [20,20]])
        self.yolo_layers = [self.yololayer]

    def forward(self, x):
        img_size = x.shape[-2:]
        yolo_out, out = [], []

        x = self.layers(x)
        x = self.yololayer(x, img_size)

        yolo_out.append(x)

        if self.training:  # train
            return yolo_out
        else:  # test
            io, p = zip(*yolo_out)  # inference output, training output
            return torch.cat(io, 1), p
        return x

def inference(weights=None,
         batch_size=16,
         img_size=416,
         model=None,
         path = None):
    # Initialize/load model and set device
    if model is None:
        device = select_device(opt.device, batch_size=batch_size)

        # Initialize model
        model = TestNetQua().to(device)

        model.nc = 1
        model.arc = 'default'

        if weights.endswith('.pt'):  # pytorch format
            model.load_state_dict(torch.load(weights, map_location=device)['model'])

        if torch.cuda.device_count() > 1:
            model = nn.DataParallel(model)

    # load image
    img = cv2.imread(path)  # BGR
    assert img is not None, 'Image Not Found ' + path
    h0, w0 = img.shape[:2]  # orig hw

    fig = plt.figure()
    plt.subplot()

    interp = cv2.INTER_LINEAR  # LINEAR for training, AREA for testing
    img = cv2.resize(img, (img_size, img_size // 2), interpolation=interp)
    plt.imshow(img)
    img = np.expand_dims(np.transpose(img,[2,0,1]),axis=0).copy()
    img= torch.from_numpy(img)

    model.eval()

    with torch.no_grad():
        img = img.to(device).float()/255.0 # uint8 to float32, 0 - 255 to 0.0 - 1.0
        # run the model
        inference_out, training_out = model(img)
        inference_out = inference_out.view(inference_out.shape[0], 6, -1)
        print(inference_out.shape)
        inference_out_t = torch.zeros_like(inference_out[:, 0, :])
        for i in range(inference_out.shape[1]):
          inference_out_t += inference_out[:, i, :]
        inference_out_t = inference_out_t.view(inference_out_t.shape[0], -1, 6) / 6
        print(inference_out_t.shape)
        FloatTensor = torch.cuda.FloatTensor if inference_out_t.is_cuda else torch.FloatTensor
        n = inference_out_t.size(0)
        p_boxes = FloatTensor(n, 4)
        pred_boxes = inference_out_t[...,:4]
        pred_conf = inference_out_t[...,4]
        for i in range(n):
            _, index = pred_conf[i].max(0) # 返回每一列最大值组成的数据
            p_boxes[i] = pred_boxes[i][index]
        print(p_boxes.shape)
        print(p_boxes)
        img = img.cpu().numpy()
        p_boxes = p_boxes.cpu().numpy()
        print(p_boxes)
        bs, channel, h, w = img.shape
        # Convert bounding box format from [x, y, w, h] to [x1, y1, x2, y2]
        y = torch.zeros_like(p_boxes) if isinstance(p_boxes, torch.Tensor) else np.zeros_like(p_boxes)
        y[:, 0] = p_boxes[:, 0] - p_boxes[:, 2] / 2
        y[:, 1] = p_boxes[:, 1] - p_boxes[:, 3] / 2
        y[:, 2] = p_boxes[:, 0] + p_boxes[:, 2] / 2
        y[:, 3] = p_boxes[:, 1] + p_boxes[:, 3] / 2
        y=y.T
        print(y)

        plt.plot(y[[0,2,2,0,0]],y[[1,1,3,3,1]],'.-')
        plt.axis('off')
        fig.savefig("test.png")
        plt.close()
    return

if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog='test.py')
    parser.add_argument('--weights', type=str, default='weights/test_best.pt', help='weights path')
    parser.add_argument('--batch-size', type=int, default=8, help='size of each image batch')
    parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
    parser.add_argument('--device', default='', help='device id (i.e. 0 or 0,1) or cpu')
    parser.add_argument('--path',type=str,default= "../data/data_test/boat1/000001.jpg")
    opt = parser.parse_args()
    print(opt)

    # Test
    inference(opt.weights,
            opt.batch_size,
            opt.img_size,
            path = opt.path)

max_stars_repo_name: sjtu-tcloud/Tiny-OFA

max_issues_repo_name: sjtu-tcloud/Tiny-OFA

max_forks_repo_name: sjtu-tcloud/Tiny-OFA


text:
from typing import Dict, List
import torch
import cv2
import numpy as np
from torchvision import models
from models.losses import JointsMSELoss
# from model.unet.model import UNet3D
from models.aggregate.model import AggregateModel
from data.build import make_dataloader
from utils.load_model import load_model_state_dict
from utils.vis.vis import save_batch_multi_view_with_heatmap, save_batch_keypoints, save_keypoint_detection
from utils.vis.graph import group_bar_plot
# from model.resnet.resnet import CustomizedResnet
from config.defualt import get_cfg_defaults
from pathlib import Path
from datetime import datetime
from utils.writer.writer import TensorBoardWriter
from utils.load_model import load_checkpoint
from utils.utils import find_keypoints_from_heatmaps, match_detected_groundtruth_keypoint
from trainer.model_trainer import save_checkpoint

PATH_TO_CHECKPOINT = '/home/wei/master-thesis/pretrain/unet-baseline/best_0.pth'
PATH_TO_HEATMAP_MODEL = '/home/wei/master-thesis/pretrain/CutomizeResNet-experiment/best_13.pth'

def main():
    cfg = get_cfg_defaults()
    # log_dir = str(Path.cwd().joinpath(
    #     'runs', 'myunet', datetime.today().strftime('%Y-%m-%d-%H:%M:%S')).resolve())
    # tsboard = TensorBoardWriter(log_dir)

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    agg_model = AggregateModel(
        in_channels=55, out_channels=55, num_feature=110, num_levels=2).to(device)
        # weight_resnet=load_model_state_dict(PATH_TO_HEATMAP_MODEL, 'cpu'),
        # weight_unet=load_model_state_dict(PATH_TO_CHECKPOINT, 'cpu'))
    agg_model = agg_model.load_weight(load_model_state_dict(PATH_TO_HEATMAP_MODEL, device), load_model_state_dict(PATH_TO_CHECKPOINT, device))
    # unet = UNet3D(in_channels=55, out_channels=55,
    #               num_feature=110, num_levels=2)
    # unet = unet.to(device)
    # heatmap_model = CustomizedResnet()
    # heatmap_model.load_state_dict(load_model_state_dict(PATH_TO_HEATMAP_MODEL))
    # heatmap_model.to(device)

    data_loader = {
        'train': make_dataloader(cfg, dataset_name='cmu', is_train=True),
        'valid': make_dataloader(cfg, dataset_name='cmu', is_train=False)
    }
    criterion = JointsMSELoss(use_target_weight=True)
    num_view = cfg.DATASET.NUM_VIEW
    num_frame = cfg.DATASET.NUM_FRAME_PER_SUBSEQ

    lowest_loss = float('inf')

    # stats
    stats = {'resnet': {'tp': 0, 'fp': 0, 'fn': 0},
             'unet': {'tp': 0, 'fp': 0, 'fn': 0}}

    for epoch in range(1):
        for phase in ['valid']:
            epoch_loss = {phase: 0}

            # if phase == 'train':
            #     unet.train()  # Set model to training mode
            #     num_view = cfg.DATASET.NUM_VIEW
            # else:
            #     unet.eval()   # Set model to evaluate mode
            #     num_view = 4
            #     valid_images = {}
            #     for i in range(num_view):
            #         valid_images['input_heatmap_cam_view_{}'.format(i)] = []
            #         valid_images['predict_heatmap_cam_view_{}'.format(i)] = []
            #         valid_images['predict_groundtruth_cam_view_{}'.format(i)] = [
            #         ]

            running_loss = 0.0
            # Iterate over data.
            for i, data in enumerate(data_loader[phase]):
                print('{}/{}'.format(i, len(data_loader[phase])))

                # optimizer.zero_grad()

                batch_kpt, batch_hm_gt, batch_img, num_person = \
                    data['keypoint2d'], data['heatmap'], data['img'], data['num_person']
                batch_hm_gt, batch_img = batch_hm_gt.to(
                    device), batch_img.float().to(device)
                print(agg_model)
                agg_model(data)

                # heatmap_input = torch.zeros(batch_hm_gt.size()).cuda(device)
                # batch_img = batch_img.permute((0, 3, 1, 2, 4, 5))
                # with torch.set_grad_enabled(phase == 'train'):
                #     for cam_view in range(num_view):
                #         for frame in range(num_frame):
                #             heatmap_input[:, :, :, :, cam_view, frame] = heatmap_model(
                #                 batch_img[:, :, :, :, cam_view, frame])

                # forward
                # track history if only in train
                    # loss = 0.0
                    # batch_hm_gt = batch_hm_gt.to(device)
                    # x = unet(heatmap_input[:, :, :, :, :, -1])
                    # for k in range(num_view):
                    #     loss += criterion(x[:, :, :, :, k],
                    #                       batch_hm_gt[:, :, :, :, k, -1])
                    # if phase == 'train':
                    #     loss.backward()
                    #     optimizer.step()

                    # if phase == 'valid':
                    #     for k in range(num_view):
                    #         unet_detected_keypoint = find_keypoints_from_heatmaps(
                    #             x[:, :, :, :, k], threshold=0.5)
                    #         unet_result = match_detected_groundtruth_keypoint(
                    #             batch_kpt[:, :, :, :, k, -1], unet_detected_keypoint, 1)
                    #         resnet_detected_keypoint = find_keypoints_from_heatmaps(
                    #             heatmap_input[:, :, :, :, k, -1], threshold=0.5)
                    #         resnet_result = match_detected_groundtruth_keypoint(
                    #             batch_kpt[:, :, :, :, k, -1], resnet_detected_keypoint, 1)
                            # stats['resnet']['fp']+= resnet_result['false positive']['num']
                            # stats['resnet']['tp']+= resnet_result['true positive']['num']
                            # stats['resnet']['fn']+= resnet_result['false negative']['num']
                            # stats['unet']['fp']+= unet_result['false positive']['num']
                            # stats['unet']['tp']+= unet_result['true positive']['num']
                            # stats['unet']['fn']+= unet_result['false negative']['num']
                            # unet_img = save_keypoint_detection(batch_image=batch_img[:, :, :, :, k, -1],
                            #                                    batch_heatmaps=x[:,
                            #                                                     :, :, :, k],
                            #                                    batch_fp_points=unet_result['false positive']['points'],
                            #                                    batch_fn_points=unet_result['false negative']['points'],
                            #                                    batch_tp_points=unet_result['true positive']['points'])
                            # resnet_img = save_keypoint_detection(batch_image=batch_img[:, :, :, :, k, -1],
                            #                                      batch_heatmaps=heatmap_input[:,
                            #                                                                   :, :, :, k, -1],
                            #                                      batch_fp_points=resnet_result['false positive']['points'],
                            #                                      batch_fn_points=resnet_result['false negative']['points'],
                            #                                      batch_tp_points=resnet_result['true positive']['points'])
                            # group_bar_plot(
                            #     [[unet_result['false positive']['num'], unet_result['true positive']['num'], unet_result['false negative']['num']],
                            #      [resnet_result['false positive']['num'], resnet_result['true positive']['num'], resnet_result['false negative']['num']]], ['unet', 'resnet'], ['fp', 'tp', 'fn']
                            # )
                            # test_img = save_batch_keypoints(batch_image=batch_img[:,:,:,:,k,-1],
                            #                     batch_heatmaps=x[:,:,:,:,k],
                            #                     gt_keypoints=batch_kpt[:,:,:,:,k,-1],
                            #                     pred_keypoints=centroids,
                            #                     num_joints_to_show=17)
                            # test_img = save_batch_heatmaps_multi(batch_image=batch_img[:,:,:,:,k,-1], batch_heatmaps=batch_hm_gt[:,:,:,:,k,-1])
                            # cv2.imwrite('unet_{}.png'.format(k), unet_img)
                            # cv2.imwrite('resnet_{}.png'.format(k), resnet_img)

                # statistics
            #     running_loss += loss.item()
            #     if i % 100 == 0:    # print every 100 mini-batches
            #         print('[%s][%d, %3d/%3d] loss: %.3f' %
            #               (phase, epoch, i, len(data_loader[phase]), loss.item()))
            #         if phase == 'valid':
            #             a = save_batch_multi_view_with_heatmap(
            #                 batch_img[:, :, :, :, :, -1], heatmap_input, 'test')
            #             b = save_batch_multi_view_with_heatmap(
            #                 batch_img[:, :, :, :, :, -1], x, 'test')
            #             c = save_batch_multi_view_with_heatmap(
            #                 batch_img[:, :, :, :, :, -1], batch_hm_gt, 'test')

            #             for k in range(num_view):
            #                 valid_images['input_heatmap_cam_view_{}'.format(
            #                     k)].append(np.flip(a[k], 2))
            #                 valid_images['predict_heatmap_cam_view_{}'.format(
            #                     k)].append(np.flip(b[k], 2))
            #                 valid_images['predict_groundtruth_cam_view_{}'.format(
            #                     k)].append(np.flip(c[k], 2))
            #     print('[%s][%d, %3d/%3d] loss: %.3f' %
            #           (phase, epoch, i, len(data_loader[phase]), loss.item()))

            # epoch_loss[phase] = running_loss/len(data_loader[phase])
            # if phase == 'valid':
            #     tsboard.add_images('valid', valid_images, epoch)
            #     tsboard.add_scalar('valid', epoch_loss[phase], epoch)
            #     del valid_images
            #     if epoch_loss[phase] < lowest_loss:
            #         lowest_loss = epoch_loss[phase]
            #         save_checkpoint(unet, optimizer, epoch,
            #                         epoch_loss[phase], log_dir, 'best')

            # elif phase == 'train':
            #     tsboard.add_scalar('train', epoch_loss[phase], epoch)
            #     save_checkpoint(unet, optimizer, epoch,
            #                     epoch_loss[phase], log_dir, 'checkpoint')
    # tot_sample = len(data_loader['valid']) * cfg.DATASET.CMU_BATCH_SIZE * num_view
    # bar_chart_data = []
    # bar_chart_data.append([
    #     stats['resnet']['tp']/tot_sample,
    #     stats['resnet']['fp']/tot_sample,
    #     stats['resnet']['fn']/tot_sample])
    # bar_chart_data.append([
    #     stats['unet']['tp']/tot_sample,
    #     stats['unet']['fp']/tot_sample,
    #     stats['unet']['fn']/tot_sample])
    # group_bar_plot(bar_chart_data, ['resnet', 'unet'], ['tp','fp','fn'], file_name='mean')

if __name__ == "__main__":
    main()

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_licenses: MIT Unlicense


text:
import numpy as np

class History:
    def __init__(self, config):
        self.cnn_format = config.cnn_format

        self.length = config.history_length

        num_channels, screen_height, screen_width = config.observation_space

        self.history = np.zeros(
            [self.length, num_channels, screen_height, screen_width], dtype=np.float32)

    def add(self, screen):
        self.history[:-1] = self.history[1:]
        self.history[-1] = screen

    def reset(self):
        self.history *= 0

    def get(self):
        if self.cnn_format == 'NHWC':
            return np.transpose(self.history, (2, 3, 1, 0))
        return self.history

max_stars_repo_name: evenmarbles/rva-tensorflow

max_issues_repo_name: evenmarbles/rva-tensorflow

max_forks_repo_name: evenmarbles/rva-tensorflow


text:
"""
Author: David Torpey

License: Apache 2.0

Redistribution Licensing:
- NumPy: https://www.numpy.org/license.html#

Module with unit tests for the VLAD implementation.
"""

import unittest

import numpy as np

from theama.feature_encoding import VLAD

class VLADTests(unittest.TestCase):
    """
    Class for VLAD unit tests.
    """

    def setUp(self):
        self.K = 32
        self.vlad = VLAD(self.K)
        self.D = 128
        self.dummy_descriptors = np.random.random((100, self.D))

    def tearDown(self):
        pass

    def test_codebook_creation(self):
        """Function to test successful learning of the VLAD
        codebook. Asserts that codebook is initialised.
        """

        self.vlad.learn_codebook(self.dummy_descriptors)

        self.assertTrue(self.vlad.codebook is not None)

    def test_compute_vlad_without_codebook_creation(self):
        """Function to test successful error raising when
        compute_vlad_descriptor is called without a prior
        call to the learn_codebook method. Asserts that
        exception is raised.
        """

        with self.assertRaises(Exception) as context:
            self.vlad.compute_feature_vector(self.dummy_descriptors)

        self.assertTrue('Please run learn_codebook method.' in str(context.exception))

    def test_compute_vlad_with_codebook_creation(self):
        """Function to test VLAD descriptors are successfully
        computed when a prior call to learn_codebook has been
        made. Asserts that the resulting VLAD descriptor
        dimensionality is correct.
        """

        self.vlad.learn_codebook(self.dummy_descriptors)

        vlad_descriptor = self.vlad.compute_feature_vector(self.dummy_descriptors)
        vlad_descriptor_dimension = len(vlad_descriptor)

        self.assertEqual(vlad_descriptor_dimension, self.D * self.K)

max_stars_repo_name: DavidTorpey/theama

max_issues_repo_name: DavidTorpey/theama

max_forks_repo_name: DavidTorpey/theama


text:
# Copyright (c) 2019, Ahmed M. Alaa
# Licensed under the BSD 3-clause license (see LICENSE.txt)

# stdlib
from typing import Tuple

# third party
import numpy as np
from scipy import stats

def squared_difference(x: np.ndarray, y: np.ndarray) -> np.ndarray:
    """
    Compute (x - y)(x - y) element-wise.
    """
    return (x - y) ** 2

def sqrt_PEHE(y: np.ndarray, hat_y: np.ndarray) -> float:
    """
    Precision in Estimation of Heterogeneous Effect(Numpy version).
    PEHE reflects the ability to capture individual variation in treatment effects.
    Args:
        y: expected outcome.
        hat_y: estimated outcome.
    """
    return np.sqrt(
        np.mean(squared_difference((y[:, 1] - y[:, 0]), (hat_y[:, 1] - hat_y[:, 0])))
    )

def RPol(t: np.ndarray, y: np.ndarray, hat_y: np.ndarray) -> np.ndarray:
    """
    Policy risk(RPol).
    RPol is the average loss in value when treating according to the policy implied by an ITE estimator.
    Args:
        t: treatment vector.
        y: expected outcome.
        hat_y: estimated outcome.
    Output:

    """
    hat_t = np.sign(hat_y[:, 1] - hat_y[:, 0])
    hat_t = 0.5 * (hat_t + 1)
    new_hat_t = np.abs(1 - hat_t)

    # Intersection
    idx1 = hat_t * t
    idx0 = new_hat_t * (1 - t)

    # risk policy computation
    RPol1 = (np.sum(idx1 * y) / (np.sum(idx1) + 1e-8)) * np.mean(hat_t)
    RPol0 = (np.sum(idx0 * y) / (np.sum(idx0) + 1e-8)) * np.mean(new_hat_t)

    return 1 - (RPol1 + RPol0)

def ATE(y: np.ndarray, hat_y: np.ndarray) -> np.ndarray:
    """
    Average Treatment Effect.
    ATE measures what is the expected causal effect of the treatment across all individuals in the population.
    Args:
        y: expected outcome.
        hat_y: estimated outcome.
    """
    return np.abs(np.mean(y[:, 1] - y[:, 0]) - np.mean(hat_y[:, 1] - hat_y[:, 0]))

def ATT(t: np.ndarray, y: np.ndarray, hat_y: np.ndarray) -> np.ndarray:
    """
    Average Treatment Effect on the Treated(ATT).
    ATT measures what is the expected causal effect of the treatment for individuals in the treatment group.
    Args:
        t: treatment vector.
        y: expected outcome.
        hat_y: estimated outcome.
    """
    # Original ATT
    ATT_value = np.sum(t * y) / (np.sum(t) + 1e-8) - np.sum((1 - t) * y) / (
        np.sum(1 - t) + 1e-8
    )
    # Estimated ATT
    ATT_estimate = np.sum(t * (hat_y[:, 1] - hat_y[:, 0])) / (np.sum(t) + 1e-8)
    return np.abs(ATT_value - ATT_estimate)

def mean_confidence_interval(
    data: np.ndarray, confidence: float = 0.95
) -> Tuple[float, float]:
    """
    Generate the mean and a confindence interval over observed data.
    Args:
        data: observed data
        confidence: confidence level
    """
    a = 1.0 * np.array(data)
    n = len(a)
    m, se = np.mean(a), stats.sem(a)
    h = se * stats.t.ppf((1 + confidence) / 2.0, n - 1)

    return m, h

max_stars_repo_name: bcebere/ite-api

max_issues_repo_name: bcebere/ite-api

max_forks_repo_name: bcebere/ite-api


text:
import sys
import logging
import time
import random
import numpy as np
from collections import defaultdict, namedtuple
from optparse import OptionParser

import h5py
import fielutil

class StepShingler:

    ShingleSize=namedtuple('ShingleSize', ['rows','cols'])

    def __init__(self, img,
                 hstep, vstep,
                 shingle_size=ShingleSize(rows=120,cols=120),
                 fill_value=255):
        """ given an image of a document fragment,
        produce deterministic shingles from that document

        Arguments:
            img -- array (numpy array interface)
            hstep -- number of columns to step between shingles
            vstep -- number of rows to step between shingles
            shingle_size -- ROWS, COLS format.
        """
        self.img = img
        self.shingle_size = StepShingler.ShingleSize(*shingle_size)
        self.vstep = vstep
        self.hstep = hstep

        # warn if danger of leaving off right or bottom edge
        h_step_space = img.shape[1] - self.shingle_size.cols
        # number of pixels left over on right edge
        h_step_margin = h_step_space % hstep
        if h_step_margin != 0:
            # fill horizontal margin with fill_value
            fill_block = np.empty((img.shape[0], h_step_margin))
            fill_block.fill(fill_value)
            img = np.concatenate((img, fill_block), axis=1)
        v_step_space = img.shape[0] - self.shingle_size.rows
        # number of pixels left over on bottom edge
        v_step_margin = v_step_space % vstep
        if v_step_margin != 0:
            # fill vertical margin with fill_value
            fill_block = np.empty((v_step_margin,img.shape[1]))
            fill_block.fill(fill_value)
            img = np.concatenate((img, fill_block), axis=0)

    def __iter__(self):
        for row_i in xrange(0, self.img.shape[0] - self.shingle_size[0], self.vstep):
            for col_j in xrange(0, self.img.shape[1] - self.shingle_size[1], self.hstep):
                logger = logging.getLogger(__name__)
                #logger.info("Row {0}, Col {1}".format(row_i, col_j))
                end_col = col_j + self.shingle_size[1]
                end_row = row_i + self.shingle_size[0]
                yield self.img[row_i:end_row, col_j:end_col]

def zero_one(x):
    """ Normalize byte-sized integer to 0-1 and flip it so 255 (white)
    maps to 0.0
    """
    x = 1. - (x/255.)
    return x

def shingles_4_inference(authors_fragments_set, hstep=90, vstep=90,
                         shingle_size=StepShingler.ShingleSize(rows=120, cols=120),
                         var_threshold=None, postprocess=zero_one,
                         verbose=False):
    """ Given a dictionary-like object (e.g. HDF5 Groups, etc.) of
    author-keyed fragments, iterate over individual authors, fragments,
    and yield pairs of shingle (using deterministic step shingling) and author id.

    Arguments:
        authors_fragments_set -- dictionary-like object
        hstep -- horizontal step size in pixels/cols
        vstep -- vertical step size in pixels/rows
        shingle_size -- (rows, cols) tuple indicating size of shingle
            StepShingler.ShingleSize provides a useful namedtuple to avoid
            parameter order confusion
        var_threshold -- float or None; if given, do not yield any shingle whose
            variance falls below this quantity
        verbose -- if True, will warn on fragments where there are unshingled pixels at the right
            or bottom edge
    """

    try:
        if verbose:
            old_level = logging.getLogger(__name__).getEffectiveLevel()
            logging.getLogger(__name__).setLevel(logging.INFO)
        for author_key in authors_fragments_set:
            logger=logging.getLogger(__name__); logger.info("Author: {0}".format(author_key))
            author_fragments = authors_fragments_set[author_key]
            for fragment_key in author_fragments:
                fragment = author_fragments[fragment_key]
                logger.info("Fragment shape: {0}".format(fragment.shape))
                shingle_iterator = StepShingler(fragment, hstep, vstep, shingle_size)
                for shingle in shingle_iterator:
                    if postprocess is not None:
                        shingle=postprocess(shingle)
                    if var_threshold is not None:
                        shingle_var = np.var(shingle)
                        if shingle_var < var_threshold:
                            continue
                    yield shingle, author_key, fragment_key
    finally:
        if verbose:
            logging.getLogger(__name__).setLevel(old_level)

class ICDARFeaturizer:
    """
    Easy interface for featurizing documents in ICDAR13
    """
    def __init__(self, icdar_hdf5_path, fiel_weights):
        """ hdf5_path -- path to docid-only ICDAR13 set"""
        self.fielnet = fielutil.tfdnet(fiel_weights, layer='fc7')
        # vacuous compile, will not be trained
        self.fielnet.compile(optimizer='sgd', loss='mse')
        self.icdar_hdf5_path = icdar_hdf5_path

    def fielify_img(self, img):
        fiel_features = self.fielnet.predict(img)
        return fiel_features

    def fielify_doc_by_id(self, doc_id, hstep=120, vstep=120, return_mean=False, stdev_threshold=None, num_shingles=None):
        """ Get Fiel features for a document

        Arguments:
            doc_id -- document ID (e.g. '002_3.tif')
            hstep, vstep -- horizontal and vertical stride between shingles
            return_mean -- Return features for all features (False) or do
                mean aggregation?
            stdev_threshold -- shingle must have stdev > this to be counted, if not None
            num_shingles -- if not None, sort shingles by variance and extract features
                only from this many. If fewer than num_shingles shingles are available in a document,
                ValueError is raised.
        """
        with h5py.File(self.icdar_hdf5_path, "r") as icdar_hdf5:
            img = icdar_hdf5[doc_id]
            # Get shingle generator
            shingler = StepShingler(img, hstep=hstep, vstep=vstep)
            all_features = []
            for shingle in shingler:
                shingle = np.expand_dims(np.expand_dims(shingle, 0),0)
                # normalize
                shingle = zero_one(shingle)
                # skip if threshold not satisfied
                if stdev_threshold is not None and np.std(shingle) < stdev_threshold:
                    continue
                # extract features
                fiel_features=self.fielify_img(shingle)
                all_features.append(fiel_features)
            all_features = np.array(all_features)
            # handle conditions involving shingle quantity
            if num_shingles is not None and all_features.shape[0] < num_shingles:
                raise ValueError("Not enough shingles in document")
            if num_shingles is not None and all_features.shape[0] > num_shingles:
                # sort shingles by variance and pick the top few
                all_features = np.array(all_features)
                variances = np.var(all_features, axis=(1,2))
                variance_rankings = np.argsort(-variances)
                all_features = all_features[variance_rankings[:num_shingles], :]

                assert(all_features.shape[0] == num_shingles,
                    "Wanted {0} shingles, got {1}.\n{2}".format(num_shingles,
                                                                all_features.shape[0],
                                                                variance_rankings[:30]))
            # do mean aggregation if required
            if return_mean:
                all_features=np.mean(all_features, axis=0)
            return all_features

def get_icdar_features(features_hdf5_path,
                       icdar_docsonly_hdf5_path, icdar_authors_docs_hdf5_path,
                       fielnet_weights_path="../convnets/fielnet/fielnet.hdf5",
                       **kwargs):
    """
    Get Fiel features for ICDAR13. Requires both author-document and documents-only
    HDF5 files for the set.

    Arguments:
        features_hdf5_path -- save results to an HDF5 file at this path
        icdar_docsonly_hdf5_path -- path to documents-only ICDAR13 HDF5 set
        icdar_authors_docs_hdf5_path -- path to authors+docs ICDAR13 HDF5 set
        fielnet_weights_path -- path to HDF5 set of saved weights for Fiel-style convnet
        kwargs -- arguments to be passed along to ICDARFeaturizer.fielify_doc_by_id for each
            document in ICDAR13
    """
    ic_feat = ICDARFeaturizer(icdar_docsonly_hdf5_path,fielnet_weights_path)

    # featurize documents, authors
    with h5py.File(features_hdf5_path, "w") as fiel_feats:
        with h5py.File(icdar_authors_docs_hdf5_path, "r") as f:
            # do document-level features and author-level features (just means of document feats for now)
            authors_group = fiel_feats.create_group("authors")
            fragments_group = fiel_feats.create_group("fragments")
            for i, author in enumerate(f):
                fragments_author_group = fragments_group.create_group(author)
                author_fragments = []
                for fragment in f[author]:
                    fragments_fiel_features = ic_feat.fielify_doc_by_id(fragment, **kwargs)
                    fragment_dataset = fragments_author_group.create_dataset(fragment, data=fragments_fiel_features)
                    # check that any features were extracted at all, if not
                    # then do not use fragment for author-level features
                    if len(fragments_fiel_features.shape) > 0:
                        author_fragments.append(fragment_dataset)
                    else:
                        logger = logging.getLogger(__name__)
                        logger.info("No features extracted for {0} (threshold too high?)".format(fragment))
                # get author-level features by aggregating over documents
                try:
                    author_mean = np.mean(author_fragments, axis=0)
                except:
                    print author_fragments
                    raise
                authors_group.create_dataset(author, data=author_mean)

def demo_shingles():
    # get an ICDAR image
    icdar_path = "/fileserver/icdar13/benchmarking-processed/author_icdar_be.hdf5"
    icdar_hdf5 = h5py.File(icdar_path, "r")
    img = icdar_hdf5['001']['001_1.tif']

    inf_shingler = StepShingler(img, hstep=90, vstep=120, shingle_size=(120,120))

    plt.figure(figsize=(10, 3))
    for i, shingle in enumerate(inf_shingler):
        plt.subplot(1,9,i+1)
        plt.imshow(shingle,cmap='gray')
        plt.tick_params(
            axis='both',
            which='both',
            bottom='off',
            top='off',
            left='off',
            right='off',
            labelbottom='off',
            labelleft='off')

        if (i+1) % 9 == 0:
            plt.show()
            break

def demo_shingle_iteration():
    # get ICDAR images
    icdar_path = "/fileserver/icdar13/benchmarking-processed/author_icdar_be.hdf5"
    icdar_hdf5 = h5py.File(icdar_path, "r")

    icdar_shingles = shingles_4_inference(icdar_hdf5, var_threshold=0.025, verbose=False)
    if icdar_shingles is not None:
        for icdar_shingle, author_id, fragment_id in icdar_shingles:
            logger=logging.getLogger(__name__)
            logger.info(icdar_shingle.shape)
            #a2p.display_img_array(icdar_shingle)
            plt.imshow(icdar_shingle, cmap='gray')
            plt.show()
            print "Author: {0}, Fragment: {1}".format(author_id, fragment_id)
            shingle_var = np.var(icdar_shingle.reshape(-1))
            print "Variance: {0}".format(shingle_var)
            IPython.display.clear_output(wait=True)
            time.sleep(0.15)

# demo_shingles()
# demo_shingle_iteration()
if __name__ == "__main__":

    demo_shingle_iteration()


text:
import time
import functools
import collections

import torch
import numpy as np
from matplotlib import patches, text
from torch.nn import functional as F

top = 0
left = 1
bottom = 2
right = 3

def calculate_iou(bbox_a, bbox_b):
    yA = torch.max(bbox_a[:, top], bbox_b[:, top])
    xA = torch.max(bbox_a[:, left], bbox_b[:, left])

    yB = torch.min(bbox_a[:, bottom], bbox_b[:, bottom])
    xB = torch.min(bbox_a[:, right], bbox_b[:, right])

    interArea = (xB - xA) * (yB - yA)

    mask_a = (xB - xA) > 0
    mask_b = (yB - yA) > 0

    no_intersect_mask = (mask_a & mask_b) ^ 1

    # compute the area of both the prediction and ground-truth
    # rectangles
    boxAArea = (bbox_a[:, top] - bbox_a[:, bottom]) * (bbox_a[:, left] - bbox_a[:, right])
    boxBArea = (bbox_b[:, top] - bbox_b[:, bottom]) * (bbox_b[:, left] - bbox_b[:, right])

    # compute the intersection over union by taking the intersection
    # area and dividing it by the sum of prediction + ground-truth
    # areas - the interesection area

    iou = interArea.float() / (boxAArea + boxBArea - interArea).float()

    iou[no_intersect_mask] = 0

    # return the intersection over union value
    return iou

def img_to_np(img):
    img = img.cpu().numpy()
    img = np.copy(img)
    img = np.swapaxes(img, 0, 1)
    img = np.swapaxes(img, 1, 2)

    return img

def add_bbox(ax, bbox, color, alpha=1, text=""):
    top, left, bottom, right = bbox
    rect = patches.Rectangle((left,top), right-left, bottom-top, linewidth=2, edgecolor=color, facecolor='none', alpha=alpha)
    ax.add_patch(rect)
    ax.text(
        left,
        top,
        text,
        fontsize=12,
        bbox=dict(
            boxstyle="square",
            fc=color,
        )
    )

def visualize_anchors(ax, anchors):
    for a in anchors:
        add_bbox(ax, torch.round(a).long(), [1.0, 1.0, 1.0], 0.2)

def visualize_rpn(ax, nms_reg, nms_cls, img, color=[1.0, 1.0, 1.0], draw_all=False):
    np_img = img_to_np(img)

    for r, c in zip(nms_reg, nms_cls):
        if c >= 0.5 or draw_all:
            add_bbox(ax, r, color=color, text="c={:.2f}".format(c))

def visualize_rcnn(ax, rcnn_reg, rcnn_cls, color_map):
    for rcnn_r, rcnn_c in zip(rcnn_reg, rcnn_cls):
        cls, index = torch.max(rcnn_c, dim=0)

        if index == 0:
            continue

        cls_color = color_map[0]
        if int(index) in color_map:
            cls_color = color_map[int(index)]

        add_bbox(ax, rcnn_r, color=cls_color, text="class={}: {:.2f}".format(index, cls))

class GPURuntimeProfiler:
    def __init__(self):
        self.stats = collections.defaultdict(int)

    def measure_gpu(self, name):
        def decorator(f):
            @functools.wraps(f)
            def measure(*args, **kwargs):
                torch.cuda.synchronize()
                start = time.perf_counter()

                result = f(*args, **kwargs)

                torch.cuda.synchronize() # wait for mm to finish
                end = time.perf_counter()

                duration = end - start

                self.stats[name] += duration

                return result

            return measure

        return decorator

max_stars_repo_name: Mctigger/understandable-faster-rcnn

max_issues_repo_name: mctigger/understandable-faster-rcnn

max_forks_repo_name: mctigger/understandable-faster-rcnn


text:
#!/usr/bin/env python3

import numpy as np
RNNCell = __import__('0-rnn_cell').RNNCell

np.random.seed(0)
rnn_cell = RNNCell(10, 15, 5)
print("Wh:", rnn_cell.Wh)
print("Wy:", rnn_cell.Wy)
print("bh:", rnn_cell.bh)
print("by:", rnn_cell.by)
rnn_cell.bh = np.random.randn(1, 15)
rnn_cell.by = np.random.randn(1, 5)
h_prev = np.random.randn(8, 15)
x_t = np.random.randn(8, 10)
h, y = rnn_cell.forward(h_prev, x_t)
print(h.shape)
print(h)
print(y.shape)
print(y)


text:
import pytest
from scipy.signal import butter, filtfilt
import numpy as np
from harmoni.extratools import hilbert_

@pytest.fixture(scope="session")
def general_setting():
    sfreq = 512  # sampling frequency
    f0 = 10  # fundamental frequency
    t_len = 60 * 2  # total simulation time
    n_samp = sfreq * t_len  # number of time samples
    n = 2  # the ratio of the fast and slow oscillations, i.e. the harmonic of interest

    # the filter coefficients of the frequency band of the fundamental frequency
    b1, a1 = butter(N=2, Wn=np.array([f0 - 1, f0 + 1]) / sfreq * 2, btype='bandpass')

    # the filter coefficients of the frequency band of the n-th frequency
    b2, a2 = butter(N=2, Wn=np.array([n * f0 - 1, n * f0 + 1]) / sfreq * 2, btype='bandpass')
    return sfreq, b1, a1, b2, a2, n, n_samp

@pytest.fixture()
def generate_nonsinsig_fortest(general_setting):
    """
    help function to generate non-sinusoidal signal for test functions

    :param general_setting: the fixture to generate the general settings
    :return: a non-signusoidal signal with fundamental and 2nd harmonic components
    """
    sfreq, b1, a1, b2, a2, n, n_samp = general_setting
    x1 = filtfilt(b1, a1, np.random.randn(n_samp))
    x1_h = hilbert_(x1)
    y1 = np.real(np.abs(x1_h) * np.exp(1j * n * np.angle(x1_h)))
    return x1 + 0.25 * y1

@pytest.fixture()
def generate2_nonsinsig_fortest(general_setting):
    """
    help function to generate non-sinusoidal signal for test functions

    :param general_setting: the fixture to generate the general settings

    :return:
    z1: a non-signusoidal signal with fundamental and 2nd harmonic components
    z2: a mixed signal, whose alpha and beta components are not harmonics (they do not have phase synchronization)
    note: the beta part of z2 has 1:2 synchronization to the alpha part of z1

    """
    sfreq, b1, a1, b2, a2, n, n_samp = general_setting
    x1 = filtfilt(b1, a1, np.random.randn(n_samp))
    x1_h = hilbert_(x1)
    y1 = np.real(np.abs(x1_h) * np.exp(1j * n * np.angle(x1_h)))
    phi0 = np.pi / 2 * np.random.random(1) + np.pi / 4
    x2 = filtfilt(b1, a1, np.random.randn(n_samp))
    y2 = np.real(np.abs(x1_h) * np.exp(1j * n * np.angle(x1_h)) + phi0)
    z1 = x1 + 0.25 * y1
    z2 = x2 + 0.25 * y2
    return z1, z2

max_stars_repo_name: harmonic-minimization/harmoni

max_issues_repo_name: harmonic-minimization/harmoni

max_forks_repo_name: harmonic-minimization/harmoni


text:
# Copyright 2018 Google LLC
#
# 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
#
#     https://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 collections
import functools
from functools import partial
import operator as op
from typing import (Any, Callable, Hashable, Iterable, Optional, Tuple, Type,
                    TypeVar, overload, TYPE_CHECKING)

from ..lib import pytree

from .._src.util import safe_zip, unzip2

from .._src import traceback_util
traceback_util.register_exclusion(__file__)

T = TypeVar("T")
U = TypeVar("U")

if TYPE_CHECKING:
  PyTreeDef = pytree.PyTreeDef
else:
  PyTreeDef = Any

def tree_flatten(tree, is_leaf: Optional[Callable[[Any], bool]] = None):
  """Flattens a pytree.

  Args:
    tree: a pytree to flatten.
    is_leaf: an optionally specified function that will be called at each
      flattening step. It should return a boolean, which indicates whether
      the flattening should traverse the current object, or if it should be
      stopped immediately, with the whole subtree being treated as a leaf.

  Returns:
    A pair where the first element is a list of leaf values and the second
    element is a treedef representing the structure of the flattened tree.
  """
  return pytree.flatten(tree, is_leaf)

def tree_unflatten(treedef, leaves):
  """Reconstructs a pytree from the treedef and the leaves.

  The inverse of :func:`tree_flatten`.

  Args:
    treedef: the treedef to reconstruct
    leaves: the list of leaves to use for reconstruction. The list must match
      the leaves of the treedef.

  Returns:
    The reconstructed pytree, containing the ``leaves`` placed in the structure
    described by ``treedef``.
  """
  return treedef.unflatten(leaves)

def tree_leaves(tree):
  """Gets the leaves of a pytree."""
  return pytree.flatten(tree)[0]

def tree_structure(tree):
  """Gets the treedef for a pytree."""
  return pytree.flatten(tree)[1]

def treedef_tuple(treedefs):
  """Makes a tuple treedef from a list of child treedefs."""
  return pytree.tuple(list(treedefs))

def treedef_children(treedef):
  return treedef.children()

def treedef_is_leaf(treedef):
  return treedef.num_nodes == 1

def all_leaves(iterable):
  """Tests whether all elements in the given iterable are all leaves.

  >>> tree = {"a": [1, 2, 3]}
  >>> assert all_leaves(jax.tree_leaves(tree))
  >>> assert not all_leaves([tree])

  This function is useful in advanced cases, for example if a library allows
  arbitrary map operations on a flat list of leaves it may want to check if
  the result is still a flat list of leaves.

  Args:
    iterable: Iterable of leaves.

  Returns:
    A boolean indicating if all elements in the input are leaves.
  """
  return pytree.all_leaves(iterable)

_Children = TypeVar("_Children", bound=Iterable[Any])
_AuxData = TypeVar("_AuxData", bound=Hashable)

def register_pytree_node(nodetype: Type[T],
                         flatten_func: Callable[[T], Tuple[_Children, _AuxData]],
                         unflatten_func: Callable[[_AuxData, _Children], T]):
  """Extends the set of types that are considered internal nodes in pytrees.

  See `example usage <pytrees.html>`_.

  Args:
    nodetype: a Python type to treat as an internal pytree node.
    flatten_func: a function to be used during flattening, taking a value of
      type ``nodetype`` and returning a pair, with (1) an iterable for the
      children to be flattened recursively, and (2) some hashable auxiliary
      data to be stored in the treedef and to be passed to the
      ``unflatten_func``.
    unflatten_func: a function taking two arguments: the auxiliary data that was
      returned by ``flatten_func`` and stored in the treedef, and the
      unflattened children. The function should return an instance of
      ``nodetype``.
  """
  pytree.register_node(nodetype, flatten_func, unflatten_func)
  _registry[nodetype] = _RegistryEntry(flatten_func, unflatten_func)

def register_pytree_node_class(cls):
  """Extends the set of types that are considered internal nodes in pytrees.

  This function is a thin wrapper around ``register_pytree_node``, and provides
  a class-oriented interface::

    @register_pytree_node_class
    class Special:
      def __init__(self, x, y):
        self.x = x
        self.y = y
      def tree_flatten(self):
        return ((self.x, self.y), None)
      @classmethod
      def tree_unflatten(cls, aux_data, children):
        return cls(*children)
  """
  register_pytree_node(cls, op.methodcaller('tree_flatten'), cls.tree_unflatten)
  return cls

def tree_map(f: Callable[..., Any], tree: Any, *rest: Any,
             is_leaf: Optional[Callable[[Any], bool]] = None) -> Any:
  """Maps a multi-input function over pytree args to produce a new pytree.

  Args:
    f: function that takes ``1 + len(rest)`` arguments, to be applied at the
      corresponding leaves of the pytrees.
    tree: a pytree to be mapped over, with each leaf providing the first
      positional argument to ``f``.
    *rest: a tuple of pytrees, each of which has the same structure as tree or
      or has tree as a prefix.
    is_leaf: an optionally specified function that will be called at each
      flattening step. It should return a boolean, which indicates whether
      the flattening should traverse the current object, or if it should be
      stopped immediately, with the whole subtree being treated as a leaf.

  Returns:
    A new pytree with the same structure as ``tree`` but with the value at each
    leaf given by ``f(x, *xs)`` where ``x`` is the value at the corresponding
    leaf in ``tree`` and ``xs`` is the tuple of values at corresponding nodes in
    ``rest``.
  """
  leaves, treedef = tree_flatten(tree, is_leaf)
  all_leaves = [leaves] + [treedef.flatten_up_to(r) for r in rest]
  return treedef.unflatten(f(*xs) for xs in zip(*all_leaves))

tree_multimap = tree_map

# TODO(mattjj,phawkins): consider removing this function
def _process_pytree(process_node, tree):
  leaves, treedef = pytree.flatten(tree)
  return treedef.walk(process_node, None, leaves), treedef

def build_tree(treedef, xs):
  return treedef.from_iterable_tree(xs)

def tree_transpose(outer_treedef, inner_treedef, pytree_to_transpose):
  flat, treedef = tree_flatten(pytree_to_transpose)
  inner_size = inner_treedef.num_leaves
  outer_size = outer_treedef.num_leaves
  if treedef.num_leaves != (inner_size * outer_size):
    expected_treedef = outer_treedef.compose(inner_treedef)
    raise TypeError(f"Mismatch\n{treedef}\n != \n{expected_treedef}")
  flat = iter(flat)
  lol = [[next(flat) for _ in range(inner_size)] for __ in range(outer_size)]
  transposed_lol = zip(*lol)
  subtrees = map(partial(tree_unflatten, outer_treedef), transposed_lol)
  return tree_unflatten(inner_treedef, subtrees)

# TODO(mattjj): remove the Python-side registry when the C++-side registry is
# sufficiently queryable that we can express _replace_nones. That may mean once
# we have a flatten_one function.
_RegistryEntry = collections.namedtuple("_RegistryEntry", ["to_iter", "from_iter"])
_registry = {
    tuple: _RegistryEntry(lambda xs: (xs, None), lambda _, xs: tuple(xs)),
    list: _RegistryEntry(lambda xs: (xs, None), lambda _, xs: list(xs)),
    dict: _RegistryEntry(lambda xs: unzip2(sorted(xs.items()))[::-1],
                         lambda keys, xs: dict(zip(keys, xs))),
    type(None): _RegistryEntry(lambda z: ((), None), lambda _, xs: None),
}
def _replace_nones(sentinel, tree):
  """Replaces ``None`` in ``tree`` with ``sentinel``."""
  if tree is None:
    return sentinel
  else:
    handler = _registry.get(type(tree))
    if handler:
      children, metadata = handler.to_iter(tree)
      proc_children = [_replace_nones(sentinel, child) for child in children]
      return handler.from_iter(metadata, proc_children)
    elif isinstance(tree, tuple) and hasattr(tree, '_fields'):
      # handle namedtuple as a special case, based on heuristic
      children = iter(tree)
      proc_children = [_replace_nones(sentinel, child) for child in children]
      return type(tree)(*proc_children)
    else:
      return tree

no_initializer = object()

@overload
def tree_reduce(function: Callable[[T, Any], T],
                tree: Any) -> T:
    ...

@overload
def tree_reduce(function: Callable[[T, Any], T],
                tree: Any,
                initializer: T) -> T:
    ...

def tree_reduce(function: Callable[[T, Any], T],
                tree: Any,
                initializer: Any = no_initializer) -> T:
  if initializer is no_initializer:
    return functools.reduce(function, tree_leaves(tree))
  else:
    return functools.reduce(function, tree_leaves(tree), initializer)

def tree_all(tree):
  return all(tree_leaves(tree))

register_pytree_node(
  collections.OrderedDict,
  lambda x: (tuple(x.values()), tuple(x.keys())),
  lambda keys, values: collections.OrderedDict(safe_zip(keys, values)))

register_pytree_node(
  collections.defaultdict,
  lambda x: (tuple(x.values()), (x.default_factory, tuple(x.keys()))),
  lambda s, values: collections.defaultdict(s[0], safe_zip(s[1], values)))  # type: ignore[index]

class Partial(functools.partial):
  """A version of functools.partial that works in pytrees.

  Use it for partial function evaluation in a way that is compatible with JAX's
  transformations, e.g., ``Partial(func, *args, **kwargs)``.

  (You need to explicitly opt-in to this behavior because we didn't want to give
  functools.partial different semantics than normal function closures.)

  For example, here is a basic usage of ``Partial`` in a manner similar to
  ``functools.partial``:

  >>> import jax.numpy as jnp
  >>> add_one = Partial(jnp.add, 1)
  >>> add_one(2)
  DeviceArray(3, dtype=int32, weak_type=True)

  Pytree compatibility means that the resulting partial function can be passed
  as an argument within transformed JAX functions, which is not possible with a
  standard ``functools.partial`` function:

  >>> from jax import jit
  >>> @jit
  ... def call_func(f, *args):
  ...   return f(*args)
  ...
  >>> call_func(add_one, 2)
  DeviceArray(3, dtype=int32, weak_type=True)

  Passing zero arguments to ``Partial`` effectively wraps the original function,
  making it a valid argument in JAX transformed functions:

  >>> call_func(Partial(jnp.add), 1, 2)
  DeviceArray(3, dtype=int32, weak_type=True)

  Had we passed ``jnp.add`` to ``call_func`` directly, it would have resulted in a
  ``TypeError``.

  Note that if the result of ``Partial`` is used in the context where the
  value is traced, it results in all bound arguments being traced when passed
  to the partially-evaluated function:

  >>> print_zero = Partial(print, 0)
  >>> print_zero()
  0
  >>> call_func(print_zero)
  Traced<ShapedArray(int32[], weak_type=True)>with<DynamicJaxprTrace(level=0/1)>
  """

register_pytree_node(
    Partial,
    lambda partial_: ((partial_.args, partial_.keywords), partial_.func),
    lambda func, xs: Partial(func, *xs[0], **xs[1]),  # type: ignore[index]
)

max_stars_repo_name: nonconvexopt/jax

max_issues_repo_name: nonconvexopt/jax

max_forks_repo_name: nonconvexopt/jax

reason: import jax,from jax


text:
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import glob
import json, csv
import wave
from PIL import Image
from scipy import fromstring, int16
import struct

# keras系
from keras import models
from keras import layers
from keras.layers import Input,merge
from keras.layers.core import Reshape,Dense,Dropout,Activation,Flatten,MaxoutDense,Merge
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D, UpSampling2D, Deconvolution2D
from keras.layers.normalization import BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import load_img
from keras.models import Sequential, Model
from keras.optimizers import Adam, Adadelta
from keras.utils.generic_utils import Progbar
from keras.utils.visualize_util import plot
from keras.datasets import cifar100
from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint
import keras.backend.tensorflow_backend as KTF
import tensorflow as tf
from src import train, generate, set_model
import glob

if __name__ == '__main__':
    # load model
    model = Sequential()
    model = set_model(model)
    model.load_weights('./model/weights.hdf5')

    img_paths = glob.glob('./imgs/*.png')
    generate(img_paths, model)


text:
# Simple Linear Regression

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

# Fitting Simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
reg = LinearRegression()
reg.fit(X_train, y_train)

# Predicting the Test set results
y_pred = reg.predict(X_test)

# Visualising the Training set results
fig = plt.figure()
plt.scatter(X_train, y_train, color='green')
plt.plot(X_train, reg.predict(X_train), color='blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
fig.savefig('train.jpg')

# Visualising the Test set results
fig1 = plt.figure()
plt.scatter(X_test, y_test, color='red')
plt.plot(X_train, reg.predict(X_train), color='blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
fig1.savefig('test.jpg')