-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader_cnn.py
191 lines (159 loc) · 6.76 KB
/
data_loader_cnn.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
185
186
187
188
189
190
191
from __future__ import print_function
import tensorflow as tf
import os
import signal
import sys
def signal_handler(signal, frame):
print('Ctrl-C Captured, exiting')
print("Do you want to save your model? (y/n)")
answ = input()
if(answ == 'n'):
sys.exit(0)
print("enter a file name for the model to be saved to")
name = input()
save_path = saver.save(sess, './models/'+name+'.ckpt')
print("model was saved to " + save_path)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Dataset Parameters - CHANGE HERE
DATASET_PATH = 'TRANCOS_v3/'
# Image Parameters
N_CLASSES = 1
IMG_HEIGHT = 112
IMG_WIDTH = 200
CHANNELS = 3
# Reading the dataset
def read_images(dataset_path, batch_size, mode):
image_paths, labels = [], []
# Read dataset file
if mode == 'train':
dataset_path_mod = dataset_path + 'image_sets/trainval.txt'
elif mode == 'validation':
dataset_path_mod = dataset_path + 'image_sets/validation.txt'
elif mode == 'test':
dataset_path_mod = dataset_path + 'image_sets/test.txt'
with open(dataset_path_mod, 'r') as f:
data = f.read().splitlines()
for d in data:
image_paths.append(dataset_path+'images/'+d)
label_path = dataset_path+'images/'+d.replace('.jpg','.txt')
with open(label_path, 'r', encoding='utf-8') as f:
for i, l in enumerate(f):
pass
labels.append(i+1)
# Convert to Tensor
imagepaths = tf.convert_to_tensor(image_paths, dtype=tf.string)
labels = tf.convert_to_tensor(labels, dtype=tf.float32)
# Build a TF Queue, shuffle data
image, label = tf.train.slice_input_producer([imagepaths, labels],
shuffle=True)
# Read images from disk
image = tf.read_file(image)
image = tf.image.decode_jpeg(image, channels=CHANNELS)
# Resize images to a common size
image = tf.image.resize_images(image, [IMG_HEIGHT, IMG_WIDTH])
# Normalize
#image = image * 1.0/127.5 - 1.0 ################################################################### sketchy
image = tf.image.per_image_standardization(image) #better ?
# Create batches
X, Y = tf.train.batch([image, label], batch_size=batch_size,
capacity=batch_size * 8)#,
#num_threads=4) ############################################################# num threads?
return X, Y
# Parameters
learning_rate = 0.0017
num_steps = 1000
batch_size = 32
val_size = 420
test_size = 421
display_step = 100
dropout = 0.1610
# Build the data input
X_train_batch, Y_train_batch = read_images(DATASET_PATH, batch_size, mode='train')
X_test_batch, Y_test_batch = read_images(DATASET_PATH, test_size, mode='test')
X_val_batch, Y_val_batch = read_images(DATASET_PATH, val_size, mode='validation')
# Create model
# Create model
def conv_net(x, dropout=0.1, kernel_size=3, filters=32, hiddenunits=500):
# Define a scope for reusing the variables
conv1 = tf.layers.conv2d(inputs=x,
filters=filters,
kernel_size=kernel_size,
activation=tf.nn.relu)
maxpool1 = tf.layers.max_pooling2d(inputs=conv1,
pool_size=2,
strides=2)
conv2 = tf.layers.conv2d(inputs=maxpool1,
filters=filters,
kernel_size=kernel_size,
activation=tf.nn.relu)
maxpool2 = tf.layers.max_pooling2d(inputs=conv2,
pool_size=2,
strides=2)
conv3 = tf.layers.conv2d(inputs=maxpool2,
filters=filters,
kernel_size=kernel_size,
activation=tf.nn.relu)
maxpool3 = tf.layers.max_pooling2d(inputs=conv2,
pool_size=2,
strides=2)
flat = tf.contrib.layers.flatten(inputs=maxpool3)
dropout1 = tf.layers.dropout(inputs=flat,
rate=dropout,
training=True)
dense1 = tf.layers.dense(inputs=dropout1,
units=hiddenunits,
activation=tf.nn.relu)
dropout2 = tf.layers.dropout(inputs=dense1,
rate=dropout,
training=True)
out = tf.layers.dense(dropout2,
units=1)
#hidden = tf.layers.dense(inputs=x,
# units=100,
# activation=tf.nn.relu)
#out = tf.layers.dense(hidden, 1)
return out
# Because Dropout have different behavior at training and prediction time, we
# need to create 2 distinct computation graphs that share the same weights.
# Create a graph for training
logits_train = conv_net(X_train_batch)
# Create another graph for testing that reuse the same weights
logits_test = conv_net(X_test_batch)
# Define loss and optimizer (with train logits, for dropout to take effect)
loss_op = tf.reduce_mean(tf.square(tf.subtract(Y_train_batch, logits_train))) #mean square error
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op)
# Evaluate model (with test logits, for dropout to be disabled)
test_loss = tf.reduce_mean(tf.square(tf.subtract(Y_test_batch, logits_test)))
# Saver object
saver = tf.train.Saver()
main = 1
save = 1
if main == 1:
# Start training
with tf.Session() as sess:
# Run the initializer
sess.run(tf.global_variables_initializer())
# initialize the queue threads to start to shovel data
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
# Training cycle
for step in range(1, num_steps+1):
if step % display_step == 0:
# Run optimization and calculate batch loss and accuracy
_, trainloss = sess.run([train_op, loss_op])
print("Step " + str(step) + ", Train minibatch Loss= " + \
"{:.4f}".format(trainloss))
testloss = sess.run(test_loss)
print("Test loss = " + "{:.4f}".format(testloss))
else:
# Only run the optimization op (backprop)
sess.run(train_op)
coord.request_stop()
coord.join(threads)
# Save your model
#export_dir = './'
#builder = tf.saved_model_builder.SavedModelBuilder(export_dir)
#save_path = saver.save(sess, "/tmp/model.ckpt")
saver.save(sess, './my_tf_model')