Esempio n. 1
0
def artif_exp_4():
    exp = Experiment()
    plotting = Plotter()
    plotting.initialize(yscale='log',
                        filt="median, movingAverage",
                        skip=100,
                        ylabel="Average Squared Error",
                        col=['c', 'r', 'g', 'orange', 'k'])

    T = 1000

    ag = RandomAction(mu=0, sigma=0.3)

    exp.run_experiments_multiple(Setting4(), {"timesteps" : T},\
          [OnlineWaveFilteringParameterFree(),\
           OnlineWaveFiltering(),\
           EMKalmanFilter(),\
           SSIDKalmanFilter(),\
           Consistency()],\
          [{'timesteps': T, 'max_k' : 30, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
           {'timesteps': T, 'k' : 10, 'lr': 1e-2, 'action_dim': 1, 'out_dim': 1, 'R_m': 3.0},\
           {'timesteps': T, 'order': 2, 'data': 100, 'iter': 500},\
           {'timesteps': T, 'order': 2, 'data': 100},\
           {'timesteps': T, 'out_dim': 1}],\
          ['OnlineWaveFilteringParameterFree',\
           'OnlineWaveFiltering',\
           'EM',\
           '4SID',\
           'Consistency'],\
          n_runs = 20,\
          plotter = plotting,\
          action_generator = ag,\
          verbose = True)
def roomTemperature():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "median, movingAverage", skip = 500)

	dimension = 20

	exp.run_experiments_multiple(RoomTemperature(), {},\
						 [VAR(),\
						  ECVARMAOGD(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree()
						  ],\
						 [{'p' : 8, 'dim': dimension},\
						  {'p' : 8, 'dim': dimension, 'lr': 1},\
						  {'max_k' : 10, 'action_dim': dimension, 'out_dim': dimension, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 30, 'action_dim': dimension, 'out_dim': dimension, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 50, 'action_dim': dimension, 'out_dim': dimension, 'opt': Hedge(), 'optForSubPredictors': FTRL()}
						  ],\
						 ['VAR_8',\
						  'ECVARMA_OGD16',\
						  'OnlineWaveFilteringParameterFree10',\
						  'OnlineWaveFilteringParameterFree30',\
						  'OnlineWaveFilteringParameterFree50'
						  ],\
						 n_runs = 20,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(RoomTemperature()))
def sp500():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "movingAverage", skip = 100)

	exp.run_experiments_multiple(SP500(), {},\
						 [ARMA(),\
						  ARIMA(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree(),\
						  OnlineWaveFilteringParameterFree()
						  ],\
						 [{'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'max_k' : 20, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 50, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
						  {'max_k' : 100, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()}
						  ],\
						 ['ARMA_OGD',\
						  'ARIMA_OGD',\
						  'OnlineWaveFilteringParameterFree20',\
						  'OnlineWaveFilteringParameterFree50',\
						  'OnlineWaveFilteringParameterFree100'
						  ],\
						 n_runs = 20,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(SP500()))
Esempio n. 4
0
def plot_nn(x, y, suffix=''):
    # Test-train split
    x_train, x_test, y_train, y_test = train_test_split(x,
                                                        y,
                                                        test_size=0.2,
                                                        random_state=1)
    x_train, x_test = scale_data(x_train, x_test)

    iter_counts = np.arange(1, 1100, 50, dtype=int)

    # Create Red Wine Quality NNs
    nns = create_nns(x_train, y_train, iter_counts)

    # Get training/testing accuracy for Red Wine Quality
    accs_train, accs_test = [], []

    for nn in nns:
        accs_train.append(accuracy_score(nn.predict(x_train), y_train))
        accs_test.append(accuracy_score(nn.predict(x_test), y_test))

    # Generate graph for Red Wine Quality
    plot = Plotter(name='red-wine{}'.format(suffix),
                   learner='nn',
                   axes={
                       'x': 'Number of weight updates',
                       'y': 'Accuracy score'
                   })
    plot.add_plot(iter_counts, accs_train, 'training data', 'None')
    plot.add_plot(iter_counts, accs_test, 'testing data', 'None')
    plot.find_max(iter_counts, accs_test, 'testing')
    plot.save()
Esempio n. 5
0
 def __init__(self, model, modelParam, config, saveRestorer, env):
     self.model = model
     self.modelParam = modelParam
     self.config = config
     self.saveRestorer = saveRestorer
     self.env = env
     self.plotter = Plotter(self.modelParam, self.config)
     return
Esempio n. 6
0
 def __init__(self, model, modelParam, config, dataLoader, saveRestorer):
     self.model        = model
     self.modelParam   = modelParam
     self.config       = config
     self.dataLoader   = dataLoader
     self.saveRestorer = saveRestorer
     self.plotter      = Plotter(self.modelParam, self.config)
     return
Esempio n. 7
0
def plot_step(sample):
    # reshape [C, H, W] -> [W, H, C]
    # sample = sample.permute(2, 1, 0)
    # sample = sample.contiguous().view(1024, 3)
    # sample = sample.cpu().data.numpy()
    # plot
    for i in range(1024):
        img = sample[:i + 1, :]
        filepath = sample_path + '/step_{:04d}.png'.format(i)
        Plotter.plot_pc_color(img, filepath)
        print(i)
Esempio n. 8
0
def plot_samples(samples, epoch, name, nsamples=1, color=False):
    # reshape [N, C, H, W] -> [N, W, H, C]
    samples = samples.permute(0, 3, 2, 1)
    samples = samples.contiguous().view(samples.shape[0], 1024, 3)
    samples = samples.cpu().data.numpy()
    # plot
    for i in range(nsamples):
        img = samples[i, :, :]
        filepath = sample_path + '/{}_{}_{}.png'.format(name, epoch, i)
        if color:
            Plotter.plot_pc_color(img, filepath)
        else:
            Plotter.plot_pc(img, filepath)
class SimpleMnistDataVisualizer:
    def __init__(self, X_train, y_train, mapp):
        self.X_train = X_train
        self.y_train = y_train
        self.mapp = mapp
        self.plotter = Plotter()

    def plot_specified_digit(self, code):
        """
        Plot a digit or character
        :param code:
        """
        for i in range(self.y_train.shape[0]):
            if (self.y_train[i, code] == 1):
                self.plotter.plot_image(self.X_train[i].reshape(28,28))
                break

    def plot_first_digit(self, ):
        """
        Plot first element of the training set
        """
        self.plotter.plot_image(self.X_train[0].reshape(28, 28))

    def plot_range(self):
        """
        Plot characters from training set using a range
        """
        for i in range(100, 109):
            plt.subplot(330 + (i + 1))
            plt.imshow(self.X_train[i].reshape(28, 28), cmap=plt.get_cmap('gray'))
            plt.title(chr(self.mapp[np.argmax(self.y_train[i])]))
        plt.show()

    def plotgraph(self, epochs, acc, val_acc):
        """
        Plot a graph
        :param epochs:
        :param acc:
        :param val_acc:
        """
        self.plotter.plotgraph(epochs, acc, val_acc)

    def plot_loss_acc(self):
        """
        Plot loss based on parameters of each epoch
        """
        history_path = './experiments/2019-12-15/conv_emnist_from_config/history_params/parameters.csv'
        data_frame = pd.read_csv(history_path, delimiter=',')
        self.plotter.plotgraph(data_frame['epoch'].values, data_frame['acc'].values, data_frame['val_acc'])
        self.plotter.plotgraph(data_frame['epoch'].values, data_frame['loss'].values, data_frame['val_loss'])
 def __init__(self, model, data, map, config, weights=''):
     super(ConvMnistDataPredictor, self).__init__(model, data, config)
     self.map = map
     self.plotter = Plotter()
     if config.evaluator.custom_weight and len(
             weights) > 0:  # Load a custom weight if there is any
         self.model.load_weights(weights)
def train(hparams, ModuleClass, ModelClass, DatasetClass, loggers, train_dataloader, val_loaders):
    model = ModelClass(hparams=hparams)
    module = ModuleClass(hparams, model, None)

    metric = "val_loss"
    checkpoint_callback = ModelCheckpoint(
        dirpath=f"{hparams.output_path}/checkpoints/",
        save_top_k=hparams.save_top_k,
        # save_last=True,
        verbose=True,
        monitor=metric,
        mode='min',
        prefix=hparams.name,
        filename=f'{{epoch}}-{{{metric}:.5f}}'
    )
    early_stop_callback = EarlyStopping(
        monitor=metric,
        min_delta=0.00,
        verbose=True,
        patience=3,
        mode='min')

    plotter = Plotter(hparams.current_train_fold)
    # ------------------------
    # 3 INIT TRAINER
    # ------------------------
    trainer = pl.Trainer(
        # gpus=hparams.gpus,
        logger=loggers,
        # fast_dev_run=hparams.fast_dev_run,
        min_epochs=hparams.min_epochs,
        max_epochs=hparams.max_epochs,
        checkpoint_callback=True,
        resume_from_checkpoint=hparams.resume_from_checkpoint,
        callbacks=[early_stop_callback, checkpoint_callback, plotter],
        weights_summary='full',
        # auto_lr_find=True,
        num_sanity_val_steps=hparams.num_sanity_val_steps,
        log_every_n_steps=hparams.log_every_n_steps,
        # check_val_every_n_epoch=1,  # how often the model will be validated
        limit_train_batches=hparams.limit_train,  # How much of training dataset to check (if 1 check all)
        limit_val_batches=hparams.limit_val,
        # limit_test_batches=LIMIT_TEST,
        # auto_scale_batch_size=True,   # search for optimal batch size
        # automatic_optimization=True,
        profiler="simple"
    )
    print('MIN EPOCHS: ', trainer.min_epochs)
    print('MAX EPOCHS: ', trainer.max_epochs)

    # wandb_logger.watch(module, log="all", log_freq=100)  # plots the gradients
    print(f'---------------TRAIN FIT--------------\n'
          f'VAL DISABLED?: {trainer.disable_validation} \n'
          f'VAL ENABLED?: {trainer.enable_validation}')
    seed_everything(seed=0)
    trainer.fit(module, train_dataloader=train_dataloader, val_dataloaders=val_loaders)
    print('---------------TEST--------------')
        # trainer.test(test_dataloaders=test_loaders)

    return trainer.checkpoint_callback.best_model_score  # best_model_loss
Esempio n. 12
0
def plot_with_map(currMicroClusters, dataContext, title):
    p = Plotter(currMicroClusters, dataContext)
    ax = plt.gca()  # we try the one with the mc size
    # map
    map = Basemap(projection='cyl', )
    map.drawmapboundary(fill_color='aqua')
    map.fillcontinents(color='gray', lake_color='aqua')
    map.drawcoastlines()
    # plot
    p.plotMicroClustersSize(ax)
    fig = plt.gcf()
    fig.canvas.manager.window.showMaximized()
    fig.suptitle(title, fontweight="bold")
    # show
    if not generate_gif:
        plt.show()  # plot figure to see resutls; for testing
    return fig
Esempio n. 13
0
def artif_exp_2():
    exp = Experiment()
    plotting = Plotter()
    plotting.initialize(yscale='log',
                        filt="median, movingAverage",
                        skip=100,
                        ylabel="Average Squared Error",
                        col=['c', 'r', 'g', 'orange', 'k'])

    A = np.array([[0.999, 0], [0, 0.5]])
    B = np.array([[1.0], [1.0]])
    C = np.array([[1.0, 1.0]])
    D = np.array([[0.0]])

    T = 1000

    ag = RandomAction(mu=0, sigma=0.3)

    exp.run_experiments_multiple(LDS(), {"timesteps" : T, 'action_dim': 1, 'hidden_dim': 2, 'out_dim': 1, 'partially_observable': True,\
            'system_params': {'A': A, 'B' : B, 'C': C, 'D': D, 'noise_distribution': 'normal'}},\
          [OnlineWaveFilteringParameterFree(),\
           OnlineWaveFiltering(),\
           EMKalmanFilter(),\
           SSIDKalmanFilter(),\
           Consistency()],\
          [{'timesteps': T, 'max_k' : 30, 'action_dim': 1, 'out_dim': 1, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
           {'timesteps': T, 'k' : 10, 'lr': 1e-3, 'action_dim': 1, 'out_dim': 1, 'R_m': 1.0},\
           {'timesteps': T, 'order': 2, 'data': 100, 'iter': 500},\
           {'timesteps': T, 'order': 2, 'data': 100},\
           {'timesteps': T, 'out_dim': 1}],\
          ['OnlineWaveFilteringParameterFree',\
           'OnlineWaveFiltering',\
           'EM',\
           '4SID',\
           'Consistency'],\
          n_runs = 20,\
          plotter = plotting,\
          action_generator = ag,\
          verbose = True)
Esempio n. 14
0
    def __init__(self, opt, model, criterion, optimizer, optimState=None):
        self.model = model
        self.optimState = optimState
        self.opt = opt
        H = opt.hype

        self.optimizer = optimizer
        self.criterion = criterion

        if self.optimState is not None:
            self.optimizer.load_state_dict(self.optimState)

        self.num_bin = self.opt.hype['num_bin']
        self.grid_h = self.grid_w = self.opt.hype['grid_size']
        self.batch_size = opt.batch_size
        self.num_grids = self.grid_h * self.grid_w
        self.max_ratio = 1000.

        self.w0 = H['loss_weights'][:2]
        self.w1 = H['loss_weights'][2:4]

        self.plot_ = Plotter(opt.hype)
        self.saveDir = Path(self.opt.saveDir)

        if opt.lr_param['lr_policy'] == 'step':
            self.lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(
                self.optimizer,
                milestones=opt.lr_param['steppoints'],
                gamma=0.1)

        self.pretrained_model = ''

        self.test_junc_thresh_list = [0.1 * x for x in range(10)]
        self.train_junc_thresh_list = [0.1, 0.5]

        self.log_interval = 100
        self.logfile = ref.logdir / "{}.log".format(self.opt.exp)
        with open(self.logfile, 'a') as fn:
            fn.write('\n{}\n'.format(datetime.datetime.now()))
def main():

    dataset = PrepareDataset()
    cfg = Config()
    plotter = Plotter()
    preprocessor = Preprocess()

    train_img, train_label, test_img, test_label = dataset.get_dataset()

    train_img, train_label = dataset.filter_2500(images=train_img,
                                                 labels=train_label,
                                                 class_num_to_filter=[2, 4, 9],
                                                 images_to_keep=2500)

    # dataset.plot_sample(x_train)

    x_train = preprocessor.normalize(train_img)
    y_train = x_train.copy()

    if cfg.add_noise:
        x_train = preprocessor.add_noise(x_train)
    # dataset.plot_sample(x_train)
    x_test = preprocessor.normalize(test_img)
    x_val = x_test.copy()
    y_val = x_test.copy()

    model, classifier = AutoEncoder(return_encoder_classifier=True,
                                    use_skip_conn=cfg.use_skip_conn)

    #model = EncoderWithSkipConn()

    if os.path.exists(cfg.model_name):
        print("Saved Model found, Loading...")
        model = load_model(cfg.model_name)

    if not os.path.exists(cfg.model_name) and cfg.train_encoder == False:
        raise ('No saved model')

    if cfg.train_encoder:
        history = train_encoder(cfg, model, x_train, y_train, x_val, y_val)

        plotter.plot_history(history)
    test_encoder(x_test, test_label, model, plotter)

    train_img = preprocessor.normalize(train_img)
    train_label = to_categorical(train_label)

    #print(train_label)

    classifier = copy_weights(model, classifier)
    classifier.save("classifier.h5")

    history = train_classifier(cfg, classifier, train_img, train_label)

    plotter.plot_history(history)

    test_img = preprocessor.normalize(test_img)
    test_label = [i[0] for i in test_label.tolist()]
    test_classifier(cfg, classifier, test_img, test_label, plotter)
Esempio n. 16
0
def artif_exp_5():
    exp = Experiment()
    plotting = Plotter()
    plotting.initialize(yscale='log',
                        filt="median, movingAverage",
                        skip=100,
                        ylabel="Average Squared Error",
                        col=['c', 'r', 'g', 'k'])

    A = np.diag([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
    B = np.eye(10)
    C = np.random.normal(size=(10, 10)) * 0.3
    D = np.zeros((10, 10))

    T = 1000

    ag = BlockAction(prob_repeat=0.8, sigma=0.3)

    exp.run_experiments_multiple(LDS(), {'action_dim': 10, 'hidden_dim': 10, 'out_dim': 10, 'partially_observable': True,\
            'system_params': {'A': A, 'B' : B, 'C': C, 'D': D}},\
          [OnlineWaveFilteringParameterFree(),\
           OnlineWaveFiltering(),\
           EMKalmanFilter(),\
           Consistency()],\
          [{'timesteps': T, 'max_k' : 30, 'action_dim': 10, 'out_dim': 10, 'opt': Hedge(), 'optForSubPredictors': FTRL()},\
           {'timesteps': T, 'k' : 30, 'lr': 1e-4, 'action_dim': 10, 'out_dim': 10, 'R_m': 5},\
           {'timesteps': T, 'order': 10, 'data': 100, 'iter': 500},\
           {'timesteps': T, 'out_dim': 10}],\
          ['OnlineWaveFilteringParameterFree',\
           'OnlineWaveFiltering',\
           'EM',\
           'Consistency'],\
          action_generator = ag,\
          n_runs = 20,\
          plotter = plotting,\
          verbose = True)
Esempio n. 17
0
def main():
    try:
        p = FileParser(sys.argv[1])
    except Exception:
        print('Usage: You must give a text file from the Whatsapp export feature ')
        sys.exit(84)
    p.readTextfile()
    parsedMessage = []
    for messages in p.content:
        tmpMessage = StructureText(messages)
        if len(tmpMessage.message) != 0:
            parsedMessage.append(tmpMessage)
    analyzed = Analyzer(parsedMessage)
    analyzed.displayDataText()
    plotter = Plotter(analyzed.wordAnalyserAimee.getFilteredTopWords(25))
    plotter.displayBubblePlot('Aimée top 25 words', "Word", "Number of occurences")
    plotter = Plotter(analyzed.wordAnalyserThomas.getFilteredTopWords(25))
    plotter.displayBubblePlot('Thomas top 25 words', "Word", "Number of occurences")
Esempio n. 18
0
def analyze(exp, runs=1):
  set_one_thread()
  cfg['exp'] = exp
  cfg['runs'] = runs
  plotter = Plotter(cfg)

  plotter.csv_results('Test', get_csv_result_dict, get_process_result_dict)
  plotter.plot_results(mode='Test', indexes='all')
  
  envs = ["HalfCheetah-v2", "Hopper-v2", "Walker2d-v2", "Swimmer-v2", "Ant-v2", "Reacher-v2"]
  indexes = {
    'ppo': [1, 2, 3, 4, 5, 6],
    'rpg': [7, 8, 9, 10, 11, 12]
  }
  if exp == 'rpg':
    for i in range(6):
      for mode in ['Test']:
        expIndexModeList = [['rpg', indexes['ppo'][i], mode], ['rpg', indexes['rpg'][i], mode]]
        plotter.plot_expIndexModeList(expIndexModeList, f'{mode}_{envs[i]}')
Esempio n. 19
0
def regression():
    """
    Regression.
    """
    # create data
    X, y = DataCreator().make_regression(name="custom")
    
    # train Gaussian process regressor
    reg = GaussianProcess()
    reg.fit(X, y)
    reg.plot()
    
    # train kernel ridge regressor
#    reg = KernelRegression()
#    reg.fit(X, y, kernel="gaussian")
    
    # train kNN regression
#    reg = KnnRegression()
#    reg.fit(X, y, k=5)
    
    # train bayesian linear regression
#    n = 100
#    reg = BayesRegression(alpha=2.0, beta=30.0, poly=True)
#    reg.fit(X[:n, :], y[:n])
#    reg.plot(X[:n, :], y[:n], std_devs=1)
    
    # train SVR (using gradient descent)
#    reg = SVR_GD()
#    reg.fit(X, y, epsilon=0.5, n_epochs=1000, learning_rate=0.1)
    
    # train SVR (sklearn)
#    reg = SVR_sklearn()
#    reg.fit(X, y, epsilon=0.05, C=5.0, kernel="rbf")
#    reg.plot()
    
    # plot boundary
    Plotter(X, y).plot_regression(reg, n_points=500, title="kNN regression")
Esempio n. 20
0
class Trainer():
    def __init__(self, model, modelParam, config, saveRestorer, env):
        self.model = model
        self.modelParam = modelParam
        self.config = config
        self.saveRestorer = saveRestorer
        self.env = env
        self.plotter = Plotter(self.modelParam, self.config)
        return

    def train(self):
        running_reward = 10
        given_range = range(self.model.update_counter,
                            self.modelParam['numb_of_updates'])
        if self.modelParam['inNotebook']:
            tt = tqdm_notebook(given_range,
                               desc='',
                               leave=True,
                               mininterval=0.01,
                               file=sys.stdout)
        else:
            tt = tqdm(given_range,
                      desc='',
                      leave=True,
                      mininterval=0.01,
                      file=sys.stdout)
        for update_counter in tt:
            if self.modelParam['is_train']:
                self.model.policyNet.train()
            else:
                self.model.policyNet.eval()
            loss, reward = self.run_update()

            reward = reward / self.modelParam["episode_batch"]
            running_reward = 0.2 * reward + (1 - 0.2) * running_reward

            desc = f'Update_counter={update_counter} | reward={reward:.4f} | | running_reward={running_reward:.4f}'
            tt.set_description(desc)
            tt.update()

            if update_counter % self.modelParam['storeModelFreq'] == 0:
                self.plotter.update(update_counter, running_reward)
                self.saveRestorer.save(update_counter, running_reward,
                                       self.model)
        return

    def run_update(self):
        episodes_summary = {
            'episodes_log_probs': [],
            'episodes_rewards': [],
            'episodes_total_reward': [],
            'episodes_return': [],
        }

        for episode_ind in range(self.modelParam['episode_batch']):
            #play episode
            ep_log_probs, ep_rewards, ep_total_reward, ep_returns = self.play_episode(
            )
            episodes_summary['episodes_log_probs'].append(ep_log_probs)
            episodes_summary['episodes_rewards'].append(ep_rewards)
            episodes_summary['episodes_total_reward'].append(ep_total_reward)
            episodes_summary['episodes_return'].append(ep_returns)

        loss = self.gradient_update(episodes_summary)
        reward = sum(episodes_summary['episodes_total_reward'])
        return loss, reward

    def play_episode(self):
        ep_log_probs = []
        ep_rewards = []
        state, ep_total_reward = self.env.reset(), 0
        for t in range(1, self.modelParam['max_episode_len']
                       ):  # Don't infinite loop while learning
            action, log_prob = self.model.select_action(state)
            state, reward, done, _ = self.env.step(action)
            if self.modelParam['render']:
                self.env.render()
            ep_rewards.append(reward)
            ep_log_probs.append(log_prob)
            ep_total_reward += reward
            if done:
                break
        #calculate return
        ep_returns = []
        G_t = 0
        for r in ep_rewards[::-1]:
            G_t = r + self.config['gamma'] * G_t
            ep_returns.insert(0, G_t)
        return ep_log_probs, ep_rewards, ep_total_reward, ep_returns

    def gradient_update(self, episodes_summary):
        policy_loss = []
        #flatten the list of lists into a single list
        episodes_return = [
            item for sublist in episodes_summary['episodes_return']
            for item in sublist
        ]
        episodes_log_probs = [
            item for sublist in episodes_summary['episodes_log_probs']
            for item in sublist
        ]

        #normalize the return to zero mean ans unit variance
        eps = np.finfo(np.float32).eps.item()
        episodes_return = torch.tensor(episodes_return,
                                       device=self.model.device)
        episodes_return = (episodes_return - episodes_return.mean()) / (
            episodes_return.std() + eps)
        for log_prob, R in zip(episodes_log_probs, episodes_return):
            policy_loss.append(-log_prob *
                               R)  #we multiply with -1 to get gradient ascent
        self.model.optimizer.zero_grad()
        policy_loss = torch.cat(policy_loss).sum()
        policy_loss.backward()
        self.model.optimizer.step()

        return policy_loss.detach().cpu().item()
Esempio n. 21
0
"""
from init_fusionnet import *
from fusionnet.unet import UnetModel
from ops import load_inet, evaluate_unet
import time
from utils.plotter import Plotter

save_name = '../results/curves_' + args.fnet_name + '.npy'

if args.flag_plot:
    if os.path.isfile(save_name):
        scores = np.load(save_name)
        # scores[:, :, 0] = scores[:, :, 0]**2
        x = np.arange(args.niter) * 10
        Plotter.plot_curves(
            scores, x, 0, 'Loss learning curve', 'Loss',
            '../results/plots/curves_' + args.fnet_name + '_loss.png')
        Plotter.plot_curves(
            scores, x, 1, 'Match learning curve', 'Match',
            '../results/plots/curves_' + args.fnet_name + '_match.png')
    else:
        print('File not exist')
    sys.exit('Done')
isvalid, inet = load_inet(args, device)
if not (isvalid):
    sys.exit('Invalid inet')

inet.eval()
# Fusion Net
args.norm = 'batch'
args.dataset_mode = 'aligned'
Esempio n. 22
0
def main():
    # ====================== Parameters ======================
    name_subtask = "pretraining"

    test_every = 1
    save_every = 5

    smpc = False
    output_folder = "output"

    # ====================== User inputs =====================

    parser = argparse.ArgumentParser()
    parser.add_argument("-d",
                        "--data",
                        help="Name of the dataset",
                        type=str,
                        default="sleep",
                        choices=["sleep", "mnist"])
    parser.add_argument("-a",
                        "--algo",
                        help="Federated algorithm",
                        type=str,
                        default="fedavg",
                        choices=["fedavg", "scaffold"])
    parser.add_argument("-c",
                        "--clients",
                        help="Number of clients",
                        type=int,
                        default=2)
    parser.add_argument("-s",
                        "--samples",
                        help="Number of samples per clients",
                        type=int,
                        default=1000)
    parser.add_argument("-k",
                        help="Number of clients per round",
                        type=int,
                        default=2)
    parser.add_argument("-r",
                        "--rounds",
                        help="Number of rounds",
                        type=int,
                        default=20)
    parser.add_argument("-e",
                        "--epochs",
                        help="Number of local epochs (client epochs)",
                        type=int,
                        default=1)
    parser.add_argument("-b", help="Batch size", type=int, default=32)
    parser.add_argument("--lr", help="Learning rate", type=float, default=0.01)

    args = parser.parse_args()

    problem_name = args.data
    algo = args.algo
    scaffold = True if algo == "scaffold" else False
    n_rounds = args.rounds
    n_local_epochs = args.epochs

    n_clients = args.clients
    n_clients_round = args.k
    max_samples = args.samples

    lr = args.lr
    batch_size = args.b

    subtask_folder = os.path.join(output_folder, f"{n_clients}-clients",
                                  f"{n_local_epochs}-epochs", algo,
                                  name_subtask)

    # ================== Create clients ======================

    hook = sy.TorchHook(torch)
    clients = [
        sy.VirtualWorker(hook, id=f"client{i}") for i in range(n_clients)
    ]
    crypto_provider = sy.VirtualWorker(hook, id="crypto_provider")

    # ===================== Load data =======================

    data_loader = DataLoader(problem_name,
                             clients,
                             max_samples_per_client=max_samples)
    data_loader.send_data_to_clients()

    #  ==================== Load model ======================

    model = Model("EEG_CNN", clients)  # ["MNIST_CNN", "EEG_CNN"]
    if smpc:
        model.send_model_to_clients()

    #  ==================== Train model =====================

    save_folder = os.path.join(subtask_folder, "model")
    trainer = FedAvg(model, data_loader, crypto_provider, save_folder)
    trainer.train(n_rounds,
                  n_local_epochs,
                  n_clients_round,
                  lr,
                  batch_size,
                  test_every,
                  save_every,
                  scaffold=scaffold,
                  smpc=smpc)

    #  =================== Plot results ======================

    list_test_loss_client = trainer.list_test_loss_client
    list_train_loss_client = trainer.list_train_loss_client
    list_accuracy_client = trainer.list_accuracy_client
    list_test_rounds = trainer.list_test_rounds

    # list_test_loss_client = [[4, 2, 1, 0.5, 0.25]] * n_clients
    # list_test_rounds = list(range(0, n_rounds*2, test_every))
    plotter = Plotter(subtask_folder)

    # Loss learning curve
    plotter.plot_learning_curve_avg(list_test_rounds, list_test_loss_client,
                                    list_train_loss_client)
    plotter.plot_learning_curve_clients(list_test_rounds,
                                        list_test_loss_client,
                                        list_train_loss_client,
                                        n_clients=n_clients)

    # Accuracy learning curve
    plotter.plot_learning_curve_avg(list_test_rounds,
                                    list_accuracy_client,
                                    label="accuracy",
                                    filename="accuracy-avg")
    plotter.plot_learning_curve_clients(list_test_rounds,
                                        list_accuracy_client,
                                        n_clients=n_clients,
                                        label="accuracy",
                                        filename="accuracy-clients")
Esempio n. 23
0
class Trainer():
    def __init__(self, model, modelParam, config, dataLoader, saveRestorer):
        self.model        = model
        self.modelParam   = modelParam
        self.config       = config
        self.dataLoader   = dataLoader
        self.saveRestorer = saveRestorer
        self.plotter      = Plotter(self.modelParam, self.config)
        return

    def train(self):
        for cur_epoch in range(self.model.start_epoch, self.modelParam['numbOfEpochs']):
            for modeSetup in self.modelParam['modeSetups']:
                mode     = modeSetup[0]
                is_train = modeSetup[1]
                if mode == 'train':
                    self.model.net.train()
                    loss = self.run_epoch(mode, self.model, is_train, cur_epoch)
                else:
                    with torch.no_grad():
                        self.model.net.eval()
                        loss = self.run_epoch(mode, self.model, is_train, cur_epoch)
                self.plotter.update(cur_epoch, loss, mode)
            self.saveRestorer.save(cur_epoch, loss, self.model)
        return


    def run_epoch(self, mode, model, is_train, cur_epoch):
        cur_it = -1
        epochTotalLoss = 0
        numbOfWordsInEpoch = 0

        if self.modelParam['inNotebook']:
            tt = tqdm_notebook(self.dataLoader.myDataDicts[mode], desc='', leave=True, mininterval=0.01, file=sys.stdout)
        else:
            # tt = tqdm_notebook(self.dataLoader.myDataDicts[mode], desc='', leave=True, mininterval=0.01,file=sys.stdout)
            tt = tqdm(self.dataLoader.myDataDicts[mode], desc='', leave=True, mininterval=0.01, file=sys.stdout)
        for dataDict in tt:
            for key in ['xTokens', 'yTokens', 'yWeights', 'vgg_fc7_features']:
                dataDict[key] = dataDict[key].to(model.device)
            cur_it += 1
            batchTotalLoss = 0
            numbOfWordsInBatch = 0
            for iter in range(dataDict['numbOfTruncatedSequences']):
                xTokens  = dataDict['xTokens'][:, :, iter]
                yTokens  = dataDict['yTokens'][:, :, iter]
                yWeights = dataDict['yWeights'][:, :, iter]
                vgg_fc7_features = dataDict['vgg_fc7_features']
                if iter==0:
                    logits, current_hidden_state = model.net(vgg_fc7_features, xTokens,  is_train)
                else:
                    logits, current_hidden_state_Ref = model.net(vgg_fc7_features, xTokens,  is_train, current_hidden_state)
                sumLoss, meanLoss = model.loss_fn(logits, yTokens, yWeights)

                if mode == 'train':
                    model.optimizer.zero_grad()
                    meanLoss.backward(retain_graph=True)
                    model.optimizer.step()

                batchTotalLoss += sumLoss.item()
                numbOfWordsInBatch += yWeights.sum().item()

            epochTotalLoss += batchTotalLoss
            numbOfWordsInEpoch +=numbOfWordsInBatch

            desc = f'{mode} | Epcohs={cur_epoch} | loss={batchTotalLoss/numbOfWordsInBatch:.4f}'
            tt.set_description(desc)
            tt.update()

            epochLoss = epochTotalLoss/numbOfWordsInEpoch
        return epochLoss
class ImagePreProcessor():

    def __init__(self):
        self.plotter = Plotter()

    def execute(self, image, readImage=True):
        """
        Pre process image to make it look as close as possible to emnist data set
        :param image:
        :param readImage:
        :return:
        """
        # create an array where we can store our picture
        images = np.zeros((1, 784))
        # and the correct values
        correct_vals = np.zeros((1, 10))
        # read the image and transform to black and white
        gray = 0
        if readImage:
            gray = cv2.imread(image, 0)
        else:
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            (threshi, img_bw) = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
        self.plotter.plot_image(gray) # Plot image in gray scale
        gray = self.cut(gray) # Cut image
        if len(gray) > 0:
            # resize the images and invert it (black background)
            gray = cv2.resize(255 - gray, (28, 28))
            self.plotter.plot_image(gray) # Plot cut image
            cv2.imwrite("test_images/image_1.png", gray) # Save image for testing purposes
            # better black and white version
            (thresh, gray) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
            self.plotter.plot_image(gray)

            """
            We want to fit the images into this 20x20 pixel box. 
            Therefore we need to remove every row and column at the sides 
            of the image which are completely black.
            """
            while np.sum(gray[0]) == 0:
                gray = gray[1:]

            while np.sum(gray[:, 0]) == 0:
                gray = np.delete(gray, 0, 1)

            while np.sum(gray[-1]) == 0:
                gray = gray[:-1]

            while np.sum(gray[:, -1]) == 0:
                gray = np.delete(gray, -1, 1)

            rows, cols = gray.shape

            """
            Now we want to resize our outer box to fit it into a 20x20 box. 
            We need a resize factor for this.
            """
            if rows > cols:
                factor = 20.0 / rows
                rows = 20
                cols = int(round(cols * factor))
                # first cols than rows
                gray = cv2.resize(gray, (cols, rows))
            else:
                factor = 20.0 / cols
                cols = 20
                rows = int(round(rows * factor))
                # first cols than rows
                gray = cv2.resize(gray, (cols, rows))
            """
            But at the end we need a 28x28 pixel image so we add the missing 
            black rows and columns using the np.lib.pad function which adds 0s 
            to the sides.
            """
            colsPadding = (int(math.ceil((28 - cols) / 2.0)), int(math.floor((28 - cols) / 2.0)))
            rowsPadding = (int(math.ceil((28 - rows) / 2.0)), int(math.floor((28 - rows) / 2.0)))
            gray = np.lib.pad(gray, (rowsPadding, colsPadding), 'constant')
            self.plotter.plot_image(gray)

            shiftx, shifty = self.get_best_shift(gray)
            shifted = self.shift(gray, shiftx, shifty)
            gray = shifted

            cv2.imwrite("test_images/image_2.png", gray)

            """
            all images in the training set have an range from 0-1
            and not from 0-255 so we divide our flatten images
            (a one dimensional vector with our 784 pixels)
            to use the same 0-1 based range
            """
            # im = gray / 255
        im = gray
        return im

    def cut(self, img):
        """
        Cut image
        :param img:
        :return:
        """
        left = img.shape[1]
        top = img.shape[0]
        right, bottom = 0, 0
        for i in range(img.shape[0]):
            for j in range(img.shape[1]):
                if img[i, j] != 255:
                    left = min(left, j)
                    right = max(right, j)
                    top = min(top, i)
                    bottom = max(bottom, i)
        length = bottom - top
        width = right - left
        left = max(0, left - int(length / 2))
        right = min(right + int(length / 2), img.shape[1])
        top = max(0, top - int(width / 2))
        bottom = min(bottom + int(width / 2), img.shape[0])
        print(str(left) + "," + str(right) + "," + str(top) + "," + str(bottom))
        img = img[top:bottom, left:right]
        return img

    def get_best_shift(self, img):
        cy, cx = ndimage.measurements.center_of_mass(img)
        rows, cols = img.shape
        shiftx = np.round(cols / 2.0 - cx).astype(int)
        shifty = np.round(rows / 2.0 - cy).astype(int)
        return shiftx, shifty

    def shift(self, img, sx, sy):
        rows, cols = img.shape
        M = np.float32([[1, 0, sx], [0, 1, sy]])
        shifted = cv2.warpAffine(img, M, (cols, rows))
        return shifted

    def rotate(self, image):
        """
        Rotate image and flip it
        :param image:
        :return:
        """
        image = image.reshape([HEIGHT, WIDTH])
        image = np.fliplr(image)
        image = np.rot90(image)
        return image

    def split_letters(self, img):
        """
        Method that takes an images and splits all the letters in the image, it does this by using contours
        :param image:
        :return:
        """
        images = []
        image = cv2.imread(img)
        height, width, depth = image.shape

        # resizing the image to find spaces better
        image = cv2.resize(image, dsize=(width * 5, height * 4), interpolation=cv2.INTER_CUBIC)

        # grayscale
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # binary
        ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)

        # dilation
        kernel = np.ones((5, 5), np.uint8)
        img_dilation = cv2.dilate(thresh, kernel, iterations=1)

        # adding GaussianBlur
        gsblur = cv2.GaussianBlur(img_dilation, (5, 5), 0)

        # find contours
        ctrs, hier = cv2.findContours(gsblur.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        m = list()
        # sort contours
        sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
        pchl = list()
        dp = image.copy()

        for i, ctr in enumerate(sorted_ctrs):
            # Get bounding box
            x, y, w, h = cv2.boundingRect(ctr)
            cv2.rectangle(dp, (x - 10, y - 10), (x + w + 10, y + h + 10), (90, 0, 255), 9)
            crop_img = image[y:y + h, x:x + w]
            bordersize = 700
            border = cv2.copyMakeBorder(
                crop_img,
                top=bordersize,
                bottom=bordersize,
                left=bordersize,
                right=bordersize,
                borderType=cv2.BORDER_CONSTANT,
                value=[255, 255, 255]
            )
            images.append(border)
            # border = cv2.resize(border, dsize=(28,28), interpolation=cv2.INTER_CUBIC)
            cv2.imwrite("cropped{0}.jpg".format(i), border)

            # roi = cv2.cvtColor(roi,cv2.COLOR_BGR2GRAY)
            # roi = np.array(roi)
            plt.imshow(border)
            plt.show()
        cv2.imwrite("boxes.jpg", dp)
        plt.imshow(dp)
        plt.show()
        return images
 def __init__(self):
     self.plotter = Plotter()
Esempio n. 26
0
    dyclee = Dyclee(dataContext=dataContext,
                    relativeSize=relativeSize,
                    uncommonDimensions=uncommonDimensions,
                    closenessThreshold=closenessThreshold)
    # start
    X = non_time_series_datasets[datIndx]['dataset']
    dName = non_time_series_datasets[datIndx]['name']
    k = non_time_series_datasets[datIndx]['k']
    baseFolder = getClusteringResultsPath() + dName + '/'
    # normalize dataset for easier parameter selection
    X = StandardScaler().fit_transform(X)
    ac = 0  # processed samples
    # iterate over the data points
    for dataPoint in X:  # column index
        ac += 1
        dyclee.trainOnElement(dataPoint)
    currMicroClusters = dyclee.getClusteringResult(
    )  # we wanna show the clustering at the end, only once
    res = prepareResultFrom(currMicroClusters)
    folder = baseFolder + getDycleeName() + '/'
    #  storeNonTimeSeriesResult(res, folder) # FIXME: uncomment!
    # store algo config
    algoConfig = dyclee.getConfig()
    #  storeAlgoConfig(algoConfig, folder) # FIXME: uncomment!
    # IMPORTANT: plotting outside dyclee
    p = Plotter(currMicroClusters, dataContext)
    # p.plotClusters() # FIXME: uncomment if you want all 3 plots to be displayed - and comment the whole following block
    ax = plt.gca()  # we try the one with the mc size
    p.plotMicroClustersSize(ax)
    plt.show()
Esempio n. 27
0
def roomTemperatureTuning():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "median, movingAverage", skip = 0, printLast = True)

	dimension = 20

	exp.run_experiments_multiple(RoomTemperature(), {},\
						 [VAR(),\
						  VAR(),\
						  VAR(),\
						  VAR(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD(),\
						  ECVARMAOGD()
						 ],\
						 [{'p' : 8, 'dim': dimension},\
						  {'p' : 16, 'dim': dimension},\
						  {'p' : 32, 'dim': dimension},\
						  {'p' : 64, 'dim': dimension},\
						  {'p' : 8, 'dim': dimension, 'lr': 1},\
						  {'p' : 16, 'dim': dimension, 'lr': 1},\
						  {'p' : 32, 'dim': dimension, 'lr': 1},\
						  {'p' : 64, 'dim': dimension, 'lr': 1},\
						  {'p' : 8, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 16, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 32, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 64, 'dim': dimension, 'lr': 0.1},\
						  {'p' : 8, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 16, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 32, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 64, 'dim': dimension, 'lr': 0.01},\
						  {'p' : 8, 'dim': dimension, 'lr': 0.001},\
						  {'p' : 16, 'dim': dimension, 'lr': 0.001},\
						  {'p' : 32, 'dim': dimension, 'lr': 0.001},\
						  {'p' : 64, 'dim': dimension, 'lr': 0.001}
						  ],\
						 ['VAR_8',\
						  'VAR_16',\
						  'VAR_32',\
						  'VAR_64',\
						  'ECVARMA_OGD8_1',\
						  'ECVARMA_OGD16_1',\
						  'ECVARMA_OGD32_1',\
						  'ECVARMA_OGD64_1',\
						  'ECVARMA_OGD8_01',\
						  'ECVARMA_OGD16_01',\
						  'ECVARMA_OGD32_01',\
						  'ECVARMA_OGD64_01',\
						  'ECVARMA_OGD8_001',\
						  'ECVARMA_OGD16_001',\
						  'ECVARMA_OGD32_001',\
						  'ECVARMA_OGD64_001',\
						  'ECVARMA_OGD8_0001',\
						  'ECVARMA_OGD16_0001',\
						  'ECVARMA_OGD32_0001',\
						  'ECVARMA_OGD64_0001'
						  ],\
						 n_runs = 5,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(RoomTemperature()))
 def __init__(self, X_train, y_train, mapp):
     self.X_train = X_train
     self.y_train = y_train
     self.mapp = mapp
     self.plotter = Plotter()
Esempio n. 29
0
def sp500Tuning():
	exp = Experiment()
	plotting = Plotter()
	plotting.initialize(filt = "movingAverage", skip = 100)
	exp.run_experiments_multiple(SP500(), {},\
						 [ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ARIMA(),\
						  ],\
						 [{'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 8, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 16, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 32, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 64, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 8, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 16, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 32, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':10.0})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':1.0})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.1})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.01})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.001})},\
						  {'p' : 64, 'd' : 2, 'optimizer': RealOGD(hyperparameters={'lr':0.0001})}
						  ],\
						 ['ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD',\
						  'ARIMA_OGD'
						  ],\
						 n_runs = 5,\
						 plotter = plotting,\
						 verbose = True,
						 action_generator = ProblemBasedAction(SP500()))
class Trainer():
    def __init__(self, opt, model, criterion, optimizer, optimState=None):
        self.model = model
        self.optimState = optimState
        self.opt = opt
        H = opt.hype
        
        self.optimizer = optimizer
        self.criterion = criterion

        if self.optimState is not None:
            self.optimizer.load_state_dict(self.optimState)
            
        self.num_bin = self.opt.hype['num_bin']
        self.grid_h = self.grid_w = self.opt.hype['grid_size']
        self.batch_size = opt.batch_size
        self.num_grids = self.grid_h * self.grid_w
        self.max_ratio = 1000.

        self.w0 = H['loss_weights'][:2]
        self.w1 = H['loss_weights'][2:4]

        self.plot_ = Plotter(opt.hype)
        self.saveDir = Path(self.opt.saveDir)

        if opt.lr_param['lr_policy'] == 'step':
            self.lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(
                self.optimizer, milestones=opt.lr_param['steppoints'], gamma=0.1)

        self.pretrained_model = ''

        self.test_junc_thresh_list = [0.1 * x for x in range(10)]
        self.train_junc_thresh_list = [0.1, 0.5]

        self.log_interval = 100
        self.logfile = ref.logdir / "{}.log".format(self.opt.exp)
        with open(self.logfile, 'a') as fn:
            fn.write('\n{}\n'.format(datetime.datetime.now()))

    def train(self, train_loader, val_loader=None):
        for epoch in range(1, self.opt.max_epochs + 1):
            self.lr_scheduler.step()
            self.step(epoch, train_loader)
            torch.save(self.model, self.saveDir / 'model_{}.pth'.format(epoch))
            if val_loader is not None:
               self.step(epoch, val_loader, is_val=True, split='val')

    def test(self, dataLoader, epoch, model_path):
        self.model = torch.load(model_path)
        self.model.eval()
            
        test_res_dir = ref.result_dir / self.opt.exp / str(epoch)
        self.test_dir = test_res_dir
        if not self.test_dir.is_dir():
            os.makedirs(self.test_dir)

        epoch_num_iter = len(dataLoader)
        bar = Bar('==>', max=epoch_num_iter * len(self.test_junc_thresh_list))
        
        start = time.time()
        for i, input_data in enumerate(dataLoader):
            inp, imgname, size_info = input_data

            input_var = torch.autograd.Variable(inp).float().cuda()
            junc_conf_var = None # torch.autograd.Variable(junc_conf).long().cuda()
            junc_res_var = None # torch.autograd.Variable(junc_res).float().cuda()
            bin_conf_var = None # torch.autograd.Variable(bin_conf).long().cuda()
            bin_res_var = None # torch.autograd.Variable(bin_res).float().cuda()

            (junction_logits,
                junction_loc,
                bin_logits,
                bin_residual
             ) = self.model(input_var, junc_conf_var, junc_res_var, bin_conf_var, bin_res_var)

            if self.plot_ is not None:
                junc_conf_result, junc_res_result, bin_conf_result, bin_res_result = self.conf_loc(
                        junction_logits, junction_loc, bin_logits, bin_residual)

                num_inp = inp.size(0)
                split='test'

                for junc_thresh in self.test_junc_thresh_list:
                    bar.suffix = '{split} epoch: [{0}][{1}/{2}]| threshold: {3}'.format(
                        epoch, i, epoch_num_iter, junc_thresh, split=split)
                    
                    test_thresh_dir = self.test_dir / str(int(10 * junc_thresh))
                    if not os.path.isdir(test_thresh_dir):
                        os.mkdir(test_thresh_dir)

                    for idx_inside in range(num_inp):
                        filename = imgname[idx_inside]
                        ## theta_thresh = 0.5
                        tmp2 = {}
                        tmp2['h'], tmp2['w'] = size_info[idx_inside]
                        (image, tmp2['junctions'], tmp2['thetas'], tmp2['theta_confs']) = self.plot_.plot_junction_simple(
                            inp[idx_inside].numpy(),
                            [junc_conf_result.data.cpu()[idx_inside].numpy(),
                             junc_res_result.data.cpu()[idx_inside].numpy(),
                             bin_conf_result.data.cpu()[idx_inside].numpy(),
                             bin_res_result.data.cpu()[idx_inside].numpy()
                             ],
                            junc_thresh=junc_thresh, 
                            theta_thresh=0.5, 
                            size_info = size_info[idx_inside],
                            keep_conf=True
                        )

                        imwrite("{}/{}_5.png".format(test_thresh_dir, filename), image)
                        sio.savemat("{}/{}.mat".format(test_thresh_dir, filename), tmp2)
                        with open(test_thresh_dir / "{}_5.pkl".format(filename), 'wb') as fn:
                            pickle.dump(tmp2, fn)
                    bar.next()
        bar.finish()
        return 0


    def step(self, epoch, dataLoader, is_training=True, is_val=False, is_testing=False, split='train'):
        if is_training:
            self.model.train()
        else:
            self.model.eval()

        iter_per_epoch = len(dataLoader)
        bar = Bar('==>', max=iter_per_epoch * len(self.test_junc_thresh_list) if is_testing else iter_per_epoch)
        
        Loss, LossJuncConf, LossJuncRes, LossBinConf, LossBinRes = AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter()
        
        start = time.time()
        for i, input_data in enumerate(dataLoader):
            inp, junc_conf, junc_res, bin_conf, bin_res, imgname, size_info = input_data

            input_var = torch.autograd.Variable(inp).float().cuda()
            junc_conf_var = torch.autograd.Variable(junc_conf).long().cuda()
            junc_res_var = torch.autograd.Variable(junc_res).float().cuda()
            bin_conf_var = torch.autograd.Variable(bin_conf).long().cuda()
            bin_res_var = torch.autograd.Variable(bin_res).float().cuda()
            # mask_var = torch.autograd.Variable(mask).float().cuda()

            (junction_logits,
               junction_loc,
               bin_logits,
               bin_residual
            ) = self.model(input_var, junc_conf, junc_res, bin_conf, bin_res)

            (loss,
                junc_conf_loss,
                junc_res_loss,
                bin_conf_loss,
                bin_res_loss
             ) = self.criterion(junction_logits, junction_loc, bin_logits, bin_residual, junc_conf_var, junc_res_var, bin_conf_var, bin_res_var)

            if not is_val:
                self.optimizer.zero_grad()
                loss.backward()
                #torch.nn.utils.clip_grad_norm(self.model.parameters(), 1.0)
                self.optimizer.step()
            Loss.update(
                loss.data[0],
                inp.size(0)
            )
            LossJuncConf.update(
                junc_conf_loss.data[0],
                inp.size(0)
            )
            LossBinConf.update(
                bin_conf_loss.data[0],
                inp.size(0)
            )
            LossJuncRes.update(
                junc_res_loss.data[0],
                inp.size(0)
            )
            LossBinRes.update(
                bin_res_loss.data[0],
                inp.size(0)
            )

            split = 'val' if is_val else 'train'
            elapsed  = time.time() - start
            remaining_seconds = elapsed * (iter_per_epoch - i) / (i+1)
            mins, secs = divmod(remaining_seconds, 60)
            hr, mins = divmod(mins, 60)
            rsecs = "{:02d}:{:02d}:{:02d}".format(int(hr), int(mins), int(secs))

            logStr = '{split} epoch: [{0}][{1}/{2}]| LR: {3:.6f}| Speed: {4:.2f}/s| Remaining: {5} | Loss {Loss.avg:.6f}|  JuncConf {JC.avg:.6f}| JuncRes {JR.avg:.6f}| BinConf {BC.avg:.6f}| BinRes {BR.avg:.6f}'.format(epoch, i, iter_per_epoch, self.lr_scheduler.get_lr()[0], float(i+1.)/elapsed, rsecs, split=split, Loss=Loss, JC=LossJuncConf, JR=LossJuncRes, BC=LossBinConf, BR=LossBinRes)


            bar.suffix = logStr
            if i == iter_per_epoch - 1:
                with open(self.logfile, 'a') as fn:
                    fn.write('{}\n'.format(logStr))
            bar.next()

            # if self.plot_ is not None:
            #     junc_conf_result, junc_res_result, bin_conf_result, bin_res_result = self.conf_loc(
            #             junction_logits, junction_loc, bin_logits, bin_residual)

            #     if is_training and i % self.log_interval == 0:                    
            #         filename = imgname[0]
            #         for junc_thresh in self.train_junc_thresh_list:
            #             (
            #                 image, _, _, _
            #             ) = self.plot_.plot_junction_simple(
            #                 inp[0].numpy(),
            #                 [junc_conf_result.data.cpu()[0].numpy(),
            #                  junc_res_result.data.cpu()[0].numpy(),
            #                  bin_conf_result.data.cpu()[0].numpy(),
            #                  bin_res_result.data.cpu()[0].numpy()],
            #                 junc_thresh=junc_thresh, 
            #                 theta_thresh=0.5, 
            #                 size_info=size_info[0]
            #             )
            #             imwrite( valres_dir / "{}_{}_{}_{}.png".format(epoch, int(i / self.log_interval), filename[:-4], int(junc_thresh * 10)), 
            #                         image)

        bar.finish()
        return 0

    def resume(self, model_path):
        pretrained_model = torch.load(model_path)
        pretrained_dict = pretrained_model.state_dict()
        model_dict = self.model.state_dict()
        model_dict.update(pretrained_dict)
        self.model.load_state_dict(model_dict)

    def conf_loc(self, junction_logits, junction_loc, bin_logits, bin_residual):
        junction_logits = junction_logits[:, :2, :, :]
        junc_conf_result = F.softmax(junction_logits, dim=1)
        junc_conf_result = junc_conf_result[:, 1, :, :]
        junc_res_result = junction_loc

        bin_logits = bin_logits.view(-1, 2, self.num_bin, self.grid_h, self.grid_w)
        bin_conf_result = F.softmax(bin_logits, dim=1)
        bin_conf_result = bin_conf_result[:, 1, :, :, :]
        bin_res_result = bin_residual

        return junc_conf_result, junc_res_result, bin_conf_result, bin_res_result