Skip to content
Snippets Groups Projects
Commit 1b1114e0 authored by Oiwi Parker Jones's avatar Oiwi Parker Jones
Browse files

example convnet

parent 91584d90
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Convolutional Neural Network Example
Build a convolutional neural network with TensorFlow.
This example is using TensorFlow layers API, see 'convolutional_network_raw' example
for a raw TensorFlow implementation with variables.
- Author: Aymeric Damien
- Project: https://github.com/aymericdamien/TensorFlow-Examples/
%% Cell type:markdown id: tags:
## MNIST Dataset Overview
This example is using MNIST handwritten digits. The dataset contains 60,000 examples for training and 10,000 examples for testing. The digits have been size-normalized and centered in a fixed-size image (28x28 pixels) with values from 0 to 1. For simplicity, each image has been flattened and converted to a 1-D numpy array of 784 features (28*28).
![MNIST Dataset](http://neuralnetworksanddeeplearning.com/images/mnist_100_digits.png)
More info: http://yann.lecun.com/exdb/mnist/
## CNN Overview
![CNN](http://personal.ie.cuhk.edu.hk/~ccloy/project_target_code/images/fig3.png)
%% Cell type:code id: tags:
``` python
from __future__ import division, print_function, absolute_import
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
```
%% Output
Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
%% Cell type:code id: tags:
``` python
# Training Parameters
learning_rate = 0.001
num_steps = 2000
batch_size = 128
# Network Parameters
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)
dropout = 0.25 # Dropout, probability to drop a unit
```
%% Cell type:code id: tags:
``` python
# Create the neural network
def conv_net(x_dict, n_classes, dropout, reuse, is_training):
# Define a scope for reusing the variables
with tf.variable_scope('ConvNet', reuse=reuse):
# TF Estimator input is a dict, in case of multiple inputs
x = x_dict['images']
# MNIST data input is a 1-D vector of 784 features (28*28 pixels)
# Reshape to match picture format [Height x Width x Channel]
# Tensor input become 4-D: [Batch Size, Height, Width, Channel]
x = tf.reshape(x, shape=[-1, 28, 28, 1])
# Convolution Layer with 32 filters and a kernel size of 5
conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
# Max Pooling (down-sampling) with strides of 2 and kernel size of 2
conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
# Convolution Layer with 64 filters and a kernel size of 3
conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
# Max Pooling (down-sampling) with strides of 2 and kernel size of 2
conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
# Flatten the data to a 1-D vector for the fully connected layer
fc1 = tf.contrib.layers.flatten(conv2)
# Fully connected layer (in tf contrib folder for now)
fc1 = tf.layers.dense(fc1, 1024)
# Apply Dropout (if is_training is False, dropout is not applied)
fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
# Output layer, class prediction
out = tf.layers.dense(fc1, n_classes)
return out
```
%% Cell type:code id: tags:
``` python
# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):
# Build the neural network
# Because Dropout have different behavior at training and prediction time, we
# need to create 2 distinct computation graphs that still share the same weights.
logits_train = conv_net(features, num_classes, dropout, reuse=False, is_training=True)
logits_test = conv_net(features, num_classes, dropout, reuse=True, is_training=False)
# Predictions
pred_classes = tf.argmax(logits_test, axis=1)
pred_probas = tf.nn.softmax(logits_test)
# If prediction mode, early return
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
# Define loss and optimizer
loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
# Evaluate the accuracy of the model
acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
# TF Estimators requires to return a EstimatorSpec, that specify
# the different ops for training, evaluating, ...
estim_specs = tf.estimator.EstimatorSpec(
mode=mode,
predictions=pred_classes,
loss=loss_op,
train_op=train_op,
eval_metric_ops={'accuracy': acc_op})
return estim_specs
```
%% Cell type:code id: tags:
``` python
# Build the Estimator (i.e. class for training and evaluating network)
model = tf.estimator.Estimator(model_fn)
```
%% Output
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/c3/rhg93t4n0cjbgvkb9tp99_h40000gq/T/tmpqhkrjnut
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {}
%% Cell type:code id: tags:
``` python
# Define the input function for training
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.train.images}, y=mnist.train.labels,
batch_size=batch_size, num_epochs=None, shuffle=True)
# Train the Model
model.train(input_fn, steps=num_steps)
```
%% Output
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /var/folders/c3/rhg93t4n0cjbgvkb9tp99_h40000gq/T/tmpqhkrjnut/model.ckpt.
INFO:tensorflow:step = 1, loss = 2.32753
INFO:tensorflow:global_step/sec: 12.1422
INFO:tensorflow:step = 101, loss = 0.127041 (8.237 sec)
INFO:tensorflow:global_step/sec: 11.4857
INFO:tensorflow:step = 201, loss = 0.0393114 (8.707 sec)
INFO:tensorflow:global_step/sec: 8.48714
INFO:tensorflow:step = 301, loss = 0.0843611 (11.783 sec)
INFO:tensorflow:global_step/sec: 7.15251
INFO:tensorflow:step = 401, loss = 0.0262136 (13.981 sec)
INFO:tensorflow:global_step/sec: 6.4515
INFO:tensorflow:step = 501, loss = 0.0497303 (15.500 sec)
INFO:tensorflow:global_step/sec: 6.70897
INFO:tensorflow:step = 601, loss = 0.0142919 (14.905 sec)
INFO:tensorflow:global_step/sec: 6.6801
INFO:tensorflow:step = 701, loss = 0.042797 (14.970 sec)
INFO:tensorflow:global_step/sec: 6.33292
INFO:tensorflow:step = 801, loss = 0.0159051 (15.790 sec)
INFO:tensorflow:global_step/sec: 6.51359
INFO:tensorflow:step = 901, loss = 0.0560886 (15.352 sec)
INFO:tensorflow:global_step/sec: 7.00529
INFO:tensorflow:step = 1001, loss = 0.0389398 (14.275 sec)
INFO:tensorflow:global_step/sec: 11.4372
INFO:tensorflow:step = 1101, loss = 0.000826523 (8.743 sec)
INFO:tensorflow:global_step/sec: 11.8734
INFO:tensorflow:step = 1201, loss = 0.0288895 (8.422 sec)
INFO:tensorflow:global_step/sec: 11.4033
INFO:tensorflow:step = 1301, loss = 0.0109369 (8.769 sec)
INFO:tensorflow:global_step/sec: 12.1424
INFO:tensorflow:step = 1401, loss = 0.016869 (8.236 sec)
INFO:tensorflow:global_step/sec: 12.2184
INFO:tensorflow:step = 1501, loss = 0.025425 (8.184 sec)
INFO:tensorflow:global_step/sec: 12.9909
INFO:tensorflow:step = 1601, loss = 0.00609953 (7.698 sec)
INFO:tensorflow:global_step/sec: 12.3233
INFO:tensorflow:step = 1701, loss = 0.0149776 (8.114 sec)
INFO:tensorflow:global_step/sec: 12.452
INFO:tensorflow:step = 1801, loss = 0.0356573 (8.031 sec)
INFO:tensorflow:global_step/sec: 12.6611
INFO:tensorflow:step = 1901, loss = 0.0126303 (7.898 sec)
INFO:tensorflow:Saving checkpoints for 2000 into /var/folders/c3/rhg93t4n0cjbgvkb9tp99_h40000gq/T/tmpqhkrjnut/model.ckpt.
INFO:tensorflow:Loss for final step: 0.0101263.
<tensorflow.python.estimator.estimator.Estimator at 0x124517438>
%% Cell type:code id: tags:
``` python
# Evaluate the Model
# Define the input function for evaluating
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': mnist.test.images}, y=mnist.test.labels,
batch_size=batch_size, shuffle=False)
# Use the Estimator 'evaluate' method
model.evaluate(input_fn)
```
%% Output
INFO:tensorflow:Starting evaluation at 2018-02-22-18:15:46
INFO:tensorflow:Restoring parameters from /var/folders/c3/rhg93t4n0cjbgvkb9tp99_h40000gq/T/tmpqhkrjnut/model.ckpt-2000
INFO:tensorflow:Finished evaluation at 2018-02-22-18:15:48
INFO:tensorflow:Saving dict for global step 2000: accuracy = 0.9878, global_step = 2000, loss = 0.0447589
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
{'accuracy': 0.9878, 'global_step': 2000, 'loss': 0.044758931}
%% Cell type:code id: tags:
``` python
# Predict single images
n_images = 4
# Get images from test set
test_images = mnist.test.images[:n_images]
# Prepare the input data
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': test_images}, shuffle=False)
# Use the model to predict the images class
preds = list(model.predict(input_fn))
# Display
for i in range(n_images):
plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
plt.show()
print("Model prediction:", preds[i])
```
%% Output
INFO:tensorflow:Restoring parameters from /var/folders/c3/rhg93t4n0cjbgvkb9tp99_h40000gq/T/tmpqhkrjnut/model.ckpt-2000
Model prediction: 7
Model prediction: 2
Model prediction: 1
Model prediction: 0
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment