forked from WynMew/Age-Gender-Predication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgePreModel.py
57 lines (50 loc) · 1.77 KB
/
AgePreModel.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
from __future__ import print_function, division
import torch
import torch.nn as nn
from torch.autograd import Variable
import torchvision.models as models
class FeatureExtraction(torch.nn.Module):
def __init__(self):
super(FeatureExtraction, self).__init__()
self.vgg = models.vgg16(pretrained=True)
# keep feature extraction network up to pool4 (last layer - 7)
self.vgg = nn.Sequential(*list(self.vgg.features.children())[:-7])
# freeze parameters
for param in self.vgg.parameters():
param.requires_grad = False
# move to GPU
self.vgg.cuda()
def forward(self, image_batch):
return self.vgg(image_batch)
class FeatureRegression(nn.Module):
def __init__(self, output_dim=100):
super(FeatureRegression, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(512, 128, kernel_size=3, padding=0),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 64, kernel_size=3, padding=0),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
)
self.linear = nn.Linear(1024, output_dim)
self.conv.cuda()
self.linear.cuda()
def forward(self, x):
x = self.conv(x)
x = x.view(x.size(0), -1)
#print(x)
x = self.linear(x)
return x
class AgePre(nn.Module):
def __init__(self):
super(AgePre, self).__init__()
self.FeatureExtraction = FeatureExtraction()
output_dim = 100
self.FeatureRegression = FeatureRegression(output_dim)
self.ReLU = nn.ReLU(inplace=True)
def forward(self, img):
# do feature extraction
feature = self.FeatureExtraction(img)
Age = self.FeatureRegression(feature)
return Age