참고자료 : https://pytorch.kr/hub/pytorch_vision_vgg/
모델 구성
class VGG11(nn.Module):
def __init__(self):
super(VGG11, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, padding=1, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, padding=0, stride=2)
)
self.conv2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, padding=1, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, padding=0, stride=2)
)
self.conv3 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=256, padding=1, kernel_size=3),
nn.ReLU(),
nn.Conv2d(in_channels=256, out_channels=256, padding=1, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, padding=0, stride=2)
)
self.conv4 = nn.Sequential(
nn.Conv2d(in_channels=256, out_channels=512, padding=1, kernel_size=3),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, padding=1, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, padding=0, stride=2)
)
self.conv5 = nn.Sequential(
nn.Conv2d(in_channels=512, out_channels=512, padding=1, kernel_size=3),
nn.ReLU(),
nn.Conv2d(in_channels=512, out_channels=512, padding=1, kernel_size=3),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, padding=0, stride=2)
)
self.classifier = nn.Sequential(
nn.Linear(in_features=512*7*7, out_features=4096),
nn.ReLU(),
nn.Linear(in_features=4096, out_features=4096),
nn.ReLU(),
nn.Linear(in_features=4096, out_features=1000)
)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = x.view(x.shape[0], -1)
x = self.classifier(x)
return x
모델 테스트
from torchsummary import summary
model = VGG11()
summary(model, input_size=(3, 224, 224))
tensor = torch.randn(size=(1, 3, 224, 224)) # B, C, H, W
x = model.forward(tensor)
print(x.shape)
print(f'X: index={x.argmax().item()} x_pred={x.flatten()[x.argmax()].item()}')
출력
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 224, 224] 1,792
ReLU-2 [-1, 64, 224, 224] 0
MaxPool2d-3 [-1, 64, 112, 112] 0
Conv2d-4 [-1, 128, 112, 112] 73,856
ReLU-5 [-1, 128, 112, 112] 0
MaxPool2d-6 [-1, 128, 56, 56] 0
Conv2d-7 [-1, 256, 56, 56] 295,168
ReLU-8 [-1, 256, 56, 56] 0
Conv2d-9 [-1, 256, 56, 56] 590,080
ReLU-10 [-1, 256, 56, 56] 0
MaxPool2d-11 [-1, 256, 28, 28] 0
Conv2d-12 [-1, 512, 28, 28] 1,180,160
ReLU-13 [-1, 512, 28, 28] 0
Conv2d-14 [-1, 512, 28, 28] 2,359,808
ReLU-15 [-1, 512, 28, 28] 0
MaxPool2d-16 [-1, 512, 14, 14] 0
Conv2d-17 [-1, 512, 14, 14] 2,359,808
ReLU-18 [-1, 512, 14, 14] 0
Conv2d-19 [-1, 512, 14, 14] 2,359,808
ReLU-20 [-1, 512, 14, 14] 0
MaxPool2d-21 [-1, 512, 7, 7] 0
Linear-22 [-1, 4096] 102,764,544
ReLU-23 [-1, 4096] 0
Linear-24 [-1, 4096] 16,781,312
ReLU-25 [-1, 4096] 0
Linear-26 [-1, 1000] 4,097,000
================================================================
Total params: 132,863,336
Trainable params: 132,863,336
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 125.12
Params size (MB): 506.83
Estimated Total Size (MB): 632.53
----------------------------------------------------------------
torch.Size([1, 1000])
X: index=184 x_pred=0.024277426302433014
'개발 > AI' 카테고리의 다른 글
[llama.cpp] quantize: 양자화 예시 (0) | 2024.01.04 |
---|---|
[Pytorch] GoogLeNet : 구현 연습 (0) | 2023.11.29 |
[Pytorch] Multiclass Classification : 임의의 클러스터 학습 (1) | 2023.11.21 |
[Pytorch] XOR 학습 (0) | 2023.11.20 |
[Python] ANN : XOR - 2 layer 구현하기 (0) | 2023.11.16 |