






"""simple docstring"""
import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
			import torch

if is_tf_available():
			import tensorflow as tf


@require_vision
@require_torchvision
class _lowerCAmelCase						(    unittest.TestCase       ):







						def 			A      (    self							)		->							Any:
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    tempfile.mkdtemp()
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    SamImageProcessor()
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    SamProcessor(__lowerCAmelCase							)
										processor.save_pretrained(self.tmpdirname							)







						def 			A      (    self   ,				**lowerCAmelCase_							)		->							Dict:
										return AutoProcessor.from_pretrained(self.tmpdirname   ,				**__lowerCAmelCase							).image_processor







						def 			A      (    self							)		->							Optional[int]:
										shutil.rmtree(self.tmpdirname							)







						def 			A      (    self							)		->							Dict:
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    [np.random.randint(2_5_5   ,				size=(3, 3_0, 4_0_0)   ,				dtype=np.uinta							)]

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    [Image.fromarray(np.moveaxis(__lowerCAmelCase   ,				0   ,				-1							)							) for x in image_inputs]

										return image_inputs







						def 			A      (    self							)		->							int:
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    SamProcessor(image_processor=self.get_image_processor()							)
										processor.save_pretrained(self.tmpdirname							)

										_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.get_image_processor(do_normalize=__lowerCAmelCase   ,				padding_value=1.0							)

										_SCREAMING_SNAKE_CASE   :		Dict       		=    SamProcessor.from_pretrained(self.tmpdirname   ,				do_normalize=__lowerCAmelCase   ,				padding_value=1.0							)

										self.assertEqual(processor.image_processor.to_json_string()   ,				image_processor_add_kwargs.to_json_string()							)
										self.assertIsInstance(processor.image_processor   ,				__lowerCAmelCase							)







						def 			A      (    self							)		->							Tuple:
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    self.get_image_processor()

										_SCREAMING_SNAKE_CASE   :		str       		=    SamProcessor(image_processor=__lowerCAmelCase							)

										_SCREAMING_SNAKE_CASE   :		Any       		=    self.prepare_image_inputs()

										_SCREAMING_SNAKE_CASE   :		Tuple       		=    image_processor(__lowerCAmelCase   ,				return_tensors='np'							)
										_SCREAMING_SNAKE_CASE   :		Dict       		=    processor(images=__lowerCAmelCase   ,				return_tensors='np'							)

										input_feat_extract.pop('original_sizes'							)  # pop original_sizes as it is popped in the processor
										input_feat_extract.pop('reshaped_input_sizes'							)  # pop original_sizes as it is popped in the processor

										for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum()   ,				input_processor[key].sum()   ,				delta=1e-2							)







						@require_torch
						def 			A      (    self							)		->							Any:
										_SCREAMING_SNAKE_CASE   :		str       		=    self.get_image_processor()

										_SCREAMING_SNAKE_CASE   :		List[str]       		=    SamProcessor(image_processor=__lowerCAmelCase							)
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    [torch.ones((1, 3, 5, 5)							)]

										_SCREAMING_SNAKE_CASE   :		int       		=    [[1_7_6_4, 2_6_4_6]]

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    [[6_8_3, 1_0_2_4]]
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    processor.post_process_masks(__lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase							)
										self.assertEqual(masks[0].shape   ,				(1, 3, 1_7_6_4, 2_6_4_6)							)

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    processor.post_process_masks(
										    __lowerCAmelCase   ,				torch.tensor(__lowerCAmelCase							)   ,				torch.tensor(__lowerCAmelCase							)							)
										self.assertEqual(masks[0].shape   ,				(1, 3, 1_7_6_4, 2_6_4_6)							)

										# should also work with np
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    [np.ones((1, 3, 5, 5)							)]
										_SCREAMING_SNAKE_CASE   :		Any       		=    processor.post_process_masks(__lowerCAmelCase   ,				np.array(__lowerCAmelCase							)   ,				np.array(__lowerCAmelCase							)							)

										self.assertEqual(masks[0].shape   ,				(1, 3, 1_7_6_4, 2_6_4_6)							)

										_SCREAMING_SNAKE_CASE   :		Dict       		=    [[1, 0], [0, 1]]
										with self.assertRaises(__lowerCAmelCase							):
														_SCREAMING_SNAKE_CASE   :		Any       		=    processor.post_process_masks(__lowerCAmelCase   ,				np.array(__lowerCAmelCase							)   ,				np.array(__lowerCAmelCase							)							)







@require_vision
@require_tf
class _lowerCAmelCase						(    unittest.TestCase       ):







						def 			A      (    self							)		->							Any:
										_SCREAMING_SNAKE_CASE   :		Dict       		=    tempfile.mkdtemp()
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    SamImageProcessor()
										_SCREAMING_SNAKE_CASE   :		Dict       		=    SamProcessor(__lowerCAmelCase							)
										processor.save_pretrained(self.tmpdirname							)







						def 			A      (    self   ,				**lowerCAmelCase_							)		->							int:
										return AutoProcessor.from_pretrained(self.tmpdirname   ,				**__lowerCAmelCase							).image_processor







						def 			A      (    self							)		->							Tuple:
										shutil.rmtree(self.tmpdirname							)







						def 			A      (    self							)		->							int:
										_SCREAMING_SNAKE_CASE   :		str       		=    [np.random.randint(2_5_5   ,				size=(3, 3_0, 4_0_0)   ,				dtype=np.uinta							)]

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    [Image.fromarray(np.moveaxis(__lowerCAmelCase   ,				0   ,				-1							)							) for x in image_inputs]

										return image_inputs







						def 			A      (    self							)		->							Tuple:
										_SCREAMING_SNAKE_CASE   :		int       		=    SamProcessor(image_processor=self.get_image_processor()							)
										processor.save_pretrained(self.tmpdirname							)

										_SCREAMING_SNAKE_CASE   :		List[str]       		=    self.get_image_processor(do_normalize=__lowerCAmelCase   ,				padding_value=1.0							)

										_SCREAMING_SNAKE_CASE   :		Any       		=    SamProcessor.from_pretrained(self.tmpdirname   ,				do_normalize=__lowerCAmelCase   ,				padding_value=1.0							)

										self.assertEqual(processor.image_processor.to_json_string()   ,				image_processor_add_kwargs.to_json_string()							)
										self.assertIsInstance(processor.image_processor   ,				__lowerCAmelCase							)







						def 			A      (    self							)		->							Optional[int]:
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    self.get_image_processor()

										_SCREAMING_SNAKE_CASE   :		str       		=    SamProcessor(image_processor=__lowerCAmelCase							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    self.prepare_image_inputs()

										_SCREAMING_SNAKE_CASE   :		Any       		=    image_processor(__lowerCAmelCase   ,				return_tensors='np'							)
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    processor(images=__lowerCAmelCase   ,				return_tensors='np'							)

										input_feat_extract.pop('original_sizes'							)  # pop original_sizes as it is popped in the processor
										input_feat_extract.pop('reshaped_input_sizes'							)  # pop reshaped_input_sizes as it is popped in the processor

										for key in input_feat_extract.keys():
														self.assertAlmostEqual(input_feat_extract[key].sum()   ,				input_processor[key].sum()   ,				delta=1e-2							)







						@require_tf
						def 			A      (    self							)		->							Any:
										_SCREAMING_SNAKE_CASE   :		Any       		=    self.get_image_processor()

										_SCREAMING_SNAKE_CASE   :		List[str]       		=    SamProcessor(image_processor=__lowerCAmelCase							)
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    [tf.ones((1, 3, 5, 5)							)]

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    [[1_7_6_4, 2_6_4_6]]

										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    [[6_8_3, 1_0_2_4]]
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    processor.post_process_masks(__lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase   ,				return_tensors='tf'							)
										self.assertEqual(masks[0].shape   ,				(1, 3, 1_7_6_4, 2_6_4_6)							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    processor.post_process_masks(
										    __lowerCAmelCase   ,				tf.convert_to_tensor(__lowerCAmelCase							)   ,				tf.convert_to_tensor(__lowerCAmelCase							)   ,				return_tensors='tf'   ,				)
										self.assertEqual(masks[0].shape   ,				(1, 3, 1_7_6_4, 2_6_4_6)							)

										# should also work with np
										_SCREAMING_SNAKE_CASE   :		Any       		=    [np.ones((1, 3, 5, 5)							)]
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    processor.post_process_masks(
										    __lowerCAmelCase   ,				np.array(__lowerCAmelCase							)   ,				np.array(__lowerCAmelCase							)   ,				return_tensors='tf'							)

										self.assertEqual(masks[0].shape   ,				(1, 3, 1_7_6_4, 2_6_4_6)							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    [[1, 0], [0, 1]]
										with self.assertRaises(tf.errors.InvalidArgumentError							):
														_SCREAMING_SNAKE_CASE   :		List[Any]       		=    processor.post_process_masks(
														    __lowerCAmelCase   ,				np.array(__lowerCAmelCase							)   ,				np.array(__lowerCAmelCase							)   ,				return_tensors='tf'							)







@require_vision
@require_torchvision
class _lowerCAmelCase						(    unittest.TestCase       ):







						def 			A      (    self							)		->							str:
										_SCREAMING_SNAKE_CASE   :		str       		=    tempfile.mkdtemp()
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    SamImageProcessor()
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    SamProcessor(__lowerCAmelCase							)
										processor.save_pretrained(self.tmpdirname							)







						def 			A      (    self   ,				**lowerCAmelCase_							)		->							Any:
										return AutoProcessor.from_pretrained(self.tmpdirname   ,				**__lowerCAmelCase							).image_processor







						def 			A      (    self							)		->							Optional[Any]:
										shutil.rmtree(self.tmpdirname							)







						def 			A      (    self							)		->							Union[str, Any]:
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    [np.random.randint(2_5_5   ,				size=(3, 3_0, 4_0_0)   ,				dtype=np.uinta							)]

										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    [Image.fromarray(np.moveaxis(__lowerCAmelCase   ,				0   ,				-1							)							) for x in image_inputs]

										return image_inputs







						@is_pt_tf_cross_test
						def 			A      (    self							)		->							str:
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    self.get_image_processor()

										_SCREAMING_SNAKE_CASE   :		Any       		=    SamProcessor(image_processor=__lowerCAmelCase							)
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    np.random.randint(0   ,				2   ,				size=(1, 3, 5, 5)							).astype(np.floataa							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    [tf.convert_to_tensor(__lowerCAmelCase							)]
										_SCREAMING_SNAKE_CASE   :		int       		=    [torch.tensor(__lowerCAmelCase							)]

										_SCREAMING_SNAKE_CASE   :		Any       		=    [[1_7_6_4, 2_6_4_6]]

										_SCREAMING_SNAKE_CASE   :		int       		=    [[6_8_3, 1_0_2_4]]
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    processor.post_process_masks(
										    __lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase   ,				return_tensors='tf'							)
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    processor.post_process_masks(
										    __lowerCAmelCase   ,				__lowerCAmelCase   ,				__lowerCAmelCase   ,				return_tensors='pt'							)

										self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)







						@is_pt_tf_cross_test
						def 			A      (    self							)		->							List[Any]:
										_SCREAMING_SNAKE_CASE   :		int       		=    self.get_image_processor()

										_SCREAMING_SNAKE_CASE   :		List[str]       		=    SamProcessor(image_processor=__lowerCAmelCase							)

										_SCREAMING_SNAKE_CASE   :		Dict       		=    self.prepare_image_inputs()

										_SCREAMING_SNAKE_CASE   :		Any       		=    image_processor(__lowerCAmelCase   ,				return_tensors='pt'							)['pixel_values'].numpy()
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    processor(images=__lowerCAmelCase   ,				return_tensors='pt'							)['pixel_values'].numpy()

										_SCREAMING_SNAKE_CASE   :		int       		=    image_processor(__lowerCAmelCase   ,				return_tensors='tf'							)['pixel_values'].numpy()
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    processor(images=__lowerCAmelCase   ,				return_tensors='tf'							)['pixel_values'].numpy()

										self.assertTrue(np.allclose(__lowerCAmelCase   ,				__lowerCAmelCase							)							)
										self.assertTrue(np.allclose(__lowerCAmelCase   ,				__lowerCAmelCase							)							)
										self.assertTrue(np.allclose(__lowerCAmelCase   ,				__lowerCAmelCase							)							)






from __future__ import annotations

def 	SCREAMING_SNAKE_CASE_							(	_snake_case	:dict	,   _snake_case	:str				)  ->     set[str]:
   _A  ,    _A      =       set(_snake_case				), [start]

   while stack:
      _A      =       stack.pop()
      explored.add(_snake_case				)
      # Differences from BFS:
      # 1) pop last element instead of first one
      # 2) add adjacent elements to stack without exploring them
      for adj in reversed(graph[v]				):
         if adj not in explored:
            stack.append(_snake_case				)
   return explored


UpperCAmelCase_		 =					{
    """A""": ["""B""", """C""", """D"""],
    """B""": ["""A""", """D""", """E"""],
    """C""": ["""A""", """F"""],
    """D""": ["""B""", """D"""],
    """E""": ["""B""", """F"""],
    """F""": ["""C""", """E""", """G"""],
    """G""": ["""F"""],
}

if __name__ == "__main__":
   import doctest

   doctest.testmod()
   print(depth_first_search(G, """A"""))



