Skip to content

Commit 36d1578

Browse files
author
Shashank Tyagi
committed
add loading
1 parent 97f243c commit 36d1578

14 files changed

+118
-47
lines changed

.DS_Store

6 KB
Binary file not shown.

aflw_train.tfrecords

74.1 MB
Binary file not shown.

annotations.npy

1.27 KB
Binary file not shown.

data_prep.py

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import numpy as np
22
import tensorflow as tf
3-
from skimage import io
3+
#from skimage import io
44
import sqlite3
5-
import cv2
5+
#import cv2
66
import matplotlib.pyplot as plt
77
import os
88
import random
9+
from tqdm import tqdm
910

10-
select_string = "faceimages.filepath, faces.face_id, facepose.roll, facepose.pitch, facepose.yaw, facerect.x, facerect.y, facerect.w, facerect.h"
11-
from_string = "faceimages, faces, facepose, facerect"
12-
where_string = "faces.face_id = facepose.face_id and faces.file_id = faceimages.file_id and faces.face_id = facerect.face_id"
13-
query_string = "SELECT " + select_string + " FROM " + from_string + " WHERE " + where_string
11+
# select_string = "faceimages.filepath, faces.face_id, facepose.roll, facepose.pitch, facepose.yaw, facerect.x, facerect.y, facerect.w, facerect.h"
12+
# from_string = "faceimages, faces, facepose, facerect"
13+
# where_string = "faces.face_id = facepose.face_id and faces.file_id = faceimages.file_id and faces.face_id = facerect.face_id"
14+
# query_string = "SELECT " + select_string + " FROM " + from_string + " WHERE " + where_string
1415

15-
conn = sqlite3.connect('/home/shashank/Documents/CSE-252C/AFLW/aflw/data/aflw.sqlite')
16-
c = conn.cursor()
16+
# conn = sqlite3.connect('/home/shashank/Documents/CSE-252C/AFLW/aflw/data/aflw.sqlite')
17+
# c = conn.cursor()
1718

1819
img_path = '/home/shashank/Documents/CSE-252C/AFLW/'
1920

20-
tfrecords_train_filename = 'aflw_train.tfrecords'
21-
tfrecords_test_filename = 'aflw_test.tfrecords'
22-
23-
writer_train = tf.python_io.TFRecordWriter(tfrecords_train_filename)
24-
writer_test = tf.python_io.TFRecordWriter(tfrecords_test_filename)
21+
# tfrecords_train_filename = 'aflw_train.tfrecords'
22+
# tfrecords_test_filename = 'aflw_test.tfrecords'
23+
tfrecords_filename = 'aflw_train.tfrecords'
24+
# writer_train = tf.python_io.TFRecordWriter(tfrecords_train_filename)
25+
# writer_test = tf.python_io.TFRecordWriter(tfrecords_test_filename)
2526

2627
def _bytes_feature(value):
2728
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
@@ -103,9 +104,10 @@ def make_tfrecord(test_images):
103104
writer_train.close()
104105
writer_test.close()
105106

106-
def extract_tfrecord():
107+
def extract_tfrecord(session):
107108
record_iterator = tf.python_io.tf_record_iterator(path=tfrecords_filename)
108-
109+
save_data = None
110+
save_euler = []
109111
for string_record in record_iterator:
110112
example = tf.train.Example()
111113
example.ParseFromString(string_record)
@@ -118,13 +120,29 @@ def extract_tfrecord():
118120
loc_y = int(example.features.feature['loc_y'].int64_list.value[0])
119121
loc_w = int(example.features.feature['loc_w'].int64_list.value[0])
120122
loc_h = int(example.features.feature['loc_h'].int64_list.value[0])
121-
cv2.rectangle(img_1d,(loc_x,loc_y),(loc_x+loc_w,loc_y+loc_h),(0,255,0),3)
122-
cv2.imshow('result',img_1d)
123-
cv2.waitKey(0)
124-
123+
roll = float(example.features.feature['roll'].float_list.value[0])
124+
yaw = float(example.features.feature['yaw'].float_list.value[0])
125+
pitch = float(example.features.feature['pitch'].float_list.value[0])
126+
127+
boxes = np.asarray([[loc_y/float(img_height),loc_x/float(img_width),(loc_y+loc_h)/float(img_height),(loc_x+loc_w)/float(img_width)]])
128+
resized_and_cropped_image = tf.image.crop_and_resize(img_1d[np.newaxis,:,:,:].astype(np.float32), boxes.astype(np.float32), [0]*1, crop_size=[227,227])
129+
if save_data is not None:
130+
save_data = np.concatenate([save_data,resized_and_cropped_image.eval(session=session)],axis=0)
131+
else:
132+
save_data = resized_and_cropped_image.eval(session=session)
133+
save_euler.append([roll,yaw,pitch])
134+
135+
np.save('truth_data.npy',save_data)
136+
np.save('annotations.npy',np.asarray(save_euler))
137+
138+
# cv2.rectangle(img_1d,(loc_x,loc_y),(loc_x+loc_w,loc_y+loc_h),(0,255,0),3)
139+
# cv2.imshow('result',img_1d)
140+
# cv2.waitKey(0)
141+
125142

126143
if __name__ == '__main__':
127-
test_images = test_names()
128-
make_tfrecord(test_images)
129-
#extract_tfrecord()
144+
#test_images = test_names()
145+
#make_tfrecord(test_images)
146+
session = tf.Session()
147+
extract_tfrecord(session)
130148

main.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import os
33
from model import *
44

5+
6+
weights_path = '/Users/shashank/Tensorflow/SPN/weights/'
7+
imgs_path = '/Users/shashank/Tensorflow/CSE252C-Hyperface/git/truth_data.npy'
8+
59
if not os.path.exists('./logs'):
610
os.makedirs('./logs')
711

@@ -14,5 +18,6 @@
1418
print 'Graph Built!'
1519
sess.run(tf.global_variables_initializer())
1620
net.print_variables()
17-
# net.train()
21+
net.load_weights(weights_path)
22+
net.predict(imgs_path)
1823

model.py

+49-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import tensorflow as tf
22
import tensorflow.contrib.slim as slim
33
import numpy as np
4+
from tqdm import tqdm
5+
from pdb import set_trace as brk
46

57

68
class HyperFace(object):
@@ -62,7 +64,10 @@ def train(self):
6264
writer = tf.summary.FileWriter('./logs', self.sess.graph)
6365
loss_summ = tf.summary.scalar('loss', self.loss)
6466

65-
def network(self,inputs):
67+
def network(self,inputs,reuse=False):
68+
69+
if reuse:
70+
tf.get_variable_scope().reuse_variables()
6671

6772
with slim.arg_scope([slim.conv2d, slim.fully_connected],
6873
activation_fn = tf.nn.relu,
@@ -87,21 +92,40 @@ def network(self,inputs):
8792
conv_all = slim.conv2d(concat_feat, 192, [1,1], 1, padding= 'VALID', scope='conv_all')
8893

8994
shape = int(np.prod(conv_all.get_shape()[1:]))
90-
fc_full = slim.fully_connected(tf.reshape(conv_all, [-1, shape]), 3072, scope='fc_full')
95+
fc_full = slim.fully_connected(tf.reshape(tf.transpose(conv_all, [0,3,1,2]), [-1, shape]), 3072, scope='fc_full')
96+
97+
fc_detection = slim.fully_connected(fc_full, 512, scope='fc_detection1')
98+
fc_landmarks = slim.fully_connected(fc_full, 512, scope='fc_landmarks1')
99+
fc_visibility = slim.fully_connected(fc_full, 512, scope='fc_visibility1')
100+
fc_pose = slim.fully_connected(fc_full, 512, scope='fc_pose1')
101+
fc_gender = slim.fully_connected(fc_full, 512, scope='fc_gender1')
102+
103+
out_detection = slim.fully_connected(fc_detection, 2, scope='fc_detection2', activation_fn = None)
104+
out_landmarks = slim.fully_connected(fc_landmarks, 42, scope='fc_landmarks2', activation_fn = None)
105+
out_visibility = slim.fully_connected(fc_visibility, 21, scope='fc_visibility2', activation_fn = None)
106+
out_pose = slim.fully_connected(fc_pose, 3, scope='fc_pose2', activation_fn = None)
107+
out_gender = slim.fully_connected(fc_gender, 2, scope='fc_gender2', activation_fn = None)
108+
109+
return [tf.nn.softmax(out_detection), out_landmarks, out_visibility, out_pose, tf.nn.softmax(out_gender)]
110+
91111

92-
fc_detection = slim.fully_connected(fc_full, 512, scope='fc_detection')
93-
fc_landmarks = slim.fully_connected(fc_full, 512, scope='fc_landmarks')
94-
fc_visibility = slim.fully_connected(fc_full, 512, scope='fc_visibility')
95-
fc_pose = slim.fully_connected(fc_full, 512, scope='fc_pose')
96-
fc_gender = slim.fully_connected(fc_full, 512, scope='fc_gender')
97112

98-
out_detection = slim.fully_connected(fc_detection, 2, scope='out_detection')
99-
out_landmarks = slim.fully_connected(fc_landmarks, 42, scope='out_landmarks')
100-
out_visibility = slim.fully_connected(fc_visibility, 21, scope='out_visibility')
101-
out_pose = slim.fully_connected(fc_pose, 3, scope='out_pose')
102-
out_gender = slim.fully_connected(fc_gender, 2, scope='out_gender')
113+
def predict(self, imgs_path):
114+
print 'Running inference...'
115+
np.set_printoptions(suppress=True)
116+
imgs = (np.load(imgs_path) - 127.5)/128.0
117+
shape = imgs.shape
118+
self.X = tf.placeholder(tf.float32, [shape[0], self.img_height, self.img_width, self.channel], name='images')
119+
pred = self.network(self.X, reuse = True)
120+
121+
net_preds = self.sess.run(pred, feed_dict={self.X: imgs})
122+
123+
print 'gender: \n', net_preds[-1]
124+
import matplotlib.pyplot as plt
125+
plt.imshow(imgs[-1]);plt.show()
126+
127+
brk()
103128

104-
return [out_detection, out_landmarks, out_visibility, out_pose, out_gender]
105129

106130
def load_from_tfRecord(self,filename_queue):
107131

@@ -129,6 +153,18 @@ def load_from_tfRecord(self,filename_queue):
129153

130154
return images
131155

156+
def load_weights(self, path):
157+
variables = slim.get_model_variables()
158+
print 'Loading weights...'
159+
for var in tqdm(variables):
160+
if ('conv' in var.name) and ('weights' in var.name):
161+
self.sess.run(var.assign(np.load(path+var.name.split('/')[0]+'/W.npy').transpose((2,3,1,0))))
162+
elif ('fc' in var.name) and ('weights' in var.name):
163+
self.sess.run(var.assign(np.load(path+var.name.split('/')[0]+'/W.npy').T))
164+
elif 'biases' in var.name:
165+
self.sess.run(var.assign(np.load(path+var.name.split('/')[0]+'/b.npy')))
166+
print 'Weights loaded!!'
167+
132168
def print_variables(self):
133169
variables = slim.get_model_variables()
134170
print 'Model Variables:'

model.pyc

7.73 KB
Binary file not shown.

split_tf_record.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import tensorflow as tf
22
import numpy as np
33
import dlib
4+
from pdb import set_trace as brk
45

56
tf_record_file = 'aflw_train.tfrecords'
67

@@ -45,7 +46,8 @@ def perform_selective_search(img,w,h,ground_truth):
4546

4647
return np.asarray(filter_positive_rects),np.asarray(filter_negative_rects)
4748

48-
def split_(filename_queue):
49+
def split_(filename_queue, sess):
50+
brk()
4951
reader = tf.TFRecordReader()
5052
_, serialized_example = reader.read(filename_queue)
5153

@@ -72,17 +74,19 @@ def split_(filename_queue):
7274

7375
image_shape = tf.pack([height, width, 3])
7476
image = tf.reshape(image, image_shape)
75-
boxes,box_ind = perform_selective_search(,tf.cast(width,tf.float32),tf.cast(height,tf.float32),(loc_x,loc_y,loc_x+loc_w,loc_y+loc_h))
76-
77-
resized_and_cropped_image = tf.image.crop_and_resize(image, boxes, box_ind, crop_size=[227,227])
77+
height,width,loc_x,loc_y,loc_h,loc_w = sess.run([height,width,loc_x,loc_y,loc_h,loc_w])
78+
# boxes,box_ind = perform_selective_search(,tf.cast(width,tf.float32),tf.cast(height,tf.float32),(loc_x,loc_y,loc_x+loc_w,loc_y+loc_h))
79+
boxes = np.asarray([[loc_y/float(height),loc_x/float(width),(loc_y+loc_h)/float(height),(loc_x+loc_w)/float(width)]])
80+
resized_and_cropped_image = tf.image.crop_and_resize(image.astype(np.float32), boxes.astype(np.float32), [0]*1, crop_size=[227,227])
7881

82+
7983
images = tf.train.shuffle_batch([resized_and_cropped_image],batch_size=10,num_threads=1,capacity=50,min_after_dequeue=10)
8084

8185
return images
8286

8387
filename_queue = tf.train.string_input_producer([tf_record_file], num_epochs=1)
8488

85-
images = split_(filename_queue)
89+
8690

8791
init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
8892

@@ -91,6 +95,8 @@ def split_(filename_queue):
9195
with tf.Session() as sess:
9296

9397
sess.run(init_op)
98+
images = split_(filename_queue, sess)
99+
94100
coord = tf.train.Coordinator()
95101
threads = tf.train.start_queue_runners(coord=coord)
96102
op_images = sess.run([images])

truth_data.npy

30.1 MB
Binary file not shown.

with SPN/main.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
with tf.Session() as sess:
1616
print 'Building Graph...'
1717
model = Network(sess,tf_record_file_path)
18-
print 'Graph Built!'
18+
print 'Done!\nInitializing variables...'
1919
sess.run(tf.global_variables_initializer())
20+
print 'Done!'
2021
model.train()

with SPN/model.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from spatial_transformer import transformer
55
from tqdm import tqdm
66
from pdb import set_trace as brk
7+
import time
78

89
class Network(object):
910

@@ -82,8 +83,11 @@ def train(self):
8283
loss_summ = tf.summary.scalar('loss', self.loss)
8384
img_summ = tf.summary.image('cropped_image', self.cropped)
8485

85-
print self.sess.run(self.T_mat, feed_dict={self.X: np.random.randn(self.batch_size, self.img_height, self.img_width, self.channel)})
8686

87+
tic = time.time()
88+
print self.sess.run(self.T_mat, feed_dict={self.X: np.random.randn(self.batch_size, self.img_height, self.img_width, self.channel)})
89+
toc = time.time()
90+
print toc-tic
8791
images = self.load_from_tfRecord(self.filename_queue)
8892

8993
coord = tf.train.Coordinator()
@@ -122,8 +126,7 @@ def hyperface(self,inputs, reuse = False):
122126
conv_all = slim.conv2d(concat_feat, 192, [1,1], 1, padding= 'VALID', scope='conv_all')
123127

124128
shape = int(np.prod(conv_all.get_shape()[1:]))
125-
# transposed for weight loading from chainer model
126-
fc_full = slim.fully_connected(tf.reshape(tf.transpose(conv_all, [0,3,1,2]), [-1, shape]), 3072, scope='fc_full')
129+
fc_full = slim.fully_connected(tf.reshape(conv_all, [-1, shape]), 3072, scope='fc_full')
127130

128131
fc_detection = slim.fully_connected(fc_full, 512, scope='fc_detection1')
129132
fc_landmarks = slim.fully_connected(fc_full, 512, scope='fc_landmarks1')
@@ -191,8 +194,10 @@ def localization_squeezenet(self, inputs):
191194
fire9 = self.fire_module(pool8, 64, 256, scope = 'fire9', res_connection=True)
192195
conv10 = slim.conv2d(fire9, 128, [1,1], 1, scope='conv10')
193196
shape = int(np.prod(conv10.get_shape()[1:]))
194-
fc11 = slim.fully_connected(tf.reshape(conv10, [-1, shape]), 6, biases_initializer = tf.constant_initializer(np.array([[1., 0., 0.],
195-
[0., 1., 0.]])) , scope='fc11')
197+
identity = np.array([[1., 0., 0.],
198+
[0., 1., 0.]])
199+
identity = identity.flatten()
200+
fc11 = slim.fully_connected(tf.reshape(conv10, [-1, shape]), 6, biases_initializer = tf.constant_initializer(identity), scope='fc11')
196201
return fc11
197202

198203

with SPN/model.pyc

12.9 KB
Binary file not shown.

with SPN/spatial_transformer.pyc

6.43 KB
Binary file not shown.

0 commit comments

Comments
 (0)