diff --git a/train.py b/train.py index b0a5ec86..512377d3 100644 --- a/train.py +++ b/train.py @@ -11,19 +11,33 @@ from models import * from utils.datasets import * from utils.utils import * -# Hyperparameters: train.py --evolve --epochs 2 --img-size 320, Metrics: 0.204 0.302 0.175 0.234 (square smart) +# Hyperparameters: train.py --data data/coco.data --img-size 320 --single-scale --batch-size 64 --accumulate 1 --epochs 1 --evolve 0.087 0.281 0.109 0.121 hyp = {'giou': .035, # giou loss gain 'xy': 0.20, # xy loss gain 'wh': 0.10, # wh loss gain 'cls': 0.035, # cls loss gain + 'cls_pw': 79.0, # cls BCELoss positive_weight 'conf': 1.61, # conf loss gain - 'conf_bpw': 3.53, # conf BCELoss positive_weight + 'conf_pw': 3.53, # conf BCELoss positive_weight 'iou_t': 0.29, # iou target-anchor training threshold 'lr0': 0.001, # initial learning rate 'lrf': -4., # final learning rate = lr0 * (10 ** lrf) 'momentum': 0.90, # SGD momentum 'weight_decay': 0.0005} # optimizer weight decay +# hyp = {'giou': 1.0, # giou loss gain +# 'xy': 1.0, # xy loss gain +# 'wh': 1.0, # wh loss gain +# 'cls': 1.0, # cls loss gain +# 'cls_pw': 79.0, # cls BCELoss positive_weight +# 'conf': 1.0, # conf loss gain +# 'conf_pw': 6.0, # conf BCELoss positive_weight +# 'iou_t': 0.29, # iou target-anchor training threshold +# 'lr0': 0.001, # initial learning rate +# 'lrf': -4., # final learning rate = lr0 * (10 ** lrf) +# 'momentum': 0.90, # SGD momentum +# 'weight_decay': 0.0005} # optimizer weight decay + def train( cfg, @@ -329,7 +343,7 @@ if __name__ == '__main__': # Mutate hyperparameters old_hyp = hyp.copy() init_seeds(seed=int(time.time())) - s = [.4, .4, .4, .4, .4, .4, .4, .4 * 0, .4 * 0, .04 * 0, .4 * 0] # fractional sigmas + s = [.4, .4, .4, .4, .4, .4, .4, .4, .4 * 0, .4 * 0, .04 * 0, .4 * 0] # fractional sigmas for i, k in enumerate(hyp.keys()): x = (np.random.randn(1) * s[i] + 1) ** 1.1 # plt.hist(x.ravel(), 100) hyp[k] = hyp[k] * float(x) # vary by about 30% 1sigma diff --git a/utils/utils.py b/utils/utils.py index 8b9640ce..4c0b179c 100755 --- a/utils/utils.py +++ b/utils/utils.py @@ -279,7 +279,8 @@ def compute_loss(p, targets, model, giou_loss=False): # predictions, targets, m # Define criteria MSE = nn.MSELoss() CE = nn.CrossEntropyLoss() # (weight=model.class_weights) - BCE = nn.BCEWithLogitsLoss(pos_weight=ft([h['conf_bpw']])) + BCEcls = nn.BCEWithLogitsLoss(pos_weight=ft([h['cls_pw']])) + BCEconf = nn.BCEWithLogitsLoss(pos_weight=ft([h['conf_pw']])) # Compute losses bs = p[0].shape[0] # batch size @@ -301,13 +302,17 @@ def compute_loss(p, targets, model, giou_loss=False): # predictions, targets, m else: lxy += (k * h['xy']) * MSE(torch.sigmoid(pi[..., 0:2]), txy[i]) # xy loss lwh += (k * h['wh']) * MSE(pi[..., 2:4], twh[i]) # wh yolo loss + + # tclsm = torch.zeros_like(pi[..., 5:]) + # tclsm[range(len(b)), tcls[i]] = 1.0 + # lcls += (k * h['cls']) * BCEcls(pi[..., 5:], tclsm) # class_conf loss lcls += (k * h['cls']) * CE(pi[..., 5:], tcls[i]) # class_conf loss # # Append to text file # with open('targets.txt', 'a') as file: # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)] - lconf += (k * h['conf']) * BCE(pi0[..., 4], tconf) # obj_conf loss + lconf += (k * h['conf']) * BCEconf(pi0[..., 4], tconf) # obj_conf loss loss = lxy + lwh + lconf + lcls return loss, torch.cat((lxy, lwh, lconf, lcls, loss)).detach()