-
Notifications
You must be signed in to change notification settings - Fork 1
/
AgeGPreModelResNet34_256.py
66 lines (59 loc) · 1.93 KB
/
AgeGPreModelResNet34_256.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
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.resnet34(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=100):
super(Classifier, self).__init__()
self.fc1 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, output_dim),
)
self.fc1.cuda()
self.fc2 = nn.Sequential(
nn.Linear(2048, 512),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(512, 48),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(48, 1),
)
self.fc2.cuda()
def forward(self, x):
x = x.view(x.size(0), -1) # flatten
#print(x)
x1 = self.fc1(x)
x2 = self.fc2(x)
return x1, x2
class AgeGPre(nn.Module):
def __init__(self):
super(AgeGPre, self).__init__()
self.FeatureExtraction = FeatureExtraction()
output_dim = 100
self.classifier = Classifier(output_dim)
def forward(self, img):
# do feature extraction
feature = self.FeatureExtraction(img)
Age, Gender = self.classifier(feature)
return Age, Gender