python - CNN 模型预测任何输入的相同输出
问题描述
enter code here
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import MaxPooling2D
classifier = Sequential()
classifier.add(Convolution2D(32,(3,3),input_shape = (64,64,3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(32,(3,3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
classifier.add(Dense(units=32,activation = 'relu'))
classifier.add(Dense(units=64,activation = 'relu'))
classifier.add(Dense(units=128,activation = 'relu'))
classifier.add(Dense(units=256,activation = 'relu'))
classifier.add(Dense(units=256,activation = 'relu'))
classifier.add(Dense(units=6,activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255, # To rescaling the image in range of [0,1]
shear_range = 0.2, # To randomly shear the images
zoom_range = 0.2, # To randomly zoom the images
horizontal_flip = True) # for randomly flipping half of the images
horizontally
test_datagen = ImageDataGenerator(rescale = 1./255)
print("\nTraining the data...\n")
training_set = train_datagen.flow_from_directory('train',
target_size=(64,64),
batch_size=12, #Total no. of batches
class_mode='categorical')
test_set = test_datagen.flow_from_directory('test',
target_size=(64,64),
batch_size=12,
class_mode='categorical')
classifier.fit_generator(training_set,
steps_per_epoch=len(training_set), # Total training images
epochs = 20, # Total no. of epochs
validation_data = test_set,
validation_steps = len(test_set)) # Total testing images
classifier.save("model.h5")
#Prediction
classes = ['Fresh Apple','Fresh Banana','Fresh Orange','Rotten Apple','Rotten Banana','Rotten
Orange']
from keras.preprocessing import image
from keras.models import load_model
import numpy as np
new_model = load_model('model.h5')
filename = 'a1.jpeg'
new_model.summary()
test_image = image.load_img('images\\a1.jpg',target_size=(64,64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = new_model(test_image)
result1 = result[0]
for i in range(6):
if result1[i] == 1.:
break;
prediction = classes[i]
print(prediction)
我的模型为任何输入提供相同的输出。错误和警告已被删除,但输出仍然保持不变。早些时候,模型在删除警告之前给出了相同的值“A”(示例),在删除警告之后,模型给出了相同的值“B”。我不知道我的代码中的问题是在模型中还是在#Prediction 中。
解决方案
有几件事。在您的生成器中,您将批量大小设置为 12。然后在 model.fit 中,您有 steps_per_epoch=len(training_set)。这意味着您将在每个 epoch 中遍历您的训练集 12 次。我通常将每个时期的步骤和验证步骤保留为无。model.fit 将在内部确定值,但如果你想然后设置
steps_per_epoch = int(len(train_set/batch_size) + 1
validation_steps= int(len(test_set/batch_size) +1
现在在预测中。您将训练和测试图像缩放了 1/255。您需要对希望预测的图像执行相同的操作。所以在扩展维度的代码之后添加代码
test_image=test_image/255
推荐阅读
- ios - 如何在情节提要的标签栏上启动导航控制器的第三个视图控制器
- android-layout - 在 CollapsingToolbarLayout 中隐藏 Cardview 和 ImageView
- c# - C# FluentAssertions 在断言失败后继续
- javascript - 尝试启用 CORS:未定义要求错误
- node.js - 会话不与 express-session 和 connect-mongo 保持一致
- python - 如何使用python中的url将文件上传到存储桶?
- python - 使用 SQL Server 连接主机和实例的 Django 数据库
- android - Flutter:在堆栈小部件中动态添加拖放小部件
- java - 使用加速度计和陀螺仪移动鼠标光标
- bdd - 空手道是否支持 Def 变量更新最新数据