Utils.draw_bounding_boxes() outputs black image

Sameeruddin_Muhammad: <p>Hi all,</p>
<p>I have just started tinkering with the PyTorch framework.<br>
I am getting an error while using utils library to draw the bounding boxes.</p>
<p>Here is the code:</p>
<pre><code class="lang-auto">#draw the bounding boxes around the objects detected in the image
try:
    input_value = img_tensor.cpu() #copying image transformed tensor data from GPU
    plt.imshow(utils.make_grid(input_value, nrow=1).permute(1,2,0)) #display the input image
    plt.show()
    boxes_tensor = torch.Tensor(pred_boxes) #converting boxes into a tensor
    preprocess_img = transforms.Compose([transforms.ToTensor()])(cpy_img) #transforming image to a tensor from the same input file
    out_img_tensor = preprocess_img.squeeze(0)
    out_img_type = out_img_tensor.type(torch.uint8) #converting the flaot tensor into uint8 as required by utils
    final_output = utils.draw_bounding_boxes(out_img_type, boxes_tensor, pred_classes) #draw the bounding boxes
    plt.imshow(utils.make_grid(final_output, nrow=1).permute(1,2,0)) #display the ouput result
    plt.show()
except Exception as er4:
    print(er4)
</code></pre>
<p>Please find the below images for reference:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/3/634a9049b63a00bb5a097f3cb58e5627ab61b748.png" alt="output_img" data-base62-sha1="ean1kh6AKMaHsiUqcjHRE7FWpDi" width="443" height="444"></p>
<p>Thank you</p>

Sameeruddin_Muhammad: <p>The input image was:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/1/41fe64ae6bb9bb5a4028873b4853cfe1cf67a3b6.png" alt="input_img" data-base62-sha1="9pO4nHr2mCYjGp82aeJkm58H8eG" width="450" height="413"></p>

ptrblck: <p>I guess the transformation to <code>uint8</code> could create an all-zero image as described in the comments:</p>
<pre><code class="lang-python"># ToTensor returns a tensor in the range [0, 1]
preprocess_img = transforms.Compose([transforms.ToTensor()])(cpy_img) 
# add dimension
out_img_tensor = preprocess_img.squeeze(0)
# transform float32 [0, 1] to uint8 [0, 255], which will create the black image
out_img_type = out_img_tensor.type(torch.uint8)
</code></pre>
<p>so you could scale the pixel values to e.g. <code>[0, 255]</code> before transforming them.</p>

Sameeruddin_Muhammad: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a> , I have tried couple of approaches after your comments to tranform the pixel value but, I haven’t landed in a correct solution. However, I have tried few approaches such as converting the tensor into a numpy array → multiplying the array by 255 and again converting back to tensor. still, I haven’t found a good the output. If you can point me towards a good article/tutorial that would be helpful. Thanks</p>

ptrblck: <p>Multiplying by <code>255</code> works for me, so I’m unsure which approaches you’ve used:</p>
<pre><code class="lang-python"># create random image
cpy_img = np.random.randint(0, 255, (100, 100, 3)).astype(np.uint8)
cpy_img = PIL.Image.fromarray(cpy_img)

# ToTensor returns a tensor in the range [0, 1]
preprocess_img = transforms.Compose([transforms.ToTensor()])(cpy_img) 
# add dimension
out_img_tensor = preprocess_img.squeeze(0)
# transform float32 [0, 1] to uint8 [0, 255], which will create the black image
out_img_type = out_img_tensor.type(torch.uint8)
print((out_img_type==0).sum()) # all zeros
plt.imshow(out_img_type.permute(1, 2, 0).numpy()) # all black

print(out_img_tensor.min(), out_img_tensor.max())
&gt; tensor(0.) tensor(0.9961)

out_img_tensor = out_img_tensor * 255
out_img_type = out_img_tensor.type(torch.uint8)
plt.imshow(out_img_type.permute(1, 2, 0).numpy()) # works
</code></pre>
<p>I can’t point you to a specific article, as the error is caused by transforming floating point data in the range <code>[0, 1]</code> to <code>uint8</code>, which will create an (almost) all-black image.</p>

Sameeruddin_Muhammad: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>I got the desired output.</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/8/18defa0d2091b3283b9f5cf0fcd4c7d5ce2ed9dd.png" alt="output" data-base62-sha1="3y1bsns6B90Ynt7S8XLK0ouzjfD" width="468" height="437"></p>