CoolFace
Apppublic

chendl/compositional_test

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
test_pipelines_video_classification.py99 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 unittest16 17from huggingface_hub import hf_hub_download18 19from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor20from transformers.pipelines import VideoClassificationPipeline, pipeline21from transformers.testing_utils import (22    is_pipeline_test,23    nested_simplify,24    require_decord,25    require_tf,26    require_torch,27    require_torch_or_tf,28    require_vision,29)30 31from .test_pipelines_common import ANY32 33 34@is_pipeline_test35@require_torch_or_tf36@require_vision37@require_decord38class VideoClassificationPipelineTests(unittest.TestCase):39    model_mapping = MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING40 41    def get_test_pipeline(self, model, tokenizer, processor):42        example_video_filepath = hf_hub_download(43            repo_id="nateraw/video-demo", filename="archery.mp4", repo_type="dataset"44        )45        video_classifier = VideoClassificationPipeline(model=model, image_processor=processor, top_k=2)46        examples = [47            example_video_filepath,48            "https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4",49        ]50        return video_classifier, examples51 52    def run_pipeline_test(self, video_classifier, examples):53        for example in examples:54            outputs = video_classifier(example)55 56            self.assertEqual(57                outputs,58                [59                    {"score": ANY(float), "label": ANY(str)},60                    {"score": ANY(float), "label": ANY(str)},61                ],62            )63 64    @require_torch65    def test_small_model_pt(self):66        small_model = "hf-internal-testing/tiny-random-VideoMAEForVideoClassification"67        small_feature_extractor = VideoMAEFeatureExtractor(68            size={"shortest_edge": 10}, crop_size={"height": 10, "width": 10}69        )70        video_classifier = pipeline(71            "video-classification", model=small_model, feature_extractor=small_feature_extractor, frame_sampling_rate=472        )73 74        video_file_path = hf_hub_download(repo_id="nateraw/video-demo", filename="archery.mp4", repo_type="dataset")75        outputs = video_classifier(video_file_path, top_k=2)76        self.assertEqual(77            nested_simplify(outputs, decimals=4),78            [{"score": 0.5199, "label": "LABEL_0"}, {"score": 0.4801, "label": "LABEL_1"}],79        )80 81        outputs = video_classifier(82            [83                video_file_path,84                video_file_path,85            ],86            top_k=2,87        )88        self.assertEqual(89            nested_simplify(outputs, decimals=4),90            [91                [{"score": 0.5199, "label": "LABEL_0"}, {"score": 0.4801, "label": "LABEL_1"}],92                [{"score": 0.5199, "label": "LABEL_0"}, {"score": 0.4801, "label": "LABEL_1"}],93            ],94        )95 96    @require_tf97    def test_small_model_tf(self):98        pass99