Loading Torch7 trained models (.t7) in PyTorch

16,858

Solution 1

As of PyTorch 1.0 torch.utils.serialization is completely removed. Hence no one can import models from Lua Torch into PyTorch anymore. Instead, I would suggest installing PyTorch 0.4.1 through pip in a conda environment (so that you can remove it after this) and use this repo to convert your Lua Torch model to PyTorch model, not just the torch.nn.legacy model that you cannot use for training. Then use PyTorch 1.xx to do whatever with it. You can also train your converted Lua Torch models in PyTorch this way :)

Solution 2

The correct function is load_lua:

from torch.utils.serialization import load_lua

x = load_lua('x.t7')
Share:
16,858
Arul
Author by

Arul

Updated on June 20, 2022

Comments

  • Arul
    Arul almost 2 years

    I am using Torch7 library for implementing neural networks. Mostly, I rely on pre-trained models. In Lua I use torch.load function to load a model saved as torch .t7 file. I am curious about switching to PyTorch( http://pytorch.org) and I read the documents. I couldn't find any information regarding the mechanisms to load a pre-trained model. The only relevant information I was able to find is this page:http://pytorch.org/docs/torch.html

    But the function torch.load described in the page seems to load a file saved with pickle. If someone has additional information on loading .t7 models in PyTorch, please share it here.

  • RockTheStar
    RockTheStar almost 7 years
    so after I get x, how can I use it?
  • Arul
    Arul almost 7 years
    @RockTheStar: You can use x.forward() just like other PyTorch models.
  • oarfish
    oarfish over 5 years
    It should be noted that load_lua does not exist in PyTorch. Not sure if they removed it.
  • Amir
    Amir about 5 years
    @oarfish torch.utils.serialization is completely removed in PyTorch 1.0 onward.. Look at my answer above for an alternative solution.
  • Roy Shilkrot
    Roy Shilkrot over 4 years
    Note: convert_torch_to_pytorch doesn't support LeakyReLU.