Issue with torchvision.utils.draw_bouding_boxes

4158ndfkvBHJ1: <p>Hi everybody,<br>
I am following the torchvision documentation for this function (uint8 data type for the image input, (N,4)-sized boxes tensor) and I think the following minimal example should work:</p>
<pre><code class="lang-python">from torchvision.utils import draw_bounding_boxes
import torch 

image = torch.ones(1,1000,1000).type(torch.uint8)
boxes = torch.Tensor([[250,250,500,500]])
print("image.dtype: {}".format(image.dtype))
print("boxes.size(): {}".format(boxes.size()))
print("type(boxes): {}".format(type(boxes))) 
bboxes = draw_bounding_boxes(image, boxes)
</code></pre>
<p>but instead I get the following stacktrace:</p>
<pre><code class="lang-auto">image.dtype: torch.uint8
boxes.size(): torch.Size([1, 4])
type(boxes): &lt;class 'torch.Tensor'&gt;
Traceback (most recent call last):
  File "pytorch/1.8.1-py3.7/lib/python3.7/site-packages/PIL/Image.py", line 2772, in fromarray
    mode, rawmode = _fromarray_typemap[typekey]
KeyError: ((1, 1, 1), '|u1')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "minimal_example.py", line 9, in &lt;module&gt;
    bboxes = draw_bounding_boxes(image, boxes)
  File "pytorch/1.8.1-py3.7/lib/python3.7/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "pytorch/1.8.1-py3.7/lib/python3.7/site-packages/torchvision/utils.py", line 179, in draw_bounding_boxes
    img_to_draw = Image.fromarray(ndarr)
  File "pytorch/1.8.1-py3.7/lib/python3.7/site-packages/PIL/Image.py", line 2774, in fromarray
    raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1, 1), |u1
</code></pre>
<p>This issue discusses my problem, but as far as I can see the boxes tensor is actually of type torch.Tensor: <a href="https://discuss.pytorch.org/t/typeerror-cannot-handle-this-data-type-1-1-1-u1-when-using-torchvision-utils-draw-bounding-boxes/117823/2" class="inline-onebox">`TypeError: Cannot handle this data type: (1, 1, 1), |u1` when using `torchvision.utils.draw_bounding_boxes` - #2 by ptrblck</a><br>
I am using torchvision 0.9.1 if that changes anything, but the documentation is identical to the most up-to-date version.<br>
Any help would be greatly appreciated.</p>
<p>EDIT:</p>
<p>As far as I know, <code>PIL.fromarray</code> expects arrays valued between 0 and 1, for example <code>torchvision.utils.save_image</code> will spit out an error for images not valued in this range, but <code>torchvision.utils.draw_bounding_boxes</code> expect 8 bit images, so there seem to be a bit of an issue there ?</p>

ptrblck: <p>The code works fine for me and results in:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/b/1/b1229d4860f5ecba714ebd6bcd74cd947e107fe4.png" alt="image" data-base62-sha1="ph0FJY1xLRlMYZuyx6bkqOBSMdu" width="257" height="252"></p>
<p>Based on the error message it seems a numpy array is expected as one of the input arguments and I guess the error is raised in <a href="https://github.com/pytorch/vision/blob/d4a03fc02d0566ec97341046de58160370a35bd2/torchvision/utils.py#L251">this line of code</a>.<br>
Could you update <code>torchvision</code> and see if this would fix the issue as I currently don’t know what might be causing it?</p>

4158ndfkvBHJ1: <p>Sorry for the late reply, yes the latest torchvision fixed the issue.</p>