


'''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 DeformableDetrImageProcessor






class        __snake_case	(       unittest.TestCase):







				"""simple docstring"""



				def __init__(       self   :  List[str]   ,						lowerCamelCase   :  Optional[int]   ,						lowerCamelCase   :  Tuple=7   ,						lowerCamelCase   :  Union[str, Any]=3   ,						lowerCamelCase   :  int=30   ,						lowerCamelCase   :  Optional[int]=4_00   ,						lowerCamelCase   :  Tuple=True   ,						lowerCamelCase   :  str=None   ,						lowerCamelCase   :  Tuple=True   ,						lowerCamelCase   :  List[Any]=[0.5, 0.5, 0.5]   ,						lowerCamelCase   :  Tuple=[0.5, 0.5, 0.5]   ,						lowerCamelCase   :  int=True   ,						lowerCamelCase   :  Optional[Any]=1 / 2_55   ,						lowerCamelCase   :  Optional[Any]=True   ,						)       ->  str:
						lowerCAmelCase_      :				Optional[int]      =       size if size is not None else {"shortest_edge": 18, "longest_edge": 13_33}
						lowerCAmelCase_      :				Optional[Any]      =       parent
						lowerCAmelCase_      :				Optional[int]      =       batch_size
						lowerCAmelCase_      :				int      =       num_channels
						lowerCAmelCase_      :				Optional[int]      =       min_resolution
						lowerCAmelCase_      :				List[str]      =       max_resolution
						lowerCAmelCase_      :				str      =       do_resize
						lowerCAmelCase_      :				str      =       size
						lowerCAmelCase_      :				str      =       do_normalize
						lowerCAmelCase_      :				List[str]      =       image_mean
						lowerCAmelCase_      :				str      =       image_std
						lowerCAmelCase_      :				Union[str, Any]      =       do_rescale
						lowerCAmelCase_      :				Optional[Any]      =       rescale_factor
						lowerCAmelCase_      :				List[str]      =       do_pad

				def __lowercase       (       self   :  List[Any]		)       ->  Union[str, Any]:
						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 __lowercase       (       self   :  Dict   ,						lowerCamelCase   :  Union[str, Any]   ,						lowerCamelCase   :  int=False		)       ->  Any:




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

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

						return expected_height, expected_width







@require_torch
@require_vision
class        __snake_case	(       lowerCamelCase__     ,unittest.TestCase):







				"""simple docstring"""



				lowercase       			= DeformableDetrImageProcessor if is_vision_available() else None

				def __lowercase       (       self   :  Tuple		)       ->  Tuple:
						lowerCAmelCase_      :				Tuple      =       DeformableDetrImageProcessingTester(self		)

				@property
				def __lowercase       (       self   :  Optional[int]		)       ->  str:
						return self.image_processor_tester.prepare_image_processor_dict()

				def __lowercase       (       self   :  Tuple		)       ->  List[Any]:
						lowerCAmelCase_      :				int      =       self.image_processing_class(**self.image_processor_dict		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""image_mean"""		)		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""image_std"""		)		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""do_normalize"""		)		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""do_resize"""		)		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""do_rescale"""		)		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""do_pad"""		)		)
						self.assertTrue(hasattr(lowerCamelCase   ,						"""size"""		)		)

				def __lowercase       (       self   :  str		)       ->  Dict:
						lowerCAmelCase_      :				Dict      =       self.image_processing_class.from_dict(self.image_processor_dict		)
						self.assertEqual(image_processor.size   ,						{"""shortest_edge""": 18, """longest_edge""": 13_33}		)
						self.assertEqual(image_processor.do_pad   ,						lowerCamelCase		)

						lowerCAmelCase_      :				Any      =       self.image_processing_class.from_dict(
						    self.image_processor_dict   ,						size=42   ,						max_size=84   ,						pad_and_return_pixel_mask=lowerCamelCase		)
						self.assertEqual(image_processor.size   ,						{"""shortest_edge""": 42, """longest_edge""": 84}		)
						self.assertEqual(image_processor.do_pad   ,						lowerCamelCase		)

				def __lowercase       (       self   :  Dict		)       ->  List[str]:
						pass

				def __lowercase       (       self   :  List[Any]		)       ->  Any:
						lowerCAmelCase_      :				List[str]      =       self.image_processing_class(**self.image_processor_dict		)
						# create random PIL images
						lowerCAmelCase_      :				str      =       prepare_image_inputs(self.image_processor_tester   ,						equal_resolution=lowerCamelCase		)
						for image in image_inputs:
								self.assertIsInstance(lowerCamelCase   ,						Image.Image		)

						# Test not batched input
						lowerCAmelCase_      :				List[Any]      =       image_processing(image_inputs[0]   ,						return_tensors="""pt"""		).pixel_values

						lowerCAmelCase_      :				Dict      =       self.image_processor_tester.get_expected_values(lowerCamelCase		)

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

						# Test batched
						lowerCAmelCase_      :				Tuple      =       self.image_processor_tester.get_expected_values(lowerCamelCase   ,						batched=lowerCamelCase		)

						lowerCAmelCase_      :				Tuple      =       image_processing(lowerCamelCase   ,						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 __lowercase       (       self   :  str		)       ->  Any:
						lowerCAmelCase_      :				List[str]      =       self.image_processing_class(**self.image_processor_dict		)
						# create random numpy tensors
						lowerCAmelCase_      :				Tuple      =       prepare_image_inputs(self.image_processor_tester   ,						equal_resolution=lowerCamelCase   ,						numpify=lowerCamelCase		)
						for image in image_inputs:
								self.assertIsInstance(lowerCamelCase   ,						np.ndarray		)

						# Test not batched input
						lowerCAmelCase_      :				Union[str, Any]      =       image_processing(image_inputs[0]   ,						return_tensors="""pt"""		).pixel_values

						lowerCAmelCase_      :				Dict      =       self.image_processor_tester.get_expected_values(lowerCamelCase		)

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

						# Test batched
						lowerCAmelCase_      :				str      =       image_processing(lowerCamelCase   ,						return_tensors="""pt"""		).pixel_values

						lowerCAmelCase_      :				Optional[Any]      =       self.image_processor_tester.get_expected_values(lowerCamelCase   ,						batched=lowerCamelCase		)

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

				def __lowercase       (       self   :  Tuple		)       ->  List[str]:
						lowerCAmelCase_      :				List[Any]      =       self.image_processing_class(**self.image_processor_dict		)
						# create random PyTorch tensors
						lowerCAmelCase_      :				str      =       prepare_image_inputs(self.image_processor_tester   ,						equal_resolution=lowerCamelCase   ,						torchify=lowerCamelCase		)
						for image in image_inputs:
								self.assertIsInstance(lowerCamelCase   ,						torch.Tensor		)

						# Test not batched input
						lowerCAmelCase_      :				Tuple      =       image_processing(image_inputs[0]   ,						return_tensors="""pt"""		).pixel_values

						lowerCAmelCase_      :				Tuple      =       self.image_processor_tester.get_expected_values(lowerCamelCase		)

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

						# Test batched
						lowerCAmelCase_      :				Union[str, Any]      =       image_processing(lowerCamelCase   ,						return_tensors="""pt"""		).pixel_values

						lowerCAmelCase_      :				Union[str, Any]      =       self.image_processor_tester.get_expected_values(lowerCamelCase   ,						batched=lowerCamelCase		)

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

				@slow
				def __lowercase       (       self   :  int		)       ->  int:
						lowerCAmelCase_      :				List[Any]      =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
						with open("""./tests/fixtures/tests_samples/COCO/coco_annotations.txt"""   ,						"""r"""		) as f:
								lowerCAmelCase_      :				Optional[int]      =       json.loads(f.read()		)

						lowerCAmelCase_      :				Tuple      =       {"image_id": 3_97_69, "annotations": target}

						# encode them
						lowerCAmelCase_      :				Dict      =       DeformableDetrImageProcessor()
						lowerCAmelCase_      :				Optional[int]      =       image_processing(images=lowerCamelCase   ,						annotations=lowerCamelCase   ,						return_tensors="""pt"""		)

						# verify pixel values
						lowerCAmelCase_      :				int      =       torch.Size([1, 3, 8_00, 10_66]		)
						self.assertEqual(encoding["""pixel_values"""].shape   ,						lowerCamelCase		)

						lowerCAmelCase_      :				str      =       torch.tensor([0.2_796, 0.3_138, 0.3_481]		)
						self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]   ,						lowerCamelCase   ,						atol=1E-4		)		)

						# verify area
						lowerCAmelCase_      :				List[str]      =       torch.tensor([5_887.9_600, 11_250.2_061, 489_353.8_438, 837_122.7_500, 147_967.5_156, 165_732.3_438]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]   ,						lowerCamelCase		)		)
						# verify boxes
						lowerCAmelCase_      :				Optional[Any]      =       torch.Size([6, 4]		)
						self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape   ,						lowerCamelCase		)
						lowerCAmelCase_      :				Union[str, Any]      =       torch.tensor([0.5_503, 0.2_765, 0.0_604, 0.2_215]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]   ,						lowerCamelCase   ,						atol=1E-3		)		)
						# verify image_id
						lowerCAmelCase_      :				Any      =       torch.tensor([3_97_69]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]   ,						lowerCamelCase		)		)
						# verify is_crowd
						lowerCAmelCase_      :				Optional[Any]      =       torch.tensor([0, 0, 0, 0, 0, 0]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]   ,						lowerCamelCase		)		)
						# verify class_labels
						lowerCAmelCase_      :				int      =       torch.tensor([75, 75, 63, 65, 17, 17]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]   ,						lowerCamelCase		)		)
						# verify orig_size
						lowerCAmelCase_      :				Optional[Any]      =       torch.tensor([4_80, 6_40]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]   ,						lowerCamelCase		)		)
						# verify size
						lowerCAmelCase_      :				List[str]      =       torch.tensor([8_00, 10_66]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]   ,						lowerCamelCase		)		)





				@slow
				def __lowercase       (       self   :  List[Any]		)       ->  Union[str, Any]:
						lowerCAmelCase_      :				Any      =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		)
						with open("""./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"""   ,						"""r"""		) as f:
								lowerCAmelCase_      :				str      =       json.loads(f.read()		)

						lowerCAmelCase_      :				Optional[Any]      =       {"file_name": "000000039769.png", "image_id": 3_97_69, "segments_info": target}

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

						# encode them
						lowerCAmelCase_      :				List[Any]      =       DeformableDetrImageProcessor(format="""coco_panoptic"""		)
						lowerCAmelCase_      :				Dict      =       image_processing(images=lowerCamelCase   ,						annotations=lowerCamelCase   ,						masks_path=lowerCamelCase   ,						return_tensors="""pt"""		)

						# verify pixel values
						lowerCAmelCase_      :				List[Any]      =       torch.Size([1, 3, 8_00, 10_66]		)
						self.assertEqual(encoding["""pixel_values"""].shape   ,						lowerCamelCase		)

						lowerCAmelCase_      :				Optional[Any]      =       torch.tensor([0.2_796, 0.3_138, 0.3_481]		)
						self.assertTrue(torch.allclose(encoding["""pixel_values"""][0, 0, 0, :3]   ,						lowerCamelCase   ,						atol=1E-4		)		)

						# verify area
						lowerCAmelCase_      :				Tuple      =       torch.tensor([147_979.6_875, 165_527.0_469, 484_638.5_938, 11_292.9_375, 5_879.6_562, 7_634.1_147]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""area"""]   ,						lowerCamelCase		)		)
						# verify boxes
						lowerCAmelCase_      :				Dict      =       torch.Size([6, 4]		)
						self.assertEqual(encoding["""labels"""][0]["""boxes"""].shape   ,						lowerCamelCase		)
						lowerCAmelCase_      :				List[Any]      =       torch.tensor([0.2_625, 0.5_437, 0.4_688, 0.8_625]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""boxes"""][0]   ,						lowerCamelCase   ,						atol=1E-3		)		)
						# verify image_id
						lowerCAmelCase_      :				Union[str, Any]      =       torch.tensor([3_97_69]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""image_id"""]   ,						lowerCamelCase		)		)
						# verify is_crowd
						lowerCAmelCase_      :				str      =       torch.tensor([0, 0, 0, 0, 0, 0]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""iscrowd"""]   ,						lowerCamelCase		)		)
						# verify class_labels
						lowerCAmelCase_      :				str      =       torch.tensor([17, 17, 63, 75, 75, 93]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""class_labels"""]   ,						lowerCamelCase		)		)
						# verify masks
						lowerCAmelCase_      :				Dict      =       82_28_73
						self.assertEqual(encoding["""labels"""][0]["""masks"""].sum().item()   ,						lowerCamelCase		)
						# verify orig_size
						lowerCAmelCase_      :				List[Any]      =       torch.tensor([4_80, 6_40]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""orig_size"""]   ,						lowerCamelCase		)		)
						# verify size
						lowerCAmelCase_      :				str      =       torch.tensor([8_00, 10_66]		)
						self.assertTrue(torch.allclose(encoding["""labels"""][0]["""size"""]   ,						lowerCamelCase		)		)







'''simple docstring'''






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


__A :  str    							=				"Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"







def     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				List[Any]      =       _ask_options(
      """In which compute environment are you running?"""							,   ["""This machine""", """AWS (Amazon SageMaker)"""]							,   _convert_compute_environment							,   )
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    lowerCAmelCase_      :				str      =       get_sagemaker_input()
  else:
    lowerCAmelCase_      :				Optional[int]      =       get_cluster_input()
  return config







def     UpperCamelCase_      (   A__ :       Optional[Any]=None ):
  '''simple docstring'''





  if subparsers is not None:
    lowerCAmelCase_      :				List[str]      =       subparsers.add_parser("""config"""							,   description=A__ )
  else:
    lowerCAmelCase_      :				Optional[int]      =       argparse.ArgumentParser("""Accelerate config command"""							,   description=A__ )

  parser.add_argument(
      """--config_file"""							,   default=A__							,   help=(
          """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """
          """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """
          """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """
          """with 'huggingface'."""
      )							,   )

  if subparsers is not None:
    parser.set_defaults(func=A__ )
  return parser







def     UpperCamelCase_      (   A__ :       Any ):
  '''simple docstring'''





  lowerCAmelCase_      :				Dict      =       get_user_input()
  if args.config_file is not None:
    lowerCAmelCase_      :				List[str]      =       args.config_file
  else:
    if not os.path.isdir(A__ ):
      os.makedirs(A__ )
    lowerCAmelCase_      :				List[Any]      =       default_yaml_config_file

  if config_file.endswith(""".json""" ):
    config.to_json_file(A__ )
  else:
    config.to_yaml_file(A__ )
  print(f'accelerate configuration saved at {config_file}' )







def     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				str      =       config_command_parser()
  lowerCAmelCase_      :				Tuple      =       parser.parse_args()
  config_command(A__ )


if __name__ == "__main__":
  main()



