Image Classification Using Deep Learning: “Hello World” Tutorial

Setup

pip install --user tensorflow tflearn scikit-image

Code

# deepneuralnet.pyimport tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.metrics import Accuracy
acc = Accuracy()
network = input_data(shape=[None, 28, 28, 1])
# Conv layers
network = conv_2d(network, 64, 3, strides=1, activation='relu', name = 'conv1_3_3_1')
network = max_pool_2d(network, 2, strides=2)
network = conv_2d(network, 64, 3, strides=1, activation='relu', name = 'conv1_3_3_2')
network = conv_2d(network, 64, 3, strides=1, activation='relu', name = 'conv1_3_3_3')
network = max_pool_2d(network, 2, strides=2)
# Fully Connected Layer
network = fully_connected(network, 1024, activation='tanh')
# Dropout layer
network = dropout(network, 0.5)
# Fully Connected Layer
network = fully_connected(network, 10, activation='softmax')
# Final network
network = regression(network, optimizer='momentum',
loss='categorical_crossentropy',
learning_rate=0.001, metric=acc)
# The model with details on where to save
# Will save in current directory
model = tflearn.DNN(network, checkpoint_path='model-', best_checkpoint_path='best-model-')
# train.pyimport deepneuralnet as net
import tflearn.datasets.mnist as mnist
# Get the model
model = net.model
# Load data
X, Y, testX, testY = mnist.load_data(one_hot=True)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])
model.fit(X, Y, n_epoch=15, validation_set=(testX, testY), show_metric=True, run_id="deep_nn")model.save('final-model.tflearn')

Train

Predict

# predict.pyimport deepneuralnet as net
import random
import tflearn.datasets.mnist as mnist
from skimage import io
model = net.model
path_to_model = 'final-model.tflearn'
_, _, testX, _ = mnist.load_data(one_hot=True)
model.load(path_to_model)
# Randomly take an image from the test set
rand_index = random.randint(0, len(testX) - 1)
x = testX[rand_index].reshape((28, 28, 1))
result = model.predict([x])[0] # Predict
prediction = result.index(max(result)) # The index represents the number predicted in this case
print("Prediction", prediction)
io.imsave('testimage.jpg', x.reshape(28, 28)) # This shows the image in the computer for you to see

Predict (Custom Image File)

# predict_custom_image.pyimport deepneuralnet as net
import random
import tflearn.datasets.mnist as mnist
import numpy as np
from skimage import io
model = net.model
path_to_model = 'final-model.tflearn'
path_to_image = 'custom.jpg' # Change this to the file path/name of the image file you want to use
model.load(path_to_model)# Load image (normalized)
x = io.imread(path_to_image).reshape((28, 28, 1)).astype(np.float) / 255
result = model.predict([x])[0] # Predict
prediction = result.index(max(result)) # The index represents the number predicted in this case
print("Prediction", prediction)

Shehzad Noor Taus Priyo

Written by

Deep Learning Software Engineer at NVIDIA. International Scholar at UBC.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade