我正在尝试用不同类型的水果训练我的模型

问题描述

我正在尝试用不同类型的水果训练我的模型,但是无论给出的图像如何,我都得到相同的预测,我通常只会得到一个结果,

最后,我的模型总是给出相同的预测,请您帮忙

"""
Created on Sat Sep 13 22:26:33 2020

@author: thummago
"""
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
train_datagen = ImageDataGenerator(
        rescale=1./255,shear_range=0.2,zoom_range=0.2,horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        'gopi_cnn_training_data',target_size=(64,64),batch_size=32)
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        'gopi_cnn_test_data',batch_size=32)
print(training_set)
cnn = tf.keras.models.Sequential()
cnn.add(tf.keras.layers.Conv2D(filters=32,kernel_size=3,activation='relu',input_shape=[64,64,3]))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
cnn.add(tf.keras.layers.Conv2D(filters=32,activation='relu'))
cnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))
cnn.add(tf.keras.layers.Flatten())
cnn.add(tf.keras.layers.Dense(units=128,activation='relu'))
cnn.add(tf.keras.layers.Dense(units=1,activation='softmax')) 
cnn.compile(optimizer='adam',loss = 'binary_crossentropy',metrics = ['accuracy'])
cnn.fit(x = training_set,validation_data = test_set,epochs = 25)
from keras.preprocessing import image
test_image = image.load_img('validation_set to check the model/0_100.jpg',target_size = (64,64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image,axis = 0)
result = cnn.predict(test_image)
training_set.class_indices
if result == 0:
    prediction = 'ApplRed' 
elif result == 1:
    prediction = 'AppleBraeburn'
elif result == 2:
    prediction = 'AppleBraeburn'
elif result == 3:
    prediction = 'Apricot'
elif result == 4:
    prediction = 'Avocado'
elif result == 5:
    prediction = 'Banana'
elif result == 6:
    prediction = 'Cherry1'
elif result == 7:
    prediction = 'Clementine'
elif result == 8:
    prediction = 'eggplant'
elif result == 9:
    prediction = 'Grape White'
elif result == 10:
    prediction = 'Hazelnut'
elif result == 11:
    prediction = 'Kiwi'
elif result == 12:
    prediction =  'Mango'
elif result == 13:
    prediction = 'Orange'
elif result == 14:
    prediction = 'Papaya'
elif result == 15:
    prediction = 'Pineapple'
elif result == 16:
    prediction = 'Pomegranate'
elif result == 17:
    prediction = 'Potato Red'
elif result == 18:
    prediction = 'StrawBerry'
elif result == 19:
    prediction = 'Tomato1'
elif result == 20:
    prediction = 'Watermelon'
else:
    prediction = 'WrongFruit'
print(prediction

我获得了所有时代的准确性

Found 10349 images belonging to 21 classes.
Found 3466 images belonging to 21 classes.
<keras.preprocessing.image.DirectoryIterator object at 0x0000028ABB26AC48>
Train for 324 steps,validate for 109 steps
Epoch 1/25
324/324 [==============================] - 51s 159ms/step - loss: 14.6031 - accuracy: 0.0476 - val_loss: 14.6031 - val_accuracy: 0.0476
Epoch 2/25
105/324 [========>.....................] - ETA: 54s - loss: 14.6031 - accuracy: 0.0476

解决方法

问题是您在只有一个节点的输出层上使用了softmax激活功能。对长度为1的向量进行软最大化将始终 返回1。

在构建可以完全选择 n 个选项之一的分类器神经网络时,应使输出层具有 n 个节点,并使用softmax激活。为了使其正常工作,您必须one-hot-encode目标数据。您还必须将损失更改为categorical_crossentropy

这里是short tutorial。请注意,他使用Keras的to_categorical函数完成了一次热编码。