Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit e29fada

Browse files
committed
working
1 parent 2265c77 commit e29fada

File tree

9 files changed

+66
-26
lines changed

9 files changed

+66
-26
lines changed

src/sparseml/yolov8/export.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615

1716
import click
1817
from sparseml.yolov8.trainers import SparseYOLO
19-
from ultralytics.yolo.utils import USER_CONFIG_DIR, get_settings, yaml_save
2018

2119

2220
# Options generated from
@@ -88,12 +86,6 @@
8886
help="Path to override default datasets dir.",
8987
)
9088
def main(**kwargs):
91-
if kwargs["datasets_dir"] is not None:
92-
settings = get_settings()
93-
settings["datasets_dir"] = os.path.abspath(
94-
os.path.expanduser(kwargs["datasets_dir"])
95-
)
96-
yaml_save(USER_CONFIG_DIR / "settings.yaml", settings)
9789

9890
model = SparseYOLO(kwargs["model"])
9991
model.export(**kwargs)
12.3 MB
Binary file not shown.
Binary file not shown.

src/sparseml/yolov8/exported/deployment/config.json

Whitespace-only changes.

src/sparseml/yolov8/train.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
# limitations under the License.
1414

1515
import logging
16-
import os
1716

1817
import click
1918
from sparseml.yolov8.trainers import SparseYOLO
20-
from ultralytics.yolo.utils import USER_CONFIG_DIR, get_settings, yaml_save
19+
from sparseml.yolov8.utils import data_from_datasets_dir
2120

2221

2322
logger = logging.getLogger()
@@ -219,11 +218,7 @@
219218
)
220219
def main(**kwargs):
221220
if kwargs["datasets_dir"] is not None:
222-
settings = get_settings()
223-
settings["datasets_dir"] = os.path.abspath(
224-
os.path.expanduser(kwargs["datasets_dir"])
225-
)
226-
yaml_save(USER_CONFIG_DIR / "settings.yaml", settings)
221+
kwargs["data"] = data_from_datasets_dir(kwargs["data"], kwargs["datasets_dir"])
227222

228223
model = SparseYOLO(kwargs["model"])
229224
model.train(**kwargs)

src/sparseml/yolov8/trainers.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
from sparseml.pytorch.utils.helpers import download_framework_model_by_recipe_type
3333
from sparseml.pytorch.utils.logger import LoggerManager, PythonLogger, WANDBLogger
3434
from sparseml.yolov8.modules import Bottleneck, Conv
35-
from sparseml.yolov8.utils import check_coco128_segmentation, create_grad_sampler
35+
from sparseml.yolov8.utils import (
36+
check_coco128_segmentation,
37+
create_grad_sampler,
38+
data_from_datasets_dir,
39+
)
3640
from sparseml.yolov8.utils.export_samples import export_sample_inputs_outputs
3741
from sparseml.yolov8.validators import (
3842
SparseClassificationValidator,
@@ -655,6 +659,11 @@ def export(self, **kwargs):
655659
if kwargs["device"] is not None and "cpu" not in kwargs["device"]:
656660
overrides["device"] = "cuda:" + kwargs["device"]
657661
overrides["deterministic"] = kwargs["deterministic"]
662+
if kwargs["datasets_dir"] is not None:
663+
overrides["data"] = data_from_datasets_dir(
664+
overrides["data"], kwargs["datasets_dir"]
665+
)
666+
658667
trainer = self.TrainerClass(overrides=overrides)
659668
self.model = self.model.to(trainer.device)
660669

@@ -710,9 +719,12 @@ def export(self, **kwargs):
710719
if args["export_samples"]:
711720
trainer_config = get_cfg(cfg=DEFAULT_SPARSEML_CONFIG_PATH)
712721

722+
if args["datasets_dir"] is not None:
723+
args["data"] = data_from_datasets_dir(
724+
args["data"], args["datasets_dir"]
725+
)
713726
trainer_config.data = args["data"]
714727
trainer_config.imgsz = args["imgsz"]
715-
716728
trainer = DetectionTrainer(trainer_config)
717729
# inconsistency in name between
718730
# validation and test sets

src/sparseml/yolov8/utils/helpers.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import glob
1516
import os
1617
import warnings
1718
from argparse import Namespace
1819
from typing import Any, Dict
1920

21+
import yaml
22+
2023
from ultralytics.yolo.data.dataloaders.v5loader import create_dataloader
24+
from ultralytics.yolo.data.utils import ROOT
2125
from ultralytics.yolo.engine.model import DetectionModel
2226
from ultralytics.yolo.engine.trainer import BaseTrainer
2327

2428

25-
__all__ = ["check_coco128_segmentation", "create_grad_sampler"]
29+
__all__ = [
30+
"check_coco128_segmentation",
31+
"create_grad_sampler",
32+
"data_from_datasets_dir",
33+
]
2634

2735

2836
def check_coco128_segmentation(args: Namespace) -> Namespace:
@@ -69,3 +77,41 @@ def create_grad_sampler(
6977
/ train_loader.batch_size,
7078
)
7179
return grad_sampler
80+
81+
82+
def data_from_datasets_dir(data: str, datasets_dir: str) -> str:
83+
"""
84+
Given a dataset name, fetch the yaml config for the dataset
85+
from the Ultralytics dataset repo, overwrite its 'path'
86+
attribute (dataset root dir) to point to the `datasets_dir`
87+
and finally save it to the `datasets_dir` directory.
88+
This allows to create load data yaml config files that point
89+
to the arbitrary directories on the disk.
90+
91+
92+
:param data: name of the dataset (e.g. "coco.yaml")
93+
:param datasets_dir: path to the directory where the dataset is expected to be
94+
(and where the new yaml config will be saved)
95+
:return: a path to the new yaml config file
96+
"""
97+
ultralytics_dataset_path = glob.glob(os.path.join(ROOT, "**", data), recursive=True)
98+
if len(ultralytics_dataset_path) != 1:
99+
raise ValueError(
100+
"Expected to find a single path to the "
101+
f"dataset yaml file: {data}, but found {ultralytics_dataset_path}"
102+
)
103+
ultralytics_dataset_path = ultralytics_dataset_path[0]
104+
with open(ultralytics_dataset_path, "r") as f:
105+
yaml_config = yaml.safe_load(f)
106+
107+
local_dataset_path = os.path.join(
108+
datasets_dir, os.path.basename(yaml_config["path"])
109+
)
110+
yaml_config["path"] = local_dataset_path
111+
112+
yaml_save_path = os.path.join(datasets_dir, data)
113+
114+
# save the new dataset yaml file
115+
with open(yaml_save_path, "w") as outfile:
116+
yaml.dump(yaml_config, outfile, default_flow_style=False)
117+
return yaml_save_path

src/sparseml/yolov8/val.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615

1716
import click
1817
from sparseml.yolov8.trainers import SparseYOLO
19-
from ultralytics.yolo.utils import USER_CONFIG_DIR, get_settings, yaml_save
18+
from sparseml.yolov8.utils import data_from_datasets_dir
2019

2120

2221
@click.command(
@@ -74,16 +73,12 @@
7473
@click.option(
7574
"--datasets-dir",
7675
type=str,
77-
default=None,
76+
default="/home/ubuntu/damian/sparseml/funny_dir",
7877
help="Path to override default datasets dir.",
7978
)
8079
def main(**kwargs):
8180
if kwargs["datasets_dir"] is not None:
82-
settings = get_settings()
83-
settings["datasets_dir"] = os.path.abspath(
84-
os.path.expanduser(kwargs["datasets_dir"])
85-
)
86-
yaml_save(USER_CONFIG_DIR / "settings.yaml", settings)
81+
kwargs["data"] = data_from_datasets_dir(kwargs["data"], kwargs["datasets_dir"])
8782

8883
model = SparseYOLO(kwargs["model"])
8984
model.val(**kwargs)

src/sparseml/yolov8/yolov8n.pt

6.23 MB
Binary file not shown.

0 commit comments

Comments
 (0)