Exemple #1
0
def fc_orth(input_shape, num_classes, dense_classifier=False, pretrained=False, L=6, N=100, nonlinearity=nn.ReLU()):
  def _orthogonal_init(m):
    if (type(m) == Layers.layers.Linear):
      torch.nn.init.orthogonal_(m.weight)

  size = np.prod(input_shape)

  # Linear feature extractor
  modules = [nn.Flatten()]
  modules.append(layers.Linear(size, N))
  modules.append(nonlinearity)
  for i in range(L - 2):
    modules.append(layers.Linear(N, N))
    modules.append(nonlinearity)

  # Linear classifier
  if dense_classifier:
    modules.append(nn.Linear(N, num_classes))
  else:
    modules.append(layers.Linear(N, num_classes))
  model = nn.Sequential(*modules)

  model.apply(_orthogonal_init)

  # Pretrained model
  if pretrained:
    print("WARNING: this model does not have pretrained weights.")

  return model
Exemple #2
0
def fc(input_shape,
       num_classes,
       dense_classifier=False,
       pretrained=False,
       L=5,
       N=256,
       nonlinearity=nn.ReLU()):
    size = np.prod(input_shape)

    # Linear feature extractor
    modules = [nn.Flatten()]
    modules.append(layers.Linear(size, N))
    modules.append(nonlinearity)
    for i in range(L - 2):
        modules.append(layers.Linear(N, N))
        modules.append(nonlinearity)

    # Linear classifier
    if dense_classifier:
        modules.append(nn.Linear(N, num_classes))
    else:
        modules.append(layers.Linear(N, num_classes))
    model = nn.Sequential(*modules)

    # Pretrained model
    if pretrained:
        print("WARNING: this model does not have pretrained weights.")

    return model
 def __init__(self, features, num_classes=1000, init_weights=True):
     super(VGG, self).__init__()
     self.features = features
     self.avgpool = nn.AdaptiveAvgPool2d((7, 7))
     self.classifier = nn.Sequential(
         layers.Linear(512 * 7 * 7, 4096),
         nn.ReLU(True),
         nn.Dropout(),
         layers.Linear(4096, 4096),
         nn.ReLU(True),
         nn.Dropout(),
         layers.Linear(4096, num_classes),
     )
     if init_weights:
         self._initialize_weights()
Exemple #4
0
def conv(input_shape,
         num_classes,
         dense_classifier=False,
         pretrained=False,
         L=3,
         N=32,
         nonlinearity=nn.ReLU()):
    channels, width, height = input_shape

    # Convolutional feature extractor
    modules = []
    modules.append(layers.Conv2d(channels, N, kernel_size=3, padding=3 // 2))
    modules.append(nonlinearity)
    for i in range(L - 2):
        modules.append(layers.Conv2d(N, N, kernel_size=3, padding=3 // 2))
        modules.append(nonlinearity)

    # Linear classifier
    modules.append(nn.Flatten())
    if dense_classifier:
        modules.append(nn.Linear(N * width * height, num_classes))
    else:
        modules.append(layers.Linear(N * width * height, num_classes))
    model = nn.Sequential(*modules)

    # Pretrained model
    if pretrained:
        print("WARNING: this model does not have pretrained weights.")

    return model
Exemple #5
0
    def __init__(self, plan, num_classes, dense_classifier):
        super(ResNet, self).__init__()

        # Initial convolution.
        current_filters = plan[0][0]
        self.conv = layers.Conv2d(
            3, current_filters, kernel_size=3, stride=1, padding=1, bias=False
        )
        self.bn = layers.BatchNorm2d(current_filters)

        # The subsequent blocks of the ResNet.
        blocks = []
        for segment_index, (filters, num_blocks) in enumerate(plan):
            for block_index in range(num_blocks):
                downsample = segment_index > 0 and block_index == 0
                blocks.append(Block(current_filters, filters, downsample))
                current_filters = filters

        self.blocks = nn.Sequential(*blocks)

        self.fc = layers.Linear(plan[-1][0], num_classes)
        if dense_classifier:
            self.fc = nn.Linear(plan[-1][0], num_classes)

        self._initialize_weights()
Exemple #6
0
    def __init__(self,
                 plan,
                 conv,
                 num_classes=10,
                 dense_classifier=False,
                 path=False):
        super(VGG, self).__init__()
        layer_list = []
        filters = 3

        self.path = path

        for spec in plan:
            if spec == 'M':
                if self.path:
                    layer_list.append(nn.LPPool2d(1.0, kernel_size=2,
                                                  stride=2))
                else:
                    layer_list.append(nn.MaxPool2d(kernel_size=2, stride=2))
            else:
                layer_list.append(conv(filters, spec))
                filters = spec

        self.layers = nn.Sequential(*layer_list)

        self.fc = layers.Linear(512, num_classes)
        if dense_classifier:
            self.fc = nn.Linear(512, num_classes)

        self._initialize_weights()
Exemple #7
0
    def __init__(self,
                 block,
                 num_block,
                 base_width,
                 num_classes=200,
                 dense_classifier=False):
        super().__init__()

        self.in_channels = 64

        self.conv1 = nn.Sequential(
            layers.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
            layers.BatchNorm2d(64), nn.ReLU(inplace=True))
        #we use a different inputsize than the original paper
        #so conv2_x's stride is 1
        self.conv2_x = self._make_layer(block, 64, num_block[0], 1, base_width)
        self.conv3_x = self._make_layer(block, 128, num_block[1], 2,
                                        base_width)
        self.conv4_x = self._make_layer(block, 256, num_block[2], 2,
                                        base_width)
        self.conv5_x = self._make_layer(block, 512, num_block[3], 2,
                                        base_width)
        self.avg_pool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = layers.Linear(512 * block.expansion, num_classes)
        if dense_classifier:
            self.fc = nn.Linear(512 * block.expansion, num_classes)
        self._initialize_weights()
    def __init__(self,
                 block,
                 layer_list,
                 num_classes=1000,
                 zero_init_residual=False,
                 groups=1,
                 width_per_group=64,
                 replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResNet, self).__init__()
        if norm_layer is None:
            norm_layer = layers.BatchNorm2d
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(
                                 replace_stride_with_dilation))
        self.groups = groups
        self.base_width = width_per_group
        self.conv1 = layers.Conv2d(3,
                                   self.inplanes,
                                   kernel_size=7,
                                   stride=2,
                                   padding=3,
                                   bias=False)
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layer_list[0])
        self.layer2 = self._make_layer(block,
                                       128,
                                       layer_list[1],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block,
                                       256,
                                       layer_list[2],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(block,
                                       512,
                                       layer_list[3],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[2])
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = layers.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, layers.Conv2d):
                nn.init.kaiming_normal_(m.weight,
                                        mode='fan_out',
                                        nonlinearity='relu')
            elif isinstance(m, (layers.BatchNorm2d, nn.GroupNorm)):
                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)