jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x_1 = self.layer1(x)
        x_2 = self.layer2(x_1)
        x_3 = self.layer3(x_2)
        x_4 = self.layer4(x_3)
        x_4 = self.gcn4(x_4)
        x_4 = self.br1_4(x_4)
        x_4_up = F.upsample_bilinear(x_4, x_3.size()[2:])
        x_3 = self.gcn3(x_3)
        x_3 = self.br1_3(x_3)
        x_3_skip = x_3 + x_4_up
        x_3_skip = self.br2_3(x_3_skip)
        x_3_up = F.upsample_bilinear(x_3_skip, x_2.size()[2:])
        x_2 = self.gcn2(x_2)
        x_2 = self.br1_2(x_2)
        x_2_skip = x_2 + x_3_up
        x_2_skip = self.br2_2(x_2_skip)
        x_2_up = F.upsample_bilinear(x_2_skip, x_1.size()[2:])
        x_1 = self.gcn1(x_1)
        x_1 = self.br1_1(x_1)
        x_1_skip = x_1 + x_2_up
        x_1_skip = self.br2_1(x_1_skip)
        x_1_up = F.upsample_bilinear(x_1_skip, scale_factor=2)
        x_out = self.br3_1(x_1_up)
        x_out = F.upsample_bilinear(x_out, scale_factor=2)
        x_out = self.br3_2(x_out)
        return x_out
//  [python]
def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)
        x_1 = self.layer1(x)
        x_2 = self.layer2(x_1)
        x_3 = self.layer3(x_2)
        x_4 = self.layer4(x_3)
        x_4 = self.gcn4(x_4)
        x_4 = self.br1_4(x_4)
        x_4_up = F.interpolate(x_4, size=x_3.size()[2:], mode='bilinear')
        x_3 = self.gcn3(x_3)
        x_3 = self.br1_3(x_3)
        x_3_skip = x_3 + x_4_up
        x_3_skip = self.br2_3(x_3_skip)
        x_3_up = F.interpolate(x_3_skip, size=x_2.size()[2:], mode='bilinear')
        x_2 = self.gcn2(x_2)
        x_2 = self.br1_2(x_2)
        x_2_skip = x_2 + x_3_up
        x_2_skip = self.br2_2(x_2_skip)
        x_2_up = F.interpolate(x_2_skip, size=x_1.size()[2:], mode='bilinear')
        x_1 = self.gcn1(x_1)
        x_1 = self.br1_1(x_1)
        x_1_skip = x_1 + x_2_up
        x_1_skip = self.br2_1(x_1_skip)
        x_1_up = F.interpolate(x_1_skip, scale_factor=2, mode='bilinear')
        x_out = self.br3_1(x_1_up)
        x_out = F.interpolate(x_out, scale_factor=2, mode='bilinear')
        x_out = self.br3_2(x_out)
        return x_out
//  [python]
def forward(self, x):
        y1 = self.res1(x)
        y2 = self.res2(x)
        y3 = self.res3(x)
        y1 = F.upsample_bilinear(y1, x.size()[2:])
        y2 = F.upsample_bilinear(y2, x.size()[2:])
        y3 = F.upsample_bilinear(y3, x.size()[2:])
        out = x + y1 + y2 + y3
        out = self.relu(out)
        return out
//  [python]
def forward(self, x):
        y1 = self.res1(x)
        y2 = self.res2(x)
        y3 = self.res3(x)
        y1 = F.interpolate(y1, size=x.size()[2:], mode='bilinear')
        y2 = F.interpolate(y2, size=x.size()[2:], mode='bilinear')
        y3 = F.interpolate(y3, size=x.size()[2:], mode='bilinear')
        out = x + y1 + y2 + y3
        out = self.relu(out)
        return out
//  [python]
def forward(self, x):
        out_size = x.size()[2:]
        down1_x = self.down1(x)
        maxpool1_x = self.maxpool1(down1_x)
        down2_x = self.down2(maxpool1_x)
        maxpool2_x = self.maxpool2(down2_x)
        down3_x = self.down3(maxpool2_x)
        maxpool3_x = self.maxpool3(down3_x)
        down4_x = self.down4(maxpool3_x)
        maxpool4_x = self.maxpool1(down4_x)
        center_x = self.center(maxpool4_x)
        up4_x = self.up4(center_x, down4_x)
        up3_x = self.up3(up4_x, down3_x)
        up2_x = self.up2(up3_x, down2_x)
        up1_x = self.up1(up2_x, down1_x)
        x = self.classifier(up1_x)
        x = F.upsample_bilinear(x, out_size)
        return x
//  [python]
def forward(self, x):
        out_size = x.size()[2:]
        down1_x = self.down1(x)
        maxpool1_x = self.maxpool1(down1_x)
        down2_x = self.down2(maxpool1_x)
        maxpool2_x = self.maxpool2(down2_x)
        down3_x = self.down3(maxpool2_x)
        maxpool3_x = self.maxpool3(down3_x)
        down4_x = self.down4(maxpool3_x)
        maxpool4_x = self.maxpool1(down4_x)
        center_x = self.center(maxpool4_x)
        up4_x = self.up4(center_x, down4_x)
        up3_x = self.up3(up4_x, down3_x)
        up2_x = self.up2(up3_x, down2_x)
        up1_x = self.up1(up2_x, down1_x)
        x = self.classifier(up1_x)
        x = F.interpolate(x, out_size, mode='bilinear')
        return x
//  [python]
def forward(self, image):
        if self.transform is None:
            return self.conv_fn(image)
        else:
            down_image = self.pool(image)
            down_feature = self.transform(down_image)

            up_image = F.upsample_nearest(down_image, scale_factor=self.factor)
            up_feature = F.upsample_nearest(down_feature, scale_factor=self.factor)
            dimage = image - up_image
            feature = torch.cat([dimage, up_feature], dim=1)
            return self.conv_fn(feature)
//  [python]
def forward(self, image):
        if self.transform is None:
            return self.conv_fn(image)
        else:
            down_image = self.pool(image)
            down_feature = self.transform(down_image)

            up_image = F.interpolate(down_image, scale_factor=self.factor, mode='nearest')
            up_feature = F.interpolate(down_feature, scale_factor=self.factor, mode='nearest')
            dimage = image - up_image
            feature = torch.cat([dimage, up_feature], dim=1)
            return self.conv_fn(feature)
//  [python]
def forward(self, x):
        x = self.conv1(x)
        x_1 = self.relu1(x)
        x = self.maxpool1(x_1)
        x = self.fire1_1(x)
        x_2 = self.fire1_2(x)
        x = self.maxpool2(x_2)
        x = self.fire2_1(x)
        x_3 = self.fire2_2(x)
        x = self.maxpool3(x_3)
        x = self.fire3_1(x)
        x = self.fire3_2(x)
        x = self.fire3_3(x)
        x = self.parallel(x)
        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)
        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)
        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)
        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)
        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_1 = self.relu1(x)
        x = self.maxpool1(x_1)
        x = self.fire1_1(x)
        x_2 = self.fire1_2(x)
        x = self.maxpool2(x_2)
        x = self.fire2_1(x)
        x_3 = self.fire2_2(x)
        x = self.maxpool3(x_3)
        x = self.fire3_1(x)
        x = self.fire3_2(x)
        x = self.fire3_3(x)
        x = self.parallel(x)
        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)
        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)
        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)
        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)
        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):
        inp_shape = x.size()[2:]

        # H, W -> H/2, W/2
        x = self.convbnrelu1_3(self.convbnrelu1_2(self.convbnrelu1_1(x)))
        # H/2, W/2 -> H/4, W/4
        x = F.max_pool2d(x, 3, 2, 1)
        # H/4, W/4 -> H/8, W/8
        x = self.res_block5(self.res_block4(self.res_block3(self.res_block2(x))))
        x = self.pyramid_pooling(x)
        x = F.dropout2d(self.cbr_final(x), p=0.1, inplace=True)
        x = self.classification(x)
        x = F.upsample(x, size=inp_shape, mode='bilinear')
        return x
//  [python]
def forward(self, x):
        inp_shape = x.size()[2:]

        # H, W -> H/2, W/2
        x = self.convbnrelu1_3(self.convbnrelu1_2(self.convbnrelu1_1(x)))
        # H/2, W/2 -> H/4, W/4
        x = F.max_pool2d(x, 3, 2, 1)
        # H/4, W/4 -> H/8, W/8
        x = self.res_block5(self.res_block4(self.res_block3(self.res_block2(x))))
        x = self.pyramid_pooling(x)
        x = F.dropout2d(self.cbr_final(x), p=0.1, inplace=True)
        x = self.classification(x)
        x = F.interpolate(x, size=inp_shape, mode='bilinear')
        return x
//  [python]
def forward(
            self,
            y: torch.Tensor,
            sf: int = 2) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        x = F.upsample(y, scale_factor=sf, mode='bicubic')
        d = None
        k = self.head_k.repeat(x.size(0), 1, 1, 1, 1).to(x.device)

        return x, d, k
//  [python]
def forward(
            self,
            y: torch.Tensor,
            sf: int = 2) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        x = F.interpolate(y, scale_factor=sf, mode='bicubic')
        d = None
        k = self.head_k.repeat(x.size(0), 1, 1, 1, 1).to(x.device)

        return x, d, k
//  [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):
        y1 = self.res1(x)
        y2 = self.res2(x)
        y3 = self.res3(x)
        # print('y1:', y1.size())
        # print('y2:', y2.size())
        # print('y3:', y3.size())
        y1 = F.upsample_bilinear(y1, x.size()[2:])
        y2 = F.upsample_bilinear(y2, x.size()[2:])
        y3 = F.upsample_bilinear(y3, x.size()[2:])
        out = x + y1 + y2 + y3
        out = self.relu(out)
        return out
//  [python]
def forward(self, x):
        # 输出已经默认包括x
        output_slices = [x]
        # 输出宽度需要和x相同
        h, w = x.view()[2:]

        for module, pool_size in zip(self.path_module_list, self.pool_sizes):
            # 金字塔池化操作，分别对每一个module和sizes进行操作
            # 首先使用平均池化层操作获得相应size的池化特征图
            out = F.avg_pool2d(x, pool_size, stride=1, padding=0)
            # 通过module减小维度降低计算量
            out = module(out)
            # 然后上采样即可
            out = F.upsample(out, size=(h,w), mode='bilinear')
            output_slices.append(out)

        # 最后把[x, pool1_up, pool2_up, pool3_up, pool4_up] concat即可
        return torch.cat(output_slices, dim=1)
//  [python]
def forward(self, x):
        # 输出已经默认包括x
        output_slices = [x]
        # 输出宽度需要和x相同
        h, w = x.view()[2:]

        for module, pool_size in zip(self.path_module_list, self.pool_sizes):
            # 金字塔池化操作，分别对每一个module和sizes进行操作
            # 首先使用平均池化层操作获得相应size的池化特征图
            out = F.avg_pool2d(x, pool_size, stride=1, padding=0)
            # 通过module减小维度降低计算量
            out = module(out)
            # 然后上采样即可
            out = F.interpolate(out, size=(h,w), mode='bilinear')
            output_slices.append(out)

        # 最后把[x, pool1_up, pool2_up, pool3_up, pool4_up] concat即可
        return torch.cat(output_slices, dim=1)
//  [python]
def forward(self, x):
        # ----normal forward----
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x_1 = self.layer1(x)
        x_2 = self.layer2(x_1)
        x_3 = self.layer3(x_2)
        x_4 = self.layer4(x_3)
        # ----normal forward----

        x_4 = self.gcn4(x_4)
        x_4 = self.br1_4(x_4)
        x_4_up = F.upsample_bilinear(x_4, x_3.size()[2:])

        x_3 = self.gcn3(x_3)
        x_3 = self.br1_3(x_3)
        x_3_skip = x_3 + x_4_up
        x_3_skip = self.br2_3(x_3_skip)
        x_3_up = F.upsample_bilinear(x_3_skip, x_2.size()[2:])

        x_2 = self.gcn2(x_2)
        x_2 = self.br1_2(x_2)
        x_2_skip = x_2 + x_3_up
        x_2_skip = self.br2_2(x_2_skip)
        x_2_up = F.upsample_bilinear(x_2_skip, x_1.size()[2:])

        x_1 = self.gcn1(x_1)
        x_1 = self.br1_1(x_1)
        x_1_skip = x_1 + x_2_up
        x_1_skip = self.br2_1(x_1_skip)
        x_1_up = F.upsample_bilinear(x_1_skip, scale_factor=2)

        x_out = self.br3_1(x_1_up)
        x_out = F.upsample_bilinear(x_out, scale_factor=2)
        x_out = self.br3_2(x_out)

        return x_out
//  [python]
def forward(self, x):
        # ----normal forward----
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x_1 = self.layer1(x)
        x_2 = self.layer2(x_1)
        x_3 = self.layer3(x_2)
        x_4 = self.layer4(x_3)
        # ----normal forward----

        x_4 = self.gcn4(x_4)
        x_4 = self.br1_4(x_4)
        x_4_up = F.interpolate(x_4, size=x_3.size()[2:], mode='bilinear')

        x_3 = self.gcn3(x_3)
        x_3 = self.br1_3(x_3)
        x_3_skip = x_3 + x_4_up
        x_3_skip = self.br2_3(x_3_skip)
        x_3_up = F.interpolate(x_3_skip, size=x_2.size()[2:], mode='bilinear')

        x_2 = self.gcn2(x_2)
        x_2 = self.br1_2(x_2)
        x_2_skip = x_2 + x_3_up
        x_2_skip = self.br2_2(x_2_skip)
        x_2_up = F.interpolate(x_2_skip, size=x_1.size()[2:], mode='bilinear')

        x_1 = self.gcn1(x_1)
        x_1 = self.br1_1(x_1)
        x_1_skip = x_1 + x_2_up
        x_1_skip = self.br2_1(x_1_skip)
        x_1_up = F.interpolate(x_1_skip, scale_factor=2, mode='bilinear')

        x_out = self.br3_1(x_1_up)
        x_out = F.interpolate(x_out, scale_factor=2, mode='bilinear')
        x_out = self.br3_2(x_out)

        return x_out
//  [python]
def forward(self, feats):
		feats = list(feats)
		_,_,b_h,b_w = feats[0].shape
		feats[0] = F.upsample_bilinear(feats[0], (int(b_h/2.),int(b_w/2.)))
		feats[4] = F.upsample_bilinear(feats[4], (int(b_h/8.),int(b_w/8.)))

		cls_score, mask_score = multi_apply(self.forward_single, feats, self.solo_cls, self.solo_mask, self.grid_num)
		return cls_score, mask_score
//  [python]
def forward(self, feats):
		feats = list(feats)
		_,_,b_h,b_w = feats[0].shape
		feats[0] = F.interpolate(feats[0], (int(b_h/2.),int(b_w/2.)), mode='bilinear')
		feats[4] = F.interpolate(feats[4], (int(b_h/8.),int(b_w/8.)), mode='bilinear')

		cls_score, mask_score = multi_apply(self.forward_single, feats, self.solo_cls, self.solo_mask, self.grid_num)
		return cls_score, mask_score
//  [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)

        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)

        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, layout):
    """
    Output will have same size as layout
    """
    # H, W = self.output_size
    N, _, H, W = layout.size()
    self.layout = layout

    # Figure out size of input
    input_H, input_W = H, W
    for _ in range(len(self.refinement_modules)):
      input_H //= 2
      input_W //= 2

    assert input_H != 0
    assert input_W != 0

    feats = torch.zeros(N, 1, input_H, input_W).to(layout)
    for mod in self.refinement_modules:
      feats = F.upsample(feats, scale_factor=2, mode='nearest')
      feats = mod(layout, feats)

    out = self.output_conv(feats)
    return out
//  [python]
def forward(self, layout):
    """
    Output will have same size as layout
    """
    # H, W = self.output_size
    N, _, H, W = layout.size()
    self.layout = layout

    # Figure out size of input
    input_H, input_W = H, W
    for _ in range(len(self.refinement_modules)):
      input_H //= 2
      input_W //= 2

    assert input_H != 0
    assert input_W != 0

    feats = torch.zeros(N, 1, input_H, input_W).to(layout)
    for mod in self.refinement_modules:
      feats = F.interpolate(feats, scale_factor=2, mode='nearest')
      feats = mod(layout, feats)

    out = self.output_conv(feats)
    return out
//  [python]
def forward(self, x,x1,x2,x3):
        ## 256x256
        x4=(self.dense_block4(x))
        x4=self.trans_block4(x4)
        x_4=func.upsample_nearest(x4, scale_factor=2)
        x_4=torch.cat([x_4,x3],1)

        x5=(self.dense_block5(x_4))
        x5=self.trans_block5(x5)
        x_5=func.upsample_nearest(x5, scale_factor=2)
        x_5=torch.cat([x_5,x2],1)

        x6=(self.dense_block6(x_5))
        x6=(self.trans_block6(x6))
        x_6=func.upsample_nearest(x6, scale_factor=2)
        x_6=torch.cat([x_6,x1],1)

        return x_5,x_6
//  [python]
def forward(self, x,x1,x2,x3):
        ## 256x256
        x4=(self.dense_block4(x))
        x4=self.trans_block4(x4)
        x_4=func.interpolate(x4, scale_factor=2, mode='nearest')
        x_4=torch.cat([x_4,x3],1)

        x5=(self.dense_block5(x_4))
        x5=self.trans_block5(x5)
        x_5=func.interpolate(x5, scale_factor=2, mode='nearest')
        x_5=torch.cat([x_5,x2],1)

        x6=(self.dense_block6(x_5))
        x6=(self.trans_block6(x6))
        x_6=func.interpolate(x6, scale_factor=2, mode='nearest')
        x_6=torch.cat([x_6,x1],1)

        return x_5,x_6
//  [python]
def forward(self, input):
        x, low_level_features = self.resnet_features(input)
        x1 = self.aspp1(x)
        x2 = self.aspp2(x)
        x3 = self.aspp3(x)
        x4 = self.aspp4(x)
        x5 = self.global_avg_pool(x)
        x5 = F.upsample(x5, size=x4.size()[2:], mode='bilinear', align_corners=True)

        x = torch.cat((x1, x2, x3, x4, x5), dim=1)

        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = F.upsample(x, size=(int(math.ceil(input.size()[-2]/4)),
                                int(math.ceil(input.size()[-1]/4))), mode='bilinear', align_corners=True)

        low_level_features = self.conv2(low_level_features)
        low_level_features = self.bn2(low_level_features)
        low_level_features = self.relu(low_level_features)


        x = torch.cat((x, low_level_features), dim=1)
        x = self.last_conv(x)
        x = F.interpolate(x, size=input.size()[2:], mode='bilinear', align_corners=True)

        return x
//  [python]
def forward(self, input):
        x, low_level_features = self.resnet_features(input)
        x1 = self.aspp1(x)
        x2 = self.aspp2(x)
        x3 = self.aspp3(x)
        x4 = self.aspp4(x)
        x5 = self.global_avg_pool(x)
        x5 = F.interpolate(x5, size=x4.size()[2:], mode='bilinear', align_corners=True)

        x = torch.cat((x1, x2, x3, x4, x5), dim=1)

        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = F.interpolate(x, size=(int(math.ceil(input.size()[-2]/4)),
                                int(math.ceil(input.size()[-1]/4))), mode='bilinear', align_corners=True)

        low_level_features = self.conv2(low_level_features)
        low_level_features = self.bn2(low_level_features)
        low_level_features = self.relu(low_level_features)


        x = torch.cat((x, low_level_features), dim=1)
        x = self.last_conv(x)
        x = F.interpolate(x, size=input.size()[2:], mode='bilinear', align_corners=True)

        return x
//  [python]
def loss(self,
				cls_scores,
				mask_preds,
				gt_bboxes,
				gt_labels,
				gt_masks,
				category_targets,
				point_ins,
				img_metas,
				cfg,
				gt_bboxes_ignore=None):
		assert len(cls_scores) == len(mask_preds)
		_, _, b_h, b_w = mask_preds[0].shape

		for i in range(len(self.grid_num)):
			mask_preds[i] = F.sigmoid(F.upsample_bilinear(mask_preds[i], (b_h, b_w)))
		mask_preds = torch.cat([mask_pred for mask_pred in mask_preds], dim=1)

		bound = [self.grid_num[i] ** 2 for i in range(5)]
		bound = [0] + bound
		for i in range(1, 6):
			bound[i] += bound[i - 1]

		num_imgs = len(category_targets)
		for i in range(num_imgs):
			_, i_h, i_w = gt_masks[i].shape
			gt_masks[i] = nn.ConstantPad2d((0, b_w * self.strides[0] - i_w, 0, b_h * self.strides[0] - i_h), 0)(torch.tensor(gt_masks[i]))
			gt_masks[i] = F.upsample_nearest(gt_masks[i].float().unsqueeze(0), (b_h, b_w))[0]

		flatten_cls_scores = [cls_score.permute(0, 2, 3, 1).reshape(num_imgs, -1, self.cls_out_channels) for cls_score in cls_scores]
		# need to check images first or dimensions first: image first and then row first
		flatten_cls_scores = torch.cat([cls_score for cls_score in flatten_cls_scores], dim=1).reshape(-1, self.cls_out_channels)

		# calculate loss
		loss_mask = torch.zeros(num_imgs).to(mask_preds[0].device)

		t_num = 0
		for i in range(num_imgs):
			ind = torch.nonzero(category_targets[i]).squeeze(-1)
			ins_ind = point_ins[i][ind]
			ins_mask = F.upsample_bilinear(gt_masks[i][ins_ind].to(mask_preds.device).unsqueeze(0), (self.loss_level*b_h, self.loss_level*b_w))[0]
			pred_mask = F.sigmoid(F.upsample_bilinear(mask_preds[i][ind].unsqueeze(0), (self.loss_level*b_h, self.loss_level*b_w))[0])
			loss_mask[i] = self.dict_loss_batch(pred_mask, ins_mask, reduce='sum')
			t_num += len(ind)

		loss_mask = self.dict_weight * torch.sum(loss_mask)/t_num
		category_targets = torch.cat(category_targets)
		num_pos = (category_targets > 0).sum()

		loss_cls = self.loss_cls(flatten_cls_scores, category_targets, avg_factor=num_pos + num_imgs)

		return dict(
			loss_cls=loss_cls,
			loss_mask=loss_mask)
//  [python]
def loss(self,
				cls_scores,
				mask_preds,
				gt_bboxes,
				gt_labels,
				gt_masks,
				category_targets,
				point_ins,
				img_metas,
				cfg,
				gt_bboxes_ignore=None):
		assert len(cls_scores) == len(mask_preds)
		_, _, b_h, b_w = mask_preds[0].shape

		for i in range(len(self.grid_num)):
			mask_preds[i] = F.sigmoid(F.interpolate(mask_preds[i], (b_h, b_w), mode='bilinear'))
		mask_preds = torch.cat([mask_pred for mask_pred in mask_preds], dim=1)

		bound = [self.grid_num[i] ** 2 for i in range(5)]
		bound = [0] + bound
		for i in range(1, 6):
			bound[i] += bound[i - 1]

		num_imgs = len(category_targets)
		for i in range(num_imgs):
			_, i_h, i_w = gt_masks[i].shape
			gt_masks[i] = nn.ConstantPad2d((0, b_w * self.strides[0] - i_w, 0, b_h * self.strides[0] - i_h), 0)(torch.tensor(gt_masks[i]))
			gt_masks[i] = F.interpolate(gt_masks[i].float().unsqueeze(0), (b_h, b_w), mode='nearest')[0]

		flatten_cls_scores = [cls_score.permute(0, 2, 3, 1).reshape(num_imgs, -1, self.cls_out_channels) for cls_score in cls_scores]
		# need to check images first or dimensions first: image first and then row first
		flatten_cls_scores = torch.cat([cls_score for cls_score in flatten_cls_scores], dim=1).reshape(-1, self.cls_out_channels)

		# calculate loss
		loss_mask = torch.zeros(num_imgs).to(mask_preds[0].device)

		t_num = 0
		for i in range(num_imgs):
			ind = torch.nonzero(category_targets[i]).squeeze(-1)
			ins_ind = point_ins[i][ind]
			ins_mask = F.interpolate(gt_masks[i][ins_ind].to(mask_preds.device).unsqueeze(0), (self.loss_level*b_h, self.loss_level*b_w), mode='bilinear')[0]
			pred_mask = F.sigmoid(F.interpolate(mask_preds[i][ind].unsqueeze(0), (self.loss_level*b_h, self.loss_level*b_w), mode='bilinear')[0])
			loss_mask[i] = self.dict_loss_batch(pred_mask, ins_mask, reduce='sum')
			t_num += len(ind)

		loss_mask = self.dict_weight * torch.sum(loss_mask)/t_num
		category_targets = torch.cat(category_targets)
		num_pos = (category_targets > 0).sum()

		loss_cls = self.loss_cls(flatten_cls_scores, category_targets, avg_factor=num_pos + num_imgs)

		return dict(
			loss_cls=loss_cls,
			loss_mask=loss_mask)
//  [python]
def forward(self, x5):
        v1 = self.conv_t(x5)
        v2 = v1 > 0
        v3 = v1 * 0.4987
        v4 = torch.where(v2, v1, v3)
        v5 = v4 + torch.nn.functional.upsample_bilinear(v4, (13, 31))
        return v4
//  [python]
def forward(self, x5):
        v1 = self.conv_t(x5)
        v2 = v1 > 0
        v3 = v1 * 0.4987
        v4 = torch.where(v2, v1, v3)
        v5 = v4 + torch.nn.functional.interpolate(v4, (13, 31), mode='bilinear')
        return v4
//  [python]
def infer_vis(self, img_metas, cls_scores, det_masks, det_bboxes, det_labels, det_scs, out_path="vis_tmp"):

		# ==================vis====================
		# cls_scores_ori
		# ==================vis====================
		ori_h, ori_w, _ = img_metas[0]['ori_shape']
		imgname = img_metas[0]['filename']
		imgname2 = imgname.split('/')
		imgname2 = imgname2[-1][:-4]
		img = np.array(Image.open(imgname))
		det_masks_int = det_masks * 1
		out_ = os.path.join(out_path, imgname2)
		out_cls = os.path.join(out_path, imgname2+'_cls')

		if not os.path.exists(out_):
			os.makedirs(out_)
		if not os.path.exists(out_cls):
			os.makedirs(out_cls)
		
		# # vis cls
		cls_scores_ori = []
		 
		for i in range(5):
			cls_sc = cls_scores[i].clone().detach()
			cls_sc = F.upsample_bilinear(cls_sc, (ori_h, ori_w))
			cls_scores_ori.append(cls_sc)
		cls_all = cls_color(cls_scores_ori, self.use_sigmoid, img)
		 
		for i, cls_ in enumerate(cls_all):
			out_name = os.path.join(out_cls, "scale_%d"%(i) + '.jpg')
			clsim = Image.fromarray(np.uint8(cls_))
			clsim.save(out_name)
		 
		for i in range(5):
			if self.use_sigmoid:
				cls_scores_ori_i = cls_scores_ori[i].sigmoid().cpu()
			else:
				cls_scores_ori_i = cls_scores_ori[i].softmax(dim=1)[:,1:].cpu()

			for j in range(80):
				cls_scores_ori_ij = Image.fromarray(np.uint8(cls_scores_ori_i[0,j,:,:]*255))
				out_name = os.path.join(out_cls, "%02d"%(i) +'_' + "%02d"%(j)+'_'+CLASSES[j]+'.jpg')
				cls_scores_ori_ij.save(out_name)
		
		ins_all, scs_sort = ins_pred_color(img, det_masks_int,
							det_bboxes[:,-1], det_labels, det_scs)
		 
		for i in range(len(ins_all)):
			img2 = Image.fromarray(ins_all[i])
			out_name = os.path.join(out_, "%03d"%(i) + '_' + 'scale'+str(scs_sort[i]) + '.jpg')
			img2.save(out_name)
//  [python]
def infer_vis(self, img_metas, cls_scores, det_masks, det_bboxes, det_labels, det_scs, out_path="vis_tmp"):

		# ==================vis====================
		# cls_scores_ori
		# ==================vis====================
		ori_h, ori_w, _ = img_metas[0]['ori_shape']
		imgname = img_metas[0]['filename']
		imgname2 = imgname.split('/')
		imgname2 = imgname2[-1][:-4]
		img = np.array(Image.open(imgname))
		det_masks_int = det_masks * 1
		out_ = os.path.join(out_path, imgname2)
		out_cls = os.path.join(out_path, imgname2+'_cls')

		if not os.path.exists(out_):
			os.makedirs(out_)
		if not os.path.exists(out_cls):
			os.makedirs(out_cls)
		
		# # vis cls
		cls_scores_ori = []
		 
		for i in range(5):
			cls_sc = cls_scores[i].clone().detach()
			cls_sc = F.interpolate(cls_sc, (ori_h, ori_w), mode='bilinear')
			cls_scores_ori.append(cls_sc)
		cls_all = cls_color(cls_scores_ori, self.use_sigmoid, img)
		 
		for i, cls_ in enumerate(cls_all):
			out_name = os.path.join(out_cls, "scale_%d"%(i) + '.jpg')
			clsim = Image.fromarray(np.uint8(cls_))
			clsim.save(out_name)
		 
		for i in range(5):
			if self.use_sigmoid:
				cls_scores_ori_i = cls_scores_ori[i].sigmoid().cpu()
			else:
				cls_scores_ori_i = cls_scores_ori[i].softmax(dim=1)[:,1:].cpu()

			for j in range(80):
				cls_scores_ori_ij = Image.fromarray(np.uint8(cls_scores_ori_i[0,j,:,:]*255))
				out_name = os.path.join(out_cls, "%02d"%(i) +'_' + "%02d"%(j)+'_'+CLASSES[j]+'.jpg')
				cls_scores_ori_ij.save(out_name)
		
		ins_all, scs_sort = ins_pred_color(img, det_masks_int,
							det_bboxes[:,-1], det_labels, det_scs)
		 
		for i in range(len(ins_all)):
			img2 = Image.fromarray(ins_all[i])
			out_name = os.path.join(out_, "%03d"%(i) + '_' + 'scale'+str(scs_sort[i]) + '.jpg')
			img2.save(out_name)
//  [python]
def loss_ml(self,
				cls_scores,
				mask_preds,
				gt_bboxes,
				gt_labels,
				gt_masks,
				category_targets,
				point_ins,
				img_metas,
				cfg,
				gt_bboxes_ignore=None):
		assert len(cls_scores) == len(mask_preds)
		_, _, b_h, b_w = mask_preds[0].shape
		_, _, b_h3, b_w3 = mask_preds[3].shape

		mask_preds[4] = F.upsample_bilinear(mask_preds[4], (b_h3, b_w3))

		bound = [self.grid_num[i] ** 2 for i in range(5)]
		bound = [0] + bound
		for i in range(1, 6):
			bound[i] += bound[i - 1]

		num_imgs = len(category_targets)
		for i in range(num_imgs):
			_, i_h, i_w = gt_masks[i].shape
			gt_masks[i] = nn.ConstantPad2d((0, b_w * self.strides[0] - i_w, 0, b_h * self.strides[0] - i_h), 0)(torch.tensor(gt_masks[i]))


		flatten_cls_scores = [cls_score.permute(0, 2, 3, 1).reshape(num_imgs, -1, self.cls_out_channels) for cls_score
							  in cls_scores]
		# need to check images first or dimensions first: image first and then row first
		flatten_cls_scores = torch.cat([cls_score for cls_score in flatten_cls_scores], dim=1).reshape(-1, self.cls_out_channels)

		# calculate loss
		loss_mask = torch.zeros(len(self.grid_num), num_imgs).to(mask_preds[0].device)

		t_num = 0
		for i in range(num_imgs):
			for j in range(len(self.grid_num)):
				mask_ = mask_preds[j][i]
				ind = torch.nonzero(category_targets[i][bound[j]:bound[j + 1]]).squeeze(-1)
				if len(ind) > 0:
					ins_ind = point_ins[i][bound[j]:bound[j + 1]][ind]
					_, b_h_i, b_w_i = mask_.shape

					ins_mask = gt_masks[i][ins_ind].float().to(mask_.device)		
					ins_mask = F.upsample_nearest(ins_mask.unsqueeze(0), (2*b_h_i, 2*b_w_i))[0]
					pred_mask = mask_[ind]
					pred_mask = F.sigmoid(F.upsample_bilinear(pred_mask.unsqueeze(0), (2*b_h_i, 2*b_w_i))[0])
					loss_mask[j,i] = self.dict_loss_batch(pred_mask, ins_mask, reduce='sum')		
					t_num += len(ind)


		loss_mask = self.dict_weight * torch.sum(loss_mask)/t_num
		category_targets = torch.cat(category_targets)
		num_pos = (category_targets > 0).sum()

		loss_cls = self.loss_cls(flatten_cls_scores, category_targets, avg_factor=num_pos + num_imgs)

		return dict(
			loss_cls=loss_cls,
			loss_mask=loss_mask)
//  [python]
def loss_ml(self,
				cls_scores,
				mask_preds,
				gt_bboxes,
				gt_labels,
				gt_masks,
				category_targets,
				point_ins,
				img_metas,
				cfg,
				gt_bboxes_ignore=None):
		assert len(cls_scores) == len(mask_preds)
		_, _, b_h, b_w = mask_preds[0].shape
		_, _, b_h3, b_w3 = mask_preds[3].shape

		mask_preds[4] = F.interpolate(mask_preds[4], (b_h3, b_w3), mode='bilinear')

		bound = [self.grid_num[i] ** 2 for i in range(5)]
		bound = [0] + bound
		for i in range(1, 6):
			bound[i] += bound[i - 1]

		num_imgs = len(category_targets)
		for i in range(num_imgs):
			_, i_h, i_w = gt_masks[i].shape
			gt_masks[i] = nn.ConstantPad2d((0, b_w * self.strides[0] - i_w, 0, b_h * self.strides[0] - i_h), 0)(torch.tensor(gt_masks[i]))


		flatten_cls_scores = [cls_score.permute(0, 2, 3, 1).reshape(num_imgs, -1, self.cls_out_channels) for cls_score
							  in cls_scores]
		# need to check images first or dimensions first: image first and then row first
		flatten_cls_scores = torch.cat([cls_score for cls_score in flatten_cls_scores], dim=1).reshape(-1, self.cls_out_channels)

		# calculate loss
		loss_mask = torch.zeros(len(self.grid_num), num_imgs).to(mask_preds[0].device)

		t_num = 0
		for i in range(num_imgs):
			for j in range(len(self.grid_num)):
				mask_ = mask_preds[j][i]
				ind = torch.nonzero(category_targets[i][bound[j]:bound[j + 1]]).squeeze(-1)
				if len(ind) > 0:
					ins_ind = point_ins[i][bound[j]:bound[j + 1]][ind]
					_, b_h_i, b_w_i = mask_.shape

					ins_mask = gt_masks[i][ins_ind].float().to(mask_.device)		
					ins_mask = F.interpolate(ins_mask.unsqueeze(0), (2*b_h_i, 2*b_w_i), mode='nearest')[0]
					pred_mask = mask_[ind]
					pred_mask = F.sigmoid(F.interpolate(pred_mask.unsqueeze(0), (2*b_h_i, 2*b_w_i), mode='bilinear')[0])
					loss_mask[j,i] = self.dict_loss_batch(pred_mask, ins_mask, reduce='sum')		
					t_num += len(ind)


		loss_mask = self.dict_weight * torch.sum(loss_mask)/t_num
		category_targets = torch.cat(category_targets)
		num_pos = (category_targets > 0).sum()

		loss_cls = self.loss_cls(flatten_cls_scores, category_targets, avg_factor=num_pos + num_imgs)

		return dict(
			loss_cls=loss_cls,
			loss_mask=loss_mask)
//  [python]
def forward(self, x1):
        self.dropout = torch.nn.Dropout(0.8)
        z1 = self.dropout(x1)
        t1 = torch.rand_like(z1)
        h1 = z1 * t1
        j1 = torch.nn.functional.upsample_nearest(h1, size=32)
        k1 = torch.nn.functional.softmax(j1, dim=-1)
        l1 = torch.nn.functional.batch_norm(h1, track_running_stats=True)
        m1 = torch.nn.functional.pad(k1, (1, 1, 1, 1, 1, 2))
        return torch.nn.functional.max_pool1d(m1, kernel_size=2, stride=1, padding=1)
//  [python]
def forward(self, x1):
        self.dropout = torch.nn.Dropout(0.8)
        z1 = self.dropout(x1)
        t1 = torch.rand_like(z1)
        h1 = z1 * t1
        j1 = torch.nn.functional.interpolate(h1, size=32, mode='nearest')
        k1 = torch.nn.functional.softmax(j1, dim=-1)
        l1 = torch.nn.functional.batch_norm(h1, track_running_stats=True)
        m1 = torch.nn.functional.pad(k1, (1, 1, 1, 1, 1, 2))
        return torch.nn.functional.max_pool1d(m1, kernel_size=2, stride=1, padding=1)
//  [python]
def validate(Pre_data, model, rate_model, args):
    print ('begin test')
    test_loader = torch.utils.data.DataLoader(
        dataset.listDataset(Pre_data, args.task_id,
                            shuffle=False,
                            transform=transforms.Compose([
                                transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                                            std=[0.229, 0.224, 0.225]),
                            ]), train=False),
        batch_size=args.batch_size)

    model.eval()

    mae = 0
    mse = 0
    original_mae = 0
    visi = []

    for i, (img,target,kpoint,fname,sigma_map) in enumerate(test_loader):

        img = img.cuda()
        target = target.cuda()

        d2, d3, d4, d5, d6, fs = model(img, target, refine_flag=True)

        density_map = d6.data.cpu().numpy()
        original_count = density_map.sum()
        original_density = d6
        [x, y, w, h] = findmaxcontours(density_map, fname, args)

        rate_feature = F.adaptive_avg_pool2d(fs[:, :, y:(y + h), x:(x + w)], (14, 14))
        rate = rate_model(rate_feature).clamp_(0.5, 9)
        rate = torch.sqrt(rate)

        if (float(w * h) / (img.size(2) * img.size(3))) > args.area_threshold:

            img_pros = img[:, :, y:(y + h), x:(x + w)]

            img_transed = F.upsample_bilinear(img_pros, scale_factor=rate.item())

            pt2d = target_transform(kpoint, rate)
            target_choose = gt_transform(pt2d, [x, y, w, h], rate.item())

            target_choose = torch.from_numpy(target_choose).type(torch.FloatTensor).unsqueeze(0)

            dd2, dd3, dd4, dd5, dd6 = model(img_transed, target_choose, refine_flag=False)

            # dd6[dd6<0]=0
            temp = dd6.data.cpu().numpy().sum()
            original_density[:, :, y:(y + h), x:(x + w)] = 0
            count = original_density.data.cpu().numpy().sum() + temp

        else:
            count = d6.data.cpu().numpy().sum()

        mae += abs(count - target.data.cpu().numpy().sum())
        mse += abs(count - target.data.cpu().numpy().sum()) * abs(count - target.data.cpu().numpy().sum())


        if i % args.print_freq == 0:
            print(fname[0], 'rate {rate:.3f}'.format(rate=rate.item()), 'gt', int(target.data.cpu().numpy().sum()),
                  "pred", int(count), "original:", int(original_count))

    mae = mae / len(test_loader)
    mse = math.sqrt(mse/len(test_loader))

    print(' \n* MAE {mae:.3f}\n'.format(mae=mae), '* MSE {mse:.3f}\n'.format(mse=mse))

    return mae, original_mae, visi
//  [python]
def validate(Pre_data, model, rate_model, args):
    print ('begin test')
    test_loader = torch.utils.data.DataLoader(
        dataset.listDataset(Pre_data, args.task_id,
                            shuffle=False,
                            transform=transforms.Compose([
                                transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                                                            std=[0.229, 0.224, 0.225]),
                            ]), train=False),
        batch_size=args.batch_size)

    model.eval()

    mae = 0
    mse = 0
    original_mae = 0
    visi = []

    for i, (img,target,kpoint,fname,sigma_map) in enumerate(test_loader):

        img = img.cuda()
        target = target.cuda()

        d2, d3, d4, d5, d6, fs = model(img, target, refine_flag=True)

        density_map = d6.data.cpu().numpy()
        original_count = density_map.sum()
        original_density = d6
        [x, y, w, h] = findmaxcontours(density_map, fname, args)

        rate_feature = F.adaptive_avg_pool2d(fs[:, :, y:(y + h), x:(x + w)], (14, 14))
        rate = rate_model(rate_feature).clamp_(0.5, 9)
        rate = torch.sqrt(rate)

        if (float(w * h) / (img.size(2) * img.size(3))) > args.area_threshold:

            img_pros = img[:, :, y:(y + h), x:(x + w)]

            img_transed = F.interpolate(img_pros, scale_factor=rate.item(), mode='bilinear')

            pt2d = target_transform(kpoint, rate)
            target_choose = gt_transform(pt2d, [x, y, w, h], rate.item())

            target_choose = torch.from_numpy(target_choose).type(torch.FloatTensor).unsqueeze(0)

            dd2, dd3, dd4, dd5, dd6 = model(img_transed, target_choose, refine_flag=False)

            # dd6[dd6<0]=0
            temp = dd6.data.cpu().numpy().sum()
            original_density[:, :, y:(y + h), x:(x + w)] = 0
            count = original_density.data.cpu().numpy().sum() + temp

        else:
            count = d6.data.cpu().numpy().sum()

        mae += abs(count - target.data.cpu().numpy().sum())
        mse += abs(count - target.data.cpu().numpy().sum()) * abs(count - target.data.cpu().numpy().sum())


        if i % args.print_freq == 0:
            print(fname[0], 'rate {rate:.3f}'.format(rate=rate.item()), 'gt', int(target.data.cpu().numpy().sum()),
                  "pred", int(count), "original:", int(original_count))

    mae = mae / len(test_loader)
    mse = math.sqrt(mse/len(test_loader))

    print(' \n* MAE {mae:.3f}\n'.format(mae=mae), '* MSE {mse:.3f}\n'.format(mse=mse))

    return mae, original_mae, visi
//  [python]
def forward(self,o):
        x=o
        # downfeats=[]
        for fe in self.downblocks:
            x=fe(x)
            # downfeats.append(x)
        vector=self.global_vector(x)
        feat1=F.upsample(x,(o.shape[2],o.shape[3])) 
        feat1=self.drop(feat1)
        feat1=self.conv2(feat1)   # 128
        
        # feat4 = self.upsample1(self.conv1(feat16))+downfeats[-4]  # 32
        # feat4=self.drop(feat4)
        # feat1 = self.conv2(self.upsample2(feat4))
        if False:
            x=x.cpu()
            torch.cuda.empty_cache()
        return vector.squeeze(2).squeeze(2),feat1
//  [python]
def forward(self,o):
        x=o
        # downfeats=[]
        for fe in self.downblocks:
            x=fe(x)
            # downfeats.append(x)
        vector=self.global_vector(x)
        feat1=F.interpolate(x,(o.shape[2],o.shape[3])) 
        feat1=self.drop(feat1)
        feat1=self.conv2(feat1)   # 128
        
        # feat4 = self.upsample1(self.conv1(feat16))+downfeats[-4]  # 32
        # feat4=self.drop(feat4)
        # feat1 = self.conv2(self.upsample2(feat4))
        if False:
            x=x.cpu()
            torch.cuda.empty_cache()
        return vector.squeeze(2).squeeze(2),feat1
//  [python]
def get_bboxes_logi(self,cls_scores, mask_preds, img_metas, cfg, rescale=None, score_thr=True):
		_, _, b_h, b_w = mask_preds[0].shape
		crop_h, crop_w, _ = img_metas[0]['img_shape']
		ori_h, ori_w, _ = img_metas[0]['ori_shape']
		cls_scores_ori = []
		for i in range(5):
			cls_sc = cls_scores[i].clone().detach()
			cls_sc = F.upsample_bilinear(cls_sc, (ori_h, ori_w))
			cls_scores_ori.append(cls_sc)
		# cat dim: default 0
		if self.use_sigmoid:
			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()
		else:
			flatten_cls_scores = torch.cat([cls_score.permute(0, 2, 3, 1).reshape(-1, self.cls_out_channels) for cls_score in cls_scores]).softmax(dim=1)
			flatten_cls_scores = flatten_cls_scores[:,1:]  
		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]
		# logit and class (per pixel)
		scores, labels = torch.max(flatten_cls_scores,dim=-1)
		nms_pre = cfg.get('nms_pre', -1)
		if score_thr:
			score_thr = cfg.get('score_thr', -1)
		else:
			score_thr = 0
		mask_thr = cfg.get('mask_thr_binary',-1)
		det_masks = np.array([])
		det_bboxes = np.zeros((0,5))
		det_labels = np.zeros(0).astype(int)
		# store feature scale
		sc_ind = []
		for i in range(5):
			sc_ind.append(torch.zeros((self.grid_num[i] ** 2, 1)) + i)
		sc_ind = torch.cat(sc_ind, axis=0)
		if nms_pre > 0 and len(scores) >=1 and scores.max() > score_thr:
			# HERE: need to change scores for softmax
			valid_inds = torch.nonzero(scores >= score_thr).squeeze()
			if valid_inds.dim()!=0:
				scores = scores[valid_inds]
				labels = labels[valid_inds]
				mask_preds = mask_preds[valid_inds]
				sc_ind = sc_ind[valid_inds]

				if scores.shape[0] < nms_pre:
					nms_pre = scores.shape[0]

				_, topk_inds = scores.topk(nms_pre)
				scores = scores[topk_inds]
				labels = labels[topk_inds]
				mask_preds = mask_preds[topk_inds]
				sc_ind = sc_ind[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, sc_ind, cfg.nms.iou_thr)
				n = len(masks)
				det_masks = []
				det_bboxes = np.zeros((n, 5))
				det_labels = np.zeros(n).astype(int)
				det_scs = np.zeros(n).astype(int)

				for i in range(n):
					det_bboxes[i, -1] = masks[i][-2]
					det_labels[i] = masks[i][-3]
					det_masks.append(masks[i][0])
					det_scs[i] = masks[i][-1]
				det_masks = np.array(det_masks)

				if self.out_path is not None:
					self.infer_vis(img_metas, cls_scores, det_masks, det_bboxes, det_labels, det_scs, out_path=self.out_path)

		return det_bboxes, det_labels, det_masks