1

I'm doing python mnist predict local picture

I'm facing AttributeError AttributeError: 'Sequential' object has no attribute 'predict_classes'

after read through discussion
(I see suggestion, but now see who they change/improve the code to solve the problem so that I still need a hand here)
I don't know how can address to my issue
Keras AttributeError: 'Sequential' object has no attribute 'predict_classes'

here is the code

import keras
from keras.datasets import mnist
import matplotlib.pyplot as plt
import PIL
from PIL import Image
(train_images,train_labels),(test_images,test_labels) = mnist.load_data()
train_images.shape
len(train_labels)
train_labels
test_images.shape
len(test_labels)
test_labels


'''plt.imshow(train_images[819], cmap=plt.get_cmap('gray'))
print(train_images[819])
print(train_labels[819])'''


from keras import models
from keras import layers
network = models.Sequential()
network.add(layers.Dense(512,activation='relu',input_shape=(28*28,)))
network.add(layers.Dense(10,activation='softmax'))


network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])


train_images = train_images.reshape((60000,28*28))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000,28*28))
test_images = test_images.astype('float32')/255

from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

network.fit(train_images,train_labels,epochs=1,batch_size=128)


test_loss , test_acc = network.evaluate(test_images,test_labels)
print('test_acc:',test_acc)



network.save('m_lenet.h5')


#########

import numpy as np
from keras.models import load_model
import matplotlib.pyplot as plt
from PIL import Image

model = load_model('/content/m_lenet.h5')

picPath = '/content/00_a.png'
img = Image.open(picPath)


reIm = img.resize((28,28),Image.ANTIALIAS)

im1 = np.array(reIm.convert("L"))



im1 = im1.reshape((1,28*28))
im1 = im1.astype('float32')/255

predict = model.predict_classes(im1)
print ('predict as:')
print (predict)
  • output:
469/469 [==============================] - 5s 10ms/step - loss: 0.2560 - accuracy: 0.9255
313/313 [==============================] - 1s 3ms/step - loss: 0.1435 - accuracy: 0.9571
test_acc: 0.957099974155426
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-44-40782e28c955> in <module>
     73 im1 = im1.astype('float32')/255
     74 
---> 75 predict = model.predict_classes(im1)
     76 print ('predict as:')
     77 print (predict)

AttributeError: 'Sequential' object has no attribute 'predict_classes'

the way I tried:

  • the discussion solution predictions = model.predict_classes(x_test) not works for me

0 Answers0