What's Optional[Tensor]?

I got this error while calling tensor.size(0) in my project:

RuntimeError:
Arguments for call are not valid.
The following operator variants are available:

  aten::size.int(Tensor self, int dim) -> (int):
  Expected a value of type 'Tensor' for argument 'self' but instead found type 'Optional[Tensor]'.

  aten::size(Tensor self) -> (int[]):
  Expected a value of type 'Tensor' for argument 'self' but instead found type 'Optional[Tensor]'.

The original call is:
at ocr.py:78:23
        img_tensor = resize_img(img_tensor)
        # self.show(img_tensor)

        img_tensor_ = (img_tensor - 0.5) / 0.5
        img_tensor_ = img_tensor_.permute(2,0,1).unsqueeze(0) # --> n x c x h x w
        boxes = self.east(img_tensor_)
        # self.show(img_tensor,boxes)
        new_boxes = []
        pass_list:List[int] = []
        for i in range(boxes.size(0)):
                       ~~~~~~~~~~ <--- HERE

How can I get the first dimension size of a tensor in torchscript?

https://pytorch.org/docs/stable/jit.html#optional-type-refinement
here you can get information about Optional

1 Like