import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1,
out_channels=6,
kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6,
out_channels=16,
kernel_size=5)
# y = Wx + b
self.fc1 = nn.Linear(in_features = 16 * 4 * 4, out_features=120) # bias = True 가 기본
self.fc2 = nn.Linear(in_features=120, out_features=84)
self.fc3 = nn.Linear(in_features=84, out_features=10)
def forward(self, x):
# x.shape : b, 1, 28,28 --> b, 6, 24, 24
# H(input 높이) , K(kernel size) : K(conv) --> H - K + 1
x = self.conv1(x)
x = F.relu(x)
# x.shape: b, 6, 24, 24 --> b, 6, 12,12
x = F.max_pool2d(x, (2,2))
# x.shape: b, 6, 12, 12 --> b, 16, 8, 8
x = self.conv2(x)
x = F.relu(x)
# x.shape: b, 16, 8, 8 --> b, 16, 4, 4
x = F.max_pool2d(x, (2,2))
# x.shape: b, 16, 4, 4 - > b, 256 (-1 이 쫙피게 연산해줌)
x = x.view(x.size(0), -1)
# x.shape: b , 256 --> b, 120
x = self.fc1(x)
x = F.relu(x)
# x.shape: b, 120 --> b, 84
x = self.fc2(x)
x = F.relu(x)
# x.shape: b,84 --> b, 10
x = self.fc3(x)
return x
net = Net()
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
#print(device)
net = net.to(device=device)
print(net)
batch_size = 128
# transform(x)
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,),(0.5,))] # mean, variance
)
trainset = datasets.MNIST(root='./data', train=True, download=True,
transform=transform)
trainloader = DataLoader(trainset, batch_size=batch_size,
shuffle=True,
num_workers=2)
testset = datasets.MNIST(root='./data', train=False, download=True,
transform=transform)
testloader = DataLoader(testset, batch_size=batch_size,
shuffle=False,
num_workers=2)
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9)
criterion = nn.CrossEntropyLoss()
def train(epoch):
net.train()
running_loss = 0.0
for i,(inputs, labels) in enumerate(trainloader):
inputs, labels = inputs.to(device), labels.to(device) # loss.__class__: Tensor.
#zero the parameter gradients
optimizer.zero_grad()
#forward + backward + optimize
#net.foward(inputs)
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i%50 == 49:
print('[%d,%d]loss: %.3f'%(epoch+1, i+1, running_loss/50))
running_loss = 0.0
def test():
net.eval()
correct = 0.
total = 0.
class_correct = [0.] * 10 #[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]
class_total = [0.] * 10
for images, labels in testloader:
images, labels = images.to(device), labels.to(device)
with torch.no_grad():
outputs = net(images)
correct += labels.eq(torch.argmax(outputs, dim=1)).sum().item() # 0 이 COLUMN 방향 1 이 RAW 방향이라 그방향으로 보는것
total += labels.size(0)
for idx in range(10): #mnist 는 class 가 10개니까 0~9
label_idx = (labels == idx) # label_idx 는 bool 형태임
if label_idx.sum().item() == 0:
continue
class_correct[idx] += labels[label_idx].eq(torch.argmax(
outputs[label_idx],1)).sum().item()
class_total[idx] += labels[label_idx].size(0)
print('Accuracy: %d %%' %(100. * correct/total))
for i in range(10):
print('Accuracy of %5s : %2d %%' %(str(i), 100.*class_correct[i]/
class_total[i]))
for epoch in range(5):
train(epoch)
test()
import matplotlib.pyplot as plt
import numpy as np
batch_size = 128
# transform(x)
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),(0.5, 0.5, 0.5))] # mean, variance
)
trainset = datasets.CIFAR10(root='./data', train=True, download=True,
transform=transform)
trainloader = DataLoader(trainset, batch_size=batch_size,
shuffle=True,
num_workers=2)
testset = datasets.CIFAR10(root='./data', train=False, download=True,
transform=transform)
testloader = DataLoader(testset, batch_size=batch_size,
shuffle=False,
num_workers=2)
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % labels[j].item() for j in range(4)))
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=3,
out_channels=6,
kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6,
out_channels=16,
kernel_size=5)
# y = Wx + b
self.fc1 = nn.Linear(in_features = 16 * 5 * 5, out_features=120) # bias = True 가 기본
self.fc2 = nn.Linear(in_features=120, out_features=84)
self.fc3 = nn.Linear(in_features=84, out_features=10)
def forward(self, x):
# x.shape : b, 1, 28,28 --> b, 6, 24, 24
# H(input 높이) , K(kernel size) : K(conv) --> H - K + 1
x = self.conv1(x)
x = F.relu(x)
# x.shape: b, 6, 24, 24 --> b, 6, 12,12
x = F.max_pool2d(x, (2,2))
# x.shape: b, 6, 12, 12 --> b, 16, 8, 8
x = self.conv2(x)
x = F.relu(x)
# x.shape: b, 16, 8, 8 --> b, 16, 4, 4
x = F.max_pool2d(x, (2,2))
# x.shape: b, 16, 4, 4 - > b, 256 (-1 이 쫙피게 연산해줌)
x = x.view(x.size(0), -1)
# x.shape: b , 256 --> b, 120
x = self.fc1(x)
x = F.relu(x)
# x.shape: b, 120 --> b, 84
x = self.fc2(x)
x = F.relu(x)
# x.shape: b,84 --> b, 10
x = self.fc3(x)
return x
net = Net()
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
#print(device)
net = net.to(device=device)
print(net)
optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9)
criterion = nn.CrossEntropyLoss()
def test():
net.eval()
correct = 0.
total = 0.
class_correct = [0.] * 10 #[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]
class_total = [0.] * 10
for images, labels in testloader:
images, labels = images.to(device), labels.to(device)
with torch.no_grad():
outputs = net(images)
correct += labels.eq(torch.argmax(outputs, dim=1)).sum().item() # 0 이 COLUMN 방향 1 이 RAW 방향이라 그방향으로 보는것
total += labels.size(0)
for idx in range(10): #mnist 는 class 가 10개니까 0~9
label_idx = (labels == idx) # label_idx 는 bool 형태임
if label_idx.sum().item() == 0:
continue
class_correct[idx] += labels[label_idx].eq(torch.argmax(
outputs[label_idx],1)).sum().item()
class_total[idx] += labels[label_idx].size(0)
print('Accuracy: %d %%' %(100. * correct/total))
classes = ('plane', 'car', 'bird', 'cat', 'deer','dog', 'frog', 'horse', 'ship', 'truck')
for i in range(10):
print('Accuracy of %5s : %2d %%' %(classes[i], 100.*class_correct[i]/
class_total[i]))
for epoch in range(10):
train(epoch)
test()