We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
CUDA error: device-side assert triggered
The error is related to learning_rate, I want to use 4 gpus and each gpu has 24 images to train ssd, so according to your markdown, I should set lr = 3e-3, but CUDA error occurs, if I decreasing the lr, the problem is fixed, so what's the problem?

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mmdet/models/anchor_heads/ssd_head.py`
Content:
```
1 import numpy as np
2 import torch
3 import torch.nn as nn
4 import torch.nn.functional as F
5 from mmcv.cnn import xavier_init
6 
7 from mmdet.core import AnchorGenerator, anchor_target, multi_apply
8 from ..losses import smooth_l1_loss
9 from ..registry import HEADS
10 from .anchor_head import AnchorHead
11 
12 
13 # TODO: add loss evaluator for SSD
14 @HEADS.register_module
15 class SSDHead(AnchorHead):
16 
17     def __init__(self,
18                  input_size=300,
19                  num_classes=81,
20                  in_channels=(512, 1024, 512, 256, 256, 256),
21                  anchor_strides=(8, 16, 32, 64, 100, 300),
22                  basesize_ratio_range=(0.1, 0.9),
23                  anchor_ratios=([2], [2, 3], [2, 3], [2, 3], [2], [2]),
24                  target_means=(.0, .0, .0, .0),
25                  target_stds=(1.0, 1.0, 1.0, 1.0)):
26         super(AnchorHead, self).__init__()
27         self.input_size = input_size
28         self.num_classes = num_classes
29         self.in_channels = in_channels
30         self.cls_out_channels = num_classes
31         num_anchors = [len(ratios) * 2 + 2 for ratios in anchor_ratios]
32         reg_convs = []
33         cls_convs = []
34         for i in range(len(in_channels)):
35             reg_convs.append(
36                 nn.Conv2d(
37                     in_channels[i],
38                     num_anchors[i] * 4,
39                     kernel_size=3,
40                     padding=1))
41             cls_convs.append(
42                 nn.Conv2d(
43                     in_channels[i],
44                     num_anchors[i] * num_classes,
45                     kernel_size=3,
46                     padding=1))
47         self.reg_convs = nn.ModuleList(reg_convs)
48         self.cls_convs = nn.ModuleList(cls_convs)
49 
50         min_ratio, max_ratio = basesize_ratio_range
51         min_ratio = int(min_ratio * 100)
52         max_ratio = int(max_ratio * 100)
53         step = int(np.floor(max_ratio - min_ratio) / (len(in_channels) - 2))
54         min_sizes = []
55         max_sizes = []
56         for r in range(int(min_ratio), int(max_ratio) + 1, step):
57             min_sizes.append(int(input_size * r / 100))
58             max_sizes.append(int(input_size * (r + step) / 100))
59         if input_size == 300:
60             if basesize_ratio_range[0] == 0.15:  # SSD300 COCO
61                 min_sizes.insert(0, int(input_size * 7 / 100))
62                 max_sizes.insert(0, int(input_size * 15 / 100))
63             elif basesize_ratio_range[0] == 0.2:  # SSD300 VOC
64                 min_sizes.insert(0, int(input_size * 10 / 100))
65                 max_sizes.insert(0, int(input_size * 20 / 100))
66         elif input_size == 512:
67             if basesize_ratio_range[0] == 0.1:  # SSD512 COCO
68                 min_sizes.insert(0, int(input_size * 4 / 100))
69                 max_sizes.insert(0, int(input_size * 10 / 100))
70             elif basesize_ratio_range[0] == 0.15:  # SSD512 VOC
71                 min_sizes.insert(0, int(input_size * 7 / 100))
72                 max_sizes.insert(0, int(input_size * 15 / 100))
73         self.anchor_generators = []
74         self.anchor_strides = anchor_strides
75         for k in range(len(anchor_strides)):
76             base_size = min_sizes[k]
77             stride = anchor_strides[k]
78             ctr = ((stride - 1) / 2., (stride - 1) / 2.)
79             scales = [1., np.sqrt(max_sizes[k] / min_sizes[k])]
80             ratios = [1.]
81             for r in anchor_ratios[k]:
82                 ratios += [1 / r, r]  # 4 or 6 ratio
83             anchor_generator = AnchorGenerator(
84                 base_size, scales, ratios, scale_major=False, ctr=ctr)
85             indices = list(range(len(ratios)))
86             indices.insert(1, len(indices))
87             anchor_generator.base_anchors = torch.index_select(
88                 anchor_generator.base_anchors, 0, torch.LongTensor(indices))
89             self.anchor_generators.append(anchor_generator)
90 
91         self.target_means = target_means
92         self.target_stds = target_stds
93         self.use_sigmoid_cls = False
94         self.cls_focal_loss = False
95         self.fp16_enabled = False
96 
97     def init_weights(self):
98         for m in self.modules():
99             if isinstance(m, nn.Conv2d):
100                 xavier_init(m, distribution='uniform', bias=0)
101 
102     def forward(self, feats):
103         cls_scores = []
104         bbox_preds = []
105         for feat, reg_conv, cls_conv in zip(feats, self.reg_convs,
106                                             self.cls_convs):
107             cls_scores.append(cls_conv(feat))
108             bbox_preds.append(reg_conv(feat))
109         return cls_scores, bbox_preds
110 
111     def loss_single(self, cls_score, bbox_pred, labels, label_weights,
112                     bbox_targets, bbox_weights, num_total_samples, cfg):
113         loss_cls_all = F.cross_entropy(
114             cls_score, labels, reduction='none') * label_weights
115         pos_inds = (labels > 0).nonzero().view(-1)
116         neg_inds = (labels == 0).nonzero().view(-1)
117 
118         num_pos_samples = pos_inds.size(0)
119         num_neg_samples = cfg.neg_pos_ratio * num_pos_samples
120         if num_neg_samples > neg_inds.size(0):
121             num_neg_samples = neg_inds.size(0)
122         topk_loss_cls_neg, _ = loss_cls_all[neg_inds].topk(num_neg_samples)
123         loss_cls_pos = loss_cls_all[pos_inds].sum()
124         loss_cls_neg = topk_loss_cls_neg.sum()
125         loss_cls = (loss_cls_pos + loss_cls_neg) / num_total_samples
126 
127         loss_bbox = smooth_l1_loss(
128             bbox_pred,
129             bbox_targets,
130             bbox_weights,
131             beta=cfg.smoothl1_beta,
132             avg_factor=num_total_samples)
133         return loss_cls[None], loss_bbox
134 
135     def loss(self,
136              cls_scores,
137              bbox_preds,
138              gt_bboxes,
139              gt_labels,
140              img_metas,
141              cfg,
142              gt_bboxes_ignore=None):
143         featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]
144         assert len(featmap_sizes) == len(self.anchor_generators)
145 
146         device = cls_scores[0].device
147 
148         anchor_list, valid_flag_list = self.get_anchors(
149             featmap_sizes, img_metas, device=device)
150         cls_reg_targets = anchor_target(
151             anchor_list,
152             valid_flag_list,
153             gt_bboxes,
154             img_metas,
155             self.target_means,
156             self.target_stds,
157             cfg,
158             gt_bboxes_ignore_list=gt_bboxes_ignore,
159             gt_labels_list=gt_labels,
160             label_channels=1,
161             sampling=False,
162             unmap_outputs=False)
163         if cls_reg_targets is None:
164             return None
165         (labels_list, label_weights_list, bbox_targets_list, bbox_weights_list,
166          num_total_pos, num_total_neg) = cls_reg_targets
167 
168         num_images = len(img_metas)
169         all_cls_scores = torch.cat([
170             s.permute(0, 2, 3, 1).reshape(
171                 num_images, -1, self.cls_out_channels) for s in cls_scores
172         ], 1)
173         all_labels = torch.cat(labels_list, -1).view(num_images, -1)
174         all_label_weights = torch.cat(label_weights_list,
175                                       -1).view(num_images, -1)
176         all_bbox_preds = torch.cat([
177             b.permute(0, 2, 3, 1).reshape(num_images, -1, 4)
178             for b in bbox_preds
179         ], -2)
180         all_bbox_targets = torch.cat(bbox_targets_list,
181                                      -2).view(num_images, -1, 4)
182         all_bbox_weights = torch.cat(bbox_weights_list,
183                                      -2).view(num_images, -1, 4)
184 
185         losses_cls, losses_bbox = multi_apply(
186             self.loss_single,
187             all_cls_scores,
188             all_bbox_preds,
189             all_labels,
190             all_label_weights,
191             all_bbox_targets,
192             all_bbox_weights,
193             num_total_samples=num_total_pos,
194             cfg=cfg)
195         return dict(loss_cls=losses_cls, loss_bbox=losses_bbox)
196
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mmdet/models/anchor_heads/ssd_head.py b/mmdet/models/anchor_heads/ssd_head.py
--- a/mmdet/models/anchor_heads/ssd_head.py
+++ b/mmdet/models/anchor_heads/ssd_head.py
@@ -182,6 +182,12 @@
         all_bbox_weights = torch.cat(bbox_weights_list,
                                      -2).view(num_images, -1, 4)
 
+        # check NaN and Inf
+        assert torch.isfinite(all_cls_scores).all().item(), \
+            'classification scores become infinite or NaN!'
+        assert torch.isfinite(all_bbox_preds).all().item(), \
+            'bbox predications become infinite or NaN!'
+
         losses_cls, losses_bbox = multi_apply(
             self.loss_single,
             all_cls_scores,

{"golden_diff": "diff --git a/mmdet/models/anchor_heads/ssd_head.py b/mmdet/models/anchor_heads/ssd_head.py\n--- a/mmdet/models/anchor_heads/ssd_head.py\n+++ b/mmdet/models/anchor_heads/ssd_head.py\n@@ -182,6 +182,12 @@\n         all_bbox_weights = torch.cat(bbox_weights_list,\n                                      -2).view(num_images, -1, 4)\n \n+        # check NaN and Inf\n+        assert torch.isfinite(all_cls_scores).all().item(), \\\n+            'classification scores become infinite or NaN!'\n+        assert torch.isfinite(all_bbox_preds).all().item(), \\\n+            'bbox predications become infinite or NaN!'\n+\n         losses_cls, losses_bbox = multi_apply(\n             self.loss_single,\n             all_cls_scores,\n", "issue": "CUDA error: device-side assert triggered\nThe error is related to learning_rate, I want to use 4 gpus and each gpu has 24 images to train ssd, so according to your markdown, I should set lr = 3e-3, but CUDA error occurs, if I decreasing the lr, the problem is fixed, so what's the problem?\n", "before_files": [{"content": "import numpy as np\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom mmcv.cnn import xavier_init\n\nfrom mmdet.core import AnchorGenerator, anchor_target, multi_apply\nfrom ..losses import smooth_l1_loss\nfrom ..registry import HEADS\nfrom .anchor_head import AnchorHead\n\n\n# TODO: add loss evaluator for SSD\n@HEADS.register_module\nclass SSDHead(AnchorHead):\n\n    def __init__(self,\n                 input_size=300,\n                 num_classes=81,\n                 in_channels=(512, 1024, 512, 256, 256, 256),\n                 anchor_strides=(8, 16, 32, 64, 100, 300),\n                 basesize_ratio_range=(0.1, 0.9),\n                 anchor_ratios=([2], [2, 3], [2, 3], [2, 3], [2], [2]),\n                 target_means=(.0, .0, .0, .0),\n                 target_stds=(1.0, 1.0, 1.0, 1.0)):\n        super(AnchorHead, self).__init__()\n        self.input_size = input_size\n        self.num_classes = num_classes\n        self.in_channels = in_channels\n        self.cls_out_channels = num_classes\n        num_anchors = [len(ratios) * 2 + 2 for ratios in anchor_ratios]\n        reg_convs = []\n        cls_convs = []\n        for i in range(len(in_channels)):\n            reg_convs.append(\n                nn.Conv2d(\n                    in_channels[i],\n                    num_anchors[i] * 4,\n                    kernel_size=3,\n                    padding=1))\n            cls_convs.append(\n                nn.Conv2d(\n                    in_channels[i],\n                    num_anchors[i] * num_classes,\n                    kernel_size=3,\n                    padding=1))\n        self.reg_convs = nn.ModuleList(reg_convs)\n        self.cls_convs = nn.ModuleList(cls_convs)\n\n        min_ratio, max_ratio = basesize_ratio_range\n        min_ratio = int(min_ratio * 100)\n        max_ratio = int(max_ratio * 100)\n        step = int(np.floor(max_ratio - min_ratio) / (len(in_channels) - 2))\n        min_sizes = []\n        max_sizes = []\n        for r in range(int(min_ratio), int(max_ratio) + 1, step):\n            min_sizes.append(int(input_size * r / 100))\n            max_sizes.append(int(input_size * (r + step) / 100))\n        if input_size == 300:\n            if basesize_ratio_range[0] == 0.15:  # SSD300 COCO\n                min_sizes.insert(0, int(input_size * 7 / 100))\n                max_sizes.insert(0, int(input_size * 15 / 100))\n            elif basesize_ratio_range[0] == 0.2:  # SSD300 VOC\n                min_sizes.insert(0, int(input_size * 10 / 100))\n                max_sizes.insert(0, int(input_size * 20 / 100))\n        elif input_size == 512:\n            if basesize_ratio_range[0] == 0.1:  # SSD512 COCO\n                min_sizes.insert(0, int(input_size * 4 / 100))\n                max_sizes.insert(0, int(input_size * 10 / 100))\n            elif basesize_ratio_range[0] == 0.15:  # SSD512 VOC\n                min_sizes.insert(0, int(input_size * 7 / 100))\n                max_sizes.insert(0, int(input_size * 15 / 100))\n        self.anchor_generators = []\n        self.anchor_strides = anchor_strides\n        for k in range(len(anchor_strides)):\n            base_size = min_sizes[k]\n            stride = anchor_strides[k]\n            ctr = ((stride - 1) / 2., (stride - 1) / 2.)\n            scales = [1., np.sqrt(max_sizes[k] / min_sizes[k])]\n            ratios = [1.]\n            for r in anchor_ratios[k]:\n                ratios += [1 / r, r]  # 4 or 6 ratio\n            anchor_generator = AnchorGenerator(\n                base_size, scales, ratios, scale_major=False, ctr=ctr)\n            indices = list(range(len(ratios)))\n            indices.insert(1, len(indices))\n            anchor_generator.base_anchors = torch.index_select(\n                anchor_generator.base_anchors, 0, torch.LongTensor(indices))\n            self.anchor_generators.append(anchor_generator)\n\n        self.target_means = target_means\n        self.target_stds = target_stds\n        self.use_sigmoid_cls = False\n        self.cls_focal_loss = False\n        self.fp16_enabled = False\n\n    def init_weights(self):\n        for m in self.modules():\n            if isinstance(m, nn.Conv2d):\n                xavier_init(m, distribution='uniform', bias=0)\n\n    def forward(self, feats):\n        cls_scores = []\n        bbox_preds = []\n        for feat, reg_conv, cls_conv in zip(feats, self.reg_convs,\n                                            self.cls_convs):\n            cls_scores.append(cls_conv(feat))\n            bbox_preds.append(reg_conv(feat))\n        return cls_scores, bbox_preds\n\n    def loss_single(self, cls_score, bbox_pred, labels, label_weights,\n                    bbox_targets, bbox_weights, num_total_samples, cfg):\n        loss_cls_all = F.cross_entropy(\n            cls_score, labels, reduction='none') * label_weights\n        pos_inds = (labels > 0).nonzero().view(-1)\n        neg_inds = (labels == 0).nonzero().view(-1)\n\n        num_pos_samples = pos_inds.size(0)\n        num_neg_samples = cfg.neg_pos_ratio * num_pos_samples\n        if num_neg_samples > neg_inds.size(0):\n            num_neg_samples = neg_inds.size(0)\n        topk_loss_cls_neg, _ = loss_cls_all[neg_inds].topk(num_neg_samples)\n        loss_cls_pos = loss_cls_all[pos_inds].sum()\n        loss_cls_neg = topk_loss_cls_neg.sum()\n        loss_cls = (loss_cls_pos + loss_cls_neg) / num_total_samples\n\n        loss_bbox = smooth_l1_loss(\n            bbox_pred,\n            bbox_targets,\n            bbox_weights,\n            beta=cfg.smoothl1_beta,\n            avg_factor=num_total_samples)\n        return loss_cls[None], loss_bbox\n\n    def loss(self,\n             cls_scores,\n             bbox_preds,\n             gt_bboxes,\n             gt_labels,\n             img_metas,\n             cfg,\n             gt_bboxes_ignore=None):\n        featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]\n        assert len(featmap_sizes) == len(self.anchor_generators)\n\n        device = cls_scores[0].device\n\n        anchor_list, valid_flag_list = self.get_anchors(\n            featmap_sizes, img_metas, device=device)\n        cls_reg_targets = anchor_target(\n            anchor_list,\n            valid_flag_list,\n            gt_bboxes,\n            img_metas,\n            self.target_means,\n            self.target_stds,\n            cfg,\n            gt_bboxes_ignore_list=gt_bboxes_ignore,\n            gt_labels_list=gt_labels,\n            label_channels=1,\n            sampling=False,\n            unmap_outputs=False)\n        if cls_reg_targets is None:\n            return None\n        (labels_list, label_weights_list, bbox_targets_list, bbox_weights_list,\n         num_total_pos, num_total_neg) = cls_reg_targets\n\n        num_images = len(img_metas)\n        all_cls_scores = torch.cat([\n            s.permute(0, 2, 3, 1).reshape(\n                num_images, -1, self.cls_out_channels) for s in cls_scores\n        ], 1)\n        all_labels = torch.cat(labels_list, -1).view(num_images, -1)\n        all_label_weights = torch.cat(label_weights_list,\n                                      -1).view(num_images, -1)\n        all_bbox_preds = torch.cat([\n            b.permute(0, 2, 3, 1).reshape(num_images, -1, 4)\n            for b in bbox_preds\n        ], -2)\n        all_bbox_targets = torch.cat(bbox_targets_list,\n                                     -2).view(num_images, -1, 4)\n        all_bbox_weights = torch.cat(bbox_weights_list,\n                                     -2).view(num_images, -1, 4)\n\n        losses_cls, losses_bbox = multi_apply(\n            self.loss_single,\n            all_cls_scores,\n            all_bbox_preds,\n            all_labels,\n            all_label_weights,\n            all_bbox_targets,\n            all_bbox_weights,\n            num_total_samples=num_total_pos,\n            cfg=cfg)\n        return dict(loss_cls=losses_cls, loss_bbox=losses_bbox)\n", "path": "mmdet/models/anchor_heads/ssd_head.py"}], "after_files": [{"content": "import numpy as np\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom mmcv.cnn import xavier_init\n\nfrom mmdet.core import AnchorGenerator, anchor_target, multi_apply\nfrom ..losses import smooth_l1_loss\nfrom ..registry import HEADS\nfrom .anchor_head import AnchorHead\n\n\n# TODO: add loss evaluator for SSD\n@HEADS.register_module\nclass SSDHead(AnchorHead):\n\n    def __init__(self,\n                 input_size=300,\n                 num_classes=81,\n                 in_channels=(512, 1024, 512, 256, 256, 256),\n                 anchor_strides=(8, 16, 32, 64, 100, 300),\n                 basesize_ratio_range=(0.1, 0.9),\n                 anchor_ratios=([2], [2, 3], [2, 3], [2, 3], [2], [2]),\n                 target_means=(.0, .0, .0, .0),\n                 target_stds=(1.0, 1.0, 1.0, 1.0)):\n        super(AnchorHead, self).__init__()\n        self.input_size = input_size\n        self.num_classes = num_classes\n        self.in_channels = in_channels\n        self.cls_out_channels = num_classes\n        num_anchors = [len(ratios) * 2 + 2 for ratios in anchor_ratios]\n        reg_convs = []\n        cls_convs = []\n        for i in range(len(in_channels)):\n            reg_convs.append(\n                nn.Conv2d(\n                    in_channels[i],\n                    num_anchors[i] * 4,\n                    kernel_size=3,\n                    padding=1))\n            cls_convs.append(\n                nn.Conv2d(\n                    in_channels[i],\n                    num_anchors[i] * num_classes,\n                    kernel_size=3,\n                    padding=1))\n        self.reg_convs = nn.ModuleList(reg_convs)\n        self.cls_convs = nn.ModuleList(cls_convs)\n\n        min_ratio, max_ratio = basesize_ratio_range\n        min_ratio = int(min_ratio * 100)\n        max_ratio = int(max_ratio * 100)\n        step = int(np.floor(max_ratio - min_ratio) / (len(in_channels) - 2))\n        min_sizes = []\n        max_sizes = []\n        for r in range(int(min_ratio), int(max_ratio) + 1, step):\n            min_sizes.append(int(input_size * r / 100))\n            max_sizes.append(int(input_size * (r + step) / 100))\n        if input_size == 300:\n            if basesize_ratio_range[0] == 0.15:  # SSD300 COCO\n                min_sizes.insert(0, int(input_size * 7 / 100))\n                max_sizes.insert(0, int(input_size * 15 / 100))\n            elif basesize_ratio_range[0] == 0.2:  # SSD300 VOC\n                min_sizes.insert(0, int(input_size * 10 / 100))\n                max_sizes.insert(0, int(input_size * 20 / 100))\n        elif input_size == 512:\n            if basesize_ratio_range[0] == 0.1:  # SSD512 COCO\n                min_sizes.insert(0, int(input_size * 4 / 100))\n                max_sizes.insert(0, int(input_size * 10 / 100))\n            elif basesize_ratio_range[0] == 0.15:  # SSD512 VOC\n                min_sizes.insert(0, int(input_size * 7 / 100))\n                max_sizes.insert(0, int(input_size * 15 / 100))\n        self.anchor_generators = []\n        self.anchor_strides = anchor_strides\n        for k in range(len(anchor_strides)):\n            base_size = min_sizes[k]\n            stride = anchor_strides[k]\n            ctr = ((stride - 1) / 2., (stride - 1) / 2.)\n            scales = [1., np.sqrt(max_sizes[k] / min_sizes[k])]\n            ratios = [1.]\n            for r in anchor_ratios[k]:\n                ratios += [1 / r, r]  # 4 or 6 ratio\n            anchor_generator = AnchorGenerator(\n                base_size, scales, ratios, scale_major=False, ctr=ctr)\n            indices = list(range(len(ratios)))\n            indices.insert(1, len(indices))\n            anchor_generator.base_anchors = torch.index_select(\n                anchor_generator.base_anchors, 0, torch.LongTensor(indices))\n            self.anchor_generators.append(anchor_generator)\n\n        self.target_means = target_means\n        self.target_stds = target_stds\n        self.use_sigmoid_cls = False\n        self.cls_focal_loss = False\n        self.fp16_enabled = False\n\n    def init_weights(self):\n        for m in self.modules():\n            if isinstance(m, nn.Conv2d):\n                xavier_init(m, distribution='uniform', bias=0)\n\n    def forward(self, feats):\n        cls_scores = []\n        bbox_preds = []\n        for feat, reg_conv, cls_conv in zip(feats, self.reg_convs,\n                                            self.cls_convs):\n            cls_scores.append(cls_conv(feat))\n            bbox_preds.append(reg_conv(feat))\n        return cls_scores, bbox_preds\n\n    def loss_single(self, cls_score, bbox_pred, labels, label_weights,\n                    bbox_targets, bbox_weights, num_total_samples, cfg):\n        loss_cls_all = F.cross_entropy(\n            cls_score, labels, reduction='none') * label_weights\n        pos_inds = (labels > 0).nonzero().view(-1)\n        neg_inds = (labels == 0).nonzero().view(-1)\n\n        num_pos_samples = pos_inds.size(0)\n        num_neg_samples = cfg.neg_pos_ratio * num_pos_samples\n        if num_neg_samples > neg_inds.size(0):\n            num_neg_samples = neg_inds.size(0)\n        topk_loss_cls_neg, _ = loss_cls_all[neg_inds].topk(num_neg_samples)\n        loss_cls_pos = loss_cls_all[pos_inds].sum()\n        loss_cls_neg = topk_loss_cls_neg.sum()\n        loss_cls = (loss_cls_pos + loss_cls_neg) / num_total_samples\n\n        loss_bbox = smooth_l1_loss(\n            bbox_pred,\n            bbox_targets,\n            bbox_weights,\n            beta=cfg.smoothl1_beta,\n            avg_factor=num_total_samples)\n        return loss_cls[None], loss_bbox\n\n    def loss(self,\n             cls_scores,\n             bbox_preds,\n             gt_bboxes,\n             gt_labels,\n             img_metas,\n             cfg,\n             gt_bboxes_ignore=None):\n        featmap_sizes = [featmap.size()[-2:] for featmap in cls_scores]\n        assert len(featmap_sizes) == len(self.anchor_generators)\n\n        device = cls_scores[0].device\n\n        anchor_list, valid_flag_list = self.get_anchors(\n            featmap_sizes, img_metas, device=device)\n        cls_reg_targets = anchor_target(\n            anchor_list,\n            valid_flag_list,\n            gt_bboxes,\n            img_metas,\n            self.target_means,\n            self.target_stds,\n            cfg,\n            gt_bboxes_ignore_list=gt_bboxes_ignore,\n            gt_labels_list=gt_labels,\n            label_channels=1,\n            sampling=False,\n            unmap_outputs=False)\n        if cls_reg_targets is None:\n            return None\n        (labels_list, label_weights_list, bbox_targets_list, bbox_weights_list,\n         num_total_pos, num_total_neg) = cls_reg_targets\n\n        num_images = len(img_metas)\n        all_cls_scores = torch.cat([\n            s.permute(0, 2, 3, 1).reshape(\n                num_images, -1, self.cls_out_channels) for s in cls_scores\n        ], 1)\n        all_labels = torch.cat(labels_list, -1).view(num_images, -1)\n        all_label_weights = torch.cat(label_weights_list,\n                                      -1).view(num_images, -1)\n        all_bbox_preds = torch.cat([\n            b.permute(0, 2, 3, 1).reshape(num_images, -1, 4)\n            for b in bbox_preds\n        ], -2)\n        all_bbox_targets = torch.cat(bbox_targets_list,\n                                     -2).view(num_images, -1, 4)\n        all_bbox_weights = torch.cat(bbox_weights_list,\n                                     -2).view(num_images, -1, 4)\n\n        # check NaN and Inf\n        assert torch.isfinite(all_cls_scores).all().item(), \\\n            'classification scores become infinite or NaN!'\n        assert torch.isfinite(all_bbox_preds).all().item(), \\\n            'bbox predications become infinite or NaN!'\n\n        losses_cls, losses_bbox = multi_apply(\n            self.loss_single,\n            all_cls_scores,\n            all_bbox_preds,\n            all_labels,\n            all_label_weights,\n            all_bbox_targets,\n            all_bbox_weights,\n            num_total_samples=num_total_pos,\n            cfg=cfg)\n        return dict(loss_cls=losses_cls, loss_bbox=losses_bbox)\n", "path": "mmdet/models/anchor_heads/ssd_head.py"}]}