Skip to content

legacy_cli_adaptors

sleap.legacy_cli_adaptors

Adaptors for legacy CLI commands.

Functions:

Name Description
track_command

Track instances in video data using trained SLEAP models.

train_command

Train a SLEAP model with the specified configuration.

track_command(data_path, models, frames, only_labeled_frames, only_suggested_frames, output, video_dataset, video_input_format, video_index, cpu, first_gpu, last_gpu, gpu, max_edge_length_ratio, dist_penalty_weight, batch_size, open_in_gui, peak_threshold, max_instances, tracking_tracker, tracking_max_tracking, tracking_max_tracks, tracking_post_connect_single_breaks, tracking_similarity, tracking_match, tracking_robust, tracking_track_window, tracking_min_new_track_points, tracking_min_match_points, tracking_img_scale, tracking_of_window_size, tracking_of_max_levels)

Track instances in video data using trained SLEAP models.

Source code in sleap/legacy_cli_adaptors.py
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@click.argument("data_path", required=True)
@click.option(
    "-m",
    "--model",
    "models",
    multiple=True,
    help=(
        "Path to trained model directory (with training_config json/yaml). "
        "Multiple models can be specified, each preceded by --model."
    ),
)
@click.option(
    "--frames",
    "frames",
    help=(
        "List of frames to predict when running on a video. Can be specified as a "
        "comma separated list (e.g. 1,2,3) or a range separated by hyphen (e.g., 1-3, "
        "for 1,2,3). If not provided, defaults to predicting on the entire video."
    ),
)
@click.option(
    "--only-labeled-frames",
    "only_labeled_frames",
    is_flag=True,
    help=(
        "Only run inference on user labeled frames when running on labels dataset. "
        "This is useful for generating predictions to compare against ground truth."
    ),
)
@click.option(
    "--only-suggested-frames",
    "only_suggested_frames",
    is_flag=True,
    help=(
        "Only run inference on unlabeled suggested frames when running on labels "
        "dataset. This is useful for generating predictions for initialization during "
        "labeling."
    ),
)
@click.option(
    "-o",
    "--output",
    "output",
    help=(
        "The output filename or directory path to use for the predicted data. If not "
        "provided, defaults to '[data_path].predictions.slp'."
    ),
)
@click.option("--video.dataset", "video_dataset", help="The dataset for HDF5 videos.")
@click.option(
    "--video.input_format",
    "video_input_format",
    help="The input_format for HDF5 videos.",
)
@click.option(
    "--video.index",
    "video_index",
    type=int,
    help=(
        "Integer index of video in .slp file to predict on. To be used with an .slp "
        "path as an alternative to specifying the video path."
    ),
)
@click.option(
    "--cpu",
    "cpu",
    is_flag=True,
    help="Run inference only on CPU. If not specified, will use available GPU.",
)
@click.option(
    "--first-gpu",
    "first_gpu",
    is_flag=True,
    help="Run inference on the first GPU, if available.",
)
@click.option(
    "--last-gpu",
    "last_gpu",
    is_flag=True,
    help="Run inference on the last GPU, if available.",
)
@click.option(
    "--gpu",
    "gpu",
    help=(
        "Run training on the i-th GPU on the system. If 'auto', run on the GPU with "
        "the highest percentage of available memory."
    ),
)
@click.option(
    "--max_edge_length_ratio",
    "max_edge_length_ratio",
    type=float,
    help=(
        "The maximum expected length of a connected pair of points as a fraction of "
        "the "
        "image size. Candidate connections longer than this length will be penalized "
        "during matching. Only applies to bottom-up (PAF) models."
    ),
)
@click.option(
    "--dist_penalty_weight",
    "dist_penalty_weight",
    type=float,
    help=(
        "A coefficient to scale weight of the distance penalty. Set to values greater "
        "than 1.0 to enforce the distance penalty more strictly. Only applies to "
        "bottom-up (PAF) models."
    ),
)
@click.option(
    "--batch_size",
    "batch_size",
    type=int,
    help=(
        "Number of frames to predict at a time. Larger values result in faster "
        "inference speeds, but require more memory."
    ),
)
@click.option(
    "--open-in-gui",
    "open_in_gui",
    is_flag=True,
    help="Open the resulting predictions in the GUI when finished.",
)
@click.option(
    "--peak_threshold",
    "peak_threshold",
    type=float,
    help="Minimum confidence map value to consider a peak as valid.",
)
@click.option(
    "-n",
    "--max_instances",
    "max_instances",
    type=int,
    help=(
        "Limit maximum number of instances in multi-instance models. Not available for "
        "ID models. Defaults to None."
    ),
)
@click.option(
    "--tracking.tracker",
    "tracking_tracker",
    help="Options: simple, flow, simplemaxtracks, flowmaxtracks, None (default: None)",
)
@click.option(
    "--tracking.max_tracking",
    "tracking_max_tracking",
    type=int,
    help="If 1 (True) then the tracker will cap the max number of tracks. 0 (False)",
)
@click.option(
    "--tracking.max_tracks",
    "tracking_max_tracks",
    type=int,
    help="Maximum number of tracks to be tracked by the tracker. (default: None)",
)
@click.option(
    "--tracking.post_connect_single_breaks",
    "tracking_post_connect_single_breaks",
    type=int,
    help=(
        "If non-zero and target_instance_count is also non-zero, then connect track "
        "breaks when exactly one track is lost and exactly one track is spawned in "
        "frame. (default: 0)"
    ),
)
@click.option(
    "--tracking.similarity",
    "tracking_similarity",
    help=(
        "Options: instance, normalized_instance, object_keypoint, centroid, iou "
        "(default: instance)"
    ),
)
@click.option(
    "--tracking.match",
    "tracking_match",
    help="Options: hungarian, greedy (default: greedy)",
)
@click.option(
    "--tracking.robust",
    "tracking_robust",
    type=float,
    help=(
        "Robust quantile of similarity score for instance matching. If equal to 1, "
        "keep the max similarity score (non-robust). (default: 1)"
    ),
)
@click.option(
    "--tracking.track_window",
    "tracking_track_window",
    type=int,
    help="How many frames back to look for matches (default: 5)",
)
@click.option(
    "--tracking.min_new_track_points",
    "tracking_min_new_track_points",
    type=int,
    help="Minimum number of instance points for spawning new track (default: 0)",
)
@click.option(
    "--tracking.min_match_points",
    "tracking_min_match_points",
    type=int,
    help="Minimum points for match candidates (default: 0)",
)
@click.option(
    "--tracking.img_scale",
    "tracking_img_scale",
    type=float,
    help="For optical-flow: Image scale (default: 1.0)",
)
@click.option(
    "--tracking.of_window_size",
    "tracking_of_window_size",
    type=int,
    help=(
        "For optical-flow: Optical flow window size to consider at each pyramid "
        "(default: 21)"
    ),
)
@click.option(
    "--tracking.of_max_levels",
    "tracking_of_max_levels",
    type=int,
    help="For optical-flow: Number of pyramid scale levels to consider (default: 3)",
)
def track_command(
    data_path,
    models,
    frames,
    only_labeled_frames,
    only_suggested_frames,
    output,
    video_dataset,
    video_input_format,
    video_index,
    cpu,
    first_gpu,
    last_gpu,
    gpu,
    max_edge_length_ratio,
    dist_penalty_weight,
    batch_size,
    open_in_gui,
    peak_threshold,
    max_instances,
    tracking_tracker,
    tracking_max_tracking,
    tracking_max_tracks,
    tracking_post_connect_single_breaks,
    tracking_similarity,
    tracking_match,
    tracking_robust,
    tracking_track_window,
    tracking_min_new_track_points,
    tracking_min_match_points,
    tracking_img_scale,
    tracking_of_window_size,
    tracking_of_max_levels,
):
    """Track instances in video data using trained SLEAP models."""
    import torch

    # Build kwargs for the tracking function
    kwargs = {}

    if models is not None:
        kwargs["model_paths"] = models
    if frames is not None:
        kwargs["frames"] = frames
    if only_labeled_frames is not None and only_labeled_frames:
        kwargs["only_labeled_frames"] = True
    if only_suggested_frames is not None and only_suggested_frames:
        kwargs["only_suggested_frames"] = True
    kwargs["output_path"] = output
    if output is None:
        kwargs["output_path"] = f"{data_path}.predictions.slp"
    if video_dataset is not None:
        kwargs["video_dataset"] = video_dataset
    if video_input_format is not None:
        kwargs["video_input_format"] = video_input_format
    if video_index is not None:
        kwargs["video_index"] = video_index
    if cpu is not None and cpu:
        kwargs["device"] = "cpu"
    if torch.cuda.is_available():
        if first_gpu:
            kwargs["device"] = "cuda:0"
        if last_gpu:
            n_gpus = torch.cuda.device_count()
            kwargs["device"] = f"cuda:{n_gpus - 1}"
        if gpu:
            kwargs["device"] = f"cuda:{gpu}" if gpu != "auto" else "cuda"
    if max_edge_length_ratio is not None:
        kwargs["max_edge_length_ratio"] = max_edge_length_ratio
    if dist_penalty_weight is not None:
        kwargs["dist_penalty_weight"] = dist_penalty_weight
    if batch_size is not None:
        kwargs["batch_size"] = batch_size
    # if open_in_gui:
    #     kwargs['open_in_gui'] = True #TODO
    if peak_threshold is not None:
        kwargs["peak_threshold"] = peak_threshold
    if max_instances is not None:
        kwargs["max_instances"] = max_instances

    if tracking_tracker:
        if "flow" in tracking_tracker:
            kwargs["use_flow"] = True

    # Check max_tracking flag int value for True/False
    if tracking_max_tracking is not None and tracking_max_tracking == 1:
        kwargs["candidates_method"] = "local_queues"
        kwargs["max_tracks"] = tracking_max_tracks

    if tracking_similarity is not None:
        if tracking_similarity == "oks":
            kwargs["features"] = "keypoints"
            kwargs["scoring_method"] = "oks"
        elif tracking_similarity == "centroids":
            kwargs["features"] = "centroids"
            kwargs["scoring_method"] = "euclidean_dist"
        elif tracking_similarity == "iou":
            kwargs["features"] = "bboxes"
            kwargs["scoring_method"] = "iou"
    if (
        tracking_post_connect_single_breaks is not None
        and tracking_post_connect_single_breaks
    ):
        kwargs["post_connect_single_breaks"] = tracking_post_connect_single_breaks
    if tracking_match is not None:
        kwargs["track_matching_method"] = tracking_match
    if tracking_robust is not None:
        kwargs["robust_best_instance"] = tracking_robust
    if tracking_track_window is not None:
        kwargs["tracking_window_size"] = tracking_track_window
    if tracking_min_new_track_points is not None:
        kwargs["min_new_track_points"] = tracking_min_new_track_points
    if tracking_min_match_points is not None:
        kwargs["min_match_points"] = tracking_min_match_points
    if tracking_img_scale is not None:
        kwargs["of_img_scale"] = tracking_img_scale
    if tracking_of_window_size is not None:
        kwargs["of_window_size"] = tracking_of_window_size
    if tracking_of_max_levels is not None:
        kwargs["of_max_levels"] = tracking_of_max_levels

    # # Call the original tracking function with kwargs
    predict(data_path=data_path, **kwargs)

    if open_in_gui:
        import subprocess

        # Launch SLEAP GUI with the output file
        subprocess.run(["sleap-label", str(kwargs.get("output_path", ""))])

train_command(training_job_path, labels_path, video_paths, val_labels, test_labels, base_checkpoint, save_viz, keep_viz, zmq, run_name, prefix, suffix, cpu, first_gpu, last_gpu, gpu)

Train a SLEAP model with the specified configuration.

Source code in sleap/legacy_cli_adaptors.py
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@click.command(context_settings={"help_option_names": ["-h", "--help"]})
@click.argument("training_job_path", required=True)
@click.argument("labels_path", required=False)
@click.option(
    "--video-paths",
    "video_paths",
    help=(
        "List of paths for finding videos in case paths inside labels file are not "
        "accessible."
    ),
)
@click.option(
    "--val_labels",
    "--val",
    "val_labels",
    help=(
        "Path to labels file to use for validation. If specified, overrides the path "
        "specified in the training job config."
    ),
)
@click.option(
    "--test_labels",
    "--test",
    "test_labels",
    help=(
        "Path to labels file to use for test. If specified, overrides the path "
        "specified in the training job config."
    ),
)
@click.option(
    "--base_checkpoint",
    "base_checkpoint",
    help=(
        "Path to base checkpoint (directory containing best_model.h5) to resume "
        "training from."
    ),
)
@click.option(
    "--save_viz",
    "save_viz",
    is_flag=True,
    help=(
        "Enable saving of prediction visualizations to the run folder if not already "
        "specified in the training job config."
    ),
)
@click.option(
    "--keep_viz",
    "keep_viz",
    is_flag=True,
    help=(
        "Keep prediction visualization images in the run folder after training if "
        "--save_viz is enabled."
    ),
)
@click.option(
    "--zmq",
    "zmq",
    is_flag=True,
    help=(
        "Enable ZMQ logging (for GUI) if not already specified in the training job "
        "config."
    ),
)
@click.option(
    "--run_name",
    "run_name",
    help=("Run name to use when saving file, overrides other run name settings."),
)
@click.option("--prefix", "prefix", help="Prefix to prepend to run name.")
@click.option("--suffix", "suffix", help="Suffix to append to run name.")
@click.option(
    "--cpu",
    "cpu",
    is_flag=True,
    help="Run training only on CPU. If not specified, will use available GPU.",
)
@click.option(
    "--first-gpu",
    "first_gpu",
    is_flag=True,
    help="Run training on the first GPU, if available.",
)
@click.option(
    "--last-gpu",
    "last_gpu",
    is_flag=True,
    help="Run training on the last GPU, if available.",
)
@click.option(
    "--gpu",
    "gpu",
    help=(
        "Run training on the i-th GPU on the system. If 'auto', run on the GPU with "
        "the highest percentage of available memory."
    ),
)
def train_command(
    training_job_path,
    labels_path,
    video_paths,
    val_labels,
    test_labels,
    base_checkpoint,
    save_viz,
    keep_viz,
    zmq,
    run_name,
    prefix,
    suffix,
    cpu,
    first_gpu,
    last_gpu,
    gpu,
):
    """Train a SLEAP model with the specified configuration."""
    # Convert click arguments to the format expected by the training function
    if training_job_path.endswith(".json"):
        config = TrainingJobConfig.load_sleap_config(training_job_path)
    elif training_job_path.endswith((".yaml", ".yml")):
        config = OmegaConf.load(training_job_path)

    if labels_path is not None:
        config.data_config.train_labels_path = [labels_path]

    if video_paths:
        video_paths = video_paths.split(",")
    if val_labels is not None:
        config.data_config.val_labels_path = [val_labels]
    if test_labels is not None:
        config.data_config.test_file_path = [test_labels]
    if base_checkpoint is not None:
        config.model_config.pretrained_backbone_weights = base_checkpoint
        config.model_config.pretrained_head_weights = base_checkpoint
    if save_viz is not None and save_viz:
        config.trainer_config.visualize_preds_during_training = True
    if keep_viz is not None and keep_viz:
        config.trainer_config.keep_viz = True
    if zmq is not None and not zmq:
        config.trainer_config.zmq.publish_port = None
        config.trainer_config.zmq.controller_port = None
    if run_name is not None:
        config.trainer_config.run_name = run_name
    if prefix is not None:
        config.trainer_config.run_name = prefix + config.trainer_config.run_name
    if suffix is not None:
        config.trainer_config.run_name = config.trainer_config.run_name + suffix
    if cpu is not None and cpu:
        config.trainer_config.trainer_accelerator = "cpu"
    if first_gpu:
        config.trainer_config.trainer_accelerator = "gpu"
        config.trainer_config.trainer_device_indices = [0]
    if last_gpu:
        config.trainer_config.trainer_accelerator = "gpu"
        config.trainer_config.trainer_device_indices = [-1]
    if gpu:
        config.trainer_config.trainer_accelerator = "gpu"
        config.trainer_config.trainer_device_indices = [gpu] if gpu != "auto" else None

    # Call the original training function with the arguments
    if video_paths is not None:
        train = load_labels_video_search(labels_path, video_paths)
        val = (
            load_labels_video_search(val_labels, video_paths)
            if val_labels is not None
            else None
        )
    else:
        train = sio.load_slp(labels_path)
        val = sio.load_slp(val_labels) if val_labels is not None else None

    start_train_time = time.time()
    start_timestamp = str(datetime.now())
    logger.info(f"Started training at: {start_timestamp}")

    trainer = ModelTrainer.get_model_trainer_from_config(
        config=config,
        train_labels=[train],
        val_labels=[val] if val is not None else None,
    )
    trainer.train()

    finish_timestamp = str(datetime.now())
    total_elapsed = time.time() - start_train_time
    logger.info(f"Finished training at: {finish_timestamp}")
    logger.info(f"Total training time: {total_elapsed} secs")

    rank = trainer.trainer.global_rank if trainer.trainer is not None else -1

    logger.info(f"Training Config: {OmegaConf.to_yaml(trainer.config)}")

    if rank in [0, -1]:
        # run inference on val dataset
        if trainer.config.trainer_config.save_ckpt:
            data_paths = {}
            for index, path in enumerate(trainer.config.data_config.train_labels_path):
                ckpt_path = (
                    Path(trainer.config.trainer_config.ckpt_dir)
                    / trainer.config.trainer_config.run_name
                ).as_posix()
                logger.info(f"Training labels path for index {index}: {ckpt_path}")
                data_paths[f"train_{index}"] = (
                    Path(trainer.config.trainer_config.ckpt_dir)
                    / trainer.config.trainer_config.run_name
                    / f"labels_train_gt_{index}.slp"
                ).as_posix()
                data_paths[f"val_{index}"] = (
                    Path(trainer.config.trainer_config.ckpt_dir)
                    / trainer.config.trainer_config.run_name
                    / f"labels_val_gt_{index}.slp"
                ).as_posix()

            if (
                OmegaConf.select(config, "data_config.test_file_path", default=None)
                is not None
            ):
                data_paths["test"] = config.data_config.test_file_path

            for d_name, path in data_paths.items():
                labels = sio.load_slp(path)

                pred_labels = predict(
                    data_path=path,
                    model_paths=[
                        Path(trainer.config.trainer_config.ckpt_dir)
                        / trainer.config.trainer_config.run_name
                    ],
                    peak_threshold=0.2,
                    make_labels=True,
                    device=trainer.trainer.strategy.root_device,
                    output_path=Path(trainer.config.trainer_config.ckpt_dir)
                    / trainer.config.trainer_config.run_name
                    / f"pred_{d_name}.slp",
                    ensure_rgb=config.data_config.preprocessing.ensure_rgb,
                    ensure_grayscale=config.data_config.preprocessing.ensure_grayscale,
                )

                if not len(pred_labels):
                    logger.info(
                        f"Skipping eval on `{d_name}` dataset as there are no labeled "
                        f"frames..."
                    )
                    continue  # skip if there are no labeled frames

                evaluator = Evaluator(
                    ground_truth_instances=labels, predicted_instances=pred_labels
                )
                metrics = evaluator.evaluate()
                np.savez(
                    (
                        Path(trainer.config.trainer_config.ckpt_dir)
                        / trainer.config.trainer_config.run_name
                        / f"{d_name}_pred_metrics.npz"
                    ).as_posix(),
                    **metrics,
                )

                logger.info(f"---------Evaluation on `{d_name}` dataset---------")
                logger.info(f"OKS mAP: {metrics['voc_metrics']['oks_voc.mAP']}")
                logger.info(f"Average distance: {metrics['distance_metrics']['avg']}")
                logger.info(f"p90 dist: {metrics['distance_metrics']['p90']}")
                logger.info(f"p50 dist: {metrics['distance_metrics']['p50']}")