Search code examples
luakeraspytorchtorch

Convert a torch t7 model to keras h5


How can we convert a t7 model to keras' h5 ? I am trying to do so for c3d-sports1m-kinetics.t7 that you can find here https://github.com/kenshohara/3D-ResNets/releases

The least I can ask for is a way to load the t7 model to python (pytorch) and then extract its weights, but I couldn't do it with the load_lua() function...

I get an error while trying to do it using this function https://github.com/pytorch/pytorch/blob/c6529f4851bb8ac95f05d3f17dea178a0367aaee/torch/utils/serialization/read_lua_file.py

The error that I get is the following :

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

Solution

  • As mentioned in this link, https://github.com/pytorch/pytorch/issues/15307#issuecomment-448086741

    with torchfile package, the load was successful. You can dump the contents of model to a file and then understand the contents. Each layer information is stored as a dictionary. Knowing the model architecture would make it easier to parse the contents.

    >>> 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,)