


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
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 ViTImageProcessor







class 				lowerCamelCase__       (	unittest.TestCase):





     def __init__(self		,					UpperCAmelCase		,					UpperCAmelCase=7		,					UpperCAmelCase=3		,					UpperCAmelCase=1_8		,					UpperCAmelCase=3_0		,					UpperCAmelCase=4_0_0		,					UpperCAmelCase=True		,					UpperCAmelCase=None		,					UpperCAmelCase=True		,					UpperCAmelCase=[0.5, 0.5, 0.5]		,					UpperCAmelCase=[0.5, 0.5, 0.5]		,					)					->       int:
      _lowercase											=size if size is not None else {'''height''': 1_8, '''width''': 1_8}
      _lowercase											=parent
      _lowercase											=batch_size
      _lowercase											=num_channels
      _lowercase											=image_size
      _lowercase											=min_resolution
      _lowercase											=max_resolution
      _lowercase											=do_resize
      _lowercase											=size
      _lowercase											=do_normalize
      _lowercase											=image_mean
      _lowercase											=image_std





     def 					__A (self	)					->       Optional[int]:
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
      }







@require_torch
@require_vision
class 				lowerCamelCase__       (	lowerCAmelCase       ,							unittest.TestCase):
     SCREAMING_SNAKE_CASE__				      =      ViTImageProcessor if is_vision_available() else None





     def 					__A (self	)					->       str:
      _lowercase											=ViTImageProcessingTester(self	)





     @property
     def 					__A (self	)					->       Tuple:
      return self.image_processor_tester.prepare_image_processor_dict()





     def 					__A (self	)					->       Optional[Any]:
      _lowercase											=self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(UpperCAmelCase		,					'''image_mean'''	)	)
      self.assertTrue(hasattr(UpperCAmelCase		,					'''image_std'''	)	)
      self.assertTrue(hasattr(UpperCAmelCase		,					'''do_normalize'''	)	)
      self.assertTrue(hasattr(UpperCAmelCase		,					'''do_resize'''	)	)
      self.assertTrue(hasattr(UpperCAmelCase		,					'''size'''	)	)





     def 					__A (self	)					->       Dict:
      _lowercase											=self.image_processing_class.from_dict(self.image_processor_dict	)
      self.assertEqual(image_processor.size		,					{'''height''': 1_8, '''width''': 1_8}	)

      _lowercase											=self.image_processing_class.from_dict(self.image_processor_dict		,					size=4_2	)
      self.assertEqual(image_processor.size		,					{'''height''': 4_2, '''width''': 4_2}	)





     def 					__A (self	)					->       Optional[Any]:
      pass





     def 					__A (self	)					->       Optional[Any]:
      # Initialize image_processing
      _lowercase											=self.image_processing_class(**self.image_processor_dict	)
      # create random PIL images
      _lowercase											=prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCAmelCase	)
      for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase		,					Image.Image	)

      # Test not batched input
      _lowercase											=image_processing(image_inputs[0]		,					return_tensors='''pt'''	).pixel_values
      self.assertEqual(
          encoded_images.shape		,					(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )		,					)

      # Test batched
      _lowercase											=image_processing(UpperCAmelCase		,					return_tensors='''pt'''	).pixel_values
      self.assertEqual(
          encoded_images.shape		,					(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )		,					)





     def 					__A (self	)					->       List[Any]:
      # Initialize image_processing
      _lowercase											=self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      _lowercase											=prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCAmelCase		,					numpify=UpperCAmelCase	)
      for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase		,					np.ndarray	)

      # Test not batched input
      _lowercase											=image_processing(image_inputs[0]		,					return_tensors='''pt'''	).pixel_values
      self.assertEqual(
          encoded_images.shape		,					(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )		,					)

      # Test batched
      _lowercase											=image_processing(UpperCAmelCase		,					return_tensors='''pt'''	).pixel_values
      self.assertEqual(
          encoded_images.shape		,					(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )		,					)





     def 					__A (self	)					->       str:
      # Initialize image_processing
      _lowercase											=self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      _lowercase											=prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCAmelCase		,					torchify=UpperCAmelCase	)
      for image in image_inputs:
       self.assertIsInstance(UpperCAmelCase		,					torch.Tensor	)

      # Test not batched input
      _lowercase											=image_processing(image_inputs[0]		,					return_tensors='''pt'''	).pixel_values
      self.assertEqual(
          encoded_images.shape		,					(
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )		,					)

      # Test batched
      _lowercase											=image_processing(UpperCAmelCase		,					return_tensors='''pt'''	).pixel_values
      self.assertEqual(
          encoded_images.shape		,					(
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.size['''height'''],
              self.image_processor_tester.size['''width'''],
          )		,					)








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase__            =							{'''configuration_visual_bert''': ['''VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VisualBertConfig''']}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__            =							[
      '''VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''VisualBertForMultipleChoice''',
      '''VisualBertForPreTraining''',
      '''VisualBertForQuestionAnswering''',
      '''VisualBertForRegionToPhraseAlignment''',
      '''VisualBertForVisualReasoning''',
      '''VisualBertLayer''',
      '''VisualBertModel''',
      '''VisualBertPreTrainedModel''',
  ]


if TYPE_CHECKING:
  from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_visual_bert import (
        VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
        VisualBertForMultipleChoice,
        VisualBertForPreTraining,
        VisualBertForQuestionAnswering,
        VisualBertForRegionToPhraseAlignment,
        VisualBertForVisualReasoning,
        VisualBertLayer,
        VisualBertModel,
        VisualBertPreTrainedModel,
    )


else:
  import sys

  UpperCAmelCase__            =							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

