forked from blueskyM01/SD_GAN_Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpShapePoseNet.py
executable file
·165 lines (140 loc) · 7.14 KB
/
ExpShapePoseNet.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
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
import sys
import numpy as np
import tensorflow as tf
import cv2
import scipy.io as sio
import pose_utils as pu
import os
import os.path
import glob
import time
import scipy
import scipy.io as sio
import ST_model_nonTrainable_AlexNetOnFaces as Pose_model
import utils_3DMM
import csv
import argparse
# sys.path.append('./ResNet')
from ThreeDMM_shape import ResNet_101 as resnet101_shape
from ThreeDMM_expr import ResNet_101 as resnet101_expr
class m4_3DMM:
def __init__(self, cfg):
self.cfg = cfg
# Get training image/labels mean/std for pose CNN
try:
file = np.load(self.cfg.train_imgs_mean_file_path, )
self.train_mean_vec = file["train_mean_vec"] # [0,1]
print('Load ' + self.cfg.train_imgs_mean_file_path + ' successful....')
except:
raise Exception('Load ' + self.cfg.train_imgs_mean_file_path + ' failed....')
del file
try:
file = np.load(self.cfg.train_labels_mean_std_file_path)
self.mean_labels = file["mean_labels"]
self.std_labels = file["std_labels"]
print('Load ' + self.cfg.train_labels_mean_std_file_path + ' successful....')
except:
raise Exception('Load ' + self.cfg.train_labels_mean_std_file_path + ' failed....')
del file
try:
# Get training image mean for Shape CNN
mean_image_shape = np.load(self.cfg.ThreeDMM_shape_mean_file_path) # 3 x 224 x 224
self.mean_image_shape = np.transpose(mean_image_shape, [1, 2, 0]) # 224 x 224 x 3, [0,255]
print('Load ' + self.cfg.ThreeDMM_shape_mean_file_path +' successful....')
except:
raise Exception('Load ' + self.cfg.ThreeDMM_shape_mean_file_path +' failed....')
# 程序中没有用到
# try:
# # Get training image mean for Expression CNN
# mean_image_exp = np.load('./Expression_Model/3DMM_expr_mean.npy') # 3 x 224 x 224
# self.mean_image_exp = np.transpose(mean_image_exp, [1, 2, 0]) # 224 x 224 x 3, [0,255]
# print('Load ' + self.cfg.ThreeDMM_shape_mean_file_path + ' successful....')
# except:
# raise Exception('Load ' + self.cfg.ThreeDMM_shape_mean_file_path + ' failed....')
def extract_PSE_feats(self, x, reuse=False):
'''
:param x: x format is RGB and is value range is [-1,1].
:return: fc1ls: shape, fc1le: expression, pose_model.preds_unNormalized: pose
'''
x = tf.image.resize_images(x, [227, 227])
# x is RGB and is value range is [-1,1].
# first we need to change RGB to BGR;
batch_, height_, width_, nc = x.get_shape().as_list()
R = tf.reshape(x[:, :, :, 0], [batch_, height_, width_, 1])
G = tf.reshape(x[:, :, :, 1], [batch_, height_, width_, 1])
B = tf.reshape(x[:, :, :, 2], [batch_, height_, width_, 1])
x = tf.concat([B, G, R], axis=3)
# second change range [-1,1] to [0,255]
x = (x + 1.0) * 127.5
###################
# Face Pose-Net
###################
try:
net_data = np.load(self.cfg.PAM_frontal_ALexNet_file_path, encoding="latin1").item()
pose_labels = np.zeros([self.cfg.batch_size, 6])
print('Load ' + self.cfg.PAM_frontal_ALexNet_file_path+ ' successful....')
except:
raise Exception('Load ' + self.cfg.PAM_frontal_ALexNet_file_path+ ' failed....')
x1 = tf.image.resize_bilinear(x, tf.constant([227, 227], dtype=tf.int32))
# Image normalization
x1 = x1 / 255. # from [0,255] to [0,1]
# subtract training mean
mean = tf.reshape(self.train_mean_vec, [1, 1, 1, 3])
mean = tf.cast(mean, 'float32')
x1 = x1 - mean
pose_model = Pose_model.Pose_Estimation(x1, pose_labels, 'valid', 0, 1, 1, 0.01, net_data, self.cfg.batch_size,
self.mean_labels, self.std_labels)
pose_model._build_graph(reuse=reuse)
self.pose = pose_model.preds_unNormalized
del net_data
###################
# Shape CNN
###################
x2 = tf.image.resize_bilinear(x, tf.constant([224, 224], dtype=tf.int32))
x2 = tf.cast(x2, 'float32')
x2 = tf.reshape(x2, [self.cfg.batch_size, 224, 224, 3])
# Image normalization
mean = tf.reshape(self.mean_image_shape, [1, 224, 224, 3])
mean = tf.cast(mean, 'float32')
x2 = x2 - mean
with tf.variable_scope('shapeCNN', reuse=reuse):
net_shape = resnet101_shape({'input': x2}, trainable=True)
pool5 = net_shape.layers['pool5']
pool5 = tf.squeeze(pool5)
pool5 = tf.reshape(pool5, [self.cfg.batch_size, -1])
try:
npzfile = np.load(self.cfg.ShapeNet_fc_weights_file_path)
print('Load ' + self.cfg.ShapeNet_fc_weights_file_path + ' successful....')
except:
raise Exception('Load ' + self.cfg.ShapeNet_fc_weights_file_path + ' failed....')
ini_weights_shape = npzfile['ini_weights_shape']
ini_biases_shape = npzfile['ini_biases_shape']
with tf.variable_scope('shapeCNN_fc1'):
# fc1ws = tf.Variable(tf.reshape(ini_weights_shape, [2048, -1]), trainable=True, name='weights')
# fc1bs = tf.Variable(tf.reshape(ini_biases_shape, [-1]), trainable=True, name='biases')
fc1ws = tf.get_variable(initializer=tf.reshape(ini_weights_shape, [2048, -1]), trainable=True, name='weights')
fc1bs = tf.get_variable(initializer=tf.reshape(ini_biases_shape, [-1]), trainable=True, name='biases')
self.fc1ls = tf.nn.bias_add(tf.matmul(pool5, fc1ws), fc1bs)
###################
# Expression CNN
###################
with tf.variable_scope('exprCNN', reuse=reuse):
net_expr = resnet101_expr({'input': x2}, trainable=True)
pool5 = net_expr.layers['pool5']
pool5 = tf.squeeze(pool5)
pool5 = tf.reshape(pool5, [self.cfg.batch_size, -1])
try:
npzfile = np.load(self.cfg.ExpNet_fc_weights_file_path)
ini_weights_expr = npzfile['ini_weights_expr']
ini_biases_expr = npzfile['ini_biases_expr']
print('Load ' + self.cfg.ExpNet_fc_weights_file_path + ' successful....')
except:
raise Exception('Load ' + self.cfg.ExpNet_fc_weights_file_path + ' failed....')
# time.sleep(30)
with tf.variable_scope('exprCNN_fc1'):
# fc1we = tf.Variable(tf.reshape(ini_weights_expr, [2048, 29]), trainable=True, name='weights')
# fc1be = tf.Variable(tf.reshape(ini_biases_expr, [29]), trainable=True, name='biases')
fc1we = tf.get_variable(initializer=tf.reshape(ini_weights_expr, [2048, 29]), trainable=True, name='weights')
fc1be = tf.get_variable(initializer=tf.reshape(ini_biases_expr, [29]), trainable=True, name='biases')
self.fc1le = tf.nn.bias_add(tf.matmul(pool5, fc1we), fc1be)
# return fc1ls, fc1le, pose_model.preds_unNormalized