forked from pcchenxi/domain_adapt_grasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper_result.py
211 lines (169 loc) · 8.49 KB
/
paper_result.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from __future__ import print_function
import argparse
import os
import random
import torch
import numpy as np
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from algos import data_loader as training_data
from matplotlib import pyplot as plt
import algos.tool_func as tool
from sklearn.manifold import TSNE
tsne = TSNE(n_components=2)
parser = argparse.ArgumentParser()
parser.add_argument('--workers', type=int, help='number of data loading workers', default=2)
parser.add_argument('--batchSize', type=int, default=32, help='input batch size')
parser.add_argument('--imageSize', type=int, default=64, help='the height / width osf the input image to network')
parser.add_argument('--nz', type=int, default=100, help='size of the latent z vector')
parser.add_argument('--ngf', type=int, default=64)
parser.add_argument('--ndf', type=int, default=64)
parser.add_argument('--niter', type=int, default=10000, help='number of epochs to train for')
parser.add_argument('--lr', type=float, default=0.0001, help='learning rate, default=0.0002')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam. default=0.5')
parser.add_argument('--cuda', action='store_true', help='enables cuda')
parser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use')
parser.add_argument('--netG', default='', help="path to netG (to continue training)")
parser.add_argument('--netD', default='', help="path to netD (to continue training)")
parser.add_argument('--outf', default='.', help='folder to output images and model checkpoints')
parser.add_argument('--manualSeed', type=int, help='manual seed')
parser.add_argument('--train_model', type=str, help='model name to train')
parser.add_argument('--method', type=str, help='model name to train')
opt = parser.parse_args()
print(opt)
try:
path = 'results/%s/%s/' % (opt.outf, opt.method)
os.makedirs(path)
except OSError:
pass
if opt.manualSeed is None:
opt.manualSeed = random.randint(1, 10000)
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
torch.manual_seed(opt.manualSeed)
if torch.cuda.is_available() and not opt.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
device = torch.device("cuda:0")
data_obj = training_data.get_dataset(img_type='obj', root_dir='./dataset_test/')
loader_src = torch.utils.data.DataLoader(data_src, batch_size=opt.batchSize,
shuffle=True, num_workers=16)
loader_obj = torch.utils.data.DataLoader(data_obj, batch_size=opt.batchSize,
shuffle=True, num_workers=16)
loader_noobj = torch.utils.data.DataLoader(data_noobj, batch_size=opt.batchSize,
shuffle=True, num_workers=16)
loader_test = torch.utils.data.DataLoader(data_test, batch_size=100,
shuffle=True, num_workers=16)
# for data_src in loader_noobj:
# print('1')
# break
# for data_src in loader_src: #zip(loader_obj, loader_noobj):
# print(len(data_src['image']))
# print('--------------------dd')
# if opt.method == 'vae_binary':
# from algos.domain_adapt_vae_binary import BinaryVAE
# net = BinaryVAE()
# if opt.method == 'vae_multi':
# from algos.vae_multi_category import MultiCategoryVAE
# net = MultiCategoryVAE()
if opt.method == 'vae_multi':
# elif opt.method == 'vae_multi_binar':
from algos.vae_multi_binary import MultiCategoryVAE
net = MultiCategoryVAE()
elif opt.method == 'spatial':
from algos.vae_spatial import SpatialVAE
net = SpatialVAE()
print('spatial encoder')
elif opt.method == 'adda':
from algos.vae_multi_binary_adda import MultiCategoryADDA
net = MultiCategoryADDA()
print('spatial encoder')
batch_loss = []
batch_loss_xyt = []
batch_loss_xyt_std = []
model_loss = []
model_batch_loss = []
net.load_models(opt.outf, opt.method)
batch_loss = np.load('results/%s/%s/test_loss.npy' % (opt.outf, opt.method)).tolist()
batch_loss_xyt = np.load('results/%s/%s/test_loss_xyt.npy' % (opt.outf, opt.method)).tolist()
batch_loss_xyt_std = np.load('results/%s/%s/test_loss_xyt_std.npy' % (opt.outf, opt.method)).tolist()
model_loss = np.load('results/%s/%s/model_loss.npy' % (opt.outf, opt.method)).tolist()
min_loss = 100
for epoch in range(500):
i = 0
data_test = next(iter(loader_test))
# net.net_encoder_target.cpu()
# net.net_decoder.cpu()
net.net_encoder_target.eval()
net.net_decoder.eval()
test_img = data_test['image'].to(device)
test_label = data_test['label'].to(device)
# test_z, test_conv = net.net_encoder_target(test_img.detach())
test_z, test_conv = net.net_encoder_target(test_img.detach())
# test_mu, test_mu_conv = net.net_encoder_target.get_mu_conv(test_img.detach())
if opt.method == 'spatial':
key, src_feature = net.net_encoder_sourse.get_softmax_feature(test_img, test_conv)
pred_label = net.net_decoder(key.detach()).detach()
size = test_conv[0].shape
tool.save_test_imgs(opt.outf, opt.method, test_img, test_conv[0].view(-1, 1, size[1], size[2]), src_feature.view(-1, 3, 60, 105), ext='')
test_loss = net.get_autoed_loss(net.net_encoder_target, test_conv.detach(), test_label.detach()).detach().item()
else:
pred_label = net.net_decoder(test_z.detach()).detach()
size = test_conv[0].shape
tool.save_test_imgs(opt.outf, opt.method, test_img, test_z.view(-1, 1, size[1], size[2]), test_conv, ext='')
test_loss = net.get_autoed_loss(net.net_encoder_target, test_z.detach(), test_label.detach()).detach().item()
diff = (pred_label - test_label).detach().cpu().numpy()
xyt_mse = (np.abs(diff)).mean(axis=0)
xyt_std = diff.std(axis=0)
print(pred_label[0], test_label[0], diff[0], np.mean(np.square(diff)[:,2]))
batch_loss.append(test_loss)
batch_loss_xyt.append(xyt_mse)
batch_loss_xyt_std.append(xyt_std)
model_loss.append(np.mean(model_batch_loss))
# net.net_encoder_target.to(device)
# net.net_decoder.to(device)
net.net_encoder_target.train()
net.net_decoder.train()
plt.clf()
plt.plot(np.array(batch_loss), color='b')
plt.plot(np.array(model_loss), color='r')
plt.gca().set_ylim([0, 0.15])
plt.yticks(np.arange(0, 0.15, 0.05))
plt.savefig('results/%s/%s/test_loss.png' % (opt.outf, opt.method))
np.save('results/%s/%s/test_loss' % (opt.outf, opt.method), batch_loss)
np.save('results/%s/%s/model_loss' % (opt.outf, opt.method), model_loss)
plt.clf()
plt.plot(np.array(batch_loss_xyt)[:,0], color='r')
plt.plot(np.array(batch_loss_xyt)[:,1], color='g')
plt.plot(np.array(batch_loss_xyt)[:,2], color='b')
plt.gca().set_ylim([0, 0.5])
plt.yticks(np.arange(0, 0.5, 0.05))
plt.savefig('results/%s/%s/test_loss_xyt.png' % (opt.outf, opt.method))
np.save('results/%s/%s/test_loss_xyt' % (opt.outf, opt.method), batch_loss_xyt)
plt.clf()
plt.plot(np.array(batch_loss_xyt_std)[:,0], color='r')
plt.plot(np.array(batch_loss_xyt_std)[:,1], color='g')
plt.plot(np.array(batch_loss_xyt_std)[:,2], color='b')
plt.gca().set_ylim([0, 0.5])
plt.yticks(np.arange(0, 0.5, 0.05))
plt.savefig('results/%s/%s/test_loss_xyt_std.png' % (opt.outf, opt.method))
np.save('results/%s/%s/test_loss_xyt_std' % (opt.outf, opt.method), batch_loss_xyt_std)
model_batch_loss = []
for data_src, data_obj, data_noobj in zip(loader_src, loader_obj, loader_noobj):
# for data_src, data_obj, data_noobj in zip(loader_src, loader_obj[i%obj_num], loader_noobj):
# data_obj = next(iter(loader_obj[i%obj_num]))
# print(i%obj_num)
if len(data_src['image']) != opt.batchSize or len(data_obj['image']) != opt.batchSize or len(data_noobj['image']) != opt.batchSize:
continue
loss = net.update_net(data_src, data_obj, data_noobj, epoch, i, opt.outf, opt.method)
# print('[%d/%d][%d/%d] Loss: %.4f %.4f %.4f | %.4f %.4f %.4f' % (epoch, opt.niter, i, len(loader_src), errD_src.item(), errD_obj.item(), errD_noobj.item(), errG_obj.item(), errG_noobj.item(), err_autoed.item()))
model_batch_loss.append(loss)
i += 1
# python run_domain_adapt.py --method vae --outf vae_combine --train_model domian_adapt
# python run_domain_adapt_multi.py --method vae --outf vae_joint
# python run_exp.py --method vae_multi --outf vae_joint