-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectclassifier.py
66 lines (62 loc) · 2.51 KB
/
selectclassifier.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
import torch
from torch import nn
from collections import OrderedDict
from torchvision import datasets, transforms, models
#selection between vgg13 and vgg16
def selectclassifier(model, hidden_layer):
if (hidden_layer is None):
hl = 0
else:
hl = int(hidden_layer)
if (model == 'vgg13'):
model = models.vgg13(pretrained=True)
for param in model.parameters():
param.requires_grad=False
neuralinput = 25088
neuraloutput = 102
if (hl <= neuralinput and hl >= neuraloutput):
classifier = nn.Sequential(OrderedDict([
('input_hidden', nn.Linear(neuralinput,hl)),
('Activation_ReLU', nn.ReLU()),
('Dropout', nn.Dropout(0.2)),
('hidden_output', nn.Linear(hl,neuraloutput)),
('output', nn.LogSoftmax(dim=1))]))
model.classifier = classifier
print(model)
return model
else:
classifier = nn.Sequential(OrderedDict([
('input_hidden', nn.Linear(neuralinput,4096)),
('Activation_ReLU', nn.ReLU()),
('Dropout', nn.Dropout(0.2)),
('hidden_output', nn.Linear(4096,neuraloutput)),
('output', nn.LogSoftmax(dim=1))]))
model.classifier = classifier
print(model)
return model
if (model == 'vgg16'):
model = models.vgg16(pretrained=True)
for param in model.parameters():
param.requires_grad=False
neuralinput = 25088
neuraloutput = 102
if (hl <= neuralinput and hl >= neuraloutput):
classifier = nn.Sequential(OrderedDict([
('input_hidden', nn.Linear(neuralinput,hl)),
('Activation_ReLU', nn.ReLU()),
('Dropout', nn.Dropout(0.2)),
('hidden_output', nn.Linear(hl,neuraloutput)),
('output', nn.LogSoftmax(dim=1))]))
model.classifier = classifier
print(model)
return model
else:
classifier = nn.Sequential(OrderedDict([
('input_hidden', nn.Linear(neuralinput,4096)),
('Activation_ReLU', nn.ReLU()),
('Dropout', nn.Dropout(0.2)),
('hidden_output', nn.Linear(4096,neuraloutput)),
('output', nn.LogSoftmax(dim=1))]))
model.classifier = classifier
print(model)
return model