基于CNN模型的图像预测总是返回相似的结果

2024-03-29 14:52:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想问你一个关于一个模型的问题,我已经在这个模型上断头几天了。。。 我试图写一个CNN模型来预测两个类之间的结果(类似于猫或狗的问题)。 我的代码可以识别Train和Validation文件夹中的图片,以及Test文件夹中要预测的图片,但是所有预测图片的结果几乎相同,并且精度非常低(0.49),(附上一张图片)。photo_of_the_results_from_sample_data

我试着在adam,SGD,RMSprop之间改变优化器 我试着把损失改成稀疏的绝对的 我尝试将度量值更改为混乱矩阵(出于某种原因,它甚至没有识别) 什么都没用。。。。。。你知道吗

有人能帮我修改代码吗? 非常感谢您的先进!!你知道吗

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, 
MaxPooling2D
from keras.optimizers import adam, SGD, RMSprop
from keras.preprocessing.image import ImageDataGenerator

# 1 - Load data
train_dataset = "\\Pictures_Dogs_DataSet\\Train1/"
validation_dataset="\\Pictures_Dogs_DataSet\\Validation1/"
test_dataset = "\\Pictures_Dogs_DataSet\\Test/"

# 2 - Create network layers
image_width = 200 
image_height = 200

model = Sequential()

# add convolutional layer (CNN)
# 32 X 32 pixel matrix
# 3 layars (RGB)
# 3 X 3 sliding matrix
# a pixel value for each colour is between 0-255
model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape= 
(image_width,image_height,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
# Add another block for more abstract features
model.add(Conv2D(32,(3,3), input_shape=(image_width, 
image_height,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
# Add another block for more abstract features
model.add(Conv2D(64,(3,3), input_shape=(image_width, 
image_height,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
# Add another block for more abstract features
model.add(Conv2D(128,(3,3), input_shape=(image_width, 
image_height,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

# Let's DropOut some connections to avoid over-fitting.
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(32,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1,activation='sigmoid'))


# Using the prediction model
sgd = SGD(lr=0.001)
model.compile(optimizer=sgd,loss='binary_crossentropy',
          metrics=['accuracy'])

# used to rescale the pixel values from [0, 255] to [0, 1] 
interval
datagen =ImageDataGenerator(rescale=1./255)

# automatically retrieve images and their classes for train 
and validation sets
train_generator = datagen.flow_from_directory(
        train_dataset,
        target_size=(image_width, image_height),
        batch_size=32,
        class_mode='binary')

validation_generator = datagen.flow_from_directory(
        validation_dataset,
        target_size=(image_width, image_height),
        batch_size=32,
        class_mode='binary')

#  3 - Train
model.fit_generator(
        train_generator,
        steps_per_epoch=10,
        epochs=1,  
        #just 1 epoch to practice and make sure it works#
        validation_data=validation_generator,
        validation_steps=5)


#evaluate the accuracy of the model
model.evaluate_generator(validation_generator,verbose=0)

#save modules
#model.save('model.h5')

# 5 - predict
test_generator =datagen.flow_from_directory(
        directory=test_dataset,
        target_size=(image_width,image_height),
        class_mode=None,
        batch_size=32)

print(model.predict_generator(test_generator))

Tags: thefromimageaddsizemodelwidthgenerator