



'''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     ):
  '''simple docstring'''




  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      lowerCAmelCase__ :  int							      =					tempfile.mkdtemp()
      lowerCAmelCase__ :  Union[str, Any]							      =					SamImageProcessor()
      lowerCAmelCase__ :  int							      =					SamProcessor(__UpperCAmelCase      )
      processor.save_pretrained(self.tmpdirname      )




  def 							UpperCAmelCase_	(    self  ,**__UpperCAmelCase      )    ->		int:
      return AutoProcessor.from_pretrained(self.tmpdirname  ,**__UpperCAmelCase      ).image_processor




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




  def 							UpperCAmelCase_	(    self      )    ->		Any:
      lowerCAmelCase__ :  Dict							      =					[np.random.randint(255  ,size=(3, 30, 400)  ,dtype=np.uinta      )]

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

      return image_inputs




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

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

      lowerCAmelCase__ :  Union[str, Any]							      =					SamProcessor.from_pretrained(self.tmpdirname  ,do_normalize=__UpperCAmelCase  ,padding_value=1.0      )

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




  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      lowerCAmelCase__ :  Dict							      =					self.get_image_processor()

      lowerCAmelCase__ :  List[str]							      =					SamProcessor(image_processor=__UpperCAmelCase      )

      lowerCAmelCase__ :  int							      =					self.prepare_image_inputs()

      lowerCAmelCase__ :  List[Any]							      =					image_processor(__UpperCAmelCase  ,return_tensors="""np"""      )
      lowerCAmelCase__ :  int							      =					processor(images=__UpperCAmelCase  ,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      )    ->		List[Any]:
      lowerCAmelCase__ :  List[str]							      =					self.get_image_processor()

      lowerCAmelCase__ :  Optional[Any]							      =					SamProcessor(image_processor=__UpperCAmelCase      )
      lowerCAmelCase__ :  int							      =					[torch.ones((1, 3, 5, 5)      )]

      lowerCAmelCase__ :  Union[str, Any]							      =					[[1764, 2646]]

      lowerCAmelCase__ :  List[Any]							      =					[[683, 1024]]
      lowerCAmelCase__ :  List[str]							      =					processor.post_process_masks(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      )
      self.assertEqual(masks[0].shape  ,(1, 3, 1764, 2646)      )

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

      # should also work with np
      lowerCAmelCase__ :  List[Any]							      =					[np.ones((1, 3, 5, 5)      )]
      lowerCAmelCase__ :  Dict							      =					processor.post_process_masks(__UpperCAmelCase  ,np.array(__UpperCAmelCase      )  ,np.array(__UpperCAmelCase      )      )

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

      lowerCAmelCase__ :  int							      =					[[1, 0], [0, 1]]
      with self.assertRaises(__UpperCAmelCase      ):
          lowerCAmelCase__ :  List[str]							      =					processor.post_process_masks(__UpperCAmelCase  ,np.array(__UpperCAmelCase      )  ,np.array(__UpperCAmelCase      )      )







@require_vision
@require_tf
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




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




  def 							UpperCAmelCase_	(    self  ,**__UpperCAmelCase      )    ->		List[Any]:
      return AutoProcessor.from_pretrained(self.tmpdirname  ,**__UpperCAmelCase      ).image_processor




  def 							UpperCAmelCase_	(    self      )    ->		Dict:
      shutil.rmtree(self.tmpdirname      )




  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  str							      =					[np.random.randint(255  ,size=(3, 30, 400)  ,dtype=np.uinta      )]

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

      return image_inputs




  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      lowerCAmelCase__ :  Union[str, Any]							      =					SamProcessor(image_processor=self.get_image_processor()      )
      processor.save_pretrained(self.tmpdirname      )

      lowerCAmelCase__ :  Dict							      =					self.get_image_processor(do_normalize=__UpperCAmelCase  ,padding_value=1.0      )

      lowerCAmelCase__ :  List[Any]							      =					SamProcessor.from_pretrained(self.tmpdirname  ,do_normalize=__UpperCAmelCase  ,padding_value=1.0      )

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




  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      lowerCAmelCase__ :  Tuple							      =					self.get_image_processor()

      lowerCAmelCase__ :  int							      =					SamProcessor(image_processor=__UpperCAmelCase      )

      lowerCAmelCase__ :  int							      =					self.prepare_image_inputs()

      lowerCAmelCase__ :  Any							      =					image_processor(__UpperCAmelCase  ,return_tensors="""np"""      )
      lowerCAmelCase__ :  Union[str, Any]							      =					processor(images=__UpperCAmelCase  ,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      )    ->		Any:
      lowerCAmelCase__ :  Union[str, Any]							      =					self.get_image_processor()

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

      lowerCAmelCase__ :  str							      =					[[1764, 2646]]

      lowerCAmelCase__ :  Optional[Any]							      =					[[683, 1024]]
      lowerCAmelCase__ :  Optional[Any]							      =					processor.post_process_masks(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,return_tensors="""tf"""      )
      self.assertEqual(masks[0].shape  ,(1, 3, 1764, 2646)      )

      lowerCAmelCase__ :  Union[str, Any]							      =					processor.post_process_masks(
          __UpperCAmelCase  ,tf.convert_to_tensor(__UpperCAmelCase      )  ,tf.convert_to_tensor(__UpperCAmelCase      )  ,return_tensors="""tf"""  ,)
      self.assertEqual(masks[0].shape  ,(1, 3, 1764, 2646)      )

      # should also work with np
      lowerCAmelCase__ :  Union[str, Any]							      =					[np.ones((1, 3, 5, 5)      )]
      lowerCAmelCase__ :  Any							      =					processor.post_process_masks(
          __UpperCAmelCase  ,np.array(__UpperCAmelCase      )  ,np.array(__UpperCAmelCase      )  ,return_tensors="""tf"""      )

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

      lowerCAmelCase__ :  List[Any]							      =					[[1, 0], [0, 1]]
      with self.assertRaises(tf.errors.InvalidArgumentError      ):
          lowerCAmelCase__ :  List[Any]							      =					processor.post_process_masks(
              __UpperCAmelCase  ,np.array(__UpperCAmelCase      )  ,np.array(__UpperCAmelCase      )  ,return_tensors="""tf"""      )







@require_vision
@require_torchvision
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  def 							UpperCAmelCase_	(    self      )    ->		int:
      lowerCAmelCase__ :  Any							      =					tempfile.mkdtemp()
      lowerCAmelCase__ :  Optional[Any]							      =					SamImageProcessor()
      lowerCAmelCase__ :  Tuple							      =					SamProcessor(__UpperCAmelCase      )
      processor.save_pretrained(self.tmpdirname      )




  def 							UpperCAmelCase_	(    self  ,**__UpperCAmelCase      )    ->		List[Any]:
      return AutoProcessor.from_pretrained(self.tmpdirname  ,**__UpperCAmelCase      ).image_processor




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




  def 							UpperCAmelCase_	(    self      )    ->		str:
      lowerCAmelCase__ :  Optional[Any]							      =					[np.random.randint(255  ,size=(3, 30, 400)  ,dtype=np.uinta      )]

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

      return image_inputs




  @is_pt_tf_cross_test
  def 							UpperCAmelCase_	(    self      )    ->		Any:
      lowerCAmelCase__ :  Any							      =					self.get_image_processor()

      lowerCAmelCase__ :  List[Any]							      =					SamProcessor(image_processor=__UpperCAmelCase      )
      lowerCAmelCase__ :  Dict							      =					np.random.randint(0  ,2  ,size=(1, 3, 5, 5)      ).astype(np.floataa      )
      lowerCAmelCase__ :  int							      =					[tf.convert_to_tensor(__UpperCAmelCase      )]
      lowerCAmelCase__ :  Dict							      =					[torch.tensor(__UpperCAmelCase      )]

      lowerCAmelCase__ :  Optional[int]							      =					[[1764, 2646]]

      lowerCAmelCase__ :  Dict							      =					[[683, 1024]]
      lowerCAmelCase__ :  Optional[Any]							      =					processor.post_process_masks(
          __UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,return_tensors="""tf"""      )
      lowerCAmelCase__ :  List[str]							      =					processor.post_process_masks(
          __UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase  ,return_tensors="""pt"""      )

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






  @is_pt_tf_cross_test
  def 							UpperCAmelCase_	(    self      )    ->		List[Any]:
      lowerCAmelCase__ :  Tuple							      =					self.get_image_processor()

      lowerCAmelCase__ :  Tuple							      =					SamProcessor(image_processor=__UpperCAmelCase      )

      lowerCAmelCase__ :  int							      =					self.prepare_image_inputs()

      lowerCAmelCase__ :  Union[str, Any]							      =					image_processor(__UpperCAmelCase  ,return_tensors="""pt"""      )["""pixel_values"""].numpy()
      lowerCAmelCase__ :  Dict							      =					processor(images=__UpperCAmelCase  ,return_tensors="""pt"""      )["""pixel_values"""].numpy()

      lowerCAmelCase__ :  Optional[Any]							      =					image_processor(__UpperCAmelCase  ,return_tensors="""tf"""      )["""pixel_values"""].numpy()
      lowerCAmelCase__ :  int							      =					processor(images=__UpperCAmelCase  ,return_tensors="""tf"""      )["""pixel_values"""].numpy()

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







'''simple docstring'''





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow


if is_torch_available():
 import torch

 from transformers import XLMRobertaModel






@require_sentencepiece
@require_tokenizers
@require_torch
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  @slow
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  Tuple							      =					XLMRobertaModel.from_pretrained("""xlm-roberta-base"""      )
      lowerCAmelCase__ :  Optional[int]							      =					torch.tensor([[0, 581, 1_0269, 83, 9_9942, 136, 6_0742, 23, 70, 8_0583, 1_8276, 2]]      )
      # The dog is cute and lives in the garden house

      lowerCAmelCase__ :  str							      =					torch.Size((1, 12, 768)      )  # batch_size, sequence_length, embedding_vector_dim
      lowerCAmelCase__ :  Dict							      =					torch.tensor(
          [[-0.0_1_0_1, 0.1_2_1_8, -0.0_8_0_3, 0.0_8_0_1, 0.1_3_2_7, 0.0_7_7_6, -0.1_2_1_5, 0.2_3_8_3, 0.3_3_3_8, 0.3_1_0_6, 0.0_3_0_0, 0.0_2_5_2]]      )
      #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.base')
      #  xlmr.eval()
      #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
      with torch.no_grad():
          lowerCAmelCase__ :  Optional[int]							      =					model(__UpperCAmelCase      )["""last_hidden_state"""].detach()
      self.assertEqual(output.shape  ,__UpperCAmelCase      )
      # compare the actual values for a slice of last dim
      self.assertTrue(torch.allclose(output[:, :, -1]  ,__UpperCAmelCase  ,atol=1E-3      )      )






  @slow
  def 							UpperCAmelCase_	(    self      )    ->		int:
      lowerCAmelCase__ :  Union[str, Any]							      =					XLMRobertaModel.from_pretrained("""xlm-roberta-large"""      )
      lowerCAmelCase__ :  Optional[Any]							      =					torch.tensor([[0, 581, 1_0269, 83, 9_9942, 136, 6_0742, 23, 70, 8_0583, 1_8276, 2]]      )
      # The dog is cute and lives in the garden house

      lowerCAmelCase__ :  Dict							      =					torch.Size((1, 12, 1024)      )  # batch_size, sequence_length, embedding_vector_dim
      lowerCAmelCase__ :  Union[str, Any]							      =					torch.tensor(
          [[-0.0_6_9_9, -0.0_3_1_8, 0.0_7_0_5, -0.1_2_4_1, 0.0_9_9_9, -0.0_5_2_0, 0.1_0_0_4, -0.1_8_3_8, -0.4_7_0_4, 0.1_4_3_7, 0.0_8_2_1, 0.0_1_2_6]]      )
      #  xlmr = torch.hub.load('pytorch/fairseq', 'xlmr.large')
      #  xlmr.eval()
      #  expected_output_values_last_dim = xlmr.extract_features(input_ids[0])[:, :, -1]
      with torch.no_grad():
          lowerCAmelCase__ :  Union[str, Any]							      =					model(__UpperCAmelCase      )["""last_hidden_state"""].detach()
      self.assertEqual(output.shape  ,__UpperCAmelCase      )
      # compare the actual values for a slice of last dim
      self.assertTrue(torch.allclose(output[:, :, -1]  ,__UpperCAmelCase  ,atol=1E-3      )      )


