From 0256520794529ef86b2d3e9fbefd20b54b0c7426 Mon Sep 17 00:00:00 2001 From: Nicola De Angeli <112023843+niqodea@users.noreply.github.com> Date: Tue, 1 Oct 2024 18:59:59 +0200 Subject: [PATCH] fix: repair depth estimation multiprocessing (#33759) * fix: repair depth estimation multiprocessing * test: add test for multiprocess depth estimation --- .../pipelines/depth_estimation.py | 6 ++++-- .../test_pipelines_depth_estimation.py | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/transformers/pipelines/depth_estimation.py b/src/transformers/pipelines/depth_estimation.py index 79a85008e7..be45bdde8d 100644 --- a/src/transformers/pipelines/depth_estimation.py +++ b/src/transformers/pipelines/depth_estimation.py @@ -89,20 +89,22 @@ class DepthEstimationPipeline(Pipeline): def preprocess(self, image, timeout=None): image = load_image(image, timeout) - self.image_size = image.size model_inputs = self.image_processor(images=image, return_tensors=self.framework) if self.framework == "pt": model_inputs = model_inputs.to(self.torch_dtype) + model_inputs["target_size"] = image.size[::-1] return model_inputs def _forward(self, model_inputs): + target_size = model_inputs.pop("target_size") model_outputs = self.model(**model_inputs) + model_outputs["target_size"] = target_size return model_outputs def postprocess(self, model_outputs): predicted_depth = model_outputs.predicted_depth prediction = torch.nn.functional.interpolate( - predicted_depth.unsqueeze(1), size=self.image_size[::-1], mode="bicubic", align_corners=False + predicted_depth.unsqueeze(1), size=model_outputs["target_size"], mode="bicubic", align_corners=False ) output = prediction.squeeze().cpu().numpy() formatted = (output * 255 / np.max(output)).astype("uint8") diff --git a/tests/pipelines/test_pipelines_depth_estimation.py b/tests/pipelines/test_pipelines_depth_estimation.py index 1f2700fa74..16edeed453 100644 --- a/tests/pipelines/test_pipelines_depth_estimation.py +++ b/tests/pipelines/test_pipelines_depth_estimation.py @@ -116,3 +116,23 @@ class DepthEstimationPipelineTests(unittest.TestCase): def test_small_model_pt(self): # This is highly irregular to have no small tests. self.skipTest(reason="There is not hf-internal-testing tiny model for either GLPN nor DPT") + + @require_torch + def test_multiprocess(self): + depth_estimator = pipeline( + model="hf-internal-testing/tiny-random-DepthAnythingForDepthEstimation", + num_workers=2, + ) + outputs = depth_estimator( + [ + "./tests/fixtures/tests_samples/COCO/000000039769.png", + "./tests/fixtures/tests_samples/COCO/000000039769.png", + ] + ) + self.assertEqual( + [ + {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)}, + {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)}, + ], + outputs, + )