class VisdomScatter(object): """Scatter to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom(use_incoming_socket=False) self.env = env_name self.scatters = {} def scatter(self, X, Y, title, legend, markersize=10): if title not in self.scatters: self.scatters[title] = self.viz.scatter( X=X, Y=Y, env=self.env, opts=dict( #legend=legend, markersize=markersize, title=title)) else: self.viz.scatter( X=X, Y=Y, env=self.env, win=self.scatters[title], opts=dict( #legend=legend, markersize=markersize, title=title))
class VisualizeLatent(object): def __init__(self, data_loader, method='tsne'): self.data_loader = data_loader self.labels = [] for _, label in self.data_loader: self.labels.append(label) self.labels = torch.cat(self.labels, 0).cpu().numpy() + 1 self.viz = Visdom() self.win = None if method == 'tsne': self.model = TSNE elif method == 'pca': self.model = PCA else: raise ValueError('Unknown embedding method') def __call__(self, trainer, gan_model): latent_list = [] for data, _ in self.data_loader: latent = gan_model.inference(data.type(FloatTensor)) latent_list.append(latent) latent = torch.cat(latent_list, 0).detach().cpu().numpy() if latent.shape[1] > 2: latent = self.model(n_components=2).fit_transform(latent) if self.win is None: self.win = self.viz.scatter(latent, self.labels, opts=dict(markersize=5)) else: self.viz.scatter(latent, self.labels, win=self.win, opts=dict(markersize=5))
def VisdomDrawScatters(points_list, legends=None, title=None): # 画散点图 # legends必须显式给定,即这样调用:mu.VisdomDrawLines(train_acc, test_acc, legends=['train', 'test']) ''' :param points_list: 点列表,例如[3,5,3,4,3,5,7,7,4,3,2,4,6,8,6,3,2,3,6,7,8,5] :param legends: :param title: :return: ''' assert isinstance(points_list, list) and (isinstance( points_list[0], int) or isinstance(points_list[0], float)) index = np.arange(len(points_list)).reshape(-1, 1) X = np.column_stack((index, points_list)) if title == None: title = datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S') if legends == None: opts = dict(title=title, markersize=3) else: if isinstance(legends, str): # 目前一次只能画一组散点,所以现在图例也没什么卵用 legends = [legends] opts = dict(legend=legends, title=title, markersize=3) from visdom import Visdom viz = Visdom() viz.scatter(X=X, opts=opts)
def Visualization(result, labels): vis = Visdom() vis.scatter( X=result, Y=labels + 1, # 将label的最小值从0变为1,显示时label不可为0 opts=dict(markersize=5, title='Dimension reduction to %dD' % (result.shape[1])), )
def draw(items, labels): length = len(items) viz = Visdom() assert viz.check_connection() Y = labels freq = items[:, 4] norm_F = items[:, 5] freq = freq.reshape((length, 1)) norm_F = norm_F.reshape((length, 1)) print('[INFO] draw : frew.shape', freq.shape) print('[INFO] draw : norm_F.shape', norm_F.shape) X = np.concatenate([freq, norm_F], 1) print('[INFO] draw : X.shape', X.shape) # scatter = viz.scatter( # X=np.random.rand(100, 2), # Y=(Y[Y > 0] + 1.5).astype(int), # opts=dict( # legend=['Apples', 'Pears'], # xtickmin=0, # xtickmax=1, # xtickstep=0.5, # ytickmin=0, # ytickmax=1, # ytickstep=0.5, # markersymbol='cross-thin-open', # ), # ) # viz.scatter( # X=np.random.rand(255, 2), # #随机指定1或者2 # Y=(np.random.rand(255) + 1.5).astype(int), # opts=dict( # markersize=10, # ## 分配两种颜色 # markercolor=np.random.randint(0, 255, (2, 3,)), # ), # ) #3D 散点图 viz.scatter( X=X, Y=Y, opts=dict( legend=['NEG', 'POS'], markersize=5, ) )
class VisdomLinePlotter(VisdomPlotterInterface): """Plots to Visdom""" def __init__(self, env_name='main', port=8097, server="localhost"): self.viz = Visdom(port=port, server=server) self.env = env_name self.plotted_windows = {} self.traces = {} def plot(self, x_label, y_label, trace_name, title_name, x, y, reset=False): win_id = y_label if win_id not in self.traces: self.traces[win_id] = [] if trace_name not in self.traces[win_id]: self.traces[win_id].append(trace_name) params = dict( X=x, Y=y, env=self.env, name=trace_name, ) extra = dict(opts=dict( legend=self.traces[win_id], title=title_name, xlabel=x_label, ylabel=y_label, )) if win_id in self.plotted_windows: # just at the first time _extra = dict(win=self.plotted_windows[y_label], ) if not reset: _extra.update(dict(update='append')) extra.update(_extra) params.update(extra) self.plotted_windows[win_id] = self.viz.line(**params) def scatter(self, x, y, y_label, trace_name, color=(0, 0, 0)): color = numpy.array(color).reshape(-1, 3) win = self.plotted_windows[y_label] self.viz.scatter(X=x, Y=y, opts=dict( markersize=10, markercolor=color, ), name=trace_name, update='append', win=win)
def test_circle(): viz = Visdom(port=8000, env='circle') for i in range(10): task = circle_task(n_way=3, n_support=5, n_query=7) viz.scatter(X=task['x_train'], Y=np.argmax(task['y_train'], axis=1) + 1, opts=dict(title=f'task {i}: train')) viz.scatter(X=task['x_test'], Y=np.argmax(task['y_test'], axis=1) + 1, opts=dict(title=f'task {i}: test')) viz.save(viz.get_env_list())
class VisdomScatterPlotter(object): """Plots to Visdom""" def __init__(self, env_name='main', port=8097): self.viz = Visdom(port=port) self.env = env_name def plot(self, title_name, x, y, legends=None): self.viz.scatter(X=x, Y=y, env=self.env, opts=dict(title=title_name, legends=[legends], markersymbol='dot'))
def test_vis_test_get_transmission(self): self.prepareCredients() trans = self.get_transmission( self.refine_main_meter_labels_idx2_ps( self.dlsm.main_meter_labels_idx2_ps)) embedding = torch.nn.Embedding(len(self.dlsm.idx2stateidx) + 1, 10) embedding.load_state_dict(torch.load('s2v_embedding_states.pth')) points_high = np.array([ embedding(torch.LongTensor([i])).reshape(-1).detach().numpy() for i in list(self.dlsm.idx2stateidx.keys()) ]) embedded_2 = TSNE(n_components=3).fit_transform(points_high) vis = Visdom() vis.scatter(X=embedded_2)
class Plot(object): def __init__(self, title, port=8097): self.viz = Visdom(port=port) self.windows = {} self.title = title def register_scatterplot(self, name, xlabel, ylabel): win = self.viz.scatter( X=numpy.zeros((1, 2)), opts=dict(title=self.title, markersize=5, xlabel=xlabel, ylabel=ylabel) ) self.windows[name] = win def update_scatterplot(self, name, x, y): self.viz.line( X=numpy.array([x]), Y=numpy.array([y]), win=self.windows[name], update = 'append' ) def register_line(self, name, xlabel, ylabel): win = self.viz.line( Y=(numpy.linspace(0, 1, 100000)), opts=dict(title=self.title, markersize=5, xlabel=xlabel, ylabel=ylabel) ) self.windows[name] = win def update_line(self, name, x, y): self.viz.line( X=numpy.array([x]), Y=numpy.array([y]), win=self.windows[name], update = 'append' )
class VisdomLogger(): """ Logger that uses visdom to create learning curves Parameters ---------- - env: str, name of the visdom environment - log_checkpoints: bool, whether to use checkpoints or epoch averages for training loss - legend: tuple, names of the different losses that will be plotted. """ def __init__(self, server='http://localhost', port=8097): if Visdom is None: warnings.warn("Couldn't import visdom: `pip install visdom`") else: self.viz = Visdom(server=server, port=port) # self.viz.delete_env() def deleteWindow(self, win): self.viz.close(win=win) def appendLine(self, name, win, X, Y, xlabel='empty', ylabel='empty'): if xlabel == 'empty' or ylabel == 'empty': self.viz.line(X=X, Y=Y, win=win, name=name, update='append', opts=dict(title="Loss")) else: self.viz.line(X=X, Y=Y, win=win, name=name, update='append', opts=dict(title="Loss", xlabel=xlabel, ylabel=ylabel, showlegend=True)) def plotLine(self, name, win, X, Y): self.viz.line(X=X, Y=Y, win=win, name=name) def plotImage(self, image, win, title="Image", caption="Just a Image"): self.viz.image(image, win=win, opts=dict(title=title, caption=caption)) def plotImages(self, images, win, nrow, caption="Validation Output"): self.viz.images(images, win=win, nrow=nrow, opts=dict(caption=caption)) def plot3dScatter(self, point, win): print("Point is", point) self.viz.scatter(X = point, win=win, opts=dict(update='update'))
class Visualizer: def __init__(self, env="main"): self._viz = Visdom(env=env, use_incoming_socket=False) self._viz.close(env=env) def plot_line(self, values, steps, name, legend=None): if legend is None: opts = dict(title=name) else: opts = dict(title=name, legend=legend) self._viz.line(X=numpy.column_stack(steps), Y=numpy.column_stack(values), win=name, update='append', opts=opts) def plot_text(self, text, title, pre=True): _width = max([len(x) for x in text.split("\n")]) * 10 _heigth = len(text.split("\n")) * 20 _heigth = max(_heigth, 120) if pre: text = "<pre>{}</pre>".format(text) self._viz.text(text, win=title, opts=dict(title=title, width=min(_width, 400), height=min(_heigth, 400))) def plot_scatter(self, data, labels, title): X = numpy.concatenate(data, axis=0) Y = numpy.concatenate( [numpy.full(len(d), i) for i, d in enumerate(data, 1)], axis=0) self._viz.scatter(win=title, X=X, Y=Y, opts=dict(legend=labels, title=title, markersize=5, webgl=True, width=600, height=600, markeropacity=0.5))
class VisdomLinePlotter(object): """Plots to Visdom""" def __init__(self, env_name='main'): self.viz = Visdom(port=8889) self.env = env_name self.plots = {} def plot(self, var_name, split_name, title_name, x, y): if var_name not in self.plots: self.plots[var_name] = self.viz.line(X=np.array([x, x]), Y=np.array([y, y]), env=self.env, opts=dict(legend=[split_name], title=title_name, xlabel='Epochs', ylabel=var_name)) else: self.viz.line(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[var_name], name=split_name, update='append') def plot_scatter(self, var_name, split_name, title_name, x, y, color): if var_name not in self.plots: self.plots[var_name] = self.viz.scatter( X=np.array([[x, y]]), env=self.env, opts=dict(legend=[split_name], title=title_name, xlabel='Epochs', ylabel=var_name, markercolor=np.array([color]))) else: self.viz.scatter(X=np.array([[x, y]]), env=self.env, win=self.plots[var_name], name=split_name, update='append', opts=dict(markercolor=np.array([color])))
class Plot(object): def __init__(self, title, port=8080): self.viz = Visdom(port=port) self.windows = {} self.title = title def register_scatterplot(self, name, xlabel, ylabel): win = self.viz.scatter(X=numpy.zeros((1, 2)), opts=dict(title=self.title, markersize=5, xlabel=xlabel, ylabel=ylabel)) self.windows[name] = win def update_scatterplot(self, name, x, y): self.viz.updateTrace(X=numpy.array([x]), Y=numpy.array([y]), win=self.windows[name])
from visdom import Visdom import numpy as np vis = Visdom(env='test') x = np.linspace(0, 2 * np.pi, num=180) y = np.sin(x) vis.line(Y=y, X=x) y2 = np.cos(x) vis.line(Y=np.column_stack((y, y2))) help(vis.line) loss_win = vis.line(np.arange(10)) # loss_win = vis.line(np.arange(10)) # 向散点图中加入新的描述 vis.scatter(X=np.random.rand(255, 2), win=loss_win) x = np.linspace(0, 100, 10) y = np.sin(x) acc_cure = vis.line(X=x, Y=y) import time for i in range(100): vis.line(X=x, Y=np.sin(x), win=acc_cure, update='append') time.sleep(1)
opts=dict(title='Random!', caption='How random.'), ) # grid of images viz.images(np.random.randn(20, 3, 64, 64), opts=dict(title='Random images', caption='How random.')) # scatter plots Y = np.random.rand(100) old_scatter = viz.scatter( X=np.random.rand(100, 2), Y=(Y[Y > 0] + 1.5).astype(int), opts=dict( legend=['Didnt', 'Update'], xtickmin=-50, xtickmax=50, xtickstep=0.5, ytickmin=-50, ytickmax=50, ytickstep=0.5, markersymbol='cross-thin-open', ), ) viz.update_window_opts( win=old_scatter, opts=dict( legend=['Apples', 'Pears'], xtickmin=0, xtickmax=1, xtickstep=0.5, ytickmin=0,
noise = init.normal(torch.FloatTensor(num_data, 1), std=0.5) x = init.uniform(torch.Tensor(num_data, 1), -15, 10) y = -x**3 - 8 * (x**2) + 7 * x + 3 x_noise = x + noise y_noise = -x_noise**3 - 8 * (x_noise**2) + 7 * x_noise + 3 input_data = torch.cat([x, y_noise], 1) win = viz.scatter( X=input_data, opts=dict( xtickmin=-15, xtickmax=10, xtickstep=1, ytickmin=-300, ytickmax=200, ytickstep=1, markersymbol='dot', markercolor=np.random.randint(0, 255, num_data), markersize=5, ), ) viz.updateTrace( X=x, Y=y, win=win, ) # model & optimizer
x = init.uniform(torch.Tensor(num_data, 1), -10, 10) y = init.uniform(torch.Tensor(num_data, 1), -10, 10) z = x**2 + y**2 x_noise = x + init.normal(torch.FloatTensor(num_data, 1), std=0.5) y_noise = y + init.normal(torch.FloatTensor(num_data, 1), std=0.5) z_noise = x_noise**2 + y_noise**2 data_noise = torch.cat([x, y, z_noise], 1) # data visualization win_1 = viz.scatter(X=data_noise, opts=dict( markersize=MARKER_SIZE, markercolor=np.ndarray(shape=[num_data, 3], dtype=float, buffer=[51, 153, 255] * np.ones(shape=[num_data, 3])))) model = nn.Sequential( nn.Linear(2, 20), nn.ReLU(), nn.Linear(20, 10), nn.ReLU(), nn.Linear(10, 5), nn.ReLU(), nn.Linear(5, 5), nn.ReLU(), nn.Linear(5, 1), ).cuda()
x = init.uniform(torch.Tensor(num_data,1),-10,10) y = init.uniform(torch.Tensor(num_data,1),-10,10) z = x**2 + y**2 x_noise = x + init.normal(torch.FloatTensor(num_data,1),std=0.5) y_noise = y + init.normal(torch.FloatTensor(num_data,1),std=0.5) z_noise = x_noise**2 + y_noise**2 data_noise = torch.cat([x_noise,y_noise,z_noise],1) # visualize data win_1=viz.scatter( X=data_noise, opts=dict( markersize=5, markercolor=np.ndarray(shape=[num_data,3],dtype=float,buffer=[51,153,255]*np.ones(shape=[num_data,3])) ) ) model = nn.Sequential( nn.Linear(2,20), nn.ReLU(), nn.Linear(20,10), nn.ReLU(), nn.Linear(10,5), nn.ReLU(), nn.Linear(5,5), nn.ReLU(), nn.Linear(5,1), ).cuda()
) # don't ignore the clipped eigenvalues when doing linear regression slope, intercept, r_value, p_value, std_err = stats.linregress(logX) if args.dataset == 'sinusoid': ytickmin = -10 elif args.dataset == 'omniglot': ytickmin = -4 spectrum_plot = viz.scatter( X=X, name='raw', opts=dict( title= f'NTK spectrum, iter {i}, training data, fixed evaluation task', xtype='log', ytype='log', xlabel='index', ylabel='eigenvalue', width=800, height=600, ytickmin=ytickmin, ytickmax=3, showlegend=True)) X_fit = onp.stack([ind, 10**(slope * onp.log10(ind) + intercept)], axis=1) viz.line(Y=X_fit[:, 1], X=X_fit[:, 0], win=spectrum_plot, update='append', name=f'slope {slope:.3f}, intercept {intercept:.3f}')
y_noise = y + noise input_data = torch.cat([x, y_noise], dim=1) print(input_data) opts = dict(xtickmin=-15, xtickmax=15, xtickstep=1, ytickmin=0, ytickmax=500, ytickstep=1, markersymbol='dot', markersize=5, markercolor=np.random.randint(0, 255, num_data)) win = viz.scatter(X=input_data, opts=opts) print("\n2. 모델과 근사 방법을 정의") # 1->6->10->6->1 model = nn.Sequential(nn.Linear(1, 6), nn.ReLU(), nn.Linear(6, 10), nn.ReLU(), nn.Linear(10, 6), nn.ReLU(), nn.Linear(6, 1)).cuda() output = model(x.cuda()) loss_func = nn.L1Loss() optimizer = optim.SGD(model.parameters(), lr=0.0005) print("\n3. 훈련") loss_arr = [] label = y_noise.cuda() for i in range(num_epoch): optimizer.zero_grad() # 그라디언트 초기화 output = model(x.cuda())
def main(): start_time = time.clock() lr = 0.02 iteration = 10000 bias = 0.5 viz = Visdom(env='sin') trainingData = np.empty([0, 1, 1]) for i in range(40): a = i * math.pi / 40 trainingData = np.append(trainingData, [[[a]]], 0) testData = np.random.uniform(0, math.pi, (30, 1, 1)) rightResult = np.sin(testData) rightDot = np.append(testData[:, :, 0], rightResult[:, :, 0], 1) neuralNetwork = [] inputLayer = InputLayer(len(trainingData[0, 0])) h1 = NeuroLayer(5, inputLayer, bias) neuralNetwork.append(h1) a1 = Sigmoid(h1) neuralNetwork.append(a1) outputLayer = NeuroLayer(1, a1, bias) outputActionLayer = PRelu(outputLayer) errorLayer = ErrorLayer(outputActionLayer) neuralNetwork.append(outputLayer) neuralNetwork.append(outputActionLayer) neuralNetwork.append(errorLayer) init_weight = h1.weight.copy() for itr in range(1, iteration): np.random.shuffle(trainingData) last_error = 0 for d in trainingData: inputLayer.data = d errorLayer.target = [math.sin(d[0, 0])] for layer in neuralNetwork: layer.forward() for layer in reversed(neuralNetwork): layer.backward() last_error += errorLayer.data for layer in neuralNetwork: layer.update(lr) if (100 > itr > 19 or itr % 20 == 0): if (itr == 20): win = viz.line(X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), name="sin", win='loss') win2 = viz.scatter( X=np.random.rand(1, 2), name="sin dot", win='fitting', ) viz.updateTrace( X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), win=win, ) testResult = np.empty([30, 1]) for i in range(len(testData)): inputLayer.data = testData[i] for layer in neuralNetwork[:-1]: layer.forward() testResult[i][0] = outputActionLayer.data[0][0] testDot = np.append(testData[:, :, 0], testResult, 1) dot = np.append(rightDot, testDot, 0) viz.scatter(X=dot, name="sin dot", win=win2, Y=[1] * 30 + [2] * 30, opts=dict( legend=['right', 'test'], markersize=5, )) print('=================================') print('last_error', last_error) elapsed_time = time.clock() - start_time print("all time", elapsed_time) print('=================================') print('init weight', init_weight) print('last weight', h1.weight) print('=================================') last_error = 0 for d in testData: inputLayer.data = d errorLayer.target = np.sin(d) for layer in neuralNetwork: layer.forward() last_error += errorLayer.data print('_______________') print(d, np.sin(d)) print("output", outputActionLayer.data) print("error", errorLayer.data) print('_______________') errorLayer.update(0) print('last_error', last_error / len(testData))
text_window = viz.text("Hello Pytorch") print("\n그림창") image_window = viz.image(np.random.rand(3, 256, 256), opts=dict(title="random", caption="random noise")) images_window = viz.images(np.random.rand(10, 3, 64, 64), opts=dict(title="random", caption="random noise")) print("\n2D 분산형 차트 그리기") Y = np.random.rand(100) scatter_window = viz.scatter(X=np.random.rand(100, 2), Y=(Y + 1.5).astype(int), opts=dict(legend=['Apples', 'Pears'], xtickmin=0, xtickmax=2, xtickstep=0.5, ytickmin=0, ytickmax=2, ytickstep=0.5, markersymbol='cross-thin-open')) print("\n분산형 차트 업데이트") viz.scatter(X=np.random.rand(50), Y=np.random.rand(50), win=scatter_window, name='bananas', update='replace') print("\n3D 분산형 차트 그리기") viz.scatter(X=np.random.rand(100, 3), Y=(Y + 1.5).astype(int),
# image demo viz.image( np.random.rand(3, 512, 256), opts=dict(title='Random!', caption='How random.'), ) # scatter plots Y = np.random.rand(100) viz.scatter( X=np.random.rand(100, 2), Y=(Y[Y > 0] + 1.5).astype(int), opts=dict( legend=['Apples', 'Pears'], xtickmin=-5, xtickmax=5, xtickstep=0.5, ytickmin=-5, ytickmax=5, ytickstep=0.5, markersymbol='cross-thin-open', ), ) viz.scatter( X=np.random.rand(100, 3), Y=(Y + 1.5).astype(int), opts=dict( legend=['Men', 'Women'], markersize=5, ) )
# grid of images viz.images( np.random.randn(20, 3, 64, 64), opts=dict(title='Random images', caption='How random.') ) # scatter plots Y = np.random.rand(100) old_scatter = viz.scatter( X=np.random.rand(100, 2), Y=(Y[Y > 0] + 1.5).astype(int), opts=dict( legend=['Didnt', 'Update'], xtickmin=-50, xtickmax=50, xtickstep=0.5, ytickmin=-50, ytickmax=50, ytickstep=0.5, markersymbol='cross-thin-open', ), ) viz.update_window_opts( win=old_scatter, opts=dict( legend=['Apples', 'Pears'], xtickmin=0, xtickmax=1, xtickstep=0.5, ytickmin=0,
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)
def eval_pcpnet(opt): opt.models = opt.models.split() if opt.seed < 0: opt.seed = random.randint(1, 10000) device = torch.device("cpu" if opt.gpu_idx < 0 else "cuda:%d" % opt.gpu_idx) for model_name in opt.models: print("Random Seed: %d" % (opt.seed)) random.seed(opt.seed) torch.manual_seed(opt.seed) model_filename = os.path.join(opt.modeldir, model_name+opt.modelpostfix) param_filename = os.path.join(opt.modeldir, model_name+opt.parmpostfix) # load model and training parameters trainopt = torch.load(param_filename) if opt.batchSize == 0: model_batchSize = trainopt.batchSize else: model_batchSize = opt.batchSize # get indices in targets and predictions corresponding to each output pred_dim = 0 output_pred_ind = [] for o in trainopt.outputs: if o == 'unoriented_normals' or o == 'oriented_normals': output_pred_ind.append(pred_dim) pred_dim += 3 elif o == 'max_curvature' or o == 'min_curvature': output_pred_ind.append(pred_dim) pred_dim += 1 else: raise ValueError('Unknown output: %s' % (o)) #print(trainopt.patch_radius) dataset = PointcloudPatchDataset( root=opt.indir, shape_list_filename=opt.dataset, patch_radius=trainopt.patch_radius, points_per_patch=trainopt.points_per_patch, #patch_features=[], seed=opt.seed, #use_pca=trainopt.use_pca, center=trainopt.patch_center, #point_tuple=trainopt.point_tuple, #sparse_patches=opt.sparse_patches, cache_capacity=opt.cache_capacity) if opt.sampling == 'full': datasampler = SequentialPointcloudPatchSampler(dataset) elif opt.sampling == 'sequential_shapes_random_patches': datasampler = SequentialShapeRandomPointcloudPatchSampler( dataset, patches_per_shape=opt.patches_per_shape, seed=opt.seed, sequential_shapes=True, identical_epochs=False) else: raise ValueError('Unknown sampling strategy: %s' % opt.sampling) dataloader = torch.utils.data.DataLoader( dataset, sampler=datasampler, batch_size=model_batchSize, num_workers=int(opt.workers)) regressor = DSAC( trainopt.hypotheses, trainopt.inlierthreshold, trainopt.inlierbeta, trainopt.inlieralpha, trainopt.normal_loss, trainopt.seed,device, use_point_stn=trainopt.use_point_stn, use_feat_stn=trainopt.use_feat_stn, use_mask=trainopt.use_mask ) regressor.load_state_dict(torch.load(model_filename)) regressor.to(device) regressor.eval() shape_ind = 0 shape_patch_offset = 0 if opt.sampling == 'full': shape_patch_count = dataset.shape_patch_count[shape_ind] elif opt.sampling == 'sequential_shapes_random_patches': shape_patch_count = min(opt.patches_per_shape, dataset.shape_patch_count[shape_ind]) else: raise ValueError('Unknown sampling strategy: %s' % opt.sampling) shape_properties = torch.zeros(shape_patch_count, pred_dim, dtype=torch.float, device=device) # append model name to output directory and create directory if necessary model_outdir = os.path.join(opt.outdir, model_name) if not os.path.exists(model_outdir): os.makedirs(model_outdir) num_batch = len(dataloader) batch_enum = enumerate(dataloader, 0) for batchind, data in batch_enum: # get batch and upload to GPU #index =[] points, data_trans,mask_t = data points = points.transpose(2, 1) points = points.to(device) data_trans = data_trans.to(device) mask_t = mask_t.to(device) with torch.no_grad(): exp_loss, top_loss,normal,pts,mask = regressor(points,data_trans) viz = Visdom() assert viz.check_connection() Y=torch.zeros(512+32+1) Z=torch.zeros(512+1) Z[0:512]+=1 Z[512]+=2 Y[0:512]+=1 Y[512:512+32]+=2 Y[512+32]+=5 print(top_loss.mean()) for i in range(points.size(0)): #print("input",x[i].transpose(0,1)[0:100]) # print("predict",i,normal[i]) # print("ground truth",i,data_trans[i]) # print("top_loss_loss",i,top_loss[i],"\n") # print(mask_t[i]) # print(mask[i].view(-1)) viz.scatter( X=torch.cat((points[i].transpose(0,1),pts[i],torch.zeros(1,3).cuda()),0), Y=Y, opts=dict( title = str(i), #'legend': ['Men', 'Women'], markersize= 2, #markercolor=np.random.randint(0, 255, (3, 3,)), ) ) # # # viz.scatter( # # # X=torch.mul(points[i].transpose(0,1),mask[i]), # # # opts=dict( # # # title = str(i), # # # #'legend': ['Men', 'Women'], # # # markersize= 2, # # # #markercolor=np.random.randint(0, 255, (3, 3,)), # # # ) # # # ) # viz.scatter( # X=torch.cat((torch.mul(points[i].transpose(0,1),mask_t[i].view(-1,1)),torch.zeros(1,3).cuda(2)),0), # Y=Z, # opts=dict( # title = str(i)+"true", # #'legend': ['Men', 'Women'], # markersize= 2, # #markercolor=np.random.randint(0, 255, (3, 3,)), # ) # ) #print("pts",i,pts[i]) # # post-processing of the prediction # for oi, o in enumerate(trainopt.outputs): # if o == 'unoriented_normals' or o == 'oriented_normals': # o_pred = pred[:, output_pred_ind[oi]:output_pred_ind[oi]+3] # if trainopt.use_point_stn: # # transform predictions with inverse transform # # since we know the transform to be a rotation (QSTN), the transpose is the inverse # o_pred[:, :] = torch.bmm(o_pred.unsqueeze(1), trans.transpose(2, 1)).squeeze(dim=1) # if trainopt.use_pca: # # transform predictions with inverse pca rotation (back to world space) # o_pred[:, :] = torch.bmm(o_pred.unsqueeze(1), data_trans.transpose(2, 1)).squeeze(dim=1) # # normalize normals # o_pred_len = torch.max(o_pred.new_tensor([sys.float_info.epsilon*100]), o_pred.norm(p=2, dim=1, keepdim=True)) # o_pred = o_pred / o_pred_len # elif o == 'max_curvature' or o == 'min_curvature': # o_pred = pred[:, output_pred_ind[oi]:output_pred_ind[oi]+1] # # undo patch size normalization: # o_pred[:, :] = o_pred / dataset.patch_radius_absolute[shape_ind][0] # else: # raise ValueError('Unsupported output type: %s' % (o)) print('[%s %d/%d] shape %s' % (model_name, batchind, num_batch-1, dataset.shape_names[shape_ind])) batch_offset = 0 while batch_offset < normal.size(0): shape_patches_remaining = shape_patch_count-shape_patch_offset batch_patches_remaining = normal.size(0)-batch_offset # append estimated patch properties batch to properties for the current shape shape_properties[shape_patch_offset:shape_patch_offset+min(shape_patches_remaining, batch_patches_remaining), :] = normal[ batch_offset:batch_offset+min(shape_patches_remaining, batch_patches_remaining), :] batch_offset = batch_offset + min(shape_patches_remaining, batch_patches_remaining) shape_patch_offset = shape_patch_offset + min(shape_patches_remaining, batch_patches_remaining) if shape_patches_remaining <= batch_patches_remaining: # save shape properties to disk # prop_saved = [False]*len(trainopt.outputs) # # save normals # oi = [i for i, o in enumerate(trainopt.outputs) if o in ['unoriented_normals', 'oriented_normals']] # if len(oi) > 1: # raise ValueError('Duplicate normal output.') # elif len(oi) == 1: # oi = oi[0] # normal_prop = shape_properties[:, output_pred_ind[oi]:output_pred_ind[oi]+3] # np.savetxt(os.path.join(model_outdir, dataset.shape_names[shape_ind]+'.normals'), normal_prop.cpu().numpy()) # prop_saved[oi] = True # # save curvatures # oi1 = [i for i, o in enumerate(trainopt.outputs) if o == 'max_curvature'] # oi2 = [i for i, o in enumerate(trainopt.outputs) if o == 'min_curvature'] # if len(oi1) > 1 or len(oi2) > 1: # raise ValueError('Duplicate minimum or maximum curvature output.') # elif len(oi1) == 1 or len(oi2) == 1: # curv_prop = shape_properties.new_zeros(shape_properties.size(0), 2) # if len(oi1) == 1: # oi1 = oi1[0] # curv_prop[:, 0] = shape_properties[:, output_pred_ind[oi1]] # prop_saved[oi1] = True # if len(oi2) == 1: # oi2 = oi2[0] # curv_prop[:, 1] = shape_properties[:, output_pred_ind[oi2]] # prop_saved[oi2] = True # np.savetxt(os.path.join(model_outdir, dataset.shape_names[shape_ind]+'.curv'), curv_prop.cpu().numpy()) # if not all(prop_saved): # raise ValueError('Not all shape properties were saved, some of them seem to be unsupported.') # # save point indices # if opt.sampling != 'full': # np.savetxt(os.path.join(model_outdir, dataset.shape_names[shape_ind]+'.idx'), datasampler.shape_patch_inds[shape_ind], fmt='%d') # start new shape if shape_ind + 1 < len(dataset.shape_names): shape_patch_offset = 0 shape_ind = shape_ind + 1 if opt.sampling == 'full': shape_patch_count = dataset.shape_patch_count[shape_ind] elif opt.sampling == 'sequential_shapes_random_patches': # shape_patch_count = min(opt.patches_per_shape, dataset.shape_patch_count[shape_ind]) shape_patch_count = len(datasampler.shape_patch_inds[shape_ind]) else: raise ValueError('Unknown sampling strategy: %s' % opt.sampling) shape_properties = shape_properties.new_zeros(shape_patch_count, pred_dim)
class Visualizer: def __init__(self, env="main", server="http://localhost", port=8097, base_url="/", http_proxy_host=None, http_proxy_port=None): self._viz = Visdom(env=env, server=server, port=port, http_proxy_host=http_proxy_host, http_proxy_port=http_proxy_port, use_incoming_socket=False) self._viz.close(env=env) def plot_line(self, values, steps, name, legend=None): if legend is None: opts = dict(title=name) else: opts = dict(title=name, legend=legend) self._viz.line(X=numpy.column_stack(steps), Y=numpy.column_stack(values), win=name, update='append', opts=opts) def plot_text(self, text, title, pre=True): _width = max([len(x) for x in text.split("\n")]) * 10 _heigth = len(text.split("\n")) * 20 _heigth = max(_heigth, 120) if pre: text = "<pre>{}</pre>".format(text) self._viz.text(text, win=title, opts=dict(title=title, width=min(_width, 400), height=min(_heigth, 400))) def plot_bar(self, data, labels, title): self._viz.bar(win=title, X=data, opts=dict(legend=labels, stacked=False, title=title)) def plot_scatter(self, data, labels, title): X = numpy.concatenate(data, axis=0) Y = numpy.concatenate( [numpy.full(len(d), i) for i, d in enumerate(data, 1)], axis=0) self._viz.scatter(win=title, X=X, Y=Y, opts=dict(legend=labels, title=title, markersize=5, webgl=True, width=400, height=400, markeropacity=0.5)) def plot_heatmap(self, data, labels, title): self._viz.heatmap( win=title, X=data, opts=dict( title=title, columnnames=labels[1], rownames=labels[0], width=700, height=700, layoutopts={ 'plotly': { 'xaxis': { 'side': 'top', 'tickangle': -60, # 'autorange': "reversed" }, 'yaxis': { 'autorange': "reversed" }, } }))
'title': 'multi-images', }) # 散点图 Y = np.random.rand(100) Y = (Y[Y > 0] + 1.5).astype(int), # 100个标签1和2 old_scatter = viz.scatter( X=np.random.rand(100, 2) * 100, Y=Y, opts={ 'title': 'Scatter', 'legend': ['A', 'B'], 'xtickmin': 0, 'xtickmax': 100, 'xtickstep': 10, 'ytickmin': 0, 'ytickmax': 100, 'ytickstep': 10, 'markersymbol': 'cross-thin-open', 'width': 800, 'height': 600 }, ) # time.sleep(5) # 更新样式 viz.update_window_opts(win=old_scatter, opts={ 'title': 'New Scatter', 'legend': ['Apple', 'Banana'], 'markersymbol': 'dot'
opts=dict(title='Random!', caption='How random.'), ) # grid of images viz.images(np.random.randn(20, 3, 64, 64), opts=dict(title='Random images', caption='How random.')) # scatter plots Y = np.random.rand(100) viz.scatter( X=np.random.rand(100, 2), Y=(Y[Y > 0] + 1.5).astype(int), opts=dict( legend=['Apples', 'Pears'], xtickmin=-5, xtickmax=5, xtickstep=0.5, ytickmin=-5, ytickmax=5, ytickstep=0.5, markersymbol='cross-thin-open', ), ) viz.scatter(X=np.random.rand(100, 3), Y=(Y + 1.5).astype(int), opts=dict( legend=['Men', 'Women'], markersize=5, )) # 2D scatterplot with custom intensities (red channel)
def main(): start_time = time.clock() lr = 0.0000006 # 学习率 iteration = 10000 # 迭代次数 bias = 0.5 # 初始化bias testNum = 30 # 测试集数量 viz = Visdom(env='x1+x2') trainingData = np.random.uniform(-100, 100, (30, 1, 2)) testData = np.random.uniform(-500, 500, (testNum, 1, 2)) rightResult = testData[:, :, 0] + testData[:, :, 1] rightDot = np.append(testData[:, 0, :], rightResult, 1) neuralNetwork = [] inputLayer = InputLayer(len(trainingData[0, 0])) h1 = NeuroLayer(5, inputLayer, bias) neuralNetwork.append(h1) a1 = PRelu(h1) neuralNetwork.append(a1) outputLayer = NeuroLayer(1, a1, bias) outputActionLayer = PRelu(outputLayer) errorLayer = ErrorLayer(outputActionLayer) neuralNetwork.append(outputLayer) neuralNetwork.append(outputActionLayer) neuralNetwork.append(errorLayer) init_weight = h1.weight.copy() for itr in range(1, iteration): np.random.shuffle(trainingData) last_error = 0 for d in trainingData: inputLayer.data = d errorLayer.target = [d[0, 0] + d[0, 1]] for layer in neuralNetwork: layer.forward() for layer in reversed(neuralNetwork): layer.backward() last_error += errorLayer.data for layer in neuralNetwork: layer.update(lr) if(100 > itr > 19 or itr % 20 == 0): if(itr == 20): win = viz.line( X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), name="x1+x2", win='loss' ) win2 = viz.scatter( X=np.random.rand(1, 2), name="x1+x2 dot", win='fitting', ) viz.updateTrace( X=np.array([itr]), Y=np.array(last_error[0] / len(trainingData)), win=win, ) testResult = np.empty([testNum, 1]) for i in range(len(testData)): inputLayer.data = testData[i] for layer in neuralNetwork[:-1]: layer.forward() testResult[i][0] = outputActionLayer.data[0][0] testDot = np.append(testData[:, 0, :], testResult, 1) dot = np.append(rightDot, testDot, 0) viz.scatter( X=dot, name="x1+x2 dot", win=win2, Y=[1] * testNum + [2] * testNum, opts=dict( legend=['right', 'test'], markersize=5, ) ) print('=================================') print('last_error', last_error) elapsed_time = time.clock() - start_time print("all time", elapsed_time) print('=================================') print('init weight', init_weight) print('last weight', h1.weight) print('=================================') last_error = 0 for d in testData: inputLayer.data = d errorLayer.target = [d[0, 0] + d[0, 1]] for layer in neuralNetwork: layer.forward() last_error += errorLayer.data print('_______________') print(d, [d[0, 0] + d[0, 1]]) print("output", outputActionLayer.data) print("error", errorLayer.data) print('_______________') print('last_error', last_error / len(testData))
input_data = torch.cat([x, y_noise], dim=1) print(input_data) opts = dict( xtickmin=-15, xtickmax=10, xtickstep=1, ytickmin=-300, ytickmax=200, ytickstep=1, markersymbol='dot', markersize=5, markercolor=np.random.randint(0, 255, num_data) ) win = viz.scatter(X=input_data, opts=opts) print("\n2. 모델과 근사 방법을 정의") model = nn.Linear(1, 1) # 입력 1개, 출력 1개 output = model(x) loss_func = nn.MSELoss() optimizer = optim.SGD(model.parameters(), lr=0.01) print("\n3. 훈련") loss_arr = [] for i in range(num_epoch): optimizer.zero_grad() # 그라디언트 초기화 output = model(x) loss = loss_func(output, y_noise) loss.backward()