-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_lfw.py
163 lines (131 loc) · 4.97 KB
/
train_lfw.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
163
# Imports the relevant libraries
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import json
import os
import os.path
import re
import sys
import tarfile
import numpy as np
import tensorflow as tf
from six.moves import urllib
# Sets the logging level for TensorFlow library.
tf.logging.set_verbosity(tf.logging.INFO)
RESIZE_HEIGHT = 32
RESIZE_WIDTH = 32
CHANNEL = 3
BATCH = -1
extract_dir = "LFW_extract"
label_dir = "LFW_label"
label_male_names = "male_names.txt"
label_female_names = "female_names.txt"
model_dir = "LFW_model"
LOGGING_NAME = "sigmoid_tensor"
# Model function for CNN.
def cnn_model_fn(features, labels, mode):
labels = tf.reshape(labels, [1, 1])
# Defines the topology of the network here.
input_layer = tf.reshape(features, [BATCH, RESIZE_HEIGHT, RESIZE_WIDTH, CHANNEL])
print("The input layer size is %s" % input_layer.shape)
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[3, 3],
padding="same",
activation=tf.nn.relu)
print("The conv1 layer size is %s" % conv1.shape)
conv2 = tf.layers.conv2d(
inputs=conv1,
filters=32,
kernel_size=[3, 3],
padding="same",
activation=tf.nn.relu)
print("The conv2 layer size is %s" % conv2.shape)
pool1 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
print("The pool1 layer size is %s" % conv2.shape)
conv3 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[3, 3],
padding="same",
activation=tf.nn.relu)
print("The conv3 layer size is %s" % conv3.shape)
conv4 = tf.layers.conv2d(
inputs=conv3,
filters=64,
kernel_size=[3, 3],
padding="same",
activation=tf.nn.relu)
print("The conv4 layer size is %s" % conv4.shape)
pool2 = tf.layers.max_pooling2d(inputs=conv4, pool_size=[2, 2], strides=2)
print("The pool2 layer size is %s" % pool2.shape)
flat = tf.reshape(pool2, [-1, 8 * 8 * 64])
print("The flat layer size is %s" % flat.shape)
dense = tf.layers.dense(inputs=flat, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)
logits = tf.layers.dense(inputs=dropout, units=1)
predictions = {
# Add `sigmoid_tensor` to the graph. It is used for PREDICT and by the `logging_hook`.
"probabilities": tf.nn.sigmoid(logits, name=LOGGING_NAME)
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
loss = tf.losses.sigmoid_cross_entropy(multi_class_labels=labels, logits=logits)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(loss=loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {"accuracy": tf.metrics.accuracy(labels=labels, predictions=tf.round(predictions["probabilities"]))}
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def readAndResizeImageToTensor(imagePath, do_resize=True):
file = tf.read_file(imagePath)
decoded = tf.image.decode_jpeg(file, channels=CHANNEL)
if do_resize:
return tf.image.resize_images(decoded, [RESIZE_HEIGHT, RESIZE_WIDTH])
else:
return decoded
def list_files(folder):
return [os.path.join(folder, f) for f in os.listdir(folder)
if os.path.isfile(os.path.join(folder, f)) and f.endswith(".jpg")]
# Label female => 0, male => 1
def createDataset(male_folder, female_folder):
files = [readAndResizeImageToTensor(f) for f in list_files(male_folder)]
labels = [1] * len(files)
female = [readAndResizeImageToTensor(f) for f in list_files(female_folder)]
files += female
labels += [0] * len(female)
print("Going to create a dataset with %d images and %d labels." % (len(files), len(labels)))
return tf.data.Dataset.from_tensor_slices((files, labels))
def main(argv):
def train_input_fn():
return createDataset(os.path.join(extract_dir, "train_male/"), os.path.join(extract_dir, "train_female/"))
def val_input_fn():
return createDataset(os.path.join(extract_dir, "val_male/"), os.path.join(extract_dir, "val_female/"))
# Set up logging for predictions
tensors_to_log = {"probabilities": LOGGING_NAME}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=100)
# Create the Estimator
gender_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn,
model_dir=model_dir)
train_spec = tf.estimator.TrainSpec(
input_fn=train_input_fn,
hooks=[logging_hook],
max_steps=2000
)
val_spec = tf.estimator.EvalSpec(
input_fn=val_input_fn,
hooks=[logging_hook],
throttle_secs=15,
start_delay_secs=15
)
# Train the model (can increase the number of steps to improve accuracy)
tf.estimator.train_and_evaluate(gender_classifier, train_spec, val_spec)
if __name__ == "__main__":
tf.app.run()