@ -159,16 +159,18 @@ class Tuner:
"""
t0 = time . time ( )
best_save_dir , best_metrics = None , None
self . tune_dir . mkdir ( parents = True , exist_ok = True )
for i in range ( iterations ) :
# Mutate hyperparameters
mutated_hyp = self . _mutate ( )
LOGGER . info ( f ' { prefix } Starting iteration { i + 1 } / { iterations } with hyperparameters: { mutated_hyp } ' )
# Initialize and train YOLOv8 model
try :
# Train YOLO model with mutated hyperparameters
train_args = { * * vars ( self . args ) , * * mutated_hyp }
fitness = ( deepcopy ( model ) or YOLO ( self . args . model ) ) . train ( * * train_args ) . fitness # results.fitness
results = ( deepcopy ( model ) or YOLO ( self . args . model ) ) . train ( * * train_args )
fitness = results . fitness
except Exception as e :
LOGGER . warning ( f ' WARNING ❌️ training failure for hyperparameter tuning iteration { i } \n { e } ' )
fitness = 0.0
@ -179,14 +181,25 @@ class Tuner:
with open ( self . evolve_csv , ' a ' ) as f :
f . write ( headers + ' , ' . join ( map ( str , log_row ) ) + ' \n ' )
# Print tuning results
x = np . loadtxt ( self . evolve_csv , ndmin = 2 , delimiter = ' , ' , skiprows = 1 )
fitness = x [ : , 0 ] # first column
i = np . argsort ( - fitness ) [ 0 ] # best fitness index
LOGGER . info ( f ' \n { prefix } All iterations complete ✅ ( { time . time ( ) - t0 : .2f } s) \n '
f ' { prefix } Results saved to { colorstr ( " bold " , self . tune_dir ) } \n '
f ' { prefix } Best fitness= { fitness [ i ] } observed at iteration { i } ' )
# Save turning results
yaml_save ( self . tune_dir / ' best.yaml ' , data = { k : float ( x [ 0 , i + 1 ] ) for i , k in enumerate ( self . space . keys ( ) ) } )
yaml_print ( self . tune_dir / ' best.yaml ' )
# Print tuning results
x = np . loadtxt ( self . evolve_csv , ndmin = 2 , delimiter = ' , ' , skiprows = 1 )
fitness = x [ : , 0 ] # first column
best_idx = fitness . argmax ( )
best_is_current = best_idx == i
if best_is_current :
best_save_dir = results . save_dir
best_metrics = { k : round ( v , 5 ) for k , v in results . results_dict . items ( ) }
header = ( f ' { prefix } { i + 1 } iterations complete ✅ ( { time . time ( ) - t0 : .2f } s) \n '
f ' { prefix } Results saved to { colorstr ( " bold " , self . tune_dir ) } \n '
f ' { prefix } Best fitness= { fitness [ best_idx ] } observed at iteration { best_idx + 1 } \n '
f ' { prefix } Best fitness metrics are { best_metrics } \n '
f ' { prefix } Best fitness model is { best_save_dir } \n '
f ' { prefix } Best fitness hyperparameters are printed below. \n ' )
LOGGER . info ( ' \n ' + header )
# Save turning results
data = { k : float ( x [ 0 , i + 1 ] ) for i , k in enumerate ( self . space . keys ( ) ) }
header = header . replace ( prefix , ' # ' ) . replace ( ' [1m/' , ' ' ) . replace ( ' [0m' , ' ' ) + ' \n '
yaml_save ( self . tune_dir / ' best.yaml ' , data = data , header = header )
yaml_print ( self . tune_dir / ' best.yaml ' )