forked from xianyuMeng/FacialCapture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisualModule.py
More file actions
232 lines (211 loc) · 9.67 KB
/
Copy pathVisualModule.py
File metadata and controls
232 lines (211 loc) · 9.67 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Imports
import numpy as np
import tensorflow as tf
import pickle
import pprint
from tensorflow.contrib import learn
from tensorflow.contrib.learn.python.learn.estimators import model_fn as model_fn_lib
class Module(object):
def __init__(self, config, mode):
assert mode in {'train', 'inference'}
self.mode = mode
self.config = config
self.reader = tf.TFRecordReader()
def build_bshape(self):
pkl_file = open(self.config.pkl_fname)
self.bshape = pickle.load(pkl_file)
print("Loading pickle file of shape {}".format(self.bshape.shape))
self.bshape = tf.constant(self.bshape)
pkl_file.close()
mean_mesh_file = open(self.config.mean_pkl_fname)
self.mean_mesh = pickle.load(mean_mesh_file)
print("Loading mean mesh file of shape {}".format(self.mean_mesh.shape))
self.mean_mesh = tf.constant(self.mean_mesh)
mean_mesh_file.close()
def build_inputs(self):
if self.mode == 'train':
data_files = []
for pattern in self.config.file_pattern.split(','):
print(tf.gfile.Glob(pattern))
data_files.extend(tf.gfile.Glob(pattern))
#data_files = tf.gfile.Glob(self.config.file_pattern)
print("number of trainig record files : {}".format(len(data_files)))
filename_queue = tf.train.string_input_producer( \
data_files, \
shuffle = True, \
capacity = 16)
_, example = self.reader.read(filename_queue)
proto_value = tf.parse_single_example(\
example, \
features = { \
self.config.image_feature_name: tf.FixedLenFeature([], dtype=tf.string),\
self.config.predict_feature_name: tf.FixedLenFeature([self.config.predict_num], dtype=tf.float32) \
})
image = proto_value[self.config.image_feature_name]
weight = proto_value[self.config.predict_feature_name]
image = self.process_image(image)
#weight = self.process_weight(weight)
self.image, self.weight = tf.train.batch_join([(image, weight)], batch_size = self.config.batch_size)
print('get image of shape{}'.format(self.image.get_shape()))
else:
self.image_feed = tf.placeholder(dtype=tf.string, shape=[], name='image_feed')
self.image = tf.expand_dims(self.process_image(self.image_feed),0)
def process_image(self, im_str):
image = tf.reshape(tf.decode_raw(im_str, out_type=tf.uint8), (self.config.image_width, self.config.image_height, 1))
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = tf.image.resize_images( \
image, \
size=[self.config.image_height, self.config.image_width],\
method=tf.image.ResizeMethod.BILINEAR)
return image
def setup_global_step(self):
self.global_step = tf.Variable( \
initial_value=0, \
name = 'global_step', \
trainable = False, \
collections = [tf.GraphKeys.GLOBAL_STEP, tf.GraphKeys.GLOBAL_VARIABLES])
def build_model(self):
with tf.variable_scope('face'):
weight_regularizer = tf.contrib.layers.l2_regularizer(scale = 0.1)
# See Section 3.1 of paper ` Production-Level Facial Performance Capture ` for more information
conv1a = tf.contrib.layers.conv2d( \
self.image, \
64, \
kernel_size = [3, 3], \
stride = 2, \
weights_initializer = tf.contrib.layers.xavier_initializer(uniform = False), \
# weights_regularizer = weight_regularizer, \
scope = "conv1a" \
)
conv1b = tf.contrib.layers.conv2d( \
conv1a, \
64, \
kernel_size = [3, 3], \
stride = 1 , \
# weights_initializer = tf.contrib.layers.xavier_initializer(uniform = False), \
weights_regularizer = weight_regularizer, \
scope = "conv1b"\
)
conv2a = tf.contrib.layers.conv2d( \
conv1b, \
96, \
kernel_size = [3, 3], \
stride = 2, \
weights_initializer = tf.contrib.layers.xavier_initializer(uniform = False),\
# weights_regularizer = weight_regularizer, \
scope = "conv2a" \
)
conv2b = tf.contrib.layers.conv2d( \
conv2a, \
96, \
kernel_size = [3, 3], \
stride = 1, \
weights_initializer = tf.contrib.layers.xavier_initializer(uniform = False),\
# weights_regularizer = weight_regularizer, \
scope = "conv2b")
conv3a = tf.contrib.layers.conv2d( \
conv2b, \
144, \
kernel_size = [3, 3], \
stride = 2, \
weights_initializer = tf.contrib.layers.xavier_initializer(uniform = False), \
# weights_regularizer = weight_regularizer, \
scope = 'conv3a')
conv3b = tf.contrib.layers.conv2d( \
conv3a, \
144, \
kernel_size = [3, 3], \
stride = 1, \
weights_initializer = tf.contrib.layers.xavier_initializer(uniform = False), \
# weights_regularizer = weight_regularizer, \
scope = 'conv3b')
print("conv3b shape is {}".format(conv3b.get_shape()))
conv4a = tf.contrib.layers.conv2d( \
conv3b, \
216, \
kernel_size = [3, 3], \
stride = 2, \
# weights_regularizer = weight_regularizer, \
scope = 'conv4a')
conv4b = tf.contrib.layers.conv2d( \
conv4a, \
216, \
kernel_size = [3, 3], \
stride = 1, \
# weights_regularizer = weight_regularizer, \
scope = 'conv4b')
conv5a = tf.contrib.layers.conv2d( \
conv4b, \
324, \
kernel_size = [3, 3], \
stride = 2, \
# weights_regularizer = weight_regularizer, \
scope = 'conv5a')
conv5b = tf.contrib.layers.conv2d( \
conv5a, \
324, \
kernel_size = [3, 3], \
stride = 1, \
# weights_regularizer = weight_regularizer, \
scope = 'conv5b')
conv6a = tf.contrib.layers.conv2d( \
conv5b, \
486, \
kernel_size = [3, 3], \
# weights_regularizer = weight_regularizer, \
stride = 2, \
scope = 'conv6a')
conv6b = tf.contrib.layers.conv2d( \
conv6a, \
486, \
kernel_size = [3, 3], \
# weights_regularizer = weight_regularizer, \
stride = 1, \
scope = 'conv6b')
print("conv6b size is {}".format(conv6b.get_shape()))
drop = tf.contrib.layers.dropout(\
conv6b, \
0.8, \
is_training = (self.mode == 'train'), \
scope = 'dropout')
fc = tf.contrib.layers.fully_connected( \
inputs = drop, \
num_outputs = self.config.pca_num, \
activation_fn = None, \
scope = 'fully_connected')
print("fc shape {}".format(fc.get_shape()))
output = tf.contrib.layers.flatten(fc)
output = tf.contrib.layers.fully_connected( \
inputs = output, \
num_outputs = self.config.pca_num, \
activation_fn = None, \
scope = 'output')
#self.bshape = tf.reshape(self.bshape, [1, self.config.predict_num])
self.pca_coef = output
output = (tf.matmul(output, self.bshape))
# output = tf.contrib.layers.fully_connected( \
# inputs = output, \
# num_outputs = self.config.predict_num, \
# scope = 'output')
print('shape is {}'.format(output.get_shape()))
output = tf.add(self.mean_mesh, output)
if self.mode == 'inference' :
self.prediction = output
else :
losses = tf.square(output - self.weight)
batch_loss = tf.reduce_sum(losses)
tf.losses.add_loss(batch_loss)
tf.summary.scalar('losses/batch_loss', batch_loss)
self.total_loss = tf.losses.get_total_loss()
tf.summary.scalar('losses/total_loss', self.total_loss)
for var in tf.trainable_variables():
tf.summary.histogram(var.op.name, var)
def build(self):
self.build_bshape()
if self.mode == 'train' :
self.setup_global_step()
self.build_inputs()
self.build_model()