代码之家  ›  专栏  ›  技术社区  ›  noobcoder

如何在Keras中改变预先训练的CNN模型中的层输出?

  •  0
  • noobcoder  · 技术社区  · 5 年前

    model = VGG16()
    image = load_img('mug.jpg', target_size=(224, 224))
    image = img_to_array(image)
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    image = preprocess_input(image)
    yhat = model.predict(image)
    label = decode_predictions(that)
    label = label[0][0]
    
    # print the output
    print('%s (%.2f%%)' % (label[1], label[2]*100))
    

    现在,我想查看第一层的输出,并将其更改/添加噪波,并查看分类如何更改。我不确定如何做到这一点,也找不到任何与我的查询匹配的合适资源。

    我是新来的Keras,所以在这方面的任何帮助将不胜感激。谢谢您!

    0 回复  |  直到 5 年前
        1
  •  2
  •   Berkay Berabi    5 年前

    任何层的输出都可以通过

    model.layers[index].output
    

    所以你可以这么做

    outputlayer1 = model.layers[0].output
    outputlayer1 += noise
    

    稍后要进行前向传递,可以在层上迭代并执行前向传递。有关前向传递,请参阅此链接中的调用函数 https://keras.io/api/layers/base_layer/

    推荐文章