참고
Click here to download the full example code
파이토치(PyTorch) 기본 익히기 || 빠른 시작 || 텐서(Tensor) || Dataset과 Dataloader || 변형(Transform) || 신경망 모델 구성하기 || Autograd || 최적화(Optimization) || 모델 저장하고 불러오기
변형(Transform)¶
데이터가 항상 머신러닝 알고리즘 학습에 필요한 최종 처리가 된 형태로 제공되지는 않습니다. 변형(transform) 을 해서 데이터를 조작하고 학습에 적합하게 만듭니다.
모든 TorchVision 데이터셋들은 변형 로직을 갖는, 호출 가능한 객체(callable)를 받는 매개변수 두개
( 특징(feature)을 변경하기 위한 transform
과 정답(label)을 변경하기 위한 target_transform
)를 갖습니다
torchvision.transforms 모듈은
주로 사용하는 몇가지 변형(transform)을 제공합니다.
FashionMNIST 특징(feature)은 PIL Image 형식이며, 정답(label)은 정수(integer)입니다.
학습을 하려면 정규화(normalize)된 텐서 형태의 특징(feature)과 원-핫(one-hot)으로 부호화(encode)된 텐서 형태의
정답(label)이 필요합니다. 이러한 변형(transformation)을 하기 위해 ToTensor
와 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<07:34, 58044.68it/s]
0%| | 65536/26421880 [00:01<09:31, 46138.82it/s]
0%| | 98304/26421880 [00:02<10:12, 42995.58it/s]
0%| | 131072/26421880 [00:02<09:03, 48365.04it/s]
1%| | 163840/26421880 [00:03<07:06, 61503.77it/s]
1%| | 196608/26421880 [00:03<05:56, 73561.57it/s]
1%| | 229376/26421880 [00:03<05:11, 84153.43it/s]
1%| | 262144/26421880 [00:03<04:41, 92906.47it/s]
1%|1 | 294912/26421880 [00:04<04:20, 100227.29it/s]
1%|1 | 327680/26421880 [00:04<04:07, 105545.01it/s]
1%|1 | 393216/26421880 [00:04<03:02, 142672.82it/s]
2%|1 | 458752/26421880 [00:04<02:32, 169881.69it/s]
2%|1 | 524288/26421880 [00:05<02:16, 190299.76it/s]
2%|2 | 622592/26421880 [00:05<01:48, 238573.35it/s]
3%|2 | 720896/26421880 [00:05<01:33, 274001.59it/s]
3%|3 | 819200/26421880 [00:06<01:25, 300384.73it/s]
4%|3 | 950272/26421880 [00:06<01:12, 353297.78it/s]
4%|4 | 1114112/26421880 [00:06<00:59, 423690.07it/s]
5%|4 | 1277952/26421880 [00:06<00:52, 477512.20it/s]
6%|5 | 1474560/26421880 [00:07<00:45, 547085.42it/s]
6%|6 | 1671168/26421880 [00:07<00:41, 599277.66it/s]
7%|7 | 1900544/26421880 [00:07<00:36, 670565.09it/s]
8%|8 | 2162688/26421880 [00:07<00:32, 754450.29it/s]
9%|9 | 2457600/26421880 [00:08<00:28, 848964.63it/s]
10%|# | 2719744/26421880 [00:08<00:26, 881431.82it/s]
12%|#1 | 3047424/26421880 [00:08<00:24, 973332.05it/s]
13%|#2 | 3342336/26421880 [00:09<00:22, 1006038.56it/s]
14%|#3 | 3670016/26421880 [00:09<00:21, 1058648.84it/s]
15%|#5 | 3964928/26421880 [00:09<00:21, 1064037.12it/s]
16%|#6 | 4292608/26421880 [00:09<00:20, 1098566.37it/s]
17%|#7 | 4587520/26421880 [00:10<00:19, 1098768.27it/s]
19%|#8 | 4915200/26421880 [00:10<00:19, 1118292.13it/s]
20%|#9 | 5210112/26421880 [00:10<00:19, 1113138.01it/s]
21%|## | 5537792/26421880 [00:10<00:18, 1125569.91it/s]
22%|##2 | 5832704/26421880 [00:11<00:18, 1120530.74it/s]
23%|##3 | 6160384/26421880 [00:11<00:17, 1128660.48it/s]
24%|##4 | 6455296/26421880 [00:11<00:17, 1125050.66it/s]
26%|##5 | 6782976/26421880 [00:12<00:17, 1133929.16it/s]
27%|##6 | 7077888/26421880 [00:12<00:17, 1126604.17it/s]
28%|##8 | 7405568/26421880 [00:12<00:16, 1135079.85it/s]
29%|##9 | 7700480/26421880 [00:12<00:16, 1145011.17it/s]
30%|### | 8028160/26421880 [00:13<00:16, 1130579.16it/s]
32%|###1 | 8323072/26421880 [00:13<00:13, 1317408.78it/s]
32%|###2 | 8486912/26421880 [00:13<00:14, 1239605.51it/s]
33%|###2 | 8650752/26421880 [00:13<00:17, 1041855.10it/s]
34%|###3 | 8945664/26421880 [00:13<00:13, 1285343.10it/s]
34%|###4 | 9109504/26421880 [00:14<00:14, 1201965.62it/s]
35%|###5 | 9273344/26421880 [00:14<00:17, 1000949.23it/s]
36%|###6 | 9568256/26421880 [00:14<00:16, 1050570.31it/s]
37%|###7 | 9895936/26421880 [00:14<00:15, 1073773.61it/s]
39%|###8 | 10190848/26421880 [00:14<00:12, 1299412.19it/s]
39%|###9 | 10354688/26421880 [00:15<00:13, 1209101.81it/s]
40%|###9 | 10518528/26421880 [00:15<00:15, 1017415.66it/s]
41%|#### | 10813440/26421880 [00:15<00:12, 1285083.60it/s]
42%|####1 | 10977280/26421880 [00:15<00:13, 1179475.98it/s]
42%|####2 | 11141120/26421880 [00:15<00:15, 1013354.70it/s]
43%|####3 | 11468800/26421880 [00:16<00:14, 1055351.25it/s]
45%|####4 | 11763712/26421880 [00:16<00:11, 1314823.66it/s]
45%|####5 | 11927552/26421880 [00:16<00:12, 1203885.96it/s]
46%|####5 | 12091392/26421880 [00:16<00:13, 1038836.37it/s]
47%|####7 | 12419072/26421880 [00:17<00:12, 1082097.79it/s]
48%|####8 | 12746752/26421880 [00:17<00:12, 1135595.06it/s]
49%|####9 | 13074432/26421880 [00:17<00:09, 1366938.70it/s]
50%|##### | 13238272/26421880 [00:17<00:10, 1247363.46it/s]
51%|##### | 13434880/26421880 [00:17<00:11, 1084033.70it/s]
52%|#####2 | 13762560/26421880 [00:17<00:09, 1401229.94it/s]
53%|#####2 | 13926400/26421880 [00:18<00:09, 1257531.10it/s]
54%|#####3 | 14155776/26421880 [00:18<00:10, 1126276.15it/s]
55%|#####4 | 14483456/26421880 [00:18<00:08, 1452164.36it/s]
56%|#####5 | 14680064/26421880 [00:18<00:08, 1339579.60it/s]
56%|#####6 | 14876672/26421880 [00:18<00:10, 1150528.43it/s]
58%|#####7 | 15237120/26421880 [00:19<00:07, 1521572.20it/s]
58%|#####8 | 15433728/26421880 [00:19<00:07, 1380264.17it/s]
59%|#####9 | 15663104/26421880 [00:19<00:08, 1225617.82it/s]
61%|###### | 16056320/26421880 [00:19<00:06, 1627723.63it/s]
62%|######1 | 16252928/26421880 [00:19<00:06, 1454363.72it/s]
63%|######2 | 16515072/26421880 [00:20<00:07, 1315134.88it/s]
64%|######4 | 16941056/26421880 [00:20<00:05, 1762935.03it/s]
65%|######4 | 17170432/26421880 [00:20<00:05, 1601615.22it/s]
66%|######5 | 17399808/26421880 [00:20<00:06, 1342300.03it/s]
67%|######6 | 17694720/26421880 [00:20<00:05, 1580799.27it/s]
68%|######7 | 17891328/26421880 [00:20<00:06, 1407463.71it/s]
68%|######8 | 18055168/26421880 [00:21<00:07, 1094461.20it/s]
70%|######9 | 18415616/26421880 [00:21<00:06, 1260796.12it/s]
71%|#######1 | 18776064/26421880 [00:21<00:04, 1551238.67it/s]
72%|#######1 | 18972672/26421880 [00:21<00:05, 1395800.28it/s]
73%|#######2 | 19202048/26421880 [00:22<00:05, 1216219.06it/s]
74%|#######4 | 19595264/26421880 [00:22<00:05, 1359243.35it/s]
76%|#######5 | 19988480/26421880 [00:22<00:03, 1657436.99it/s]
76%|#######6 | 20185088/26421880 [00:22<00:04, 1473716.88it/s]
77%|#######7 | 20447232/26421880 [00:22<00:04, 1315972.04it/s]
79%|#######8 | 20840448/26421880 [00:22<00:03, 1738007.79it/s]
80%|#######9 | 21069824/26421880 [00:23<00:03, 1566261.94it/s]
81%|######## | 21299200/26421880 [00:23<00:03, 1407247.28it/s]
82%|########2 | 21725184/26421880 [00:23<00:02, 1778186.79it/s]
83%|########3 | 21954560/26421880 [00:23<00:02, 1587946.67it/s]
84%|########3 | 22183936/26421880 [00:23<00:02, 1625181.32it/s]
85%|########4 | 22380544/26421880 [00:23<00:02, 1553839.36it/s]
86%|########5 | 22642688/26421880 [00:24<00:02, 1450215.09it/s]
87%|########7 | 23068672/26421880 [00:24<00:01, 1870323.12it/s]
88%|########8 | 23298048/26421880 [00:24<00:01, 1598675.33it/s]
89%|########9 | 23560192/26421880 [00:24<00:01, 1485592.18it/s]
91%|######### | 24018944/26421880 [00:24<00:01, 1622460.45it/s]
92%|#########1| 24281088/26421880 [00:25<00:01, 1795660.97it/s]
93%|#########2| 24477696/26421880 [00:25<00:01, 1590326.03it/s]
94%|#########3| 24739840/26421880 [00:25<00:00, 1792431.87it/s]
95%|#########4| 24969216/26421880 [00:25<00:01, 1352376.62it/s]
96%|#########6| 25427968/26421880 [00:25<00:00, 1555516.03it/s]
98%|#########7| 25853952/26421880 [00:25<00:00, 2008439.83it/s]
99%|#########8| 26116096/26421880 [00:26<00:00, 1744165.21it/s]
100%|#########9| 26345472/26421880 [00:26<00:00, 1571310.75it/s]
100%|##########| 26421880/26421880 [00:26<00:00, 998740.16it/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, 110669.88it/s]
100%|##########| 29515/29515 [00:00<00:00, 110483.79it/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:35, 125332.24it/s]
1%|1 | 65536/4422102 [00:00<00:34, 125157.41it/s]
3%|2 | 131072/4422102 [00:00<00:23, 182173.73it/s]
5%|5 | 229376/4422102 [00:01<00:16, 258553.68it/s]
10%|# | 458752/4422102 [00:01<00:08, 481067.60it/s]
21%|## | 917504/4422102 [00:01<00:03, 912724.62it/s]
41%|####1 | 1835008/4422102 [00:01<00:01, 1759418.56it/s]
83%|########2 | 3670016/4422102 [00:02<00:00, 3429473.57it/s]
100%|##########| 4422102/4422102 [00:02<00:00, 2099933.33it/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, 29257827.90it/s]
Extracting data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw
ToTensor()¶
ToTensor
는 PIL Image나 NumPy ndarray
를 FloatTensor
로 변환하고, 이미지의 픽셀의 크기(intensity) 값을 [0., 1.] 범위로
비례하여 조정(scale)합니다.
Lambda 변형(Transform)¶
Lambda 변형은 사용자 정의 람다(lambda) 함수를 적용합니다. 여기에서는 정수를 원-핫으로 부호화된 텐서로 바꾸는
함수를 정의합니다.
이 함수는 먼저 (데이터셋 정답의 개수인) 크기 10짜리 영 텐서(zero tensor)를 만들고,
scatter_ 를 호출하여
주어진 정답 y
에 해당하는 인덱스에 value=1
을 할당합니다.
target_transform = Lambda(lambda y: torch.zeros(
10, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))
더 읽어보기¶
Total running time of the script: ( 0 minutes 34.008 seconds)