编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

Keras尺寸与ImageDataGenerator不匹配:Keras dimension mismatch with ImageDataGenerator

torressam333 keras 2022-5-10 14:14 14人围观

腾讯云服务器
Keras尺寸与ImageDataGenerator不匹配的处理方法

我正在尝试使用Keras将数据流"入神经网络.我正在使用.flow_from_directory方法,该过程适合我.我正在使用keras文档中的基本示例(我正在使用tensorflow):

I am attempting to 'flow' my data into a neural network with Keras. I am using the .flow_from_directory method and the process is giving me fits. I am using the basic example from the keras documentation (I am using tensorflow):

ROWS = 64 COLS = 64 CHANNELS = 3 from keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator( rescale=1./255) test_datagen = ImageDataGenerator(rescale=1./255) train_generator = train_datagen.flow_from_directory( 'train', target_size=(64, 64), batch_size=32, class_mode='binary') validation_generator = test_datagen.flow_from_directory( '../tutorial/l1/kaggle_solutions/dogs_vs_cats/valid', target_size=(64, 64), batch_size=1, class_mode='binary') from keras.models import Sequential from keras.layers import Convolution2D from keras.layers import Dense, Activation, Flatten, Dropout, MaxPooling2D from keras.regularizers import l2 model = Sequential() model.add(Convolution2D(4, 4, 4, border_mode='same', input_shape=(64, 64,3), activation='relu')) from keras.utils.np_utils import to_categorical from keras.optimizers import SGD, RMSprop model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=1e-4), metrics=['accuracy']) model.fit_generator( train_generator, samples_per_epoch=2500, nb_epoch=20, validation_data=validation_generator, nb_val_samples=3100) 

运行此命令会出现以下错误:

Running this i get the following error:

Exception: Error when checking model target: expected convolution2d_84 to have 4 dimensions, but got array with shape (32, 1) 

我已经研究了很长时间,发现了以下内容-将'model.add'切换为灰度输入model.add(Convolution2D(4,4,4,border_mode ='same',input_shape =(64,64,3),activation ='relu'))给我以下错误(按预期方式-但似乎确认我的原始输入正确):

I have been tinkering around for a long time and found the following--switching the 'model.add' to grayscale input model.add(Convolution2D(4, 4, 4, border_mode='same', input_shape=(64, 64,3), activation='relu')) gives me the following error (as expected--but appears to confirm my original input was correct):

Error when checking model input: expected convolution2d_input_49 to have shape (None, 64, 64, 1) but got array with shape (32, 64, 64, 3) 

所以我(在原始文件中)与原始文件一起传递了4、32、64、64、3数组,但是我得到了我认为是的错误预期(1,64,64,3)并得到(32,64,64,3)

So I am passing (in the original) a 4-d array of 32,64,64,3 with the original, but I am getting the error that I THINK means Expected (1,64,64,3) and got (32,64,64,3)

当我以32个批次发送数据时,如果我将批次设置为零(给出0、64、64、3输入),我就很奇怪了:

As I am sending data in batches of 32. Curiously enough if I set the batch to zero (to give a 0,64,64,3 input) I get:

Exception: Error when checking model target: expected convolution2d_87 to have 4 dimensions, but got array with shape (0, 1) 

基于文档,我无法找出将数据流到模型中的正确方法-使用fit_generator时无法将批处理大小传递给模型,并且看来batch_size(样本数)是问题.

Based on the documentation, I cannot figure out the proper way to flow the data into the model--i cannot pass the batch size to the model when using fit_generator, and it appears that the batch_size (num of samples) is the problem.

任何帮助将不胜感激.

问题解答

您的 ImageDataGenerator 没问题.如错误消息中所述,模型输出的形状与其目标的形状之间不匹配.您使用 class_mode ='binary',因此模型的预期输出是单个值,但它会产生形状为(batch_size,64,64,4)的输出,因为您只有一个卷积层,而模型中没有其他东西.

There is no problem with your ImageDataGenerator. As stated in the error message there is a mismatch between the shape of your model output and the shape of its targets. You use class_mode = 'binary', so expected output of your model is a single value, but instead it yields output of shape (batch_size, 64, 64, 4) since you have one convolutional layer and nothing else in your model.

尝试这样的事情:

model.add(Convolution2D(4, 4, 4, border_mode='same', input_shape=(64, 64,3), activation='relu')) model.add(Flatten()) model.add(Dense(1)) model.add(Activation('sigmoid')) 

这篇关于Keras尺寸与ImageDataGenerator不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^