File size: 1,177 Bytes
3dcfb26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-

"""
Misc download and visualization helper functions and class wrappers.
"""

import sys
import time
import torch
from visdom import Visdom


def reporthook(count, block_size, total_size):
    global start_time
    if count == 0:
        start_time = time.time()
        return
    duration = time.time() - start_time
    progress_size = int(count * block_size)
    speed = int(progress_size / (1024 * duration))
    percent = min(int(count * block_size * 100 / total_size), 100)
    sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" %
                     (percent, progress_size / (1024 * 1024), speed, duration))
    sys.stdout.flush()


class VisdomWrapper(Visdom):
    def __init__(self, *args, env=None, **kwargs):
        Visdom.__init__(self, *args, **kwargs)
        self.env = env
        self.plots = {}

    def init_line_plot(self, name,
                       X=torch.zeros((1,)).cpu(),
                       Y=torch.zeros((1,)).cpu(), **opts):
        self.plots[name] = self.line(X=X, Y=Y, env=self.env, opts=opts)

    def plot_line(self, name, **kwargs):
        self.line(win=self.plots[name], env=self.env, **kwargs)