如何使用Keras功能模型移除图层?

pcww981p  于 2022-12-13  发布在  其他
关注(0)|答案(1)|浏览(143)

我试图修改ResNet50 * 开头 * 的层,因此include_top=False将不起作用。我知道使用顺序模型的标准方法存在问题,因为ResNet是一个功能模型,因为存在跳过连接。
基本上我想去掉前20层左右,用我自己的层来代替它们。我能用函数API做到这一点吗?
谢谢

wxclj1h5

wxclj1h51#

如果有人在未来看到这个问题,我在无数次谷歌搜索和多个论坛发帖后终于找到了答案。我在任何地方都没有找到答案,只是在函数中玩,直到我找到了解决方案。

base_model = ResNet50(weights="imagenet", include_top=False, input_shape=(224, 224, 3))      

truncated_model = Model(inputs = base_model.layers[7].input, outputs = base_model.layers[-1].output) #truncates the functional model from layer 7 to final layer  

keras.utils.plot_model(truncated_model, "mini_resnet.png", show_shapes=True) #Plots the functional model graph

相关问题