gpt4 book ai didi

python - 使用 keras ImageGenerator 训练多输入模型

转载 作者:行者123 更新时间:2023-12-04 12:43:34 25 4
gpt4 key购买 nike

我建了一个 型号 它由两个分支组成,然后合并为一个。对于模型的训练,我想使用 ImageGenerator 来扩充图像数据,但不知道如何为混合输入类型工作。有人知道如何在 keras 中处理这个问题吗?
任何帮助将不胜感激!

最好的事物,
缺口

型号
第一个分支将图像作为输入:

img_model = Sequential()
img_model.add(Convolution2D( 4, 9,9, border_mode='valid', input_shape=(1, 120, 160)))
img_model.add(Activation('relu'))
img_model.add(MaxPooling2D(pool_size=(2, 2)))
img_model.add(Dropout(0.5))
img_model.add(Flatten())

第二个分支将辅助数据作为输入:

aux_model = Sequential()
aux_model.add(Dense(3, input_dim=3))

然后那些得到 合并到最终模型 :

model = Sequential()
model.add(Merge([img_model, aux_model], mode='concat'))
model.add(Dropout(0.5))
model.add(Dense(5))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])

培训/问题:
我尝试执行以下显然失败的操作:

datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=10, #180, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=False, # randomly flip images
vertical_flip=False) # randomly flip images

model.fit_generator( datagen.flow( [X,I], Y, batch_size=64),
samples_per_epoch=X.shape[0],
nb_epoch=20,
validation_data=([Xval, Ival], Yval))

这会产生以下错误消息:

Traceback (most recent call last):
File "importdata.py", line 139, in <module>
model.fit_generator( datagen.flow( [X,I], Y, batch_size=64),
File "/usr/local/lib/python3.5/dist-packages/keras/preprocessing/image.py", line 261, in flow
save_to_dir=save_to_dir, save_prefix=save_prefix, save_format=save_format)
File "/usr/local/lib/python3.5/dist-packages/keras/preprocessing/image.py", line 454, in __init__
'Found: X.shape = %s, y.shape = %s' % (np.asarray(X).shape, np.asarray(y).shape))
File "/usr/local/lib/python3.5/dist-packages/numpy/core/numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not broadcast input array from shape (42700,1,120,160) into shape (42700)

最佳答案

我想我有办法完成这项工作。假设我们有多个输入模型。

#declare a final model with multiple inputs.
# final_model ...

train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2)
train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(224, 224), batch_size=32, class_mode='binary')

# NOTE: the zip combining multiple image generators with on the fly augmentation.
final_generator = zip(train_generator, train_generator)
final_model.fit_generator(final_generator, samples_per_epoch=nb_train_samples, nb_epoch=nb_epoch, validation_data=test_generator, nb_val_samples=nb_validation_samples)

关于python - 使用 keras ImageGenerator 训练多输入模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38749021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com