|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# @Time : 19-6-21 下午2:56 |
| 4 | +# @Author : zj |
| 5 | + |
| 6 | +import torch |
| 7 | +import torch.nn as nn |
| 8 | + |
| 9 | +__all__ = ['NIN', 'nin'] |
| 10 | + |
| 11 | +model_urls = { |
| 12 | + 'nin': '' |
| 13 | +} |
| 14 | + |
| 15 | + |
| 16 | +class NIN(nn.Module): |
| 17 | + |
| 18 | + def __init__(self, in_channels=1, out_channels=10): |
| 19 | + super(NIN, self).__init__() |
| 20 | + |
| 21 | + self.features1 = nn.Sequential( |
| 22 | + nn.Conv2d(in_channels, 192, (5, 5), stride=1, padding=2), |
| 23 | + nn.ReLU(), |
| 24 | + nn.Conv2d(192, 160, (1, 1), stride=1, padding=0), |
| 25 | + nn.ReLU(), |
| 26 | + nn.Conv2d(160, 96, (1, 1), stride=1, padding=0), |
| 27 | + nn.ReLU(), |
| 28 | + nn.MaxPool2d(2, stride=2), |
| 29 | + nn.Dropout2d() |
| 30 | + ) |
| 31 | + self.features2 = nn.Sequential( |
| 32 | + nn.Conv2d(96, 192, (5, 5), stride=1, padding=2), |
| 33 | + nn.ReLU(), |
| 34 | + nn.Conv2d(192, 192, (1, 1), stride=1, padding=0), |
| 35 | + nn.ReLU(), |
| 36 | + nn.Conv2d(192, 192, (1, 1), stride=1, padding=0), |
| 37 | + nn.ReLU(), |
| 38 | + nn.MaxPool2d(2, stride=2), |
| 39 | + nn.Dropout2d() |
| 40 | + ) |
| 41 | + self.features3 = nn.Sequential( |
| 42 | + nn.Conv2d(192, 192, (3, 3), stride=1, padding=1), |
| 43 | + nn.ReLU(), |
| 44 | + nn.Conv2d(192, 192, (1, 1), stride=1, padding=0), |
| 45 | + nn.ReLU(), |
| 46 | + nn.Conv2d(192, out_channels, (1, 1), stride=1, padding=0), |
| 47 | + nn.ReLU(), |
| 48 | + ) |
| 49 | + |
| 50 | + self.gap = nn.AvgPool2d(8) |
| 51 | + |
| 52 | + def forward(self, inputs): |
| 53 | + x = self.features1(inputs) |
| 54 | + x = self.features2(x) |
| 55 | + x = self.features3(x) |
| 56 | + x = self.gap(x) |
| 57 | + |
| 58 | + return x.view(x.shape[0], x.shape[1]) |
| 59 | + |
| 60 | + |
| 61 | +def nin(pretrained=False, **kwargs): |
| 62 | + """ |
| 63 | + 创建模型对象 |
| 64 | + """ |
| 65 | + |
| 66 | + model = NIN(**kwargs) |
| 67 | + # if pretrained: |
| 68 | + # params = load_params(model_urls['nin']) |
| 69 | + # model.set_params(params) |
| 70 | + return model |
0 commit comments