-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathAttrPreModelRes18_256V0.py
110 lines (103 loc) · 3.27 KB
/
AttrPreModelRes18_256V0.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
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.models as models
import torch.nn.functional as F
class FeatureExtraction(torch.nn.Module):
def __init__(self):
super(FeatureExtraction, self).__init__()
self.resnet = models.resnet18(pretrained=True)
#self.resnet = models.resnet34(pretrained=False)
self.resnet = nn.Sequential(*list(self.resnet.children())[:-1])
# freeze parameters
#for param in self.vgg.parameters():
# param.requires_grad = False
# move to GPU
self.resnet.cuda()
def forward(self, image_batch):
return self.resnet(image_batch)
class Classifier(nn.Module):
def __init__(self, output_dim=1):
super(Classifier, self).__init__()
self.fc1 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, output_dim),
)
self.fc1.cuda()
self.fc2 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, output_dim),
)
self.fc2.cuda()
self.fc3 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, output_dim),
)
self.fc3.cuda()
self.fc4 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, output_dim),
)
self.fc4.cuda()
self.fc5 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, output_dim),
)
self.fc5.cuda()
self.fc6 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 128),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(128, output_dim),
)
self.fc6.cuda()
def forward(self, x):
x = x.view(x.size(0), -1) # flatten
#print(x)
x1 = self.fc1(x)
x2 = self.fc2(x)
x3 = self.fc3(x)
x4 = self.fc4(x)
x5 = self.fc5(x)
x6 = self.fc6(x)
return x1, x2, x3, x4, x5, x6
class AttrPre(nn.Module):
def __init__(self):
super(AttrPre, self).__init__()
self.FeatureExtraction = FeatureExtraction()
output_dim = 1
self.classifier = Classifier(output_dim)
def forward(self, img):
# do feature extraction
feature = self.FeatureExtraction(img)
Attractive, EyeGlasses, Male, MouthOpen, Smiling, Young = self.classifier(feature)
return Attractive, EyeGlasses, Male, MouthOpen, Smiling, Young