Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I encounted with the same problem "AttributeError: 'NoneType' object has no attribute 'gradInput'" #24

Open
Sunshine352 opened this issue Dec 20, 2017 · 5 comments

Comments

@Sunshine352
Copy link

Before someone came up with this problem, your suggestion is that you can debug to locate where the attribute 'gradInput' is, Thus? next what we should do? comment or what? May you in detail explain how to revise the code? Thank you very much!

@gkyustc
Copy link

gkyustc commented Dec 28, 2017

I come up with the same problem, do you solve this ?

@kenchon
Copy link

kenchon commented Jan 25, 2018

Hi, I had same problem which have been solved.
Before you run the code, please check your model by:

from torch.utils.serialization import load_lua
model = load_lua("path_to_your_model.t7")
print(model)

In my case, I got following output:

{'mean': [0.5657177752729754, 0.5381838567195789, 0.4972228365504561],
 'std': [0.29023818639817184, 0.2874722565279285, 0.2933830104791508],
 'stylenet': nn.Sequential {
   [input -> (0) -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> (18) -> (19) -> (20) -> (21) -> (22) -> (23) -> (24) -> output]
   (0): nn.SpatialConvolution(3 -> 64, 3x3, 1, 1, 1, 1)
   (1): nn.ReLU
   (2): nn.SpatialConvolution(64 -> 64, 3x3, 1, 1, 1, 1)
   (3): nn.ReLU
   (4): nn.Dropout(0.2500)
   (5): nn.SpatialMaxPooling(4x4, 4, 4)
   (6): nn.SpatialBatchNormalization
   (7): nn.SpatialConvolution(64 -> 128, 3x3, 1, 1, 1, 1)
   (8): nn.ReLU
   (9): nn.SpatialConvolution(128 -> 128, 3x3, 1, 1, 1, 1)
   (10): nn.ReLU
   (11): nn.Dropout(0.2500)
   (12): nn.SpatialMaxPooling(4x4, 4, 4)
   (13): nn.SpatialBatchNormalization
   (14): nn.SpatialConvolution(128 -> 256, 3x3, 1, 1, 1, 1)
   (15): nn.ReLU
   (16): nn.SpatialConvolution(256 -> 256, 3x3, 1, 1, 1, 1)
   (17): nn.ReLU
   (18): nn.Dropout(0.2500)
   (19): nn.SpatialMaxPooling(4x4, 4, 4)
   (20): nn.SpatialBatchNormalization
   (21): nn.SpatialConvolution(256 -> 128, 1x1)
   (22): nn.ReLU
   (23): nn.Reshape(3072)
   (24): nn.Linear(3072 -> 128)
 }}

This seems complicated but it's just a dict type object.
According to the output, my model(nn.Sequential) is named as 'stylenet', so you can access model (nn.Sequential) by

model = model['stylenet']

Then, the problem is occuring in torch_to_pytorch() block in convert_torch.py:

def torch_to_pytorch(t7_filename,outputname=None):
    model = load_lua(t7_filename,unknown_classes=True)
    if type(model).__name__=='hashable_uniq_dict':
        model=model.model 
        model.gradInput = None
    slist = lua_recursive_source(lnn.Sequential().add(model))
    s = simplify_source(slist)
    header = '''

you can see the line model = model.model which don't make sense.
So you can modify this line like

model = model['stylenet']

Finally,

def torch_to_pytorch(t7_filename,outputname=None):
    model = load_lua(t7_filename,unknown_classes=True)
    if type(model).__name__=='hashable_uniq_dict':
        model = model['stylenet'] 
        model.gradInput = None
    slist = lua_recursive_source(lnn.Sequential().add(model))
    s = simplify_source(slist)
    header = '''

worked.

@Jamesswiz
Copy link

Jamesswiz commented Jun 12, 2018

Hi @kenmikanmi
I am also getting a similar error: 'list' object has no attribute 'gradInput'
I have a saved model which dosen't have a name:
print(model)
[nn.Sequential {
[input -> (0) -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> output]
(0): nn.TemporalConvolution
(1): nn.TemporalMaxPooling
(2): nn.Threshold
(3): nn.TemporalConvolution
(4): nn.TemporalMaxPooling
(5): nn.Reshape(6960)
(6): nn.Threshold
(7): nn.Linear(6960 -> 100)
(8): nn.Threshold
(9): nn.Linear(100 -> 462)
(10): nn.LogSoftMax
}

As you pointed out, the error seems to be in command: model = model.model
I tried setting " model=model[0] " but got the same error.

Can you help on this?

Best

@kenchon
Copy link

kenchon commented Jun 12, 2018

@Jamesswiz
Do you get same error ('list' object has no attribute 'gradInput') after you modified model = model.model as model=model[0] ?
Could you show me all output of print(model) ?

@Jamesswiz
Copy link

Jamesswiz commented Jun 12, 2018

@kenmikanmi

Yes I get the same error.

another thing which i tried was to replace the whole 'if' statement itself with just model=model[0].
The code ran (not sure correctly) but then it could not convert the model since, the layers I am using e.g., Temporal convolution, maxpooling are not defined.

but I still don't have exact idea so I may be wrong.

Here is my model.
print(model)
[nn.Sequential {
[input -> (0) -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> output]
(0): nn.TemporalConvolution
(1): nn.TemporalMaxPooling
(2): nn.Threshold
(3): nn.TemporalConvolution
(4): nn.TemporalMaxPooling
(5): nn.Reshape(6960)
(6): nn.Threshold
(7): nn.Linear(6960 -> 100)
(8): nn.Threshold
(9): nn.Linear(100 -> 462)
(10): nn.LogSoftMax
}, {'kW2': 10, 'mp2': 3, 'arch': 'cnn10', 'nhu4': 0 ..............}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants