ValueError:期望卷积2d_input_8具有形状(无,3,150,150)但具有形状的数组(32,150,150,3)

时间:2017-02-18 06:07:45

标签: python tensorflow deep-learning keras

我收到以下错误

  

ValueError:检查模型输入时出错:预期卷积2d_input_8具有形状(无,3,150,150)但是具有形状的数组(32,150,150,3)

我认为这是因为dim_ordering。我正在使用tensorflow后端。 但如果我删除它,我会收到另一个错误:

  

ValueError:由于Conv2D_17'从Conv2D_17'中减去3而导致的负尺寸大小。 (op:' Conv2D')输入形状:[?,1,74,16],[3,3,16,32]。

import keras
import numpy as np
np.random.seed(123)  # reproducibility

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense


# expected image size
img_width, img_height = 150, 150
# folder containing the images on which
# the network will train. The train folder 
# has two sub folders, dogs and cats.
train_data_dir = 'C:/Users/Ishan Sohony/Downloads/Compressed/train'
# folder containing the validation samples
# folder structure is same as the training folder
validation_data_dir = 'C:/Users/Ishan Sohony/Downloads/Compressed/test1'
# how many images to be considered for training
train_samples = 2000
# how many images to be used for validation
validation_samples = 800
# how many runs will the network make
# over the training set before starting on
# validation
epoch = 50
# this is the augmentation configuration we will use for training
# we are generating a lot of transformed images so that the model
# can handle variety in the real world scenario
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this section is actually taking images from the folder
# and passing on to the ImageGenerator which then
# creates a lot of transformed versions

train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=32,
        class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=32,
        class_mode='binary')
# ** Model Begins **
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height),dim_ordering='th'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering='th'))

model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering='th'))

model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2),dim_ordering='th'))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
# ** Model Ends **
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])


# this is where the actual processing happens
# it will take some time to run this step.
model.fit_generator(
        train_generator,
        samples_per_epoch=train_samples,
        nb_epoch=epoch,
        validation_data=validation_generator,
        nb_val_samples=validation_samples)

model.save_weights('trial.h5')

1 个答案:

答案 0 :(得分:0)

  

输入形状

     

4D张量形状:(样本,通道,行,列)if   dim_ordering ='th'或4D张量形状:(样本,行,列,   渠道)如果dim_ordering ='tf'。

     

输出形状

     

4D张量形状:(样本,nb_filter,new_rows,new_cols)如果   dim_ordering ='th'或4D张量形状:(样本,new_rows,   new_cols,nb_filter)如果dim_ordering ='tf'。行和列值可能   由于填充而发生了变化。   source

确实它来自尺寸排序。 使用Theano或Tensorflow时,不仅输入必须不同,输出也是如此。

如果我理解正确,你应该像往常一样使用tf后端,但是然后将输入形状改为(150,150,3)。 如果您真的想要更改为Theano,请考虑将dim_ordering设置为theano以获得所有卷积层,以便它们可以相互“说话”输出和输入形状。

这有帮助吗?