Python torch.nn.ConstantPad2d() Examples

The following are 30 code examples of torch.nn.ConstantPad2d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch.nn , or try the search function .
Example #1
Source File: layers01.py    From packnet-sfm with MIT License 6 votes vote down vote up
def __init__(self, in_channels, out_channels=1, min_depth=0.5):
        """
        Initializes an InvDepth object.

        Parameters
        ----------
        in_channels : int
            Number of input channels
        out_channels : int
            Number of output channels
        min_depth : float
            Minimum depth value to calculate
        """
        super().__init__()
        self.min_depth = min_depth
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1)
        self.pad = nn.ConstantPad2d([1] * 4, value=0)
        self.activ = nn.Sigmoid() 
Example #2
Source File: cell_operations.py    From AutoDL-Projects with MIT License 6 votes vote down vote up
def __init__(self, C_in, C_out, stride, affine, track_running_stats):
    super(FactorizedReduce, self).__init__()
    self.stride = stride
    self.C_in   = C_in  
    self.C_out  = C_out  
    self.relu   = nn.ReLU(inplace=False)
    if stride == 2:
      #assert C_out % 2 == 0, 'C_out : {:}'.format(C_out)
      C_outs = [C_out // 2, C_out - C_out // 2]
      self.convs = nn.ModuleList()
      for i in range(2):
        self.convs.append( nn.Conv2d(C_in, C_outs[i], 1, stride=stride, padding=0, bias=False) )
      self.pad = nn.ConstantPad2d((0, 1, 0, 1), 0)
    elif stride == 1:
      self.conv = nn.Conv2d(C_in, C_out, 1, stride=stride, padding=0, bias=False)
    else:
      raise ValueError('Invalid stride : {:}'.format(stride))
    self.bn = nn.BatchNorm2d(C_out, affine=affine, track_running_stats=track_running_stats) 
Example #3
Source File: yolo2.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, config_channels, anchors, num_cls, channels=16):
        nn.Module.__init__(self)
        layers = []

        bn = config_channels.config.getboolean('batch_norm', 'enable')
        for _ in range(5):
            layers.append(Conv2d(config_channels.channels, config_channels(channels, 'layers.%d.conv.weight' % len(layers)), 3, bn=bn, padding=True))
            layers.append(nn.MaxPool2d(kernel_size=2))
            channels *= 2
        layers.append(Conv2d(config_channels.channels, config_channels(channels, 'layers.%d.conv.weight' % len(layers)), 3, bn=bn, padding=True))
        layers.append(nn.ConstantPad2d((0, 1, 0, 1), float(np.finfo(np.float32).min)))
        layers.append(nn.MaxPool2d(kernel_size=2, stride=1))
        channels *= 2
        for _ in range(2):
            layers.append(Conv2d(config_channels.channels, config_channels(channels, 'layers.%d.conv.weight' % len(layers)), 3, bn=bn, padding=True))
        layers.append(Conv2d(config_channels.channels, model.output_channels(len(anchors), num_cls), 1, bn=False, act=False))
        self.layers = nn.Sequential(*layers)

        self.init() 
Example #4
Source File: operations.py    From AutoDL-Projects with MIT License 6 votes vote down vote up
def __init__(self, C_in, C_out, stride, affine=True):
    super(FactorizedReduce, self).__init__()
    self.stride = stride
    self.C_in   = C_in  
    self.C_out  = C_out  
    self.relu   = nn.ReLU(inplace=False)
    if stride == 2:
      #assert C_out % 2 == 0, 'C_out : {:}'.format(C_out)
      C_outs = [C_out // 2, C_out - C_out // 2]
      self.convs = nn.ModuleList()
      for i in range(2):
        self.convs.append( nn.Conv2d(C_in, C_outs[i], 1, stride=stride, padding=0, bias=False) )
      self.pad = nn.ConstantPad2d((0, 1, 0, 1), 0)
    elif stride == 4:
      assert C_out % 4 == 0, 'C_out : {:}'.format(C_out)
      self.convs = nn.ModuleList()
      for i in range(4):
        self.convs.append( nn.Conv2d(C_in, C_out // 4, 1, stride=stride, padding=0, bias=False) )
      self.pad = nn.ConstantPad2d((0, 3, 0, 3), 0)
    else:
      raise ValueError('Invalid stride : {:}'.format(stride))
    
    self.bn = nn.BatchNorm2d(C_out, affine=affine) 
Example #5
Source File: resfcn256.py    From centerpose with MIT License 6 votes vote down vote up
def __init__(self, in_c, out_c, conv_num=2):
        super().__init__()
        additional_conv = []
        layer_length = 4

        for i in range(1, conv_num+1):
            additional_conv += [
                nn.ConstantPad2d((2, 1, 2, 1), 0),
                nn.ConvTranspose2d(out_c, out_c, kernel_size=4, stride=1, padding=3, bias=False),
                nn.BatchNorm2d(out_c, eps=0.001, momentum=0.001),
                nn.ReLU(inplace=True)
            ]
        self.main = nn.Sequential(
            # nn.ConstantPad2d((0, 1, 0, 1), 0),
            nn.ConvTranspose2d(in_c, out_c, kernel_size=4, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(out_c, eps=0.001, momentum=0.001),
            nn.ReLU(inplace=True),
            *additional_conv
            ) 
Example #6
Source File: dec_pixelcnn.py    From vae-lagging-encoder with MIT License 6 votes vote down vote up
def __init__(self, in_dim, out_dim=None, kernel_size = 3, mask = 'B'):
        super(GatedMaskedConv2d, self).__init__()
        if out_dim is None:
            out_dim = in_dim
        self.dim = out_dim
        self.size = kernel_size
        self.mask = mask
        pad = self.size // 2

        #vertical stack
        self.v_conv = nn.Conv2d(in_dim, 2*self.dim, kernel_size=(pad+1, self.size))
        self.v_pad1 = nn.ConstantPad2d((pad, pad, pad, 0), 0)
        self.v_pad2 = nn.ConstantPad2d((0, 0, 1, 0), 0)
        self.vh_conv = nn.Conv2d(2*self.dim, 2*self.dim, kernel_size = 1)

        #horizontal stack
        self.h_conv = nn.Conv2d(in_dim, 2*self.dim, kernel_size=(1, pad+1))
        self.h_pad1 = nn.ConstantPad2d((self.size // 2, 0, 0, 0), 0)
        self.h_pad2 = nn.ConstantPad2d((1, 0, 0, 0), 0)
        self.h_conv_res = nn.Conv2d(self.dim, self.dim, 1) 
Example #7
Source File: dense_net.py    From MatchZoo-py with Apache License 2.0 6 votes vote down vote up
def _make_conv_block(
        cls,
        in_channels: int,
        out_channels: int,
        kernel_size: tuple
    ) -> nn.Module:
        """Make conv block."""
        return nn.Sequential(
            nn.ConstantPad2d(
                (0, kernel_size[1] - 1, 0, kernel_size[0] - 1), 0
            ),
            nn.Conv2d(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=kernel_size
            ),
            nn.ReLU()
        ) 
Example #8
Source File: arcii.py    From MatchZoo-py with Apache License 2.0 6 votes vote down vote up
def _make_conv_pool_block(
        cls,
        in_channels: int,
        out_channels: int,
        kernel_size: tuple,
        activation: nn.Module,
        pool_size: tuple,
    ) -> nn.Module:
        """Make conv pool block."""
        return nn.Sequential(
            # Same padding
            nn.ConstantPad2d(
                (0, kernel_size[1] - 1, 0, kernel_size[0] - 1), 0
            ),
            nn.Conv2d(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=kernel_size
            ),
            activation,
            nn.MaxPool2d(kernel_size=pool_size)
        ) 
Example #9
Source File: match_pyramid.py    From MatchZoo-py with Apache License 2.0 6 votes vote down vote up
def _make_conv_pool_block(
        cls,
        in_channels: int,
        out_channels: int,
        kernel_size: tuple,
        activation: nn.Module
    ) -> nn.Module:
        """Make conv pool block."""
        return nn.Sequential(
            # Same padding
            nn.ConstantPad2d(
                (0, kernel_size[1] - 1, 0, kernel_size[0] - 1), 0
            ),
            nn.Conv2d(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=kernel_size
            ),
            activation
        ) 
Example #10
Source File: oth_prnet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self, in_c, out_c, conv_num=2):
        super().__init__()
        additional_conv = []
        layer_length = 4

        for i in range(1, conv_num+1):
            additional_conv += [
                nn.ConstantPad2d((2, 1, 2, 1), 0),
                nn.ConvTranspose2d(out_c, out_c, kernel_size=4, stride=1, padding=3, bias=False),
                nn.BatchNorm2d(out_c, eps=0.001, momentum=0.001),
                nn.ReLU(inplace=True)
            ]
        self.main = nn.Sequential(
            # nn.ConstantPad2d((0, 1, 0, 1), 0),
            nn.ConvTranspose2d(in_c, out_c, kernel_size=4, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(out_c, eps=0.001, momentum=0.001),
            nn.ReLU(inplace=True),
            *additional_conv
            ) 
Example #11
Source File: utils.py    From tvnet_pytorch with MIT License 5 votes vote down vote up
def get_module_list(module, n_modules):
    ml = nn.ModuleList()
    for _ in range(n_modules):
        ml.append(module())
    return ml

# def conv2d_padding_same(input_size, input_channels, output_channels, kernel_size, \
#                         stride=[1,1], bias=True, weight=None, padding_value=0):
#     """
#         This function is following tensorflow padding style, indicating that it tries to
#         pad evenly left(top) and right(bottom), but if the amount of columns to be added is odd, 
#         it will add the extra column to the right(bottom). 
#     """
#     if weight is not None:
#         weight = np.asarray(weight)

#     assert weight is None or list(weight.shape) == [output_channels, input_channels, *kernel_size]

#     height = float(input_size[2])
#     width  = float(input_size[3])

#     out_size = np.ceil([height / stride[0], width / stride[1]])
#     padding_vertical = (out_size[0] - 1) * stride[0] + kernel_size[0] - height
#     padding_horizontal = (out_size[1] - 1) * stride[1] + kernel_size[1] - width

#     padding_left = int(np.floor(padding_horizontal / 2))
#     padding_right = int(np.ceil(padding_horizontal / 2))
#     padding_top = int(np.floor(padding_vertical / 2))
#     padding_bottom = int(np.ceil(padding_vertical / 2))

#     assert padding_left + padding_right == padding_horizontal, "{}, {}, {}".format(padding_left, padding_right, padding_horizontal)
    
#     padding_layer = nn.ConstantPad2d((padding_left, padding_right, padding_top, padding_bottom), padding_value)
#     conv_layer = nn.Conv2d(input_channels, output_channels, kernel_size, stride=tuple(stride), bias=bias)
#     if weight is not None:
#         conv_layer.weight.data = torch.FloatTensor(weight)
    
#     return nn.Sequential(padding_layer, conv_layer) 
Example #12
Source File: utils.py    From multiple-objects-gan with MIT License 5 votes vote down vote up
def pad_imgs(img, pad=2):
    m = nn.ConstantPad2d((pad, pad, pad, pad), 0)
    return m(img) 
Example #13
Source File: matchpyramid.py    From sigir19-neural-ir with Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 word_embeddings: TextFieldEmbedder,
                 conv_output_size: List[int],
                 conv_kernel_size: List[Tuple[int,int]],
                 adaptive_pooling_size: List[Tuple[int,int]]):

        super(MatchPyramid, self).__init__()

        self.word_embeddings = word_embeddings
        self.cosine_module = CosineMatrixAttention()
        #self.cosine_module = DotProductMatrixAttention()

        if len(conv_output_size) != len(conv_kernel_size) or len(conv_output_size) != len(adaptive_pooling_size):
            raise Exception("conv_output_size, conv_kernel_size, adaptive_pooling_size must have the same length")

        conv_layer_dict = OrderedDict()
        last_channel_out = 1
        for i in range(len(conv_output_size)):
            conv_layer_dict["pad " +str(i)] = nn.ConstantPad2d((0,conv_kernel_size[i][0] - 1,0, conv_kernel_size[i][1] - 1), 0)
            conv_layer_dict["conv "+str(i)] = nn.Conv2d(kernel_size=conv_kernel_size[i], in_channels=last_channel_out, out_channels=conv_output_size[i])
            conv_layer_dict["relu "+str(i)] = nn.ReLU()
            conv_layer_dict["pool "+str(i)] = nn.AdaptiveMaxPool2d(adaptive_pooling_size[i]) # this is strange - but so written in the paper
                                                                                             # would think only to pool at the end ??
            last_channel_out = conv_output_size[i]

        self.conv_layers = nn.Sequential(conv_layer_dict)

        #self.dropout = nn.Dropout(0)

        self.dense = nn.Linear(conv_output_size[-1] * adaptive_pooling_size[-1][0] * adaptive_pooling_size[-1][1], out_features=100, bias=True)
        self.dense2 = nn.Linear(100, out_features=10, bias=True)
        self.dense3 = nn.Linear(10, out_features=1, bias=False)

        # init with small weights, otherwise the dense output is way to high for the tanh -> resulting in loss == 1 all the time
        #torch.nn.init.uniform_(self.dense.weight, -0.014, 0.014)  # inits taken from matchzoo
        #self.dense.bias.data.fill_(0.0) 
Example #14
Source File: co_pacrr.py    From transformer-kernel-ranking with Apache License 2.0 5 votes vote down vote up
def __init__(self,

                 unified_query_length:int,
                 unified_document_length:int,

                 max_conv_kernel_size: int, # 2 to n
                 conv_output_size: int, # conv output channels

                 kmax_pooling_size: int): # per query k-max pooling
                 
        super(CO_PACRR,self).__init__()

        self.cosine_module = CosineMatrixAttention()

        self.unified_query_length = unified_query_length
        self.unified_document_length = unified_document_length

        self.convolutions = []
        for i in range(2, max_conv_kernel_size + 1):
            self.convolutions.append(
                nn.Sequential(
                    nn.ConstantPad2d((0,i - 1,0, i - 1), 0), # this outputs [batch,1,unified_query_length + i - 1 ,unified_document_length + i - 1]
                    nn.Conv2d(kernel_size=i, in_channels=1, out_channels=conv_output_size), # this outputs [batch,32,unified_query_length,unified_document_length]
                    nn.MaxPool3d(kernel_size=(conv_output_size,1,1)) # this outputs [batch,1,unified_query_length,unified_document_length]
            ))
        self.convolutions = nn.ModuleList(self.convolutions) # register conv as part of the model

        context_pool_size = 6
        self.doc_context_pool = nn.Sequential(
                                    nn.ConstantPad1d((0,context_pool_size - 1),0),
                                    nn.AvgPool1d(kernel_size=context_pool_size,stride=1))

        self.masked_softmax = MaskedSoftmax()
        self.kmax_pooling_size = kmax_pooling_size

        kmax_pooling_view_percent = [0.25,0.5,0.75,1]
        self.kmax_pooling_views = [int(unified_document_length * x) for x in kmax_pooling_view_percent]

        self.dense = nn.Linear(len(self.kmax_pooling_views) * 2 * kmax_pooling_size * unified_query_length * max_conv_kernel_size, out_features=100, bias=True)
        self.dense2 = nn.Linear(100, out_features=10, bias=True)
        self.dense3 = nn.Linear(10, out_features=1, bias=False) 
Example #15
Source File: matchpyramid.py    From transformer-kernel-ranking with Apache License 2.0 5 votes vote down vote up
def __init__(self,
                 conv_output_size: List[int],
                 conv_kernel_size: List[Tuple[int,int]],
                 adaptive_pooling_size: List[Tuple[int,int]]):

        super(MatchPyramid, self).__init__()

        self.cosine_module = CosineMatrixAttention()

        if len(conv_output_size) != len(conv_kernel_size) or len(conv_output_size) != len(adaptive_pooling_size):
            raise Exception("conv_output_size, conv_kernel_size, adaptive_pooling_size must have the same length")

        conv_layer_dict = OrderedDict()
        last_channel_out = 1
        for i in range(len(conv_output_size)):
            conv_layer_dict["pad " +str(i)] = nn.ConstantPad2d((0,conv_kernel_size[i][0] - 1,0, conv_kernel_size[i][1] - 1), 0)
            conv_layer_dict["conv "+str(i)] = nn.Conv2d(kernel_size=conv_kernel_size[i], in_channels=last_channel_out, out_channels=conv_output_size[i])
            conv_layer_dict["relu "+str(i)] = nn.ReLU()
            conv_layer_dict["pool "+str(i)] = nn.AdaptiveMaxPool2d(adaptive_pooling_size[i])
            last_channel_out = conv_output_size[i]

        self.conv_layers = nn.Sequential(conv_layer_dict)

        self.dense = nn.Linear(conv_output_size[-1] * adaptive_pooling_size[-1][0] * adaptive_pooling_size[-1][1], out_features=100, bias=True)
        self.dense2 = nn.Linear(100, out_features=10, bias=True)
        self.dense3 = nn.Linear(10, out_features=1, bias=False)

        # init with small weights, otherwise the dense output is way to high for the tanh -> resulting in loss == 1 all the time
        #torch.nn.init.uniform_(self.dense.weight, -0.014, 0.014)  # inits taken from matchzoo
        #self.dense.bias.data.fill_(0.0) 
Example #16
Source File: pacrr.py    From transformer-kernel-ranking with Apache License 2.0 5 votes vote down vote up
def __init__(self,

                 unified_query_length:int,
                 unified_document_length:int,

                 max_conv_kernel_size: int, # 2 to n
                 conv_output_size: int, # conv output channels

                 kmax_pooling_size: int): # per query k-max pooling
                 
        super(PACRR,self).__init__()

        self.cosine_module = CosineMatrixAttention()

        self.unified_query_length = unified_query_length
        self.unified_document_length = unified_document_length

        self.convolutions = []
        for i in range(2, max_conv_kernel_size + 1):
            self.convolutions.append(
                nn.Sequential(
                    nn.ConstantPad2d((0,i - 1,0, i - 1), 0), # this outputs [batch,1,unified_query_length + i - 1 ,unified_document_length + i - 1]
                    nn.Conv2d(kernel_size=i, in_channels=1, out_channels=conv_output_size), # this outputs [batch,32,unified_query_length,unified_document_length]
                    nn.MaxPool3d(kernel_size=(conv_output_size,1,1)) # this outputs [batch,1,unified_query_length,unified_document_length]
            ))
        self.convolutions = nn.ModuleList(self.convolutions) # register conv as part of the model

        self.masked_softmax = MaskedSoftmax()
        self.kmax_pooling_size = kmax_pooling_size

        self.dense = nn.Linear(kmax_pooling_size * unified_query_length * max_conv_kernel_size, out_features=100, bias=True)
        self.dense2 = nn.Linear(100, out_features=10, bias=True)
        self.dense3 = nn.Linear(10, out_features=1, bias=False) 
Example #17
Source File: InferDeepMask.py    From deepmask-pytorch with MIT License 5 votes vote down vote up
def forward(self, img):
        tic = time.time()
        imgPyramid = [pyramid(img) for pyramid in self.pyramid]
        self.timer[0] = time.time() - tic

        self.mask, self.score = [], []
        for inp in imgPyramid:
            tic = time.time()
            imgPad = nn.ConstantPad2d(self.bw, 0.5).cuda()(inp)
            # cv2.imshow('pad image', np.transpose(imgPad.squeeze().cpu().data.numpy(), axes=(1, 2, 0))[:,::-1])
            # cv2.waitKey(0)
            imgPad = imgPad.sub_(self.mean).div_(self.std)
            self.timer[1] += time.time() - tic

            tic = time.time()
            outTrunk = self.trunk(imgPad)
            self.timer[2] += time.time() - tic

            tic = time.time()
            outMask = self.mHead(outTrunk)
            self.timer[3] += time.time() - tic

            tic = time.time()
            outScore = self.sHead(outTrunk)
            self.timer[4] += time.time() - tic

            # mask_show = vutils.make_grid(outMask.sigmoid().transpose(0, 1), nrow=outScore.shape[-1], pad_value=0)
            # mask_show_numpy = np.transpose(mask_show.cpu().data.numpy(), axes=(1, 2, 0))
            # plt.imshow(mask_show_numpy[:,:,0], cmap='jet')
            # plt.show()
            self.mask.append(outMask.sigmoid().cpu().data.numpy())
            self.score.append(outScore.sigmoid().cpu().data.numpy()) 
Example #18
Source File: operations.py    From PNASNet.pytorch with Apache License 2.0 5 votes vote down vote up
def __init__(self, C_in, C_out, affine=True):
    super(FactorizedReduce, self).__init__()
    assert C_out % 2 == 0
    self.relu = nn.ReLU(inplace=False)
    self.conv_1 = nn.Conv2d(C_in, C_out // 2, 1, stride=2, padding=0, bias=False)
    self.conv_2 = nn.Conv2d(C_in, C_out // 2, 1, stride=2, padding=0, bias=False) 
    self.bn = nn.BatchNorm2d(C_out, eps=1e-3, affine=affine)
    self.pad = nn.ConstantPad2d((0, 1, 0, 1), 0) 
Example #19
Source File: test_caffe2.py    From onnx-fb-universe with MIT License 5 votes vote down vote up
def test_constantpad2d(self):
        model = nn.ConstantPad2d((1, 2, 3, 4), 3.5)
        self.run_model_test(model, train=False, batch_size=BATCH_SIZE) 
Example #20
Source File: pacrr.py    From OpenNIR with MIT License 5 votes vote down vote up
def __init__(self, shape, n_filters, k, channels=1):
        super(ConvMax2d, self).__init__()
        self.shape = shape
        if shape != 1:
            self.pad = nn.ConstantPad2d((0, shape-1, 0, shape-1), 0)
        else:
            self.pad = None
        self.conv = nn.Conv2d(channels, n_filters, shape)
        self.activation = nn.ReLU()
        self.k = k
        self.shape = shape
        self.channels = channels 
Example #21
Source File: networks.py    From cyclegan-qp with MIT License 5 votes vote down vote up
def __init__(self, in_channels, out_channels, kernel_size=3, stride=2, padding=1, upsample=None, output_padding=1):
        super(ConvTranspose2d, self).__init__()
        self.upsample = upsample
        if upsample:
            self.scale_factor = 4

        self.upsample_layer = F.interpolate
        reflection_pad = kernel_size // 2
        self.reflection_pad = nn.ConstantPad2d(reflection_pad, value=0)
        self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride, bias=False)
        self.convtrans2d = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding, output_padding, bias=False)

        # Initialize the weights with Xavier Glorot technique
        self.params_init() 
Example #22
Source File: layers01.py    From packnet-sfm with MIT License 5 votes vote down vote up
def __init__(self, in_channels, out_channels, kernel_size, stride):
        super().__init__()
        self.kernel_size = kernel_size
        self.conv_base = nn.Conv2d(
            in_channels, out_channels, kernel_size=kernel_size, stride=stride)
        self.pad = nn.ConstantPad2d([kernel_size // 2] * 4, value=0)
        self.normalize = torch.nn.GroupNorm(16, out_channels)
        self.activ = nn.ELU(inplace=True) 
Example #23
Source File: vgg16.py    From Distilling-Object-Detectors with MIT License 5 votes vote down vote up
def _init_modules(self):
    vgg = models.vgg16()
    if self.pretrained:
        print("Loading pretrained weights from %s" %(self.model_path))
        state_dict = torch.load(self.model_path)
        vgg.load_state_dict({k:v for k,v in state_dict.items() if k in vgg.state_dict()})

    vgg.classifier = nn.Sequential(*list(vgg.classifier._modules.values())[:-1])

    # not using the last maxpool layer
    self.RCNN_base = nn.Sequential(*list(vgg.features._modules.values())[:-1])

    # Fix the layers before conv3:
    for layer in range(10):
      for p in self.RCNN_base[layer].parameters(): p.requires_grad = False

    # self.RCNN_base = _RCNN_base(vgg.features, self.classes, self.dout_base_model)

    self.RCNN_top = vgg.classifier

    # not using the last maxpool layer
    self.RCNN_cls_score = nn.Linear(4096, self.n_classes)

    # self.stu_feature_adap = nn.Sequential(nn.Conv2d(512, 1024, kernel_size=3, padding=1),
    #                                       nn.ReLU(),
    #                                       nn.ConstantPad2d((0,1,0,1), 0.))
    #
    # self.stu_mask_pad = nn.ConstantPad2d((0, 1, 0, 1), 0.)


    if self.class_agnostic:
      self.RCNN_bbox_pred = nn.Linear(4096, 4)
    else:
      self.RCNN_bbox_pred = nn.Linear(4096, 4 * self.n_classes) 
Example #24
Source File: oth_prnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def padding_same_conv2d(input_size, in_c, out_c, kernel_size=4, stride=1):
    output_size = input_size // stride
    padding_num = stride * (output_size - 1) - input_size + kernel_size
    if padding_num % 2 == 0:
        return nn.Sequential(nn.Conv2d(in_c, out_c, kernel_size=kernel_size, stride=stride, padding=padding_num // 2, bias=False))
    else:
        return nn.Sequential(
            nn.ConstantPad2d((padding_num // 2, padding_num // 2 + 1, padding_num // 2, padding_num // 2 + 1), 0),
            nn.Conv2d(in_c, out_c, kernel_size=kernel_size, stride=stride, padding=0, bias=False)
        ) 
Example #25
Source File: resfcn256.py    From centerpose with MIT License 5 votes vote down vote up
def padding_same_conv2d(input_size, in_c, out_c, kernel_size=4, stride=1):
    output_size = input_size // stride
    padding_num = stride * (output_size - 1) - input_size + kernel_size
    if padding_num % 2 == 0:
        return nn.Sequential(nn.Conv2d(in_c, out_c, kernel_size=kernel_size, stride=stride, padding=padding_num // 2, bias=False))
    else:
        return nn.Sequential(
            nn.ConstantPad2d((padding_num // 2, padding_num // 2 + 1, padding_num // 2, padding_num // 2 + 1), 0),
            nn.Conv2d(in_c, out_c, kernel_size=kernel_size, stride=stride, padding=0, bias=False)
        ) 
Example #26
Source File: cspn.py    From DenseMatchingBenchmark with MIT License 5 votes vote down vote up
def get_pad_operation(self):
        if self.op in ['Conv2d']:
            lr = (self.dilation[1]) * (self.kernel_size[1] // 2)
            hw = (self.dilation[0]) * (self.kernel_size[0] // 2)
            self.pad_op = nn.ConstantPad2d((lr, lr, hw, hw), 0)
        if self.op in ['Conv3d']:
            lr = (self.dilation[2]) * (self.kernel_size[2] // 2)
            hw = (self.dilation[1]) * (self.kernel_size[1] // 2)
            fb = (self.dilation[0]) * (self.kernel_size[0] // 2)  # (front, back) => depth dimension
            self.pad_op = nn.ConstantPad3d((lr, lr, hw, hw, fb, fb), 0) 
Example #27
Source File: mesh_unpool.py    From MeshCNN with MIT License 5 votes vote down vote up
def pad_groups(self, group, unroll_start):
        start, end = group.shape
        padding_rows =  unroll_start - start
        padding_cols = self.unroll_target - end
        if padding_rows != 0 or padding_cols !=0:
            padding = nn.ConstantPad2d((0, padding_cols, 0, padding_rows), 0)
            group = padding(group)
        return group 
Example #28
Source File: mesh_union.py    From MeshCNN with MIT License 5 votes vote down vote up
def prepare_groups(self, features, mask):
        tensor_mask = torch.from_numpy(mask)
        self.groups = torch.clamp(self.groups[tensor_mask, :], 0, 1).transpose_(1, 0)
        padding_a = features.shape[1] - self.groups.shape[0]
        if padding_a > 0:
            padding_a = ConstantPad2d((0, 0, 0, padding_a), 0)
            self.groups = padding_a(self.groups) 
Example #29
Source File: mesh_union.py    From MeshCNN with MIT License 5 votes vote down vote up
def rebuild_features_average(self, features, mask, target_edges):
        self.prepare_groups(features, mask)
        fe = torch.matmul(features.squeeze(-1), self.groups)
        occurrences = torch.sum(self.groups, 0).expand(fe.shape)
        fe = fe / occurrences
        padding_b = target_edges - fe.shape[1]
        if padding_b > 0:
            padding_b = ConstantPad2d((0, padding_b, 0, 0), 0)
            fe = padding_b(fe)
        return fe 
Example #30
Source File: glu.py    From neural_sp with Apache License 2.0 4 votes vote down vote up
def __init__(self, kernel_size, in_ch, out_ch, bottlececk_dim=0, dropout=0.):
        super().__init__()

        self.conv_residual = None
        if in_ch != out_ch:
            self.conv_residual = nn.utils.weight_norm(
                nn.Conv2d(in_channels=in_ch,
                          out_channels=out_ch,
                          kernel_size=(1, 1)), name='weight', dim=0)
            self.dropout_residual = nn.Dropout(p=dropout)

        self.pad_left = nn.ConstantPad2d((0, 0, kernel_size - 1, 0), 0)

        layers = OrderedDict()
        if bottlececk_dim == 0:
            layers['conv'] = nn.utils.weight_norm(
                nn.Conv2d(in_channels=in_ch,
                          out_channels=out_ch * 2,
                          kernel_size=(kernel_size, 1)), name='weight', dim=0)
            # TODO(hirofumi0810): padding?
            layers['dropout'] = nn.Dropout(p=dropout)
            layers['glu'] = nn.GLU()

        elif bottlececk_dim > 0:
            layers['conv_in'] = nn.utils.weight_norm(
                nn.Conv2d(in_channels=in_ch,
                          out_channels=bottlececk_dim,
                          kernel_size=(1, 1)), name='weight', dim=0)
            layers['dropout_in'] = nn.Dropout(p=dropout)
            layers['conv_bottleneck'] = nn.utils.weight_norm(
                nn.Conv2d(in_channels=bottlececk_dim,
                          out_channels=bottlececk_dim,
                          kernel_size=(kernel_size, 1)), name='weight', dim=0)
            layers['dropout'] = nn.Dropout(p=dropout)
            layers['glu'] = nn.GLU()
            layers['conv_out'] = nn.utils.weight_norm(
                nn.Conv2d(in_channels=bottlececk_dim,
                          out_channels=out_ch * 2,
                          kernel_size=(1, 1)), name='weight', dim=0)
            layers['dropout_out'] = nn.Dropout(p=dropout)

        self.layers = nn.Sequential(layers)