-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_extractor.py
78 lines (60 loc) · 2.32 KB
/
feature_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
68
69
70
71
72
73
74
75
76
77
78
import torch
import torch.nn as nn
import numpy as np
import torchvision
from torchvision.models import resnet18
def get_name_to_module(model):
name_to_module = {}
for m in model.named_modules():
name_to_module[m[0]] = m[1]
return name_to_module
def get_activation(all_outputs, name):
def hook(model, input, output):
all_outputs[name] = output.detach()
return hook
def add_hooks(model, outputs, output_layer_names):
"""
:param model:
:param outputs: Outputs from layers specified in `output_layer_names` will be stored in `output` variable
:param output_layer_names:
:return:
"""
name_to_module = get_name_to_module(model)
for output_layer_name in output_layer_names:
name_to_module[output_layer_name].register_forward_hook(get_activation(outputs, output_layer_name))
class ModelWrapper(nn.Module):
def __init__(self, model, output_layer_names, return_single=True):
super(ModelWrapper, self).__init__()
self.model = model
self.output_layer_names = output_layer_names
self.outputs = {}
self.return_single = return_single
add_hooks(self.model, self.outputs, self.output_layer_names)
def forward(self, images):
self.model(images)
output_vals = [self.outputs[output_layer_name] for output_layer_name in self.output_layer_names]
if self.return_single:
return output_vals[0]
else:
return output_vals
class BBResNet18(object):
def __init__(self):
self.model = resnet18(pretrained=True)
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.model.eval()
self.model = ModelWrapper(self.model, ['avgpool'], True)
self.model.eval()
self.model.to(self.device)
def feature_extraction(self, x:np.ndarray):
'''
param:
x: numpy ndarray of shape: [None, 3, 224, 224] and dtype: np.float32
return:
numpy ndarray (feature vector) of shape: [None, 512] and dtype: np.float32
'''
x = torch.from_numpy(x).to(self.device)
with torch.no_grad():
out = self.model(x).cpu().detach()
out = out.view(out.size(0), -1)
out = out.numpy()
return out