Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] add eval_interval, save the best model add information about the eval… #74

Open
wants to merge 1 commit into
base: TRAIN
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions yolo/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ class TrainConfig:
task: str
epoch: int
data: DataConfig
eval_interval: int
optimizer: OptimizerConfig
loss: LossConfig
scheduler: SchedulerConfig
Expand Down
1 change: 1 addition & 0 deletions yolo/config/task/train.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defaults:
- validation: ../validation

epoch: 500
eval_interval: 10

data:
batch_size: 16
Expand Down
27 changes: 19 additions & 8 deletions yolo/tools/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, cfg: Config, model: YOLO, vec2box: Vec2Box, progress: Progres
self.loss_fn = create_loss_function(cfg, vec2box)
self.progress = progress
self.num_epochs = cfg.task.epoch
self.eval_interval = cfg.task.eval_interval
self.mAPs_dict = defaultdict(list)

self.weights_dir = self.progress.save_path / "weights"
Expand Down Expand Up @@ -126,11 +127,11 @@ def save_checkpoint(self, epoch_idx: int, file_name: Optional[str] = None):
torch.save(checkpoint, file_path)

def good_epoch(self, mAPs: Dict[str, Tensor]) -> bool:
save_flag = True
save_flag = False
for mAP_key, mAP_val in mAPs.items():
if not self.mAPs_dict[mAP_key] or mAP_val > max(self.mAPs_dict[mAP_key]):
save_flag = True
self.mAPs_dict[mAP_key].append(mAP_val)
if mAP_val < max(self.mAPs_dict[mAP_key]):
save_flag = False
return save_flag

def solve(self, dataloader: DataLoader):
Expand All @@ -146,9 +147,13 @@ def solve(self, dataloader: DataLoader):
epoch_loss = self.train_one_epoch(dataloader)
self.progress.finish_one_epoch(epoch_loss, epoch_idx=epoch_idx)

mAPs = self.validator.solve(self.validation_dataloader, epoch_idx=epoch_idx)
if mAPs is not None and self.good_epoch(mAPs):
self.save_checkpoint(epoch_idx=epoch_idx)
if (epoch_idx + 1) % self.eval_interval == 0:
mAPs = self.validator.solve(self.validation_dataloader, epoch_idx=epoch_idx)
if mAPs is not None:
self.save_checkpoint(epoch_idx=epoch_idx+1)
if self.good_epoch(mAPs):
self.save_checkpoint(epoch_idx=epoch_idx+1, file_name="best.pt")

# TODO: save model if result are better than before
self.progress.finish_train()

Expand Down Expand Up @@ -259,9 +264,15 @@ def solve(self, dataloader, epoch_idx=1):
if self.progress.local_rank != 0:
return
json.dump(predict_json, f)
if hasattr(self, "coco_gt"):

if predict_json and hasattr(self, "coco_gt"):
self.progress.start_pycocotools()
result = calculate_ap(self.coco_gt, predict_json)
self.progress.finish_pycocotools(result, epoch_idx)
else:
if not predict_json:
logger.warning("⚠️ No predictions available for evaluation.")
if not hasattr(self, "coco_gt"):
logger.warning("⚠️ COCO ground truth not found. Please check dataset configuration.")

return avg_mAPs
return avg_mAPs