기록 - 2022/02/05
- torch.utils.data : 데이터셋의 표준을 정의하고 데이터셋을 불러오고 자르고 섞는데 쓰는 도구들이 들어있는 모듈, 파이토치 모델을 학습시키기 위한 데이터셋의 표준을 torch.utils.data.Dataset에 정의합니다. Dataset 모듈을 상속하는 파생 클래스는 학습에 필요한 데이터를 로딩해 주는 torch.utils.data.DataLoader ㅇ니스턴스의 입력으로 사용할 수 있습니다.
- torchvision.datasets : torch.utils.data.Dataset을 상속하는 이미지 데이터셋의 모음이다. fashio mnist가 여기 있다
- torchvision.transfomers : 이미지 데이터셋에 쓸 수 있는 여러가지 변환 필터를 담고 있는 모듈로, 예를들면 텐서로 변환한다든지, 크기조절, 크롭으로 이미지를 수정할 수도 있고 밝기, 대비 등을 조절하는데 사용 될 수 있음
- torchvision.utils : 이미지 데이터를 저장하고 시각화 하기 위한 도구가 들어있는 모듈
from torchvision import datasets, transforms, utils
from torch.utils import data
In [69]:
import matplotlib.pyplot as plt
import numpy as np
In [70]:
#Compose안에 transfomer 기능 나열해주면 순서대로 변환이 이루어진다( Totensor, resize, normalize etc..)
transform = transforms.Compose([
transforms.ToTensor()
])
In [71]:
#data load
#torchvision.datasets로 생선된 객체는 파이토치 내부 클래서 torh.utils.data.Dataset을 상속하므로 파이토치의 DataLoader 데이터셋을 로딩하는클래스에 바로 넣어서 사용가능하다
#DataLoader는 데이터셋을 배치라는 단위로 쪼개서 학습시 반복문 안에서 데이터를 공급해주는 클래스 이다.
trainset = datasets.FashionMNIST(
root = './.data',
train = True,
download = True,
transform = transform)
testset = datasets.FashionMNIST(
root = './.data',
train = False,
download = True,
transform = transform)
In [72]:
batch_size = 16
train_loader = data.DataLoader(
dataset = trainset,
batch_size = batch_size)
test_loader = data.DataLoader(
dataset = testset,
batch_size = batch_size)
In [73]:
#1개 배치를 뽑아서 데이터가 어떤지 확인하기
dataiter = iter(train_loader)
images, labels = next(dataiter)
In [74]:
#utils.make_grid : 여러이미지를 모아 하나의 이미지로 만들수 있다.
img = utils.make_grid(images, padding=0)
npimg = img.numpy() #img는 파이토치 텐서라서 넘파이 행렬로 변경해주기
plt.figure(figsize=(10,7))
plt.imshow(np.transpose(npimg,(1,2,0)))
plt.show()

In [75]:
print(labels)
tensor([9, 0, 0, 3, 0, 2, 7, 2, 5, 5, 0, 9, 5, 5, 7, 9])
In [76]:
CLASSES = {
0 : 'top',
1 : 'trouser',
2 : 'pullover',
3 : 'dress',
4 : 'coat',
5 : 'sandal',
6 : 'shirts',
7 : 'sneaker',
8 : 'bag',
9 : 'ankle'
}
In [77]:
for label in labels:
index = label.item()
print(label,index,CLASSES[index])
tensor(9) 9 ankle
tensor(0) 0 top
tensor(0) 0 top
tensor(3) 3 dress
tensor(0) 0 top
tensor(2) 2 pullover
tensor(7) 7 sneaker
tensor(2) 2 pullover
tensor(5) 5 sandal
tensor(5) 5 sandal
tensor(0) 0 top
tensor(9) 9 ankle
tensor(5) 5 sandal
tensor(5) 5 sandal
tensor(7) 7 sneaker
tensor(9) 9 ankle
<인공신경망으로 분류 모델 만들기>
In [78]:
import torch
import torch.nn as nn
import torch.optim as optim #최적화를 위한 모듈
import torch.nn.functional as F #nn모듈의 함수버전
from torchvision import transforms, datasets
In [79]:
#torch.cuda.is_available() 현재 컴퓨터에서 CUDA를 사용 할 수 있는지 알아보는 함수
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
In [80]:
EPOCHS = 30
BATCH_SIZE = 64
In [81]:
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
#nn.Linear : 선형결합을 수행하는 객체를 만든다
#self.fc1 : 픽셀값 784(28*28*1 - 이미지 가로 * 세로 * 색상값(fashion mnist는 흑백이라 1)를 입력받아 가중치를 행렬곱하고 편향을 더해
#256개를 출력한다
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 10)#라벨수 만큼 출력 총 라벨 = 10 개
def forward(self, x):
x = x.view(-1, 784)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
#모델 선언, to()함수로 연산을 어디서 수행할지 정할 수 있다. to()함수는 모델의 파라미터들을 지정한 장치의 메모리로 보내는 역할을 한다.
#GPU를 사용하라면 to("cuda")로 지정하여 GPU메모리로 보내야 한다
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr = 0.01)
#model.parameters() : 모델 내부 정보 넘겨줌
#학습 연산
def train(model, train_loader, optimizer) :
model.train() #훈련 모드 변경
for batch_idx, (data, target) in enumerate(train_loader):
#data의 shape는 [배치크기, 색, 높이, 넓이]로 이루어져 있다.
#위에서 모델의 가중치를 GPU로 보냈다면 학습데이터도 같은 장치로 보내야 연산 수행이 가능하기 때문에 to 함수 사용
data, target = data.to(device), target.to(device)
#반복때마다 기울기를 새로 계산해야해서 optimizer.zero_grad() 호출
optimizer.zero_grad()
#모델 예측값 받아 오고
output = model(data)
#예측값과 훈련값 사이에 Loss값 구하고
loss = F.cross_entropy(output, target)
loss.backward() #기울기 계산
optimizer.step() #계산한 기울기를 앞서 정의한 알고리즘에 맞춰 가중치를 수정 함
#성능평가 epoch한번 끝날때 마다 모델 성능 측정 해보기 단순평가를 위한 코드 s는
def evaluate(model, test_loader):
model.eval() #평가 모드로 변경
#테스트 오차와 예측이 맞은 수를 담은 변수를 0으로 초기화 시키기
test_loss = 0
correct = 0
#평가 과정에서는 기울기를 계산하지 않아도 되므로 torch.no_grad() 명시
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device) #같은 GPU or CPU로 보내주기
output = model(data) #모델 예측값 받아오기
#오차값을 받아올때 reduction='sum'을 지정해주어 미니배치의 평균대신 합을 받아와야 합니다. 테스트셋을 다 돌고 나면 test_loss는 모든 테스트셋의 오차의 합이 될 것
test_loss += F.cross_entropy(output, target, reduction='sum').item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = 100. * correct / len(test_loader.dataset)
return test_loss, test_accuracy
for epoch in range(1, EPOCHS+1):
train(model, train_loader, optimizer)
test_loss, test_accuracy = evaluate(model, test_loader)
print('[{}] test loss : {:.4f}, accuracy: {:.2f}%'.format(epoch, test_loss, test_accuracy))
[1] test loss : 0.5658, accuracy: 80.11%
[2] test loss : 0.4928, accuracy: 82.33%
[3] test loss : 0.4541, accuracy: 83.82%
[4] test loss : 0.4285, accuracy: 84.71%
[5] test loss : 0.4062, accuracy: 85.47%
[6] test loss : 0.3920, accuracy: 86.01%
[7] test loss : 0.3813, accuracy: 86.39%
[8] test loss : 0.3736, accuracy: 86.64%
[9] test loss : 0.3659, accuracy: 86.91%
[10] test loss : 0.3604, accuracy: 87.03%
[11] test loss : 0.3539, accuracy: 87.40%
[12] test loss : 0.3492, accuracy: 87.55%
[13] test loss : 0.3452, accuracy: 87.76%
[14] test loss : 0.3408, accuracy: 88.12%
[15] test loss : 0.3373, accuracy: 88.17%
[16] test loss : 0.3356, accuracy: 88.27%
[17] test loss : 0.3331, accuracy: 88.31%
[18] test loss : 0.3324, accuracy: 88.31%
[19] test loss : 0.3306, accuracy: 88.51%
[20] test loss : 0.3302, accuracy: 88.53%
[21] test loss : 0.3291, accuracy: 88.62%
[22] test loss : 0.3295, accuracy: 88.51%
[23] test loss : 0.3299, accuracy: 88.61%
[24] test loss : 0.3296, accuracy: 88.49%
[25] test loss : 0.3291, accuracy: 88.82%
[26] test loss : 0.3298, accuracy: 88.77%
[27] test loss : 0.3326, accuracy: 88.71%
[28] test loss : 0.3333, accuracy: 88.68%
[29] test loss : 0.3365, accuracy: 88.65%
[30] test loss : 0.3402, accuracy: 88.47%
reference - 3분 딥러닝 파이토치맛