| 130 | |
| 131 | # 定义辅助分类器结构 |
| 132 | class InceptionAux(nn.Module): |
| 133 | def __init__(self, in_channels, num_classes): |
| 134 | super(InceptionAux, self).__init__() |
| 135 | self.averagePool = nn.AvgPool2d(kernel_size=5, stride=3) |
| 136 | self.conv = BasicConv2d(in_channels, 128, kernel_size=1) |
| 137 | |
| 138 | self.fc1 = nn.Linear(2048, 1024) |
| 139 | self.fc2 = nn.Linear(1024, num_classes) |
| 140 | |
| 141 | def forward(self, x): |
| 142 | # aux1: N x 512 x 14 x14, aux2: N x 528 x 14 x 14 |
| 143 | x = self.averagePool(x) |
| 144 | # aux1: N x 512 x 4 x 4, aux2: N x 528 x 4 x 4 |
| 145 | s = self.conv(x) |
| 146 | # N x 128 x 4 x 4 |
| 147 | x = torch.flatten(x, 1) |
| 148 | # 当我们实例化一个模型model后,可以通过model.train()和model.eval()来控制模型的状态 |
| 149 | # 在model.train()状态下self.training=True,在model.eval()状态下,self.trining=False |
| 150 | x = F.dropout(x, 0.5, training=self.training) |
| 151 | # N x 2048 |
| 152 | x = F.relu(self.fc1(x), inplace=True) |
| 153 | x = F.dropout(x, 0.5, training=self.training) |
| 154 | |
| 155 | |
| 156 | class BasicConv2d(nn.Module): |