Skip to content

Hub

Bases: Hub

new(name, task=None, model_type=None, model_size=None, categories=None, root_dir=None, *args, **kwargs) classmethod

Create Ultralytics Hub.

Parameters:

Name Type Description Default
name str

Hub name

required
task str

Task Name. See UltralyticsHub.TASKS. Defaults to None.

None
model_type str

Model Type. See UltralyticsHub.MODEL_TYPES. Defaults to None.

None
model_size str

Model Size. See UltralyticsHub.MODEL_SIZES. Defaults to None.

None
categories Union[list[dict], list]

class dictionary or list. [{"supercategory": "name"}, ] or ["name",].

None
root_dir str

Root directory of hub repository. Defaults to None.

None

artifact_dir: Path property cached

Artifact Directory. This is raw output of each backend.

backend: str property writable

Backend name

best_ckpt_file: Path property cached

Best Checkpoint File

draw_dir: Path property cached

Draw Results Directory

evaluate_file: Path property cached

Evaluate Json File

hub_dir: Path property cached

Hub(Model) Directory

inference_dir: Path property cached

Inference Results Directory

inference_file: Path property cached

Inference Results File

last_ckpt_file: Path property cached

Last Checkpoint File

metric_file: Path property cached

Metric Csv File

model_config_file: Path property cached

Model Config yaml File

model_size: str property writable

Model Size

model_type: str property writable

Model Type

name: str property writable

Hub name

onnx_file: Path property cached

Best Checkpoint File

root_dir: Path property writable

Root Directory

task: str property writable

Task Name

train_config_file: Path property cached

Train Config yaml File

version: str property writable

Version

benchmark(image_size=None, batch_size=16, device='0', half=False, trial=100)

Benchmark Model

Parameters:

Name Type Description Default
image_size Union[int, list[int]]

inference image size. None for same with train_config (recommended).

None
batch_size int

dynamic batch size. Defaults to 16.

16
device str

device. "cpu" or "gpu_id". Defaults to "0".

'0'
half bool

half. Defaults to False.

False
trial int

number of trials. Defaults to 100.

100
Example

hub.benchmark( image_size=640, batch_size=16, device="0", half=False, trial=100, ) { "inference_time": 0.123, "fps": 123.123, "image_size": [640, 640], "batch_size": 16, "device": "0", "cpu_name": "Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz", "gpu_name": "GeForce GTX 1080 Ti", }

Returns:

Name Type Description
dict dict

benchmark result

check_train_sanity()

Check if all essential files are exist.

Returns:

Name Type Description
bool bool

True if all files are exist else False

delete_artifact()

Delete Artifact Directory. It can be trained again.

delete_hub()

Delete all artifacts of Hub. Hub name can be used again.

evaluate(dataset, dataset_root_dir=None, set_name='test', batch_size=4, image_size=None, letter_box=None, confidence_threshold=0.25, iou_threshold=0.5, half=False, workers=2, device='0', draw=False, hold=True)

Start Evaluate

Parameters:

Name Type Description Default
dataset Union[Dataset, str]

Waffle Dataset object or path or name.

required
dataset_root_dir str

Waffle Dataset root directory. Defaults to None.

None
batch_size int

batch size. Defaults to 4.

4
image_size Union[int, list[int]]

image size. Defaults to None.

None
letter_box bool

letter box. Defaults to None.

None
confidence_threshold float

confidence threshold. Defaults to 0.25.

0.25
iou_threshold float

iou threshold. Defaults to 0.5.

0.5
half bool

half. Defaults to False.

False
workers int

workers. Defaults to 2.

2
device str

device. Defaults to "0".

'0'
draw bool

draw. Defaults to False.

False
hold bool

hold. Defaults to True.

True

Raises:

Type Description
FileNotFoundError

if can not detect appropriate dataset.

e

something gone wrong with ultralytics

Examples:

>>> evaluate_result = hub.evaluate(
        dataset=detection_dataset,
        batch_size=4,
        image_size=640,
        letterbox=False,
        confidence_threshold=0.25,
        iou_threshold=0.5,
        workers=4,
        device="0",
    )
# or you can use train option by passing None
>>> evaluate_result = hub.evaluate(
        ...
        image_size=None,  # use train option
        letterbox=None,  # use train option
        ...
    )
>>> evaluate_result.metrics
[{"tag": "mAP", "value": 0.1}, ...]

Returns:

Name Type Description
EvaluateResult EvaluateResult

evaluate result

export(image_size=None, batch_size=16, opset_version=11, half=False, device='0', hold=True)

Export Model

Parameters:

Name Type Description Default
image_size Union[int, list[int]]

inference image size. None for same with train_config (recommended).

None
batch_size int

dynamic batch size. Defaults to 16.

16
opset_version int

onnx opset version. Defaults to 11.

11
half bool

half. Defaults to False.

False
device str

device. "cpu" or "gpu_id". Defaults to "0".

'0'
hold bool

hold or not. If True then it holds until task finished. If False then return Inferece Callback and run in background. Defaults to True.

True
Example

export_result = hub.export( image_size=640, batch_size=16, opset_version=11, )

or simply use train option by passing None

export_result = hub.export( ..., image_size=None, # use train option ... ) export_result.export_file hubs/my_hub/weights/model.onnx

Returns:

Name Type Description
ExportResult ExportResult

export result

from_model_config(name, model_config_file, root_dir=None) classmethod

Create new Hub with model config.

Parameters:

Name Type Description Default
name str

hub name.

required
model_config_file str

model config yaml file.

required
root_dir str

hub root directory. Defaults to None.

None

Returns:

Name Type Description
Hub Hub

New Hub instance

get_available_backends() classmethod

Get available backends

Returns:

Type Description
list[str]

list[str]: Available backends

get_available_model_sizes(backend=None, task=None, model_type=None) classmethod

Get available model sizes

Parameters:

Name Type Description Default
backend str

Backend name

None
task str

Task name

None
model_type str

Model type

None

Raises:

Type Description
ModuleNotFoundError

If backend is not supported

Returns:

Type Description
list[str]

list[str]: Available model sizes

get_available_model_types(backend=None, task=None) classmethod

Get available model types

Parameters:

Name Type Description Default
backend str

Backend name

None
task str

Task name

None

Raises:

Type Description
ModuleNotFoundError

If backend is not supported

Returns:

Type Description
list[str]

list[str]: Available model types

get_available_tasks(backend=None) classmethod

Get available tasks

Parameters:

Name Type Description Default
backend str

Backend name

None

Raises:

Type Description
ModuleNotFoundError

If backend is not supported

Returns:

Type Description
list[str]

list[str]: Available tasks

get_default_train_params(backend=None, task=None, model_type=None, model_size=None) classmethod

Get default train params

Parameters:

Name Type Description Default
backend str

Backend name

None
task str

Task name

None
model_type str

Model type

None
model_size str

Model size

None

Raises:

Type Description
ModuleNotFoundError

If backend is not supported

Returns:

Name Type Description
dict dict

Default train params

get_evaluate_result()

Get evaluate result from evaluate file.

Example

hub.get_evaluate_result() [ { "tag": "mAP", "value": 0.5, }, ]

Returns:

Name Type Description
dict list[dict]

evaluate result

get_hub_class(backend=None) classmethod

Get hub class

Parameters:

Name Type Description Default
backend str

Backend name

None

Raises:

Type Description
ModuleNotFoundError

If backend is not supported

Returns:

Name Type Description
Hub Hub

Backend hub Class

get_hub_list(root_dir=None) classmethod

Get hub name list in root_dir.

Parameters:

Name Type Description Default
root_dir str

hub root directory. Defaults to None.

None

Returns:

Type Description
list[str]

list[str]: hub name list

get_image_loader()

Get image loader function.

Returns:

Type Description
tuple[torch.Tensor, ImageInfo]

tuple[torch.Tensor, ImageInfo]: input transform function

Example

transform = hub.get_image_loader() image, image_info = transform("path/to/image.jpg") model = hub.get_model() output = model(image.unsqueeze(0))

get_inference_result()

Get inference result from inference file.

Example

hub.get_inference_result() [ { "id": "00000001", "category": "person", "bbox": [0.1, 0.2, 0.3, 0.4], "score": 0.9, }, ]

Returns:

Type Description
list[dict]

list[dict]: inference result

get_metrics()

Get metrics per epoch from metric file.

Example

hub.get_metrics() [ [ { "tag": "epoch", "value": "1", }, { "tag": "train_loss", "value": "0.0012", } ], ]

Returns:

Type Description
list[list[dict]]

list[dict]: metrics per epoch

get_model_config()

Get model config from model config file.

Returns:

Name Type Description
ModelConfig ModelConfig

model config

get_train_config()

Get train config from train config file.

Returns:

Name Type Description
TrainConfig TrainConfig

train config

inference(source, recursive=True, image_size=None, letter_box=None, batch_size=4, confidence_threshold=0.25, iou_threshold=0.5, half=False, workers=2, device='0', draw=False, hold=True)

Start Inference

Parameters:

Name Type Description Default
source str

source directory

required
recursive bool

recursive. Defaults to True.

True
image_size Union[int, list[int]]

image size. None for using training config. Defaults to None.

None
letter_box bool

letter box. None for using training config. Defaults to None.

None
batch_size int

batch size. Defaults to 4.

4
confidence_threshold float

confidence threshold. Defaults to 0.25.

0.25
iou_threshold float

iou threshold. Defaults to 0.5.

0.5
half bool

half. Defaults to False.

False
workers int

workers. Defaults to 2.

2
device str

device. "cpu" or "gpu_id". Defaults to "0".

'0'
draw bool

draw. Defaults to False.

False
hold bool

hold. Defaults to True.

True

Raises:

Type Description
FileNotFoundError

if can not detect appropriate dataset.

e

something gone wrong with ultralytics

Example

inference_result = hub.inference( source="path/to/images", batch_size=4, image_size=640, letterbox=False, confidence_threshold=0.25, iou_threshold=0.5, workers=4, device="0", draw=True, )

or simply use train option by passing None

inference_result = hub.inference( ... image_size=None, # use train option letterbox=None, # use train option ... ) inference_result.predictions [{"relative/path/to/image/file": [{"category": "1", "bbox": [0, 0, 100, 100], "score": 0.9}, ...]}, ...]

Returns:

Name Type Description
InferenceResult InferenceResult

inference result

load(name, root_dir=None) classmethod

Load Hub by name.

Parameters:

Name Type Description Default
name str

hub name.

required
root_dir str

hub root directory. Defaults to None.

None

Raises:

Type Description
FileNotFoundError

if hub is not exist in root_dir

Returns:

Name Type Description
Hub Hub

Hub instance

new(name, backend=None, task=None, model_type=None, model_size=None, categories=None, root_dir=None, *args, **kwargs) classmethod

Create Hub.

Parameters:

Name Type Description Default
name str

Hub name

required
backend str

Backend name. See Hub.BACKENDS. Defaults to None.

None
task str

Task Name. See Hub.TASKS. Defaults to None.

None
model_type str

Model Type. See Hub.MODEL_TYPES. Defaults to None.

None
model_size str

Model Size. See Hub.MODEL_SIZES. Defaults to None.

None
categories Union[list[dict], list]

class dictionary or list. [{"supercategory": "name"}, ] or ["name",].

None
root_dir str

Root directory of hub repository. Defaults to None.

None

Returns:

Name Type Description
Hub Hub

Hub instance

train(dataset, dataset_root_dir=None, epochs=None, batch_size=None, image_size=None, learning_rate=None, letter_box=None, pretrained_model=None, device='0', workers=2, seed=0, verbose=True, hold=True)

Start Train

Parameters:

Name Type Description Default
dataset Union[Dataset, str]

Waffle Dataset object or path or name.

required
dataset_root_dir str

Waffle Dataset root directory. Defaults to None.

None
epochs int

number of epochs. None to use default. Defaults to None.

None
batch_size int

batch size. None to use default. Defaults to None.

None
image_size Union[int, list[int]]

image size. None to use default. Defaults to None.

None
learning_rate float

learning rate. None to use default. Defaults to None.

None
letter_box bool

letter box. None to use default. Defaults to None.

None
pretrained_model str

pretrained model. None to use default. Defaults to None.

None
device str

"cpu" or "gpu_id" or comma seperated "gpu_ids". Defaults to "0".

'0'
workers int

number of workers. Defaults to 2.

2
seed int

random seed. Defaults to 0.

0
verbose bool

verbose. Defaults to True.

True
hold bool

hold process. Defaults to True.

True

Raises:

Type Description
FileExistsError

if trained artifact exists.

FileNotFoundError

if can not detect appropriate dataset.

ValueError

if can not detect appropriate dataset.

e

something gone wrong with ultralytics

Example

train_result = hub.train( dataset=dataset, epochs=100, batch_size=16, image_size=640, learning_rate=0.001, letterbox=False, device="0", # use gpu 0 # device="0,1,2,3", # use gpu 0,1,2,3 # device="cpu", # use cpu workers=2, seed=123 ) train_result.best_ckpt_file hubs/my_hub/weights/best_ckpt.pt train_result.metrics [[{"tag": "epoch", "value": 1}, {"tag": "train/loss", "value": 0.1}, ...], ...]

Returns:

Name Type Description
TrainResult TrainResult

train result