mirror of
https://github.com/THU-MIG/yolov10.git
synced 2025-05-23 21:44:22 +08:00
Make YOLO a module (#111)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
This commit is contained in:
parent
0303ced8ab
commit
34829a6b29
18
README.md
18
README.md
@ -16,19 +16,21 @@ pip install -e .
|
|||||||
### 1. CLI
|
### 1. CLI
|
||||||
To simply use the latest Ultralytics YOLO models
|
To simply use the latest Ultralytics YOLO models
|
||||||
```bash
|
```bash
|
||||||
yolo task=detect mode=train model=s.yaml ...
|
yolo task=detect mode=train model=yolov8n.yaml ...
|
||||||
classify infer s-cls.yaml
|
classify predict yolov8n-cls.yaml
|
||||||
segment val s-seg.yaml
|
segment val yolov8n-seg.yaml
|
||||||
```
|
```
|
||||||
### 2. Python SDK
|
### 2. Python SDK
|
||||||
To use pythonic interface of Ultralytics YOLO model
|
To use pythonic interface of Ultralytics YOLO model
|
||||||
```python
|
```python
|
||||||
import ultralytics
|
|
||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
|
|
||||||
model = YOLO()
|
model = YOLO.new('yolov8n.yaml') # create a new model from scratch
|
||||||
model.new("s-seg.yaml") # automatically detects task type
|
model = YOLO.load('yolov8n.pt') # load a pretrained model (recommended for best training results)
|
||||||
model.load("s-seg.pt") # load checkpoint
|
|
||||||
model.train(data="coco128-segments", epochs=1, lr0=0.01, ...)
|
results = model.train(data='coco128.yaml', epochs=100, imgsz=640, ...)
|
||||||
|
results = model.val()
|
||||||
|
results = model.predict(source='bus.jpg')
|
||||||
|
success = model.export(format='onnx')
|
||||||
```
|
```
|
||||||
If you're looking to modify YOLO for R&D or to build on top of it, refer to [Using Trainer]() Guide on our docs.
|
If you're looking to modify YOLO for R&D or to build on top of it, refer to [Using Trainer]() Guide on our docs.
|
||||||
|
@ -3,45 +3,49 @@ import torch
|
|||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
|
||||||
|
def test_model_init():
|
||||||
|
model = YOLO.new("yolov8n.yaml")
|
||||||
|
model.info()
|
||||||
|
try:
|
||||||
|
YOLO()
|
||||||
|
except Exception:
|
||||||
|
print("Successfully caught constructor assert!")
|
||||||
|
raise Exception("constructor error didn't occur")
|
||||||
|
|
||||||
|
|
||||||
def test_model_forward():
|
def test_model_forward():
|
||||||
model = YOLO()
|
model = YOLO.new("yolov8n.yaml")
|
||||||
model.new("yolov8n.yaml")
|
|
||||||
img = torch.rand(512 * 512 * 3).view(1, 3, 512, 512)
|
img = torch.rand(512 * 512 * 3).view(1, 3, 512, 512)
|
||||||
model.forward(img)
|
model.forward(img)
|
||||||
model(img)
|
model(img)
|
||||||
|
|
||||||
|
|
||||||
def test_model_info():
|
def test_model_info():
|
||||||
model = YOLO()
|
model = YOLO.new("yolov8n.yaml")
|
||||||
model.new("yolov8n.yaml")
|
|
||||||
model.info()
|
model.info()
|
||||||
model.load("best.pt")
|
model = model.load("best.pt")
|
||||||
model.info(verbose=True)
|
model.info(verbose=True)
|
||||||
|
|
||||||
|
|
||||||
def test_model_fuse():
|
def test_model_fuse():
|
||||||
model = YOLO()
|
model = YOLO.new("yolov8n.yaml")
|
||||||
model.new("yolov8n.yaml")
|
|
||||||
model.fuse()
|
model.fuse()
|
||||||
model.load("best.pt")
|
model.load("best.pt")
|
||||||
model.fuse()
|
model.fuse()
|
||||||
|
|
||||||
|
|
||||||
def test_visualize_preds():
|
def test_visualize_preds():
|
||||||
model = YOLO()
|
model = YOLO.load("best.pt")
|
||||||
model.load("best.pt")
|
|
||||||
model.predict(source="ultralytics/assets")
|
model.predict(source="ultralytics/assets")
|
||||||
|
|
||||||
|
|
||||||
def test_val():
|
def test_val():
|
||||||
model = YOLO()
|
model = YOLO.load("best.pt")
|
||||||
model.load("best.pt")
|
|
||||||
model.val(data="coco128.yaml", imgsz=32)
|
model.val(data="coco128.yaml", imgsz=32)
|
||||||
|
|
||||||
|
|
||||||
def test_model_resume():
|
def test_model_resume():
|
||||||
model = YOLO()
|
model = YOLO.new("yolov8n.yaml")
|
||||||
model.new("yolov8n.yaml")
|
|
||||||
model.train(epochs=1, imgsz=32, data="coco128.yaml")
|
model.train(epochs=1, imgsz=32, data="coco128.yaml")
|
||||||
try:
|
try:
|
||||||
model.resume(task="detect")
|
model.resume(task="detect")
|
||||||
@ -50,10 +54,9 @@ def test_model_resume():
|
|||||||
|
|
||||||
|
|
||||||
def test_model_train_pretrained():
|
def test_model_train_pretrained():
|
||||||
model = YOLO()
|
model = YOLO.load("best.pt")
|
||||||
model.load("best.pt")
|
|
||||||
model.train(data="coco128.yaml", epochs=1, imgsz=32)
|
model.train(data="coco128.yaml", epochs=1, imgsz=32)
|
||||||
model.new("yolov8n.yaml")
|
model = model.new("yolov8n.yaml")
|
||||||
model.train(data="coco128.yaml", epochs=1, imgsz=32)
|
model.train(data="coco128.yaml", epochs=1, imgsz=32)
|
||||||
img = torch.rand(512 * 512 * 3).view(1, 3, 512, 512)
|
img = torch.rand(512 * 512 * 3).view(1, 3, 512, 512)
|
||||||
model(img)
|
model(img)
|
||||||
|
@ -5,7 +5,7 @@ from ultralytics import yolo # noqa required for python usage
|
|||||||
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, SegmentationModel, attempt_load_weights
|
from ultralytics.nn.tasks import ClassificationModel, DetectionModel, SegmentationModel, attempt_load_weights
|
||||||
# from ultralytics.yolo.data.utils import check_dataset, check_dataset_yaml
|
# from ultralytics.yolo.data.utils import check_dataset, check_dataset_yaml
|
||||||
from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG
|
from ultralytics.yolo.engine.trainer import DEFAULT_CONFIG
|
||||||
from ultralytics.yolo.utils import LOGGER
|
from ultralytics.yolo.utils import HELP_MSG, LOGGER
|
||||||
from ultralytics.yolo.utils.checks import check_yaml
|
from ultralytics.yolo.utils.checks import check_yaml
|
||||||
from ultralytics.yolo.utils.configs import get_config
|
from ultralytics.yolo.utils.configs import get_config
|
||||||
from ultralytics.yolo.utils.files import yaml_load
|
from ultralytics.yolo.utils.files import yaml_load
|
||||||
@ -28,12 +28,16 @@ class YOLO:
|
|||||||
"""
|
"""
|
||||||
Python interface which emulates a model-like behaviour by wrapping trainers.
|
Python interface which emulates a model-like behaviour by wrapping trainers.
|
||||||
"""
|
"""
|
||||||
|
__init_key = object()
|
||||||
|
|
||||||
def __init__(self, type="v8") -> None:
|
def __init__(self, init_key=None, type="v8") -> None:
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
type (str): Type/version of models to use
|
type (str): Type/version of models to use
|
||||||
"""
|
"""
|
||||||
|
if init_key != YOLO.__init_key:
|
||||||
|
raise Exception(HELP_MSG)
|
||||||
|
|
||||||
self.type = type
|
self.type = type
|
||||||
self.ModelClass = None
|
self.ModelClass = None
|
||||||
self.TrainerClass = None
|
self.TrainerClass = None
|
||||||
@ -44,8 +48,10 @@ class YOLO:
|
|||||||
self.task = None
|
self.task = None
|
||||||
self.ckpt = None
|
self.ckpt = None
|
||||||
self.overrides = {}
|
self.overrides = {}
|
||||||
|
self.init_disabled = False
|
||||||
|
|
||||||
def new(self, cfg: str):
|
@classmethod
|
||||||
|
def new(cls, cfg: str):
|
||||||
"""
|
"""
|
||||||
Initializes a new model and infers the task type from the model definitions
|
Initializes a new model and infers the task type from the model definitions
|
||||||
|
|
||||||
@ -55,12 +61,15 @@ class YOLO:
|
|||||||
cfg = check_yaml(cfg) # check YAML
|
cfg = check_yaml(cfg) # check YAML
|
||||||
with open(cfg, encoding='ascii', errors='ignore') as f:
|
with open(cfg, encoding='ascii', errors='ignore') as f:
|
||||||
cfg = yaml.safe_load(f) # model dict
|
cfg = yaml.safe_load(f) # model dict
|
||||||
self.task = self._guess_task_from_head(cfg["head"][-1][-2])
|
obj = cls(init_key=cls.__init_key)
|
||||||
self.ModelClass, self.TrainerClass, self.ValidatorClass, self.PredictorClass = self._guess_ops_from_task(
|
obj.task = obj._guess_task_from_head(cfg["head"][-1][-2])
|
||||||
self.task)
|
obj.ModelClass, obj.TrainerClass, obj.ValidatorClass, obj.PredictorClass = obj._guess_ops_from_task(obj.task)
|
||||||
self.model = self.ModelClass(cfg) # initialize
|
obj.model = obj.ModelClass(cfg) # initialize
|
||||||
|
|
||||||
def load(self, weights: str):
|
return obj
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load(cls, weights: str):
|
||||||
"""
|
"""
|
||||||
Initializes a new model and infers the task type from the model head
|
Initializes a new model and infers the task type from the model head
|
||||||
|
|
||||||
@ -68,15 +77,18 @@ class YOLO:
|
|||||||
weights (str): model checkpoint to be loaded
|
weights (str): model checkpoint to be loaded
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.ckpt = torch.load(weights, map_location="cpu")
|
obj = cls(init_key=cls.__init_key)
|
||||||
self.task = self.ckpt["train_args"]["task"]
|
obj.ckpt = torch.load(weights, map_location="cpu")
|
||||||
self.overrides = dict(self.ckpt["train_args"])
|
obj.task = obj.ckpt["train_args"]["task"]
|
||||||
self.overrides["device"] = '' # reset device
|
obj.overrides = dict(obj.ckpt["train_args"])
|
||||||
|
obj.overrides["device"] = '' # reset device
|
||||||
LOGGER.info("Device has been reset to ''")
|
LOGGER.info("Device has been reset to ''")
|
||||||
|
|
||||||
self.ModelClass, self.TrainerClass, self.ValidatorClass, self.PredictorClass = self._guess_ops_from_task(
|
obj.ModelClass, obj.TrainerClass, obj.ValidatorClass, obj.PredictorClass = obj._guess_ops_from_task(
|
||||||
task=self.task)
|
task=obj.task)
|
||||||
self.model = attempt_load_weights(weights)
|
obj.model = attempt_load_weights(weights)
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"""
|
"""
|
||||||
|
@ -21,6 +21,32 @@ FONT = 'Arial.ttf' # https://ultralytics.com/assets/Arial.ttf
|
|||||||
VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode
|
VERBOSE = str(os.getenv('YOLOv5_VERBOSE', True)).lower() == 'true' # global verbose mode
|
||||||
TQDM_BAR_FORMAT = '{l_bar}{bar:10}{r_bar}' # tqdm bar format
|
TQDM_BAR_FORMAT = '{l_bar}{bar:10}{r_bar}' # tqdm bar format
|
||||||
LOGGING_NAME = 'yolov5'
|
LOGGING_NAME = 'yolov5'
|
||||||
|
HELP_MSG = \
|
||||||
|
"""
|
||||||
|
Please refer to below Usage examples for help running YOLOv8
|
||||||
|
For help visit Ultralytics Community at https://community.ultralytics.com/
|
||||||
|
Submit bug reports to https//github.com/ultralytics/ultralytics
|
||||||
|
|
||||||
|
Install:
|
||||||
|
pip install ultralytics
|
||||||
|
|
||||||
|
Python usage:
|
||||||
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
model = YOLO.new('yolov8n.yaml') # create a new model from scratch
|
||||||
|
model = YOLO.load('yolov8n.pt') # load a pretrained model (recommended for best training results)
|
||||||
|
results = model.train(data='coco128.yaml')
|
||||||
|
results = model.val()
|
||||||
|
results = model.predict(source='bus.jpg')
|
||||||
|
success = model.export(format='onnx')
|
||||||
|
|
||||||
|
CLI usage:
|
||||||
|
yolo task=detect mode=train model=yolov8n.yaml ...
|
||||||
|
classify predict yolov8n-cls.yaml
|
||||||
|
segment val yolov8n-seg.yaml
|
||||||
|
|
||||||
|
For all arguments see https://github.com/ultralytics/ultralytics/blob/main/ultralytics/yolo/utils/configs/default.yaml
|
||||||
|
"""
|
||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
# torch.set_printoptions(linewidth=320, precision=5, profile='long')
|
# torch.set_printoptions(linewidth=320, precision=5, profile='long')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user