인공지능/공부

인공지능 MNIST - CNN

이게될까 2023. 12. 1. 15:47
728x90
728x90

이번에는 CNN을 통한 FCN과 비교이다. CNN코드는 밑에 작성하겠다.

import torch
import torch.nn as nn
import torch.nn.functional as f
import torch.optim as optim
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import time
device= "cuda" if torch.cuda.is_available() else "cpu"
print(device)
trainset = dsets.MNIST(root='dataset/',
                       train=True,
                       transform= transforms.ToTensor(),
                       download = True)
testset = dsets.MNIST(root='dataset/',
                       train=False,
                       transform= transforms.ToTensor(),
                       download = True)
trainset.data = trainset.data/255
testset.data = testset.data/255
relu = nn.ReLU()
model=nn.Sequential(nn.Conv2d(in_channels=1,out_channels=6,kernel_size = 5,stride=1, padding=2,dilation=1,groups=1,bias=True),
                    nn.BatchNorm2d(6),
                    relu,
                    nn.MaxPool2d(kernel_size = 2,stride = 2,padding = 0, dilation = 1, ceil_mode= False),
                    nn.Conv2d(in_channels=6,out_channels=20,kernel_size = 5,stride=1, padding=2,dilation=1,groups=1,bias=True),
                    nn.BatchNorm2d(20),
                    relu,
                    nn.MaxPool2d(kernel_size = 2,stride = 2,padding = 0, dilation = 1, ceil_mode= False),
                    
                    nn.Conv2d(in_channels=20,out_channels=40,kernel_size = 5,stride=1, padding=2,dilation=1,groups=1,bias=True),
                    nn.BatchNorm2d(40),
                    relu,
                    nn.MaxPool2d(kernel_size = 2,stride = 2,padding = 1, dilation = 1, ceil_mode= False),
                    
                    nn.Conv2d(in_channels=40,out_channels=80,kernel_size = 3,stride=1, padding=1,dilation=1,groups=1,bias=True),
                    nn.BatchNorm2d(80),
                    relu,
                    nn.MaxPool2d(kernel_size = 2,stride = 2,padding = 0, dilation = 1, ceil_mode= False),
                    
                    nn.Conv2d(in_channels=80,out_channels=160,kernel_size = 3,stride=1, padding=1,dilation=1,groups=1,bias=True),
                    relu,
                    nn.Conv2d(in_channels=160,out_channels=160,kernel_size = 3,stride=1, padding=1,dilation=1,groups=1,bias=True),
                    nn.AdaptiveAvgPool2d((1,1)),
                    nn.Flatten(),
                    nn.Linear(160,40,bias =True),
                    nn.BatchNorm1d(40),
                    relu,
                    nn.Dropout(0.3),
                    nn.Linear(40,10,bias =True),
                    nn.Softmax(dim = 1)
                     ).to(device)
lr = 0.0001
epochs = 200
bathsize = 4000
dropout = 0.1
dataset=TensorDataset(trainset.data,trainset.targets)
dataloader = DataLoader(dataset, batch_size= bathsize,shuffle=True)
optimizer = optim.Adam(model.parameters(),lr=lr,betas=(0.9,0.999), weight_decay=0.001)
loss_graph=[]
start= time.time()
for k in range (epochs +1):
  for i ,sample in enumerate(dataloader) :
    (x,y) = sample
    optimizer.zero_grad()
    cost = f.cross_entropy(model(torch.unsqueeze(x,1).float().to(device)),y.to(device)).to(device)
    cost.backward()
    optimizer.step()
  print(k, cost.item())
  loss_graph.append(cost.item())
print("time : {} sec".format(time.time()-start))

plt.figure()
plt.plot(loss_graph)
plt.xlabel('Number of iterations')
plt.ylabel('loss_graph')

print((torch.argmax(model(torch.unsqueeze(testset.data,1).data.float().to(device)),dim=1).float()==testset.targets.to(device)).float().mean())
print((torch.argmax(model(torch.unsqueeze(trainset.data[:10000,:,:] ,1).float().to(device)),dim=1).float()==trainset.targets[:10000].to(device)).float().mean())

또 비슷한 코드들의 반복이다. train과 test 전부 100점 받기는 힘든 일인 것 같다.....
그래도 정규화, 스케일링 등 내가 배운 여러가지 지식들이 이 코드에 녹아있다. 궁금하신 부분 있으면 댓글 작성해 주시고 이 코드 돌리면 아마 test에서 99점의 적중률이 나올 것이다.

728x90