class VisdomMonitor(Wrapper):
    def __init__(self, env, cmdl):
        super(VisdomMonitor, self).__init__(env)

        self.freq = cmdl.report_freq  # in steps
        self.cmdl = cmdl

        if self.cmdl.display_plots:
            self.vis = Visdom()
            self.plot = self.vis.line(
                Y=np.array([0]), X=np.array([0]),
                opts=dict(
                    title=cmdl.label,
                    caption="Episodic reward per 1200 steps.")
            )

        self.step_cnt = 0
        self.ep_cnt = -1
        self.ep_rw = []
        self.last_reported_ep = 0

    def _step(self, action):
        # self._before_step(action)
        observation, reward, done, info = self.env.step(action)
        done = self._after_step(observation, reward, done, info)
        return observation, reward, done, info

    def _reset(self):
        self._before_reset()
        observation = self.env.reset()
        self._after_reset(observation)
        return observation

    def _after_step(self, o, r, done, info):
        self.ep_rw[self.ep_cnt] += r
        self.step_cnt += 1
        if self.step_cnt % self.freq == 0:
            self._update_plot()
        return done

    def _before_reset(self):
        self.ep_rw.append(0)

    def _after_reset(self, observation):
        self.ep_cnt += 1
        # print("[%2d][%4d]  RESET" % (self.ep_cnt, self.step_cnt))

    def _update_plot(self):
        # print(self.last_reported_ep, self.ep_cnt + 1)
        completed_eps = self.ep_rw[self.last_reported_ep:self.ep_cnt + 1]
        ep_mean_reward = sum(completed_eps) / len(completed_eps)
        if self.cmdl.display_plots:
            self.vis.line(
                X=np.array([self.step_cnt]),
                Y=np.array([ep_mean_reward]),
                win=self.plot,
                update='append'
            )
        self.last_reported_ep = self.ep_cnt + 1
    def set_model(self, model):
        """Save the model instance and initialize any required variables which would be updated throughout training.

        Parameters
        ----------
        model : :class:`~gensim.models.basemodel.BaseTopicModel`
            The model for which the training will be reported (logged or visualized) by the callback.

        """
        self.model = model
        self.previous = None
        # check for any metric which need model state from previous epoch
        if any(isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics):
            self.previous = copy.deepcopy(model)
            # store diff diagonals of previous epochs
            self.diff_mat = Queue()
        if any(metric.logger == "visdom" for metric in self.metrics):
            if not VISDOM_INSTALLED:
                raise ImportError("Please install Visdom for visualization")
            self.viz = Visdom()
            # store initial plot windows of every metric (same window will be updated with increasing epochs)
            self.windows = []
        if any(metric.logger == "shell" for metric in self.metrics):
            # set logger for current topic model
            self.log_type = logging.getLogger('gensim.models.ldamodel')
    def testCallbackUpdateGraph(self):

        # Popen have no context-manager in 2.7, for this reason - try/finally.
        try:
            # spawn visdom.server
            proc = subprocess.Popen(['python', '-m', 'visdom.server', '-port', str(self.port)])

            # wait for visdom server startup (any better way?)
            time.sleep(3)

            viz = Visdom(server=self.host, port=self.port)
            assert viz.check_connection()

            # clear screen
            viz.close()

            self.model.update(self.corpus)
        finally:
            proc.kill()
    def __init__(self, env, cmdl):
        super(VisdomMonitor, self).__init__(env)

        self.freq = cmdl.report_freq  # in steps
        self.cmdl = cmdl

        if self.cmdl.display_plots:
            self.vis = Visdom()
            self.plot = self.vis.line(
                Y=np.array([0]), X=np.array([0]),
                opts=dict(
                    title=cmdl.label,
                    caption="Episodic reward per 1200 steps.")
            )

        self.step_cnt = 0
        self.ep_cnt = -1
        self.ep_rw = []
        self.last_reported_ep = 0
    def __init__(self, env, cmdl):
        super(EvaluationMonitor, self).__init__(env)

        self.freq = cmdl.eval_freq  # in steps
        self.eval_steps = cmdl.evaluator.eval_steps
        self.cmdl = cmdl

        if self.cmdl.display_plots:
            self.vis = Visdom()
            self.plot = self.vis.line(
                Y=np.array([0]), X=np.array([0]),
                opts=dict(
                    title=cmdl.label,
                    caption="Episodic reward per %d steps." % self.eval_steps)
            )

        self.crt_step = 0
        self.step_cnt = 0
        self.ep_cnt = 0
        self.total_rw = 0
        self.max_mean_rw = -100
Exemple #6
0
 def __init__(self,
              env=None,
              log_checkpoints=True,
              losses=('loss', ),
              phases=('train', 'valid'),
              server='http://localhost',
              port=8097,
              max_y=None,
              **opts):
     self.viz = None
     if Visdom is not None:
         self.viz = Visdom(server=server, port=port, env=env)
     self.legend = ['%s.%s' % (p, l) for p in phases for l in losses]
     opts.update({'legend': self.legend})
     self.opts = opts
     self.env = env
     self.max_y = max_y
     self.log_checkpoints = log_checkpoints
     self.losses = set(losses)
     self.last = {p: {l: None for l in losses} for p in phases}
     self.pane = self._init_pane()
Exemple #7
0
def main():
    args = parser.parse_args()
    cf = ConfigParser.ConfigParser()
    try:
        cf.read(args.conf)
    except:
        print("conf file not exists")
    
    logger = init_logger(os.path.join(args.log_dir, 'train_cnn_lstm_ctc.log'))
    dataset = cf.get('Data', 'dataset')
    data_dir = cf.get('Data', 'data_dir')
    feature_type = cf.get('Data', 'feature_type')
    out_type = cf.get('Data', 'out_type')
    n_feats = cf.getint('Data', 'n_feats')
    batch_size = cf.getint("Training", 'batch_size')
    
    #Data Loader
    train_dataset = myDataset(data_dir, data_set='train', feature_type=feature_type, out_type=out_type, n_feats=n_feats)
    train_loader = myCNNDataLoader(train_dataset, batch_size=batch_size, shuffle=True,
                        num_workers=4, pin_memory=False)
    dev_dataset = myDataset(data_dir, data_set="test", feature_type=feature_type, out_type=out_type, n_feats=n_feats)
    dev_loader = myCNNDataLoader(dev_dataset, batch_size=batch_size, shuffle=False,
                        num_workers=4, pin_memory=False)
    
    #decoder for dev set
    decoder = GreedyDecoder(dev_dataset.int2phone, space_idx=-1, blank_index=0)
    
    #Define Model
    rnn_input_size = cf.getint('Model', 'rnn_input_size')
    rnn_hidden_size = cf.getint('Model', 'rnn_hidden_size')
    rnn_layers = cf.getint('Model', 'rnn_layers')
    rnn_type = RNN[cf.get('Model', 'rnn_type')]
    bidirectional = cf.getboolean('Model', 'bidirectional')
    batch_norm = cf.getboolean('Model', 'batch_norm')
    num_class = cf.getint('Model', 'num_class')
    drop_out = cf.getfloat('Model', 'num_class')
    model = CNN_LSTM_CTC(rnn_input_size=rnn_input_size, rnn_hidden_size=rnn_hidden_size, rnn_layers=rnn_layers, 
                        rnn_type=rnn_type, bidirectional=bidirectional, batch_norm=batch_norm, 
                        num_class=num_class, drop_out=drop_out)
    #model.apply(xavier_uniform_init)
    print(model.name)
    
    #Training
    init_lr = cf.getfloat('Training', 'init_lr')
    num_epoches = cf.getint('Training', 'num_epoches')
    end_adjust_acc = cf.getfloat('Training', 'end_adjust_acc')
    decay = cf.getfloat("Training", 'lr_decay')
    weight_decay = cf.getfloat("Training", 'weight_decay')
    try:
        seed = cf.getint('Training', 'seed')
    except:
        seed = torch.cuda.initial_seed()
    params = { 'num_epoches':num_epoches, 'end_adjust_acc':end_adjust_acc, 'seed':seed,
                'decay':decay, 'learning_rate':init_lr, 'weight_decay':weight_decay, 'batch_size':batch_size,
                'feature_type':feature_type, 'n_feats': n_feats, 'out_type': out_type }
    
    if USE_CUDA:
        torch.cuda.manual_seed(seed)
        model = model.cuda()
    
    print(params)

    loss_fn = CTCLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=init_lr, weight_decay=weight_decay)

    #visualization for training
    from visdom import Visdom
    viz = Visdom(env='863_corpus')
    title = dataset+' '+feature_type+str(n_feats)+' CNN_LSTM_CTC'
    opts = [dict(title=title+" Loss", ylabel = 'Loss', xlabel = 'Epoch'),
            dict(title=title+" CER on Train", ylabel = 'CER', xlabel = 'Epoch'),
            dict(title=title+' CER on DEV', ylabel = 'DEV CER', xlabel = 'Epoch')]
    viz_window = [None, None, None]
    
    count = 0
    learning_rate = init_lr
    acc_best = -100
    acc_best_true = -100
    adjust_rate_flag = False
    stop_train = False
    adjust_time = 0
    start_time = time.time()
    loss_results = []
    training_cer_results = []
    dev_cer_results = []
    
    while not stop_train:
        if count >= num_epoches:
            break
        count += 1
        
        if adjust_rate_flag:
            learning_rate *= decay
            adjust_rate_flag = False
            for param in optimizer.param_groups:
                param['lr'] *= decay
        
        print("Start training epoch: %d, learning_rate: %.5f" % (count, learning_rate))
        logger.info("Start training epoch: %d, learning_rate: %.5f" % (count, learning_rate))
        
        loss = train(model, train_loader, loss_fn, optimizer, logger, print_every=20)
        loss_results.append(loss)
        cer = dev(model, train_loader, decoder, logger)
        print("cer on training set is %.4f" % cer)
        logger.info("cer on training set is %.4f" % cer)
        training_cer_results.append(cer)
        acc = dev(model, dev_loader, decoder, logger)
        dev_cer_results.append(acc)
        
        #model_path_accept = './log/epoch'+str(count)+'_lr'+str(learning_rate)+'_cv'+str(acc)+'.pkl'
        #model_path_reject = './log/epoch'+str(count)+'_lr'+str(learning_rate)+'_cv'+str(acc)+'_rejected.pkl'
        
        if acc > (acc_best + end_adjust_acc):
            acc_best = acc
            adjust_rate_count = 0
            model_state = copy.deepcopy(model.state_dict())
            op_state = copy.deepcopy(optimizer.state_dict())
        elif (acc > acc_best - end_adjust_acc):
            adjust_rate_count += 1
            if acc > acc_best and acc > acc_best_true:
                acc_best_true = acc
                model_state = copy.deepcopy(model.state_dict())
                op_state = copy.deepcopy(optimizer.state_dict())
        else:
            adjust_rate_count = 0
        #torch.save(model.state_dict(), model_path_reject)
        print("adjust_rate_count:"+str(adjust_rate_count))
        print('adjust_time:'+str(adjust_time))
        logger.info("adjust_rate_count:"+str(adjust_rate_count))
        logger.info('adjust_time:'+str(adjust_time))

        if adjust_rate_count == 10:
            adjust_rate_flag = True
            adjust_time += 1
            adjust_rate_count = 0
            acc_best = acc_best_true
            model.load_state_dict(model_state)
            optimizer.load_state_dict(op_state)
        
        if adjust_time == 8:    
            stop_train = True   
        
        time_used = (time.time() - start_time) / 60
        print("epoch %d done, cv acc is: %.4f, time_used: %.4f minutes" % (count, acc, time_used))
        logger.info("epoch %d done, cv acc is: %.4f, time_used: %.4f minutes" % (count, acc, time_used))
        x_axis = range(count)
        y_axis = [loss_results[0:count], training_cer_results[0:count], dev_cer_results[0:count]]
        for x in range(len(viz_window)):
            if viz_window[x] is None:
                viz_window[x] = viz.line(X = np.array(x_axis), Y = np.array(y_axis[x]), opts = opts[x],)
            else:
                viz.line(X = np.array(x_axis), Y = np.array(y_axis[x]), win = viz_window[x], update = 'replace',)

    print("End training, best cv acc is: %.4f" % acc_best)
    logger.info("End training, best cv acc is: %.4f" % acc_best)
    best_path = os.path.join(args.log_dir, 'best_model'+'_cv'+str(acc_best)+'.pkl')
    cf.set('Model', 'model_file', best_path)
    cf.write(open(args.conf, 'w'))
    params['epoch']=count
    torch.save(CNN_LSTM_CTC.save_package(model, optimizer=optimizer, epoch=params, loss_results=loss_results, training_cer_results=training_cer_results, dev_cer_results=dev_cer_results), best_path)
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.init as init
from torch.autograd import Variable
from visdom import Visdom
viz = Visdom()

num_data=1000
num_epoch=400

x = init.uniform(torch.Tensor(num_data,1),-10,10)
y = init.uniform(torch.Tensor(num_data,1),-10,10)
z = x**2 + y**2

x_noise = x + init.normal(torch.FloatTensor(num_data,1),std=0.5)
y_noise = y + init.normal(torch.FloatTensor(num_data,1),std=0.5)
z_noise = x_noise**2 + y_noise**2
data_noise = torch.cat([x_noise,y_noise,z_noise],1)

# visualize data

win_1=viz.scatter(
		X=data_noise,
		opts=dict(
			markersize=5,
			markercolor=np.ndarray(shape=[num_data,3],dtype=float,buffer=[51,153,255]*np.ones(shape=[num_data,3]))
			)
		)
from visdom import Visdom
import numpy as np
import math

vis = Visdom()

vis.heatmap(X=np.outer(np.arange(1, 6), np.arange(1, 11)),
            opts=dict(
                columnnames=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
                rownames=['y1', 'y2', 'y3', 'y4', 'y5'],
                colormap='Electric',
            ))
            nn.Linear(200, 10),
            nn.LeakyReLU(inplace=True),
        )

    def forward(self, x):
        x = self.model(x)

        return x


device = torch.device('cuda:0')
net = MLP().to(device)
optimizer = optim.SGD(net.parameters(), lr=learning_rate, weight_decay=0.01)
criteon = nn.CrossEntropyLoss().to(device)

viz = Visdom()

viz.line([0.], [0.], win='train_loss', opts=dict(title='train loss'))
viz.line([[0.0, 0.0]], [0.],
         win='test',
         opts=dict(title='test loss&acc.', legend=['loss', 'acc.']))
global_step = 0

for epoch in range(epochs):

    for batch_idx, (data, target) in enumerate(train_loader):
        data = data.view(-1, 28 * 28)
        data, target = data.to(device), target.cuda()

        logits = net(data)
        loss = criteon(logits, target)
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from visdom import Visdom
import time
import numpy as np

try:
    viz = Visdom()

    startup_sec = 1
    while not viz.check_connection() and startup_sec > 0:
        time.sleep(0.1)
        startup_sec -= 0.1
    assert viz.check_connection(), 'No connection could be formed quickly'

    # image callback demo
    def show_color_image_window(color, win=None):
        image = np.full([3, 256, 256], color, dtype=float)
        return viz.image(
            image,
            opts=dict(title='Colors', caption='Press arrows to alter color.'),
            win=win
        )
Exemple #12
0
import MyDataloader
from TumorNetwithoutSource import *

import csv
import pandas as pd
import SimpleITK as sitk
from medpy import metric
import numpy as np
import time
import shutil
import sys
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

from visdom import Visdom
viz = Visdom(env='PiaNet TumorNet without Source 134')
viz.line([0], [0], win='train')
viz.line([0], [0], win='valid')
viz.line([0], [0], win='tumor')


#################initialization network##############
def weights_init(model):
    if isinstance(model, nn.Conv3d) or isinstance(model, nn.ConvTranspose3d):
        nn.init.kaiming_uniform_(model.weight.data, 0.25)
        nn.init.constant_(model.bias.data, 0)
    # elif isinstance(model, nn.InstanceNorm3d):
    # 	nn.init.constant_(model.weight.data,1.0)
    # 	nn.init.constant_(model.bias.data, 0)

Exemple #13
0
class Callback(object):
    """A class representing routines called reactively at specific phases during trained.

    These can be used to log or visualize the training progress using any of the metric scores developed before.
    The values are stored at the end of each training epoch. The following metric scores are currently available:

        * :class:`~gensim.models.callbacks.CoherenceMetric`
        * :class:`~gensim.models.callbacks.PerplexityMetric`
        * :class:`~gensim.models.callbacks.DiffMetric`
        * :class:`~gensim.models.callbacks.ConvergenceMetric`

    """
    def __init__(self, metrics):
        """

        Parameters
        ----------
        metrics : list of :class:`~gensim.models.callbacks.Metric`
            The list of metrics to be reported by the callback.

        """
        self.metrics = metrics

    def set_model(self, model):
        """Save the model instance and initialize any required variables which would be updated throughout training.

        Parameters
        ----------
        model : :class:`~gensim.models.basemodel.BaseTopicModel`
            The model for which the training will be reported (logged or visualized) by the callback.

        """
        self.model = model
        self.previous = None
        # check for any metric which need model state from previous epoch
        if any(isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics):
            self.previous = copy.deepcopy(model)
            # store diff diagonals of previous epochs
            self.diff_mat = Queue()
        if any(metric.logger == "visdom" for metric in self.metrics):
            if not VISDOM_INSTALLED:
                raise ImportError("Please install Visdom for visualization")
            self.viz = Visdom()
            # store initial plot windows of every metric (same window will be updated with increasing epochs)
            self.windows = []
        if any(metric.logger == "shell" for metric in self.metrics):
            # set logger for current topic model
            self.log_type = logging.getLogger('gensim.models.ldamodel')

    def on_epoch_end(self, epoch, topics=None):
        """Report the current epoch's metric value.

        Called at the end of each training iteration.

        Parameters
        ----------
        epoch : int
            The epoch that just ended.
        topics : list of list of str, optional
            List of tokenized topics. This is required for the coherence metric.

        Returns
        -------
        dict of (str, object)
            Mapping from metric names to their values. The type of each value depends on the metric type,
            for example :class:`~gensim.models.callbacks.DiffMetric` computes a matrix while
            :class:`~gensim.models.callbacks.ConvergenceMetric` computes a float.

        """
        # stores current epoch's metric values
        current_metrics = {}

        # plot all metrics in current epoch
        for i, metric in enumerate(self.metrics):
            label = str(metric)
            value = metric.get_value(topics=topics, model=self.model, other_model=self.previous)

            current_metrics[label] = value

            if metric.logger == "visdom":
                if epoch == 0:
                    if value.ndim > 0:
                        diff_mat = np.array([value])
                        viz_metric = self.viz.heatmap(
                            X=diff_mat.T, env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label)
                        )
                        # store current epoch's diff diagonal
                        self.diff_mat.put(diff_mat)
                        # saving initial plot window
                        self.windows.append(copy.deepcopy(viz_metric))
                    else:
                        viz_metric = self.viz.line(
                            Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env,
                            opts=dict(xlabel='Epochs', ylabel=label, title=label)
                        )
                        # saving initial plot window
                        self.windows.append(copy.deepcopy(viz_metric))
                else:
                    if value.ndim > 0:
                        # concatenate with previous epoch's diff diagonals
                        diff_mat = np.concatenate((self.diff_mat.get(), np.array([value])))
                        self.viz.heatmap(
                            X=diff_mat.T, env=metric.viz_env, win=self.windows[i],
                            opts=dict(xlabel='Epochs', ylabel=label, title=label)
                        )
                        self.diff_mat.put(diff_mat)
                    else:
                        self.viz.line(
                            Y=np.array([value]),
                            X=np.array([epoch]),
                            env=metric.viz_env,
                            win=self.windows[i],
                            update='append'
                        )

            if metric.logger == "shell":
                statement = "".join(("Epoch ", str(epoch), ": ", label, " estimate: ", str(value)))
                self.log_type.info(statement)

        # check for any metric which need model state from previous epoch
        if any(isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics):
            self.previous = copy.deepcopy(self.model)

        return current_metrics
def get_c(inflection, b):
    """
    c = log(b) / inflection
    """
    return math.log(b) / inflection


if __name__ == '__main__':
    from argparse import ArgumentParser
    parser = ArgumentParser()
    parser.add_argument('--env', default='sigmoid_schedule')
    parser.add_argument('--inflection', default=5000, type=int)
    args = parser.parse_args()

    from visdom import Visdom
    viz = Visdom(env=args.env)

    import numpy as np
    Y = np.linspace(1, args.inflection * 2, 1000)

    for b in (10, 100, 1000):
        c = get_c(args.inflection, b)
        title = 'c=%.g;b=%d;inflection=%d' % (c, b, args.inflection)
        viz.line(np.array([generic_sigmoid(b=b, c=c)(i) for i in Y]), Y,
                 opts={'title': title})
    for c in (0.0001, 0.001, 0.005):
        b = get_b(args.inflection, c)
        title = 'c=%.g;b=%d;inflection=%d' % (c, b, args.inflection)
        viz.line(np.array([generic_sigmoid(b=b, c=c)(i) for i in Y]), Y,
                 opts={'title': title})
 def __init__(self, env_name='main'):
     self.viz = Visdom()
     self.env = env_name
     self.scatters = {}
    image = np.transpose(image, (2, 0, 1))

    all_timesteps = tx
    all_rewards = ty

    # np.save('./results_temp/rewards.npy', all_rewards)
    # np.save('./results_temp/timesteps.npy', all_timesteps)

    # return viz.image(image, win=win)
    return viz.image(image, win=win)


def get_stats(folder, smooth=1, bin_size=100):
    tx, ty = load_data(folder, smooth, bin_size)
    all_timesteps = tx
    all_rewards = ty

    return all_timesteps, all_rewards


if __name__ == "__main__":
    from visdom import Visdom
    viz = Visdom()
    visdom_plot(viz,
                None,
                './results_temp/',
                'BreakOut',
                'a2c',
                bin_size=100,
                smooth=1)
Exemple #17
0
 def __init__(self):
     try:
         from visdom import Visdom
         viz = Visdom(port=8097, server='http://localhost')
     except socket_error as e:
         viz = None
Exemple #18
0
 def __init__(self, server, port, env_name='main'):
     self.viz = Visdom(server=server, port=port)
     self.env = env_name
     self.plots = {}
     self.opts = {}
Exemple #19
0
def main():
    print("#######")
    print(
        "WARNING: All rewards are clipped or normalized so you need to use a monitor (see envs.py) or visdom plot to get true rewards"
    )
    print("#######")

    os.environ['OMP_NUM_THREADS'] = '1'

    if args.vis:
        from visdom import Visdom
        viz = Visdom()
        win = None

    envs = [
        make_env(args.env_name, args.seed, i, args.log_dir)
        for i in range(args.num_processes)
    ]

    if args.num_processes > 1:
        envs = SubprocVecEnv(envs)
    else:
        envs = DummyVecEnv(envs)

    if len(envs.observation_space.shape) == 1:
        envs = VecNormalize(envs)

    obs_shape = envs.observation_space.shape
    obs_shape = (obs_shape[0] * args.num_stack, *obs_shape[1:]
                 )  # I guess the obs_shape[0] is channel number

    if len(envs.observation_space.shape) == 3:
        actor_critic = CNNPolicy(obs_shape[0], envs.action_space,
                                 args.recurrent_policy)
    else:
        assert not args.recurrent_policy, \
            "Recurrent policy is not implemented for the MLP controller"
        actor_critic = MLPPolicy(obs_shape[0], envs.action_space)

    if envs.action_space.__class__.__name__ == "Discrete":
        action_shape = 1
    else:
        action_shape = envs.action_space.shape[0]

    if args.cuda:
        actor_critic.cuda()

    if args.algo == 'a2c':
        optimizer = optim.RMSprop(actor_critic.parameters(),
                                  args.lr,
                                  eps=args.eps,
                                  alpha=args.alpha)
    elif args.algo == 'ppo':
        optimizer = optim.Adam(actor_critic.parameters(),
                               args.lr,
                               eps=args.eps)
    elif args.algo == 'acktr':
        optimizer = KFACOptimizer(actor_critic)

    rollouts = RolloutStorage(args.num_steps, args.num_processes, obs_shape,
                              envs.action_space, actor_critic.state_size)
    current_obs = torch.zeros(args.num_processes, *obs_shape)

    def update_current_obs(obs):
        shape_dim0 = envs.observation_space.shape[0]
        obs = torch.from_numpy(obs).float()
        if args.num_stack > 1:
            current_obs[:, :-shape_dim0] = current_obs[:, shape_dim0:]
        current_obs[:, -shape_dim0:] = obs

    obs = envs.reset()
    update_current_obs(obs)

    rollouts.observations[0].copy_(current_obs)

    # These variables are used to compute average rewards for all processes.
    episode_rewards = torch.zeros([args.num_processes, 1])
    final_rewards = torch.zeros([args.num_processes, 1])

    if args.cuda:
        current_obs = current_obs.cuda()
        rollouts.cuda()

    start = time.time()
    for j in range(num_updates):
        for step in range(args.num_steps):
            # args.num_steps should be the length of interactions before each updating/training
            # Sample actions
            value, action, action_log_prob, states = actor_critic.act(
                Variable(rollouts.observations[step], volatile=True),
                Variable(rollouts.states[step], volatile=True),
                Variable(rollouts.masks[step], volatile=True))
            cpu_actions = action.data.squeeze(1).cpu().numpy(
            )  # returns are state value, sampled action, act_log_prob, hidden states

            # Obser reward and next obs
            obs, reward, done, info = envs.step(cpu_actions)
            reward = torch.from_numpy(np.expand_dims(np.stack(reward),
                                                     1)).float()
            episode_rewards += reward

            # If done then clean the history of observations.
            masks = torch.FloatTensor([[0.0] if done_ else [1.0]
                                       for done_ in done])
            final_rewards *= masks
            final_rewards += (1 - masks) * episode_rewards
            episode_rewards *= masks

            if args.cuda:
                masks = masks.cuda()

            if current_obs.dim() == 4:
                current_obs *= masks.unsqueeze(2).unsqueeze(2)
            else:
                current_obs *= masks

            update_current_obs(obs)
            rollouts.insert(
                step, current_obs, states.data, action.data,
                action_log_prob.data, value.data, reward, masks
            )  # so the rollout stores one batch of interaction sequences, each sequence has length of args.num_steps

        next_value = actor_critic(
            Variable(rollouts.observations[-1], volatile=True),
            Variable(rollouts.states[-1], volatile=True),
            Variable(rollouts.masks[-1], volatile=True))[0].data

        rollouts.compute_returns(next_value, args.use_gae, args.gamma,
                                 args.tau)

        if args.algo in ['a2c', 'acktr']:
            values, action_log_probs, dist_entropy, states = actor_critic.evaluate_actions(
                Variable(rollouts.observations[:-1].view(-1, *obs_shape)),
                Variable(rollouts.states[0].view(-1, actor_critic.state_size)),
                Variable(rollouts.masks[:-1].view(-1, 1)),
                Variable(rollouts.actions.view(-1, action_shape)))
            # values should be values of observations, states are the hidden states used in rnn module, by pwang8

            values = values.view(
                args.num_steps, args.num_processes,
                1)  # values are estimated current state values
            action_log_probs = action_log_probs.view(args.num_steps,
                                                     args.num_processes, 1)

            # rollouts.returns are current "Action" value calculted following Bellmans' eqaution gamma * State_value(t+1) + reward(t)
            advantages = Variable(
                rollouts.returns[:-1]
            ) - values  # This is also the definition of advantage value (action_value - state_value).
            value_loss = advantages.pow(
                2).mean()  # values are estimated current state_value(t)

            action_loss = -(Variable(advantages.data) *
                            action_log_probs).mean()

            # If ACKTR is utilized, it is not only a different optimizer is used, they also added some new loss source
            if args.algo == 'acktr' and optimizer.steps % optimizer.Ts == 0:
                # Sampled fisher, see Martens 2014
                actor_critic.zero_grad()
                pg_fisher_loss = -action_log_probs.mean()

                value_noise = Variable(torch.randn(values.size()))
                if args.cuda:
                    value_noise = value_noise.cuda()

                sample_values = values + value_noise
                vf_fisher_loss = -(
                    values - Variable(sample_values.data)
                ).pow(2).mean(
                )  # don't know what is the difference between this and just randomly sample some noise

                fisher_loss = pg_fisher_loss + vf_fisher_loss
                optimizer.acc_stats = True
                fisher_loss.backward(retain_graph=True)
                optimizer.acc_stats = False

            optimizer.zero_grad()
            (value_loss * args.value_loss_coef + action_loss -
             dist_entropy * args.entropy_coef).backward()

            if args.algo == 'a2c':
                nn.utils.clip_grad_norm(actor_critic.parameters(),
                                        args.max_grad_norm)

            optimizer.step()
        elif args.algo == 'ppo':
            advantages = rollouts.returns[:-1] - rollouts.value_preds[:
                                                                      -1]  # calculating the advantage value of an action
            advantages = (advantages - advantages.mean()) / (advantages.std() +
                                                             1e-5)

            # The difference from this ppo optimization to the optimization above is that: it updates params for
            # multiple epochs in ppo optimization. Because of this, it samples from the rollouts storage a minibatch
            # every time to calculate gradient. Sampling is conducted for optimization purpose.
            for e in range(args.ppo_epoch):
                if args.recurrent_policy:
                    data_generator = rollouts.recurrent_generator(
                        advantages, args.num_mini_batch)
                else:
                    data_generator = rollouts.feed_forward_generator(
                        advantages, args.num_mini_batch)

                for sample in data_generator:
                    observations_batch, states_batch, actions_batch, \
                       return_batch, masks_batch, old_action_log_probs_batch, \
                            adv_targ = sample

                    # Reshape to do in a single forward pass for all steps
                    values, action_log_probs, dist_entropy, states = actor_critic.evaluate_actions(
                        Variable(observations_batch), Variable(states_batch),
                        Variable(masks_batch), Variable(actions_batch))
                    # For the 1st epoch of updating, I guess the action_log_probls is the same as old_action_log_probs_batch
                    # because params of the NN have not been updated at that time. But later, in other updating epochs,
                    # this ratio will generate some error. The old_action_log_probs_batch will not be updated during
                    # these param updating epochs.
                    # action_log_probs is the log prob of that action taken by the agent. So it's one value here, not
                    # log_prob for all actions with certain input observation/state. By pwang8, Dec 31, 2017
                    adv_targ = Variable(adv_targ)
                    ratio = torch.exp(action_log_probs -
                                      Variable(old_action_log_probs_batch))
                    surr1 = ratio * adv_targ
                    surr2 = torch.clamp(ratio, 1.0 - args.clip_param,
                                        1.0 + args.clip_param) * adv_targ
                    action_loss = -torch.min(
                        surr1,
                        surr2).mean()  # PPO's pessimistic surrogate (L^CLIP)
                    # compared to a2c, the major difference for ppo is that action_loss is calculated in controlled way
                    value_loss = (Variable(return_batch) -
                                  values).pow(2).mean()

                    optimizer.zero_grad()
                    (value_loss + action_loss -
                     dist_entropy * args.entropy_coef).backward()
                    nn.utils.clip_grad_norm(actor_critic.parameters(),
                                            args.max_grad_norm)
                    optimizer.step()

        rollouts.after_update()

        if j % args.save_interval == 0 and args.save_dir != "":
            save_path = os.path.join(args.save_dir, args.algo)
            try:
                os.makedirs(save_path)
            except OSError:
                pass

            # A really ugly way to save a model to CPU
            save_model = actor_critic
            if args.cuda:
                save_model = copy.deepcopy(actor_critic).cpu()

            save_model = [
                save_model,
                hasattr(envs, 'ob_rms') and envs.ob_rms or None
            ]

            torch.save(save_model,
                       os.path.join(save_path, args.env_name + ".pt"))

        if j % args.log_interval == 0:
            end = time.time()
            total_num_steps = (j + 1) * args.num_processes * args.num_steps
            print(
                "Updates {}, num timesteps {}, FPS {}, mean/median reward {:.1f}/{:.1f}, min/max reward {:.1f}/{:.1f}, entropy {:.5f}, value loss {:.5f}, policy loss {:.5f}"
                .format(j, total_num_steps,
                        int(total_num_steps / (end - start)),
                        final_rewards.mean(), final_rewards.median(),
                        final_rewards.min(), final_rewards.max(),
                        dist_entropy.data[0], value_loss.data[0],
                        action_loss.data[0]))
        if args.vis and j % args.vis_interval == 0:
            try:
                # Sometimes monitor doesn't properly flush the outputs
                win = visdom_plot(viz, win, args.log_dir, args.env_name,
                                  args.algo)
            except IOError:
                pass
Exemple #20
0
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 23 10:43:59 2018

@author: Administrator
"""

from visdom import Visdom

vis = Visdom()

svgstr = """
<svg height="300" width="300">
  <ellipse cx="80" cy="80" rx="50" ry="30"
   style="fill:red;stroke:purple;stroke-width:2" />
  抱歉,你的浏览器不支持在线显示SVG对象.
</svg>
"""
vis.svg(svgstr=svgstr, opts=dict(title='SVG图像'))
Exemple #21
0
class VisdomLinePlotter(object):
    """Plots to Visdom"""
    def __init__(self, env_name='main'):
        self.viz = Visdom()
        self.env = env_name
        self.plots = {}
        self.moving = {}
        self.moving_counter = {}
        self.rotation = torch.Tensor(
            R.from_euler('zyz', [-90, 0, 0], degrees=True).as_dcm())
        print("Rotation", self.rotation)

    def plot_point_cloud(self, points, var_name):
        if (len(list(points.shape)) == 3):
            points = points.view(3, -1)
        points = points.transpose(1, 0)
        points = torch.mm(points, self.rotation)

        # Rotate matrix to plot better

        print(points.shape)
        if var_name not in self.plots:
            self.plots[var_name] = self.viz.scatter(X=points,
                                                    env=self.env,
                                                    name="pointplot",
                                                    opts=dict(markersize=1))
            # print("YOU HAVE 10s to rotate the graph")
            # time.sleep(10)
        else:
            self.viz.scatter(X=points,
                             env=self.env,
                             name="pointplot",
                             win=self.plots[var_name],
                             opts=dict(markersize=1))

    def plot(self, var_name, split_name, title_name, x, y):
        if var_name not in self.plots:
            self.plots[var_name] = self.viz.line(X=np.array([x, x]),
                                                 Y=np.array([y, y]),
                                                 env=self.env,
                                                 opts=dict(legend=[split_name],
                                                           title=title_name,
                                                           xlabel='Epochs',
                                                           ylabel=var_name))
        else:
            self.viz.line(X=np.array([x]),
                          Y=np.array([y]),
                          env=self.env,
                          win=self.plots[var_name],
                          name=split_name,
                          update='append')

    def plot_moving_avg(self,
                        var_name,
                        split_name,
                        title_name,
                        x,
                        y,
                        window=10):
        if var_name + split_name not in self.moving:
            self.moving[var_name + split_name] = np.zeros(window)
            self.moving_counter[var_name + split_name] = 0

        self.moving_counter[var_name + split_name] += 1
        index = self.moving_counter[var_name + split_name]
        self.moving[var_name + split_name][index % window] = y

        if self.moving_counter[var_name + split_name] >= window:
            avg = self.moving[var_name + split_name].mean()
            if var_name not in self.plots:
                self.plots[var_name] = self.viz.line(X=np.array([avg, avg]),
                                                     Y=np.array([y, y]),
                                                     env=self.env,
                                                     opts=dict(
                                                         legend=[split_name],
                                                         title=title_name,
                                                         xlabel='Epochs',
                                                         ylabel=var_name))
            else:
                self.viz.line(X=np.array([x]),
                              Y=np.array([avg]),
                              env=self.env,
                              win=self.plots[var_name],
                              name=split_name,
                              update='append')
 def __init__(self, env_name='main', heatsize=None):
     self.vis = Visdom()
     self.env = env_name
     self.hmaps = {}
     self.heatsize = heatsize
Exemple #23
0
class Visualizer:

    def __init__(self, env="main",
                 server="http://localhost",
                 port=8097,
                 base_url="/",
                 http_proxy_host=None,
                 http_proxy_port=None,
                 log_to_filename=None):
        self._viz = Visdom(env=env,
                           server=server,
                           port=port,
                           http_proxy_host=http_proxy_host,
                           http_proxy_port=http_proxy_port,
                           log_to_filename=log_to_filename,
                           use_incoming_socket=False)
        self._viz.close(env=env)

    def plot_line(self, values, steps, name, legend=None):
        if legend is None:
            opts = dict(title=name)
        else:
            opts = dict(title=name, legend=legend)

        self._viz.line(
            X=numpy.column_stack(steps),
            Y=numpy.column_stack(values),
            win=name,
            update='append',
            opts=opts
        )

    def plot_text(self, text, title, pre=True):
        _width = max([len(x) for x in text.split("\n")]) * 10
        _heigth = len(text.split("\n")) * 20
        _heigth = max(_heigth, 120)
        if pre:
            text = "<pre>{}</pre>".format(text)

        self._viz.text(text, win=title, opts=dict(title=title,
                                                  width=min(_width, 400),
                                                  height=min(_heigth, 400)))

    def plot_bar(self, data, labels, title):
        self._viz.bar(win=title, X=data,
                      opts=dict(legend=labels, stacked=False, title=title))

    def plot_scatter(self, data, labels, title):
        X = numpy.concatenate(data, axis=0)
        Y = numpy.concatenate([numpy.full(len(d), i)
                               for i, d in enumerate(data, 1)], axis=0)
        self._viz.scatter(win=title, X=X, Y=Y,
                          opts=dict(legend=labels, title=title,
                                    markersize=5,
                                    webgl=True,
                                    width=400,
                                    height=400,
                                    markeropacity=0.5))

    def plot_heatmap(self, data, labels, title):
        self._viz.heatmap(win=title,
                          X=data,
                          opts=dict(
                              title=title,
                              columnnames=labels[1],
                              rownames=labels[0],
                              width=700,
                              height=700,
                              layoutopts={'plotly': {
                                  'xaxis': {
                                      'side': 'top',
                                      'tickangle': -60,
                                      # 'autorange': "reversed"
                                  },
                                  'yaxis': {
                                      'autorange': "reversed"
                                  },
                              }
                              }
                          ))
       "_" + Configure.dataDistribution + \
       "_" + dataParameter

if Configure.standard == 'max':
    name += "_max"
# if Configure.cases!=None:
#     name+=Configure.cases
if Configure.relate:
    name += "_relate"

pool = None
if Configure.mulThread > 0:
    pool = threadpool.ThreadPool(Configure.mulThread)
id = 0

viz = Visdom(server="114.212.82.243", env=name)


class experimentThread(threading.Thread):
    def __init__(self, _allDeviceNum, _ratio, errorDistribution,
                 errorParameter, dataDistribution, dataParameter, id):
        threading.Thread.__init__(self)
        self._allDeviceNum = _allDeviceNum
        self._ratio = _ratio
        self.errorDistribution = errorDistribution
        self.errorParameter = errorParameter
        self.dataDistribution = dataDistribution
        self.dataParameter = dataParameter
        self.id = id
        self.success = False
Exemple #25
0
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torchvision
import torchvision.transforms as transforms
from models import nin
from visdom import Visdom
import thop
import time
from thop import profile
from models import nin_gc
from models import standard_dw
viz = Visdom()

line = viz.line(np.arange(10))


# 随机种子——训练结果可复现
def setup_seed(seed):
    # 为CPU设置种子用于生成随机数,以使得结果是确定的
    torch.manual_seed(seed)
    #torch.cuda.manual_seed(seed)
    # 为GPU设置种子用于生成随机数,以使得结果是确定的
    torch.cuda.manual_seed_all(seed)
    # 为numpy设置种子用于生成随机数,以使得结果是确定的
    np.random.seed(seed)
    # 将会让程序在开始时花费一点额外时间,为整个网络的每个卷积层
    # 搜索最适合它的卷积实现算法,进而实现网络的加速。适用场景是
Exemple #26
0
def get_scheduler(optimizer, opt):
    """Return a learning rate scheduler

    Parameters:
        optimizer          -- the optimizer of the network
        opt (option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions锛庛€€
                              opt.lr_policy is the name of learning rate policy: linear | step | plateau | cosine

    For 'linear', we keep the same learning rate for the first <opt.niter> epochs
    and linearly decay the rate to zero over the next <opt.niter_decay> epochs.
    For other schedulers (step, plateau, and cosine), we use the default PyTorch schedulers.
    See https://pytorch.org/docs/stable/optim.html for more details.
    """
    if opt.lr_policy == 'linear':

        def lambda_rule(step):
            lr_l = 1.0 - max(0, step - opt.niter) / float(opt.niter_decay + 1)
            return lr_l

        scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)

    elif opt.lr_policy == 'step':
        scheduler = lr_scheduler.StepLR(optimizer,
                                        step_size=opt.lr_decay_iters,
                                        gamma=0.1)

    elif opt.lr_policy == 'plateau':
        #scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.2, threshold=0.01, patience=5)
        scheduler = lr_scheduler.ReduceLROnPlateau(
            optimizer,
            mode='min',
            factor=opt.lr_reduce_factor,
            threshold=opt.lr_reduce_threshold,
            patience=opt.step_patience,
            min_lr=opt.min_lr)

    elif opt.lr_policy == 'cosine':
        scheduler = lr_scheduler.CosineAnnealingLR(optimizer,
                                                   T_max=opt.niter,
                                                   eta_min=0)

    elif opt.lr_policy == 'warmup':
        # opt.factor = 2
        # opt.warmup = 4000
        # optimizer, factor, d_model, warmup_steps=4000, init_steps = 0
        scheduler = WarmupLR(optimizer=optimizer,
                             factor=opt.lr_factor,
                             d_model=opt.d_model,
                             warmup_steps=opt.warmup_step,
                             init_steps=opt.steps,
                             visdom_freq_steps=opt.visdom_freq_steps)
        from visdom import Visdom
        vis = Visdom(server=opt.display_server,
                     port=opt.display_port,
                     env=opt.visdom_id + 'Learning Rate',
                     raise_exceptions=True)
        scheduler.set_visdom(visdom_lr=opt.visdom_lr, vis=vis)
    else:
        return NotImplementedError(
            'learning rate policy [%s] is not implemented', opt.lr_policy)
    return scheduler
 def __init__(self, env_name='main'):
     self.viz = Visdom()
     self.env = env_name
     self.plots = {}
Exemple #28
0
                    help='IP of the visdom server')
parser.add_argument('--visdom_port',
                    type=int,
                    default=DEFAULT_VISDOM_PORT,
                    help='IP port of the visdom server')
parser.add_argument('--value',
                    type=str,
                    default=DEFAULT_DATA_VALUE,
                    help='Y value for the line plot')
args = parser.parse_args()

print("Connecting to visdom server on ", args.visdom_host, ":",
      args.visdom_port)
value = args.value

viz = Visdom(server="http://" + args.visdom_host, port=args.visdom_port)
assert viz.check_connection()

#if not viz.win_exists(WIN):
#  viz.text("Bitcoin variation notifications:\n", win=WIN)

if os.path.exists(store_file):
    f = open(store_file, 'rb')
    iteration = int(pickle.load(f))
    f.close()
else:
    iteration = 0

print(iteration, value)

if iteration == 0:
Exemple #29
0
def main():
    os.environ['OMP_NUM_THREADS'] = '1'

    if args.vis:
        from visdom import Visdom
        viz = Visdom()
        win = None

    envs = [
        make_env(args.env_name, args.seed, i, args.log_dir,
                 args.start_container) for i in range(args.num_processes)
    ]

    if args.num_processes > 1:
        envs = SubprocVecEnv(envs)
    else:
        envs = DummyVecEnv(envs)

    obs_shape = envs.observation_space.shape
    obs_shape = (obs_shape[0] * args.num_stack, *obs_shape[1:])
    obs_numel = reduce(operator.mul, obs_shape, 1)

    if len(obs_shape) == 3 and obs_numel > 1024:
        actor_critic = CNNPolicy(obs_shape[0], envs.action_space,
                                 args.recurrent_policy)
    else:
        assert not args.recurrent_policy, \
            "Recurrent policy is not implemented for the MLP controller"
        actor_critic = MLPPolicy(obs_numel, envs.action_space)

    modelSize = 0
    for p in actor_critic.parameters():
        pSize = reduce(operator.mul, p.size(), 1)
        modelSize += pSize
    print(str(actor_critic))
    print('Total model size: %d' % modelSize)

    if envs.action_space.__class__.__name__ == "Discrete":
        action_shape = 1
    else:
        action_shape = envs.action_space.shape[0]

    if args.cuda:
        actor_critic.cuda()

    if args.algo == 'a2c':
        optimizer = optim.RMSprop(actor_critic.parameters(),
                                  args.lr,
                                  eps=args.eps,
                                  alpha=args.alpha)
    elif args.algo == 'ppo':
        optimizer = optim.Adam(actor_critic.parameters(),
                               args.lr,
                               eps=args.eps)
    elif args.algo == 'acktr':
        optimizer = KFACOptimizer(actor_critic)

    rollouts = RolloutStorage(args.num_steps, args.num_processes, obs_shape,
                              envs.action_space, actor_critic.state_size)
    current_obs = torch.zeros(args.num_processes, *obs_shape)

    def update_current_obs(obs):
        shape_dim0 = envs.observation_space.shape[0]
        obs = torch.from_numpy(obs).float()
        if args.num_stack > 1:
            current_obs[:, :-shape_dim0] = current_obs[:, shape_dim0:]
        current_obs[:, -shape_dim0:] = obs

    obs = envs.reset()
    update_current_obs(obs)

    rollouts.observations[0].copy_(current_obs)

    # These variables are used to compute average rewards for all processes.
    episode_rewards = torch.zeros([args.num_processes, 1])
    final_rewards = torch.zeros([args.num_processes, 1])
    reward_avg = 0

    if args.cuda:
        current_obs = current_obs.cuda()
        rollouts.cuda()

    start = time.time()
    for j in range(num_updates):
        for step in range(args.num_steps):
            # Sample actions
            value, action, action_log_prob, states = actor_critic.act(
                Variable(rollouts.observations[step]),
                Variable(rollouts.states[step]),
                Variable(rollouts.masks[step]))
            cpu_actions = action.data.squeeze(1).cpu().numpy()

            # Observation, reward and next obs
            obs, reward, done, info = envs.find_action(cpu_actions)

            # Maxime: clip the reward within [0,1] for more reliable training
            # This code deals poorly with large reward values
            reward = np.clip(reward, a_min=0, a_max=None) / 400

            reward = torch.from_numpy(np.expand_dims(np.stack(reward),
                                                     1)).float()
            episode_rewards += reward

            # If done then clean the history of observations.
            masks = torch.FloatTensor([[0.0] if done_ else [1.0]
                                       for done_ in done])
            final_rewards *= masks
            final_rewards += (1 - masks) * episode_rewards
            episode_rewards *= masks

            if args.cuda:
                masks = masks.cuda()

            if current_obs.dim() == 4:
                current_obs *= masks.unsqueeze(2).unsqueeze(2)
            else:
                current_obs *= masks

            update_current_obs(obs)
            rollouts.insert(step, current_obs, states.data, action.data,
                            action_log_prob.data, value.data, reward, masks)

        next_value = actor_critic(Variable(rollouts.observations[-1]),
                                  Variable(rollouts.states[-1]),
                                  Variable(rollouts.masks[-1]))[0].data

        rollouts.compute_returns(next_value, args.use_gae, args.gamma,
                                 args.tau)

        if args.algo in ['a2c', 'acktr']:
            values, action_log_probs, dist_entropy, states = actor_critic.evaluate_actions(
                Variable(rollouts.observations[:-1].view(-1, *obs_shape)),
                Variable(rollouts.states[0].view(-1, actor_critic.state_size)),
                Variable(rollouts.masks[:-1].view(-1, 1)),
                Variable(rollouts.actions.view(-1, action_shape)))

            values = values.view(args.num_steps, args.num_processes, 1)
            action_log_probs = action_log_probs.view(args.num_steps,
                                                     args.num_processes, 1)

            advantages = Variable(rollouts.returns[:-1]) - values
            value_loss = advantages.pow(2).mean()

            action_loss = -(Variable(advantages.data) *
                            action_log_probs).mean()

            if args.algo == 'acktr' and optimizer.steps % optimizer.Ts == 0:
                # Sampled fisher, see Martens 2014
                actor_critic.zero_grad()
                pg_fisher_loss = -action_log_probs.mean()

                value_noise = Variable(torch.randn(values.size()))
                if args.cuda:
                    value_noise = value_noise.cuda()

                sample_values = values + value_noise
                vf_fisher_loss = -(values -
                                   Variable(sample_values.data)).pow(2).mean()

                fisher_loss = pg_fisher_loss + vf_fisher_loss
                optimizer.acc_stats = True
                fisher_loss.backward(retain_graph=True)
                optimizer.acc_stats = False

            optimizer.zero_grad()
            (value_loss * args.value_loss_coef + action_loss -
             dist_entropy * args.entropy_coef).backward()

            if args.algo == 'a2c':
                nn.utils.clip_grad_norm(actor_critic.parameters(),
                                        args.max_grad_norm)

            optimizer.step()

        elif args.algo == 'ppo':
            advantages = rollouts.returns[:-1] - rollouts.value_preds[:-1]
            advantages = (advantages - advantages.mean()) / (advantages.std() +
                                                             1e-5)

            for e in range(args.ppo_epoch):
                if args.recurrent_policy:
                    data_generator = rollouts.recurrent_generator(
                        advantages, args.num_mini_batch)
                else:
                    data_generator = rollouts.feed_forward_generator(
                        advantages, args.num_mini_batch)

                for sample in data_generator:
                    observations_batch, states_batch, actions_batch, \
                       return_batch, masks_batch, old_action_log_probs_batch, \
                            adv_targ = sample

                    # Reshape to do in a single forward pass for all steps
                    values, action_log_probs, dist_entropy, states = actor_critic.evaluate_actions(
                        Variable(observations_batch), Variable(states_batch),
                        Variable(masks_batch), Variable(actions_batch))

                    adv_targ = Variable(adv_targ)
                    ratio = torch.exp(action_log_probs -
                                      Variable(old_action_log_probs_batch))
                    surr1 = ratio * adv_targ
                    surr2 = torch.clamp(ratio, 1.0 - args.clip_param,
                                        1.0 + args.clip_param) * adv_targ
                    action_loss = -torch.min(
                        surr1,
                        surr2).mean()  # PPO's pessimistic surrogate (L^CLIP)

                    value_loss = (Variable(return_batch) -
                                  values).pow(2).mean()

                    optimizer.zero_grad()
                    (value_loss + action_loss -
                     dist_entropy * args.entropy_coef).backward()
                    nn.utils.clip_grad_norm(actor_critic.parameters(),
                                            args.max_grad_norm)
                    optimizer.step()

        rollouts.after_update()

        if j % args.save_interval == 0 and args.save_dir != "":
            save_path = os.path.join(args.save_dir, args.algo)
            try:
                os.makedirs(save_path)
            except OSError:
                pass

            # A really ugly way to save a model to CPU
            save_model = actor_critic
            if args.cuda:
                save_model = copy.deepcopy(actor_critic).cpu()

            save_model = [
                save_model,
                hasattr(envs, 'ob_rms') and envs.ob_rms or None
            ]

            torch.save(save_model,
                       os.path.join(save_path, args.env_name + ".pt"))

        if j % args.log_interval == 0:
            reward_avg = 0.99 * reward_avg + 0.01 * final_rewards.mean()
            end = time.time()
            total_num_steps = (j + 1) * args.num_processes * args.num_steps

            print(
                "Updates {}, num timesteps {}, FPS {}, running avg reward {:.3f}, entropy {:.5f}, value loss {:.5f}, policy loss {:.5f}"
                .format(j, total_num_steps,
                        int(total_num_steps / (end - start)), reward_avg,
                        dist_entropy.data[0], value_loss.data[0],
                        action_loss.data[0]))
            """
            print("Updates {}, num timesteps {}, FPS {}, mean/median reward {:.1f}/{:.1f}, min/max reward {:.1f}/{:.1f}, entropy {:.5f}, value loss {:.5f}, policy loss {:.5f}".
                format(
                    j,
                    total_num_steps,
                    int(total_num_steps / (end - start)),
                    final_rewards.mean(),
                    final_rewards.median(),
                    final_rewards.min(),
                    final_rewards.max(), dist_entropy.data[0],
                    value_loss.data[0], action_loss.data[0])
                )
            """

        if args.vis and j % args.vis_interval == 0:
            try:
                # Sometimes monitor doesn't properly flush the outputs
                win = visdom_plot(viz, win, args.log_dir, args.env_name,
                                  args.algo)
            except IOError:
                pass
    main_proc = True
    if args.distributed:
        if args.gpu_rank:
            torch.cuda.set_device(int(args.gpu_rank))
        dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
                                world_size=args.world_size, rank=args.rank)
        main_proc = args.rank == 0  # Only the first proc should save models
    save_folder = args.save_folder

    loss_results, cer_results, wer_results = torch.Tensor(args.epochs), torch.Tensor(args.epochs), torch.Tensor(
        args.epochs)
    best_wer = None
    if args.visdom and main_proc:
        from visdom import Visdom

        viz = Visdom()
        opts = dict(title=args.id, ylabel='', xlabel='Epoch', legend=['Loss', 'WER', 'CER'])
        viz_window = None
        epochs = torch.arange(1, args.epochs + 1)
    if args.tensorboard and main_proc:
        os.makedirs(args.log_dir, exist_ok=True)
        from tensorboardX import SummaryWriter

        tensorboard_writer = SummaryWriter(args.log_dir)
    os.makedirs(save_folder, exist_ok=True)

    avg_loss, start_epoch, start_iter = 0, 0, 0
    if args.continue_from:  # Starting from previous model
        print("Loading checkpoint model %s" % args.continue_from)
        package = torch.load(args.continue_from, map_location=lambda storage, loc: storage)
        model = DeepSpeech.load_model_package(package)
Exemple #31
0
class Visualizer(object):
    def __init__(self, name, save=True, output_dir="."):
        self.visdom = Visdom()
        self.name = name
        self.plots = dict()
        self.save = save
        if save:
            if not os.path.exists(output_dir):
                raise ValueError("output_dir does not exists")

            # output directory for reconstructions
            self.recon_dir = os.path.join(output_dir, "reconstructions")
            if not os.path.exists(self.recon_dir):
                os.mkdir(self.recon_dir)

            # output directory for traversals
            self.trav_dir = os.path.join(output_dir, "traversals")
            if not os.path.exists(self.trav_dir):
                os.mkdir(self.trav_dir)

    def traverse(self,
                 decoder,
                 latent_vector,
                 dims=None,
                 num_traversals=None,
                 iter_n=""):
        """ Traverses a latent vector along a given dimension(s).
        Args:
            decoder: (torch.nn.Module) decoder model that generates
                the reconstructions from a latent vector 
            latent_vector: (torch.tensor) latent vector representation to be traversed
                of shape (z_dim)
            dims: (list, range or torch.tensor) list of dimensions to traverse in the latent vector
                (optional)
            num_traversals: (int) how many reconstructions to generate for each dimension.
                The image grid will be of shape: len(dims) x num_traversals
            iter_n: (str) iteration at which plotting and/or image index (OPTIONAL)
        """

        if dims is None:
            dims = torch.arange(latent_vector.size(0))
        elif not (isinstance(dims, list) or isinstance(dims, range)
                  or isinstance(dims, torch.tensor)):
            raise ValueError(
                f"dims must either be a list or a torch.tensor, received {type(dims)}"
            )

        if num_traversals is None:
            num_traversals = latent_vector.size(0)
        elif not isinstance(num_traversals, int):
            raise ValueError(
                f"num_traversals must either be an int, received {type(num_traversals)}"
            )

        traversals = torch.linspace(-3., 3., steps=num_traversals).to(
            latent_vector.device)

        reconstructions = []
        for dim in dims:
            tiles = latent_vector.repeat(num_traversals, 1)
            tiles[:, dim] = traversals
            dim_recon = decoder(tiles)
            reconstructions.append(dim_recon)
        reconstructions = torch.sigmoid(torch.cat(reconstructions, dim=0))
        reconstructed = torchvision.utils.make_grid(reconstructions,
                                                    normalize=True,
                                                    nrow=len(dims))
        self.visdom.images(reconstructed.cpu(),
                           env=self.name + "-traversals",
                           opts={"title": iter_n},
                           nrow=len(dims))

        if self.save:
            torchvision.utils.save_image(reconstructions,
                                         os.path.join(
                                             self.trav_dir,
                                             f"traversals-{iter_n}.png"),
                                         normalize=True,
                                         nrow=len(dims))

    def show_reconstructions(self, images, reconstructions, iter_n=""):
        """ Plots the ELBO loss, reconstruction loss and KL divergence
        Args:
            images: (torch.tensor)  of shape batch_size x 3 x size x size
            reconstructions: (torch.Tensor) of shape batch_size x 3 x size x size
            iter_n: (str) iteration at which plotting (OPTIONAL)
        """
        original = torchvision.utils.make_grid(images, normalize=True)
        reconstructed = torchvision.utils.make_grid(
            torch.sigmoid(reconstructions), normalize=True)
        self.visdom.images(torch.stack([original, reconstructed], dim=0).cpu(),
                           env=self.name + "-reconstructed",
                           opts={"title": iter_n},
                           nrow=8)

        if self.save:
            torchvision.utils.save_image(original,
                                         os.path.join(
                                             self.recon_dir,
                                             f"original-{iter_n}.png"),
                                         normalize=True)
            torchvision.utils.save_image(torch.sigmoid(reconstructions),
                                         os.path.join(
                                             self.recon_dir,
                                             f"reconstructed-{iter_n}.png"),
                                         normalize=True)

    def __init_plots(self, iter_n, elbo, reconstruction_loss, kl_loss):
        self.plots["elbo"] = self.visdom.line(torch.tensor([elbo]),
                                              X=torch.tensor([iter_n]),
                                              env=self.name + "-stats",
                                              opts={
                                                  "title": "ELBO",
                                                  "width": 600,
                                                  "height": 500
                                              })
        self.plots["reconstruction_loss"] = self.visdom.line(
            torch.tensor([reconstruction_loss]),
            X=torch.tensor([iter_n]),
            env=self.name + "-stats",
            opts={
                "title": "Reconstruction loss",
                "width": 600,
                "height": 500
            })
        self.plots["kl_loss"] = self.visdom.line(torch.tensor([kl_loss]),
                                                 X=torch.tensor([iter_n]),
                                                 env=self.name + "-stats",
                                                 opts={
                                                     "title": "KL divergence",
                                                     "width": 600,
                                                     "height": 500
                                                 })

    def plot_stats(self, iter_n, elbo, reconstruction_loss, kl_loss):
        """ Plots the ELBO loss, reconstruction loss and KL divergence
        Args:
            iter_n: (int) iteration at which plotting
            elbo: (int)
            reconstruction_loss: (int)
            kl_loss: (int)
        """
        # Initialize the plots
        if not self.plots:
            self.__init_plots(iter_n, elbo, reconstruction_loss, kl_loss)
            return
        self.plots["elbo"] = self.visdom.line(torch.tensor([elbo]),
                                              X=torch.tensor([iter_n]),
                                              win=self.plots["elbo"],
                                              update="append",
                                              env=self.name + "-stats",
                                              opts={
                                                  "title": "ELBO",
                                                  "width": 600,
                                                  "height": 500
                                              })

        self.plots["reconstruction_loss"] = self.visdom.line(
            torch.tensor([reconstruction_loss]),
            X=torch.tensor([iter_n]),
            win=self.plots["reconstruction_loss"],
            update="append",
            env=self.name + "-stats",
            opts={
                "title": "Reconstruction Loss",
                "width": 600,
                "height": 500
            })

        self.plots["kl_loss"] = self.visdom.line(torch.tensor([kl_loss]),
                                                 X=torch.tensor([iter_n]),
                                                 win=self.plots["kl_loss"],
                                                 update="append",
                                                 env=self.name + "-stats",
                                                 opts={
                                                     "title": "KL Divergence",
                                                     "width": 600,
                                                     "height": 500
                                                 })

    def plot_means(self, z):
        """ Plots dimension-wise boxplot distribution
        Args:
            z: (torch.tensor) single batch of latent vector representations
        """
        if not self.plots.get("latent", False):
            self.plots["latent"] = self.visdom.boxplot(
                X=z,
                env=self.name + "-stats",
                opts={
                    "title": "Latent stats",
                    "width": 1200,
                    "height": 600,
                    "legend": [f"z_{i}" for i in range(1,
                                                       z.size(1) + 1)]
                })
        else:
            self.plots["latent"] = self.visdom.boxplot(
                X=z,
                win=self.plots["latent"],
                env=self.name + "-stats",
                opts={
                    "title": "Latent stats",
                    "width": 1200,
                    "height": 600,
                    "legend": [f"z_{i}" for i in range(1,
                                                       z.size(1) + 1)]
                })
Exemple #32
0
 def __init__(self, env='main'):
     self.viz = Visdom()
     self.env = env
     self.meter = AverageMeter()
     self.windows = defaultdict(lambda: None)
Exemple #33
0
# LICENSE file in the root directory of this source tree.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from visdom import Visdom
import numpy as np
import math
import os.path
import getpass
from sys import platform as _platform
from six.moves import urllib

viz = Visdom()

assert viz.check_connection()

textwindow = viz.text('Hello World!')

updatetextwindow = viz.text('Hello World! More text should be here')
assert updatetextwindow is not None, 'Window was none'
viz.text('And here it is', win=updatetextwindow, append=True)

# video demo:
try:
    video = np.empty([256, 250, 250, 3], dtype=np.uint8)
    for n in range(256):
        video[n, :, :, :].fill(n)
    viz.video(tensor=video)
Exemple #34
0
class visdomer(object):
    def __init__(self, port, hostname, model_name, compare_phase,
                 init_elements, init_params):
        self.port = port
        self.hostname = hostname
        self.env_name = model_name
        self.viz = Visdom(server=hostname, port=port, env=model_name)
        assert self.viz.check_connection(
            timeout_seconds=3), 'No connection could be formed quickly'
        self.compare_phase = compare_phase
        self.init_params = init_params

        self.windows = {}
        for element in init_elements:
            self.windows[element] = self.visdom_init(element)

    def get_viz(self):
        return self.viz

    def get_window(self, metric):
        # metric :: loss, accuracy,
        return self.windows[metric]

    def visdom_init(self, element):
        if element == 'lr':
            init_lr = self.init_params.get('lr')
            assert init_lr is not None, 'init_lr is None when init visom of learning rate'
            window = self.viz.line(X=torch.ones(1),
                                   Y=torch.tensor([init_lr]),
                                   opts=dict(title='{}'.format(element),
                                             showlegend=True,
                                             legend=['{}'.format(element)],
                                             xtype='linear',
                                             xlabel='epoch',
                                             xtickmin=0,
                                             xtick=True,
                                             xtickstep=10,
                                             ytype='linear',
                                             ytickmin=0,
                                             ylabel='{}'.format(element),
                                             ytick=True))
            return window
        elif element in ['loss', 'accuracy', 'miou', 'f1score']:
            assert isinstance(self.compare_phase, list) and len(
                self.compare_phase
            ) == 2, 'compare_phase must be list and length with 2'
            window = self.viz.line(
                X=torch.stack((torch.ones(1), torch.ones(1)), 1),
                Y=torch.stack((torch.ones(1), torch.ones(1)), 1),
                opts=dict(title='{}-{}-{}'.format(self.compare_phase[0],
                                                  self.compare_phase[1],
                                                  element),
                          showlegend=True,
                          legend=[
                              '{}-{}'.format(self.compare_phase[0], element),
                              '{}-{}'.format(self.compare_phase[1], element)
                          ],
                          xtype='linear',
                          label='epoch',
                          xtickmin=0,
                          xtick=True,
                          xtickstep=10,
                          ytype='linear',
                          ylabel='{}'.format(element),
                          ytickmin=0,
                          ytick=True))
        elif element in ['cell_entropy', 'network_entropy', 'entropy']:
            init_entropy = self.init_params['{:}'.format(element)]
            window = self.viz.line(X=torch.ones(1),
                                   Y=torch.tensor([init_entropy]),
                                   opts=dict(title='{}'.format(element),
                                             showlegend=True,
                                             legend=['{}'.format(element)],
                                             xtype='linear',
                                             xlabel='epoch',
                                             xtickmin=0,
                                             xtick=True,
                                             xtickstep=10,
                                             ytype='linear',
                                             ytickmin=0,
                                             ylabel='{}'.format(element),
                                             ytick=True))
        elif element in ['warmup_loss', 'warmup_miou']:
            window = self.viz.line(X=torch.ones(1),
                                   Y=torch.ones(1),
                                   opts=dict(title='{}'.format(element),
                                             showlegend=True,
                                             legend=['{}'.format(element)],
                                             xtype='linear',
                                             xlabel='epoch',
                                             xtickmin=0,
                                             xtick=True,
                                             xtickstep=10,
                                             ytype='linear',
                                             ytickmin=0,
                                             ylabel='{}'.format(element),
                                             ytick=True))
            return window
        else:
            raise NotImplementedError(
                'do not support metric {}'.format(element))
        return window

    def visdom_update(self, epoch, update_element, update_value):
        if update_element in ['loss', 'accuracy', 'miou', 'f1score']:
            #print(update_value)
            assert isinstance(update_value, list) and len(
                update_value
            ) == 2, 'update_value should be list and with length 2, but got {:} with length {:}'.format(
                type(update_value), len(update_value))
            train_log = update_value[0]
            valid_log = update_value[1]
            window = self.get_window(update_element)
            self.viz.line(
                X=torch.stack((torch.ones(1) * epoch, torch.ones(1) * epoch),
                              1),
                Y=torch.stack(
                    (torch.tensor([train_log]), torch.tensor([valid_log])), 1),
                win=window,
                update='append' if epoch != 1 else 'insert')
        elif update_element == 'lr':
            current_lr = update_value[0]
            window = self.get_window(update_element)
            self.viz.line(X=torch.ones(1) * epoch,
                          Y=torch.tensor([current_lr]),
                          win=window,
                          update='append' if epoch != 1 else 'insert')
        elif update_element in ['cell_entropy', 'network_entropy', 'entropy']:
            current_entropy = update_value[0]
            window = self.get_window(update_element)
            self.viz.line(X=torch.ones(1) * epoch,
                          Y=torch.tensor([current_entropy]),
                          win=window,
                          update='append' if epoch != 1 else 'insert')
        elif update_element in ['warmup_loss', 'warmup_miou']:
            current_value = update_value[0]
            window = self.get_window(update_element)
            self.viz.line(X=torch.ones(1) * epoch,
                          Y=torch.tensor([current_value]),
                          win=window,
                          update='append' if epoch != 1 else 'insert')
Exemple #35
0
class VisdomLogger(Logger):
    """
    Logger that uses visdom to create learning curves

    Parameters:
    ===========
    - env: str, name of the visdom environment
    - log_checkpoints: bool, whether to use checkpoints or epoch averages
        for training loss
    - legend: tuple, names of the different losses that will be plotted.
    """
    def __init__(self,
                 env=None,
                 log_checkpoints=True,
                 losses=('loss', ),
                 phases=('train', 'valid'),
                 server='http://localhost',
                 port=8097,
                 max_y=None,
                 **opts):
        self.viz = None
        if Visdom is not None:
            self.viz = Visdom(server=server, port=port, env=env)
        self.legend = ['%s.%s' % (p, l) for p in phases for l in losses]
        opts.update({'legend': self.legend})
        self.opts = opts
        self.env = env
        self.max_y = max_y
        self.log_checkpoints = log_checkpoints
        self.losses = set(losses)
        self.last = {p: {l: None for l in losses} for p in phases}
        self.pane = self._init_pane()

    @skip_on_import_error(Visdom)
    def _init_pane(self):
        nan = np.array([np.NAN, np.NAN])
        X = np.column_stack([nan] * len(self.legend))
        Y = np.column_stack([nan] * len(self.legend))
        return self.viz.line(
            X=X, Y=Y, env=self.env, opts=self.opts)

    def _update_last(self, epoch, loss, phase, loss_label):
        self.last[phase][loss_label] = {'X': epoch, 'Y': loss}

    def _plot_line(self, X, Y, phase, loss_label):
        name = "%s.%s" % (phase, loss_label)
        X = np.array([self.last[phase][loss_label]['X'], X])
        Y = np.array([self.last[phase][loss_label]['Y'], Y])
        if self.max_y:
            Y = np.clip(Y, Y.min(), self.max_y)
        self.viz.updateTrace(
            X=X, Y=Y, name=name, append=True, win=self.pane, env=self.env)

    def _plot_payload(self, epoch, losses, phase):
        for label, loss in losses.items():
            if label not in self.losses:
                continue
            if self.last[phase][label] is not None:
                self._plot_line(epoch, loss, phase=phase, loss_label=label)
            self._update_last(epoch, loss, phase, label)

    @skip_on_import_error(Visdom)
    def epoch_end(self, payload):
        if self.log_checkpoints:
            # only use epoch end if checkpoint isn't being used
            return
        losses, epoch = payload['loss'], payload['epoch'] + 1
        self._plot_payload(epoch, losses, 'train')

    @skip_on_import_error(Visdom)
    def validation_end(self, payload):
        losses, epoch = payload['loss'], payload['epoch'] + 1
        self._plot_payload(epoch, losses, 'valid')

    @skip_on_import_error(Visdom)
    def checkpoint(self, payload):
        epoch = payload['epoch'] + payload["batch"] / payload["total_batches"]
        losses = payload['loss']
        self._plot_payload(epoch, losses, 'train')

    @skip_on_import_error(Visdom)
    def attention(self, payload):
        title = "epoch {epoch}/ batch {batch_num}".format(**payload)
        if 'title' in self.opts:
            title = self.opts['title'] + ": " + title
        self.viz.heatmap(
            X=np.array(payload["att"]),
            env=self.env,
            opts={'rownames': payload["hyp"],
                  'columnnames': payload["target"],
                  'title': title})
config['weight_decay'] = 1e-4
config['start_epoch'] = 0
config['batch_size'] = 26
config['valid_batch_size'] = 4
config['save_freq'] = 100
config['sigma'] = 5.
config['root_dir'] = r"./tcdata/round2train"
config['label_dir'] = r"./tcdata/round2train_checked.json"
config['valid_root_dir'] = r"./tcdata/round2_valid53/valid"
config['valid_label_dir'] = r"./tcdata/round2_valid53/round2_valid53_annotation51.json"



if config['showFlag'] == 1:
    #生成一个viz的环境
    viz = Visdom()
    # python -m visdom.server
    #初始化两个小的窗格,来分别绘制train,test的情况
    # 绘制初始点,原点
    viz.line([0.], [0.], win='train_loss', opts=dict(title='train loss'))  #single-line
    viz.line([0.], [0.], win='coor_loss', opts=dict(title='coor loss'))  #single-line
    viz.line([0.], [0.], win='valid_train_loss', opts=dict(title='valid_train loss'))  #single-line
    viz.line([0.], [0.], win='acc_coor', opts=dict(title='average acc_coor'))  #single-line

# 数据预处理设置
# normMean = [0.16783774]
# normStd = [0.18892017]
# normMean = [0.168036]
# normStd = [0.177935]
normMean = [0.168036]
normStd = [0.177935]
Exemple #37
0
def main():
    args = parser.parse_args()
    cf = ConfigParser.ConfigParser()
    try:
        cf.read(args.conf)
    except:
        print("conf file not exists")
        sys.exit(1)
    USE_CUDA = cf.getboolean('Training', 'use_cuda')
    try:
        seed = long(cf.get('Training', 'seed'))
    except:
        seed = torch.cuda.initial_seed()
        cf.set('Training', 'seed', seed)
        cf.write(open(args.conf, 'w'))
    
    torch.manual_seed(seed)
    if USE_CUDA:
        torch.cuda.manual_seed(seed)
    
    log_dir = cf.get('Data', 'log_dir')
    log_file = os.path.join(log_dir, cf.get('Data', 'log_file'))
    logger = init_logger(log_file)
    
    #Define Model
    rnn_input_size = cf.getint('Model', 'rnn_input_size')
    rnn_hidden_size = cf.getint('Model', 'rnn_hidden_size')
    rnn_layers = cf.getint('Model', 'rnn_layers')
    rnn_type = RNN[cf.get('Model', 'rnn_type')]
    bidirectional = cf.getboolean('Model', 'bidirectional')
    batch_norm = cf.getboolean('Model', 'batch_norm')
    rnn_param = {"rnn_input_size":rnn_input_size, "rnn_hidden_size":rnn_hidden_size, "rnn_layers":rnn_layers, 
                    "rnn_type":rnn_type, "bidirectional":bidirectional, "batch_norm":batch_norm}
    num_class = cf.getint('Model', 'num_class')
    drop_out = cf.getfloat('Model', 'drop_out')

    model = CTC_Model(rnn_param=rnn_param, num_class=num_class, drop_out=drop_out)
    print("Model Structure:")
    logger.info("Model Structure:")
    for idx, m in enumerate(model.children()):
        print(idx, m)
        logger.info(str(idx) + "->" + str(m))
    
    data_dir = cf.get('Data', 'data_dir')
    batch_size = cf.getint("Training", 'batch_size')
    
    #Data Loader
    train_dataset = SpeechDataset(data_dir, data_set='train')
    dev_dataset = SpeechDataset(data_dir, data_set="dev")
    train_loader = SpeechDataLoader(train_dataset, batch_size=batch_size, shuffle=True,
                                        num_workers=4, pin_memory=False)
    dev_loader = SpeechDataLoader(dev_dataset, batch_size=batch_size, shuffle=False,
                                            num_workers=4, pin_memory=False)
    
    #ensure the feats is equal to the rnn_input_Size
    assert train_dataset.n_feats == rnn_input_size
    
    #decoder for dev set
    decoder = GreedyDecoder(int2char, space_idx=len(int2char) - 1, blank_index=0)
        
    #Training
    init_lr = cf.getfloat('Training', 'init_lr')
    num_epoches = cf.getint('Training', 'num_epoches')
    end_adjust_acc = cf.getfloat('Training', 'end_adjust_acc')
    decay = cf.getfloat("Training", 'lr_decay')
    weight_decay = cf.getfloat("Training", 'weight_decay')
    
    params = { 'num_epoches':num_epoches, 'end_adjust_acc':end_adjust_acc, 'seed':seed,
            'decay':decay, 'learning_rate':init_lr, 'weight_decay':weight_decay, 'batch_size':batch_size, 'n_feats':train_dataset.n_feats }
    print(params)
    
    if USE_CUDA:
        model = model.cuda()
    
    loss_fn = CTCLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=init_lr, weight_decay=weight_decay)

    #visualization for training
    from visdom import Visdom
    viz = Visdom()
    title = 'TIMIT LSTM_CTC Acoustic Model'

    opts = [dict(title=title+" Loss", ylabel = 'Loss', xlabel = 'Epoch'),
            dict(title=title+" Loss on Dev", ylabel = 'DEV Loss', xlabel = 'Epoch'),
            dict(title=title+' CER on DEV', ylabel = 'DEV CER', xlabel = 'Epoch')]
    viz_window = [None, None, None]
    
    count = 0
    learning_rate = init_lr
    loss_best = 1000
    loss_best_true = 1000
    adjust_rate_flag = False
    stop_train = False
    adjust_time = 0
    acc_best = 0
    start_time = time.time()
    loss_results = []
    dev_loss_results = []
    dev_cer_results = []
    
    while not stop_train:
        if count >= num_epoches:
            break
        count += 1
        
        if adjust_rate_flag:
            learning_rate *= decay
            adjust_rate_flag = False
            for param in optimizer.param_groups:
                param['lr'] *= decay
        
        print("Start training epoch: %d, learning_rate: %.5f" % (count, learning_rate))
        logger.info("Start training epoch: %d, learning_rate: %.5f" % (count, learning_rate))
        
        loss = train(model, train_loader, loss_fn, optimizer, logger, print_every=20, USE_CUDA=USE_CUDA)
        loss_results.append(loss)
        acc, dev_loss = dev(model, dev_loader, loss_fn, decoder, logger, USE_CUDA=USE_CUDA)
        print("loss on dev set is %.4f" % dev_loss)
        logger.info("loss on dev set is %.4f" % dev_loss)
        dev_loss_results.append(dev_loss)
        dev_cer_results.append(acc)
        
        #adjust learning rate by dev_loss
        #adjust_rate_count  :  表示连续超过count个epoch的loss在end_adjust_acc区间内认为稳定
        if dev_loss < (loss_best - end_adjust_acc):
            loss_best = dev_loss
            loss_best_true = dev_loss
            adjust_rate_count = 0
            acc_best = acc
            best_model_state = copy.deepcopy(model.state_dict())
            best_op_state = copy.deepcopy(optimizer.state_dict())
        elif (dev_loss < loss_best + end_adjust_acc):
            adjust_rate_count += 1
            if dev_loss < loss_best and dev_loss < loss_best_true:
                loss_best_true = dev_loss
                acc_best = acc
                best_model_state = copy.deepcopy(model.state_dict())
                best_op_state = copy.deepcopy(optimizer.state_dict())
        else:
            adjust_rate_count = 10
        
        print("adjust_rate_count: %d" % adjust_rate_count)
        print('adjust_time: %d' % adjust_time)
        logger.info("adjust_rate_count: %d" % adjust_rate_count)
        logger.info('adjust_time: %d' % adjust_time)

        if adjust_rate_count == 10:
            adjust_rate_flag = True
            adjust_time += 1
            adjust_rate_count = 0
            if loss_best > loss_best_true:
                loss_best = loss_best_true
            model.load_state_dict(best_model_state)
            optimizer.load_state_dict(best_op_state)

        if adjust_time == 8:
            stop_train = True
        
        time_used = (time.time() - start_time) / 60
        print("epoch %d done, dev acc is: %.4f, time_used: %.4f minutes" % (count, acc, time_used))
        logger.info("epoch %d done, dev acc is: %.4f, time_used: %.4f minutes" % (count, acc, time_used))
        
        x_axis = range(count)
        y_axis = [loss_results[0:count], dev_loss_results[0:count], dev_cer_results[0:count]]
        for x in range(len(viz_window)):
            if viz_window[x] is None:
                viz_window[x] = viz.line(X = np.array(x_axis), Y = np.array(y_axis[x]), opts = opts[x],)
            else:
                viz.line(X = np.array(x_axis), Y = np.array(y_axis[x]), win = viz_window[x], update = 'replace',)

    print("End training, best dev loss is: %.4f, acc is: %.4f" % (loss_best_true, acc_best))
    logger.info("End training, best dev loss acc is: %.4f, acc is: %.4f" % (loss_best_true, acc_best)) 
    model.load_state_dict(best_model_state)
    optimizer.load_state_dict(best_op_state)
    best_path = os.path.join(log_dir, 'best_model'+'_dev'+str(acc_best)+'.pkl')
    cf.set('Model', 'model_file', best_path)
    cf.write(open(args.conf, 'w'))
    params['epoch']=count

    torch.save(CTC_Model.save_package(model, optimizer=optimizer, epoch=params, loss_results=loss_results, dev_loss_results=dev_loss_results, dev_cer_results=dev_cer_results), best_path)
Exemple #38
0
class Visualizer(object):
    """
    封装visdom的基本操作
    """

    def __init__(self, env='default', **kwargs):
        self.vis = Visdom(env=env,use_incoming_socket=False, **kwargs)
        # 以文字和图的形式展示
        # {'loss1':23,'loss2':24}
        self.index = {}
        self.log_text = ''
        self.log_index = {}  # {win:[log_text]}
    def reinit(self, env='default', **kwargs):
        self.vis = Visdom(env=env, **kwargs)

    def img(self, name ,img_,  **kwargs):
        # img_: batchsize*channels*H*W 0-1 cuda tensor
        self.vis.images(img_.cpu().numpy(),
                        win=name,
                        opts=dict(title=name),
                        **kwargs
                        )

    def log(self, info='', win='log_text', append=False ):
        """
        self.log({'loss':1,'lr':0.0001})
        :param info:
        :param win:
        :return:
        """
        log_text = self.log_index.get(win,'')
        if append:
            log_text +=(
                '[{time}]{info}<br>'.format(
                    time = time.strftime('%m%d_%H:%M:%S'),
                    info = info
                                            )
            )
        else:
             log_text =(
                '[{time}]{info}<br>'.format(
                    time = time.strftime('%m%d_%H:%M:%S'),
                    info = info
                                            )
            )  
        self.vis.text(log_text, win=win, opts= dict(title=win))
        self.log_index[win] = log_text

    def plot(self, win, y,  **kwargs):
        """
        plot('loss',2)
        :param win:
        :param y:
        :param kwargs:
        :return:
        """

        x = self.index.get(win,0)
        self.vis.line(
            X=np.array([x]), Y=np.array([y]),
            win = win,
            opts= dict(title=win),
            update = None if x ==0 else 'append',
            **kwargs
        )
        self.index[win] = x+1


    def img_many(self,d):
        # d: {'1.jpg':b*c*H*W,'2.jpg':b*c*H*W}
        for k,v in d.items():
            self.img(k,v)

    def plot_many(self,d):
        # d:{'loss1':2,'loass2':4}
        for k,v in d.items():
            self.plot(k,v)

    def __getattr__(self,name):
        # self,function->self.vis.funtion
        return getattr(self.vis, name)
        
Exemple #39
0
def main():
    args = parser.parse_args()
    cf = ConfigParser.ConfigParser()
    try:
        cf.read(args.conf)
    except:
        print("conf file not exists")
        sys.exit(1)
    try:
        seed = cf.get('Training', 'seed')
        seed = long(seed)
    except:
        seed = torch.cuda.initial_seed()
        cf.set('Training', 'seed', seed)
        cf.write(open(args.conf, 'w'))
    
    USE_CUDA = cf.getboolean("Training", "use_cuda")
    torch.manual_seed(seed)
    if USE_CUDA:
        torch.cuda.manual_seed(seed)
    
    logger = init_logger(os.path.join(args.log_dir, 'train_ctc_model.log'))
    
    #Define Model
    rnn_input_size = cf.getint('Model', 'rnn_input_size')
    rnn_hidden_size = cf.getint('Model', 'rnn_hidden_size')
    rnn_layers = cf.getint('Model', 'rnn_layers')
    rnn_type = supported_rnn[cf.get('Model', 'rnn_type')]
    bidirectional = cf.getboolean('Model', 'bidirectional')
    batch_norm = cf.getboolean('Model', 'batch_norm')
    rnn_param = {"rnn_input_size":rnn_input_size, "rnn_hidden_size":rnn_hidden_size, "rnn_layers":rnn_layers, 
                    "rnn_type":rnn_type, "bidirectional":bidirectional, "batch_norm":batch_norm}
    
    num_class = cf.getint('Model', 'num_class')
    drop_out = cf.getfloat('Model', 'drop_out')
    add_cnn = cf.getboolean('Model', 'add_cnn')
    
    cnn_param = {}
    layers = cf.getint('CNN', 'layers')
    channel = eval(cf.get('CNN', 'channel'))
    kernel_size = eval(cf.get('CNN', 'kernel_size'))
    stride = eval(cf.get('CNN', 'stride'))
    padding = eval(cf.get('CNN', 'padding'))
    pooling = eval(cf.get('CNN', 'pooling'))
    batch_norm = cf.getboolean('CNN', 'batch_norm')
    activation_function = supported_activate[cf.get('CNN', 'activation_function')]
    
    cnn_param['batch_norm'] = batch_norm
    cnn_param['activate_function'] = activation_function
    cnn_param["layer"] = []
    for layer in range(layers):
        layer_param = [channel[layer], kernel_size[layer], stride[layer], padding[layer]]
        if pooling is not None:
            layer_param.append(pooling[layer])
        else:
            layer_param.append(None)
        cnn_param["layer"].append(layer_param)

    model = CTC_Model(rnn_param=rnn_param, add_cnn=add_cnn, cnn_param=cnn_param, num_class=num_class, drop_out=drop_out)
    
    for idx, m in enumerate(model.children()):
        print(idx, m)
        logger.info(str(idx) + "->" + str(m))
    
    dataset = cf.get('Data', 'dataset')
    data_dir = cf.get('Data', 'data_dir')
    feature_type = cf.get('Data', 'feature_type')
    out_type = cf.get('Data', 'out_type')
    n_feats = cf.getint('Data', 'n_feats')
    mel = cf.getboolean('Data', 'mel')
    batch_size = cf.getint("Training", 'batch_size')
    
    #Data Loader
    train_dataset = SpeechDataset(data_dir, data_set='train', feature_type=feature_type, out_type=out_type, n_feats=n_feats, mel=mel)
    dev_dataset = SpeechDataset(data_dir, data_set="dev", feature_type=feature_type, out_type=out_type, n_feats=n_feats, mel=mel)
    
    if add_cnn:
        train_loader = SpeechCNNDataLoader(train_dataset, batch_size=batch_size, shuffle=True,
                                            num_workers=4, pin_memory=False)
        dev_loader = SpeechCNNDataLoader(dev_dataset, batch_size=batch_size, shuffle=False,
                                            num_workers=4, pin_memory=False)
    else:
        train_loader = SpeechDataLoader(train_dataset, batch_size=batch_size, shuffle=True,
                                            num_workers=4, pin_memory=False)
        dev_loader = SpeechDataLoader(dev_dataset, batch_size=batch_size, shuffle=False,
                                            num_workers=4, pin_memory=False)
    #decoder for dev set
    decoder = GreedyDecoder(dev_dataset.int2class, space_idx=-1, blank_index=0)
        
    #Training
    init_lr = cf.getfloat('Training', 'init_lr')
    num_epoches = cf.getint('Training', 'num_epoches')
    end_adjust_acc = cf.getfloat('Training', 'end_adjust_acc')
    decay = cf.getfloat("Training", 'lr_decay')
    weight_decay = cf.getfloat("Training", 'weight_decay')
    
    params = { 'num_epoches':num_epoches, 'end_adjust_acc':end_adjust_acc, 'mel': mel, 'seed':seed,
                'decay':decay, 'learning_rate':init_lr, 'weight_decay':weight_decay, 'batch_size':batch_size,
                'feature_type':feature_type, 'n_feats': n_feats, 'out_type': out_type }
    print(params)
    
    if USE_CUDA:
        model = model.cuda()
    
    loss_fn = CTCLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=init_lr, weight_decay=weight_decay)

    #visualization for training
    from visdom import Visdom
    viz = Visdom()
    if add_cnn:
        title = dataset+' '+feature_type+str(n_feats)+' CNN_LSTM_CTC'
    else:
        title = dataset+' '+feature_type+str(n_feats)+' LSTM_CTC'

    opts = [dict(title=title+" Loss", ylabel = 'Loss', xlabel = 'Epoch'),
            dict(title=title+" Loss on Dev", ylabel = 'DEV Loss', xlabel = 'Epoch'),
            dict(title=title+' CER on DEV', ylabel = 'DEV CER', xlabel = 'Epoch')]
    viz_window = [None, None, None]
    
    count = 0
    learning_rate = init_lr
    loss_best = 1000
    loss_best_true = 1000
    adjust_rate_flag = False
    stop_train = False
    adjust_time = 0
    acc_best = 0
    start_time = time.time()
    loss_results = []
    dev_loss_results = []
    dev_cer_results = []
    
    while not stop_train:
        if count >= num_epoches:
            break
        count += 1
        
        if adjust_rate_flag:
            learning_rate *= decay
            adjust_rate_flag = False
            for param in optimizer.param_groups:
                param['lr'] *= decay
        
        print("Start training epoch: %d, learning_rate: %.5f" % (count, learning_rate))
        logger.info("Start training epoch: %d, learning_rate: %.5f" % (count, learning_rate))
        
        loss = train(model, train_loader, loss_fn, optimizer, logger, add_cnn=add_cnn, print_every=20, USE_CUDA=USE_CUDA)
        loss_results.append(loss)
        acc, dev_loss = dev(model, dev_loader, loss_fn, decoder, logger, add_cnn=add_cnn, USE_CUDA=USE_CUDA)
        print("loss on dev set is %.4f" % dev_loss)
        logger.info("loss on dev set is %.4f" % dev_loss)
        dev_loss_results.append(dev_loss)
        dev_cer_results.append(acc)
        
        #adjust learning rate by dev_loss
        if dev_loss < (loss_best - end_adjust_acc):
            loss_best = dev_loss
            loss_best_true = dev_loss
            #acc_best = acc
            adjust_rate_count = 0
            model_state = copy.deepcopy(model.state_dict())
            op_state = copy.deepcopy(optimizer.state_dict())
        elif (dev_loss < loss_best + end_adjust_acc):
            adjust_rate_count += 1
            if dev_loss < loss_best and dev_loss < loss_best_true:
                loss_best_true = dev_loss
                #acc_best = acc
                model_state = copy.deepcopy(model.state_dict())
                op_state = copy.deepcopy(optimizer.state_dict())
        else:
            adjust_rate_count = 10
        
        if acc > acc_best:
            acc_best = acc
            best_model_state = copy.deepcopy(model.state_dict())
            best_op_state = copy.deepcopy(optimizer.state_dict())

        print("adjust_rate_count:"+str(adjust_rate_count))
        print('adjust_time:'+str(adjust_time))
        logger.info("adjust_rate_count:"+str(adjust_rate_count))
        logger.info('adjust_time:'+str(adjust_time))

        if adjust_rate_count == 10:
            adjust_rate_flag = True
            adjust_time += 1
            adjust_rate_count = 0
            if loss_best > loss_best_true:
                loss_best = loss_best_true
            model.load_state_dict(model_state)
            optimizer.load_state_dict(op_state)

        if adjust_time == 8:
            stop_train = True
        
        time_used = (time.time() - start_time) / 60
        print("epoch %d done, cv acc is: %.4f, time_used: %.4f minutes" % (count, acc, time_used))
        logger.info("epoch %d done, cv acc is: %.4f, time_used: %.4f minutes" % (count, acc, time_used))
        
        x_axis = range(count)
        y_axis = [loss_results[0:count], dev_loss_results[0:count], dev_cer_results[0:count]]
        for x in range(len(viz_window)):
            if viz_window[x] is None:
                viz_window[x] = viz.line(X = np.array(x_axis), Y = np.array(y_axis[x]), opts = opts[x],)
            else:
                viz.line(X = np.array(x_axis), Y = np.array(y_axis[x]), win = viz_window[x], update = 'replace',)

    print("End training, best dev loss is: %.4f, acc is: %.4f" % (loss_best, acc_best))
    logger.info("End training, best dev loss acc is: %.4f, acc is: %.4f" % (loss_best, acc_best)) 
    model.load_state_dict(best_model_state)
    optimizer.load_state_dict(best_op_state)
    best_path = os.path.join(args.log_dir, 'best_model'+'_dev'+str(acc_best)+'.pkl')
    cf.set('Model', 'model_file', best_path)
    cf.write(open(args.conf, 'w'))
    params['epoch']=count

    torch.save(CTC_Model.save_package(model, optimizer=optimizer, epoch=params, loss_results=loss_results, dev_loss_results=dev_loss_results, dev_cer_results=dev_cer_results), best_path)
Exemple #40
0
 def reinit(self, env='default', **kwargs):
     self.vis = Visdom(env=env, **kwargs)
Exemple #41
0
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from visdom import Visdom
import numpy as np
import math
import os.path
import getpass

viz = Visdom()

textwindow = viz.text('Hello World!')

# video demo:
try:
    video = np.empty([256, 250, 250, 3], dtype=np.uint8)
    for n in range(256):
        video[n, :, :, :].fill(n)
    viz.video(tensor=video)

    # video demo: download video from http://media.w3.org/2010/05/sintel/trailer.ogv
    videofile = '/home/%s/trailer.ogv' % getpass.getuser()
    if os.path.isfile(videofile):
        viz.video(videofile=videofile)
except ImportError:
Exemple #42
0
import gym
import numpy as np
import matplotlib.pyplot as plt
from visdom import Visdom
viz = Visdom(env="mean_reward")
env = gym.make("Taxi-v2")
state_space = 500
action_space = 6
epsilon = 0.1
num_episode = 30000
q = np.zeros((state_space, action_space))
q1 = np.zeros((state_space, action_space))
q2 = np.zeros((state_space, action_space))
r_normal = []
r_double = []


def choose_action(state, epsilon, flag=True):
    if flag:
        if np.random.rand() > epsilon:
            action = np.argmax(q[state])
        else:
            action = np.random.randint(action_space)
        return action
    else:
        if np.random.rand() > epsilon:
            action = np.argmax((q1[state] + q2[state]) / 2)
        else:
            action = np.random.randint(action_space)
        return action
from visdom import Visdom
import numpy as np
import math
import os.path
import getpass
from sys import platform as _platform
from six.moves import urllib
import re

viz = Visdom()
f=open('log_purerl_1to0_5class11').read()
print f[:100]
# ttt0,ttt1,ttt2,ttt3,ttt4,ttt5=[],[],[],[],[],[]
ttt=[]
ttt.append([float(i) for i in re.findall('ttt0 is.* (.*)',f)])
ttt.append([float(i) for i in re.findall('acc:(.*?)\t',f)])
ttt.append([float(i) for i in re.findall('ttt2 is.* (.*)',f)])
ttt.append([float(i) for i in re.findall('ttt3 is.* (.*)',f)])
ttt.append([float(i) for i in re.findall('ttt4 is.* (.*)',f)])
ttt.append([float(i) for i in re.findall('ttt5 is.* (.*)',f)])
times=range(0,306,3)
# times=range(0,60,3)

# tt=[]
# for i in ttt:
#     tt.append(i[:20])

# ttt=tt
# textwindow = viz.text('Hello World!')

win = viz.line(
Exemple #44
0
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.init as init
from torch.autograd import Variable

# visdom is a visualization tool from facebook
from visdom import Visdom
viz = Visdom()

num_data = 1000
num_epoch = 1000

noise = init.normal(torch.FloatTensor(num_data, 1), std=1)
x = init.uniform(torch.Tensor(num_data, 1), -10, 10)

y = 2 * x + 3
y_noise = 2 * x + 3 + noise

# visualize data with visdom
input_data = torch.cat([x, y_noise], 1)
# win = viz.scatter(
#     X=input_data,
#     opts=dict(
#         xtickmin=-10,
#         xtickmax=10,
#         xtickstep=1,
#         ytickmin=-20,
#         ytickmax=20,
#         ytickstep=1,
class EvaluationMonitor(Wrapper):
    def __init__(self, env, cmdl):
        super(EvaluationMonitor, self).__init__(env)

        self.freq = cmdl.eval_freq  # in steps
        self.eval_steps = cmdl.evaluator.eval_steps
        self.cmdl = cmdl

        if self.cmdl.display_plots:
            self.vis = Visdom()
            self.plot = self.vis.line(
                Y=np.array([0]), X=np.array([0]),
                opts=dict(
                    title=cmdl.label,
                    caption="Episodic reward per %d steps." % self.eval_steps)
            )

        self.crt_step = 0
        self.step_cnt = 0
        self.ep_cnt = 0
        self.total_rw = 0
        self.max_mean_rw = -100

    def get_crt_step(self, crt_step):
        self.crt_step = crt_step

    def _reset_monitor(self):
        self.step_cnt, self.ep_cnt, self.total_rw = 0, 0, 0

    def _step(self, action):
        # self._before_step(action)
        observation, reward, done, info = self.env.step(action)
        done = self._after_step(observation, reward, done, info)
        return observation, reward, done, info

    def _reset(self):
        observation = self.env.reset()
        self._after_reset(observation)
        return observation

    def _after_step(self, o, r, done, info):
        self.total_rw += r
        self.step_cnt += 1
        if self.step_cnt == self.eval_steps:
            self._update_plot()
            self._reset_monitor()
        return done

    def _after_reset(self, observation):
        self.ep_cnt += 1
        # print("[%2d][%4d]  RESET" % (self.ep_cnt, self.step_cnt))

    def _update_plot(self):
        mean_rw = self.total_rw / self.ep_cnt
        max_mean_rw = self.max_mean_rw
        bg_color = 'on_blue'
        bg_color = 'on_magenta' if mean_rw > max_mean_rw else bg_color
        self.max_mean_rw = mean_rw if mean_rw > max_mean_rw else max_mean_rw

        if self.cmdl.display_plots:
            self.vis.line(
                X=np.array([self.crt_step]),
                Y=np.array([mean_rw]),
                win=self.plot,
                update='append'
            )
        print(clr("[Evaluator] done in %5d steps. " % self.step_cnt,
              'grey', 'on_white')
              + clr(" rw/ep=%3.2f " % mean_rw, 'white', bg_color))
Exemple #46
0
    args = parser.parse_args()
    return args


def print_args(args):
    print("##############################################")
    for k, v in vars(args).items():
        print(k + ': ' + str(v))
    print("##############################################")
    print()


# pip install visdom
# python -m visdom.server
# open web page --> http://localhost:8097
vis = Visdom()

episode_reward_plt = None
avg_episode_reward_plt = None
episode_loss_plt = None
epsilon_plt = None


def vis_plt(method, args):
    global episode_reward_plt
    global avg_episode_reward_plt
    global episode_loss_plt
    global epsilon_plt

    layout_opts_dict = {
        'plotly': {
Exemple #47
0
GPU_ID = None
epochs = 100
batch_size = 32
start_epoch = 1
save_snapshot_interval_epoch = 1
peek_interval_epoch = 1
save_train_hr_interval_epoch = 1
loss_average_win_size = 2
validate_interval_epoch = 1
validate_batch_size = 4
plot_loss_start_epoch = 1
only_validate = False  #

from visdom import Visdom

vis = Visdom(server='http://127.0.0.1', port=8097)

# =================== config for model and dataset =====================================================================
from squid.data import Photo2PhotoData
from squid.data import RandomCropPhoto2PhotoData
from squid.model import SuperviseModel
import torch
import torch.nn as nn
from squid.loss import VGGLoss
from squid.net import AOD_Deep1_Net

target_net = AOD_Deep1_Net()
target_net = nn.DataParallel(target_net).cuda()

model = SuperviseModel({
    'net':
 def __init__(self, env_name='main', imsize=None):
     self.vis = Visdom()
     self.env = env_name
     self.images = {}
     self.imsize = imsize
Exemple #49
0
class Callback(object):
    """
    Used to log/visualize the evaluation metrics during training. The values are stored at the end of each epoch.
    """
    def __init__(self, metrics):
        """
        Args:
            metrics : a list of callbacks. Possible values:
                "CoherenceMetric"
                "PerplexityMetric"
                "DiffMetric"
                "ConvergenceMetric"
        """
        # list of metrics to be plot
        self.metrics = metrics

    def set_model(self, model):
        """
        Save the model instance and initialize any required variables which would be updated throughout training
        """
        self.model = model
        self.previous = None
        # check for any metric which need model state from previous epoch
        if any(isinstance(metric, (DiffMetric, ConvergenceMetric)) for metric in self.metrics):
            self.previous = copy.deepcopy(model)
            # store diff diagonals of previous epochs
            self.diff_mat = Queue()
        if any(metric.logger == "visdom" for metric in self.metrics):
            if not VISDOM_INSTALLED:
                raise ImportError("Please install Visdom for visualization")
            self.viz = Visdom()
            # store initial plot windows of every metric (same window will be updated with increasing epochs)
            self.windows = []
        if any(metric.logger == "shell" for metric in self.metrics):
            # set logger for current topic model
            self.log_type = logging.getLogger('gensim.models.ldamodel')

    def on_epoch_end(self, epoch, topics=None):
        """
        Log or visualize current epoch's metric value

        Args:
            epoch : current epoch no.
            topics : topic distribution from current epoch (required for coherence of unsupported topic models)
        """
        # stores current epoch's metric values
        current_metrics = {}

        # plot all metrics in current epoch
        for i, metric in enumerate(self.metrics):
            label = str(metric)
            value = metric.get_value(topics=topics, model=self.model, other_model=self.previous)

            current_metrics[label] = value

            if metric.logger == "visdom":
                if epoch == 0:
                    if value.ndim > 0:
                        diff_mat = np.array([value])
                        viz_metric = self.viz.heatmap(
                            X=diff_mat.T, env=metric.viz_env, opts=dict(xlabel='Epochs', ylabel=label, title=label)
                        )
                        # store current epoch's diff diagonal
                        self.diff_mat.put(diff_mat)
                        # saving initial plot window
                        self.windows.append(copy.deepcopy(viz_metric))
                    else:
                        viz_metric = self.viz.line(
                            Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env,
                            opts=dict(xlabel='Epochs', ylabel=label, title=label)
                        )
                        # saving initial plot window
                        self.windows.append(copy.deepcopy(viz_metric))
                else:
                    if value.ndim > 0:
                        # concatenate with previous epoch's diff diagonals
                        diff_mat = np.concatenate((self.diff_mat.get(), np.array([value])))
                        self.viz.heatmap(
                            X=diff_mat.T, env=metric.viz_env, win=self.windows[i],
                            opts=dict(xlabel='Epochs', ylabel=label, title=label)
                        )
                        self.diff_mat.put(diff_mat)
                    else:
                        self.viz.updateTrace(
                            Y=np.array([value]), X=np.array([epoch]), env=metric.viz_env, win=self.windows[i]
                        )

            if metric.logger == "shell":
                statement = "".join(("Epoch ", str(epoch), ": ", label, " estimate: ", str(value)))
                self.log_type.info(statement)

        # check for any metric which need model state from previous epoch
        if isinstance(metric, (DiffMetric, ConvergenceMetric)):
            self.previous = copy.deepcopy(self.model)

        return current_metrics
from io import BytesIO
from PIL import Image
from argparse import ArgumentParser
from visdom import Visdom

visdom_endpoint = variables.get("ENDPOINT_VISDOM") if variables.get("ENDPOINT_VISDOM") else results[0].__str__()
print("VISDOM_ENDPOINT: ", visdom_endpoint)

assert visdom_endpoint is not None

visdom_endpoint = visdom_endpoint.replace("http://", "")

(VISDOM_HOST, VISDOM_PORT) = visdom_endpoint.split(":")

print("Connecting to %s:%s" % (VISDOM_HOST, VISDOM_PORT))
vis = Visdom(server="http://"+VISDOM_HOST, port=int(VISDOM_PORT))
assert vis.check_connection()

# text plot
textwindow = vis.text('Hello World!')
#updatetextwindow = vis.text('Hello World! More text should be here')
#vis.text('And here it is', win=updatetextwindow, append=True)

# show ActiveEon logo
url_image = 'http://s3.eu-west-2.amazonaws.com/activeeon-public/images/logo.jpg'
http = urllib3.PoolManager()
r = http.request('GET', url_image)
image = np.asarray(Image.open(BytesIO(r.data))).astype(np.uint8)
vis_image = image.transpose((2, 0, 1)).astype(np.float64)
vis.image(vis_image,opts=dict(title='ActiveEon', caption='ActiveEon'))