简体   繁体   English

CNN model 预测任何输入的相同 output

[英]CNN model predicting the same output for any inputs

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)

My model is giving the same output for any input.我的 model 为任何输入提供相同的 output。 The errors and warnings have been removed but the output still remains the same.错误和警告已被删除,但 output 仍然保持不变。 Earlier the model was giving same value 'A'(example) before removing Warnings and after removing Warnings, the model is giving same value 'B'.早些时候,model 在删除警告之前给出相同的值“A”(示例),在删除警告之后,model 给出相同的值“B”。 I don't know where is the problem in my code whether it is in model or whether it is in #Prediction.我不知道我的代码中的问题是在 model 中还是在#Prediction 中。

A couple of things.有几件事。 In your generators you set a batch size of 12. then in model.fit you have steps_per_epoch=len(training_set).在您的生成器中,您将批量大小设置为 12。然后在 model.fit 中,您有 steps_per_epoch=len(training_set)。 This means you will go through your training set 12 times per epoch.这意味着您将在每个 epoch 12 次通过您的训练集进行 go。 I usually leave steps per epoch and validation steps as None.我通常将每个时期的步骤和验证步骤保留为无。 model.fit will determine the value internally but if you want to then set model.fit 将在内部确定该值,但如果您想再设置

steps_per_epoch = int(len(train_set/batch_size) + 1
validation_steps= int(len(test_set/batch_size) +1

Now in predictions.现在在预测中。 You scaled your train and test images by 1/255.您将训练和测试图像缩放了 1/255。 You need to do the same for images you wish to predict.您需要对希望预测的图像执行相同的操作。 So right after the code to expand dimension add code所以在扩展维度的代码之后添加代码

test_image=test_image/255

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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