Python torch.nn.UpsamplingBilinear2d() Examples

The following are 30 code examples of torch.nn.UpsamplingBilinear2d(). 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: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 7 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_shared, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(812, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(448, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(1536, reddim, kernel_size=1, padding=0)
        elif model == 'resnet50':
            self.Feat_ext = resnet50_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(3584, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #2
Source File: siammask_sharp.py    From SiamMask with MIT License 6 votes vote down vote up
def select_mask_logistic_loss(p_m, mask, weight, o_sz=63, g_sz=127):
    weight = weight.view(-1)
    pos = Variable(weight.data.eq(1).nonzero().squeeze())
    if pos.nelement() == 0: return p_m.sum() * 0, p_m.sum() * 0, p_m.sum() * 0, p_m.sum() * 0

    if len(p_m.shape) == 4:
        p_m = p_m.permute(0, 2, 3, 1).contiguous().view(-1, 1, o_sz, o_sz)
        p_m = torch.index_select(p_m, 0, pos)
        p_m = nn.UpsamplingBilinear2d(size=[g_sz, g_sz])(p_m)
        p_m = p_m.view(-1, g_sz * g_sz)
    else:
        p_m = torch.index_select(p_m, 0, pos)

    mask_uf = F.unfold(mask, (g_sz, g_sz), padding=0, stride=8)
    mask_uf = torch.transpose(mask_uf, 1, 2).contiguous().view(-1, g_sz * g_sz)

    mask_uf = torch.index_select(mask_uf, 0, pos)
    loss = F.soft_margin_loss(p_m, mask_uf)
    iou_m, iou_5, iou_7 = iou_measure(p_m, mask_uf)
    return loss, iou_m, iou_5, iou_7 
Example #3
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_individual, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext1 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext2 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext3 = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(232, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext1 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext2 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext3 = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(96, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext1 = vgg_base(loadweight,downsample)
            self.Feat_ext2 = vgg_base(loadweight,downsample)
            self.Feat_ext3 = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(512, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #4
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_shared, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(812, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(448, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(1536, reddim, kernel_size=1, padding=0)
        elif model == 'resnet50':
            self.Feat_ext = resnet50_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(3584, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #5
Source File: deeplab_resnet.py    From pytorch-deeplab-resnet with MIT License 6 votes vote down vote up
def forward(self,x):
        input_size = x.size()[2]
	self.interp1 = nn.UpsamplingBilinear2d(size = (  int(input_size*0.75)+1,  int(input_size*0.75)+1  ))
        self.interp2 = nn.UpsamplingBilinear2d(size = (  int(input_size*0.5)+1,   int(input_size*0.5)+1   ))
        self.interp3 = nn.UpsamplingBilinear2d(size = (  outS(input_size),   outS(input_size)   ))
        out = []
        x2 = self.interp1(x)
        x3 = self.interp2(x)
	out.append(self.Scale(x))	# for original scale
	out.append(self.interp3(self.Scale(x2)))	# for 0.75x scale
	out.append(self.Scale(x3))	# for 0.5x scale


        x2Out_interp = out[1]
        x3Out_interp = self.interp3(out[2])
        temp1 = torch.max(out[0],x2Out_interp)
        out.append(torch.max(temp1,x3Out_interp))
	return out 
Example #6
Source File: croppingModel.py    From Grid-Anchor-based-Image-Cropping-Pytorch with MIT License 6 votes vote down vote up
def __init__(self, alignsize = 8, reddim = 32, loadweight = True, model = None, downsample = 4):
        super(crop_model_multi_scale_individual, self).__init__()

        if model == 'shufflenetv2':
            self.Feat_ext1 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext2 = shufflenetv2_base(loadweight,downsample)
            self.Feat_ext3 = shufflenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(232, reddim, kernel_size=1, padding=0)
        elif model == 'mobilenetv2':
            self.Feat_ext1 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext2 = mobilenetv2_base(loadweight,downsample)
            self.Feat_ext3 = mobilenetv2_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(96, reddim, kernel_size=1, padding=0)
        elif model == 'vgg16':
            self.Feat_ext1 = vgg_base(loadweight,downsample)
            self.Feat_ext2 = vgg_base(loadweight,downsample)
            self.Feat_ext3 = vgg_base(loadweight,downsample)
            self.DimRed = nn.Conv2d(512, reddim, kernel_size=1, padding=0)

        self.downsample2 = nn.UpsamplingBilinear2d(scale_factor=1.0/2.0)
        self.upsample2 = nn.UpsamplingBilinear2d(scale_factor=2.0)
        self.RoIAlign = RoIAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.RoDAlign = RoDAlignAvg(alignsize, alignsize, 1.0/2**downsample)
        self.FC_layers = fc_layers(reddim*2, alignsize) 
Example #7
Source File: postprocess.py    From soccerontable with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def convert_network_prediction_to_depthmap(netpred, cropbox, output_nc=51):

    x1, y1, x2, y2 = cropbox[:4]

    crop_w = (x2 - x1)
    crop_h = (y2 - y1)
    upsampler = nn.UpsamplingBilinear2d(size=(int(crop_h), int(crop_w)))
    final_prediction = upsampler(netpred)
    final_prediction = np.argmax(final_prediction.cpu().data.numpy(), axis=1)[0, :, :]

    estimated_mask = np.zeros_like(final_prediction)
    I, J = (final_prediction > 0).nonzero()
    estimated_mask[I, J] = 1

    bins = np.linspace(-0.5, 0.5, output_nc - 1)
    estimated_depthmap = bins[final_prediction - 1]

    return estimated_depthmap, estimated_mask 
Example #8
Source File: ReDWebNet.py    From YouTube3D with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def forward(self, in_left, in_up):
        """
            Monocular Relative Depth Perception with Web Stereo Data Supervision
            Figure 4
        """
        x_left = self.conv_trans_left(in_left)
        x_left = self.bn_trans_left(x_left)        
        x_left = self.resConv_left(x_left)
        

        x_up = self.conv_trans_up(in_up)
        x_up = self.bn_trans_up(x_up)        

        x = x_left + x_up

        x = self.resConv_down(x)
        x = nn.UpsamplingBilinear2d(scale_factor=2)(x)
                       
        return x 
Example #9
Source File: siammask.py    From SiamMask with MIT License 6 votes vote down vote up
def select_mask_logistic_loss(p_m, mask, weight, o_sz=63, g_sz=127):
    weight = weight.view(-1)
    pos = Variable(weight.data.eq(1).nonzero().squeeze())
    if pos.nelement() == 0: return p_m.sum() * 0, p_m.sum() * 0, p_m.sum() * 0, p_m.sum() * 0

    p_m = p_m.permute(0, 2, 3, 1).contiguous().view(-1, 1, o_sz, o_sz)
    p_m = torch.index_select(p_m, 0, pos)
    p_m = nn.UpsamplingBilinear2d(size=[g_sz, g_sz])(p_m)
    p_m = p_m.view(-1, g_sz * g_sz)

    mask_uf = F.unfold(mask, (g_sz, g_sz), padding=32, stride=8)
    mask_uf = torch.transpose(mask_uf, 1, 2).contiguous().view(-1, g_sz * g_sz)

    mask_uf = torch.index_select(mask_uf, 0, pos)
    loss = F.soft_margin_loss(p_m, mask_uf)
    iou_m, iou_5, iou_7 = iou_measure(p_m, mask_uf)
    return loss, iou_m, iou_5, iou_7 
Example #10
Source File: model.py    From FeatureFlow with MIT License 6 votes vote down vote up
def __init__(self, inchannel, outchannel, upsampling=False, end=False):
        """
        Reverse Vgg19_bn block
        :param inchannel: input channel
        :param outchannel: output channel
        :param upsampling: judge for adding upsampling module
        :param padding: padding mode: 'zero', 'reflect', by default:'reflect'
        """
        super(ReVggBlock, self).__init__()

        model = []
        model += [nn.ReplicationPad2d(1)]
        model += [nn.Conv2d(inchannel, outchannel, 3)]

        if upsampling:
            model += [nn.UpsamplingBilinear2d(scale_factor=2)]

        if not end:
            model += [nn.LeakyReLU(True), nn.BatchNorm2d(outchannel)]

        self.model = nn.Sequential(*model) 
Example #11
Source File: deeplab_resnet.py    From MaskTrack with MIT License 6 votes vote down vote up
def forward(self,x):
        input_size_1 = x.size()[2]
        input_size_2 = x.size()[3]
        #print(x.size())

        self.interp1 = nn.UpsamplingBilinear2d(size = (int(input_size_1*0.75)+1, int(input_size_2*0.75)+1))
        self.interp2 = nn.UpsamplingBilinear2d(size = (int(input_size_1*0.5)+1, int(input_size_2*0.5)+1))
        self.interp3 = nn.UpsamplingBilinear2d(size = (outS(input_size_1), outS(input_size_2)))

        out = []
        x2 = self.interp1(x)
        x3 = self.interp2(x)
        out.append(self.Scale(x))	# for original scale
        #print(out[0].shape)

        out.append(self.interp3(self.Scale(x2)))	# for 0.75x scale
        #print(out[1].shape)

        out.append(self.interp3(self.Scale(x3)))	# for 0.5x scale

        x2Out_interp = out[1]
        x3Out_interp = out[2]
        temp1 = torch.max(out[0], x2Out_interp)
        out.append(torch.max(temp1, x3Out_interp))
        return out 
Example #12
Source File: deeplab_resnet_2D.py    From pytorch-mri-segmentation-3D with MIT License 6 votes vote down vote up
def forward(self,x):
        input_size = x.size()[2]
        self.interp1 = nn.UpsamplingBilinear2d(size = (  int(input_size*0.75)+1,  int(input_size*0.75)+1  ))
        self.interp2 = nn.UpsamplingBilinear2d(size = (  int(input_size*0.5)+1,   int(input_size*0.5)+1   ))
        self.interp3 = nn.UpsamplingBilinear2d(size = (  outS(input_size),   outS(input_size)   ))

        out = []
        x2 = self.interp1(x)        
        x3 = self.interp2(x)

        out.append(self.Scale(x))                   #1.0x
        out.append(self.interp3(self.Scale(x2)))    #0.75x
        out.append(self.interp3(self.Scale(x3)))    #0.5x
        #out.append(self.Scale(x3))  # for 0.5x scale
        
        x2Out_interp = out[1]
        x3Out_interp = out[2]
        temp1 = torch.max(out[0],x2Out_interp)
        out.append(torch.max(temp1,x3Out_interp))
        return out 
Example #13
Source File: utils.py    From CAG_UDA with MIT License 5 votes vote down vote up
def __init__(self, in_size, out_size, is_deconv):
        super(unetUp, self).__init__()
        self.conv = unetConv2(in_size, out_size, False)
        if is_deconv:
            self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2)
        else:
            self.up = nn.UpsamplingBilinear2d(scale_factor=2) 
Example #14
Source File: keypoint_rcnn_heads.py    From detectron-self-train with MIT License 5 votes vote down vote up
def __init__(self, dim_in):
        super().__init__()
        self.upsample_heatmap = (cfg.KRCNN.UP_SCALE > 1)

        if cfg.KRCNN.USE_DECONV:
            # Apply ConvTranspose to the feature representation; results in 2x # upsampling
            self.deconv = nn.ConvTranspose2d(
                dim_in, cfg.KRCNN.DECONV_DIM, cfg.KRCNN.DECONV_KERNEL,
                2, padding=int(cfg.KRCNN.DECONV_KERNEL / 2) - 1)
            dim_in = cfg.KRCNN.DECONV_DIM

        if cfg.KRCNN.USE_DECONV_OUTPUT:
            # Use ConvTranspose to predict heatmaps; results in 2x upsampling
            self.classify = nn.ConvTranspose2d(
                dim_in, cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.DECONV_KERNEL,
                2, padding=int(cfg.KRCNN.DECONV_KERNEL / 2 - 1))
        else:
            # Use Conv to predict heatmaps; does no upsampling
            self.classify = nn.Conv2d(dim_in, cfg.KRCNN.NUM_KEYPOINTS, 1, 1, padding=0)

        if self.upsample_heatmap:
            # self.upsample = nn.UpsamplingBilinear2d(scale_factor=cfg.KRCNN.UP_SCALE)
            self.upsample = mynn.BilinearInterpolation2d(
                cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.UP_SCALE)

        self._init_weights() 
Example #15
Source File: utils.py    From efficientdensenet_crnn with MIT License 5 votes vote down vote up
def assureRatio(img):
    """Ensure imgH <= imgW."""
    b, c, h, w = img.size()
    if h > w:
        main = nn.UpsamplingBilinear2d(size=(h, h), scale_factor=None)
        img = main(img)
    return img 
Example #16
Source File: siammask_sharp.py    From SiamMask with MIT License 5 votes vote down vote up
def __init__(self, anchors=None, o_sz=127, g_sz=127):
        super(SiamMask, self).__init__()
        self.anchors = anchors  # anchor_cfg
        self.anchor_num = len(self.anchors["ratios"]) * len(self.anchors["scales"])
        self.anchor = Anchors(anchors)
        self.features = None
        self.rpn_model = None
        self.mask_model = None
        self.o_sz = o_sz
        self.g_sz = g_sz
        self.upSample = nn.UpsamplingBilinear2d(size=[g_sz, g_sz])

        self.all_anchors = None 
Example #17
Source File: siammask.py    From SiamMask with MIT License 5 votes vote down vote up
def __init__(self, anchors=None, o_sz=63, g_sz=127):
        super(SiamMask, self).__init__()
        self.anchors = anchors  # anchor_cfg
        self.anchor_num = len(self.anchors["ratios"]) * len(self.anchors["scales"])
        self.anchor = Anchors(anchors)
        self.features = None
        self.rpn_model = None
        self.mask_model = None
        self.o_sz = o_sz
        self.g_sz = g_sz
        self.upSample = nn.UpsamplingBilinear2d(size=[g_sz, g_sz])

        self.all_anchors = None 
Example #18
Source File: layers.py    From LiverCancerSeg with MIT License 5 votes vote down vote up
def __init__(self, in_size, out_size, is_deconv):
        super(unetUp, self).__init__()
        self.conv = unetConv2(in_size, out_size, False)
        if is_deconv:
            self.up = nn.ConvTranspose2d(in_size, out_size, kernel_size=2, stride=2)
        else:
            self.up = nn.UpsamplingBilinear2d(scale_factor=2) 
Example #19
Source File: ReDWebNet.py    From YouTube3D with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def forward(self,x):
        # the input resolution is supposed to be 384 x 384        
        grey96_96_256, grey48_48_512, grey24_24_1024, grey12_12_2048 = self.resnet_model(x)

        blue24_24_2048 = nn.UpsamplingBilinear2d(size=grey24_24_1024.size()[2:])(grey12_12_2048)

        blue48_48 = self.feafu3(in_left = grey24_24_1024, in_up = blue24_24_2048)

        blue96_96 = self.feafu2(in_left = grey48_48_512, in_up = blue48_48)

        blue192_192 = self.feafu1(in_left = grey96_96_256, in_up = blue96_96)

        out = self.ada_out(blue192_192)

        return out 
Example #20
Source File: utils.py    From crnn.pytorch with MIT License 5 votes vote down vote up
def assureRatio(img):
    """Ensure imgH <= imgW."""
    b, c, h, w = img.size()
    if h > w:
        main = nn.UpsamplingBilinear2d(size=(h, h), scale_factor=None)
        img = main(img)
    return img 
Example #21
Source File: model.py    From finegan with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self):
        super(G_NET, self).__init__()
        self.gf_dim = cfg.GAN.GF_DIM
        self.define_module()
	self.upsampling = Upsample(scale_factor = 2, mode = 'bilinear')
	self.scale_fimg = nn.UpsamplingBilinear2d(size = [126, 126]) 
Example #22
Source File: ReDWebNet.py    From YouTube3D with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def forward(self,x):
        # the input resolution is supposed to be 384 x 384        
        grey96_96_256, grey48_48_512, grey24_24_1024, grey12_12_2048 = self.resnet_model(x)

        blue24_24_2048 = nn.UpsamplingBilinear2d(size=grey24_24_1024.size()[2:])(grey12_12_2048)

        blue48_48 = self.feafu3(in_left = grey24_24_1024, in_up = blue24_24_2048)

        blue96_96 = self.feafu2(in_left = grey48_48_512, in_up = blue48_48)

        blue192_192 = self.feafu1(in_left = grey96_96_256, in_up = blue96_96)

        out = self.ada_out(blue192_192)

        return out 
Example #23
Source File: ReDWebNet.py    From YouTube3D with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def forward(self,x):
        # the input resolution is supposed to be 384 x 384        
        grey96_96_256, grey48_48_512, grey24_24_1024, grey12_12_2048 = self.resnet_model(x)

        blue24_24_2048 = nn.UpsamplingBilinear2d(size=grey24_24_1024.size()[2:])(grey12_12_2048)

        blue48_48 = self.feafu3(in_left = grey24_24_1024, in_up = blue24_24_2048)

        blue96_96 = self.feafu2(in_left = grey48_48_512, in_up = blue48_48)

        blue192_192 = self.feafu1(in_left = grey96_96_256, in_up = blue96_96)

        out = self.ada_out(blue192_192)

        return out 
Example #24
Source File: ReDWebNet.py    From YouTube3D with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def forward(self, x):
        # Monocular Relative Depth Perception with Web Stereo Data Supervision
        # Figure 4
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.conv2(x)
        x = nn.UpsamplingBilinear2d(scale_factor=2)(x)
                       
        return x 
Example #25
Source File: utils.py    From PAN-PSEnet with Apache License 2.0 5 votes vote down vote up
def assureRatio(img):
    """Ensure imgH <= imgW."""
    b, c, h, w = img.size()
    if h > w:
        main = nn.UpsamplingBilinear2d(size=(h, h), scale_factor=None)
        img = main(img)
    return img 
Example #26
Source File: keypoint_rcnn_heads.py    From Context-aware-ZSR with MIT License 5 votes vote down vote up
def __init__(self, dim_in):
        super().__init__()
        self.upsample_heatmap = (cfg.KRCNN.UP_SCALE > 1)

        if cfg.KRCNN.USE_DECONV:
            # Apply ConvTranspose to the feature representation; results in 2x # upsampling
            self.deconv = nn.ConvTranspose2d(
                dim_in, cfg.KRCNN.DECONV_DIM, cfg.KRCNN.DECONV_KERNEL,
                2, padding=int(cfg.KRCNN.DECONV_KERNEL / 2) - 1)
            dim_in = cfg.KRCNN.DECONV_DIM

        if cfg.KRCNN.USE_DECONV_OUTPUT:
            # Use ConvTranspose to predict heatmaps; results in 2x upsampling
            self.classify = nn.ConvTranspose2d(
                dim_in, cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.DECONV_KERNEL,
                2, padding=int(cfg.KRCNN.DECONV_KERNEL / 2 - 1))
        else:
            # Use Conv to predict heatmaps; does no upsampling
            self.classify = nn.Conv2d(dim_in, cfg.KRCNN.NUM_KEYPOINTS, 1, 1, padding=0)

        if self.upsample_heatmap:
            # self.upsample = nn.UpsamplingBilinear2d(scale_factor=cfg.KRCNN.UP_SCALE)
            self.upsample = mynn.BilinearInterpolation2d(
                cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.UP_SCALE)

        self._init_weights() 
Example #27
Source File: keypoint_rcnn_heads.py    From Detectron.pytorch with MIT License 5 votes vote down vote up
def __init__(self, dim_in):
        super().__init__()
        self.upsample_heatmap = (cfg.KRCNN.UP_SCALE > 1)

        if cfg.KRCNN.USE_DECONV:
            # Apply ConvTranspose to the feature representation; results in 2x # upsampling
            self.deconv = nn.ConvTranspose2d(
                dim_in, cfg.KRCNN.DECONV_DIM, cfg.KRCNN.DECONV_KERNEL,
                2, padding=int(cfg.KRCNN.DECONV_KERNEL / 2) - 1)
            dim_in = cfg.KRCNN.DECONV_DIM

        if cfg.KRCNN.USE_DECONV_OUTPUT:
            # Use ConvTranspose to predict heatmaps; results in 2x upsampling
            self.classify = nn.ConvTranspose2d(
                dim_in, cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.DECONV_KERNEL,
                2, padding=int(cfg.KRCNN.DECONV_KERNEL / 2 - 1))
        else:
            # Use Conv to predict heatmaps; does no upsampling
            self.classify = nn.Conv2d(dim_in, cfg.KRCNN.NUM_KEYPOINTS, 1, 1, padding=0)

        if self.upsample_heatmap:
            # self.upsample = nn.UpsamplingBilinear2d(scale_factor=cfg.KRCNN.UP_SCALE)
            self.upsample = mynn.BilinearInterpolation2d(
                cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.NUM_KEYPOINTS, cfg.KRCNN.UP_SCALE)

        self._init_weights() 
Example #28
Source File: model.py    From FeatureFlow with MIT License 5 votes vote down vote up
def __init__(self, inchannel, dg=2):
        super(DeformableConv, self).__init__()
        self.dg = dg

        self.offset_cnn1 = nn.Sequential(nn.Conv2d(2 * inchannel, inchannel, 3, padding=1), nn.BatchNorm2d(inchannel), nn.LeakyReLU(True))
        self.offset_cnn2 = nn.Sequential(nn.Conv2d(2 * 2 * 2 * dg * 9, 2 * 2 * dg * 9, 3, padding=1), nn.BatchNorm2d(2 * 2 * dg * 9), nn.LeakyReLU(True))
        self.offset_cnn3 = nn.Sequential(*([ResnetBlock_bn(inchannel)] * 5 + [ResnetBlock(inchannel)] * 3 + [nn.Conv2d(inchannel, 2 * 2 * dg * 9, 3, padding=1)]))

        self.emb = nn.Conv2d(inchannel, inchannel, 3, padding=1)
        self.mix_map = nn.Sequential(nn.Conv2d(2 * inchannel, inchannel, 3, padding=1), nn.LeakyReLU(True), *([ResnetBlock(inchannel)] * 3), nn.Conv2d(inchannel, 2 * dg, 3, padding=1))

        self.dcn = DeformConv(inchannel, inchannel, 3, padding=1, deformable_groups=dg)
        self.up = nn.UpsamplingBilinear2d(scale_factor=2) 
Example #29
Source File: attention.py    From Torchelie with MIT License 5 votes vote down vote up
def __init__(self, ch, inner):
        super(UBlock, self).__init__()
        self.inner = inner
        if inner is not None:
            self.skip = Block(ch, ch)
        self.encode = tnn.CondSeq(nn.MaxPool2d(3, 2, 1), Block(ch, ch))
        self.decode = tnn.CondSeq(Block(ch, ch),
                                  nn.UpsamplingBilinear2d(scale_factor=2)) 
Example #30
Source File: segment.py    From drn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, model_name, classes, pretrained_model=None,
                 pretrained=True, use_torch_up=False):
        super(DRNSeg, self).__init__()
        model = drn.__dict__.get(model_name)(
            pretrained=pretrained, num_classes=1000)
        pmodel = nn.DataParallel(model)
        if pretrained_model is not None:
            pmodel.load_state_dict(pretrained_model)
        self.base = nn.Sequential(*list(model.children())[:-2])

        self.seg = nn.Conv2d(model.out_dim, classes,
                             kernel_size=1, bias=True)
        self.softmax = nn.LogSoftmax()
        m = self.seg
        n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
        m.weight.data.normal_(0, math.sqrt(2. / n))
        m.bias.data.zero_()
        if use_torch_up:
            self.up = nn.UpsamplingBilinear2d(scale_factor=8)
        else:
            up = nn.ConvTranspose2d(classes, classes, 16, stride=8, padding=4,
                                    output_padding=0, groups=classes,
                                    bias=False)
            fill_up_weights(up)
            up.weight.requires_grad = False
            self.up = up