Visualising and understanding MNIST dataset and solving with simple DNN using tflearn

This post is a sort of getting started with digits dataset for deep learning.

MNIST dataset is available in csv format, it is confusing at the start in understanding, how the image dataset is arranged in a csv file. To start with, MNIST dataset consist of image data as scalar, one dimension array of 784 values. In 2D array form, it will be a 28×28 matrix.

The training dataset consist of header in first row detailing what type of data the column contains. First column is the label for image data and remaining 784 columns contain pixel value.MNIST dataset visualisation

The test dataset follow the same format without the label information.

MNIST test dataset visualisation

First the csv file is read to a dataframe using pandas.

train = pd.read_csv("./train.csv")

Each row of the dataframe contain label information as the first item and pixel values in the remaining items. The following code parse through first 10 rows of data.

#loop through the dataframe line by line and print the image for the arranged data
for x in range(1, 10): #first 10 rows are parsed
    rowData=np.array(train[x-1:x]) #np array rowData contain all 785 values (1 label value+784 pixel value)
    label=np.resize(rowData,(1,1)) #np array label gets the first value from rowdata
    print('label shape             ->',label.shape) #printing the shape of np array label
    print('label                   ->',label.ravel()) #Image label
    rowWithIndex = rowData.ravel()#scalar data with 785 items    
    print('row with index shape    ->',rowWithIndex.shape)
    rowWithOutIndex = rowWithIndex[1:785:1]#scalar image data with 784 pixel values
    print('row without index shape ->',rowWithOutIndex.shape)
    Image1=np.resize(rowWithOutIndex,(28,28)) #28x28 Image
    print('Image shape             ->',Image1.shape) #printing Image shape
    plt.imshow(Image1, interpolation='nearest') #plotting
    plt.show()

The data when plotted looks like this,

MNIST data plotThat is all about visualisation and back to actual data preparation and training. For training the DNN, we need label and data as two separate arrays. The label data need to be one-hot encoded for passing it to tensorflow training function.

# Split data into training set and labels
y_train = train.ix[:,0].values #all input labels, first cloumn(index 0) of each row in the train csv file
trainX = train.ix[:,1:].values #remaining 784 values after(from index 1 till end) the first colum. 

print(y_train.shape)
print(trainX.shape)
#one hot encoded form of labels
y_train_one_hot = to_categorical(y_train)
print(y_train_one_hot)

one hot encoding for training label

Once the data is prepped, creating a DNN using tflearn is as easy as defining input layer, hidden layers, output layer and regression function.

#DNN - input layer of 784 inputs, 4 hidden layers and a softmax layer at output
def build_model():
    tf.reset_default_graph() 
    net = tflearn.input_data([None, 784]) #input layer with 784 inputs
    net = tflearn.fully_connected(net, 128, activation='ReLU') #hidden layer1
    net = tflearn.fully_connected(net, 64, activation='ReLU') #hidden layer2
    net = tflearn.fully_connected(net, 32, activation='ReLU') #hidden layer3
    net = tflearn.fully_connected(net, 10, activation='softmax') #output layer
    net = tflearn.regression(net, optimizer='sgd', learning_rate=0.1, loss='categorical_crossentropy')
    model = tflearn.DNN(net)
    return model
model = build_model()

The updated kernel is available in kaggle and in github page.

 

Leave a Reply

Your email address will not be published. Required fields are marked *