-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtest.py
More file actions
148 lines (118 loc) · 4.73 KB
/
Copy pathtest.py
File metadata and controls
148 lines (118 loc) · 4.73 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
import tensorflow as tf
import numpy as np
from dan import DAN
import time
from remtime import *
import warnings
import pickle
warnings.filterwarnings("ignore")
BATCH_SIZE = 50
REG_PENALTY = 0
# NUM_VID = 500
NUM_IMAGES = 599900
NUM_TEST_IMAGES = 199900
# NUM_IMAGES = 10000
# NUM_TEST_IMAGES = 4000
N_EPOCHS = 1
imgs = tf.placeholder('float', [None, 224, 224, 3], name="image_placeholder")
values = tf.placeholder('float', [None, 5], name="value_placeholder")
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.8
with tf.Session(config=config) as sess:
model = DAN(imgs, REG_PENALTY=REG_PENALTY, preprocess='vggface')
# output = model.output
tr_reader = tf.TFRecordReader()
tr_filename_queue = tf.train.string_input_producer(['train_full.tfrecords'], num_epochs=N_EPOCHS)
_, tr_serialized_example = tr_reader.read(tr_filename_queue)
# Decode the record read by the reader
tr_feature = {'train/image': tf.FixedLenFeature([], tf.string), 'train/label': tf.FixedLenFeature([], tf.string)}
tr_features = tf.parse_single_example(tr_serialized_example, features=tr_feature)
# Convert the image data from string back to the numbers
tr_image = tf.decode_raw(tr_features['train/image'], tf.uint8)
tr_label = tf.decode_raw(tr_features['train/label'], tf.float32)
# Reshape image data into the original shape
tr_image = tf.reshape(tr_image, [224, 224, 3])
tr_label = tf.reshape(tr_label, [5])
tr_images, tr_labels = tf.train.shuffle_batch([tr_image, tr_label], batch_size=BATCH_SIZE, capacity=100, min_after_dequeue=BATCH_SIZE, allow_smaller_final_batch=True)
val_reader = tf.TFRecordReader()
val_filename_queue = tf.train.string_input_producer(['val_full.tfrecords'], num_epochs=N_EPOCHS)
_, val_serialized_example = val_reader.read(val_filename_queue)
# Decode the record read by the reader
val_feature = {'val/image': tf.FixedLenFeature([], tf.string), 'val/label': tf.FixedLenFeature([], tf.string)}
val_features = tf.parse_single_example(val_serialized_example, features=val_feature)
# Convert the image data from string back to the numbers
val_image = tf.decode_raw(val_features['val/image'], tf.uint8)
val_label = tf.decode_raw(val_features['val/label'], tf.float32)
# Reshape image data into the original shape
val_image = tf.reshape(val_image, [224, 224, 3])
val_label = tf.reshape(val_label, [5])
val_images, val_labels = tf.train.shuffle_batch([val_image, val_label], batch_size=BATCH_SIZE, capacity=100, min_after_dequeue=BATCH_SIZE, allow_smaller_final_batch=True)
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
file_list = ["param"+str((60/N_EPOCHS)*(x+1))+".pkl" for x in range(0,N_EPOCHS)]
file_list=["param25.pkl"]
training_accuracy = []
validation_accuracy = []
epoch=0
stime = time.time()
print ("Testing Started")
for pickle_file in file_list:
error=0
model.load_trained_model(pickle_file, sess)
tr_acc_list = []
val_acc_list=[]
i=0
while i<NUM_IMAGES:
i+=BATCH_SIZE
try:
epoch_x, epoch_y = sess.run([tr_images, tr_labels])
except:
print ("Error in reading this batch")
if error>=5:
break
error+=1
continue
output = sess.run([model.output], feed_dict = {imgs: epoch_x.astype(np.float32)})
tr_mean_acc = np.mean(1-np.absolute(output-epoch_y))
tr_acc_list.append(tr_mean_acc)
if not i%20000:
print (i, "images completed in training")
tr_mean_acc = np.mean(tr_acc_list)
training_accuracy.append(tr_mean_acc)
i=0
while i<NUM_TEST_IMAGES:
i+=BATCH_SIZE
try:
epoch_x, epoch_y = sess.run([val_images, val_labels])
except:
print ("Error in reading this batch")
if error>=5:
break
error+=1
continue
output = sess.run([model.output], feed_dict = {imgs: epoch_x.astype(np.float32)})
val_mean_acc = np.mean(1-np.absolute(output-epoch_y))
val_acc_list.append(val_mean_acc)
if not i%20000:
print (i, "images completed in validation")
sess.run(tf.local_variables_initializer())
val_mean_acc = np.mean(val_acc_list)
validation_accuracy.append(val_mean_acc)
print("Epoch"+ str(epoch+1)+" completed out of "+str(N_EPOCHS))
print("Tr. Mean Acc:"+str(round(tr_mean_acc,4))+", Val. Mean Acc:"+str(round(val_mean_acc,4)))
ftime = time.time()
remtime = (ftime-stime)*(N_EPOCHS-epoch-1)
stime=ftime
printTime(remtime)
epoch+=1
if not epoch%(N_EPOCHS/2):
with open("acc_plot25.pkl", "wb") as nfile:
pickle.dump([training_accuracy, validation_accuracy], nfile)
print ("Half testing saved")
coord.request_stop()
# Wait for threads to stop
coord.join(threads)
print ("Testing done... Values saved successfully")