我想创建一个多输入深度学习模型。该模型从不同的数据集中获取两个输入(图像),并计算它们的平均值。查看代码:
input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
a = keras.layers.average([x1, x2])
out = keras.layers.Dense(4)(a)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
我尝试使用以下代码创建生成器,但出现错误:
input_imgen = ImageDataGenerator(
rotation_range=10,
shear_range=0.2,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1
)
test_imgen = ImageDataGenerator()
def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
genX1 = generator.flow_from_directory(dir1,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
while True:
X2i = genX2.next()
X1i = genX1.next()
yield X1i[0], X2i[0]
inputgenerator=generate_generator_multiple(generator=input_imgen,
dir1=train_data1,
dir2=train_data2,
batch_size=32,
img_height=224,
img_width=224)
validgenerator=generate_generator_multiple(generator=test_imgen,
dir1=valid_data1,
dir2=valid_data2,
batch_size=32,
img_height=224,
img_width=224)
testgenerator=generate_generator_multiple(generator=test_imgen,
dir1=test_data1,
dir2=test_data2,
batch_size=32,
img_height=224,
img_width=224)
# compile the model
multi_model.compile(
loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy']
)
# train the model and save the history
history = multi_model.fit_generator(
inputgenerator,
steps_per_epoch=len(train_data) // batch_size,
epochs=10,
verbose=1,
validation_data=validgenerator,
validation_steps=len(valid_data) // batch_size,
use_multiprocessing=True,
shuffle=False)
我收到此错误:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[108.930984, 108.930984, 108.930984],
[113.63957 , 113.63957 , 113.63957 ],
[113.07516 , 113.07516 , 113.07516 ],
...,
[ 99.46968 , 99.46968 , 99.46968 ...
如何解决此问题并创建生成器?
答案 0 :(得分:1)
引发错误,因为您的模型有两个输入,但在此行中:
yield X1i[0], X2i[0]
生成器将返回两个数组的元组。在fit_generator
中,第一个解释为模型输入,第二个解释为模型输出。因此,您将得到该错误,提示您仅将一个输入传递给模型。要解决此问题,请将输入放入列表中,并返回标签,无论它们是什么:
yield [X1i[0], X2i[0]], the_labels_array