From acda00ca7ca2afdfa7479c8710c4257fafce7ac7 Mon Sep 17 00:00:00 2001 From: ram-from-tvl Date: Tue, 10 Mar 2026 01:48:49 +0530 Subject: [PATCH 1/3] Fix BFloat16 to numpy conversion error in classification scripts - Convert BFloat16 tensors to float32 before calling .numpy() - Fixes TypeError: Got unsupported ScalarType BFloat16 - Affects classification_cvd.py, classification_sex.py, and classification_vendor.py - Numpy doesn't support BFloat16 directly, requires conversion to float32 first --- cinema/examples/inference/classification_cvd.py | 2 +- cinema/examples/inference/classification_sex.py | 2 +- cinema/examples/inference/classification_vendor.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cinema/examples/inference/classification_cvd.py b/cinema/examples/inference/classification_cvd.py index 417f4b3..da21ba0 100644 --- a/cinema/examples/inference/classification_cvd.py +++ b/cinema/examples/inference/classification_cvd.py @@ -50,7 +50,7 @@ def run(trained_dataset: str, view: str, seed: int, device: torch.device, dtype: with torch.no_grad(), torch.autocast("cuda", dtype=dtype, enabled=torch.cuda.is_available()): logits = model(batch) # (1, n_classes) probs = torch.softmax(logits, dim=1)[0] # (n_classes,) - probs_dict = dict(zip(classes, probs.cpu().numpy(), strict=False)) + probs_dict = dict(zip(classes, probs.cpu().float().numpy(), strict=False)) print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201 print(f"The predicted class is {classes[np.argmax(logits)]}, ground truth should be HCM.") # noqa: T201 print(f"The probabilities are {probs_dict}.") # noqa: T201 diff --git a/cinema/examples/inference/classification_sex.py b/cinema/examples/inference/classification_sex.py index 99459e4..ecedf5c 100644 --- a/cinema/examples/inference/classification_sex.py +++ b/cinema/examples/inference/classification_sex.py @@ -51,7 +51,7 @@ def run(seed: int, device: torch.device, dtype: torch.dtype) -> None: with torch.no_grad(), torch.autocast("cuda", dtype=dtype, enabled=torch.cuda.is_available()): logits = model(batch) # (1, n_classes) probs = torch.softmax(logits, dim=1)[0] # (n_classes,) - probs_dict = dict(zip(classes, probs.cpu().numpy(), strict=False)) + probs_dict = dict(zip(classes, probs.cpu().float().numpy(), strict=False)) print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201 print(f"The predicted class is {classes[np.argmax(logits)]}, ground truth should be Female.") # noqa: T201 print(f"The probabilities are {probs_dict}.") # noqa: T201 diff --git a/cinema/examples/inference/classification_vendor.py b/cinema/examples/inference/classification_vendor.py index df9672a..1bf8f9b 100644 --- a/cinema/examples/inference/classification_vendor.py +++ b/cinema/examples/inference/classification_vendor.py @@ -51,7 +51,7 @@ def run(view: str, seed: int, device: torch.device, dtype: torch.dtype) -> None: with torch.no_grad(), torch.autocast("cuda", dtype=dtype, enabled=torch.cuda.is_available()): logits = model(batch) # (1, n_classes) probs = torch.softmax(logits, dim=1)[0] # (n_classes,) - probs_dict = dict(zip(classes, probs.cpu().numpy(), strict=False)) + probs_dict = dict(zip(classes, probs.cpu().float().numpy(), strict=False)) print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201 print(f"The predicted class is {classes[np.argmax(logits)]}, ground truth should be SIEMENS.") # noqa: T201 print(f"The probabilities are {probs_dict}.") # noqa: T201 From 2b5b33bca809daad33d0467aef1fc0091f745c49 Mon Sep 17 00:00:00 2001 From: ram-from-tvl Date: Tue, 10 Mar 2026 01:46:01 +0530 Subject: [PATCH 2/3] Fix BFloat16 to numpy conversion in landmark_coordinate.py - Add .cpu().float() before .numpy() to handle BFloat16 tensors - Fixes TypeError: Got unsupported ScalarType BFloat16 in landmark detection --- cinema/examples/inference/landmark_coordinate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cinema/examples/inference/landmark_coordinate.py b/cinema/examples/inference/landmark_coordinate.py index 33c1cf3..2bce943 100644 --- a/cinema/examples/inference/landmark_coordinate.py +++ b/cinema/examples/inference/landmark_coordinate.py @@ -35,7 +35,7 @@ def run(view: str, seed: int, device: torch.device, dtype: torch.dtype) -> None: batch = transform({view: torch.from_numpy(images[None, ..., 0, t])}) batch = {k: v[None, ...].to(device=device, dtype=dtype) for k, v in batch.items()} with torch.no_grad(), torch.autocast("cuda", dtype=dtype, enabled=torch.cuda.is_available()): - coords = model(batch)[0].numpy() # (6,) + coords = model(batch)[0].cpu().float().numpy() # (6,) coords *= np.array([w, h, w, h, w, h]) coords = [int(x) for x in coords] coords_list.append(coords) From b533dd947983b4a82c238bc0cf646c2c92dff486 Mon Sep 17 00:00:00 2001 From: ram-from-tvl Date: Sat, 14 Mar 2026 01:27:46 +0530 Subject: [PATCH 3/3] Fix additional BFloat16 conversion issues - Fix np.argmax(logits) in classification scripts - Fix heatmap_soft_argmax output in landmark_heatmap.py - Ensure all tensor to numpy conversions use .cpu().float().numpy() --- cinema/examples/inference/classification_cvd.py | 2 +- cinema/examples/inference/classification_sex.py | 2 +- cinema/examples/inference/classification_vendor.py | 2 +- cinema/examples/inference/landmark_heatmap.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cinema/examples/inference/classification_cvd.py b/cinema/examples/inference/classification_cvd.py index da21ba0..10d7aa3 100644 --- a/cinema/examples/inference/classification_cvd.py +++ b/cinema/examples/inference/classification_cvd.py @@ -52,7 +52,7 @@ def run(trained_dataset: str, view: str, seed: int, device: torch.device, dtype: probs = torch.softmax(logits, dim=1)[0] # (n_classes,) probs_dict = dict(zip(classes, probs.cpu().float().numpy(), strict=False)) print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201 - print(f"The predicted class is {classes[np.argmax(logits)]}, ground truth should be HCM.") # noqa: T201 + print(f"The predicted class is {classes[np.argmax(logits.cpu().float().numpy())]}, ground truth should be HCM.") # noqa: T201 print(f"The probabilities are {probs_dict}.") # noqa: T201 diff --git a/cinema/examples/inference/classification_sex.py b/cinema/examples/inference/classification_sex.py index ecedf5c..5a003d5 100644 --- a/cinema/examples/inference/classification_sex.py +++ b/cinema/examples/inference/classification_sex.py @@ -53,7 +53,7 @@ def run(seed: int, device: torch.device, dtype: torch.dtype) -> None: probs = torch.softmax(logits, dim=1)[0] # (n_classes,) probs_dict = dict(zip(classes, probs.cpu().float().numpy(), strict=False)) print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201 - print(f"The predicted class is {classes[np.argmax(logits)]}, ground truth should be Female.") # noqa: T201 + print(f"The predicted class is {classes[np.argmax(logits.cpu().float().numpy())]}, ground truth should be Female.") # noqa: T201 print(f"The probabilities are {probs_dict}.") # noqa: T201 diff --git a/cinema/examples/inference/classification_vendor.py b/cinema/examples/inference/classification_vendor.py index 1bf8f9b..aa85108 100644 --- a/cinema/examples/inference/classification_vendor.py +++ b/cinema/examples/inference/classification_vendor.py @@ -53,7 +53,7 @@ def run(view: str, seed: int, device: torch.device, dtype: torch.dtype) -> None: probs = torch.softmax(logits, dim=1)[0] # (n_classes,) probs_dict = dict(zip(classes, probs.cpu().float().numpy(), strict=False)) print(f"Using {view} view with model trained on {trained_dataset} dataset with seed {seed}.") # noqa: T201 - print(f"The predicted class is {classes[np.argmax(logits)]}, ground truth should be SIEMENS.") # noqa: T201 + print(f"The predicted class is {classes[np.argmax(logits.cpu().float().numpy())]}, ground truth should be SIEMENS.") # noqa: T201 print(f"The probabilities are {probs_dict}.") # noqa: T201 diff --git a/cinema/examples/inference/landmark_heatmap.py b/cinema/examples/inference/landmark_heatmap.py index e5938ea..f4fef35 100644 --- a/cinema/examples/inference/landmark_heatmap.py +++ b/cinema/examples/inference/landmark_heatmap.py @@ -214,7 +214,7 @@ def run(view: str, seed: int, device: torch.device, dtype: torch.dtype) -> None: logits = model(batch)[view] # (1, 3, x, y) probs = torch.sigmoid(logits) # (1, 3, width, height) probs_list.append(probs[0].detach().to(torch.float32).cpu().numpy()) - coords = heatmap_soft_argmax(probs)[0].numpy() + coords = heatmap_soft_argmax(probs)[0].cpu().float().numpy() coords = [int(x) for x in coords] coords_list.append(coords) probs = np.stack(probs_list, axis=-1) # (3, x, y, t)