pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
82.76k stars 22.3k forks source link

Unable to import torchvision due to an error with the "op" function. #91773

Closed sidhant-tomar-003 closed 1 year ago

sidhant-tomar-003 commented 1 year ago

I'm new to this stuff so I can't be sure what it is, but I can't seem to import torchvision. Its asking me to "replace the existing function with new function." I did have an outdated version of torch which I downloaded on a terminal window,which I had to close and relaunch in admin to finish the install. Here's the traceback that told me to open up an issue here:

C:\Users\(username)\AppData\Roaming\Python\Python310\site-packages\torch\onnx\_internal\registration.py:167: 
OnnxExporterWarning: Symbolic function 'aten::_shape_as_tensor' already registered for opset 9. Replacing the existing function with new function. This is unexpected. Please report it on https://github.com/pytorch/pytorch/issues.
  warnings.warn(
C:\Users\(username)\AppData\Roaming\Python\Python310\site-packages\torch\onnx\_internal\registration.py:167: 
OnnxExporterWarning: Symbolic function 'aten::_reshape_from_tensor' already registered for opset 9. Replacing the existing function with new function. This is unexpected. Please report it on https://github.com/pytorch/pytorch/issues.
  warnings.warn(

the code I attempted to execute was:

import torch as th
import torchvision.transforms as T
import requests
from PIL import Image

And traceback points out that the error is from the second line (torchvision import).

P.S: I tried to run it again a while later and it no longer tells me to open an issue, just flashes an error about quantized args. Here's the new traceback (for the exact same code):

Input In [29], in <module>
      1 import torch as th
----> 2 import torchvision.transforms as T
      3 import requests
      4 from PIL import Image

File C:\Program Files\Python310\lib\site-packages\torchvision\__init__.py:5, in <module>
      2 import warnings
      4 import torch
----> 5 from torchvision import datasets, io, models, ops, transforms, utils
      7 from .extension import _HAS_OPS
      9 try:

File C:\Program Files\Python310\lib\site-packages\torchvision\models\__init__.py:4, in <module>
      2 from .convnext import *
      3 from .densenet import *
----> 4 from .efficientnet import *
      5 from .googlenet import *
      6 from .inception import *

File C:\Program Files\Python310\lib\site-packages\torchvision\models\efficientnet.py:10, in <module>
      8 import torch
      9 from torch import nn, Tensor
---> 10 from torchvision.ops import StochasticDepth
     12 from ..ops.misc import Conv2dNormActivation, SqueezeExcitation
     13 from ..transforms._presets import ImageClassification, InterpolationMode

File C:\Program Files\Python310\lib\site-packages\torchvision\ops\__init__.py:30, in <module>
     27 from .roi_pool import roi_pool, RoIPool
     28 from .stochastic_depth import stochastic_depth, StochasticDepth
---> 30 _register_custom_op()
     33 __all__ = [
     34     "masks_to_boxes",
     35     "deform_conv2d",
   (...)
     72     "DropBlock3d",
     73 ]

File C:\Program Files\Python310\lib\site-packages\torchvision\ops\_register_onnx_ops.py:10, in _register_custom_op()
      9 def _register_custom_op():
---> 10     from torch.onnx.symbolic_helper import parse_args
     11     from torch.onnx.symbolic_opset11 import select, squeeze, unsqueeze
     12     from torch.onnx.symbolic_opset9 import _cast_Long

File ~\AppData\Roaming\Python\Python310\site-packages\torch\onnx\symbolic_helper.py:17, in <module>
     14 from torch import _C
     16 # Monkey-patch graph manipulation methods on Graph, used for the ONNX symbolics
---> 17 from torch.onnx import (  # noqa: F401
     18     _constants,
     19     _deprecation,
     20     _patch_torch,
     21     _type_utils,
     22     errors,
     23 )
     24 from torch.onnx._globals import GLOBALS
     25 from torch.onnx._internal import _beartype, jit_utils

File ~\AppData\Roaming\Python\Python310\site-packages\torch\onnx\_patch_torch.py:11, in <module>
      8 from torch._C import _onnx as _C_onnx
     10 # Import utils to get _params_dict because it is a global that is accessed by c++ code
---> 11 from torch.onnx import _deprecation, utils
     12 from torch.onnx._globals import GLOBALS
     13 from torch.onnx._internal import _beartype

File ~\AppData\Roaming\Python\Python310\site-packages\torch\onnx\utils.py:39, in <module>
     37 import torch.serialization
     38 from torch import _C
---> 39 from torch.onnx import (  # noqa: F401
     40     _constants,
     41     _deprecation,
     42     _exporter_states,
     43     _patch_torch,
     44     errors,
     45     symbolic_caffe2,
     46     symbolic_helper,
     47 )
     48 from torch.onnx._globals import GLOBALS
     49 from torch.onnx._internal import _beartype, diagnostics, jit_utils, registration

File ~\AppData\Roaming\Python\Python310\site-packages\torch\onnx\symbolic_caffe2.py:4, in <module>
      1 import importlib
      2 import inspect
----> 4 from torch.onnx import symbolic_helper, symbolic_opset9 as opset9
      5 from torch.onnx._internal import jit_utils, registration
      8 def register_quantized_ops(domain: str, version: int):
      9     # Register all quantized ops

File ~\AppData\Roaming\Python\Python310\site-packages\torch\onnx\symbolic_opset9.py:333, in <module>
    328         shape = g.op("Concat", *shape, axis_i=0)
    329     return reshape(g, input, shape)
    332 @_onnx_symbolic("aten::reshape")
--> 333 @symbolic_helper.quantized_args(True)
    334 @_beartype.beartype
    335 def reshape(g: jit_utils.GraphContext, self, shape):
    336     return symbolic_helper._reshape_helper(g, self, shape)
    339 @_onnx_symbolic("aten::reshape_as")
    340 @symbolic_helper.quantized_args(True)
    341 @_beartype.beartype
    342 def reshape_as(g: jit_utils.GraphContext, self, other):
AttributeError: partially initialized module 'torch.onnx.symbolic_helper' has no attribute 'quantized_args' (most likely due to a circular import)

Versions

PyTorch version: 1.12.1+cpu Is debug build: False CUDA used to build PyTorch: Could not collect ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 Home Single Language GCC version: Could not collect Clang version: Could not collect CMake version: Could not collect Libc version: N/A

Python version: 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] (64-bit runtime) Python platform: Windows-10-10.0.19044-SP0 Is CUDA available: False CUDA runtime version: Could not collect CUDA_MODULE_LOADING set to: N/A GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3050 Laptop GPU Nvidia driver version: 527.56 cuDNN version: Could not collect HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

Versions of relevant libraries: [pip3] mypy-extensions 0.4.3 [pip3] numpy 1.22.2 [pip3] torch 1.13.1 [pip3] torchvision 0.14.1 [conda] Could not collect

mikaylagawarecki commented 1 year ago

It seems like you might have multiple pytorch installations, see the first line under version PyTorch version: 1.12.1+cpu and the line under Versions of relevant libraries: [pip3] torch 1.13.1. Could you try creating a conda environment and try installing torch 1.13.1 and torchvision 0.14.1 then rerunning the script?

sidhant-tomar-003 commented 1 year ago

It seems like you might have multiple pytorch installations, see the first line under version PyTorch version: 1.12.1+cpu and the line under Versions of relevant libraries: [pip3] torch 1.13.1. Could you try creating a conda environment and try installing torch 1.13.1 and torchvision 0.14.1 then rerunning the script?

Thanks! That was indeed the problem. I think my entire install of torch seemed a bit fudged, but I don't think that has any reason to be brought up as an issue here. Thanks again for the quick response!