Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 4aa2850

Browse files
committed
first commit
0 parents  commit 4aa2850

14 files changed

+859
-0
lines changed

.gitignore

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
data/*.mp4
2+
# Byte-compiled / optimized / DLL files
3+
**/__pycache__/
4+
*.py[cod]
5+
*$py.class
6+
.vscode/
7+
# C extensions
8+
*.so
9+
10+
# Distribution / packaging
11+
.Python
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*.cover
48+
.hypothesis/
49+
.pytest_cache/
50+
51+
# Translations
52+
*.mo
53+
*.pot
54+
55+
# Django stuff:
56+
*.log
57+
local_settings.py
58+
db.sqlite3
59+
60+
# Flask stuff:
61+
instance/
62+
.webassets-cache
63+
64+
# Scrapy stuff:
65+
.scrapy
66+
67+
# Sphinx documentation
68+
docs/_build/
69+
70+
# PyBuilder
71+
target/
72+
73+
# Jupyter Notebook
74+
**/.ipynb_checkpoints
75+
76+
# pyenv
77+
.python-version
78+
79+
# celery beat schedule file
80+
celerybeat-schedule
81+
82+
# SageMath parsed files
83+
*.sage.py
84+
85+
# Environments
86+
.env
87+
.venv
88+
env/
89+
venv/
90+
ENV/
91+
env.bak/
92+
venv.bak/
93+
94+
# Spyder project settings
95+
.spyderproject
96+
.spyproject
97+
98+
# Rope project settings
99+
.ropeproject
100+
101+
# mkdocs documentation
102+
/site
103+
104+
# mypy
105+
.mypy_cache/

data/README.md

Whitespace-only changes.

data/make_db.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import argparse
2+
import os
3+
import numpy as np
4+
from scipy.io import loadmat

model/inceptionv3.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
from keras.models import Model
3+
from keras.layers import Dense, GlobalAveragePooling2D
4+
from keras.applications.inception_v3 import InceptionV3
5+
from keras.utils import plot_model
6+
7+
class AgenderNetInceptionV3(Model):
8+
def __init__(self):
9+
self.input_size = 140
10+
base = InceptionV3(
11+
input_shape=(140,140,3),
12+
include_top=False,
13+
weights='weight/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5')
14+
top_layer = GlobalAveragePooling2D(name='avg_pool')(base.output)
15+
gender_layer = Dense(2, activation='softmax', name='gender_prediction')(top_layer)
16+
age_layer = Dense(101, activation='softmax', name='age_prediction')(top_layer)
17+
super().__init__(inputs=base.input, outputs=[gender_layer, age_layer], name='AgenderNetInceptionV3')
18+
19+
def prep_phase1(self):
20+
for layer in self.layers[:311]:
21+
layer.trainable = False
22+
23+
def prep_phase2(self):
24+
for layer in self.layers[165:]:
25+
layer.trainable = True
26+
27+
@staticmethod
28+
def decode_prediction(prediction):
29+
gender_predicted = np.argmax(prediction[0], axis=1)
30+
age_predicted = prediction[1].dot(np.arange(0, 101).reshape(101, 1)).flatten()
31+
return gender_predicted, age_predicted
32+
33+
@staticmethod
34+
def prep_image(data):
35+
data = data.astype('float16')
36+
data /= 127.5
37+
data -= 1.
38+
return data
39+
40+
if __name__ == '__main__':
41+
model = AgenderNetInceptionV3()
42+
print(model.summary())

model/mobilenetv2.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import numpy as np
2+
from keras.models import Model
3+
from keras.layers import Dense, GlobalAveragePooling2D
4+
from keras.applications.mobilenetv2 import MobileNetV2
5+
from keras.utils import plot_model
6+
7+
class AgenderNetMobileNetV2(Model):
8+
def __init__(self):
9+
self.input_size = 96
10+
base = MobileNetV2(
11+
input_shape=(96,96,3),
12+
include_top=False,
13+
weights=None)
14+
top_layer = GlobalAveragePooling2D()(base.output)
15+
gender_layer = Dense(2, activation='softmax', name='gender_prediction')(top_layer)
16+
age_layer = Dense(101, activation='softmax', name='age_prediction')(top_layer)
17+
super().__init__(inputs=base.input, outputs=[gender_layer, age_layer], name='AgenderNetMobileNetV2')
18+
19+
def prep_phase1(self):
20+
for layer in self.layers[:132]:
21+
layer.trainable = False
22+
23+
def prep_phase2(self):
24+
for layer in self.layers[78:]:
25+
layer.trainable = True
26+
27+
@staticmethod
28+
def decode_prediction(prediction):
29+
gender_predicted = np.argmax(prediction[0], axis=1)
30+
age_predicted = prediction[1].dot(np.arange(0, 101).reshape(101, 1)).flatten()
31+
return gender_predicted, age_predicted
32+
33+
@staticmethod
34+
def prep_image(data):
35+
data = data.astype('float16')
36+
data /= 128.
37+
data -= 1.
38+
return data
39+
40+
if __name__ == '__main__':
41+
model = AgenderNetMobileNetV2()
42+
print(model.summary())
8.73 MB
Binary file not shown.

model/ssrnet.py

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import numpy as np
2+
from keras.layers import Dense, Flatten, Dropout, GlobalAveragePooling2D, Input, Conv2D
3+
from keras.layers import Activation, Multiply, Lambda, AveragePooling2D, MaxPooling2D, BatchNormalization
4+
from keras.models import Model
5+
from keras.utils import plot_model
6+
from keras import backend as K
7+
8+
class AgenderSSRNet(Model):
9+
def __init__(self, image_size,stage_num,lambda_local,lambda_d):
10+
self.input_size = image_size
11+
if K.image_dim_ordering() == "th":
12+
self.__channel_axis = 1
13+
self.__input_shape = (3, image_size, image_size)
14+
else:
15+
self.__channel_axis = -1
16+
self.__input_shape = (image_size, image_size, 3)
17+
18+
self.__stage_num = stage_num
19+
self.__lambda_local = lambda_local
20+
self.__lambda_d = lambda_d
21+
22+
self.__x_layer1 = None
23+
self.__x_layer2 = None
24+
self.__x_layer3 = None
25+
self.__x = None
26+
27+
self.__s_layer1 = None
28+
self.__s_layer2 = None
29+
self.__s_layer3 = None
30+
self.__s = None
31+
32+
inputs = Input(shape=self.__input_shape)
33+
self.__extraction_block(inputs)
34+
35+
pred_gender = self.__classifier_block(1, 'gender')
36+
pred_age = self.__classifier_block(101, 'age')
37+
38+
super().__init__(inputs=inputs, outputs=[pred_gender, pred_age], name='SSR_Net')
39+
40+
def __extraction_block(self, inputs):
41+
x = Conv2D(32,(3,3))(inputs)
42+
x = BatchNormalization(axis=self.__channel_axis)(x)
43+
x = Activation('relu')(x)
44+
self.__x_layer1 = AveragePooling2D(2,2)(x)
45+
x = Conv2D(32,(3,3))(self.__x_layer1)
46+
x = BatchNormalization(axis=self.__channel_axis)(x)
47+
x = Activation('relu')(x)
48+
self.__x_layer2 = AveragePooling2D(2,2)(x)
49+
x = Conv2D(32,(3,3))(self.__x_layer2)
50+
x = BatchNormalization(axis=self.__channel_axis)(x)
51+
x = Activation('relu')(x)
52+
self.__x_layer3 = AveragePooling2D(2,2)(x)
53+
x = Conv2D(32,(3,3))(self.__x_layer3)
54+
x = BatchNormalization(axis=self.__channel_axis)(x)
55+
self.__x = Activation('relu')(x)
56+
#-------------------------------------------------------------------------------------------------------------------------
57+
s = Conv2D(16,(3,3))(inputs)
58+
s = BatchNormalization(axis=self.__channel_axis)(s)
59+
s = Activation('tanh')(s)
60+
self.__s_layer1 = MaxPooling2D(2,2)(s)
61+
s = Conv2D(16,(3,3))(self.__s_layer1)
62+
s = BatchNormalization(axis=self.__channel_axis)(s)
63+
s = Activation('tanh')(s)
64+
self.__s_layer2 = MaxPooling2D(2,2)(s)
65+
s = Conv2D(16,(3,3))(self.__s_layer2)
66+
s = BatchNormalization(axis=self.__channel_axis)(s)
67+
s = Activation('tanh')(s)
68+
self.__s_layer3 = MaxPooling2D(2,2)(s)
69+
s = Conv2D(16,(3,3))(self.__s_layer3)
70+
s = BatchNormalization(axis=self.__channel_axis)(s)
71+
self.__s = Activation('tanh')(s)
72+
73+
def __classifier_block(self, V, name):
74+
s_layer4 = Conv2D(10,(1,1),activation='relu')(self.__s)
75+
s_layer4 = Flatten()(s_layer4)
76+
s_layer4_mix = Dropout(0.2)(s_layer4)
77+
s_layer4_mix = Dense(units=self.__stage_num[0], activation="relu")(s_layer4_mix)
78+
79+
x_layer4 = Conv2D(10,(1,1),activation='relu')(self.__x)
80+
x_layer4 = Flatten()(x_layer4)
81+
x_layer4_mix = Dropout(0.2)(x_layer4)
82+
x_layer4_mix = Dense(units=self.__stage_num[0], activation="relu")(x_layer4_mix)
83+
84+
feat_s1_pre = Multiply()([s_layer4,x_layer4])
85+
delta_s1 = Dense(1,activation='tanh',name=name+'_delta_s1')(feat_s1_pre)
86+
87+
feat_s1 = Multiply()([s_layer4_mix,x_layer4_mix])
88+
feat_s1 = Dense(2*self.__stage_num[0],activation='relu')(feat_s1)
89+
pred_s1 = Dense(units=self.__stage_num[0], activation="relu",name=name+'_pred_stage1')(feat_s1)
90+
local_s1 = Dense(units=self.__stage_num[0], activation='tanh', name=name+'_local_delta_stage1')(feat_s1)
91+
#-------------------------------------------------------------------------------------------------------------------------
92+
s_layer2 = Conv2D(10,(1,1),activation='relu')(self.__s_layer2)
93+
s_layer2 = MaxPooling2D(4,4)(s_layer2)
94+
s_layer2 = Flatten()(s_layer2)
95+
s_layer2_mix = Dropout(0.2)(s_layer2)
96+
s_layer2_mix = Dense(self.__stage_num[1],activation='relu')(s_layer2_mix)
97+
98+
x_layer2 = Conv2D(10,(1,1),activation='relu')(self.__x_layer2)
99+
x_layer2 = AveragePooling2D(4,4)(x_layer2)
100+
x_layer2 = Flatten()(x_layer2)
101+
x_layer2_mix = Dropout(0.2)(x_layer2)
102+
x_layer2_mix = Dense(self.__stage_num[1],activation='relu')(x_layer2_mix)
103+
104+
feat_s2_pre = Multiply()([s_layer2,x_layer2])
105+
delta_s2 = Dense(1,activation='tanh',name=name+'_delta_s2')(feat_s2_pre)
106+
107+
feat_s2 = Multiply()([s_layer2_mix,x_layer2_mix])
108+
feat_s2 = Dense(2*self.__stage_num[1],activation='relu')(feat_s2)
109+
pred_s2 = Dense(units=self.__stage_num[1], activation="relu",name=name+'_pred_stage2')(feat_s2)
110+
local_s2 = Dense(units=self.__stage_num[1], activation='tanh', name=name+'_local_delta_stage2')(feat_s2)
111+
#-------------------------------------------------------------------------------------------------------------------------
112+
s_layer1 = Conv2D(10,(1,1),activation='relu')(self.__s_layer1)
113+
s_layer1 = MaxPooling2D(8,8)(s_layer1)
114+
s_layer1 = Flatten()(s_layer1)
115+
s_layer1_mix = Dropout(0.2)(s_layer1)
116+
s_layer1_mix = Dense(self.__stage_num[2],activation='relu')(s_layer1_mix)
117+
118+
x_layer1 = Conv2D(10,(1,1),activation='relu')(self.__x_layer1)
119+
x_layer1 = AveragePooling2D(8,8)(x_layer1)
120+
x_layer1 = Flatten()(x_layer1)
121+
x_layer1_mix = Dropout(0.2)(x_layer1)
122+
x_layer1_mix = Dense(self.__stage_num[2],activation='relu')(x_layer1_mix)
123+
124+
feat_s3_pre = Multiply()([s_layer1,x_layer1])
125+
delta_s3 = Dense(1,activation='tanh',name=name+'_delta_s3')(feat_s3_pre)
126+
127+
feat_s3 = Multiply()([s_layer1_mix,x_layer1_mix])
128+
feat_s3 = Dense(2*self.__stage_num[2],activation='relu')(feat_s3)
129+
pred_s3 = Dense(units=self.__stage_num[2], activation="relu",name=name+'_pred_stage3')(feat_s3)
130+
local_s3 = Dense(units=self.__stage_num[2], activation='tanh', name=name+'_local_delta_stage3')(feat_s3)
131+
#-------------------------------------------------------------------------------------------------------------------------
132+
133+
def SSR_module(x,s1,s2,s3,lambda_local,lambda_d, V):
134+
a = x[0][:,0]*0
135+
b = x[0][:,0]*0
136+
c = x[0][:,0]*0
137+
138+
for i in range(0,s1):
139+
a = a+(i+lambda_local*x[6][:,i])*x[0][:,i]
140+
a = K.expand_dims(a,-1)
141+
a = a/(s1*(1+lambda_d*x[3]))
142+
143+
for j in range(0,s2):
144+
b = b+(j+lambda_local*x[7][:,j])*x[1][:,j]
145+
b = K.expand_dims(b,-1)
146+
b = b/(s1*(1+lambda_d*x[3]))/(s2*(1+lambda_d*x[4]))
147+
148+
for k in range(0,s3):
149+
c = c+(k+lambda_local*x[8][:,k])*x[2][:,k]
150+
c = K.expand_dims(c,-1)
151+
c = c/(s1*(1+lambda_d*x[3]))/(s2*(1+lambda_d*x[4]))/(s3*(1+lambda_d*x[5]))
152+
153+
154+
out = (a+b+c)*V
155+
return out
156+
157+
pred = Lambda(SSR_module,
158+
arguments={'s1':self.__stage_num[0],
159+
's2':self.__stage_num[1],
160+
's3':self.__stage_num[2],
161+
'lambda_local':self.__lambda_local,
162+
'lambda_d':self.__lambda_d,
163+
'V':V},
164+
name=name+'_prediction')([pred_s1,pred_s2,pred_s3,delta_s1,delta_s2,delta_s3, local_s1, local_s2, local_s3])
165+
return pred
166+
167+
def prep_phase1(self):
168+
pass
169+
170+
def prep_phase2(self):
171+
pass
172+
173+
@staticmethod
174+
def decode_prediction(prediction):
175+
gender_predicted = np.around(prediction[0]).astype('int').squeeze()
176+
age_predicted = prediction[1].squeeze()
177+
return gender_predicted, age_predicted
178+
179+
@staticmethod
180+
def prep_image(data):
181+
data = data.astype('float16')
182+
return data
183+
184+
if __name__ == '__main__':
185+
model = AgenderSSRNet(64, [3,3,3], 1.0, 1.0)
186+
print(model.summary())
Binary file not shown.

requirement.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pandas
2+
scipy
3+
numpy
4+
tensorflow-gpu
5+
mxnet-cu90
6+
scikit-learn
7+
tqdm
8+
scikit-image
9+
cython

0 commit comments

Comments
 (0)