-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontroller.py
184 lines (170 loc) · 6.89 KB
/
controller.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import datetime
import os
import time
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import pandas as pd
import conv_nn
import data_helpers
import linear_nn
num_epochs = 10
def get_index(str):
if str == "left_eye_center_x":
return 0
if str == "left_eye_center_y":
return 1
if str == "right_eye_center_x":
return 2
if str == "right_eye_center_y":
return 3
if str == "left_eye_inner_corner_x":
return 4
if str == "left_eye_inner_corner_y":
return 5
if str == "left_eye_outer_corner_x":
return 6
if str == "left_eye_outer_corner_y":
return 7
if str == "right_eye_inner_corner_x":
return 8
if str == "right_eye_inner_corner_y":
return 9
if str == "right_eye_outer_corner_x":
return 10
if str == "right_eye_outer_corner_y":
return 11
if str == "left_eyebrow_inner_end_x":
return 12
if str == "left_eyebrow_inner_end_y":
return 13
if str == "left_eyebrow_outer_end_x":
return 14
if str == "left_eyebrow_outer_end_y":
return 15
if str == "right_eyebrow_inner_end_x":
return 16
if str == "right_eyebrow_inner_end_y":
return 17
if str == "right_eyebrow_outer_end_x":
return 18
if str == "right_eyebrow_outer_end_y":
return 19
if str == "nose_tip_x":
return 20
if str == "nose_tip_y":
return 21
if str == "mouth_left_corner_x":
return 22
if str == "mouth_left_corner_y":
return 23
if str == "mouth_right_corner_x":
return 24
if str == "mouth_right_corner_y":
return 25
if str == "mouth_center_top_lip_x":
return 26
if str == "mouth_center_top_lip_y":
return 27
if str == "mouth_center_bottom_lip_x":
return 28
if str == "mouth_center_bottom_lip_y":
return 29
def linear_NN(X, y):
graph = tf.Graph()
with graph.as_default():
nn = linear_nn.nn_linear(X, y)
global_step = tf.Variable(0, name="global_step", trainable=False)
optimizer = tf.train.MomentumOptimizer(
learning_rate=0.001,
momentum=0.9,
use_nesterov=True,
).minimize(nn.loss, global_step=global_step)
with tf.Session(graph=graph) as session:
train_loss_history = []
session.run(tf.global_variables_initializer())
batches = data_helpers.batch_iter(zip(X, y), batch_size=64, num_epochs=num_epochs, shuffle=True)
for batch in batches:
X_train, y_train = zip(*batch)
feed_dict = {nn.input_x: np.asarray(X_train), nn.input_y: np.asarray(y_train)}
_, step, loss, predictions = session.run([optimizer, global_step, nn.loss, nn.predictions], feed_dict)
time_str = datetime.datetime.now().isoformat()
print("{}: step {}, loss {:g}".format(time_str, step, loss))
train_loss_history.append(loss)
# if step % 10 == 0:
# pass
x_axis = np.arange(step)
plt.plot(x_axis, train_loss_history, "b-", linewidth=2, label="train")
plt.grid()
plt.legend()
plt.ylabel("loss")
plt.show()
def conv_NN(X, y, test=False):
graph = tf.Graph()
with graph.as_default():
with tf.Session(graph=graph) as session:
if test == True:
checkpoint_dir = "data/1495080366/checkpoints"
checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(session, checkpoint_file)
input_x = graph.get_operation_by_name("x_input").outputs[0]
scores = graph.get_operation_by_name("scores").outputs[0]
with open("IdLookupTable.csv") as f:
lines = [line.split() for line in f]
locations = []
for i, line in enumerate(lines):
if i == 0:
continue
img_id = int(line[0].split(',')[1])
idx = int(get_index(line[0].split(',')[2][1:-1]))
X_train = np.asarray(X[img_id - 1])
feed_dict = {input_x: X_train.reshape(1, len(X_train))}
score = session.run([scores], feed_dict)
location = ((score[0] * 48) + 48)[0][idx]
if location > 96.0:
location = 96
locations.append(location)
df = pd.DataFrame(locations, columns=["Location"])
df.to_csv("submit.csv", index=False)
else:
cnn = conv_nn.conv_nn(num_classes=30)
global_step = tf.Variable(0, name="global_step", trainable=False)
optimizer = tf.train.MomentumOptimizer(
learning_rate=0.001,
momentum=0.9,
use_nesterov=True,
).minimize(cnn.loss, global_step=global_step)
timestamp = str(int(time.time()))
out_dir = os.path.abspath(os.path.join(os.path.curdir, "data", timestamp))
checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
checkpoint_prefix = os.path.join(checkpoint_dir, "model")
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
saver = tf.train.Saver(tf.all_variables())
train_loss_history = []
session.run(tf.global_variables_initializer())
batches = data_helpers.batch_iter(zip(X, y), batch_size=64, num_epochs=num_epochs, shuffle=True)
for batch in batches:
X_train, y_train = zip(*batch)
feed_dict = {cnn.x: np.asarray(X_train), cnn.y: np.asarray(y_train)}
_, step, loss, scores = session.run([optimizer, global_step, cnn.loss, cnn.scores], feed_dict)
time_str = datetime.datetime.now().isoformat()
print("{}: step {}, loss {:g}".format(time_str, step, loss))
if step % 10 == 0 and test == False:
path = saver.save(session, checkpoint_prefix, global_step=step)
print("Saved model checkpoint to {}\n".format(path))
train_loss_history.append(loss)
if test == False:
x_axis = np.arange(step)
plt.plot(x_axis, train_loss_history, "b-", linewidth=2, label="train")
plt.grid()
plt.legend()
plt.ylabel("loss")
plt.show()
'''Training part'''
# X, y = data_helpers.get_data()
# conv_NN(X, y)
'''Testing part'''
X, y = data_helpers.get_data(test=True)
conv_NN(X, y, True)