Load_lua with unknown_classes=True

I loaded a torch7 model and I used unknown_classes=True (otherwise, I get issues with cudnn.SpatialConvolution).

m = load_lua('overfeatNet.t7', unknown_classes=True)
type(m)
torch.legacy.nn.Sequential.Sequential

The documentation (and @apaszke in this thread) says that using unknown_classes=True loads the object as a dict. But I don’t know how to use this dict. I know how to use the state_dict and load_state_dict with torchvision.models.model(pretrained=True).

One example I did for loading partial resnet18 model is as follows,

# ResNetBackBone is implementation of partial resnet18
backbone = ResNetBackBone(BasicBlock, [2, 2, 2, 2])

# load the state_dict from resnet18 to partial model 
backbone_dict = backbone.state_dict()
resnet18_dict = resnet18.state_dict()
resnet18_dict = {k: v for k, v in resnet18_dict.items() if k in backbone_dict}
backbone_dict.update(resnet18_dict)
backbone.load_state_dict(resnet18_dict)

The problem I facing in doing similar load_state_dict is, load_lua returns torch.legacy.nn.Sequential.Sequential that does not contain any state_dict member. Could someone please help me in loading models using load_lua ?

the models loaded via load_lua are to be used similarly to how you use them in lua-torch, i.e. model.forward(x); model.backward(x, grad) etc.

@smth Thanks for your reply :slight_smile:
But When I do model.forward(x) I am getting TypeError: 'NoneType' object is not callable.

The full error stack:

model.forward(t)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-c3b310dc48c1> in <module>()
----> 1 model.forward(t)

/opt/python3.5/lib/python3.5/site-packages/torch/legacy/nn/Module.py in forward(self, input)
     31 
     32     def forward(self, input):
---> 33         return self.updateOutput(input)
     34 
     35     def backward(self, input, gradOutput, scale=1):

/opt/python3.5/lib/python3.5/site-packages/torch/legacy/nn/Sequential.py in updateOutput(self, input)
     34         currentOutput = input
     35         for i, module in enumerate(self.modules):
---> 36             currentOutput = module.updateOutput(currentOutput)
     37         self.output = currentOutput
     38         return self.output

TypeError: 'NoneType' object is not callable

Here are steps I do while saving the model in Lua Torch:

require 'nn'
require 'cunn'
networkd = torch.load('~/models/mlp0_ds4_gpu.t7')
networkd = networkd:cuda()

# forward once to create all the model variables 
x = torch.FloatTensor():resize(1,3,96,96):cuda()
networkd:forward(x)

# [cudnn.convert script](https://github.com/soumith/cudnn.torch/blob/master/convert.lua) 
net = cudnn.convert(networkd, cudnn)
net = net:float()
torch.save('/tmp/overfeatNet.t7', net)

Then loading in PyTorch:

import torch as th
from torch.utils.serialization import load_lua
model = load_lua('overfeatNet.t7', unknown_classes=True)
t = th.randn(3,480, 640).cuda()
model = model.cuda()
d = model.forward(t)

Could you please help me in understanding what mistake I am doing?
For now, I am doing the forward pass in Lua, saving the resulting feature maps to disk, and loading them in PyTorch. It will be nice to get this error fixed. Thanks :slight_smile:

follow this link: https://github.com/clcarwin/convert_torch_to_pytorch to convert your torch model in pytorch platform

I know this is a late reply but did you find an answer for this behaviour?
Kind regards.