Skip to content
This repository was archived by the owner on Jun 1, 2019. It is now read-only.

Commit 3b6f876

Browse files
author
Frederik
committed
Added image renaming file
1 parent 269a50f commit 3b6f876

10 files changed

+73
-27
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ venv.bak/
107107

108108
# custom
109109
/data
110+
data/
111+
data__old/
110112
*.xml
111113
.idea/NIST.iml
112114
log/
113115
test/
114-
*.npy
116+
*.npy
117+
by_merge/
118+
models/

Preprocessing the data.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### 1. Downloading the data
2+
In this example, I will use the [Speical Database 19](https://www.nist.gov/srd/nist-special-database-19) published by the [National Institute for Standards And Technology](https://www.nist.gov/). It contains over 800,000 pre-classified images of handwritten letters and digits. It differentiates between 47 classes: All uppercase letters, all numbers and a few lower case letters. I downloaded the `by_merge.zip` file and saved in in my projects folder. To make working with the files easier, I wrote [a python script]() to move all the images into the same folder and rename them `class_[class]_index_[index].jpeg`, for example `class_25_Index_3743.jpeg`.
Binary file not shown.
-1.28 KB
Binary file not shown.
-233 KB
Binary file not shown.

models/checkpoint

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
model_checkpoint_path: "32x32_3conv_16_32_64_1norm_512.ckpt"
2-
all_model_checkpoint_paths: "..\\log\\model.ckpt-19400"
3-
all_model_checkpoint_paths: "..\\log\\model.ckpt-19600"
4-
all_model_checkpoint_paths: "..\\log\\model.ckpt-19800"
5-
all_model_checkpoint_paths: "..\\log\\model.ckpt-20000"
6-
all_model_checkpoint_paths: "32x32_3conv_16_32_64_1norm_512.ckpt"
1+
model_checkpoint_path: "32x32_2conv_32_64_1norm_1024.ckpt"
2+
all_model_checkpoint_paths: "..\\log\\model.ckpt-14400"
3+
all_model_checkpoint_paths: "..\\log\\model.ckpt-14600"
4+
all_model_checkpoint_paths: "..\\log\\model.ckpt-14800"
5+
all_model_checkpoint_paths: "..\\log\\model.ckpt-15000"
6+
all_model_checkpoint_paths: "32x32_2conv_32_64_1norm_1024.ckpt"

predict.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def normal_full_layer(input_layer, size, act=tf.nn.relu, name="unspecified"):
7979
def main():
8080
single_image = dh.get_2d_array(sys.argv[1], (32,32,1)) # Convert image to numpy array
8181
X = np.array([single_image]) # Create array from image to fit shape of x (?,32,32,1)
82-
checkpoint = "models/32x32_3conv_16_32_64_1norm_512.ckpt" # Model used for prediction, must have the same graph structure!
82+
checkpoint = "models/32x32_2conv_32_64_1norm_1024.ckpt" # Model used for prediction, must have the same graph structure!
8383

8484
# DICT
8585
classes = {
@@ -138,19 +138,20 @@ def main():
138138

139139
# MODEL
140140
# filter size=(4,4); channels=1; filters=16; shape=?x32x32x32
141-
convo_1 = convolutional_layer(x, shape=[4, 4, 1, 16], name="Convolutional_1")
141+
convo_1 = convolutional_layer(x, shape=[4, 4, 1, 32], name="Convolutional_1")
142142
convo_1_pooling = max_pool_2by2(convo_1) # shape=?x16x16x32
143143

144144
# filter size=(4,4); channels=16; filters=32; shape=?x16x16x64
145-
convo_2 = convolutional_layer(convo_1_pooling, shape=[4, 4, 16, 32], name="Convolutional_2")
145+
convo_2 = convolutional_layer(convo_1_pooling, shape=[4, 4, 32, 64], name="Convolutional_2")
146146
convo_2_pooling = max_pool_2by2(convo_2) # shape=?x8x8x64
147+
convo_2_flat = tf.reshape(convo_2_pooling, [-1, 8 * 8 * 64])
147148

148149
# filter size=(4,4); channels=32; filters=64; shape=?x8x8x32
149-
convo_3 = convolutional_layer(convo_2_pooling, shape=[4, 4, 32, 64], name="Convolutional_3")
150-
convo_3_pooling = max_pool_2by2(convo_3) # shape=4x4x32
151-
convo_3_flat = tf.reshape(convo_3_pooling, [-1, 4 * 4 * 64]) # Flatten convolutional layer
150+
#convo_3 = convolutional_layer(convo_2_pooling, shape=[4, 4, 32, 64], name="Convolutional_3")
151+
#convo_3_pooling = max_pool_2by2(convo_3) # shape=4x4x32
152+
#convo_3_flat = tf.reshape(convo_3_pooling, [-1, 4 * 4 * 64]) # Flatten convolutional layer
152153

153-
full_layer_one = normal_full_layer(convo_3_flat, 512, tf.nn.relu, name="Normal_Layer_1")
154+
full_layer_one = normal_full_layer(convo_2_flat, 1024, tf.nn.relu, name="Normal_Layer_1")
154155
with tf.name_scope("dropout"):
155156
hold_prob = tf.placeholder(tf.float32)
156157
tf.summary.scalar("dropout_keep_probability", hold_prob)

rename_images.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
from shutil import copyfile
3+
4+
by_merge_dir = "./by_merge"
5+
output_dir = "./data/"
6+
7+
8+
def get_class(str):
9+
return str.split("\\")[1]
10+
11+
12+
def main():
13+
index = 0
14+
class_index = -1
15+
classes = []
16+
for subdir, dirs, files in os.walk(by_merge_dir):
17+
for file in files:
18+
if get_class(subdir) not in classes:
19+
classes.append(get_class(subdir))
20+
class_index += 1
21+
index = 0
22+
copyfile(os.path.join(subdir, file),
23+
os.path.join(output_dir, "class_" + str(class_index) + "_index_" + str(index) + ".png"))
24+
25+
print("Copied " + os.path.join(subdir, file) + " to "
26+
+ os.path.join(output_dir, "class_" + str(class_index) + "_index_" + str(index) + ".png"))
27+
index += 1
28+
29+
30+
if __name__ == "__main__":
31+
main()

start_tensorboard.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tensorboard --logdir=D:/Coding/python/Tensorflow/NIST/log

training_32x32.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import tensorflow as tf
33
import math
44
import sys
5+
import time
56
import datetime
7+
import os
68

9+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
710

811
class NISTHelper():
912
def __init__(self, train_img, train_label, test_img, test_label):
@@ -132,19 +135,20 @@ def main():
132135

133136
# MODEL
134137
# filter size=(4,4); channels=1; filters=16; shape=?x32x32x32
135-
convo_1 = convolutional_layer(x, shape=[4, 4, 1, 16], name="Convolutional_1")
138+
convo_1 = convolutional_layer(x, shape=[4, 4, 1, 32], name="Convolutional_1")
136139
convo_1_pooling = max_pool_2by2(convo_1) # shape=?x16x16x32
137140

138141
# filter size=(4,4); channels=16; filters=32; shape=?x16x16x64
139-
convo_2 = convolutional_layer(convo_1_pooling, shape=[4, 4, 16, 32], name="Convolutional_2")
142+
convo_2 = convolutional_layer(convo_1_pooling, shape=[4, 4, 32, 64], name="Convolutional_2")
140143
convo_2_pooling = max_pool_2by2(convo_2) # shape=?x8x8x64
141-
144+
convo_2_flat = tf.reshape(convo_2_pooling, [-1, 8*8*64])
145+
142146
# filter size=(4,4); channels=32; filters=64; shape=?x8x8x32
143-
convo_3 = convolutional_layer(convo_2_pooling, shape=[4, 4, 32, 64], name="Convolutional_3")
144-
convo_3_pooling = max_pool_2by2(convo_3) # shape=4x4x32
145-
convo_3_flat = tf.reshape(convo_3_pooling, [-1, 4 * 4 * 64]) # Flatten convolutional layer
147+
#convo_3 = convolutional_layer(convo_2_pooling, shape=[4, 4, 32, 64], name="Convolutional_3")
148+
#convo_3_pooling = max_pool_2by2(convo_3) # shape=4x4x32
149+
#convo_3_flat = tf.reshape(convo_3_pooling, [-1, 4 * 4 * 64]) # Flatten convolutional layer
146150

147-
full_layer_one = normal_full_layer(convo_3_flat, 512, tf.nn.relu, name="Normal_Layer_1")
151+
full_layer_one = normal_full_layer(convo_2_flat, 1024, tf.nn.relu, name="Normal_Layer_1")
148152
with tf.name_scope("dropout"):
149153
hold_prob = tf.placeholder(tf.float32)
150154
tf.summary.scalar("dropout_keep_probability", hold_prob)
@@ -154,11 +158,10 @@ def main():
154158
with tf.name_scope("cross_entropy"):
155159
with tf.name_scope("total"):
156160
# Calculate cross-entropy
157-
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred))
161+
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true, logits=y_pred))
158162
tf.summary.scalar("cross_entropy", cross_entropy)
159163
with tf.name_scope("train"):
160-
optimizer = tf.train.AdamOptimizer(learning_rate=0.002) # Optimizer
161-
train = optimizer.minimize(cross_entropy)
164+
train = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cross_entropy)
162165
with tf.name_scope("accuracy"):
163166
with tf.name_scope("correct_predictions"):
164167
correct_predictions = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y_true, 1)) # use argmax to get the index
@@ -172,7 +175,7 @@ def main():
172175
merged = tf.summary.merge_all()
173176
init = tf.global_variables_initializer()
174177
saver = tf.train.Saver()
175-
178+
epoch_start = 0
176179
with tf.Session() as sess:
177180
train_writer = tf.summary.FileWriter("log/train", sess.graph)
178181
test_writer = tf.summary.FileWriter("log/test")
@@ -193,9 +196,13 @@ def main():
193196
summary, accuracy = sess.run([merged, acc], feed_dict={x: batch[0], y_true: batch[1], hold_prob: 1})
194197
test_writer.add_summary(summary, i) # Save the results of test batch
195198
saver.save(sess, "log/model.ckpt", i) # Save model
196-
log("Step: " + str(i) + "; Accuracy: " + str(accuracy))
199+
if i > 0:
200+
log("Step: " + str(i) + "; Accuracy: " + str(accuracy) + "; Time (200 Steps): " + str((time.time() - epoch_start)))
201+
else:
202+
log("Step: " + str(i) + "; Accuracy: " + str(accuracy) + ";")
203+
epoch_start = time.time()
197204
log("Finished training.")
198-
model_path = "models/32x32_3conv_16_32_64_1norm_512.ckpt"
205+
model_path = "models/32x32_2conv_32_64_1norm_1024.ckpt"
199206
saver.save(sess, model_path) # Save final model
200207
log("Model saved in " + model_path)
201208

0 commit comments

Comments
 (0)