Jerry
Jerry

Reputation: 49

How to code keras to read my own picture using my trained model?

I used minst to train my own model of number reading, but when I tried to upload my own picture for predicting, it told me an error said "ValueError: Input 0 of layer dense_3 is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 84]"(My model was trained correctly, predicting picture in minst was successful. Here is my code:

import numpy as np
from keras.applications.imagenet_utils import decode_predictions
from keras.preprocessing import image
from keras.applications import *
import glob
 
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

img = []
x = []

images = image.load_img("/content/gdrive/My Drive/Colab Notebooks/num.png", target_size=(28, 28))
x = image.img_to_array(images)
x = np.expand_dims(x, axis=1)
img.append(x)

print(len(x))
x = np.concatenate([x for x in img])
 
model = tf.keras.models.load_model('num_reader.model')
y = model.predict(x)
print('Predicted:', decode_predictions(y, top=3))

This is my error:

28    #This is printed by "print(len(x))"
WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input Tensor("flatten_1_input_3:0", shape=(None, 28, 28), dtype=float32), but it was called on an input with incompatible shape (None, 1, 28, 3).
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-cd1af7600aac> in <module>()
     27 
     28 model = tf.keras.models.load_model('num_reader.model')
---> 29 y = model.predict(x)
     30 print('Predicted:', decode_predictions(y, top=3))

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    971           except Exception as e:  # pylint:disable=broad-except
    972             if hasattr(e, "ag_error_metadata"):
--> 973               raise e.ag_error_metadata.to_exception(e)
    974             else:
    975               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1462 predict_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1452 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1445 run_step  **
        outputs = model.predict_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1418 predict_step
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/sequential.py:372 call
        return super(Sequential, self).call(inputs, training=training, mask=mask)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:386 call
        inputs, training=training, mask=mask)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/functional.py:508 _run_internal_graph
        outputs = node.layer(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility
        ' but received input with shape ' + str(shape))

    ValueError: Input 0 of layer dense_3 is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape [None, 84]

Minst is a 28x28 picture so this code is using 28x28 picture. My environment of develop is: "Google Colab" and "Juypter Notebook", and this error I have tried both environment and still getting this error. Can somebody help? import tensorflow as tf # deep learning library. Tensors are just multi-dimensional arrays

mnist = tf.keras.datasets.mnist  # mnist is a dataset of 28x28 images of handwritten digits and their labels
(x_train, y_train),(x_test, y_test) = mnist.load_data()  # unpacks images to x_train/x_test and labels to y_train/y_test

x_train = tf.keras.utils.normalize(x_train, axis=1)  # scales data between 0 and 1
x_test = tf.keras.utils.normalize(x_test, axis=1)  # scales data between 0 and 1

model = tf.keras.models.Sequential()  # a basic feed-forward model
model.add(tf.keras.layers.Flatten())  # takes our 28x28 and makes it 1x784
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))  # a simple fully-connected layer, 128 units, relu activation
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))  # our output layer. 10 units for 10 classes. Softmax for probability distribution

model.compile(optimizer='adam',  # Good default optimizer to start with
              loss='sparse_categorical_crossentropy',  # how will we calculate our "error." Neural network aims to minimize loss.
              metrics=['accuracy'])  # what to track

model.fit(x_train, y_train, epochs=10)  # train the model

val_loss, val_acc = model.evaluate(x_test, y_test)  # evaluate the out of sample data with model
print(val_loss)  # model's loss (error)
print(val_acc)  # model's accuracy

This is the picture of num.png

Upvotes: 4

Views: 39083

Answers (3)

glitch
glitch

Reputation: 21

Check out the number of neurons of the output layer if it matches the number of the classes or not. I have face the same problem, and it turned out that I was setting the output units of the models 2 in binary classification, which should be one.

Upvotes: 0

Alperen Sayar
Alperen Sayar

Reputation: 1

You need to change your train and test labels using to_categorical before fitting the model.

from keras.utils import to_categorical
train_label=to_categorical(train_label)
test_labels=to_categorical(test_labels)

Upvotes: 0

Stat Tistician
Stat Tistician

Reputation: 883

It is a bit hard to tell without exactly knowing how your model was built and how the num.png looks like. However, from the error message it seems you have two mistakes: You are loading rgb color images (although MNIST and likely your model is trained on greyscale images) and therefore (or this is a second bug) you got the dimensions wrong (3 for RGB and not one for grayscale). 28*3 = 84 is how I would explain the error message:

but received input with shape [None, 84]

Try to add rgb_to_grayscale and axis=0:

import tensorflow as tf
from keras_preprocessing import image
images = image.load_img("/content/gdrive/My Drive/Colab Notebooks/num.png", target_size=(28, 28))    
x = image.img_to_array(images)
x = tf.image.rgb_to_grayscale(x)
x = np.expand_dims(x, axis=0)
x = x/255.0
img.append(x)

print(len(x))
x = np.concatenate([x for x in img])
 
model = tf.keras.models.load_model('num_reader.model')
y = model.predict(x)
print('Predicted:', decode_predictions(y, top=3))

Please note that I added x=x/255.0, because I assume that you also forgot to rescale to [0,1]. I assume that if you stick closely to the MNIST ML tutorials you trained your model applying a rescaling to your images in your preprocessing. You have to apply that too.

Update: It works for me too with passing x directly to model.predict:

images = image.load_img("/content/gdrive/My Drive/Colab Notebooks/num.png", target_size=(28, 28))    
x = image.img_to_array(images)
x = tf.image.rgb_to_grayscale(x)
x = np.expand_dims(x, axis=0)
x = x/255.0

model = tf.keras.models.load_model('num_reader.model')
model.predict(x)

Upvotes: 3

Related Questions