コード例 #1
0
    def __init__(self,
                 width,
                 height,
                 spec_conv_layers,
                 spec_max_pooling,
                 spec_linear,
                 spec_dropout_rates,
                 useBatchNorm=False,
                 useAffineTransformInBatchNorm=False,
                 num_channels_input=3,
                 num_outputs=10):
        '''
        The structure of the network is: a number of convolutional layers, intermittend max-pooling and dropout layers,
        and a number of linear layers. The max-pooling layers are inserted in the positions specified, as do the dropout
        layers.

        :param spec_conv_layers: list of tuples with (numFilters, width, height) (one tuple for each layer);
        :param spec_max_pooling: list of tuples with (posToInsert, width, height) of max-pooling layers
        :param spec_dropout_rates list of tuples with (posToInsert, rate of dropout) (applied after max-pooling)
        :param spec_linear: list with numNeurons for each layer (i.e. [100, 200, 300] creates 3 layers)
        '''

        super(ConvolForwardNet, self).__init__()

        self.width = width
        self.height = height
        self.conv_layers = []
        self.max_pooling_layers = []
        self.dropout_layers = []
        self.linear_layers = []
        self.max_pooling_positions = []
        self.dropout_positions = []
        self.useBatchNorm = useBatchNorm
        self.batchNormalizationLayers = []

        #creating the convolutional layers
        oldNumChannels = num_channels_input
        for idx in range(len(spec_conv_layers)):
            currSpecLayer = spec_conv_layers[idx]
            numFilters = currSpecLayer[0]
            kernel_size = (currSpecLayer[1], currSpecLayer[2])
            #The padding needs to be such that width and height of the image are unchanges after each conv layer
            padding = ((kernel_size[0] - 1) // 2, (kernel_size[1] - 1) // 2)
            newConvLayer = nn.Conv2d(in_channels=oldNumChannels,
                                     out_channels=numFilters,
                                     kernel_size=kernel_size,
                                     padding=padding)
            xavier_uniform(
                newConvLayer.weight,
                calculate_gain('conv2d'))  #glorot weight initialization
            self.conv_layers.append(newConvLayer)
            self.batchNormalizationLayers.append(
                nn.BatchNorm2d(numFilters,
                               affine=useAffineTransformInBatchNorm))
            oldNumChannels = numFilters

        #creating the max pooling layers
        for idx in range(len(spec_max_pooling)):
            currSpecLayer = spec_max_pooling[idx]
            kernel_size = (currSpecLayer[1], currSpecLayer[2])
            self.max_pooling_layers.append(nn.MaxPool2d(kernel_size))
            self.max_pooling_positions.append(currSpecLayer[0])

        #creating the dropout layers
        for idx in range(len(spec_dropout_rates)):
            currSpecLayer = spec_dropout_rates[idx]
            rate = currSpecLayer[1]
            currPosition = currSpecLayer[0]
            if currPosition < len(self.conv_layers):
                #we use dropout2d only for the conv_layers, otherwise we use the usual dropout
                self.dropout_layers.append(nn.Dropout2d(rate))
            else:
                self.dropout_layers.append(nn.Dropout(rate))
            self.dropout_positions.append(currPosition)

        #creating the linear layers
        oldInputFeatures = oldNumChannels * width * height // 2**(
            2 * len(self.max_pooling_layers))
        for idx in range(len(spec_linear)):
            currNumFeatures = spec_linear[idx]
            newLinearLayer = nn.Linear(in_features=oldInputFeatures,
                                       out_features=currNumFeatures)
            xavier_uniform(
                newLinearLayer.weight,
                calculate_gain('linear'))  # glorot weight initialization
            self.linear_layers.append(newLinearLayer)
            self.batchNormalizationLayers.append(
                nn.BatchNorm1d(currNumFeatures,
                               affine=useAffineTransformInBatchNorm))
            oldInputFeatures = currNumFeatures

        #final output layer
        self.out_layer = nn.Linear(in_features=oldInputFeatures,
                                   out_features=num_outputs)
        xavier_uniform(self.out_layer.weight, calculate_gain('linear'))

        self.conv_layers = nn.ModuleList(self.conv_layers)
        self.max_pooling_layers = nn.ModuleList(self.max_pooling_layers)
        self.dropout_layers = nn.ModuleList(self.dropout_layers)
        self.linear_layers = nn.ModuleList(self.linear_layers)
        self.batchNormalizationLayers = nn.ModuleList(
            self.batchNormalizationLayers)
        self.num_conv_layers = len(self.conv_layers)
        self.total_num_layers = self.num_conv_layers + len(self.linear_layers)
コード例 #2
0
ファイル: test_mnist.py プロジェクト: JoeHolt/adadamp
 def __init__(self):
     super(Net, self).__init__()
     self.conv1 = nn.Conv2d(1, 32, 3, 1)
     self.dropout1 = nn.Dropout2d(0.25)
     self.fc1 = nn.Linear(32 * 26 * 26, 10)
コード例 #3
0
    def __init__(self, args_dict=scalogram_encoder_default_dict):
        super().__init__()
        self.num_layers = len(args_dict['kernel_sizes'])

        self.cqt = CQT(sr=args_dict['sample_rate'],
                       fmin=args_dict['fmin'],
                       n_bins=args_dict['n_bins'],
                       bins_per_octave=args_dict['bins_per_octave'],
                       filter_scale=args_dict['filter_scale'],
                       hop_length=args_dict['hop_length'],
                       trainable=args_dict['trainable_cqt'])

        self.phase = args_dict['phase']
        if self.phase:
            args_dict['channel_count'][0] = 2
            self.phase_diff = PhaseDifference(
                sr=args_dict['sample_rate'],
                fmin=args_dict['fmin'],
                n_bins=args_dict['n_bins'],
                bins_per_octave=args_dict['bins_per_octave'],
                hop_length=args_dict['hop_length'])
        else:
            args_dict['channel_count'][0] = 1

        self.module_list = nn.ModuleList()

        for l in range(self.num_layers):
            if args_dict['top_padding'][l] > 0:
                self.module_list.add_module(
                    'pad_' + str(l),
                    nn.ZeroPad2d((0, 0, args_dict['top_padding'][l], 0)))

            if l > 0 and args_dict['separable']:
                self.module_list.add_module(
                    'conv_' + str(l),
                    Conv2dSeparable(in_channels=args_dict['channel_count'][l],
                                    out_channels=args_dict['channel_count'][l +
                                                                            1],
                                    kernel_size=args_dict['kernel_sizes'][l],
                                    bias=args_dict['bias'],
                                    stride=args_dict['stride'][l]))
            else:
                bias = False
                if args_dict['kernel_sizes'][l][1] > 1:
                    bias = args_dict['bias']
                self.module_list.add_module(
                    'conv_' + str(l),
                    nn.Conv2d(in_channels=args_dict['channel_count'][l],
                              out_channels=args_dict['channel_count'][l + 1],
                              kernel_size=args_dict['kernel_sizes'][l],
                              bias=bias,
                              stride=args_dict['stride'][l]))

            if args_dict['lowpass_init'] > 0 and args_dict['kernel_sizes'][l][
                    1] == 1:
                lowpass_init(
                    list(self.module_list)[-1].weight,
                    args_dict['lowpass_init'])

            if args_dict['pooling'][l] > 1:
                self.module_list.add_module(
                    'pooling_' + str(l),
                    nn.MaxPool2d(kernel_size=args_dict['pooling'][l]))

            if l < self.num_layers - 1:
                self.module_list.add_module('relu_' + str(l), nn.ReLU())

                if args_dict['dropout'] > 0.:
                    self.module_list.add_module(
                        'dropout_' + str(l),
                        nn.Dropout2d(args_dict['dropout']))

                if args_dict['batch_norm']:
                    self.module_list.add_module(
                        'batch_norm_' + str(l),
                        nn.BatchNorm2d(
                            num_features=args_dict['channel_count'][l + 1]))

                if args_dict['instance_norm']:
                    self.module_list.add_module(
                        'instance_norm_' + str(l),
                        nn.InstanceNorm2d(
                            num_features=args_dict['channel_count'][l + 1],
                            affine=True,
                            track_running_stats=True))

        self.receptive_field = self.cqt.conv_kernel_sizes[0]
        s = args_dict['hop_length']
        for i in range(self.num_layers):
            self.receptive_field += (args_dict['kernel_sizes'][i][1] - 1) * s
            s *= args_dict['pooling'][i] * args_dict['stride'][i]

        self.downsampling_factor = args_dict['hop_length'] * np.prod(
            args_dict['pooling']) * np.prod(args_dict['stride'])
コード例 #4
0
 def __init__(self, cfg, **kwargs):
     super(EMANet, self).__init__(cfg, **kwargs)
     align_corners, norm_cfg, act_cfg = self.align_corners, self.norm_cfg, self.act_cfg
     # build the EMA module
     ema_cfg = cfg['ema']
     self.ema_in_conv = nn.Sequential(
         nn.Conv2d(ema_cfg['in_channels'],
                   ema_cfg['ema_channels'],
                   kernel_size=3,
                   stride=1,
                   padding=1,
                   bias=False),
         BuildNormalization(norm_cfg['type'],
                            (ema_cfg['ema_channels'], norm_cfg['opts'])),
         BuildActivation(act_cfg['type'], **act_cfg['opts']),
     )
     self.ema_mid_conv = nn.Conv2d(ema_cfg['ema_channels'],
                                   ema_cfg['ema_channels'],
                                   kernel_size=1,
                                   stride=1,
                                   padding=0)
     for param in self.ema_mid_conv.parameters():
         param.requires_grad = False
     self.ema_module = EMAModule(channels=ema_cfg['ema_channels'],
                                 num_bases=ema_cfg['num_bases'],
                                 num_stages=ema_cfg['num_stages'],
                                 momentum=ema_cfg['momentum'])
     self.ema_out_conv = nn.Sequential(
         nn.Conv2d(ema_cfg['ema_channels'],
                   ema_cfg['ema_channels'],
                   kernel_size=1,
                   stride=1,
                   padding=0,
                   bias=False),
         BuildNormalization(norm_cfg['type'],
                            (ema_cfg['ema_channels'], norm_cfg['opts'])),
     )
     self.bottleneck = nn.Sequential(
         nn.Conv2d(ema_cfg['ema_channels'],
                   ema_cfg['ema_channels'],
                   kernel_size=3,
                   stride=1,
                   padding=1,
                   bias=False),
         BuildNormalization(norm_cfg['type'],
                            (ema_cfg['ema_channels'], norm_cfg['opts'])),
         BuildActivation(act_cfg['type'], **act_cfg['opts']),
     )
     # build decoder
     decoder_cfg = cfg['decoder']
     self.decoder = nn.Sequential(
         nn.Conv2d(decoder_cfg['in_channels'],
                   decoder_cfg['out_channels'],
                   kernel_size=3,
                   stride=1,
                   padding=1,
                   bias=False),
         BuildNormalization(
             norm_cfg['type'],
             (decoder_cfg['out_channels'], norm_cfg['opts'])),
         BuildActivation(act_cfg['type'], **act_cfg['opts']),
         nn.Dropout2d(decoder_cfg['dropout']),
         nn.Conv2d(decoder_cfg['out_channels'],
                   cfg['num_classes'],
                   kernel_size=1,
                   stride=1,
                   padding=0))
     # build auxiliary decoder
     self.setauxiliarydecoder(cfg['auxiliary'])
     # freeze normalization layer if necessary
     if cfg.get('is_freeze_norm', False): self.freezenormalization()
コード例 #5
0
    def __init__(self, params):
        super(Backbone, self).__init__()
        self.use_range = params["input_depth"]["range"]
        self.use_xyz = params["input_depth"]["xyz"]
        self.use_remission = params["input_depth"]["remission"]
        self.drop_prob = params["dropout"]
        self.bn_d = params["bn_d"]
        self.OS = params["OS"]
        self.layers = params["extra"]["layers"]
        print("Using squeezesegv3" + str(self.layers) + " Backbone")
        self.input_depth = 0
        self.input_idxs = []
        if self.use_range:
            self.input_depth += 1
            self.input_idxs.append(0)
        if self.use_xyz:
            self.input_depth += 3
            self.input_idxs.extend([1, 2, 3])
        if self.use_remission:
            self.input_depth += 1
            self.input_idxs.append(4)
        print("Depth of backbone input = ", self.input_depth)

        self.strides = [2, 2, 2, 1, 1]

        current_os = 1
        for s in self.strides:
            current_os *= s
        print("Original OS: ", current_os)

        if self.OS > current_os:
            print("Can't do OS, ", self.OS,
                  " because it is bigger than original ", current_os)
        else:

            for i, stride in enumerate(reversed(self.strides), 0):
                if int(current_os) != self.OS:
                    if stride == 2:
                        current_os /= 2
                        self.strides[-1 - i] = 1
                    if int(current_os) == self.OS:
                        break
            print("New OS: ", int(current_os))
            print("Strides: ", self.strides)

        assert self.layers in model_blocks.keys()

        self.blocks = model_blocks[self.layers]

        self.conv1 = nn.Conv2d(self.input_depth,
                               32,
                               kernel_size=3,
                               stride=1,
                               padding=1,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(32, momentum=self.bn_d)
        self.relu1 = nn.LeakyReLU(0.1)

        self.enc1 = self._make_enc_layer(SACBlock, [32, 64],
                                         self.blocks[0],
                                         stride=self.strides[0],
                                         DS=True,
                                         bn_d=self.bn_d)
        self.enc2 = self._make_enc_layer(SACBlock, [64, 128],
                                         self.blocks[1],
                                         stride=self.strides[1],
                                         DS=True,
                                         bn_d=self.bn_d)
        self.enc3 = self._make_enc_layer(SACBlock, [128, 256],
                                         self.blocks[2],
                                         stride=self.strides[2],
                                         DS=True,
                                         bn_d=self.bn_d)
        self.enc4 = self._make_enc_layer(SACBlock, [256, 256],
                                         self.blocks[3],
                                         stride=self.strides[3],
                                         DS=False,
                                         bn_d=self.bn_d)
        self.enc5 = self._make_enc_layer(SACBlock, [256, 256],
                                         self.blocks[4],
                                         stride=self.strides[4],
                                         DS=False,
                                         bn_d=self.bn_d)

        self.dropout = nn.Dropout2d(self.drop_prob)

        self.last_channels = 256
コード例 #6
0
  def __init__(self, params):
    super(Backbone, self).__init__()
    self.use_range = params["input_depth"]["range"]
    self.use_xyz = params["input_depth"]["xyz"]
    self.use_remission = params["input_depth"]["remission"]
    self.use_normals = params["input_depth"]["normals"]
    self.use_curvature = params["input_depth"]["curvature"]
    self.drop_prob = params["dropout"]
    self.bn_d = params["bn_d"]
    self.OS = params["OS"]
    self.layers = params["extra"]["layers"]
    print("Using DarknetNet" + str(self.layers) + " Backbone")

    # input depth calc
    self.input_depth = 0
    self.input_idxs = []
    if self.use_range:
      self.input_depth += 1
      self.input_idxs.append(0)
    if self.use_xyz:
      self.input_depth += 3
      self.input_idxs.extend([1, 2, 3])
    if self.use_remission:
      self.input_depth += 1
      self.input_idxs.append(4)
    if self.use_normals:
      self.input_depth += 3
      self.input_idxs.extend([5,6,7])
    if self.use_curvature:
      self.input_depth += 1
      self.input_idxs.append(8)
    print("Depth of backbone input = ", self.input_depth)

    # stride play
    self.strides = [2, 2, 2, 2, 2]
    # check current stride
    current_os = 1
    for s in self.strides:
      current_os *= s
    print("Original OS: ", current_os)

    # make the new stride
    if self.OS > current_os:
      print("Can't do OS, ", self.OS,
            " because it is bigger than original ", current_os)
    else:
      # redo strides according to needed stride
      for i, stride in enumerate(reversed(self.strides), 0):
        if int(current_os) != self.OS:
          if stride == 2:
            current_os /= 2
            self.strides[-1 - i] = 1
          if int(current_os) == self.OS:
            break
      print("New OS: ", int(current_os))
      print("Strides: ", self.strides)

    # check that darknet exists
    assert self.layers in model_blocks.keys()

    # generate layers depending on darknet type
    self.blocks = model_blocks[self.layers]

    # input layer
    self.conv1 = nn.Conv2d(self.input_depth, 32, kernel_size=3,
                           stride=1, padding=1, bias=False)
    self.bn1 = nn.BatchNorm2d(32, momentum=self.bn_d)
    self.relu1 = nn.LeakyReLU(0.1)

    # encoder
    self.enc1 = self._make_enc_layer(BasicBlock, [32, 64], self.blocks[0],
                                     stride=self.strides[0], bn_d=self.bn_d)
    self.enc2 = self._make_enc_layer(BasicBlock, [64, 128], self.blocks[1],
                                     stride=self.strides[1], bn_d=self.bn_d)
    self.enc3 = self._make_enc_layer(BasicBlock, [128, 256], self.blocks[2],
                                     stride=self.strides[2], bn_d=self.bn_d)
    self.enc4 = self._make_enc_layer(BasicBlock, [256, 512], self.blocks[3],
                                     stride=self.strides[3], bn_d=self.bn_d)
    self.enc5 = self._make_enc_layer(BasicBlock, [512, 1024], self.blocks[4],
                                     stride=self.strides[4], bn_d=self.bn_d)

    # for a bit of fun
    self.dropout = nn.Dropout2d(self.drop_prob)

    # last channels
    self.last_channels = 1024
コード例 #7
0
    def __init__(
        self,
        encoder_channels=[512, 256, 128, 64],
        pyramid_channels=256,
        segmentation_channels=128,
        final_channels=1,
        dropout=0.2,
    ):
        super().__init__()
        self.base_model = models.resnet18(pretrained=True)
        self.base_layers = list(self.base_model.children())
        # ==> encoder layers
        self.layer_down0 = nn.Sequential(
            *self.base_layers[:3])  # size=(N, 64, x.H/2, x.W/2)
        self.layer_down1 = nn.Sequential(
            *self.base_layers[3:5])  # size=(N, 64, x.H/4, x.W/4)
        self.layer_down2 = self.base_layers[5]  # size=(N, 128, x.H/8, x.W/8)
        self.layer_down3 = self.base_layers[6]  # size=(N, 256, x.H/16, x.W/16)
        self.layer_down4 = self.base_layers[7]  # size=(N, 512, x.H/32, x.W/32)

        self.conv1 = nn.Conv2d(encoder_channels[0],
                               pyramid_channels,
                               kernel_size=(1, 1))

        # ==> DS module
        self.d5 = DSBlock(encoder_channels[0], final_channels)
        self.d4 = DSBlock(encoder_channels[1], final_channels)
        self.d3 = DSBlock(encoder_channels[2], final_channels)
        self.d2 = DSBlock(encoder_channels[3], final_channels)

        self.p4 = FPNBlock(pyramid_channels, encoder_channels[1])
        self.p3 = FPNBlock(pyramid_channels, encoder_channels[2])
        self.p2 = FPNBlock(pyramid_channels, encoder_channels[3])

        self.s5 = SegmentationBlock(pyramid_channels,
                                    segmentation_channels,
                                    n_upsamples=3)
        self.s4 = SegmentationBlock(pyramid_channels,
                                    segmentation_channels,
                                    n_upsamples=2)
        self.s3 = SegmentationBlock(pyramid_channels,
                                    segmentation_channels,
                                    n_upsamples=1)
        self.s2 = SegmentationBlock(pyramid_channels,
                                    segmentation_channels,
                                    n_upsamples=0)

        # ==> DS module, fnd
        self.fnd_s5 = SegmentationBlock(encoder_channels[0],
                                        segmentation_channels,
                                        n_upsamples=3)
        self.fnd_s4 = SegmentationBlock(encoder_channels[1],
                                        segmentation_channels,
                                        n_upsamples=2)
        self.fnd_s3 = SegmentationBlock(encoder_channels[2],
                                        segmentation_channels,
                                        n_upsamples=1)
        self.fnd_s2 = SegmentationBlock(encoder_channels[3],
                                        segmentation_channels,
                                        n_upsamples=0)

        # ==> DS module, fpd
        self.fpd_s5 = SegmentationBlock(encoder_channels[0],
                                        segmentation_channels,
                                        n_upsamples=3)
        self.fpd_s4 = SegmentationBlock(encoder_channels[1],
                                        segmentation_channels,
                                        n_upsamples=2)
        self.fpd_s3 = SegmentationBlock(encoder_channels[2],
                                        segmentation_channels,
                                        n_upsamples=1)
        self.fpd_s2 = SegmentationBlock(encoder_channels[3],
                                        segmentation_channels,
                                        n_upsamples=0)

        self.dropout = nn.Dropout2d(p=dropout, inplace=True)
        self.final_conv = nn.Conv2d(segmentation_channels,
                                    final_channels,
                                    kernel_size=1,
                                    padding=0)

        # ==> DS module, final
        self.dropout_fnd = nn.Dropout2d(p=dropout, inplace=True)
        self.dropout_fpd = nn.Dropout2d(p=dropout, inplace=True)
        self.final_conv_fnd = nn.Conv2d(segmentation_channels,
                                        final_channels,
                                        kernel_size=1,
                                        padding=0)
        self.final_conv_fpd = nn.Conv2d(segmentation_channels,
                                        final_channels,
                                        kernel_size=1,
                                        padding=0)

        self.initialize()
コード例 #8
0
ファイル: Model.py プロジェクト: singyaowu/ML2019SPRING
    def __init__(self):
        super(MyCNN, self).__init__()
        self.conv1 = nn.Sequential(           
            nn.Conv2d(in_channels=1,out_channels=64,
                kernel_size=3,stride=1,padding=1,), 
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.Dropout2d(0.05),
            nn.Conv2d(in_channels=64,out_channels=64,
            kernel_size=5,stride=1,padding=2,),    
            nn.ReLU(),
            nn.BatchNorm2d(64),
            nn.MaxPool2d(kernel_size=2),                 # output shape(16, 22, 22)
            nn.BatchNorm2d(64),
            nn.Dropout2d(0.05),

            nn.Conv2d(in_channels=64,out_channels=128,
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(128),
            nn.Dropout2d(0.1),
            nn.Conv2d(in_channels=128,out_channels=128, 
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.Dropout2d(0.08),
            nn.BatchNorm2d(128),
            nn.MaxPool2d(kernel_size=2),                 # output shape(32, 10, 10)
            nn.BatchNorm2d(128),
            nn.Dropout2d(0.05),

            nn.Conv2d(in_channels=128,out_channels=256, 
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(256),
            nn.Dropout2d(0.08),
            #nn.Conv2d(in_channels=256,out_channels=256,   
            #    kernel_size=3,stride=1,padding=1,),
            #nn.ReLU(),
            #nn.BatchNorm2d(256),
            #nn.Dropout2d(0.1),
            nn.Conv2d(in_channels=256,out_channels=256,  
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(256),
            nn.MaxPool2d(kernel_size=2),                 # output shape(32, 4, 4)
            nn.BatchNorm2d(256),
            nn.Dropout2d(0.05),

            nn.Conv2d(in_channels=256,out_channels=512,  
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.Dropout2d(0.05),
            #nn.Conv2d(in_channels=512,out_channels=512,   # output shape(32, 8, 8)
            #    kernel_size=3,stride=1,padding=1,),
            #nn.ReLU(),
            #nn.BatchNorm2d(512),
            #nn.Dropout2d(0.1),
            nn.Conv2d(in_channels=512,out_channels=512,   # output shape(32, 8, 8)
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.MaxPool2d(kernel_size=2),                 # output shape(32, 4, 4)
            nn.BatchNorm2d(512),
            nn.Dropout2d(0.05),

            nn.Conv2d(in_channels=512,out_channels=512,   # output shape(32, 8, 8)
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.Dropout2d(0.05),
            #nn.Conv2d(in_channels=512,out_channels=512,   # output shape(32, 8, 8)
            #    kernel_size=3,stride=1,padding=1,),
            #nn.ReLU(),
            #nn.BatchNorm2d(512),
            #nn.Dropout2d(0.1),
            nn.Conv2d(in_channels=512,out_channels=512,   # output shape(32, 8, 8)
                kernel_size=3,stride=1,padding=1,),
            nn.ReLU(),
            nn.BatchNorm2d(512),
            nn.MaxPool2d(kernel_size=2),                 # output shape(32, 4, 4)
            nn.BatchNorm2d(512),
            nn.Dropout2d(0.5),
        )
        self.fc = nn.Sequential(
            nn.Linear(512 * 1 * 1, 1000),
            nn.ReLU(),
            nn.BatchNorm1d(1000),
            nn.Dropout(0.5),
            nn.Linear(1000, 7),
        )
コード例 #9
0
ファイル: beat-sota.py プロジェクト: CatLassie/BeatChord
    def __init__(self):
        super(BeatNet, self).__init__()

        self.l1 = nn.Sequential(
            nn.Conv2d(cnn_in_size,
                      cnn_h1_size,
                      cnn_k_1_size,
                      padding=cnn_padding), nn.BatchNorm2d(cnn_h1_size),
            nn.ELU(), nn.MaxPool2d(kernel_size=cnn_max_pool_k_size),
            nn.Dropout2d(p=cnn_dropout_rate))

        self.l2 = nn.Sequential(
            nn.Conv2d(cnn_h1_size,
                      cnn_h2_size,
                      cnn_k_1_size,
                      padding=cnn_padding), nn.BatchNorm2d(cnn_h2_size),
            nn.ELU(), nn.MaxPool2d(kernel_size=cnn_max_pool_k_size),
            nn.Dropout2d(p=cnn_dropout_rate))

        self.l2b = nn.Sequential(
            nn.Conv2d(cnn_h2_size,
                      cnn_h3_size,
                      cnn_k_1_size,
                      padding=cnn_padding), nn.BatchNorm2d(cnn_h3_size),
            nn.ELU(), nn.MaxPool2d(kernel_size=cnn_max_pool_k_size),
            nn.Dropout2d(p=cnn_dropout_rate))

        self.l3 = nn.Sequential(
            nn.Conv2d(cnn_h3_size, cnn_h3_size, cnn_k_2_size),
            # nn.BatchNorm2d(cnn_h_size), # cant use because spec is reduced to 1x1
            # NOTE: if needed try Instance normalization (InstanceNorm2d)
            nn.ELU(),
            nn.Dropout2d(p=cnn_dropout_rate))

        self.ld1 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[0],
                      dilation=tcn_dilations[0]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld2 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[1],
                      dilation=tcn_dilations[1]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld3 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[2],
                      dilation=tcn_dilations[2]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld4 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[3],
                      dilation=tcn_dilations[3]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld5 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[4],
                      dilation=tcn_dilations[4]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld6 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[5],
                      dilation=tcn_dilations[5]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld7 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[6],
                      dilation=tcn_dilations[6]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.ld8 = nn.Sequential(
            nn.Conv1d(tcn_h_size,
                      tcn_h_size,
                      tcn_k_size,
                      padding=tcn_paddings[7],
                      dilation=tcn_dilations[7]), nn.BatchNorm1d(tcn_h_size),
            nn.ELU(), nn.Dropout(p=tcn_dropout_rate))

        self.lfc = nn.Sequential(nn.Conv1d(fc_h_size, fc_out_size, fc_k_size),
                                 nn.Sigmoid())
コード例 #10
0
    def __init__(self):
        super(FCNs, self).__init__()

        # conv1
        self.conv1 = nn.Sequential(
            nn.Conv2d(3, 64, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 64, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2, ceil_mode=True)
        )

        # conv2
        self.conv2 = nn.Sequential(
            nn.Conv2d(64, 128, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(128, 128, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2, ceil_mode=True)
        )

        # conv3
        self.conv3 = nn.Sequential(
            nn.Conv2d(128, 256, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2, ceil_mode=True)
        )

        # conv4
        self.conv4 = nn.Sequential(
            nn.Conv2d(256, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2, ceil_mode=True)
        )

        # conv5
        self.conv5 = nn.Sequential(
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(512, 512, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(2, stride=2, ceil_mode=True)
        )

        # fc6
        self.fc6 = nn.Sequential(
            nn.Conv2d(512, 4096, 7),
            nn.ReLU(inplace=True),
            nn.Dropout2d()
        )

        # fc7
        self.fc7 = nn.Sequential(
            nn.Conv2d(4096, 4096, 1),
            nn.ReLU(inplace=True),
            nn.Dropout2d()
        )
        # 以下custom up8
        self.up8 = nn.Sequential(
            nn.Conv2d(4096, 512, 1),
            nn.ReLU(inplace=True),
            nn.ConvTranspose2d(512, 512, 4, stride=2, bias=False),
            nn.ReLU(inplace=True)
        )

        # up9
        self.up9 = nn.Sequential(
            nn.Conv2d(512, 256, 1),
            nn.ReLU(inplace=True),
            nn.ConvTranspose2d(256, 256, 4, stride=2, padding=1, bias=False),
            nn.ReLU(inplace=True)
        )
        # up10
        self.up10 = nn.Sequential(
            nn.Conv2d(256, 128, 1),
            nn.ReLU(inplace=True),
            nn.ConvTranspose2d(128, 128, 4, stride=2, padding=1, bias=False),
            nn.ReLU(inplace=True)
        )

        self.up11 = nn.Sequential(
            nn.Conv2d(128, 64, 1),
            nn.ReLU(inplace=True),
            nn.ConvTranspose2d(64, 64, 4, stride=2, padding=1, bias=False),
            nn.ReLU(inplace=True)
        )

        self.last = nn.Sequential(
            nn.Conv2d(64, 3, 1),
            nn.Tanh()
        )
コード例 #11
0
    def __init__(self):
        """CNN Builder."""
        super(SpinalCNN, self).__init__()

        self.conv_layer = nn.Sequential(

            # Conv Layer block 1
            nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3,
                      padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=32,
                      out_channels=64,
                      kernel_size=3,
                      padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),

            # Conv Layer block 2
            nn.Conv2d(in_channels=64,
                      out_channels=128,
                      kernel_size=3,
                      padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=128,
                      out_channels=128,
                      kernel_size=3,
                      padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Dropout2d(p=0.05),

            # Conv Layer block 3
            nn.Conv2d(in_channels=128,
                      out_channels=256,
                      kernel_size=3,
                      padding=1),
            nn.BatchNorm2d(256),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=256,
                      out_channels=256,
                      kernel_size=3,
                      padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),
        )

        self.fc_spinal_layer1 = nn.Sequential(
            nn.Dropout(p=0.1),
            nn.Linear(Half_width, layer_width),
            nn.ReLU(inplace=True),
        )
        self.fc_spinal_layer2 = nn.Sequential(
            nn.Dropout(p=0.1),
            nn.Linear(Half_width + layer_width, layer_width),
            nn.ReLU(inplace=True),
        )
        self.fc_spinal_layer3 = nn.Sequential(
            nn.Dropout(p=0.1),
            nn.Linear(Half_width + layer_width, layer_width),
            nn.ReLU(inplace=True),
        )
        self.fc_spinal_layer4 = nn.Sequential(
            nn.Dropout(p=0.1),
            nn.Linear(Half_width + layer_width, layer_width),
            nn.ReLU(inplace=True),
        )
        self.fc_out = nn.Sequential(nn.Dropout(p=0.1),
                                    nn.Linear(layer_width * 4, 100))
コード例 #12
0
    def __init__(self):
        super(deeplab_vgg, self).__init__()

        self.conv_flag = True
        self.use_residual = False

        self.conv1 = nn.Sequential(
            nn.Conv2d(in_channels=3,out_channels=64,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=64,out_channels=64,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3,stride=2,padding=1,ceil_mode=True),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(in_channels=64,out_channels=128,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=128,out_channels=128,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3,stride=2,padding=1,ceil_mode=True),
        )
        self.conv3 = nn.Sequential(

            nn.Conv2d(in_channels=128,out_channels=256,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=256,out_channels=256,kernel_size=3,stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3,stride=2,padding=1 ,ceil_mode=True),
        )

        self.conv4 = nn.Sequential(

            nn.Conv2d(in_channels=256,out_channels=512,kernel_size=3, stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=512, out_channels= 512, kernel_size=3, stride=1,padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=1,padding=1,ceil_mode=True),
        )

        self.conv5 = nn.Sequential(

            nn.Conv2d(in_channels=512,out_channels=512,kernel_size=3, dilation=2 ,stride=1,padding=2),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=512,out_channels=512, kernel_size=3, dilation=2 ,stride=1, padding=2),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels=512,out_channels=512, kernel_size=3, dilation=2, stride=1, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3 ,stride=1, padding=1,ceil_mode=True),
            nn.AvgPool2d(kernel_size=3 , stride=1, padding=1,ceil_mode=True),
        )

        self.fc6 = nn.Sequential(

            nn.Conv2d(in_channels=512,out_channels=1024,kernel_size=3,dilation=12,padding=12),
            nn.ReLU(inplace=True),
            nn.Dropout2d()
        )

        self.fc7 = nn.Sequential(

            nn.Conv2d(in_channels=1024,out_channels=1024,kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Dropout2d(),
        )
        self.score = nn.Conv2d(in_channels=1024,out_channels=21,kernel_size=1)

        self.AttentionBranch = AB.AttentionBranch(self.conv_flag,self.use_residual)

        self.WeightedContextEmedding = AB.Attention_Weighted_Context_Generation()

        self.Adaptive_Scale_Feature_Embedding = AB.Adaptive_Scale_Feature_Embedding(embedding_dim=1024,out_feature_dim=1024)
コード例 #13
0
    def __init__(self,
                 ratio=1,
                 keyPoints=68,
                 dropOutRate=0.1,
                 temperature=1.,
                 imageSize=80):
        super(GeneratorGhostConv, self).__init__()
        self.featureMaps = [int(64 // ratio), int(128 // ratio)]
        self.keyPoints = keyPoints
        self.activation = nn.LeakyReLU(0.2)
        self.dropOutRate = dropOutRate
        self.softmax = nn.Softmax(dim=2)
        self.temperature = temperature
        self.imageSize = imageSize
        self.spatialRange = torch.Tensor(list(range(0, self.imageSize)))

        self.blockE1 = nn.Sequential(GhostModule(3, self.featureMaps[0]),
                                     #             self.activation
                                     )
        self.blockE2 = nn.Sequential(
            nn.MaxPool2d(kernel_size=2),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate))
        self.blockE3 = nn.Sequential(
            nn.MaxPool2d(kernel_size=2),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate))
        self.blockE4 = nn.Sequential(
            nn.MaxPool2d(kernel_size=2),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate))
        self.blockD4 = nn.Sequential(
            nn.MaxPool2d(kernel_size=2),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Upsample(scale_factor=2))
        self.blockD3 = nn.Sequential(
            GhostModule(self.featureMaps[1], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Upsample(scale_factor=2))
        self.blockD2 = nn.Sequential(
            GhostModule(self.featureMaps[1], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Upsample(scale_factor=2))
        self.blockD1 = nn.Sequential(
            GhostModule(self.featureMaps[1], self.featureMaps[0]),
            #             self.activation,
            nn.Dropout2d(self.dropOutRate),
            GhostModule(self.featureMaps[0], self.featureMaps[0]),
            #             self.activation,
            nn.Upsample(scale_factor=2))
        self.blockT = nn.Sequential(
            GhostModule(self.featureMaps[1], self.featureMaps[1]),
            #             self.activation,
            nn.Dropout(self.dropOutRate),
            nn.Conv2d(self.featureMaps[1],
                      self.keyPoints,
                      kernel_size=1,
                      stride=1,
                      padding=0),
            nn.BatchNorm2d(self.keyPoints),
            self.activation,
            nn.Dropout(self.dropOutRate),
            nn.Conv2d(self.keyPoints,
                      self.keyPoints,
                      kernel_size=1,
                      stride=1,
                      padding=0),
            nn.BatchNorm2d(self.keyPoints),
            self.activation,
            nn.Conv2d(self.keyPoints,
                      self.keyPoints,
                      kernel_size=1,
                      stride=1,
                      padding=0),
        )
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_uniform_(m.weight)
                nn.init.constant_(m.bias, 0.)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1.)
                nn.init.constant_(m.bias, 0.)
コード例 #14
0
    def __init__(
        self,
        layers=3,
        n_feature=256,
        dropout=0.0,
        h_height=14,
        h_width=3,
        height=117,
        width=24,
        state_dimension=5,
    ):
        super(Decoder, self).__init__()
        self.layers = layers
        self.n_feature = n_feature
        self.dropout = dropout
        self.h_height = h_height
        self.h_width = h_width
        self.height = height
        self.width = width
        self.state_dimension = state_dimension

        assert self.layers == 3
        assert self.n_feature % 4 == 0
        self.feature_maps = [
            int(self.n_feature),
            int(self.n_feature / 2),
            int(self.n_feature / 4),
        ]
        self.f_decoder = nn.Sequential(
            nn.ConvTranspose2d(
                self.feature_maps[0], self.feature_maps[1], (4, 4), 2, 1
            ),
            # nn.GroupNorm(self.feature_maps[1] // GROUP_NORM_ELEMENTS, self.feature_maps[1]),
            nn.Dropout2d(p=self.dropout, inplace=True),
            nn.LeakyReLU(0.2, inplace=True),
            nn.ConvTranspose2d(
                self.feature_maps[1], self.feature_maps[2], (5, 5), 2, (0, 1),
            ),
            nn.Dropout2d(p=self.dropout, inplace=True),
            nn.LeakyReLU(0.2, inplace=True),
            nn.ConvTranspose2d(self.feature_maps[2], 3, (2, 2), 2, (0, 1)),
        )

        self.h_reducer = nn.Sequential(
            nn.Conv2d(self.n_feature, self.n_feature, 4, 2, 1),
            # nn.GroupNorm(self.n_feature // GROUP_NORM_ELEMENTS, self.n_feature),
            nn.Dropout2d(p=self.dropout, inplace=True),
            nn.LeakyReLU(0.2, inplace=True),
            nn.Conv2d(self.n_feature, self.n_feature, (4, 1), (2, 1), 0,),
            nn.Dropout2d(p=self.dropout, inplace=True),
            nn.LeakyReLU(0.2, inplace=True),
        )

        if self.state_dimension > 0:
            self.s_predictor = nn.Sequential(
                nn.Linear(2 * self.n_feature, self.n_feature),
                nn.Dropout(p=self.dropout, inplace=True),
                nn.LeakyReLU(0.2, inplace=True),
                nn.Linear(self.n_feature, self.n_feature),
                nn.Dropout(p=self.dropout, inplace=True),
                nn.LeakyReLU(0.2, inplace=True),
                nn.Linear(self.n_feature, self.state_dimension),
            )
コード例 #15
0
    def __init__(self, input_size=256):
        super(Unet, self).__init__()
        self.maxpool = nn.MaxPool2d(kernel_size=2)
        self.spatial_dropout = nn.Dropout2d(p=0.5, inplace=False)
        self.relu = nn.ReLU(inplace=False)
        self.maxpool = nn.MaxPool2d(kernel_size=2)
        self.sigmoid = nn.Sigmoid()
        self.num_groups = 1
        self.conv1_1 = DepthwiseSeparableConv(input_size, n_channels_in=3, n_channels_out=64,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_1_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=64)
        self.conv1_2 = DepthwiseSeparableConv(input_size, n_channels_in=64, n_channels_out=64,
                                              kernel_size=3, padding='same', stride=1)
        self.convmax1_2 = DepthwiseSeparableConv(input_size, n_channels_in=64, n_channels_out=64,
                                                 kernel_size=2, padding=0, stride=2)
        self.batch_norm_1_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=64)

        self.conv2_1 = DepthwiseSeparableConv(input_size / 2, n_channels_in=64, n_channels_out=128,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_2_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=128)
        self.conv2_2 = DepthwiseSeparableConv(input_size / 2, n_channels_in=128, n_channels_out=128,
                                              kernel_size=3, padding='same', stride=1)
        self.convmax2_2 = DepthwiseSeparableConv(input_size / 2, n_channels_in=128, n_channels_out=128,
                                                 kernel_size=2, padding=0, stride=2)
        self.batch_norm_2_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=128)

        self.conv3_1 = DepthwiseSeparableConv(input_size / 4, n_channels_in=128, n_channels_out=256,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_3_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=256)
        self.conv3_2 = DepthwiseSeparableConv(input_size / 4, n_channels_in=256, n_channels_out=256,
                                              kernel_size=3, padding='same', stride=1)
        self.convmax3_2 = DepthwiseSeparableConv(input_size / 4, n_channels_in=256, n_channels_out=256,
                                                 kernel_size=2, padding=0, stride=2)
        self.batch_norm_3_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=256)

        self.conv4_1 = DepthwiseSeparableConv(input_size / 8, n_channels_in=256, n_channels_out=512,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_4_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=512)
        self.conv4_2 = DepthwiseSeparableConv(input_size / 8, n_channels_in=512, n_channels_out=512,
                                              kernel_size=3, padding='same', stride=1)
        self.convmax4_2 = DepthwiseSeparableConv(input_size / 8, n_channels_in=512, n_channels_out=512,
                                                 kernel_size=2, padding=0, stride=2)
        self.batch_norm_4_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=512)

        self.conv5_1 = DepthwiseSeparableConv(input_size / 16, n_channels_in=512, n_channels_out=1024,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_5_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=1024)
        self.conv5_2 = DepthwiseSeparableConv(input_size / 16, n_channels_in=1024, n_channels_out=1024,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_5_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=1024)

        self.upconv6_1 = TransposeConv(input_size/16, n_channels_in=1024, n_channels_out=512,
                                       kernel_size=2, stride=2, padding=0)
        self.batch_norm_up6_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=512)
        self.conv6_1 = DepthwiseSeparableConv(input_size / 8, n_channels_in=512+512, n_channels_out=512,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_conv6_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=512)
        self.conv6_2 = DepthwiseSeparableConv(input_size / 8, n_channels_in=512, n_channels_out=512,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_6_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=512)

        self.upconv7_1 = TransposeConv(input_size / 8, n_channels_in=512, n_channels_out=256,
                                       kernel_size=2, stride=2,
                                       padding=0)
        self.batch_norm_up7_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=256)
        self.conv7_1 = DepthwiseSeparableConv(input_size / 4, n_channels_in=256 + 256, n_channels_out=256,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_conv7_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=256)
        self.conv7_2 = DepthwiseSeparableConv(input_size / 4, n_channels_in=256, n_channels_out=256,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_7_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=256)

        self.upconv8_1 = TransposeConv(input_size / 4, n_channels_in=256, n_channels_out=128, kernel_size=2, stride=2,
                                       padding=0)
        self.batch_norm_up8_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=128)
        self.conv8_1 = DepthwiseSeparableConv(input_size / 2, n_channels_in=128 + 128, n_channels_out=128,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_conv8_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=128)
        self.conv8_2 = DepthwiseSeparableConv(input_size / 2, n_channels_in=128, n_channels_out=128,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_8_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=128)

        self.upconv9_1 = TransposeConv(input_size / 2, n_channels_in=128, n_channels_out=64, kernel_size=2, stride=2,
                                       padding=0)
        self.batch_norm_up9_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=64)
        self.conv9_1 = DepthwiseSeparableConv(input_size, n_channels_in=64 + 64, n_channels_out=64,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_conv9_1 = nn.GroupNorm(num_groups=self.num_groups, num_channels=64)
        self.conv9_2 = DepthwiseSeparableConv(input_size, n_channels_in=64, n_channels_out=64,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_9_2 = nn.GroupNorm(num_groups=self.num_groups, num_channels=64)

        self.conv9_3 = DepthwiseSeparableConv(input_size, n_channels_in=64, n_channels_out=2,
                                              kernel_size=3, padding='same', stride=1)
        self.batch_norm_9_3 = nn.GroupNorm(num_groups=self.num_groups, num_channels=2)

        self.conv9_4 = DepthwiseSeparableConv(input_size, n_channels_in=2, n_channels_out=1,
                                              kernel_size=1, padding='same', stride=1)
コード例 #16
0
    def __init__(self,
                 block,
                 layers,
                 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 = nn.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 = nn.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, layers[0])
        self.layer2 = self._make_layer(block,
                                       128,
                                       layers[1],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block,
                                       256,
                                       layers[2],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(block,
                                       512,
                                       layers[3],
                                       stride=2,
                                       dilate=replace_stride_with_dilation[2])
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)
        self.dropout1 = nn.Dropout(0.5)
        self.dropout2_hard = nn.Dropout2d(0.5)
        self.dropout2_mid = nn.Dropout2d(0.48)
        self.dropout2_light = nn.Dropout2d(0.46)

        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.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)
コード例 #17
0
    def __init__(self, n_class=2):
        super(FCN8s, self).__init__()
        # conv1
        self.conv1_1 = nn.Conv2d(3, 64, 3, padding=100)
        self.relu1_1 = nn.ReLU(inplace=True)
        self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1)
        self.relu1_2 = nn.ReLU(inplace=True)
        self.pool1 = nn.MaxPool2d(2, stride=2, ceil_mode=True)  # 1/2

        # conv2
        self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1)
        self.relu2_1 = nn.ReLU(inplace=True)
        self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1)
        self.relu2_2 = nn.ReLU(inplace=True)
        self.pool2 = nn.MaxPool2d(2, stride=2, ceil_mode=True)  # 1/4

        # conv3
        self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1)
        self.relu3_1 = nn.ReLU(inplace=True)
        self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1)
        self.relu3_2 = nn.ReLU(inplace=True)
        self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1)
        self.relu3_3 = nn.ReLU(inplace=True)
        self.pool3 = nn.MaxPool2d(2, stride=2, ceil_mode=True)  # 1/8

        # conv4
        self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1)
        self.relu4_1 = nn.ReLU(inplace=True)
        self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1)
        self.relu4_2 = nn.ReLU(inplace=True)
        self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1)
        self.relu4_3 = nn.ReLU(inplace=True)
        self.pool4 = nn.MaxPool2d(2, stride=2, ceil_mode=True)  # 1/16

        # conv5
        self.conv5_1 = nn.Conv2d(512, 512, 3, padding=1)
        self.relu5_1 = nn.ReLU(inplace=True)
        self.conv5_2 = nn.Conv2d(512, 512, 3, padding=1)
        self.relu5_2 = nn.ReLU(inplace=True)
        self.conv5_3 = nn.Conv2d(512, 512, 3, padding=1)
        self.relu5_3 = nn.ReLU(inplace=True)
        self.pool5 = nn.MaxPool2d(2, stride=2, ceil_mode=True)  # 1/32

        # fc6
        self.fc6 = nn.Conv2d(512, 4096, 7)
        self.relu6 = nn.ReLU(inplace=True)
        self.drop6 = nn.Dropout2d()

        # fc7
        self.fc7 = nn.Conv2d(4096, 4096, 1)
        self.relu7 = nn.ReLU(inplace=True)
        self.drop7 = nn.Dropout2d()

        self.score_fr = nn.Conv2d(4096, n_class, 1)
        self.score_pool3 = nn.Conv2d(256, n_class, 1)
        self.score_pool4 = nn.Conv2d(512, n_class, 1)

        self.upscore2 = nn.ConvTranspose2d(n_class,
                                           n_class,
                                           4,
                                           stride=2,
                                           bias=False)
        self.upscore8 = nn.ConvTranspose2d(n_class,
                                           n_class,
                                           16,
                                           stride=8,
                                           bias=False)
        self.upscore_pool4 = nn.ConvTranspose2d(n_class,
                                                n_class,
                                                4,
                                                stride=2,
                                                bias=False)
コード例 #18
0
    def __init__(self, outputs, inputs):
        super(BBB3Conv3FC, self).__init__()

        self.num_classes = outputs

        self.conv1 = BBBConv2d(inputs, 32, 3, alpha_shape=(1, 1), stride=1, padding=1, bias=True, name='conv1')
        self.bn1 = nn.BatchNorm2d(32)
        self.activate1 = nn.ELU()
        self.drop1 = nn.Dropout2d(0.25)

        self.conv2 = BBBConv2d(32, 64, 5, alpha_shape=(1, 1), stride=1, padding=2, bias=True, name='conv2')
        self.bn2 = nn.BatchNorm2d(64)
        self.activate2 = nn.ELU()
        self.drop2 = nn.Dropout2d(0.25)

        self.pool1 = BBBConv2d(64, 64, 2, alpha_shape=(1, 1), stride=2, padding=0, bias=True, name='pool1')
        self.pool1_bn = nn.BatchNorm2d(64)
        self.pool1_activate = nn.ELU()
        self.pool1_drop = nn.Dropout2d(0.25)

        self.conv4 = BBBConv2d(64, 128, 3, alpha_shape=(1, 1), stride=1, padding=1, bias=True, name='conv4')
        self.bn4 = nn.BatchNorm2d(128)
        self.activate4 = nn.ELU()
        self.drop4 = nn.Dropout2d(0.25)

        self.conv5 = BBBConv2d(128, 256, 5, alpha_shape=(1, 1), stride=1, padding=2, bias=True, name='conv5')
        self.bn5 = nn.BatchNorm2d(256)
        self.activate5 = nn.ELU()
        self.drop5 = nn.Dropout2d(0.25)

        self.pool2 = BBBConv2d(256, 256, 2, alpha_shape=(1, 1), stride=2, padding=0, bias=True, name='pool2')
        self.pool2_bn = nn.BatchNorm2d(256)
        self.pool2_activate = nn.ELU()
        self.pool2_drop = nn.Dropout2d(0.25)

        self.conv7 = BBBConv2d(256, 384, 3, alpha_shape=(1, 1), stride=1, padding=1, bias=True, name='conv7')
        self.bn7 = nn.BatchNorm2d(384)
        self.activate7 = nn.ELU()
        self.drop7 = nn.Dropout2d(0.25)

        self.conv8 = BBBConv2d(384, 512, 5, alpha_shape=(1, 1), stride=1, padding=2, bias=True, name='conv8')
        self.bn8 = nn.BatchNorm2d(512)
        self.activate8 = nn.ELU()
        self.drop8 = nn.Dropout2d(0.25)

        self.pool3 = BBBConv2d(512, 512, 2, alpha_shape=(1, 1), stride=2, padding=0, bias=True, name='pool3')
        self.pool3_bn = nn.BatchNorm2d(512)
        self.pool3_activate = nn.ELU()
        self.pool3_drop = nn.Dropout2d(0.25)

        self.conv10 = BBBConv2d(512, 640, 3, alpha_shape=(1, 1), stride=1, padding=1, bias=True, name='conv10')
        self.bn10 = nn.BatchNorm2d(640)
        self.activate10 = nn.ELU()
        self.drop10 = nn.Dropout2d(0.25)

        self.conv11 = BBBConv2d(640, 768, 5, alpha_shape=(1, 1), stride=1, padding=2, bias=True, name='conv11')
        self.bn11 = nn.BatchNorm2d(768)
        self.activate11 = nn.ELU()
        self.drop11 = nn.Dropout2d(0.25)

        self.pool4 = BBBConv2d(768, 768, 2, alpha_shape=(1, 1), stride=2, padding=0, bias=True, name='pool4')
        self.pool4_bn = nn.BatchNorm2d(768)
        self.pool4_activate = nn.ELU()
        self.pool4_drop = nn.Dropout2d(0.25)

        self.flatten = FlattenLayer(2 * 8 * 768)

        self.fc1 = BBBLinear(2 * 8 * 768, 512, alpha_shape=(1, 1), bias=True, name='fc1')
        self.fc1_bn = nn.BatchNorm1d(512)
        self.fc1_activate = nn.ELU()
        self.fc1_drop = nn.Dropout(0.50)

        self.fc2 = BBBLinear(512, 256, alpha_shape=(1, 1), bias=True, name='fc2')
        self.fc2_bn = nn.BatchNorm1d(256)
        self.fc2_activate = nn.ELU()
        self.fc2_drop = nn.Dropout(0.50)

        self.fc3 = BBBLinear(256, 64, alpha_shape=(1, 1), bias=True, name='fc3')
        self.fc3_bn = nn.BatchNorm1d(64)
        self.fc3_activate = nn.ELU()
        self.fc3_drop = nn.Dropout(0.50)

        self.fc4 = BBBLinear(64, outputs, alpha_shape=(1, 1), bias=True, name='fc4')
コード例 #19
0
ファイル: ERFNet.py プロジェクト: hedgefair/box-convolutions
 def __init__(self, in_channels, out_channels, dropout_prob=0.0):
     super().__init__()
     self.conv = nn.Conv2d(in_channels, out_channels-in_channels, (3,3), 2, 1, bias=False)
     self.bn = nn.BatchNorm2d(out_channels)
     self.dropout = nn.Dropout2d(dropout_prob)
コード例 #20
0
 def __init__(self):
     super(resnet_modified_small, self).__init__()
     self.resnet = tv.models.resnet34(pretrained=False)
     self.dropout2d = nn.Dropout2d(0.2)
     #probably want linear, relu, dropout
     '''self.linear = nn.Linear(7*7*512, 1024)
コード例 #21
0
    def __init__(self, drop_rate=0.4, bn_momentum=0.1, base_num_filters=64):
        super().__init__()

        self.conv1a = nn.Conv2d(1, base_num_filters, kernel_size=3, padding=1)
        self.conv1a_bn = nn.BatchNorm2d(base_num_filters, momentum=bn_momentum)
        self.conv1a_drop = nn.Dropout2d(drop_rate)
        self.conv1b = nn.Conv2d(base_num_filters,
                                base_num_filters,
                                kernel_size=3,
                                padding=1)
        self.conv1b_bn = nn.BatchNorm2d(base_num_filters, momentum=bn_momentum)
        self.conv1b_drop = nn.Dropout2d(drop_rate)

        self.conv2a = nn.Conv2d(base_num_filters,
                                base_num_filters,
                                kernel_size=3,
                                padding=2,
                                dilation=2)
        self.conv2a_bn = nn.BatchNorm2d(base_num_filters, momentum=bn_momentum)
        self.conv2a_drop = nn.Dropout2d(drop_rate)
        self.conv2b = nn.Conv2d(base_num_filters,
                                base_num_filters,
                                kernel_size=3,
                                padding=2,
                                dilation=2)
        self.conv2b_bn = nn.BatchNorm2d(base_num_filters, momentum=bn_momentum)
        self.conv2b_drop = nn.Dropout2d(drop_rate)

        # Branch 1x1 convolution
        self.branch1a = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=1)
        self.branch1a_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch1a_drop = nn.Dropout2d(drop_rate)
        self.branch1b = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=1)
        self.branch1b_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch1b_drop = nn.Dropout2d(drop_rate)

        # Branch for 3x3 rate 6
        self.branch2a = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=6,
                                  dilation=6)
        self.branch2a_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch2a_drop = nn.Dropout2d(drop_rate)
        self.branch2b = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=6,
                                  dilation=6)
        self.branch2b_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch2b_drop = nn.Dropout2d(drop_rate)

        # Branch for 3x3 rate 12
        self.branch3a = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=12,
                                  dilation=12)
        self.branch3a_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch3a_drop = nn.Dropout2d(drop_rate)
        self.branch3b = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=12,
                                  dilation=12)
        self.branch3b_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch3b_drop = nn.Dropout2d(drop_rate)

        # Branch for 3x3 rate 18
        self.branch4a = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=18,
                                  dilation=18)
        self.branch4a_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch4a_drop = nn.Dropout2d(drop_rate)
        self.branch4b = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=18,
                                  dilation=18)
        self.branch4b_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch4b_drop = nn.Dropout2d(drop_rate)

        # Branch for 3x3 rate 24
        self.branch5a = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=24,
                                  dilation=24)
        self.branch5a_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch5a_drop = nn.Dropout2d(drop_rate)
        self.branch5b = nn.Conv2d(base_num_filters,
                                  base_num_filters,
                                  kernel_size=3,
                                  padding=24,
                                  dilation=24)
        self.branch5b_bn = nn.BatchNorm2d(base_num_filters,
                                          momentum=bn_momentum)
        self.branch5b_drop = nn.Dropout2d(drop_rate)

        self.concat_drop = nn.Dropout2d(drop_rate)
        self.concat_bn = nn.BatchNorm2d(6 * base_num_filters,
                                        momentum=bn_momentum)

        self.amort = nn.Conv2d(6 * base_num_filters,
                               base_num_filters * 2,
                               kernel_size=1)
        self.amort_bn = nn.BatchNorm2d(base_num_filters * 2,
                                       momentum=bn_momentum)
        self.amort_drop = nn.Dropout2d(drop_rate)

        self.prediction = nn.Conv2d(base_num_filters * 2, 1, kernel_size=1)
コード例 #22
0
ファイル: unetplus.py プロジェクト: ethancohen123/EDD2020
 def __init__(self, in_feat, out_feat, drop_rate=0.3):
     super(DownConv, self).__init__()
     self.conv1 = nn.Conv2d(in_feat, out_feat, kernel_size=3, padding=1)
     self.conv2 = nn.Conv2d(out_feat, out_feat, kernel_size=3, padding=1)
     self.conv1_drop = nn.Dropout2d(drop_rate)
コード例 #23
0
    def __init__(self):
        super(DualGansGenerator, self).__init__()
        nc = 3
        ngf = 64

        # Convolution layers
        # input is (nc) x 256 x 256
        self.conv1 = nn.Conv2d(nc, ngf, 4, 2, 1, bias=True)
        self.lr1 = nn.LeakyReLU(inplace=True)
        # state size. (ngf) x 128 x 128
        self.conv2 = nn.Conv2d(ngf, ngf * 2, 4, 2, 1, bias=True)
        self.bn2 = nn.BatchNorm2d(ngf * 2)
        self.lr2 = nn.LeakyReLU(inplace=True)
        # state size. (ngf*2) x 64 x 64
        self.conv3 = nn.Conv2d(ngf * 2, ngf * 4, 4, 2, 1, bias=True)
        self.bn3 = nn.BatchNorm2d(ngf * 4)
        self.lr3 = nn.LeakyReLU(inplace=True)
        # state size. (ngf*4) x 32 x 32
        self.conv4 = nn.Conv2d(ngf * 4, ngf * 8, 4, 2, 1, bias=True)
        self.bn4 = nn.BatchNorm2d(ngf * 8)
        self.lr4 = nn.LeakyReLU(inplace=True)
        # state size. (ngf*8) x 16 x 16
        self.conv5 = nn.Conv2d(ngf * 8, ngf * 8, 4, 2, 1, bias=True)
        self.bn5 = nn.BatchNorm2d(ngf * 8)
        self.lr5 = nn.LeakyReLU(inplace=True)
        # state size. (ngf*8) x 8 x 8
        self.conv6 = nn.Conv2d(ngf * 8, ngf * 8, 4, 2, 1, bias=True)
        self.bn6 = nn.BatchNorm2d(ngf * 8)
        self.lr6 = nn.LeakyReLU(inplace=True)
        # state size. (ngf*8) x 4 x 4
        self.conv7 = nn.Conv2d(ngf * 8, ngf * 8, 4, 2, 1, bias=True)
        self.bn7 = nn.BatchNorm2d(ngf * 8)
        self.lr7 = nn.LeakyReLU(inplace=True)
        # state size. (ngf*8) x 2 x 2
        self.conv8 = nn.Conv2d(ngf * 8, ngf * 8, 4, 2, 1, bias=True)
        self.bn8 = nn.BatchNorm2d(ngf * 8)
        self.r8 = nn.ReLU(inplace=True)

        # Transposed Convolutional Layers

        # input is (ngf*8) x 1 x 1
        self.tr_conv1 = nn.ConvTranspose2d(ngf * 8,
                                           ngf * 8,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn1 = nn.BatchNorm2d(ngf * 8)
        self.tr_d1 = nn.Dropout2d(p=0.5, inplace=True)
        self.tr_r1 = nn.ReLU(inplace=True)
        # state size. (ngf*8)*2 x 2 x 2
        self.tr_conv2 = nn.ConvTranspose2d((ngf * 8) * 2,
                                           ngf * 8,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn2 = nn.BatchNorm2d(ngf * 8)
        self.tr_d2 = nn.Dropout2d(p=0.5, inplace=True)
        self.tr_r2 = nn.ReLU(inplace=True)
        # state size. (ngf*8)*2 x 4 x 4
        self.tr_conv3 = nn.ConvTranspose2d((ngf * 8) * 2,
                                           ngf * 8,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn3 = nn.BatchNorm2d(ngf * 8)
        self.tr_d3 = nn.Dropout2d(p=0.5, inplace=True)
        self.tr_r3 = nn.ReLU(inplace=True)
        # state size. (ngf*8)*2 x 8 x 8
        self.tr_conv4 = nn.ConvTranspose2d((ngf * 8) * 2,
                                           ngf * 8,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn4 = nn.BatchNorm2d(ngf * 8)
        self.tr_r4 = nn.ReLU(inplace=True)
        # state size. (ngf*8)*2 x 16 x 16
        self.tr_conv5 = nn.ConvTranspose2d((ngf * 8) * 2,
                                           ngf * 4,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn5 = nn.BatchNorm2d(ngf * 4)
        self.tr_r5 = nn.ReLU(inplace=True)
        # state size. (ngf*4)*2 x 32 x 32
        self.tr_conv6 = nn.ConvTranspose2d((ngf * 4) * 2,
                                           ngf * 2,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn6 = nn.BatchNorm2d(ngf * 2)
        self.tr_r6 = nn.ReLU(inplace=True)
        # state size. (ngf*2)*2 x 64 x 64
        self.tr_conv7 = nn.ConvTranspose2d((ngf * 2) * 2,
                                           ngf,
                                           4,
                                           2,
                                           1,
                                           bias=True)
        self.tr_bn7 = nn.BatchNorm2d(ngf)
        self.tr_r7 = nn.ReLU(inplace=True)
        # state size. (ngf)*2 x 128 x 128
        self.tr_conv8 = nn.ConvTranspose2d((ngf) * 2, nc, 4, 2, 1, bias=True)
        self.out = nn.Tanh()
コード例 #24
0
    def __init__(self, hyper) -> None:
        super(MultiHeadSelection, self).__init__()
        self.device = torch.device('cpu')
        self.hyper = hyper
        self.data_root = hyper.data_root
        self.gpu = hyper.gpu
        self.word_vocab = json.load(
            open(os.path.join(self.data_root, 'word_vocab.json'), 'r'))
        self.relation_vocab = json.load(
            open(os.path.join(self.data_root, 'relation_vocab.json'), 'r'))
        self.bio_vocab = json.load(
            open(os.path.join(self.data_root, 'bio_vocab.json'), 'r'))

        self.id2bio = {v: k for k, v in self.bio_vocab.items()}

        self.word_embeddings = nn.Embedding(num_embeddings=len(
            self.word_vocab),
                                            embedding_dim=hyper.emb_size)

        self.relation_emb = nn.Embedding(num_embeddings=len(
            self.relation_vocab),
                                         embedding_dim=hyper.rel_emb_size)

        # bio + pad
        self.bio_emb = nn.Embedding(num_embeddings=len(self.bio_vocab),
                                    embedding_dim=hyper.bio_emb_size)

        if hyper.cell_name == 'gru':
            self.encoder = nn.GRU(hyper.emb_size,
                                  hyper.hidden_size,
                                  bidirectional=True,
                                  batch_first=True)

        elif hyper.cell_name == 'lstm':
            self.encoder = nn.LSTM(hyper.emb_size,
                                   hyper.hidden_size,
                                   bidirectional=True,
                                   batch_first=True,
                                   num_layers=2)

        elif hyper.cell_name == 'bert':
            self.post_lstm = nn.LSTM(hyper.emb_size,
                                     hyper.hidden_size,
                                     bidirectional=True,
                                     batch_first=True)
            self.encoder = BertModel.from_pretrained('data/bert-base-uncased')
            for name, param in self.encoder.named_parameters():
                if '11' in name:
                    param.requires_grad = True
                else:
                    param.requires_grad = False
                    # print(name, param.size())
        else:
            raise ValueError('cell name should be gru/lstm/bert!')

        if hyper.activation.lower() == 'relu':
            self.activation = nn.ReLU()
        elif hyper.activation.lower() == 'tanh':
            self.activation = nn.Tanh()
        else:
            raise ValueError('unexpected activation!')

        self.emission = nn.Linear(hyper.hidden_size, len(self.bio_vocab) - 1)
        self.norm1 = nn.BatchNorm1d(self.hyper.max_text_len)
        self.dropout1 = nn.Dropout(0)

        self.tagger = CRF(len(self.bio_vocab) - 1, batch_first=True)
        self.selection_u = nn.Linear(hyper.hidden_size + hyper.bio_emb_size,
                                     hyper.rel_emb_size)
        self.norm2 = nn.BatchNorm1d(self.hyper.max_text_len)
        self.dropout2 = nn.Dropout(0)
        self.selection_v = nn.Linear(hyper.hidden_size + hyper.bio_emb_size,
                                     hyper.rel_emb_size)
        self.norm3 = nn.BatchNorm1d(self.hyper.max_text_len)
        self.dropout3 = nn.Dropout(0)
        self.selection_uv = nn.Linear(2 * hyper.rel_emb_size,
                                      hyper.rel_emb_size)
        self.norm4 = nn.BatchNorm2d(self.hyper.max_text_len,
                                    self.hyper.max_text_len)
        self.dropout4 = nn.Dropout2d(0)

        self.bert2hidden = nn.Linear(768, hyper.hidden_size)
        # for bert_lstm
        # self.bert2hidden = nn.Linear(768, hyper.emb_size)

        if self.hyper.cell_name == 'bert':
            self.bert_tokenizer = BertTokenizer.from_pretrained(
                'bert-base-uncased')
コード例 #25
0
 def __init__(self):
     super(Model, self).__init__()
     self.conv = nn.ConvTranspose2d(3, 8, 3)
     self.dropout = nn.Dropout2d()
コード例 #26
0
def MulResUnet(num_input_channels=1,
               num_output_channels=1,
               num_channels_down=[16, 32, 64, 128, 256],
               num_channels_up=[16, 32, 64, 128, 256],
               num_channels_skip=[16, 32, 64, 128],
               alpha=1.67,
               last_act_fun=None,
               need_bias=True,
               upsample_mode='nearest',
               act_fun='LeakyReLU',
               dropout=0.):
    assert len(num_channels_down) == len(num_channels_up) == (len(num_channels_skip) + 1)
    
    n_scales = len(num_channels_down)
    
    if not (isinstance(upsample_mode, list) or isinstance(upsample_mode, tuple)):
        upsample_mode = [upsample_mode] * n_scales
    
    model = nn.Sequential()
    model_tmp = model
    multires = MultiResBlock(num_channels_down[0], num_input_channels,
                             alpha=alpha, act_fun=act_fun, bias=need_bias, drop=dropout)
    
    model_tmp.add(multires)
    input_depth = multires.out_dim
    
    for i in range(1, n_scales):
        
        deeper = nn.Sequential()
        skip = nn.Sequential()
        # multi-res Block in the encoders
        multires = MultiResBlock(num_channels_down[i], input_depth,
                                 alpha=alpha, act_fun=act_fun, bias=need_bias, drop=dropout)
        # stride downsampling.
        deeper.add(conv(input_depth, input_depth, 3, stride=2, bias=need_bias))
        deeper.add(act(act_fun))
        deeper.add(nn.Dropout2d(dropout))
        deeper.add(multires)
        
        if num_channels_skip[i - 1] != 0:
            # add the path residual block, note that the number of filters is set to 1.
            skip.add(PathRes(input_depth, num_channels_skip[i - 1], 1, act_fun=act_fun, bias=need_bias, drop=dropout))
            model_tmp.add(Concat(1, skip, deeper))
        else:
            model_tmp.add(deeper)
        
        deeper_main = nn.Sequential()
        
        if i != len(num_channels_down) - 1:
            # not the deepest
            deeper.add(deeper_main)
        # add upsampling to the decoder
        deeper.add(nn.Upsample(scale_factor=2, mode=upsample_mode[i]))
        # add multi-res block to the decoder
        model_tmp.add(MultiResBlock(num_channels_up[i - 1], multires.out_dim + num_channels_skip[i - 1],
                                    alpha=alpha, act_fun=act_fun, bias=need_bias, drop=dropout))
        
        input_depth = multires.out_dim
        model_tmp = deeper_main
    
    W = num_channels_up[0] * alpha
    last_kernel = int(W * 0.167) + int(W * 0.333) + int(W * 0.5)
    
    # add the convolutional filter for output.
    model.add(conv(last_kernel, num_output_channels, 1, bias=need_bias))
    if isinstance(last_act_fun, str) and last_act_fun.lower() == 'none':
        last_act_fun = None
    if last_act_fun is not None:
        model.add(act(last_act_fun))
    
    return model
コード例 #27
0
ファイル: detnasnet.py プロジェクト: yuyijie1995/DetNAS
    def __init__(self, model_size='DETNAS-300M'):
        super(ShuffleNetV2DetNAS, self).__init__()
        print('Model size is {}.'.format(model_size))

        n_class = 1000
        if '300M' in model_size:
            stage_repeats = [4, 4, 8, 4]
            stage_out_channels = [-1, 16, 64, 160, 320, 640, 1024]
        elif '1.3G' in model_size:
            stage_repeats = [8, 8, 16, 8]
            stage_out_channels = [-1, 48, 96, 240, 480, 960, 1024]
        else:
            raise NotImplementedError

        self.stage_repeats = stage_repeats
        self.first_conv = ConvBNReLU(in_channel=3,
                                     out_channel=stage_out_channels[1],
                                     k_size=3,
                                     stride=2,
                                     padding=1,
                                     gaussian_init=True)

        self.features = list()
        self.stage_ends_idx = list()

        in_channels = stage_out_channels[1]
        i_th = 0
        for id_stage in range(1, len(stage_repeats) + 1):
            out_channels = stage_out_channels[id_stage + 1]
            repeats = stage_repeats[id_stage - 1]
            for id_repeat in range(repeats):
                prefix = str(id_stage) + chr(ord('a') + id_repeat)
                stride = 1 if id_repeat > 0 else 2

                _ops = nn.ModuleList()
                for i in range(len(blocks_key)):
                    _ops.append(
                        ShuffleNetV2BlockSearched(
                            prefix,
                            in_channels=in_channels,
                            out_channels=out_channels,
                            stride=stride,
                            base_mid_channels=out_channels // 2,
                            id=i))
                self.features.append(_ops)

                in_channels = out_channels
                i_th += 1
            self.stage_ends_idx.append(i_th - 1)

        self.features = nn.Sequential(*self.features)

        self.last_conv = ConvBNReLU(in_channel=in_channels,
                                    out_channel=stage_out_channels[-1],
                                    k_size=1,
                                    stride=1,
                                    padding=0)
        self.drop_out = nn.Dropout2d(p=0.2)
        self.global_pool = nn.AvgPool2d(7)
        self.fc = FC(in_channels=stage_out_channels[-1], out_channels=n_class)
        self._initialize_weights()
コード例 #28
0
    def __init__(self,
                 num_classes=1,
                 num_filters=32,
                 encoder=None,
                 pretrained=False,
                 is_deconv=False,
                 dropout=0,
                 base_num_filters=512):
        """
        :param num_classes:
        :param num_filters:
        :param pretrained:
            False - no pre-trained network is used
            True  - encoder is pre-trained with resnet34
        :is_deconv:
            False: bilinear interpolation is used in decoder
            True: deconvolution is used in decoder
        """
        super().__init__()
        self.num_classes = num_classes

        self.encoder = encoder
        if encoder is None:
            self.encoder = torchvision.models.resnet34(pretrained=pretrained)

        self.relu = nn.ReLU(inplace=True)

        try:
            self.initial = self.encoder.layer0
        except:
            self.initial = self.encoder
        try:
            self.pool = self.initial.pool
        except:
            self.pool = nn.MaxPool2d(3, stride=2, ceil_mode=True)
        self.conv1 = nn.Sequential(self.initial.conv1, self.initial.bn1,
                                   nn.ReLU(inplace=True), self.pool)

        self.conv2 = self.encoder.layer1

        self.conv3 = self.encoder.layer2

        self.conv4 = self.encoder.layer3

        self.conv5 = self.encoder.layer4

        self.center = DecoderBlockV2(base_num_filters, num_filters * 8 * 2,
                                     num_filters * 8, is_deconv)
        self.dec5 = DecoderBlockV2(base_num_filters + num_filters * 8,
                                   num_filters * 8 * 2, num_filters * 8,
                                   is_deconv)
        self.dec4 = DecoderBlockV2(base_num_filters // 2 + num_filters * 8,
                                   num_filters * 8 * 2, num_filters * 8,
                                   is_deconv)
        self.dec3 = DecoderBlockV2(base_num_filters // 4 + num_filters * 8,
                                   num_filters * 4 * 2, num_filters * 2,
                                   is_deconv)
        self.dec2 = DecoderBlockV2(base_num_filters // 8 + num_filters * 2,
                                   num_filters * 2 * 2, num_filters * 2 * 2,
                                   is_deconv)
        self.dec1 = DecoderBlockV2(num_filters * 2 * 2, num_filters * 2 * 2,
                                   num_filters, is_deconv)
        self.dec0 = ConvRelu(num_filters, num_filters)
        self.final = nn.Conv2d(num_filters, num_classes, kernel_size=1)
        self.dropout2d = nn.Dropout2d(p=dropout) if dropout else None
        self.pool = nn.MaxPool2d(2, 2)
コード例 #29
0
    def __init__(self,
                 channels,
                 internal_ratio=4,
                 kernel_size=3,
                 padding=0,
                 dilation=1,
                 asymmetric=False,
                 dropout_prob=0,
                 bias=False,
                 relu=True):
        super().__init__()

        # Check in the internal_scale parameter is within the expected range
        # [1, channels]
        if internal_ratio <= 1 or internal_ratio > channels:
            raise RuntimeError(
                "Value out of range. Expected value in the "
                "interval [1, {0}], got internal_scale={1}.".format(
                    channels, internal_ratio))

        internal_channels = channels // internal_ratio

        if relu:
            activation = nn.ReLU()
        else:
            activation = nn.PReLU()

        # Main branch - shortcut connection

        # Extension branch - 1x1 convolution, followed by a regular, dilated or
        # asymmetric convolution, followed by another 1x1 convolution, and,
        # finally, a regularizer (spatial dropout). Number of channels is constant.

        # 1x1 projection convolution
        self.ext_conv1 = nn.Sequential(
            nn.Conv2d(channels,
                      internal_channels,
                      kernel_size=1,
                      stride=1,
                      bias=bias), nn.BatchNorm2d(internal_channels),
            activation)

        # If the convolution is asymmetric we split the main convolution in
        # two. Eg. for a 5x5 asymmetric convolution we have two convolution:
        # the first is 5x1 and the second is 1x5.
        if asymmetric:
            self.ext_conv2 = nn.Sequential(
                nn.Conv2d(internal_channels,
                          internal_channels,
                          kernel_size=(kernel_size, 1),
                          stride=1,
                          padding=(padding, 0),
                          dilation=dilation,
                          bias=bias), nn.BatchNorm2d(internal_channels),
                activation,
                nn.Conv2d(internal_channels,
                          internal_channels,
                          kernel_size=(1, kernel_size),
                          stride=1,
                          padding=(0, padding),
                          dilation=dilation,
                          bias=bias), nn.BatchNorm2d(internal_channels),
                activation)
        else:
            self.ext_conv2 = nn.Sequential(
                nn.Conv2d(internal_channels,
                          internal_channels,
                          kernel_size=kernel_size,
                          stride=1,
                          padding=padding,
                          dilation=dilation,
                          bias=bias), nn.BatchNorm2d(internal_channels),
                activation)

        # 1x1 expansion convolution
        self.ext_conv3 = nn.Sequential(
            nn.Conv2d(internal_channels,
                      channels,
                      kernel_size=1,
                      stride=1,
                      bias=bias), nn.BatchNorm2d(channels), activation)

        self.ext_regul = nn.Dropout2d(p=dropout_prob)

        # PReLU layer to apply after adding the branches
        self.out_prelu = activation
コード例 #30
0
    def __init__(self,
                 input_channels=1,
                 nb_filter=[32, 64, 128, 256, 512],
                 layers=[3, 4, 6, 3]):
        super().__init__()
        self.deepsupervision = False

        backbone_num = [64, 128, 256, 512]

        self.dropout2d = nn.Dropout2d(p=0.05)
        self.pool = nn.MaxPool2d(2, 2)
        self.up = nn.Upsample(scale_factor=2,
                              mode='bilinear',
                              align_corners=True)

        self.conv0_0 = VGGBlock(32, nb_filter[0], nb_filter[0])
        self.conv1_0 = VGGBlock(nb_filter[0], nb_filter[1], nb_filter[1])
        self.conv2_0 = VGGBlock(nb_filter[1], nb_filter[2], nb_filter[2])
        self.conv3_0 = VGGBlock(nb_filter[2], nb_filter[3], nb_filter[3])
        self.conv4_0 = VGGBlock(nb_filter[3], nb_filter[4], nb_filter[4])

        self.conv0_1 = VGGBlock(nb_filter[0] + nb_filter[1], nb_filter[0],
                                nb_filter[0])
        self.conv1_1 = VGGBlock(nb_filter[1] + nb_filter[2], nb_filter[1],
                                nb_filter[1])
        self.conv2_1 = VGGBlock(nb_filter[2] + nb_filter[3], nb_filter[2],
                                nb_filter[2])
        self.conv3_1 = VGGBlock(nb_filter[3] + nb_filter[4], nb_filter[3],
                                nb_filter[3])
        self.ca0 = CALayer(nb_filter[0] + nb_filter[1])
        self.ca1 = CALayer(nb_filter[1] + nb_filter[2])
        self.ca2 = CALayer(nb_filter[2] + nb_filter[3])
        self.ca3 = CALayer(nb_filter[3] + nb_filter[4])

        self.conv0_2 = VGGBlock(nb_filter[0] * 2 + nb_filter[1], nb_filter[0],
                                nb_filter[0])
        self.conv1_2 = VGGBlock(nb_filter[1] * 2 + nb_filter[2], nb_filter[1],
                                nb_filter[1])
        self.conv2_2 = VGGBlock(nb_filter[2] * 2 + nb_filter[3], nb_filter[2],
                                nb_filter[2])
        self.ca4 = CALayer(nb_filter[0] * 2 + nb_filter[1])
        self.ca5 = CALayer(nb_filter[1] * 2 + nb_filter[2])
        self.ca6 = CALayer(nb_filter[2] * 2 + nb_filter[3])

        self.conv0_3 = VGGBlock(nb_filter[0] * 3 + nb_filter[1], nb_filter[0],
                                nb_filter[0])
        self.conv1_3 = VGGBlock(nb_filter[1] * 3 + nb_filter[2], nb_filter[1],
                                nb_filter[1])
        self.ca7 = CALayer(nb_filter[0] * 3 + nb_filter[1])
        self.ca8 = CALayer(nb_filter[1] * 3 + nb_filter[2])

        self.conv0_4 = VGGBlock(nb_filter[0] * 4 + nb_filter[1], nb_filter[0],
                                nb_filter[0])

        self.last = nn.Conv2d(nb_filter[0], 1, kernel_size=1)
        self.out_2 = nn.Softmax(dim=1)

        if self.deepsupervision:
            self.final1 = nn.Conv2d(nb_filter[0], nb_filter[0], kernel_size=1)
            self.final2 = nn.Conv2d(nb_filter[0], nb_filter[0], kernel_size=1)
            self.final3 = nn.Conv2d(nb_filter[0], nb_filter[0], kernel_size=1)
            self.final4 = nn.Conv2d(nb_filter[0], nb_filter[0], kernel_size=1)
        else:
            self.final = nn.Conv2d(nb_filter[0], 2, kernel_size=1)

        self.ResNet = ResNet(input_channels, Bottleneck, layers, nb_filter)

        # N FUSION MODULE

        self.fusion1 = Add_fusion(nb_filter[0], nb_filter[1])
        self.fusion2 = Add_fusion(nb_filter[1], nb_filter[2])
        self.fusion3 = Add_fusion(nb_filter[2], nb_filter[3])
        self.ac1 = nn.Conv2d(nb_filter[3], nb_filter[2], 1)
        self.ac2 = nn.Conv2d(nb_filter[2], nb_filter[1], 1)
        self.ac3 = nn.Conv2d(nb_filter[1], nb_filter[0], 1)

        self.fusion_conv = nn.Conv2d(nb_filter[0], 2, kernel_size=1)

        self.c1 = nn.Conv2d(nb_filter[1], 2, 1)
        self.c2 = nn.Conv2d(nb_filter[2], 2, 1)
        self.c3 = nn.Conv2d(nb_filter[3], 2, 1)