jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def forward(self, *features):
        """
        Forward pass of the decoder module.

        Args:
            *features (list of torch.Tensor): Encoder's output feature maps, where each tensor corresponds
                to a different resolution level.

        Returns:
            torch.Tensor: Segmentation output tensor.
        """
        features = features[1:]  # remove first skip with same spatial resolution
        features = features[::-1]  # reverse channels to start from head of encoder

        head = features[0]
        skips = features[1:]

        x = self.center(head)

        xs = []
        for i, decoder_block in enumerate(self.blocks):
            skip = skips[i] if i < len(skips) else None

            x = decoder_block(x, skip)
            xs.append(x)

        if self.use_hypercolumns:
            h, w = x.size()[2:]
            x = torch.cat([
                F.upsample_bilinear(xs[-4], size=(h, w)),
                F.upsample_bilinear(xs[-3], size=(h, w)),
                F.upsample_bilinear(xs[-2], size=(h, w)),
                x
            ], 1)

            x = self.hypercolumns_conv(x)

        return x
//  [python]
def forward(self, *features):
        """
        Forward pass of the decoder module.

        Args:
            *features (list of torch.Tensor): Encoder's output feature maps, where each tensor corresponds
                to a different resolution level.

        Returns:
            torch.Tensor: Segmentation output tensor.
        """
        features = features[1:]  # remove first skip with same spatial resolution
        features = features[::-1]  # reverse channels to start from head of encoder

        head = features[0]
        skips = features[1:]

        x = self.center(head)

        xs = []
        for i, decoder_block in enumerate(self.blocks):
            skip = skips[i] if i < len(skips) else None

            x = decoder_block(x, skip)
            xs.append(x)

        if self.use_hypercolumns:
            h, w = x.size()[2:]
            x = torch.cat([
                F.interpolate(xs[-4], size=(h, w), mode='bilinear'),
                F.interpolate(xs[-3], size=(h, w), mode='bilinear'),
                F.interpolate(xs[-2], size=(h, w), mode='bilinear'),
                x
            ], 1)

            x = self.hypercolumns_conv(x)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet101_8s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet101_8s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet18_16s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet18_16s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet18_32s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet18_32s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet34_32s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet34_32s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet34_16s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet34_16s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

        return x
//  [python]
def forward(self, in_x):
        features = self.encoder(in_x)
        x = features.reshape((in_x.shape[0], self.feature_channels[-2], -1))
        for i in range(len(features) - 2, -1, -1):
            conv = getattr(self, 'shrink%d' % i)
            x = F.upsample_nearest(x, scale_factor=2)
            x = conv(x)
            if features[i].shape[-1] != x.shape[-1]:
                x2 = F.upsample_nearest(features[i], scale_factor=2)
            else:
                x2 = features[i]
            x = torch.cat([x, x2], 1)
            conv2 = getattr(self, 'shrink2%d' % i)
            x = conv2(x)
            bn = getattr(self, 'batchnorm%d' % i)
            x = bn(x)
            x = F.relu(x)
            x = self.dropout(x)
        x = F.upsample_nearest(x, scale_factor=2)
        logits = self.outc(x)
        return logits
//  [python]
def forward(self, in_x):
        features = self.encoder(in_x)
        x = features.reshape((in_x.shape[0], self.feature_channels[-2], -1))
        for i in range(len(features) - 2, -1, -1):
            conv = getattr(self, 'shrink%d' % i)
            x = F.interpolate(x, scale_factor=2, mode='nearest')
            x = conv(x)
            if features[i].shape[-1] != x.shape[-1]:
                x2 = F.interpolate(features[i], scale_factor=2, mode='nearest')
            else:
                x2 = features[i]
            x = torch.cat([x, x2], 1)
            conv2 = getattr(self, 'shrink2%d' % i)
            x = conv2(x)
            bn = getattr(self, 'batchnorm%d' % i)
            x = bn(x)
            x = F.relu(x)
            x = self.dropout(x)
        x = F.interpolate(x, scale_factor=2, mode='nearest')
        logits = self.outc(x)
        return logits
//  [python]
def forward(self, x):

        input_spatial_dim = x.size()[2:]

        x = self.resnet50_32s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet50_32s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet50_8s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet50_8s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

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

        input_spatial_dim = x.size()[2:]

        if feature_alignment:

            x = adjust_input_image_size_for_proper_feature_alignment(x, output_stride=8)

        x = self.resnet34_8s(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        if feature_alignment:

            x = adjust_input_image_size_for_proper_feature_alignment(x, output_stride=8)

        x = self.resnet34_8s(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

        return x
//  [python]
def forward(self, inputs1, inputs2):
        outputs2 = self.up(F.upsample_bilinear(inputs2,inputs1.size()[2:]))
        return self.cat(torch.cat([inputs1, outputs2], 1))
//  [python]
def forward(self, inputs1, inputs2):
        outputs2 = self.up(F.interpolate(inputs2, inputs1.size()[2:], mode='bilinear'))
        return self.cat(torch.cat([inputs1, outputs2], 1))
//  [python]
def visualize(**kwargs):
    data_loader = kwargs['data_loader']
    net = kwargs['net']

    # metrics initialization
    loss_container.reset()
    raw_metric.reset()
    drop_metric.reset()

    # begin validation
    start_time = time.time()
    net.eval()

    savepath = config.visual_path  # './vis_counterfactual/'

    if not os.path.isdir(savepath):
        os.mkdir(savepath)

    with torch.no_grad():
        for i, (X, y) in enumerate(data_loader):
            X = X.to(device)

            p, attention_maps = net.visualize(X)
            attention_maps = torch.max(attention_maps, dim=1, keepdim=True)[0]
            attention_maps = F.upsample_bilinear(attention_maps, size=(X.size(2), X.size(3)))
            attention_maps = torch.sqrt(attention_maps.cpu() / attention_maps.max().item())

            # get heat attention maps
            heat_attention_maps = generate_heatmap(attention_maps)

            # raw_image, heat_attention, raw_attention
            raw_image = X.cpu() * STD + MEAN
            heat_attention_image = raw_image * 0.5 + heat_attention_maps * 0.5
            raw_attention_image = raw_image * attention_maps

            for batch_idx in range(X.size(0)):
                rimg = ToPILImage(raw_image[batch_idx])
                # raimg = ToPILImage(raw_attention_image[batch_idx])
                haimg = ToPILImage(heat_attention_image[batch_idx])
                rimg.save(os.path.join(savepath, '%03d_raw.jpg' % (i * config.batch_size + batch_idx)))
                # raimg.save(os.path.join(savepath, '%03d_raw_atten.jpg' % (i * config.batch_size + batch_idx)))
                haimg.save(os.path.join(savepath, '%03d_heat_atten.jpg' % (i * config.batch_size + batch_idx)))

            print('iter %d / %d done!' % (i, len(data_loader)))
//  [python]
def visualize(**kwargs):
    data_loader = kwargs['data_loader']
    net = kwargs['net']

    # metrics initialization
    loss_container.reset()
    raw_metric.reset()
    drop_metric.reset()

    # begin validation
    start_time = time.time()
    net.eval()

    savepath = config.visual_path  # './vis_counterfactual/'

    if not os.path.isdir(savepath):
        os.mkdir(savepath)

    with torch.no_grad():
        for i, (X, y) in enumerate(data_loader):
            X = X.to(device)

            p, attention_maps = net.visualize(X)
            attention_maps = torch.max(attention_maps, dim=1, keepdim=True)[0]
            attention_maps = F.interpolate(attention_maps, size=(X.size(2), X.size(3)), mode='bilinear')
            attention_maps = torch.sqrt(attention_maps.cpu() / attention_maps.max().item())

            # get heat attention maps
            heat_attention_maps = generate_heatmap(attention_maps)

            # raw_image, heat_attention, raw_attention
            raw_image = X.cpu() * STD + MEAN
            heat_attention_image = raw_image * 0.5 + heat_attention_maps * 0.5
            raw_attention_image = raw_image * attention_maps

            for batch_idx in range(X.size(0)):
                rimg = ToPILImage(raw_image[batch_idx])
                # raimg = ToPILImage(raw_attention_image[batch_idx])
                haimg = ToPILImage(heat_attention_image[batch_idx])
                rimg.save(os.path.join(savepath, '%03d_raw.jpg' % (i * config.batch_size + batch_idx)))
                # raimg.save(os.path.join(savepath, '%03d_raw_atten.jpg' % (i * config.batch_size + batch_idx)))
                haimg.save(os.path.join(savepath, '%03d_heat_atten.jpg' % (i * config.batch_size + batch_idx)))

            print('iter %d / %d done!' % (i, len(data_loader)))
//  [python]
def forward(self, x):

        input_spatial_dim = x.size()[2:]

        x = self.resnet18_8s.conv1(x)
        x = self.resnet18_8s.bn1(x)
        x = self.resnet18_8s.relu(x)
        x = self.resnet18_8s.maxpool(x)

        x = self.resnet18_8s.layer1[0](x)
        x = self.resnet18_8s.layer2[0](x)
        x = self.resnet18_8s.layer3[0](x)
        x = self.resnet18_8s.layer4[0](x)

        x = self.resnet18_8s.fc(x)

        x = nn.functional.upsample_bilinear(input=x, size=input_spatial_dim)

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

        input_spatial_dim = x.size()[2:]

        x = self.resnet18_8s.conv1(x)
        x = self.resnet18_8s.bn1(x)
        x = self.resnet18_8s.relu(x)
        x = self.resnet18_8s.maxpool(x)

        x = self.resnet18_8s.layer1[0](x)
        x = self.resnet18_8s.layer2[0](x)
        x = self.resnet18_8s.layer3[0](x)
        x = self.resnet18_8s.layer4[0](x)

        x = self.resnet18_8s.fc(x)

        x = nn.functional.interpolate(input=x, size=input_spatial_dim, mode='bilinear')

        return x
//  [python]
def forward(self, x, y):
        out = F.upsample_bilinear(self.conv1(x), y.size()[2:])
        out = F.relu(out+y, inplace=True)
        out = F.sigmoid(out)
        out = F.relu(self.conv2(out*y),inplace=True)
        return out
//  [python]
def forward(self, x, y):
        out = F.interpolate(self.conv1(x), size=y.size()[2:], mode='bilinear')
        out = F.relu(out+y, inplace=True)
        out = F.sigmoid(out)
        out = F.relu(self.conv2(out*y),inplace=True)
        return out
//  [python]
def forward(self, x,target):
        x1=self.conv1(torch.cat([x,target],1))
        x1 = self.trans_block1(x1)
        x2=self.conv2(x1)
        x2 = self.trans_block2(x2)
        x3=self.conv3(x2)
        x3 = self.trans_block3(x3)
        x4=self.conv3(x3)
        x4 = self.trans_block4(x4)
        #print(x4.size())
        residual = self.sig(self.conv_refin(self.sig(F.avg_pool2d(x4,16))))
        #print(residual)
        residual = F.upsample_nearest(residual, scale_factor=128)
        #print(residual.size())
        return residual
//  [python]
def forward(self, x,target):
        x1=self.conv1(torch.cat([x,target],1))
        x1 = self.trans_block1(x1)
        x2=self.conv2(x1)
        x2 = self.trans_block2(x2)
        x3=self.conv3(x2)
        x3 = self.trans_block3(x3)
        x4=self.conv3(x3)
        x4 = self.trans_block4(x4)
        #print(x4.size())
        residual = self.sig(self.conv_refin(self.sig(F.avg_pool2d(x4,16))))
        #print(residual)
        residual = F.interpolate(residual, scale_factor=128, mode='nearest')
        #print(residual.size())
        return residual
//  [python]
def forward(self, x):
        c2, c3, c4, c5 = x

        in5 = self.ins_conv[3](c5)
        in4 = self.ins_conv[2](c4)
        in3 = self.ins_conv[1](c3)
        in2 = self.ins_conv[0](c2)

        out4 = in4 + F.upsample(
            in5, scale_factor=2, mode="nearest")  # 1/16
        out3 = in3 + F.upsample(
            out4, scale_factor=2, mode="nearest")  # 1/8
        out2 = in2 + F.upsample(
            out3, scale_factor=2, mode="nearest")  # 1/4

        p5 = self.inp_conv[3](in5)
        p4 = self.inp_conv[2](out4)
        p3 = self.inp_conv[1](out3)
        p2 = self.inp_conv[0](out2)

        p5 = F.upsample(p5, scale_factor=8, mode="nearest")
        p4 = F.upsample(p4, scale_factor=4, mode="nearest")
        p3 = F.upsample(p3, scale_factor=2, mode="nearest")

        fuse = torch.cat([p5, p4, p3, p2], dim=1)
        return fuse
//  [python]
def forward(self, x):
        c2, c3, c4, c5 = x

        in5 = self.ins_conv[3](c5)
        in4 = self.ins_conv[2](c4)
        in3 = self.ins_conv[1](c3)
        in2 = self.ins_conv[0](c2)

        out4 = in4 + F.interpolate(
            in5, scale_factor=2, mode="nearest")  # 1/16
        out3 = in3 + F.interpolate(
            out4, scale_factor=2, mode="nearest")  # 1/8
        out2 = in2 + F.interpolate(
            out3, scale_factor=2, mode="nearest")  # 1/4

        p5 = self.inp_conv[3](in5)
        p4 = self.inp_conv[2](out4)
        p3 = self.inp_conv[1](out3)
        p2 = self.inp_conv[0](out2)

        p5 = F.interpolate(p5, scale_factor=8, mode="nearest")
        p4 = F.interpolate(p4, scale_factor=4, mode="nearest")
        p3 = F.interpolate(p3, scale_factor=2, mode="nearest")

        fuse = torch.cat([p5, p4, p3, p2], dim=1)
        return fuse
//  [python]
def forward(self, x):
        channel_att_sum = None
        b,c,h,w=x.size()

        for pool_type in self.pool_types:
            if pool_type=='avg':
                avg_pool = F.avg_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.conv_fc1(avg_pool).view(b,c,self.num,self.num)
            elif pool_type=='max':
                max_pool = F.max_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.conv_fc2(max_pool).view(b,c,self.num,self.num)

            if channel_att_sum is None:
                channel_att_sum = channel_att_raw
            else:
                channel_att_sum = torch.cat([channel_att_sum, channel_att_raw],dim=2)
        
        channel_weight=self.conv(channel_att_sum)
        scale = nn.functional.upsample_bilinear(channel_weight, [h, w])

        return x * scale
//  [python]
def forward(self, x):
        channel_att_sum = None
        b,c,h,w=x.size()

        for pool_type in self.pool_types:
            if pool_type=='avg':
                avg_pool = F.avg_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.conv_fc1(avg_pool).view(b,c,self.num,self.num)
            elif pool_type=='max':
                max_pool = F.max_pool2d( x, (x.size(2), x.size(3)), stride=(x.size(2), x.size(3)))
                channel_att_raw = self.conv_fc2(max_pool).view(b,c,self.num,self.num)

            if channel_att_sum is None:
                channel_att_sum = channel_att_raw
            else:
                channel_att_sum = torch.cat([channel_att_sum, channel_att_raw],dim=2)
        
        channel_weight=self.conv(channel_att_sum)
        scale = nn.functional.interpolate(channel_weight, [h, w], mode='bilinear')

        return x * scale
//  [python]
def forward(self, x):
        x = self.features4(x)       
        x = self.de_pred(x)

        x = F.upsample(x,scale_factor=8)

        return x
//  [python]
def forward(self, x):
        x = self.features4(x)       
        x = self.de_pred(x)

        x = F.interpolate(x,scale_factor=8)

        return x
//  [python]
def forward(self, x):
        next_x = []
        for level, feature in enumerate(x):                         # Loop for each fpn layer

            temp_fea = self.Pconv[1](level, feature)
            if level > 0:
                temp_fea += self.Pconv[2](level, x[level - 1])
            if level < len(x) - 1:
                temp_fea += F.upsample_bilinear(
                    self.Pconv[0](level, x[level + 1]),
                    size=[temp_fea.size(2), temp_fea.size(3)])
            next_x.append(temp_fea)
        if self.iBN:
            next_x = iBN(next_x, self.bn)
        next_x = [self.relu(item) for item in next_x]
        return next_x
//  [python]
def forward(self, x):
        next_x = []
        for level, feature in enumerate(x):                         # Loop for each fpn layer

            temp_fea = self.Pconv[1](level, feature)
            if level > 0:
                temp_fea += self.Pconv[2](level, x[level - 1])
            if level < len(x) - 1:
                temp_fea += F.interpolate(
                    self.Pconv[0](level, x[level + 1]),
                    size=[temp_fea.size(2), temp_fea.size(3)], mode='bilinear')
            next_x.append(temp_fea)
        if self.iBN:
            next_x = iBN(next_x, self.bn)
        next_x = [self.relu(item) for item in next_x]
        return next_x
//  [python]
def forward(self, x):
        c2, c3, c4, c5 = x

        in5 = self.ins_conv[3](c5)
        in4 = self.ins_conv[2](c4)
        in3 = self.ins_conv[1](c3)
        in2 = self.ins_conv[0](c2)

        out4 = in4 + F.upsample(
            in5, scale_factor=2, mode="nearest")  # 1/16
        out3 = in3 + F.upsample(
            out4, scale_factor=2, mode="nearest")  # 1/8
        out2 = in2 + F.upsample(
            out3, scale_factor=2, mode="nearest")  # 1/4

        f5 = self.inp_conv[3](in5)
        f4 = self.inp_conv[2](out4)
        f3 = self.inp_conv[1](out3)
        f2 = self.inp_conv[0](out2)

        pan3 = f3 + self.pan_head_conv[0](f2)
        pan4 = f4 + self.pan_head_conv[1](pan3)
        pan5 = f5 + self.pan_head_conv[2](pan4)

        p2 = self.pan_lat_conv[0](f2)
        p3 = self.pan_lat_conv[1](pan3)
        p4 = self.pan_lat_conv[2](pan4)
        p5 = self.pan_lat_conv[3](pan5)

        if self.intracl is True:
            p5 = self.incl4(p5)
            p4 = self.incl3(p4)
            p3 = self.incl2(p3)
            p2 = self.incl1(p2)

        p5 = F.upsample(p5, scale_factor=8, mode="nearest")
        p4 = F.upsample(p4, scale_factor=4, mode="nearest")
        p3 = F.upsample(p3, scale_factor=2, mode="nearest")

        fuse = torch.cat([p5, p4, p3, p2], dim=1)
        return fuse
//  [python]
def forward(self, x):
        c2, c3, c4, c5 = x

        in5 = self.ins_conv[3](c5)
        in4 = self.ins_conv[2](c4)
        in3 = self.ins_conv[1](c3)
        in2 = self.ins_conv[0](c2)

        out4 = in4 + F.interpolate(
            in5, scale_factor=2, mode="nearest")  # 1/16
        out3 = in3 + F.interpolate(
            out4, scale_factor=2, mode="nearest")  # 1/8
        out2 = in2 + F.interpolate(
            out3, scale_factor=2, mode="nearest")  # 1/4

        f5 = self.inp_conv[3](in5)
        f4 = self.inp_conv[2](out4)
        f3 = self.inp_conv[1](out3)
        f2 = self.inp_conv[0](out2)

        pan3 = f3 + self.pan_head_conv[0](f2)
        pan4 = f4 + self.pan_head_conv[1](pan3)
        pan5 = f5 + self.pan_head_conv[2](pan4)

        p2 = self.pan_lat_conv[0](f2)
        p3 = self.pan_lat_conv[1](pan3)
        p4 = self.pan_lat_conv[2](pan4)
        p5 = self.pan_lat_conv[3](pan5)

        if self.intracl is True:
            p5 = self.incl4(p5)
            p4 = self.incl3(p4)
            p3 = self.incl2(p3)
            p2 = self.incl1(p2)

        p5 = F.interpolate(p5, scale_factor=8, mode="nearest")
        p4 = F.interpolate(p4, scale_factor=4, mode="nearest")
        p3 = F.interpolate(p3, scale_factor=2, mode="nearest")

        fuse = torch.cat([p5, p4, p3, p2], dim=1)
        return fuse
//  [python]
def compute_clip_loss(device, c_net, img, text):
    img = torch.nn.functional.upsample_bilinear(img, (224, 224))
    tokenized_text = clip.tokenize([text]).to(device)

    img_logits, _text_logits = c_net(img, tokenized_text)

    return 1/img_logits * 100
//  [python]
def compute_clip_loss(device, c_net, img, text):
    img = torch.nn.functional.interpolate(img, (224, 224), mode='bilinear')
    tokenized_text = clip.tokenize([text]).to(device)

    img_logits, _text_logits = c_net(img, tokenized_text)

    return 1/img_logits * 100
//  [python]
def forward(self, x):
        '''out = x
        for i in xrange(len(self.scale)):
            self.res[i] = F.avg_pool2d(x, self.scale[i], self.stride[i])
            self.res[i] = F.upsample_bilinear(self.res[i], size=self.output_size)
            out += self.res[i]'''
        res1 = F.avg_pool2d(x, self.scale[0], self.stride[0])
        res1_up = F.upsample_bilinear(res1, size=self.output_size)
        res2 = F.avg_pool2d(x, self.scale[1], self.stride[1])
        res2_up = F.upsample_bilinear(res2, size=self.output_size)
        res3 = F.avg_pool2d(x, self.scale[2], self.stride[2])
        res3_up = F.upsample_bilinear(res3, size=self.output_size)
        res4 = F.avg_pool2d(x, self.scale[3], self.stride[3])
        res4_up = F.upsample_bilinear(res4, size=self.output_size)
        out = res1_up + res2_up + res3_up + res4_up + x

        return out
//  [python]
def forward(self, x):
        res1 = F.avg_pool2d(x, self.scale[0], self.stride[0])
        res1_up = F.interpolate(res1, size=self.output_size, mode='bilinear')
        res2 = F.avg_pool2d(x, self.scale[1], self.stride[1])
        res2_up = F.interpolate(res2, size=self.output_size, mode='bilinear')
        res3 = F.avg_pool2d(x, self.scale[2], self.stride[2])
        res3_up = F.interpolate(res3, size=self.output_size, mode='bilinear')
        res4 = F.avg_pool2d(x, self.scale[3], self.stride[3])
        res4_up = F.interpolate(res4, size=self.output_size, mode='bilinear')
        out = res1_up + res2_up + res3_up + res4_up + x

        return out
//  [python]
def visualize(features, args, file_name=None):
    """
    Converts a 4d map of features to alpha attention weights,
    According to their 2-Norm across dimensions 0 and 1.
    Then saves the input RGB image as an RGBA image using an upsampling of this attention map.
    """
    save_file = os.path.join(args.viz_dir, file_name)
    img_path = args.image

    # Scale map to [0, 1]
    f_map = (features ** 2).mean(0).mean(1).squeeze().sqrt()
    f_map_shifted = f_map - f_map.min().expand_as(f_map)
    f_map_scaled = f_map_shifted / f_map_shifted.max().expand_as(f_map_shifted)

    if save_file is None:
      print(f_map_scaled)
    else:
      # Read original image
      img = imread(img_path, mode='RGB')
      orig_img_size = img.shape

      # Convert to image format
      alpha = (255 * f_map_scaled).round()
      alpha4d = alpha.unsqueeze(0).unsqueeze(0)
      alpha_upsampled = torch.nn.functional.upsample_bilinear(
        alpha4d, size=torch.Size(orig_img_size)).squeeze(0).transpose(1, 0).transpose(1, 2)
      alpha_upsampled_np = alpha_upsampled.cpu().data.numpy()

      # Create and save visualization
      imga = np.concatenate([img, alpha_upsampled_np], axis=2)
      if save_file[-4:] != '.png': save_file += '.png'
      imsave(save_file, imga)

    return f_map_scaled
//  [python]
def visualize(features, args, file_name=None):
    """
    Converts a 4d map of features to alpha attention weights,
    According to their 2-Norm across dimensions 0 and 1.
    Then saves the input RGB image as an RGBA image using an upsampling of this attention map.
    """
    save_file = os.path.join(args.viz_dir, file_name)
    img_path = args.image

    # Scale map to [0, 1]
    f_map = (features ** 2).mean(0).mean(1).squeeze().sqrt()
    f_map_shifted = f_map - f_map.min().expand_as(f_map)
    f_map_scaled = f_map_shifted / f_map_shifted.max().expand_as(f_map_shifted)

    if save_file is None:
      print(f_map_scaled)
    else:
      # Read original image
      img = imread(img_path, mode='RGB')
      orig_img_size = img.shape

      # Convert to image format
      alpha = (255 * f_map_scaled).round()
      alpha4d = alpha.unsqueeze(0).unsqueeze(0)
      alpha_upsampled = torch.nn.functional.interpolate(
        alpha4d, size=torch.Size(orig_img_size), mode='bilinear').squeeze(0).transpose(1, 0).transpose(1, 2)
      alpha_upsampled_np = alpha_upsampled.cpu().data.numpy()

      # Create and save visualization
      imga = np.concatenate([img, alpha_upsampled_np], axis=2)
      if save_file[-4:] != '.png': save_file += '.png'
      imsave(save_file, imga)

    return f_map_scaled
//  [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 forward_single(self, x, solo_cls, solo_mask, grid_num):
        cls_feat = F.upsample_bilinear(x,(grid_num,grid_num))
        mask_feat = x
        for cls_layer in self.cls_convs:
            cls_feat = cls_layer(cls_feat)
        cls_score = solo_cls(cls_feat)

        for mask_layer in self.mask_convs:
            mask_feat = mask_layer(mask_feat)
        mask_score = solo_mask(mask_feat)
        # scale the bbox_pred of different level
        # float to avoid overflow when enabling FP16
        return cls_score, mask_score
//  [python]
def forward_single(self, x, solo_cls, solo_mask, grid_num):
        cls_feat = F.interpolate(x,(grid_num,grid_num),mode='bilinear')
        mask_feat = x
        for cls_layer in self.cls_convs:
            cls_feat = cls_layer(cls_feat)
        cls_score = solo_cls(cls_feat)

        for mask_layer in self.mask_convs:
            mask_feat = mask_layer(mask_feat)
        mask_score = solo_mask(mask_feat)
        # scale the bbox_pred of different level
        # float to avoid overflow when enabling FP16
        return cls_score, mask_score
//  [python]
def forward(self,
              x,
              alpha,
              **kwargs):

    img_size = x.shape[-1]
    if img_size < 128:
      x = F.upsample_bilinear(x, size=128)


    # if global_cfg.tl_debug:
    #   VerboseModel.forward_verbose(self.first_layer,
    #                                inputs_args=(x.clone(), ),
    #                                submodels=['model'],
    #                                name_prefix=f"first_layer.")
    # x = self.first_layer(x)

    if kwargs.get('instance_noise', 0) > 0:
      x = x + torch.randn_like(x) * kwargs['instance_noise']

    for i, layer in enumerate(self.layers):

      if global_cfg.tl_debug:
        VerboseModel.forward_verbose(layer,
                                     inputs_args=(x.clone(),),
                                     submodels=['network', 'proj'],
                                     name_prefix=f"layers[{i}].")
      x = layer(x)

    x = F.adaptive_avg_pool2d(x, output_size=1)
    x = x.flatten(1)

    if global_cfg.tl_debug:
      VerboseModel.forward_verbose(self.final_layer,
                                   inputs_args=(x,),
                                   name_prefix=f"final_layer.")
    x = self.final_layer(x).reshape(x.shape[0], -1)

    prediction = x[..., 0:1]
    latent = x[..., 1:1+self.dim_z]
    position = x[..., 1+self.dim_z:]

    return prediction, latent, position
//  [python]
def forward(self,
              x,
              alpha,
              **kwargs):

    img_size = x.shape[-1]
    if img_size < 128:
      x = F.interpolate(x, size=128, mode='bilinear')


    # if global_cfg.tl_debug:
    #   VerboseModel.forward_verbose(self.first_layer,
    #                                inputs_args=(x.clone(), ),
    #                                submodels=['model'],
    #                                name_prefix=f"first_layer.")
    # x = self.first_layer(x)

    if kwargs.get('instance_noise', 0) > 0:
      x = x + torch.randn_like(x) * kwargs['instance_noise']

    for i, layer in enumerate(self.layers):

      if global_cfg.tl_debug:
        VerboseModel.forward_verbose(layer,
                                     inputs_args=(x.clone(),),
                                     submodels=['network', 'proj'],
                                     name_prefix=f"layers[{i}].")
      x = layer(x)

    x = F.adaptive_avg_pool2d(x, output_size=1)
    x = x.flatten(1)

    if global_cfg.tl_debug:
      VerboseModel.forward_verbose(self.final_layer,
                                   inputs_args=(x,),
                                   name_prefix=f"final_layer.")
    x = self.final_layer(x).reshape(x.shape[0], -1)

    prediction = x[..., 0:1]
    latent = x[..., 1:1+self.dim_z]
    position = x[..., 1+self.dim_z:]

    return prediction, latent, position
//  [python]
def forward(self, x, w):
        x = F.upsample_nearest(x, size=(12,12))
        x = F.upsample_nearest(x, scale_factor=2)

        w = F.upsample_nearest(w, scale_factor=(2.976744,2.976744))
        return x, w
//  [python]
def forward(self, x, w):
        x = F.interpolate(x, size=(12,12), mode='nearest')
        x = F.interpolate(x, scale_factor=2, mode='nearest')

        w = F.interpolate(w, scale_factor=(2.976744,2.976744), mode='nearest')
        return x, w
//  [python]
def forward(self, x):
        x = self.conv1(x)
        # x = self.bn1(x)
        x_1 = self.relu1(x)
        # print "x_1: %s" % str(x_1.size())
        x = self.maxpool1(x_1)
        x = self.fire1_1(x)
        x_2 = self.fire1_2(x)
        # print "x_2: %s" % str(x_2.size())
        x = self.maxpool2(x_2)
        x = self.fire2_1(x)
        x_3 = self.fire2_2(x)
        # print "x_3: %s" % str(x_3.size())
        x = self.maxpool3(x_3)
        x = self.fire3_1(x)
        x = self.fire3_2(x)
        x = self.fire3_3(x)
        x = self.parallel(x)
        # print "x: %s" % str(x.size())
        y_3 = self.deconv1(x)
        y_3 = self.relu2(y_3)
        x_3 = self.conv3_1(x_3)
        x_3 = self.relu3_1(x_3)
        # print "y_3: %s" % str(y_3.size())
        # x = x.transpose(1, 2, 0)
        # print('x_3.size():', x_3.size())
        # print('y_3.size():', y_3.size())
        x_3 = F.upsample_bilinear(x_3, y_3.size()[2:])
        x = torch.cat([x_3, y_3], 1)
        x = self.conv3_2(x)
        x = self.relu3_2(x)
        # concat x_3
        y_2 = self.deconv2(x)
        y_2 = self.relu3(y_2)
        x_2 = self.conv2_1(x_2)
        x_2 = self.relu2_1(x_2)
        # print "y_2: %s" % str(y_2.size())
        # concat x_2
        # print('x_2.size():', x_2.size())
        # print('y_2.size():', y_2.size())
        y_2 = F.upsample_bilinear(y_2, x_2.size()[2:])
        x = torch.cat([x_2, y_2], 1)
        x = self.conv2_2(x)
        x = self.relu2_2(x)
        y_1 = self.deconv3(x)
        y_1 = self.relu4(y_1)
        x_1 = self.conv1_1(x_1)
        x_1 = self.relu1_1(x_1)
        # print "y_1: %s" % str(y_1.size())
        # concat x_1
        x = torch.cat([x_1, y_1], 1)
        x = self.conv1_2(x)
        x = self.relu1_2(x)
        x = self.deconv4(x)
        return x
//  [python]
def forward(self, x):
        x = self.conv1(x)
        # x = self.bn1(x)
        x_1 = self.relu1(x)
        # print "x_1: %s" % str(x_1.size())
        x = self.maxpool1(x_1)
        x = self.fire1_1(x)
        x_2 = self.fire1_2(x)
        # print "x_2: %s" % str(x_2.size())
        x = self.maxpool2(x_2)
        x = self.fire2_1(x)
        x_3 = self.fire2_2(x)
        # print "x_3: %s" % str(x_3.size())
        x = self.maxpool3(x_3)
        x = self.fire3_1(x)
        x = self.fire3_2(x)
        x = self.fire3_3(x)
        x = self.parallel(x)
        # print "x: %s" % str(x.size())
        y_3 = self.deconv1(x)
        y_3 = self.relu2(y_3)
        x_3 = self.conv3_1(x_3)
        x_3 = self.relu3_1(x_3)
        # print "y_3: %s" % str(y_3.size())
        # x = x.transpose(1, 2, 0)
        # print('x_3.size():', x_3.size())
        # print('y_3.size():', y_3.size())
        x_3 = F.interpolate(x_3, y_3.size()[2:], mode='bilinear')
        x = torch.cat([x_3, y_3], 1)
        x = self.conv3_2(x)
        x = self.relu3_2(x)
        # concat x_3
        y_2 = self.deconv2(x)
        y_2 = self.relu3(y_2)
        x_2 = self.conv2_1(x_2)
        x_2 = self.relu2_1(x_2)
        # print "y_2: %s" % str(y_2.size())
        # concat x_2
        # print('x_2.size():', x_2.size())
        # print('y_2.size():', y_2.size())
        y_2 = F.interpolate(y_2, x_2.size()[2:], mode='bilinear')
        x = torch.cat([x_2, y_2], 1)
        x = self.conv2_2(x)
        x = self.relu2_2(x)
        y_1 = self.deconv3(x)
        y_1 = self.relu4(y_1)
        x_1 = self.conv1_1(x_1)
        x_1 = self.relu1_1(x_1)
        # print "y_1: %s" % str(y_1.size())
        # concat x_1
        x = torch.cat([x_1, y_1], 1)
        x = self.conv1_2(x)
        x = self.relu1_2(x)
        x = self.deconv4(x)
        return x
//  [python]
def forward(self, x):

        if self.num_levels == 2:
                                    # if x: 512
            fm0 = self.layer0(x)    # 256
            fm1 = self.layer1(fm0)  # 128
            fm2 = self.layer2(fm1)  # 64

            gcfm1 = self.brm3(self.gcm3(fm2))  # 64
            gcfm2 = self.brm4(self.gcm4(fm1))  # 128

            fs1 = self.brm7(F.upsample_bilinear(gcfm1, fm1.size()[2:]) + gcfm2)  # 128
            fs2 = self.brm8(F.upsample_bilinear(fs1,   fm0.size()[2:]))          # 256
            out = self.brm9(F.upsample_bilinear(fs2,     x.size()[2:]))          # 512

            return out

        elif self.num_levels == 3:
                                    # if x: 512
            fm0 = self.layer0(x)    # 256
            fm1 = self.layer1(fm0)  # 128
            fm2 = self.layer2(fm1)  # 64
            fm3 = self.layer3(fm2)  # 32

            gcfm1 = self.brm2(self.gcm2(fm3))  # 32
            gcfm2 = self.brm3(self.gcm3(fm2))  # 64
            gcfm3 = self.brm4(self.gcm4(fm1))  # 128

            fs1 = self.brm6(F.upsample_bilinear(gcfm1, fm2.size()[2:]) + gcfm2)  # 64
            fs2 = self.brm7(F.upsample_bilinear(fs1,   fm1.size()[2:]) + gcfm3)  # 128
            fs3 = self.brm8(F.upsample_bilinear(fs2,   fm0.size()[2:]))          # 256
            out = self.brm9(F.upsample_bilinear(fs3,     x.size()[2:]))          # 512

            return out

        else:
                                    # if x: 512
            fm0 = self.layer0(x)    # 256
            fm1 = self.layer1(fm0)  # 128
            fm2 = self.layer2(fm1)  # 64
            fm3 = self.layer3(fm2)  # 32
            fm4 = self.layer4(fm3)  # 16

            gcfm1 = self.brm1(self.gcm1(fm4))  # 16
            gcfm2 = self.brm2(self.gcm2(fm3))  # 32
            gcfm3 = self.brm3(self.gcm3(fm2))  # 64
            gcfm4 = self.brm4(self.gcm4(fm1))  # 128

            fs1 = self.brm5(F.upsample_bilinear(gcfm1, fm3.size()[2:]) + gcfm2)  # 32
            fs2 = self.brm6(F.upsample_bilinear(fs1,   fm2.size()[2:]) + gcfm3)  # 64
            fs3 = self.brm7(F.upsample_bilinear(fs2,   fm1.size()[2:]) + gcfm4)  # 128
            fs4 = self.brm8(F.upsample_bilinear(fs3,   fm0.size()[2:]))          # 256
            out = self.brm9(F.upsample_bilinear(fs4,     x.size()[2:]))          # 512

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

        if self.num_levels == 2:
                                    # if x: 512
            fm0 = self.layer0(x)    # 256
            fm1 = self.layer1(fm0)  # 128
            fm2 = self.layer2(fm1)  # 64

            gcfm1 = self.brm3(self.gcm3(fm2))  # 64
            gcfm2 = self.brm4(self.gcm4(fm1))  # 128

            fs1 = self.brm7(F.interpolate(gcfm1, fm1.size()[2:], mode='bilinear') + gcfm2)  # 128
            fs2 = self.brm8(F.interpolate(fs1,   fm0.size()[2:], mode='bilinear'))          # 256
            out = self.brm9(F.interpolate(fs2,     x.size()[2:], mode='bilinear'))          # 512

            return out

        elif self.num_levels == 3:
                                    # if x: 512
            fm0 = self.layer0(x)    # 256
            fm1 = self.layer1(fm0)  # 128
            fm2 = self.layer2(fm1)  # 64
            fm3 = self.layer3(fm2)  # 32

            gcfm1 = self.brm2(self.gcm2(fm3))  # 32
            gcfm2 = self.brm3(self.gcm3(fm2))  # 64
            gcfm3 = self.brm4(self.gcm4(fm1))  # 128

            fs1 = self.brm6(F.interpolate(gcfm1, fm2.size()[2:], mode='bilinear') + gcfm2)  # 64
            fs2 = self.brm7(F.interpolate(fs1,   fm1.size()[2:], mode='bilinear') + gcfm3)  # 128
            fs3 = self.brm8(F.interpolate(fs2,   fm0.size()[2:], mode='bilinear'))          # 256
            out = self.brm9(F.interpolate(fs3,     x.size()[2:], mode='bilinear'))          # 512

            return out

        else:
                                    # if x: 512
            fm0 = self.layer0(x)    # 256
            fm1 = self.layer1(fm0)  # 128
            fm2 = self.layer2(fm1)  # 64
            fm3 = self.layer3(fm2)  # 32
            fm4 = self.layer4(fm3)  # 16

            gcfm1 = self.brm1(self.gcm1(fm4))  # 16
            gcfm2 = self.brm2(self.gcm2(fm3))  # 32
            gcfm3 = self.brm3(self.gcm3(fm2))  # 64
            gcfm4 = self.brm4(self.gcm4(fm1))  # 128

            fs1 = self.brm5(F.interpolate(gcfm1, fm3.size()[2:], mode='bilinear') + gcfm2)  # 32
            fs2 = self.brm6(F.interpolate(fs1,   fm2.size()[2:], mode='bilinear') + gcfm3)  # 64
            fs3 = self.brm7(F.interpolate(fs2,   fm1.size()[2:], mode='bilinear') + gcfm4)  # 128
            fs4 = self.brm8(F.interpolate(fs3,   fm0.size()[2:], mode='bilinear'))          # 256
            out = self.brm9(F.interpolate(fs4,     x.size()[2:], mode='bilinear'))          # 512

            return out
//  [python]
def forward(self, x):
        dec1 = self.dec1(x)
        dec2 = self.dec2(dec1)
        dec3 = self.dec3(dec2)
        dec4 = self.dec4(dec3)
        center = self.center(dec4)

        enc4 = self.enc4(torch.cat([
            center, F.upsample_bilinear(dec4, scale_factor=center.size()[2] / dec4.size()[2])], 1))
        enc3 = self.enc3(torch.cat([
            enc4, F.upsample_bilinear(dec3, scale_factor=enc4.size()[2] / dec3.size()[2])], 1))
        enc2 = self.enc2(torch.cat([
            enc3, F.upsample_bilinear(dec2, scale_factor=enc3.size()[2] / dec2.size()[2])], 1))
        enc1 = self.enc1(torch.cat([
            enc2, F.upsample_bilinear(dec1, scale_factor=enc2.size()[2] / dec1.size()[2])], 1))

        return self.final(enc1)
//  [python]
def forward(self, x):
        dec1 = self.dec1(x)
        dec2 = self.dec2(dec1)
        dec3 = self.dec3(dec2)
        dec4 = self.dec4(dec3)
        center = self.center(dec4)

        enc4 = self.enc4(torch.cat([
            center, F.interpolate(dec4, size=center.size()[2:], mode='bilinear')], 1))
        enc3 = self.enc3(torch.cat([
            enc4, F.interpolate(dec3, size=enc4.size()[2:], mode='bilinear')], 1))
        enc2 = self.enc2(torch.cat([
            enc3, F.interpolate(dec2, size=enc3.size()[2:], mode='bilinear')], 1))
        enc1 = self.enc1(torch.cat([
            enc2, F.interpolate(dec1, size=enc2.size()[2:], mode='bilinear')], 1))

        return self.final(enc1)
//  [python]
def get_bboxes(self,cls_scores, mask_preds, img_metas, cfg, rescale=None):
        flatten_cls_scores = torch.cat([cls_score.permute(0, 2, 3, 1).reshape(-1, self.cls_out_channels) for cls_score in cls_scores]).sigmoid()
        _, _, b_h, b_w = mask_preds[0].shape
        for i in range(5):
            mask_preds[i] = F.upsample_bilinear(mask_preds[i],(b_h,b_w))
        mask_preds = torch.cat(mask_preds,dim=1)[0]
        scores, labels = torch.max(flatten_cls_scores,dim=-1)
        nms_pre = cfg.get('nms_pre', -1)
        mask_thr = cfg.get('mask_thr_binary',-1)
        crop_h, crop_w, _ = img_metas[0]['img_shape']
        ori_h, ori_w, _ = img_metas[0]['ori_shape']
        if nms_pre > 0 and scores.shape[0] > nms_pre:
            _, topk_inds = scores.topk(nms_pre)
            scores = scores[topk_inds]
            labels = labels[topk_inds]

            mask_preds = mask_preds[topk_inds]
            mask_preds = F.upsample_bilinear(mask_preds.unsqueeze(0), (b_h*self.strides[0], b_w*self.strides[0]))
            mask_preds = mask_preds[:, :, :crop_h, :crop_w]
            mask_preds = F.sigmoid(F.upsample_bilinear(mask_preds, (ori_h, ori_w)))[0]
            mask_preds = mask_preds > mask_thr

            masks = self.nms(scores, labels, mask_preds, cfg.nms.iou_thr)
            n = len(masks)
            det_bboxes = np.zeros((n, 5))
            det_labels = np.zeros(n).astype(int)
            det_masks = []
            for i in range(n):
                det_bboxes[i, -1] = masks[i][-1]
                det_labels[i] = masks[i][-2]
                det_masks.append(masks[i][0])
            det_masks = np.array(det_masks)
        return det_bboxes, det_labels, det_masks
//  [python]
def get_bboxes(self,cls_scores, mask_preds, img_metas, cfg, rescale=None):
        flatten_cls_scores = torch.cat([cls_score.permute(0, 2, 3, 1).reshape(-1, self.cls_out_channels) for cls_score in cls_scores]).sigmoid()
        _, _, b_h, b_w = mask_preds[0].shape
        for i in range(5):
            mask_preds[i] = F.interpolate(mask_preds[i],(b_h,b_w),mode='bilinear')
        mask_preds = torch.cat(mask_preds,dim=1)[0]
        scores, labels = torch.max(flatten_cls_scores,dim=-1)
        nms_pre = cfg.get('nms_pre', -1)
        mask_thr = cfg.get('mask_thr_binary',-1)
        crop_h, crop_w, _ = img_metas[0]['img_shape']
        ori_h, ori_w, _ = img_metas[0]['ori_shape']
        if nms_pre > 0 and scores.shape[0] > nms_pre:
            _, topk_inds = scores.topk(nms_pre)
            scores = scores[topk_inds]
            labels = labels[topk_inds]

            mask_preds = mask_preds[topk_inds]
            mask_preds = F.interpolate(mask_preds.unsqueeze(0), (b_h*self.strides[0], b_w*self.strides[0]),mode='bilinear')
            mask_preds = mask_preds[:, :, :crop_h, :crop_w]
            mask_preds = F.sigmoid(F.interpolate(mask_preds, (ori_h, ori_w),mode='bilinear'))[0]
            mask_preds = mask_preds > mask_thr

            masks = self.nms(scores, labels, mask_preds, cfg.nms.iou_thr)
            n = len(masks)
            det_bboxes = np.zeros((n, 5))
            det_labels = np.zeros(n).astype(int)
            det_masks = []
            for i in range(n):
                det_bboxes[i, -1] = masks[i][-1]
                det_labels[i] = masks[i][-2]
                det_masks.append(masks[i][0])
            det_masks = np.array(det_masks)
        return det_bboxes, det_labels, det_masks
//  [python]
def forward(self, x):
        conv1 = self.conv1_block(x)
        conv2 = self.conv2_block(conv1)
        conv3 = self.conv3_block(conv2)
        conv4 = self.conv4_block(conv3)
        conv5 = self.conv5_block(conv4)
        score = self.classifier(conv5)

        if self.module_type=='16s' or self.module_type=='8s':
            score_pool4 = self.score_pool4(conv4)
        if self.module_type=='8s':
            score_pool3 = self.score_pool3(conv3)
        # print(conv1.data.size())
        # print(conv2.data.size())
        # print(conv4.data.size())
        # print(conv5.data.size())
        # print(score.data.size())
        # print(x.data.size())
        if self.module_type=='16s' or self.module_type=='8s':
            score = F.upsample_bilinear(score, score_pool4.size()[2:])
            score += score_pool4
        if self.module_type=='8s':
            score = F.upsample_bilinear(score, score_pool3.size()[2:])
            score += score_pool3

        out = F.upsample_bilinear(score, x.size()[2:])
        return out
//  [python]
def forward(self, x):
        conv1 = self.conv1_block(x)
        conv2 = self.conv2_block(conv1)
        conv3 = self.conv3_block(conv2)
        conv4 = self.conv4_block(conv3)
        conv5 = self.conv5_block(conv4)
        score = self.classifier(conv5)

        if self.module_type=='16s' or self.module_type=='8s':
            score_pool4 = self.score_pool4(conv4)
        if self.module_type=='8s':
            score_pool3 = self.score_pool3(conv3)
        # print(conv1.data.size())
        # print(conv2.data.size())
        # print(conv4.data.size())
        # print(conv5.data.size())
        # print(score.data.size())
        # print(x.data.size())
        if self.module_type=='16s' or self.module_type=='8s':
            score = F.interpolate(score, size=score_pool4.size()[2:], mode='bilinear')
            score += score_pool4
        if self.module_type=='8s':
            score = F.interpolate(score, size=score_pool3.size()[2:], mode='bilinear')
            score += score_pool3

        out = F.interpolate(score, size=x.size()[2:], mode='bilinear')
        return out