-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg16_extractor.py
67 lines (51 loc) · 1.92 KB
/
vgg16_extractor.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
import pretrainedmodels
import torch.nn as nn
from pretrainedmodels import utils
from file_path_manager import FilePathManager
class Vgg16Extractor:
def __init__(self, regions_count=49, use_gpu: bool = True, transform: bool = True):
super().__init__()
print('USING VGG16')
self.cnn = pretrainedmodels.vgg16()
self.regions_count = regions_count
self.regions_features_size = 512
self.features_size = 4096
if regions_count == 49:
self.regions = self.cnn._features
else:
self.regions = nn.Sequential(*(self.cnn._features[:-2]))
self.regions_out = nn.Sequential(*(self.cnn._features[-2:]))
self.tf_image = utils.TransformImage(self.cnn)
self.transform = transform
self.use_gpu = use_gpu
if self.use_gpu:
self.cnn = self.cnn.cuda()
self.cnn.eval()
for param in self.cnn.parameters():
param.requires_grad = False
def forward(self, image):
if self.transform:
image = self.tf_image(image)
if len(image.size()) == 3:
image = image.unsqueeze(0)
if self.use_gpu:
image = image.cuda()
regions = self.regions(image)
feat = regions
if self.regions_count == 196:
feat = self.regions_out(regions)
x = feat.view(feat.size(0), -1)
x = self.cnn.linear0(x)
x = self.cnn.relu0(x)
x = self.cnn.dropout0(x)
features = self.cnn.linear1(x)
return features, regions.view(regions.size(0), self.regions_count, regions.size(1))
def __call__(self, image):
return self.forward(image)
if __name__ == '__main__':
extractor = Vgg16Extractor()
load_img = utils.LoadImage()
image_path = FilePathManager.resolve("misc/images/airplane.jpg")
feat, reg = extractor.forward(load_img(image_path))
print(feat.shape)
print(reg.shape)