Update loss criteria constructor (#1711)

This commit is contained in:
Glenn Jocher authored and GitHub committed 2020-12-16 08:39:35 -08:00
1 parent 799724108f
commit 8bc0027afc
4 files changed
+18 -15

No files matched your search

+4 -4
View File
@@ -1,5 +1,6 @@
import argparse
import logging
import math
import os
import random
import time
@@ -7,7 +8,6 @@ from pathlib import Path
from threading import Thread
from warnings import warn
import math
import numpy as np
import torch.distributed as dist
import torch.nn as nn
@@ -217,7 +217,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
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)
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) # attach class weights
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
model.names = names
# Start training
@@ -238,7 +238,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
if opt.image_weights:
# Generate indices
if rank in [-1, 0]:
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 # class weights
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
# Broadcast if DDP
@@ -330,7 +330,7 @@ def train(hyp, opt, device, tb_writer=None, wandb=None):
if rank in [-1, 0]:
# mAP
if ema:
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'gr', 'names', 'stride'])
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'gr', 'names', 'stride', 'class_weights'])
final_epoch = epoch + 1 == epochs
if not opt.notest or final_epoch: # Calculate mAP
results, maps, times = test.test(opt.data,