-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaingnn.py
478 lines (438 loc) · 29.7 KB
/
maingnn.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
from dataset import UCMDataset, RSICDDataset, collate_fn_captions, collate_fn_classifier, collate_fn_full, augmented_collate_fn, collate_fn_waterfall
from models import CaptionGenerator, TripletClassifier, AugmentedCaptionGenerator, FinalModel, MultiHeadClassifier, FinetunedModel
from train import caption_trainer, classifier_trainer, augmented_caption_trainer, full_pipeline_trainer, enc_finetuning, waterfall_trainer
from eval import eval_captions, augmented_eval_captions, eval_classification, eval_pipeline, eval_waterfall
from graph_utils import save_plots, produce_graphs
import torch
import os
# Imports for waterfall pipeline
import pickle
from models import TextGenerator
def train_gnn(dataset, task, epochs, lr, batch_size, decoder, network_name, early_stopping, threshold, gnn, vir, depth, attributes, plot, combo, pil):
'''
Function that initialize the training for the gnn depending on the task and dataset
Args:
dataset (str): dataset used for training
task (str): type of desired task
epochs (int): number of training epochs
lr (float): learning rate to be used
batch_size (int): batch size used for training
decoder (str): decoder used for training
network_name (str): name of the file to which the network will be saved
early_stopping (bool): True if allow the use of early stopping; False otherwise
threshold (int): number of epochs after which early stopping activates
Return:
None
'''
if task == "tripl2caption":
# Dataset definition
if dataset == 'ucm':
train_filenames = 'dataset/UCM_dataset/filenames/filenames_train.txt'
val_filenames = 'dataset/UCM_dataset/filenames/filenames_val.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
train_dataset = UCMDataset(img_path, train_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='train', pil=pil)
val_dataset = UCMDataset(img_path, val_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='val', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
if attributes:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd_attributes.json'
else:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['imgid', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
train_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='train')
val_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='val')
# Network training part
feats_n = torch.Tensor(train_dataset.node_feats[list(train_dataset.node_feats.keys())[0]])[0].size(0)
max = train_dataset.max_capt_length
if val_dataset.max_capt_length>max:
max = val_dataset.max_capt_length
model = CaptionGenerator(feats_n, max, train_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
trainer = caption_trainer(model,train_dataset,val_dataset,collate_fn_captions, train_dataset.word2idx, max, network_name)
trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold)
elif task == "img2tripl":
if dataset == 'ucm':
train_filenames = 'dataset/UCM_dataset/filenames/filenames_train.txt'
val_filenames = 'dataset/UCM_dataset/filenames/filenames_val.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['image','triplets']
img_dim = 256
train_dataset = UCMDataset(img_path, train_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='train', pil=pil)
val_dataset = UCMDataset(img_path, val_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='val', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
if attributes:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd_attributes.json'
else:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['image','triplets']
img_dim = 224
train_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='train')
val_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='val')
# copied from main.py
model = MultiHeadClassifier(img_dim, len(train_dataset.triplet_to_idx))
trainer = classifier_trainer(model,train_dataset,val_dataset,collate_fn_classifier, network_name)
trainer.fit(epochs, lr, batch_size)
elif task == "augmented_tripl2caption":
if dataset == 'ucm':
train_filenames = 'dataset/UCM_dataset/filenames/filenames_train.txt'
val_filenames = 'dataset/UCM_dataset/filenames/filenames_val.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 256
train_dataset = UCMDataset(img_path, train_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='train', pil=pil)
val_dataset = UCMDataset(img_path, val_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='val', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
if attributes:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd_attributes.json'
else:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['imgid', 'image', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 224
train_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='train')
val_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='val')
feats_n = torch.Tensor(train_dataset.node_feats[list(train_dataset.node_feats.keys())[0]])[0].size(0)
max = train_dataset.max_capt_length
if val_dataset.max_capt_length>max:
max = val_dataset.max_capt_length
img_encoder = TripletClassifier(img_dim,len(train_dataset.triplet_to_idx))
model = AugmentedCaptionGenerator(img_encoder, feats_n, max, train_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
trainer = augmented_caption_trainer(model,train_dataset,val_dataset, augmented_collate_fn, train_dataset.word2idx, max, network_name)
trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold)
elif task == "img2caption":
if dataset == 'ucm':
train_filenames = 'dataset/UCM_dataset/filenames/filenames_train.txt'
val_filenames = 'dataset/UCM_dataset/filenames/filenames_val.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/filtered_triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/filtered_triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'triplets', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 256
train_dataset = UCMDataset(img_path, train_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='train', pil=pil)
val_dataset = UCMDataset(img_path, val_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='val', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
if attributes:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd_attributes.json'
else:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['imgid', 'image', 'triplets', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 224
train_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='train')
val_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='val')
feats_n = torch.Tensor(train_dataset.node_feats[list(train_dataset.node_feats.keys())[0]])[0].size(0)
max = train_dataset.max_capt_length
if val_dataset.max_capt_length>max:
max = val_dataset.max_capt_length
img_encoder = TripletClassifier(img_dim,len(train_dataset.triplet_to_idx), pil)
model = FinalModel(img_encoder, feats_n, max, train_dataset.word2idx, img_dim, train_dataset.triplet_to_idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder, pil=pil)
trainer = full_pipeline_trainer(model,train_dataset,val_dataset, collate_fn_full, train_dataset.word2idx, max, network_name, pil=pil)
if not plot:
trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold, plot=False, combo=combo)
else:
train_losses, val_losses = trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold, plot=True, combo=combo)
os.environ['KMP_DUPLICATE_LIB_OK']='True'
save_plots(train_losses, val_losses, epochs, combo, gnn, 'ci')
elif task == 'finetune':
if dataset == 'ucm':
train_filenames = 'dataset/UCM_dataset/filenames/filenames_train.txt'
val_filenames = 'dataset/UCM_dataset/filenames/filenames_val.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/filtered_triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/filtered_triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'triplets', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 256
train_dataset = UCMDataset(img_path, train_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='train', pil=pil)
val_dataset = UCMDataset(img_path, val_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='val', pil=pil)
feats_n = torch.Tensor(train_dataset.node_feats[list(train_dataset.node_feats.keys())[0]])[0].size(0)
max = train_dataset.max_capt_length
if val_dataset.max_capt_length>max:
max = val_dataset.max_capt_length
model = FinetunedModel(train_dataset.word2idx, img_dim, train_dataset.triplet_to_idx, 'decoder.pth')
trainer = enc_finetuning(model,train_dataset,val_dataset, collate_fn_full, train_dataset.word2idx, max, network_name)
trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold)
elif task == 'waterfall':
if pil:
if dataset == 'ucm':
train_filenames = 'dataset/UCM_dataset/filenames/filenames_train.txt'
val_filenames = 'dataset/UCM_dataset/filenames/filenames_val.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/filtered_triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/filtered_triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'triplets', 'captions']
img_dim = 256
train_dataset = UCMDataset(img_path, train_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='train', pil=pil)
val_dataset = UCMDataset(img_path, val_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='val', pil=pil)
else:
print("In this specific task, the dataset has not been implemented yet.")
exit(0)
# Here goes the first part of waterfall (so the caption production)
# 1. load the dictionaries for the captions
# Load the dictionaries
with open('dictionaries.pkl','rb') as file:
value_to_idx,idx_to_value = pickle.load(file)
# 2. Define the variables for the model (depend on the pre-training done on the model)
type = 'gru'
backbone = 'resnet152'
max_len = 30
k = 1 # Number of captions to be generated with beam search
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 3. Create the generator model
capt_gen = TextGenerator(len(value_to_idx.keys()),img_dim,type,backbone)
capt_gen.load_state_dict(torch.load(r'weights\ucm\textGenerator_GRU_resnet152.pt',map_location=device))
# 4. produce the captions and return the triplets
train_triplets = produce_graphs(capt_gen, idx_to_value, value_to_idx, max_len, k, device, train_dataset)
val_triplets = produce_graphs(capt_gen, idx_to_value, value_to_idx, max_len, k, device, val_dataset)
# 5. replace the old triplets with the new ones
train_dataset.triplets = train_triplets
val_dataset.triplets = val_triplets
print("Initializing the model...")
feats_n = torch.Tensor(train_dataset.node_feats[list(train_dataset.node_feats.keys())[0]])[0].size(0)
model = CaptionGenerator(feats_n, max_len, train_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
# img_encoder = TripletClassifier(img_dim,len(train_dataset.triplet_to_idx), pil)
# model = AugmentedCaptionGenerator(img_encoder, feats_n, max_len, train_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
trainer = waterfall_trainer(model,train_dataset,val_dataset, collate_fn_waterfall, train_dataset.word2idx, max_len, network_name)
print("Training the model...")
if not plot:
trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold, plot=False)
else:
train_losses, val_losses = trainer.fit(epochs, lr, batch_size, model._loss, early_stopping=early_stopping, tol_threshold=threshold, plot=True,)
os.environ['KMP_DUPLICATE_LIB_OK']='True'
save_plots(train_losses, val_losses, epochs, combo, gnn, 'waterfall')
else:
print("Need to use PIL images.")
else:
print("Task not yet implemented.")
def test_gnn(dataset, task, decoder, network_name, filename, gnn, vir, depth, attributes, pil):
'''
Function that initialize the training for the gnn depending on the task and dataset
Args:
dataset (str): dataset used for training
task (str): type of desired task
epochs (int): number of training epochs
lr (float): learning rate to be used
batch_size (int): batch size used for training
decoder (str): decoder used for training
network_name (str): name of the file to which the network will be saved
early_stopping (bool): True if allow the use of early stopping; False otherwise
threshold (int): number of epochs after which early stopping activates
Return:
None
'''
if task == "tripl2caption":
# Dataset definition
if dataset == 'ucm':
test_filenames = 'dataset/UCM_dataset/filenames/filenames_test.txt'
img_path = 'dataset/UCM_dataset/images/'
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
test_dataset = UCMDataset(img_path, test_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='test', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['imgid', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
test_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='test')
# Network training part
feats_n = torch.Tensor(test_dataset.node_feats[list(test_dataset.node_feats.keys())[0]])[0].size(0)
max = test_dataset.max_capt_length
model = CaptionGenerator(feats_n, max, test_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
model = torch.load(network_name)
eval_captions(test_dataset, model, filename)
elif task == "augmented_tripl2caption":
if dataset == 'ucm':
test_filenames = 'dataset/UCM_dataset/filenames/filenames_test.txt'
img_path = 'dataset/UCM_dataset/images/'
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 256
test_dataset = UCMDataset(img_path, test_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='test', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['imgid', 'image', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 224
test_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='test')
feats_n = torch.Tensor(test_dataset.node_feats[list(test_dataset.node_feats.keys())[0]])[0].size(0)
max = test_dataset.max_capt_length
img_encoder = TripletClassifier(img_dim,len(test_dataset.triplet_to_idx))
model = AugmentedCaptionGenerator(img_encoder, feats_n, max, test_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
model = torch.load(network_name)
augmented_eval_captions(test_dataset, model, filename)
elif task == "img2tripl":
# Dataset definition
if dataset == 'ucm':
test_filenames = 'dataset/UCM_dataset/filenames/filenames_test.txt'
img_path = 'dataset/UCM_dataset/images/'
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['image','triplets']
img_dim = 256
test_dataset = UCMDataset(img_path, test_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='test', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['image','triplets']
img_dim = 224
test_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='test')
model = TripletClassifier(img_dim,len(test_dataset.triplet_to_idx))
model = torch.load(network_name)
eval_classification(test_dataset, model, filename)
elif task == "img2caption":
if dataset == 'ucm':
test_filenames = 'dataset/UCM_dataset/filenames/filenames_test.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'triplets', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 256
test_dataset = UCMDataset(img_path, test_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='test', pil=pil)
if dataset == 'rsicd':
graph_path = 'dataset/RSICD_dataset/Graph_data'
word2idx_path = 'dataset/RSICD_dataset/caption_dict_RSICD.json'
anno_path = 'dataset/RSICD_dataset/polished_dataset.json'
img_path = 'dataset/RSICD_dataset/RSICD_images'
if attributes:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd_attributes.json'
else:
tripl_path = 'dataset/RSICD_dataset/triplets_rsicd.json'
return_k = ['imgid', 'image', 'triplets', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 224
test_dataset = RSICDDataset(img_path, graph_path, tripl_path, anno_path, word2idx_path, return_k, split='test')
feats_n = torch.Tensor(test_dataset.node_feats[list(test_dataset.node_feats.keys())[0]])[0].size(0)
max = test_dataset.max_capt_length
img_encoder = TripletClassifier(img_dim,len(test_dataset.triplet_to_idx))
model = FinalModel(img_encoder, feats_n, max, test_dataset.word2idx, img_dim, test_dataset.triplet_to_idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
model = torch.load(network_name)
eval_pipeline(test_dataset, model, filename, pil)
elif task == 'finetune':
if dataset == 'ucm':
test_filenames = 'dataset/UCM_dataset/filenames/filenames_test.txt'
img_path = 'dataset/UCM_dataset/images/'
if attributes:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm_attributes.json'
else:
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'triplets', 'src_ids', 'dst_ids', 'node_feats', 'captions', 'num_nodes']
img_dim = 256
test_dataset = UCMDataset(img_path, test_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='test', pil=pil)
feats_n = torch.Tensor(test_dataset.node_feats[list(test_dataset.node_feats.keys())[0]])[0].size(0)
max = test_dataset.max_capt_length
model = FinetunedModel(test_dataset.word2idx, img_dim, test_dataset.triplet_to_idx, 'decoder.pth')
model = torch.load(network_name)
eval_pipeline(test_dataset, model, filename)
elif task == 'waterfall':
if dataset == 'ucm':
test_filenames = 'dataset/UCM_dataset/filenames/filenames_test.txt'
img_path = 'dataset/UCM_dataset/images/'
polished_tripl_path = 'dataset/UCM_dataset/triplets_ucm.json'
anno_path = 'dataset/UCM_dataset/filenames/descriptions_UCM.txt'
word2idx_path = 'dataset/UCM_dataset/caption_dict_UCM.json'
graph_path = 'dataset/UCM_dataset/Graph_data'
return_k = ['imgid', 'image', 'triplets', 'captions']
img_dim = 256
test_dataset = UCMDataset(img_path, test_filenames, graph_path, polished_tripl_path, anno_path, word2idx_path, return_keys=return_k, split='test', pil=pil)
else:
print("In this specific task, the dataset has not been implemented yet.")
exit(0)
# Here goes the first part of waterfall (so the caption production)
# 1. load the dictionaries for the captions
# Load the dictionaries
with open('dictionaries.pkl','rb') as file:
value_to_idx,idx_to_value = pickle.load(file)
# 2. Define the variables for the model (depend on the pre-training done on the model)
type = 'gru'
backbone = 'resnet152'
max_len = 30
k = 1 # Number of captions to be generated with beam search
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 3. Create the generator model
capt_gen = TextGenerator(len(value_to_idx.keys()),img_dim,type,backbone)
capt_gen.load_state_dict(torch.load(r'weights\ucm\textGenerator_GRU_resnet152.pt',map_location=device))
# 4. produce the captions and return the triplets
test_triplets = produce_graphs(capt_gen, idx_to_value, value_to_idx, max_len, k, device, test_dataset)
# 5. replace the old triplets with the new ones
test_dataset.triplets = test_triplets
print("Initializing the model...")
feats_n = torch.Tensor(test_dataset.node_feats[list(test_dataset.node_feats.keys())[0]])[0].size(0)
model = CaptionGenerator(feats_n, max_len, test_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
# img_encoder = TripletClassifier(img_dim,len(test_dataset.triplet_to_idx), pil)
# model = AugmentedCaptionGenerator(img_encoder, feats_n, max_len, test_dataset.word2idx, gnn=gnn, vir=vir, depth=depth, decoder=decoder)
model = torch.load(network_name)
print("Beginning the evaluation...")
eval_waterfall(test_dataset, model, filename, pil)
else:
print("Task not yet implemented.")