jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def forward(self, X, label, requires_loss):
        """Forward pass of the network.
        """
        N = X.size()[0]
        X1 = self.features1(X)
        H = X1.size()[2]
        W = X1.size()[3]
        assert X1.size()[1] == 512
        X2 = self.features2(X)
        H2 = X2.size()[2]
        W2 = X2.size()[3]
        assert X2.size()[1] == 128

        if (H != H2) | (W != W2):
            X2 = F.interpolate(X2, (H, W), mode='bilinear')

        X1 = X1.view(N, 512, H * W)
        X2 = X2.view(N, 128, H * W)
        X = torch.bmm(X1, torch.transpose(X2, 1, 2)) / (H * W)  # Bilinear
        assert X.size() == (N, 512, 128)
        X = X.view(N, 512 * 128)
        X = torch.sqrt(X + 1e-8)
        X = torch.nn.functional.normalize(X)
        X = self.fc(X)
        assert X.size() == (N, 1)
        if requires_loss:
            return X, label, self.loss_build(X, label)
        else:
            return X
//  [python]
def forward(self, x):
        x = self.C1(x)
        x = self.C2(x)
        c2_out = x
        x = self.C3(x)
        c3_out = x
        x = self.C4(x)
        c4_out = x
        x = self.C5(x)
        p5_out = self.P5_conv1(x)
        p4_out = self.P4_conv1(c4_out) + F.upsample(p5_out, scale_factor=2)
        p3_out = self.P3_conv1(c3_out) + F.upsample(p4_out, scale_factor=2)
        p2_out = self.P2_conv1(c2_out) + F.upsample(p3_out, scale_factor=2)

        p5_out = self.P5_conv2(p5_out)
        p4_out = self.P4_conv2(p4_out)
        p3_out = self.P3_conv2(p3_out)
        p2_out = self.P2_conv2(p2_out)

        # P6 is used for the 5th anchor scale in RPN. Generated by
        # subsampling from P5 with stride of 2.
        p6_out = self.P6(p5_out)

        return [p2_out, p3_out, p4_out, p5_out, p6_out]
//  [python]
def forward(self, x):
        x = self.C1(x)
        x = self.C2(x)
        c2_out = x
        x = self.C3(x)
        c3_out = x
        x = self.C4(x)
        c4_out = x
        x = self.C5(x)
        p5_out = self.P5_conv1(x)
        p4_out = self.P4_conv1(c4_out) + F.interpolate(p5_out, scale_factor=2)
        p3_out = self.P3_conv1(c3_out) + F.interpolate(p4_out, scale_factor=2)
        p2_out = self.P2_conv1(c2_out) + F.interpolate(p3_out, scale_factor=2)

        p5_out = self.P5_conv2(p5_out)
        p4_out = self.P4_conv2(p4_out)
        p3_out = self.P3_conv2(p3_out)
        p2_out = self.P2_conv2(p2_out)

        # P6 is used for the 5th anchor scale in RPN. Generated by
        # subsampling from P5 with stride of 2.
        p6_out = self.P6(p5_out)

        return [p2_out, p3_out, p4_out, p5_out, p6_out]
//  [python]
def forward(self, up, main):
        main = self.main_conv(main)
        _, _, H, W = main.size()
        res = F.upsample(up, scale_factor=2, mode='bilinear')
        if res.size(2) != main.size(2) or res.size(3) != main.size(3):
            res = res[:, :, 0:H, 0:W]
        res = res + main
        return res
//  [python]
def forward(self, up, main):
        main = self.main_conv(main)
        _, _, H, W = main.size()
        res = F.interpolate(up, scale_factor=2, mode='bilinear')
        if res.size(2) != main.size(2) or res.size(3) != main.size(3):
            res = res[:, :, 0:H, 0:W]
        res = res + main
        return res
//  [python]
def forward(self, x, return_weight=False):
        """
        Args:
            x (torch.cuda.FloatTensor): input image batch
            return_weight (bool): set to False by default,
                                  if set to True return the predicted offsets of AIN, denoted as x_{offsets}

        Returns:
            torch.Tensor: rectified image [batch_size x I_channel_num x I_height x I_width], the same as the input size
        """

        assert x.data.type() == self.inputDataType
        if self.spt:
            feat = self.spt_convnet(x)
            fc1 = self.stucture_fc1(feat)
            sp_weight_fusion = self.stucture_fc2(fc1)
            sp_weight_fusion = sp_weight_fusion.view(x.size(0), self.out_weight, 1)
            if self.offsets:  # SPIN w. AIN
                lambda_color = sp_weight_fusion[:, self.spt_length, 0]
                lambda_color = self.sigmoid(lambda_color).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
                sp_weight = sp_weight_fusion[:, :self.spt_length, :]
                offsets = self.pool(self.offset_fc2(self.offset_fc1(feat)))

                assert offsets.size(2) == 2  # 2
                assert offsets.size(3) == 6  # 16
                offsets = self.sigmoid(offsets)  # v12

                if return_weight:
                    return offsets
                offsets = nn.functional.upsample(offsets, size=(x.size(2), x.size(3)), mode='bilinear')

                if self.stn is not None:
                    batch_C_prime = sp_weight_fusion[:, (self.spt_length + 1):, :].view(x.size(0), self.F, 2)
                    build_P_prime = self.GridGenerator.build_P_prime(batch_C_prime)
                    build_P_prime_reshape = build_P_prime.reshape([build_P_prime.size(0),
                                                                   self.I_r_size[0],
                                                                   self.I_r_size[1],
                                                                   2])

            else:  # SPIN w.o. AIN
                sp_weight = sp_weight_fusion[:, :self.spt_length, :]
                lambda_color, offsets = None, None

                if self.stn is not None:
                    batch_C_prime = sp_weight_fusion[:, self.spt_length:, :].view(x.size(0), self.F, 2)
                    build_P_prime = self.GridGenerator.build_P_prime(batch_C_prime)
                    build_P_prime_reshape = build_P_prime.reshape([build_P_prime.size(0),
                                                                   self.I_r_size[0],
                                                                   self.I_r_size[1],
                                                                   2])

            x = self.sp_net(x, sp_weight, offsets, lambda_color)
            if self.stn:
                x = F.grid_sample(x, build_P_prime_reshape, padding_mode='border')
        return x
//  [python]
def forward(self, x, return_weight=False):
        """
        Args:
            x (torch.cuda.FloatTensor): input image batch
            return_weight (bool): set to False by default,
                                  if set to True return the predicted offsets of AIN, denoted as x_{offsets}

        Returns:
            torch.Tensor: rectified image [batch_size x I_channel_num x I_height x I_width], the same as the input size
        """

        assert x.data.type() == self.inputDataType
        if self.spt:
            feat = self.spt_convnet(x)
            fc1 = self.stucture_fc1(feat)
            sp_weight_fusion = self.stucture_fc2(fc1)
            sp_weight_fusion = sp_weight_fusion.view(x.size(0), self.out_weight, 1)
            if self.offsets:  # SPIN w. AIN
                lambda_color = sp_weight_fusion[:, self.spt_length, 0]
                lambda_color = self.sigmoid(lambda_color).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
                sp_weight = sp_weight_fusion[:, :self.spt_length, :]
                offsets = self.pool(self.offset_fc2(self.offset_fc1(feat)))

                assert offsets.size(2) == 2  # 2
                assert offsets.size(3) == 6  # 16
                offsets = self.sigmoid(offsets)  # v12

                if return_weight:
                    return offsets
                offsets = nn.functional.interpolate(offsets, size=(x.size(2), x.size(3)), mode='bilinear')

                if self.stn is not None:
                    batch_C_prime = sp_weight_fusion[:, (self.spt_length + 1):, :].view(x.size(0), self.F, 2)
                    build_P_prime = self.GridGenerator.build_P_prime(batch_C_prime)
                    build_P_prime_reshape = build_P_prime.reshape([build_P_prime.size(0),
                                                                   self.I_r_size[0],
                                                                   self.I_r_size[1],
                                                                   2])

            else:  # SPIN w.o. AIN
                sp_weight = sp_weight_fusion[:, :self.spt_length, :]
                lambda_color, offsets = None, None

                if self.stn is not None:
                    batch_C_prime = sp_weight_fusion[:, self.spt_length:, :].view(x.size(0), self.F, 2)
                    build_P_prime = self.GridGenerator.build_P_prime(batch_C_prime)
                    build_P_prime_reshape = build_P_prime.reshape([build_P_prime.size(0),
                                                                   self.I_r_size[0],
                                                                   self.I_r_size[1],
                                                                   2])

            x = self.sp_net(x, sp_weight, offsets, lambda_color)
            if self.stn:
                x = F.grid_sample(x, build_P_prime_reshape, padding_mode='border')
        return x
//  [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)

        y_list = [x]
        for i in range(self.num_stages):
            x_list = []
            transition = getattr(self, "transition{}".format(i + 1))
            for j in range(self.stages_spec["NUM_BRANCHES"][i]):
                if transition[j]:
                    x_list.append(transition[j](y_list[-1]))
                else:
                    x_list.append(y_list[j])
            y_list = getattr(self, "stage{}".format(i + 2))(x_list)

        x0_h, x0_w = y_list[0].size(2), y_list[0].size(3)
        x = torch.cat(
            [
                y_list[0],
                F.upsample(y_list[1], size=(x0_h, x0_w), mode="bilinear"),
                F.upsample(y_list[2], size=(x0_h, x0_w), mode="bilinear"),
                F.upsample(y_list[3], size=(x0_h, x0_w), mode="bilinear"),
            ],
            1,
        )

        heatmap = self.head_heatmap[1](self.head_heatmap[0](self.transition_heatmap(x)))

        final_offset = []
        offset_feature = self.transition_offset(x)

        for j in range(self.num_joints):
            final_offset.append(
                self.offset_final_layer[j](self.offset_feature_layers[j](offset_feature[:, j * self.offset_prekpt : (j + 1) * self.offset_prekpt]))
            )

        offset = torch.cat(final_offset, dim=1)
        return self.heatmap_activation(heatmap), offset
//  [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)

        y_list = [x]
        for i in range(self.num_stages):
            x_list = []
            transition = getattr(self, "transition{}".format(i + 1))
            for j in range(self.stages_spec["NUM_BRANCHES"][i]):
                if transition[j]:
                    x_list.append(transition[j](y_list[-1]))
                else:
                    x_list.append(y_list[j])
            y_list = getattr(self, "stage{}".format(i + 2))(x_list)

        x0_h, x0_w = y_list[0].size(2), y_list[0].size(3)
        x = torch.cat(
            [
                y_list[0],
                F.interpolate(y_list[1], size=(x0_h, x0_w), mode="bilinear"),
                F.interpolate(y_list[2], size=(x0_h, x0_w), mode="bilinear"),
                F.interpolate(y_list[3], size=(x0_h, x0_w), mode="bilinear"),
            ],
            1,
        )

        heatmap = self.head_heatmap[1](self.head_heatmap[0](self.transition_heatmap(x)))

        final_offset = []
        offset_feature = self.transition_offset(x)

        for j in range(self.num_joints):
            final_offset.append(
                self.offset_final_layer[j](self.offset_feature_layers[j](offset_feature[:, j * self.offset_prekpt : (j + 1) * self.offset_prekpt]))
            )

        offset = torch.cat(final_offset, dim=1)
        return self.heatmap_activation(heatmap), offset
//  [python]
def forward(self, x: Tensor, feat_1: Tensor, feat_2: Tensor):
        _, _, height, width = feat_1.shape
        if self.upsample > 1:
            x = F.upsample_nearest(torch.unsqueeze(x, dim=1), (height, width))
        x = torch.cat([feat_1, x, feat_2], dim=1)
        x = self.inconv(x)
        for layer in self.layers:
            x = layer(x)
        x = self.outconv(x)
        return torch.squeeze(x, dim=1)
//  [python]
def forward(self, x: Tensor, feat_1: Tensor, feat_2: Tensor):
        _, _, height, width = feat_1.shape
        if self.upsample > 1:
            x = F.interpolate(torch.unsqueeze(x, dim=1), (height, width), mode='nearest')
        x = torch.cat([feat_1, x, feat_2], dim=1)
        x = self.inconv(x)
        for layer in self.layers:
            x = layer(x)
        x = self.outconv(x)
        return torch.squeeze(x, dim=1)
//  [python]
def forward(self, x):

        # pass to initial conv
        x = self.conv1(x)

        # pass through residual units
        for i in range(3):
            x = self.up_residual_units[i](x)

        # divide stream
        y = x
        z = self.split_conv(x)

        prev_channels = 48
        # encoding
        for n_blocks, channels, scale in self.encoder_frru_specs:
            # maxpool bigger feature map
            y_pooled = F.max_pool2d(y, stride=2, kernel_size=2, padding=0)
            # pass through encoding FRRUs
            for block in range(n_blocks):
                key = "_".join(
                    map(str, ["encoding_frru", n_blocks, channels, scale, block])
                )
                y, z = getattr(self, key)(y_pooled, z)
            prev_channels = channels

        # decoding
        for n_blocks, channels, scale in self.decoder_frru_specs:
            # bilinear upsample smaller feature map
            upsample_size = torch.Size([_s * 2 for _s in y.size()[-2:]])
            y_upsampled = F.upsample(y, size=upsample_size, mode="bilinear", align_corners=True)
            # pass through decoding FRRUs
            for block in range(n_blocks):
                key = "_".join(
                    map(str, ["decoding_frru", n_blocks, channels, scale, block])
                )
                # print("Incoming FRRU Size: ", key, y_upsampled.shape, z.shape)
                y, z = getattr(self, key)(y_upsampled, z)
                # print("Outgoing FRRU Size: ", key, y.shape, z.shape)
            prev_channels = channels

        # merge streams
        x = torch.cat([F.upsample(y, scale_factor=2, mode="bilinear", align_corners=True), z], dim=1)
        x = self.merge_conv(x)

        # pass through residual units
        for i in range(3):
            x = self.down_residual_units[i](x)

        # final 1x1 conv to get classification
        x = self.classif_conv(x)

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

        # pass to initial conv
        x = self.conv1(x)

        # pass through residual units
        for i in range(3):
            x = self.up_residual_units[i](x)

        # divide stream
        y = x
        z = self.split_conv(x)

        prev_channels = 48
        # encoding
        for n_blocks, channels, scale in self.encoder_frru_specs:
            # maxpool bigger feature map
            y_pooled = F.max_pool2d(y, stride=2, kernel_size=2, padding=0)
            # pass through encoding FRRUs
            for block in range(n_blocks):
                key = "_".join(
                    map(str, ["encoding_frru", n_blocks, channels, scale, block])
                )
                y, z = getattr(self, key)(y_pooled, z)
            prev_channels = channels

        # decoding
        for n_blocks, channels, scale in self.decoder_frru_specs:
            # bilinear upsample smaller feature map
            upsample_size = torch.Size([_s * 2 for _s in y.size()[-2:]])
            y_upsampled = F.interpolate(y, size=upsample_size, mode="bilinear", align_corners=True)
            # pass through decoding FRRUs
            for block in range(n_blocks):
                key = "_".join(
                    map(str, ["decoding_frru", n_blocks, channels, scale, block])
                )
                # print("Incoming FRRU Size: ", key, y_upsampled.shape, z.shape)
                y, z = getattr(self, key)(y_upsampled, z)
                # print("Outgoing FRRU Size: ", key, y.shape, z.shape)
            prev_channels = channels

        # merge streams
        x = torch.cat([F.interpolate(y, scale_factor=2, mode="bilinear", align_corners=True), z], dim=1)
        x = self.merge_conv(x)

        # pass through residual units
        for i in range(3):
            x = self.down_residual_units[i](x)

        # final 1x1 conv to get classification
        x = self.classif_conv(x)

        return x
//  [python]
def __call__(self, img):
        im_h, im_w = img.shape[-2:]
        scale = min(self.size / im_h, self.size / im_w)
        resized_h = int(np.round(im_h * scale))
        resized_w = int(np.round(im_w * scale))
        out = F.upsample(
            Variable(img).unsqueeze(0), size=(resized_h, resized_w),
            mode='bilinear',align_corners=True).squeeze().data
        return out
//  [python]
def __call__(self, img):
        im_h, im_w = img.shape[-2:]
        scale = min(self.size / im_h, self.size / im_w)
        resized_h = int(np.round(im_h * scale))
        resized_w = int(np.round(im_w * scale))
        out = F.interpolate(
            Variable(img).unsqueeze(0), size=(resized_h, resized_w),
            mode='bilinear',align_corners=True).squeeze().data
        return out
//  [python]
def __call__(self, img, size):
        if not isinstance(size, (int, Iterable)):
            raise TypeError('Got inappropriate size arg: {}'.format(size))
        im_h, im_w = img.data.shape[:2]
        input_h, input_w = size
        scale = max(input_h / im_h, input_w / im_w)
        # scale = torch.Tensor([[input_h / im_h, input_w / im_w]]).max()
        resized_h = int(np.round(im_h * scale))
        # resized_h = torch.round(im_h * scale)
        resized_w = int(np.round(im_w * scale))
        # resized_w = torch.round(im_w * scale)
        crop_h = int(np.floor(resized_h - input_h) / 2)
        # crop_h = torch.floor(resized_h - input_h) // 2
        crop_w = int(np.floor(resized_w - input_w) / 2)
        # crop_w = torch.floor(resized_w - input_w) // 2
        # resized_img = cv2.resize(img, (resized_w, resized_h))
        resized_img = F.upsample(
            img.unsqueeze(0).unsqueeze(0), size=(resized_h, resized_w),
            mode='bilinear',align_corners=True)

        resized_img = resized_img.squeeze().unsqueeze(0)

        return resized_img[0, crop_h: crop_h + input_h,
                           crop_w: crop_w + input_w]
//  [python]
def __call__(self, img, size):
        if not isinstance(size, (int, Iterable)):
            raise TypeError('Got inappropriate size arg: {}'.format(size))
        im_h, im_w = img.data.shape[:2]
        input_h, input_w = size
        scale = max(input_h / im_h, input_w / im_w)
        # scale = torch.Tensor([[input_h / im_h, input_w / im_w]]).max()
        resized_h = int(np.round(im_h * scale))
        # resized_h = torch.round(im_h * scale)
        resized_w = int(np.round(im_w * scale))
        # resized_w = torch.round(im_w * scale)
        crop_h = int(np.floor(resized_h - input_h) / 2)
        # crop_h = torch.floor(resized_h - input_h) // 2
        crop_w = int(np.floor(resized_w - input_w) / 2)
        # crop_w = torch.floor(resized_w - input_w) // 2
        # resized_img = cv2.resize(img, (resized_w, resized_h))
        resized_img = F.interpolate(
            img.unsqueeze(0).unsqueeze(0), size=(resized_h, resized_w),
            mode='bilinear',align_corners=True)

        resized_img = resized_img.squeeze().unsqueeze(0)

        return resized_img[0, crop_h: crop_h + input_h,
                           crop_w: crop_w + input_w]
//  [python]
def forward(self, image1, image2, iters_lev0=6, scale=1):
        image1 = image1.contiguous()
        image2 = image2.contiguous()

        with autocast(enabled=False):
            fmap1 = self.encoder(image1).float()
            fmap2 = self.encoder(image2).float()

        corr_fn = CorrBlock(fmap1, fmap2, num_levels=2, radius=4)
        coords0, coords1 = self.initialize_flow_4(image1)

        sz = fmap1.shape
        self.sz = sz
        four_point_disp = torch.zeros((sz[0], 2, 2, 2)).to(fmap1.device)

        grid_loss = 0
        disps, flows = [], []

        for itr in range(iters_lev0):
            corr = corr_fn(coords1)
            flow = coords1 - coords0
            with autocast(enabled=False):
                delta_four_point = self.update_block_32(corr, flow)

            four_point_disp =  four_point_disp + delta_four_point
            """
            if not self.constrain:
                four_point_disp =  four_point_disp + delta_four_point
            else:
                four_point_disp =  four_point_disp + torch.tanh(delta_four_point) * 64 * scale
            """

            coords1 = self.get_flow_now_4(four_point_disp)
            disps.append(four_point_disp)
            flow = self.scaling_flow(flow, self.feat_size, self.base_size, scale)
            flow_ = F.upsample_bilinear(flow, None, [16*scale,16*scale]).permute(0,2,3,1)
#            flow_ = F.upsample_bilinear(flow/31*(512*scale-1), None, [16*scale,16*scale]).permute(0,2,3,1)
            flows.append(flow_)

        return flows, disps, grid_loss
//  [python]
def forward(self, image1, image2, iters_lev0=6, scale=1):
        image1 = image1.contiguous()
        image2 = image2.contiguous()

        with autocast(enabled=False):
            fmap1 = self.encoder(image1).float()
            fmap2 = self.encoder(image2).float()

        corr_fn = CorrBlock(fmap1, fmap2, num_levels=2, radius=4)
        coords0, coords1 = self.initialize_flow_4(image1)

        sz = fmap1.shape
        self.sz = sz
        four_point_disp = torch.zeros((sz[0], 2, 2, 2)).to(fmap1.device)

        grid_loss = 0
        disps, flows = [], []

        for itr in range(iters_lev0):
            corr = corr_fn(coords1)
            flow = coords1 - coords0
            with autocast(enabled=False):
                delta_four_point = self.update_block_32(corr, flow)

            four_point_disp =  four_point_disp + delta_four_point
            """
            if not self.constrain:
                four_point_disp =  four_point_disp + delta_four_point
            else:
                four_point_disp =  four_point_disp + torch.tanh(delta_four_point) * 64 * scale
            """

            coords1 = self.get_flow_now_4(four_point_disp)
            disps.append(four_point_disp)
            flow = self.scaling_flow(flow, self.feat_size, self.base_size, scale)
            flow_ = F.interpolate(flow, size=[16*scale,16*scale], mode='bilinear').permute(0,2,3,1)
#            flow_ = F.interpolate(flow/31*(512*scale-1), size=[16*scale,16*scale], mode='bilinear').permute(0,2,3,1)
            flows.append(flow_)

        return flows, disps, grid_loss
//  [python]
def forward(self, inp):
        """Get Inception feature maps

        Parameters
        ----------
        inp : torch.autograd.Variable
            Input tensor of shape Bx3xHxW. Values are expected to be in 
            range (0, 1)

        Returns
        -------
        List of torch.autograd.Variable, corresponding to the selected output 
        block, sorted ascending by index
        """
        outp = []
        x = inp

        if self.resize_input:
            x = F.upsample(x, size=(299, 299), mode='bilinear', align_corners=True)

        if self.normalize_input:
            x = x.clone()
            x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5

        for idx, block in enumerate(self.blocks):
            x = block(x)
            if idx in self.output_blocks:
                outp.append(x)

            if idx == self.last_needed_block:
                break

        return outp
//  [python]
def forward(self, inp):
        """Get Inception feature maps

        Parameters
        ----------
        inp : torch.autograd.Variable
            Input tensor of shape Bx3xHxW. Values are expected to be in 
            range (0, 1)

        Returns
        -------
        List of torch.autograd.Variable, corresponding to the selected output 
        block, sorted ascending by index
        """
        outp = []
        x = inp

        if self.resize_input:
            x = F.interpolate(x, size=(299, 299), mode='bilinear', align_corners=True)

        if self.normalize_input:
            x = x.clone()
            x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5

        for idx, block in enumerate(self.blocks):
            x = block(x)
            if idx in self.output_blocks:
                outp.append(x)

            if idx == self.last_needed_block:
                break

        return outp
//  [python]
def forward(self, x, res_size):
        x = F.upsample(x, (res_size[0] // 4, res_size[1] // 4), mode='bilinear', align_corners=False)

        x = self.conv1(x)
        x = self.relu(x)
        x = F.upsample(x, (res_size[0] // 2, res_size[1] // 2), mode='bilinear', align_corners=False)
        x = self.conv2(x)
        x = self.relu(x)
        x = F.upsample(x, res_size, mode='bilinear', align_corners=False)
        x = self.conv3(x)

        return x
//  [python]
def forward(self, x, res_size):
        x = F.interpolate(x, (res_size[0] // 4, res_size[1] // 4), mode='bilinear', align_corners=False)

        x = self.conv1(x)
        x = self.relu(x)
        x = F.interpolate(x, (res_size[0] // 2, res_size[1] // 2), mode='bilinear', align_corners=False)
        x = self.conv2(x)
        x = self.relu(x)
        x = F.interpolate(x, res_size, mode='bilinear', align_corners=False)
        x = self.conv3(x)

        return x
//  [python]
def forward(self, x, res_size):
        x = self.conv1(x)
        x = F.upsample(x, res_size, mode='bilinear')

        return x
//  [python]
def forward(self, x, res_size):
        x = self.conv1(x)
        x = F.interpolate(x, res_size, mode='bilinear')

        return x
//  [python]
def forward(self, input):
        outsize = input.size()[2:]
        layer = input
        for index in range(0, 2):
            layer = self.base_model.features[index](layer)
        concat1 = layer
        for index in range(2, 4):
            layer = self.base_model.features[index](layer)
        concat2 = layer
        for index in range(4, 7):
            layer = self.base_model.features[index](layer)
        concat3 = layer
        for index in range(7, 14):
            layer = self.base_model.features[index](layer)
        concat4 = layer
        for index in range(14, 19):
            layer = self.base_model.features[index](layer)
        up1 = torch.cat([concat4, self.dconv1(layer)], dim = 1)
        up1 = self.invres1(up1)
        up2 = torch.cat([concat3, self.dconv2(up1)], dim = 1)
        up2 = self.invres2(up2)
        up3 = torch.cat([concat2, self.dconv3(up2)], dim = 1)
        up3 = self.invres3(up3)
        up4 = torch.cat([concat1, self.dconv4(up3)], dim = 1)
        up4 = self.invres4(up4)
        layer = self.conv_last(up4)
        layer = self.conv_score(layer)
        layer = torch.nn.functional.upsample_bilinear(layer, outsize)

        # layer = torch.nn.Sigmoid()(layer)
        # print(layer.shape,'layer')
        return layer
//  [python]
def forward(self, input):
        outsize = input.size()[2:]
        layer = input
        for index in range(0, 2):
            layer = self.base_model.features[index](layer)
        concat1 = layer
        for index in range(2, 4):
            layer = self.base_model.features[index](layer)
        concat2 = layer
        for index in range(4, 7):
            layer = self.base_model.features[index](layer)
        concat3 = layer
        for index in range(7, 14):
            layer = self.base_model.features[index](layer)
        concat4 = layer
        for index in range(14, 19):
            layer = self.base_model.features[index](layer)
        up1 = torch.cat([concat4, self.dconv1(layer)], dim = 1)
        up1 = self.invres1(up1)
        up2 = torch.cat([concat3, self.dconv2(up1)], dim = 1)
        up2 = self.invres2(up2)
        up3 = torch.cat([concat2, self.dconv3(up2)], dim = 1)
        up3 = self.invres3(up3)
        up4 = torch.cat([concat1, self.dconv4(up3)], dim = 1)
        up4 = self.invres4(up4)
        layer = self.conv_last(up4)
        layer = self.conv_score(layer)
        layer = torch.nn.functional.interpolate(layer, size=outsize, mode='bilinear')

        # layer = torch.nn.Sigmoid()(layer)
        # print(layer.shape,'layer')
        return layer
//  [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')

#     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(), )
    if 'dsn' 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)

    # result = interp(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')

#     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(), )
    if 'dsn' 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)

    # result = interp(full_prediction).cpu().data.numpy().transpose(0,2,3,1)
    return result
//  [python]
def forward(self, x):
        h = x.size(2)
        w = x.size(3)
        x1 = self.input_layer(x) + self.input_skip(x)

        x2 = self.squeeze_excite1(x1)
        x2 = self.residual_conv1(x2)

        x3 = self.squeeze_excite2(x2)
        x3 = self.residual_conv2(x3)

        x4 = self.squeeze_excite3(x3)
        x4 = self.residual_conv3(x4)

        x5 = self.aspp_bridge(x4)

        x6 = self.attn1(x3, x5)
        x6 = self.upsample1(x6)
        x6 = torch.cat([x6, x3], dim=1)
        x6 = self.up_residual_conv1(x6)

        x7 = self.attn2(x2, x6)
        x7 = self.upsample2(x7)
        x7 = torch.cat([x7, x2], dim=1)
        x7 = self.up_residual_conv2(x7)

        x8 = self.attn3(x1, x7)
        x8 = self.upsample3(x8)
        x8 = torch.cat([x8, x1], dim=1)
        x8 = self.up_residual_conv3(x8)

        xx = self.aspp_out(x8)
#         out = self.output_layer(x9)
#         out = F.sigmoid(out)
                
         ## side output
        d1 = self.dsn1(x1)
        d2 = F.upsample_bilinear(self.dsn2(x2), size=(h,w))
        d3 = F.upsample_bilinear(self.dsn3(x3), size=(h,w))
        d4 = F.upsample_bilinear(self.dsn4(x4), size=(h,w))
        d5 = F.upsample_bilinear(self.dsn5(x5), size=(h,w))
#
        ###########sigmoid ver
        d1_out = F.sigmoid(d1)
        d2_out = F.sigmoid(d2)
        d3_out = F.sigmoid(d3)
        d4_out = F.sigmoid(d4)
        d5_out = F.sigmoid(d5)

        concat = torch.cat((d1_out, d2_out, d3_out, d4_out, d5_out), 1)

        fuse_box = self.fuse(concat)
        GAP = F.adaptive_avg_pool2d(fuse_box,(1,1))
        GAP = GAP.view(-1, 32)        
        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( [xx, 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, d5_out, boundary_out, mask_out
        elif self.mode =='Infer':
            return mask_out
//  [python]
def forward(self, x, input_size):
        x = self.upconv1(x)
        x = self.relu(x)
        x = F.upsample(x, (input_size[0] // 4, input_size[1] // 4), mode='bilinear', align_corners=False)
        x = self.upconv2(x)
        x = self.relu(x)

        res_4x = self.conv1_flow(x)

        x = F.upsample(x, scale_factor=2, mode='bilinear', align_corners=False)
        x = self.conv2_flow(x)
        x = self.relu(x)
        res_4x_up = F.upsample(res_4x, scale_factor=2, mode='bilinear', align_corners=False)
        conv3_input = torch.cat([x, res_4x_up], dim=1)
        res_2x = self.conv3_flow(conv3_input)

        x = F.upsample(x, scale_factor=2, mode='bilinear', align_corners=False)
        x = self.conv4_flow(x)
        x = self.relu(x)
        res_2x_up = F.upsample(res_2x, scale_factor=2, mode='bilinear', align_corners=False)
        conv5_input = torch.cat([x, res_2x_up], dim=1)
        res_1x = self.conv5_flow(conv5_input)

        return res_1x, res_2x, res_4x
//  [python]
def forward(self, x, input_size):
        x = self.upconv1(x)
        x = self.relu(x)
        x = F.interpolate(x, (input_size[0] // 4, input_size[1] // 4), mode='bilinear', align_corners=False)
        x = self.upconv2(x)
        x = self.relu(x)

        res_4x = self.conv1_flow(x)

        x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
        x = self.conv2_flow(x)
        x = self.relu(x)
        res_4x_up = F.interpolate(res_4x, scale_factor=2, mode='bilinear', align_corners=False)
        conv3_input = torch.cat([x, res_4x_up], dim=1)
        res_2x = self.conv3_flow(conv3_input)

        x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
        x = self.conv4_flow(x)
        x = self.relu(x)
        res_2x_up = F.interpolate(res_2x, scale_factor=2, mode='bilinear', align_corners=False)
        conv5_input = torch.cat([x, res_2x_up], dim=1)
        res_1x = self.conv5_flow(conv5_input)

        return res_1x, res_2x, res_4x
//  [python]
def forward(self, x):
        cls, segmap =self.base_model(x)
        segout= self.seg_fc(segmap)
        return cls,F.upsample_bilinear(segout,scale_factor=8)
//  [python]
def forward(self, x):
        cls, segmap =self.base_model(x)
        segout= self.seg_fc(segmap)
        return cls,F.interpolate(segout,scale_factor=8,mode='bilinear')
//  [python]
def forward(self, x):
        x_size = x.size()[2:]

        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)

        x = F.upsample_bilinear(x, x_size)
        return x
//  [python]
def forward(self, x):
        x_size = x.size()[2:]

        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)

        x = F.interpolate(x, size=x_size, mode='bilinear')
        return x
//  [python]
def forward(self, x):
        """Extract feature vectors from input images."""
        # pdb.set_trace()
        # with torch.no_grad():
        #     features = self.resnet(images)
        # features = features.reshape(features.size(0), -1)
        # features = self.bn(self.linear(features))
        # return features
        self.feature.feats[x.device.index] = []
        x = self.feature(x)
        feats = self.feature.feats[x.device.index]
        feats += [x]
        feats = feats[::-1]
        msk = self.preds[0](feats[0])
        big_msk = msk
        # big_msk = self.upscales[0](msk)
        # big_msk = F.upsample(msk, scale_factor=16)
        msk = F.sigmoid(msk)
        msk_feat = self.reduce(feats[0])*msk
        # msk_feat = F.avg_pool2d(msk_feat, 16).squeeze(3).squeeze(2)
        msk_feat = self.param_pool(msk_feat)
        msk_feat = self.linear(msk_feat)
        big_msk = F.upsample_bilinear(big_msk, scale_factor=16)
        if self.training:
            return big_msk, msk, msk_feat
        else:
            return big_msk
//  [python]
def forward(self, x):
        """Extract feature vectors from input images."""
        self.feature.feats[x.device.index] = []
        x = self.feature(x)
        feats = self.feature.feats[x.device.index]
        feats += [x]
        feats = feats[::-1]
        msk = self.preds[0](feats[0])
        big_msk = msk
        # big_msk = self.upscales[0](msk)
        # big_msk = F.upsample(msk, scale_factor=16)
        msk = F.sigmoid(msk)
        msk_feat = self.reduce(feats[0])*msk
        # msk_feat = F.avg_pool2d(msk_feat, 16).squeeze(3).squeeze(2)
        msk_feat = self.param_pool(msk_feat)
        msk_feat = self.linear(msk_feat)
        big_msk = F.interpolate(big_msk, scale_factor=16, mode='bilinear')
        if self.training:
            return big_msk, msk, msk_feat
        else:
            return big_msk
//  [python]
def forward(self, inp):
        """Get Inception feature maps
        Parameters
        ----------
        inp : torch.autograd.Variable
            Input tensor of shape Bx3xHxW. Values are expected to be in 
            range (0, 1)
        Returns
        -------
        List of torch.autograd.Variable, corresponding to the selected output 
        block, sorted ascending by index
        """
        outp = []
        x = inp

        if self.resize_input:
            x = F.upsample(x, size=(299, 299), mode='bilinear')

        if self.normalize_input:
            x = x.clone()
            x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5

        for idx, block in enumerate(self.blocks):
            x = block(x)
            if idx in self.output_blocks:
                outp.append(x)

            if idx == self.last_needed_block:
                break

        return outp
//  [python]
def forward(self, inp):
        """Get Inception feature maps
        Parameters
        ----------
        inp : torch.autograd.Variable
            Input tensor of shape Bx3xHxW. Values are expected to be in 
            range (0, 1)
        Returns
        -------
        List of torch.autograd.Variable, corresponding to the selected output 
        block, sorted ascending by index
        """
        outp = []
        x = inp

        if self.resize_input:
            x = F.interpolate(x, size=(299, 299), mode='bilinear')

        if self.normalize_input:
            x = x.clone()
            x[:, 0] = x[:, 0] * (0.229 / 0.5) + (0.485 - 0.5) / 0.5
            x[:, 1] = x[:, 1] * (0.224 / 0.5) + (0.456 - 0.5) / 0.5
            x[:, 2] = x[:, 2] * (0.225 / 0.5) + (0.406 - 0.5) / 0.5

        for idx, block in enumerate(self.blocks):
            x = block(x)
            if idx in self.output_blocks:
                outp.append(x)

            if idx == self.last_needed_block:
                break

        return outp
//  [python]
def forward(self, x):
        x = self.dec4(x)
        x = self.dec3(x)
        x = self.dec2(x)
        x = self.final(x)
        x = F.upsample(x, (self.resize, 2 * self.resize), mode='bilinear')
        return x
//  [python]
def forward(self, x):
        x = self.dec4(x)
        x = self.dec3(x)
        x = self.dec2(x)
        x = self.final(x)
        x = F.interpolate(x, (self.resize, 2 * self.resize), mode='bilinear')
        return x
//  [python]
def forward(self,x):

        # Assuming input of size 240x320

        x = self.layer0(x) ## 120x160x64

        layer1 = self.layer1(x) ## 60x80x256
        layer2 = self.layer2(layer1) ## 30x40x512
        layer3 = self.layer3(layer2) ## 15x 20x1024
        layer4 = self.layer4(layer3) ## 7x10x2048

        enc1 = self.br256(self.gcn256(layer1))
        enc2 = self.br512(self.gcn512(layer2))
        enc3 = self.br1024(self.gcn1024(layer3))
        enc4 = self.br2048(self.gcn2048(layer4)) ## 8x10x1
        dec1 = self.br1(F.upsample_bilinear(enc4, size=enc3.size()[2:])+ enc3)
        dec2 = self.br2(F.upsample_bilinear(dec1, enc2.size()[2:]) + enc2)
        dec3 = self.br3(F.upsample_bilinear(dec2, enc1.size()[2:]) + enc1)
        dec4 = self.br4(self.deconv1(dec3))

        score_map = self.br5(self.deconv2(dec4))

        return self.activation(score_map)
//  [python]
def forward(self,x):

        # Assuming input of size 240x320

        x = self.layer0(x) ## 120x160x64

        layer1 = self.layer1(x) ## 60x80x256
        layer2 = self.layer2(layer1) ## 30x40x512
        layer3 = self.layer3(layer2) ## 15x 20x1024
        layer4 = self.layer4(layer3) ## 7x10x2048

        enc1 = self.br256(self.gcn256(layer1))
        enc2 = self.br512(self.gcn512(layer2))
        enc3 = self.br1024(self.gcn1024(layer3))
        enc4 = self.br2048(self.gcn2048(layer4)) ## 8x10x1
        dec1 = self.br1(F.interpolate(enc4, size=enc3.size()[2:], mode='bilinear')+ enc3)
        dec2 = self.br2(F.interpolate(dec1, size=enc2.size()[2:], mode='bilinear') + enc2)
        dec3 = self.br3(F.interpolate(dec2, size=enc1.size()[2:], mode='bilinear') + enc1)
        dec4 = self.br4(self.deconv1(dec3))

        score_map = self.br5(self.deconv2(dec4))

        return self.activation(score_map)
//  [python]
def forward(self, x):
        output_conv33 = self.conv_33(x)
        
        output_conv55 = self.conv_55(output_conv33)

        output_conv77 = self.conv_77(output_conv55)
        output_conv77_toC = self.conv_77_toC(output_conv77)
        output_conv77_upsample = self.upsample_times2(output_conv77_toC)

        output_conv55_toC = self.conv_55_toC(output_conv55)
        output_conv55_toC = output_conv55_toC + output_conv77_upsample
        output_conv55_upsample = self.upsample_times2(output_conv55_toC)

        output_conv33_toC = self.conv_33_toC(output_conv33)
        output_conv33_toC = output_conv33_toC + output_conv55_upsample
        output_conv33_upsample = self.upsample_times2(output_conv33_toC)

        output_conv11 = self.conv_11(x)

        output_global_pool = self.global_pool(x)
        output_pool_conv_toC = self.pool_conv_toC(output_global_pool)
        output_pool_upsample = self.upsample_globalPool(output_pool_conv_toC)

        output_conv11 = output_conv11 * output_conv33_upsample
        output_conv11 = output_conv11 + output_pool_upsample

        final_output = F.upsample_bilinear(output_conv11, scale_factor=8)

        return final_output
//  [python]
def forward(self, x):
        output_conv33 = self.conv_33(x)
        
        output_conv55 = self.conv_55(output_conv33)

        output_conv77 = self.conv_77(output_conv55)
        output_conv77_toC = self.conv_77_toC(output_conv77)
        output_conv77_upsample = self.upsample_times2(output_conv77_toC)

        output_conv55_toC = self.conv_55_toC(output_conv55)
        output_conv55_toC = output_conv55_toC + output_conv77_upsample
        output_conv55_upsample = self.upsample_times2(output_conv55_toC)

        output_conv33_toC = self.conv_33_toC(output_conv33)
        output_conv33_toC = output_conv33_toC + output_conv55_upsample
        output_conv33_upsample = self.upsample_times2(output_conv33_toC)

        output_conv11 = self.conv_11(x)

        output_global_pool = self.global_pool(x)
        output_pool_conv_toC = self.pool_conv_toC(output_global_pool)
        output_pool_upsample = self.upsample_globalPool(output_pool_conv_toC)

        output_conv11 = output_conv11 * output_conv33_upsample
        output_conv11 = output_conv11 + output_pool_upsample

        final_output = F.interpolate(output_conv11, scale_factor=8, mode='bilinear')

        return final_output
//  [python]
def sample_upsample(sample, factor=2, size=None):
    """
    :param sample: [B, H, W, 2]
    :param factor: up_sample factor
    :param size: up_sample size
    :return: upsampled sample
    """
    sample = sample.permute(0, 3, 1, 2)
    if size is None:
        sample = F.upsample_bilinear(sample, scale_factor=factor)
    else:
        sample = F.upsample_bilinear(sample, size=size)
    return sample.permute(0, 2, 3, 1)
//  [python]
def sample_upsample(sample, factor=2, size=None):
    """
    :param sample: [B, H, W, 2]
    :param factor: up_sample factor
    :param size: up_sample size
    :return: upsampled sample
    """
    sample = sample.permute(0, 3, 1, 2)
    if size is None:
        sample = F.interpolate(sample, scale_factor=factor, mode='bilinear')
    else:
        sample = F.interpolate(sample, size=size, mode='bilinear')
    return sample.permute(0, 2, 3, 1)
//  [python]
def forward(self, image, text):
        image = torch.nn.functional.upsample_bilinear(image, (224, 224))
        #image = self.avg_pool(self.upsample(image))
        similarity = 1 - self.model(image, text)[0] / 100
        return similarity
//  [python]
def forward(self, image, text):
        image = torch.nn.functional.interpolate(image, (224, 224), mode='bilinear')
        #image = self.avg_pool(self.upsample(image))
        similarity = 1 - self.model(image, text)[0] / 100
        return similarity