Home [PyTorch] CNN VGG
Post
Cancel

[PyTorch] CNN VGG

Library Call

1
2
3
4
5
6
7
8
9
10
11
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import torchvision.models.vgg as vgg
from torch.utils.data import DataLoader
import random
import numpy as np
import os
1
2
3
# device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
device
1
'cuda'
1
2
3
4
5
6
7
8
9
10
11
# seed 고정
def seed_everything(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic=True
    torch.backends.cudnn.benchmark=False

seed_everything(111)

Data Load

1
2
3
4
5
6
7
8
9
10
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

train_data = datasets.CIFAR10(root='./', train=True, download=True, transform=transform)
test_data = datasets.CIFAR10(root='./', train=False, download=True, transform=transform)

train = DataLoader(dataset=train_data, batch_size=512, shuffle=True, num_workers=0)
test = DataLoader(dataset=test_data, batch_size=4, shuffle=True, num_workers=0)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
1
2
3
4
5
6
7
8
9
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./cifar-10-python.tar.gz



  0%|          | 0/170498071 [00:00<?, ?it/s]


Extracting ./cifar-10-python.tar.gz to ./
Files already downloaded and verified

Make VGG16

1
cfg = [32,32,'M', 64,64,128,128,128,'M',256,256,256,512,512,512,'M']
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
class VGG(nn.Module):

    def __init__(self, features, num_classes=1000, init_weights=True):
        super(VGG, self).__init__()
        # Convolution Layer
        self.features = features

        # Fully-Connected Layer
        self.classifier = nn.Sequential(
            # Image SIze가 다른 경우 nn.Linear(512 * 4 * 4, 4096) 부분 수정 필요
            nn.Linear(512 * 4 * 4, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )

        # Weight Initializer
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        x = self.features(x) # Convolution
        x = x.view(x.size(0), -1) # Flatten
        x = self.classifier(x) # Fully-Connected Layer
        return x

    def _initialize_weights(self):
        # self.modules는 features의 Layer의 값을 하나씩 return
        for m in self.modules():
            # m이 nn.Conv2D 일 때
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            # m이 nn.BatchNorm2d 일 때
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            # m이 nn.Linear 일 때
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)
1
vgg16 = VGG(vgg.make_layers(cfg), 10, True).to(device)
1
vgg16
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
VGG(
  (features): Sequential(
    (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU(inplace=True)
    (2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU(inplace=True)
    (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (5): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (6): ReLU(inplace=True)
    (7): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (8): ReLU(inplace=True)
    (9): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (10): ReLU(inplace=True)
    (11): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (12): ReLU(inplace=True)
    (13): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (14): ReLU(inplace=True)
    (15): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (16): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (17): ReLU(inplace=True)
    (18): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (19): ReLU(inplace=True)
    (20): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (21): ReLU(inplace=True)
    (22): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (23): ReLU(inplace=True)
    (24): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (25): ReLU(inplace=True)
    (26): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (27): ReLU(inplace=True)
    (28): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=8192, out_features=4096, bias=True)
    (1): ReLU(inplace=True)
    (2): Dropout(p=0.5, inplace=False)
    (3): Linear(in_features=4096, out_features=4096, bias=True)
    (4): ReLU(inplace=True)
    (5): Dropout(p=0.5, inplace=False)
    (6): Linear(in_features=4096, out_features=10, bias=True)
  )
)
1
2
3
4
5
# Loss & Optimizer
criterion = nn.CrossEntropyLoss().to(device)
optimizer = torch.optim.SGD(vgg16.parameters(), lr = 0.005,momentum=0.9)

lr_sche = optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.9)

Training

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
epochs = 50

for epoch in range(epochs):  # loop over the dataset multiple times
    running_loss = 0.0
    lr_sche.step()
    for i, data in enumerate(train, 0):
        # get the inputs
        inputs, labels = data
        inputs = inputs.to(device)
        labels = labels.to(device)

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = vgg16(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 30 == 29:    # print every 30 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 30))
            running_loss = 0.0
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
/usr/local/lib/python3.9/dist-packages/torch/optim/lr_scheduler.py:138: UserWarning: Detected call of `lr_scheduler.step()` before `optimizer.step()`. In PyTorch 1.1.0 and later, you should call them in the opposite order: `optimizer.step()` before `lr_scheduler.step()`.  Failure to do this will result in PyTorch skipping the first value of the learning rate schedule. See more details at https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate
  warnings.warn("Detected call of `lr_scheduler.step()` before `optimizer.step()`. "


[1,    30] loss: 2.302
[1,    60] loss: 2.299
[1,    90] loss: 2.293
[2,    30] loss: 2.224
[2,    60] loss: 2.099
[2,    90] loss: 1.953
[3,    30] loss: 1.855
[3,    60] loss: 1.787
[3,    90] loss: 1.708
[4,    30] loss: 1.679
[4,    60] loss: 1.630
[4,    90] loss: 1.624
[5,    30] loss: 1.542
[5,    60] loss: 1.514
[5,    90] loss: 1.479
[6,    30] loss: 1.437
[6,    60] loss: 1.421
[6,    90] loss: 1.423
[7,    30] loss: 1.370
[7,    60] loss: 1.331
[7,    90] loss: 1.352
[8,    30] loss: 1.267
[8,    60] loss: 1.270
[8,    90] loss: 1.258
[9,    30] loss: 1.203
[9,    60] loss: 1.170
[9,    90] loss: 1.175
[10,    30] loss: 1.110
[10,    60] loss: 1.105
[10,    90] loss: 1.091
[11,    30] loss: 1.043
[11,    60] loss: 1.037
[11,    90] loss: 1.011
[12,    30] loss: 1.027
[12,    60] loss: 0.994
[12,    90] loss: 0.972
[13,    30] loss: 0.953
[13,    60] loss: 0.896
[13,    90] loss: 0.906
[14,    30] loss: 0.842
[14,    60] loss: 0.857
[14,    90] loss: 0.886
[15,    30] loss: 0.814
[15,    60] loss: 0.818
[15,    90] loss: 0.801
[16,    30] loss: 0.758
[16,    60] loss: 0.757
[16,    90] loss: 0.769
[17,    30] loss: 0.747
[17,    60] loss: 0.753
[17,    90] loss: 0.733
[18,    30] loss: 0.691
[18,    60] loss: 0.691
[18,    90] loss: 0.674
[19,    30] loss: 0.668
[19,    60] loss: 0.666
[19,    90] loss: 0.654
[20,    30] loss: 0.615
[20,    60] loss: 0.618
[20,    90] loss: 0.624
[21,    30] loss: 0.560
[21,    60] loss: 0.569
[21,    90] loss: 0.581
[22,    30] loss: 0.558
[22,    60] loss: 0.542
[22,    90] loss: 0.536
[23,    30] loss: 0.503
[23,    60] loss: 0.537
[23,    90] loss: 0.522
[24,    30] loss: 0.503
[24,    60] loss: 0.470
[24,    90] loss: 0.490
[25,    30] loss: 0.453
[25,    60] loss: 0.442
[25,    90] loss: 0.430
[26,    30] loss: 0.411
[26,    60] loss: 0.423
[26,    90] loss: 0.439
[27,    30] loss: 0.383
[27,    60] loss: 0.394
[27,    90] loss: 0.400
[28,    30] loss: 0.356
[28,    60] loss: 0.354
[28,    90] loss: 0.363
[29,    30] loss: 0.325
[29,    60] loss: 0.329
[29,    90] loss: 0.337
[30,    30] loss: 0.280
[30,    60] loss: 0.281
[30,    90] loss: 0.292
[31,    30] loss: 0.256
[31,    60] loss: 0.260
[31,    90] loss: 0.259
[32,    30] loss: 0.221
[32,    60] loss: 0.225
[32,    90] loss: 0.255
[33,    30] loss: 0.204
[33,    60] loss: 0.214
[33,    90] loss: 0.213
[34,    30] loss: 0.191
[34,    60] loss: 0.191
[34,    90] loss: 0.211
[35,    30] loss: 0.146
[35,    60] loss: 0.146
[35,    90] loss: 0.175
[36,    30] loss: 0.128
[36,    60] loss: 0.145
[36,    90] loss: 0.142
[37,    30] loss: 0.127
[37,    60] loss: 0.135
[37,    90] loss: 0.122
[38,    30] loss: 0.100
[38,    60] loss: 0.100
[38,    90] loss: 0.122
[39,    30] loss: 0.095
[39,    60] loss: 0.097
[39,    90] loss: 0.099
[40,    30] loss: 0.098
[40,    60] loss: 0.080
[40,    90] loss: 0.086
[41,    30] loss: 0.082
[41,    60] loss: 0.069
[41,    90] loss: 0.066
[42,    30] loss: 0.063
[42,    60] loss: 0.071
[42,    90] loss: 0.070
[43,    30] loss: 0.049
[43,    60] loss: 0.053
[43,    90] loss: 0.066
[44,    30] loss: 0.067
[44,    60] loss: 0.071
[44,    90] loss: 0.059
[45,    30] loss: 0.043
[45,    60] loss: 0.045
[45,    90] loss: 0.053
[46,    30] loss: 0.048
[46,    60] loss: 0.042
[46,    90] loss: 0.048
[47,    30] loss: 0.038
[47,    60] loss: 0.036
[47,    90] loss: 0.037
[48,    30] loss: 0.033
[48,    60] loss: 0.033
[48,    90] loss: 0.028
[49,    30] loss: 0.038
[49,    60] loss: 0.037
[49,    90] loss: 0.032
[50,    30] loss: 0.027
[50,    60] loss: 0.025
[50,    90] loss: 0.028
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
correct = 0
total = 0

with torch.no_grad():
    for data in test:
        images, labels = data
        images = images.to(device)
        labels = labels.to(device)
        outputs = vgg16(images)
        
        _, predicted = torch.max(outputs.data, 1)
        
        total += labels.size(0)
        
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))
1
Accuracy of the network on the 10000 test images: 77 %
This post is licensed under CC BY 4.0 by the author.