Skip to content

Instantly share code, notes, and snippets.

@geoffreyp
Last active March 10, 2018 14:49
Show Gist options
  • Save geoffreyp/cc8e97aab1bff4d39e10001118c6322e to your computer and use it in GitHub Desktop.
Save geoffreyp/cc8e97aab1bff4d39e10001118c6322e to your computer and use it in GitHub Desktop.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Imports
import numpy as np
import tensorflow as tf
def cnn_model_fn(features, labels, mode):
"""Model function for CNN."""
# Input Layer
# array[0] = batch size = Size of the subset of examples to use when performing gradient descent during training.
# array[1] = width of images
# array[2] = height of images
# array[3] = channels = Number of color channels in the example images (1 for grey, 3 for rgb)
input_layer = tf.reshape(features, [-1, 40, 40, 1])
# Convolutional Layer #1
conv1 = tf.layers.conv2d(
inputs=input_layer,
filters=32,
kernel_size=[5, 5],
# To specify that the output tensor should have the same width and height values as the input tensor
# value can be "same" ou "valid"
padding="same",
activation=tf.nn.relu)
# Pooling Layer #1
pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2)
# Convolutional Layer #2 and Pooling Layer #2
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=64,
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu)
pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2)
# Dense Layer
# pool2 width * pool2 height * pool2 channels
pool2_shape = pool2.get_shape().as_list()
pool2_flat = tf.reshape(pool2, [-1, pool2_shape[1] * pool2_shape[2] * pool2_shape[3]])
dense = tf.layers.dense(inputs=pool2_flat, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(
inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)
# Logits Layer
logits = tf.layers.dense(inputs=dropout, units=1)
print("================")
print(logits)
print("================")
print("================")
print(labels)
print("================")
predictions = {
# Generate predictions (for PREDICT and EVAL mode)
"classes": tf.argmax(input=logits, axis=1),
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the
# `logging_hook`.
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
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.sparse_softmax_cross_entropy(labels=labels, logits=logits)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.GradientDescentOptimizer(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=predictions["classes"])}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
def train_input_fn_custom(filenames_array, labels_array, batch_size):
# Reads an image from a file, decodes it into a dense tensor, and resizes it to a fixed shape.
def _parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_png(image_string)
image_resized = tf.image.resize_images(image_decoded, [40, 40])
return image_resized, label
filenames = tf.constant(filenames_array)
labels = tf.constant(labels_array)
# Convert the inputs to a Dataset
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
# Convert filenames to a decoded image
dataset = dataset.map(_parse_function)
# Shuffle, repeat, and batch the examples.
dataset = dataset.shuffle(1000).repeat().batch(batch_size)
# Build the Iterator, and return the read end of the pipeline.
return dataset.make_one_shot_iterator().get_next()
def train_label_custom():
return [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
def eval_input_fn_custom(filenames_array, labels_array, batch_size):
# Reads an image from a file, decodes it into a dense tensor, and resizes it to a fixed shape.
def _parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_png(image_string)
image_resized = tf.image.resize_images(image_decoded, [40, 40])
return image_resized, label
filenames = tf.constant(filenames_array)
labels = tf.constant(labels_array)
# Convert the inputs to a Dataset
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
# Convert filenames to a decoded image
dataset = dataset.map(_parse_function)
# Shuffle, repeat, and batch the examples.
dataset = dataset.batch(batch_size)
# Build the Iterator, and return the read end of the pipeline.
return dataset.make_one_shot_iterator().get_next()
def eval_label_custom():
return [0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
def main(self):
tf.logging.set_verbosity(tf.logging.INFO)
filenames_train = ['blackcorner-data/1.png', 'blackcorner-data/2.png', 'blackcorner-data/3.png',
'blackcorner-data/4.png', 'blackcorner-data/n1.png', 'blackcorner-data/n2.png',
'blackcorner-data/n3.png', 'blackcorner-data/n4.png',
'blackcorner-data/11.png', 'blackcorner-data/21.png', 'blackcorner-data/31.png',
'blackcorner-data/41.png', 'blackcorner-data/n11.png', 'blackcorner-data/n21.png',
'blackcorner-data/n31.png', 'blackcorner-data/n41.png'
]
filenames_eval = ['blackcorner-data-eval/1.png', 'blackcorner-data-eval/2.png', 'blackcorner-data-eval/3.png',
'blackcorner-data-eval/4.png', 'blackcorner-data-eval/n1.png', 'blackcorner-data-eval/n2.png',
'blackcorner-data-eval/n3.png', 'blackcorner-data-eval/n4.png',
'blackcorner-data-eval/11.png', 'blackcorner-data-eval/21.png', 'blackcorner-data-eval/31.png',
'blackcorner-data-eval/41.png', 'blackcorner-data-eval/n11.png', 'blackcorner-data-eval/n21.png',
'blackcorner-data-eval/n31.png', 'blackcorner-data-eval/n41.png'
]
# Create the Estimator
mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn, model_dir="/tmp/test_convnet_model")
# Set up logging for predictions
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=50)
# Train the model
cust_train_input_fn = lambda: train_input_fn_custom(
filenames_array=filenames_train, labels_array=train_label_custom(), batch_size=1)
mnist_classifier.train(
input_fn=cust_train_input_fn,
steps=20000,
hooks=[logging_hook])
# Evaluate the model and print results
# eval_input_fn = tf.estimator.inputs.numpy_input_fn(
# x={"x": eval_data},
# y=eval_labels,
# num_epochs=1,
# shuffle=False)
# eval_results = mnist_classifier.evaluate(input_fn=cust_eval_input_fn)
# print("======================")
# print("Eval results : ")
# print(eval_results)
if __name__ == "__main__":
tf.app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment