In [ ]:
from __future__ import division, print_function, absolute_import
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected, flatten
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression
import matplotlib.pyplot as plt
import random
import numpy as np
In [ ]:
# Data loading and preprocessing
import tflearn.datasets.mnist as mnist
X, Y, testX, testY = mnist.load_data(one_hot=False)
X = X.reshape([-1, 28, 28, 1])
testX = testX.reshape([-1, 28, 28, 1])
In [ ]:
NUM_SAMPLES_TO_GEN = 100
WIDTH_SAMPLE = 2
NUM_IMGS_AVAILABLE = X.shape[0]
In [ ]:
X_seq = np.empty(shape=(NUM_SAMPLES_TO_GEN, 28, 28 * WIDTH_SAMPLE,1),dtype='float32')
Y_seq = np.zeros(shape=(NUM_SAMPLES_TO_GEN, 10**WIDTH_SAMPLE),dtype='float64')
for i in range(NUM_SAMPLES_TO_GEN): # For each sample to generate
indices = np.random.randint(0,NUM_IMGS_AVAILABLE - 1, size=WIDTH_SAMPLE) # generate indices for creating this wide image
X_seq[i] = np.concatenate(X[indices], axis=1)
Y_seq_index = Y[indices[1]] * 10 + Y[indices[0]]
Y_seq[i][Y_seq_index] = 1. # Convert to One hot
In [ ]:
# Building convolutional network
network = input_data(shape=[None, 28, 28 * WIDTH_SAMPLE,1], name='input')
network = conv_2d(network, 32, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation='relu', regularizer="L2")
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = flatten(network)
network = fully_connected(network, 128, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 256, activation='tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 100, activation='softmax')
network = regression(network, optimizer='adam', learning_rate=0.01,
loss='categorical_crossentropy', name='target')
In [ ]:
# Training
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit({'input': X_seq}, {'target': Y_seq}, n_epoch=5,
validation_set=0.1,run_id='convnet_mnist_5', show_metric=True)
In [ ]: