预训练模型的层输出意外产生不同的输出

我有一个名为content_image(1, 224, 224, 3)大小的numpy数组。这是VGG网络输入的大小。

当我按照下面的方式将content_image传输到VGG网络的输入时:

model = vgg19.VGG19(input_tensor=K.variable(content_image), weights='imagenet', include_top=False)for layer in model .layers:    if layer.name == 'block5_conv2':        model_output = layer.output

这似乎产生了范围在[0, 1]的输出:

[0.06421799 0.07012904 0.         ... 0.         0.05865938    0.        ]   [0.21104832 0.27097407 0.         ... 0.         0.    0.        ] ...

另一方面,当我根据keras文档(从VGG19的任意中间层提取特征)应用以下方法时:

from keras.models import Modelbase_model = vgg19.VGG19(weights='imagenet'), include_top=False) model = Model(inputs=base_model.input, outputs=base_model.get_layer('block5_conv2').output)model_output = model.predict(content_image)

这种方法似乎产生了不同的输出。

[ 82.64436     40.37433    142.94958    ...   0.     27.992153     0.        ]   [105.935936    91.84446      0.         ...   0.     86.96397      0.        ] ...

这两种方法使用了相同的网络和相同的权重,并且传输了相同的numpy数组(content_image)作为输入,但它们产生了不同的输出。我期望它们应该产生相同的结果。


回答:

我认为如果你在第一种方法中使用Keras隐式创建的会话,你会得到相同的结果:

sess = K.get_session()with sess.as_default():    output = model_output.eval()    print(output)

我认为通过创建一个新会话并使用init = tf.global_variables_initializer()sess.run(init),你会改变变量的值。一般来说,不要创建新会话,而是使用Keras创建的会话(除非你有充分的理由这样做)。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注