- Deep Learning with PyTorch
- Vishnu Subramanian
- 328字
- 2021-06-24 19:16:29
Loading data into PyTorch tensors
The PyTorch torchvision.datasets package provides a utility class called ImageFolder that can be used to load images along with their associated labels when data is presented in the aforementioned format. It is a common practice to perform the following preprocessing steps:
- Resize all the images to the same size. Most of the deep learning architectures expect the images to be of the same size.
- Normalize the dataset with the mean and standard deviation of the dataset.
- Convert the image dataset to a PyTorch tensor.
PyTorch makes a lot of these preprocessing steps easier by providing a lot of utility functions in the transforms module. For our example, let's apply three transformations:
- Scale to a 256 x 256 image size
- Convert to a PyTorch tensor
- Normalize the data (we will talk about how we arrived at the mean and standard deviation in Chapter 5, Deep Learning for Computer Vision)
The following code demonstrates how transformation can be applied and images are loaded using the ImageFolder class:
simple_transform=transforms.Compose([transforms.Scale((224,224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
train = ImageFolder('dogsandcats/train/',simple_transform)
valid = ImageFolder('dogsandcats/valid/',simple_transform)
The train object holds all the images and associated labels for the dataset. It contains two important attributes: one that gives a mapping between classes and the associated index used in the dataset and another one that gives a list of classes:
- train.class_to_idx - {'cat': 0, 'dog': 1}
- train.classes - ['cat', 'dog']
It is often a best practice to visualize the data loaded into tensors. To visualize the tensors, we have to reshape the tensors and denormalize the values. The following function does that for us:
def imshow(inp):
"""Imshow for Tensor."""
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp)
Now, we can pass our tensor to the preceding imshow function, which converts it into an image:
imshow(train[50][0])
The preceding code generates the following output:

- Aftershot Pro:Non-destructive photo editing and management
- Istio入門(mén)與實(shí)戰(zhàn)
- 深入淺出SSD:固態(tài)存儲(chǔ)核心技術(shù)、原理與實(shí)戰(zhàn)
- INSTANT Wijmo Widgets How-to
- Artificial Intelligence Business:How you can profit from AI
- Learning Game Physics with Bullet Physics and OpenGL
- 基于Proteus仿真的51單片機(jī)應(yīng)用
- Blender Game Engine:Beginner's Guide
- 單片機(jī)原理及應(yīng)用:基于C51+Proteus仿真
- Python Machine Learning Blueprints
- Spring Cloud實(shí)戰(zhàn)
- Mastering Quantum Computing with IBM QX
- 筆記本電腦維修技能實(shí)訓(xùn)
- 創(chuàng)客電子:Arduino和Raspberry Pi智能制作項(xiàng)目精選
- 詳解FPGA:人工智能時(shí)代的驅(qū)動(dòng)引擎