@@ -37,41 +37,48 @@ def compute_score_with_logits(logits, labels):
37
37
def evaluate_attention_model (args ):
38
38
39
39
class_data = pd .read_csv (args .class_metadata_file )
40
- class_label_map = {0 :"no" ,1 :"yes" }
40
+ # class_label_map={0:"no",1:"yes"}
41
41
42
- # class_label_map=class_data['Label_names'].tolist()
42
+ class_label_map = class_data ['Label_names' ].tolist ()
43
43
44
44
print ('Loading model checkpoint' )
45
45
attention_model_checkpoint = torch .load (args .model_path )
46
-
47
46
new_state_dict = OrderedDict ()
48
47
for k , v in attention_model_checkpoint .items ():
49
48
name = k [7 :] # remove `module.`
50
49
new_state_dict [name ] = v
51
50
print ('Model checkpoint loaded' )
51
+ #new_state_dict["classifier.main.2.bias"]=new_state_dict.pop("classifier.main.3.bias")
52
+ #new_state_dict["classifier.main.2.weight_g"]=new_state_dict.pop("classifier.main.3.weight_g")
53
+ #new_state_dict["classifier.main.2.weight_v"]=new_state_dict.pop("classifier.main.3.weight_v")
52
54
53
55
print (new_state_dict .keys ())
54
56
print ('Loading Dictionary' )
55
57
dictionary = Dictionary .load_from_file (args .pickle_path )
56
58
57
59
train_dataset = Dataset_VQA (img_root_dir = args .image_root_dir ,feats_data_path = args .feats_data_path ,dictionary = dictionary ,choice = 'train' ,dataroot = args .data_root ,arch_choice = args .arch_choice ,layer_option = args .layer_option )
58
60
print ('Loading the attention model' )
59
- attention_model = attention_mfh (train_dataset , num_hid = args .num_hid , dropout = args .dropout , norm = args .norm ,\
61
+ attention_model = attention_baseline (train_dataset , num_hid = args .num_hid , dropout = args .dropout , norm = args .norm ,\
60
62
activation = args .activation , drop_L = args .dropout_L , drop_G = args .dropout_G ,\
61
- drop_W = args .dropout_W , drop_C = args .dropout_C ,mfb_out_dim = args .mfb_out_dim )
63
+ drop_W = args .dropout_W , drop_C = args .dropout_C )
64
+
65
+ #attention_model=attention_mfh(train_dataset, num_hid=args.num_hid, dropout= args.dropout, norm=args.norm,\
66
+ #activation=args.activation, drop_L=args.dropout_L, drop_G=args.dropout_G,\
67
+ #drop_W=args.dropout_W, drop_C=args.dropout_C,mfb_out_dim=args.mfb_out_dim)
62
68
attention_model .load_state_dict (new_state_dict )
63
69
attention_model .eval ()
64
70
65
71
torch .manual_seed (args .seed )
66
- torch .cuda .manual_seed (args .seed )
72
+ torch .cuda .manual_seed_all (args .seed )
73
+ #torch.cuda.manual_seed(args.seed)
67
74
torch .cuda .set_device (args .device )
68
75
attention_model .to (args .device )
69
76
if (args .image_model is None ):
70
77
"""use extracted features as a Dataset and Dataloader
71
78
"""
72
79
print ('Using validation features' )
73
- dataset_temp = Dataset_VQA (img_root_dir = args .image_root_dir ,feats_data_path = args .feats_data_path ,dictionary = dictionary ,bert_option = args .bert_option ,rcnn_pkl_path = args . rcnn_path ,choice = args .choice ,dataroot = args .data_root ,arch_choice = args .arch_choice ,layer_option = args .layer_option )
74
- loader = DataLoader (dataset_temp , batch_size = args .batch_size , shuffle = False , num_workers = 10 )
80
+ dataset_temp = Dataset_VQA (img_root_dir = args .image_root_dir ,feats_data_path = args .feats_data_path ,dictionary = dictionary ,bert_option = args .bert_option ,rcnn_pkl_path = None ,choice = args .choice ,dataroot = args .data_root ,arch_choice = args .arch_choice ,layer_option = args .layer_option )
81
+ loader = DataLoader (dataset_temp , batch_size = args .batch_size , shuffle = False , num_workers = 1 )
75
82
print ('Length of validation dataloader:' , len (loader ))
76
83
upper_bound = 0
77
84
num_data = 0
@@ -82,25 +89,26 @@ def evaluate_attention_model(args):
82
89
predicted_class_labels = []
83
90
question_set = []
84
91
question_id = []
85
- for data in tqdm (loader ):
86
-
87
- feat ,quest ,quest_sent ,quest_id ,target = data
88
- feat = feat .to (args .device )
89
- quest = quest .to (args .device )
90
- target = target .to (args .device )
91
-
92
- question_id = question_id + quest_id .tolist ()
93
- pred = attention_model (feat , quest , target )
94
- question_set = question_set + list (quest_sent )
95
- loss = instance_bce_with_logits (pred , target )
96
- V_loss += loss .item () * feat .size (0 )
97
- score_temp , logits , class_labels = compute_score_with_logits (pred , target .data )
98
- actual_class_labels = actual_class_labels + list (class_labels .cpu ().numpy ())
99
- predicted_class_labels = predicted_class_labels + list (logits .cpu ().numpy ())
100
- batch_score = score_temp .sum ()
101
- score += batch_score
102
- upper_bound += (target .max (1 )[0 ]).sum ()
103
- num_data += pred .size (0 )
92
+ count = 0
93
+ for data in tqdm (loader ):
94
+ feat ,quest ,quest_sent ,quest_id ,target = data
95
+ feat = feat .to (args .device )
96
+ quest = quest .to (args .device )
97
+ target = target .to (args .device )
98
+
99
+ question_id = question_id + quest_id .tolist ()
100
+ pred = attention_model (feat , quest )
101
+ question_set = question_set + list (quest_sent )
102
+ loss = instance_bce_with_logits (pred , target )
103
+ V_loss += loss .item () * feat .size (0 )
104
+ score_temp , logits , class_labels = compute_score_with_logits (pred , target .data )
105
+ actual_class_labels = actual_class_labels + list (class_labels .cpu ().numpy ())
106
+ predicted_class_labels = predicted_class_labels + list (logits .cpu ().numpy ())
107
+ batch_score = score_temp .sum ()
108
+ score += batch_score
109
+ upper_bound += (target .max (1 )[0 ]).sum ()
110
+ num_data += pred .size (0 )
111
+ #count=count+1
104
112
105
113
106
114
@@ -112,10 +120,10 @@ def evaluate_attention_model(args):
112
120
for index ,val in tqdm (enumerate (question_id )):
113
121
temp = {"answer" :class_predicted_name [index ],"question_id" :val }
114
122
list_set .append (temp )
115
- with open ('validation_results .json' , 'w' ) as fout :
123
+ with open ('validation_results_resnet_152_attention_baseline_num_hid_512_batch_size_512 .json' , 'w' ) as fout :
116
124
json .dump (list_set , fout )
117
- # predicted_df=pd.DataFrame({'Questions':question_set,'Actual_Answers':class_actual_name,'Predicted_Answers':class_predicted_name})
118
- # predicted_df.to_csv('Validation_Stats .csv')
125
+ predicted_df = pd .DataFrame ({'Question_id' : question_id , 'Questions' :question_set ,'Actual_Answers' :class_actual_name ,'Predicted_Answers' :class_predicted_name })
126
+ predicted_df .to_csv ('Validation_Stats_resnet_152_attention_baseline_num_hid_512_batch_size_512 .csv' )
119
127
score = score / len (loader .dataset )
120
128
V_loss /= len (loader .dataset )
121
129
upper_bound = upper_bound / len (loader .dataset )
@@ -136,10 +144,10 @@ def evaluate_attention_model(args):
136
144
parser .add_argument ('--feats_data_path' , type = str , default = "/data/digbose92/VQA/COCO/train_hdf5_COCO/" )
137
145
parser .add_argument ('--data_root' , type = str , default = "/proj/digbose92/VQA/VisualQuestion_VQA/common_resources" )
138
146
parser .add_argument ('--npy_file' , type = str , default = "../../VisualQuestion_VQA/Visual_All/data/glove6b_init_300d.npy" )
139
- parser .add_argument ('--model_path' , type = str , default = "results_GRU_uni/results_rcnn_hid_1280_mfh_YES_NO_ADAM /model.pth" )
147
+ parser .add_argument ('--model_path' , type = str , default = "results_GRU_uni/results_resnet_152_hid_512_YES_NO_ADAM /model.pth" )
140
148
parser .add_argument ('--image_model' , type = str , default = None )
141
- parser .add_argument ('--batch_size' , type = int , default = 32 )
142
- parser .add_argument ('--num_hid' , type = int , default = 1280 ) # they used 1024
149
+ parser .add_argument ('--batch_size' , type = int , default = 512 )
150
+ parser .add_argument ('--num_hid' , type = int , default = 512 ) # they used 1024
143
151
parser .add_argument ('--dropout' , type = float , default = 0.3 )
144
152
parser .add_argument ('--dropout_L' , type = float , default = 0.1 )
145
153
parser .add_argument ('--dropout_G' , type = float , default = 0.2 )
0 commit comments