如何从KERAS应用程序修改内置模型

发布于 2025-02-05 17:57:25 字数 1323 浏览 4 评论 0原文

我正在一些小数据集上训练预先训练的KERAS应用模型,然后使用Grad-CAM可视化图像中每一层中发生的情况。基本上,我想看看哪些层负责识别图像中的兴趣点。 之后,我想做的是删除对我的目标没有贡献的层,并查看所得的体系结构是否提供相同的指标,或者它的效率降低。

我找不到如何从内置KERAS应用程序模型中删除某些层。有人知道这是否可能吗?如果是,如何?

更新

最终解决了它。这是代码:


initial_model = tf.keras.applications.VGG16(weights = 'imagenet',include_top = False)
initial_model.trainable = False

#adding all the layers from the architecture into a list
layers = [l for l in initial_model.layers]

#building a new model with the existing layers with their 'imagenet' weights
modified_model = keras.Sequential()
inputs = keras.Input(shape = (None,None,3))
modified_model.add(inputs)

#removing the last block from my VGG16 architecture and
for layer in layers:
  if 'block5' not in layer.name:
    layer._inbound_nodes = []
    layer._outbound_nodes = []
    modified_model.add(layer)


modified_model.add(keras.layers.GlobalAveragePooling2D())
#my data has 38 classes, this last layer depends on the number of classes
modified_model.add(keras.layers.Dense(38, activation="softmax"))

modified_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])


history = modified_model.fit(ds_train,
                    epochs = 40,
                    validation_data = ds_valid,
                    verbose = 1)

I am training the pre-trained keras application models on some small dataset and then visualize what happens in each layer of the model to the images using Grad-CAM. Basically, I want to see which layers are in charge of recognizing the points of interest in the image.
What I want to do after that is to remove the layers that do not contribute to my goal and see if the resulting architecture delivers the same metrics or it will become less efficient.

I wasn't able to find how to remove certain layers from a built-in Keras application model. does any one know if this is possible and if yes, how?

Update

Finally solved it. Here is the code:


initial_model = tf.keras.applications.VGG16(weights = 'imagenet',include_top = False)
initial_model.trainable = False

#adding all the layers from the architecture into a list
layers = [l for l in initial_model.layers]

#building a new model with the existing layers with their 'imagenet' weights
modified_model = keras.Sequential()
inputs = keras.Input(shape = (None,None,3))
modified_model.add(inputs)

#removing the last block from my VGG16 architecture and
for layer in layers:
  if 'block5' not in layer.name:
    layer._inbound_nodes = []
    layer._outbound_nodes = []
    modified_model.add(layer)


modified_model.add(keras.layers.GlobalAveragePooling2D())
#my data has 38 classes, this last layer depends on the number of classes
modified_model.add(keras.layers.Dense(38, activation="softmax"))

modified_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])


history = modified_model.fit(ds_train,
                    epochs = 40,
                    validation_data = ds_valid,
                    verbose = 1)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文