简体   繁体   English

keras 和 tf.keras 中的 ResNet 模型对同一图像给出不同的输出

[英]ResNet model in keras and tf.keras give different output for the same image

For a given image, I'm extracting ResNet features ie after all conv layers and global max pooling, which gives a 2048 length vector per image.对于给定的图像,我正在提取 ResNet 特征,即在所有 conv 层和全局最大池化之后,每个图像给出 2048 长度的向量。

Earlier, I was using keras==2.3.1 with backend tensorflow==1.13.1 .早些时候,我使用keras==2.3.1和后端tensorflow==1.13.1 Now, I've shifted to tensorflow==2.0.0 since keras has been merged with tensorflow.现在,自从 keras 与 tensorflow 合并后,我已经转移到tensorflow==2.0.0 I replaced my code with tf.keras instead of keras .我用tf.keras而不是keras替换了我的代码。

But now the features extracted are not the same as the features extracted earlier.但是现在提取的特征和之前提取的特征不一样了。 ResNet is a model which is independent of tensorflow/keras or even pytorch for that matter. ResNet 是一个独立于 tensorflow/keras 甚至 pytorch 的模型。 It's functionality is predefined.它的功能是预定义的。 Why is this difference occuring?为什么会出现这种差异? Are there any parameters that can be tweaked to get the same functionality?是否有任何参数可以调整以获得相同的功能?

Edit 1 : Adding code编辑1 :添加代码

from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
resnet_model = ResNet50(include_top=False)

The models themselves keep updating and the weights with better results are used.模型本身不断更新,并使用具有更好结果的权重。 Here is the releases folder where you can see releases of different weight files.是发布文件夹,您可以在其中查看不同权重文件的发布。 The folder linked is for keras, which is most probably also used by tf.keras.链接的文件夹用于 keras,它很可能也被 tf.keras 使用。 The problem you are facing is probably because of the version of library rather than method of calling the model.您面临的问题可能是因为库的版本而不是调用模型的方法。

For example, here you can find the keras_applications code and check the weight path which points to release v0.2(linked is keras but similarly for tensorflow.keras).例如, 在这里您可以找到 keras_applications 代码并检查指向版本 v0.2 的权重路径(链接是 keras,但对于 tensorflow.keras 也是如此)。

The older library versions have older links, updating the libraries also updates the paths.较旧的库版本具有较旧的链接,更新库也会更新路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM