尝试增强图像时fit_generator错误

时间:2017-09-22 13:12:12

标签: deep-learning keras

我的图像数据非常少,我想进行数据增加以增加训练数据的数量。 我正在尝试在keras博客中实现代码:

https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

但是,当我尝试朗读fit_generator函数时,我得到错误:

"检查目标时出错:预期activation_5有形状(无,1)但是有阵形(8,2)"

你能看一下我的简单代码吗,请帮我弄清楚问题是什么?

from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
K.clear_session()
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(1,256,256)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

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.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2)
X_train = X_train.reshape(X_train.shape[0] ,1, 256, 256)
X_test = X_test.reshape(X_test.shape[0] ,1, 256, 256)
train_datagen = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rescale = 1./255,
    rotation_range=20,
    zoom_range = 0.2,
    height_shift_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(featurewise_center=True,
                                 featurewise_std_normalization = True,
                                 rescale = 1./255)
train_datagen.fit(X_train)
test_datagen.fit(X_test)
train_generator = train_datagen.flow(X_train,y_train,batch_size = 8 , save_to_dir='train',save_prefix='aug')
validation_generator = test_datagen.flow(X_test,y_test,batch_size =8 , save_to_dir='test' , save_prefix = 'aug')
batch_size = 8
model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // batch_size,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=800 // batch_size)
model.save_weights('first_try.h5')  # always save your weights after training or during training

然后我得到了那个错误..

0 个答案:

没有答案