# Copyright (c) OpenMMLab. All rights reserved.
from unittest import TestCase

import torch
from mmengine import Config
from mmengine.structures import InstanceData

from mmdet import *  # noqa
from mmdet.models.dense_heads import YOLOFHead


class TestYOLOFHead(TestCase):

    def test_yolof_head_loss(self):
        """Tests yolof head loss when truth is empty and non-empty."""
        s = 256
        img_metas = [{
            'img_shape': (s, s, 3),
            'scale_factor': 1,
            'pad_shape': (s, s, 3)
        }]
        train_cfg = Config(
            dict(
                assigner=dict(
                    type='UniformAssigner',
                    pos_ignore_thr=0.15,
                    neg_ignore_thr=0.7),
                allowed_border=-1,
                pos_weight=-1,
                debug=False))
        yolof_head = YOLOFHead(
            num_classes=4,
            in_channels=1,
            feat_channels=1,
            reg_decoded_bbox=True,
            train_cfg=train_cfg,
            anchor_generator=dict(
                type='AnchorGenerator',
                ratios=[1.0],
                scales=[1, 2, 4, 8, 16],
                strides=[32]),
            bbox_coder=dict(
                type='DeltaXYWHBBoxCoder',
                target_means=[.0, .0, .0, .0],
                target_stds=[1., 1., 1., 1.],
                add_ctr_clamp=True,
                ctr_clamp=32),
            loss_cls=dict(
                type='FocalLoss',
                use_sigmoid=True,
                gamma=2.0,
                alpha=0.25,
                loss_weight=1.0),
            loss_bbox=dict(type='GIoULoss', loss_weight=1.0))
        feat = [torch.rand(1, 1, s // 32, s // 32)]
        cls_scores, bbox_preds = yolof_head.forward(feat)

        # Test that empty ground truth encourages the network to predict
        # background
        gt_instances = InstanceData()
        gt_instances.bboxes = torch.empty((0, 4))
        gt_instances.labels = torch.LongTensor([])
        empty_gt_losses = yolof_head.loss_by_feat(cls_scores, bbox_preds,
                                                  [gt_instances], img_metas)
        # When there is no truth, the cls loss should be nonzero but there
        # should be no box loss.
        empty_cls_loss = empty_gt_losses['loss_cls']
        empty_box_loss = empty_gt_losses['loss_bbox']
        self.assertGreater(empty_cls_loss.item(), 0,
                           'cls loss should be non-zero')
        self.assertEqual(
            empty_box_loss.item(), 0,
            'there should be no box loss when there are no true boxes')

        # When truth is non-empty then both cls and box loss should be nonzero
        # for random inputs
        gt_instances = InstanceData()
        gt_instances.bboxes = torch.Tensor(
            [[23.6667, 23.8757, 238.6326, 151.8874]])
        gt_instances.labels = torch.LongTensor([2])
        one_gt_losses = yolof_head.loss_by_feat(cls_scores, bbox_preds,
                                                [gt_instances], img_metas)
        onegt_cls_loss = one_gt_losses['loss_cls']
        onegt_box_loss = one_gt_losses['loss_bbox']
        self.assertGreater(onegt_cls_loss.item(), 0,
                           'cls loss should be non-zero')
        self.assertGreater(onegt_box_loss.item(), 0,
                           'box loss should be non-zero')

# Copyright (c) OpenMMLab. All rights reserved.
from unittest import TestCase

import torch
from mmengine import Config
from mmengine.data import InstanceData

from mmdet import *  # noqa
from mmdet.models.dense_heads import YOLOFHead


class TestYOLOFHead(TestCase):

    def test_yolof_head_loss(self):
        """Tests yolof head loss when truth is empty and non-empty."""
        s = 256
        img_metas = [{
            'img_shape': (s, s, 3),
            'scale_factor': 1,
            'pad_shape': (s, s, 3)
        }]
        train_cfg = Config(
            dict(
                assigner=dict(
                    type='UniformAssigner',
                    pos_ignore_thr=0.15,
                    neg_ignore_thr=0.7),
                allowed_border=-1,
                pos_weight=-1,
                debug=False))
        yolof_head = YOLOFHead(
            num_classes=4,
            in_channels=1,
            feat_channels=1,
            reg_decoded_bbox=True,
            train_cfg=train_cfg,
            anchor_generator=dict(
                type='AnchorGenerator',
                ratios=[1.0],
                scales=[1, 2, 4, 8, 16],
                strides=[32]),
            bbox_coder=dict(
                type='DeltaXYWHBBoxCoder',
                target_means=[.0, .0, .0, .0],
                target_stds=[1., 1., 1., 1.],
                add_ctr_clamp=True,
                ctr_clamp=32),
            loss_cls=dict(
                type='FocalLoss',
                use_sigmoid=True,
                gamma=2.0,
                alpha=0.25,
                loss_weight=1.0),
            loss_bbox=dict(type='GIoULoss', loss_weight=1.0))
        feat = [torch.rand(1, 1, s // 32, s // 32)]
        cls_scores, bbox_preds = yolof_head.forward(feat)

        # Test that empty ground truth encourages the network to predict
        # background
        gt_instances = InstanceData()
        gt_instances.bboxes = torch.empty((0, 4))
        gt_instances.labels = torch.LongTensor([])
        empty_gt_losses = yolof_head.loss_by_feat(cls_scores, bbox_preds,
                                                  [gt_instances], img_metas)
        # When there is no truth, the cls loss should be nonzero but there
        # should be no box loss.
        empty_cls_loss = empty_gt_losses['loss_cls']
        empty_box_loss = empty_gt_losses['loss_bbox']
        self.assertGreater(empty_cls_loss.item(), 0,
                           'cls loss should be non-zero')
        self.assertEqual(
            empty_box_loss.item(), 0,
            'there should be no box loss when there are no true boxes')

        # When truth is non-empty then both cls and box loss should be nonzero
        # for random inputs
        gt_instances = InstanceData()
        gt_instances.bboxes = torch.Tensor(
            [[23.6667, 23.8757, 238.6326, 151.8874]])
        gt_instances.labels = torch.LongTensor([2])
        one_gt_losses = yolof_head.loss_by_feat(cls_scores, bbox_preds,
                                                [gt_instances], img_metas)
        onegt_cls_loss = one_gt_losses['loss_cls']
        onegt_box_loss = one_gt_losses['loss_bbox']
        self.assertGreater(onegt_cls_loss.item(), 0,
                           'cls loss should be non-zero')
        self.assertGreater(onegt_box_loss.item(), 0,
                           'box loss should be non-zero')
