From daf958212f24b15bd4d9a5d265f1cdd66fb3fca1 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 21 Oct 2024 14:56:58 +0900 Subject: [PATCH 01/10] Create my_fastrcnn_loss_with_focal_loss.py --- my_fastrcnn_loss_with_focal_loss.py | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 my_fastrcnn_loss_with_focal_loss.py diff --git a/my_fastrcnn_loss_with_focal_loss.py b/my_fastrcnn_loss_with_focal_loss.py new file mode 100644 index 0000000000..48bcd9af38 --- /dev/null +++ b/my_fastrcnn_loss_with_focal_loss.py @@ -0,0 +1,62 @@ +import torch.nn.functional as F +from torch import nn + +class FocalLoss(nn.Module): + + def __init__(self, weight=None, + gamma=2.5, reduction='mean'): + nn.Module.__init__(self) + self.weight=weight + self.gamma = gamma + self.reduction = reduction + + def forward(self, input_tensor, target_tensor): + log_prob = F.log_softmax(input_tensor, dim=-1) + prob = torch.exp(log_prob) + return F.nll_loss( + ((1 - prob) ** self.gamma) * log_prob, + target_tensor, + weight=self.weight, + reduction = self.reduction + ) + +def fastrcnn_loss(class_logits, box_regression, labels, regression_targets): + # type: (Tensor, Tensor, List[Tensor], List[Tensor]) -> Tuple[Tensor, Tensor] + """ + Computes the loss for Faster R-CNN. + Args: + class_logits (Tensor) + box_regression (Tensor) + labels (list[BoxList]) + regression_targets (Tensor) + Returns: + classification_loss (Tensor) + box_loss (Tensor) + """ + + labels = torch.cat(labels, dim=0) + regression_targets = torch.cat(regression_targets, dim=0) + + #この部分をfocal_lossへ変更する + #classification_loss = F.cross_entropy(class_logits, labels) + focal=FocalLoss() + classification_loss = focal(class_logits, labels) + #変更はここまで + + # get indices that correspond to the regression targets for + # the corresponding ground truth labels, to be used with + # advanced indexing + sampled_pos_inds_subset = torch.where(labels > 0)[0] + labels_pos = labels[sampled_pos_inds_subset] + N, num_classes = class_logits.shape + box_regression = box_regression.reshape(N, box_regression.size(-1) // 4, 4) + + box_loss = F.smooth_l1_loss( + box_regression[sampled_pos_inds_subset, labels_pos], + regression_targets[sampled_pos_inds_subset], + beta=1 / 9, + reduction='sum', + ) + box_loss = box_loss / labels.numel() + + return classification_loss, box_loss From e65b18d8755f4cbfce47936a15e5c53b1c00b670 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:26:30 +0900 Subject: [PATCH 02/10] Update fast_rcnn.py 342_347 --- detectron2/modeling/roi_heads/fast_rcnn.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/detectron2/modeling/roi_heads/fast_rcnn.py b/detectron2/modeling/roi_heads/fast_rcnn.py index 039e2490fa..5a8a2b8c7d 100644 --- a/detectron2/modeling/roi_heads/fast_rcnn.py +++ b/detectron2/modeling/roi_heads/fast_rcnn.py @@ -338,10 +338,13 @@ def losses(self, predictions, proposals): else: proposal_boxes = gt_boxes = torch.empty((0, 4), device=proposal_deltas.device) + from my_fastrcnn_loss_with_focal_loss import fastrcnn_loss #add if self.use_sigmoid_ce: - loss_cls = self.sigmoid_cross_entropy_loss(scores, gt_classes) + # loss_cls = self.sigmoid_cross_entropy_loss(scores, gt_classes) + loss_cls = fastrcnn_loss #add else: - loss_cls = cross_entropy(scores, gt_classes, reduction="mean") + # loss_cls = cross_entropy(scores, gt_classes, reduction="mean") + loss_cls = fastrcnn_loss #add losses = { "loss_cls": loss_cls, From f38f8a965ba9ea03a273066363c431dbb29d2342 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:27:39 +0900 Subject: [PATCH 03/10] Add files via upload --- .../my_fastrcnn_loss_with_focal_loss.py | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 detectron2/modeling/roi_heads/my_fastrcnn_loss_with_focal_loss.py diff --git a/detectron2/modeling/roi_heads/my_fastrcnn_loss_with_focal_loss.py b/detectron2/modeling/roi_heads/my_fastrcnn_loss_with_focal_loss.py new file mode 100644 index 0000000000..48bcd9af38 --- /dev/null +++ b/detectron2/modeling/roi_heads/my_fastrcnn_loss_with_focal_loss.py @@ -0,0 +1,62 @@ +import torch.nn.functional as F +from torch import nn + +class FocalLoss(nn.Module): + + def __init__(self, weight=None, + gamma=2.5, reduction='mean'): + nn.Module.__init__(self) + self.weight=weight + self.gamma = gamma + self.reduction = reduction + + def forward(self, input_tensor, target_tensor): + log_prob = F.log_softmax(input_tensor, dim=-1) + prob = torch.exp(log_prob) + return F.nll_loss( + ((1 - prob) ** self.gamma) * log_prob, + target_tensor, + weight=self.weight, + reduction = self.reduction + ) + +def fastrcnn_loss(class_logits, box_regression, labels, regression_targets): + # type: (Tensor, Tensor, List[Tensor], List[Tensor]) -> Tuple[Tensor, Tensor] + """ + Computes the loss for Faster R-CNN. + Args: + class_logits (Tensor) + box_regression (Tensor) + labels (list[BoxList]) + regression_targets (Tensor) + Returns: + classification_loss (Tensor) + box_loss (Tensor) + """ + + labels = torch.cat(labels, dim=0) + regression_targets = torch.cat(regression_targets, dim=0) + + #この部分をfocal_lossへ変更する + #classification_loss = F.cross_entropy(class_logits, labels) + focal=FocalLoss() + classification_loss = focal(class_logits, labels) + #変更はここまで + + # get indices that correspond to the regression targets for + # the corresponding ground truth labels, to be used with + # advanced indexing + sampled_pos_inds_subset = torch.where(labels > 0)[0] + labels_pos = labels[sampled_pos_inds_subset] + N, num_classes = class_logits.shape + box_regression = box_regression.reshape(N, box_regression.size(-1) // 4, 4) + + box_loss = F.smooth_l1_loss( + box_regression[sampled_pos_inds_subset, labels_pos], + regression_targets[sampled_pos_inds_subset], + beta=1 / 9, + reduction='sum', + ) + box_loss = box_loss / labels.numel() + + return classification_loss, box_loss From 0e22bd5a95ecf9f5287beafc917d63fdf129ddd3 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 11 Nov 2024 15:12:14 +0900 Subject: [PATCH 04/10] Update fast_rcnn.py --- detectron2/modeling/roi_heads/fast_rcnn.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/detectron2/modeling/roi_heads/fast_rcnn.py b/detectron2/modeling/roi_heads/fast_rcnn.py index 5a8a2b8c7d..a00c95302c 100644 --- a/detectron2/modeling/roi_heads/fast_rcnn.py +++ b/detectron2/modeling/roi_heads/fast_rcnn.py @@ -12,6 +12,8 @@ from detectron2.structures import Boxes, Instances from detectron2.utils.events import get_event_storage +mode = 1 #0:default 1:focal + __all__ = ["fast_rcnn_inference", "FastRCNNOutputLayers"] @@ -338,13 +340,13 @@ def losses(self, predictions, proposals): else: proposal_boxes = gt_boxes = torch.empty((0, 4), device=proposal_deltas.device) - from my_fastrcnn_loss_with_focal_loss import fastrcnn_loss #add - if self.use_sigmoid_ce: - # loss_cls = self.sigmoid_cross_entropy_loss(scores, gt_classes) - loss_cls = fastrcnn_loss #add + if mode == 1: + from my_fastrcnn_loss_with_focal_loss import fastrcnn_loss + loss_cls = fastrcnn_loss + elif self.use_sigmoid_ce: + loss_cls = self.sigmoid_cross_entropy_loss(scores, gt_classes) else: - # loss_cls = cross_entropy(scores, gt_classes, reduction="mean") - loss_cls = fastrcnn_loss #add + loss_cls = cross_entropy(scores, gt_classes, reduction="mean") losses = { "loss_cls": loss_cls, From eb1828695a4965cf5381366669b79fbf5ab77c17 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:13:31 +0900 Subject: [PATCH 05/10] Update fast_rcnn.py add new loss function --- detectron2/modeling/roi_heads/fast_rcnn.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/detectron2/modeling/roi_heads/fast_rcnn.py b/detectron2/modeling/roi_heads/fast_rcnn.py index a00c95302c..0d4d51c744 100644 --- a/detectron2/modeling/roi_heads/fast_rcnn.py +++ b/detectron2/modeling/roi_heads/fast_rcnn.py @@ -340,13 +340,22 @@ def losses(self, predictions, proposals): else: proposal_boxes = gt_boxes = torch.empty((0, 4), device=proposal_deltas.device) - if mode == 1: - from my_fastrcnn_loss_with_focal_loss import fastrcnn_loss - loss_cls = fastrcnn_loss + #書き換えここから + loss_type = self.cfg.MODEL.ROI_HEADS.LOSS_TYPE + if loss_type == "focal": + # Focal Loss + gamma = self.cfg.MODEL.ROI_HEADS.FOCAL_LOSS_GAMMA + alpha = self.cfg.MODEL.ROI_HEADS.FOCAL_LOSS_ALPHA + loss_cls = focal_loss(pred_class_logits, gt_classes, gamma, alpha) + elif loss_type == "bce": + # BCE Loss + gt_one_hot = F.one_hot(gt_classes, num_classes=pred_class_logits.size(1)).float() + loss_cls = F.binary_cross_entropy_with_logits(pred_class_logits, gt_one_hot, reduction="mean") elif self.use_sigmoid_ce: loss_cls = self.sigmoid_cross_entropy_loss(scores, gt_classes) else: loss_cls = cross_entropy(scores, gt_classes, reduction="mean") + #ここまで losses = { "loss_cls": loss_cls, From 0fd37da989930651e7ec653c7b6c16ee7747b71b Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 23 Dec 2024 12:19:14 +0900 Subject: [PATCH 06/10] Update defaults.py add default loss function --- detectron2/config/defaults.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/detectron2/config/defaults.py b/detectron2/config/defaults.py index 506651730e..3c72580e58 100644 --- a/detectron2/config/defaults.py +++ b/detectron2/config/defaults.py @@ -654,3 +654,8 @@ # Do not commit any configs into it. _C.GLOBAL = CN() _C.GLOBAL.HACK = 1.0 + +# ここから追加 +_C.MODEL.ROI_HEADS.LOSS_TYPE = "bce" # "focal"または"bce"も選択可能 +_C.MODEL.ROI_HEADS.FOCAL_LOSS_GAMMA = 2.0 +_C.MODEL.ROI_HEADS.FOCAL_LOSS_ALPHA = 0.25 From c843ec681d2ed181ccfaa41e16fdd695ea96c2b8 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 13 Jan 2025 12:41:42 +0900 Subject: [PATCH 07/10] Create note --- note | 1 + 1 file changed, 1 insertion(+) create mode 100644 note diff --git a/note b/note new file mode 100644 index 0000000000..6b34b21ca4 --- /dev/null +++ b/note @@ -0,0 +1 @@ +my_fastrcnn_loss_with_focal_loss.pyは新しく追加したものである。 From 3394fe6c8cb70c085c5b8e180b0eea16ce85b2e8 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 13 Jan 2025 12:45:13 +0900 Subject: [PATCH 08/10] Update note --- note | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/note b/note index 6b34b21ca4..142504f0ad 100644 --- a/note +++ b/note @@ -1 +1 @@ -my_fastrcnn_loss_with_focal_loss.pyは新しく追加したものである。 +# my_fastrcnn_loss_with_focal_loss.pyは新しく追加したもの From f97e9fa13b48bf3e2890b6cebbdecaf37926af82 Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:54:05 +0900 Subject: [PATCH 09/10] Update fast_rcnn.py --- detectron2/modeling/roi_heads/fast_rcnn.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/detectron2/modeling/roi_heads/fast_rcnn.py b/detectron2/modeling/roi_heads/fast_rcnn.py index 0d4d51c744..f1f15d1779 100644 --- a/detectron2/modeling/roi_heads/fast_rcnn.py +++ b/detectron2/modeling/roi_heads/fast_rcnn.py @@ -351,6 +351,9 @@ def losses(self, predictions, proposals): # BCE Loss gt_one_hot = F.one_hot(gt_classes, num_classes=pred_class_logits.size(1)).float() loss_cls = F.binary_cross_entropy_with_logits(pred_class_logits, gt_one_hot, reduction="mean") + elif loss_type == 'dummy': + # dummy loss + loss_cls = torch.tensor(1.0, requires_grad=True, device=predictions[0].device) elif self.use_sigmoid_ce: loss_cls = self.sigmoid_cross_entropy_loss(scores, gt_classes) else: From 19e8237ee3f11888677033349dba8a206f26e03b Mon Sep 17 00:00:00 2001 From: ricky0614 <162050908+ricky0614@users.noreply.github.com> Date: Mon, 3 Feb 2025 14:58:51 +0900 Subject: [PATCH 10/10] Create new_roy_heads.py --- detectron2/modeling/roi_heads/new_roy_heads.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 detectron2/modeling/roi_heads/new_roy_heads.py diff --git a/detectron2/modeling/roi_heads/new_roy_heads.py b/detectron2/modeling/roi_heads/new_roy_heads.py new file mode 100644 index 0000000000..2735fbb2fc --- /dev/null +++ b/detectron2/modeling/roi_heads/new_roy_heads.py @@ -0,0 +1,11 @@ +from .roi_heads import ROI_HEADS_REGISTRY, StandardROIHeads +import torch + +@ROI_HEADS_REGISTRY.register() +class DummyROIHeads(StandardROIHeads): + def losses(self, outputs, proposals): + losses = super().losses(outputs, proposals) + losses["loss_cls"] = torch.randn_like(losses["loss_cls"]) * 100 #ノイズ追加 + losses["loss_box_reg"] = torch.tensor(1e5, device=losses["loss_box_reg"].device) #回帰の破壊で予測無効化 + + return losses