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

Fixing yolov8-seg validation #1522

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion src/sparseml/yolov8/trainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ def _load(self, weights: str):
self.model = self.ModelClass(dict(self.ckpt["model_yaml"]))
else:
self.model = self.ModelClass(dict(self.ckpt["model"].yaml))
if "recipe" in self.ckpt:

if "recipe" in self.ckpt and self.ckpt["recipe"]:
manager = ScheduledModifierManager.from_yaml(self.ckpt["recipe"])
epoch = self.ckpt.get("epoch", -1)
if epoch < 0:
Expand Down
17 changes: 15 additions & 2 deletions src/sparseml/yolov8/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
import os
import warnings
from argparse import Namespace
from typing import Any, Dict
from typing import Any, Dict, List, Tuple, Union

import torch

from ultralytics.yolo.data.dataloaders.v5loader import create_dataloader
from ultralytics.yolo.engine.model import DetectionModel
from ultralytics.yolo.engine.trainer import BaseTrainer


__all__ = ["check_coco128_segmentation", "create_grad_sampler"]
__all__ = ["check_coco128_segmentation", "create_grad_sampler", "detach"]


def check_coco128_segmentation(args: Namespace) -> Namespace:
Expand Down Expand Up @@ -69,3 +71,14 @@ def create_grad_sampler(
/ train_loader.batch_size,
)
return grad_sampler


def detach(x: Union[torch.Tensor, List, Tuple]):
abhinavnmagic marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(x, torch.Tensor):
return x.detach()
elif isinstance(x, List):
return [detach(e) for e in x]
elif isinstance(x, Tuple):
return tuple([detach(e) for e in x])
else:
raise ValueError("Unexpected type to detach")
3 changes: 2 additions & 1 deletion src/sparseml/yolov8/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import torch
from tqdm import tqdm

from sparseml.yolov8.utils import detach
from ultralytics.nn.autobackend import AutoBackend
from ultralytics.yolo.data.utils import check_cls_dataset, check_det_dataset
from ultralytics.yolo.engine.validator import BaseValidator
Expand Down Expand Up @@ -136,7 +137,7 @@ def __call__(self, trainer=None, model=None):

# During QAT the resulting preds are grad required, breaking
# the update metrics function.
detached_preds = [p.detach() for p in preds]
detached_preds = detach(preds)
self.update_metrics(detached_preds, batch)

if self.args.plots and batch_i < 3:
Expand Down