forked from sled-group/Comparative-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
327 lines (278 loc) · 9.38 KB
/
main.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import os
import torch
import clip
import time
import pickle
import random
import argparse
import torch.nn as nn
import torch.optim as optim
from PIL import Image
import json
from pprint import pprint
from torch.utils.data import DataLoader
from config import *
from dataset import *
from models import *
device = "cuda" if torch.cuda.is_available() else "cpu"
def my_train_clip_encoder(dt, memory, attr, lesson):
# get model
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device)
model = CLIP_AE_Encode(hidden_dim_clip, latent_dim, isAE=False)
if lesson in memory.keys():
print("______________ loading_____________________")
model.load_state_dict(memory[lesson]['model'])
optimizer = optim.Adam(model.parameters(), lr=lr)
model.train().to(device)
loss_sim = None
loss_dif = None
loss = 10
ct = 0
centroid_sim = torch.rand(1, latent_dim).to(device)
while loss > 0.008:
##
ct += 1
if ct > 5:
break
for i in range(200):
# Get Inputs: sim_batch, (sim_batch, 4, 128, 128)
base_name_sim, images_sim = dt.get_better_similar(attr, lesson)
images_sim = images_sim.to(device)
# run similar model
z_sim = model(clip_model, images_sim)
centroid_sim = centroid_sim.detach()
centroid_sim, loss_sim = get_sim_loss(torch.vstack((z_sim, centroid_sim)))
# Run Difference
base_name_dif, images_dif = dt.get_better_similar_not(attr, lesson)
images_dif = images_dif.to(device)
# run difference model
z_dif = model(clip_model, images_dif)
loss_dif = get_sim_not_loss(centroid_sim, z_dif)
# compute loss
loss = (loss_sim)**2 + (loss_dif-1)**2
optimizer.zero_grad()
loss.backward()
optimizer.step()
##
print('[', ct, ']', loss.detach().item(), loss_sim.detach().item(),
loss_dif.detach().item())
############ save model #########
with torch.no_grad():
memory[lesson] = {'model': model.to('cpu').state_dict(),
'arch': ['Filter', ['para_block1']],
'centroid': centroid_sim.to('cpu')
}
return memory
def my_clip_evaluation(in_path, source, memory, in_base, types, dic, vocab):
with torch.no_grad():
# get vocab dictionary
if source == 'train':
dic = dic_test
else:
dic = dic_train
# get dataset
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device)
dt = MyDataset(in_path, source, in_base, types, dic, vocab,
clip_preprocessor=clip_preprocess)
data_loader = DataLoader(dt, batch_size=128, shuffle=True)
top3 = 0
top3_color = 0
top3_material = 0
top3_shape = 0
tot_num = 0
for base_is, images in data_loader:
# Prepare the inputs
images = images.to(device)
ans = []
batch_size_i = len(base_is)
# go through memory
for label in vocab:
if label not in memory.keys():
ans.append(torch.full((batch_size_i, 1), 1000.0).squeeze(1))
continue
# load model
model = CLIP_AE_Encode(hidden_dim_clip, latent_dim, isAE=False)
model.load_state_dict(memory[label]['model'])
model.to(device)
model.eval()
# load centroid
centroid_i = memory[label]['centroid'].to(device)
centroid_i = centroid_i.repeat(batch_size_i, 1)
# compute stats
z = model(clip_model, images).squeeze(0)
disi = ((z - centroid_i)**2).mean(dim=1)
ans.append(disi.detach().to('cpu'))
# get top3 incicies
ans = torch.stack(ans, dim=1)
values, indices = ans.topk(3, largest=False)
_, indices_lb = base_is.topk(3)
indices_lb, _ = torch.sort(indices_lb)
# calculate stats
tot_num += len(indices)
for bi in range(len(indices)):
ci = 0
mi = 0
si = 0
if indices_lb[bi][0] in indices[bi]:
ci = 1
if indices_lb[bi][1] in indices[bi]:
mi = 1
if indices_lb[bi][2] in indices[bi]:
si = 1
top3_color += ci
top3_material += mi
top3_shape += si
if (ci == 1) and (mi == 1) and (si == 1):
top3 += 1
print(tot_num, top3_color/tot_num, top3_material/tot_num,
top3_shape/tot_num, top3/tot_num)
return top3/tot_num
def memory_evaluation(in_path, source, memory, in_base, types, dic, vocab):
with torch.no_grad():
# get dataset
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device)
dt = MyDataset(in_path, source, in_base, types, dic, vocab,
clip_preprocessor=clip_preprocess)
data_loader = DataLoader(dt, batch_size=128, shuffle=True)
top3 = 0
top3_color = 0
top3_material = 0
top3_shape = 0
tot_num = 0
for base_is, images in data_loader:
# Prepare the inputs
images = images.to(device)
ans = []
batch_size_i = len(base_is)
# go through memory
for label in vocab:
if label not in memory.keys():
ans.append(torch.full((batch_size_i, 1), 1000.0).squeeze(1))
continue
# load model
model = CLIP_AE_Encode(hidden_dim_clip, latent_dim, isAE=False)
model.load_state_dict(memory[label]['model'])
model.to(device)
model.eval()
# load centroid
centroid_i = memory[label]['centroid'].to(device)
centroid_i = centroid_i.repeat(batch_size_i, 1)
# compute stats
z = model(clip_model, images).squeeze(0)
disi = ((z - centroid_i)**2).mean(dim=1)
ans.append(disi.detach().to('cpu'))
# get top3 incicies
ans = torch.stack(ans, dim=1)
values, indices = ans.topk(3, largest=False)
_, indices_lb = base_is.topk(3)
indices_lb, _ = torch.sort(indices_lb)
# calculate stats
tot_num += len(indices)
for bi in range(len(indices)):
ci = 0
mi = 0
si = 0
if indices_lb[bi][0] in indices[bi]:
ci = 1
if indices_lb[bi][1] in indices[bi]:
mi = 1
if indices_lb[bi][2] in indices[bi]:
si = 1
top3_color += ci
top3_material += mi
top3_shape += si
if (ci == 1) and (mi == 1) and (si == 1):
top3 += 1
return tot_num, top3/tot_num, top3_color/tot_num, top3_material/tot_num, top3_shape/tot_num
def my_clip_train(in_path, out_path, model_name, source, in_base,
types, dic, vocab, pre_trained_model=None):
# get data
clip_model, clip_preprocess = clip.load("ViT-B/32", device=device)
dt = MyDataset(in_path, source, in_base, types, dic, vocab,
clip_preprocessor=clip_preprocess)
# load encoder models from memory
memory = {}
if pre_trained_model is not None:
print(">>>>> loading memory >>>>>")
in_memory = os.path.join(out_path, pre_trained_model)
infile = open(in_memory, 'rb')
memory = pickle.load(infile)
infile.close()
best_nt = 0
t_tot = 0
for i in range(epochs):
for tl in types_learning: # attr
random.shuffle(dic[tl])
for vi in dic[tl]: # lesson
print("#################### Learning: " + str(i) + " ----- " + str(vi))
t_start = time.time()
memory = my_train_clip_encoder(dt, memory, tl, vi)
t_end = time.time()
t_dur = t_end - t_start
t_tot += t_dur
print("Time: ", t_dur, t_tot)
# evaluate
top_nt = my_clip_evaluation(in_path, 'novel_test/', memory,
bn_n_test, ['rgba'], dic_train, vocab)
if top_nt > best_nt:
best_nt = top_nt
print("++++++++++++++ BEST NT: " + str(best_nt))
with open(os.path.join(out_path, model_name), 'wb') as handle:
pickle.dump(memory, handle, protocol=pickle.HIGHEST_PROTOCOL)
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument('--in_path', '-i',
help='Data input path', required=True)
argparser.add_argument('--out_path', '-o',
help='Model memory output path', required=True)
argparser.add_argument('--pre_train', '-p', default=None,
help='Pretrained model import name (saved in outpath)', required=False)
args = argparser.parse_args()
evaluations = {
'mare_novel_comp': [],
'mare_var': []
}
# Train without 9 combs and 3 attrs
model_name = 'comparative_base_mem.pickle'
#my_clip_train(args.in_path, args.out_path, model_name,
# 'novel_train/', bn_n_train, ['rgba'], dic_train, vocabs, args.pre_train)
# Multi-Attribute Recognition Evaluation
in_memory = os.path.join(args.out_path, model_name)
infile = open(in_memory, 'rb')
memory = pickle.load(infile)
infile.close()
# evaluate with novel compositions
evaluations['mare_novel_comp'].append(memory_evaluation(args.in_path, 'novel_test/', memory,
bn_n_test, ['rgba'], dic_train, vocabs))
# evaluate with variations (dic_test)
evaluations['mare_var'].append(memory_evaluation(args.in_path, 'test/', memory,
bn_test, ['rgba'], dic_test, vocabs))
pprint(evaluations)
with open(os.path.join(args.out_path, 'evaluations.json'), 'w') as json_file:
json.dump(evaluations, json_file)
## Train for new word acquisition
## without
#model_name = 'nw_acquisition_old.pickle'
#my_clip_train(args.in_path, args.out_path, model_name,
# 'novel_train/', bsn_novel_train_1, ['rgba'], dic_train, vocabs, args.pre_train)
#
#in_memory = os.path.join(args.out_path, model_name)
#infile = open(in_memory, 'rb')
#memory = pickle.load(infile)
#infile.close()
#
#evaluations['mare_new_word_old_new_comp'].append(memory_evaluation(args.in_path, 'novel_test/', memory,
# bsn_novel_test_1, ['rgba'], dic_train, vocabs))
#
#evaluations['mare_new_word_old_new_comp_var'].append(memory_evaluation(args.in_path, 'novel_test/', memory,
# bsn_novel_test_1, ['rgba'], dic_test, vocabs))
#
## with new word
#model_name = 'nw_acquisition_new.pickle'
#my_clip_train(args.in_path, args.out_path, model_name,
# 'novel_train/', bsn_novel_train_2_old, ['rgba'], dic_train, vocabs, pre_train = 'nw_acquisition_old.pickle')
#
#in_memory = os.path.join(args.out_path, model_name)
#infile = open(in_memory, 'rb')
#memory = pickle.load(infile)
#infile.close()