0

Currently I am working on a binary classification model using Keras(version '2.6.0'). And I build simple model with three Blocks of 2D Convolution (Conv2D + ReLU + Pooling), then a finale blocks contain a Flatten, Dropout and two Dense layers. I have a small dataset of images in my disk and they are organized in a main directory like this:

/content/data/
.............train/
..................classA/
........................img1.jpg
........................img2.jpg
.
.
.
..................classB/
........................img1.jpg
........................img2.jpg
.
.
.

After the training step i have the following learning curves: enter image description here enter image description here

Even with the noisy behave, they seems great for me (correct me if I am wrong). No overfitting the training and the validation curves have the same behavior, and after 15 epochs I get 1 of accuracy and less than 0.2 as losses.

Question:

When I test the model, I want to display to which classes the image belong A or B ?

I tried the following :

predictions = MODEL.predict(img_array)
score = np.argmax(predictions)
prob = tf.nn.sigmoid(predictions[0])

but i get the same score (0) for two different images belong to two different classes.

I appreciate any suggestions or written documents, because the documentations at Keras didn't specified the details of this step. Thanks in advance.

8
  • Did you have a look at this tutorial ? pyimagesearch.com/2017/12/11/…
    – abdou_dev
    Sep 27, 2021 at 23:24
  • Not sure why you use 2 Dense layers? and hence what np.argmax(predictions) be giving, in case of 1 dense layer and binary class, it will give class index not score.
    – A.B
    Sep 27, 2021 at 23:26
  • @abdou_dev Thanks for the quick comment, I give it a try as described in the articles, by writing: " (cl_A, cl_B) = saved_model.predict(image)[0] " but i got an error of " not enough values to unpack (expected 2, got 1) ". Because the value of " Model.predict(img) " is an array [[0.9163068]] of (1, 1) shape. If there is an alternative way ? anyway thank you for your help.
    – Lambda
    Sep 27, 2021 at 23:53
  • What activation function are you using in your output layer? And how many neurons are in the output layer?
    – NotAName
    Sep 28, 2021 at 0:00
  • @A.B For the two layers i was just testing the difference the first have some units I was varying them (Note I am testing on small dataset) with an ReLU activation and end it by an other Dense layers for units=1. So i was just testing. For the output of the model using " Model.predict(img) " the output is an array with a shape of 1 by 1.
    – Lambda
    Sep 28, 2021 at 0:03

1 Answer 1

0

Try this :

ImagePath = "YourImagePath" 
img = keras.preprocessing.image.load_img(
    ImagePath, target_size=image_size
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)  # Create batch axis

predictions = model.predict(img_array)
score = predictions[0]
print(
    "This image is %.2f percent cat and %.2f percent dog."
    % (100 * (1 - score), 100 * score)
) # Will print on the Console

And this is a tutorial of Adrian Rosebrock that you can follow it for more details :

Image classification with Keras and deep learning

4
  • I tried This answer, but it have no sense when it comes to differentiate between the classes.
    – Lambda
    Sep 28, 2021 at 0:15
  • The example was very clear it will print the percent of an image that could be cat and the percent of an image that could be dog , so the firstScore it will be calculated using model.predict(img_array) and the second will be calculated from the first ( 1 - firstScore) .
    – abdou_dev
    Sep 28, 2021 at 7:45
  • Could you please provide more details about what you got as an output?
    – abdou_dev
    Sep 28, 2021 at 7:46
  • After i give it another try i corrected my mistakes, and the way that was mentioned in pyimagesearch.com it worked finally thanks also to @pavel for underlining my mistakes.
    – Lambda
    Sep 28, 2021 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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