


'''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 							__UpperCamelCase    (		unittest.TestCase ):




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  tempfile.mkdtemp()
						__a      :	List[str]    =  SamImageProcessor()
						__a      :	int    =  SamProcessor(__a							)
						processor.save_pretrained(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self						,    **__a							):

						'''simple docstring'''





						return AutoProcessor.from_pretrained(self.tmpdirname						,    **__a							).image_processor




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						shutil.rmtree(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Optional[Any]    =  [np.random.randint(255						,    size=(3, 30, 400)						,    dtype=np.uinta							)]

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

						return image_inputs




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[Any]    =  SamProcessor(image_processor=self.get_image_processor()							)
						processor.save_pretrained(self.tmpdirname							)

						__a      :	List[Any]    =  self.get_image_processor(do_normalize=__a						,    padding_value=1.0							)

						__a      :	Tuple    =  SamProcessor.from_pretrained(self.tmpdirname						,    do_normalize=__a						,    padding_value=1.0							)

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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  self.get_image_processor()

						__a      :	Tuple    =  SamProcessor(image_processor=__a							)

						__a      :	Tuple    =  self.prepare_image_inputs()

						__a      :	Union[str, Any]    =  image_processor(__a						,    return_tensors='np'							)
						__a      :	Tuple    =  processor(images=__a						,    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 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Any    =  self.get_image_processor()

						__a      :	List[str]    =  SamProcessor(image_processor=__a							)
						__a      :	Tuple    =  [torch.ones((1, 3, 5, 5)							)]

						__a      :	int    =  [[1764, 2646]]

						__a      :	Optional[int]    =  [[683, 1024]]
						__a      :	Union[str, Any]    =  processor.post_process_masks(__a						,    __a						,    __a							)
						self.assertEqual(masks[0].shape						,    (1, 3, 1764, 2646)							)

						__a      :	int    =  processor.post_process_masks(
						    __a						,    torch.tensor(__a							)						,    torch.tensor(__a							)							)
						self.assertEqual(masks[0].shape						,    (1, 3, 1764, 2646)							)

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

						self.assertEqual(masks[0].shape						,    (1, 3, 1764, 2646)							)

						__a      :	Tuple    =  [[1, 0], [0, 1]]
						with self.assertRaises(__a							):
											__a      :	Optional[int]    =  processor.post_process_masks(__a						,    np.array(__a							)						,    np.array(__a							)							)





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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	str    =  tempfile.mkdtemp()
						__a      :	Union[str, Any]    =  SamImageProcessor()
						__a      :	str    =  SamProcessor(__a							)
						processor.save_pretrained(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self						,    **__a							):

						'''simple docstring'''





						return AutoProcessor.from_pretrained(self.tmpdirname						,    **__a							).image_processor




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						shutil.rmtree(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Dict    =  [np.random.randint(255						,    size=(3, 30, 400)						,    dtype=np.uinta							)]

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

						return image_inputs




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Dict    =  SamProcessor(image_processor=self.get_image_processor()							)
						processor.save_pretrained(self.tmpdirname							)

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

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

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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.get_image_processor()

						__a      :	str    =  SamProcessor(image_processor=__a							)

						__a      :	List[Any]    =  self.prepare_image_inputs()

						__a      :	Optional[Any]    =  image_processor(__a						,    return_tensors='np'							)
						__a      :	int    =  processor(images=__a						,    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 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Any    =  self.get_image_processor()

						__a      :	int    =  SamProcessor(image_processor=__a							)
						__a      :	Optional[int]    =  [tf.ones((1, 3, 5, 5)							)]

						__a      :	Optional[Any]    =  [[1764, 2646]]

						__a      :	Union[str, Any]    =  [[683, 1024]]
						__a      :	Any    =  processor.post_process_masks(__a						,    __a						,    __a						,    return_tensors='tf'							)
						self.assertEqual(masks[0].shape						,    (1, 3, 1764, 2646)							)

						__a      :	int    =  processor.post_process_masks(
						    __a						,    tf.convert_to_tensor(__a							)						,    tf.convert_to_tensor(__a							)						,    return_tensors='tf'						,    )
						self.assertEqual(masks[0].shape						,    (1, 3, 1764, 2646)							)

						# should also work with np
						__a      :	List[str]    =  [np.ones((1, 3, 5, 5)							)]
						__a      :	str    =  processor.post_process_masks(
						    __a						,    np.array(__a							)						,    np.array(__a							)						,    return_tensors='tf'							)

						self.assertEqual(masks[0].shape						,    (1, 3, 1764, 2646)							)

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





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




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Union[str, Any]    =  tempfile.mkdtemp()
						__a      :	int    =  SamImageProcessor()
						__a      :	Tuple    =  SamProcessor(__a							)
						processor.save_pretrained(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self						,    **__a							):

						'''simple docstring'''





						return AutoProcessor.from_pretrained(self.tmpdirname						,    **__a							).image_processor




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						shutil.rmtree(self.tmpdirname							)




	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	List[str]    =  [np.random.randint(255						,    size=(3, 30, 400)						,    dtype=np.uinta							)]

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

						return image_inputs




	@is_pt_tf_cross_test
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	Any    =  self.get_image_processor()

						__a      :	str    =  SamProcessor(image_processor=__a							)
						__a      :	List[str]    =  np.random.randint(0						,    2						,    size=(1, 3, 5, 5)							).astype(np.floataa							)
						__a      :	Tuple    =  [tf.convert_to_tensor(__a							)]
						__a      :	Any    =  [torch.tensor(__a							)]

						__a      :	List[Any]    =  [[1764, 2646]]

						__a      :	Optional[int]    =  [[683, 1024]]
						__a      :	Optional[int]    =  processor.post_process_masks(
						    __a						,    __a						,    __a						,    return_tensors='tf'							)
						__a      :	int    =  processor.post_process_masks(
						    __a						,    __a						,    __a						,    return_tensors='pt'							)

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





	@is_pt_tf_cross_test
	def 				__UpperCAmelCase							(       self							):

						'''simple docstring'''





						__a      :	int    =  self.get_image_processor()

						__a      :	Dict    =  SamProcessor(image_processor=__a							)

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

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

						__a      :	List[Any]    =  image_processor(__a						,    return_tensors='tf'							)['pixel_values'].numpy()
						__a      :	Tuple    =  processor(images=__a						,    return_tensors='tf'							)['pixel_values'].numpy()

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







'''simple docstring'''



import os






def   lowerCamelCase							():
					with open(os.path.dirname(_SCREAMING_SNAKE_CASE       ) + '/p022_names.txt'       ) as file:
										__a      :	List[Any]    =  str(file.readlines()[0]       )
										__a      :	str    =  names.replace('"'						,				''       ).split(','       )

					names.sort()

					__a      :	Union[str, Any]    =  0
					__a      :	Tuple    =  0

					for i, name in enumerate(_SCREAMING_SNAKE_CASE       ):
										for letter in name:
															name_score += ord(_SCREAMING_SNAKE_CASE       ) - 64

										total_score += (i + 1) * name_score
										__a      :	Any    =  0
					return total_score


if __name__ == "__main__":
	print(solution())



