Example #1
0
def plot_loss(l1, l2, ac1, ac2, name, fold):
    """
    Plots train and validation losses and accuracies
    :param l1: list of train losses
    :param l2: list of validation losses
    :param ac1: list of train accuracies
    :param ac2: list of validation accuracies
    :param name: train file name
    :param fold: extension to identify plot images
    """
    plt.clf()
    viz = Visdom()
    #os.system("python -m visdom.server &")
    plt.plot(l1, 'k', label='train', linewidth=.5)
    plt.plot(l2, 'r', label='val', linewidth=.5)
    plt.plot(ac1, 'y', label='Train accuracy', linewidth=.5)
    plt.plot(ac2, 'g', label='Val accuracy', linewidth=.5)
    plt.title(name)
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.gca().spines['top'].set_visible(False)
    plt.gca().spines['right'].set_visible(False)
    plt.legend(loc=0)
    viz.matplot(plt)
    plt.savefig('plots/loss_fold_' + str(fold) + '.png')
def show_pe_chart(max_pe=150, pe_key='3'):
    plt.style.use('seaborn-whitegrid')
    plt.ylabel(u'市盈率', fontproperties='SimHei')
    plt.xlabel(u'时间轴', fontproperties='SimHei')
    plt.title(titles[pe_key], fontproperties='SimHei')
    plt.ylim(-1, max_pe + 10)
    plt.legend(loc=0, prop=font)
    plt.grid(True)
    viz = Visdom(env='main')
    viz.matplot(plt)
Example #3
0
def vis_show_image(image: np.array):
    from matplotlib import pyplot as plt
    import matplotlib
    import cv2
    from visdom import Visdom
    matplotlib.use('Agg')
    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    plt.imshow(rgb_image)
    vis = Visdom()
    vis.matplot(rgb_image)
Example #4
0
def show_salary_chart(ylabel=''):
    global ylabel_dict
    plt.style.use('seaborn-whitegrid')
    plt.xlabel(u'时间轴', fontproperties='SimHei')
    plt.xticks(rotation=-90)
    plt.title(ylabel, fontproperties='SimHei')
    # plt.xlim(2000, 2020)
    # plt.ylim(-1, max_pe+10)
    plt.legend(loc=0, prop=font)
    plt.grid(True)
    viz = Visdom(env='main')
    viz.matplot(plt)
class VisdomWebServer(object):
    def __init__(self):

        DEFAULT_PORT = 8097
        DEFAULT_HOSTNAME = "http://localhost"

        self.vis = Visdom(port=DEFAULT_PORT, server=DEFAULT_HOSTNAME)

    def update(self, metrics):

        if not self.vis.check_connection():
            'No connection could be formed quickly'
            return

        # Learning curve
        try:
            fig, ax = plt.subplots()
            plt.plot(metrics['train_loss'],
                     label='Training loss',
                     color='#32526e')
            plt.plot(metrics['val_loss'],
                     label='Validation loss',
                     color='#ff6b57')
            plt.legend()
            ax.spines['right'].set_visible(False)
            ax.spines['top'].set_visible(False)
            plt.grid(zorder=0, color='lightgray', linestyle='--')
            self.vis.matplot(plt, win='lrcurve')
            plt.close()
            plt.clf()

            fig, ax = plt.subplots()
            plt.plot(metrics['learning_rate'], color='#32526e')
            ax.spines['right'].set_visible(False)
            ax.spines['top'].set_visible(False)
            plt.grid(zorder=0, color='lightgray', linestyle='--')
            self.vis.matplot(plt, win='lr_rate')
            plt.close()
            plt.clf()

            #plt.figure()
            #plt.plot(metrics['zernike_train_loss'], label='Zernike train loss', color='blue')
            #plt.plot(metrics['zernike_val_loss'], label='Zernike val loss', color='red')
            #plt.legend()
            #plt.grid()
            #self.vis.matplot(plt, win='lrcurve_z')
            #plt.close()
            #plt.clf()
        except BaseException as err:
            print('Skipped matplotlib example')
            print('Error message: ', err)
class VisdomLinePlotter(object):
    def __init__(self, env_name='main'):
        self.vis = Visdom()
        self.env = env_name
        self.plots = {}

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

    def plot_matplotlib(self, plot_name, plt):
        self.plots[plot_name] = self.vis.matplot(plt, env=self.env)

    def plot_text(self, text, title='Text'):
        self.vis.text(text, env=self.env, opts=dict(title=title))
def show_detect_result(imgs_arr=[], all_boxes=[]):
    trans_func = transforms.ToPILImage()
    imgs_one_line = int(len(imgs_arr) / 2 + (len(imgs_arr) % 2))
    for idx, img in enumerate(imgs_arr):
        # img_plt = trans_func(img).convert('RGB')
        axes = plt.subplot(2, imgs_one_line, (idx + 1))
        i = 0
        objs = all_boxes[idx]
        print('len(objs): ', len(objs))
        for obj_class in objs:
            # print('item[0]: ', int(item[0].item()))
            for item in obj_class:
                print('item.shape: ', item.shape)
                name = labelmap[int(item[0])]
                score = item[1]
                xmin = int(item[2])
                ymin = int(item[3])
                xmax = int(item[4])
                ymax = int(item[5] )
                # print('name: ', name)
                # print('(xmin, ymin, xmax, ymax): ', xmin, ymin, xmax, ymax)
                i += 1
                i %= len(color_list)
                rect = patches.Rectangle((xmin, ymin), (xmax - xmin), (ymax - ymin),
                                        linewidth=2, edgecolor=color_list[i], fill=False)
                if args.score == True:
                    axes.text(rect.xy[0], rect.xy[1], str(score),
                            va='center', ha='center', color=color_list[i],
                            bbox=dict(facecolor='w'))
                if args.bbox == True:
                    axes.add_patch(rect)

                if args.label == True:
                    axes.text(rect.xy[0], rect.xy[1], name,
                            va='center', ha='center', color='k',
                            bbox=dict(facecolor='w'))
        plt.imshow(img)
        plt.axis('off')
        plt.ioff()

    viz = Visdom(env='ssd_obj_detect')
    viz.matplot(plt)
Example #8
0
class Display_board:
    def __init__(self, port=8097, viz=None, env_name=None):
        if viz is None:
            self.viz = Visdom(port=port, env=env_name)
        else:
            self.viz = viz

    def add_Line_windows(self, name, X=0, Y=0):

        w = self.viz.line(X=np.array([X]),
                          Y=np.array([Y]),
                          opts=dict(title=name))
        return w

    def update_line(self, w, X, Y):
        self.viz.line(X=np.array([X]), Y=np.array([Y]), win=w, update="append")

    def show_image(self, image):
        plt.imshow(image)
        self.viz.matplot(plt)
Example #9
0
class VisdomPlotter:
    def __init__(self):
        self.viz = Visdom(env='BachPropagation')
        self.plots = {}

    def plot_line(self,
                  plot_name,
                  line_label=None,
                  title=None,
                  y_label=None,
                  x=None,
                  y=None,
                  color=None):
        if plot_name not in self.plots:
            opts = {
                'title': title,
                'xlabel': 'Epochs',
                'ylabel': y_label,
                'linecolor': color
            }
            if line_label is not None:
                opts['legend'] = [line_label]
            self.plots[plot_name] = self.viz.line(X=x, Y=y, opts=opts)
        else:
            self.viz.line(X=x,
                          Y=y,
                          win=self.plots[plot_name],
                          name=line_label,
                          update='append',
                          opts={'linecolor': color})

    def display_matplot_figure(self, figure, plot_name):
        if plot_name not in self.plots:
            self.plots[plot_name] = self.viz.matplot(figure)
        else:
            self.viz.matplot(figure, win=self.plots[plot_name])

    def add_song(self, path):
        self.viz.audio(audiofile=path, tensor=None)
Example #10
0
    def train_test_model(self, model_info):
        model_name = model_info["model_name"]
        print("[Current model] : ", model_name, "\nModel setting : ",
              model_info)
        model, model_info = model_handler.create_model_object(model_info)

        model = self.model_cuda_selecter(model)

        # If you want to use pre-trained model, and train the last layer, set the "train_last_layer" as True
        if model_info["train_last_layer"]:
            model = layer_manipulation.train_last_layer(model)

        # create transform object.
        train_loader = self.__create_dataloader(self.train_folder,
                                                self.train_transforms,
                                                model_info["input_size"],
                                                model_info["batch_size"],
                                                model_info["mean"],
                                                model_info["std"], True)
        test_loader = self.__create_dataloader(self.test_folder,
                                               self.test_transforms,
                                               model_info["input_size"],
                                               model_info["batch_size"],
                                               model_info["mean"],
                                               model_info["std"], False)

        loss_func = loss_func_handler.create_loss_func(
            loss_func_name=model_info["loss_func"], **model_info["lf_setting"])
        optimizer = optimizer_handler.create_optimizer(
            model,
            learning_rate=model_info["lr"],
            optimizer_name=model_info["optimizer"],
            **model_info["optimizer_setting"])
        print(model_info["learning_scheduler_setting"])
        scheduler = scheduler_handler.create_train_scheduler(
            optimizer, model_info["learning_scheduler"],
            **model_info["learning_scheduler_setting"])

        epoch_train_acc_list, epoch_train_loss_list = [], []
        epoch_test_acc_list, epoch_test_loss_list = [], []

        # create a several folders into the log folder

        model_main_path = os.path.join(self.log_folder, model_name)
        model_save_path = os.path.join(self.log_folder, model_name,
                                       "model_epoch")
        best_model_save_path = os.path.join(self.log_folder, model_name,
                                            "best_model")
        if os.path.exists(model_main_path) == False:
            os.mkdir(model_main_path)
        if os.path.exists(model_save_path) == False:
            os.mkdir(model_save_path)
        if os.path.exists(best_model_save_path) == False:
            os.mkdir(best_model_save_path)
        txt_path = os.path.join(model_main_path, model_name + "_train_log.txt")
        model_txt_path = os.path.join(model_main_path,
                                      model_name + "_model_architecture.txt")
        train_environment_path = os.path.join(
            model_main_path, model_name + "_train_environment.txt")
        excel_path = os.path.join(model_main_path,
                                  model_name + "_train_result.xlsx")

        result_excel_log = {
            "train_acc": epoch_train_acc_list,
            "train_loss": epoch_train_loss_list,
            "test_acc": epoch_test_acc_list,
            "test_loss": epoch_test_loss_list
        }

        logger.save_txt(model_txt_path, model.__str__())
        logger.save_txt(train_environment_path, train_loader.__str__())
        logger.save_txt(train_environment_path, test_loader.__str__())
        logger.save_txt(train_environment_path, str(model_info))

        best_acc = 0.0
        best_loss = 100000.0

        logger.save_txt(txt_path,
                        "EPOCH, train_acc, train_loss, test_acc, test_loss")

        # Visdom logging session

        if self.use_visdom:
            model_visdom = Visdom(env=model_name)
            self.visdom.append(model_visdom)
            model_visdom.text(model.__str__(),
                              opts=dict(title="model architecture"))
            train_environment = model_visdom.text(
                train_loader.__str__(), opts=dict(title="Training setting"))
            model_visdom.text(test_loader.__str__(),
                              win=train_environment,
                              opts=dict(title="Training setting"),
                              append=True)
            model_visdom.text(str(model_info),
                              win=train_environment,
                              opts=dict(title="Training setting"),
                              append=True)

            model_acc_graph = lineplotstream(
                model_visdom, "{} Train Test Accuracy".format(model_name))
            model_loss_graph = lineplotstream(
                model_visdom, "{} Train Test Loss".format(model_name))

            weight_change = logger.PolygonHistogram3D("Weight", "Epoch",
                                                      "Number of Count",
                                                      "Weight Histogram")
            weight_change_plot = model_visdom.matplot(weight_change.plot())

        # Train, test model

        for epoch in range(1, model_info["max_epoch"] + 1):
            torch.set_grad_enabled(True)
            model, train_acc, train_loss = self.train_model(
                epoch, train_loader, model, loss_func, optimizer)
            scheduler.step()

            torch.set_grad_enabled(False)
            test_acc, test_loss = self.test_model(epoch, test_loader, model,
                                                  loss_func)
            epoch_train_acc_list.append(train_acc)
            epoch_train_loss_list.append(train_loss)
            epoch_test_acc_list.append(test_acc)
            epoch_test_loss_list.append(test_loss)
            text_log = [
                str(x)
                for x in [epoch, train_acc, train_loss, test_acc, test_loss]
            ]
            text_log = "\t".join(text_log)
            logger.save_txt(txt_path, text_log)
            #print(result_excel_log)
            logger.save_excel(
                excel_path,
                [("{}_result".format(model_name), result_excel_log)])

            if self.use_visdom:
                model_acc_graph.update([train_acc], [epoch], legend="Train")
                model_acc_graph.update([test_acc], [epoch], legend="Test")
                model_loss_graph.update([train_loss], [epoch], legend="Train")
                model_loss_graph.update([test_acc], [epoch], legend="Test")
                weights = logger.trainable_parameters(model)
                print(weights)
                weights = [
                    weight.detach().numpy().reshape(-1) for weight in weights
                ]
                weights = np.concatenate(weights)
                print(weights)
                weight_change.update(weights)
                model_visdom.matplot(weight_change.plot(),
                                     win=weight_change_plot)

            model_handler.save_checkpoint(
                model,
                os.path.join(model_save_path, "epoch_{}.pth".format(epoch)))

            if best_acc < test_acc:
                model_handler.save_checkpoint(
                    model,
                    os.path.join(best_model_save_path,
                                 "best_acc_model.pth".format(epoch)))
                best_acc = test_acc
            if best_loss > test_loss:
                model_handler.save_checkpoint(
                    model,
                    os.path.join(best_model_save_path,
                                 "best_loss_model.pth".format(epoch)))
                best_loss = test_loss

        return model, epoch_train_acc_list, epoch_test_loss_list, epoch_test_acc_list, epoch_test_loss_list
Example #11
0
class VisdomPlotter(object):
  def __init__(self, env_name='main', port=80, hostname='localhost'):
    '''
    Params:
      * env_name : str
      * port : int
      * hostname : str
    '''
    self.server_is_running = server_is_running(port, hostname)
    self.viz = Visdom(port=port) if self.server_is_running else None
    self.env = env_name
    self.plots = {}

  def line_plot(self, var_name, split_name, title_name, x, y, x_label='Epochs'):
    '''
    Params:
      * var_name : variable name (e.g. loss, acc)
      * split_name : split name (e.g. train, val)
      * title_name : titles of the graph (e.g. Classification Accuracy)
      * x : x axis value (e.g. epoch number)
      * y : y axis value (e.g. epoch loss)
    '''
    if not self.server_is_running:
      return

    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=x_label,
        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 heatmap_plot(self, var_name, split_name, title_name, data, x_label='Epochs'):
    '''
    Params:
      * var_name : str
      * split_name : str
      * title_name : str
      * data
      * x_label (optional) : str
    '''
    if not self.server_is_running and data is not None:
      return

    labels = list(map(lambda x: x[0], data[0]))
    y = np.array([list(map(lambda x: x[1], line)) for line in data]).T
    ax = sns.heatmap(y, yticklabels=labels, cbar=False)

    plt.tight_layout()
    plt.title(title_name)
    plt.xlabel(x_label)
    plt.ylabel('layers')

    self.viz.matplot(plt, win=var_name, env=self.env)
  
  def matplot_plot(self, var_name, data):
    '''
    Params:
      * var_name : str
      * data
    '''
    if not self.server_is_running and data is not None:
      return
    self.viz.matplot(data, win=var_name, env=self.env)
  
  def image_plot(self, var_name, image):
    '''
    Params:
      * var_name : str
      * image
    '''
    if not self.server_is_running or image is None:
      return
    self.viz.image(image, win=var_name, env=self.env)
Example #12
0
class visual_block(nn.Module):
    def __init__(self, max_row=10, max_column=10, w=60, h=60, margin_right=2, margin_top=2, dpi=30):
        super(visual_block, self).__init__()
        self.axes_pool = {}
        self.max_column = max_column
        self.max_row = max_row
        self.dpi = dpi
        self.layer_count = 0
        self.layer_pool = {}
        self.image_figure_pool = {}
        self.figure_width = self.max_column * (w + margin_right) * 1.0 / dpi
        self.figure_height = self.max_row * (h + margin_top) * 1.0 / dpi
        self.image_w = w * 1.0 / (self.figure_width * dpi)
        self.image_h = h * 1.0 / (self.figure_height * dpi)
        self.width_bias = margin_right / (self.figure_width * dpi)
        self.height_bias = margin_top / (self.figure_height * dpi)
        self.viz = Visdom()

    def update_graph(self, figure, data, params):
        data = data.cpu()
        # 解析参数
        options = {'mode': 'feature_map', 'layer': 'conv1', 'channel_num': 'all',
                   'cmap': None, 'image': ''}
        for i in params:
            if i in options:
                options[i] = params[i]

        if options['channel_num'] == 'all':
            channel_num = len(data)
        else:
            channel_num = options['channel_num']

        index = options['image'] + '_' + options['layer']

        if index not in self.axes_pool:
            if options['layer'] not in self.layer_pool:
                self.layer_count += 1
                self.layer_pool[options['layer']] = self.layer_count
            cli = current_layer_index = self.layer_pool[options['layer']]
            self.axes_pool[index] = []

            for i in range(len(data)):
                width = 1.0 / self.max_column - self.width_bias
                height = 1.0 / self.max_row - self.height_bias
                left = (cli - 1) * 1.0 / self.max_column
                bottom = (self.max_row - i - 1) * 1.0 / self.max_row
                react = [left, bottom, width, height]
                axi = figure.add_axes(react)
                axi.axis('off')
                self.axes_pool[index].append(axi)
        axes = self.axes_pool[index]

        if options['mode'] == 'source_image':
            data = self.denormalize(data)
            img = data.detach().numpy()
            img = np.transpose(img, [1, 2, 0])
            axes[0].imshow(img)
        else:
            data = data.detach().numpy()
            if channel_num > self.max_row:
                channel_num = self.max_row
            for channel in range(channel_num):
                axes[channel].imshow(data[channel], cmap=options['cmap'])
        return figure

    def denormalize(self, x_hat):
        mean = [0.485, 0.456, 0.406]
        std = [0.229, 0.224, 0.225]
        mean = torch.tensor(mean).unsqueeze(1).unsqueeze(1)
        std = torch.tensor(std).unsqueeze(1).unsqueeze(1)
        x = x_hat * std + mean
        return x

    def forward(self, x):

        """
        可视化
        Options:
            mode:   'feature_map','kernel_weights','source_image'
                指定输入的tensor是属于特征图、卷积核,或者是原图
            layer: 'conv1',''
                当前层名称,必填。用于索引 ax 子图
            channel: 'all',1,2,...
                指定要显示的特征图或者卷积核参数数量
        """
        params = x[1]
        x = x[0]
        show_data = x
        for image in range(len(show_data)):
            if 'image%d' % image not in self.image_figure_pool:
                fig = plt.figure(figsize=(self.figure_width, self.figure_height), dpi=self.dpi)
                fig.tight_layout()
                self.image_figure_pool['image%d' % image] = fig
            params['image'] = 'image%d' % image
            figure = self.image_figure_pool['image%d' % image]
            self.update_graph(figure, show_data[image], params)
            if 'end' in params and params['end']:
                self.viz.matplot(plot=self.image_figure_pool['image%d' % image], win='image%d' % image,
                                 env='image%d' % image)
        return x
Example #13
0
            elif event['key'] == 'Backspace':
                curr_txt = curr_txt[:-1]
            elif event['key'] == 'Delete':
                curr_txt = txt
            elif len(event['key']) == 1:
                curr_txt += event['key']
            viz.text(curr_txt, win=callback_text_window)

    viz.register_event_handler(type_callback, callback_text_window)

    # matplotlib demo:
    try:
        import matplotlib.pyplot as plt
        plt.plot([1, 23, 2, 4])
        plt.ylabel('some numbers')
        viz.matplot(plt)
    except BaseException as err:
        print('Skipped matplotlib example')
        print('Error message: ', err)

    # 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
        video_url = 'http://media.w3.org/2010/05/sintel/trailer.ogv'
        # linux
Example #14
0
        image, label = data
        if (cuda_predicate == True):
            image = image.cuda()
            label = label.cuda()
        output = model(image)
        optimizer.zero_grad()
        loss = criterion(output, label)
        loss.backward()
        optimizer.step()
        print("Batch Num:", i)
        if i % WINDOW == WINDOW - 1:
            torch.save(model, './current_model')
            print(loss.item())
            LOSSES.append(loss.item())
            plt.plot(LOSSES)
            viz.matplot(plt, win="loss")

#===============================================================================
# Testing Phase
#===============================================================================
correct = 0
with torch.no_grad():
    for image, label in test_loader:
        if (cuda_predicate == True):
            image = image.cuda()
            label = label.cuda()
        output = model(image)
        pred = output.max(1, keepdim=True)[1]
        correct += pred.eq(label.view_as(pred)).sum().item()
print("Accuracy: ", correct)
Example #15
0
class VisdomLogger():
    def __init__(self,**kwargs):

        if Visdom is None:
            self.viz = None # do nothing
            return

        self.connected = True
        try:
            self.viz = Visdom(raise_exceptions=True,**kwargs)
        except Exception as e:
            print("Could not reach visdom server...")
            self.connected = False
            pass

        self.windows = dict()

        r = np.random.RandomState(1)
        self.colors = r.randint(0,255, size=(255,3))
        self.colors[0] = np.array([1., 1., 1.])
        self.colors[1] = np.array([0. , 0.18431373, 0.65490196]) # ikb blue

    def update(self, data):
        if self.connected:
            self.plot_epochs(data)

    def bar(self,X, name="barplot"):
        if self.connected:
            X[np.isnan(X)] = 0

            win = name.replace(" ","_")

            opts = dict(
                title=name,
                xlabel='t',
                ylabel="P(t)",
                width=600,
                height=200,
                marginleft=20,
                marginright=20,
                marginbottom=20,
                margintop=30
            )

            self.viz.bar(X,win=win,opts=opts)

    def plot(self, X, name="plot",**kwargs):
        if self.connected:

            X[np.isnan(X)] = 0

            win = "pl_"+name.replace(" ","_")

            opts = dict(
                title=name,
                xlabel='t',
                ylabel="P(t)",
                width=600,
                height=200,
                marginleft=20,
                marginright=20,
                marginbottom=20,
                margintop=30,
                **kwargs
            )

            self.viz.line(X ,win=win, opts=opts)

    def confusion_matrix(self, cm, title="Confusion Matrix", norm=None):
        if self.connected:
            plt.clf()

            if norm is not None:
                cm /= np.expand_dims(cm.sum(norm),axis=norm)
                cm[np.isnan(cm)] = 0
                cm[np.isinf(cm)] = 0
                vmin = 0
                vmax = 1
            else:
                vmin = None
                vmax = None

            name=title

            plt.rcParams['figure.figsize'] = (9, 9)
            #sn.set(font_scale=1.4)  # for label size
            ax = sn.heatmap(cm, annot=True, annot_kws={"size": 11}, vmin=vmin, vmax=vmax)  # font size
            ax.set(xlabel='ground truth', ylabel='predicted', title=title)
            plt.tight_layout()
            opts = dict(
                resizeable=True
            )

            self.viz.matplot(plt, win=name, opts=opts)

    def plot_class_p(self,X):
        if self.connected:
            plt.clf()

            x = X.detach().cpu().numpy()
            plt.plot(x[0, :])

            name="confusion matrix"

            plt.rcParams['figure.figsize'] = (6, 6)
            #sn.set(font_scale=1.4)  # for label size
            ax = sn.heatmap(cm, annot=True, annot_kws={"size": 11})  # font size
            ax.set(xlabel='ground truth', ylabel='predicted', title="Confusion Matrix")
            plt.tight_layout()
            opts = dict(
                resizeable=True
            )

            self.viz.matplot(plt, win=name, opts=opts)

    def plot_boxplot(self, labels, t_stops, tmin=None, tmax=None):

        if self.connected:
            grouped = [t_stops[labels == i] for i in np.unique(labels)]
            #legend = ["class {}".format(i) for i in np.unique(labels)]

            plt.clf()

            name = "boxplot"

            plt.rcParams['figure.figsize'] = (9, 9)
            # sn.set(font_scale=1.4)  # for label size
            ax = sn.boxplot(data=grouped, orient="h")
            ax.set_xlabel("t_stop")
            ax.set_ylabel("class")
            ax.set_xlim(tmin, tmax)
            #ax = sn.heatmap(cm, annot=True, annot_kws={"size": 11}, vmin=vmin, vmax=vmax)  # font size
            #ax.set(xlabel='ground truth', ylabel='predicted', title=title)
            plt.tight_layout()
            opts = dict(
                resizeable=True
            )

            self.viz.matplot(plt, win=name, opts=opts)


        pass

    def plot_epochs(self, data):
        """
        Plots mean of epochs
        :param data:
        :return:
        """
        if self.connected:

            data_mean_per_epoch = data.groupby(["mode", "epoch"]).mean()
            cols = data_mean_per_epoch.columns
            modes = data_mean_per_epoch.index.levels[0]

            for name in cols:

                 if name in self.windows.keys():
                     win = self.windows[name]
                     update = 'new'
                 else:
                     win = name # first log -> new window
                     update = None

                 opts = dict(
                     title=name,
                     showlegend=True,
                     xlabel='epochs',
                     ylabel=name)

                 for mode in modes:

                     epochs = data_mean_per_epoch[name].loc[mode].index
                     values = data_mean_per_epoch[name].loc[mode]

                     win = self.viz.line(
                         X=epochs,
                         Y=values,
                         name=mode,
                         win=win,
                         opts=opts,
                         update=update
                     )
                     update='insert'

                 self.windows[name] = win
Example #16
0
class ppo:
    def __init__(self,
                 env_fn,
                 seed=0,
                 steps_per_epoch=4000,
                 epochs=50,
                 gamma=0.99,
                 clip_ratio=0.2,
                 pi_lr=3e-4,
                 vf_lr=1e-3,
                 train_pi_iters=80,
                 train_v_iters=80,
                 lam=0.97,
                 max_ep_len=1000,
                 target_kl=0.01,
                 save_freq=10,
                 exp_name='',
                 test_agent=False,
                 load=False):
        # seed
        self.seed = seed
        self.seed += 10000 * proc_id()
        tf.set_random_seed(self.seed)
        np.random.seed(self.seed)
        random.seed(self.seed)

        # Hyper-parameter
        self.env = env_fn()
        self.obs_dim = self.env.observation_space.shape
        self.act_dim = self.env.action_space.shape
        self.gamma, self.lam = gamma, lam
        self.steps_per_epoch, self.epochs = steps_per_epoch, epochs

        self.pi_lr, self.vf_lr = pi_lr, vf_lr
        self.clip_ratio, self.target_kl = clip_ratio, target_kl
        self.train_pi_iters, self.train_v_iters = train_pi_iters, train_v_iters
        self.exp_name, self.save_freq, self.max_ep_len = exp_name, save_freq, max_ep_len

        if test_agent:
            from visdom import Visdom
            self.viz = Visdom()
            assert self.viz.check_connection()
            self.win = self.viz.matplot(plt)

        # Experience buffer
        self.buf = PPOBuffer(self.obs_dim, self.act_dim, self.steps_per_epoch,
                             self.gamma, self.lam)

        self.graph = tf.Graph()
        with self.graph.as_default():
            self.sess = tf.Session(config=config, graph=self.graph)

            self.__make_model()
            self.sess.run(tf.global_variables_initializer())

            # Sync params across processes
            self.sess.run(sync_all_params())
            Count_Variables()
            print('Trainable_variables:')
            for v in tf.compat.v1.trainable_variables():
                print('{}\t  {}'.format(v.name, str(v.shape)))

            var_list = tf.global_variables()
            self.saver = tf.compat.v1.train.Saver(var_list=var_list,
                                                  max_to_keep=1)

            # summary
            self.writer = tf.compat.v1.summary.FileWriter("logs/" + exp_name)
            if load:
                self.load()
            self.sess.run(sync_all_params())

            self.ep_ret_ph = tf.placeholder(tf.float32,
                                            shape=(),
                                            name="ep_ret_ph")
            self.ep_Entropy_ph = tf.placeholder(tf.float32,
                                                shape=(),
                                                name="Entropy")
            self.clipfrac_ph = tf.placeholder(tf.float32,
                                              shape=(),
                                              name="clipfrac")
            self.ep_len_ph = tf.placeholder(tf.float32,
                                            shape=(),
                                            name="ep_len_ph")
            self.test_summary = tf.compat.v1.summary.merge([
                tf.compat.v1.summary.scalar('EP_ret',
                                            self.ep_ret_ph,
                                            family='test'),
                tf.compat.v1.summary.scalar('EP_len',
                                            self.ep_len_ph,
                                            family='test')
            ])
            self.entropy_summary = tf.compat.v1.summary.merge([
                tf.compat.v1.summary.scalar('Entropy',
                                            self.ep_Entropy_ph,
                                            family='test'),
                tf.compat.v1.summary.scalar('clipfrac',
                                            self.clipfrac_ph,
                                            family='test')
            ])

    def __make_model(self):

        self.o1_ph = tf.placeholder(tf.float32, [None, 128, 128, 3],
                                    name='o1_ph')
        self.o2_ph = tf.placeholder(tf.float32, [None, 128, 128, 3],
                                    name='o2_ph')
        self.o_low_dim_ph = tf.placeholder(tf.float32, [None, 5],
                                           name='o_low_dim_ph')
        self.f_s_ph = tf.placeholder(tf.float32, [None, 14], name='f_s_ph')

        a_ph, adv_ph, ret_ph, logp_old_ph = ppo_core.placeholders(
            self.act_dim, None, None, None)
        pi, logp, logp_pi, self.v = ppo_core.mlp_actor_critic(
            self.o1_ph,
            self.o2_ph,
            self.o_low_dim_ph,
            self.f_s_ph,
            a_ph,
            action_space=self.env.action_space)

        self.all_phs = [
            self.o1_ph, self.o2_ph, self.o_low_dim_ph, self.f_s_ph, a_ph,
            adv_ph, ret_ph, logp_old_ph
        ]

        self.get_action_ops = [pi, self.v, logp_pi]

        # PPO objectives
        ratio = tf.exp(logp - logp_old_ph)  # pi(a|s) / pi_old(a|s)
        min_adv = tf.where(adv_ph > 0, (1 + self.clip_ratio) * adv_ph,
                           (1 - self.clip_ratio) * adv_ph)
        self.pi_loss = -tf.reduce_mean(tf.minimum(ratio * adv_ph, min_adv))
        self.v_loss = tf.reduce_mean((ret_ph - self.v)**2)

        # Info (useful to watch during learning)
        self.approx_kl = tf.reduce_mean(
            logp_old_ph -
            logp)  # a sample estimate for KL-divergence, easy to compute
        self.approx_ent = tf.reduce_mean(
            -logp)  # a sample estimate for entropy, also easy to compute
        clipped = tf.logical_or(ratio > (1 + self.clip_ratio), ratio <
                                (1 - self.clip_ratio))
        self.clipfrac = tf.reduce_mean(tf.cast(clipped, tf.float32))

        # Optimizers
        self.train_pi = MpiAdamOptimizer(learning_rate=self.pi_lr).minimize(
            self.pi_loss)
        self.train_v = MpiAdamOptimizer(learning_rate=self.vf_lr).minimize(
            self.v_loss)

    def update(self):
        inputs = {k: v for k, v in zip(self.all_phs, self.buf.get())}

        # Training
        for i in range(self.train_pi_iters):
            _, kl = self.sess.run([self.train_pi, self.approx_kl],
                                  feed_dict=inputs)
            kl = mpi_avg(kl)

            if kl > 1.5 * self.target_kl:
                print(
                    'process %d: Early stopping at step %d due to reaching max kl.'
                    % (proc_id(), i))
                break

        for _ in range(self.train_v_iters):
            self.sess.run(self.train_v, feed_dict=inputs)

    def rollout(self):
        o, r, d, ep_ret, ep_len = self.env.reset(), 0, False, 0, 0

        # Main loop: collect experience in env and update/log each epoch
        for epoch in tqdm(range(self.epochs)):
            for t in range(self.steps_per_epoch):
                f_s = self.env.full_state()
                a, v_t, logp_t = self.sess.run(self.get_action_ops,
                                               feed_dict={
                                                   self.o1_ph:
                                                   o[0][np.newaxis, ],
                                                   self.o2_ph:
                                                   o[1][np.newaxis, ],
                                                   self.o_low_dim_ph:
                                                   o[2][np.newaxis, ],
                                                   self.f_s_ph:
                                                   f_s[np.newaxis, ]
                                               })

                o2, r, d, _ = self.env.step(a[0])
                ep_ret += r
                ep_len += 1

                # save and log
                self.buf.store(o, f_s, a, r, v_t, logp_t)

                # Update obs (critical!)
                o = o2

                terminal = d or (ep_len == self.max_ep_len)
                if terminal or (t == self.steps_per_epoch - 1):
                    if not (terminal):
                        print(
                            'process %d: trajectory cut off by epoch at %d steps.'
                            % (proc_id(), ep_len))
                    # if trajectory didn't reach terminal state, bootstrap value target
                    last_val = 0 if d else self.sess.run(
                        self.v,
                        feed_dict={
                            self.o1_ph: o[0][np.newaxis, ],
                            self.o2_ph: o[1][np.newaxis, ],
                            self.o_low_dim_ph: o[2][np.newaxis, ],
                            self.f_s_ph: f_s[np.newaxis, ]
                        })
                    self.buf.finish_path(last_val)

                    if proc_id() == 0:
                        ep_s = self.sess.run(self.test_summary, {
                            self.ep_ret_ph: ep_ret,
                            self.ep_len_ph: ep_len
                        })
                        self.writer.add_summary(ep_s, global_step=epoch)

                    o, ep_ret, ep_len = self.env.reset(), 0, 0

            # Save model
            if (epoch % self.save_freq == 0) or (epoch == self.epochs - 1):
                if proc_id() == 0:
                    self.Save()

            # Perform PPO update!
            self.update()

    def Save(self):
        path = "model/" + self.exp_name + "/model.ckpt"
        print("process %d:  Save model to the: '{}'".format(path) % proc_id())
        self.saver.save(self.sess, save_path=path)

    def load(self):
        path = "model/" + self.exp_name + "/model.ckpt"
        print("\nLoad model From the: '{}'\n".format(path))
        self.saver.restore(self.sess, save_path=path)

    def _choose_action(self, o):
        a, v_t, logp_t = self.sess.run(self.get_action_ops,
                                       feed_dict={
                                           self.o1_ph: o[0][np.newaxis, ],
                                           self.o2_ph: o[1][np.newaxis, ],
                                           self.o_low_dim_ph:
                                           o[2][np.newaxis, ]
                                       })
        return a[0], v_t

    def test_agent(self, n=1):
        ep_r = []
        ep_l = []
        plt.figure(figsize=(9, 5))
        for j in range(n):
            print('------------------Epoch: %d-------------------' % j)

            o, r, d, ep_ret, ep_len, level_plot = self.env.reset(
            ), 0, False, 0, 0, []
            v_plot = []
            while not (d or (ep_len == self.max_ep_len)):

                # Take deterministic actions at test time
                a, v_t = self._choose_action(o)
                print('V: {}'.format(v_t[0]))
                v_plot.append(v_t)
                # _ = input()
                o, r, d, info = self.env.step(a, _step=ep_len)
                print("step: {} a: {}  r: {}".format(ep_len,
                                                     np.around(a, decimals=2),
                                                     r))
                ep_ret += r
                ep_len += 1
                level_plot.append(self.env._rank_before)
                self.win = R_plot(level_plot, v_plot, self.viz, self.win)

            print('EP_ret: %d, \t EP_Len: %d' % (ep_ret, ep_len))

            if info['is_success']:
                print('success!')
            ep_r.append(ep_ret), ep_l.append(ep_len)
        plt.close()
Example #17
0
                     type=int,
                     default=201,
                     help='case id to visualize.')
 args = parser.parse_args()
 # --------- Connect to visdom server -------------------------------------------------
 viz = Visdom(server='http://127.0.0.1', port=8097, env='test')
 assert viz.check_connection()
 # img_data = load_volume(args.case_num).get_fdata()
 img_data = \
     Kits2019DataLoader3D.load_patient(os.path.join('/home/data_share/npy_data/', str(args.case_num).zfill(5)))[0][1]
 img_shape = img_data.shape
 print(img_data.dtype)
 assert len(np.unique(img_data)) == 3
 print(img_shape)
 plt.imshow(img_data[:, :, img_shape[-1] // 2], cmap='gray')
 viz.matplot(plt, opts={'title': 'case_original', 'showlegend': True})
 mirrored = np.flip(img_data.copy(), axis=(0, 1, 2))
 # print(mirrored.shape)
 # plt.imshow(mirrored[img_shape[-1] // 2, :, :], cmap='gray')
 # viz.matplot(
 #     plt,
 #     opts={
 #         'title': 'case_mirrored',
 #         'showlegend': True
 #     }
 # )
 patches = image.extract_patches(img_data, patch_size, strides)
 print(patches.shape)
 m_patches = image.extract_patches(mirrored, patch_size, strides)
 print(m_patches.shape)
 # patches = patches.reshape((-1, patch_size[0], patch_size[1], patch_size[2]))
Example #18
0
def main():
    global args
    args = parser.parse_args()

    # Get options
    opt_list = []
    for filename in args.path_opts:
        with open(filename, 'r') as f:
            opt = yaml.load(f)
        opt_list.append(opt)
    with open(args.vis_path_opt, 'r') as handle:
        vis_options = yaml.load(handle)
    print('## args')
    pprint(vars(args))

    # Get the relevant curves
    algo = args.algo
    env_name = args.env_name
    num_trials = args.num_trials
    trial_offset = args.trial_offset
    x_curves = []
    mean_curves = []
    bottom_curves = []
    top_curves = []
    for opt in opt_list:
        # Get the curves for all the trials for each yaml file (mean and variance)
        x_curve, mean_curve, top_curve, bottom_curve = get_eval_vals(
            opt, vis_options, args.eval_key, algo, env_name, num_trials,
            trial_offset, args.bin_size, args.smooth, args.mode)
        x_curves.append(x_curve)
        mean_curves.append(mean_curve)
        bottom_curves.append(bottom_curve)
        top_curves.append(top_curve)
    assert (len(x_curves) == len(args.legend_names))
    assert (len(mean_curves) == len(args.legend_names))
    assert (len(bottom_curves) == len(args.legend_names))
    assert (len(top_curves) == len(args.legend_names))

    # Plot the curves
    if args.no_vis:
        raise Exception("No, we need visdom to display")
    vis = Visdom(port=args.port)
    plt.figure()
    for leg_name, x, mean, bot, top in zip(args.legend_names, x_curves,
                                           mean_curves, bottom_curves,
                                           top_curves):
        # Subsample (because why not?)
        x = x[::100]
        mean = mean[::100]
        p = plt.plot(x, mean, label=leg_name)
        if args.mode == 'all':
            for curve in top:
                curve = curve[::100]
                plt.plot(x, curve)
        else:
            bot = bot[::100]
            top = top[::100]
            plt.fill_between(x, bot, top, alpha=0.2)
    plt.legend()
    plt.xlabel('Number of Timesteps')
    plt.ylabel(args.eval_key)
    plt.title(args.eval_key)
    plt.grid(True)
    vis.matplot(plt)
    plt.close()
Example #19
0
    policy_losses = np.zeros(args.train_episodes)
    value_losses = np.zeros(args.train_episodes)
    lengths = np.zeros(args.train_episodes)
    rewards_mean = np.zeros(args.train_episodes // args.episodes_per_eval + 1)
    rewards_std = np.zeros(args.train_episodes // args.episodes_per_eval + 1)
    rewards_mean[0], rewards_std[0] = a2c.eval(args.test_episodes)
    print('episode', 0, 'reward average', rewards_mean[0], 'reward std', rewards_std[0])
    plt.xlabel('episodes')
    plt.ylabel('average reward')
    errbar = plt.errorbar(np.arange(1), rewards_mean[:1], rewards_std[:1], capsize=3)

    viz = Visdom()
    policy_loss_plot = None
    value_loss_plot = None
    length_plot = None
    reward_plot = viz.matplot(plt, env=args.task_name)

    for i in range(args.train_episodes):
        policy_losses[i], value_losses[i], lengths[i] = a2c.train(args.gamma, args.r_scale)
        if (i + 1) % args.episodes_per_plot == 0:
            if policy_loss_plot is None:
                opts = dict(xlabel='episodes', ylabel='policy loss')
                policy_loss_plot = viz.line(X=np.arange(1, i + 2), Y=policy_losses[:i + 1],
                                            env=args.task_name, opts=opts)
            else:
                viz.line(X=np.arange(i - args.episodes_per_plot + 1, i + 2),
                         Y=policy_losses[i - args.episodes_per_plot:i + 1],
                         env=args.task_name, win=policy_loss_plot, update='append')
            if value_loss_plot is None:
                opts = dict(xlabel='episodes', ylabel='value loss')
                value_loss_plot = viz.line(X=np.arange(1, i + 2), Y=value_losses[:i + 1],
class Visdom_Plot(object):
    def __init__(self, title, port=8097, env_name='main'):
        #当‘env_name'不是main时,创建一个新环境
        self.viz = Visdom(port=port, env=env_name)
        self.loss_win = {}
        self.acc_win = {}
        self.text_win = {}
        self.plt_img_win = {}
        self.images_win = {}
        self.gray_win = {}
        self.title = title

    def _new_win(self,
                 type='loss_win',
                 win_name='default_loss_win',
                 id='train_loss',
                 H_img=100):
        '''
        type: loss_win, acc_win, text_win, plt_img_win
        name: default is the default win in class. you can specify a window's name
        id: the line's name
        '''

        assert type in [
            'loss_win', 'acc_win', 'text_win', 'plt_img_win', 'gray_win'
        ], "win type must a string inside  ['loss_win', 'acc_win', 'text_win', 'plt_img_win'] "
        if type == 'loss_win':
            self.loss_win[win_name] = self.viz.line(X=np.array([0]),
                                                    Y=np.array([0]),
                                                    name=id,
                                                    opts=dict(
                                                        xlabel='Epoch.batch',
                                                        ylabel='Loss',
                                                        title=win_name,
                                                        marginleft=60,
                                                        marginbottom=60,
                                                        margintop=80,
                                                        width=800,
                                                        height=600,
                                                    ))
        elif type == 'acc_win':
            self.acc_win[win_name] = self.viz.line(X=np.array([0]),
                                                   Y=np.array([0]),
                                                   name=id,
                                                   opts=dict(
                                                       xlabel='Epoch.batch',
                                                       ylabel='Top1 accuracy',
                                                       title=win_name,
                                                       showlegend=True,
                                                       markercolor=np.array(
                                                           [[255, 0, 0]]),
                                                       marginleft=60,
                                                       marginbottom=60,
                                                       margintop=60,
                                                       width=800,
                                                       height=600,
                                                   ))
        elif type == 'plt_img_win' or type == 'gray_win':
            getattr(self,
                    type)[win_name] = self.viz.images(np.random.randn(
                        1, 3, 100, 100),
                                                      opts=dict(
                                                          height=H_img * 5,
                                                          width=H_img * 5,
                                                      ))
        elif type == 'text_win':
            self.text_win[win_name] = self.viz.text('Text Window')

    def append_loss(self,
                    loss,
                    epoch_batches,
                    win_name='default_loss_win',
                    id='train_loss'):
        if win_name not in self.loss_win:
            self._new_win(type='loss_win', win_name=win_name, id=id)
        self.viz.line(X=np.array([epoch_batches]),
                      Y=np.array([loss]),
                      win=self.loss_win[win_name],
                      name=id,
                      opts=dict(showlegend=True),
                      update='append')

    def append_acc(self,
                   train_acc,
                   epoch_batches,
                   win_name='default_acc_win',
                   id='train_acc'):
        if win_name not in self.acc_win:
            self._new_win(type='acc_win', win_name=win_name, id=id)

        self.viz.line(X=np.array([epoch_batches]),
                      Y=np.array([train_acc]),
                      win=self.acc_win[win_name],
                      name=id,
                      opts=dict(showlegend=True),
                      update='append')

    def lr_scatter(self, epoch, lr, win_name='default_acc_win'):
        self.viz.scatter(
            X=np.array([[epoch, 20]]),
            name='lr=' + str(lr),
            win=self.acc_win[win_name],
            opts=dict(showlegend=True),
            update='append',
        )

    def img_plot(self, images, lm=None, mode='update', caption=''):
        '''
        Input:
        images : tensors, N x 3 x H x W, so transfer to N x H x W x 3 is needed
        lm : N x K x 2, is not None, then landmarks will be scattered.
        '''
        win_exist = len(self.plt_img_win)
        N, C, H, W = images.size()
        if N > win_exist:
            for i in range(win_exist, N, 1):
                self._new_win(type='plt_img_win',
                              win_name='image' + str(i),
                              H_img=H)
        if lm is not None:
            N, K, m = lm.size()
            assert N == images.size(
            )[0] and m == 2, "landmarks have illegal size"
            lm = lm.cpu()
        images = images.cpu()

        plt.figure(figsize=(H * 0.06, W * 0.06))
        for n, image in enumerate(images[:]):
            # print(image.size())
            image = image.transpose(0, 1).transpose(1, 2)
            # print(image.size())
            plt.imshow(image.detach().numpy(
            ))  # convert to H x W x 3. plt的输入是HxWx3,而viz.images())的输入是3xHxW
            if lm is not None:
                color = np.linspace(0, 1, num=K)
                plt.scatter(x=lm[n, :, 0].detach().numpy(),
                            y=lm[n, :, 1].detach().numpy(),
                            c=color,
                            marker='x',
                            s=200)
                self.viz.matplot(plt,
                                 win=self.plt_img_win['image' + str(n)],
                                 opts=dict(caption='image' + str(n)))
                plt.clf()

    def images(self, images, win_name='default_images_win'):
        '''
        Input:
        images:N x 3 x H x W, tensors
        '''
        images = images.cpu()
        if win_name not in self.images_win:
            self.images_win[win_name] = self.viz.images(
                images.detach().numpy())
        else:
            self.viz.images(images.detach().numpy(),
                            win=self.images_win[win_name])

    def gray_images(self, images, win_name='default_gray_win'):
        '''
        Input:
        images : K x H x W, tensors
        '''
        images = images.cpu()
        win_exist = len(self.gray_win)
        K, H, W = images.size()
        if K > win_exist:
            for i in range(win_exist, K, 1):
                self._new_win(type='gray_win',
                              win_name='gray' + str(i),
                              H_img=H // 2)

        plt.figure(figsize=(H / 2 * 0.06, W / 2 * 0.06))
        for n, image in enumerate(images):
            plt.imshow(image.detach().numpy(
            ))  # convert to H x W x 3. plt的输入是HxWx3,而viz.images())的输入是3xHxW
            self.viz.matplot(plt, win=self.gray_win['gray' + str(n)])
            plt.clf()

    def append_text(self, text, win_name='default_text_win', append=True):
        if win_name not in self.text_win:
            self._new_win(type='text_win', win_name=win_name)
        self.viz.text(text, win=self.text_win[win_name], append=append)
Example #21
0
class GenericVisdomPlotter(GenericPlotter):
    '''
    Visdom based generic plotter implementation
    '''
    def __init__(self, config):
        '''
        Initializer
        '''
        super(GenericVisdomPlotter, self).__init__(config)

        # prepare the environment name
        self.env = 'Cami_' + config
        # default host and port
        hostname = 'deep'  # "deep"
        baseurl = "/visdom"
        myport = 80
        # replace host and port by the one provided in the config file
        # if 'hostname' in config['visualization']:
        #     hostname = config['visualization']['hostname']
        # if 'port' in config['visualization']:
        #     port = int(config['visualization']['port'])

        # initialize the object for visualization
        self.viz = Visdom(server=hostname,
                          base_url=baseurl,
                          port=myport,
                          use_incoming_socket=False,
                          use_polling=True)
        # the dictionary of plots and figures
        self.figures = dict()
        self.plots = dict()

        # initialize the current epoch in 0
        self.current_epoch = 0

    def plot(self, plot_name, split_name, x, y, x_label='Epochs'):
        '''
        Plot a line plot
        '''

        # if the plot is not in the dictionary, initialize one
        if (plot_name not in self.plots):
            self.plots[plot_name] = self.viz.line(X=np.array([x, x]),
                                                  Y=np.array([y, y]),
                                                  env=self.env,
                                                  opts=dict(
                                                      legend=[str(split_name)],
                                                      title=plot_name,
                                                      xlabel=x_label,
                                                      ylabel=plot_name))
        # if the plot is already there, update
        else:
            self.viz.line(X=np.array([x]),
                          Y=np.array([y]),
                          env=self.env,
                          update='append',
                          win=self.plots[plot_name],
                          name=str(split_name))

    def plot_matplotlib(self, plt, plot_name="matplotlib_progreso"):
        self.viz.matplot(plt, env=self.env, win=plot_name)

    def plot_multiple_statistics(self, plot_name, x, y_values):
        '''
        Plot multiple statistics within the same plot
        '''
        # get the split names
        split_names = y_values.keys()
        # iterate for each of them
        for split in split_names:
            # plot the values
            self.plot(plot_name, split, x, y_values[split])

    def plot_text(self, info, plot_name="some_text"):
        '''
        Plot some text
        '''
        self.viz.text(info, env=self.env, win=plot_name)

    def plot_scalar(self, plot_name, x, y, legend):
        '''
        Plot a line plot
        '''
        self.plot(plot_name, legend, x, y)

    def display_image(self, image_key, image, caption=''):
        '''
        Display given image in the plot
        '''
        # if the image is already in the plot, remove it to replace it for the new one
        if image_key in self.figures:
            self.viz.close(win=self.figures[image_key], env=self.env)
            del self.figures[image_key]
        # plot the image
        self.figures[image_key] = self.viz.images(image,
                                                  env=self.env,
                                                  opts=dict(title=caption))

    def close_client(self):

        self.viz.use_socket = False
Example #22
0
class Visualizer:
    def __init__(self, cfg: DictConfig):
        self.cfg = cfg.visdom

        visdom_command = 'screen -S visdom_{} -d -m bash -c "python -m visdom.server -port {}"'.format(
            self.cfg.port, self.cfg.port
        )

        os.mkdir("visdom")
        os.system(visdom_command)
        time.sleep(2)
        # self.env = self.cfg.default_env_name  # TODO: What is this
        self.vis = Visdom(
            port=self.cfg.port,
            log_to_filename=os.path.join("visdom", self.cfg.log_to_filename),
            offline=self.cfg.offline,
        )
        (self.x_min, self.x_max), (self.y_min, self.y_max) = (
            (self.cfg.x_min, self.cfg.x_max),
            (self.cfg.y_min, self.cfg.y_max),
        )
        self.counter = 0
        self.plots = {}

    def img_result(
        self,
        img_list: List[np.ndarray],
        nrow: int,
        caption: str = "view",
        title: str = "title",
        win: int = 1,
        env: str = None,
    ):
        self.vis.images(
            img_list,
            nrow=nrow,
            win=win,
            opts={"caption": caption, "title": title},
            env=env,
        )

    def plot_img_255(
        self, img_key: str, img: np.ndarray, env: str = None, opts: Dict = None,
    ):
        """Visdom plot a single image (channels-first CxHxW)"""
        self.vis.image(img, win=img_key, opts=opts, env=env)

    def plot_matplotlib(self, fig, caption="view", title="title", win=1, env=None):
        self.vis.matplot(
            fig,
            win=win,
            opts={"caption": caption, "title": title, "resizable": True},
            env=env,
        )

    def plot_plotly(self, fig, caption="view", title="title", win=1, env=None):
        self.vis.plotlyplot(fig, win=win, env=env)

    def plot(
        self,
        plot_key: str,
        split_name: str,
        x: int,
        y: int,
        env: str = None,
        opts: Dict = None,
    ):
        """Visdom plot line data"""
        if plot_key not in self.plots:
            self.plots[plot_key] = self.vis.line(
                X=np.array([x, x]),
                Y=np.array([y, y]),
                env=env,
                opts={**opts, "legend": [split_name]},
            )
        else:
            self.vis.line(
                X=np.array([x]),
                Y=np.array([y]),
                env=env,
                win=self.plots[plot_key],
                name=split_name,
                update="append",
            )
Example #23
0
def align_execute_mean_two():
    temp = []
    f = open(two_task_time_csv, 'r')
    csv_r = csv.reader(f)
    for p in csv_r:
        temp.append(p)
    f.close()
    length = len(temp)

    print('length of rough pearson correlation list: %d' % length)
    # Build the dictionary for storing the complemented correlation
    str_i = '-'
    inference_exe_join = []
    for i in inference_exe:
        # inference_exe_join.append(str_i.join(i))
        inference_exe_join.append(i[2] + '_' + i[3] + '_' + i[1])

    dict_two = {}
    # em_exe = poly_exe + inference_exe_join
    em_exe = poly_exe + inference_exe_join + example_exe

    for p in em_exe:
        dict_two[p] = []

    # search each kind of benchmark and complement all the combinations with it
    for i in range(length):
        obj = temp[i]
        print('*** %s *** & *** %s ***' % (obj[0], obj[1]))
        group = temp[i]
        if group[0] != group[1]:
            x1, x2 = dict_two[group[0]], dict_two[group[1]]
            x1.append(group)
            x2.append(
                [group[1], group[0], group[4], group[5], group[2], group[3]])
            dict_two[group[0]], dict_two[group[1]] = x1, x2
        else:
            x1 = dict_two[group[0]]
            x1.append(group)
            dict_two[group[0]] = x1

    # write the dictionary to csv file
    f = open(align_two_task_time_csv, 'a+')
    csv_w = csv.writer(f)

    for p in dict_two.keys():
        list_cor = dict_two[p]
        for i in list_cor:
            # print i
            csv_w.writerow(i)
    f.close()

    # write the dictionary to csv file
    f = open(align_two_task_time_gap_csv, 'a+')
    csv_w = csv.writer(f)

    x = []
    y1 = []
    y2 = []

    for p in dict_two.keys():
        list_cor = dict_two[p]
        for i in list_cor:
            gap_1 = float(i[3]) - float(i[2])
            gap_2 = float(i[5]) - float(i[4])
            csv_w.writerow([i[0], i[1], gap_1, gap_2])
            x.append(i[0] + '&' + i[1])
            y1.append(gap_1)
            y2.append(gap_2)
    f.close()

    # plot the diagram
    viz = Visdom()
    assert viz.check_connection()

    # ----------------------------------------------------------------------------------------------
    # plot all the points via matplotlib.pyplot
    # ----------------------------------------------------------------------------------------------
    try:
        import matplotlib.pyplot as plt
        plt.switch_backend('agg')
        # plt.plot(y1[6500:7000])
        plt.plot(y2)
        plt.ylabel('numerical numbers')
        win = viz.matplot(plt)
    except BaseException as err:
        print('Skipped matplotlib example')
        print('Error message: ', err)

    assert viz.win_exists(win), 'Created window marked as not existing'
Example #24
0
class Dashboard(object):
    def __init__(self, opt, vis_opt, logpath, vis=True, port=8097, mode='realtime'):
        # Get relevant options
        exp_name = opt['logs']['exp_name']
        if 'env' in opt:
            self.game = opt['env']['env-name']
            self.alg_name = opt['alg']['algo']
            self.num_steps = opt['optim']['num_frames']
            trial = opt['trial']
            self.unique_name = exp_name + ' ' + self.game + ' ' + self.alg_name + ' trial %d' % trial    
        else:
            self.unique_name = exp_name + ' supervised'

        # Get strings for different monitors
        self.episode_monitor_str = vis_opt['episode_monitor_str'] if 'episode_monitor_str' in vis_opt else None
        self.step_monitor_str = vis_opt['step_monitor_str'] if 'step_monitor_str' in vis_opt else None
        self.alg_monitor_str = vis_opt['alg_monitor_str'] if 'alg_monitor_str' in vis_opt else None   
        self.ll_alg_monitor_str = vis_opt['ll_alg_monitor_str'] if 'll_alg_monitor_str' in vis_opt else None
        # TODO - do this later if we care
        self.logpath = logpath
       
        # Get keys we want to plot
        self.plot_keys = vis_opt['plot_keys']
 
        # Timing stuff
        # Keep track of last time we updated info for a specific episode
        # This lets us 
        self.time_since_update = {}
        
        # Set up Visdom server and windows
        if vis: 
            self.viz = Visdom(port=port)
            self.wins = {}

        # Save opt for special cases
        self.opt = opt        
       
    # Save the current windows to file
    def dump_plots(self):
        # TODO unsure of usage, but this seems good to add
        return

    # Do some smoothing on our curves
    def smooth_curve(self, x, y):
        # Halfwidth of our smoothing convolution
        halfwidth = min(31, int(np.ceil(len(x) / 30)))
        k = halfwidth
        xsmoo = x[k:-k]
        ysmoo = np.convolve(y, np.ones(2 * k + 1), mode='valid') / \
            np.convolve(np.ones_like(y), np.ones(2 * k + 1), mode='valid')
        downsample = max(int(np.floor(len(xsmoo) / 1e3)), 1)
        return xsmoo[::downsample], ysmoo[::downsample]

    # Does binning of x and y over the given interval
    def fix_point(self, x, y, interval):
        np.insert(x, 0, 0)
        np.insert(y, 0, 0)

        fx, fy = [], []
        pointer = 0

        ninterval = int(max(x) / interval + 1)

        for i in range(ninterval):
            tmpx = interval * i

            while pointer + 1 < len(x) and tmpx > x[pointer + 1]:
                pointer += 1

            if pointer + 1 < len(x):
                alpha = (y[pointer + 1] - y[pointer]) / \
                    (x[pointer + 1] - x[pointer])
                tmpy = y[pointer] + alpha * (tmpx - x[pointer])
                fx.append(tmpx)
                fy.append(tmpy)

        return fx, fy

    # TODO - Make a function that outputs our variance plots over multiple trials
    def plot_trial_var_values(self):
        return
    
    # Given keys and a list of lists, return a dictionary which for each key contains a list
    def format_data(self, keys, data):
        # Make struct, and init each key with empty list
        ret_struct = {}
        for key in keys:
            ret_struct[key] = []
            
        # Go through our data, and populate the lists
        for data_row in data:
            if len(keys) != len(data_row):
                pdb.set_trace()
                # TODO - there is some weird error where there is a missing newline between the header row and first data row
                # Figure out how to deal with this elegantly, or just restart the job
            #assert(len(keys) == len(data_row))
            for key, val in zip(keys, data_row):
                ret_struct[key].append(val)
        
        # Check length makes sense
        for key in keys:
            assert(len(ret_struct[key]) == len(data))
            
        return ret_struct
    
    # Preload all the file data
    def preload_data(self):
        # Episode monitor (one for each thread)
        if self.episode_monitor_str:
            episode_monitor_data = []
            infiles = glob.glob(os.path.join(self.logpath, '*.' + self.episode_monitor_str))
            for inf in infiles:
                # Read all the lines at once quickly
                with open(inf, 'r') as f:
                    rawlines = f.readlines()
                rawlines = [l.rstrip('\n') for l in rawlines]           
 
                # Get the header with key names
                episode_monitor_keys = rawlines[1].split(',')
                time_index = episode_monitor_keys.index('episode_dt')
            
                # Get values line by line
                for line in rawlines[2:]:
                    line_vals = line.split(',')
                    t_time = float(line_vals[time_index])
                    tmp = [t_time, line_vals]
                    episode_monitor_data.append(tmp)
        
            # Resort episode monitor by timestamp
            episode_monitor_data = sorted(episode_monitor_data, key=lambda x: x[0])
            episode_monitor_data = [x[1] for x in episode_monitor_data]
            
            # Extract the lists for each key
            self.episode_monitor_data = self.format_data(episode_monitor_keys, episode_monitor_data)
            
            # Get the x axis (episode_len, but using cumulitive sum)
            self.episode_monitor_x = np.cumsum([int(x) for x in self.episode_monitor_data['episode_len']])
            self.episode_monitor_x = np.concatenate((np.array([0]), self.episode_monitor_x[:-1]))
                
        # Load the last episode's step monitor into memory (note, only one, even for multithreaded)
        if self.step_monitor_str:
            step_monitor_data = []
            # Read all the lines at once quickly
            with open(os.path.join(self.logpath, self.step_monitor_str), 'r') as f:
                rawlines = f.readlines()
            rawlines = [l.rstrip('\n') for l in rawlines]               
 
            # Get the header with key names
            step_monitor_keys = rawlines[1].split(',')
            
            # Get values line by line
            for line in rawlines[2:]:
                line_vals = line.split(',')
                step_monitor_data.append(line_vals)

            # Extract lists for each key
            self.step_monitor_data = self.format_data(step_monitor_keys, step_monitor_data)
            
            # Get the x axis (just the step count)
            self.step_monitor_x = [i for i in range(len(step_monitor_data))]
            
        # Load the algorithm monitor data
        if self.alg_monitor_str:
            alg_monitor_data = []
            # Read all the lines at once quickly
            with open(os.path.join(self.logpath, self.alg_monitor_str), 'r') as f:
                rawlines = f.readlines()
            rawlines = [l.rstrip('\n') for l in rawlines]
   
            # Get the header with key names
            alg_monitor_keys = rawlines[1].split(',')
            
            # Get values line by line
            for line in rawlines[2:]:
                line_vals = line.split(',')
                alg_monitor_data.append(line_vals)
                
            # Extract lists for each key
            self.alg_monitor_data = self.format_data(alg_monitor_keys, alg_monitor_data)
            
            # Get the x axis (just the update count)
            self.alg_monitor_x = [i for i in range(len(alg_monitor_data))]
    
    # Load the raw plotting data from source
    def load_data(self, data_src, data_type, log_name):
        # Load the raw values 
        if data_src == 'episode_monitor':
            if not self.episode_monitor_str:
                raise Exception("Episode monitor data not loaded")
            # Get the data for this log name from the right data source (should be already loaded)
            monitor = self.episode_monitor_data
            raw_data_x = self.episode_monitor_x
        elif data_src == 'step_monitor':
            if not self.step_monitor_str:
                raise Exception("Step monitor data not loaded")
            monitor = self.step_monitor_data
            raw_data_x = self.step_monitor_x
        elif data_src == 'alg_monitor':
            if not self.alg_monitor_str:
                raise Exception("Alg monitor data not loaded")
            monitor = self.alg_monitor_data
            raw_data_x = self.alg_monitor_x
        else:
            raise NotImplementedError

        # If multiscalar, load from all the compnent keys
        if data_type in ['multiscalar', 'special']:
            raw_data_y = {}
            for key in log_name:
                #pdb.set_trace()
                if log_name[key] or type(log_name[key]) is str:
                    assert(key in monitor)
                if key in monitor:
                    raw_data_y[key] = monitor[key]
        # Else, just get from monitor
        else:
            raw_data_y = monitor[log_name]        

        return raw_data_x, raw_data_y

    # Plot a trace of an agents x y movement
    def display_movement(self, xypos, plot_struct, thetas=None, yaws=None):
        # Do matplot for xy movement
        xlabel_name = 'X'
        ylabel_name = 'Y'
        plt.figure()
        x = [dat[0] for dat in xypos]
        y = [dat[1] for dat in xypos]
        plt.scatter(x, y, marker='*')
        plt.xlabel(xlabel_name)
        plt.ylabel(ylabel_name)
        plt.title(self.unique_name)

        # If applicable, draw theta direction
        if thetas is not None:
            last_theta = None
            for t, theta in enumerate(thetas):
                if len(thetas) > 100 and t % 10 > 0:
                    continue
                if last_theta is None or np.linalg.norm(last_theta-theta) > 1e-6: 
                    if np.linalg.norm(theta) > 1e-3:
                        x_offset = x[t]
                        y_offset = y[t]                  
                        plt.arrow(x_offset, y_offset, theta[0], theta[1], head_width=0.05, head_length=0.1, fc='k', ec='k')
                last_theta = theta

        if yaws is not None:
            last_yaw = None
            for t, yaw in enumerate(yaws):
                if len(yaws) > 100 and t % 10 > 0:
                    continue
                if last_yaw is None or np.linalg.norm(last_yaw-yaw) > 1e-6:
                    if np.linalg.norm(yaw) > 1e-3:
                        x_offset = x[t]
                        y_offset = y[t]
                        plt.arrow(x_offset, y_offset, yaw[0], yaw[1], head_width=0.05, head_length=0.1, fc='g', ec='g')
                last_yaw = yaw

        # Update window
        # TODO - is there a draw over option maybe?
        if plot_struct['window_once']:
            win = self.viz.matplot(plt)
        elif keyname in self.wins:
            self.wins[keyname] = self.viz.matplot(plt, win=self.wins[keyname])
        else:
            self.wins[keyname] = self.viz.matplot(plt)
        plt.close()

    # Do simple x, y plot
    def simple_plot(self, x, y, keyname, plot_struct):
        # Check we've reached bin_size
        bin_size = plot_struct['bin_size']
        if len(x) < bin_size:
            return

        # Time subset (if applicable)
        if 'time_start' in plot_struct:
            start = plot_struct['time_start']
            end = plot_struct['time_end']
            x = x[start:end+1]
            y = y[start:end+1]

        # Do smoothing or any other work for x and y
        if plot_struct['smooth'] == 1:
            x, y = self.smooth_curve(x, y)
        elif plot_struct['smooth'] == 2:
            y = medfilt(y, kernel_size=9)
        if bin_size > 1:
            x, y = self.fix_point(x, y, bin_size)
       
        # Do matplot
        if plot_struct['data_src'] == 'episode_monitor':
            xlabel_name = 'Number of Timesteps'
        elif plot_struct['data_src'] == 'step_monitor':
            xlabel_name = 'Number of Steps'
        elif plot_struct['data_src'] == 'alg_monitor':
            xlabel_name = 'Number of Updates'
        else:
            raise NotImplementedError 
        plt.figure()
        plt.plot(x, y)
        plt.xlabel(xlabel_name)
        plt.ylabel(keyname)
        plt.title(self.unique_name)
        plt.grid(True)

        # Update window
        if keyname in self.wins:
            self.wins[keyname] = self.viz.matplot(plt, win=self.wins[keyname])
        else:
            self.wins[keyname] = self.viz.matplot(plt)
        plt.close()

    # Plot multiple y values
    def multi_plot(self, x, y_dict, keyname, plot_struct):
        # Check we've reached bin_size
        bin_size = plot_struct['bin_size']
        if len(x) < bin_size:
            return

        # Convert values to float
        for k in y_dict:
            y_dict[k] = [float(val.replace('\x00','')) for val in y_dict[k]]

        # Do smoothing or any other work for x and y
        orig_x = x
        if plot_struct['smooth'] == 1:
            for key in y_dict:
                y_orig = y_dict[key]
                x = np.copy(orig_x)
                x, y = self.smooth_curve(x, y_orig)
                y_dict[key] = y
        elif plot_struct['smooth'] == 2:
            for key in y_dict:
                y_dict[key] = medfilt(y_dict[key], kernel_size=9)
        orig_x = x
        for key in y_dict:
            x = np.copy(orig_x)
            y_orig = y_dict[key]
            x, y = self.fix_point(x, y_orig, bin_size)
            y_dict[key] = y

        # Do matplot
        if plot_struct['data_src'] == 'episode_monitor':
            xlabel_name = 'Number of Timesteps'
        elif plot_struct['data_src'] == 'step_monitor':
            xlabel_name = 'Number of Steps'
        elif plot_struct['data_src'] == 'alg_monitor':
            xlabel_name = 'Number of Updates'
        else:
            raise NotImplementedError 
        fig = plt.figure()

        # Put each y value on
        legend_handles = []
        legend_labels = []
        for key in y_dict:  # Kind of hack. First few values often plot garbage
            plt.plot(x, y_dict[key], label=key)
        plt.legend()
        plt.xlabel(xlabel_name)
        plt.ylabel(keyname)
        plt.title(self.unique_name)
        plt.grid(True)

        # Update window
        if keyname in self.wins:
            self.wins[keyname] = self.viz.matplot(plt, win=self.wins[keyname])
        else:
            self.wins[keyname] = self.viz.matplot(plt)
        plt.close()

    # Do simple value display
    def display_simple_value(self, value_str, keyname, plot_struct):
        display_text = self.unique_name + '\n' + keyname + ': ' + value_str
        if keyname in self.wins:
            self.wins[keyname] = self.viz.text(display_text, win=self.wins[keyname])
        else:
            self.wins[keyname] = self.viz.text(display_text)
 
    # Update visdom plot
    def update_display(self, raw_data_x, raw_data_y, keyname, plot_struct):
        # First split on data type
        # If scalar, pretty simple plotting
        data_type = plot_struct['data_type']
        # If scalar, call simple plot
        if data_type == 'scalar':
            raw_data_y = [float(y) for y in raw_data_y]
            self.simple_plot(raw_data_x, raw_data_y, keyname, plot_struct)
        # If multiscalar, do more complex plot with legend
        elif data_type == 'multiscalar':
            self.multi_plot(raw_data_x, raw_data_y, keyname, plot_struct)
        # If array, either do elementwise or get norm
        elif data_type == 'array':
            # Clean up and convert arrays
            conv_array = lambda s: np.fromstring(s.replace('[', '').replace(']', '').replace('"', ''), dtype=float, sep=' ') 
            raw_data_y = [conv_array(y) for y in raw_data_y]
            display_type = plot_struct['display_type']

            # If norm, take np norm and do simple plot
            if display_type == 'norm':
                raw_data_y = [np.linalg.norm(y) for y in raw_data_y]
                self.simple_plot(raw_data_x, raw_data_y, keyname, plot_struct)
            # If elementwise, plot for each element
            elif display_type == 'elementwise':
                for i in range(len(raw_data_y[0])):
                    new_key = keyname + '[%d]' % i
                    data_y = [y[i] for y in raw_data_y]
                    self.simple_plot(raw_data_x, data_y, new_key, plot_struct)
            # Same as above, but only show subset of indices
            elif display_type == 'elementwise_subset':
                # Display for [start_ind, end_ind)
                start_ind = plot_struct['start_ind']
                end_ind = plot_struct['end_ind']
                for i in range(start_ind, end_ind):
                    new_key = keyname + '[%d]' % i 
                    data_y = [y[i] for y in raw_data_y]
                    self.simple_plot(raw_data_x, data_y, new_key, plot_struct)
            else:
                raise NotImplementedError
        # If single value, display as text
        elif data_type == 'single_value':
            dat = raw_data_y[0]
            self.display_simple_value(dat, keyname, plot_struct)
        elif data_type == 'special':
            if keyname == 'theta_xy_plot':
                # Get xy and get theta 
                conv_array = lambda s: np.fromstring(s.replace('[', '').replace(']', '').replace('"', ''), dtype=float, sep=' ') 
                state_raw = [conv_array(y) for y in raw_data_y['state']]
                obs_raw = [conv_array(y) for y in raw_data_y['obs']]
                xypos = [y[:2] for y in state_raw]
                quats = [y[3:7] for y in state_raw]
                yaws = []
                for q in quats:
                    _, _, yaw = geom_utils.quaternion_to_euler_angle(q)
                    yaws.append(yaw)
                if 'theta_sz' in self.opt['env']:
                    theta_sz = self.opt['env']['theta_sz']
                    if self.opt['env']['add_timestep']:
                        start_ind = -(theta_sz+1)
                        end_ind = -2
                    else:
                        start_ind = -theta_sz
                        end_ind = -1
                    theta_space_mode = self.opt['env']['theta_space_mode']
                    yaws_2d = None
                    if theta_space_mode in ['arbitrary', 'arbitrary_stop']:
                        thetas = [np.array([y[start_ind], y[end_ind]]) for y in obs_raw]
                        assert(abs(np.linalg.norm(thetas[0]) - 1) < 1e-6)
                    elif theta_space_mode in ['simple_four']:
                        thetas = []
                        yaws_2d = []
                        for obs, yaw in zip(obs_raw, yaws):
                            if obs[start_ind] == 1:
                                theta = np.array([1, 0])
                            elif obs[start_ind+1] == 1:
                                theta = np.array([-1, 0])
                            elif obs[start_ind+2] == 1:
                                theta = np.array([0, 1])
                            elif obs[start_ind+3] == 1:
                                theta = np.array([0, -1])
                            else:
                                raise Exception("Something is wrong")
                            # Turn with yaw
                            #pdb.set_trace()
                            theta = geom_utils.convert_vector_to_egocentric(-yaw, theta)
                            thetas.append(theta)
                            yaws_2d.append(np.array([math.cos(yaw), math.sin(yaw)]))
                    elif theta_space_mode in ['simple_eight']:
                        thetas = []
                        yaws_2d = []
                        for obs, yaw in zip(obs_raw, yaws):
                            if obs[start_ind] == 1:
                                theta = np.array([1, 0])
                            elif obs[start_ind+1] == 1:
                                theta = np.array([-1, 0])
                            elif obs[start_ind+2] == 1:
                                theta = np.array([0, 1])
                            elif obs[start_ind+3] == 1:
                                theta = np.array([0, -1])
                            elif obs[start_ind+4] == 1:
                                theta = np.array([math.sqrt(0.5), math.sqrt(0.5)])
                            elif obs[start_ind+5] == 1:
                                theta = np.array([-math.sqrt(0.5), math.sqrt(0.5)])
                            elif obs[start_ind+6] == 1:
                                theta = np.array([-math.sqrt(0.5), -math.sqrt(0.5)])
                            elif obs[start_ind+7] == 1:
                                theta = np.array([math.sqrt(0.5), -math.sqrt(0.5)])
                            else:
                                raise Exception("Something is wrong")
                            # Turn with yaw
                            #pdb.set_trace()
                            theta = geom_utils.convert_vector_to_egocentric(-yaw, theta)
                            thetas.append(theta)
                        yaws_2d = None
                    elif theta_space_mode in ['k_theta']:
                        yaws_2d = []
                        for yaw in yaws:
                            yaws_2d.append(np.array([math.cos(yaw), math.sin(yaw)])) 
                        thetas = None
                    else:
                        thetas = None
                else:
                    thetas = None
                    yaws_2d = None
                #pdb.set_trace()
                self.display_movement(xypos, plot_struct, thetas, yaws_2d)
        else:
            raise NotImplementedError

    # Plot all values in visdom
    def visdom_plot(self):
        # Preload the monitor data
        self.preload_data() 
        
        # For each plot key
        for key in self.plot_keys:
            # Get values
            plot_struct = self.plot_keys[key]
            data_src = plot_struct['data_src']
            data_type = plot_struct['data_type']
            log_name = plot_struct['log_name']       

            # Check if we want to wait until a certain delay
            if 'update_delay' in plot_struct:                
                update_delay = plot_struct['update_delay']
                # Skip this if it hasn't been long enough since last update
                if key in self.time_since_update and time.time() - self.time_since_update[key] < update_delay:
                    continue
                else:
                    # Update time delay value
                    self.time_since_update[key] = time.time()
                            
            # Get the data from the correct source
            data_x, data_y = self.load_data(data_src, data_type, log_name)
            
            # Display the data
            self.update_display(data_x, data_y, key, plot_struct)
Example #25
0
class GPU_logger():
    def __init__(self,
                 host_names,
                 time_interval,
                 time_range,
                 show_summary=True,
                 show_monitor=True,
                 show_cpu_mem=False,
                 port=8098,
                 env_name='main',
                 verbose=False):
        self.viz = Visdom(port=port)
        self.env = env_name
        self.hosts = host_names
        self.sleep = time_interval
        self.range = time_range
        self.show_summary = show_summary
        self.show_monitor = show_monitor
        self.show_cpu_mem = show_cpu_mem
        self.verbose = verbose
        self.win = {}
        self.num_gpus = 4
        self.max_length = self.range // self.sleep
        self.tick_ds = self.max_length // 6
        self.time_indices = deque()
        self.memory_queue = np.zeros([len(host_names), self.num_gpus, 0])
        self.usages_queue = np.zeros([len(host_names), self.num_gpus, 0])
        self.table_W = 780
        self.table_H = 470
        self.restore(PWD)

    def reset(self):
        self.viz.close(win=None, env=self.env)

    def save(self, output_dir):
        if not isdir(output_dir):
            os.makedirs(output_dir)

        with open(join(output_dir, 'cached_data'), 'wb') as f:
            pickle.dump(
                {
                    'time': self.time_indices,
                    'memory': self.memory_queue,
                    'usages': self.usages_queue
                }, f, pickle.HIGHEST_PROTOCOL)

    def restore(self, output_dir):
        if isfile(join(output_dir, 'cached_data')):
            with open(join(output_dir, 'cached_data'), 'rb') as f:
                cache = pickle.load(f)
                self.time_indices = cache['time']
                self.memory_queue = cache['memory']
                self.usages_queue = cache['usages']

    def record(self):
        if self.show_summary:
            gpu_tabel_opts = {
                'title': 'README',
                'resize': False,
                'width': self.table_W,
                'height': self.table_H
            }
            col_W = self.table_W / len(self.hosts)
            self.win['summary'] = self.viz.text(README,
                                                env=self.env,
                                                opts=gpu_tabel_opts)
        if self.show_cpu_mem:
            cpu_tabel_opts = {
                'title': 'CPU Memory',
                'resize': True,
                'width': self.table_W,
                'height': 150
            }
            col_W_c = self.table_W / len(self.hosts)
            self.win['cpu_mem'] = self.viz.text('',
                                                env=self.env,
                                                opts=cpu_tabel_opts)

        while True:
            if len(self.time_indices) >= self.max_length:
                self.time_indices.popleft()
            self.time_indices.append(time.strftime("%H:%M"))

            if isfile(join(HOME, '.tcshrc')):  # hotfix to handle my zsh
                os.rename(join(HOME, '.tcshrc'), join(HOME, '.tcshrc.bak'))

            #gpu_memory = ssh_host(self.hosts, mode='memory')
            #gpu_usage  = ssh_host(self.hosts, mode='gpu')
            gpu_memory, gpu_usage, cpu_memory = ssh_host(self.hosts,
                                                         mode='all')

            if self.verbose:
                print('gpu memory:', gpu_memory)
                print('gpu usage:', gpu_usage)
                print('cpu memory:', cpu_memory)

            if isfile(join(HOME, '.tcshrc.bak')):
                os.rename(join(HOME, '.tcshrc.bak'), join(HOME, '.tcshrc'))

            if self.memory_queue.shape[-1] < self.max_length:
                self.memory_queue = np.append(
                    self.memory_queue,
                    np.reshape(gpu_memory,
                               [len(self.hosts), self.num_gpus, 1]), -1)
                self.usages_queue = np.append(
                    self.usages_queue,
                    np.reshape(gpu_usage, [len(self.hosts), self.num_gpus, 1]),
                    -1)
            else:
                self.memory_queue = np.append(
                    np.delete(self.memory_queue, 0, -1),
                    np.reshape(gpu_memory,
                               [len(self.hosts), self.num_gpus, 1]), -1)
                self.usages_queue = np.append(
                    np.delete(self.usages_queue, 0, -1),
                    np.reshape(gpu_usage, [len(self.hosts), self.num_gpus, 1]),
                    -1)
            gpu_status_table, cpu_status_table = [], []
            gpu_status_table.append(
                get_tablerow('', ['GPU:01', 'GPU:02', 'GPU:03', 'GPU:04']))
            cpu_status_table.append(
                get_tablerow('', ['Total:', 'Used:', 'Available:']))
            for k, host in enumerate(self.hosts):
                fig = plt.figure(figsize=(20, 2.7))
                fig.suptitle(host, size=24, fontweight="bold", y=1.)
                gpu_status_row = []
                for idx in range(4):
                    memory_, usage_ = self.memory_queue[
                        k, idx, :], self.usages_queue[k, idx, :]
                    nonzero_idx = np.where(memory_ > 1)[0]
                    m_memory_ = np.mean(memory_[nonzero_idx[0]:]) if len(
                        nonzero_idx) > 0 else np.mean(memory_)
                    m_usage_ = np.mean(usage_[nonzero_idx]) if len(
                        nonzero_idx) > 0 else np.mean(usage_)
                    if m_memory_ == -1:  # I've set empty gpu to -1
                        gpu_status_row.append(' ')
                        continue

                    if np.any(
                            memory_[-5:] > 1
                    ):  #if last 5 checkpoint is empty -> not used now
                        status = get_gpu_health(m_memory_, m_usage_)
                    else:
                        status = status_kw[0]

                    box_prop = dict(facecolor=color_map[status],
                                    edgecolor='none',
                                    pad=2,
                                    alpha=0.6)
                    gpu_status_row.append(status)

                    plt.subplot(1, 4, idx + 1)
                    plt.xticks(
                        np.arange(0, len(self.time_indices), self.tick_ds))
                    plt.fill_between(list(self.time_indices),
                                     memory_,
                                     color='r',
                                     label="Memory",
                                     alpha=0.4)
                    plt.fill_between(list(self.time_indices),
                                     usage_,
                                     color='g',
                                     label="Usages",
                                     alpha=0.4)
                    plt.ylim(-1, 101)
                    plt.title('GPU-{:02d}'.format(idx), size=15, bbox=box_prop)
                    plt.xlabel('Average Memory: {:.1f}% Average Usage: {:.1f}%'\
                                .format(m_memory_, m_usage_), size=14)
                    plt.tight_layout()
                    plt.legend()

                gpu_status_table.append(
                    get_tablerow(host.replace('emon', '-'),
                                 gpu_status_row))  #shorten hostname
                cpu_status_table.append(
                    get_tablerow(host.replace('emon', '-'), [
                        str(cpu_memory[k][0]) + 'G',
                        str(cpu_memory[k][1]) + 'G',
                        str(cpu_memory[k][-1]) + 'G'
                    ]))
                if self.show_monitor:
                    opts = {
                        'title': host,
                        'resizable': True,
                        'height': 190,
                        'width': 1400
                    }
                    if host not in self.win.keys():
                        self.win[host] = self.viz.matplot(plt,
                                                          env=self.env,
                                                          opts=opts)
                    else:
                        self.viz.matplot(plt,
                                         win=self.win[host],
                                         env=self.env,
                                         opts=opts)
                plt.close()

            TIMESTAMP = f"Updated at {time.strftime('%Y/%m/%d-%H:%M:%S')}<br>"
            if 'summary' in self.win.keys():
                status_table_g = [list(t) for t in zip(*gpu_status_table)]
                table_ = HTML.table(status_table_g,
                                    border='3',
                                    cellpadding=5,
                                    col_width=[col_W] * (len(self.hosts) + 1),
                                    col_align=['center'] *
                                    (len(self.hosts) + 1))

                self.viz.text(README + table_ + TIMESTAMP + ACKNOWLEDGE,
                              env=self.env,
                              win=self.win['summary'],
                              opts=gpu_tabel_opts)
            if 'cpu_mem' in self.win.keys():
                status_table_c = [list(t) for t in zip(*cpu_status_table)]
                table_c = HTML.table(
                    status_table_c,
                    border='2',
                    cellpadding=2,
                    col_width=[col_W_c] * (len(self.hosts) + 1),
                    col_align=['center'] * (len(self.hosts) + 1))
                self.viz.text(table_c + TIMESTAMP,
                              env=self.env,
                              win=self.win['cpu_mem'],
                              opts=cpu_tabel_opts)
            time.sleep(self.sleep)
Example #26
0
class sac_ES:
    def __init__(self,
                 env_fn,
                 seed=0,
                 steps_per_epoch=5000,
                 epochs=100,
                 gamma=0.7,
                 polyak=0.995,
                 lr=3e-4,
                 alpha=0.2,
                 batch_size=1024,
                 start_steps=1,
                 max_ep_len=1000,
                 memory_size=int(1e+4),
                 max_kl=0.01,
                 experiment_name='none',
                 use_adv=False,
                 use_half_dete=False,
                 use_sample_dist=False,
                 Test_agent=False):

        tf.set_random_seed(seed)
        np.random.seed(seed)
        self._success_obs_item = len(os.listdir('result/success_obs/'))
        self.env = env_fn()
        self.ac_per_state = 30
        self.memory_size = memory_size
        self.batch_size = batch_size
        self.gamma = gamma
        self.alpha = alpha
        self.lr = lr
        self.polyak = polyak
        self.start_steps = start_steps
        self.max_ep_len = max_ep_len
        self.epochs = epochs
        self.steps_per_epoch = steps_per_epoch
        self.max_kl = max_kl
        self.use_adv = use_adv
        self.entropy_too_low = False
        self.win = None
        if Test_agent:
            from visdom import Visdom
            self.viz = Visdom()
            assert self.viz.check_connection()
            self.win = self.viz.matplot(plt)

        self.act_dim = self.env.action_space.shape[0]
        self.replay_buffer = ReplayBuffer(act_dim=self.act_dim,
                                          size=self.memory_size)
        self.use_half_dete = use_half_dete
        self.use_sample_dist = use_sample_dist

        # Build computer graph
        self.graph = tf.Graph()

        with tf.device('/gpu:0'), self.graph.as_default():
            self.sess = tf.Session(config=config, graph=self.graph)

            self.__make_model()

            # Summary
            self.experiment_name = experiment_name
            self.writer = tf.compat.v1.summary.FileWriter("logs/" +
                                                          self.experiment_name)
            self.ep_ret_ph = tf.placeholder(tf.float32,
                                            shape=(),
                                            name="ep_ret_ph")
            self.ep_len_ph = tf.placeholder(tf.float32,
                                            shape=(),
                                            name="ep_len_ph")
            self.test_summary = tf.compat.v1.summary.merge([
                tf.compat.v1.summary.scalar('EP_ret',
                                            self.ep_ret_ph,
                                            family='test'),
                tf.compat.v1.summary.scalar('EP_len',
                                            self.ep_len_ph,
                                            family='test')
            ])
            self.sess.run(tf.global_variables_initializer())
            self.sess.run(self.V_target_init)

            _ = os.system("clear")
            Count_Variables()
            print('Trainable_variables:')
            for v in tf.compat.v1.trainable_variables():
                print('{}\t  {}'.format(v.name, str(v.shape)))

    def __make_model(self):
        self.x1_ph = tf.placeholder(tf.float32, [None, 128, 128, 3],
                                    name='o1_ph')

        self.x2_ph = tf.placeholder(tf.float32, [None, 128, 128, 3],
                                    name='o2_ph')

        self.a_ph, self.r_ph, self.d_ph, self.q_ij_ph = core.placeholders(
            self.act_dim, None, None, self.ac_per_state)

        with tf.variable_scope('main'):
            self.dist, self.sample_dist, self.mean, self.pi, self.q1, self.q2, self.q1_pi, self.q2_pi, self.v, self.std = core.actor_critic(
                self.x1_ph,
                self.a_ph,
                action_space=self.env.action_space,
                hidden_size=(64, 32))

        vf_mlp = lambda x_: tf.squeeze(
            core.mlp(core._cnn(x_), [64, 32, 1], tf.nn.relu, None), axis=1)

        with tf.variable_scope('target'):
            self.v_target = vf_mlp(self.x2_ph)

        # Policy Evaluate
        with tf.variable_scope('Policy_Evaluate'):
            # Min Double-Q:
            min_q_pi = tf.minimum(self.q1_pi, self.q2_pi)

            # Targets for Q and V regression
            q_target = tf.stop_gradient(self.r_ph + self.gamma *
                                        (1 - self.d_ph) * self.v_target)
            logp_pi = tf.reduce_sum(self.dist.log_prob(self.pi),
                                    axis=-1,
                                    keep_dims=True)  # sum the each dim_of_A
            v_target = tf.stop_gradient(min_q_pi - self.alpha * logp_pi)

            q1_loss = 0.5 * tf.reduce_mean((q_target - self.q1)**2)
            q2_loss = 0.5 * tf.reduce_mean((q_target - self.q2)**2)
            v_loss = 0.5 * tf.reduce_mean((v_target - self.v)**2)
            value_loss = q1_loss + q2_loss + v_loss
        # set_target_update
        self.V_target_init = tf.group([
            tf.assign(v_targ, v_main)
            for v_main, v_targ in zip(get_vars('main/v'), get_vars('target'))
        ])

        # Policy Improvement
        with tf.variable_scope('Policy_Improvement'):
            # (samples_per_state, batch, self.act_dim) --> (-1, self.act_dim)
            self.a_flatten_s_dist = self._samp_a(self.sample_dist)
            self.a_flatten_dist = self._samp_a(self.dist)

            if self.use_adv:
                q = self.q1 - self.v_target
            else:
                q = self.q1

            q_ij = tf.stop_gradient(
                tf.nn.softmax(tf.reshape(q, [self.ac_per_state, -1]), axis=0))

            logp = tf.reduce_sum(self.dist.log_prob(self.a_ph),
                                 axis=-1,
                                 keep_dims=True)  # sum the each dim_of_A
            logp_ij = tf.reshape(logp, [self.ac_per_state, -1])

            self.entropy = tf.reduce_mean(self.dist.entropy())

            likelihood_term = q_ij * logp_ij

        # Value train op
        value_optimizer = tf.compat.v1.train.AdamOptimizer(
            learning_rate=self.lr)
        value_params = get_vars('main/q') + get_vars('main/v')

        self.train_value_op = value_optimizer.minimize(value_loss,
                                                       var_list=value_params)

        # value_learn -> update_target_value
        self.soft_update = tf.group([
            tf.assign(v_targ,
                      self.polyak * v_targ + (1 - self.polyak) * v_main)
            for v_main, v_targ in zip(get_vars('main/v'), get_vars('target'))
        ])

        # Maximum likelihood
        pi_loss = -tf.reduce_sum(likelihood_term)
        pi_optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=self.lr)
        self.train_pi_op = pi_optimizer.minimize(pi_loss,
                                                 var_list=get_vars('main/pi'))

        var_list = tf.global_variables()
        self.saver = tf.compat.v1.train.Saver(var_list=var_list, max_to_keep=1)

        assert tf.get_collection(tf.GraphKeys.UPDATE_OPS) == [
        ]  # 确保没有 batch norml

        # summary
        self.a_summary = tf.compat.v1.summary.merge(
            [tf.compat.v1.summary.scalar('pi_loss', pi_loss, family='actor')])
        self.c_summary = tf.compat.v1.summary.merge([
            tf.compat.v1.summary.scalar('value_loss',
                                        value_loss,
                                        family='critic'),
            tf.compat.v1.summary.scalar('q1_loss', q1_loss, family='critic'),
            tf.compat.v1.summary.scalar('q2_loss', q2_loss, family='critic'),
            tf.compat.v1.summary.scalar('v_loss', v_loss, family='critic'),
            tf.compat.v1.summary.scalar('pi_entropy',
                                        self.entropy,
                                        family='actor')
        ])

    def _samp_a(self, dist):
        # (samples_per_state, batch, self.act_dim) --> (-1, self.act_dim)
        a = tf.reshape(dist.sample(self.ac_per_state), [-1, self.act_dim]),
        return tf.clip_by_value(a, self.env.action_space.low[0],
                                self.env.action_space.high[0])

    def Save(self):
        path = "model/" + self.experiment_name + "/model.ckpt"
        # print("Save model to the: '{}'".format(path))
        self.saver.save(self.sess, save_path=path)

    def load(self):
        path = "model/" + self.experiment_name + "/model.ckpt"
        print("\nLoad model From the: '{}'\n".format(path))
        self.saver.restore(self.sess, save_path=path)

    def _choose_action(self, o, deterministic=False):
        act_op = self.mean if deterministic else self.pi
        return self.sess.run(act_op, feed_dict={self.x1_ph:
                                                o[np.newaxis, ]})[0]

    def learn(self):
        batch = self.replay_buffer.sample_batch(self.batch_size)

        a_flatten_opt = self.a_flatten_s_dist  #if self.entropy_too_low else self.a_flatten_dist

        opt_step_1 = [self.train_value_op, a_flatten_opt, self.c_summary]
        feed_step_1 = {
            self.x1_ph: batch['obs1'],
            self.x2_ph: batch['obs2'],
            self.a_ph: batch['acts'],
            self.r_ph: batch['rews'],
            self.d_ph: batch['done']
        }
        _, a_Flatten, c_s = self.sess.run(opt_step_1, feed_step_1)
        self.writer.add_summary(c_s)

        feed_step_2 = {
            self.a_ph: np.squeeze(a_Flatten),
            self.x1_ph: np.tile(batch['obs1'], [self.ac_per_state, 1, 1, 1]),
            self.x2_ph: np.tile(batch['obs2'], [self.ac_per_state, 1, 1, 1])
        }
        opt_step_2 = [self.entropy, self.train_pi_op, self.a_summary]

        # update policy by max_likelihood
        entropy, _, a_s = self.sess.run(opt_step_2, feed_step_2)

        self.entropy_too_low = True if entropy < -0.5 else False

        self.writer.add_summary(a_s)

        # update target_net and old pi
        self.sess.run(self.soft_update)

    def test_agent(self, deterministic=False, n=1):
        ep_r = []
        ep_l = []
        plt.figure(figsize=(9, 5))
        for j in range(n):
            print('------------------Epoch: %d-------------------' % j)

            o, r, d, ep_ret, ep_len, level_plot = self.env.reset(
            ), 0, False, 0, 0, []

            while not (d or (ep_len == self.max_ep_len)):

                # Take deterministic actions at test time
                a = self._choose_action(o, deterministic=deterministic)
                o, r, d, info = self.env.step(a, _step=ep_len)
                print("step: {} a: {}  r: {}".format(ep_len,
                                                     np.around(a, decimals=2),
                                                     r))
                ep_ret += r
                ep_len += 1
                level_plot.append(self.env._before)
                self.win = R_plot(level_plot, self.viz, self.win)

            print('EP_ret: %d, \t EP_Len: %d' % (ep_ret, ep_len))

            if info['is_success']:
                print('success!')
            ep_r.append(ep_ret), ep_l.append(ep_len)
        plt.close()

    def rollout(self):
        total_steps = self.steps_per_epoch * self.epochs
        o, r, d, ep_ret, ep_len = self.env.reset(), 0, False, 0, 0

        deterministic = False
        log_t = []
        log_episode_ret = []

        for t in tqdm(range(total_steps)):

            if t > self.start_steps:
                if self.use_half_dete:
                    deterministic = True if np.random.randn() > 0 else False
                a = self._choose_action(o, deterministic)
            else:
                a = self.env.action_space.sample()

            o2, r, d, info = self.env.step(a)
            ep_ret += r
            ep_len += 1

            if info['is_success']:
                im = Image.fromarray(o2)
                im.save('result/success_obs/' + str(self._success_obs_item) +
                        '.png')
                self._success_obs_item += 1

            d = False if ep_len == self.max_ep_len else d

            self.replay_buffer.store(o, a, r, o2, d)

            o = o2

            if d or (ep_len == self.max_ep_len):

                if (t + 1) >= 500:
                    [self.learn() for _ in range(ep_len)]  # todo::

                ep_s = self.sess.run(self.test_summary, {
                    self.ep_ret_ph: ep_ret,
                    self.ep_len_ph: ep_len
                })
                self.writer.add_summary(ep_s, global_step=t)
                log_episode_ret.append(ep_ret)
                log_t.append(t)
                print("episode return: ", ep_ret)
                o, r, d, ep_ret, ep_len = self.env.reset(), 0, False, 0, 0

            if (t + 1) % 100 == 0:
                self.Save()

        np.save('result/' + self.experiment_name + '_reward.npy',
                (log_t, log_episode_ret),
                allow_pickle=True)
Example #27
0
if use_vis:
    viz = Visdom()
    assert viz.check_connection()
    initial_c = torch.rand(batch_size, 3, img_size, img_size)
    initial_s = torch.rand(batch_size, 1, img_size, img_size)
    temp = np.linspace(1, 5, 5)
    plt.plot(temp)

    result_c = viz.images(initial_c,
                          nrow=batch_size // 2,
                          opts=dict(caption="cover vs. stego"))
    result_s = viz.images(initial_s,
                          nrow=batch_size // 2,
                          opts=dict(caption="secret vs. reveal"))

    lc_ssim = viz.matplot(plt)
    ls_ssim = viz.matplot(plt)
    l_dis = viz.matplot(plt)
    l_net = viz.matplot(plt)

# loss hyper
# L(c, c0) = α (1-SSIM(c, c0)) +(1-α)(1 MSSIM(c,c0))+ β MSE(c,c0)
# L(s, s0) = α (1-SSIM(s, s0)) +(1-α)(1 MSSIM(s,s0))+ β MSE(s,s0)
# L(c, c0, s, s0) = L(c, c0) + λ L(s, s0)

alpha = 0.7
beta = 0.5
gamma = 0.85
lambda0 = 0.002
# optimizer hyper for adam
Example #28
0
def visom_show(plt=None):
    viz = Visdom(env='main')
    viz.matplot(plt)
Example #29
0
            elif event['key'] == 'Backspace':
                curr_txt = curr_txt[:-1]
            elif event['key'] == 'Delete':
                curr_txt = txt
            elif len(event['key']) == 1:
                curr_txt += event['key']
            viz.text(curr_txt, win=callback_text_window)

    viz.register_event_handler(type_callback, callback_text_window)

    # matplotlib demo:
    try:
        import matplotlib.pyplot as plt
        plt.plot([1, 23, 2, 4])
        plt.ylabel('some numbers')
        viz.matplot(plt)
    except BaseException as err:
        print('Skipped matplotlib example')
        print('Error message: ', err)

    # 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
        video_url = 'http://media.w3.org/2010/05/sintel/trailer.ogv'
        # linux