使用辅助数据进行Keras图像增强

时间:2018-08-22 18:34:54

标签: python image machine-learning keras classification

我正在尝试使用两个输入对图像进行分类:1)图像,和2)附加到图像的辅助数据,例如,图像调整为网络输入之前的原始宽度:

input_anci = Input(shape=(1, ), name='extra_input')
input_images = Input(shape=(img_width, img_height, 3), name='image_input')

vgg_out = base_model(input_images)  # Here base_model is a VGG16
f0 = Flatten()(vgg_out)
f0 = Dense(64, activation = 'relu')(f0)
f0 = Dropout(0.5)(f0)

f1 = Dense(8, activation = 'relu')(input_anci)
f01 = Concatenate(axis=-1)([f0, f1])

classidx = Dense(num_classes, activation = 'softmax')(f01)
model = Model(inputs=[input_images, input_anci], outputs=classidx)

为了对图像进行增强,我使用了ImageDataGenerator

train_datagen = ImageDataGenerator(
        rotation_range = 360,
        width_shift_range = 0.2,
        height_shift_range = 0.2,
        shear_range = 0.2,
        zoom_range = 0.2,
        horizontal_flip = True,
        vertical_flip = True,
        fill_mode = 'nearest',
        validation_split = 0.25)

然后我定义了训练和验证生成器:

train_generator = train_datagen.flow((X, X_anci), Y, subset = 'training')

validation_generator = train_datagen.flow((X, X_anci), Y, subset = 'validation')

然后我用fit_generator训练模型:

history = model.fit_generator(train_generator,validation_data = validation_generator, ...)

运行它时,出现以下错误消息:

Traceback (most recent call last):
  File "vgg16-v3x.py", line 265, in <module>
    subset = 'training')
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 705, in flow
    subset=subset)
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\keras\preprocessing\image.py", line 1116, in __init__
    (np.asarray(x).shape, np.asarray(y).shape))
  File "C:\Users\wzhou\AppData\Local\Continuum\Anaconda2\envs\tensorflow\lib\site-packages\numpy\core\numeric.py", line 492, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (824,56,56,3) into shape (824)

我该怎么做才能确保以正确的方式将(X, X_anci)馈入[input_images, input_anci]

PS。这是我第一次发问题。希望格式不会太差。

0 个答案:

没有答案