이론 문제 답안

  1. c. 3X244X244가 기준임
  2. 레이어를 거치면서 채널의 수가 변경되기 때문이다.
  3. b,f,e
  4. 이미지를 텐서로 변환시켰기 때문에 에러가 일어난다. 5-1. 16개 5-2. 16
  5. convolution layer, pooling layer, fc layer
  6. 행렬의 합성곱 연산이 있다.
  7. 작다
  8. 커널 사이즈가 크면 이미지 사이즈 축소가 급격하게 이루어져 깊은 층 만들기가 어렵고, 파라미터 개수와 연산량이 많아지기 때문
  9. 5번

실습 문제 답안

cfg = [64,'M',128,'M', 256,256,'M',512,512'M',512,1024'M']

def make_layer(cfg, batch_norm=False):
	layer = []
	in_channel = 3
	for v in cfg:
		if v =='M':
			layers +=[nn.MaxPool2d(kernel_size=2,stride=2)]
		else:
			conv2d = mm.Con2d(in_channel, v, kernel_size=3, padding=1)
			if batch_norm:
				layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
			else:
				layers += [con2d, nn,ReLU(inplace=True)]
			in_channels = v
class VGG(nn.Module):
    def __init__(self, features, num_classes=1000, init_weights=True):
        super(VGG, self).__init__()
        
        self.features = features #convolution
        
        self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
        
        self.classifier = nn.Sequential(
            nn.Linear(512 * 7 * 7, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )#FC layer
        
        if init_weights:
            self._initialize_weights()

    def forward(self, x):
        x = self.features(x) #Convolution 
        x = self.avgpool(x) # avgpool
        x = x.view(x.size(0), -1) #
        x = self.classifier(x) #FC layer
        return x

    def _initialize_weights(self):
        for m in self.modules():
            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)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)

위의 코드에서 fc layer가 3개가 되어 있으므로 ‘A’, ‘B’, ‘D’, ‘E’ 모두 FC layer가 3개씩 존재하게 된다.

cfg = {
    'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], #8 + 3 =11 == vgg11
    'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], # 10 + 3 = vgg 13
    'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'], #13 + 3 = vgg 16
    'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'], # 16 +3 =vgg 19
}