본문 바로가기

머신러닝공부

PyTorch_Tutorial_Transforms 파이토치 공식사이트

728x90
반응형

Transform(번형)

Data does not always come in its final processed form that is required for training machine learning algorithms.

We must transfroms to perform some manipulation of the data and make it suiable for training.

 

All TorchVision datasets have two parameters-transform to modify the features and target_transform to modify the labels -that accept callables containing the transformation logic. 

The torchvision.transforms module offers several commonly-used transforms out of the box.

 

The FashionMNIST features are in PIL image format, and the labels are integers. For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors.

To make ehese transformations, we use ToTensor and Lambda.

import torch
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda

ds = datasets.FashionMNIST(
	root="data",
    train=True,
    download=True,
    transform=ToTensor(),
    target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/raw/train-images-idx3-ubyte.gz

  0%|          | 0/26421880 [00:00<?, ?it/s]
  0%|          | 32768/26421880 [00:00<01:27, 301987.31it/s]
  0%|          | 65536/26421880 [00:00<01:27, 300088.33it/s]
  0%|          | 131072/26421880 [00:00<01:00, 436175.65it/s]
  1%|          | 229376/26421880 [00:00<00:42, 618929.58it/s]
  2%|1         | 491520/26421880 [00:00<00:20, 1258979.83it/s]
  4%|3         | 950272/26421880 [00:00<00:11, 2256387.11it/s]
  7%|7         | 1933312/26421880 [00:00<00:05, 4452306.22it/s]
 15%|#4        | 3833856/26421880 [00:00<00:02, 8561459.07it/s]
 26%|##6       | 6946816/26421880 [00:00<00:01, 14743744.88it/s]
 37%|###7      | 9863168/26421880 [00:01<00:00, 18349633.52it/s]
 49%|####9     | 13008896/26421880 [00:01<00:00, 21475631.91it/s]
 61%|######1   | 16154624/26421880 [00:01<00:00, 23628587.30it/s]
 73%|#######2  | 19267584/26421880 [00:01<00:00, 25086630.77it/s]
 84%|########3 | 22151168/26421880 [00:01<00:00, 25329203.87it/s]
 96%|#########5| 25264128/26421880 [00:01<00:00, 26270172.13it/s]
100%|##########| 26421880/26421880 [00:01<00:00, 16005255.44it/s]
Extracting data/FashionMNIST/raw/train-images-idx3-ubyte.gz to data/FashionMNIST/raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw/train-labels-idx1-ubyte.gz

  0%|          | 0/29515 [00:00<?, ?it/s]
100%|##########| 29515/29515 [00:00<00:00, 266667.13it/s]
100%|##########| 29515/29515 [00:00<00:00, 265405.93it/s]
Extracting data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz

  0%|          | 0/4422102 [00:00<?, ?it/s]
  1%|          | 32768/4422102 [00:00<00:14, 296583.04it/s]
  1%|1         | 65536/4422102 [00:00<00:14, 295323.46it/s]
  3%|2         | 131072/4422102 [00:00<00:09, 429591.20it/s]
  5%|5         | 229376/4422102 [00:00<00:06, 609026.89it/s]
 11%|#1        | 491520/4422102 [00:00<00:03, 1237949.90it/s]
 21%|##1       | 950272/4422102 [00:00<00:01, 2220238.80it/s]
 44%|####3     | 1933312/4422102 [00:00<00:00, 4382792.40it/s]
 87%|########6 | 3833856/4422102 [00:00<00:00, 8433191.21it/s]
100%|##########| 4422102/4422102 [00:00<00:00, 4956273.72it/s]
Extracting data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz

  0%|          | 0/5148 [00:00<?, ?it/s]
100%|##########| 5148/5148 [00:00<00:00, 22680963.23it/s]
Extracting data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw

ToTehsor()

ToTensor converts a PIL image or Numpy ndarray into a FloatTensor.

and scales the image's pixel intensity values in the range[0., 1.]

Lambda Transforms

Lambda transforms apply any user-defined lambda function.

Here, we define a function to turn the integer into a one-hot encoded tensor.

It first creates a zero tensor of size 10 and calls scatter_ which assigns a value=1 on the index as given by the label y.

target_transform = Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y),value=1))
반응형