3

这是完整的错误:

Exception: Error when checking model input: expected convolution2d_input_1 to have shape (None, 3, 224, 224) but got array with shape (20, 3, 244, 244)

一切正常,直到最后model.fit_generator(...)的代码块。我正在使用 theano 后端。

我对 keras 很陌生,所以我不确定如何进行。检查文档我可以看到Noneinlayers.convolutional.Convolution2D对应于批次(或样品)的数量?替换input_shape=(20,3,244,244)产生以下错误Exception: Input 0 is incompatible with layer conv1_1: expected ndim=4, found ndim=5。使用 23000 而不是 20 会产生相同的错误。

任何帮助表示赞赏。

下面是我的代码:

# ======================
# load data
# ======================

# Set relevant paths for dir structure
current_dir = "/home/ubuntu/nbs/"
DATA_HOME_DIR = current_dir + 'lesson1/data/redux'
path = DATA_HOME_DIR + '/'
train_path = DATA_HOME_DIR + '/train/'
valid_path = DATA_HOME_DIR + '/valid/'
test_path = DATA_HOME_DIR + '/test/'

nb_train_samples = 23000
nb_validation_samples = 2000
nb_epoch = 4

# ======================
# import stuff
# ======================
import numpy as np 
from keras.utils.data_utils import get_file 
from keras import backend as K 
from keras.layers.normalization import BatchNormalization
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.layers.convolutional import Convolution2D, MaxPooling2D,         ZeroPadding2D
from keras.layers.pooling import GlobalAveragePooling2D
from keras.optimizers import SGD, RMSprop, Adam
from keras.preprocessing import image
from keras.preprocessing.image  import  ImageDataGenerator



# ======================
# define model
# ======================

def vgg():
model = Sequential()
model.add(Convolution2D(64, 3, 3,input_shape=(3,224,224), activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
return model 


model = vgg()

print model.summary()

#### load weights
   fname = 'vgg16.h5'
model.load_weights(get_file(fname, 'http://www.platform.ai/models/'+fname, cache_subdir='models'))

print "successfully created model and loaded weights"







#### Finetune model
model.pop()
for layer in model.layers: layer.trainable=False
    model.add(Dense(batches.nb_class, activation='softmax'))

#### Compile model
model.compile(optimizer=Adam(lr=0.01),
                loss='categorical_crossentropy', metrics=['accuracy'])






train_datagen = ImageDataGenerator(
    rescale = 1./255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_path, 
    target_size=(244,244),
    batch_size = 20,
    class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
    valid_path,
    target_size=(244,244),
    batch_size=20,
    class_mode='categorical')



model.fit_generator(
    train_generator,
    samples_per_epoch=nb_train_samples,
    nb_epoch=nb_epoch,
    validation_data=validation_generator,
    nb_val_samples=nb_validation_samples)
4

1 回答 1

4

图像的预期大小与实际大小不匹配。您的模型需要大小的图像,224 x 224根据附加的错误消息,实际大小是244 x 244.

于 2017-01-25T09:09:51.723 回答