-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvis.py
161 lines (123 loc) · 4.83 KB
/
vis.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#------------------------------------------------------------------
# Written at FrostLabs
# Licence: MIT
#------------------------------------------------------------------
import numpy as np
import torch
from sklearn.manifold import MDS
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import matplotlib
import matplotlib.patches as mpatches
import argparse
import sys
import copy
from multiprocessing import Pool
from contextlib import contextmanager
import multiprocessing
import os
import time
def plotter(X1, X2, args, tsne=False):
plt.clf()
filename1 = args['version'] + '/proj_' + str(args['epoch'])
filename2 = args['version'] + '/ans_' + str(args['epoch'])
if (tsne):
filename1 += '_tsne'
filename2 += '_tsne'
else:
filename1 += '_mds'
filename2 += '_mds'
red_patch = mpatches.Patch(color='red', label=filename1)
blue_patch = mpatches.Patch(color='blue', label=filename2)
x1_min, x1_max = np.min(X1, 0), np.max(X1, 0)
X1 = (X1 - x1_min) / (x1_max - x1_min)
x2_min, x2_max = np.min(X2, 0), np.max(X2, 0)
X2 = (X2 - x2_min) / (x2_max - x2_min)
plt.scatter(X1[:,0], X1[:,1], c='red', s=3, alpha=0.5)
plt.scatter(X2[:,0], X2[:,1], c='blue', s=3, alpha=0.5)
plt.legend(handles=[red_patch, blue_patch])
# plt.show()
if (tsne):
plt.savefig('./saved/' + args['version'] + '/vis_' + args['version'] + '_' + str(args['epoch']) + '_tsne' + '.png')
else:
plt.savefig('./saved/' + args['version'] + '/vis_' + args['version'] + '_' + str(args['epoch']) + '_mds' + '.png')
def vis_func(args, k=None):
'''
args = {
'version': 'baseline_wa',
'epoch': 5, #will be replaced by k (if passed)
'num_samples': 1000,
'till': True
}
'''
if (k is not None):
args['epoch'] = k
filename1 = './saved/' + args['version'] + '/z_proj_' + str(args['epoch']) + '.npy'
filename2 = './saved/' + args['version'] + '/z_ans_' + str(args['epoch']) + '.npy'
print("Started for epoch %d with pid %d: " % (args['epoch'], os.getpid()))
if (args['num_samples'] is not None):
num_samples = int(args['num_samples'])
else:
num_samples = None
#print("Loading file 1")
z1_load = np.load(filename1)
z1_load = torch.from_numpy(z1_load)
#print("z1 shape: ", z1_load.shape)
#print("Loading file 2")
z2_load = np.load(filename2)
z2_load = torch.from_numpy(z2_load)
#print("z2 shape: ", z2_load.shape)
embedding = MDS(n_components=2)
tsne_embedding = TSNE()
#print("Transforming z1, z2")
if (num_samples is None):
z1_transformed = embedding.fit_transform(z1_load)
z2_transformed = embedding.fit_transform(z2_load)
z1_transformed_tsne = embedding.fit_transform(z1_load)
z2_transformed_tsne = embedding.fit_transform(z2_load)
else:
z1_transformed = embedding.fit_transform(z1_load[:num_samples])
z2_transformed = embedding.fit_transform(z2_load[:num_samples])
z1_transformed_tsne = embedding.fit_transform(z1_load[:num_samples])
z2_transformed_tsne = embedding.fit_transform(z2_load[:num_samples])
plotter(z1_transformed, z2_transformed, args)
print("Mds Image save successful for: ", args['epoch'])
plotter(z1_transformed_tsne, z2_transformed_tsne, args, tsne=True)
print("Tsne Image save successful for: ", args['epoch'])
def vis_func_unpacker(args):
vis_func(*args)
@contextmanager
def poolcontext(*args, **kwargs):
pool = multiprocessing.Pool(*args, **kwargs)
yield pool
pool.terminate()
'''
args_parsed = {
'version': 'baseline_wa',
'epoch': 5, #will be replaced by k (if passed)
'num_samples': 1000,
'till': True
}
'''
def visualize(args_parsed):
start = time.time()
if (args_parsed['till'] is True):
dic = copy.deepcopy(args_parsed)
n = int(dic['epoch'])
with poolcontext(processes= 4) as p:
p.map(vis_func_unpacker, [(dic, i) for i in range(n+1)])
else:
vis_func(args_parsed)
end = time.time()
print("Time taken to vis (in s): ", end-start)
if __name__ == '__main__':
torch.manual_seed(0)
ap = argparse.ArgumentParser()
# Add the arguments to the parser
ap.add_argument("-v", "--version", required=True, type=str, help="folder name in saved/")
ap.add_argument("-e", "--epoch", required=True, type=int, help="epoch number")
ap.add_argument("-n", "--num_samples", required=False, type=int, default=1000, help="number of samples to plot")
ap.add_argument("-t", "--till", required=False, type=bool, default=False,
help="Set true to make plots from epoch 0 to --epoch [both included], False to make plots only for epoch number --epoch")
args_parsed = vars(ap.parse_args())
visualize(args_parsed)