In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to display tensor type images with batch in pytorch". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "pytorch with batch tensor type image how to display" it!
The most commonly used library for displaying images and drawings is matplotlib:pip install matplotlib
The matplotlib.pyplot.imshow method is used to display the image. According to the official documents, the number of channels of the image received by this method should be put at the back.
The dimension of the data in the data loader is [B, C, H, W]. We only take out one data at a time, which is [C, H, W], while the input dimension required by matplotlib.pyplot.imshow is [H, W, C], so we need to exchange the data dimensions and put the number of channels at the end. Here we use the permute method in pytorch (transpose method is fine, but it is not convenient to exchange it twice. The transpose method in numpy can be exchanged at once)
Examples of usage are as follows: > x = torch.randn (2,3,5) > > x.size () torch.Size ([2,3,5]) > x.permute (1,2,0). Size () torch.Size ([3,5,2]) Code example #% Import module import torchimport matplotlib.pyplot as pltfrom torchvision.utils import make_gridfrom torch.utils.data import DataLoaderfrom torchvision import datasets, transforms#%% download dataset train_file = datasets.MNIST (root='./dataset/' Train=True, transform=transforms.Compose ([transforms.ToTensor (), transforms.Normalize ((0.1307,), (0.3081,)), download=True) #% make data loader train_loader = DataLoader (dataset=train_file, batch_size=9, shuffle=True) #% training data Visualization images, labels = next (iter (train_loader)) print (images.size ()) # torch.Size ([9,1,28) 28]) plt.figure (figsize= (9,9)) for i in range (9): plt.subplot (3,3, iTun1) plt.title (labels [I] .item () plt.imshow (images [I] .permute (1,2,0), cmap='gray') plt.axis ('off') plt.show ()
Here, take the mnist dataset as an example to demonstrate the display effect. There is actually a little problem with my code. When the data is enhanced, I standardize it, or in line 7: Normalize ((0.1307,), (0.3081,)).
So, if you want to view the original image of the training set, you have to de-standardize it.
Standardization: image = (image-mean) / std
Anti-standardization: image = image*std+mean
I did an experiment with a subset of ants and bees in imagenet, and the difference before and after standardization is still obvious.
Add: PIL,plt displays images of tensor type
This method is aimed at displaying the image read by Dataloader.
The corresponding operation in PIL is different from that in plt, but the principle is the same. I tried to use the following code Image to show on plt, and I don't know why.
# method 1:Image.show () # transforms.ToPILImage () has a sentence # npimg = np.transpose (pic.numpy (), (1,2,0)) # so pic can only be 3D Tensor, so use image [0] to eliminate the one-dimensional batch img = transforms.ToPILImage (image [0]) img.show () # method 2:plt.imshow (ndarray) img = image [0] # plt.imshow () can only accept 3D Tensor So it is also necessary to use image [0] to eliminate the one-dimensional batch img = img.numpy () # FloatTensor to ndarray img = np.transpose (img, (1jing2jin0)) # put the channel dimension to the last # display picture plt.imshow (img) plt.show () cnt + = 1 to this point, I believe you have a deeper understanding of "pytorch with batch tensor type image display", might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.