我想向 Resnet50 模型添加更多层,我的问题是 - 我需要编译它并在新数据上训练它还是可以直接使用它?它只会给我 Resnet50 结果吗?
这是我正在尝试的:
def base_model():
resnet = resnet50.ResNet50(weights="imagenet", include_top=False)
x = resnet.output
x = Conv2D(128, (3, 3), activation='relu',padding='same')(x)
x = Conv2D(128, (3, 3), activation='relu',padding='same')(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Conv2D(256, (3, 3), activation='relu',padding='same')(x)
x = Conv2D(256, (3, 3), activation='relu',padding='same')(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = Conv2D(512, (3, 3), activation='relu',padding='same')(x)
x = Conv2D(512, (3, 3), activation='relu',padding='same')(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
x = GlobalAveragePooling2D()(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.6)(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.6)(x)
x = Lambda(lambda x_: K.l2_normalize(x,axis=1))(x)
return Model(inputs=resnet.input, outputs=x)
然后像这样使用它:
enhanced_resnet = base_model()
img = image.load_img(file, target_size=(224, 224))
img = image.img_to_array(img)
x = resnet50.preprocess_input(img)
x = np.array([x])
feature = enhanced_resnet.predict(x)
我想要的返回值是图像的特征而不是预测,因为我使用距离方程来判断图像之间的相似性。