Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add onnx support for InstanceNorm#4626

Merged
ezyang merged 2 commits into
pytorch:masterfrom
houseroad:instancenorm
Feb 7, 2018
Merged

Add onnx support for InstanceNorm#4626
ezyang merged 2 commits into
pytorch:masterfrom
houseroad:instancenorm

Conversation

@houseroad
Copy link
Copy Markdown
Member

Now, we export the InstanceNorm as an InstanceNorm op, not Reshape + BatchNorm + Reshape

@houseroad
Copy link
Copy Markdown
Member Author

Address #4584, it works end 2 end so far. But some unused tensors got exported, too. Will fix it soon.

@houseroad
Copy link
Copy Markdown
Member Author

@ezyang, the unused stuff got removed, ready for review. :-)

@onnxbot retest this please

@houseroad
Copy link
Copy Markdown
Member Author

Please ignore the failed onnx-fb-universe CI, I already updated the expected files in onnxbot/onnx-fb-universe#268

Comment thread torch/nn/functional.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/csrc/jit/export.cpp Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/csrc/jit/export.cpp Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

This comment was marked as off-topic.

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Jan 16, 2018

CC @linkerzhang

Copy link
Copy Markdown
Collaborator

@dzhulgakov dzhulgakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, when it lands I can also upstream some changes from https://github.com/dzhulgakov/fast-neural-style to pytorch/examples (onnx exporter)

Comment thread torch/csrc/jit/export.cpp Outdated

This comment was marked as off-topic.

Comment thread torch/onnx/symbolic.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/nn/functional.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/onnx/__init__.py Outdated

This comment was marked as off-topic.

This comment was marked as off-topic.

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Jan 19, 2018

I'm on the hook for finishing this.

Copy link
Copy Markdown
Collaborator

@dzhulgakov dzhulgakov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For mapping the tensor inputs - I wish we could find a better solution.

Comment thread torch/onnx/symbolic.py Outdated

This comment was marked as off-topic.

@houseroad houseroad force-pushed the instancenorm branch 3 times, most recently from 6f2776d to 60abfe9 Compare February 6, 2018 19:42
Comment thread torch/csrc/jit/export.cpp Outdated

This comment was marked as off-topic.

Comment thread torch/csrc/jit/export.cpp Outdated

This comment was marked as off-topic.

@ezyang ezyang merged commit c111cdf into pytorch:master Feb 7, 2018
@CynthiaLu1119
Copy link
Copy Markdown

After reading this post, I tried to install pytorch by source and the onnx does work on instanceNorm2D now. Thanks.

However, I encountered some other problems. I trained some pytorch model using the pytorch version installed through http://pytorch.org/. I was able to save the trained model as .pth file and load it back in successfully by doing the following:

model = myNet(parameters...) model.load_state_dict(torch.load('xxx.pth'))

However, after I install the latest pytorch from source. The load_state_dict is reporting errors:

File "/anaconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 528, in load_state_dict
.format(name))
KeyError: 'unexpected key "pre_model.1.running_mean" in state_dict'

I did the following to check which layer was causing the problem
model1 = myNet(parameters...)
model2 = torch.load('xxx.pth')

for name, param in model2.items():
print(name)

for name, param in model1.state_dict().items():
print(name)

I found that for the instanceNorm2D layers, model2 contains xxx.running_mean and xxx.running_var, but not model1. Any idea what I should do?

@CynthiaLu1119
Copy link
Copy Markdown

Another question:
If I use the instanceNorm2D layer with affine=True, nn.InstanceNorm2d(ngf,affine=True), then I get the following problem when doing torch.onnx.export

torch.onnx.export(model, (dummy_input, dummy_input), MODEL_DIR + "eye_G.proto", verbose=True)
File "/anaconda2/lib/python2.7/site-packages/torch/onnx/init.py", line 83, in export
_export(model, args, f, export_params, verbose, training, input_names, output_names)
File "/anaconda2/lib/python2.7/site-packages/torch/onnx/init.py", line 138, in _export
_optimize_trace(trace, aten)
File "/anaconda2/lib/python2.7/site-packages/torch/onnx/init.py", line 94, in _optimize_trace
torch._C._jit_pass_onnx(trace, aten)
File "/anaconda2/lib/python2.7/site-packages/torch/onnx/init.py", line 180, in _run_symbolic_method
return symbolic_fn(*args)
File "/anaconda2/lib/python2.7/site-packages/torch/onnx/init.py", line 396, in symbolic
symbolic_output = symbolic_fn(g, *symbolic_args, **kwargs)
File "/anaconda2/lib/python2.7/site-packages/torch/onnx/symbolic.py", line 552, in instance_norm
if not weight:
RuntimeError: bool value of Tensor with more than one value is ambiguous

If I do nn.InstanceNorm2d(ngf,affine=False) or nn.InstanceNorm2d(ngf), then the export will succeed. Please help. Thanks.

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
* Add ONNX symbolic for instancenorm

* Fix some bugs
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

Successfully merging this pull request may close these issues.

4 participants