text:
# There are several obvious downsides to auto-regressive models:
#
#   1. We need many input neurons to account for all of the previous outputs
#       that we care about. The input space can grow huge when we need to
#       account for many previous output time-steps.
#   2. This also mean that each timestep is learned separately because it uses
#       differnet neurons. Learning will take much longer.
#   2. We are limited by how far back we need to look. The decision needs to be
#       made ahead of time when designing the model, instead of being learned.
#   3. We are only only relying on the previous input and output, instead of
#       learning more complicated dynamics or state.
#
# To illustrate these limitations, in this exercise we'll train a model that
# performs binary addition (and by extension, any positive integer addition),
# which can be defined as:
#
#   x1, x2, Carry
#   ---------------------------
#   0,  0,  0   => 0 (Carry: 0)
#   0,  1,  0   => 1 (Carry: 0)
#   1,  0,  0   => 1 (Carry: 0)
#   1,  1,  0   => 0 (Carry: 1)
#   0,  0,  1   => 1 (Carry: 0)
#   0,  1,  1   => 0 (Carry: 1)
#   1,  0,  1   => 0 (Carry: 1)
#   1,  1,  1   => 1 (Carry: 1)
#
# In some sense it's like the auto-regressive model, except that we don't need
# to concatenate the full history of events, but only the Carry. Essentially
# that carry neuron along with the input is enough to produce the output (and
# the new bit to carry over to the next timestep). The carry is the "memory" or
# state of the network spanning over multiple timesteps. This way we can
# maintain a "running-state" over the time-steps, instead of having to access
# all of the previous time-steps.
#
# A Recurent Neural Network is a system that's able to learn some internal
# state (in this case, simply the carry bit), learn when and how the state
# should be modified and finally access the state to determine the new value.
# One way to think about it is like having two different models: one for
# learning the recurrent state, and the other for learning the actual results
# normally.
import numpy as np
np.random.seed(1)

EPOCHS = 650
ALPHA = 0.005
H = 5

# input are a few examples of small binary strings. NOTE that while developing
# it, I've also experimented with randomly generating and endless stream of
# inputs (randint(255)) and managed to converge within a few hundred epochs. But
# I've decided to not include this code here, because the randomization code was
# pretty big and hairy and distracted from the core idea of how this algorithm
# works. Also NOTE that we're using a dot to separate the numbers. This allows
# the network to learn when the carry state should be reset between numbers,
# without us having to force it on the network superficially.
X1 = "00101.01100.01001.101.0.01100.01101.0110.010.01110.010.01011.01010.01101."
X2 = "01011.01000.01011.000.1.00111.01100.1001.100.00101.010.01100.01100.01110."
T  = "10000.10100.10100.101.1.10011.11001.1111.110.10011.100.10111.10110.11011."

# binary addition is done right-to-left. it's impossible to know the output of
# the addition of two bits unless we know the result (carry) from the bits to
# the right.
X1 = "".join(reversed(X1))
X2 = "".join(reversed(X2))
T  = "".join(reversed(T))

# finally one-hot encode
X1 = np.array([np.eye(3)["01.".index(c)] for c in X1])
X2 = np.array([np.eye(3)["01.".index(c)] for c in X2])
T  = np.array([np.eye(3)["01.".index(c)] for c in T])
X  = np.concatenate((X1, X2), axis=1) # both digits are part of the same input

class Recurrent(object):
    def __init__(self, n, m):
        # we're using different weights for the input and the hidden state,
        # Wx and Wh respectively. Just like in the auto-regressive model, we can
        # just concatenate the input x and the hidden state h and then use a
        # single set of weights in size (m, n + m + 1). But this will make the
        # backwards pass a bit more involved. See below.
        self.Wx = np.random.randn(m, n + 1) * 0.01
        self.Wh = np.random.randn(m, m) * 0.01

        # initial hidden state.
        self._h = np.zeros(m)

        # for back propagation, we'll need the full history of all inputs (x, h)
        # and outputs (y). See below.
        self._prevs = []

    def forward(self, x):
        h = self._h # current state of the network

        # this is the exactly the same as concatenating x and h, and then
        # passing it through a single set of weights. If we concatenate two
        # values x, h:          z = x * w1 + h * w2
        # This is the same as:  z = (x * w1) + (h * w2)
        #
        # Which is the form we're using here. Using a different set of weights
        # keeps back propagation a bit simpler.
        x = np.append(x, 1.)
        z = np.dot(self.Wx, x) + np.dot(self.Wh, h)
        y = np.tanh(z)

        # for backprop, we'll need the full history of both inputs (x, h) and
        # the output y.
        self._prevs.append((x, h, y))

        # over hundreds of epochs, we'll end up with thousands of previous
        # values that will (a) consume a huge amount of memory and (b) be slow
        # as we need to iterate over all of them while back-proping. We're
        # truncating the history to only the past 25 instances. This means that
        # our algorithm is limited as it will be unable to discover events that
        # only emerge after more than 25 time steps. It might be better to
        # control this truncation limit via a hyper param.
        self._prevs = self._prevs[-25:] # truncate to the last 25 time steps

        # finally, this layer stores and returns its state. This means that the
        # state doesn't only encapsulate the carry, but also the rest of the
        # meaningful information in its input.
        self._h = y
        return y

    def backward(self, dy, t = 1):
        # back propagation was the most difficult part for me to grasp. Finding
        # the derivatives of our weights involves a slight modification to our
        # normal back propagation algorithm, known as Back Propagation Through
        # Time (BPTT). But really, it's just normal back propagation. Here's
        # how I managed to make sense of it. Basically, the idea is that as we
        # produce new h values, we can keep track of every operation we
        # performed going from the initial h of zeros all the way to the current
        # time step. For example, lets review the forward pass of the first 2
        # inputs (x0, x1):
        #
        #   h0 = zeros
        #   z1 = dot(Wx, x1) + dot(Wh, h0)
        #   y1 = tanh(z1)
        #
        #   h1 = y1
        #   z2 = dot(Wx, x2) + dot(Wh, h1)
        #   y2 = tanh(z2)
        #
        # Now, if we want to back propagate to find the derivative wrt to Wx
        # and Wh:
        #
        #   dy2 = <- given from next layer (softmax)
        #   dz2 = (1 - y2 ** 2) * dy        # backprop tanh
        #   dwx = [x2 * d for d in dz2]
        #   dwh = [h1 * d for d in dz2]
        #
        # Normally, we'd be done. But because of how h1 was computed, it's clear
        # that h1 is also depedent on Wx and Wh. So, unlike x1 which is just a
        # flat input, we need to continue back propagating through time to the
        # previous time-step that produced h1. See above how h1 was initially
        # computed:
        #
        #   z1 = dot(Wx, x1) + dot(Wh, h0)
        #   h1 = y1 = tanh(z1)
        #
        # So we need to back propagate:
        #
        #   dh1 = dot(Wh, dz2)
        #   dz1 = (1 - y1 ** 2) * dh1
        #   dwx = [x1 * d for d in dz1]
        #   dwh = [h0 * d for d in dz1]
        #
        # So we've now computed dwx and dwh twice: once per time-step. We can
        # sum these modifications to find the accumulated influence of Wx and Wh
        # on the final error through all of the time steps. Of course, with more
        # previous events, we'll have more such iterations to back prop. In fact
        # for every time t, we'll need t iterations. So in total, we'll need 1 +
        # 2 + 3 + 4 + ... iterations. By the 100th input, we'll need a total of
        # 4950 iterations. Over time, our learning code will slow to a halt.
        # This is why it's advised to truncate the history and not keep it
        # endless as it grows very big and slow.
        if t > len(self._prevs):
            # stopping condition. we've finished going back in time up to our
            # truncation limit.
            return

        # normal derivation, only for a specific point in time (-t).
        x, h, y = self._prevs[-t]
        dz = (1 - y ** 2) * dy
        dwx = np.array([d * x for d in dz])
        dwh = np.array([d * h for d in dz])
        dx  = np.dot(dz, self.Wx)
        dh  = np.dot(dz, self.Wh)

        self.Wx += ALPHA * -dwx
        self.Wh += ALPHA * -dwh

        # backprop to the previous point in time recursively. we don't care
        # about the returned derivative of x for the previous timestamp. We only
        # need to update the weights proportionaly to the sum of all of the
        # derivatives.
        self.backward(dh, t + 1)

        # only the initial caller, at the first timestep (t=1) is really
        # concerned about the return value. We may choose avoid computing dx
        # unless t == 1.
        return np.delete(dx, -1)

class Linear(object):
    def __init__(self, n, m):
        self.W = np.random.randn(m, n + 1) * 0.01

    def forward(self, x):
        x = np.append(x, 1.) # add the fixed input for bias
        y = np.dot(self.W, x) # derivate: x
        self._x = x
        return y

    def backward(self, dy):
        x = self._x
        dw = np.array([d * x for d in dy])
        dx = np.dot(dy, self.W)
        self.W -= ALPHA * dw
        return np.delete(dx, -1)

class Softmax(Linear):
    def forward(self, x):
        y = super(Softmax, self).forward(x)
        exps = np.exp(y - np.max(y))
        p = exps / np.sum(exps)
        self._p = p
        return p

    def error(self, t):
        p = self._p
        return -np.log(p[np.argmax(t)])

    def backward(self, t):
        p = self._p
        dy = p - t
        return super(Softmax, self).backward(dy)

l1 = Recurrent(6, H)
l2 = Softmax(H, 3)
layers = [l1, l2]
data = zip(X, T)
for i in xrange(EPOCHS):
    e = 0.
    accuracy = 0

    for x, t in data:

        # forward
        y = reduce(lambda x, l: l.forward(x), layers, x)

        # backward
        e += layers[-1].error(t)
        d = reduce(lambda d, l: l.backward(d), reversed(layers), t)

        # update the accuracy
        accuracy += 1 if np.argmax(t) == np.argmax(y) else 0

    e /= len(data)
    accuracy = accuracy * 100 / len(data)
    print "%s: ERROR = %s ; ACCURACY = %s%%" % (i, e, accuracy)

max_stars_repo_name: avinoamr/ai-neural

max_issues_repo_name: avinoamr/learn-ai

max_forks_repo_name: avinoamr/learn-ai


text:
import numpy as np
import os
import sys
import tensorflow as tf

__exec_dir = sys.path[0]
while os.path.basename(__exec_dir) != "src":
	__exec_dir = os.path.dirname(__exec_dir)
	sys.path.insert(0, __exec_dir)

from data import DataProvider
from data.preprocessors import BBoxPreprocessor, DefaultPreprocessor, ImagePreprocessor
from datasets.common.utils import get_dataset
from models.ssd.common.utils import get_model
from utils import ArgumentList, AveragePrecision, Run
from utils.common.logging import logging_error, logging_info, logging_eval
from utils.common.terminal import query_yes_no
from utils.tfu import tfu_get_uninitialized_variables, tfu_set_logging

if __name__ == "__main__":

	# parse arguments
	argument_list = ArgumentList(
		description = "Evaluate model from a run and compute mAP."
	)
	argument_list.add_run_argument("The run from which to evaluate the model.", required=True)
	argument_list.add_dataset_argument("The dataset to use for evaluation.", default="voc2007")
	argument_list.add_dataset_split_argument("The dataset split to use for evaluation.", default="test")
	argument_list.add_num_parallel_calls_argument("Number of parallel calls for preprocessing the data.", default=6)
	argument_list.add_prefetch_buffer_size_argument("Buffer size for prefetching the data.", default=2)
	argument_list.add_batch_size_argument("Batch size for evaluation.", default=32)
	argument_list.add_input_device_argument("Device for processing inputs.", default="/cpu:0")
	argument_list.add_inference_device_argument("Device for inference.", default="/gpu:0")
	argument_list.add_optimization_device_argument("Device for optimization.", default="/cpu:0")
	argument_list.add_tf_verbosity_argument("Tensorflow verbosity.", default="info")
	argument_list.add_tf_min_log_level_argument("Tensorflow minimum log level.", default=3)
	arguments = argument_list.parse()

	# load run
	run = Run(run_id=arguments.run)
	if not run.open():
		logging_error("There is no run '{}'.".format(arguments.run))

	# print some information
	logging_info("Load run '{}'.".format(arguments.run))
	logging_info("Model:                        {}".format(run.get_config_value("model", "name")))
	logging_info("Dataset:                      {} {}".format(arguments.dataset, arguments.dataset_split))
	logging_info("Preprocessing parallel calls: {}".format(arguments.num_parallel_calls))
	logging_info("Prefetch buffer size:         {}".format(arguments.prefetch_buffer_size))
	logging_info("Batch size:                   {}".format(arguments.batch_size))
	logging_info("Input device:                 {}".format(arguments.input_device))
	logging_info("Inference device:             {}".format(arguments.inference_device))
	logging_info("Optimization device:          {}".format(arguments.optimization_device))
	logging_info("Tensorflow verbosity:         {}".format(arguments.tf_verbosity))
	logging_info("Tensorflow minimum log level: {}".format(arguments.tf_min_log_level))

	should_continue = query_yes_no("Continue?", default="yes")
	if not should_continue:
		exit()

	# get dataset
	dataset = get_dataset(arguments.dataset)

	# set verbosity of tensorflow
	tfu_set_logging(arguments.tf_verbosity, min_log_level=arguments.tf_min_log_level)

	# start a new Tensorflow session
	with tf.Session() as session:
		# get and set random seeds
		random_seed = run.get_config_value("model", "random_seed")
		op_random_seed = run.get_config_value("model", "op_random_seed")
		tf.set_random_seed(random_seed)

		# init model
		model = get_model(
			run.get_config_value("model", "name"),
			session,
			op_seed = op_random_seed,
			num_classes = dataset.num_classes + 1
		)

		with tf.device(arguments.input_device):
			# setup data provider
			data_provider = DataProvider(arguments.dataset)
			data_provider.init_dataset(
				arguments.dataset_split,
				batch_size = arguments.batch_size,
				num_parallel_calls = arguments.num_parallel_calls,
				prefetch_buffer_size = arguments.prefetch_buffer_size,
				preprocessor = [
					DefaultPreprocessor(),
					ImagePreprocessor(model.hyperparams["image_shape"], data_augmentation=False),
					BBoxPreprocessor(
						model.get_default_anchor_boxes(),
						model.hyperparams["prior_scaling"],
						model.hyperparams["matching_threshold"]
					)
				],
				features = [
					"image",
					"image/object/bbox",
					"image/object/bbox/label",
					"image/object/encoding/bbox",
					"image/object/encoding/bbox/class",
					"image/object/encoding/bbox/score"
				],
				padded_shapes = {
					"image": model.hyperparams["tf_image_shape"],
					"image/object/bbox": [None, 4],
					"image/object/bbox/label": [None],
					"image/object/encoding/bbox": [None, 4],
					"image/object/encoding/bbox/class": [None],
					"image/object/encoding/bbox/score": [None]
				},
				name = "data_provider"
			)

			# get batch
			batch = data_provider.get(arguments.dataset_split)

		# build model
		model.build_from_scratch(
			weight_decay = 5e-5,
			training = False,
			inference_device = arguments.inference_device,
			optimization_device = arguments.optimization_device
		)

		# initialize variables
		uninitialized_variables = tfu_get_uninitialized_variables(session)
		if len(uninitialized_variables) > 0:
			session.run(tf.variables_initializer(uninitialized_variables))

		# restore model
		saver = tf.train.Saver()
		latest_checkpoint = tf.train.latest_checkpoint(run.checkpoints_path)
		saver.restore(session, latest_checkpoint)

		# setup some other stuff
		average_precision = AveragePrecision(matching_threshold=0.5)

		# evaluation loop
		batch_index = 0
		session.run(data_provider.get_initializer(arguments.dataset_split))
		while True:
			# run a step
			try:
				batch_data = session.run(batch)
				result = session.run({
					"output": model.output
				}, feed_dict={
					model.image_input: batch_data["image"],
					model.groundtruth_classes: batch_data["image/object/encoding/bbox/class"],
					model.groundtruth_scores: batch_data["image/object/encoding/bbox/score"],
					model.groundtruth_localizations: batch_data["image/object/encoding/bbox"]
				})
			except tf.errors.OutOfRangeError:
				break

			# add predictions for average precision calculation
			average_precision.add_predictions(
				batch_data["image/object/bbox/label"],
				batch_data["image/object/bbox"],
				result["output"]["classes"],
				result["output"]["scores"],
				result["output"]["localizations"]
			)

			logging_eval("Processed batch #{}.".format(batch_index + 1))
			batch_index += 1

		# compute average precision values
		logging_eval("Matching predictions with groundtruth values.")
		cl_precision, cl_recall = average_precision.precision_recall()
		cl_ap = average_precision.average_precision(cl_precision, cl_recall)
		for cl in sorted(cl_ap):
			logging_eval("AP[cl={}] = {}".format(cl, cl_ap[cl]))
		mAP = np.mean(list(cl_ap.values()))
		logging_eval("mAP = {}".format(mAP))

		logging_eval("Successfully finished evaluation.")

ext: py

max_stars_repo_name: PranavEranki/SSD-Algorithm

max_issues_repo_name: PranavEranki/SSD-Algorithm

max_forks_repo_name: PranavEranki/SSD-Algorithm

reason: import numpy


text:
# -*- coding: utf-8 -*-
# !/usr/bin/python
"""
Created on Mar 18th 10:58:37 2016

test a continuous-time sequential model

@author: hongyuan
"""

import pickle
import time
import numpy
import theano
from theano import sandbox
import theano.tensor as tensor
import os
import sys
from collections import defaultdict
from theano.tensor.shared_randomstreams import RandomStreams
import modules.utils as utils
import modules.models as models
import modules.optimizers as optimizers
import modules.controllers as controllers
import modules.data_processers as data_processers

import run_models
import datetime

dtype=theano.config.floatX

#
import argparse
__author__ = 'Hongyuan Mei'

def main():

    parser = argparse.ArgumentParser(
        description='Testing model ... '
    )
    #
    parser.add_argument(
        '-m', '--Model', required=True,
        choices = ['hawkes', 'hawkesinhib', 'conttime'],
        help='Which model to test? hawkes (SE-MPP)? hawkesinhib (D-SM-MPP)? conttime (N-SM-MPP)?'
    )
    parser.add_argument(
        '-fd', '--FileData', required=True,
        help='Path of the dataset (e.g. ./data/data_hawkes/)'
    )
    parser.add_argument(
        '-fp', '--FilePretrain', required=True,
        help='File of pretrained model (e.g. ./tracks/track_PID=XX_TIME=YY/model.pkl)'
    )
    parser.add_argument(
        '-ts', '--TagSplit', required=True,
        choices = ['dev', 'test', 'test1'],
        help='Which split to test? Dev or Test?'
    )
    #
    parser.add_argument(
        '-s', '--Seed', #required=False,
        default = 12345, type = int,
        help='Seed of random state'
    )
    parser.add_argument(
        '-md', '--MultipleDev', #required=False,
        default = 10, type = int,
        help='Multiple of events to sample (integral) for dev/test'
    )
    parser.add_argument(
        '-sl', '--SaveLog', #required=False,
        default = 1, type = int,
        choices = [0,1],
        help='Do you want to save the log ? 0--False, 1--True'
    )
    parser.add_argument(
        '-pp', '--PartialPredict', #required=False,
        default = 0, type = int,
        choices = [0,1],
        help='What to only predict part of stream ? 0--False, 1--True'
    )
    parser.add_argument(
        '-ps', '--PruneStream', #required=False,
        default = 0, type = int,
        help='Prune stream? Give me the index ! 0 is nothng to prune. Note : index specifies a COMBINATION of event types by its binary coding (e.g. 0--00000, 1--00001, 31-11111 where 1 means this type is pruned)!'
    )
    parser.add_argument(
        '-pf', '--PredictFirst', #required=False,
        default = 1, type = int,
        choices = [0,1],
        help='Predict the first event ? 0--False, 1--True Note: in our project, this is False ONLY on MIMIC, SO and Financial datasets'
    )
    #
    #TODO: related to intensity eval
    parser.add_argument(
        '-pl', '--PredictLambda', #required=False,
        default = 0, type = int,
        choices = [0,1],
        help='Predict Lambda (intensity) ? 0--False, 1--True Note: this is used ONLY in intensity evaluation'
    )
    parser.add_argument(
        '-fg', '--FileGold', required=False,
        help='Gold model to eval intensity ? (e.g. ./gold_models/model.pkl)'
    )
    parser.add_argument(
        '-mg', '--ModelGold', required=False,
        choices = ['hawkes', 'hawkesinhib', 'conttime'],
        help='Gold Model to be used '
    )
    #
    args = parser.parse_args()
    #
    #if args.TrackPeriod == None:
    #    args.TrackPeriod = numpy.int32(100)
    #else:
    #    args.TrackPeriod = numpy.int32(args.TrackPeriod)
    #
    #
    args.Seed = numpy.int32(args.Seed)
    args.MultipleDev = numpy.int32(args.MultipleDev)
    if args.SaveLog == 0:
        args.SaveLog = False
    else:
        args.SaveLog = True
    #
    if args.PartialPredict == 0:
        args.PartialPredict = False
    else:
        args.PartialPredict = True
    #
    args.PruneStream = numpy.int32(args.PruneStream)
    #
    if args.PredictFirst == 0:
        args.PredictFirst = False
    else:
        args.PredictFirst = True
    #
    # For intensity eval
    #
    if args.PredictLambda == 0:
        args.PredictLambda = False
    else:
        args.PredictLambda = True
    #
    if args.FileGold == None:
        assert(args.PredictLambda==False)
    else:
        args.FileGold = str(args.FileGold)
    if args.ModelGold == None:
        assert(args.PredictLambda==False)
    else:
        args.ModelGold = str(args.ModelGold)
    #
    #
    id_process = os.getpid()
    time_current = datetime.datetime.now().isoformat()
    #
    ## show values ##
    print ("PID is : %s" % str(id_process) )
    print ("TIME is : %s" % time_current )
    print ("Model is : %s" % args.Model )
    #print ("CoefL2 is : %s" % str(args.CoefL2) )
    print ("FileData is : %s" % args.FileData )
    #if 'lstm' in args.Model:
    #    print ("DimLSTM is : %s" % str(args.DimLSTM) )
    print ("Seed is : %s" % str(args.Seed) )
    print ("FilePretrain is : %s" % args.FilePretrain)
    #print ("TrackPeriod is : %s" % str(args.TrackPeriod) )
    #print ("MaxEpoch is : %s" % str(args.MaxEpoch) )
    print ("SizeBatch is : %s" % str(1) )
    print ("PartialPredict is : %s" % args.PartialPredict)
    print ("PruneStream is : %s" % str(args.PruneStream) )
    print ("PredictFirst is: %s" % args.PredictFirst )
    print ("PredictLambda is : %s" % str(args.PredictLambda) )
    print ("ModelGold is : %s" % args.ModelGold )
    print ("FileGold is : %s" % args.FileGold )
    #print ("Optimizer is : %s" % args.Optimizer)
    flag_show_1 = (
        args.Model == 'hawkesinhib' or args.Model == 'neural' or args.Model == 'neuralgeneral' or args.Model == 'neuraladapt' or args.Model == 'neuralsimple' or args.Model == 'neuraltime' or args.Model == 'neuralgeneraltime' or args.Model == 'neuraladapttime'
    )
    flag_show_2 = (
        args.Model == 'hawkesinhibscale' or args.Model == 'neuraladapttimescale' or args.Model == 'neuralreduce' or args.Model == 'conttime'
    )
    #
    if (flag_show_1 and flag_show_2):
        print ("Multiple for testing is : %s" % args.MultipleDev)
    #
    #
    dict_args = {
        'PID': id_process,
        'TIME': time_current,
        'Model': args.Model,
        #'CoefL2': args.CoefL2,
        'FileData': args.FileData,
        #'DimLSTM': args.DimLSTM,
        'Seed': args.Seed,
        'FilePretrain': args.FilePretrain,
        #'TrackPeriod': args.TrackPeriod,
        #'MaxEpoch': args.MaxEpoch,
        'SizeBatch': numpy.int32(1),
        #'ConfidenceLevel': args.ConfidenceLevel,
        #'NumBootstrap': args.NumBootstrap,
        #'Optimizer': args.Optimizer,
        #'MultipleTrain': args.MultipleTrain,
        'MultipleDev': args.MultipleDev,
        'PartialPredict': args.PartialPredict,
        'PruneStream': args.PruneStream,
        'PredictLambda': args.PredictLambda,
        'ModelGold': args.ModelGold,
        'FileGold': args.FileGold
    }
    #
    #some unuseful arguments are set to dummy values here
    #like optimizer, max_epoch, size_batch
    input_test = {
        'model': args.Model,
        'seed_random': args.Seed,
        'path_rawdata': args.FileData,
        'path_pre_train': args.FilePretrain,
        'track_period': None,
        'max_epoch': numpy.int32(1),
        'size_batch': numpy.int32(1),
        #'dim_model': args.DimLSTM,
        'optimizer': 'adam',
        'save_file_path': None,
        'log_file': None,
        'tag_split': args.TagSplit,
        'args': dict_args,
        'coef_l2': numpy.float32(0.0),
        #'cl': args.ConfidenceLevel,
        #'num_bootstrap': args.NumBootstrap,
        'loss_type': 'loglikehood',
        'partial_predict': args.PartialPredict,
        'prune_stream': args.PruneStream,
        'predict_lambda': args.PredictLambda,
        'path_gold': args.FileGold,
        'model_gold': args.ModelGold
    }
    #
    if '_so' in args.FileData or '_mimic' in args.FileData or '_bookorder' in args.FileData:
        input_test['predict_first'] = False
    else:
        if args.PredictFirst:
            input_test['predict_first'] = True
        else:
            input_test['predict_first'] = False
    #
    #
    path_pre_train = input_test['path_pre_train']
    path_with_no_name = path_pre_train.replace(
        '/'+os.path.basename(path_pre_train), ''
    )
    tag_with_track = os.path.basename(path_with_no_name)
    tag_model = tag_with_track.replace(
        'track', ''
    )
    #
    input_test['path_logs'] = path_with_no_name
    if args.SaveLog == False:
        input_test['path_logs'] = None
    # code drifts with time
    # when model is trained model,
    # tag_model is PID and TIME
    # but when model is gen_model
    # tag_model is just gen_model the string
    # so we will add _ to make the format match
    #if '_' not in tag_model:
    if tag_model[0] != '_':
        tag_model = '_' + tag_model
    #
    if '_hawkes' in input_test['path_rawdata']:
        tag_data = 'hawkes'
    if '_hawkesinhib' in input_test['path_rawdata']:
        tag_data = 'hawkesinhib'
    if '_neural' in input_test['path_rawdata']:
        tag_data = 'neural'
    if '_neuralgeneral' in input_test['path_rawdata']:
        tag_data = 'neuralgeneral'
    if '_neuraladapt' in input_test['path_rawdata']:
        tag_data = 'neuraladapt'
    if '_neuralsimple' in input_test['path_rawdata']:
        tag_data = 'neuralsimple'
    if 'data_kaggle' in input_test['path_rawdata']:
        tag_data = 'kaggle'
    if 'data_retweet' in input_test['path_rawdata']:
        tag_data = 'retweet'
    if 'data_food' in input_test['path_rawdata']:
        tag_data = 'food'
    if 'data_meme' in input_test['path_rawdata']:
        tag_data = 'meme'
    if 'data_mimic' in input_test['path_rawdata']:
        tag_data = 'mimic'
    #
    if 'data_so' in input_test['path_rawdata']:
        tag_data = 'so'
    if 'data_bookorder' in input_test['path_rawdata']:
        tag_data = 'bookorder'
    #
    if 'data_neuraladapttimescale' in input_test['path_rawdata']:
        tag_data = 'neuraladapttimescale'
    if 'data_neuralreduce' in input_test['path_rawdata']:
        tag_data = 'neuralreduce'
    if 'data_missing' in input_test['path_rawdata']:
        tag_data = 'missing'
    #
    if 'data_conttime' in input_test['path_rawdata']:
        tag_data = 'conttime'
    #
    name_model = 'results_Model='+input_test[
        'args'
    ]['Model']+'_Data='+tag_data+'_Split='+input_test[
        'tag_split'
    ]+tag_model+'.pkl'
    #
    path_to_save_results = os.path.abspath(
        './results_'+tag_data
    )
    if args.PredictLambda:
        path_to_save_results += '_predictlambda'
    file_to_save_results = path_to_save_results + '/' + name_model
    #
    if not os.path.exists(path_to_save_results):
        os.makedirs(path_to_save_results)
    #
    input_test['file_to_save_results'] = file_to_save_results
    #
    flag_multiple_1 = (
        args.Model == 'hawkesinhib' or args.Model == 'neural' or args.Model == 'neuralgeneral' or args.Model == 'neuraladapt' or args.Model == 'neuralsimple' or args.Model == 'neuraltime' or args.Model == 'neuralgeneraltime' or args.Model == 'neuraladapttime'
    )
    flag_multiple_2 = (
        args.Model == 'hawkesinhibscale' or args.Model == 'neuraladapttimescale' or args.Model == 'neuralreduce' or args.Model == 'conttime'
    )
    #
    if (flag_multiple_1 or flag_multiple_2):
        input_test['multiple_sample_for_train'] = numpy.int32(1)
        input_test['multiple_sample_for_dev'] = numpy.int32(
            args.MultipleDev
        )
    #
    #
    #
    # start running
    if args.PredictLambda:
        run_models.test_intensity_and_save(input_test)
    else:
        if args.Model == 'hawkes':
            run_models.test_hawkes_ctsm_and_save(
                input_test
            )
        elif args.Model == 'hawkesinhib' or args.Model == 'hawkesinhibscale':
            run_models.test_hawkesinhib_ctsm_and_save(
                input_test
            )
            #run_models.test_hawkesinhib_ctsm_confidence_interval(input_test)
        elif args.Model == 'neural':
            run_models.test_neural_hawkes_ctsm_and_save(
                input_test
            )
            #run_models.test_neural_hawkes_ctsm_confidence_interval(input_test)
        elif args.Model == 'neuralgeneral':
            run_models.test_generalized_neural_hawkes_ctsm_and_save(
                input_test, tag_neural_type = 'general'
            )
        elif args.Model == 'neuraladapt':
            run_models.test_generalized_neural_hawkes_ctsm_and_save(
                input_test, tag_neural_type = 'adaptive'
            )
        elif args.Model == 'neuralsimple':
            run_models.test_generalized_neural_hawkes_ctsm_and_save(
                input_test, tag_neural_type = 'simple'
            )
        elif args.Model == 'neuraltime':
            run_models.test_generalized_neural_hawkes_ctsm_and_save_time(
                input_test, tag_neural_type = 'neural'
            )
        elif args.Model == 'neuralgeneraltime':
            run_models.test_generalized_neural_hawkes_ctsm_and_save_time(
                input_test, tag_neural_type = 'general'
            )
        elif args.Model == 'neuraladapttime' or args.Model == 'neuraladapttimescale' or args.Model == 'neuralreduce' or args.Model == 'conttime':
            run_models.test_generalized_neural_hawkes_ctsm_and_save_time(
                input_test, tag_neural_type='adaptive'
            )
            #if args.PredictLambda:
            #    run_models.test_generalized_neural_hawkes_intensity_and_save_time(
            #        input_test, tag_neural_type='adaptive'
            #    )
            #else:
        else:
            print "Model not implemented yet !!! "
        #
    #

if __name__ == "__main__": main()

max_stars_repo_name: HMEIatJHU/neurawkes

max_issues_repo_name: hongyuanmei/neurawkes

max_forks_repo_name: HMEIatJHU/neurawkes

reason: import numpy,import theano,from theano


text:
import param
import bokeh.plotting
from bokeh.plotting import show
import holoviews as hv
import panel as pn
import numpy as np
import bokeh.io
from erddapy import ERDDAP
hv.extension(bokeh)

class A(param.Parameterized):
    title = param.String(default="sum", doc="Title for the result")

class B(A):
    a = param.Integer( bounds=(0, 10), doc="First addend")
    b = param.Integer(3, bounds=(0, 10), doc="Second addend")

    def __call__(self):
        return print(self.title + ": " + str(self.a + self.b))
object = B(b=5, title="this here is")
object.a=10
object()

opts = dict(show_grid=True,  color="#1f77b3")

mu_slider = pn.widgets.FloatSlider(
    name="m", start=-5, end=5, step=0.1, value=0
)
sigma_slider = pn.widgets.FloatSlider(
    name="b", start=0.1, end=5, step=0.1, value=1
)

pn.extension()

@pn.depends(mu_slider.param.value, sigma_slider.param.value)
def plot_normal_pdf(mu=0, sigma=1):
    x = np.linspace(0, 100, 200, endpoint=True)
    y = mu+sigma*x
    return hv.Curve(data=(x, y)).opts(
        **opts
    )
#plot_normal_pdf(0, 1)
widgets = pn.Column(
    pn.Spacer(height=30),
    mu_slider,
    pn.Spacer(height=15),
    sigma_slider,
    width=200,
)
plotnormalpdfPanel = pn.Row(plot_normal_pdf, pn.Spacer(width=15), widgets)
plotnormalpdfPanel.servable()

max_stars_repo_path: W2/param examples.py

max_stars_repo_name: munroelab/cioos-viz

max_issues_repo_path: W2/param examples.py

max_issues_repo_name: munroelab/cioos-viz

max_forks_repo_path: W2/param examples.py

max_forks_repo_name: munroelab/cioos-viz


text:
from __future__ import division
import abc
import numpy as np
from numpy.fft import fftn, ifftn, fft2
import scipy.linalg

from menpo.feature import gradient

# TODO: Do we want residuals to support masked templates?
class Residual(object):
    """
    An abstract base class for calculating the residual between two images
    within the Lucas-Kanade algorithm. The classes were designed
    specifically to work within the Lucas-Kanade framework and so no
    guarantee is made that calling methods on these subclasses will generate
    correct results.
    """
    @classmethod
    def gradient(cls, image, forward=None):
        r"""
        Calculates the gradients of the given method.

        If `forward` is provided, then the gradients are warped
        (as required in the forward additive algorithm)

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the gradients for
        forward : `tuple` or ``None``, optional
            A `tuple` containing the extra weights required for the function
            `warp` (which should be passed as a function handle), i.e.
            ``(`menpo.image.Image`, `menpo.transform.AlignableTransform>`)``. If
            ``None``, then the optimization algorithm is assumed to be inverse.
        """
        if forward:
            # Calculate the gradient over the image
            # grad:  (dims x ch) x H x W
            grad = gradient(image)
            # Warp gradient for forward additive using the given transform
            # grad:  (dims x ch) x h x w
            template, transform = forward
            grad = grad.warp_to_mask(template.mask, transform,
                                     warp_landmarks=False)
        else:
            # Calculate the gradient over the image and set one pixels along
            # the boundary of the image mask to zero (no reliable gradient
            # can be computed there!)
            # grad:  (dims x ch) x h x w
            grad = gradient(image)
            grad.set_boundary_pixels()
        return grad

    @abc.abstractmethod
    def steepest_descent_images(self, image, dW_dp, **kwargs):
        r"""
        Calculates the standard steepest descent images.

        Within the forward additive framework this is defined as

        .. math::
             \nabla I \frac{\partial W}{\partial p}

        The input image is vectorised (`N`-pixels) so that masked images can
        be handled.

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the steepest descent images from, could be
            either the template or input image depending on which framework is
            used.
        dW_dp : `ndarray`
            The Jacobian of the warp.

        Returns
        -------
        VT_dW_dp : ``(N, n_params)`` `ndarray`
            The steepest descent images
        """
        pass

    @abc.abstractmethod
    def hessian(self, sdi):
        r"""
        Calculates the Gauss-Newton approximation to the Hessian.

        This is abstracted because some residuals expect the Hessian to be
        pre-processed. The Gauss-Newton approximation to the Hessian is
        defined as:

        .. math::
            \mathbf{J J^T}

        Parameters
        ----------
        J : ``(N, n_params)`` `ndarray`
            The steepest descent images.

        Returns
        -------
        H : ``(n_params, n_params)`` `ndarray`
            The approximation to the Hessian
        """
        pass

    @abc.abstractmethod
    def steepest_descent_update(self, sdi, image, template):
        r"""
        Calculates the steepest descent parameter updates.

        These are defined, for the forward additive algorithm, as:

        .. math::
            \sum_x [ \nabla I \frac{\partial W}{\partial p} ]^T [ T(x) - I(W(x;p)) ]

        Parameters
        ----------
        J : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        image : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)
        template : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)

        Returns
        -------
        sd_delta_p : ``(n_params,)`` `ndarray`
            The steepest descent parameter updates.
        """
        pass

    @abc.abstractmethod
    def cost_closure(self):
        r"""
        Method to compute the optimization cost.

        Returns
        -------
        cost : `float`
            The cost value.
        """
        pass

class SSD(Residual):
    r"""
    Class for Sum of Squared Differences residual.

    References
    ----------
    .. [1] B.D. Lucas, and T. Kanade, "An iterative image registration
        technique with an application to stereo vision", International Joint
        Conference on Artificial Intelligence, pp. 674-679, 1981.
    """
    def __init__(self, kernel=None):
        self._kernel = kernel

    def steepest_descent_images(self, image, dW_dp, forward=None):
        r"""
        Calculates the standard steepest descent images.

        Within the forward additive framework this is defined as

        .. math::
             \nabla I \frac{\partial W}{\partial p}

        The input image is vectorised (`N`-pixels) so that masked images can
        be handled.

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the steepest descent images from, could be
            either the template or input image depending on which framework is
            used.
        dW_dp : `ndarray`
            The Jacobian of the warp.
        forward : `tuple` or ``None``, optional
            A `tuple` containing the extra weights required for the function
            `warp` (which should be passed as a function handle), i.e.
            ``(`menpo.image.Image`, `menpo.transform.AlignableTransform>`)``. If
            ``None``, then the optimization algorithm is assumed to be inverse.

        Returns
        -------
        VT_dW_dp : ``(N, n_params)`` `ndarray`
            The steepest descent images
        """
        # compute gradient
        # grad:  dims x ch x h x w
        nabla = self.gradient(image, forward=forward)
        nabla = nabla.as_vector().reshape((image.n_dims, image.n_channels) +
                                          nabla.shape)

        # compute steepest descent images
        # gradient: dims x ch x h x w
        # dw_dp:    dims x    x h x w x params
        # sdi:             ch x h x w x params
        sdi = 0
        a = nabla[..., None] * dW_dp[:, None, ...]
        for d in a:
            sdi += d

        if self._kernel is None:
            # reshape steepest descent images
            # sdi:           (ch x h x w) x params
            # filtered_sdi:  (ch x h x w) x params
            sdi = sdi.reshape((-1, sdi.shape[-1]))
            filtered_sdi = sdi
        else:
            # if required, filter steepest descent images
            # fft_sdi:  ch x h x w x params
            filtered_sdi = ifftn(self._kernel[..., None] *
                                 fftn(sdi, axes=(-3, -2)),
                                 axes=(-3, -2))
            # reshape steepest descent images
            # sdi:           (ch x h x w) x params
            # filtered_sdi:  (ch x h x w) x params
            sdi = sdi.reshape((-1, sdi.shape[-1]))
            filtered_sdi = filtered_sdi.reshape(sdi.shape)

        return filtered_sdi, sdi

    def hessian(self, sdi, sdi2=None):
        r"""
        Calculates the Gauss-Newton approximation to the Hessian.

        This is abstracted because some residuals expect the Hessian to be
        pre-processed. The Gauss-Newton approximation to the Hessian is
        defined as:

        .. math::
            \mathbf{J J^T}

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        sdi2 : ``(N, n_params)`` `ndarray` or ``None``, optional
            The steepest descent images.

        Returns
        -------
        H : ``(n_params, n_params)`` `ndarray`
            The approximation to the Hessian
        """
        # compute hessian
        # sdi.T:   params x (ch x h x w)
        # sdi:              (ch x h x w) x params
        # hessian: params x               x params
        if sdi2 is None:
            H = sdi.T.dot(sdi)
        else:
            H = sdi.T.dot(sdi2)
        return H

    def steepest_descent_update(self, sdi, image, template):
        r"""
        Calculates the steepest descent parameter updates.

        These are defined, for the forward additive algorithm, as:

        .. math::
            \sum_x [ \nabla I \frac{\partial W}{\partial p} ]^T [ T(x) - I(W(x;p)) ]

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        image : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)
        template : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)

        Returns
        -------
        sd_delta_p : ``(n_params,)`` `ndarray`
            The steepest descent parameter updates.
        """
        self._error_img = image.as_vector() - template.as_vector()
        return sdi.T.dot(self._error_img)

    def cost_closure(self):
        r"""
        Method to compute the optimization cost.

        Returns
        -------
        cost : `float`
            The cost value.
        """
        def cost_closure(x, k):
            if k is None:
                return lambda: x.T.dot(x)
            else:
                x = x.reshape((-1,) + k.shape[-2:])
                kx = ifftn(k[..., None] * fftn(x, axes=(-2, -1)),
                           axes=(-2, -1))
                return lambda: x.ravel().T.dot(kx.ravel())
        return cost_closure(self._error_img, self._kernel)

    def __str__(self):
        return "Sum of Squared Differences Residual"

# TODO: Does not support masked templates at the moment
class FourierSSD(Residual):
    r"""
    Class for Sum of Squared Differences on the Fourier domain residual.

    References
    ----------
    .. [1] A.B. Ashraf, S. Lucey, and T. Chen. "Fast Image Alignment in the
        Fourier Domain", IEEE Proceedings of International Conference on
        Computer Vision and Pattern Recognition, pp. 2480-2487, 2010.
    """
    def __init__(self, kernel=None):
        self._kernel = kernel

    def steepest_descent_images(self, image, dW_dp, forward=None):
        r"""
        Calculates the standard steepest descent images.

        Within the forward additive framework this is defined as

        .. math::
             \nabla I \frac{\partial W}{\partial p}

        The input image is vectorised (`N`-pixels) so that masked images can
        be handled.

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the steepest descent images from, could be
            either the template or input image depending on which framework is
            used.
        dW_dp : `ndarray`
            The Jacobian of the warp.
        forward : `tuple` or ``None``, optional
            A `tuple` containing the extra weights required for the function
            `warp` (which should be passed as a function handle), i.e.
            ``(`menpo.image.Image`, `menpo.transform.AlignableTransform>`)``. If
            ``None``, then the optimization algorithm is assumed to be inverse.

        Returns
        -------
        VT_dW_dp : ``(N, n_params)`` `ndarray`
            The steepest descent images
        """
        # compute gradient
        # grad:  dims x ch x h x w
        nabla = self.gradient(image, forward=forward)
        nabla = nabla.as_vector().reshape((image.n_dims, image.n_channels) +
                                          nabla.shape)

        # compute steepest descent images
        # gradient: dims x ch x h x w
        # dw_dp:    dims x    x h x w x params
        # sdi:             ch x h x w x params
        sdi = 0
        a = nabla[..., None] * dW_dp[:, None, ...]
        for d in a:
            sdi += d

        # compute steepest descent images fft
        # fft_sdi:  ch x h x w x params
        fft_sdi = fftn(sdi, axes=(-3, -2))

        if self._kernel is None:
            # reshape steepest descent images
            # fft_sdi:           (ch x h x w) x params
            # filtered_fft_sdi:  (ch x h x w) x params
            fft_sdi = fft_sdi.reshape((-1, fft_sdi.shape[-1]))
            filtered_fft_sdi = fft_sdi
        else:
            # if required, filter steepest descent images
            filtered_fft_sdi = self._kernel[..., None] * fft_sdi
            # reshape steepest descent images
            # fft_sdi:           (ch x h x w) x params
            # filtered_fft_sdi:  (ch x h x w) x params
            fft_sdi = fft_sdi.reshape((-1, fft_sdi.shape[-1]))
            filtered_fft_sdi = filtered_fft_sdi.reshape(fft_sdi.shape)

        return filtered_fft_sdi, fft_sdi

    def hessian(self, sdi, sdi2=None):
        r"""
        Calculates the Gauss-Newton approximation to the Hessian.

        This is abstracted because some residuals expect the Hessian to be
        pre-processed. The Gauss-Newton approximation to the Hessian is
        defined as:

        .. math::
            \mathbf{J J^T}

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        sdi2 : ``(N, n_params)`` `ndarray` or ``None``, optional
            The steepest descent images.

        Returns
        -------
        H : ``(n_params, n_params)`` `ndarray`
            The approximation to the Hessian
        """
        if sdi2 is None:
            H = sdi.conjugate().T.dot(sdi)
        else:
            H = sdi.conjugate().T.dot(sdi2)
        return H

    def steepest_descent_update(self, sdi, image, template):
        r"""
        Calculates the steepest descent parameter updates.

        These are defined, for the forward additive algorithm, as:

        .. math::
            \sum_x [ \nabla I \frac{\partial W}{\partial p} ]^T [ T(x) - I(W(x;p)) ]

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        image : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)
        template : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)

        Returns
        -------
        sd_delta_p : ``(n_params,)`` `ndarray`
            The steepest descent parameter updates.
        """
        # compute error image
        # error_img:  ch x h x w
        self._error_img = image.pixels - template.pixels

        # compute error image fft
        # fft_error_img:  ch x (h x w)
        fft_error_img = fft2(self._error_img)

        # compute steepest descent update
        # fft_sdi:        params x (ch x h x w)
        # fft_error_img:           (ch x h x w)
        # fft_sdu:        params
        return sdi.conjugate().T.dot(fft_error_img.ravel())

    def cost_closure(self):
        r"""
        Method to compute the optimization cost.

        Returns
        -------
        cost : `float`
            The cost value.
        """
        def cost_closure(x, k):
            if k is None:
                return lambda: x.ravel().T.dot(x.ravel())
            else:
                kx = ifftn(k[..., None] * fftn(x, axes=(-2, -1)),
                           axes=(-2, -1))
                return lambda: x.ravel().T.dot(kx.ravel())
        return cost_closure(self._error_img, self._kernel)

    def __str__(self):
        return "Fourier Sum of Squared Differences Residual"

class ECC(Residual):
    r"""
    Class for Enhanced Correlation Coefficient residual.

    References
    ----------
    .. [1] G.D. Evangelidis, and E.Z. Psarakis. "Parametric Image Alignment
        Using Enhanced Correlation Coefficient Maximization", IEEE Transactions
        on Pattern Analysis and Machine Intelligence, 30(10): 1858-1865, 2008.
    """
    def _normalise_images(self, image):
        # TODO: do we need to copy the image?
        # TODO: is this supposed to be per channel normalization?
        norm_image = image.copy()
        norm_image.normalize_norm_inplace()
        return norm_image

    def steepest_descent_images(self, image, dW_dp, forward=None):
        r"""
        Calculates the standard steepest descent images.

        Within the forward additive framework this is defined as

        .. math::
             \nabla I \frac{\partial W}{\partial p}

        The input image is vectorised (`N`-pixels) so that masked images can
        be handled.

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the steepest descent images from, could be
            either the template or input image depending on which framework is
            used.
        dW_dp : `ndarray`
            The Jacobian of the warp.
        forward : `tuple` or ``None``, optional
            A `tuple` containing the extra weights required for the function
            `warp` (which should be passed as a function handle), i.e.
            ``(`menpo.image.Image`, `menpo.transform.AlignableTransform>`)``. If
            ``None``, then the optimization algorithm is assumed to be inverse.

        Returns
        -------
        VT_dW_dp : ``(N, n_params)`` `ndarray`
            The steepest descent images
        """
        # normalize image
        norm_image = self._normalise_images(image)

        # compute gradient
        # gradient:  dims x ch x pixels
        grad = self.gradient(norm_image, forward=forward)
        grad = grad.as_vector().reshape((image.n_dims, image.n_channels) +
                                         grad.shape)

        # compute steepest descent images
        # gradient: dims x ch x pixels
        # dw_dp:    dims x    x pixels x params
        # sdi:             ch x pixels x params
        sdi = 0
        a = grad[..., None] * dW_dp[:, None, ...]
        for d in a:
            sdi += d

        # reshape steepest descent images
        # sdi: (ch x pixels) x params
        sdi = sdi.reshape((-1, sdi.shape[-1]))

        return sdi, sdi

    def hessian(self, sdi, sdi2=None):
        r"""
        Calculates the Gauss-Newton approximation to the Hessian.

        This is abstracted because some residuals expect the Hessian to be
        pre-processed. The Gauss-Newton approximation to the Hessian is
        defined as:

        .. math::
            \mathbf{J J^T}

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        sdi2 : ``(N, n_params)`` `ndarray` or ``None``, optional
            The steepest descent images.

        Returns
        -------
        H : ``(n_params, n_params)`` `ndarray`
            The approximation to the Hessian
        """
        # compute hessian
        # sdi.T:   params x (ch x h x w)
        # sdi:              (ch x h x w) x params
        # hessian: params x               x params
        if sdi2 is None:
            H = sdi.T.dot(sdi)
        else:
            H = sdi.T.dot(sdi2)
        self._H_inv = scipy.linalg.inv(H)
        return H

    def steepest_descent_update(self, sdi, image, template):
        r"""
        Calculates the steepest descent parameter updates.

        These are defined, for the forward additive algorithm, as:

        .. math::
            \sum_x [ \nabla I \frac{\partial W}{\partial p} ]^T [ T(x) - I(W(x;p)) ]

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        image : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)
        template : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)

        Returns
        -------
        sd_delta_p : ``(n_params,)`` `ndarray`
            The steepest descent parameter updates.
        """
        self._normalised_IWxp = self._normalise_images(image).as_vector()
        self._normalised_template = self._normalise_images(
            template).as_vector()

        Gt = sdi.T.dot(self._normalised_template)
        Gw = sdi.T.dot(self._normalised_IWxp)

        # Calculate the numerator
        IWxp_norm = scipy.linalg.norm(self._normalised_IWxp)
        num1 = IWxp_norm ** 2
        num2 = np.dot(Gw.T, np.dot(self._H_inv, Gw))
        num = num1 - num2

        # Calculate the denominator
        den1 = np.dot(self._normalised_template, self._normalised_IWxp)
        den2 = np.dot(Gt.T, np.dot(self._H_inv, Gw))
        den = den1 - den2

        # Calculate lambda to choose the step size
        # Avoid division by zero
        if den > 0:
            l = num / den
        else:
            den3 = np.dot(Gt.T, np.dot(self._H_inv, Gt))
            l1 = np.sqrt(num2 / den3)
            l2 = - den / den3
            l = np.maximum(l1, l2)

        self._error_img = l * self._normalised_IWxp - self._normalised_template

        return sdi.T.dot(self._error_img)

    def cost_closure(self):
        r"""
        Method to compute the optimization cost.

        Returns
        -------
        cost : `float`
            The cost value.
        """
        def cost_closure(x, y):
            return lambda: x.T.dot(y)
        return cost_closure(self._normalised_IWxp, self._normalised_template)

    def __str__(self):
        return "Enhanced Correlation Coefficient Residual"

class GradientImages(Residual):
    r"""
    Class for Gradient Images residual.

    References
    ----------
    .. [1] G. Tzimiropoulos, S. Zafeiriou, and M. Pantic. "Robust and
        Efficient Parametric Face Alignment", IEEE Proceedings of International
        Conference on Computer Vision (ICCV), pp. 1847-1854, November 2011.
    """
    def _regularise_gradients(self, grad):
        pixels = grad.pixels
        ab = np.sqrt(np.sum(pixels**2, axis=0))
        m_ab = np.median(ab)
        ab = ab + m_ab
        grad.pixels = pixels / ab
        return grad

    def steepest_descent_images(self, image, dW_dp, forward=None):
        r"""
        Calculates the standard steepest descent images.

        Within the forward additive framework this is defined as

        .. math::
             \nabla I \frac{\partial W}{\partial p}

        The input image is vectorised (`N`-pixels) so that masked images can
        be handled.

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the steepest descent images from, could be
            either the template or input image depending on which framework is
            used.
        dW_dp : `ndarray`
            The Jacobian of the warp.
        forward : `tuple` or ``None``, optional
            A `tuple` containing the extra weights required for the function
            `warp` (which should be passed as a function handle), i.e.
            ``(`menpo.image.Image`, `menpo.transform.AlignableTransform>`)``. If
            ``None``, then the optimization algorithm is assumed to be inverse.

        Returns
        -------
        VT_dW_dp : ``(N, n_params)`` `ndarray`
            The steepest descent images
        """
        n_dims = image.n_dims
        n_channels = image.n_channels

        # compute gradient
        first_grad = self.gradient(image, forward=forward)
        self._template_grad = self._regularise_gradients(first_grad)

        # compute gradient
        # second_grad:  dims x dims x ch x pixels
        second_grad = self.gradient(self._template_grad)
        second_grad = second_grad.masked_pixels().flatten().reshape(
            (n_dims, n_dims,  n_channels) + second_grad.shape)

        # Fix crossed derivatives: dydx = dxdy
        second_grad[1, 0, ...] = second_grad[0, 1, ...]

        # compute steepest descent images
        # gradient: dims x dims x ch x (h x w)
        # dw_dp:    dims x           x (h x w) x params
        # sdi:             dims x ch x (h x w) x params
        sdi = 0
        a = second_grad[..., None] * dW_dp[:, None, None, ...]
        for d in a:
            sdi += d

        # reshape steepest descent images
        # sdi: (ch x pixels) x params
        sdi = sdi.reshape((-1, sdi.shape[-1]))

        return sdi, sdi

    def hessian(self, sdi, sdi2=None):
        r"""
        Calculates the Gauss-Newton approximation to the Hessian.

        This is abstracted because some residuals expect the Hessian to be
        pre-processed. The Gauss-Newton approximation to the Hessian is
        defined as:

        .. math::
            \mathbf{J J^T}

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        sdi2 : ``(N, n_params)`` `ndarray` or ``None``, optional
            The steepest descent images.

        Returns
        -------
        H : ``(n_params, n_params)`` `ndarray`
            The approximation to the Hessian
        """
        # compute hessian
        # sdi.T:   params x (ch x h x w)
        # sdi:              (ch x h x w) x params
        # hessian: params x               x params
        if sdi2 is None:
            H = sdi.T.dot(sdi)
        else:
            H = sdi.T.dot(sdi2)
        return H

    def steepest_descent_update(self, sdi, image, template):
        r"""
        Calculates the steepest descent parameter updates.

        These are defined, for the forward additive algorithm, as:

        .. math::
            \sum_x [ \nabla I \frac{\partial W}{\partial p} ]^T [ T(x) - I(W(x;p)) ]

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        image : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)
        template : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)

        Returns
        -------
        sd_delta_p : ``(n_params,)`` `ndarray`
            The steepest descent parameter updates.
        """
        # compute image regularized gradient
        IWxp_grad = self.gradient(image)
        IWxp_grad = self._regularise_gradients(IWxp_grad)

        # compute vectorized error_image
        # error_img: (dims x ch x pixels)
        self._error_img = (IWxp_grad.as_vector() -
                           self._template_grad.as_vector())

        # compute steepest descent update
        # sdi.T:      params x (dims x ch x pixels)
        # error_img:           (dims x ch x pixels)
        # sdu:        params
        return sdi.T.dot(self._error_img)

    def cost_closure(self):
        r"""
        Method to compute the optimization cost.

        Returns
        -------
        cost : `float`
            The cost value.
        """
        def cost_closure(x):
            return lambda: x.T.dot(x)
        return cost_closure(self._error_img)

    def __str__(self):
        return "Gradient Image Residual"

class GradientCorrelation(Residual):
    r"""
    Class for Gradient Correlation residual.

    References
    ----------
    .. [1] G. Tzimiropoulos, S. Zafeiriou, and M. Pantic. "Robust and
        Efficient Parametric Face Alignment", IEEE Proceedings of International
        Conference on Computer Vision (ICCV), pp. 1847-1854, November 2011.
    """
    def steepest_descent_images(self, image, dW_dp, forward=None):
        r"""
        Calculates the standard steepest descent images.

        Within the forward additive framework this is defined as

        .. math::
             \nabla I \frac{\partial W}{\partial p}

        The input image is vectorised (`N`-pixels) so that masked images can
        be handled.

        Parameters
        ----------
        image : `menpo.image.Image`
            The image to calculate the steepest descent images from, could be
            either the template or input image depending on which framework is
            used.
        dW_dp : `ndarray`
            The Jacobian of the warp.
        forward : `tuple` or ``None``, optional
            A `tuple` containing the extra weights required for the function
            `warp` (which should be passed as a function handle), i.e.
            ``(`menpo.image.Image`, `menpo.transform.AlignableTransform>`)``. If
            ``None``, then the optimization algorithm is assumed to be inverse.

        Returns
        -------
        VT_dW_dp : ``(N, n_params)`` `ndarray`
            The steepest descent images
        """
        n_dims = image.n_dims
        n_channels = image.n_channels

        # compute gradient
        # grad:  dims x ch x pixels
        grad = self.gradient(image, forward=forward)
        grad2 = grad.as_vector().reshape((n_dims, n_channels) + grad.shape)

        # compute IGOs (remember axis 0 is y, axis 1 is x)
        # grad:    dims x ch x pixels
        # phi:            ch x pixels
        # cos_phi:        ch x pixels
        # sin_phi:        ch x pixels
        phi = np.angle(grad2[1, ...] + 1j * grad2[0, ...])
        self._cos_phi = np.cos(phi)
        self._sin_phi = np.sin(phi)

        # concatenate sin and cos terms so that we can take the second
        # derivatives correctly. sin(phi) = y and cos(phi) = x which is the
        # correct ordering when multiplying against the warp Jacobian
        # cos_phi:         ch  x pixels
        # sin_phi:         ch  x pixels
        # grad:    (dims x ch) x pixels
        grad.from_vector_inplace(
            np.concatenate((self._sin_phi[None, ...],
                            self._cos_phi[None, ...]), axis=0).ravel())

        # compute IGOs gradient
        # second_grad:  dims x dims x ch x pixels
        second_grad = self.gradient(grad)
        second_grad = second_grad.masked_pixels().flatten().reshape(
            (n_dims, n_dims,  n_channels) + second_grad.shape)

        # Fix crossed derivatives: dydx = dxdy
        second_grad[1, 0, ...] = second_grad[0, 1, ...]

        # complete full IGOs gradient computation
        # second_grad:  dims x dims x ch x pixels
        second_grad[1, ...] = (-self._sin_phi[None, ...] * second_grad[1, ...])
        second_grad[0, ...] = (self._cos_phi[None, ...] * second_grad[0, ...])

        # compute steepest descent images
        # gradient: dims x dims x ch x pixels
        # dw_dp:    dims x           x pixels x params
        # sdi:                    ch x pixels x params
        sdi = 0
        aux = second_grad[..., None] * dW_dp[None, :, None, ...]
        for a in aux.reshape(((-1,) + aux.shape[2:])):
                sdi += a

        # compute constant N
        # N:  1
        self._N = grad.n_parameters / 2

        # reshape steepest descent images
        # sdi: (ch x pixels) x params
        sdi = sdi.reshape((-1, sdi.shape[-1]))

        return sdi, sdi

    def hessian(self, sdi, sdi2=None):
        r"""
        Calculates the Gauss-Newton approximation to the Hessian.

        This is abstracted because some residuals expect the Hessian to be
        pre-processed. The Gauss-Newton approximation to the Hessian is
        defined as:

        .. math::
            \mathbf{J J^T}

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        sdi2 : ``(N, n_params)`` `ndarray` or ``None``, optional
            The steepest descent images.

        Returns
        -------
        H : ``(n_params, n_params)`` `ndarray`
            The approximation to the Hessian
        """
        # compute hessian
        # sdi.T:   params x (ch x h x w)
        # sdi:              (ch x h x w) x params
        # hessian: params x               x params
        if sdi2 is None:
            H = sdi.T.dot(sdi)
        else:
            H = sdi.T.dot(sdi2)
        return H

    def steepest_descent_update(self, sdi, image, template):
        r"""
        Calculates the steepest descent parameter updates.

        These are defined, for the forward additive algorithm, as:

        .. math::
            \sum_x [ \nabla I \frac{\partial W}{\partial p} ]^T [ T(x) - I(W(x;p)) ]

        Parameters
        ----------
        sdi : ``(N, n_params)`` `ndarray`
            The steepest descent images.
        image : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)
        template : `menpo.image.Image`
            Either the warped image or the template (depending on the framework)

        Returns
        -------
        sd_delta_p : ``(n_params,)`` `ndarray`
            The steepest descent parameter updates.
        """
        n_dims = image.n_dims
        n_channels = image.n_channels

        # compute image gradient
        IWxp_grad = self.gradient(image)
        IWxp_grad = IWxp_grad.as_vector().reshape(
            (n_dims, n_channels) + image.shape)

        # compute IGOs (remember axis 0 is y, axis 1 is x)
        # IWxp_grad:     dims x ch x pixels
        # phi:                  ch x pixels
        # IWxp_cos_phi:         ch x pixels
        # IWxp_sin_phi:         ch x pixels
        phi = np.angle(IWxp_grad[1, ...] + 1j * IWxp_grad[0, ...])
        IWxp_cos_phi = np.cos(phi)
        IWxp_sin_phi = np.sin(phi)

        # compute error image
        # error_img:  (ch x h x w)
        self._error_img = (self._cos_phi * IWxp_sin_phi -
                           self._sin_phi * IWxp_cos_phi).ravel()

        # compute steepest descent update
        # sdi:       (ch x pixels) x params
        # error_img: (ch x pixels)
        # sdu:                      params
        sdu = sdi.T.dot(self._error_img)

        # compute step size
        qp = np.sum(self._cos_phi * IWxp_cos_phi +
                    self._sin_phi * IWxp_sin_phi)
        self._l = self._N / qp
        return self._l * sdu

    def cost_closure(self):
        r"""
        Method to compute the optimization cost.

        Returns
        -------
        cost : `float`
            The cost value.
        """
        def cost_closure(x):
            return lambda: 1/x
        return cost_closure(self._l)

    def __str__(self):
        return "Gradient Correlation Residual"

max_stars_repo_name: dykestra/menpofit

max_issues_repo_name: dykestra/menpofit

max_forks_repo_name: dykestra/menpofit

reason: import numpy,from numpy,import scipy


text:
# this code is modified from the pytorch example code: https://github.com/pytorch/examples/blob/master/imagenet/main.py
# after the model is trained, you might use convert_model.py to remove the data parallel module to make the model as standalone weight.
#
# Bolei Zhou

import argparse
import os
import shutil
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision.models as models

from torch.utils.tensorboard import SummaryWriter

import pdb
model_names = sorted(name for name in models.__dict__
    if name.islower() and not name.startswith("__")
    and callable(models.__dict__[name]))

parser = argparse.ArgumentParser(description='PyTorch Places365 Training')
parser.add_argument('data', metavar='DIR',
                    help='path to dataset')
parser.add_argument('--arch', '-a', metavar='ARCH', default='resnet18',
                    help='model architecture: ' +
                        ' | '.join(model_names) +
                        ' (default: resnet18)')
parser.add_argument('-j', '--workers', default=6, type=int, metavar='N',
                    help='number of data loading workers (default: 4)')
parser.add_argument('--epochs', default=90, type=int, metavar='N',
                    help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
                    help='manual epoch number (useful on restarts)')
parser.add_argument('-b', '--batch-size', default=256, type=int,
                    metavar='N', help='mini-batch size (default: 256)')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
                    metavar='LR', help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                    help='momentum')
parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float,
                    metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--print-freq', '-p', default=40, type=int,
                    metavar='N', help='print frequency (default: 10)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
                    help='path to latest checkpoint (default: none)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
                    help='evaluate model on validation set')
parser.add_argument('--pretrained', dest='pretrained', action='store_false',
                    help='use pre-trained model')
parser.add_argument('--num_classes',default=365, type=int, help='num of class in the model')
parser.add_argument('--dataset',default='places365',help='which dataset to train')

best_prec1 = 0

def main():
    global args, best_prec1
    args = parser.parse_args()
    print(args)

    # Writer for Tensorboard
    global writer
    time_string = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    log_dir = "logs/trainval/" + time_string
    writer = SummaryWriter(log_dir)

    # Create checkpoint folder
    os.mkdir(os.path.join('checkpoint', time_string))

    # create model
    print("=> creating model '{}'".format(args.arch))
    model = models.__dict__[args.arch](num_classes=args.num_classes)

    if args.arch.lower().startswith('alexnet') or args.arch.lower().startswith('vgg'):
        model.features = torch.nn.DataParallel(model.features)
        model.cuda()
    else:
        model = torch.nn.DataParallel(model).cuda()
    # print(model)
    # optionally resume from a checkpoint
    if args.resume:
        if os.path.isfile(args.resume):
            print("=> loading checkpoint '{}'".format(args.resume))
            checkpoint = torch.load(args.resume)
            args.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            model.load_state_dict(checkpoint['state_dict'])
            print("=> loaded checkpoint '{}' (epoch {})"
                  .format(args.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(args.resume))

    # Drop the last layer and add in a new last fully connected layer
    set_parameter_requires_grad(model, feature_extracting=True)
    classifier = list(model.classifier.children())
    model.classifier = nn.Sequential(*classifier[:-1])
    model.classifier.add_module('6', nn.Linear(classifier[-1].in_features, 3))
    model.cuda()
    print(model)

    cudnn.benchmark = True

    # Data loading code
    traindir = os.path.join(args.data, 'train')
    valdir = os.path.join(args.data, 'val')
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    train_data = datasets.ImageFolder(traindir, transforms.Compose([
                    transforms.Resize(256),
                    transforms.RandomResizedCrop(224),
                    transforms.RandomHorizontalFlip(),
                    transforms.ToTensor(),
                    normalize,
                ]))
    val_data = datasets.ImageFolder(valdir, transforms.Compose([
                    transforms.Resize(256),
                    transforms.CenterCrop(224),
                    transforms.ToTensor(),
                    normalize,
                ]))

    print(train_data.class_to_idx)

    global classes
    classes = {v: k for k, v in train_data.class_to_idx.items()}
    print(classes)

    train_loader = torch.utils.data.DataLoader(
        train_data,
        batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True)

    val_loader = torch.utils.data.DataLoader(
        val_data,
        batch_size=args.batch_size, shuffle=True,
        num_workers=args.workers, pin_memory=True)

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda()

    # optimizer = torch.optim.SGD(model.parameters(), args.lr,
    #                             momentum=args.momentum,
    #                             weight_decay=args.weight_decay)
    optimizer = torch.optim.Adam(model.parameters(), args.lr)

    if args.evaluate:
        print("Validation:\t")
        validate(val_loader, model, criterion)
        return

    best_epoch = 0

    for epoch in range(args.epochs):
        # adjust_learning_rate(optimizer, epoch)

        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch)

        # evaluate on train and validation set
        prec1 = validate(val_loader, model, criterion, epoch)

        # remember best prec@1 and save checkpoint
        is_best = prec1 > best_prec1
        best_prec1 = max(prec1, best_prec1)
        if is_best:
            best_epoch = epoch
            print("Saving best checkpoint with precision of: {0}".format(best_prec1))
        save_checkpoint({
            'epoch': epoch + 1,
            'arch': args.arch,
            'state_dict': model.state_dict(),
            'best_prec1': best_prec1,
        }, is_best, os.path.join('checkpoint', time_string, args.arch.lower()))

    print("{0}: Best Val Prec@1 {1:.3f} at epoch {2}".format(time_string, best_prec1, best_epoch))

def train(train_loader, model, criterion, optimizer, epoch):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()

    # switch to train mode
    model.train()

    end = time.time()
    for i, (input, target) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        target = target.cuda(async=True)
        input_var = torch.autograd.Variable(input)
        target_var = torch.autograd.Variable(target)
        # compute output
        output = model(input_var)
        loss = criterion(output, target_var)

        # measure accuracy and record loss
        prec1 = accuracy(output.data, target, topk=(1))
        losses.update(loss.data, input.size(0))
        top1.update(prec1[0], input.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            print('Train: [{0}][{1}/{2}]\t'
                  'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                  'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
                  'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'.format(
                   epoch, i, len(train_loader), batch_time=batch_time,
                   data_time=data_time, loss=losses, top1=top1))

    writer.add_scalars('loss', {'train loss': losses.avg}, epoch)
    print('Train: \tLoss {loss.avg:.4f}\t'
          'Prec@1 {top1.avg:.3f}\t'
          .format(loss=losses, top1=top1))

def plot_classes_preds(net, images, labels):
    '''
    Generates matplotlib Figure using a trained network, along with images
    and labels from a batch, that shows the network's top prediction along
    with its probability, alongside the actual label, coloring this
    information based on whether the prediction was correct or not.
    Uses the "images_to_probs" function.
    '''
    preds, probs = images_to_probs(net, images)
    # plot the images in the batch, along with predicted and true labels
    fig = plt.figure(figsize=(10, 10))
    for idx in np.arange(9):
        ax = fig.add_subplot(3, 3, idx+1, xticks=[], yticks=[])
        matplotlib_imshow(images[idx], one_channel=True)
        ax.set_title("{0}, {1:.1f}%\n(label: {2})".format(
            classes[preds[idx]],
            probs[idx] * 100.0,
            classes[labels[idx].item()]),
                    color=("green" if preds[idx]==labels[idx].item() else "red"))
    return fig

def validate(loader, model, criterion, epoch):
    batch_time = AverageMeter()
    losses = AverageMeter()
    top1 = AverageMeter()

    # switch to evaluate mode
    model.eval()

    end = time.time()

    with torch.no_grad():
        for i, (input, target) in enumerate(loader):
            target = target.cuda(async=True)
            input_var = torch.autograd.Variable(input)
            target_var = torch.autograd.Variable(target)

            # compute output
            output = model(input_var)
            loss = criterion(output, target_var)

            # measure accuracy and record loss
            prec1 = accuracy(output.data, target, topk=(1))
            losses.update(loss.data, input.size(0))
            top1.update(prec1[0], input.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            # if i % args.print_freq == 0:
            #     print('Test: [{0}/{1}]\t'
            #         'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
            #         'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
            #         'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'.format(
            #         i, len(loader), batch_time=batch_time, loss=losses,
            #         top1=top1))

            # if i % args.print_freq == 0:

    writer.add_figure('predictions vs. actuals',
                plot_classes_preds(model, input_var, target),
                global_step=epoch)

    writer.add_scalars('loss', {'val loss': losses.avg}, epoch)

    print('Valid: \tLoss {loss.avg:.4f}\t'
          'Prec@1 {top1.avg:.3f}\t'
          .format(loss=losses, top1=top1))

    return top1.avg

def images_to_probs(net, images):
    '''
    Generates predictions and corresponding probabilities from a trained
    network and a list of images
    '''
    output = net(images)
    # convert output probabilities to predicted class
    _, preds_tensor = torch.max(output, 1)
    preds = np.squeeze(preds_tensor.cpu().numpy())
    return preds, [F.softmax(el, dim=0)[i].item() for i, el in zip(preds, output)]

def matplotlib_imshow(img, one_channel=False):
    if one_channel:
        img = img.mean(dim=0)
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    if one_channel:
        plt.imshow(npimg, cmap="Greys")
    else:
        plt.imshow(np.transpose(npimg, (1, 2, 0)))

def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
    torch.save(state, filename + '_latest.pth.tar')
    if is_best:
        shutil.copyfile(filename + '_latest.pth.tar', filename + '_best.pth.tar')

def set_parameter_requires_grad(model, feature_extracting):
    if feature_extracting:
        for param in model.parameters():
            param.requires_grad = False

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count

def adjust_learning_rate(optimizer, epoch):
    """Sets the learning rate to the initial LR decayed by 10 every 10 epochs"""
    lr = args.lr * (0.1 ** (epoch // 10))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

def accuracy(output, target, topk=(1,)):
    """Computes the precision@k for the specified values of k"""
    # maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(1, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    # for k in topk:
    correct_k = correct[:1].view(-1).float().sum(0)
    res.append(correct_k.mul_(100.0 / batch_size))
    return res

if __name__ == '__main__':
    main()


text:
# Copyright (c) 2021 Graphcore Ltd. All rights reserved.

import pytest
import numpy as np
import popart.ir as pir
import os

def test_cache_env():
    os.environ['POPART_CACHE_DIR'] = 'PATH_TO_CACHE'

    ir = pir.Ir()
    opts = ir._pb_ir.getSessionOptions()

    assert opts.enableEngineCaching
    assert opts.cachePath == 'PATH_TO_CACHE'

    del os.environ['POPART_CACHE_DIR']

max_stars_repo_name: graphcore/popart

max_issues_repo_name: graphcore/popart

max_forks_repo_name: graphcore/popart


text:
import os
import torch.utils.data as data
import h5py
import torch
from collections import abc, defaultdict
import numpy as np
from sklearn.model_selection import train_test_split
import copy
import torchvision.transforms as transforms

__class__ = ["HDF5"]

class HDF5(torch.utils.data.Dataset):
    def __init__(self, root, transform=None, target_transform=None, target_tag="target"):
        super().__init__()
        self.root = os.path.abspath(root)
        if not os.path.isfile(self.root):
            raise FileNotFoundError(f"The file doesn't exist: `{self.root}`")
        self.transform = transform
        self.target_transform = target_transform
        self.target_tag = target_tag
        self.cache = []
        try:
            self.init_cache()
        except:
            raise RuntimeError(f"Failed to load dataset from `{self.root}`")
        else:
            print(f"Succesfully loaded {len(self)} items.")

    @property
    def targets(self):
        return [t for _, t in self.cache]

    @property
    def counter(self):
        cnt = defaultdict(lambda: 0)
        for _, target in self.cache:
            cnt[target] += 1
        cnt = sorted(cnt.items(), key=lambda x: x[0])
        return dict(cnt)

    def __getitem__(self, i):
        ref, target = self.cache[i]
        with self.open(self.root) as f:
            x = self.load_data(f[ref])
        if callable(self.transform):
            x = self.transform(x)
        if callable(self.target_transform):
            target = self.target_transform(target)
        return x, target

    def __len__(self):
        return len(self.cache)

    def split(self, train_size=0.7, random_state=None, stratify=None):
        idx = np.arange(len(self))
        train_idx, test_idx = train_test_split(idx, train_size=train_size, random_state=random_state, stratify=stratify)
        train_set = copy.copy(self).select(train_idx)
        test_set = copy.copy(self).select(test_idx)
        return train_set, test_set

    def select(self, idx):
        self.cache = [self.cache[i] for i in idx]
        return self

    def sample(self, num_per_class=1):
        tmp = []
        targets = np.array(self.targets)
        for t in np.unique(targets):
            idx = np.where(targets == t)[0]
            np.random.shuffle(idx)
            tmp.extend(idx[:num_per_class])
        return copy.copy(self).select(tmp)

    def init_cache(self):
        self.cache = []
        with self.open(self.root) as f:
            self.cache = self.load_cache_recursive(f)

    def load_cache_recursive(self, f):
        cache = []
        for key, el in f.items():
            if isinstance(el, abc.MutableMapping):
                cache.extend(self.load_cache_recursive(el))
            else:
                target = el.attrs.get(self.target_tag)
                cache.append((el.ref, target))
        return cache

    def open(self, path):
        return h5py.File(path, "r", libver="latest")

    def load_data(self, el):
        x = np.array(el[:])
        x = torch.from_numpy(x).float()
        x = torch.clamp(x / 255.0, 0.0, 1.0)
        return x

    def co(self, augment):
        transform = self.transform
        self.transform = transforms.Lambda(lambda x: (transform(x), augment(x)))
        return self