- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 mmdnn 将两个模型(vgg16 和 resnet50)从 Keras 和 TensorFlow 后端(从作为 model.save 文件)转换为 PyTorch。这是通过以下方式完成的:
mmconvert -sf keras -iw vgg.h5 -df pytorch -om keras_to_torch.pt
A = imp.load_source('MainModel','/weights/keras_to_torch.py')
model = torch.load('/weights/keras_to_torch.pt')
对同一数据集进行预测给了我一组不同的结果,因此我进行了进一步调查。
最佳答案
问题一定出在您定义 keras 模型的方式上,因为我无法使用使用 MMdnn 包提供的 h5 文件来复制该问题。如果你想使用 resnet50 和 VGG19 模型,你可以得到正确的权重,如下所示:
mmdownload -f keras -n resnet50 -o ./
mmconvert -sf keras -iw ./imagenet_resnet50.h5 -df pytorch -om keras_to_torch.pt
然后提取生成的numpy文件,
keras_to_torch.pt
和
keras_to_torch.py
来自 docker 容器(和
imagenet_resnet50.h5
用于比较)。
import keras
model = load_model('imagenet_resnet50.h5')
和火炬模型使用
import imp
import torch
torch_weights = # path_to_the_numpy_weights
A = imp.load_source('MainModel','keras_to_torch.py')
weights_torch = A.load_weights(torch_weights)
model_torch = A.KitModel(torch_weights)
我还必须设置
allow_pickle = True
在
load_weights(weight_file)
keras_to_torch.py
开头的函数文件。
torch.load('/weights/keras_to_torch.pt')
不幸的是,变体为我抛出了一个错误。
# keras model
model.layers[-1].weights
# Output:
#tensor([[-0.0149, 0.0113, -0.0507, ..., -0.0218, -0.0776, 0.0102],
# [-0.0029, 0.0032, 0.0195, ..., 0.0362, 0.0035, -0.0332],
# [-0.0175, 0.0081, 0.0085, ..., -0.0302, 0.0549, -0.0251],
# ...,
# [ 0.0253, 0.0630, 0.0204, ..., -0.0051, -0.0354, -0.0131],
# [-0.0062, -0.0162, -0.0122, ..., 0.0138, 0.0409, -0.0186],
# [-0.0267, 0.0131, -0.0185, ..., 0.0630, 0.0256, -0.0069]])
# torch model (make sure to transpose)
model_torch.fc1000.weight.data.T
# Output:
#[<tf.Variable 'fc1000/kernel:0' shape=(2048, 1000) dtype=float32, numpy=
# array([[-0.01490746, 0.0113374 , -0.05073728, ..., -0.02179668,
# -0.07764222, 0.01018347],
# [-0.00294467, 0.00319835, 0.01953556, ..., 0.03623696,
# 0.00350259, -0.03321117],
# [-0.01751374, 0.00807406, 0.00851311, ..., -0.03024036,
# 0.05494978, -0.02511911],
# ...,
# [ 0.025289 , 0.0630148 , 0.02041481, ..., -0.00508354,
# -0.03542514, -0.01306196],
# [-0.00623157, -0.01624131, -0.01221174, ..., 0.01376359,
# 0.04087579, -0.0185826 ],
# [-0.02668471, 0.0130982 , -0.01847764, ..., 0.06304929
#...
keras 和火炬模型的权重根据需要一致(最多 4 位数左右)。
imagenet_resnet50.h5
通过
mmdownload
获得模型与您在 keras 中使用 model.save 保存的模型不同,并纠正任何差异。
关于keras - 将模型从 keras h5 转换为 pytorch - 全连接层不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68002742/
我尝试在安装了多类型 MFC 库的 visual studio 2015 MFC 上运行以前编写的 MFC c++ 代码。 但是,我这里仍然有 12 个关于缺少函数的错误: IntelliSense:
我正在学习 OOP 并且有疑问。假设我有一个包含 ClassB.h 的文件 ClassA.h,并且在某些时候我的 ClassB.h 需要包含 ClassA .h。 这会产生一个错误,我想我明白为什么会
我开始使用 CUDA 进行编程,在一些示例中我找到了包含文件 cuda.h、cuda_runtime.h 和 cuda_runtime_api.h 包含在代码中。有人可以向我解释一下这些文件之间的区别
我有一些生成正则表达式的代码。那么下面的表达式实际上是: ^(?:\s*((exclude|include|hide|show|protect|risk|dir-merge|merge)),\s*((
我一直在查看一些源代码,以更好地了解我们使用的这款游戏的核心,并编写更可靠、更快速的插件。然后我发现了这段奇怪的代码...... public void setMaxH(double amount)
通常我们会使用标准类型作为 std::unordered_map 的键和值.但现在我需要自定义我自己的键和值类。 键类在block_cache_key.h 中定义如下: #ifndef BLOCK_C
例如,我想要两个头文件,它们可以依赖于另一个头文件中的函数。 //Header1.h file #include Header2.h void h1(){ //... func1(); } v
我正在研究来自 Sedgewick 的 Shell 排序 Algorithms in C part 1-4在第 172 页。 我使用 size (数组的长度),而不是 l和 r (开始和结束);所以我
我在 macOS BigSur 上通过 VMWare 使用 Ubuntu 20.04.2 LTS。我安装了最新版本的 tcl、tcl-dev、tk 和 tk-dev - 版本 8.6。我想编译 Arc
我用我的 glu 和 gl 头文件构建了一个 OpenGL 程序,默认包含在 windows 7 专业版中。现在,我买了一本描述 OpenGL 游戏开发的书。这本书的作者说,我必须在我的项目中包含 g
我想在 token 中保留特殊字符,同时仍对特殊字符进行 token 化。说我有话 "H&R Blocks" 我想将其标记为 "H", "R", "H&R", "Blocks" 我读了http://w
关于 hash 作为 trans 参数的另一个问题。在下面的代码中,简单地使用 hash 会给出不正确的结果,但是将其替换为 keys 和 values 会使其正确。怎么了? my @alph1 =
我已经编写了一个 C 程序,它获取屏幕像素的 RGB 值 (0-255),并知道其位置 (x,y)。它可以在 Linux 中运行,但是当我尝试在 Visual Studio (Windows) 中编译
我已经使用 Windows 7 专业版中默认包含的 glu 和 gl 头文件构建了一个 OpenGL 程序。现在,我买了一本描述 OpenGL 游戏开发的书。这本书的作者说,我必须将glew head
#include using namespace std; #include //#include int main() { initscr();
h:messages h:form 内的组件还显示与外部组件相关的消息。 如何限制它只显示与包含 h:form 内的组件相关的消息? 我不喜欢用单独的h:message来使我的代码膨胀。每个输入组件的
我下载了示例代码和 cpp 文件,其中包含 list.h、queue.h 和 vector.h 等头文件,如果我尝试构建,我会收到“ fatal error :没有这样的文件或目录编译终止”我想我应该
我有一个编译成功的桌面项目,但是在我向项目添加新配置以支持 Windows Mobile 平台后,我收到以下错误: error C2146: syntax error : missing ';' be
有很多关于这个错误的帖子,但我无法解决它,我希望你能拿出解决方案。我在 Ubuntu 机器上。 ~/graphmap2$ 在这个文件夹中,我下载了 zlib。可以看图 经过一番谷歌搜索后,我还注意到没
是否可以在 Visual C++ 中使用以下 header : 图.h dos.h bios.h 最佳答案 据我所知,无法在 Visual C++ 中使用它, 与此同时,我希望您关注 Open Wat
我是一名优秀的程序员,十分优秀!