我从不同的两个模型获得相同尺寸的输出。我想减去这两个输出并将结果作为输入以形成新模型。但不幸的是,我收到以下错误:
“减”对象不可下标
我的代码:
# create a data generator
datagen2 = ImageDataGenerator(horizontal_flip=True,brightness_range=[0.7,1.0],rotation_range=10,width_shift_range=[-10,10],preprocessing_function=preprocess_input)
# load anad iterate training dataset
train_it2 = datagen2.flow_from_directory('DATA/train/', class_mode='categorical', batch_size=50,subset='training',target_size=(224, 224),shuffle=True)
test_it2 = datagen2.flow_from_directory('DATA/val/', class_mode='categorical', batch_size=50,target_size=(224, 224))
#subtract layer model
model_del1 = VGG16(include_top=False, weights='imagenet')
#add layers
model_del1_x = Conv2D(512, (3, 3), activation='relu',kernel_regularizer=l2(0.001), bias_regularizer=l2(0.002))(model_del1.output)
model_del1_x = Conv2D(4,(1,1), activation='relu')(model_del1_x)
model_del1_x = GlobalAveragePooling2D()(model_del1_x)
model_del1 = Model(inputs=model_del1.input, outputs=model_del1_x)
#print(model_del1.summary())
model_del2 = VGG16(include_top=False, weights='imagenet')
#add layers
model_del2_x = Conv2D(512, (3, 3), activation='relu',kernel_regularizer=l2(0.001), bias_regularizer=l2(0.002))(model_del2.output)
model_del2_x = Conv2D(4,(1,1), activation='relu')(model_del2_x)
model_del2_x = GlobalAveragePooling2D()(model_del2_x)
model_del2 = Model(inputs=model_del2.input, outputs=model_del2_x)
#print(model_del2.summary())
model_del_fin = keras.layers.Subtract()[model_del1_x, model_del2_x]
model_del_fin_x = Activation('softmax')(model_del_fin)
model_del_fin = Model(inputs=model_del_fin.input, outputs=model_del1_x)
print(model_del_fin.summary())
我也尝试了model_del_fin = keras.layers.Subtract()[model_del1.output, model_del2.output]
,但是遇到了同样的错误。
请告诉我我犯的错误以及如何解决?
答案 0 :(得分:2)
在Python中,object[a]
始终意味着:通过object
索引('subscript')a
;您要寻找的是调用 object
来触发其call
方法(并因此将其用作函数):
keras.layers.Subtract()[model_del1.output, model_del2.output] # INCORRECT
keras.layers.Subtract()([model_del1.output, model_del2.output]) # CORRECT
但是,这不能解决整个问题-这取决于预期的用途;等待澄清。
更新:查看评论; OP似乎已经解决了。为了完成这个答案:目标是将减去模型的输出 backproagate 到整个集合,包括将其输出相减的两个模型。
这样做需要从两个模型的Input
中的每一个到第三模型的输出的完全连通的图;例如model3 = Model(inputs=[model1.input, model2.input], out)
。然后可以将数据作为model3.fit([x1, x2])
进行馈送,或者将x1 == x2
馈入model3.fit([x1, x1])
,将x1
馈给model1.input
,x2
{1}}。