




'''simple docstring'''



from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml






class 							_a				(			yaml.SafeLoader ):





       '''simple docstring'''






       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						[self.constructed_objects[key_node] for key_node, _ in node.value]
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						[tuple(A  ) if isinstance(A,							A  ) else key for key in keys]
             SCREAMING_SNAKE_CASE   :      List[str]    					=						Counter(A  )
             SCREAMING_SNAKE_CASE   :      Tuple    					=						[key for key in counter if counter[key] > 1]
             if duplicate_keys:
                   raise TypeError(F"Got duplicate yaml keys: {duplicate_keys}"  )







       def        UpperCamelCase_							(      self,							A,							A=False  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						super().construct_mapping(A,							deep=A  )
             self._check_no_duplicates_on_constructed_node(A  )
             return mapping




def   lowercase__(			__UpperCamelCase:					str    ):

      """simple docstring"""






      SCREAMING_SNAKE_CASE   :      Optional[int]    					=						list(readme_content.splitlines()    )
      if full_content and full_content[0] == "---" and "---" in full_content[1:]:
            SCREAMING_SNAKE_CASE   :      Any    					=						full_content[1:].index('---'    ) + 1
            SCREAMING_SNAKE_CASE   :      Tuple    					=						'\n'.join(full_content[1:sep_idx]    )
            return yamlblock, "\n".join(full_content[sep_idx + 1 :]    )

      return None, "\n".join(__UpperCamelCase    )






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       A						:				Optional[Any]        =						{'''train_eval_index'''}  # train-eval-index in the YAML metadata







       @classmethod
       def        UpperCamelCase_							(      cls,							A  ):







             '''simple docstring'''







             with open(A,							encoding='utf-8'  ) as readme_file:
                   SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[Any]    					=						_split_yaml_from_readme(readme_file.read()  )
             if yaml_string is not None:
                   return cls.from_yaml_string(A  )
             else:
                   return cls()







       def        UpperCamelCase_							(      self,							A  ):







             '''simple docstring'''







             if path.exists():
                   with open(A,							encoding='utf-8'  ) as readme_file:
                         SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						readme_file.read()
             else:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						None
             SCREAMING_SNAKE_CASE   :      List[str]    					=						self._to_readme(A  )
             with open(A,							'w',							encoding='utf-8'  ) as readme_file:
                   readme_file.write(A  )







       def        UpperCamelCase_							(      self,							A = None  ):







             '''simple docstring'''







             if readme_content is not None:
                   SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						_split_yaml_from_readme(A  )
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						'---\n' + self.to_yaml_string() + '---\n' + content
             else:
                   SCREAMING_SNAKE_CASE   :      Any    					=						'---\n' + self.to_yaml_string() + '---\n'
             return full_content







       @classmethod
       def        UpperCamelCase_							(      cls,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						yaml.load(A,							Loader=_NoDuplicateSafeLoader  ) or {}

             # Convert the YAML keys to DatasetMetadata fields
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						{
                 (key.replace('-',							'_'  ) if key.replace('-',							'_'  ) in cls._FIELDS_WITH_DASHES else key): value
                 for key, value in metadata_dict.items()
             }
             return cls(**A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return yaml.safe_dump(
                 {
                     (key.replace('_',							'-'  ) if key in self._FIELDS_WITH_DASHES else key): value
                     for key, value in self.items()
                 },							sort_keys=A,							allow_unicode=A,							encoding='utf-8',							).decode('utf-8'  )

UpperCamelCase_    	=				{
    "image-classification": [],
    "translation": [],
    "image-segmentation": [],
    "fill-mask": [],
    "automatic-speech-recognition": [],
    "token-classification": [],
    "sentence-similarity": [],
    "audio-classification": [],
    "question-answering": [],
    "summarization": [],
    "zero-shot-classification": [],
    "table-to-text": [],
    "feature-extraction": [],
    "other": [],
    "multiple-choice": [],
    "text-classification": [],
    "text-to-image": [],
    "text2text-generation": [],
    "zero-shot-image-classification": [],
    "tabular-classification": [],
    "tabular-regression": [],
    "image-to-image": [],
    "tabular-to-text": [],
    "unconditional-image-generation": [],
    "text-retrieval": [],
    "text-to-speech": [],
    "object-detection": [],
    "audio-to-audio": [],
    "text-generation": [],
    "conversational": [],
    "table-question-answering": [],
    "visual-question-answering": [],
    "image-to-text": [],
    "reinforcement-learning": [],
    "voice-activity-detection": [],
    "time-series-forecasting": [],
    "document-question-answering": [],
}


if __name__ == "__main__":
      from argparse import ArgumentParser

      UpperCamelCase_    	=				ArgumentParser(usage="Validate the yaml metadata block of a README.md file.")
      ap.add_argument("readme_filepath")
      UpperCamelCase_    	=				ap.parse_args()

      UpperCamelCase_    	=				Path(args.readme_filepath)
      UpperCamelCase_    	=				DatasetMetadata.from_readme(readme_filepath)
      print(dataset_metadata)
      dataset_metadata.to_readme(readme_filepath)








'''simple docstring'''



import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import YolosImageProcessor






class 							_a				(			unittest.TestCase ):





       '''simple docstring'''






       def __init__(      self,							A,							A=7,							A=3,							A=30,							A=400,							A=True,							A=None,							A=True,							A=[0.5, 0.5, 0.5],							A=[0.5, 0.5, 0.5],							A=True,							A=1 / 255,							A=True,							):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						size if size is not None else {'shortest_edge': 18, 'longest_edge': 1_333}
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						parent
             SCREAMING_SNAKE_CASE   :      Dict    					=						batch_size
             SCREAMING_SNAKE_CASE   :      int    					=						num_channels
             SCREAMING_SNAKE_CASE   :      Tuple    					=						min_resolution
             SCREAMING_SNAKE_CASE   :      int    					=						max_resolution
             SCREAMING_SNAKE_CASE   :      Tuple    					=						do_resize
             SCREAMING_SNAKE_CASE   :      Tuple    					=						size
             SCREAMING_SNAKE_CASE   :      Any    					=						do_normalize
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_mean
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_std
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						do_rescale
             SCREAMING_SNAKE_CASE   :      int    					=						rescale_factor
             SCREAMING_SNAKE_CASE   :      List[str]    					=						do_pad







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return {
                 "do_resize": self.do_resize,
                 "size": self.size,
                 "do_normalize": self.do_normalize,
                 "image_mean": self.image_mean,
                 "image_std": self.image_std,
                 "do_rescale": self.do_rescale,
                 "rescale_factor": self.rescale_factor,
                 "do_pad": self.do_pad,
             }







       def        UpperCamelCase_							(      self,							A,							A=False  ):







             '''simple docstring'''







             if not batched:
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						image_inputs[0]
                   if isinstance(A,							Image.Image  ):
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						image.size
                   else:
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						image.shape[1], image.shape[2]
                   if w < h:
                         SCREAMING_SNAKE_CASE   :      int    					=						int(self.size['shortest_edge'] * h / w  )
                         SCREAMING_SNAKE_CASE   :      int    					=						self.size['shortest_edge']
                   elif w > h:
                         SCREAMING_SNAKE_CASE   :      Any    					=						self.size['shortest_edge']
                         SCREAMING_SNAKE_CASE   :      Dict    					=						int(self.size['shortest_edge'] * w / h  )
                   else:
                         SCREAMING_SNAKE_CASE   :      Any    					=						self.size['shortest_edge']
                         SCREAMING_SNAKE_CASE   :      int    					=						self.size['shortest_edge']

             else:
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						[]
                   for image in image_inputs:
                         SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.get_expected_values([image]  )
                         expected_values.append((expected_height, expected_width)  )
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						max(A,							key=lambda A  : item[0]  )[0]
                   SCREAMING_SNAKE_CASE   :      str    					=						max(A,							key=lambda A  : item[1]  )[1]

             return expected_height, expected_width






@require_torch
@require_vision
class 							_a				(			SCREAMING_SNAKE_CASE ,      unittest.TestCase ):





       '''simple docstring'''






       A						:				List[Any]        =						YolosImageProcessor if is_vision_available() else None







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						YolosImageProcessingTester(self  )







       @property
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return self.image_processor_tester.prepare_image_processor_dict()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Any    					=						self.image_processing_class(**self.image_processor_dict  )
             self.assertTrue(hasattr(A,							'image_mean'  )  )
             self.assertTrue(hasattr(A,							'image_std'  )  )
             self.assertTrue(hasattr(A,							'do_normalize'  )  )
             self.assertTrue(hasattr(A,							'do_resize'  )  )
             self.assertTrue(hasattr(A,							'size'  )  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class.from_dict(self.image_processor_dict  )
             self.assertEqual(image_processor.size,							{'shortest_edge': 18, 'longest_edge': 1_333}  )
             self.assertEqual(image_processor.do_pad,							A  )

             SCREAMING_SNAKE_CASE   :      str    					=						self.image_processing_class.from_dict(
                 self.image_processor_dict,							size=42,							max_size=84,							pad_and_return_pixel_mask=A  )
             self.assertEqual(image_processor.size,							{'shortest_edge': 42, 'longest_edge': 84}  )
             self.assertEqual(image_processor.do_pad,							A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             pass







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random PIL images
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							Image.Image  )

             # Test not batched input
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Any    					=						self.image_processor_tester.get_expected_values(A  )

             self.assertEqual(
                 encoded_images.shape,							(1, self.image_processor_tester.num_channels, expected_height, expected_width),							)

             # Test batched
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

             SCREAMING_SNAKE_CASE   :      Tuple    					=						image_processing(A,							return_tensors='pt'  ).pixel_values
             self.assertEqual(
                 encoded_images.shape,							(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     expected_height,
                     expected_width,
                 ),							)







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random numpy tensors
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A,							numpify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							np.ndarray  )

             # Test not batched input
             SCREAMING_SNAKE_CASE   :      int    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						self.image_processor_tester.get_expected_values(A  )

             self.assertEqual(
                 encoded_images.shape,							(1, self.image_processor_tester.num_channels, expected_height, expected_width),							)

             # Test batched
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_processing(A,							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

             self.assertEqual(
                 encoded_images.shape,							(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     expected_height,
                     expected_width,
                 ),							)







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class(**self.image_processor_dict  )
             # create random PyTorch tensors
             SCREAMING_SNAKE_CASE   :      int    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A,							torchify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							torch.Tensor  )

             # Test not batched input
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						image_processing(image_inputs[0],							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.image_processor_tester.get_expected_values(A  )

             self.assertEqual(
                 encoded_images.shape,							(1, self.image_processor_tester.num_channels, expected_height, expected_width),							)

             # Test batched
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_processing(A,							return_tensors='pt'  ).pixel_values

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      str    					=						self.image_processor_tester.get_expected_values(A,							batched=A  )

             self.assertEqual(
                 encoded_images.shape,							(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_channels,
                     expected_height,
                     expected_width,
                 ),							)







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Dict    					=						self.image_processing_class(**self.image_processor_dict  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.image_processing_class(do_resize=A,							do_normalize=A,							do_rescale=A  )
             # create random PyTorch tensors
             SCREAMING_SNAKE_CASE   :      int    					=						prepare_image_inputs(self.image_processor_tester,							equal_resolution=A,							torchify=A  )
             for image in image_inputs:
                   self.assertIsInstance(A,							torch.Tensor  )

             # Test whether the method "pad" and calling the image processor return the same tensors
             SCREAMING_SNAKE_CASE   :      List[str]    					=						image_processing_a.pad(A,							return_tensors='pt'  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						image_processing_a(A,							return_tensors='pt'  )

             self.assertTrue(
                 torch.allclose(encoded_images_with_method['pixel_values'],							encoded_images['pixel_values'],							atol=1E-4  )  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             with open('./tests/fixtures/tests_samples/COCO/coco_annotations.txt',							'r'  ) as f:
                   SCREAMING_SNAKE_CASE   :      Dict    					=						json.loads(f.read()  )

             SCREAMING_SNAKE_CASE   :      Any    					=						{'image_id': 39_769, 'annotations': target}

             # encode them
             SCREAMING_SNAKE_CASE   :      Any    					=						YolosImageProcessor.from_pretrained('hustvl/yolos-small'  )
             SCREAMING_SNAKE_CASE   :      int    					=						image_processing(images=A,							annotations=A,							return_tensors='pt'  )

             # verify pixel values
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.Size([1, 3, 800, 1_066]  )
             self.assertEqual(encoding['pixel_values'].shape,							A  )

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
             self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3],							A,							atol=1E-4  )  )

             # verify area
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['area'],							A  )  )
             # verify boxes
             SCREAMING_SNAKE_CASE   :      str    					=						torch.Size([6, 4]  )
             self.assertEqual(encoding['labels'][0]['boxes'].shape,							A  )
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0],							A,							atol=1E-3  )  )
             # verify image_id
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([39_769]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'],							A  )  )
             # verify is_crowd
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						torch.tensor([0, 0, 0, 0, 0, 0]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'],							A  )  )
             # verify class_labels
             SCREAMING_SNAKE_CASE   :      int    					=						torch.tensor([75, 75, 63, 65, 17, 17]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'],							A  )  )
             # verify orig_size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([480, 640]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'],							A  )  )
             # verify size
             SCREAMING_SNAKE_CASE   :      str    					=						torch.tensor([800, 1_066]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['size'],							A  )  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
             with open('./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt',							'r'  ) as f:
                   SCREAMING_SNAKE_CASE   :      int    					=						json.loads(f.read()  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						{'file_name': '000000039769.png', 'image_id': 39_769, 'segments_info': target}

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						pathlib.Path('./tests/fixtures/tests_samples/COCO/coco_panoptic'  )

             # encode them
             SCREAMING_SNAKE_CASE   :      int    					=						YolosImageProcessor(format='coco_panoptic'  )
             SCREAMING_SNAKE_CASE   :      str    					=						image_processing(images=A,							annotations=A,							masks_path=A,							return_tensors='pt'  )

             # verify pixel values
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.Size([1, 3, 800, 1_066]  )
             self.assertEqual(encoding['pixel_values'].shape,							A  )

             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.tensor([0.27_96, 0.31_38, 0.34_81]  )
             self.assertTrue(torch.allclose(encoding['pixel_values'][0, 0, 0, :3],							A,							atol=1E-4  )  )

             # verify area
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['area'],							A  )  )
             # verify boxes
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						torch.Size([6, 4]  )
             self.assertEqual(encoding['labels'][0]['boxes'].shape,							A  )
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['boxes'][0],							A,							atol=1E-3  )  )
             # verify image_id
             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.tensor([39_769]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['image_id'],							A  )  )
             # verify is_crowd
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.tensor([0, 0, 0, 0, 0, 0]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['iscrowd'],							A  )  )
             # verify class_labels
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.tensor([17, 17, 63, 75, 75, 93]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['class_labels'],							A  )  )
             # verify masks
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						822_873
             self.assertEqual(encoding['labels'][0]['masks'].sum().item(),							A  )
             # verify orig_size
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						torch.tensor([480, 640]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['orig_size'],							A  )  )
             # verify size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						torch.tensor([800, 1_066]  )
             self.assertTrue(torch.allclose(encoding['labels'][0]['size'],							A  )  )


