gpt4 book ai didi

keras - 将模型从 keras h5 转换为 pytorch - 全连接层不匹配

转载 作者:行者123 更新时间:2023-12-04 12:22:11 25 4
gpt4 key购买 nike

我已经使用 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 模型,你可以得到正确的权重,如下所示:

  • 按照 documentation 中的规定启动 MMdnn 容器
    下载 resnet50 的 keras 模型
  • mmdownload -f keras -n resnet50 -o ./
  • 转换为 pytorch 模型
  • mmconvert -sf keras -iw ./imagenet_resnet50.h5 -df pytorch -om keras_to_torch.pt
    然后提取生成的numpy文件, keras_to_torch.ptkeras_to_torch.py 来自 docker 容器(和 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)
    我还必须设置 allow_pickle = Trueload_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 位数左右)。
    只要您不想在将 keras 中的 VGG 和 ResNet 权重转换为 Pytorch 之前更新它们,此解决方案就有效。
    如果您确实需要在转换之前更新模型权重,您应该共享用于创建 Keras 模型的代码。您可以进一步检查 imagenet_resnet50.h5通过 mmdownload 获得模型与您在 keras 中使用 model.save 保存的模型不同,并纠正任何差异。

    关于keras - 将模型从 keras h5 转换为 pytorch - 全连接层不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68002742/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com