In [1]:
import tensorflow as tf
import numpy as np
print(tf.__version__)
tf.logging.set_verbosity(tf.logging.INFO)
In [2]:
#download mnist data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
In [3]:
#start interactive session
sess = tf.InteractiveSession(config=tf.ConfigProto(log_device_placement=True))
In [4]:
#data exploration
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(np.reshape(mnist.train.images[0], [28, 28]), cmap='gray') #current shape is 28 x 28
plt.show()
In [5]:
mnist_train_data = np.array([x for (x,y) in zip(mnist.train.images,mnist.train.labels) if y[8]==0 and y[9]==0])
mnist_train_labels = np.array([y[0:8] for y in mnist.train.labels if y[8]==0 and y[9]==0])
In [6]:
mnist_train_data.shape, mnist_train_labels.shape
Out[6]:
In [7]:
mnist_test_data = np.array([x for (x,y) in zip(mnist.test.images,mnist.test.labels) if y[8]==0 and y[9]==0])
mnist_test_labels = np.array([y[0:8] for y in mnist.test.labels if y[8]==0 and y[9]==0])
In [8]:
mnist_test_data.shape, mnist_test_labels.shape
Out[8]:
In [13]:
def resize_by_padding(images):
resized_images = []
print("Total", len(images), "images..")
for i in range(len(images)):
if i%1000 == 0:
print("Resized", i, "images till now...")
"""
t= tf.constant(images[i], shape=[28,28])
paddings=tf.constant([[2,2], [2,2]])
var = tf.pad(t, paddings, "CONSTANT") #converting to array
b = tf.reshape(var,[1024])
tempnmpy = b.eval()
"""
tempnmpy = np.zeros([32,32])
b = np.reshape(images[i], [28, 28])
tempnmpy[:b.shape[0], :b.shape[1]] = b
tempnmpy = tempnmpy.reshape(1024)
#print(tempnmpy)
#plt.imshow(np.reshape(tempnmpy, [32, 32]), cmap='gray')
#plt.show()
resized_images.append(tempnmpy)
return np.array(resized_images)
In [14]:
mnist_train_data = resize_by_padding(mnist_train_data)
In [15]:
mnist_test_data = resize_by_padding(mnist_test_data)
New shapes:
In [16]:
mnist_train_data.shape, mnist_train_labels.shape, mnist_test_data.shape, mnist_test_labels.shape
Out[16]:
In [17]:
x = tf.placeholder(tf.float16, shape=[None, 1024])
y_ = tf.placeholder(tf.float16, shape=[None, 8])
In [18]:
W = tf.Variable(tf.zeros([1024,8], dtype=tf.float16), dtype=tf.float16)
b = tf.Variable(tf.zeros([8], dtype=tf.float16), dtype=tf.float16)
In [19]:
#run session
sess.run(tf.global_variables_initializer())
In [20]:
y = tf.matmul(x,W) + b
In [21]:
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
In [22]:
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
In [24]:
#get batches for training
def get_batch(X, y, batch_size = 100):
n = X.shape[0]
batch = np.floor(np.random.rand(batch_size)*n).astype(int)
batch_x = X[batch,:]
batch_y = y[batch,:]
return batch_x, batch_y
In [25]:
#train:
for _ in range(1000):
batch_x, batch_y = get_batch(mnist_train_data, mnist_train_labels, 100)
train_step.run(feed_dict={x: batch_x, y_: batch_y})
In [29]:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
In [30]:
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float16))
In [31]:
#Accuracy
print(sess.run(accuracy, feed_dict={x: mnist_test_data, y_: mnist_test_labels}))
In [32]:
# Predict single images
n_images = 4
# Get images from test set
test_images = mnist_test_data[:n_images]
In [33]:
test_labels = mnist_test_labels[:n_images]
In [34]:
# Prepare the input data
input_fn = tf.estimator.inputs.numpy_input_fn(
x={'images': test_images}, shuffle=False)
In [35]:
prediction=tf.argmax(y,1)
In [36]:
preds = prediction.eval(feed_dict={x: test_images}, session=sess)
In [38]:
# Display
for i in range(n_images):
plt.imshow(np.reshape(test_images[i], [32, 32]), cmap='gray')
plt.show()
print("Model prediction:", preds[i])