问题一定出在您定义 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
然后从 docker 容器中提取生成的 numpy 文件 keras_to_torch.pt 和 keras_to_torch.py (以及 imagenet_resnet50.h5 用于比较)。
在 Python 中加载 keras 模型
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)
我还必须在keras_to_torch.py 文件开头的load_weights(weight_file) 函数中设置allow_pickle = True。不幸的是,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 和 torch 模型的权重根据需要重合(最多 4 位左右)。
只要您不想在将 keras 中的 VGG 和 ResNet 权重转换为 Pytorch 之前更新它们,此解决方案就可以工作。
如果您确实需要在转换之前更新模型权重,您应该分享您的代码以创建 Keras 模型。您可以进一步检查使用mmdownload model 获得的imagenet_resnet50.h5 与您在keras 中使用model.save 保存的imagenet_resnet50.h5 有何不同,并纠正任何差异。