jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def forward(self, x):
        x1 = self.theta(x)
        x2 = self.phi(x)

        n, c, h, w = x1.shape

        x1 = x1.view(n, c, -1)
        x1 = x1.permute(0, 2, 1)
        x2 = x2.view(n, c, -1)
        f = torch.matmul(x1, x2)
        f_div_C = F.softmax(f, dim=-1)

        xx = self.ksi(x).view(n, c, -1)
        xx = xx.permute(0, 2, 1)
        y = torch.matmul(f_div_C, xx)
        y = y.permute(0, 2, 1).contiguous()
        y = y.view(n, c, *x.size()[2:])

        y_down = F.interpolate(y, scale_factor=0.5, mode='bilinear')

        z = y_down + x

        return z
//  [python]
def _upsample_add(self, x, y):
        _, _, H, W = y.size()
        return F.upsample(x, size=(H, W), mode='bilinear') + y
//  [python]
def _upsample_add(self, x, y):
        _, _, H, W = y.size()
        return F.interpolate(x, size=(H, W), mode='bilinear') + y
//  [python]
def _upsample(self, x, y, scale=1):
        _, _, H, W = y.size()
        return F.upsample(x, size=(H // scale, W // scale), mode='bilinear')
//  [python]
def _upsample(self, x, y, scale=1):
        _, _, H, W = y.size()
        return F.interpolate(x, size=(H // scale, W // scale), mode='bilinear')
//  [python]
def predict_whole_img(net, image, classes, method, scale):
    """
         Predict the whole image w/o using multiple crops.
         The scale specify whether rescale the input image before predicting the results.
    """
    N_, C_, H_, W_ = image.shape
    if torch_ver == '0.4':
        interp = nn.Upsample(size=(H_, W_), mode='bilinear', align_corners=True)
    else:
        interp = nn.Upsample(size=(H_, W_), mode='bilinear')
    if scale != 1:
        scaled_img = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
    else:
        scaled_img = image
    
    full_prediction_ = net(Variable(torch.from_numpy(scaled_img), volatile=True).cuda(), )
    if 'dsn' in method or 'center' in method or 'fuse' in method:
        full_prediction = full_prediction_[-1]
    else:
        full_prediction = full_prediction_

    if torch_ver == '0.4':
        full_prediction = F.upsample(input=full_prediction, size=(H_, W_), mode='bilinear', align_corners=True)
    else:
        full_prediction = F.upsample(input=full_prediction, size=(H_, W_), mode='bilinear')
    result = full_prediction.cpu().data.numpy().transpose(0,2,3,1)
    return result
//  [python]
def predict_whole_img(net, image, classes, method, scale):
    """
         Predict the whole image w/o using multiple crops.
         The scale specify whether rescale the input image before predicting the results.
    """
    N_, C_, H_, W_ = image.shape
    if torch_ver == '0.4':
        interp = nn.Upsample(size=(H_, W_), mode='bilinear', align_corners=True)
    else:
        interp = nn.Upsample(size=(H_, W_), mode='bilinear')
    if scale != 1:
        scaled_img = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
    else:
        scaled_img = image
    
    full_prediction_ = net(Variable(torch.from_numpy(scaled_img), volatile=True).cuda(), )
    if 'dsn' in method or 'center' in method or 'fuse' in method:
        full_prediction = full_prediction_[-1]
    else:
        full_prediction = full_prediction_

    if torch_ver == '0.4':
        full_prediction = F.interpolate(input=full_prediction, size=(H_, W_), mode='bilinear', align_corners=True)
    else:
        full_prediction = F.interpolate(input=full_prediction, size=(H_, W_), mode='bilinear')
    result = full_prediction.cpu().data.numpy().transpose(0,2,3,1)
    return result
//  [python]
def predict_whole_img_w_label(net, image, classes, method, scale, label):
    """
         Predict the whole image w/o using multiple crops.
         The scale specify whether rescale the input image before predicting the results.
    """
    N_, C_, H_, W_ = image.shape
    if torch_ver == '0.4':
        interp = nn.Upsample(size=(H_, W_), mode='bilinear', align_corners=True)
    else:
        interp = nn.Upsample(size=(H_, W_), mode='bilinear')

#     bug
#     if scale > 1:
#         scaled_img = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
#     else:
#         scaled_img = image

    scaled_img = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
    
    full_prediction_ = net(Variable(torch.from_numpy(scaled_img), volatile=True).cuda(), label)
    if 'dsn' in method or 'center' in method or 'fuse' in method:
        full_prediction = full_prediction_[-1]
    else:
        full_prediction = full_prediction_

    full_prediction = F.upsample(input=full_prediction, size=(H_, W_), mode='bilinear', align_corners=True)
    result = full_prediction.cpu().data.numpy().transpose(0,2,3,1)
    return result
//  [python]
def predict_whole_img_w_label(net, image, classes, method, scale, label):
    """
         Predict the whole image w/o using multiple crops.
         The scale specify whether rescale the input image before predicting the results.
    """
    N_, C_, H_, W_ = image.shape
    if torch_ver == '0.4':
        interp = nn.Upsample(size=(H_, W_), mode='bilinear', align_corners=True)
    else:
        interp = nn.Upsample(size=(H_, W_), mode='bilinear')

#     bug
#     if scale > 1:
#         scaled_img = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
#     else:
#         scaled_img = image

    scaled_img = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
    
    full_prediction_ = net(Variable(torch.from_numpy(scaled_img), volatile=True).cuda(), label)
    if 'dsn' in method or 'center' in method or 'fuse' in method:
        full_prediction = full_prediction_[-1]
    else:
        full_prediction = full_prediction_

    full_prediction = F.interpolate(input=full_prediction, size=(H_, W_), mode='bilinear', align_corners=True)
    result = full_prediction.cpu().data.numpy().transpose(0,2,3,1)
    return result
//  [python]
def forward(self, x, phase=None):
        feat = []
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)              #(2L, 256L, 129L, 129L)
        x = self.layer2(x)              #(2L, 512L, 65L, 65L)
        feat += [x]
        feat0 = x
        x = self.layer3(x)              #(2L, 1024L, 33L, 33L)
        feat += [x]
        feat1 = x
        x = self.layer4(x)
        feat += [x]
        feat2 = x
        for k, v in enumerate(self.extra_layers):
            x = v(x)
            if k in [5, 11, 17, 23]:
                feat += [x]

        ########## fusion #################################################
        feat0 = self.fusion_layers[0](feat0)
        feat1 = F.upsample_bilinear(self.fusion_layers[1](feat1),size=(65,65))
        feat2 = F.upsample_bilinear(self.fusion_layers[2](feat2),size=(65,65))
        feat[0] = F.relu(self.fusion_conv(self.fusion_bn(torch.cat([feat0, feat1, feat2], dim=1))))
        ##################################################################
        feat_new = []
        for (x,l) in zip(feat, self.att_layers):
            feat_new.append(l(x))
        ########## PreEnd #################################################
        locs = []
        conf = []
        for (x, l, c) in zip(feat_new, self.locs_layers, self.conf_layers):
            locs += [l(x).permute(0,2,3,1).contiguous()]
            conf += [c(x).permute(0,2,3,1).contiguous()]

        locs = torch.cat([o.view(o.size(0),-1) for o in locs], dim=1)
        conf = torch.cat([o.view(o.size(0),-1) for o in conf], dim=1)

        if phase == 'test':
            output = self.detect(locs.view(locs.size(0), -1, 4),
                                 self.softmax(conf.view(-1, self.num_classes)),
                                 self.prior_boxes.type(type(x.data)))
        else:
            output = (locs.view(locs.size(0),-1,4),
                      conf.view(conf.size(0),-1,self.num_classes),
                      self.prior_boxes)
        return output
//  [python]
def forward(self, x, phase=None):
        feat = []
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)              #(2L, 256L, 129L, 129L)
        x = self.layer2(x)              #(2L, 512L, 65L, 65L)
        feat += [x]
        feat0 = x
        x = self.layer3(x)              #(2L, 1024L, 33L, 33L)
        feat += [x]
        feat1 = x
        x = self.layer4(x)
        feat += [x]
        feat2 = x
        for k, v in enumerate(self.extra_layers):
            x = v(x)
            if k in [5, 11, 17, 23]:
                feat += [x]

        ########## fusion #################################################
        feat0 = self.fusion_layers[0](feat0)
        feat1 = F.interpolate(self.fusion_layers[1](feat1),size=(65,65), mode='bilinear', align_corners=False)
        feat2 = F.interpolate(self.fusion_layers[2](feat2),size=(65,65), mode='bilinear', align_corners=False)
        feat[0] = F.relu(self.fusion_conv(self.fusion_bn(torch.cat([feat0, feat1, feat2], dim=1))))
        ##################################################################
        feat_new = []
        for (x,l) in zip(feat, self.att_layers):
            feat_new.append(l(x))
        ########## PreEnd #################################################
        locs = []
        conf = []
        for (x, l, c) in zip(feat_new, self.locs_layers, self.conf_layers):
            locs += [l(x).permute(0,2,3,1).contiguous()]
            conf += [c(x).permute(0,2,3,1).contiguous()]

        locs = torch.cat([o.view(o.size(0),-1) for o in locs], dim=1)
        conf = torch.cat([o.view(o.size(0),-1) for o in conf], dim=1)

        if phase == 'test':
            output = self.detect(locs.view(locs.size(0), -1, 4),
                                 self.softmax(conf.view(-1, self.num_classes)),
                                 self.prior_boxes.type(type(x.data)))
        else:
            output = (locs.view(locs.size(0),-1,4),
                      conf.view(conf.size(0),-1,self.num_classes),
                      self.prior_boxes)
        return output
//  [python]
def pad(self, x):
        if "reflect-start" in self.pad_type:
            return self.pad_input(x)
        elif "resize-start" in self.pad_type:
            n, c, h, w = x.shape
            return F.upsample_bilinear(x, (self.diff_h + h, self.diff_w + w))
        else:
            return x
//  [python]
def pad(self, x):
        if "reflect-start" in self.pad_type:
            return self.pad_input(x)
        elif "resize-start" in self.pad_type:
            n, c, h, w = x.shape
            return F.interpolate(x, (self.diff_h + h, self.diff_w + w), mode='bilinear')
        else:
            return x
//  [python]
def forward(self,x):
        residual=x
        x_1=x=self.block1(x)
        x=self.block2(x) 
        x_2=self.STR_module2(x)
        x_2=F.upsample(x_2,scale_factor=2,mode='bilinear') 
        x=torch.cat([x_1,x_2],dim=1) #block 11
        x=self.pw_final(x)
        return x+residual
//  [python]
def forward(self,x):
        residual=x
        x_1=x=self.block1(x)
        x=self.block2(x) 
        x_2=self.STR_module2(x)
        x_2=F.interpolate(x_2,scale_factor=2,mode='bilinear') 
        x=torch.cat([x_1,x_2],dim=1) #block 11
        x=self.pw_final(x)
        return x+residual
//  [python]
def forward(self, x,y):
        x = self.stage1(x)
        x2 = self.stage2(x).detach()
        x3 = self.stage3(x2)
        x = self.stage4(x3)

        y = self.stage1(y)
        y2 = self.stage2(y).detach()
        y3 = self.stage3(y2)
        y = self.stage4(y3)

        x = self.downsample(x)
        y = self.downsample(y)

        x_s = self.self_atten_x(x,x)
        y_s = self.self_atten_y(y,y)

        support_x = (x / (F.adaptive_max_pool2d(x, (1, 1)) + 1e-5))  # Normal
        support_y = (y / (F.adaptive_max_pool2d(y, (1, 1)) + 1e-5))

        ## Define foreground mask
        support_x_fg = torch.ge(support_x, 0.3) * 1.
        support_x_fg, _ = torch.max(support_x_fg, dim=1, keepdim=True)

        support_y_fg = torch.ge(support_y, 0.3) * 1.
        support_y_fg, _ = torch.max(support_y_fg, dim=1, keepdim=True)

        support_x_mask_fg, support_y_mask_fg = self.cal_mask(x,y,support_x_fg,support_y_fg)

        x2 = F.upsample_bilinear(x2,size=x.shape[-2:])
        y2 = F.upsample_bilinear(y2,size=y.shape[-2:])
        
        support_x_mask_fg2, support_y_mask_fg2 = self.cal_mask(x2, y2, support_x_fg, support_y_fg)

        support_x_mask_fg = torch.max(support_x_mask_fg, support_x_mask_fg2)
        support_y_mask_fg = torch.max(support_y_mask_fg, support_y_mask_fg2)

        x_mask = (support_x_mask_fg / (F.adaptive_max_pool2d(support_x_mask_fg, (1, 1)) + 1e-5))  * 1.3
        y_mask = (support_y_mask_fg / (F.adaptive_max_pool2d(support_y_mask_fg, (1, 1)) + 1e-5))  * 1.3

        x_mask = self.gauss(x_mask) * x
        y_mask = self.gauss(y_mask) * y

        x_mask = self.cross_atten_x(x_mask,x_mask)
        y_mask = self.cross_atten_y(y_mask,y_mask)

        x_mask = self.classifierx(x_mask)
        y_mask = self.classifiery(y_mask)

        x = self.classifierx(x_s)
        y = self.classifiery(y_s)
        return x,y,x_mask,y_mask
//  [python]
def forward(self, x,y):
        x = self.stage1(x)
        x2 = self.stage2(x).detach()
        x3 = self.stage3(x2)
        x = self.stage4(x3)

        y = self.stage1(y)
        y2 = self.stage2(y).detach()
        y3 = self.stage3(y2)
        y = self.stage4(y3)

        x = self.downsample(x)
        y = self.downsample(y)

        x_s = self.self_atten_x(x,x)
        y_s = self.self_atten_y(y,y)

        support_x = (x / (F.adaptive_max_pool2d(x, (1, 1)) + 1e-5))  # Normal
        support_y = (y / (F.adaptive_max_pool2d(y, (1, 1)) + 1e-5))

        ## Define foreground mask
        support_x_fg = torch.ge(support_x, 0.3) * 1.
        support_x_fg, _ = torch.max(support_x_fg, dim=1, keepdim=True)

        support_y_fg = torch.ge(support_y, 0.3) * 1.
        support_y_fg, _ = torch.max(support_y_fg, dim=1, keepdim=True)

        support_x_mask_fg, support_y_mask_fg = self.cal_mask(x,y,support_x_fg,support_y_fg)

        x2 = F.interpolate(x2,size=x.shape[-2:], mode='bilinear')
        y2 = F.interpolate(y2,size=y.shape[-2:], mode='bilinear')
        
        support_x_mask_fg2, support_y_mask_fg2 = self.cal_mask(x2, y2, support_x_fg, support_y_fg)

        support_x_mask_fg = torch.max(support_x_mask_fg, support_x_mask_fg2)
        support_y_mask_fg = torch.max(support_y_mask_fg, support_y_mask_fg2)

        x_mask = (support_x_mask_fg / (F.adaptive_max_pool2d(support_x_mask_fg, (1, 1)) + 1e-5))  * 1.3
        y_mask = (support_y_mask_fg / (F.adaptive_max_pool2d(support_y_mask_fg, (1, 1)) + 1e-5))  * 1.3

        x_mask = self.gauss(x_mask) * x
        y_mask = self.gauss(y_mask) * y

        x_mask = self.cross_atten_x(x_mask,x_mask)
        y_mask = self.cross_atten_y(y_mask,y_mask)

        x_mask = self.classifierx(x_mask)
        y_mask = self.classifiery(y_mask)

        x = self.classifierx(x_s)
        y = self.classifiery(y_s)
        return x,y,x_mask,y_mask
//  [python]
def forward(self, x):
      
        convx=x=self.conv_stem(self.stem(x))
  
        # x,x_up,x_aux=self.STR(x)
        x,x_up=self.STR(x)
  
        x_coarse=self.attention1(x,x)
        
        x_coarse=self.attention2(x_coarse,x_coarse)
        
        # x_aux=F.upsample_bilinear(self.attention3(x_aux,x_aux),scale_factor=2)
        # x_aux=F.upsample_bilinear(x_aux,scale_factor=4)

        x_coarse=self.coarse_up(x_coarse)

        x_up=F.upsample(x_up,scale_factor=2,mode='bilinear')
        # x=self.Cat(convx,x_coarse,x_up,x_aux)
        x=self.Cat(convx,x_coarse,x_up)
        x=x.squeeze(1)
        return x
//  [python]
def forward(self, x):
      
        convx=x=self.conv_stem(self.stem(x))
  
        # x,x_up,x_aux=self.STR(x)
        x,x_up=self.STR(x)
  
        x_coarse=self.attention1(x,x)
        
        x_coarse=self.attention2(x_coarse,x_coarse)
        
        # x_aux=F.interpolate(self.attention3(x_aux,x_aux),scale_factor=2,mode='bilinear')
        # x_aux=F.interpolate(x_aux,scale_factor=4,mode='bilinear')

        x_coarse=self.coarse_up(x_coarse)

        x_up=F.interpolate(x_up,scale_factor=2,mode='bilinear')
        # x=self.Cat(convx,x_coarse,x_up,x_aux)
        x=self.Cat(convx,x_coarse,x_up)
        x=x.squeeze(1)
        return x
//  [python]
def get_mask_images(input_images, attentions, theta_mask=0.5):
    batch_size, _, height, weight = input_images.size()
    masks = []
    for i in range(batch_size):
        attention = attentions[i:i + 1]
        if isinstance(theta_mask, tuple):
            temp1 = random.uniform(*theta_mask)
            temp2 = attention.max()
            theta = temp1 * temp2
        else:
            theta = theta_mask * attention.max()

        masks.append(F.upsample_bilinear(attention, size=(height, weight)) < theta)
    masks = torch.cat(masks, dim=0)
    mask_images = input_images * masks.float()

    return mask_images
//  [python]
def get_mask_images(input_images, attentions, theta_mask=0.5):
    batch_size, _, height, weight = input_images.size()
    masks = []
    for i in range(batch_size):
        attention = attentions[i:i + 1]
        if isinstance(theta_mask, tuple):
            temp1 = random.uniform(*theta_mask)
            temp2 = attention.max()
            theta = temp1 * temp2
        else:
            theta = theta_mask * attention.max()

        masks.append(F.interpolate(attention, size=(height, weight), mode='bilinear') < theta)
    masks = torch.cat(masks, dim=0)
    mask_images = input_images * masks.float()

    return mask_images
//  [python]
def attribution_random_patch_augmentation(
    x,
    cam,
    image_size=84,
    output_size=4,
    quantile=0.90,
    patch_proba=0.7,
    dataset="places365_standard",
):

    if dataset == "places365_standard":
        if places_dataloader is None:
            _load_places(batch_size=x.size(0), image_size=x.size(-1))
        negative = _get_places_batch(batch_size=x.size(0)).repeat(
            1, x.size(1) // 3, 1, 1
        )
    else:
        raise NotImplementedError(
            f'overlay has not been implemented for dataset "{dataset}"'
        )
    cam = cam.to(x.device)
    cam = F.adaptive_avg_pool2d(cam, output_size=output_size)
    q = torch.quantile(cam.flatten(1), quantile, 1)
    mask = (cam >= q[:, None, None]).long()
    exploration_mask = torch.rand(*mask.shape).to(x.device)
    exploration_mask[~mask] = 0
    expl_max = torch.amax(exploration_mask.view(mask.size(0), -1), dim=1)
    exploration_mask = (
        exploration_mask.view(-1, mask.size(1), mask.size(2)) == expl_max[:, None, None]
    ).long()
    bern = torch.bernoulli(torch.ones_like(mask) * patch_proba).long().to(x.device)
    selected_patch = (mask * bern) + exploration_mask
    selected_patch[selected_patch > 1] = 1
    selected_patch = F.upsample_nearest(selected_patch.float().unsqueeze(1), image_size)
    # augmented_x = (((0.5) * (x / 255.0) + (0.5) * negative) * 255.0) * selected_patch
    augmented_x = x * selected_patch
    complementary_mask = ~(selected_patch.bool())
    negative = negative * (complementary_mask.float())
    return augmented_x + (negative * 255)
//  [python]
def attribution_random_patch_augmentation(
    x,
    cam,
    image_size=84,
    output_size=4,
    quantile=0.90,
    patch_proba=0.7,
    dataset="places365_standard",
):

    if dataset == "places365_standard":
        if places_dataloader is None:
            _load_places(batch_size=x.size(0), image_size=x.size(-1))
        negative = _get_places_batch(batch_size=x.size(0)).repeat(
            1, x.size(1) // 3, 1, 1
        )
    else:
        raise NotImplementedError(
            f'overlay has not been implemented for dataset "{dataset}"'
        )
    cam = cam.to(x.device)
    cam = F.adaptive_avg_pool2d(cam, output_size=output_size)
    q = torch.quantile(cam.flatten(1), quantile, 1)
    mask = (cam >= q[:, None, None]).long()
    exploration_mask = torch.rand(*mask.shape).to(x.device)
    exploration_mask[~mask] = 0
    expl_max = torch.amax(exploration_mask.view(mask.size(0), -1), dim=1)
    exploration_mask = (
        exploration_mask.view(-1, mask.size(1), mask.size(2)) == expl_max[:, None, None]
    ).long()
    bern = torch.bernoulli(torch.ones_like(mask) * patch_proba).long().to(x.device)
    selected_patch = (mask * bern) + exploration_mask
    selected_patch[selected_patch > 1] = 1
    selected_patch = F.interpolate(selected_patch.float().unsqueeze(1), size=image_size, mode='nearest')
    # augmented_x = (((0.5) * (x / 255.0) + (0.5) * negative) * 255.0) * selected_patch
    augmented_x = x * selected_patch
    complementary_mask = ~(selected_patch.bool())
    negative = negative * (complementary_mask.float())
    return augmented_x + (negative * 255)
//  [python]
def forward(self, x):
        #print('x', x.size())
        x = self.conv1(x)
        #print('conv1', x.size())
        x = self.layer1(x)
        #print('layer1', x.size())
        x = self.layer2(x)
        #print('layer2', x.size())
        x = self.layer3(x)
        #print('layer3', x.size())
        x = self.layer4(x)
        #print('layer4', x.size())
        x = self.final(torch.cat([
            x,
            self.layer5a(x),
            self.layer5b(x),
            self.layer5c(x),
            self.layer5d(x),
        ], 1))
        #print('final', x.size())

        return F.upsample_bilinear(final, x.size()[2:])
//  [python]
def forward(self, x):
        #print('x', x.size())
        x = self.conv1(x)
        #print('conv1', x.size())
        x = self.layer1(x)
        #print('layer1', x.size())
        x = self.layer2(x)
        #print('layer2', x.size())
        x = self.layer3(x)
        #print('layer3', x.size())
        x = self.layer4(x)
        #print('layer4', x.size())
        x = self.final(torch.cat([
            x,
            self.layer5a(x),
            self.layer5b(x),
            self.layer5c(x),
            self.layer5d(x),
        ], 1))
        #print('final', x.size())

        return F.interpolate(x, x.size()[2:], mode='bilinear')
//  [python]
def get_detail_images(input_images, attentions, theta_detail=0.5, padding=0.1):
    batch_size, _, height, width = input_images.size()   # B, C, H, W
    detail_images = []
    for i in range(batch_size):
        attention = attentions[i:i + 1]
        if isinstance(theta_detail, tuple):
            theta = random.uniform(*theta_detail) * attention.max()
        else:
            theta = theta_detail * attention.max()

        mask = F.upsample_bilinear(attention, size=(height, width)) >= theta
        nonzero_indices = torch.nonzero(mask[0, 0, ...])
        height_min = max(int((nonzero_indices[:, 0].min().item() - padding * height)), 0)
        height_max = min(int(nonzero_indices[:, 0].max().item() + padding * height), height)
        width_min = max(int(nonzero_indices[:, 1].min().item() - padding * width), 0)
        width_max = min(int(nonzero_indices[:, 1].max().item() + padding * width), width)
        detail_images.append(
            F.upsample_bilinear(input_images[i:i + 1, :, height_min:height_max, width_min:width_max],
                                size=(height, width)))

    detail_images = torch.cat(detail_images, dim=0)
    return detail_images
//  [python]
def get_detail_images(input_images, attentions, theta_detail=0.5, padding=0.1):
    batch_size, _, height, width = input_images.size()   # B, C, H, W
    detail_images = []
    for i in range(batch_size):
        attention = attentions[i:i + 1]
        if isinstance(theta_detail, tuple):
            theta = random.uniform(*theta_detail) * attention.max()
        else:
            theta = theta_detail * attention.max()

        mask = F.interpolate(attention, size=(height, width), mode='bilinear') >= theta
        nonzero_indices = torch.nonzero(mask[0, 0, ...])
        height_min = max(int((nonzero_indices[:, 0].min().item() - padding * height)), 0)
        height_max = min(int(nonzero_indices[:, 0].max().item() + padding * height), height)
        width_min = max(int(nonzero_indices[:, 1].min().item() - padding * width), 0)
        width_max = min(int(nonzero_indices[:, 1].max().item() + padding * width), width)
        detail_images.append(
            F.interpolate(input_images[i:i + 1, :, height_min:height_max, width_min:width_max],
                                size=(height, width), mode='bilinear'))

    detail_images = torch.cat(detail_images, dim=0)
    return detail_images
//  [python]
def forward(self, f, corr_feature, pos=None):
        p0 = torch.nn.functional.pad(f[0], [16,16,16,16])[:, :, 4*pos[0]:4*pos[0]+60, 4*pos[1]:4*pos[1]+60]
        p1 = torch.nn.functional.pad(f[1], [8,8,8,8])[:, :, 2*pos[0]:2*pos[0]+30, 2*pos[1]:2*pos[1]+30]
        p2 = torch.nn.functional.pad(f[2], [4,4,4,4])[:, :, pos[0]:pos[0]+15, pos[1]:pos[1]+15]

        p3 = corr_feature[:, :, pos[0], pos[1]].view(-1, 256, 1, 1)

        out = self.deconv(p3)
        out = self.post0(F.upsample(self.h2(out) + self.v2(p2), size=(30, 30)))
        out = self.post1(F.upsample(self.h1(out) + self.v1(p1), size=(60, 60)))
        out = self.post2(F.upsample(self.h0(out) + self.v0(p0), size=(120, 120)))
        out = out.view(-1, 120*120)
        return out
//  [python]
def forward(self, f, corr_feature, pos=None):
        p0 = torch.nn.functional.pad(f[0], [16,16,16,16])[:, :, 4*pos[0]:4*pos[0]+60, 4*pos[1]:4*pos[1]+60]
        p1 = torch.nn.functional.pad(f[1], [8,8,8,8])[:, :, 2*pos[0]:2*pos[0]+30, 2*pos[1]:2*pos[1]+30]
        p2 = torch.nn.functional.pad(f[2], [4,4,4,4])[:, :, pos[0]:pos[0]+15, pos[1]:pos[1]+15]

        p3 = corr_feature[:, :, pos[0], pos[1]].view(-1, 256, 1, 1)

        out = self.deconv(p3)
        out = self.post0(F.interpolate(self.h2(out) + self.v2(p2), size=(30, 30)))
        out = self.post1(F.interpolate(self.h1(out) + self.v1(p1), size=(60, 60)))
        out = self.post2(F.interpolate(self.h0(out) + self.v0(p0), size=(120, 120)))
        out = out.view(-1, 120*120)
        return out
//  [python]
def forward(self, x):
        input = x
        x = self.head(x)
        x = self.body(x)
        x = self.tail(x)
        return x + torch.nn.functional.upsample(input, scale_factor=self.scale[0], mode='bicubic')
//  [python]
def forward(self, x):
        input = x
        x = self.head(x)
        x = self.body(x)
        x = self.tail(x)
        return x + torch.nn.functional.interpolate(input, scale_factor=self.scale[0], mode='bicubic')
//  [python]
def forward(self, x):
        x  = self.conv00(self.conv01(x))
        rc = self.reduce_channels(x)
        rs = self.reduce_spatial_size(rc)
        pool = self.pool(rs)
        conv = self.conv2(pool)
        conv = conv + self.conv1(pool)
        up =  torch.nn.functional.upsample(conv, size=(rc.shape[2],rc.shape[3]), mode='nearest')
        up =  up + rc
        out = (self.sigmoid(self.increase_channels(up)) * x) *  self.sigmoid(self.bottom11_dw(self.bottom11(x)))
        return out
//  [python]
def forward(self, x):
        x  = self.conv00(self.conv01(x))
        rc = self.reduce_channels(x)
        rs = self.reduce_spatial_size(rc)
        pool = self.pool(rs)
        conv = self.conv2(pool)
        conv = conv + self.conv1(pool)
        up =  torch.nn.functional.interpolate(conv, size=(rc.shape[2],rc.shape[3]), mode='nearest')
        up =  up + rc
        out = (self.sigmoid(self.increase_channels(up)) * x) *  self.sigmoid(self.bottom11_dw(self.bottom11(x)))
        return out
//  [python]
def forward(self, x):
        # print 'x', x.size()
        # Encoder
        x = self.convbnrelu1(x)
        # print 'x', x.size()
        x = self.maxpool(x)
        # print 'x', x.size()

        e1 = self.encoder1(x)
        # print 'e1', e1.size()
        e2 = self.encoder2(e1)
        # print 'e2', e2.size()
        e3 = self.encoder3(e2)
        # print 'e3', e3.size()
        e4 = self.encoder4(e3)
        # print 'e4', e4.size()

        # Decoder with Skip Connections
        d4 = F.upsample_bilinear(self.decoder4(e4), e3.size()[2:])
        # print 'd4', d4.size()
        d4 = d4 + e3
        d3 = self.decoder3(d4)
        # print 'd3', d3.size()
        d3 = d3 + e2
        d2 = self.decoder2(d3)
        # print 'd2', d2.size()
        d2 = d2 + e1
        d1 = self.decoder1(d2)
        # print 'd1', d1.size()

        # Final Classification
        f1 = self.finaldeconvbnrelu1(d1)
        # print 'f1', f1.size()
        f2 = self.finalconvbnrelu2(f1)
        # print 'f2', f2.size()
        f3 = self.finalconv3(f2)
        # print 'f3', f3.size()
        return self.activation(f3)
//  [python]
def forward(self, x):
        # print 'x', x.size()
        # Encoder
        x = self.convbnrelu1(x)
        # print 'x', x.size()
        x = self.maxpool(x)
        # print 'x', x.size()

        e1 = self.encoder1(x)
        # print 'e1', e1.size()
        e2 = self.encoder2(e1)
        # print 'e2', e2.size()
        e3 = self.encoder3(e2)
        # print 'e3', e3.size()
        e4 = self.encoder4(e3)
        # print 'e4', e4.size()

        # Decoder with Skip Connections
        d4 = F.interpolate(self.decoder4(e4), size=e3.size()[2:], mode='bilinear')
        # print 'd4', d4.size()
        d4 = d4 + e3
        d3 = self.decoder3(d4)
        # print 'd3', d3.size()
        d3 = d3 + e2
        d2 = self.decoder2(d3)
        # print 'd2', d2.size()
        d2 = d2 + e1
        d1 = self.decoder1(d2)
        # print 'd1', d1.size()

        # Final Classification
        f1 = self.finaldeconvbnrelu1(d1)
        # print 'f1', f1.size()
        f2 = self.finalconvbnrelu2(f1)
        # print 'f2', f2.size()
        f3 = self.finalconv3(f2)
        # print 'f3', f3.size()
        return self.activation(f3)
//  [python]
def forward(self, pkgs):
        en_feat1 = self.input_encoder_layer(pkgs)
        en_feat2 = self.res_en_layer2(en_feat1)
        en_feat3 = self.res_en_layer3(en_feat2)

        bridge_feat = self.res_bridge_layer(en_feat3)

        de_feat3 = self.res_de_layer3(torch.cat([F.upsample_bilinear(bridge_feat, scale_factor=2), en_feat3], dim=1))
        de_feat2 = self.res_de_layer2(torch.cat([F.upsample_bilinear(de_feat3, scale_factor=2), en_feat2], dim=1))
        de_feat1 = self.res_de_layer1(torch.cat([F.upsample_bilinear(de_feat2, scale_factor=2), en_feat1], dim=1))

        pred = self.output_decoder_layer(de_feat1)
        return pred
//  [python]
def forward(self, pkgs):
        en_feat1 = self.input_encoder_layer(pkgs)
        en_feat2 = self.res_en_layer2(en_feat1)
        en_feat3 = self.res_en_layer3(en_feat2)

        bridge_feat = self.res_bridge_layer(en_feat3)

        de_feat3 = self.res_de_layer3(torch.cat([F.interpolate(bridge_feat, scale_factor=2, mode='bilinear'), en_feat3], dim=1))
        de_feat2 = self.res_de_layer2(torch.cat([F.interpolate(de_feat3, scale_factor=2, mode='bilinear'), en_feat2], dim=1))
        de_feat1 = self.res_de_layer1(torch.cat([F.interpolate(de_feat2, scale_factor=2, mode='bilinear'), en_feat1], dim=1))

        pred = self.output_decoder_layer(de_feat1)
        return pred
//  [python]
def forward(self, x):
        dec1 = self.dec1(x)
        dec2 = self.dec2(dec1)
        dec3 = self.dec3(dec2)
        dec4 = self.dec4(dec3)
        center = self.center(dec4)
        enc4 = self.enc4(torch.cat([center, F.upsample_bilinear(dec4, center.size()[2:])], 1))
        enc3 = self.enc3(torch.cat([enc4, F.upsample_bilinear(dec3, enc4.size()[2:])], 1))
        enc2 = self.enc2(torch.cat([enc3, F.upsample_bilinear(dec2, enc3.size()[2:])], 1))
        enc1 = self.enc1(torch.cat([enc2, F.upsample_bilinear(dec1, enc2.size()[2:])], 1))

        return F.upsample_bilinear(self.final(enc1), x.size()[2:])
//  [python]
def forward(self, x):
        dec1 = self.dec1(x)
        dec2 = self.dec2(dec1)
        dec3 = self.dec3(dec2)
        dec4 = self.dec4(dec3)
        center = self.center(dec4)
        enc4 = self.enc4(torch.cat([center, F.interpolate(dec4, center.size()[2:], mode='bilinear')], 1))
        enc3 = self.enc3(torch.cat([enc4, F.interpolate(dec3, enc4.size()[2:], mode='bilinear')], 1))
        enc2 = self.enc2(torch.cat([enc3, F.interpolate(dec2, enc3.size()[2:], mode='bilinear')], 1))
        enc1 = self.enc1(torch.cat([enc2, F.interpolate(dec1, enc2.size()[2:], mode='bilinear')], 1))

        return F.interpolate(self.final(enc1), x.size()[2:], mode='bilinear')
//  [python]
def forward(self, x, y, mask):
        while x.size()[3] > 600:
            x, y = self.downsample(x), self.downsample(y)
        #print("x =", x.size())
        #print("y =", y.size())
        x_vgg, y_vgg = self.vgg(x), self.vgg(y)
        loss = 0
        for i in range(len(x_vgg)):
            _, ch, h, w = x_vgg[i].size()
            curr_mask = F.upsample_nearest(mask, (h, w))
            curr_mask = curr_mask.expand(-1, ch, -1, -1)
            loss += self.weights[i] * self.criterion(x_vgg[i]*curr_mask, y_vgg[i].detach()*curr_mask)
        return loss
//  [python]
def forward(self, x, y, mask):
        while x.size()[3] > 600:
            x, y = self.downsample(x), self.downsample(y)
        x_vgg, y_vgg = self.vgg(x), self.vgg(y)
        loss = 0
        for i in range(len(x_vgg)):
            _, ch, h, w = x_vgg[i].size()
            curr_mask = F.interpolate(mask, (h, w), mode='nearest')
            curr_mask = curr_mask.expand(-1, ch, -1, -1)
            loss += self.weights[i] * self.criterion(x_vgg[i]*curr_mask, y_vgg[i].detach()*curr_mask)
        return loss
//  [python]
def forward(self, *input):
        [(conv2, id1, s1), (conv3, id2, s2), (conv4, id3, s3), (conv5, id4, s4)], feature = input
        
        feature = self.reg_layer(F.upsample_bilinear(feature, scale_factor=2))
        conv5 = F.max_unpool2d(conv5, id4, 2, 2, 0, s4)
        input = torch.cat([feature, conv5, conv4], 1)
        input = self.conv1(input)
        input = self.conv2(input)
        
        input = F.max_unpool2d(input, id3, 2, 2, 0, s3)
        input = torch.cat([input, conv3], 1)
        input = self.conv3(input)
        input = self.conv4(input)
        
        input = F.max_unpool2d(input, id2, 2, 2, 0, s2)
        input = torch.cat([input, conv2], 1)
        input = self.conv5(input)
        input = self.conv6(input)
        input = self.conv7(input)

        return input
//  [python]
def forward(self, *input):
        [(conv2, id1, s1), (conv3, id2, s2), (conv4, id3, s3), (conv5, id4, s4)], feature = input
        
        feature = self.reg_layer(F.interpolate(feature, scale_factor=2, mode='bilinear'))
        conv5 = F.max_unpool2d(conv5, id4, 2, 2, 0, s4)
        input = torch.cat([feature, conv5, conv4], 1)
        input = self.conv1(input)
        input = self.conv2(input)
        
        input = F.max_unpool2d(input, id3, 2, 2, 0, s3)
        input = torch.cat([input, conv3], 1)
        input = self.conv3(input)
        input = self.conv4(input)
        
        input = F.max_unpool2d(input, id2, 2, 2, 0, s2)
        input = torch.cat([input, conv2], 1)
        input = self.conv5(input)
        input = self.conv6(input)
        input = self.conv7(input)

        return input
//  [python]
def refactor_batch(input_batch, size=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], device=DEVICE):
    assert len(input_batch.shape) == 4

    output_batch = renormalize(torch.nn.functional.upsample_bilinear(input_batch, (size, size)).to(device), mean, std)

    return output_batch
//  [python]
def refactor_batch(input_batch, size=224, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], device=DEVICE):
    assert len(input_batch.shape) == 4

    output_batch = renormalize(torch.nn.functional.interpolate(input_batch, (size, size), mode='bilinear').to(device), mean, std)

    return output_batch
//  [python]
def forward(self, x):
        h = x.size(2)
        w = x.size(3)
        conv1 = self.dconv_down1(x)
#         print(conv1.shape)
        x = self.maxpool(conv1)
#         print(x.shape)
        conv2 = self.dconv_down2(x)
        x = self.maxpool(conv2)
        
        conv3 = self.dconv_down3(x)
        x = self.maxpool(conv3)   
        
        conv4 = self.center(x)
        
        x = self.deconv3(conv4) # 512 256
        x = torch.cat([conv3,x],1) # 256 + 256
        
        x = self.decoder_3(x) # 512 256
        
        x = self.deconv2(x)
        x = torch.cat([conv2,x],1)
        x = self.decoder_2(x)
        
        x = self.deconv1(x)
        x = torch.cat([conv1,x],1)
        x = self.decoder_1(x)
        
        x = self.output_1(x)
        out = self.rrm(x)
        
        
        d1 = self.dsn1(conv1)
        d2 = F.upsample_bilinear(self.dsn2(conv2), size=(h,w))
        d3 = F.upsample_bilinear(self.dsn3(conv3), size=(h,w))
        d4 = F.upsample_bilinear(self.dsn4(conv4), size=(h,w))
        
        d1_out = F.sigmoid(d1)
        d2_out = F.sigmoid(d2)
        d3_out = F.sigmoid(d3)
        d4_out = F.sigmoid(d4)
        
        concat = torch.cat((d1_out, d2_out, d3_out, d4_out), 1)
        
        fuse_box = self.fuse(concat)
        GAP = F.adaptive_avg_pool2d(fuse_box,(1,1))
        GAP = GAP.view(-1, 64)        
        se_like = self.SE_mimic(GAP)        
        se_like = torch.unsqueeze(se_like, 2)
        se_like = torch.unsqueeze(se_like, 3)

        feat_se = concat * se_like.expand_as(concat)
        boundary = self.final_boundary(feat_se)
        boundary_out = torch.unsqueeze(boundary[:,1,:,:],1)        
        bd_sftmax = F.softmax(boundary, dim=1)
        boundary_scale = torch.unsqueeze(bd_sftmax[:,1,:,:],1)        
        
        feat_concat = torch.cat( [out, fuse_box], 1)
        feat_concat_conv = self.final_conv(feat_concat)
        mask = self.final_mask(feat_concat_conv)
        mask_sftmax = F.softmax(mask,dim=1)        
        mask_scale = torch.unsqueeze(mask_sftmax[:,1,:,:],1)

        if self.mode == 'Train':
            scalefactor = torch.clamp(mask_scale+boundary_scale,0,1)            
        elif self.mode == 'Infer':
            scalefactor = torch.clamp(mask_scale+5*boundary_scale,0,1)
        
        
        mask_out = torch.unsqueeze(mask[:,1,:,:],1)
        relu = self.relu(mask_out)        
        scalar = relu.cpu().detach().numpy()
        if np.sum(scalar) == 0:
            average = 0
        else : 
            average = scalar[np.nonzero(scalar)].mean()
        mask_out = mask_out-relu + (average*scalefactor)
        
        if self.mode == 'Train':
            mask_out = F.sigmoid(mask_out)
            boundary_out = F.sigmoid(boundary_out)

            return d1_out, d2_out, d3_out, d4_out, boundary_out, mask_out
        elif self.mode =='Infer':
            return mask_out
//  [python]
def forward(self, x):
        h = x.size(2)
        w = x.size(3)
        conv1 = self.dconv_down1(x)
#         print(conv1.shape)
        x = self.maxpool(conv1)
#         print(x.shape)
        conv2 = self.dconv_down2(x)
        x = self.maxpool(conv2)
        
        conv3 = self.dconv_down3(x)
        x = self.maxpool(conv3)   
        
        conv4 = self.center(x)
        
        x = self.deconv3(conv4) # 512 256
        x = torch.cat([conv3,x],1) # 256 + 256
        
        x = self.decoder_3(x) # 512 256
        
        x = self.deconv2(x)
        x = torch.cat([conv2,x],1)
        x = self.decoder_2(x)
        
        x = self.deconv1(x)
        x = torch.cat([conv1,x],1)
        x = self.decoder_1(x)
        
        x = self.output_1(x)
        out = self.rrm(x)
        
        
        d1 = self.dsn1(conv1)
        d2 = F.interpolate(self.dsn2(conv2), size=(h,w), mode='bilinear')
        d3 = F.interpolate(self.dsn3(conv3), size=(h,w), mode='bilinear')
        d4 = F.interpolate(self.dsn4(conv4), size=(h,w), mode='bilinear')
        
        d1_out = F.sigmoid(d1)
        d2_out = F.sigmoid(d2)
        d3_out = F.sigmoid(d3)
        d4_out = F.sigmoid(d4)
        
        concat = torch.cat((d1_out, d2_out, d3_out, d4_out), 1)
        
        fuse_box = self.fuse(concat)
        GAP = F.adaptive_avg_pool2d(fuse_box,(1,1))
        GAP = GAP.view(-1, 64)        
        se_like = self.SE_mimic(GAP)        
        se_like = torch.unsqueeze(se_like, 2)
        se_like = torch.unsqueeze(se_like, 3)

        feat_se = concat * se_like.expand_as(concat)
        boundary = self.final_boundary(feat_se)
        boundary_out = torch.unsqueeze(boundary[:,1,:,:],1)        
        bd_sftmax = F.softmax(boundary, dim=1)
        boundary_scale = torch.unsqueeze(bd_sftmax[:,1,:,:],1)        
        
        feat_concat = torch.cat( [out, fuse_box], 1)
        feat_concat_conv = self.final_conv(feat_concat)
        mask = self.final_mask(feat_concat_conv)
        mask_sftmax = F.softmax(mask,dim=1)        
        mask_scale = torch.unsqueeze(mask_sftmax[:,1,:,:],1)

        if self.mode == 'Train':
            scalefactor = torch.clamp(mask_scale+boundary_scale,0,1)            
        elif self.mode == 'Infer':
            scalefactor = torch.clamp(mask_scale+5*boundary_scale,0,1)
        
        
        mask_out = torch.unsqueeze(mask[:,1,:,:],1)
        relu = self.relu(mask_out)        
        scalar = relu.cpu().detach().numpy()
        if np.sum(scalar) == 0:
            average = 0
        else : 
            average = scalar[np.nonzero(scalar)].mean()
        mask_out = mask_out-relu + (average*scalefactor)
        
        if self.mode == 'Train':
            mask_out = F.sigmoid(mask_out)
            boundary_out = F.sigmoid(boundary_out)

            return d1_out, d2_out, d3_out, d4_out, boundary_out, mask_out
        elif self.mode =='Infer':
            return mask_out
//  [python]
def forward(self, x):
        batch_size = x.shape[0]
        t1 = self.fc(x).reshape((-1, 512, self.input_size // 16, self.input_size // 16))
        
        t2 = F.relu(self.conv1(F.upsample_bilinear(t1, scale_factor=2)))
        t3 = F.relu(self.conv2(F.upsample_bilinear(t2, scale_factor=2)))
        t4 = F.relu(self.conv3(F.upsample_bilinear(t3, scale_factor=2)))


        t5 = self.conv4(F.upsample_bilinear(t4, scale_factor=2))
        
        
        return F.sigmoid(t5)
//  [python]
def forward(self, x):
        batch_size = x.shape[0]
        t1 = self.fc(x).reshape((-1, 512, self.input_size // 16, self.input_size // 16))
        
        t2 = F.relu(self.conv1(F.interpolate(t1, scale_factor=2, mode='bilinear')))
        t3 = F.relu(self.conv2(F.interpolate(t2, scale_factor=2, mode='bilinear')))
        t4 = F.relu(self.conv3(F.interpolate(t3, scale_factor=2, mode='bilinear')))


        t5 = self.conv4(F.interpolate(t4, scale_factor=2, mode='bilinear'))
        
        
        return F.sigmoid(t5)
//  [python]
def cropBox(img, ul, br, resH, resW):
    ul = ul.int()
    br = br.int()
    lenH = max(br[1] - ul[1], (br[0] - ul[0]) * resH / resW)
    lenW = lenH * resW / resH
    if img.dim() == 2:
        img = img[np.newaxis, :]

    newDim = torch.IntTensor((img.size(0), int(lenH), int(lenW)))

    if(ul[1]>=img.shape[1] or ul[0]>=img.shape[2]):
        print('This error may because yolo is not trained correctly or the weights is not used correctly.')
        raise IndexError

    newImg = img[:, ul[1]:, ul[0]:].clone()
    # Crop and Padding
    size = torch.IntTensor((int(br[1] - ul[1]), int(br[0] - ul[0])))
    newImg = SpecialCrop(size, 1)(newImg)
    newImg = Pad(newDim)(newImg)
    # Resize to output
    v_Img = torch.autograd.Variable(newImg)
    v_Img = torch.unsqueeze(v_Img, 0)
    # newImg = F.upsample_bilinear(v_Img, size=(int(resH), int(resW))).data[0]
    if torch.__version__ == '0.4.0a0+32f3bf7' or torch.__version__ == '0.4.0':
        newImg = F.upsample(v_Img, size=(int(resH), int(resW)),
                            mode='bilinear', align_corners=True).data[0]
    else:
        newImg = F.interpolate(v_Img, size=(int(resH), int(resW)),
                               mode='bilinear', align_corners=True).data[0]
    return newImg
//  [python]
def cropBox(img, ul, br, resH, resW):
    ul = ul.int()
    br = br.int()
    lenH = max(br[1] - ul[1], (br[0] - ul[0]) * resH / resW)
    lenW = lenH * resW / resH
    if img.dim() == 2:
        img = img[np.newaxis, :]

    newDim = torch.IntTensor((img.size(0), int(lenH), int(lenW)))

    if(ul[1]>=img.shape[1] or ul[0]>=img.shape[2]):
        print('This error may because yolo is not trained correctly or the weights is not used correctly.')
        raise IndexError

    newImg = img[:, ul[1]:, ul[0]:].clone()
    # Crop and Padding
    size = torch.IntTensor((int(br[1] - ul[1]), int(br[0] - ul[0])))
    newImg = SpecialCrop(size, 1)(newImg)
    newImg = Pad(newDim)(newImg)
    # Resize to output
    v_Img = torch.autograd.Variable(newImg)
    v_Img = torch.unsqueeze(v_Img, 0)
    # newImg = F.upsample_bilinear(v_Img, size=(int(resH), int(resW))).data[0]
    if torch.__version__ == '0.4.0a0+32f3bf7' or torch.__version__ == '0.4.0':
        newImg = F.interpolate(v_Img, size=(int(resH), int(resW)),
                            mode='bilinear', align_corners=True).data[0]
    else:
        newImg = F.interpolate(v_Img, size=(int(resH), int(resW)),
                               mode='bilinear', align_corners=True).data[0]
    return newImg
//  [python]
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu(x)
        x = self.layer1(x)

        x_list = []
        for i in range(self.stage2_cfg['NUM_BRANCHES']):
            if self.transition1[i] is not None:
                x_list.append(self.transition1[i](x))
            else:
                x_list.append(x)

        y_list = self.stage2(x_list)

        x_list = []
        for i in range(self.stage3_cfg['NUM_BRANCHES']):
            if self.transition2[i] is not None:
                x_list.append(self.transition2[i](y_list[-1]))
            else:
                x_list.append(y_list[i])

        y_list = self.stage3(x_list)

        x_list = []
        for i in range(self.stage4_cfg['NUM_BRANCHES']):
            if self.transition3[i] is not None:
                x_list.append(self.transition3[i](y_list[-1]))
            else:
                x_list.append(y_list[i])

        y_list = self.stage4(x_list)

        y0_h, y0_w = y_list[0].size(2), y_list[0].size(3)
        y1 = F.upsample(y_list[1], size=(y0_h, y0_w), mode='bilinear')
        y2 = F.upsample(y_list[2], size=(y0_h, y0_w), mode='bilinear')
        y3 = F.upsample(y_list[3], size=(y0_h, y0_w), mode='bilinear')

        y = torch.cat([y_list[0], y1, y2, y3], 1)
        output = self.final_layer(y)

        return output
//  [python]
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu(x)
        x = self.layer1(x)

        x_list = []
        for i in range(self.stage2_cfg['NUM_BRANCHES']):
            if self.transition1[i] is not None:
                x_list.append(self.transition1[i](x))
            else:
                x_list.append(x)

        y_list = self.stage2(x_list)

        x_list = []
        for i in range(self.stage3_cfg['NUM_BRANCHES']):
            if self.transition2[i] is not None:
                x_list.append(self.transition2[i](y_list[-1]))
            else:
                x_list.append(y_list[i])

        y_list = self.stage3(x_list)

        x_list = []
        for i in range(self.stage4_cfg['NUM_BRANCHES']):
            if self.transition3[i] is not None:
                x_list.append(self.transition3[i](y_list[-1]))
            else:
                x_list.append(y_list[i])

        y_list = self.stage4(x_list)

        y0_h, y0_w = y_list[0].size(2), y_list[0].size(3)
        y1 = F.interpolate(y_list[1], size=(y0_h, y0_w), mode='bilinear')
        y2 = F.interpolate(y_list[2], size=(y0_h, y0_w), mode='bilinear')
        y3 = F.interpolate(y_list[3], size=(y0_h, y0_w), mode='bilinear')

        y = torch.cat([y_list[0], y1, y2, y3], 1)
        output = self.final_layer(y)

        return output
//  [python]
def forward_seg_features(self, x, convnext_expansion_scale, range_limit=3):
        outs = []
        for i in range(range_limit):
            x = self.downsample_layers[i](x)
            if convnext_expansion_scale <= 0:
                outs.append(self.stages[i](x))
            else:
                outs.append(F.upsample_bilinear(self.stages[i](x), scale_factor=convnext_expansion_scale))
        return outs