Home > Software design >  CNN Model Predicting Only First Class
CNN Model Predicting Only First Class

Time:09-17

I am working on a fine-grained classification to classify car models. So I have used transfer learning ResNet50. As per my knowledge it is performing fine while training. But when I try new images it is always predicting a single class. Below is my code.

For training:

from tensorflow.keras.layers import Input, Lambda, Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.resnet50 import preprocess_input
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
import numpy as np
from glob import glob


IMAGE_SIZE = [224, 224]

train_path = 'Datasets/train'
valid_path = 'Datasets/test'

resnet = ResNet50(input_shape = IMAGE_SIZE   [3], weights='imagenet', include_top = False)


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

folders = glob('Datasets/train/*') #training folders

x = Flatten()(resnet.output)

prediction = Dense(len(folders), activation='softmax') (x) 

model = Model(inputs = resnet.input, outputs = prediction)

model.compile(
    loss = 'categorical_crossentropy',
    optimizer = 'adam',
    metrics = ['accuracy']
)

train_datagen = ImageDataGenerator(rescale = 1./255,
                                  shear_range = 0.2,
                                  zoom_range = 0.2,
                                  horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

training_set = train_datagen.flow_from_directory('Datasets/train',
                                                target_size = (224, 224),
                                                batch_size = 32,
                                                class_mode = 'categorical')


test_set = test_datagen.flow_from_directory('Datasets/test',
                                            target_size = (224, 224),
                                            batch_size = 32,
                                            class_mode = 'categorical')


r = model.fit_generator(
training_set,
validation_data=test_set,
epochs=200,
steps_per_epoch=len(training_set),
validation_steps=len(test_set)
)


from tensorflow.keras.models import load_model

model.save('model_updateV1.h5')

y_pred = model.predict(test_set)


import numpy as np
y_pred = np.argmax(y_pred, axis=1)

For Trying New Images:

from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from tensorflow.keras.applications.resnet50 import preprocess_input

model = load_model('model_updateV1.h5')

img = image.load_img('Datasets/test/mercedes/45.jpg', target_size=(224,224))

x = image.img_to_array(img)

x = x/255.

x = np.expand_dims(x, axis = 0)
img_data = preprocess_input(x)
img_data.shape

model.predict(img_data)

a = np.argmax(model.predict(img_data), axis=1)
a

CodePudding user response:

I think your problem is that you are rescaling the images twice. You have code

x=x/255

then you expand the dimensions which is fine. However you then have code

img_data = preprocess_input(x)

The preprocess_input functon I believe rescales the pixel values between -1 and 1 with the code

x=x/127.5-1. 

So now your pixel value have been scaled down twice. So just delete the code

x=x/255
  • Related