Vision: RuntimeError: builtin cannot be used as a value

Created on 17 Dec 2019  ·  9Comments  ·  Source: pytorch/vision

On commit c7c2085ec686ccc55e1df85736b240b2405d1179 I'm receiving the following error whenever i call import torchvision.models as models:

RuntimeError: 
builtin cannot be used as a value:
at venv/lib/python3.6/site-packages/torchvision-0.5.0a0+c7c2085-py3.6-linux-x86_64.egg/torchvision/models/detection/_utils.py:14:56
def zeros_like(tensor, dtype):
    # type: (Tensor, int) -> Tensor
    return torch.zeros_like(tensor, dtype=dtype, layout=tensor.layout,
                                                        ~~~~~~~~~~~~~ <--- HERE
                            device=tensor.device, pin_memory=tensor.is_pinned())
'zeros_like' is being compiled since it was called from '__torch__.torchvision.models.detection._utils.BalancedPositiveNegativeSampler.__call__'
at venv/lib/python3.6/site-packages/torchvision-0.5.0a0+c7c2085-py3.6-linux-x86_64.egg/torchvision/models/detection/_utils.py:72:12

            # randomly select positive and negative examples
            perm1 = torch.randperm(positive.numel(), device=positive.device)[:num_pos]
            perm2 = torch.randperm(negative.numel(), device=negative.device)[:num_neg]

            pos_idx_per_image = positive[perm1]
            neg_idx_per_image = negative[perm2]

            # create binary mask from indices
            pos_idx_per_image_mask = zeros_like(
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...  <--- HERE
                matched_idxs_per_image, dtype=torch.uint8
            )
            neg_idx_per_image_mask = zeros_like(
                matched_idxs_per_image, dtype=torch.uint8
            )

            pos_idx_per_image_mask[pos_idx_per_image] = torch.tensor(1, dtype=torch.uint8)
            neg_idx_per_image_mask[neg_idx_per_image] = torch.tensor(1, dtype=torch.uint8)
models object detection

Most helpful comment

I just ran into this when using torch==1.4.0 and torchvision==0.5.0. Rolling back to torch==1.3.1 and `torchvision==0.4.2" seems to fix the issue for me.

python environment details:
Python 3.7.5
torchvision 0.5.0
torch 1.4.0

host details:
Virtualization: microsoft
Operating System: Ubuntu 18.04.3 LTS
Kernel: Linux 5.0.0-1031-azure
Architecture: x86-64

traceback:

  File "./py37/lib/python3.7/site-packages/torchvision/__init__.py", line 3, in <module>
    from torchvision import models
  File "./py37/lib/python3.7/site-packages/torchvision/models/__init__.py", line 12, in <module>
    from . import detection
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/__init__.py", line 1, in <module>
    from .faster_rcnn import *
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/faster_rcnn.py", line 13, in <module>
    from .rpn import AnchorGenerator, RPNHead, RegionProposalNetwork
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/rpn.py", line 11, in <module>
    from . import _utils as det_utils
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/_utils.py", line 19, in <module>
    class BalancedPositiveNegativeSampler(object):
  File "./py37/lib/python3.7/site-packages/torch/jit/__init__.py", line 1219, in script
    _compile_and_register_class(obj, _rcb, qualified_name)
  File "./py37/lib/python3.7/site-packages/torch/jit/__init__.py", line 1076, in _compile_and_register_class
    _jit_script_class_compile(qualified_name, ast, rcb)
  File "./py37/lib/python3.7/site-packages/torch/jit/_recursive.py", line 222, in try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "./py37/lib/python3.7/site-packages/torch/jit/__init__.py", line 1226, in script
    fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
RuntimeError:
builtin cannot be used as a value:
at ./py37/lib/python3.7/site-packages/torchvision/models/detection/_utils.py:14:56
def zeros_like(tensor, dtype):
    # type: (Tensor, int) -> Tensor
    return torch.zeros_like(tensor, dtype=dtype, layout=tensor.layout,
                                                        ~~~~~~~~~~~~~ <--- HERE
                            device=tensor.device, pin_memory=tensor.is_pinned())
'zeros_like' is being compiled since it was called from '__torch__.torchvision.models.detection._utils.BalancedPositiveNegativeSampler.__call__'
at ./py37/lib/python3.7/site-packages/torchvision/models/detection/_utils.py:72:12

            # randomly select positive and negative examples
            perm1 = torch.randperm(positive.numel(), device=positive.device)[:num_pos]
            perm2 = torch.randperm(negative.numel(), device=negative.device)[:num_neg]

            pos_idx_per_image = positive[perm1]
            neg_idx_per_image = negative[perm2]

            # create binary mask from indices
            pos_idx_per_image_mask = zeros_like(
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...  <--- HERE
                matched_idxs_per_image, dtype=torch.uint8
            )
            neg_idx_per_image_mask = zeros_like(
                matched_idxs_per_image, dtype=torch.uint8
            )

            pos_idx_per_image_mask[pos_idx_per_image] = torch.tensor(1, dtype=torch.uint8)
            neg_idx_per_image_mask[neg_idx_per_image] = torch.tensor(1, dtype=torch.uint8)

All 9 comments

Thanks for the bug report!

What's your PyTorch version?

We might need to make a backwards-compatible fix for version PyTorch 1.4 if this isn't supported there.

@eellison do know you if tensor.layout is supported in the 1.4 branch?

My PyTorch version = 1.3.1 (latest from pip)

Yeah, that's expected -- you'll need a more recent version of PyTorch (at least from September, containing https://github.com/pytorch/pytorch/commit/6a4ca9abec1c18184635881c08628737c8ed2497)

@eellison told me that this patch is going to be present in the 1.4 release, so the solution here is just to update PyTorch to a nightly, or wait for 1.4 to be released in early January.

Thanks for the heads up!

@fmassa Thanks! I'll switch to nigthly for now

I just ran into this when using torch==1.4.0 and torchvision==0.5.0. Rolling back to torch==1.3.1 and `torchvision==0.4.2" seems to fix the issue for me.

python environment details:
Python 3.7.5
torchvision 0.5.0
torch 1.4.0

host details:
Virtualization: microsoft
Operating System: Ubuntu 18.04.3 LTS
Kernel: Linux 5.0.0-1031-azure
Architecture: x86-64

traceback:

  File "./py37/lib/python3.7/site-packages/torchvision/__init__.py", line 3, in <module>
    from torchvision import models
  File "./py37/lib/python3.7/site-packages/torchvision/models/__init__.py", line 12, in <module>
    from . import detection
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/__init__.py", line 1, in <module>
    from .faster_rcnn import *
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/faster_rcnn.py", line 13, in <module>
    from .rpn import AnchorGenerator, RPNHead, RegionProposalNetwork
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/rpn.py", line 11, in <module>
    from . import _utils as det_utils
  File "./py37/lib/python3.7/site-packages/torchvision/models/detection/_utils.py", line 19, in <module>
    class BalancedPositiveNegativeSampler(object):
  File "./py37/lib/python3.7/site-packages/torch/jit/__init__.py", line 1219, in script
    _compile_and_register_class(obj, _rcb, qualified_name)
  File "./py37/lib/python3.7/site-packages/torch/jit/__init__.py", line 1076, in _compile_and_register_class
    _jit_script_class_compile(qualified_name, ast, rcb)
  File "./py37/lib/python3.7/site-packages/torch/jit/_recursive.py", line 222, in try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)
  File "./py37/lib/python3.7/site-packages/torch/jit/__init__.py", line 1226, in script
    fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
RuntimeError:
builtin cannot be used as a value:
at ./py37/lib/python3.7/site-packages/torchvision/models/detection/_utils.py:14:56
def zeros_like(tensor, dtype):
    # type: (Tensor, int) -> Tensor
    return torch.zeros_like(tensor, dtype=dtype, layout=tensor.layout,
                                                        ~~~~~~~~~~~~~ <--- HERE
                            device=tensor.device, pin_memory=tensor.is_pinned())
'zeros_like' is being compiled since it was called from '__torch__.torchvision.models.detection._utils.BalancedPositiveNegativeSampler.__call__'
at ./py37/lib/python3.7/site-packages/torchvision/models/detection/_utils.py:72:12

            # randomly select positive and negative examples
            perm1 = torch.randperm(positive.numel(), device=positive.device)[:num_pos]
            perm2 = torch.randperm(negative.numel(), device=negative.device)[:num_neg]

            pos_idx_per_image = positive[perm1]
            neg_idx_per_image = negative[perm2]

            # create binary mask from indices
            pos_idx_per_image_mask = zeros_like(
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~...  <--- HERE
                matched_idxs_per_image, dtype=torch.uint8
            )
            neg_idx_per_image_mask = zeros_like(
                matched_idxs_per_image, dtype=torch.uint8
            )

            pos_idx_per_image_mask[pos_idx_per_image] = torch.tensor(1, dtype=torch.uint8)
            neg_idx_per_image_mask[neg_idx_per_image] = torch.tensor(1, dtype=torch.uint8)

@martinlightmatter this is weird, my first guess would be that you have a conflict of libraries in your system and it's picking the wrong combination of PyTorch / torchvision.

Indeed, while looking at your stacktrace, it points to

File "./py37/lib/python3.7/site-packages/torch/jit/_recursive.py", line 222, in try_compile_fn
    return torch.jit.script(fn, _rcb=rcb)

which, in PyTorch version 1.4.0, doesn't correspond to the same function, see https://github.com/pytorch/pytorch/blob/7f73f1d591afba823daa4a99a939217fb54d7688/torch/jit/_recursive.py#L222

Can you double-check your environment?

It seems that torchvision-0.4 will match pytorch-1.4

@AlexXXEL torchvision 0.5 matches PyTorch 1.4, and they should come together otherwise it won't work properly

ok,thank
---原始邮件---
发件人:"Francisco Massa"notifications@github.com
发送时间:2020年3月11日 星期三 下午10:6
收件人:"pytorch/vision"vision@noreply.github.com
抄送:"AlexXXEL"2313294547@qq.com;"Mention"mention@noreply.github.com
主题:Re: [pytorch/vision] RuntimeError: builtin cannot be used as a value(#1675)

@AlexXXELtorchvision 0.5 matches PyTorch 1.4, and they should come together otherwise it won't work properly


You are receiving this because you were mentioned.
Reply to this email directly,view it on GitHub, orunsubscribe.

Was this page helpful?
0 / 5 - 0 ratings