将 torch t7 模型转换为 keras h5

Convert a torch t7 model to keras h5

我们如何将 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

如本文所述link, 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,)