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
| class GoogLeNet(nn.Layer): def __init__(self, in_channels=3, n_classes=2): ''' * `in_channels`: 输入的通道数 * `n_classes`: 输出分类数量 ''' super(GoogLeNet, self).__init__()
self.block1 = nn.Sequential( ConvBN2d(in_channels, 64, 7, 2, 3), nn.ReLU(), nn.MaxPool2D(3, 2, 1), ) self.block2 = nn.Sequential( ConvBN2d(64, 64, 1, 1, 0), nn.ReLU(), ConvBN2d(64, 192, 3, 1, 1), nn.ReLU(), nn.MaxPool2D(3, 2, 1), ) self.block3 = nn.Sequential( Inception(192, 64, (96, 128), (16, 32), 32), Inception(256, 128, (128, 192), (32, 96), 64), nn.MaxPool2D(3, 2, 1), ) self.block4 = nn.Sequential( Inception(480, 192, (96, 208), (16, 48), 64), Inception(512, 160, (112, 224), (24, 64), 64), Inception(512, 128, (128, 256), (24, 64), 64), Inception(512, 112, (144, 288), (32, 64), 64), Inception(528, 256, (160, 320), (32, 128), 128), nn.MaxPool2D(3, 2, 1), ) self.block5 = nn.Sequential( Inception(832, 256, (160, 320), (32, 128), 128), Inception(832, 384, (192, 384), (48, 128), 128), nn.AdaptiveAvgPool2D(1), ) self.block6 = nn.Sequential( nn.Flatten(1, -1), nn.Dropout(p=0.4), nn.Linear(1024, n_classes), )
def forward(self, x): x = self.block1(x) x = self.block2(x) x = self.block3(x) x = self.block4(x) x = self.block5(x) y = self.block6(x) return y
|