jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def forward(self, x):

        map0, map1, map2, map3, map4 = self.fpn(x)

        map4 = nn.functional.upsample(self.head4(map4), scale_factor=8, mode="nearest")
        map3 = nn.functional.upsample(self.head3(map3), scale_factor=4, mode="nearest")
        map2 = nn.functional.upsample(self.head2(map2), scale_factor=2, mode="nearest")
        map1 = nn.functional.upsample(self.head1(map1), scale_factor=1, mode="nearest")

        smoothed = self.smooth(torch.cat([map4, map3, map2, map1], dim=1))
        smoothed = nn.functional.upsample(smoothed, scale_factor=2, mode="nearest")
        smoothed = self.smooth2(smoothed + map0)
        smoothed = nn.functional.upsample(smoothed, scale_factor=2, mode="nearest")

        final = self.final(smoothed)
        res = torch.tanh(final) + x

        return torch.clamp(res, min = -1,max = 1)
//  [python]
def forward(self, x):

        map0, map1, map2, map3, map4 = self.fpn(x)

        map4 = nn.functional.interpolate(self.head4(map4), scale_factor=8, mode="nearest")
        map3 = nn.functional.interpolate(self.head3(map3), scale_factor=4, mode="nearest")
        map2 = nn.functional.interpolate(self.head2(map2), scale_factor=2, mode="nearest")
        map1 = nn.functional.interpolate(self.head1(map1), scale_factor=1, mode="nearest")

        smoothed = self.smooth(torch.cat([map4, map3, map2, map1], dim=1))
        smoothed = nn.functional.interpolate(smoothed, scale_factor=2, mode="nearest")
        smoothed = self.smooth2(smoothed + map0)
        smoothed = nn.functional.interpolate(smoothed, scale_factor=2, mode="nearest")

        final = self.final(smoothed)
        res = torch.tanh(final) + x

        return torch.clamp(res, min = -1,max = 1)
//  [python]
def forward(self, x):
        x_size = x.size()[2:]
        features = []
        for name, module in self.shufflenet._modules.items()[:-1]:
            # print('name:', name)
            if name in ['stage3', 'stage4']:
                # pass
                # print('x.shape:', x.shape)
                features.append(x)
            # print('module:', module)
            x = module(x)
        score = self.classifier(x)
        # features.append(x)

        if self.module_type=='16s' or self.module_type=='8s':
            score_16s_out = self.score_16s_conv(features[1])
        if self.module_type=='8s':
            score_8s_out = self.score_8s_conv(features[0])

        if self.module_type=='16s' or self.module_type=='8s':
            if self.upsample_method == 'upsample_bilinear':
                score = F.upsample_bilinear(score, score_16s_out.size()[2:])
            elif self.upsample_method == 'ConvTranspose2d':
                score = self.upsample_1(score)
            score += score_16s_out
        if self.module_type=='8s':
            if self.upsample_method == 'upsample_bilinear':
                score = F.upsample_bilinear(score, score_8s_out.size()[2:])
            elif self.upsample_method == 'ConvTranspose2d':
                score = self.upsample_2(score)
            score += score_8s_out

        out = F.upsample_bilinear(score, x_size)

        return out
//  [python]
def forward(self, x):
        x_size = x.size()[2:]
        features = []
        for name, module in self.shufflenet._modules.items()[:-1]:
            # print('name:', name)
            if name in ['stage3', 'stage4']:
                # pass
                # print('x.shape:', x.shape)
                features.append(x)
            # print('module:', module)
            x = module(x)
        score = self.classifier(x)
        # features.append(x)

        if self.module_type=='16s' or self.module_type=='8s':
            score_16s_out = self.score_16s_conv(features[1])
        if self.module_type=='8s':
            score_8s_out = self.score_8s_conv(features[0])

        if self.module_type=='16s' or self.module_type=='8s':
            if self.upsample_method == 'upsample_bilinear':
                score = F.interpolate(score, size=score_16s_out.size()[2:], mode='bilinear')
            elif self.upsample_method == 'ConvTranspose2d':
                score = self.upsample_1(score)
            score += score_16s_out
        if self.module_type=='8s':
            if self.upsample_method == 'upsample_bilinear':
                score = F.interpolate(score, size=score_8s_out.size()[2:], mode='bilinear')
            elif self.upsample_method == 'ConvTranspose2d':
                score = self.upsample_2(score)
            score += score_8s_out

        out = F.interpolate(score, size=x_size, mode='bilinear')

        return out
//  [python]
def forward(self, x):

        # Bottom-up pathway, from ResNet
        enc0 = self.enc0(x)

        enc1 = self.enc1(enc0) # 256

        enc2 = self.enc2(enc1) # 512

        enc3 = self.enc3(enc2) # 1024

        enc4 = self.enc4(enc3) # 2048

        # Lateral connections

        lateral4 = self.pad(self.lateral4(enc4))
        lateral3 = self.pad(self.lateral3(enc3))
        lateral2 = self.lateral2(enc2)
        lateral1 = self.pad(self.lateral1(enc1))
        lateral0 = self.lateral0(enc0)

        # Top-down pathway
        pad = (1, 2, 1, 2)  # pad last dim by 1 on each side
        pad1 = (0, 1, 0, 1)
        map4 = lateral4
        map3 = lateral3 + nn.functional.upsample(map4, scale_factor=2, mode="nearest")
        map2 = F.pad(lateral2, pad, "reflect") + nn.functional.upsample(map3, scale_factor=2, mode="nearest")
        map1 = lateral1 + nn.functional.upsample(map2, scale_factor=2, mode="nearest")
        return F.pad(lateral0, pad1, "reflect"), map1, map2, map3, map4
//  [python]
def forward(self, x):

        # Bottom-up pathway, from ResNet
        enc0 = self.enc0(x)

        enc1 = self.enc1(enc0) # 256

        enc2 = self.enc2(enc1) # 512

        enc3 = self.enc3(enc2) # 1024

        enc4 = self.enc4(enc3) # 2048

        # Lateral connections

        lateral4 = self.pad(self.lateral4(enc4))
        lateral3 = self.pad(self.lateral3(enc3))
        lateral2 = self.lateral2(enc2)
        lateral1 = self.pad(self.lateral1(enc1))
        lateral0 = self.lateral0(enc0)

        # Top-down pathway
        pad = (1, 2, 1, 2)  # pad last dim by 1 on each side
        pad1 = (0, 1, 0, 1)
        map4 = lateral4
        map3 = lateral3 + nn.functional.interpolate(map4, scale_factor=2, mode="nearest")
        map2 = F.pad(lateral2, pad, "reflect") + nn.functional.interpolate(map3, scale_factor=2, mode="nearest")
        map1 = lateral1 + nn.functional.interpolate(map2, scale_factor=2, mode="nearest")
        return F.pad(lateral0, pad1, "reflect"), map1, map2, map3, map4
//  [python]
def forward(self, features, attentions,flag):
        B, C, H, W = features.size() # 85 * 64 * 19 * 19
        _, M, AH, AW = attentions.size() # 85 * 16 * 19 * 19
        if AH != H or AW != W:
            attentions = F.upsample_bilinear(attentions, size=(H, W))

        if self.pool is None:
            feature_matrix = (torch.einsum('imjk,injk->imn', (attentions, features)) / float(H * W)).view(B, -1)
        else:
            feature_matrix = []
            for i in range(M):
                AiF = self.pool(features * attentions[:, i:i + 1, ...]).view(B, -1)
                feature_matrix.append(AiF)
            feature_matrix = torch.cat(feature_matrix, dim=1)

        feature_matrix = torch.sign(feature_matrix) * torch.sqrt(torch.abs(feature_matrix) + EPSILON)

        feature_matrix = F.normalize(feature_matrix, dim=-1)
        return feature_matrix
//  [python]
def forward(self, features, attentions,flag):
        B, C, H, W = features.size() # 85 * 64 * 19 * 19
        _, M, AH, AW = attentions.size() # 85 * 16 * 19 * 19
        if AH != H or AW != W:
            attentions = F.interpolate(attentions, size=(H, W), mode='bilinear')

        if self.pool is None:
            feature_matrix = (torch.einsum('imjk,injk->imn', (attentions, features)) / float(H * W)).view(B, -1)
        else:
            feature_matrix = []
            for i in range(M):
                AiF = self.pool(features * attentions[:, i:i + 1, ...]).view(B, -1)
                feature_matrix.append(AiF)
            feature_matrix = torch.cat(feature_matrix, dim=1)

        feature_matrix = torch.sign(feature_matrix) * torch.sqrt(torch.abs(feature_matrix) + EPSILON)

        feature_matrix = F.normalize(feature_matrix, dim=-1)
        return feature_matrix
//  [python]
def batch_augment(self,images, attention_map, mode='crop', theta=0.5):
        batches, _, imgH, imgW = images.size()
        padding_ratio = 0.2 if 'ratio02' in self.args.tag else 0.1

        if mode == 'crop':
            crop_images = []
            bboxes = []
            for batch_index in range(batches):
                atten_map = attention_map[batch_index:batch_index + 1]
                if isinstance(theta, tuple):
                    theta_c = np.random.uniform(*theta) * atten_map.max()
                else:
                    theta_c = theta * atten_map.max()

                crop_mask = F.upsample_bilinear(atten_map, size=(imgH, imgW)) >= theta_c
                nonzero_indices = torch.nonzero(crop_mask[0, 0, ...])
                if len(nonzero_indices) > 0:
                    height_min = max(int(nonzero_indices[:, 0].min().item() - padding_ratio * imgH), 0)
                    height_max = min(int(nonzero_indices[:, 0].max().item() + padding_ratio * imgH), imgH)
                    width_min = max(int(nonzero_indices[:, 1].min().item() - padding_ratio * imgW), 0)
                    width_max = min(int(nonzero_indices[:, 1].max().item() + padding_ratio * imgW), imgW)

                    crop_images.append(
                        F.upsample_bilinear(
                            images[batch_index:batch_index + 1, :, height_min:height_max, width_min:width_max],
                            size=(imgH, imgW)))
                    bboxes.append(torch.tensor([[height_min,height_max,width_min,width_max]], device=images.device))
                else:
                    crop_images.append(images[batch_index:batch_index + 1])
                    bboxes.append(torch.tensor([[0, imgH, 0, imgW]], device=images.device))
            crop_images = torch.cat(crop_images, dim=0)
            bboxes = torch.cat(bboxes,dim=0)
            return crop_images, bboxes

        elif mode == 'drop':
            drop_masks = []
            for batch_index in range(batches):
                atten_map = attention_map[batch_index:batch_index + 1]
                if isinstance(theta, tuple):
                    theta_d = np.random.uniform(*theta) * atten_map.max()
                else:
                    theta_d = theta * atten_map.max()

                drop_masks.append(F.upsample_bilinear(atten_map, size=(imgH, imgW)) < theta_d)
            drop_masks = torch.cat(drop_masks, dim=0)
            drop_images = images * drop_masks.float()
            return drop_images

        else:
            raise ValueError(
                'Expected mode in [\'crop\', \'drop\'], but received unsupported augmentation method %s' % mode)
//  [python]
def batch_augment(self,images, attention_map, mode='crop', theta=0.5):
        batches, _, imgH, imgW = images.size()
        padding_ratio = 0.2 if 'ratio02' in self.args.tag else 0.1

        if mode == 'crop':
            crop_images = []
            bboxes = []
            for batch_index in range(batches):
                atten_map = attention_map[batch_index:batch_index + 1]
                if isinstance(theta, tuple):
                    theta_c = np.random.uniform(*theta) * atten_map.max()
                else:
                    theta_c = theta * atten_map.max()

                crop_mask = F.interpolate(atten_map, size=(imgH, imgW), mode='bilinear') >= theta_c
                nonzero_indices = torch.nonzero(crop_mask[0, 0, ...])
                if len(nonzero_indices) > 0:
                    height_min = max(int(nonzero_indices[:, 0].min().item() - padding_ratio * imgH), 0)
                    height_max = min(int(nonzero_indices[:, 0].max().item() + padding_ratio * imgH), imgH)
                    width_min = max(int(nonzero_indices[:, 1].min().item() - padding_ratio * imgW), 0)
                    width_max = min(int(nonzero_indices[:, 1].max().item() + padding_ratio * imgW), imgW)

                    crop_images.append(
                        F.interpolate(
                            images[batch_index:batch_index + 1, :, height_min:height_max, width_min:width_max],
                            size=(imgH, imgW), mode='bilinear'))
                    bboxes.append(torch.tensor([[height_min,height_max,width_min,width_max]], device=images.device))
                else:
                    crop_images.append(images[batch_index:batch_index + 1])
                    bboxes.append(torch.tensor([[0, imgH, 0, imgW]], device=images.device))
            crop_images = torch.cat(crop_images, dim=0)
            bboxes = torch.cat(bboxes,dim=0)
            return crop_images, bboxes

        elif mode == 'drop':
            drop_masks = []
            for batch_index in range(batches):
                atten_map = attention_map[batch_index:batch_index + 1]
                if isinstance(theta, tuple):
                    theta_d = np.random.uniform(*theta) * atten_map.max()
                else:
                    theta_d = theta * atten_map.max()

                drop_masks.append(F.interpolate(atten_map, size=(imgH, imgW), mode='bilinear') < theta_d)
            drop_masks = torch.cat(drop_masks, dim=0)
            drop_images = images * drop_masks.float()
            return drop_images

        else:
            raise ValueError(
                'Expected mode in [\'crop\', \'drop\'], but received unsupported augmentation method %s' % mode)
//  [python]
def forward(self, input):
        # return self.model(input)  
        out1 = torch.zeros(1, 3, 60, 80)
        out2 = torch.zeros(1, 3, 60, 80)
        out3 = self.model(input)

        out1 = out3
        # print("e1 size is :", e1.size())
        # out1 = out1[:, 0:3, :, :] # this is right
        out1 = torch.mean(out1, 1) # mean across channel direction
        bat, hei, wei = out1.size()
        # print("bat hei wei", bat, hei, wei)
        out2 = torch.zeros(3, hei, wei)
        out2[0, :, :] = out1
        out2[1, :, :] = out1
        out2[2, :, :] = out1
        out2 = out2.unsqueeze(0)
        out2 = torch.nn.functional.upsample_bilinear(out2, size=[120, 160])
    
        return out3, out2, out1
//  [python]
def forward(self, input):
        # return self.model(input)  
        out1 = torch.zeros(1, 3, 60, 80)
        out2 = torch.zeros(1, 3, 60, 80)
        out3 = self.model(input)

        out1 = out3
        # print("e1 size is :", e1.size())
        # out1 = out1[:, 0:3, :, :] # this is right
        out1 = torch.mean(out1, 1) # mean across channel direction
        bat, hei, wei = out1.size()
        # print("bat hei wei", bat, hei, wei)
        out2 = torch.zeros(3, hei, wei)
        out2[0, :, :] = out1
        out2[1, :, :] = out1
        out2[2, :, :] = out1
        out2 = out2.unsqueeze(0)
        out2 = torch.nn.functional.interpolate(out2, size=[120, 160], mode='bilinear')
    
        return out3, out2, out1
//  [python]
def forward(self, feat, prob):
        h, w = feat.shape[2], feat.shape[3]

        prob = torch.nn.functional.upsample_bilinear(feat, (h, w))

        context = self.ocr_gather_head(feat, prob)
        feat = self.ocr_distri_head(feat, context)

        return feat
//  [python]
def forward(self, feat, prob):
        h, w = feat.shape[2], feat.shape[3]

        prob = torch.nn.functional.interpolate(feat, (h, w), mode='bilinear')

        context = self.ocr_gather_head(feat, prob)
        feat = self.ocr_distri_head(feat, context)

        return feat
//  [python]
def attention_im(images, attention_map, theta=0.5, padding_ratio=0.1):
    images = images.clone()
    attention_map = attention_map.clone().detach()
    batches, _, imgH, imgW = images.size()

    for batch_index in range(batches):
        image_tmp = images[batch_index]
        map_tpm = attention_map[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm = torch.nn.functional.upsample_bilinear(map_tpm, size=(imgH, imgW)).squeeze()
        map_tpm = (map_tpm - map_tpm.min()) / (map_tpm.max() - map_tpm.min() + 1e-6)
        map_tpm = map_tpm >= theta
        nonzero_indices = torch.nonzero(map_tpm, as_tuple=False)
        height_min = max(int(nonzero_indices[:, 0].min().item() - padding_ratio * imgH), 0)
        height_max = min(int(nonzero_indices[:, 0].max().item() + padding_ratio * imgH), imgH)
        width_min = max(int(nonzero_indices[:, 1].min().item() - padding_ratio * imgW), 0)
        width_max = min(int(nonzero_indices[:, 1].max().item() + padding_ratio * imgW), imgW)

        image_tmp = image_tmp[:, height_min:height_max, width_min:width_max].unsqueeze(0)
        image_tmp = torch.nn.functional.upsample_bilinear(image_tmp, size=(imgH, imgW)).squeeze()

        images[batch_index] = image_tmp

    return images
//  [python]
def attention_im(images, attention_map, theta=0.5, padding_ratio=0.1):
    images = images.clone()
    attention_map = attention_map.clone().detach()
    batches, _, imgH, imgW = images.size()

    for batch_index in range(batches):
        image_tmp = images[batch_index]
        map_tpm = attention_map[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm = torch.nn.functional.interpolate(map_tpm, size=(imgH, imgW), mode='bilinear').squeeze()
        map_tpm = (map_tpm - map_tpm.min()) / (map_tpm.max() - map_tpm.min() + 1e-6)
        map_tpm = map_tpm >= theta
        nonzero_indices = torch.nonzero(map_tpm, as_tuple=False)
        height_min = max(int(nonzero_indices[:, 0].min().item() - padding_ratio * imgH), 0)
        height_max = min(int(nonzero_indices[:, 0].max().item() + padding_ratio * imgH), imgH)
        width_min = max(int(nonzero_indices[:, 1].min().item() - padding_ratio * imgW), 0)
        width_max = min(int(nonzero_indices[:, 1].max().item() + padding_ratio * imgW), imgW)

        image_tmp = image_tmp[:, height_min:height_max, width_min:width_max].unsqueeze(0)
        image_tmp = torch.nn.functional.interpolate(image_tmp, size=(imgH, imgW), mode='bilinear').squeeze()

        images[batch_index] = image_tmp

    return images
//  [python]
def forward(self, x):

        output = x

        for layer in self.layers:
            output = layer(output)

        output = self.project_layer(output)

        # Bilinear interpolation x8
        output = F.upsample(output, scale_factor=8, mode='bilinear')

        # Bilinear interpolation x2 (inference only)
        if not self.training:
            output = F.upsample(output, scale_factor=2, mode='bilinear')

        return output
//  [python]
def forward(self, x):

        output = x

        for layer in self.layers:
            output = layer(output)

        output = self.project_layer(output)

        # Bilinear interpolation x8
        output = F.interpolate(output, scale_factor=8, mode='bilinear')

        # Bilinear interpolation x2 (inference only)
        if not self.training:
            output = F.interpolate(output, scale_factor=2, mode='bilinear')

        return output
//  [python]
def syn_lap_pyr(pyr):

    cur = pyr[-1]
    levs = len(pyr)
    for i in range(0,levs-1)[::-1]:
        up_x = pyr[i].size(2)
        up_y = pyr[i].size(3)
        cur = pyr[i] + F.upsample_bilinear(cur,(up_x,up_y))

    return cur
//  [python]
def syn_lap_pyr(pyr):

    cur = pyr[-1]
    levs = len(pyr)
    for i in range(0,levs-1)[::-1]:
        up_x = pyr[i].size(2)
        up_y = pyr[i].size(3)
        cur = pyr[i] + F.interpolate(cur,(up_x,up_y),mode='bilinear')

    return cur
//  [python]
def dec_lap_pyr(X,levs):
    pyr = []
    cur = X
    for i in range(levs):
        cur_x = cur.size(2)
        cur_y = cur.size(3)

        x_small = F.upsample_bilinear(cur, (max(cur_x//2,1), max(cur_y//2,1)))
        x_back  = F.upsample_bilinear(x_small, (cur_x,cur_y))
        lap = cur - x_back
        pyr.append(lap)
        cur = x_small

    pyr.append(cur)

    return pyr
//  [python]
def dec_lap_pyr(X,levs):
    pyr = []
    cur = X
    for i in range(levs):
        cur_x = cur.size(2)
        cur_y = cur.size(3)

        x_small = F.interpolate(cur, (max(cur_x//2,1), max(cur_y//2,1)), mode='bilinear')
        x_back  = F.interpolate(x_small, (cur_x,cur_y), mode='bilinear')
        lap = cur - x_back
        pyr.append(lap)
        cur = x_small

    pyr.append(cur)

    return pyr
//  [python]
def forward(self, q,k,v,size):
        batch, channel, depth, height, width = q.shape

        n_head = self.n_head
        residual=q
        norm_q = self.norm(q)
        norm_k = self.norm(k)
        norm_v = self.norm(v)
        

        qkv=torch.cat([norm_q,norm_k,norm_v],dim=1)
        q, k, v = rearrange(qkv, 'b (qkv heads c) d h w -> qkv b heads c (d h w)', heads=n_head, qkv=3)
        k = k.softmax(dim=-1)
        context = torch.einsum('bhdn,bhen->bhde', k, v)
        out = torch.einsum('bhde,bhdn->bhen', context, q)
        out = rearrange(out, 'b heads c (d h w) -> b (heads c) d h w', heads=n_head, h=height, w=width)
        out = self.out(out)
        out=self.defmgen(out+residual)
        out=F.upsample_nearest(out,size)
        out=self.nonlinear(out)
        return out
//  [python]
def forward(self, q,k,v,size):
        batch, channel, depth, height, width = q.shape

        n_head = self.n_head
        residual=q
        norm_q = self.norm(q)
        norm_k = self.norm(k)
        norm_v = self.norm(v)
        

        qkv=torch.cat([norm_q,norm_k,norm_v],dim=1)
        q, k, v = rearrange(qkv, 'b (qkv heads c) d h w -> qkv b heads c (d h w)', heads=n_head, qkv=3)
        k = k.softmax(dim=-1)
        context = torch.einsum('bhdn,bhen->bhde', k, v)
        out = torch.einsum('bhde,bhdn->bhen', context, q)
        out = rearrange(out, 'b heads c (d h w) -> b (heads c) d h w', heads=n_head, h=height, w=width)
        out = self.out(out)
        out=self.defmgen(out+residual)
        out=F.interpolate(out,size=size,mode='nearest')
        out=self.nonlinear(out)
        return out
//  [python]
def highlight_im(images, attention_map, attention_map2, attention_map3, theta=0.5, padding_ratio=0.1):
    images = images.clone()
    attention_map = attention_map.clone().detach()
    attention_map2 = attention_map2.clone().detach()
    attention_map3 = attention_map3.clone().detach()

    batches, _, imgH, imgW = images.size()

    for batch_index in range(batches):
        image_tmp = images[batch_index]
        map_tpm = attention_map[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm = torch.nn.functional.upsample_bilinear(map_tpm, size=(imgH, imgW)).squeeze()
        map_tpm = (map_tpm - map_tpm.min()) / (map_tpm.max() - map_tpm.min() + 1e-6)


        map_tpm2 = attention_map2[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm2 = torch.nn.functional.upsample_bilinear(map_tpm2, size=(imgH, imgW)).squeeze()
        map_tpm2 = (map_tpm2 - map_tpm2.min()) / (map_tpm2.max() - map_tpm2.min() + 1e-6)

        map_tpm3 = attention_map3[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm3 = torch.nn.functional.upsample_bilinear(map_tpm3, size=(imgH, imgW)).squeeze()
        map_tpm3 = (map_tpm3 - map_tpm3.min()) / (map_tpm3.max() - map_tpm3.min() + 1e-6)

        map_tpm = (map_tpm + map_tpm2 + map_tpm3)
        map_tpm = (map_tpm - map_tpm.min()) / (map_tpm.max() - map_tpm.min() + 1e-6)
        map_tpm = map_tpm >= theta

        nonzero_indices = torch.nonzero(map_tpm, as_tuple=False)
        height_min = max(int(nonzero_indices[:, 0].min().item() - padding_ratio * imgH), 0)
        height_max = min(int(nonzero_indices[:, 0].max().item() + padding_ratio * imgH), imgH)
        width_min = max(int(nonzero_indices[:, 1].min().item() - padding_ratio * imgW), 0)
        width_max = min(int(nonzero_indices[:, 1].max().item() + padding_ratio * imgW), imgW)

        image_tmp = image_tmp[:, height_min:height_max, width_min:width_max].unsqueeze(0)
        image_tmp = torch.nn.functional.upsample_bilinear(image_tmp, size=(imgH, imgW)).squeeze()

        images[batch_index] = image_tmp

    return images
//  [python]
def highlight_im(images, attention_map, attention_map2, attention_map3, theta=0.5, padding_ratio=0.1):
    images = images.clone()
    attention_map = attention_map.clone().detach()
    attention_map2 = attention_map2.clone().detach()
    attention_map3 = attention_map3.clone().detach()

    batches, _, imgH, imgW = images.size()

    for batch_index in range(batches):
        image_tmp = images[batch_index]
        map_tpm = attention_map[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm = torch.nn.functional.interpolate(map_tpm, size=(imgH, imgW), mode='bilinear').squeeze()
        map_tpm = (map_tpm - map_tpm.min()) / (map_tpm.max() - map_tpm.min() + 1e-6)


        map_tpm2 = attention_map2[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm2 = torch.nn.functional.interpolate(map_tpm2, size=(imgH, imgW), mode='bilinear').squeeze()
        map_tpm2 = (map_tpm2 - map_tpm2.min()) / (map_tpm2.max() - map_tpm2.min() + 1e-6)

        map_tpm3 = attention_map3[batch_index].unsqueeze(0).unsqueeze(0)
        map_tpm3 = torch.nn.functional.interpolate(map_tpm3, size=(imgH, imgW), mode='bilinear').squeeze()
        map_tpm3 = (map_tpm3 - map_tpm3.min()) / (map_tpm3.max() - map_tpm3.min() + 1e-6)

        map_tpm = (map_tpm + map_tpm2 + map_tpm3)
        map_tpm = (map_tpm - map_tpm.min()) / (map_tpm.max() - map_tpm.min() + 1e-6)
        map_tpm = map_tpm >= theta

        nonzero_indices = torch.nonzero(map_tpm, as_tuple=False)
        height_min = max(int(nonzero_indices[:, 0].min().item() - padding_ratio * imgH), 0)
        height_max = min(int(nonzero_indices[:, 0].max().item() + padding_ratio * imgH), imgH)
        width_min = max(int(nonzero_indices[:, 1].min().item() - padding_ratio * imgW), 0)
        width_max = min(int(nonzero_indices[:, 1].max().item() + padding_ratio * imgW), imgW)

        image_tmp = image_tmp[:, height_min:height_max, width_min:width_max].unsqueeze(0)
        image_tmp = torch.nn.functional.interpolate(image_tmp, size=(imgH, imgW), mode='bilinear').squeeze()

        images[batch_index] = image_tmp

    return images
//  [python]
def forward(self, input):
        input_size = input.size()
        # output of spatial path
        sx = self.saptial_path(input)
        # print('sx.shape:', sx.shape)

        # output of context path
        cx1, cx2, tail = self.context_path(input)
        # print('cx1.shape:', cx1.shape)
        # print('cx2.shape:', cx2.shape)
        # print('tail.shape:', tail.shape)
        cx1 = self.attention_refinement_module1(cx1)
        cx2 = self.attention_refinement_module2(cx2)
        cx2 = torch.mul(cx2, tail)
        # upsampling
        cx1 = F.upsample_bilinear(cx1, (input_size[2]//8, input_size[3]//8))
        cx2 = F.upsample_bilinear(cx2, (input_size[2]//8, input_size[3]//8))
        # print('cx1.shape:', cx1.shape)
        # print('cx2.shape:', cx2.shape)
        cx = torch.cat((cx1, cx2), dim=1)
        # print('cx.shape:', cx.shape)

        # output of feature fusion module
        result = self.feature_fusion_module(sx, cx)

        # upsampling
        result = F.upsample_bilinear(result, scale_factor=8)
        result = self.conv(result)
        return result
//  [python]
def forward(self, input):
        input_size = input.size()
        # output of spatial path
        sx = self.saptial_path(input)
        # print('sx.shape:', sx.shape)

        # output of context path
        cx1, cx2, tail = self.context_path(input)
        # print('cx1.shape:', cx1.shape)
        # print('cx2.shape:', cx2.shape)
        # print('tail.shape:', tail.shape)
        cx1 = self.attention_refinement_module1(cx1)
        cx2 = self.attention_refinement_module2(cx2)
        cx2 = torch.mul(cx2, tail)
        # upsampling
        cx1 = F.interpolate(cx1, size=(input_size[2]//8, input_size[3]//8), mode='bilinear')
        cx2 = F.interpolate(cx2, size=(input_size[2]//8, input_size[3]//8), mode='bilinear')
        # print('cx1.shape:', cx1.shape)
        # print('cx2.shape:', cx2.shape)
        cx = torch.cat((cx1, cx2), dim=1)
        # print('cx.shape:', cx.shape)

        # output of feature fusion module
        result = self.feature_fusion_module(sx, cx)

        # upsampling
        result = F.interpolate(result, scale_factor=8, mode='bilinear')
        result = self.conv(result)
        return result
//  [python]
def run_layer(self, xyz, feature, layer, skips, os, flag=True):
    new_xyz = xyz    
    if flag == True:
      xyz, new_xyz, y = layer[:-3]([xyz, new_xyz, feature])
      y = layer[-3:](y)
      xyz = F.upsample_bilinear(xyz, size=[xyz.size()[2], xyz.size()[3]//2])
    else:
      xyz,new_xyz,y = layer([xyz, new_xyz, feature])
    if y.shape[2] < feature.shape[2] or y.shape[3] < feature.shape[3]:
      skips[os] = feature.detach()
      os *= 2
    feature = self.dropout(y)
    return xyz, feature, skips, os
//  [python]
def run_layer(self, xyz, feature, layer, skips, os, flag=True):
    new_xyz = xyz    
    if flag == True:
      xyz, new_xyz, y = layer[:-3]([xyz, new_xyz, feature])
      y = layer[-3:](y)
      xyz = F.interpolate(xyz, size=[xyz.size()[2], xyz.size()[3]//2], mode='bilinear')
    else:
      xyz,new_xyz,y = layer([xyz, new_xyz, feature])
    if y.shape[2] < feature.shape[2] or y.shape[3] < feature.shape[3]:
      skips[os] = feature.detach()
      os *= 2
    feature = self.dropout(y)
    return xyz, feature, skips, os
//  [python]
def compute_prompts(device, latent_code, g_net, c_net, prompts, img_batch_size=37):

    imgs = []
    for i in range(0, len(latent_code), img_batch_size):
        
        latents = torch.nn.Parameter(latent_code[i:i+img_batch_size], requires_grad=False)
        dlatents = latents.repeat(1,18,1)
        
        img = g_net(dlatents)
        img = torch.nn.functional.upsample_bilinear(img, (224, 224))

        imgs.append(img)

    img = torch.cat(imgs)
    loss = compute_clip_losses(device, c_net, img, prompts)
    value = loss.cpu().detach().numpy()
    
    return value
//  [python]
def compute_prompts(device, latent_code, g_net, c_net, prompts, img_batch_size=37):

    imgs = []
    for i in range(0, len(latent_code), img_batch_size):
        
        latents = torch.nn.Parameter(latent_code[i:i+img_batch_size], requires_grad=False)
        dlatents = latents.repeat(1,18,1)
        
        img = g_net(dlatents)
        img = torch.nn.functional.interpolate(img, (224, 224), mode='bilinear')

        imgs.append(img)

    img = torch.cat(imgs)
    loss = compute_clip_losses(device, c_net, img, prompts)
    value = loss.cpu().detach().numpy()
    
    return value
//  [python]
def test_upsamplingNearest1d(self):
        m = nn.Upsample(size=4, mode='nearest')
        in_t = torch.ones(1, 1, 2)
        out_t = m(Variable(in_t))
        self.assertEqual(torch.ones(1, 1, 4), out_t.data)

        input = torch.randn(1, 1, 2, requires_grad=True)
        gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
//  [python]
def test_upsamplingNearest1d(self):
        m = nn.Upsample(size=4, mode='nearest')
        in_t = torch.ones(1, 1, 2)
        out_t = m(Variable(in_t))
        self.assertEqual(torch.ones(1, 1, 4), out_t.data)

        input = torch.randn(1, 1, 2, requires_grad=True)
        gradcheck(lambda x: F.interpolate(x, 4, mode='nearest'), [input])
//  [python]
def test_upsamplingLinear1d(self):
        for align_corners in [True, False]:
            kwargs = dict(mode='linear', align_corners=align_corners)

            # test float scale factor up & downsampling
            for scale_factor in [0.5, 1.5, 2]:
                m = nn.Upsample(scale_factor=scale_factor, **kwargs)
                in_t = torch.ones(1, 1, 2)
                out_size = int(math.floor(in_t.shape[-1] * scale_factor))
                out_t = m(in_t)
                self.assertEqual(torch.ones(1, 1, out_size), out_t.data)

                input = torch.randn(1, 1, 2, requires_grad=True)
                gradcheck(lambda x: F.upsample(x, out_size, **kwargs), (input,))
//  [python]
def test_upsamplingLinear1d(self):
        for align_corners in [True, False]:
            kwargs = dict(mode='linear', align_corners=align_corners)

            # test float scale factor up & downsampling
            for scale_factor in [0.5, 1.5, 2]:
                m = nn.Upsample(scale_factor=scale_factor, **kwargs)
                in_t = torch.ones(1, 1, 2)
                out_size = int(math.floor(in_t.shape[-1] * scale_factor))
                out_t = m(in_t)
                self.assertEqual(torch.ones(1, 1, out_size), out_t.data)

                input = torch.randn(1, 1, 2, requires_grad=True)
                gradcheck(lambda x: F.interpolate(x, out_size, **kwargs), (input,))
//  [python]
def forward(self, input):
        # input_ds = self.downsample(input)
        # print("input :", input.size())
        # print("input_ds :", input_ds.size())
        # print("original input", input.size())
        input = torch.nn.functional.upsample_bilinear(input, size=[512, 640]) 
        # print("original input", input.size())
        input_small = self.downsample(input)
    
        e1 = self.conv1_1(input)
        e1 = self.relu1_1(e1)
        e1 = self.conv1_2(e1)
        e1 = self.norm1(e1)
        e1 = self.relu1_2(e1)
        e1 = self.max1(e1)
        # print("e1 size is :", e1.size())
        
        e2 = self.conv2_1(e1)
        e2 = self.relu2_1(e2)
        e2 = self.conv2_2(e2)
        e2 = self.norm2(e2)
        e2 = self.relu2_2(e2)
        e2 = self.max2(e2)
        # print("e2 size is :", e2.size())
        
        e3 = self.conv3_1(e2)
        e3 = self.relu3_1(e3)
        e3 = self.conv3_2(e3)
        e3 = self.relu3_2(e3)
        e3 = self.conv3_3(e3)
        e3 = self.norm3(e3)
        e3 = self.relu3_3(e3)
        e3 = self.max3(e3)
        # print("e3 size is :", e3.size())
        
        e4 = self.conv4_1(e3)
        e4 = self.relu4_1(e4)
        e4 = self.conv4_2(e4)
        e4 = self.relu4_2(e4)
        e4 = self.conv4_3(e4)
        e4 = self.norm4(e4)
        e4 = self.relu4_3(e4)
        e4 = self.max4(e4)
        # print("e4 size is :", e4.size())
        
        e5 = self.conv5_1(e4)
        e5 = self.relu5_1(e5)
        e5 = self.conv5_2(e5)
        e5 = self.relu5_2(e5)
        e5 = self.conv5_3(e5)
        e5 = self.norm5(e5)
        e5 = self.relu5_3(e5)
        e5 = self.max5(e5)
        # print("e5 size is :", e5.size())

        e6 = self.conv6_1(e5)
        e6 = self.relu6_1(e6)
        e6 = self.conv6_2(e6)
        e6 = self.relu6_2(e6)
        e6 = self.conv6_3(e6)
        e6 = self.norm6(e6)
        e6 = self.relu6_3(e6)
        # print("e6 size is :", e6.size())

        e7 = self.conv7_1(e6)
        e7 = self.relu7_1(e7)
        e7 = self.conv7_2(e7)
        e7 = self.relu7_2(e7)
        e7 = self.conv7_3(e7)
        e7 = self.norm7(e7)
        e7 = self.relu7_3(e7)
        # print("e7 size is :", e7.size())

        e8 = self.conv8_1(e7)
        e8 = self.relu8_1(e8)
        e8 = self.conv8_2(e8)
        e8 = self.relu8_2(e8)
        e8 = self.conv8_3(e8)
        e8 = self.norm8(e8)
        e8 = self.relu8_3(e8)
        # print("e8 size is :", e8.size())

        e9 = self.conv9_1(e8)
        e9 = self.relu9_1(e9)
        e9 = self.conv9_2(e9)
        e9 = self.norm9(e9)
        e9 = self.relu9_2(e9)
        # print("e9 size is :", e9.size())

        e10 = self.conv10_1(e9)
        e10 = self.relu10_1(e10)
        e10 = self.conv10_2(e10)
        # e10 = self.norm10(e10)
        e10 = self.relu10_2(e10)
        # print("e10 size is :", e10.size())

        e11 = self.conv11_1(e10)
        e11 = self.tanh11_1(e11)
        
        e11_out = torch.nn.functional.upsample_bilinear(e11, size=[480, 640])
        return e11_out, e5, e9
//  [python]
def forward(self, input):
        input = torch.nn.functional.interpolate(input, size=[512, 640], mode='bilinear') 
        input_small = self.downsample(input)
    
        e1 = self.conv1_1(input)
        e1 = self.relu1_1(e1)
        e1 = self.conv1_2(e1)
        e1 = self.norm1(e1)
        e1 = self.relu1_2(e1)
        e1 = self.max1(e1)
        
        e2 = self.conv2_1(e1)
        e2 = self.relu2_1(e2)
        e2 = self.conv2_2(e2)
        e2 = self.norm2(e2)
        e2 = self.relu2_2(e2)
        e2 = self.max2(e2)
        
        e3 = self.conv3_1(e2)
        e3 = self.relu3_1(e3)
        e3 = self.conv3_2(e3)
        e3 = self.relu3_2(e3)
        e3 = self.conv3_3(e3)
        e3 = self.norm3(e3)
        e3 = self.relu3_3(e3)
        e3 = self.max3(e3)
        
        e4 = self.conv4_1(e3)
        e4 = self.relu4_1(e4)
        e4 = self.conv4_2(e4)
        e4 = self.relu4_2(e4)
        e4 = self.conv4_3(e4)
        e4 = self.norm4(e4)
        e4 = self.relu4_3(e4)
        e4 = self.max4(e4)
        
        e5 = self.conv5_1(e4)
        e5 = self.relu5_1(e5)
        e5 = self.conv5_2(e5)
        e5 = self.relu5_2(e5)
        e5 = self.conv5_3(e5)
        e5 = self.norm5(e5)
        e5 = self.relu5_3(e5)
        e5 = self.max5(e5)

        e6 = self.conv6_1(e5)
        e6 = self.relu6_1(e6)
        e6 = self.conv6_2(e6)
        e6 = self.relu6_2(e6)
        e6 = self.conv6_3(e6)
        e6 = self.norm6(e6)
        e6 = self.relu6_3(e6)

        e7 = self.conv7_1(e6)
        e7 = self.relu7_1(e7)
        e7 = self.conv7_2(e7)
        e7 = self.relu7_2(e7)
        e7 = self.conv7_3(e7)
        e7 = self.norm7(e7)
        e7 = self.relu7_3(e7)

        e8 = self.conv8_1(e7)
        e8 = self.relu8_1(e8)
        e8 = self.conv8_2(e8)
        e8 = self.relu8_2(e8)
        e8 = self.conv8_3(e8)
        e8 = self.norm8(e8)
        e8 = self.relu8_3(e8)

        e9 = self.conv9_1(e8)
        e9 = self.relu9_1(e9)
        e9 = self.conv9_2(e9)
        e9 = self.norm9(e9)
        e9 = self.relu9_2(e9)

        e10 = self.conv10_1(e9)
        e10 = self.relu10_1(e10)
        e10 = self.conv10_2(e10)
        e10 = self.relu10_2(e10)

        e11 = self.conv11_1(e10)
        e11 = self.tanh11_1(e11)
        
        e11_out = torch.nn.functional.interpolate(e11, size=[480, 640], mode='bilinear')
        return e11_out, e5, e9
//  [python]
def forward(self, x, test=False):
        """Applies network layers and ops on input image(s) x.

        Args:
            x: input image or batch of images. Shape: [batch,3*batch,300,300].

        Return:
            Depending on phase:
            test:
                Variable(tensor) of output class label predictions,
                confidence score, and corresponding location predictions for
                each object detected. Shape: [batch,topk,7]

            train:
                list of concat outputs from:
                    1: confidence layers, Shape: [batch*num_priors,num_classes]
                    2: localization layers, Shape: [batch,num_priors*4]
                    3: priorbox layers, Shape: [2,num_priors*4]
        """
        sources = list()
        loc = list()
        conf = list()

        # apply vgg up to conv4_3 relu
        for k in range(23):
            x = self.base[k](x)

        s1 = self.reduce(x)

        # apply vgg up to fc7
        for k in range(23, len(self.base)):
            x = self.base[k](x)
        s2 = self.up_reduce(x)
        s2 = F.upsample(s2, scale_factor=2, mode='bilinear')
        s = torch.cat((s1, s2), 1)

        ss = self.Norm(s)
        sources.append(ss)

        # apply extra layers and cache source layer outputs
        for k, v in enumerate(self.extras):
            x = v(x)
            if k < self.indicator or k % 2 == 0:
                sources.append(x)

        # apply multibox head to source layers
        for (x, l, c) in zip(sources, self.loc, self.conf):
            loc.append(l(x).permute(0, 2, 3, 1).contiguous())
            conf.append(c(x).permute(0, 2, 3, 1).contiguous())

        # print([o.size() for o in loc])

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

        if test:
            output = (
                loc.view(loc.size(0), -1, 4),  # loc preds
                self.softmax(conf.view(-1, self.num_classes)),  # conf preds
            )
        else:
            output = (
                loc.view(loc.size(0), -1, 4),
                conf.view(conf.size(0), -1, self.num_classes),
            )
        return output
//  [python]
def forward(self, x, test=False):
        """Applies network layers and ops on input image(s) x.

        Args:
            x: input image or batch of images. Shape: [batch,3*batch,300,300].

        Return:
            Depending on phase:
            test:
                Variable(tensor) of output class label predictions,
                confidence score, and corresponding location predictions for
                each object detected. Shape: [batch,topk,7]

            train:
                list of concat outputs from:
                    1: confidence layers, Shape: [batch*num_priors,num_classes]
                    2: localization layers, Shape: [batch,num_priors*4]
                    3: priorbox layers, Shape: [2,num_priors*4]
        """
        sources = list()
        loc = list()
        conf = list()

        # apply vgg up to conv4_3 relu
        for k in range(23):
            x = self.base[k](x)

        s1 = self.reduce(x)

        # apply vgg up to fc7
        for k in range(23, len(self.base)):
            x = self.base[k](x)
        s2 = self.up_reduce(x)
        s2 = F.interpolate(s2, scale_factor=2, mode='bilinear')
        s = torch.cat((s1, s2), 1)

        ss = self.Norm(s)
        sources.append(ss)

        # apply extra layers and cache source layer outputs
        for k, v in enumerate(self.extras):
            x = v(x)
            if k < self.indicator or k % 2 == 0:
                sources.append(x)

        # apply multibox head to source layers
        for (x, l, c) in zip(sources, self.loc, self.conf):
            loc.append(l(x).permute(0, 2, 3, 1).contiguous())
            conf.append(c(x).permute(0, 2, 3, 1).contiguous())

        # print([o.size() for o in loc])

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

        if test:
            output = (
                loc.view(loc.size(0), -1, 4),  # loc preds
                self.softmax(conf.view(-1, self.num_classes)),  # conf preds
            )
        else:
            output = (
                loc.view(loc.size(0), -1, 4),
                conf.view(conf.size(0), -1, self.num_classes),
            )
        return output
//  [python]
def test_upsamplingNearest2d(self):
        m = nn.Upsample(size=4, mode='nearest')
        in_t = torch.ones(1, 1, 2, 2)
        out_t = m(Variable(in_t))
        self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data)

        input = torch.randn(1, 1, 2, 2, requires_grad=True)
        self.assertEqual(
            F.upsample(input, 4, mode='nearest'),
            F.upsample(input, scale_factor=2, mode='nearest'))
        gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
        gradgradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
//  [python]
def test_upsamplingNearest2d(self):
        m = nn.Upsample(size=4, mode='nearest')
        in_t = torch.ones(1, 1, 2, 2)
        out_t = m(Variable(in_t))
        self.assertEqual(torch.ones(1, 1, 4, 4), out_t.data)

        input = torch.randn(1, 1, 2, 2, requires_grad=True)
        self.assertEqual(
            F.interpolate(input, size=4, mode='nearest'),
            F.interpolate(input, scale_factor=2, mode='nearest'))
        gradcheck(lambda x: F.interpolate(x, size=4, mode='nearest'), [input])
        gradgradcheck(lambda x: F.interpolate(x, size=4, mode='nearest'), [input])
//  [python]
def test_upsamplingNearest3d(self):
        m = nn.Upsample(size=4, mode='nearest')
        in_t = torch.ones(1, 1, 2, 2, 2)
        out_t = m(Variable(in_t))
        self.assertEqual(torch.ones(1, 1, 4, 4, 4), out_t.data)

        input = torch.randn(1, 1, 2, 2, 2, requires_grad=True)
        gradcheck(lambda x: F.upsample(x, 4, mode='nearest'), [input])
//  [python]
def test_upsamplingNearest3d(self):
        m = nn.Upsample(size=4, mode='nearest')
        in_t = torch.ones(1, 1, 2, 2, 2)
        out_t = m(Variable(in_t))
        self.assertEqual(torch.ones(1, 1, 4, 4, 4), out_t.data)

        input = torch.randn(1, 1, 2, 2, 2, requires_grad=True)
        gradcheck(lambda x: F.interpolate(x, 4, mode='nearest'), [input])
//  [python]
def test_upsamplingBilinear2d(self):
        for align_corners in [True, False]:
            kwargs = dict(mode='bilinear', align_corners=align_corners)

            # test float scale factor up & downsampling
            for scale_factor in [0.5, 1.5, 2]:
                m = nn.Upsample(scale_factor=scale_factor, **kwargs)
                in_t = torch.ones(1, 1, 2, 2)
                out_size = int(math.floor(in_t.shape[-1] * scale_factor))
                out_t = m(in_t)
                self.assertEqual(torch.ones(1, 1, out_size, out_size), out_t.data)

                input = torch.randn(1, 1, 2, 2, requires_grad=True)
                gradcheck(lambda x: F.upsample(x, out_size, **kwargs), [input])
//  [python]
def test_upsamplingBilinear2d(self):
        for align_corners in [True, False]:
            kwargs = dict(mode='bilinear', align_corners=align_corners)

            # test float scale factor up & downsampling
            for scale_factor in [0.5, 1.5, 2]:
                m = nn.Upsample(scale_factor=scale_factor, **kwargs)
                in_t = torch.ones(1, 1, 2, 2)
                out_size = int(math.floor(in_t.shape[-1] * scale_factor))
                out_t = m(in_t)
                self.assertEqual(torch.ones(1, 1, out_size, out_size), out_t.data)

                input = torch.randn(1, 1, 2, 2, requires_grad=True)
                gradcheck(lambda x: F.interpolate(x, size=out_size, **kwargs), [input])
//  [python]
def test_upsamplingTrilinear3d(self):
        for align_corners in [True, False]:
            kwargs = dict(mode='trilinear', align_corners=align_corners)

            # test float scale factor up & downsampling
            for scale_factor in [0.5, 1.5, 2]:
                m = nn.Upsample(scale_factor=scale_factor, **kwargs)
                in_t = torch.ones(1, 1, 2, 2, 2)
                out_size = int(math.floor(in_t.shape[-1] * scale_factor))
                out_t = m(in_t)
                self.assertEqual(torch.ones(1, 1, out_size, out_size, out_size), out_t.data)

                input = torch.randn(1, 1, 2, 2, 2, requires_grad=True)
                self.assertEqual(
                    F.upsample(input, (out_size, out_size, out_size), **kwargs),
                    F.upsample(input, scale_factor=scale_factor, **kwargs))
                gradcheck(lambda x: F.upsample(x, out_size, **kwargs), [input])
                gradgradcheck(lambda x: F.upsample(x, out_size, **kwargs), [input])
//  [python]
def test_upsamplingTrilinear3d(self):
        for align_corners in [True, False]:
            kwargs = dict(mode='trilinear', align_corners=align_corners)

            # test float scale factor up & downsampling
            for scale_factor in [0.5, 1.5, 2]:
                m = nn.Upsample(scale_factor=scale_factor, **kwargs)
                in_t = torch.ones(1, 1, 2, 2, 2)
                out_size = int(math.floor(in_t.shape[-1] * scale_factor))
                out_t = m(in_t)
                self.assertEqual(torch.ones(1, 1, out_size, out_size, out_size), out_t.data)

                input = torch.randn(1, 1, 2, 2, 2, requires_grad=True)
                self.assertEqual(
                    F.interpolate(input, (out_size, out_size, out_size), **kwargs),
                    F.interpolate(input, scale_factor=scale_factor, **kwargs))
                gradcheck(lambda x: F.interpolate(x, out_size, **kwargs), [input])
                gradgradcheck(lambda x: F.interpolate(x, out_size, **kwargs), [input])
//  [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.upsample_bilinear(y, None, 0.5)

        z = y_down + x

        return z