In [1]:
#importing necessary packages
import numpy as np
import matplotlib.pyplot as plt 
import tensorflow as tf
import tflearn
import tensorflow as tf
from PIL import Image
%matplotlib inline
#for writing text files
import glob
import os     
import random 
#reading images from a text file
from tflearn.data_utils import image_preloader
import math


hdf5 is not supported on this machine (please install/reinstall h5py for optimal experience)

Data processing


In [2]:
IMAGE_FOLDER = '/Users/Enkay/Documents/Viky/python/img-classification/train'
TRAIN_DATA = '/Users/Enkay/Documents/Viky/python/img-classification/training_data.txt'
TEST_DATA = '/Users/Enkay/Documents/Viky/python/img-classification/test_data.txt'
VALIDATION_DATA = '/Users/Enkay/Documents/Viky/python/img-classification/validation_data.txt'
train_proportion=0.7
test_proportion=0.2
validation_proportion=0.1

In [3]:
#read the image directories
filenames_image = os.listdir(IMAGE_FOLDER)
#shuffling the data is important otherwise the model will be fed with a single class data for a long time and 
#network will not learn properly
random.shuffle(filenames_image)

In [4]:
#total number of images
total=len(filenames_image)
##  *****training data******** 
fr = open(TRAIN_DATA, 'w')
train_files=filenames_image[0: int(train_proportion*total)]
for filename in train_files:
    if filename[0:3] == 'cat':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:3] == 'dog':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')

fr.close()
##  *****testing data******** 
fr = open(TEST_DATA, 'w')
test_files=filenames_image[int(math.ceil(train_proportion*total)):int(math.ceil((train_proportion+test_proportion)*total))]
for filename in test_files:
    if filename[0:3] == 'cat':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:3] == 'dog':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
fr.close()

##  *****validation data******** 
fr = open(VALIDATION_DATA, 'w')
valid_files=filenames_image[int(math.ceil((train_proportion+test_proportion)*total)):total]
for filename in valid_files:
    if filename[0:3] == 'cat':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 0\n')
    elif filename[0:3] == 'dog':
        fr.write(IMAGE_FOLDER + '/'+ filename + ' 1\n')
fr.close()

In [5]:
#Importing data
X_train, Y_train = image_preloader(TRAIN_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)
X_test, Y_test = image_preloader(TEST_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)
X_val, Y_val = image_preloader(VALIDATION_DATA, image_shape=(56,56),mode='file', categorical_labels=True,normalize=True)

In [6]:
print ("Dataset")
print ("Number of training images {}".format(len(X_train)))
print ("Number of testing images {}".format(len(X_test)))
print ("Number of validation images {}".format(len(X_val)))
print ("Shape of an image {}" .format(X_train[1].shape))
print ("Shape of label:{} ,number of classes: {}".format(Y_train[1].shape,len(Y_train[1])))


Dataset
Number of training images 17500
Number of testing images 5000
Number of validation images 2500
Shape of an image (56, 56, 3)
Shape of label:(2,) ,number of classes: 2

In [7]:
#Sample Image 
plt.imshow(X_train[1])
plt.axis('off')
plt.title('Sample image with label {}'.format(Y_train[1]))
plt.show()


Model - AlexNet


In [8]:
#input image
x=tf.placeholder(tf.float32,shape=[None,56,56,3] , name='input_image') 
#input class
y_=tf.placeholder(tf.float32,shape=[None, 2] , name='input_class')

In [9]:
input_layer=x
#convolutional layer 1 --convolution+RELU activation
conv_layer1=tflearn.layers.conv.conv_2d(input_layer, nb_filter=64, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu', regularizer="L2", name='conv_layer_1')

#2x2 max pooling layer
out_layer1=tflearn.layers.conv.max_pool_2d(conv_layer1, 2)


#second convolutional layer 
conv_layer2=tflearn.layers.conv.conv_2d(out_layer1, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')
out_layer2=tflearn.layers.conv.max_pool_2d(conv_layer2, 2)
# third convolutional layer
conv_layer3=tflearn.layers.conv.conv_2d(out_layer2, nb_filter=128, filter_size=5, strides=[1,1,1,1],
                                        padding='same', activation='relu',  regularizer="L2", name='conv_layer_2')
out_layer3=tflearn.layers.conv.max_pool_2d(conv_layer3, 2)

#fully connected layer1
fcl= tflearn.layers.core.fully_connected(out_layer3, 4096, activation='relu' , name='FCL-1')
fcl_dropout_1 = tflearn.layers.core.dropout(fcl, 0.8)
#fully connected layer2
fc2= tflearn.layers.core.fully_connected(fcl_dropout_1, 4096, activation='relu' , name='FCL-2')
fcl_dropout_2 = tflearn.layers.core.dropout(fc2, 0.8)
#softmax layer output
y_predicted = tflearn.layers.core.fully_connected(fcl_dropout_2, 2, activation='softmax', name='output')

In [10]:
#loss function
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_predicted+np.exp(-10)), reduction_indices=[1]))
#optimiser -
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
#calculating accuracy of our model 
correct_prediction = tf.equal(tf.argmax(y_predicted,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

In [11]:
# session parameters
sess = tf.InteractiveSession()
#initialising variables
init = tf.global_variables_initializer()
sess.run(init)
saver = tf.train.Saver()
save_path="/Users/Enkay/Documents/Viky/python/img-classification/mark3.ckpt"

In [12]:
# grabbing the default graph
g = tf.get_default_graph()

# every operations in our graph
[op.name for op in g.get_operations()]


Out[12]:
['is_training/Initializer/Const',
 'is_training',
 'is_training/Assign',
 'is_training/read',
 'Assign/value',
 'Assign',
 'Assign_1/value',
 'Assign_1',
 'input_image',
 'input_class',
 'conv_layer_1/W/Initializer/random_uniform/shape',
 'conv_layer_1/W/Initializer/random_uniform/min',
 'conv_layer_1/W/Initializer/random_uniform/max',
 'conv_layer_1/W/Initializer/random_uniform/RandomUniform',
 'conv_layer_1/W/Initializer/random_uniform/sub',
 'conv_layer_1/W/Initializer/random_uniform/mul',
 'conv_layer_1/W/Initializer/random_uniform',
 'conv_layer_1/W',
 'conv_layer_1/W/Assign',
 'conv_layer_1/W/read',
 'conv_layer_1/W/Regularizer/L2Loss',
 'conv_layer_1/W/Regularizer/L2-Loss/y',
 'conv_layer_1/W/Regularizer/L2-Loss',
 'conv_layer_1/b/Initializer/Const',
 'conv_layer_1/b',
 'conv_layer_1/b/Assign',
 'conv_layer_1/b/read',
 'conv_layer_1/Conv2D',
 'conv_layer_1/BiasAdd',
 'conv_layer_1/Relu',
 'MaxPool2D/MaxPool',
 'conv_layer_2/W/Initializer/random_uniform/shape',
 'conv_layer_2/W/Initializer/random_uniform/min',
 'conv_layer_2/W/Initializer/random_uniform/max',
 'conv_layer_2/W/Initializer/random_uniform/RandomUniform',
 'conv_layer_2/W/Initializer/random_uniform/sub',
 'conv_layer_2/W/Initializer/random_uniform/mul',
 'conv_layer_2/W/Initializer/random_uniform',
 'conv_layer_2/W',
 'conv_layer_2/W/Assign',
 'conv_layer_2/W/read',
 'conv_layer_2/W/Regularizer/L2Loss',
 'conv_layer_2/W/Regularizer/L2-Loss/y',
 'conv_layer_2/W/Regularizer/L2-Loss',
 'conv_layer_2/b/Initializer/Const',
 'conv_layer_2/b',
 'conv_layer_2/b/Assign',
 'conv_layer_2/b/read',
 'conv_layer_2/Conv2D',
 'conv_layer_2/BiasAdd',
 'conv_layer_2/Relu',
 'MaxPool2D_1/MaxPool',
 'conv_layer_2_1/W/Initializer/random_uniform/shape',
 'conv_layer_2_1/W/Initializer/random_uniform/min',
 'conv_layer_2_1/W/Initializer/random_uniform/max',
 'conv_layer_2_1/W/Initializer/random_uniform/RandomUniform',
 'conv_layer_2_1/W/Initializer/random_uniform/sub',
 'conv_layer_2_1/W/Initializer/random_uniform/mul',
 'conv_layer_2_1/W/Initializer/random_uniform',
 'conv_layer_2_1/W',
 'conv_layer_2_1/W/Assign',
 'conv_layer_2_1/W/read',
 'conv_layer_2_1/W/Regularizer/L2Loss',
 'conv_layer_2_1/W/Regularizer/L2-Loss/y',
 'conv_layer_2_1/W/Regularizer/L2-Loss',
 'conv_layer_2_1/b/Initializer/Const',
 'conv_layer_2_1/b',
 'conv_layer_2_1/b/Assign',
 'conv_layer_2_1/b/read',
 'conv_layer_2_1/Conv2D',
 'conv_layer_2_1/BiasAdd',
 'conv_layer_2_1/Relu',
 'MaxPool2D_2/MaxPool',
 'FCL-1/W/Initializer/truncated_normal/shape',
 'FCL-1/W/Initializer/truncated_normal/mean',
 'FCL-1/W/Initializer/truncated_normal/stddev',
 'FCL-1/W/Initializer/truncated_normal/TruncatedNormal',
 'FCL-1/W/Initializer/truncated_normal/mul',
 'FCL-1/W/Initializer/truncated_normal',
 'FCL-1/W',
 'FCL-1/W/Assign',
 'FCL-1/W/read',
 'FCL-1/b/Initializer/Const',
 'FCL-1/b',
 'FCL-1/b/Assign',
 'FCL-1/b/read',
 'FCL-1/Reshape/shape',
 'FCL-1/Reshape',
 'FCL-1/MatMul',
 'FCL-1/BiasAdd',
 'FCL-1/Relu',
 'Dropout/cond/Switch',
 'Dropout/cond/switch_t',
 'Dropout/cond/switch_f',
 'Dropout/cond/pred_id',
 'Dropout/cond/dropout/keep_prob',
 'Dropout/cond/dropout/Shape/Switch',
 'Dropout/cond/dropout/Shape',
 'Dropout/cond/dropout/random_uniform/min',
 'Dropout/cond/dropout/random_uniform/max',
 'Dropout/cond/dropout/random_uniform/RandomUniform',
 'Dropout/cond/dropout/random_uniform/sub',
 'Dropout/cond/dropout/random_uniform/mul',
 'Dropout/cond/dropout/random_uniform',
 'Dropout/cond/dropout/add',
 'Dropout/cond/dropout/Floor',
 'Dropout/cond/dropout/div',
 'Dropout/cond/dropout/mul',
 'Dropout/cond/Switch_1',
 'Dropout/cond/Merge',
 'FCL-2/W/Initializer/truncated_normal/shape',
 'FCL-2/W/Initializer/truncated_normal/mean',
 'FCL-2/W/Initializer/truncated_normal/stddev',
 'FCL-2/W/Initializer/truncated_normal/TruncatedNormal',
 'FCL-2/W/Initializer/truncated_normal/mul',
 'FCL-2/W/Initializer/truncated_normal',
 'FCL-2/W',
 'FCL-2/W/Assign',
 'FCL-2/W/read',
 'FCL-2/b/Initializer/Const',
 'FCL-2/b',
 'FCL-2/b/Assign',
 'FCL-2/b/read',
 'FCL-2/MatMul',
 'FCL-2/BiasAdd',
 'FCL-2/Relu',
 'Dropout_1/cond/Switch',
 'Dropout_1/cond/switch_t',
 'Dropout_1/cond/switch_f',
 'Dropout_1/cond/pred_id',
 'Dropout_1/cond/dropout/keep_prob',
 'Dropout_1/cond/dropout/Shape/Switch',
 'Dropout_1/cond/dropout/Shape',
 'Dropout_1/cond/dropout/random_uniform/min',
 'Dropout_1/cond/dropout/random_uniform/max',
 'Dropout_1/cond/dropout/random_uniform/RandomUniform',
 'Dropout_1/cond/dropout/random_uniform/sub',
 'Dropout_1/cond/dropout/random_uniform/mul',
 'Dropout_1/cond/dropout/random_uniform',
 'Dropout_1/cond/dropout/add',
 'Dropout_1/cond/dropout/Floor',
 'Dropout_1/cond/dropout/div',
 'Dropout_1/cond/dropout/mul',
 'Dropout_1/cond/Switch_1',
 'Dropout_1/cond/Merge',
 'output/W/Initializer/truncated_normal/shape',
 'output/W/Initializer/truncated_normal/mean',
 'output/W/Initializer/truncated_normal/stddev',
 'output/W/Initializer/truncated_normal/TruncatedNormal',
 'output/W/Initializer/truncated_normal/mul',
 'output/W/Initializer/truncated_normal',
 'output/W',
 'output/W/Assign',
 'output/W/read',
 'output/b/Initializer/Const',
 'output/b',
 'output/b/Assign',
 'output/b/read',
 'output/MatMul',
 'output/BiasAdd',
 'output/Softmax',
 'add/y',
 'add',
 'Log',
 'mul',
 'Sum/reduction_indices',
 'Sum',
 'Neg',
 'Const',
 'Mean',
 'gradients/Shape',
 'gradients/Const',
 'gradients/Fill',
 'gradients/Mean_grad/Reshape/shape',
 'gradients/Mean_grad/Reshape',
 'gradients/Mean_grad/Shape',
 'gradients/Mean_grad/Tile',
 'gradients/Mean_grad/Shape_1',
 'gradients/Mean_grad/Shape_2',
 'gradients/Mean_grad/Const',
 'gradients/Mean_grad/Prod',
 'gradients/Mean_grad/Const_1',
 'gradients/Mean_grad/Prod_1',
 'gradients/Mean_grad/Maximum/y',
 'gradients/Mean_grad/Maximum',
 'gradients/Mean_grad/floordiv',
 'gradients/Mean_grad/Cast',
 'gradients/Mean_grad/truediv',
 'gradients/Neg_grad/Neg',
 'gradients/Sum_grad/Shape',
 'gradients/Sum_grad/Size',
 'gradients/Sum_grad/add',
 'gradients/Sum_grad/mod',
 'gradients/Sum_grad/Shape_1',
 'gradients/Sum_grad/range/start',
 'gradients/Sum_grad/range/delta',
 'gradients/Sum_grad/range',
 'gradients/Sum_grad/Fill/value',
 'gradients/Sum_grad/Fill',
 'gradients/Sum_grad/DynamicStitch',
 'gradients/Sum_grad/Maximum/y',
 'gradients/Sum_grad/Maximum',
 'gradients/Sum_grad/floordiv',
 'gradients/Sum_grad/Reshape',
 'gradients/Sum_grad/Tile',
 'gradients/mul_grad/Shape',
 'gradients/mul_grad/Shape_1',
 'gradients/mul_grad/BroadcastGradientArgs',
 'gradients/mul_grad/mul',
 'gradients/mul_grad/Sum',
 'gradients/mul_grad/Reshape',
 'gradients/mul_grad/mul_1',
 'gradients/mul_grad/Sum_1',
 'gradients/mul_grad/Reshape_1',
 'gradients/mul_grad/tuple/group_deps',
 'gradients/mul_grad/tuple/control_dependency',
 'gradients/mul_grad/tuple/control_dependency_1',
 'gradients/Log_grad/Reciprocal',
 'gradients/Log_grad/mul',
 'gradients/add_grad/Shape',
 'gradients/add_grad/Shape_1',
 'gradients/add_grad/BroadcastGradientArgs',
 'gradients/add_grad/Sum',
 'gradients/add_grad/Reshape',
 'gradients/add_grad/Sum_1',
 'gradients/add_grad/Reshape_1',
 'gradients/add_grad/tuple/group_deps',
 'gradients/add_grad/tuple/control_dependency',
 'gradients/add_grad/tuple/control_dependency_1',
 'gradients/output/Softmax_grad/mul',
 'gradients/output/Softmax_grad/Sum/reduction_indices',
 'gradients/output/Softmax_grad/Sum',
 'gradients/output/Softmax_grad/Reshape/shape',
 'gradients/output/Softmax_grad/Reshape',
 'gradients/output/Softmax_grad/sub',
 'gradients/output/Softmax_grad/mul_1',
 'gradients/output/BiasAdd_grad/BiasAddGrad',
 'gradients/output/BiasAdd_grad/tuple/group_deps',
 'gradients/output/BiasAdd_grad/tuple/control_dependency',
 'gradients/output/BiasAdd_grad/tuple/control_dependency_1',
 'gradients/output/MatMul_grad/MatMul',
 'gradients/output/MatMul_grad/MatMul_1',
 'gradients/output/MatMul_grad/tuple/group_deps',
 'gradients/output/MatMul_grad/tuple/control_dependency',
 'gradients/output/MatMul_grad/tuple/control_dependency_1',
 'gradients/Dropout_1/cond/Merge_grad/cond_grad',
 'gradients/Dropout_1/cond/Merge_grad/tuple/group_deps',
 'gradients/Dropout_1/cond/Merge_grad/tuple/control_dependency',
 'gradients/Dropout_1/cond/Merge_grad/tuple/control_dependency_1',
 'gradients/Switch',
 'gradients/Shape_1',
 'gradients/zeros/Const',
 'gradients/zeros',
 'gradients/Dropout_1/cond/Switch_1_grad/cond_grad',
 'gradients/Dropout_1/cond/dropout/mul_grad/Shape',
 'gradients/Dropout_1/cond/dropout/mul_grad/Shape_1',
 'gradients/Dropout_1/cond/dropout/mul_grad/BroadcastGradientArgs',
 'gradients/Dropout_1/cond/dropout/mul_grad/mul',
 'gradients/Dropout_1/cond/dropout/mul_grad/Sum',
 'gradients/Dropout_1/cond/dropout/mul_grad/Reshape',
 'gradients/Dropout_1/cond/dropout/mul_grad/mul_1',
 'gradients/Dropout_1/cond/dropout/mul_grad/Sum_1',
 'gradients/Dropout_1/cond/dropout/mul_grad/Reshape_1',
 'gradients/Dropout_1/cond/dropout/mul_grad/tuple/group_deps',
 'gradients/Dropout_1/cond/dropout/mul_grad/tuple/control_dependency',
 'gradients/Dropout_1/cond/dropout/mul_grad/tuple/control_dependency_1',
 'gradients/Dropout_1/cond/dropout/div_grad/Shape',
 'gradients/Dropout_1/cond/dropout/div_grad/Shape_1',
 'gradients/Dropout_1/cond/dropout/div_grad/BroadcastGradientArgs',
 'gradients/Dropout_1/cond/dropout/div_grad/RealDiv',
 'gradients/Dropout_1/cond/dropout/div_grad/Sum',
 'gradients/Dropout_1/cond/dropout/div_grad/Reshape',
 'gradients/Dropout_1/cond/dropout/div_grad/Neg',
 'gradients/Dropout_1/cond/dropout/div_grad/RealDiv_1',
 'gradients/Dropout_1/cond/dropout/div_grad/RealDiv_2',
 'gradients/Dropout_1/cond/dropout/div_grad/mul',
 'gradients/Dropout_1/cond/dropout/div_grad/Sum_1',
 'gradients/Dropout_1/cond/dropout/div_grad/Reshape_1',
 'gradients/Dropout_1/cond/dropout/div_grad/tuple/group_deps',
 'gradients/Dropout_1/cond/dropout/div_grad/tuple/control_dependency',
 'gradients/Dropout_1/cond/dropout/div_grad/tuple/control_dependency_1',
 'gradients/Switch_1',
 'gradients/Shape_2',
 'gradients/zeros_1/Const',
 'gradients/zeros_1',
 'gradients/Dropout_1/cond/dropout/Shape/Switch_grad/cond_grad',
 'gradients/AddN',
 'gradients/FCL-2/Relu_grad/ReluGrad',
 'gradients/FCL-2/BiasAdd_grad/BiasAddGrad',
 'gradients/FCL-2/BiasAdd_grad/tuple/group_deps',
 'gradients/FCL-2/BiasAdd_grad/tuple/control_dependency',
 'gradients/FCL-2/BiasAdd_grad/tuple/control_dependency_1',
 'gradients/FCL-2/MatMul_grad/MatMul',
 'gradients/FCL-2/MatMul_grad/MatMul_1',
 'gradients/FCL-2/MatMul_grad/tuple/group_deps',
 'gradients/FCL-2/MatMul_grad/tuple/control_dependency',
 'gradients/FCL-2/MatMul_grad/tuple/control_dependency_1',
 'gradients/Dropout/cond/Merge_grad/cond_grad',
 'gradients/Dropout/cond/Merge_grad/tuple/group_deps',
 'gradients/Dropout/cond/Merge_grad/tuple/control_dependency',
 'gradients/Dropout/cond/Merge_grad/tuple/control_dependency_1',
 'gradients/Switch_2',
 'gradients/Shape_3',
 'gradients/zeros_2/Const',
 'gradients/zeros_2',
 'gradients/Dropout/cond/Switch_1_grad/cond_grad',
 'gradients/Dropout/cond/dropout/mul_grad/Shape',
 'gradients/Dropout/cond/dropout/mul_grad/Shape_1',
 'gradients/Dropout/cond/dropout/mul_grad/BroadcastGradientArgs',
 'gradients/Dropout/cond/dropout/mul_grad/mul',
 'gradients/Dropout/cond/dropout/mul_grad/Sum',
 'gradients/Dropout/cond/dropout/mul_grad/Reshape',
 'gradients/Dropout/cond/dropout/mul_grad/mul_1',
 'gradients/Dropout/cond/dropout/mul_grad/Sum_1',
 'gradients/Dropout/cond/dropout/mul_grad/Reshape_1',
 'gradients/Dropout/cond/dropout/mul_grad/tuple/group_deps',
 'gradients/Dropout/cond/dropout/mul_grad/tuple/control_dependency',
 'gradients/Dropout/cond/dropout/mul_grad/tuple/control_dependency_1',
 'gradients/Dropout/cond/dropout/div_grad/Shape',
 'gradients/Dropout/cond/dropout/div_grad/Shape_1',
 'gradients/Dropout/cond/dropout/div_grad/BroadcastGradientArgs',
 'gradients/Dropout/cond/dropout/div_grad/RealDiv',
 'gradients/Dropout/cond/dropout/div_grad/Sum',
 'gradients/Dropout/cond/dropout/div_grad/Reshape',
 'gradients/Dropout/cond/dropout/div_grad/Neg',
 'gradients/Dropout/cond/dropout/div_grad/RealDiv_1',
 'gradients/Dropout/cond/dropout/div_grad/RealDiv_2',
 'gradients/Dropout/cond/dropout/div_grad/mul',
 'gradients/Dropout/cond/dropout/div_grad/Sum_1',
 'gradients/Dropout/cond/dropout/div_grad/Reshape_1',
 'gradients/Dropout/cond/dropout/div_grad/tuple/group_deps',
 'gradients/Dropout/cond/dropout/div_grad/tuple/control_dependency',
 'gradients/Dropout/cond/dropout/div_grad/tuple/control_dependency_1',
 'gradients/Switch_3',
 'gradients/Shape_4',
 'gradients/zeros_3/Const',
 'gradients/zeros_3',
 'gradients/Dropout/cond/dropout/Shape/Switch_grad/cond_grad',
 'gradients/AddN_1',
 'gradients/FCL-1/Relu_grad/ReluGrad',
 'gradients/FCL-1/BiasAdd_grad/BiasAddGrad',
 'gradients/FCL-1/BiasAdd_grad/tuple/group_deps',
 'gradients/FCL-1/BiasAdd_grad/tuple/control_dependency',
 'gradients/FCL-1/BiasAdd_grad/tuple/control_dependency_1',
 'gradients/FCL-1/MatMul_grad/MatMul',
 'gradients/FCL-1/MatMul_grad/MatMul_1',
 'gradients/FCL-1/MatMul_grad/tuple/group_deps',
 'gradients/FCL-1/MatMul_grad/tuple/control_dependency',
 'gradients/FCL-1/MatMul_grad/tuple/control_dependency_1',
 'gradients/FCL-1/Reshape_grad/Shape',
 'gradients/FCL-1/Reshape_grad/Reshape',
 'gradients/MaxPool2D_2/MaxPool_grad/MaxPoolGrad',
 'gradients/conv_layer_2_1/Relu_grad/ReluGrad',
 'gradients/conv_layer_2_1/BiasAdd_grad/BiasAddGrad',
 'gradients/conv_layer_2_1/BiasAdd_grad/tuple/group_deps',
 'gradients/conv_layer_2_1/BiasAdd_grad/tuple/control_dependency',
 'gradients/conv_layer_2_1/BiasAdd_grad/tuple/control_dependency_1',
 'gradients/conv_layer_2_1/Conv2D_grad/Shape',
 'gradients/conv_layer_2_1/Conv2D_grad/Conv2DBackpropInput',
 'gradients/conv_layer_2_1/Conv2D_grad/Shape_1',
 'gradients/conv_layer_2_1/Conv2D_grad/Conv2DBackpropFilter',
 'gradients/conv_layer_2_1/Conv2D_grad/tuple/group_deps',
 'gradients/conv_layer_2_1/Conv2D_grad/tuple/control_dependency',
 'gradients/conv_layer_2_1/Conv2D_grad/tuple/control_dependency_1',
 'gradients/MaxPool2D_1/MaxPool_grad/MaxPoolGrad',
 'gradients/conv_layer_2/Relu_grad/ReluGrad',
 'gradients/conv_layer_2/BiasAdd_grad/BiasAddGrad',
 'gradients/conv_layer_2/BiasAdd_grad/tuple/group_deps',
 'gradients/conv_layer_2/BiasAdd_grad/tuple/control_dependency',
 'gradients/conv_layer_2/BiasAdd_grad/tuple/control_dependency_1',
 'gradients/conv_layer_2/Conv2D_grad/Shape',
 'gradients/conv_layer_2/Conv2D_grad/Conv2DBackpropInput',
 'gradients/conv_layer_2/Conv2D_grad/Shape_1',
 'gradients/conv_layer_2/Conv2D_grad/Conv2DBackpropFilter',
 'gradients/conv_layer_2/Conv2D_grad/tuple/group_deps',
 'gradients/conv_layer_2/Conv2D_grad/tuple/control_dependency',
 'gradients/conv_layer_2/Conv2D_grad/tuple/control_dependency_1',
 'gradients/MaxPool2D/MaxPool_grad/MaxPoolGrad',
 'gradients/conv_layer_1/Relu_grad/ReluGrad',
 'gradients/conv_layer_1/BiasAdd_grad/BiasAddGrad',
 'gradients/conv_layer_1/BiasAdd_grad/tuple/group_deps',
 'gradients/conv_layer_1/BiasAdd_grad/tuple/control_dependency',
 'gradients/conv_layer_1/BiasAdd_grad/tuple/control_dependency_1',
 'gradients/conv_layer_1/Conv2D_grad/Shape',
 'gradients/conv_layer_1/Conv2D_grad/Conv2DBackpropInput',
 'gradients/conv_layer_1/Conv2D_grad/Shape_1',
 'gradients/conv_layer_1/Conv2D_grad/Conv2DBackpropFilter',
 'gradients/conv_layer_1/Conv2D_grad/tuple/group_deps',
 'gradients/conv_layer_1/Conv2D_grad/tuple/control_dependency',
 'gradients/conv_layer_1/Conv2D_grad/tuple/control_dependency_1',
 'beta1_power/initial_value',
 'beta1_power',
 'beta1_power/Assign',
 'beta1_power/read',
 'beta2_power/initial_value',
 'beta2_power',
 'beta2_power/Assign',
 'beta2_power/read',
 'conv_layer_1/W/Adam/Initializer/zeros',
 'conv_layer_1/W/Adam',
 'conv_layer_1/W/Adam/Assign',
 'conv_layer_1/W/Adam/read',
 'conv_layer_1/W/Adam_1/Initializer/zeros',
 'conv_layer_1/W/Adam_1',
 'conv_layer_1/W/Adam_1/Assign',
 'conv_layer_1/W/Adam_1/read',
 'conv_layer_1/b/Adam/Initializer/zeros',
 'conv_layer_1/b/Adam',
 'conv_layer_1/b/Adam/Assign',
 'conv_layer_1/b/Adam/read',
 'conv_layer_1/b/Adam_1/Initializer/zeros',
 'conv_layer_1/b/Adam_1',
 'conv_layer_1/b/Adam_1/Assign',
 'conv_layer_1/b/Adam_1/read',
 'conv_layer_2/W/Adam/Initializer/zeros',
 'conv_layer_2/W/Adam',
 'conv_layer_2/W/Adam/Assign',
 'conv_layer_2/W/Adam/read',
 'conv_layer_2/W/Adam_1/Initializer/zeros',
 'conv_layer_2/W/Adam_1',
 'conv_layer_2/W/Adam_1/Assign',
 'conv_layer_2/W/Adam_1/read',
 'conv_layer_2/b/Adam/Initializer/zeros',
 'conv_layer_2/b/Adam',
 'conv_layer_2/b/Adam/Assign',
 'conv_layer_2/b/Adam/read',
 'conv_layer_2/b/Adam_1/Initializer/zeros',
 'conv_layer_2/b/Adam_1',
 'conv_layer_2/b/Adam_1/Assign',
 'conv_layer_2/b/Adam_1/read',
 'conv_layer_2_1/W/Adam/Initializer/zeros',
 'conv_layer_2_1/W/Adam',
 'conv_layer_2_1/W/Adam/Assign',
 'conv_layer_2_1/W/Adam/read',
 'conv_layer_2_1/W/Adam_1/Initializer/zeros',
 'conv_layer_2_1/W/Adam_1',
 'conv_layer_2_1/W/Adam_1/Assign',
 'conv_layer_2_1/W/Adam_1/read',
 'conv_layer_2_1/b/Adam/Initializer/zeros',
 'conv_layer_2_1/b/Adam',
 'conv_layer_2_1/b/Adam/Assign',
 'conv_layer_2_1/b/Adam/read',
 'conv_layer_2_1/b/Adam_1/Initializer/zeros',
 'conv_layer_2_1/b/Adam_1',
 'conv_layer_2_1/b/Adam_1/Assign',
 'conv_layer_2_1/b/Adam_1/read',
 'FCL-1/W/Adam/Initializer/zeros',
 'FCL-1/W/Adam',
 'FCL-1/W/Adam/Assign',
 'FCL-1/W/Adam/read',
 'FCL-1/W/Adam_1/Initializer/zeros',
 'FCL-1/W/Adam_1',
 'FCL-1/W/Adam_1/Assign',
 'FCL-1/W/Adam_1/read',
 'FCL-1/b/Adam/Initializer/zeros',
 'FCL-1/b/Adam',
 'FCL-1/b/Adam/Assign',
 'FCL-1/b/Adam/read',
 'FCL-1/b/Adam_1/Initializer/zeros',
 'FCL-1/b/Adam_1',
 'FCL-1/b/Adam_1/Assign',
 'FCL-1/b/Adam_1/read',
 'FCL-2/W/Adam/Initializer/zeros',
 'FCL-2/W/Adam',
 'FCL-2/W/Adam/Assign',
 'FCL-2/W/Adam/read',
 'FCL-2/W/Adam_1/Initializer/zeros',
 'FCL-2/W/Adam_1',
 'FCL-2/W/Adam_1/Assign',
 'FCL-2/W/Adam_1/read',
 'FCL-2/b/Adam/Initializer/zeros',
 'FCL-2/b/Adam',
 'FCL-2/b/Adam/Assign',
 'FCL-2/b/Adam/read',
 'FCL-2/b/Adam_1/Initializer/zeros',
 'FCL-2/b/Adam_1',
 'FCL-2/b/Adam_1/Assign',
 'FCL-2/b/Adam_1/read',
 'output/W/Adam/Initializer/zeros',
 'output/W/Adam',
 'output/W/Adam/Assign',
 'output/W/Adam/read',
 'output/W/Adam_1/Initializer/zeros',
 'output/W/Adam_1',
 'output/W/Adam_1/Assign',
 'output/W/Adam_1/read',
 'output/b/Adam/Initializer/zeros',
 'output/b/Adam',
 'output/b/Adam/Assign',
 'output/b/Adam/read',
 'output/b/Adam_1/Initializer/zeros',
 'output/b/Adam_1',
 'output/b/Adam_1/Assign',
 'output/b/Adam_1/read',
 'Adam/learning_rate',
 'Adam/beta1',
 'Adam/beta2',
 'Adam/epsilon',
 'Adam/update_conv_layer_1/W/ApplyAdam',
 'Adam/update_conv_layer_1/b/ApplyAdam',
 'Adam/update_conv_layer_2/W/ApplyAdam',
 'Adam/update_conv_layer_2/b/ApplyAdam',
 'Adam/update_conv_layer_2_1/W/ApplyAdam',
 'Adam/update_conv_layer_2_1/b/ApplyAdam',
 'Adam/update_FCL-1/W/ApplyAdam',
 'Adam/update_FCL-1/b/ApplyAdam',
 'Adam/update_FCL-2/W/ApplyAdam',
 'Adam/update_FCL-2/b/ApplyAdam',
 'Adam/update_output/W/ApplyAdam',
 'Adam/update_output/b/ApplyAdam',
 'Adam/mul',
 'Adam/Assign',
 'Adam/mul_1',
 'Adam/Assign_1',
 'Adam',
 'ArgMax/dimension',
 'ArgMax',
 'ArgMax_1/dimension',
 'ArgMax_1',
 'Equal',
 'Cast',
 'Const_1',
 'Mean_1',
 'init',
 'save/Const',
 'save/SaveV2/tensor_names',
 'save/SaveV2/shape_and_slices',
 'save/SaveV2',
 'save/control_dependency',
 'save/RestoreV2/tensor_names',
 'save/RestoreV2/shape_and_slices',
 'save/RestoreV2',
 'save/Assign',
 'save/RestoreV2_1/tensor_names',
 'save/RestoreV2_1/shape_and_slices',
 'save/RestoreV2_1',
 'save/Assign_1',
 'save/RestoreV2_2/tensor_names',
 'save/RestoreV2_2/shape_and_slices',
 'save/RestoreV2_2',
 'save/Assign_2',
 'save/RestoreV2_3/tensor_names',
 'save/RestoreV2_3/shape_and_slices',
 'save/RestoreV2_3',
 'save/Assign_3',
 'save/RestoreV2_4/tensor_names',
 'save/RestoreV2_4/shape_and_slices',
 'save/RestoreV2_4',
 'save/Assign_4',
 'save/RestoreV2_5/tensor_names',
 'save/RestoreV2_5/shape_and_slices',
 'save/RestoreV2_5',
 'save/Assign_5',
 'save/RestoreV2_6/tensor_names',
 'save/RestoreV2_6/shape_and_slices',
 'save/RestoreV2_6',
 'save/Assign_6',
 'save/RestoreV2_7/tensor_names',
 'save/RestoreV2_7/shape_and_slices',
 'save/RestoreV2_7',
 'save/Assign_7',
 'save/RestoreV2_8/tensor_names',
 'save/RestoreV2_8/shape_and_slices',
 'save/RestoreV2_8',
 'save/Assign_8',
 'save/RestoreV2_9/tensor_names',
 'save/RestoreV2_9/shape_and_slices',
 'save/RestoreV2_9',
 'save/Assign_9',
 'save/RestoreV2_10/tensor_names',
 'save/RestoreV2_10/shape_and_slices',
 'save/RestoreV2_10',
 'save/Assign_10',
 'save/RestoreV2_11/tensor_names',
 'save/RestoreV2_11/shape_and_slices',
 'save/RestoreV2_11',
 'save/Assign_11',
 'save/RestoreV2_12/tensor_names',
 'save/RestoreV2_12/shape_and_slices',
 'save/RestoreV2_12',
 'save/Assign_12',
 'save/RestoreV2_13/tensor_names',
 'save/RestoreV2_13/shape_and_slices',
 'save/RestoreV2_13',
 'save/Assign_13',
 'save/RestoreV2_14/tensor_names',
 'save/RestoreV2_14/shape_and_slices',
 'save/RestoreV2_14',
 'save/Assign_14',
 'save/RestoreV2_15/tensor_names',
 'save/RestoreV2_15/shape_and_slices',
 'save/RestoreV2_15',
 'save/Assign_15',
 'save/RestoreV2_16/tensor_names',
 'save/RestoreV2_16/shape_and_slices',
 'save/RestoreV2_16',
 'save/Assign_16',
 'save/RestoreV2_17/tensor_names',
 'save/RestoreV2_17/shape_and_slices',
 'save/RestoreV2_17',
 'save/Assign_17',
 'save/RestoreV2_18/tensor_names',
 'save/RestoreV2_18/shape_and_slices',
 'save/RestoreV2_18',
 'save/Assign_18',
 'save/RestoreV2_19/tensor_names',
 'save/RestoreV2_19/shape_and_slices',
 'save/RestoreV2_19',
 'save/Assign_19',
 'save/RestoreV2_20/tensor_names',
 'save/RestoreV2_20/shape_and_slices',
 'save/RestoreV2_20',
 'save/Assign_20',
 'save/RestoreV2_21/tensor_names',
 'save/RestoreV2_21/shape_and_slices',
 'save/RestoreV2_21',
 'save/Assign_21',
 'save/RestoreV2_22/tensor_names',
 'save/RestoreV2_22/shape_and_slices',
 'save/RestoreV2_22',
 'save/Assign_22',
 'save/RestoreV2_23/tensor_names',
 'save/RestoreV2_23/shape_and_slices',
 'save/RestoreV2_23',
 'save/Assign_23',
 'save/RestoreV2_24/tensor_names',
 'save/RestoreV2_24/shape_and_slices',
 'save/RestoreV2_24',
 'save/Assign_24',
 'save/RestoreV2_25/tensor_names',
 'save/RestoreV2_25/shape_and_slices',
 'save/RestoreV2_25',
 'save/Assign_25',
 'save/RestoreV2_26/tensor_names',
 'save/RestoreV2_26/shape_and_slices',
 'save/RestoreV2_26',
 'save/Assign_26',
 'save/RestoreV2_27/tensor_names',
 'save/RestoreV2_27/shape_and_slices',
 'save/RestoreV2_27',
 'save/Assign_27',
 'save/RestoreV2_28/tensor_names',
 'save/RestoreV2_28/shape_and_slices',
 'save/RestoreV2_28',
 'save/Assign_28',
 'save/RestoreV2_29/tensor_names',
 'save/RestoreV2_29/shape_and_slices',
 'save/RestoreV2_29',
 'save/Assign_29',
 'save/RestoreV2_30/tensor_names',
 'save/RestoreV2_30/shape_and_slices',
 'save/RestoreV2_30',
 'save/Assign_30',
 'save/RestoreV2_31/tensor_names',
 'save/RestoreV2_31/shape_and_slices',
 'save/RestoreV2_31',
 'save/Assign_31',
 'save/RestoreV2_32/tensor_names',
 'save/RestoreV2_32/shape_and_slices',
 'save/RestoreV2_32',
 'save/Assign_32',
 'save/RestoreV2_33/tensor_names',
 'save/RestoreV2_33/shape_and_slices',
 'save/RestoreV2_33',
 'save/Assign_33',
 'save/RestoreV2_34/tensor_names',
 'save/RestoreV2_34/shape_and_slices',
 'save/RestoreV2_34',
 'save/Assign_34',
 'save/RestoreV2_35/tensor_names',
 'save/RestoreV2_35/shape_and_slices',
 'save/RestoreV2_35',
 'save/Assign_35',
 'save/RestoreV2_36/tensor_names',
 'save/RestoreV2_36/shape_and_slices',
 'save/RestoreV2_36',
 'save/Assign_36',
 'save/RestoreV2_37/tensor_names',
 'save/RestoreV2_37/shape_and_slices',
 'save/RestoreV2_37',
 'save/Assign_37',
 'save/RestoreV2_38/tensor_names',
 'save/RestoreV2_38/shape_and_slices',
 'save/RestoreV2_38',
 'save/Assign_38',
 'save/restore_all']

In [13]:
epoch=3 # run for more iterations according your hardware's power
#change batch size according to your hardware's power. For GPU's use batch size in powers of 2 like 2,4,8,16...
batch_size=20 
no_itr_per_epoch=len(X_train)//batch_size

In [14]:
no_itr_per_epoch
n_test=len(X_test) #number of test samples
n_val=len(X_val)  #number of validation samples

In [15]:
# Now iterate over our dataset n_epoch times
for iteration in range(epoch):
    print("Iteration no: {} ".format(iteration))
    
    previous_batch=0
    # Do our mini batches:
    for i in range(no_itr_per_epoch):
        current_batch=previous_batch+batch_size
        x_input=X_train[previous_batch:current_batch]
        x_images=np.reshape(x_input,[batch_size,56,56,3])
        
        y_input=Y_train[previous_batch:current_batch]
        y_label=np.reshape(y_input,[batch_size,2])
        previous_batch=previous_batch+batch_size
        
        _,loss=sess.run([train_step, cross_entropy], feed_dict={x: x_images,y_: y_label})
        if i % 100==0 :
            print ("Training loss : {}" .format(loss))
            
   
        
    x_test_images=np.reshape(X_test[0:n_test],[n_test,56,56,3])
    y_test_labels=np.reshape(Y_test[0:n_test],[n_test,2])
    Accuracy_test=sess.run(accuracy,
                           feed_dict={
                        x: x_test_images ,
                        y_: y_test_labels
                      })
    Accuracy_test=round(Accuracy_test*100,2)
    
    x_val_images=np.reshape(X_val[0:n_val],[n_val,56,56,3])
    y_val_labels=np.reshape(Y_val[0:n_val],[n_val,2])
    Accuracy_val=sess.run(accuracy,
                           feed_dict={
                        x: x_val_images ,
                        y_: y_val_labels
                      })    
    Accuracy_val=round(Accuracy_val*100,2)
    print("Accuracy ::  Test_set {} % , Validation_set {} % " .format(Accuracy_test,Accuracy_val))


Iteration no: 0 
Training loss : 0.7088624238967896
Training loss : 0.5636587738990784
Training loss : 0.7106266021728516
Training loss : 0.646752655506134
Training loss : 0.49564218521118164
Training loss : 0.5322838425636292
Training loss : 0.6077149510383606
Training loss : 0.5694921612739563
Training loss : 0.5456980466842651
Accuracy ::  Test_set 70.16 % , Validation_set 71.88 % 
Iteration no: 1 
Training loss : 0.6939454078674316
Training loss : 0.33341121673583984
Training loss : 0.6486116647720337
Training loss : 0.6708499193191528
Training loss : 0.34164077043533325
Training loss : 0.4279203414916992
Training loss : 0.45605963468551636
Training loss : 0.5492191910743713
Training loss : 0.4131855070590973
Accuracy ::  Test_set 74.1 % , Validation_set 74.96 % 
Iteration no: 2 
Training loss : 0.6067825555801392
Training loss : 0.31459251046180725
Training loss : 0.5904306173324585
Training loss : 0.5648720860481262
Training loss : 0.22201402485370636
Training loss : 0.3567794859409332
Training loss : 0.34570541977882385
Training loss : 0.43061336874961853
Training loss : 0.27252504229545593
Accuracy ::  Test_set 78.4 % , Validation_set 79.32 % 

In [16]:
def process_img(img):
        img=img.resize((56, 56), Image.ANTIALIAS) #resize the image
        img = np.array(img)
        img=img/np.max(img).astype(float) 
        img=np.reshape(img, [1,56,56,3])
        return img

In [ ]:
#test your own images 
test_image=Image.open('/path to file')
test_image= process_img(test_image)
predicted_array= sess.run(y_predicted, feed_dict={x: test_image})
predicted_class= np.argmax(predicted_array)
if predicted_class==0:
    print "It is a cat"  
else :
    print "It is a dog  "