From 3dff3351b905af40c115cc0ee1d2b4ce9663cbe4 Mon Sep 17 00:00:00 2001 From: Lorenzo Mammana Date: Mon, 8 Jul 2024 11:42:59 +0200 Subject: [PATCH 1/4] fix: Fix wrong dtype used for evaluation --- quadra/tasks/anomaly.py | 5 +++-- quadra/tasks/classification.py | 2 +- quadra/tasks/segmentation.py | 5 +---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/quadra/tasks/anomaly.py b/quadra/tasks/anomaly.py index ec617cd9..366db113 100644 --- a/quadra/tasks/anomaly.py +++ b/quadra/tasks/anomaly.py @@ -386,10 +386,11 @@ def test(self) -> None: batch_labels = batch_item["label"] image_labels.extend(batch_labels.tolist()) image_paths.extend(batch_item["image_path"]) + batch_images = batch_images.to(self.device).to(self.deployment_model.model_dtype) if self.model_data.get("anomaly_method") == "efficientad": - model_output = self.deployment_model(batch_images.to(self.device), None) + model_output = self.deployment_model(batch_images, None) else: - model_output = self.deployment_model(batch_images.to(self.device)) + model_output = self.deployment_model(batch_images) anomaly_map, anomaly_score = model_output[0], model_output[1] anomaly_map = anomaly_map.cpu() anomaly_score = anomaly_score.cpu() diff --git a/quadra/tasks/classification.py b/quadra/tasks/classification.py index eb05fb52..a43c973b 100644 --- a/quadra/tasks/classification.py +++ b/quadra/tasks/classification.py @@ -1169,7 +1169,7 @@ def test(self) -> None: with torch.set_grad_enabled(self.gradcam): for batch_item in tqdm(test_dataloader): im, target = batch_item - im = im.to(self.device).detach() + im = im.to(self.device).to(self.deployment_model.model_dtype).detach() if self.gradcam: # When gradcam is used we need to remove gradients diff --git a/quadra/tasks/segmentation.py b/quadra/tasks/segmentation.py index 327ba620..3b4ec476 100644 --- a/quadra/tasks/segmentation.py +++ b/quadra/tasks/segmentation.py @@ -346,10 +346,7 @@ def test(self) -> None: image_list, mask_list, mask_pred_list, label_list = [], [], [], [] for batch in dataloader: images, masks, labels = batch - images = images.to(self.device) - # TODO: This can be problematic for the future considering bfloat16 or float16-true. - if "16" in str(self.deployment_model.model_dtype): - images = images.half() + images = images.to(self.device).to(self.deployment_model.model_dtype) if len(masks.shape) == 3: # BxHxW -> Bx1xHxW masks = masks.unsqueeze(1) with torch.no_grad(): From 307bff501e4c8f779fc226064a767cee6b323235 Mon Sep 17 00:00:00 2001 From: Lorenzo Mammana Date: Mon, 8 Jul 2024 11:45:36 +0200 Subject: [PATCH 2/4] build: Upgrade package version --- pyproject.toml | 2 +- quadra/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 32769a62..0fd7fe3b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "quadra" -version = "2.1.11" +version = "2.1.12" description = "Deep Learning experiment orchestration library" authors = [ "Federico Belotti ", diff --git a/quadra/__init__.py b/quadra/__init__.py index f39f08a9..67acf36f 100644 --- a/quadra/__init__.py +++ b/quadra/__init__.py @@ -1,4 +1,4 @@ -__version__ = "2.1.11" +__version__ = "2.1.12" def get_version(): From 9323a11a73c152cb9eb4d8a19f529a7bfff51f64 Mon Sep 17 00:00:00 2001 From: Lorenzo Mammana Date: Mon, 8 Jul 2024 11:46:37 +0200 Subject: [PATCH 3/4] docs: Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb283378..122eea12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ # Changelog All notable changes to this project will be documented in this file. +### [2.1.12] + +#### Fixed + +- Fix wrong dtype used when evaluating finetuning or anomaly models trained in fp16 precision + ### [2.1.11] #### Fixed From 33fa210e2fd84c30f9171bdda1317d074c01c30f Mon Sep 17 00:00:00 2001 From: Lorenzo Mammana Date: Mon, 8 Jul 2024 12:21:19 +0200 Subject: [PATCH 4/4] refactor: Improve code --- quadra/tasks/anomaly.py | 2 +- quadra/tasks/classification.py | 2 +- quadra/tasks/segmentation.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/quadra/tasks/anomaly.py b/quadra/tasks/anomaly.py index 366db113..97f3431a 100644 --- a/quadra/tasks/anomaly.py +++ b/quadra/tasks/anomaly.py @@ -386,7 +386,7 @@ def test(self) -> None: batch_labels = batch_item["label"] image_labels.extend(batch_labels.tolist()) image_paths.extend(batch_item["image_path"]) - batch_images = batch_images.to(self.device).to(self.deployment_model.model_dtype) + batch_images = batch_images.to(device=self.device, dtype=self.deployment_model.model_dtype) if self.model_data.get("anomaly_method") == "efficientad": model_output = self.deployment_model(batch_images, None) else: diff --git a/quadra/tasks/classification.py b/quadra/tasks/classification.py index a43c973b..e885c2c6 100644 --- a/quadra/tasks/classification.py +++ b/quadra/tasks/classification.py @@ -1169,7 +1169,7 @@ def test(self) -> None: with torch.set_grad_enabled(self.gradcam): for batch_item in tqdm(test_dataloader): im, target = batch_item - im = im.to(self.device).to(self.deployment_model.model_dtype).detach() + im = im.to(device=self.device, dtype=self.deployment_model.model_dtype).detach() if self.gradcam: # When gradcam is used we need to remove gradients diff --git a/quadra/tasks/segmentation.py b/quadra/tasks/segmentation.py index 3b4ec476..04e68352 100644 --- a/quadra/tasks/segmentation.py +++ b/quadra/tasks/segmentation.py @@ -346,7 +346,7 @@ def test(self) -> None: image_list, mask_list, mask_pred_list, label_list = [], [], [], [] for batch in dataloader: images, masks, labels = batch - images = images.to(self.device).to(self.deployment_model.model_dtype) + images = images.to(device=self.device, dtype=self.deployment_model.model_dtype) if len(masks.shape) == 3: # BxHxW -> Bx1xHxW masks = masks.unsqueeze(1) with torch.no_grad():