Having trouble with CNN prediction

此生再无相见时 提交于 2021-01-29 06:25:12

问题


I am using Convolutional Neural Networking for vehicle identification, my first time. Currently, I am working with just 2 classes(bike and car). Training set: 420 car images and 825 bike images. Test set: 44 car images and 110 bike images Car and Bike images are in different format(bmp,jpg). In single prediction, I am always getting 'bike'. I have tried using the Sigmoid function in the output layer. Then I get only 'car'. My code is like following: ``

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense,Dropout



classifier = Sequential()


classifier.add(Conv2D(32, (3, 3), input_shape = (128, 128, 3), activation = 'relu'))


classifier.add(MaxPooling2D(pool_size = (3, 3)))

# Adding a second convolutional layer
classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (3, 3)))

# Step 3 - Flattening
classifier.add(Flatten())

# Step 4 - Full connection
classifier.add(Dropout(0.3))
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   rotation_range= 3,
                                   fill_mode = 'nearest',
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   rotation_range= 3,
                                   fill_mode = 'nearest',
                                   horizontal_flip = True)

training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size = (128, 128),
                                                 batch_size = 10,
                                                 class_mode = 'binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size = (128, 128),
                                            batch_size = 10,
                                            class_mode = 'binary')

classifier.fit_generator(training_set,
                         steps_per_epoch = 1092//10,
                         epochs = 3,
                         validation_data = test_set,
                         validation_steps = 20)

classifier.save("car_bike.h5")

And I wanted to test a single image like the following:

test_image = image.load_img('dataset/single_prediction/download (3).jpg', target_size = (128, 128))
test_image = image.img_to_array(test_image)
test_image *= (1/255.0)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
if result[0][0] == 1:
    prediction = 'bike'
else:
    prediction = 'car'

print(" {}".format(prediction))

回答1:


If you print your result matrix you'll see that it doesn't have only 1s and 0s but floats between these numbers. You may pick a threshold and set values that exceed it to 1 and everything else to 0.



来源:https://stackoverflow.com/questions/53273786/having-trouble-with-cnn-prediction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!