我正在尝试找出最适合 model.pred 的
conf
和 iou
。
from ultralytics import YOLO
import pandas as pd
import numpy as np
df= pd.DataFrame()
# Load a model
for i in range(1,105):
print('epoch: ',i)
try:
model = YOLO(f'/content/weights/epoch{i}.pt')
for confidence in np.arange(0.1,0.4,0.02):
for inter in np.arange(0.1,0.8,0.05):
# Customize validation settings
validation_results = model.val(data='/content/myproject/data.yaml',
imgsz=640,
batch=16,
conf=confidence,
iou=inter,
device='cpu')
t = pd.DataFrame([{"epoch":i,
"conf":confidence,
"iou":inter,
"map50":validation_results.box.map50}])
df = df.append(t, ignore_index = True)
print(df.sort_values(by=['map50'],ascending=False).head(3))
except:
pass
上面是我尝试每种组合并按最高平均精度 (MAP) 进行排序的尝试。
这很慢,因为它正在尝试每种组合。也许可以使用像 Optuna 这样的包或其他贝叶斯包?您会采取什么措施来优化这个场景?
使用 Optuna 的答案。
下面将设置一个目标,尝试各种
iou
和 conf
,直到找到最大 MAP50 分数。这将有助于更好地理解 iou
和 conf
值以及它们如何影响地图得分。
!pip install optuna
from ultralytics import YOLO
import pandas as pd
import numpy as np
class Objective:
def __init__(self):
self.best_map = 0
def __call__(self, trial):
i = trial.suggest_int("combos", 1, 104)
confidence = trial.suggest_float("confidence", 0.05, 0.5)
inter = trial.suggest_float("iou", 0.1, 0.8)
model = YOLO(f'/content/weights/epoch{i}.pt')
validation_results = model.val(data='/content/myproject/data.yaml',
imgsz=640,
batch=16,
conf=confidence,
iou=inter,
device='cpu')
print(validation_results.box.map50)
self._map = float(validation_results.box.map50)
map = float(validation_results.box.map50)
return map
def callback(self, study, trial):
if study.best_trial == trial:
self.best_map = self._map
print('NEW BEST MAP: ', self._map)
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning) # for log error
import optuna
objective = Objective()
# Setting SEED
from optuna.samplers import TPESampler
sampler = TPESampler(seed=10)
study = optuna.create_study(
pruner=optuna.pruners.MedianPruner(n_warmup_steps=10), direction="maximize",
sampler=sampler
)
study.optimize(objective, n_trials=1000, callbacks=[objective.callback])
print("Best trial:")
trial = study.best_trial
print(" Params: ")
for key, value in trial.params.items():
print(" {}: {}".format(key, value))