0
$\begingroup$

I'm trying to follow a tutorial about Tensorflow in Python and computer vision.

In this exercise I'm using a pre-trained model (InceptionV3) and some image augmentation about a datasets of humans vs horses. I have the training and validation folders with the datasets, and a test folder that I wanted to use to manually test the model on my images.

When I call the fit method on the model this seems to be working fine, as you can see from the prints on the terminal it has a pretty high accuracy even on the validation dataset.

However when I try to manually predict some test images on my machine something is off. I'm getting always 1 as prediction for whatever image I try to make the model predict. I have also tryed predict the same images from the validation dataset which it should have a 93% accuracy, but I'm always getting 1 (which is the class human).

Can you help me understand what I'm doing wrong? This is my code and the output from the terminal (the list of the files is cropped but the result is 1 for everyone of them)


import numpy as np
import os
import const
import stopper
from keras import Model
from keras.applications.inception_v3 import InceptionV3, layers
from keras_preprocessing.image import ImageDataGenerator
from keras_preprocessing import image
from keras.optimizers import RMSprop

TRAINING_DIR: str = const.PROJECT_PATH + '/datasets/horse-or-human/training'
TEST_DIR: str = const.PROJECT_PATH + '/datasets/horse-or-human/test'
VALIDATION_DIR: str = const.PROJECT_PATH + '/datasets/horse-or-human/validation'

IMG_DIM: int = 300


if __name__ == '__main__':

    training_generator = ImageDataGenerator(
        rescale=1. / 255,
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest'
    ).flow_from_directory(TRAINING_DIR, target_size=(IMG_DIM, IMG_DIM), class_mode='binary')

    validation_generator = ImageDataGenerator(rescale=1 / 255) \
        .flow_from_directory(VALIDATION_DIR, target_size=(IMG_DIM, IMG_DIM), class_mode='binary')

    # configure the pre-trained model
    pre_trained_model = InceptionV3(input_shape=(IMG_DIM, IMG_DIM, 3),
                                    include_top=False,
                                    weights=None)
    pre_trained_model.load_weights(f'{const.PROJECT_PATH}/pre-trained/inception_v3_weights.h5')

    for layer in pre_trained_model.layers:
        layer.trainable = False

    last_output = pre_trained_model.get_layer('mixed7').output

    x = layers.Flatten()(last_output)
    x = layers.Dense(1024, activation='relu')(x)
    x = layers.Dense(1, activation='sigmoid')(x)

    # attach the pre-trained model to your network
    model = Model(pre_trained_model.input, x)
    model.compile(optimizer=RMSprop(learning_rate=0.0001),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    model.fit(training_generator,
              epochs=50,
              callbacks=[stopper.CheckStopTraining()],
              validation_data=validation_generator)

    # try to predict my test images
    for file_name in os.listdir(TEST_DIR):
        if not file_name.startswith("."):  # Prevent to load hidden files that break the code
            img = os.path.join(TEST_DIR, file_name)
            img = image.load_img(img, target_size=(IMG_DIM, IMG_DIM))
            img = image.img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img = np.vstack([img])
            prediction = model.predict(img)
            print(f'{file_name} is {prediction[0]}')

enter image description here

$\endgroup$

1 Answer 1

0
$\begingroup$

I solved the issue. I was dividing by 255 both the training and validation generators, but not the images that I was loading from the computer.

I changed this line

img = image.img_to_array(img)

to this

img = image.img_to_array(img) / 255

And now is working.

Hope it can help someone else

$\endgroup$

Your Answer

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

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