【问题标题】:Having trouble with CNN predictionCNN预测有问题
【发布时间】:2018-11-13 04:24:54
【问题描述】:

我第一次使用卷积神经网络进行车辆识别。目前,我只使用 2 个班级(自行车和汽车)。训练集:420 个汽车图像和 825 个自行车图像。测试集:44 个汽车图像和 110 个自行车图像 汽车和自行车图像的格式不同(bmp、jpg)。在单一预测中,我总是得到“自行车”。我尝试在输出层使用 Sigmoid 函数。然后我只得到“汽车”。我的代码如下:``

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")

我想测试如下的单个图像:

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))

【问题讨论】:

    标签: python keras deep-learning


    【解决方案1】:

    如果您打印 result 矩阵,您会发现它不仅有 1 和 0,而是在这些数字之间浮动。您可以选择一个阈值并将超过它的值设置为 1,将其他所有值设置为 0。

    【讨论】:

    • 感谢您的回复。你是对的,我在结果矩阵中得到浮点数。但它的值永远不会接近 1。对于“汽车”,我得到大约 0.0003,而对于自行车,它是
    • np.argmax 仅适用于 'categorized_crossentropy' 损失函数,因此请忘记这一点,因为您使用的是 'binary_crossentropy'。我建议将 epoch 的数量从 3 增加到 100 或更多,然后再次查看结果。还要确保您拥有相同或接近数量的汽车和自行车样品。 -编辑我的答案以摆脱 argmax 部分-
    猜你喜欢
    • 2020-11-14
    • 2018-12-13
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2020-01-02
    • 2018-11-03
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多