|
|
|
@ -2,10 +2,12 @@ import math
|
|
|
|
|
import os
|
|
|
|
|
import platform
|
|
|
|
|
import time
|
|
|
|
|
import random
|
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
from copy import deepcopy
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import thop
|
|
|
|
|
import torch
|
|
|
|
|
import torch.distributed as dist
|
|
|
|
@ -198,6 +200,20 @@ def one_cycle(y1=0.0, y2=1.0, steps=100):
|
|
|
|
|
# lambda function for sinusoidal ramp from y1 to y2 https://arxiv.org/pdf/1812.01187.pdf
|
|
|
|
|
return lambda x: ((1 - math.cos(x * math.pi / steps)) / 2) * (y2 - y1) + y1
|
|
|
|
|
|
|
|
|
|
def init_seeds(seed=0, deterministic=False):
|
|
|
|
|
# Initialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html
|
|
|
|
|
random.seed(seed)
|
|
|
|
|
np.random.seed(seed)
|
|
|
|
|
torch.manual_seed(seed)
|
|
|
|
|
torch.cuda.manual_seed(seed)
|
|
|
|
|
torch.cuda.manual_seed_all(seed) # for Multi-GPU, exception safe
|
|
|
|
|
# torch.backends.cudnn.benchmark = True # AutoBatch problem https://github.com/ultralytics/yolov5/issues/9287
|
|
|
|
|
if deterministic and check_version(torch.__version__, '1.12.0'): # https://github.com/ultralytics/yolov5/pull/8213
|
|
|
|
|
torch.use_deterministic_algorithms(True)
|
|
|
|
|
torch.backends.cudnn.deterministic = True
|
|
|
|
|
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'
|
|
|
|
|
os.environ['PYTHONHASHSEED'] = str(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ModelEMA:
|
|
|
|
|
""" Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models
|
|
|
|
|