CNN모델 중 현재에도 많이 사용되고 있는 ResNet 구조를 활용해보도록 하겠습니다. (정확도 83%)

Point - Skip connection

논문링크 : https://arxiv.org/abs/1512.03385

 

<그림 출처 : https://bskyvision.com/644>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import torch
import numpy as np
import os
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms, datasets
import torch.nn.init as init
 
os.environ['KMP_DUPLICATE_LIB_OK'= 'True'
 
BATCH_SIZE = 32
EPOCHS = 10
 
if torch.cuda.is_available():
    DEVICE = torch.device('cuda')
else:
    DEVICE = torch.device('cpu')
 
print(DEVICE)
 
# Data Augmentation 기법 사용
trans = transforms.Compose([
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize((0.50.50.5), (0.50.50.5))
])
 
train_dataset = datasets.CIFAR10(root="./data/CIFAR_10",
                               train=True,
                               download=True,
                               transform=trans)
 
test_dataset = datasets.CIFAR10(root="./data/CIFAR_10",
                              train=False,
                              download=True,
                              transform=trans)
 
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=BATCH_SIZE,
                                           shuffle=True)
 
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                           batch_size=BATCH_SIZE,
                                           shuffle=False)
 
 
# 다운로드 받은 데이터셋 확인
for (x_train, y_train) in train_loader:
    print('x_train: ', x_train.size(), ' data_type: ', x_train.type())
    print('y_train: ', y_train.size(), ' data_type: ', y_train.type())
    break
 
fig = plt.figure(figsize=(51))
for i in range(5):
    plt.subplot(15, i + 1)
    plt.axis('off')
    plt.imshow(np.transpose(x_train[i], (120)))
    plt.title("class: " + str(y_train[i].item()))
 
plt.show()
 
 
class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1):
        super(ResidualBlock, self).__init__()
 
        self.conv1 = nn.Conv2d(in_channels=in_channels,
                               out_channels=out_channels,
                               kernel_size=3,
                               stride=stride,
                               padding=1,
                               bias=False)
 
        self.conv2 = nn.Conv2d(in_channels=out_channels,
                               out_channels=out_channels,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
 
        self.batch_norm1 = nn.BatchNorm2d(out_channels)
        self.batch_norm2 = nn.BatchNorm2d(out_channels)
 
        self.shortcut = nn.Sequential()
        if stride != 1 or out_channels != in_channels:
            self.shortcut = nn.Sequential(
                nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(out_channels)
            )
 
    def forward(self, input):
        x = self.conv1(input)
        x = self.batch_norm1(x)
        x = F.relu(x)
        x = self.batch_norm2(self.conv2(x))
        x += self.shortcut(input)
        x = F.relu(x)
 
        return x
 
 
class Resnet(nn.Module):
    def __init__(self, num_classes=10):
        super(Resnet, self).__init__()
 
        self.in_channels = 16
        self.conv1 = nn.Conv2d(316, kernel_size=3, stride=1, padding=1, bias=False)
        self.batch_norm1 = nn.BatchNorm2d(16)
        self.layer1 = self.gen_layer(162, stride=1)
        self.layer2 = self.gen_layer(322, stride=2)
        self.layer3 = self.gen_layer(642, stride=2)
        self.dense = nn.Linear(64, num_classes)
 
    def gen_layer(self, out_channels, num_blocks, stride):
 
        strides = [stride] + [1]*(num_blocks-1#num_blocks=3 이면, strides = [2, 1, 1]
        layers = []
        for temp in strides:
            layers.append(ResidualBlock(self.in_channels, out_channels, temp))
            self.in_channels = out_channels
 
        return nn.Sequential(*layers)
 
    def forward(self, x):
        x = self.conv1(x)
        x = self.batch_norm1(x)
        x = F.relu(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = F.avg_pool2d(x, 8)
        x = x.view(x.size(0), -1)
        x = self.dense(x)
 
        return x
 
 
 
def weight_initializer(m):
    if isinstance(m, nn.Linear):
        init.kaiming_uniform_(m.weight.data)
 
 
model = Resnet().to(DEVICE)
model.apply(weight_initializer)  #가중치 초기화 기법을 사용
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.CrossEntropyLoss()
 
print(model)
 
 
def train(model, train_loader, optimizer, interval):
    model.train()
 
    for idx, (image, label) in enumerate(train_loader):
        image = image.to(DEVICE)
        label = label.to(DEVICE)
        optimizer.zero_grad()
        output = model(image)
        loss = criterion(output, label)
        loss.backward()
        optimizer.step()
 
        if idx % interval == 0:
            print('train epoch: {}, {}/{} train_loss: {}'
                  .format(epoch, idx*len(image), len(train_loader.dataset), loss.item()))
 
 
def evaluate(model, test_loader):
    model.eval()
    test_loss = 0
    right = 0
 
    with torch.no_grad():
        for image, label in test_loader:
            image = image.to(DEVICE)
            label = label.to(DEVICE)
            output = model(image)
            test_loss += criterion(output, label).item()
            pred = output.max(1, keepdim=True)[1]
            right += pred.eq(label.view_as(pred)).sum().item()
 
    test_loss /= len(test_loader.dataset)
    test_acc = right/len(test_loader.dataset) * 100
 
    return test_loss, test_acc
 
 
for epoch in range(1, EPOCHS+1):
    train(model, train_loader, optimizer, 200)
    test_loss, test_acc = evaluate(model, test_loader)
    print("test_loss: {}, test_acc: {}".format(test_loss, test_acc))
 
cs

 

위의 소스코드를 실행시키면 아래와 같은 출력값을 얻을 수 있다.

 

 

반응형

'머신러닝_딥러닝 > Pytorch' 카테고리의 다른 글

CNN 4탄 (실전연습)  (0) 2021.10.17
CNN 3탄 (CIFAR-10 dataset)  (0) 2021.10.17
CNN 1탄 (CIFAR-10 dataset)  (0) 2021.10.17
MLP모델 (CIFAR-10 dataset)  (0) 2021.10.17
오토인코더(AutoEncoder) 구현 기초예제  (0) 2021.10.17

+ Recent posts