我从不同的两个模型获得相同尺寸的输出。我想减去这两个输出并将结果作为输入以形成新模型。但不幸的是,我收到以下错误:
“减”对象不可下标
我的代码:
# 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/',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,bias_regularizer=l2(0.002))(model_del2.output)
model_del2_x = Conv2D(4,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]
,但是遇到了同样的错误。
请告诉我我犯的错误以及如何解决?