Exemple #1
0
 def __init__(self, num_classes=10):
     super(ConvNet, self).__init__()
     self.layer1 = nn.Sequential(
         nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
         nn.BatchNorm2d(16),
         nn.ReLU(),
         nn.MaxPool2d(kernel_size=2, stride=2))
     self.layer2 = nn.Sequential(
         nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
         nn.BatchNorm2d(32),
         nn.ReLU(),
         nn.MaxPool2d(kernel_size=2, stride=2))
     self.fc = nn.Linear(7 * 7 * 32, num_classes)
Exemple #2
0
def conv2dpool(cin, cout, pool_type, bn=NormType.Batch):
    assert pool_type in ['avg', 'max']
    if pool_type == 'max':
        return nn.Sequential(
            nn.MaxPool2d(2, stride=2),
            init_default(nn.Conv2d(cin, cout, 1, bias=False),
                         nn.init.kaiming_normal_),
            batchnorm_2d(cout, norm_type=bn))
    if pool_type == 'avg':
        return nn.Sequential(
            nn.AvgPool2d(2, stride=2, ceil_mode=True, count_include_pad=False),
            init_default(nn.Conv2d(cin, cout, 1, bias=False),
                         nn.init.kaiming_normal_),
            batchnorm_2d(cout, norm_type=bn))
Exemple #3
0
    def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                conv1x1(self.inplanes, planes * block.expansion, stride),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)
Exemple #4
0
def conv2d(cin,
           cout=None,
           ksize=3,
           stride=1,
           padding=None,
           dilation=None,
           groups=None,
           use_relu=True,
           use_bn=True,
           bn=NormType.Batch,
           bias=False):
    if cout is None: cout = cin
    if padding is None: padding = ksize // 2
    if dilation is None: dilation = 1
    if groups is None: groups = 1
    layer = [
        init_default(
            nn.Conv2d(cin,
                      cout,
                      ksize,
                      stride=stride,
                      padding=padding,
                      dilation=dilation,
                      groups=groups,
                      bias=bias), nn.init.kaiming_normal_)
    ]
    if use_bn: layer.append(batchnorm_2d(cout, norm_type=bn))
    if use_relu: layer.append(relu(True))
    return nn.Sequential(*layer)
Exemple #5
0
def sepconv2d(cin, cout=None, ksize=3, stride=1, padding=None, affine=True):
    if cout is None: cout = cin
    if padding is None: padding = ksize // 2
    layer = nn.Sequential(
        nn.ReLU(inplace=False),
        init_default(
            nn.Conv2d(cin,
                      cin,
                      ksize,
                      stride=stride,
                      padding=padding,
                      groups=cin,
                      bias=False), nn.init.kaiming_normal_),
        init_default(nn.Conv2d(cin, cin, 1, padding=0, bias=False),
                     nn.init.kaiming_normal_),
        nn.BatchNorm2d(cin, affine=affine), nn.ReLU(inplace=False),
        init_default(
            nn.Conv2d(cin,
                      cin,
                      ksize,
                      stride=1,
                      padding=padding,
                      groups=cin,
                      bias=False), nn.init.kaiming_normal_),
        init_default(nn.Conv2d(cin, cout, 1, padding=0, bias=False),
                     nn.init.kaiming_normal_),
        nn.BatchNorm2d(cout, affine=affine))
    return layer
Exemple #6
0
    def __init__(self):
        super().__init__()
        self.learn = None

        # train_button = gui.button(self.controlArea, self, "开始训练", callback=self.train)
        self.label = gui.label(self.mainArea, self, "模型结构")

        #: The current evaluating task (if any)
        self._task = None  # type: Optional[Task]
        #: An executor we use to submit learner evaluations into a thread pool
        self._executor = ThreadExecutor()

        self.model = nn.Sequential(
            self.conv(1, 8),  # 14
            nn.BatchNorm2d(8),
            nn.ReLU(),
            self.conv(8, 16),  # 7
            nn.BatchNorm2d(16),
            nn.ReLU(),
            self.conv(16, 32),  # 4
            nn.BatchNorm2d(32),
            nn.ReLU(),
            self.conv(32, 16),  # 2
            nn.BatchNorm2d(16),
            nn.ReLU(),
            self.conv(16, 10),  # 1
            nn.BatchNorm2d(10),
            Flatten()  # remove (1,1) grid
        )
Exemple #7
0
 def head_layer(self):
     cin = self.channels  #* 2
     return nn.Sequential(
         nn.AdaptiveAvgPool2d(1),  #AdaptiveConcatPool2d(1),
         #nn.Dropout2d(0.5),
         Flatten(),
         init_default(nn.Linear(cin, self.classes),
                      nn.init.kaiming_normal_))
Exemple #8
0
 def gepnet_blocks(self):
     assert len(self.comp_graph) <= 4, 'Genes in a chromosome must be <= 4'
     blocks = []
     blk_size = len(self.repeat_list)
     for blk in range(blk_size):
         cin = self.channels
         for cell in range(self.repeat_list[blk]):
             blocks.append(GepBlock(cin, self.comp_graph))
         if blk < blk_size - 1:
             cout = cin * 2
             blocks.append(conv2dpool(cin, cout, pool_type='avg'))
             self.channels = cout
     return nn.Sequential(*blocks)
Exemple #9
0
    def __init__(self,
                 block,
                 layers,
                 num_classes=10,
                 zero_init_residual=False):
        super(MyResNet, self).__init__()
        self.inplanes = 64
        self.conv1 = nn.Conv2d(1,
                               64,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)

        self.classifier = nn.Sequential(
            nn.Dropout(p=0.5),
            nn.Linear(512 * block.expansion, 256),
            nn.BatchNorm1d(256),
            nn.ReLU(inplace=True),
            nn.Dropout(p=0.5),
            nn.Linear(256, num_classes),
        )
    def __init__(self, num_classes=10):
        super(VGG, self).__init__()
        self.l1 = self.two_conv_pool(1, 64, 64)
        self.l2 = self.two_conv_pool(64, 128, 128)
        self.l3 = self.three_conv_pool(128, 256, 256, 256)
        self.l4 = self.three_conv_pool(256, 256, 256, 256)

        self.classifier = nn.Sequential(
            nn.Dropout(p=0.5),
            nn.Linear(256, 512),
            nn.BatchNorm1d(512),
            nn.ReLU(inplace=True),
            nn.Dropout(p=0.5),
            nn.Linear(512, num_classes),
        )
 def two_conv_pool(self, in_channels, f1, f2):
     s = nn.Sequential(
         nn.Conv2d(in_channels, f1, kernel_size=3, stride=1, padding=1),
         nn.BatchNorm2d(f1),
         nn.ReLU(inplace=True),
         nn.Conv2d(f1, f2, kernel_size=3, stride=1, padding=1),
         nn.BatchNorm2d(f2),
         nn.ReLU(inplace=True),
         nn.MaxPool2d(kernel_size=2, stride=2),
     )
     for m in s.children():
         if isinstance(m, nn.Conv2d):
             n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
             m.weight.data.normal_(0, math.sqrt(2. / n))
         elif isinstance(m, nn.BatchNorm2d):
             m.weight.data.fill_(1)
             m.bias.data.zero_()
     return s
Exemple #12
0
def stem_blk(cin,
             cout=None,
             ksize=3,
             stride=1,
             use_relu=True,
             use_bn=True,
             bn=NormType.Batch,
             bias=False,
             pool='avg'):
    if cout is None: cout = cin
    padding = ksize // 2
    layer = [
        init_default(
            nn.Conv2d(cin,
                      cout,
                      ksize,
                      stride=stride,
                      padding=padding,
                      bias=bias), nn.init.kaiming_normal_)
    ]
    if use_bn: layer.append(batchnorm_2d(cout, norm_type=bn))
    if use_relu: layer.append(relu(True))
    if pool == 'max': layer.append(nn.MaxPool2d(2, stride=2))
    if pool == 'avg':
        layer.append(
            nn.AvgPool2d(2, stride=2, ceil_mode=True, count_include_pad=False))

    layer.append(
        init_default(
            nn.Conv2d(cout,
                      cout * 2,
                      ksize,
                      stride=stride,
                      padding=padding,
                      bias=bias), nn.init.kaiming_normal_))
    if use_bn: layer.append(batchnorm_2d(cout * 2, norm_type=bn))
    if use_relu: layer.append(relu(True))
    if pool == 'max': layer.append(nn.MaxPool2d(2, stride=2))
    if pool == 'avg':
        layer.append(
            nn.AvgPool2d(2, stride=2, ceil_mode=True, count_include_pad=False))
    return nn.Sequential(*layer)
Exemple #13
0
    def __init__(self):
        super().__init__()
        self.learn = None

        # train_button = gui.button(self.controlArea, self, "开始训练", callback=self.train)
        self.label = gui.label(self.mainArea, self, "模型结构")

        #: The current evaluating task (if any)
        self._task = None  # type: Optional[Task]
        #: An executor we use to submit learner evaluations into a thread pool
        self._executor = ThreadExecutor()

        # Device configuration
        self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

        # Hyper parameters
        num_epochs = 5
        num_classes = 10
        batch_size = 100
        learning_rate = 0.001

        dir_path = Path(__file__).resolve()
        data_path = f'{dir_path.parent.parent.parent}/datasets/'

        # MNIST dataset
        self.train_dataset = torchvision.datasets.MNIST(root=data_path,
                                                   train=True,
                                                   transform=transforms.ToTensor(),
                                                   download=False)

        self.test_dataset = torchvision.datasets.MNIST(root=data_path,
                                                  train=False,
                                                  transform=transforms.ToTensor())

        # Data loader
        self.train_loader = torch.utils.data.DataLoader(dataset=self.train_dataset,
                                                   batch_size=batch_size,
                                                   shuffle=False)

        self.test_loader = torch.utils.data.DataLoader(dataset=self.test_dataset,
                                                  batch_size=batch_size,
                                                  shuffle=False)

        # self.model = ConvNet(num_classes).to(self.device)
        self.model = nn.Sequential(
            self.conv(1, 8),  # 14
            nn.BatchNorm2d(8),
            nn.ReLU(),
            self.conv(8, 16),  # 7
            nn.BatchNorm2d(16),
            nn.ReLU(),
            self.conv(16, 32),  # 4
            nn.BatchNorm2d(32),
            nn.ReLU(),
            self.conv(32, 16),  # 2
            nn.BatchNorm2d(16),
            nn.ReLU(),
            self.conv(16, 10),  # 1
            nn.BatchNorm2d(10),
            Flatten()  # remove (1,1) grid
        ).to(self.device)

        # Loss and optimizer
        self.criterion = nn.CrossEntropyLoss()
        self.optimizer = torch.optim.Adam(self.model.parameters(), lr=learning_rate)
Exemple #14
0
 def stem(self):
     return nn.Sequential(init_default(nn.Conv2d(3, self.channels, 3, padding=1, bias=False),
                                       nn.init.kaiming_normal_),
                          nn.BatchNorm2d(self.channels))