Skip to content

courtvision.trainer

log_wb_image_and_bbox(images, preds, targets, logger, global_step)

summary

Parameters:

Name Type Description Default
images torch.tensor

description

required
preds list[dict[str, torch.Tensor]]

description

required
targets list[dict[str, torch.Tensor]]

description

required
logger wandb.sdk.wandb_run.Run

description

required
global_step int

description

required
Source code in courtvision/trainer.py
 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
def log_wb_image_and_bbox(
    images: torch.tensor,
    preds: list[dict[str, torch.Tensor]],
    targets: list[dict[str, torch.Tensor]],
    logger: wandb.sdk.wandb_run.Run,
    global_step: int,
):
    """_summary_

    Args:
        images (torch.tensor): _description_
        preds (list[dict[str, torch.Tensor]]): _description_
        targets (list[dict[str, torch.Tensor]]): _description_
        logger (wandb.sdk.wandb_run.Run): _description_
        global_step (int): _description_
    """
    images_to_log = []
    for image, pred, target in zip(images, preds, targets):
        image_height, image_width = image.shape[-2:]
        images_to_log.append(
            wandb.Image(
                image.permute(1, 2, 0).cpu().numpy(),
                boxes={
                    "predictions": {
                        "box_data": [
                            {
                                "position": {
                                    "minX": float(x_min) / image_width,
                                    "maxX": float(x_max) / image_width,
                                    "minY": float(y_min) / image_height,
                                    "maxY": float(y_max) / image_height,
                                },
                                "class_id": 1,
                                "box_caption": "ball",
                                "scores": {"score": float(score)},
                            }
                            for (x_min, y_min, x_max, y_max), score in zip(
                                pred["boxes"].cpu().numpy(),
                                pred["scores"].cpu().numpy(),
                                strict=True,
                            )
                        ]
                    },
                    "targets": {
                        "box_data": [
                            {
                                "position": {
                                    "minX": float(x_min) / image_width,
                                    "maxX": float(x_max) / image_width,
                                    "minY": float(y_min) / image_height,
                                    "maxY": float(y_max) / image_height,
                                },
                                "class_id": 1,
                                "box_caption": "ball",
                            }
                            for x_min, y_min, x_max, y_max in target["boxes"]
                            .cpu()
                            .numpy()
                        ],
                    },
                },
            )
        )

    logger.log({"image": images_to_log})