📌  相关文章
📜  transform=transforms.Compose([ transforms.ToTensor(), c((0.1307,), (0.3081,)) ]) (1)

📅  最后修改于: 2023-12-03 15:20:40.594000             🧑  作者: Mango

Introduction to transforms.Compose() in PyTorch

The transforms.Compose() function in PyTorch allows programmers to combine multiple image transformations into a single transformation pipeline. It is commonly used when preparing datasets for training or testing deep learning models.

The transforms.Compose() function takes a list of individual image transformations as input and returns a new transformation object that applies these transformations sequentially to the input data. Each transformation in the list is applied one after the other in the order they are specified.

import torchvision.transforms as transforms

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])

This example demonstrates the usage of transforms.Compose() function with two transformations: transforms.ToTensor() and transforms.Normalize(). Let's understand these transformations in detail:

  1. transforms.ToTensor():

    • This transformation converts the input image into a PyTorch tensor.
    • It converts a PIL Image or NumPy array with a shape (H, W, C) in the range [0, 255] to a float tensor of shape (C, H, W) in the range [0.0, 1.0].
    • For instance, an RGB image of size 32x32 will be converted into a tensor of size 3x32x32.
  2. transforms.Normalize(mean, std):

    • This transformation normalizes an input tensor with mean and standard deviation values.
    • The provided mean and std tuples are used to normalize each channel of the input tensor.
    • It performs the following channel-wise operation: input[channel] = (input[channel] - mean[channel]) / std[channel].
    • In the given example, the mean is (0.1307,) and std is (0.3081,).

By using transforms.Compose(), the ToTensor and Normalize transformations are applied sequentially. First, the image is converted to a tensor, and then the tensor is normalized using the specified mean and standard deviation values.

This transformation pipeline is typically used in PyTorch when preprocessing image data for training or testing deep learning models. It ensures that the input data is in the correct format and range for optimal model performance.

Markdown format is as follows:

## Introduction to `transforms.Compose()` in PyTorch

The `transforms.Compose()` function in PyTorch allows programmers to combine multiple image transformations into a single transformation pipeline. It is commonly used when preparing datasets for training or testing deep learning models.

The `transforms.Compose()` function takes a list of individual image transformations as input and returns a new transformation object that applies these transformations sequentially to the input data. Each transformation in the list is applied one after the other in the order they are specified.

```python
import torchvision.transforms as transforms

transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,), (0.3081,))
])

This example demonstrates the usage of transforms.Compose() function with two transformations: transforms.ToTensor() and transforms.Normalize(). Let's understand these transformations in detail:

  1. transforms.ToTensor():

    • This transformation converts the input image into a PyTorch tensor.
    • It converts a PIL Image or NumPy array with a shape (H, W, C) in the range [0, 255] to a float tensor of shape (C, H, W) in the range [0.0, 1.0].
    • For instance, an RGB image of size 32x32 will be converted into a tensor of size 3x32x32.
  2. transforms.Normalize(mean, std):

    • This transformation normalizes an input tensor with mean and standard deviation values.
    • The provided mean and std tuples are used to normalize each channel of the input tensor.
    • It performs the following channel-wise operation: input[channel] = (input[channel] - mean[channel]) / std[channel].
    • In the given example, the mean is (0.1307,) and std is (0.3081,).

By using transforms.Compose(), the ToTensor and Normalize transformations are applied sequentially. First, the image is converted to a tensor, and then the tensor is normalized using the specified mean and standard deviation values.

This transformation pipeline is typically used in PyTorch when preprocessing image data for training or testing deep learning models. It ensures that the input data is in the correct format and range for optimal model performance.