<jupyter_start><jupyter_text>Object Detection  Import everything<jupyter_code>import os
import sys

import cv2
import numpy as np

from ote_sdk.configuration.helper import create as create_parameters_from_parameters_schema
from ote_sdk.entities.inference_parameters import InferenceParameters
from ote_sdk.entities.label_schema import LabelSchemaEntity
from ote_sdk.entities.model import ModelEntity
from ote_sdk.entities.resultset import ResultSetEntity
from ote_sdk.entities.subset import Subset
from ote_sdk.entities.task_environment import TaskEnvironment
from ote_sdk.usecases.tasks.interfaces.export_interface import ExportType

from ote_cli.datasets import get_dataset_class
from ote_cli.registry import Registry
from ote_cli.utils.importing import get_impl_class<jupyter_output><empty_output><jupyter_text>Register templates<jupyter_code>templates_dir = '../../external'
registry = Registry(templates_dir)
print(registry)<jupyter_output><empty_output><jupyter_text>Load model template and its hyper parameters<jupyter_code>model_template = registry.get('Custom_Object_Detection_Gen3_ATSS')
hyper_parameters = model_template.hyper_parameters.data<jupyter_output><empty_output><jupyter_text>Get dataset instantiated<jupyter_code>Dataset = get_dataset_class(model_template.task_type)

dataset = Dataset(
    train_subset={'ann_file': '../../data/airport/annotation_faces_train.json',
                  'data_root': '../../data/airport/'},
    val_subset={'ann_file': '../../data/airport/annotation_faces_train.json',
                'data_root': '../../data/airport'}
)
labels_schema = LabelSchemaEntity.from_labels(dataset.get_labels())<jupyter_output><empty_output><jupyter_text>Have a look at existing parameters<jupyter_code>hyper_parameters = create_parameters_from_parameters_schema(hyper_parameters)

for p in hyper_parameters.learning_parameters.parameters:
    print(f'{p}: {getattr(hyper_parameters.learning_parameters, p)}')<jupyter_output><empty_output><jupyter_text>Tweak parameters<jupyter_code>hyper_parameters.learning_parameters.batch_size = 8
hyper_parameters.learning_parameters.num_iters = 5

for p in hyper_parameters.learning_parameters.parameters:
    print(f'{p}: {getattr(hyper_parameters.learning_parameters, p)}')<jupyter_output><empty_output><jupyter_text>Create Task<jupyter_code>Task = get_impl_class(model_template.entrypoints.base)

environment = TaskEnvironment(
    model=None,
    hyper_parameters=hyper_parameters,
    label_schema=labels_schema,
    model_template=model_template)
        
task = Task(task_environment=environment)<jupyter_output><empty_output><jupyter_text>Run training<jupyter_code>output_model = ModelEntity(
    dataset,
    environment.get_model_configuration(),
)

task.train(dataset, output_model)<jupyter_output><empty_output><jupyter_text>Evaluate quality metric<jupyter_code>validation_dataset = dataset.get_subset(Subset.VALIDATION)
predicted_validation_dataset = task.infer(
    validation_dataset.with_empty_annotations(),
    InferenceParameters(is_evaluation=True))

resultset = ResultSetEntity(
    model=output_model,
    ground_truth_dataset=validation_dataset,
    prediction_dataset=predicted_validation_dataset,
)
task.evaluate(resultset)
print(resultset.performance)<jupyter_output><empty_output><jupyter_text>Export model to OpenVINO format<jupyter_code>exported_model = ModelEntity(
    dataset,
    environment.get_model_configuration(),
)
task.export(ExportType.OPENVINO, exported_model)<jupyter_output><empty_output><jupyter_text>Evaluate the exported model<jupyter_code>environment.model = exported_model
ov_task = get_impl_class(model_template.entrypoints.openvino)(environment)
predicted_validation_dataset = ov_task.infer(
    validation_dataset.with_empty_annotations(),
    InferenceParameters(is_evaluation=True))
resultset = ResultSetEntity(
    model=output_model,
    ground_truth_dataset=validation_dataset,
    prediction_dataset=predicted_validation_dataset,
)
ov_task.evaluate(resultset)
print(resultset.performance)<jupyter_output><empty_output><jupyter_text>Draw bounding boxes<jupyter_code>import IPython
import PIL

for predictions, item in zip(predicted_validation_dataset, validation_dataset.with_empty_annotations()):
    image = item.numpy.astype(np.uint8)
    for box in predictions.annotation_scene.shapes:
        x1 = int(box.x1 * image.shape[1])
        x2 = int(box.x2 * image.shape[1])
        y1 = int(box.y1 * image.shape[0])
        y2 = int(box.y2 * image.shape[0])
        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 3)  
    IPython.display.display(PIL.Image.fromarray(image))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09575156351089066, 'fraction_numerical': 0.006038386888074186, 'mean_word_length': 7.356756756756757, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '162739', 'license': 'Apache-2.0', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1465, 'n_words': 207, 'path': 'ote_cli/notebooks/train.ipynb', 'repo_name': 'bes-dev/training_extensions', 'size': '4637'}