0
$\begingroup$

Hi downloaded the MNIST dataset images and labels and I am trying to train but I am getting low accuracy which is very low . not Even 50 %.

below is my code.

import glob
import imageio
import cv2
import pandas as pd
import numpy as np 
images=[]
for image_path in glob.glob(r"C:\\Users\\Downloads\\tmnist_bundle_rgb\\tmnist_bundle_rgb\\imgs\\*.png"):
    im = imageio.imread(image_path)
    a= cv2.imread(image_path)
    gray_img = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
    images.append(gray_img)

images = np.asarray(images,dtype=np.float32)

lables = pd.read_csv('C:\\Users\\Downloads\\tmnist_bundle_rgb\\tmnist_bundle_rgb\\index.csv',encoding='utf-8')

lables=lables.iloc[:, 0] # slicing the column

lables = np.asarray(lables) #Array

lables = np.asarray(lables,dtype=np.float32)

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(images, lables, test_size=0.1) # split

X_train = X_train.reshape(54252,28,28,1)
X_test = X_test.reshape(6028,28,28,1)

X_train = X_train/ 255
X_test = X_test/ 255

from keras.utils import to_categorical

number_of_classes=10
y_train = to_categorical(y_train,number_of_classes)
y_test = to_categorical(y_test,number_of_classes)


#Model Buidling 
from keras.models import Sequential
from keras.layers import Dense, Convolution2D, Flatten
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Dropout

model = Sequential()
model = Sequential()
model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])


model.fit(X_train, y_train,batch_size=32, nb_epoch=5, verbose=1

Output:

Epoch 1/5 54252/54252 [==============================] - 36s 663us/step - loss: 2.3024 - acc: 0.1093

Epoch 2/5 54252/54252 [==============================] - 36s 664us/step - loss: 2.3017 - acc: 0.1111

Epoch 3/5 54252/54252 [==============================] - 39s 721us/step - loss: 2.3016 - acc: 0.1115

Epoch 4/5 54252/54252 [==============================] - 40s 733us/step - loss: 2.3018 - acc: 0.1110

Epoch 5/5 54252/54252 [==============================] - 50s 912us/step - loss: 2.3015 - acc: 0.1115

$\endgroup$
1
$\begingroup$

Your model have an accuracy of 0.10 so he is correct 10% of the time, a random model would do the same. It means your model doesn't learn at all. Even a bad model learn a little. So the problem come from your dataset.

I tested your model and got 97% accuracy.

Your problem probably come from how you import your dataset. Here is how i imported:

import idx2numpy
import numpy as np
fileImg = 'data/train-images.idx3-ubyte'
fileLabel= 'data/train-labels.idx1-ubyte'
arrImg = idx2numpy.convert_from_file(fileImg)
arrLabels = idx2numpy.convert_from_file(fileLabel)

the dataset come from: http://yann.lecun.com/exdb/mnist/

You should test if you imported an processed your dataset correctly. For example :

import matplotlib.pyplot as plt
image = np.asarray(arrImg[1000])
plt.imshow(image)
plt.show()
print(arrLabels[1000])

enter image description here

$\endgroup$
  • $\begingroup$ Thank you . even I downloaded the data set from yann.lecun.com/exdb/mnist this is working fine. what I am here is doing I extracted all the images in image folder and place lables in one folder . then I want to trying to import from local computer. I have 60000 images and same lables for that . $\endgroup$ – soumyajeet Jul 5 '19 at 9:43
0
$\begingroup$

I'm fairly certain there is something wrong with the way you load the data. I modified the code like this:

import numpy as np 
from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train =  x_train.reshape([x_train.shape[0], x_train.shape[1], x_train.shape[2], 1])
x_test =  x_test.reshape([x_test.shape[0], x_test.shape[1], x_test.shape[2], 1])

x_train = x_train / 255
x_test = x_test / 255

from keras.utils import to_categorical

number_of_classes=10
y_train = to_categorical(y_train,number_of_classes)
y_test = to_categorical(y_test,number_of_classes)


#Model Buidling 
from keras.models import Sequential
from keras.layers import Dense, Convolution2D, Flatten
from keras.layers.convolutional import MaxPooling2D
from keras.layers import Dropout

#model = Sequential()
model = Sequential()
model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(number_of_classes, activation='softmax'))

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])


model.fit(x_train, y_train,batch_size=32, epochs=5, verbose=1)

This gives accuracy of >97%

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.