asn
asn

Reputation: 2607

Cannot find the reason behind very low accuracy in my Convolutional Net model with Keras

I have built and trained my Convolutional Neural Network model and trained it using the Handwritten_Dataset and have used the epochs=2 and sent the training data in batches of 128 but cannot find the reason behind its very low accuracy.

The code is :

import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import keras

from sklearn.model_selection import train_test_split

import warnings
warnings.filterwarnings('ignore')

import tables

from keras.models import Sequential
from keras.utils import np_utils 

from keras.layers import Conv2D, MaxPooling2D

from keras.layers import Activation, Flatten, Dropout, Dense


from keras.utils import to_categorical

#hd=pd.read_hdf('data.h5')
hd=pd.read_csv('../input/handwritten_data_785.csv')
hd.head()

Y=hd.iloc[:,0]
X=hd.iloc[:,1:]

Y=to_categorical(Y)

X_train,X_test,Y_train,Y_test=train_test_split(X,Y,stratify=Y,random_state=34,test_size=0.25)
X_train=X_train.values.reshape(X_train.shape[0],28,28,1)
X_test=X_test.values.reshape(X_test.shape[0],28,28,1)


X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

print("X.shape ",X.shape)
print("Y.shape ",Y.shape)
type(Y)

input_shape=(28,28,1)
n_classes=Y_train.shape[1]
batch_size=128
epochs=2
model=Sequential()
model.add(Conv2D(filters=32,kernel_size=(4,4),strides=(1,1),padding='same',activation='relu',input_shape=input_shape))

model.add(MaxPooling2D(pool_size=(2,2)))#,strides=(1,1)))

model.add(Conv2D(filters=64,kernel_size=(4,4),strides=(1,1),padding='same',activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(1,1)))

model.add(Flatten())

model.add(Dense(1000,activation='relu'))
model.add(Dense(n_classes,activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.SGD(lr=0.05),metrics=["accuracy"])
model.fit(X_train,Y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(X_test,Y_test))

model.evaluate(X_test,Y_test,verbose=0)

Can anyone point out the reason behind such low accuracy ? Have I divided the dataset correctly ?

The output Accuracy is :

Train on 279027 samples, validate on 93010 samples
Epoch 1/2
279027/279027 [==============================] - 63s 225us/step - loss: 15.6456 - acc: 0.0293 - val_loss: 15.6455 - val_acc: 0.0293
Epoch 2/2
279027/279027 [==============================] - 58s 208us/step - loss: 15.6455 - acc: 0.0293 - val_loss: 15.6455 - val_acc: 0.0293

[15.64552185918654, 0.02931942801857274]

Upvotes: 0

Views: 150

Answers (1)

asn
asn

Reputation: 2607

The following helps me attaining good accuracy :

1) Normalizing the data affect the Convolutional neural network by large scale.

Normalize the data : X /=255

The accuracy seems to go up to 90% without changing the epochs.

2) Increasing the epochs will also increase the model accuracy.

Upvotes: 1

Related Questions