2017-01-25 4 views
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のバックエンドを使用しています。

私はケラスにとってはとても新しいので、どうやって進めるか正確には分かりません。ドキュメントの確認layers.convolutional.Convolution2DNoneはバッチ(またはサンプル)の数に対応していますか? input_shape=(20,3,244,244)を代入すると、次のエラーException: Input 0 is incompatible with layer conv1_1: expected ndim=4, found ndim=5が生じました。 20の代わりに23000を使用すると、同じエラーが発生しました。

何か助けていただければ幸いです。以下は

は私のコードです:

# ====================== 
# 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

画像の予想サイズと実際のものとの間に不一致があります。お使いのモデルは、サイズが224 x 224で、付属のエラーメッセージに従って、実際のサイズは244 x 244です。

+0

私はそれを逃したとは思わない。 ありがとう!!!! –

関連する問題