diff --git a/utils/datasets.py b/utils/datasets.py index 3ea42031..2b5a0bf3 100755 --- a/utils/datasets.py +++ b/utils/datasets.py @@ -626,9 +626,8 @@ def letterbox(img, new_shape=(416, 416), color=(114, 114, 114), auto=True, scale def random_affine(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, border=0): # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10)) # https://medium.com/uruvideo/dataset-augmentation-with-random-homographies-a8f4b44830d4 + # targets = [cls, xyxy] - if targets is None: # targets = [cls, xyxy] - targets = [] height = img.shape[0] + border * 2 width = img.shape[1] + border * 2 @@ -637,6 +636,7 @@ def random_affine(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, a = random.uniform(-degrees, degrees) # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations s = random.uniform(1 - scale, 1 + scale) + # s = 2 ** random.uniform(-scale, scale) R[:2] = cv2.getRotationMatrix2D(angle=a, center=(img.shape[1] / 2, img.shape[0] / 2), scale=s) # Translation diff --git a/utils/torch_utils.py b/utils/torch_utils.py index 215772bf..d4cd1e80 100644 --- a/utils/torch_utils.py +++ b/utils/torch_utils.py @@ -12,10 +12,10 @@ import torch.nn.functional as F def init_seeds(seed=0): torch.manual_seed(seed) - # Remove randomness (may be slower on Tesla GPUs) # https://pytorch.org/docs/stable/notes/randomness.html + # Reduce randomness (may be slower on Tesla GPUs) # https://pytorch.org/docs/stable/notes/randomness.html if seed == 0: - cudnn.deterministic = True - cudnn.benchmark = False + cudnn.deterministic = False + cudnn.benchmark = True def select_device(device='', apex=False, batch_size=None): diff --git a/utils/utils.py b/utils/utils.py index f7ce54f7..88c8c541 100755 --- a/utils/utils.py +++ b/utils/utils.py @@ -103,7 +103,7 @@ def coco80_to_coco91_class(): # converts 80-index (val2014) to 91-index (paper) def xyxy2xywh(x): - # Transform box coordinates from [x1, y1, x2, y2] (where xy1=top-left, xy2=bottom-right) to [x, y, w, h] + # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] where xy1=top-left, xy2=bottom-right y = torch.zeros_like(x) if isinstance(x, torch.Tensor) else np.zeros_like(x) y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center @@ -113,7 +113,7 @@ def xyxy2xywh(x): def xywh2xyxy(x): - # Transform box coordinates from [x, y, w, h] to [x1, y1, x2, y2] (where xy1=top-left, xy2=bottom-right) + # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right y = torch.zeros_like(x) if isinstance(x, torch.Tensor) else np.zeros_like(x) y[:, 0] = x[:, 0] - x[:, 2] / 2 # top left x y[:, 1] = x[:, 1] - x[:, 3] / 2 # top left y @@ -122,26 +122,6 @@ def xywh2xyxy(x): return y -# def xywh2xyxy(box): -# # Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] -# if isinstance(box, torch.Tensor): -# x, y, w, h = box.t() -# return torch.stack((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).t() -# else: # numpy -# x, y, w, h = box.T -# return np.stack((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).T -# -# -# def xyxy2xywh(box): -# # Convert nx4 boxes from [x1, y1, x2, y2] to [x, y, w, h] -# if isinstance(box, torch.Tensor): -# x1, y1, x2, y2 = box.t() -# return torch.stack(((x1 + x2) / 2, (y1 + y2) / 2, x2 - x1, y2 - y1)).t() -# else: # numpy -# x1, y1, x2, y2 = box.T -# return np.stack(((x1 + x2) / 2, (y1 + y2) / 2, x2 - x1, y2 - y1)).T - - def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None): # Rescale coords (xyxy) from img1_shape to img0_shape if ratio_pad is None: # calculate from img0_shape @@ -187,7 +167,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls): # Create Precision-Recall curve and compute AP for each class pr_score = 0.1 # score to evaluate P and R https://github.com/ultralytics/yolov3/issues/898 - s = [len(unique_classes), tp.shape[1]] # number class, number iou thresholds (i.e. 10 for mAP0.5...0.95) + s = [unique_classes.shape[0], tp.shape[1]] # number class, number iou thresholds (i.e. 10 for mAP0.5...0.95) ap, p, r = np.zeros(s), np.zeros(s), np.zeros(s) for ci, c in enumerate(unique_classes): i = pred_cls == c @@ -601,7 +581,7 @@ def print_model_biases(model): pass -def strip_optimizer(f='weights/last.pt'): # from utils.utils import *; strip_optimizer() +def strip_optimizer(f='weights/best.pt'): # from utils.utils import *; strip_optimizer() # Strip optimizer from *.pt files for lighter files (reduced by 2/3 size) x = torch.load(f, map_location=torch.device('cpu')) x['optimizer'] = None @@ -614,12 +594,11 @@ def create_backbone(f='weights/last.pt'): # from utils.utils import *; create_b x['optimizer'] = None x['training_results'] = None x['epoch'] = -1 - for p in x['model'].values(): - try: - p.requires_grad = True - except: - pass - torch.save(x, 'weights/backbone.pt') + for p in x['model'].parameters(): + p.requires_grad = True + s = 'weights/backbone.pt' + print('%s saved as %s' % (f, s)) + torch.save(x, s) def coco_class_count(path='../coco/labels/train2014/'):