Repository: pytorch
Issue: IndexError: Input _features.0.weight is undefined!
Labels: caffe2,triaged,module: vision

## 🐛 Bug

This is a workaround of the [issue closed as expected behavior](https://github.com/pytorch/pytorch/issues/49260), but there are a similar error without pickling.

## To Reproduce

Code example for alexnet torch pretrained model:

```python
import caffe2
import numpy as np
import onnx
from caffe2.python.onnx.backend import run_model
import pretrainedmodels

model_name = "alexnet" # for alexnet
model_args = {} # for alexnet
torch_model_zoo_path = "/home/my/pretrained_new_18ww38"
os.environ['TORCH_HOME'] = os.path.join(torch_model_zoo_path, model_name)
onnx_dump_path = '/home/my/onnx_dump/pytorch_pretrained_alexnet.onnx'

net = getattr(pretrainedmodels.models, model_name)(**model_args ) 
dummy_input = torch.autograd.Variable(torch.randn([1, ] + list(net.input_size)), requires_grad=False)
torch.onnx.export(net, dummy_input, onnx_dump_path, export_params=True)

model = onnx.load(onnx_dump_path)
data = np.load("/home/my/alexnet.npz") 
res = run_model(model, data)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-15-db7d2210f29d> in <module>
----> 1 res = run_model(model, data)

~/.local/lib/python3.6/site-packages/onnx/backend/base.py in run_model(cls, model, inputs, device, **kwargs)
     82                   **kwargs  # type: Any
     83                   ):  # type: (...) -> Tuple[Any, ...]
---> 84         backend = cls.prepare(model, device, **kwargs)
     85         assert backend is not None
     86         return backend.run(inputs)

~/.local/lib/python3.6/site-packages/caffe2/python/onnx/backend.py in prepare(cls, model, device, raw_values_dict, **kwargs)
    711         device_option = get_device_option(Device(device))
    712 
--> 713         init_net, predict_net = cls._onnx_model_to_caffe2_net(model, device, opset_version, False)
    714 
    715         if raw_values_dict:

~/.local/lib/python3.6/site-packages/caffe2/python/onnx/backend.py in _onnx_model_to_caffe2_net(cls, onnx_model, device, opset_version, include_initializers)
    874         device_option = get_device_option(Device(device))
    875 
--> 876         onnx_model = onnx.utils.polish_model(onnx_model)
    877         init_model = cls.optimize_onnx(onnx_model, init=True)
    878         pred_model = cls.optimize_onnx(onnx_model, predict=True)

~/.local/lib/python3.6/site-packages/onnx/utils.py in polish_model(model)
     22     onnx.helper.strip_doc_string(model)
     23     model = onnx.shape_inference.infer_shapes(model)
---> 24     model = onnx.optimizer.optimize(model)
     25     onnx.checker.check_model(model)
     26     return model

~/.local/lib/python3.6/site-packages/onnx/optimizer.py in optimize(model, passes, fixed_point)
     53         optimized_model_str = C.optimize_fixedpoint(model_str, passes)
     54     else:
---> 55         optimized_model_str = C.optimize(model_str, passes)
     56 
     57     return onnx.load_from_string(optimized_model_str)

IndexError: Input _features.0.weight is undefined!

```

## Environment
```
Package              Version
-------------------- ------------
absl-py              0.10.0
accuracy-checker     0.8.0
addict               2.2.1
ansi2html            1.5.2
apipkg               1.5
astor                0.8.1
atomicwrites         1.4.0
attrs                19.1.0
certifi              2020.6.20
chardet              3.0.4
colorama             0.4.4
cycler               0.10.0
Cython               0.29.21
dataclasses          0.8
decorator            4.4.2
defusedxml           0.6.0
distro               1.4.0
editdistance         0.5.3
execnet              1.7.1
flaky                3.5.3
future               0.17.1
gast                 0.2.2
google-pasta         0.2.0
graphviz             0.8.4
grpcio               1.33.1
h5py                 2.10.0
idna                 2.10
imageio              2.9.0
importlib-metadata   2.0.0
iniconfig            1.1.1
joblib               0.17.0
kaldiio              2.13.9
Keras-Applications   1.0.8
Keras-Preprocessing  1.1.2
kiwisolver           1.2.0
Markdown             3.3.2
matplotlib           3.3.2
more-itertools       8.5.0
munch                2.5.0
mxnet                1.5.1
networkx             2.5
nibabel              3.2.0
numpy                1.18.5
onnx                 1.4.1
onnxruntime          1.5.2
opencv-python        3.4.3.18
opt-einsum           3.3.0
packaging            20.4
Pillow               6.2.0
pip                  20.2.4
pkg-resources        0.0.0
pluggy               0.13.1
pretrainedmodels     0.7.4
progress             1.5
protobuf             3.13.0
psutil               5.5.1
py                   1.9.0
py-cpuinfo           4.0.0
pydicom              2.1.1
pymongo              3.7.2
pyparsing            2.4.7
pypi-kenlm           0.1.20190403
pyte                 0.8.0
pytest               6.0.2
pytest-forked        1.3.0
pytest-html          1.19.0
pytest-metadata      1.7.0
pytest-timeout       1.3.2
pytest-xdist         2.1.0
python-dateutil      2.8.1
PyWavelets           1.1.1
PyYAML               5.1
requests             2.23.0
scikit-image         0.15.0
scikit-learn         0.23.2
scipy                1.5.3
sentencepiece        0.1.94
setuptools           50.3.2
Shapely              1.7.1
six                  1.15.0
tabulate             0.8.2
tensorboard          1.15.0
tensorflow           1.15.2
tensorflow-estimator 1.15.1
termcolor            1.1.0
test-generator       0.1.1
thefuck              3.30
threadpoolctl        2.1.0
tokenizers           0.9.4
toml                 0.10.2
torch                1.6.0
torchvision          0.7.0
tqdm                 4.31.1
typing               3.7.4.3
typing-extensions    3.7.4.3
urllib3              1.25.11
wcwidth              0.2.5
Werkzeug             1.0.1
wheel                0.35.1
wrapt                1.12.1
zipp                 3.3.1
```
 - PyTorch 1.6.0:
 - Ubuntu 18.04.5 LTS:
 - Python 3.6:

## Additional context

<!-- Add any other context about the problem here. -->


cc @houseroad @spandantiwari @lara-hdr @BowenBao @neginraoof @fmassa @vfdev-5