Sunday, November 18, 2018

Training a Convolutional Neural Network to detect Digits


from sklearn.externals import joblib
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import numpy as np
import pandas as pd
import os

#changes working directory
os.chdir("D:\\Kartheek\\DigitRecog")

(X_train,y_train),(X_test,y_test)=mnist.load_data()
x_train=X_train.reshape(X_train.shape[0],28,28,1).astype('float32')
x_test=X_test.reshape(X_test.shape[0],28,28,1).astype('float32')
print(X_train.shape)
print(X_test.shape)
#os.chdir("D:\\NeuralNetwork")
batch_size = 132
num_classes = 10
epochs = 16

# input image dimensions
img_rows, img_cols = 28, 28

print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
#cv2.imwrite('messigray.png',x_train[0])
model = Sequential()
model.add(Conv2D(64, kernel_size=(5, 5),
                 activation='relu',
                 input_shape=(28,28,1)))
model.add(Conv2D(128, (5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(num_classes, activation='softmax'))


model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


Images = pd.read_csv("test.csv")
Images = Images.values
Images.shape
x_predict=Images.reshape(28000,28,28,1)
y_predict=model.predict(x_predict)
Submission = pd.read_csv("submission.csv")
z=y_predict.argmax(axis=1)

Submission["Label"]=z
Submission.to_csv("submission.csv",index=False)

joblib.dump(model, "digits_cls.pkl", compress=3)
Images = pd.read_csv("test.csv")
Images.shape
Images = Images.values
Images.shape
x_predict=Images.reshape(28000,28,28,1)
y_predict=model.predict(x_predict)
Submission = pd.read_csv("submission.csv")
z=y_predict.argmax(axis=1)

Submission["Label"]=z
Submission.to_csv("submission.csv",index=False)

Sunday, October 28, 2018

Importing local files to Colaboratory

Firstly ,executing below cell should create an inline "Choose Files" button

from google.colab import files
uploaded = files.upload()

After selecting your file(s), uploaded will be a dictionary of keys (the file names) and values (the encoded file objects). To decode the files for a library such as Pandas, try

import pandas as pd
df_filename = pd.read_csv('filename.csv')


Or Upload from below navigation

Find indices of the max element of the array in a particular axis.

import numpy as np
  
# Working on 2D array
array = np.arange(12).reshape(3, 4)
print("INPUT ARRAY Values: \n", array)
  
# If No axis mentioned, so it works on entire array
print("\nMax element : ", np.argmax(array))
  
# returning Indices of the max element
# as per the indices
print("\nIndices of Max element on column: ", np.argmax(array, axis=0))
print("\nIndices of Max element on row: ", np.argmax(array, axis=1))

Python Code to convert or reshape 2D to 3D matrix array

from numpy import array
# list of data
data = [[11, 22],
[33, 44],
[55, 66]]
# array of data
data = array(data)
print(data.shape)
# reshape
data = data.reshape((data.shape[0], data.shape[1], 1))
print(data.shape)

Output:

(3, 2)
(3, 2, 1)

Saturday, October 27, 2018

python code to convert data frame to multi dimensional array(matrix) and access its columns

import pandas as pd
import os

os.chdir("D:\\DigitRecog\\Kaggle\\all")

Images = pd.read_csv("train.csv")
type(Images)

Images = Images.values
y=Images[:,0]
y.shape[0]

Thursday, October 25, 2018

Wednesday, October 24, 2018

Viewing the Intensity matrix of an image

  1. from skimage import io, viewer
  2. img = io.imread('test.jpg', as_grey=True) # load the image as grayscale
  3. print 'image matrix size: ', img.shape # print the size of image
  4. print '\n First 5 columns and rows of the image matrix: \n', img[:5,:5]*255
  5. viewer.ImageViewer(img).show() # plot the image

Tuesday, October 23, 2018

Creating Matrix with random values


numpy.random.rand(d0, d1, ..., dn)

d0,d1,d2...dn : The dimensions of the returned array, should all be positive. 
If no argument is given a single Python float is returned.

Example:
>>> np.random.rand(3,2)

array([[ 0.14022471,  0.96360618],  #random
       [ 0.37601032,  0.25528411],  #random
       [ 0.49313049,  0.94909878]]) #random



Monday, October 22, 2018

Max function in Python


np.max is just an alias for np.amax. This function only works on a single input array and finds the value of maximum element in that entire array (returning a scalar). Alternatively, it takes an axisargument and will find the maximum value along an axis of the input array (returning a new array).

>>> a = np.array([[1, 2, 6],
                  [2, 4, 1]])
>>> np.max(a)
6
>>> np.max(a, axis=0) # max of each column
array([2, 4, 6])


np.maximum will take two arrays and compute their element-wise maximum. Arrays should be compatible. Below is an example:

>>> b = np.array([3, 6, 1])
>>> c = np.array([4, 2, 9])
>>> np.maximum(b, c)
array([4, 6, 9])

Sunday, October 21, 2018

For Loop Example

# Program to iterate through a list using indexing

genre = ['popper', 'rocker', 'jazz built']

# iterate over the list using index
for i in range(len(genre)):
print("I like", genre[i])

Wednesday, August 8, 2018

Display an image

import cv2
image = cv2.imread("D:\Hand.jpg")
cv2.imshow("Diplayed..", image )
cv2.waitKey(1000)