





'''simple docstring'''



import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
	from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor






class 							__lowercase    :







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase       ,_lowerCamelCase=13       ,_lowerCamelCase=32       ,_lowerCamelCase=3       ,_lowerCamelCase=4       ,_lowerCamelCase=[10, 20, 30, 40]       ,_lowerCamelCase=[2, 2, 3, 2]       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=37       ,_lowerCamelCase="gelu"       ,_lowerCamelCase=10       ,_lowerCamelCase=0.0_2       ,_lowerCamelCase=["stage2", "stage3", "stage4"]       ,_lowerCamelCase=[2, 3, 4]       ,_lowerCamelCase=None       ,)							->							str:







				'''simple docstring'''





				__lowercase			  =							parent
				__lowercase			  =							batch_size
				__lowercase			  =							image_size
				__lowercase			  =							num_channels
				__lowercase			  =							num_stages
				__lowercase			  =							hidden_sizes
				__lowercase			  =							depths
				__lowercase			  =							is_training
				__lowercase			  =							use_labels
				__lowercase			  =							intermediate_size
				__lowercase			  =							hidden_act
				__lowercase			  =							num_labels
				__lowercase			  =							initializer_range
				__lowercase			  =							out_features
				__lowercase			  =							out_indices
				__lowercase			  =							scope


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				__lowercase			  =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				__lowercase			  =							None
				if self.use_labels:
							__lowercase			  =							ids_tensor([self.batch_size]       ,self.num_labels	)

				__lowercase			  =							self.get_config()

				return config, pixel_values, labels


	def   _UpperCAmelCase       (self	)							->							List[str]:







				'''simple docstring'''





				return ConvNextVaConfig(
				    num_channels=self.num_channels       ,hidden_sizes=self.hidden_sizes       ,depths=self.depths       ,num_stages=self.num_stages       ,hidden_act=self.hidden_act       ,is_decoder=_lowerCamelCase       ,initializer_range=self.initializer_range       ,out_features=self.out_features       ,out_indices=self.out_indices       ,num_labels=self.num_labels       ,)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							Tuple:







				'''simple docstring'''





				__lowercase			  =							ConvNextVaModel(config=_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				__lowercase			  =							model(_lowerCamelCase	)
				# expected last hidden states: B, C, H // 32, W // 32
				self.parent.assertEqual(
				    result.last_hidden_state.shape       ,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)       ,)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							str:







				'''simple docstring'''





				__lowercase			  =							ConvNextVaForImageClassification(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				__lowercase			  =							model(_lowerCamelCase       ,labels=_lowerCamelCase	)
				self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_labels)	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							List[Any]:







				'''simple docstring'''





				__lowercase			  =							ConvNextVaBackbone(config=_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				__lowercase			  =							model(_lowerCamelCase	)

				# verify hidden states
				self.parent.assertEqual(len(result.feature_maps	)       ,len(config.out_features	)	)
				self.parent.assertListEqual(list(result.feature_maps[0].shape	)       ,[self.batch_size, self.hidden_sizes[1], 4, 4]	)

				# verify channels
				self.parent.assertEqual(len(model.channels	)       ,len(config.out_features	)	)
				self.parent.assertListEqual(model.channels       ,config.hidden_sizes[1:]	)

				# verify backbone works with out_features=None
				__lowercase			  =							None
				__lowercase			  =							ConvNextVaBackbone(config=_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				__lowercase			  =							model(_lowerCamelCase	)

				# verify feature maps
				self.parent.assertEqual(len(result.feature_maps	)       ,1	)
				self.parent.assertListEqual(list(result.feature_maps[0].shape	)       ,[self.batch_size, self.hidden_sizes[-1], 1, 1]	)

				# verify channels
				self.parent.assertEqual(len(model.channels	)       ,1	)
				self.parent.assertListEqual(model.channels       ,[config.hidden_sizes[-1]]	)


	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				__lowercase			  =							self.prepare_config_and_inputs()
				__lowercase  ,						__lowercase  ,						__lowercase			  =							config_and_inputs
				__lowercase			  =							{'''pixel_values''': pixel_values}
				return config, inputs_dict


	def   _UpperCAmelCase       (self	)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							self.prepare_config_and_inputs()
				__lowercase  ,						__lowercase  ,						__lowercase			  =							config_and_inputs
				__lowercase			  =							{'''pixel_values''': pixel_values, '''labels''': labels}
				return config, inputs_dict






@require_torch
class 							__lowercase    ( lowerCAmelCase__	, lowerCAmelCase__	, unittest.TestCase  ):







	'''simple docstring'''

	a      :			Optional[Any]     =    (
	    (
	        ConvNextVaModel,
	        ConvNextVaForImageClassification,
	        ConvNextVaBackbone,
	    )
	    if is_torch_available()
	    else ()
	)
	a      :			Union[str, Any]     =    (
	    {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
	    if is_torch_available()
	    else {}
	)

	a      :			List[str]     =    False
	a      :			List[str]     =    False
	a      :			int     =    False
	a      :			Union[str, Any]     =    False
	a      :			int     =    False


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							ConvNextVaModelTester(self	)
				__lowercase			  =							ConfigTester(self       ,config_class=_lowerCamelCase       ,has_text_modality=_lowerCamelCase       ,hidden_size=37	)


	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				self.create_and_test_config_common_properties()
				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				return


	@unittest.skip(reason='''ConvNextV2 does not use inputs_embeds'''	)
	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				pass


	@unittest.skip(reason='''ConvNextV2 does not support input and output embeddings'''	)
	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				pass


	@unittest.skip(reason='''ConvNextV2 does not use feedforward chunking'''	)
	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





				pass


	def   _UpperCAmelCase       (self	)							->							Optional[Any]:







				'''simple docstring'''





				if not self.model_tester.is_training:
							return

				for model_class in self.all_model_classes:
							__lowercase  ,						__lowercase			  =							self.model_tester.prepare_config_and_inputs_with_labels()
							__lowercase			  =							True

							if model_class.__name__ in [
							    *get_values(_lowerCamelCase	),
							    *get_values(_lowerCamelCase	),
							]:
										continue

							__lowercase			  =							model_class(_lowerCamelCase	)
							model.to(_lowerCamelCase	)
							model.train()
							__lowercase			  =							self._prepare_for_class(_lowerCamelCase       ,_lowerCamelCase       ,return_labels=_lowerCamelCase	)
							__lowercase			  =							model(**_lowerCamelCase	).loss
							loss.backward()


	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





				if not self.model_tester.is_training:
							return

				for model_class in self.all_model_classes:
							__lowercase  ,						__lowercase			  =							self.model_tester.prepare_config_and_inputs_with_labels()
							__lowercase			  =							False
							__lowercase			  =							True

							if (
							    model_class.__name__
							    in [*get_values(_lowerCamelCase	), *get_values(_lowerCamelCase	)]
							    or not model_class.supports_gradient_checkpointing
							):
										continue

							__lowercase			  =							model_class(_lowerCamelCase	)
							model.to(_lowerCamelCase	)
							model.gradient_checkpointing_enable()
							model.train()
							__lowercase			  =							self._prepare_for_class(_lowerCamelCase       ,_lowerCamelCase       ,return_labels=_lowerCamelCase	)
							__lowercase			  =							model(**_lowerCamelCase	).loss
							loss.backward()


	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





				__lowercase  ,						__lowercase			  =							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__lowercase			  =							model_class(_lowerCamelCase	)
							__lowercase			  =							inspect.signature(model.forward	)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							__lowercase			  =							[*signature.parameters.keys()]

							__lowercase			  =							['''pixel_values''']
							self.assertListEqual(arg_names[:1]       ,_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							List[str]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							List[str]:







				'''simple docstring'''





				def check_hidden_states_output(_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	):
							__lowercase			  =							model_class(_lowerCamelCase	)
							model.to(_lowerCamelCase	)
							model.eval()

							with torch.no_grad():
										__lowercase			  =							model(**self._prepare_for_class(_lowerCamelCase       ,_lowerCamelCase	)	)

							__lowercase			  =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							__lowercase			  =							self.model_tester.num_stages
							self.assertEqual(len(_lowerCamelCase	)       ,expected_num_stages + 1	)

							# ConvNextV2's feature maps are of shape (batch_size, num_channels, height, width)
							self.assertListEqual(
							    list(hidden_states[0].shape[-2:]	)       ,[self.model_tester.image_size // 4, self.model_tester.image_size // 4]       ,)

				__lowercase  ,						__lowercase			  =							self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__lowercase			  =							True
							check_hidden_states_output(_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							__lowercase			  =							True

							check_hidden_states_output(_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase	)


	@slow
	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowercase			  =							ConvNextVaModel.from_pretrained(_lowerCamelCase	)
							self.assertIsNotNone(_lowerCamelCase	)





def 				_lowerCAmelCase  (			):
			__lowercase			  =							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
			return image






@require_torch
@require_vision
class 							__lowercase    ( unittest.TestCase  ):







	'''simple docstring'''

	@cached_property
	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				return AutoImageProcessor.from_pretrained('''facebook/convnextv2-tiny-1k-224'''	) if is_vision_available() else None


	@slow
	def   _UpperCAmelCase       (self	)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							ConvNextVaForImageClassification.from_pretrained('''facebook/convnextv2-tiny-1k-224'''	).to(_lowerCamelCase	)

				__lowercase			  =							self.default_image_processor
				__lowercase			  =							prepare_img()
				__lowercase			  =							preprocessor(images=_lowerCamelCase       ,return_tensors='''pt'''	).to(_lowerCamelCase	)

				# forward pass
				with torch.no_grad():
							__lowercase			  =							model(**_lowerCamelCase	)

				# verify the logits
				__lowercase			  =							torch.Size((1, 1000)	)
				self.assertEqual(outputs.logits.shape       ,_lowerCamelCase	)

				__lowercase			  =							torch.tensor([0.9_9_9_6, 0.1_9_6_6, -0.4_3_8_6]	).to(_lowerCamelCase	)
				self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,_lowerCamelCase       ,atol=1E-4	)	)












'''simple docstring'''



import sys
from typing import Tuple

import numpy as np
import torch
from PIL import Image
from torch import nn

from transformers.image_utils import PILImageResampling
from utils import img_tensorize






class 							__lowercase    :







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase       ,_lowerCamelCase=sys.maxsize	)							->							Optional[Any]:







				'''simple docstring'''





				__lowercase			  =							'''bilinear'''
				__lowercase			  =							max_size
				__lowercase			  =							short_edge_length


	def __call__(self       ,_lowerCamelCase	)							->							str:







				'''simple docstring'''





				__lowercase			  =							[]
				for img in imgs:
							__lowercase  ,						__lowercase			  =							img.shape[:2]
							# later: provide list and randomly choose index for resize
							__lowercase			  =							np.random.randint(self.short_edge_length[0]       ,self.short_edge_length[1] + 1	)
							if size == 0:
										return img
							__lowercase			  =							size * 1.0 / min(_lowerCamelCase       ,_lowerCamelCase	)
							if h < w:
										__lowercase  ,						__lowercase			  =							size, scale * w
							else:
										__lowercase  ,						__lowercase			  =							scale * h, size
							if max(_lowerCamelCase       ,_lowerCamelCase	) > self.max_size:
										__lowercase			  =							self.max_size * 1.0 / max(_lowerCamelCase       ,_lowerCamelCase	)
										__lowercase			  =							newh * scale
										__lowercase			  =							neww * scale
							__lowercase			  =							int(neww + 0.5	)
							__lowercase			  =							int(newh + 0.5	)

							if img.dtype == np.uinta:
										__lowercase			  =							Image.fromarray(_lowerCamelCase	)
										__lowercase			  =							pil_image.resize((neww, newh)       ,PILImageResampling.BILINEAR	)
										__lowercase			  =							np.asarray(_lowerCamelCase	)
							else:
										__lowercase			  =							img.permute(2       ,0       ,1	).unsqueeze(0	)  # 3, 0, 1)  # hw(c) -> nchw
										__lowercase			  =							nn.functional.interpolate(
										    _lowerCamelCase       ,(newh, neww)       ,mode=self.interp_method       ,align_corners=_lowerCamelCase	).squeeze(0	)
							img_augs.append(_lowerCamelCase	)

				return img_augs






class 							__lowercase    :







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase	)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							ResizeShortestEdge([cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST]       ,cfg.INPUT.MAX_SIZE_TEST	)
				__lowercase			  =							cfg.INPUT.FORMAT
				__lowercase			  =							cfg.SIZE_DIVISIBILITY
				__lowercase			  =							cfg.PAD_VALUE
				__lowercase			  =							cfg.INPUT.MAX_SIZE_TEST
				__lowercase			  =							cfg.MODEL.DEVICE
				__lowercase			  =							torch.tensor(cfg.MODEL.PIXEL_STD	).to(self.device	).view(len(cfg.MODEL.PIXEL_STD	)       ,1       ,1	)
				__lowercase			  =							torch.tensor(cfg.MODEL.PIXEL_MEAN	).to(self.device	).view(len(cfg.MODEL.PIXEL_STD	)       ,1       ,1	)
				__lowercase			  =							lambda _lowerCamelCase	: (x - self.pixel_mean) / self.pixel_std


	def   _UpperCAmelCase       (self       ,_lowerCamelCase	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							tuple(max(_lowerCamelCase	) for s in zip(*[img.shape for img in images]	)	)
				__lowercase			  =							[im.shape[-2:] for im in images]
				__lowercase			  =							[
				    nn.functional.pad(
				        _lowerCamelCase       ,[0, max_size[-1] - size[1], 0, max_size[-2] - size[0]]       ,value=self.pad_value       ,)
				    for size, im in zip(_lowerCamelCase       ,_lowerCamelCase	)
				]

				return torch.stack(_lowerCamelCase	), torch.tensor(_lowerCamelCase	)


	def __call__(self       ,_lowerCamelCase       ,_lowerCamelCase=False	)							->							Tuple:







				'''simple docstring'''





				with torch.no_grad():
							if not isinstance(_lowerCamelCase       ,_lowerCamelCase	):
										__lowercase			  =							[images]
							if single_image:
										assert len(_lowerCamelCase	) == 1
							for i in range(len(_lowerCamelCase	)	):
										if isinstance(images[i]       ,torch.Tensor	):
													images.insert(_lowerCamelCase       ,images.pop(_lowerCamelCase	).to(self.device	).float()	)
										elif not isinstance(images[i]       ,torch.Tensor	):
													images.insert(
													    _lowerCamelCase       ,torch.as_tensor(img_tensorize(images.pop(_lowerCamelCase	)       ,input_format=self.input_format	)	)
													    .to(self.device	)
													    .float()       ,)
            # resize smallest edge
							__lowercase			  =							torch.tensor([im.shape[:2] for im in images]	)
							__lowercase			  =							self.aug(_lowerCamelCase	)
							# transpose images and convert to torch tensors
							# images = [torch.as_tensor(i.astype("float32")).permute(2, 0, 1).to(self.device) for i in images]
							# now normalize before pad to avoid useless arithmetic
							__lowercase			  =							[self.normalizer(_lowerCamelCase	) for x in images]
							# now pad them to do the following operations
							__lowercase  ,						__lowercase			  =							self.pad(_lowerCamelCase	)
							# Normalize

							if self.size_divisibility > 0:
										raise NotImplementedError()
							# pad
							__lowercase			  =							torch.true_divide(_lowerCamelCase       ,_lowerCamelCase	)
							if single_image:
										return images[0], sizes[0], scales_yx[0]
							else:
										return images, sizes, scales_yx





def 				_lowerCAmelCase  (			lowerCamelCase_     :					List[Any]						,    lowerCamelCase_     :					Tuple			):
			boxes[:, 0::2] *= scale_yx[:, 1]
			boxes[:, 1::2] *= scale_yx[:, 0]
			return boxes




def 				_lowerCAmelCase  (			lowerCamelCase_     :					str						,    lowerCamelCase_     :					Tuple[int, int]			):
			assert torch.isfinite(lowerCamelCase_			).all(), "Box tensor contains infinite or NaN!"
			__lowercase  ,						__lowercase			  =							box_size
			tensor[:, 0].clamp_(min=0						,    max=lowerCamelCase_			)
			tensor[:, 1].clamp_(min=0						,    max=lowerCamelCase_			)
			tensor[:, 2].clamp_(min=0						,    max=lowerCamelCase_			)
			tensor[:, 3].clamp_(min=0						,    max=lowerCamelCase_			)





