-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_features_vgg16.py
145 lines (83 loc) · 3.23 KB
/
generate_features_vgg16.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
import json
import numpy as np
import glob, pickle, os
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision.transforms.functional as F
import torchvision.models as models
import skimage.io
os.environ['TORCH_HOME'] = '/share/corticalproc/spmaniya/VQA/checkpoint'
annotations = json.load( open('data/v2_mscoco_val2014_annotations.json', 'r') )
questions = json.load( open('data/v2_OpenEnded_mscoco_val2014_questions.json', 'r') )
image2question = {}
for mapping in questions['questions']:
image2question[ mapping['image_id'] ] = mapping['question']
image2answer = {}
for mapping in annotations['annotations']:
image2answer[ mapping['image_id'] ] = mapping['answers'][0]['answer']
images = glob.glob('data/val2014/*')
print('glob images done')
image2path = {}
for path in images:
index = int(path.split('.')[0][-6:])
image2path[index] = path
train = []
im2path = list(image2path.keys())
for i in range(len(list(image2path.keys()))):
image_id = im2path[i]
path = image2path[image_id]
question = image2question[image_id]
answer = image2answer[image_id]
if len(answer.split(' ')) == 1 :
train.append([path, question, answer])
img_transform = transforms.Compose([
transforms.Resize(( 224, 224 )),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
class VGG16(nn.Module):
def __init__(self):
super().__init__()
self.vgg16 = models.vgg16(pretrained=True)
self.layer1 = nn.Sequential(*list(self.vgg16.features.children()))
self.layer3 = nn.Sequential(*list(self.vgg16.classifier.children())[0:4])
def forward(self, x):
out = self.layer1(x)
out = out.view(out.size(0),-1)
out = self.layer3(out)
return out
vgg16 = VGG16()
if torch.cuda.is_available():
vgg16 = vgg16.cuda()
#vgg16 = torch.load('checkpoint/vgg16.pth')
print('Loaded VGG16 Model')
def get_feature(path, flip=False):
img = skimage.io.imread(path)
if flip: img = np.fliplr(img)
if(len(img.shape) == 2):
return False, False
img = F.to_pil_image(img)
img = img_transform(img)
img = img.unsqueeze(0)
img = img.cuda()
output = vgg16(img)
output = output.view(-1)
return output.cpu().detach().numpy(), True
print('LEN = ', len(train))
def save(start, end):
image_feature = []
for i in range(start, end):
print('%d / %d' % (i, len(train)))
path, question, answer = train[i]
f1, status = get_feature(path)
#f2, status = get_feature(path, flip=True)
if status:
image_feature.append( [ f1, path, question, answer ] )
else:
print('Gray-scale image : discarded')
with open('dumps/val_features_vgg16_'+str(start)+'_'+str(end)+'.pkl', 'wb') as f:
pickle.dump(image_feature, f)
print('\n----------------------------------------------------------------\n')
save(30000, len(train))