From d2367a7b368d1a8f62825a2c0456ca01372bf96f Mon Sep 17 00:00:00 2001 From: phattran Date: Tue, 2 Mar 2021 15:49:43 +0700 Subject: [PATCH 1/6] Add label smoothing option --- train.py | 2 ++ utils/loss.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/train.py b/train.py index e2c82339f7fe..a8561d43deac 100644 --- a/train.py +++ b/train.py @@ -221,6 +221,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None): hyp['box'] *= 3. / nl # scale to layers hyp['cls'] *= nc / 80. * 3. / nl # scale to classes and layers hyp['obj'] *= (imgsz / 640) ** 2 * 3. / nl # scale to image size and layers + hyp['label_smoothing'] = opt.label_smoothing model.nc = nc # attach number of classes to model model.hyp = hyp # attach hyperparameters to model model.gr = 1.0 # iou loss ratio (obj_loss = 1.0 or iou) @@ -474,6 +475,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None): parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment') parser.add_argument('--quad', action='store_true', help='quad dataloader') parser.add_argument('--linear-lr', action='store_true', help='linear LR') + parser.add_argument('--label-smoothing', type=int, default=0.0, help='Label smoothing epsilon') opt = parser.parse_args() # Set DDP variables diff --git a/utils/loss.py b/utils/loss.py index 2302d18de87d..7b8fbe38b8ae 100644 --- a/utils/loss.py +++ b/utils/loss.py @@ -97,7 +97,7 @@ def __init__(self, model, autobalance=False): BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 - self.cp, self.cn = smooth_BCE(eps=0.0) + self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # Focal loss g = h['fl_gamma'] # focal loss gamma From a78a676701cb9fa0f0ea7516dc69c7f0dea08280 Mon Sep 17 00:00:00 2001 From: phattran Date: Tue, 2 Mar 2021 15:55:48 +0700 Subject: [PATCH 2/6] Correct data type --- train.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/train.py b/train.py index a8561d43deac..26caaa79bd5d 100644 --- a/train.py +++ b/train.py @@ -475,7 +475,8 @@ def train(hyp, opt, device, tb_writer=None, wandb=None): parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment') parser.add_argument('--quad', action='store_true', help='quad dataloader') parser.add_argument('--linear-lr', action='store_true', help='linear LR') - parser.add_argument('--label-smoothing', type=int, default=0.0, help='Label smoothing epsilon') + parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon') + opt = parser.parse_args() # Set DDP variables From 5b28012aa2bec3c7822e0a235d641e6182ce183d Mon Sep 17 00:00:00 2001 From: phattran Date: Tue, 2 Mar 2021 16:00:08 +0700 Subject: [PATCH 3/6] add_log --- utils/loss.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/loss.py b/utils/loss.py index 7b8fbe38b8ae..7aaf7d958d4b 100644 --- a/utils/loss.py +++ b/utils/loss.py @@ -99,6 +99,8 @@ def __init__(self, model, autobalance=False): # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) + print(h.get('label_smoothing', 0.0), self.cp, self.cn) + # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: From b0d1093fca0856b09c8fd50d12f4f34f0bec786c Mon Sep 17 00:00:00 2001 From: phattran Date: Tue, 2 Mar 2021 16:02:47 +0700 Subject: [PATCH 4/6] Remove log --- utils/loss.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/utils/loss.py b/utils/loss.py index 7aaf7d958d4b..7b8fbe38b8ae 100644 --- a/utils/loss.py +++ b/utils/loss.py @@ -99,8 +99,6 @@ def __init__(self, model, autobalance=False): # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) - print(h.get('label_smoothing', 0.0), self.cp, self.cn) - # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: From a9a4f71aef6f57eb5498a262e5e9b5616ac13712 Mon Sep 17 00:00:00 2001 From: phattran Date: Wed, 3 Mar 2021 16:56:44 +0700 Subject: [PATCH 5/6] Add log --- utils/loss.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/loss.py b/utils/loss.py index 7b8fbe38b8ae..1e68c19e4caa 100644 --- a/utils/loss.py +++ b/utils/loss.py @@ -99,6 +99,8 @@ def __init__(self, model, autobalance=False): # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) + print(f'positive BCE target: {self.cp}, negative BCE target {self.cn}') + # Focal loss g = h['fl_gamma'] # focal loss gamma if g > 0: From cd3f4f67c36b3c447a21a7cba1f6da03065846c3 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Mon, 29 Mar 2021 18:29:05 +0200 Subject: [PATCH 6/6] Update loss.py remove comment (too versbose) --- utils/loss.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utils/loss.py b/utils/loss.py index 1e68c19e4caa..9e78df17fdf3 100644 --- a/utils/loss.py +++ b/utils/loss.py @@ -97,9 +97,7 @@ def __init__(self, model, autobalance=False): BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device)) # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3 - self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) - - print(f'positive BCE target: {self.cp}, negative BCE target {self.cn}') + self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets # Focal loss g = h['fl_gamma'] # focal loss gamma