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:
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.
CodePudding user response:
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 :