सामग्री पर जाएं

के लिए संदर्भ ultralytics/utils/callbacks/comet.py

नोट

यह फ़ाइल यहाँ उपलब्ध है https://github.com/ultralytics/ultralytics/बूँद/मुख्य/ultralytics/उपयोगिता/कॉलबैक/comet.py। यदि आप कोई समस्या देखते हैं तो कृपया पुल अनुरोध का योगदान करके इसे ठीक करने में मदद करें 🛠️। 🙏 धन्यवाद !



ultralytics.utils.callbacks.comet._get_comet_mode()

का मोड लौटाता है comet परिवेश चर में सेट करें, यदि सेट नहीं है तो 'ऑनलाइन' पर डिफ़ॉल्ट है।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _get_comet_mode():
    """Returns the mode of comet set in the environment variables, defaults to 'online' if not set."""
    return os.getenv("COMET_MODE", "online")



ultralytics.utils.callbacks.comet._get_comet_model_name()

के लिए मॉडल का नाम देता है Comet पर्यावरण चर से 'COMET_MODEL_NAME' या 'YOLOv8'.

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _get_comet_model_name():
    """Returns the model name for Comet from the environment variable 'COMET_MODEL_NAME' or defaults to 'YOLOv8'."""
    return os.getenv("COMET_MODEL_NAME", "YOLOv8")



ultralytics.utils.callbacks.comet._get_eval_batch_logging_interval()

पर्यावरण चर से मूल्यांकन बैच लॉगिंग अंतराल प्राप्त करें या डिफ़ॉल्ट मान 1 का उपयोग करें।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _get_eval_batch_logging_interval():
    """Get the evaluation batch logging interval from environment variable or use default value 1."""
    return int(os.getenv("COMET_EVAL_BATCH_LOGGING_INTERVAL", 1))



ultralytics.utils.callbacks.comet._get_max_image_predictions_to_log()

परिवेश चर से लॉग करने के लिए अधिकतम संख्या में छवि पूर्वानुमान प्राप्त करें।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _get_max_image_predictions_to_log():
    """Get the maximum number of image predictions to log from the environment variables."""
    return int(os.getenv("COMET_MAX_IMAGE_PREDICTIONS", 100))



ultralytics.utils.callbacks.comet._scale_confidence_score(score)

दिए गए आत्मविश्वास स्कोर को पर्यावरण चर में निर्दिष्ट कारक द्वारा मापता है।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _scale_confidence_score(score):
    """Scales the given confidence score by a factor specified in an environment variable."""
    scale = float(os.getenv("COMET_MAX_CONFIDENCE_SCORE", 100.0))
    return score * scale



ultralytics.utils.callbacks.comet._should_log_confusion_matrix()

निर्धारित करता है कि भ्रम मैट्रिक्स पर्यावरण चर सेटिंग्स के आधार पर लॉग किया जाना चाहिए या नहीं।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _should_log_confusion_matrix():
    """Determines if the confusion matrix should be logged based on the environment variable settings."""
    return os.getenv("COMET_EVAL_LOG_CONFUSION_MATRIX", "false").lower() == "true"



ultralytics.utils.callbacks.comet._should_log_image_predictions()

निर्धारित करता है कि किसी निर्दिष्ट परिवेश चर के आधार पर छवि पूर्वानुमानों को लॉग करना है या नहीं.

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _should_log_image_predictions():
    """Determines whether to log image predictions based on a specified environment variable."""
    return os.getenv("COMET_EVAL_LOG_IMAGE_PREDICTIONS", "true").lower() == "true"



ultralytics.utils.callbacks.comet._get_experiment_type(mode, project_name)

मोड और प्रोजेक्ट नाम के आधार पर एक प्रयोग लौटाएं।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _get_experiment_type(mode, project_name):
    """Return an experiment based on mode and project name."""
    if mode == "offline":
        return comet_ml.OfflineExperiment(project_name=project_name)

    return comet_ml.Experiment(project_name=project_name)



ultralytics.utils.callbacks.comet._create_experiment(args)

यह सुनिश्चित करता है कि प्रयोग ऑब्जेक्ट केवल वितरित प्रशिक्षण के दौरान एक ही प्रक्रिया में बनाया गया है।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
72 73 74 75 76 77 78 79 80 81 8283848586878889909192
def _create_experiment(args):
    """Ensures that the experiment object is only created in a single process during distributed training."""
    if RANK not in (-1, 0):
        return
    try:
        comet_mode = _get_comet_mode()
        _project_name = os.getenv("COMET_PROJECT_NAME", args.project)
        experiment = _get_experiment_type(comet_mode, _project_name)
        experiment.log_parameters(vars(args))
        experiment.log_others(
            {
                "eval_batch_logging_interval": _get_eval_batch_logging_interval(),
                "log_confusion_matrix_on_eval": _should_log_confusion_matrix(),
                "log_image_predictions": _should_log_image_predictions(),
                "max_image_predictions": _get_max_image_predictions_to_log(),
            }
        )
        experiment.log_other("Created from", "yolov8")

    except Exception as e:
        LOGGER.warning(f"WARNING ⚠️ Comet installed but not initialized correctly, not logging this run. {e}")



ultralytics.utils.callbacks.comet._fetch_trainer_metadata(trainer)

के लिए मेटाडेटा देता है YOLO युग और संपत्ति की बचत की स्थिति सहित प्रशिक्षण।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
 95 बांग्लादेश 96 97  98 99 100 101 102 103 104 105  106 107 108
def _fetch_trainer_metadata(trainer):
    """Returns metadata for YOLO training including epoch and asset saving status."""
    curr_epoch = trainer.epoch + 1

    train_num_steps_per_epoch = len(trainer.train_loader.dataset) // trainer.batch_size
    curr_step = curr_epoch * train_num_steps_per_epoch
    final_epoch = curr_epoch == trainer.epochs

    save = trainer.args.save
    save_period = trainer.args.save_period
    save_interval = curr_epoch % save_period == 0
    save_assets = save and save_period > 0 and save_interval and not final_epoch

    return dict(curr_epoch=curr_epoch, curr_step=curr_step, save_assets=save_assets, final_epoch=final_epoch)



ultralytics.utils.callbacks.comet._scale_bounding_box_to_original_image_shape(box, resized_image_shape, original_image_shape, ratio_pad)

YOLOv8 प्रशिक्षण के दौरान छवियों का आकार बदलता है और लेबल मूल्यों को इस आकार के आकार के आधार पर सामान्यीकृत किया जाता है।

यह फ़ंक्शन बाउंडिंग बॉक्स लेबल को मूल छवि आकार में पुनः स्केल करता है।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128129 130
def _scale_bounding_box_to_original_image_shape(box, resized_image_shape, original_image_shape, ratio_pad):
    """
    YOLOv8 resizes images during training and the label values are normalized based on this resized shape.

    This function rescales the bounding box labels to the original image shape.
    """

    resized_image_height, resized_image_width = resized_image_shape

    # Convert normalized xywh format predictions to xyxy in resized scale format
    box = ops.xywhn2xyxy(box, h=resized_image_height, w=resized_image_width)
    # Scale box predictions from resized image scale back to original image scale
    box = ops.scale_boxes(resized_image_shape, box, original_image_shape, ratio_pad)
    # Convert bounding box format from xyxy to xywh for Comet logging
    box = ops.xyxy2xywh(box)
    # Adjust xy center to correspond top-left corner
    box[:2] -= box[2:] / 2
    box = box.tolist()

    return box



ultralytics.utils.callbacks.comet._format_ground_truth_annotations_for_detection(img_idx, image_path, batch, class_name_map=None)

पता लगाने के लिए जमीनी सच्चाई एनोटेशन प्रारूपित करें।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159160
def _format_ground_truth_annotations_for_detection(img_idx, image_path, batch, class_name_map=None):
    """Format ground truth annotations for detection."""
    indices = batch["batch_idx"] == img_idx
    bboxes = batch["bboxes"][indices]
    if len(bboxes) == 0:
        LOGGER.debug(f"COMET WARNING: Image: {image_path} has no bounding boxes labels")
        return None

    cls_labels = batch["cls"][indices].squeeze(1).tolist()
    if class_name_map:
        cls_labels = [str(class_name_map[label]) for label in cls_labels]

    original_image_shape = batch["ori_shape"][img_idx]
    resized_image_shape = batch["resized_shape"][img_idx]
    ratio_pad = batch["ratio_pad"][img_idx]

    data = []
    for box, label in zip(bboxes, cls_labels):
        box = _scale_bounding_box_to_original_image_shape(box, resized_image_shape, original_image_shape, ratio_pad)
        data.append(
            {
                "boxes": [box],
                "label": f"gt_{label}",
                "score": _scale_confidence_score(1.0),
            }
        )

    return {"name": "ground_truth", "data": data}



ultralytics.utils.callbacks.comet._format_prediction_annotations_for_detection(image_path, metadata, class_label_map=None)

प्रारूप YOLO ऑब्जेक्ट डिटेक्शन विज़ुअलाइज़ेशन के लिए भविष्यवाणियां।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178179 180181 182 183
def _format_prediction_annotations_for_detection(image_path, metadata, class_label_map=None):
    """Format YOLO predictions for object detection visualization."""
    stem = image_path.stem
    image_id = int(stem) if stem.isnumeric() else stem

    predictions = metadata.get(image_id)
    if not predictions:
        LOGGER.debug(f"COMET WARNING: Image: {image_path} has no bounding boxes predictions")
        return None

    data = []
    for prediction in predictions:
        boxes = prediction["bbox"]
        score = _scale_confidence_score(prediction["score"])
        cls_label = prediction["category_id"]
        if class_label_map:
            cls_label = str(class_label_map[cls_label])

        data.append({"boxes": [boxes], "label": cls_label, "score": score})

    return {"name": "prediction", "data": data}



ultralytics.utils.callbacks.comet._fetch_annotations(img_idx, image_path, batch, prediction_metadata_map, class_label_map)

जमीनी सच्चाई और भविष्यवाणी एनोटेशन में शामिल हों यदि वे मौजूद हैं।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
186 187 188 189 190 191 192 193 194 195 196 197198
def _fetch_annotations(img_idx, image_path, batch, prediction_metadata_map, class_label_map):
    """Join the ground truth and prediction annotations if they exist."""
    ground_truth_annotations = _format_ground_truth_annotations_for_detection(
        img_idx, image_path, batch, class_label_map
    )
    prediction_annotations = _format_prediction_annotations_for_detection(
        image_path, prediction_metadata_map, class_label_map
    )

    annotations = [
        annotation for annotation in [ground_truth_annotations, prediction_annotations] if annotation is not None
    ]
    return [annotations] if annotations else None



ultralytics.utils.callbacks.comet._create_prediction_metadata_map(model_predictions)

मॉडल पूर्वानुमानों के लिए मेटाडेटा मानचित्र बनाएं और उन्हें छवि आईडी के आधार पर समूहीकृत करें।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
201 202 203 204 205 206 207 208
def _create_prediction_metadata_map(model_predictions):
    """Create metadata map for model predictions by groupings them based on image ID."""
    pred_metadata_map = {}
    for prediction in model_predictions:
        pred_metadata_map.setdefault(prediction["image_id"], [])
        pred_metadata_map[prediction["image_id"]].append(prediction)

    return pred_metadata_map



ultralytics.utils.callbacks.comet._log_confusion_matrix(experiment, trainer, curr_step, curr_epoch)

भ्रम मैट्रिक्स को लॉग इन करें Comet प्रयोग।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
211 212 213 214 215 216 217
def _log_confusion_matrix(experiment, trainer, curr_step, curr_epoch):
    """Log the confusion matrix to Comet experiment."""
    conf_mat = trainer.validator.confusion_matrix.matrix
    names = list(trainer.data["names"].values()) + ["background"]
    experiment.log_confusion_matrix(
        matrix=conf_mat, labels=names, max_categories=len(names), epoch=curr_epoch, step=curr_step
    )



ultralytics.utils.callbacks.comet._log_images(experiment, image_paths, curr_step, annotations=None)

वैकल्पिक एनोटेशन के साथ प्रयोग में छवियों को लॉग करता है।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
220 221 222 223 224 225 226 227 228
def _log_images(experiment, image_paths, curr_step, annotations=None):
    """Logs images to the experiment with optional annotations."""
    if annotations:
        for image_path, annotation in zip(image_paths, annotations):
            experiment.log_image(image_path, name=image_path.stem, step=curr_step, annotations=annotation)

    else:
        for image_path in image_paths:
            experiment.log_image(image_path, name=image_path.stem, step=curr_step)



ultralytics.utils.callbacks.comet._log_image_predictions(experiment, validator, curr_step)

लॉग प्रशिक्षण के दौरान एक एकल छवि के लिए बक्से की भविष्यवाणी की।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260261262 263 264 265 266 267 268 269 270 271 272 273
def _log_image_predictions(experiment, validator, curr_step):
    """Logs predicted boxes for a single image during training."""
    global _comet_image_prediction_count

    task = validator.args.task
    if task not in COMET_SUPPORTED_TASKS:
        return

    jdict = validator.jdict
    if not jdict:
        return

    predictions_metadata_map = _create_prediction_metadata_map(jdict)
    dataloader = validator.dataloader
    class_label_map = validator.names

    batch_logging_interval = _get_eval_batch_logging_interval()
    max_image_predictions = _get_max_image_predictions_to_log()

    for batch_idx, batch in enumerate(dataloader):
        if (batch_idx + 1) % batch_logging_interval != 0:
            continue

        image_paths = batch["im_file"]
        for img_idx, image_path in enumerate(image_paths):
            if _comet_image_prediction_count >= max_image_predictions:
                return

            image_path = Path(image_path)
            annotations = _fetch_annotations(
                img_idx,
                image_path,
                batch,
                predictions_metadata_map,
                class_label_map,
            )
            _log_images(
                experiment,
                [image_path],
                curr_step,
                annotations=annotations,
            )
            _comet_image_prediction_count += 1



ultralytics.utils.callbacks.comet._log_plots(experiment, trainer)

प्रयोग के लिए लॉग, मूल्यांकन भूखंड और लेबल भूखंड।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
def _log_plots(experiment, trainer):
    """Logs evaluation plots and label plots for the experiment."""
    plot_filenames = [trainer.save_dir / f"{plots}.png" for plots in EVALUATION_PLOT_NAMES]
    _log_images(experiment, plot_filenames, None)

    label_plot_filenames = [trainer.save_dir / f"{labels}.jpg" for labels in LABEL_PLOT_NAMES]
    _log_images(experiment, label_plot_filenames, None)



ultralytics.utils.callbacks.comet._log_model(experiment, trainer)

सर्वोत्तम-प्रशिक्षित मॉडल को लॉग इन करें Comet।मिलिलिटर।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
285 286 287 288
def _log_model(experiment, trainer):
    """Log the best-trained model to Comet.ml."""
    model_name = _get_comet_model_name()
    experiment.log_model(model_name, file_or_folder=str(trainer.best), file_name="best.pt", overwrite=True)



ultralytics.utils.callbacks.comet.on_pretrain_routine_start(trainer)

बनाता है या एक की शुरुआत में एक CometML प्रयोग फिर से शुरू YOLO पूर्व प्रशिक्षण दिनचर्या।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
291 292 293 294 295 296
def on_pretrain_routine_start(trainer):
    """Creates or resumes a CometML experiment at the start of a YOLO pre-training routine."""
    experiment = comet_ml.get_global_experiment()
    is_alive = getattr(experiment, "alive", False)
    if not experiment or not is_alive:
        _create_experiment(trainer.args)



ultralytics.utils.callbacks.comet.on_train_epoch_end(trainer)

मेट्रिक्स लॉग करें और प्रशिक्षण युगों के अंत में बैच छवियों को सहेजें।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
299 300 301 302 303 304 305 306 307 308 309310 311312
def on_train_epoch_end(trainer):
    """Log metrics and save batch images at the end of training epochs."""
    experiment = comet_ml.get_global_experiment()
    if not experiment:
        return

    metadata = _fetch_trainer_metadata(trainer)
    curr_epoch = metadata["curr_epoch"]
    curr_step = metadata["curr_step"]

    experiment.log_metrics(trainer.label_loss_items(trainer.tloss, prefix="train"), step=curr_step, epoch=curr_epoch)

    if curr_epoch == 1:
        _log_images(experiment, trainer.save_dir.glob("train_batch*.jpg"), curr_step)



ultralytics.utils.callbacks.comet.on_fit_epoch_end(trainer)

प्रत्येक युग के अंत में मॉडल संपत्ति लॉग करता है।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339340
def on_fit_epoch_end(trainer):
    """Logs model assets at the end of each epoch."""
    experiment = comet_ml.get_global_experiment()
    if not experiment:
        return

    metadata = _fetch_trainer_metadata(trainer)
    curr_epoch = metadata["curr_epoch"]
    curr_step = metadata["curr_step"]
    save_assets = metadata["save_assets"]

    experiment.log_metrics(trainer.metrics, step=curr_step, epoch=curr_epoch)
    experiment.log_metrics(trainer.lr, step=curr_step, epoch=curr_epoch)
    if curr_epoch == 1:
        from ultralytics.utils.torch_utils import model_info_for_loggers

        experiment.log_metrics(model_info_for_loggers(trainer), step=curr_step, epoch=curr_epoch)

    if not save_assets:
        return

    _log_model(experiment, trainer)
    if _should_log_confusion_matrix():
        _log_confusion_matrix(experiment, trainer, curr_step, curr_epoch)
    if _should_log_image_predictions():
        _log_image_predictions(experiment, trainer.validator, curr_step)



ultralytics.utils.callbacks.comet.on_train_end(trainer)

प्रशिक्षण के अंत में ऑपरेशन करें।

में स्रोत कोड ultralytics/utils/callbacks/comet.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360361362 363
def on_train_end(trainer):
    """Perform operations at the end of training."""
    experiment = comet_ml.get_global_experiment()
    if not experiment:
        return

    metadata = _fetch_trainer_metadata(trainer)
    curr_epoch = metadata["curr_epoch"]
    curr_step = metadata["curr_step"]
    plots = trainer.args.plots

    _log_model(experiment, trainer)
    if plots:
        _log_plots(experiment, trainer)

    _log_confusion_matrix(experiment, trainer, curr_step, curr_epoch)
    _log_image_predictions(experiment, trainer.validator, curr_step)
    experiment.end()

    global _comet_image_prediction_count
    _comet_image_prediction_count = 0





2023-11-12 बनाया गया, अपडेट किया गया 2023-11-25
लेखक: ग्लेन-जोचर (3), लाफिंग-क्यू (1)