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							):






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





       '''simple docstring'''





       UpperCamelCase__										=      size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333}
       UpperCamelCase__										=      parent
       UpperCamelCase__										=      batch_size
       UpperCamelCase__										=      num_channels
       UpperCamelCase__										=      min_resolution
       UpperCamelCase__										=      max_resolution
       UpperCamelCase__										=      do_resize
       UpperCamelCase__										=      size
       UpperCamelCase__										=      do_normalize
       UpperCamelCase__										=      image_mean
       UpperCamelCase__										=      image_std
       UpperCamelCase__										=      do_rescale
       UpperCamelCase__										=      rescale_factor
       UpperCamelCase__										=      do_pad






  def 		_a					(self	)		->      Optional[Any]:





       '''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 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=False	)		->      str:





       '''simple docstring'''





       if not batched:
            UpperCamelCase__										=      image_inputs[0]
            if isinstance(SCREAMING_SNAKE_CASE_   ,				Image.Image	):
                 UpperCamelCase__						,       UpperCamelCase__										=      image.size
            else:
                 UpperCamelCase__						,       UpperCamelCase__										=      image.shape[1], image.shape[2]
            if w < h:
                 UpperCamelCase__										=      int(self.size['''shortest_edge'''] * h / w	)
                 UpperCamelCase__										=      self.size['''shortest_edge''']
            elif w > h:
                 UpperCamelCase__										=      self.size['''shortest_edge''']
                 UpperCamelCase__										=      int(self.size['''shortest_edge'''] * w / h	)
            else:
                 UpperCamelCase__										=      self.size['''shortest_edge''']
                 UpperCamelCase__										=      self.size['''shortest_edge''']

       else:
            UpperCamelCase__										=      []
            for image in image_inputs:
                 UpperCamelCase__						,       UpperCamelCase__										=      self.get_expected_values([image]	)
                 expected_values.append((expected_height, expected_width)	)
            UpperCamelCase__										=      max(SCREAMING_SNAKE_CASE_   ,				key=lambda SCREAMING_SNAKE_CASE_	: item[0]	)[0]
            UpperCamelCase__										=      max(SCREAMING_SNAKE_CASE_   ,				key=lambda SCREAMING_SNAKE_CASE_	: item[1]	)[1]

       return expected_height, expected_width



@require_torch
@require_vision
class     _A   (    __UpperCamelCase	, unittest.TestCase							):
  SCREAMING_SNAKE_CASE_	:	Any						=YolosImageProcessor if is_vision_available() else None






  def 		_a					(self	)		->      Optional[int]:





       '''simple docstring'''





       UpperCamelCase__										=      YolosImageProcessingTester(self	)






  @property
  def 		_a					(self	)		->      List[Any]:





       '''simple docstring'''





       return self.image_processor_tester.prepare_image_processor_dict()






  def 		_a					(self	)		->      Optional[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      self.image_processing_class(**self.image_processor_dict	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,				'''image_mean'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,				'''image_std'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,				'''do_normalize'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,				'''do_resize'''	)	)
       self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,				'''size'''	)	)






  def 		_a					(self	)		->      Optional[int]:





       '''simple docstring'''





       UpperCamelCase__										=      self.image_processing_class.from_dict(self.image_processor_dict	)
       self.assertEqual(image_processor.size   ,				{'''shortest_edge''': 18, '''longest_edge''': 1333}	)
       self.assertEqual(image_processor.do_pad   ,				SCREAMING_SNAKE_CASE_	)

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






  def 		_a					(self	)		->      Dict:





       '''simple docstring'''





       pass






  def 		_a					(self	)		->      Dict:





       '''simple docstring'''





       UpperCamelCase__										=      self.image_processing_class(**self.image_processor_dict	)
       # create random PIL images
       UpperCamelCase__										=      prepare_image_inputs(self.image_processor_tester   ,				equal_resolution=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,				Image.Image	)

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

       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_	)

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

       # Test batched
       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_   ,				batched=SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      image_processing(SCREAMING_SNAKE_CASE_   ,				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 		_a					(self	)		->      Union[str, Any]:





       '''simple docstring'''





       UpperCamelCase__										=      self.image_processing_class(**self.image_processor_dict	)
       # create random numpy tensors
       UpperCamelCase__										=      prepare_image_inputs(self.image_processor_tester   ,				equal_resolution=SCREAMING_SNAKE_CASE_   ,				numpify=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,				np.ndarray	)

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

       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_	)

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

       # Test batched
       UpperCamelCase__										=      image_processing(SCREAMING_SNAKE_CASE_   ,				return_tensors='''pt'''	).pixel_values

       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_   ,				batched=SCREAMING_SNAKE_CASE_	)

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






  def 		_a					(self	)		->      str:





       '''simple docstring'''





       UpperCamelCase__										=      self.image_processing_class(**self.image_processor_dict	)
       # create random PyTorch tensors
       UpperCamelCase__										=      prepare_image_inputs(self.image_processor_tester   ,				equal_resolution=SCREAMING_SNAKE_CASE_   ,				torchify=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,				torch.Tensor	)

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

       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_	)

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

       # Test batched
       UpperCamelCase__										=      image_processing(SCREAMING_SNAKE_CASE_   ,				return_tensors='''pt'''	).pixel_values

       UpperCamelCase__						,       UpperCamelCase__										=      self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_   ,				batched=SCREAMING_SNAKE_CASE_	)

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






  def 		_a					(self	)		->      str:





       '''simple docstring'''





       UpperCamelCase__										=      self.image_processing_class(**self.image_processor_dict	)
       UpperCamelCase__										=      self.image_processing_class(do_resize=SCREAMING_SNAKE_CASE_   ,				do_normalize=SCREAMING_SNAKE_CASE_   ,				do_rescale=SCREAMING_SNAKE_CASE_	)
       # create random PyTorch tensors
       UpperCamelCase__										=      prepare_image_inputs(self.image_processor_tester   ,				equal_resolution=SCREAMING_SNAKE_CASE_   ,				torchify=SCREAMING_SNAKE_CASE_	)
       for image in image_inputs:
            self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,				torch.Tensor	)

       # Test whether the method "pad" and calling the image processor return the same tensors
       UpperCamelCase__										=      image_processing_a.pad(SCREAMING_SNAKE_CASE_   ,				return_tensors='''pt'''	)
       UpperCamelCase__										=      image_processing_a(SCREAMING_SNAKE_CASE_   ,				return_tensors='''pt'''	)

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






  @slow
  def 		_a					(self	)		->      Union[str, Any]:





       '''simple docstring'''





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

       UpperCamelCase__										=      {'''image_id''': 3_9769, '''annotations''': target}

       # encode them
       UpperCamelCase__										=      YolosImageProcessor.from_pretrained('''hustvl/yolos-small'''	)
       UpperCamelCase__										=      image_processing(images=SCREAMING_SNAKE_CASE_   ,				annotations=SCREAMING_SNAKE_CASE_   ,				return_tensors='''pt'''	)

       # verify pixel values
       UpperCamelCase__										=      torch.Size([1, 3, 800, 1066]	)
       self.assertEqual(encoding['''pixel_values'''].shape   ,				SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      torch.tensor([0.2796, 0.3138, 0.3481]	)
       self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]   ,				SCREAMING_SNAKE_CASE_   ,				atol=1E-4	)	)

       # verify area
       UpperCamelCase__										=      torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify boxes
       UpperCamelCase__										=      torch.Size([6, 4]	)
       self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape   ,				SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      torch.tensor([0.5503, 0.2765, 0.0604, 0.2215]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]   ,				SCREAMING_SNAKE_CASE_   ,				atol=1E-3	)	)
       # verify image_id
       UpperCamelCase__										=      torch.tensor([3_9769]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify is_crowd
       UpperCamelCase__										=      torch.tensor([0, 0, 0, 0, 0, 0]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify class_labels
       UpperCamelCase__										=      torch.tensor([75, 75, 63, 65, 17, 17]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify orig_size
       UpperCamelCase__										=      torch.tensor([480, 640]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify size
       UpperCamelCase__										=      torch.tensor([800, 1066]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']   ,				SCREAMING_SNAKE_CASE_	)	)






  @slow
  def 		_a					(self	)		->      Any:





       '''simple docstring'''





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

       UpperCamelCase__										=      {'''file_name''': '''000000039769.png''', '''image_id''': 3_9769, '''segments_info''': target}

       UpperCamelCase__										=      pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''	)

       # encode them
       UpperCamelCase__										=      YolosImageProcessor(format='''coco_panoptic'''	)
       UpperCamelCase__										=      image_processing(images=SCREAMING_SNAKE_CASE_   ,				annotations=SCREAMING_SNAKE_CASE_   ,				masks_path=SCREAMING_SNAKE_CASE_   ,				return_tensors='''pt'''	)

       # verify pixel values
       UpperCamelCase__										=      torch.Size([1, 3, 800, 1066]	)
       self.assertEqual(encoding['''pixel_values'''].shape   ,				SCREAMING_SNAKE_CASE_	)

       UpperCamelCase__										=      torch.tensor([0.2796, 0.3138, 0.3481]	)
       self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]   ,				SCREAMING_SNAKE_CASE_   ,				atol=1E-4	)	)

       # verify area
       UpperCamelCase__										=      torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify boxes
       UpperCamelCase__										=      torch.Size([6, 4]	)
       self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape   ,				SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      torch.tensor([0.2625, 0.5437, 0.4688, 0.8625]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]   ,				SCREAMING_SNAKE_CASE_   ,				atol=1E-3	)	)
       # verify image_id
       UpperCamelCase__										=      torch.tensor([3_9769]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify is_crowd
       UpperCamelCase__										=      torch.tensor([0, 0, 0, 0, 0, 0]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify class_labels
       UpperCamelCase__										=      torch.tensor([17, 17, 63, 75, 75, 93]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify masks
       UpperCamelCase__										=      82_2873
       self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()   ,				SCREAMING_SNAKE_CASE_	)
       # verify orig_size
       UpperCamelCase__										=      torch.tensor([480, 640]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']   ,				SCREAMING_SNAKE_CASE_	)	)
       # verify size
       UpperCamelCase__										=      torch.tensor([800, 1066]	)
       self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']   ,				SCREAMING_SNAKE_CASE_	)	)




# Lint as: python3
# pylint: enable=line-too-long
# pylint: disable=g-import-not-at-top,g-bad-import-order,wrong-import-position

__magic_name__								='''2.13.1'''

import platform

import pyarrow
from packaging import version


if version.parse(platform.python_version()) < version.parse('''3.7'''):
     raise ImportWarning(
         '''To use `datasets`, Python>=3.7 is required, and the current version of Python doesn\'t match this condition.'''
     )

if version.parse(pyarrow.__version__).major < 8:
     raise ImportWarning(
         '''To use `datasets`, the module `pyarrow>=8.0.0` is required, and the current version of `pyarrow` doesn\'t match this condition.\n'''
         '''If you are running this in a Google Colab, you should probably just restart the runtime to use the right version of `pyarrow`.'''
     )

del platform
del pyarrow
del version

from .arrow_dataset import Dataset
from .arrow_reader import ReadInstruction
from .builder import ArrowBasedBuilder, BeamBasedBuilder, BuilderConfig, DatasetBuilder, GeneratorBasedBuilder
from .combine import concatenate_datasets, interleave_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .download import *
from .features import *
from .fingerprint import disable_caching, enable_caching, is_caching_enabled, set_caching_enabled
from .info import DatasetInfo, MetricInfo
from .inspect import (
    get_dataset_config_info,
    get_dataset_config_names,
    get_dataset_infos,
    get_dataset_split_names,
    inspect_dataset,
    inspect_metric,
    list_datasets,
    list_metrics,
)
from .iterable_dataset import IterableDataset
from .load import load_dataset, load_dataset_builder, load_from_disk, load_metric
from .metric import Metric
from .splits import (
    NamedSplit,
    NamedSplitAll,
    Split,
    SplitBase,
    SplitDict,
    SplitGenerator,
    SplitInfo,
    SubSplitInfo,
    percent,
)
from .tasks import *
from .utils import *
from .utils import logging


# deprecated modules
from datasets import arrow_dataset as _arrow_dataset  # isort:skip
from datasets import utils as _utils  # isort:skip
from datasets.utils import download_manager as _deprecated_download_manager  # isort:skip

__magic_name__								=concatenate_datasets
__magic_name__								=DownloadConfig
__magic_name__								=DownloadManager
__magic_name__								=DownloadMode
__magic_name__								=DownloadConfig
__magic_name__								=DownloadMode
__magic_name__								=DownloadManager

del _arrow_dataset, _utils, _deprecated_download_manager



