【问题标题】:Convert a torch t7 model to keras h5将torch t7模型转换为keras h5
【发布时间】:2019-08-04 10:45:46
【问题描述】:

我们如何将 t7 模型转换为 keras 的 h5 ? 我正在尝试为 c3d-sports1m-kinetics.t7 这样做,您可以在这里找到 https://github.com/kenshohara/3D-ResNets/releases

我至少可以要求一种将 t7 模型加载到 python (pytorch) 然后提取其权重的方法,但我无法使用 load_lua() 函数来做到这一点。 .

我在尝试使用此函数https://github.com/pytorch/pytorch/blob/c6529f4851bb8ac95f05d3f17dea178a0367aaee/torch/utils/serialization/read_lua_file.py时遇到错误

我得到的错误如下:

Traceback (most recent call last):
File "convert_t7_to_hdf5.py", line 574, in <module>
    a = load_lua("model.t7")
  File "convert_t7_to_hdf5.py", line 571, in load_lua
    return reader.read()
  File "convert_t7_to_hdf5.py", line 542, in read
    typeidx = self.read_int()
  File "convert_t7_to_hdf5.py", line 440, in read_int
    return self._read('i')
  File "convert_t7_to_hdf5.py", line 431, in _read
    result = struct.unpack(fmt, self.f.read(sz))
ValueError: read of closed file

【问题讨论】:

    标签: lua keras pytorch torch


    【解决方案1】:

    如本链接所述, https://github.com/pytorch/pytorch/issues/15307#issuecomment-448086741

    torchfile包,加载成功。您可以将 model 的内容转储到文件中,然后了解其中的内容。每层信息都存储为字典。了解模型架构会更容易解析内容。

    >>> import torchfile
    >>> model = torchfile.load('c3d-sports1m-kinetics.t7')
    >>> module = model.modules[0].modules[0]
    >>> module.name
    b'conv1a'
    >>> module['weight'].shape
    (64, 3, 3, 3, 3)
    >>> module['bias'].shape
    (64,)
    

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 1970-01-01
      • 2018-07-13
      • 2020-02-21
      • 2020-09-10
      • 2021-12-09
      • 2021-02-18
      • 2019-08-29
      • 2020-11-17
      相关资源
      最近更新 更多