Participate in the 9th day of The November Gwen Challenge, see the details of the event: 2021 last Gwen Challenge

Take a look attorchvision.transforms.ToTensorWhat has been done:

Translate a pil. Image or numpy.ndarray with shape (H,W,C) into a torch.FloadTensor with shape [C,H,W]

  • So basically you have to translate FROM Ndarray to tensor.

  • Np.ndarray is [H, W, C] format: the outermost layer of the array is hight, indicating that the image pixel has several lines; The second layer element width represents several columns of image pixels, and the last layer element is the value of each channel.

    The Tensor is [C, H, W] : the first element in the array is the image has a channel, the second element is a row of pixels on a channel, and the third element is the pixels on a column on that channel.

    Here’s an example:

    •  import numpy as np
       from torchvision import transforms
       
       data = np.random.randint(0.255, size=6)
       img = data.reshape(2.1.3)
       print(img)
       img_tensor = transforms.ToTensor()(img) Translate into tensor
       print(img_tensor)
      Copy the code

      Result: [[[17, 172, 103]] [[127, 183, 174]]] tensor ([[[17], [127]], [[172], [183]], [[103], [174]]], dtype = torch. Int32)

        • The outermost layer of NdarRay is two rows of pixels per row. The middle layer is the pixel value of each column, a total of two columns; The innermost layer is the three-channel that is RGB value.
        • The tensor has a couple of channels at the outer layer, three channels means RGB; In the second layer, there are several pixel values for each column; The third layer is several pixels per row.

UserWarning

To clarify how this works, consider a user alert I encountered today.

D: \ \ Program Files \ python \ python3.8 \ lib \ site – packages \ torchvision \ datasets \ mnist py: 498: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at .. \torch\csrc\utils\tensor_numpy.cpp:180.) return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)

The general meaning is:

D: \ \ Program Files \ \ \ \ python python3.8 \ \ lib \ \ site packages \ \ torchvision \ \ dataset \ \ mnist py: 498: UserWarning: said:

The given NumPy array is unwritable, and PyTorch does not support unwritable tensors. This means that you can use tensors to write to underlying (presumably unwritable) NumPy arrays. Before converting an array to a tensor, you may need to copy the array to protect its data or make it writable.

Such warnings will be suppressed for the rest of this program. (in.. \torch\ CSC \utils\tensor\u numpy. CPP :180 internal triggers.)

Return torch. From_numpy (parsed. Astype (m[2], copy=False)).view(*s)

You know, people are saying that this stuff can handle numpy arrays, you don’t have to translate it into a tensor. But why don’t we let him display the warning.

Find the mnist file at that address above. Find line 498 and put itreturn torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)Copy false to true.