fix: separar conjunto de validação do treino com split 80/20
Antes, val e train apontavam para o mesmo diretório, causando overfitting silencioso e métricas infladas. Agora 20% das imagens são movidas para val/ antes do treino. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
432159bb2e
commit
1c39fb70f8
@@ -146,10 +146,28 @@ def _executar_treino_sync(ambiente: str, pular_triagem: bool) -> dict:
|
|||||||
if not k.endswith('.txt'):
|
if not k.endswith('.txt'):
|
||||||
redimensionar_imagem(caminho_local)
|
redimensionar_imagem(caminho_local)
|
||||||
|
|
||||||
if len(os.listdir(img_dir)) > 0:
|
imagens = [f for f in os.listdir(img_dir) if not f.endswith('.txt')]
|
||||||
|
if len(imagens) > 0:
|
||||||
|
import random, shutil
|
||||||
|
random.shuffle(imagens)
|
||||||
|
corte = max(1, int(len(imagens) * 0.8))
|
||||||
|
val_imgs = imagens[corte:]
|
||||||
|
|
||||||
|
val_img_dir = f"{dataset_local}/val/images"
|
||||||
|
val_lbl_dir = f"{dataset_local}/val/labels"
|
||||||
|
os.makedirs(val_img_dir, exist_ok=True)
|
||||||
|
os.makedirs(val_lbl_dir, exist_ok=True)
|
||||||
|
|
||||||
|
for fname in val_imgs:
|
||||||
|
shutil.move(os.path.join(img_dir, fname), os.path.join(val_img_dir, fname))
|
||||||
|
lbl = fname.rsplit('.', 1)[0] + '.txt'
|
||||||
|
lbl_src = os.path.join(lbl_dir, lbl)
|
||||||
|
if os.path.exists(lbl_src):
|
||||||
|
shutil.move(lbl_src, os.path.join(val_lbl_dir, lbl))
|
||||||
|
|
||||||
yaml_path = f"{dataset_local}/data.yaml"
|
yaml_path = f"{dataset_local}/data.yaml"
|
||||||
with open(yaml_path, 'w') as f:
|
with open(yaml_path, 'w') as f:
|
||||||
yaml.dump({'train': img_dir, 'val': img_dir, 'nc': 1, 'names': {0: ambiente}}, f)
|
yaml.dump({'train': img_dir, 'val': val_img_dir, 'nc': 1, 'names': {0: ambiente}}, f)
|
||||||
|
|
||||||
log_print(f"Treinando com {len(os.listdir(img_dir))} fotos ({'fine-tuning' if is_fine_tuning else 'do zero'})...")
|
log_print(f"Treinando com {len(os.listdir(img_dir))} fotos ({'fine-tuning' if is_fine_tuning else 'do zero'})...")
|
||||||
modelo_base.add_callback("on_fit_epoch_end", _on_fit_epoch_end)
|
modelo_base.add_callback("on_fit_epoch_end", _on_fit_epoch_end)
|
||||||
|
|||||||
Reference in New Issue
Block a user