简体   繁体   中英

CNN Image Classification: It gives me always the same prediction

I'm new on this field, still learning and I'm sorry if it is considered as silly questions. So recently I tried to learn image classification using Python and TensorFlow. I followed tutorial on some videos. But I have some problems in my code, because when I tried my model, the validation loss tends to increase, while my validation accuracy keeps fluctuating. When I tried to predict my sample image, it keeps giving me the same prediction. My images in my datasets are 730 images in total.

And here is my code for making the prediction:

import cv2
import tensorflow as tf

CATEGORIES = ["Bike", "Car"]
IMAGE_SIZE = 50

def prepare(filepath):
    image_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    image_array = image_array/255.0
    new_image_array = cv2.resize(image_array, (IMAGE_SIZE,IMAGE_SIZE))
    return new_image_array.reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 1)

model = tf.keras.models.load_model("prototype.model")

prediction = model.predict([prepare('car.jpg')])
print( CATEGORIES[int(prediction[0][0])] )

Thank you so much.

I once had a similar problem. Since you need to classify images as either "Bike" or "Car", try changing your final output layer to,

model.add(Dense(2))
model.add(Activation('softmax'))

If it still doesn't work, try sparse_categorical_crossentropy instead as your loss .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM