Exemplo n.º 1
0
 def __init__(self):
     super(DeepCNN, self).__init__()
     self.conv1 = nn.Sequential(nn.Conv2d(4, 96, kernel_size=7),
                                nn.ReLU(inplace=True),
                                nm.LocalResponseNorm(size=3),
                                nn.MaxPool2d(kernel_size=3, stride=7))
     self.conv2 = nn.Sequential(
         nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=0),
         nn.ReLU(inplace=True), nm.LocalResponseNorm(size=3),
         nn.MaxPool2d(kernel_size=2, stride=5))
     self.conv3 = nn.Sequential(
         nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=0),
         nn.ReLU(inplace=True),
         nm.LocalResponseNorm(size=3),
     )
     self.conv4 = nn.Sequential(
         nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=0),
         nn.ReLU(inplace=True),
         nm.LocalResponseNorm(size=3),
     )
     self.conv5 = nn.Sequential(
         nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=0),
         nn.ReLU(inplace=True), nm.LocalResponseNorm(size=3),
         nn.MaxPool2d(kernel_size=2, stride=3))
     self.classifier = nn.Sequential(
         nn.Dropout(0.9, inplace=True),
         nn.Linear(21 * 21 * 512, 4096),
         nn.ReLU(inplace=True),
         nn.Dropout(0.8, inplace=True),
         nn.Linear(4096, 2048),
         nn.ReLU(inplace=True),
         nn.Linear(2048, 11),
         nn.Softmax(dim=1),
     )
Exemplo n.º 2
0
 def __init__(self):
     super(ShallowCNN, self).__init__()
     self.conv1 = nn.Sequential(nn.Conv2d(4, 32, kernel_size=3), nn.ReLU(),
                                nm.LocalResponseNorm(size=3),
                                nn.MaxPool2d(kernel_size=2, stride=3))
     self.conv2 = nn.Sequential(
         nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=0), nn.ReLU(),
         nm.LocalResponseNorm(size=3), nn.MaxPool2d(kernel_size=2,
                                                    stride=3))
     self.conv3 = nn.Sequential(
         nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=0), nn.ReLU(),
         nm.LocalResponseNorm(size=3), nn.MaxPool2d(kernel_size=2,
                                                    stride=3))
     self.classifier = nn.Sequential(
         nn.Linear(7 * 7 * 128, 512),
         nn.ReLU(),
         nn.Dropout(0.9),
         nn.Linear(512, 512),
         nn.ReLU(),
         nn.Dropout(0.8),
         nn.Linear(512, 11),
         nn.Softmax(dim=1),
     )
Exemplo n.º 3
0
    def __init__(self, n, num_classes=1000, aux_logits=False):
        super().__init__()

        if type(n) is not int or n < 1:
            raise ValueError('num_classes must be a positive integer.')
        self._n = n

        if num_classes is not None and (type(num_classes) is not int
                                        or num_classes < 1):
            raise ValueError('num_classes must be a positive integer.')
        self._num_classes = num_classes

        self._aux_logits = aux_logits

        if self._num_classes is None and not self._aux_logits:
            raise ValueError(
                'Either num_classes must be set to a positive integer or aux_logits must be True'
            )

        # self._logits = logits
        # if self._logits and not self._num_classes:
        #     raise ValueError("Cannot return logits without specifying num_classes")

        self._feature_generator = nn.Sequential(
            nn.BatchNorm2d(1),
            nn.Conv2d(in_channels=1,
                      out_channels=96,
                      kernel_size=(7, 7),
                      stride=(2, 2),
                      padding=1),
            nn.ReLU(),
            normalization.LocalResponseNorm(size=5, k=2),
            nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2)),
            nn.BatchNorm2d(96),
            nn.Conv2d(in_channels=96,
                      out_channels=256,
                      kernel_size=(5, 5),
                      stride=(2, 2),
                      padding=1),
            nn.ReLU(),
            normalization.LocalResponseNorm(size=5, k=2),
            nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2)),
            nn.BatchNorm2d(256),
            nn.Conv2d(in_channels=256,
                      out_channels=384,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(384),
            nn.Conv2d(in_channels=384,
                      out_channels=256,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      padding=1),
            nn.ReLU(),
            nn.BatchNorm2d(256),
            nn.Conv2d(in_channels=256,
                      out_channels=256,
                      kernel_size=(3, 3),
                      stride=(1, 1),
                      padding=1),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(5, 3), stride=(3, 2)),
            nn.BatchNorm2d(256),
            nn.Conv2d(in_channels=256,
                      out_channels=4096,
                      kernel_size=(9, 1),
                      stride=(1, 1)),
            nn.ReLU(),
            nn.AvgPool2d(kernel_size=(1, self._n), stride=1),
            Flatten(),
        )

        if self._num_classes:
            # add classification head
            self._classifier = nn.Sequential(
                nn.Dropout(0.5), nn.Linear(in_features=4096,
                                           out_features=1024), nn.ReLU(),
                nn.Dropout(0.5),
                nn.Linear(in_features=1024, out_features=num_classes),
                nn.Softmax())
Exemplo n.º 4
0
    def __init__(self, num_classes=1000):
        super(Re3Alexnet, self).__init__()
        input_channels = 3
        self.conv1 = nn.Sequential(
            OrderedDict([('conv1',
                          nn.Conv2d(input_channels,
                                    out_channels=96,
                                    kernel_size=11,
                                    stride=4)), ('relu1', nn.ReLU()),
                         ('pool1', nn.MaxPool2d(kernel_size=3, stride=2)),
                         ('lrn1',
                          norm.LocalResponseNorm(size=2,
                                                 alpha=2e-5,
                                                 beta=0.75,
                                                 k=1.0))]))

        # We are reducing the first conv
        self.skip1 = nn.Sequential(
            OrderedDict([('conv_reduce',
                          nn.Conv2d(96,
                                    out_channels=16,
                                    kernel_size=1,
                                    stride=1)), ('prelu', nn.PReLU()),
                         ('conv_flatten', Flatten())]))

        # This is output of conv1
        self.conv2 = nn.Sequential(
            OrderedDict([('conv2',
                          nn.Conv2d(in_channels=96,
                                    out_channels=256,
                                    groups=2,
                                    kernel_size=5,
                                    padding=2)), ('relu2', nn.ReLU()),
                         ('pool2', nn.MaxPool2d(kernel_size=3, stride=2)),
                         ('lrn2',
                          norm.LocalResponseNorm(size=2,
                                                 alpha=2e-5,
                                                 beta=0.75,
                                                 k=1.0))]))

        self.skip2 = nn.Sequential(
            OrderedDict([('conv_reduce',
                          nn.Conv2d(256,
                                    out_channels=32,
                                    kernel_size=1,
                                    stride=1)), ('prelu', nn.PReLU()),
                         ('conv_flatten', Flatten())]))

        self.conv3 = nn.Sequential(
            OrderedDict([('conv3',
                          nn.Conv2d(in_channels=256,
                                    out_channels=384,
                                    kernel_size=3,
                                    padding=1)), ('relu3', nn.ReLU())]))

        self.conv4 = nn.Sequential(
            OrderedDict([('conv4',
                          nn.Conv2d(in_channels=384,
                                    out_channels=384,
                                    kernel_size=3,
                                    padding=1,
                                    groups=2)), ('relu4', nn.ReLU())]))

        self.conv5 = nn.Sequential(
            OrderedDict([('conv5',
                          nn.Conv2d(in_channels=384,
                                    out_channels=256,
                                    kernel_size=3,
                                    padding=1,
                                    groups=2)), ('relu5', nn.ReLU())]))

        self.pool5 = nn.Sequential(
            OrderedDict([('pool5', nn.MaxPool2d(kernel_size=3, stride=2))]))

        self.conv5_flat = nn.Sequential(
            OrderedDict([('conv5_flat', Flatten())]))

        self.skip5 = nn.Sequential(
            OrderedDict([('conv_reduce',
                          nn.Conv2d(256,
                                    out_channels=64,
                                    kernel_size=1,
                                    stride=1)), ('prelu', nn.PReLU()),
                         ('conv_flatten', Flatten())]))

        self.conv6 = nn.Sequential(
            OrderedDict([('fc6', nn.Linear(37104 * 2, 2048)),
                         ('relu6', nn.ReLU())]))