-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNNUtils.py
222 lines (175 loc) · 7.29 KB
/
DNNUtils.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
import torchvision.models as models;
import torchvision.transforms as transforms;
import torch;
import torch.nn as nn;
import torch.nn.parallel;
import platform;
from PIL import Image;
import pickle;
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu");
if platform.system() == 'Windows':
model_path = 'C:/codigos/Doutorado/ImageNet/imagenet/model.pth';
else:
model_path = '/root/deepLearning/doutorado/model/model.pth';
def preparaNetwork(num_classes = 200):
model_ft = models.vgg19_bn(pretrained=True);
for param in model_ft.parameters():
param.requires_grad = False;
n_inputs = model_ft.classifier[6].in_features;
model_ft.classifier[6] = nn.Sequential(
nn.Linear(n_inputs, num_classes),
nn.LogSoftmax(dim=1)
);
model_ft = model_ft.to(device);
return model_ft;
def prepareNetwork2(tipo=3):
if tipo in [3, 4, 6]:
if tipo == 3:
encoder = models.inception_v3(pretrained = True);
elif tipo == 4:
encoder = models.resnext101_32x8d(pretrained = True);
elif tipo == 6:
encoder = models.resnet152(pretrained = True);
encoder.fc = nn.Sequential();
elif tipo == 5:
encoder = models.densenet161(pretrained = True);
encoder.classifier = nn.Sequential();
encoder.eval();
for param in encoder.parameters():
param.requires_grad = False;
encoder = encoder.cuda();
return encoder;
def loadModel():
fname = model_path;
model_ft = preparaNetwork();
model_ft.load_state_dict(torch.load(fname));
model_ft.eval();
return model_ft;
def loadModel2():
model_ft = models.vgg19_bn(pretrained=True);
for param in model_ft.parameters():
param.requires_grad = False;
model_ft = model_ft.to(device);
model_ft.eval();
return model_ft;
def loadModel3():
import pretrainedmodels;
model_name = 'inceptionv4' # could be fbresnet152 or inceptionresnetv2
model = pretrainedmodels.__dict__[model_name](num_classes=1000, pretrained='imagenet');
for param in model.parameters():
param.requires_grad = False;
model.eval();
model.cuda();
return model;
def makeCoders(layerIndex=-3):
classification = loadModel();
encoder = loadModel();
if layerIndex == 0:
encoder.classifier = nn.Identity();
decoder = nn.Sequential(*list(classification.classifier.children()));
else:
encoder.classifier = nn.Sequential(*list(encoder.classifier.children())[:layerIndex]);
decoder = nn.Sequential(*list(classification.classifier.children())[layerIndex:]);
return classification, encoder, decoder;
def makeCoders2(layerIndex=-3):
classification = loadModel2();
encoder = loadModel2();
encoder.classifier = nn.Sequential(*list(encoder.classifier.children())[:layerIndex]);
decoder = nn.Sequential(*list(classification.classifier.children())[layerIndex:]);
return classification, encoder, decoder;
def makeCoders3(layerIndex=-3):
classification = loadModel2();
encoder = loadModel2();
encoder.classifier = nn.Identity();
decoder = None;
return classification, encoder, decoder;
def makeCoders4(layerIndex=-3):
classification = None;
encoder = loadModel3();
encoder.last_linear = nn.Identity();
decoder = None;
return classification, encoder, decoder;
def transformFile(transform, arq):
img = pil_loader(arq);
sample = transform(img);
sample = sample.unsqueeze(0).cuda();
return sample;
def transformFile2(load_img, tf_img, arquivo):
input_img = load_img(arquivo);
input_tensor = tf_img(input_img); # 3x400x225 -> 3x299x299 size may differ
input_tensor = input_tensor.unsqueeze(0) # 3x299x299 -> 1x3x299x299
input_vector = torch.autograd.Variable(input_tensor,requires_grad=False).cuda();
return input_vector;
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
img = Image.open(f);
return img.convert('RGB');
def createTransform(tipo=1):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
if tipo in [1,2,4,5,6]:
transform = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize]);
elif tipo == 3:
transform = transforms.Compose([transforms.Resize(299),
transforms.CenterCrop(299),
transforms.ToTensor(),
normalize]);
return transform;
def createFeatures(encoder, tipo, img_dir, lbl_train, lbl_test):
transform = createTransform(tipo);
train_dir = '%s/natural-image_training' % img_dir;
featuresTrain = [];
for i in range(len(lbl_train)):
file = '%s/%s' % (train_dir, lbl_train[i]);
sample = transformFile(transform, file);
featuresTrain.append(encoder(sample));
test_dir = '%s/natural-image_test' % img_dir;
featuresTest = [];
for i in range(len(lbl_test)):
file = '%s/%s' % (test_dir, lbl_test[i]);
sample = transformFile(transform, file);
featuresTest.append(encoder(sample));
return featuresTrain, featuresTest;
def createFeaturesFromDisk(encoder, tipo, img_dir, lbl_train, lbl_test):
with open('savedFeatures/type%d/featuresType%d.pkl' % (tipo, tipo), 'rb') as f:
featuresDict = pickle.load(f);
featuresTrain = [];
for i in range(len(lbl_train)):
featuresTrain.append(torch.from_numpy(featuresDict[lbl_train[i]]).cuda());
featuresTest = [];
for i in range(len(lbl_test)):
featuresTest.append(torch.from_numpy(featuresDict[lbl_test[i]]).cuda());
return featuresTrain, featuresTest;
def createFeatures2(encoder, tipo, img_dir, lbl_train, lbl_test):
transform = createTransform(tipo);
featuresTrain = [];
for i in range(len(lbl_train)):
file = '%s/%s' % (img_dir, lbl_train[i]);
sample = transformFile(transform, file);
featuresTrain.append(encoder(sample));
featuresTest = [];
for i in range(len(lbl_test)):
file = '%s/%s' % (img_dir, lbl_test[i]);
sample = transformFile(transform, file);
featuresTest.append(encoder(sample));
return featuresTrain, featuresTest;
def createFeatures3(encoder, img_dir, lbl_train, lbl_test):
import pretrainedmodels.utils as utils;
load_img = utils.LoadImage();
tf_img = utils.TransformImage(encoder);
train_dir = '%s/natural-image_training' % img_dir;
featuresTrain = [];
for i in range(len(lbl_train)):
file = '%s/%s' % (train_dir, lbl_train[i]);
sample = transformFile2(load_img, tf_img, file);
featuresTrain.append(encoder(sample));
test_dir = '%s/natural-image_test' % img_dir;
featuresTest = [];
for i in range(len(lbl_test)):
file = '%s/%s' % (test_dir, lbl_test[i]);
sample = transformFile2(load_img, tf_img, file);
featuresTest.append(encoder(sample));
return featuresTrain, featuresTest;