





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






class   UpperCAmelCase      (			unittest.TestCase							):
 def __init__(							self :Dict		,			lowercase_ :Optional[int]		,			lowercase_ :Any=7		,			lowercase_ :int=3		,			lowercase_ :Tuple=30		,			lowercase_ :Dict=4_00		,			lowercase_ :Dict=True		,			lowercase_ :str=None		,			lowercase_ :Dict=True		,			lowercase_ :List[str]=[0.5, 0.5, 0.5]		,			lowercase_ :List[Any]=[0.5, 0.5, 0.5]		,			lowercase_ :List[Any]=True		,			lowercase_ :str=1 / 2_55		,			lowercase_ :int=True		,			)->				Tuple:
     # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
     A__							=     size if size is not None else {"shortest_edge": 18, "longest_edge": 13_33}
     A__							=     parent
     A__							=     batch_size
     A__							=     num_channels
     A__							=     min_resolution
     A__							=     max_resolution
     A__							=     do_resize
     A__							=     size
     A__							=     do_normalize
     A__							=     image_mean
     A__							=     image_std
     A__							=     do_rescale
     A__							=     rescale_factor
     A__							=     do_pad
 def 	UpperCAmelCase_				(							self :List[str]				)->				List[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 	UpperCAmelCase_				(							self :Optional[Any]		,			lowercase_ :Dict		,			lowercase_ :Tuple=False				)->				Any:
     if not batched:
         A__							=     image_inputs[0]
         if isinstance(lowercase_		,			Image.Image				):
             A__,      A__							=     image.size
         else:
             A__,      A__							=     image.shape[1], image.shape[2]
         if w < h:
             A__							=     int(self.size["shortest_edge"] * h / w				)
             A__							=     self.size["shortest_edge"]
         elif w > h:
             A__							=     self.size["shortest_edge"]
             A__							=     int(self.size["shortest_edge"] * w / h				)
         else:
             A__							=     self.size["shortest_edge"]
             A__							=     self.size["shortest_edge"]

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

     return expected_height, expected_width






@require_torch
@require_vision
class   UpperCAmelCase      (			UpperCamelCase__     ,			unittest.TestCase							):
 __lowercase  						=							DetaImageProcessor if is_vision_available() else None
 def 	UpperCAmelCase_				(							self :List[str]				)->				Optional[Any]:
     A__							=     DetaImageProcessingTester(self				)
 @property
 def 	UpperCAmelCase_				(							self :Tuple				)->				Union[str, Any]:
     return self.image_processor_tester.prepare_image_processor_dict()
 def 	UpperCAmelCase_				(							self :int				)->				Union[str, Any]:
     A__							=     self.image_processing_class(**self.image_processor_dict				)
     self.assertTrue(hasattr(lowercase_		,			"image_mean"				)				)
     self.assertTrue(hasattr(lowercase_		,			"image_std"				)				)
     self.assertTrue(hasattr(lowercase_		,			"do_normalize"				)				)
     self.assertTrue(hasattr(lowercase_		,			"do_resize"				)				)
     self.assertTrue(hasattr(lowercase_		,			"do_rescale"				)				)
     self.assertTrue(hasattr(lowercase_		,			"do_pad"				)				)
     self.assertTrue(hasattr(lowercase_		,			"size"				)				)
 def 	UpperCAmelCase_				(							self :Tuple				)->				Any:
     A__							=     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		,			lowercase_				)
 def 	UpperCAmelCase_				(							self :Optional[int]				)->				Dict:
     pass
 def 	UpperCAmelCase_				(							self :Union[str, Any]				)->				List[Any]:
     # Initialize image_processing
     A__							=     self.image_processing_class(**self.image_processor_dict				)
     # create random PIL images
     A__							=     prepare_image_inputs(self.image_processor_tester		,			equal_resolution=lowercase_				)
     for image in image_inputs:
         self.assertIsInstance(lowercase_		,			Image.Image				)

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

     A__,      A__							=     self.image_processor_tester.get_expected_values(lowercase_				)

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

     # Test batched
     A__,      A__							=     self.image_processor_tester.get_expected_values(lowercase_		,			batched=lowercase_				)

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

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

     A__,      A__							=     self.image_processor_tester.get_expected_values(lowercase_				)

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

     # Test batched
     A__							=     image_processing(lowercase_		,			return_tensors="pt"				).pixel_values

     A__,      A__							=     self.image_processor_tester.get_expected_values(lowercase_		,			batched=lowercase_				)

     self.assertEqual(
         encoded_images.shape		,			(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             expected_height,
             expected_width,
         )		,			)
 def 	UpperCAmelCase_				(							self :Tuple				)->				str:
     # Initialize image_processing
     A__							=     self.image_processing_class(**self.image_processor_dict				)
     # create random PyTorch tensors
     A__							=     prepare_image_inputs(self.image_processor_tester		,			equal_resolution=lowercase_		,			torchify=lowercase_				)
     for image in image_inputs:
         self.assertIsInstance(lowercase_		,			torch.Tensor				)

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

     A__,      A__							=     self.image_processor_tester.get_expected_values(lowercase_				)

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

     # Test batched
     A__							=     image_processing(lowercase_		,			return_tensors="pt"				).pixel_values

     A__,      A__							=     self.image_processor_tester.get_expected_values(lowercase_		,			batched=lowercase_				)

     self.assertEqual(
         encoded_images.shape		,			(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             expected_height,
             expected_width,
         )		,			)
 @slow
 def 	UpperCAmelCase_				(							self :Union[str, Any]				)->				Union[str, Any]:
     # prepare image and target
     A__							=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
     with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt"		,			"r"				) as f:
         A__							=     json.loads(f.read()				)

     A__							=     {"image_id": 3_97_69, "annotations": target}

     # encode them
     A__							=     DetaImageProcessor()
     A__							=     image_processing(images=lowercase_		,			annotations=lowercase_		,			return_tensors="pt"				)

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

     A__							=     torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]				)
     self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]		,			lowercase_		,			atol=1E-4				)				)

     # verify area
     A__							=     torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["area"]		,			lowercase_				)				)
     # verify boxes
     A__							=     torch.Size([6, 4]				)
     self.assertEqual(encoding["labels"][0]["boxes"].shape		,			lowercase_				)
     A__							=     torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]		,			lowercase_		,			atol=1E-3				)				)
     # verify image_id
     A__							=     torch.tensor([3_97_69]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]		,			lowercase_				)				)
     # verify is_crowd
     A__							=     torch.tensor([0, 0, 0, 0, 0, 0]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]		,			lowercase_				)				)
     # verify class_labels
     A__							=     torch.tensor([75, 75, 63, 65, 17, 17]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]		,			lowercase_				)				)
     # verify orig_size
     A__							=     torch.tensor([4_80, 6_40]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]		,			lowercase_				)				)
     # verify size
     A__							=     torch.tensor([8_00, 10_66]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["size"]		,			lowercase_				)				)



 @slow
 def 	UpperCAmelCase_				(							self :List[str]				)->				int:
     # prepare image, target and masks_path
     A__							=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
     with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"		,			"r"				) as f:
         A__							=     json.loads(f.read()				)

     A__							=     {"file_name": "000000039769.png", "image_id": 3_97_69, "segments_info": target}

     A__							=     pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"				)

     # encode them
     A__							=     DetaImageProcessor(format="coco_panoptic"				)
     A__							=     image_processing(images=lowercase_		,			annotations=lowercase_		,			masks_path=lowercase_		,			return_tensors="pt"				)

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

     A__							=     torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]				)
     self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]		,			lowercase_		,			atol=1E-4				)				)

     # verify area
     A__							=     torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["area"]		,			lowercase_				)				)
     # verify boxes
     A__							=     torch.Size([6, 4]				)
     self.assertEqual(encoding["labels"][0]["boxes"].shape		,			lowercase_				)
     A__							=     torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]		,			lowercase_		,			atol=1E-3				)				)
     # verify image_id
     A__							=     torch.tensor([3_97_69]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]		,			lowercase_				)				)
     # verify is_crowd
     A__							=     torch.tensor([0, 0, 0, 0, 0, 0]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]		,			lowercase_				)				)
     # verify class_labels
     A__							=     torch.tensor([17, 17, 63, 75, 75, 93]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]		,			lowercase_				)				)
     # verify masks
     A__							=     82_28_73
     self.assertEqual(encoding["labels"][0]["masks"].sum().item()		,			lowercase_				)
     # verify orig_size
     A__							=     torch.tensor([4_80, 6_40]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]		,			lowercase_				)				)
     # verify size
     A__							=     torch.tensor([8_00, 10_66]				)
     self.assertTrue(torch.allclose(encoding["labels"][0]["size"]		,			lowercase_				)				)










'''simple docstring'''

import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
  import torch
  from torch import nn

  from transformers import (
      Adafactor,
      AdamW,
      get_constant_schedule,
      get_constant_schedule_with_warmup,
      get_cosine_schedule_with_warmup,
      get_cosine_with_hard_restarts_schedule_with_warmup,
      get_inverse_sqrt_schedule,
      get_linear_schedule_with_warmup,
      get_polynomial_decay_schedule_with_warmup,
  )





def 		UpperCamelCase							(				_lowerCamelCase   :   Any		,			_lowerCamelCase   :   Optional[Any]=10			):
    A__							=     []
    for _ in range(_lowerCamelCase			):
        lrs.append(scheduler.get_lr()[0]			)
        scheduler.step()
    return lrs





def 		UpperCamelCase							(				_lowerCamelCase   :   Tuple		,			_lowerCamelCase   :   List[str]=10			):
    A__							=     []
    for step in range(_lowerCamelCase			):
        lrs.append(scheduler.get_lr()[0]			)
        scheduler.step()
        if step == num_steps // 2:
            with tempfile.TemporaryDirectory() as tmpdirname:
                A__							=     os.path.join(_lowerCamelCase		,			"schedule.bin"			)
                torch.save(scheduler.state_dict()		,			_lowerCamelCase			)

                A__							=     torch.load(_lowerCamelCase			)
                scheduler.load_state_dict(_lowerCamelCase			)
    return lrs






@require_torch
class   UpperCAmelCase      (			unittest.TestCase							):
 def 	UpperCAmelCase_				(							self :int		,			lowercase_ :Any		,			lowercase_ :List[Any]		,			lowercase_ :Optional[Any]				)->				int:
     self.assertEqual(len(lowercase_				)		,			len(lowercase_				)				)
     for a, b in zip(lowercase_		,			lowercase_				):
         self.assertAlmostEqual(lowercase_		,			lowercase_		,			delta=lowercase_				)
 def 	UpperCAmelCase_				(							self :Union[str, Any]				)->				Dict:
     A__							=     torch.tensor([0.1, -0.2, -0.1]		,			requires_grad=lowercase_				)
     A__							=     torch.tensor([0.4, 0.2, -0.5]				)
     A__							=     nn.MSELoss()
     # No warmup, constant schedule, no gradient clipping
     A__							=     AdamW(params=[w]		,			lr=2E-1		,			weight_decay=0.0				)
     for _ in range(1_00				):
         A__							=     criterion(lowercase_		,			lowercase_				)
         loss.backward()
         optimizer.step()
         w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
         w.grad.zero_()
     self.assertListAlmostEqual(w.tolist()		,			[0.4, 0.2, -0.5]		,			tol=1E-2				)



 def 	UpperCAmelCase_				(							self :Tuple				)->				List[str]:
     A__							=     torch.tensor([0.1, -0.2, -0.1]		,			requires_grad=lowercase_				)
     A__							=     torch.tensor([0.4, 0.2, -0.5]				)
     A__							=     nn.MSELoss()
     # No warmup, constant schedule, no gradient clipping
     A__							=     Adafactor(
         params=[w]		,			lr=1E-2		,			eps=(1E-30, 1E-3)		,			clip_threshold=1.0		,			decay_rate=-0.8		,			betaa=lowercase_		,			weight_decay=0.0		,			relative_step=lowercase_		,			scale_parameter=lowercase_		,			warmup_init=lowercase_		,			)
     for _ in range(10_00				):
         A__							=     criterion(lowercase_		,			lowercase_				)
         loss.backward()
         optimizer.step()
         w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
         w.grad.zero_()
     self.assertListAlmostEqual(w.tolist()		,			[0.4, 0.2, -0.5]		,			tol=1E-2				)






@require_torch
class   UpperCAmelCase      (			unittest.TestCase							):
 __lowercase  						=							nn.Linear(50     ,			50							) if is_torch_available() else None
 __lowercase  						=							AdamW(m.parameters()     ,			lr=1_0.0							) if is_torch_available() else None
 __lowercase  						=							10
 def 	UpperCAmelCase_				(							self :Tuple		,			lowercase_ :Any		,			lowercase_ :List[Any]		,			lowercase_ :List[Any]		,			lowercase_ :str=None				)->				Optional[int]:
     self.assertEqual(len(lowercase_				)		,			len(lowercase_				)				)
     for a, b in zip(lowercase_		,			lowercase_				):
         self.assertAlmostEqual(lowercase_		,			lowercase_		,			delta=lowercase_		,			msg=lowercase_				)



 def 	UpperCAmelCase_				(							self :Optional[Any]				)->				Any:
     A__							=     {"num_warmup_steps": 2, "num_training_steps": 10}
     # schedulers doct format
     # function: (sched_args_dict, expected_learning_rates)
     A__							=     {
         get_constant_schedule: ({}, [1_0.0] * self.num_steps),
         get_constant_schedule_with_warmup: (
             {"num_warmup_steps": 4},
             [0.0, 2.5, 5.0, 7.5, 1_0.0, 1_0.0, 1_0.0, 1_0.0, 1_0.0, 1_0.0],
         ),
         get_linear_schedule_with_warmup: (
             {**common_kwargs},
             [0.0, 5.0, 1_0.0, 8.7_5, 7.5, 6.2_5, 5.0, 3.7_5, 2.5, 1.2_5],
         ),
         get_cosine_schedule_with_warmup: (
             {**common_kwargs},
             [0.0, 5.0, 1_0.0, 9.6_1, 8.5_3, 6.9_1, 5.0, 3.0_8, 1.4_6, 0.3_8],
         ),
         get_cosine_with_hard_restarts_schedule_with_warmup: (
             {**common_kwargs, "num_cycles": 2},
             [0.0, 5.0, 1_0.0, 8.5_3, 5.0, 1.4_6, 1_0.0, 8.5_3, 5.0, 1.4_6],
         ),
         get_polynomial_decay_schedule_with_warmup: (
             {**common_kwargs, "power": 2.0, "lr_end": 1E-7},
             [0.0, 5.0, 1_0.0, 7.6_5_6, 5.6_2_5, 3.9_0_6, 2.5, 1.4_0_6, 0.6_2_5, 0.1_5_6],
         ),
         get_inverse_sqrt_schedule: (
             {"num_warmup_steps": 2},
             [0.0, 5.0, 1_0.0, 8.1_6_5, 7.0_7_1, 6.3_2_5, 5.7_7_4, 5.3_4_5, 5.0, 4.7_1_4],
         ),
     }

     for scheduler_func, data in scheds.items():
         A__,      A__							=     data

         A__							=     scheduler_func(self.optimizer		,			**lowercase_				)
         self.assertEqual(len([scheduler.get_lr()[0]]				)		,			1				)
         A__							=     unwrap_schedule(lowercase_		,			self.num_steps				)
         self.assertListAlmostEqual(
             lowercase_		,			lowercase_		,			tol=1E-2		,			msg=F"failed for {scheduler_func} in normal scheduler"		,			)

         A__							=     scheduler_func(self.optimizer		,			**lowercase_				)
         if scheduler_func.__name__ != "get_constant_schedule":
             LambdaScheduleWrapper.wrap_scheduler(lowercase_				)  # wrap to test picklability of the schedule
         A__							=     unwrap_and_save_reload_schedule(lowercase_		,			self.num_steps				)
         self.assertListEqual(lowercase_		,			lowercase_		,			msg=F"failed for {scheduler_func} in save and reload"				)






class   UpperCAmelCase      :
 def __init__(							self :str		,			lowercase_ :List[str]				)->				Tuple:
     A__							=     fn
 def __call__(							self :List[Any]		,			*lowercase_ :Dict		,			**lowercase_ :Dict				)->				Tuple:
     return self.fn(*lowercase_		,			**lowercase_				)



 @classmethod
 def 	UpperCAmelCase_				(							self :Any		,			lowercase_ :Tuple				)->				List[Any]:
     A__							=     list(map(self		,			scheduler.lr_lambdas				)				)



