forked from WynMew/Age-Gender-Predication
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgePreModelV1.py
54 lines (47 loc) · 1.54 KB
/
AgePreModelV1.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
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.vgg = models.vgg16(pretrained=True)
self.vgg = nn.Sequential(*list(self.vgg.features.children())[:])
# 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 Classifier(nn.Module):
def __init__(self, output_dim=100):
super(Classifier, self).__init__()
self.fc = nn.Sequential(
nn.Linear(8192, 4096),
nn.ReLU(True),
nn.Dropout(p = 0.5),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(p = 0.5),
nn.Linear(4096, output_dim),
)
self.fc.cuda()
def forward(self, x):
x = x.view(x.size(0), -1) # flatten
#print(x)
x = self.fc(x)
return x
class AgePre(nn.Module):
def __init__(self):
super(AgePre, 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 = self.classifier(feature)
return Age