https://inha-kim.tistory.com/43?category=963044
논문 리뷰는 다음과 같이 하였습니다.
구현
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 | # VGG-11, VGG-13, VGG-16, VGG-19 총 4개의 Model이 존재 # https://www.youtube.com/watch?v=ACmuBbuXn20&t=4s&ab_channel=AladdinPersson VGG_Network ={ 'VGG11' : [64, 'M', 128, 'M', 256, 256, 'M', 512,512, 'M',512,512,'M'], 'VGG13' : [64,64, 'M', 128, 128, 'M', 256, 256, 'M', 512,512, 'M', 512,512,'M'], 'VGG16' : [64,64, 'M', 128, 128, 'M', 256, 256,256, 'M', 512,512,512, 'M',512,512,512,'M'], 'VGG19' : [64,64, 'M', 128, 128, 'M', 256, 256,256,256, 'M', 512,512,512,512, 'M',512,512,512,512,'M'] } # VGG class VGGnet(nn.Module): def __init__(self,model,in_channels = 3, num_classes = 10,init_weights = True): super(VGGnet,self).__init__() self.in_channels = in_channels # create conv layer , VGG Type self.conv_layers = self.create_conv_layer(VGG_Network[model]) self.fcs = nn.Sequential( nn.Linear(in_features = 512*7*7,out_features=4096), nn.ReLU(), nn.Dropout(), nn.Linear(in_features = 4096,out_features=4096), nn.ReLU(), nn.Dropout(), nn.Linear(4096,num_classes), ) if init_weights: self._initialize_weights() def forward(self,x): out = self.conv_layers(x) out = out.view(-1,512*7*7) out = self.fcs(out) return out def create_conv_layer(self,Network): layers = [] in_channels = self.in_channels for x in Network: if type(x) == int: # conv layer out_channels = x layers += [nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size =(3,3), stride = (1,1), padding = (1,1)), nn.BatchNorm2d(x), nn.ReLU()] in_channels = x elif x == 'M': # maxpooling layers += [nn.MaxPool2d(kernel_size=(2,2),stride=(2,2))] return nn.Sequential(*layers) 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,mean = 0,std=0.01) nn.init.constant_(m.bias,0) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(device) model = VGGnet('VGG11', in_channels=3, num_classes=10, init_weights=True).to(device) | cs |