






'''simple docstring'''







from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


__lowerCAmelCase					   =       logging.get_logger(__name__)


if is_vision_available():
  import PIL




class      __SCREAMING_SNAKE_CASE					(__A					):


  """simple docstring"""


  _a     :   Dict			 =    ['''pixel_values''']







  def __init__( self      ,  UpperCamelCase__ = True      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = PILImageResampling.BICUBIC      ,  UpperCamelCase__ = True      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = True      ,  UpperCamelCase__ = 1 / 255      ,  UpperCamelCase__ = True      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = True      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         super().__init__(**UpperCamelCase__  )
         a_	    = size if size is not None else {'shortest_edge': 224}
         a_	    = get_size_dict(UpperCamelCase__      ,  default_to_square=UpperCamelCase__  )
         a_	    = crop_size if crop_size is not None else {'height': 224, 'width': 224}
         a_	    = get_size_dict(UpperCamelCase__  )

         a_	    = do_resize
         a_	    = size
         a_	    = resample
         a_	    = do_center_crop
         a_	    = crop_size
         a_	    = do_rescale
         a_	    = rescale_factor
         a_	    = do_normalize
         a_	    = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
         a_	    = image_std if image_std is not None else OPENAI_CLIP_STD
         a_	    = do_convert_rgb







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__ = PILImageResampling.BICUBIC      ,  UpperCamelCase__ = None      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         a_	    = get_size_dict(UpperCamelCase__      ,  default_to_square=UpperCamelCase__  )
         a_	    = get_resize_output_image_size(
             UpperCamelCase__      ,  size=(size['height'], size['width'])      ,  default_to_square=UpperCamelCase__  )
         return resize(UpperCamelCase__      ,  size=UpperCamelCase__      ,  resample=UpperCamelCase__      ,  data_format=UpperCamelCase__      ,  **UpperCamelCase__  )







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__ = None      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         a_	    = get_size_dict(UpperCamelCase__  )
         return center_crop(UpperCamelCase__      ,  size=(size['height'], size['width'])      ,  data_format=UpperCamelCase__      ,  **UpperCamelCase__  )







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__ = None      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         return rescale(UpperCamelCase__      ,  scale=UpperCamelCase__      ,  data_format=UpperCamelCase__      ,  **UpperCamelCase__  )







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__ = None      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         return normalize(UpperCamelCase__      ,  mean=UpperCamelCase__      ,  std=UpperCamelCase__      ,  data_format=UpperCamelCase__      ,  **UpperCamelCase__  )







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = None      ,  UpperCamelCase__ = ChannelDimension.FIRST      ,  **UpperCamelCase__      ,  ):






         """simple docstring"""


         a_	    = do_resize if do_resize is not None else self.do_resize
         a_	    = size if size is not None else self.size
         a_	    = get_size_dict(UpperCamelCase__      ,  default_to_square=UpperCamelCase__  )
         a_	    = resample if resample is not None else self.resample
         a_	    = do_center_crop if do_center_crop is not None else self.do_center_crop
         a_	    = crop_size if crop_size is not None else self.crop_size
         a_	    = get_size_dict(UpperCamelCase__  )
         a_	    = do_rescale if do_rescale is not None else self.do_rescale
         a_	    = rescale_factor if rescale_factor is not None else self.rescale_factor
         a_	    = do_normalize if do_normalize is not None else self.do_normalize
         a_	    = image_mean if image_mean is not None else self.image_mean
         a_	    = image_std if image_std is not None else self.image_std
         a_	    = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

         a_	    = make_list_of_images(UpperCamelCase__  )

         if not valid_images(UpperCamelCase__  ):
                raise ValueError(
                    'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                    'torch.Tensor, tf.Tensor or jax.ndarray.'  )

         if do_resize and size is None:
                raise ValueError('Size must be specified if do_resize is True.'  )

         if do_center_crop and crop_size is None:
                raise ValueError('Crop size must be specified if do_center_crop is True.'  )

         if do_rescale and rescale_factor is None:
                raise ValueError('Rescale factor must be specified if do_rescale is True.'  )

         if do_normalize and (image_mean is None or image_std is None):
                raise ValueError('Image mean and std must be specified if do_normalize is True.'  )

         # PIL RGBA images are converted to RGB
         if do_convert_rgb:
                a_	    = [convert_to_rgb(UpperCamelCase__  ) for image in images]

         # All transformations expect numpy arrays.
         a_	    = [to_numpy_array(UpperCamelCase__  ) for image in images]

         if do_resize:
                a_	    = [self.resize(image=UpperCamelCase__      ,  size=UpperCamelCase__      ,  resample=UpperCamelCase__  ) for image in images]

         if do_center_crop:
                a_	    = [self.center_crop(image=UpperCamelCase__      ,  size=UpperCamelCase__  ) for image in images]

         if do_rescale:
                a_	    = [self.rescale(image=UpperCamelCase__      ,  scale=UpperCamelCase__  ) for image in images]

         if do_normalize:
                a_	    = [self.normalize(image=UpperCamelCase__      ,  mean=UpperCamelCase__      ,  std=UpperCamelCase__  ) for image in images]

         a_	    = [to_channel_dimension_format(UpperCamelCase__      ,  UpperCamelCase__  ) for image in images]

         a_	    = {'pixel_values': images}
         return BatchFeature(data=UpperCamelCase__      ,  tensor_type=UpperCamelCase__  )












'''simple docstring'''







import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
  import torch

  from transformers import BlipTextModel
  from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class      __SCREAMING_SNAKE_CASE					:


  """simple docstring"""


  def __init__( self      ,  UpperCamelCase__      ,  UpperCamelCase__=12      ,  UpperCamelCase__=7      ,  UpperCamelCase__=True      ,  UpperCamelCase__=True      ,  UpperCamelCase__=True      ,  UpperCamelCase__=99      ,  UpperCamelCase__=32      ,  UpperCamelCase__=32      ,  UpperCamelCase__=5      ,  UpperCamelCase__=4      ,  UpperCamelCase__=37      ,  UpperCamelCase__=0.1      ,  UpperCamelCase__=0.1      ,  UpperCamelCase__=512      ,  UpperCamelCase__=0.02      ,  UpperCamelCase__=0      ,  UpperCamelCase__=None      ,  ):






         """simple docstring"""


         a_	    = parent
         a_	    = batch_size
         a_	    = seq_length
         a_	    = is_training
         a_	    = use_input_mask
         a_	    = use_labels
         a_	    = vocab_size
         a_	    = hidden_size
         a_	    = projection_dim
         a_	    = num_hidden_layers
         a_	    = num_attention_heads
         a_	    = intermediate_size
         a_	    = dropout
         a_	    = attention_dropout
         a_	    = max_position_embeddings
         a_	    = initializer_range
         a_	    = scope
         a_	    = bos_token_id







  def       _a					( self  ):






         """simple docstring"""


         a_	    = ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size  )

         a_	    = None
         if self.use_input_mask:
                a_	    = random_attention_mask([self.batch_size, self.seq_length]  )

         if input_mask is not None:
                a_    ,  a_	    = input_mask.shape
                a_	    = np.random.randint(1      ,  seq_length - 1      ,  size=(batch_size,)  )
                for batch_idx, start_index in enumerate(UpperCamelCase__  ):
                       a_	    = 1
                       a_	    = 0

         a_	    = self.get_config()

         return config, input_ids, input_mask







  def       _a					( self  ):






         """simple docstring"""


         return BlipTextConfig(
             vocab_size=self.vocab_size      ,  hidden_size=self.hidden_size      ,  projection_dim=self.projection_dim      ,  num_hidden_layers=self.num_hidden_layers      ,  num_attention_heads=self.num_attention_heads      ,  intermediate_size=self.intermediate_size      ,  dropout=self.dropout      ,  attention_dropout=self.attention_dropout      ,  max_position_embeddings=self.max_position_embeddings      ,  initializer_range=self.initializer_range      ,  bos_token_id=self.bos_token_id      ,  )







  def       _a					( self      ,  UpperCamelCase__      ,  UpperCamelCase__      ,  UpperCamelCase__  ):






         """simple docstring"""


         a_	    = BlipTextModel(config=UpperCamelCase__  )
         model.to(UpperCamelCase__  )
         model.eval()
         with torch.no_grad():
                a_	    = model(UpperCamelCase__      ,  attention_mask=UpperCamelCase__  )
                a_	    = model(UpperCamelCase__  )
         self.parent.assertEqual(result.last_hidden_state.shape      ,  (self.batch_size, self.seq_length, self.hidden_size)  )
         self.parent.assertEqual(result.pooler_output.shape      ,  (self.batch_size, self.hidden_size)  )







  def       _a					( self  ):






         """simple docstring"""


         a_	    = self.prepare_config_and_inputs()
         a_    ,  a_    ,  a_	    = config_and_inputs
         a_	    = {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict





@require_torch
class      __SCREAMING_SNAKE_CASE					(__A       ,					unittest.TestCase					):


  """simple docstring"""


  _a     :   Dict			 =    (BlipTextModel,) if is_torch_available() else ()
  _a     :   str			 =    False
  _a     :   str			 =    False
  _a     :   str			 =    False







  def       _a					( self  ):






         """simple docstring"""


         a_	    = BlipTextModelTester(self  )
         a_	    = ConfigTester(self      ,  config_class=UpperCamelCase__      ,  hidden_size=37  )







  def       _a					( self  ):






         """simple docstring"""


         self.config_tester.run_common_tests()







  def       _a					( self  ):






         """simple docstring"""


         a_	    = self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase__  )







  def       _a					( self  ):






         """simple docstring"""


         pass







  def       _a					( self  ):






         """simple docstring"""


         pass







  @unittest.skip(reason='Blip does not use inputs_embeds'  )
  def       _a					( self  ):






         """simple docstring"""


         pass







  @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'  )
  def       _a					( self  ):






         """simple docstring"""


         pass







  @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'  )
  def       _a					( self  ):






         """simple docstring"""


         pass







  @slow
  def       _a					( self  ):






         """simple docstring"""


         for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                a_	    = BlipTextModel.from_pretrained(UpperCamelCase__  )
                self.assertIsNotNone(UpperCamelCase__  )







  def       _a					( self  ):






         """simple docstring"""


         super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCamelCase__  )




