데이터 공부기록

[0627] pytorch_sample_CNN 본문

카테고리 없음

[0627] pytorch_sample_CNN

standingR 2024. 6. 27. 18:13

https://www.youtube.com/watch?v=wnK3uWv_WkU

 

- 잠깐 회고

위 내용 바탕으로 CNN 코드 구현... 영어 대한 자신감이 없어서 영어로 된 강의를 잘 안찾아보고 멀리 했는데 확실히 이미 한국어로 공부한 내용을 다시 영어로 보니 구현 하기에 용이 했다.

내가 읽어야하는 논문 또한 이런 방식으로 나는 영어가 약하니, 한국어로 이해 => 그 다음 영어 원어 찬찬히 읽어보기 

이 순서로 진행하자!

 

실제 코드 구현

# imports
import torch
import torch.nn  as nn # All neural network modules, nn.Linear, nn.Conv2d, BatchNorm, Loss functions
import torch.optim as optim # for all Optimization algortim, SGD, adam, etc
import torch.nn.functional as F # 매개 변수가 없는 모든 함수 / All fuctions that don't have any paramaters
from torch.utils.data import DataLoader # Gives easier dataset management and creates mini batches
import torchvision.datasets as datasets # Has standard datasets we can import in a nice way
import torchvision.transforms as transforms # Transformation we can perform on our dataset

# Create Simple Fully Connercted Neural Network
class NN(nn.Module):
    def __init__(self, input_size, num_classes):
        super(NN.self).__init__()
        self.fc1 = nn.Linear(input_size, 50)
        self.fc2 = nn.Linear(50, num_classes)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

# TODO : create simple CNN
class CNN(nn.Module):
    def __init__(self, in_channels = 1, num_classes = 10):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels = 1, out_channels=8, kernel_size=(3, 3), stride=(1, 1), padding=(1,1))
        self.pool = nn.MaxPool2d(kernel_size=(2,2), stride =(2,2))
        self.conv2 = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=(3,3), stride=(1,1), padding=(1,1))
        self.fc1 = nn.Linear(16*7*7, num_classes)

    def forward(self, x):
        #x = F.relu(self.conv1(x))
        x = x.view(-1, 1, 28, 28)
        x = self.conv1(x)
        x = self.pool(x)
        x = self.conv2(x)
        #x = F.relu(self.conv2(x))
        x = self.pool(x)
        x = x.reshape(x.shape[0], -1)
        x = x.view(x.size(0), -1)
        x = self.fc1(x)

        return x

"""    def forward(self, x):
        x = x.view(-1, 1, 28, 28)  # reshape input to [batch_size, 1, 28, 28]
        x = self.conv1(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = self.pool(x)  # 추가: 두 번째 풀링 계층 적용
        x = x.view(x.size(0), -1)  # flatten the tensor for the fully connected layer
        x = self.fc1(x)
        return x
"""

# Set device

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Hyperparmeters
in_channels = 1
num_classes = 10
learning_rate = 0.001
batch_size = 64
num_epochs = 1

# load Data
train_dataset = datasets.MNIST(root='dataset/', train=True, transform = transforms.ToTensor(), download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_dataset = datasets.MNIST(root='dataset/', train=False, transform = transforms.ToTensor(), download=True)
test_loader = DataLoader(dataset=test_dataset, batch_size = batch_size, shuffle=True)

# Initialize network
model = CNN().to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr = learning_rate)

# Train Network
for epoch in range(num_epochs):
    for batch_idx, (data, targets) in enumerate(train_loader):
        # Get data to cuda if possible
        data = data.to(device=device)
        targets = targets.to(device=device)

        # forward
        scores = model(data)
        loss = criterion(scores, targets)

        # back ward
        optimizer.zero_grad()
        loss.backward()

        # gradient descent or adam step
        optimizer.step()

오류

self.conv1 = nn.Conv2d(in_channels = 1, out_channels=8, kernel_size=(3, 3), stride=(1, 1), padding=(1,1))

In [22]:
import torch
import torch.nn as nn

class CNN(nn.Module):
    def __init__(self, in_channels, num_classes):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        self.pool = nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        self.conv2 = nn.Conv2d(in_channels=8, out_channels=16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
        self.fc1 = nn.Linear(16 * 7 * 7, num_classes)

    def forward(self, x):
        x = x.view(-1, 1, 28, 28)  # reshape input to [batch_size, 1, 28, 28]
        x = self.conv1(x)
        x = self.pool(x)
        x = self.conv2(x)
        x = self.pool(x)  # 추가: 두 번째 풀링 계층 적용
        x = x.view(x.size(0), -1)  # flatten the tensor for the fully connected layer
        x = self.fc1(x)
        return x

# 네트워크 초기화
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = CNN(in_channels=1, num_classes=10).to(device)

# 손실 함수와 옵티마이저
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)

print(model)
CNN(
  (conv1): Conv2d(1, 8, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (pool): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
  (conv2): Conv2d(8, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (fc1): Linear(in_features=784, out_features=10, bias=True)
)
In [ ]: