Skip to content

Instantly share code, notes, and snippets.

@leechanwoo
Last active August 25, 2017 08:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leechanwoo/e26d96ef8b5349bb86a094fccb33416f to your computer and use it in GitHub Desktop.
Save leechanwoo/e26d96ef8b5349bb86a094fccb33416f to your computer and use it in GitHub Desktop.
cnn with tensorboard
import tensorflow as tf
import matplotlib.pyplot as plt
import os
%matplotlib inline
tf.reset_default_graph()
images = "dataset/test_dataset_png/"
image_dir = os.path.join(os.getcwd(), images)
imagenames = [os.path.join(image_dir, f) for f in os.listdir(image_dir)]
label = "dataset/test_dataset_csv/label.csv"
labelname = [os.path.join(os.getcwd(), label)]
imagename_queue = tf.train.string_input_producer(imagenames)
labelname_queue = tf.train.string_input_producer(labelname)
img_reader = tf.WholeFileReader()
label_reader = tf.TextLineReader()
_, image = img_reader.read(imagename_queue)
_, label = label_reader.read(labelname_queue)
decoded_img = tf.image.decode_png(image)
reshaped_img = tf.reshape(decoded_img, shape=[61, 49, 1])
reshaped_img = tf.cast(reshaped_img, tf.float32)
decoded_label = tf.decode_csv(label, record_defaults=[[0]])
x, y_ = tf.train.batch([reshaped_img, decoded_label], 10)
y_ = tf.one_hot(y_, depth=3, on_value=1.0, off_value=0.0, axis=1)
y_ = tf.reshape(y_, [-1, 3])
conv1 = tf.layers.conv2d(x, filters=10, kernel_size=[3, 3], padding="SAME", activation=tf.nn.relu)
conv2 = tf.layers.conv2d(conv1, filters=10, kernel_size=[3, 3], padding="SAME", activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(conv2, pool_size=[2, 2], strides=[2, 2])
feature_map = tf.reduce_mean(conv1, axis=3)
feature_map = tf.reshape(feature_map, [-1, 61, 49, 1])
tf.summary.image("feature_map", feature_map)
conv3 = tf.layers.conv2d(pool2, filters=10, kernel_size=[3, 3], padding="SAME", activation=tf.nn.relu)
pool3 = tf.layers.max_pooling2d(conv3, pool_size=[2, 2], strides=[2, 2])
conv4 = tf.layers.conv2d(pool3, filters=20, kernel_size=[3, 3], padding="SAME", activation=tf.nn.relu)
pool4 = tf.layers.max_pooling2d(conv4, pool_size=[2, 2], strides=[2, 2])
flat = tf.reshape(pool4, shape=[-1, 7*6*20])
drop_prob = tf.placeholder(tf.float32)
fc1 = tf.layers.dense(flat, 300)
droped_fc1 = tf.nn.dropout(fc1, drop_prob)
fc2 = tf.layers.dense(droped_fc1, 100)
droped_fc2 = tf.nn.dropout(fc2, drop_prob)
out = tf.layers.dense(droped_fc2, 3)
variables = tf.trainable_variables()
for i in range(len(variables)):
tf.summary.histogram("cnn_weight_{}".format(i), variables[i])
loss = tf.losses.softmax_cross_entropy(y_, out)
tf.summary.scalar("loss", loss)
train_op = tf.train.GradientDescentOptimizer(1e-10).minimize(loss)
pred = tf.nn.softmax(out)
comp = tf.cast(tf.equal(tf.argmax(pred, axis=1), tf.argmax(y_, axis=1)), tf.float32)
accuracy = tf.reduce_mean(comp)
merged = tf.summary.merge_all()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
thread = tf.train.start_queue_runners(sess, coord)
writer = tf.summary.FileWriter("./log", sess.graph)
for i in range(100):
_, _loss, _summary = sess.run([train_op, loss, merged], {drop_prob: 0.7})
_acc = sess.run(accuracy, {drop_prob: 1.0})
print("step: {}, loss: {}, acc: {}".format(i, _loss, _acc))
writer.add_summary(_summary, i)
coord.request_stop()
coord.join(thread)
tensorboard --logdir=./log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment