


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 ViltImageProcessor



class 					__lowerCamelCase	(unittest.TestCase		):







       def __init__(	self:				Tuple,A_:				List[str],A_:				Optional[Any]=7,A_:				List[str]=3,A_:				int=18,A_:				Any=30,A_:				str=400,A_:				List[Any]=True,A_:				List[Any]=None,A_:				Tuple=2,A_:				Union[str, Any]=True,A_:				str=[0.5, 0.5, 0.5],A_:				int=[0.5, 0.5, 0.5],):






            '''simple docstring'''




            __UpperCamelCase				=	size if size is not None else {'shortest_edge': 30}
            __UpperCamelCase				=	parent
            __UpperCamelCase				=	batch_size
            __UpperCamelCase				=	num_channels
            __UpperCamelCase				=	image_size
            __UpperCamelCase				=	min_resolution
            __UpperCamelCase				=	max_resolution
            __UpperCamelCase				=	do_resize
            __UpperCamelCase				=	size
            __UpperCamelCase				=	size_divisor
            __UpperCamelCase				=	do_normalize
            __UpperCamelCase				=	image_mean
            __UpperCamelCase				=	image_std







       def 						snake_case_					(	self:				Dict			):






            '''simple docstring'''




            return {
                "image_mean": self.image_mean,
                "image_std": self.image_std,
                "do_normalize": self.do_normalize,
                "do_resize": self.do_resize,
                "size": self.size,
                "size_divisor": self.size_divisor,
            }







       def 						snake_case_					(	self:				List[Any],A_:				str,A_:				Dict=False			):






            '''simple docstring'''




            if not batched:
                 __UpperCamelCase				=	self.size['shortest_edge']
                 __UpperCamelCase				=	image_inputs[0]
                 if isinstance(A_,Image.Image			):
                      __UpperCamelCase,  __UpperCamelCase				=	image.size
                 else:
                      __UpperCamelCase,  __UpperCamelCase				=	image.shape[1], image.shape[2]
                 __UpperCamelCase				=	size / min(A_,A_			)
                 if h < w:
                      __UpperCamelCase,  __UpperCamelCase				=	size, scale * w
                 else:
                      __UpperCamelCase,  __UpperCamelCase				=	scale * h, size

                 __UpperCamelCase				=	int((1333 / 800) * size			)
                 if max(A_,A_			) > max_size:
                      __UpperCamelCase				=	max_size / max(A_,A_			)
                      __UpperCamelCase				=	newh * scale
                      __UpperCamelCase				=	neww * scale

                 __UpperCamelCase,  __UpperCamelCase				=	int(newh + 0.5			), int(neww + 0.5			)
                 __UpperCamelCase,  __UpperCamelCase				=	(
                     newh // self.size_divisor * self.size_divisor,
                     neww // self.size_divisor * self.size_divisor,
                 )

            else:
                 __UpperCamelCase				=	[]
                 for image in image_inputs:
                      __UpperCamelCase,  __UpperCamelCase				=	self.get_expected_values([image]			)
                      expected_values.append((expected_height, expected_width)			)
                 __UpperCamelCase				=	max(A_,key=lambda A_			: item[0]			)[0]
                 __UpperCamelCase				=	max(A_,key=lambda A_			: item[1]			)[1]

            return expected_height, expected_width



@require_torch
@require_vision
class 					__lowerCamelCase	(_a							,     unittest.TestCase		):
       _lowercase    							=      ViltImageProcessor if is_vision_available() else None







       def 						snake_case_					(	self:				List[Any]			):






            '''simple docstring'''




            __UpperCamelCase				=	ViltImageProcessingTester(self			)







       @property
       def 						snake_case_					(	self:				Tuple			):






            '''simple docstring'''




            return self.image_processor_tester.prepare_image_processor_dict()







       def 						snake_case_					(	self:				str			):






            '''simple docstring'''




            __UpperCamelCase				=	self.image_processing_class(**self.image_processor_dict			)
            self.assertTrue(hasattr(A_,'image_mean'			)			)
            self.assertTrue(hasattr(A_,'image_std'			)			)
            self.assertTrue(hasattr(A_,'do_normalize'			)			)
            self.assertTrue(hasattr(A_,'do_resize'			)			)
            self.assertTrue(hasattr(A_,'size'			)			)
            self.assertTrue(hasattr(A_,'size_divisor'			)			)







       def 						snake_case_					(	self:				Union[str, Any]			):






            '''simple docstring'''




            __UpperCamelCase				=	self.image_processing_class.from_dict(self.image_processor_dict			)
            self.assertEqual(image_processor.size,{'shortest_edge': 30}			)

            __UpperCamelCase				=	self.image_processing_class.from_dict(self.image_processor_dict,size=42			)
            self.assertEqual(image_processor.size,{'shortest_edge': 42}			)







       def 						snake_case_					(	self:				Union[str, Any]			):






            '''simple docstring'''




            pass







       def 						snake_case_					(	self:				Any			):






            '''simple docstring'''




            __UpperCamelCase				=	self.image_processing_class(**self.image_processor_dict			)
            # create random PIL images
            __UpperCamelCase				=	prepare_image_inputs(self.image_processor_tester,equal_resolution=A_			)
            for image in image_inputs:
                 self.assertIsInstance(A_,Image.Image			)

            # Test not batched input
            __UpperCamelCase				=	image_processing(image_inputs[0],return_tensors='pt'			).pixel_values

            __UpperCamelCase,  __UpperCamelCase				=	self.image_processor_tester.get_expected_values(A_			)
            self.assertEqual(
                encoded_images.shape,(1, self.image_processor_tester.num_channels, expected_height, expected_width),)

            # Test batched
            __UpperCamelCase				=	image_processing(A_,return_tensors='pt'			).pixel_values

            __UpperCamelCase,  __UpperCamelCase				=	self.image_processor_tester.get_expected_values(A_,batched=A_			)
            self.assertEqual(
                encoded_images.shape,(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    expected_height,
                    expected_width,
                ),)







       def 						snake_case_					(	self:				str			):






            '''simple docstring'''




            __UpperCamelCase				=	self.image_processing_class(**self.image_processor_dict			)
            # create random numpy tensors
            __UpperCamelCase				=	prepare_image_inputs(self.image_processor_tester,equal_resolution=A_,numpify=A_			)
            for image in image_inputs:
                 self.assertIsInstance(A_,np.ndarray			)

            # Test not batched input
            __UpperCamelCase				=	image_processing(image_inputs[0],return_tensors='pt'			).pixel_values

            __UpperCamelCase,  __UpperCamelCase				=	self.image_processor_tester.get_expected_values(A_			)
            self.assertEqual(
                encoded_images.shape,(1, self.image_processor_tester.num_channels, expected_height, expected_width),)

            # Test batched
            __UpperCamelCase				=	image_processing(A_,return_tensors='pt'			).pixel_values

            __UpperCamelCase,  __UpperCamelCase				=	self.image_processor_tester.get_expected_values(A_,batched=A_			)
            self.assertEqual(
                encoded_images.shape,(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    expected_height,
                    expected_width,
                ),)







       def 						snake_case_					(	self:				List[str]			):






            '''simple docstring'''




            __UpperCamelCase				=	self.image_processing_class(**self.image_processor_dict			)
            # create random PyTorch tensors
            __UpperCamelCase				=	prepare_image_inputs(self.image_processor_tester,equal_resolution=A_,torchify=A_			)
            for image in image_inputs:
                 self.assertIsInstance(A_,torch.Tensor			)

            # Test not batched input
            __UpperCamelCase				=	image_processing(image_inputs[0],return_tensors='pt'			).pixel_values

            __UpperCamelCase,  __UpperCamelCase				=	self.image_processor_tester.get_expected_values(A_			)
            self.assertEqual(
                encoded_images.shape,(1, self.image_processor_tester.num_channels, expected_height, expected_width),)

            # Test batched
            __UpperCamelCase				=	image_processing(A_,return_tensors='pt'			).pixel_values

            __UpperCamelCase,  __UpperCamelCase				=	self.image_processor_tester.get_expected_values(A_,batched=A_			)
            self.assertEqual(
                encoded_images.shape,(
                    self.image_processor_tester.batch_size,
                    self.image_processor_tester.num_channels,
                    expected_height,
                    expected_width,
                ),)






# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__snake_case     =		{
    '''configuration_convnextv2''': [
        '''CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''ConvNextV2Config''',
    ]
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __snake_case     =		[
        '''CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''ConvNextV2ForImageClassification''',
        '''ConvNextV2Model''',
        '''ConvNextV2PreTrainedModel''',
        '''ConvNextV2Backbone''',
    ]


if TYPE_CHECKING:
    from .configuration_convnextva import (
        CONVNEXTV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
        ConvNextVaConfig,
    )

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_convnextva import (
            CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST,
            ConvNextVaBackbone,
            ConvNextVaForImageClassification,
            ConvNextVaModel,
            ConvNextVaPreTrainedModel,
        )

else:
    import sys

    __snake_case     =		_LazyModule(__name__, globals()['''__file__'''], _import_structure)


