Unable to inline display the captured image when using PIL library in jupyterLab.

Asked 1 years ago, Updated 1 years ago, 126 views

The following problems occurred while proceeding with the "Introduction to PyTorch Practice" published by Mynavi Publishing.
As the recommended environment in this document is Jupiter, we have pulled the following from DockerHub:

%docker run --rm-d-v$PWD:/app-p8888:8888\pman0214/pytorch_jupyterlab: 1.9.0

run Command Quotation Site

Pytorch+jupyterlab docker image

for M1 mac people

Follow the instructions and do the following:

from torchvision import models
alexnet=models.alexnet()
resnet=models.resnet101 (pretrained=True)

from torchvision import transforms
preprocess=transforms.Compose([
    transforms.Resize(256)
    , transforms.CenterCrop (224)
    , transforms.ToTensor()
    , transforms.Normalize(
        mean = [0.485, 0.456, 0,406]
        , std = [0.229,0,224,0.225]
    )])
from PIL import Image

img=Image.open("data/bobby.jpeg")
img

If you call img, the image of the dog stored in the folder will be displayed.
The desired behavior is inline

<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1280x720 at 0xFFFF65190E80>

I would like it to be displayed as .(The inline display above is printed as "print(img)").
Because if you do the following,

img_t=preprocess(img)

This is because there is no string and it is returned as an error.

------------------------------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-35-5823a1581b62>in<module>
---->1img_t=preprocess(img)

/usr/local/lib/python 3.9/site-packages/torchvision/transforms/transforms.py in __call__(self, img)
     58 def__call__(self, img):
     59 for tinself.transforms:
--- > 60 img = t (img)
     61 return img
     62 

/usr/local/lib/python 3.9/site-packages/torch/nn/modules/module.py in_call_impl (self, *input, **kwargs)
   1049 if not(self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or_global_backward_hooks
   1050 or_global_forward_hooks or_global_forward_pre_hooks):
->1051 return forward_call(*input,**kwargs)
   1052# Do not call functions when jit is used
   1053 full_backward_hooks, non_full_backward_hooks=[],[]

/usr/local/lib/python 3.9/site-packages/torchvision/transforms/transforms.py in forward (self, tensor)
    219 Tensor: Normalized Tensor image.
    220         """
-->221 return F.normalize (tensor, self.mean, self.std, self.inplace)
    222 
    223 def__repr__(self):

/usr/local/lib/python 3.9/site-packages/torchvision/transforms/functional.py in normalize (tensor, mean, std, place)
    328 std=torch.as_tensor(std,dtype=dtype,device=tensor.device)
    329 if(std==0).any():
-->330 raise ValueError('stdevalidated to zero after conversion to {}, leading to division by zero.'.format(dtype))
    331 if mean.ndim == 1:
    332 mean=mean.view(-1,1,1)

ValueError: stde validated to zero after conversion to torch.float32, leading to division by zero.

I looked at various sites, but as with reference books,

img

is inline with

img.show()

displays the image.
Therefore, I could not find an example like this behavior.
I don't know if it's the setting on the jupyterLab side or if the code is wrong in the first place.
Please lend me your wisdom.

By the way, nothing special happened when I ran img.show().
Also, we have verified similar behavior when we run it on Google Collaboration.
Thank you for your cooperation.

python3 docker pytorch jupyter-lab pillow

2022-09-29 21:50

1 Answers

Now that we have solved the problem, let me tell you the whole story.

Problems

The decimal point and comma of the array to be replaced by mean,std below were mixed up.

mean=[0.485, 0.456, 0,406]
, std = [0.229,0,224,0.225]

The correct description is as follows.It was an elementary mistake.

mean=[0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225]

Also, I asked you a question that inline display is not possible, but the in the repository of this document behaves the same way (we did not investigate further as the problem was resolved).

Dear Kunif, Thank you for your cooperation.


2022-09-29 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.