CoolFace
Apppublic

chendl/compositional_test

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
test_pipelines_depth_estimation.py116 linesDownload Raw Back to pipelines
1# Copyright 2021 The HuggingFace Team. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15import hashlib16import unittest17 18from transformers import MODEL_FOR_DEPTH_ESTIMATION_MAPPING, is_torch_available, is_vision_available19from transformers.pipelines import DepthEstimationPipeline, pipeline20from transformers.testing_utils import (21    is_pipeline_test,22    nested_simplify,23    require_tf,24    require_timm,25    require_torch,26    require_vision,27    slow,28)29 30from .test_pipelines_common import ANY31 32 33if is_torch_available():34    import torch35 36if is_vision_available():37    from PIL import Image38else:39 40    class Image:41        @staticmethod42        def open(*args, **kwargs):43            pass44 45 46def hashimage(image: Image) -> str:47    m = hashlib.md5(image.tobytes())48    return m.hexdigest()49 50 51@is_pipeline_test52@require_vision53@require_timm54@require_torch55class DepthEstimationPipelineTests(unittest.TestCase):56    model_mapping = MODEL_FOR_DEPTH_ESTIMATION_MAPPING57 58    def get_test_pipeline(self, model, tokenizer, processor):59        depth_estimator = DepthEstimationPipeline(model=model, image_processor=processor)60        return depth_estimator, [61            "./tests/fixtures/tests_samples/COCO/000000039769.png",62            "./tests/fixtures/tests_samples/COCO/000000039769.png",63        ]64 65    def run_pipeline_test(self, depth_estimator, examples):66        outputs = depth_estimator("./tests/fixtures/tests_samples/COCO/000000039769.png")67        self.assertEqual({"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)}, outputs)68        import datasets69 70        dataset = datasets.load_dataset("hf-internal-testing/fixtures_image_utils", "image", split="test")71        outputs = depth_estimator(72            [73                Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"),74                "http://images.cocodataset.org/val2017/000000039769.jpg",75                # RGBA76                dataset[0]["file"],77                # LA78                dataset[1]["file"],79                # L80                dataset[2]["file"],81            ]82        )83        self.assertEqual(84            [85                {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)},86                {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)},87                {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)},88                {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)},89                {"predicted_depth": ANY(torch.Tensor), "depth": ANY(Image.Image)},90            ],91            outputs,92        )93 94    @require_tf95    @unittest.skip("Depth estimation is not implemented in TF")96    def test_small_model_tf(self):97        pass98 99    @slow100    @require_torch101    def test_large_model_pt(self):102        model_id = "Intel/dpt-large"103        depth_estimator = pipeline("depth-estimation", model=model_id)104        outputs = depth_estimator("http://images.cocodataset.org/val2017/000000039769.jpg")105        outputs["depth"] = hashimage(outputs["depth"])106 107        # This seems flaky.108        # self.assertEqual(outputs["depth"], "1a39394e282e9f3b0741a90b9f108977")109        self.assertEqual(nested_simplify(outputs["predicted_depth"].max().item()), 29.304)110        self.assertEqual(nested_simplify(outputs["predicted_depth"].min().item()), 2.662)111 112    @require_torch113    def test_small_model_pt(self):114        # This is highly irregular to have no small tests.115        self.skipTest("There is not hf-internal-testing tiny model for either GLPN nor DPT")116