
import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
	from transformers.trainer import DEFAULT_CALLBACKS

	from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	def __init__(       self       ):
		lowercase       =    []





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_init_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_train_begin'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_train_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_epoch_begin'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_epoch_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_step_begin'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_step_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_evaluate'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_predict'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_save'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_log'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_prediction_step'       )







@require_torch
class        A_   (			unittest.TestCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    tempfile.mkdtemp()





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		shutil.rmtree(self.output_dir       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case=0		,    snake_case=0		,    snake_case=64		,    snake_case=64		,    snake_case=None		,    snake_case=False		,    **snake_case       ):
		# disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
		# its set to False since the tests later on depend on its value.
		lowercase       =    RegressionDataset(length=snake_case       )
		lowercase       =    RegressionDataset(length=snake_case       )
		lowercase       =    RegressionModelConfig(a=snake_case		,    b=snake_case       )
		lowercase       =    RegressionPreTrainedModel(snake_case       )

		lowercase       =    TrainingArguments(self.output_dir		,    disable_tqdm=snake_case		,    report_to=[]		,    **snake_case       )
		return Trainer(
		    snake_case		,    snake_case		,    train_dataset=snake_case		,    eval_dataset=snake_case		,    callbacks=snake_case		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case       ):
		self.assertEqual(len(snake_case       )		,    len(snake_case       )       )

		# Order doesn't matter
		lowercase       =    sorted(snake_case		,    key=lambda snake_case       : cb.__name__ if isinstance(snake_case		,    snake_case       ) else cb.__class__.__name__       )
		lowercase       =    sorted(snake_case		,    key=lambda snake_case       : cb.__name__ if isinstance(snake_case		,    snake_case       ) else cb.__class__.__name__       )

		for cba, cba in zip(snake_case		,    snake_case       ):
			if isinstance(snake_case		,    snake_case       ) and isinstance(snake_case		,    snake_case       ):
				self.assertEqual(snake_case		,    snake_case       )
			elif isinstance(snake_case		,    snake_case       ) and not isinstance(snake_case		,    snake_case       ):
				self.assertEqual(snake_case		,    cba.__class__       )
			elif not isinstance(snake_case		,    snake_case       ) and isinstance(snake_case		,    snake_case       ):
				self.assertEqual(cba.__class__		,    snake_case       )
			else:
				self.assertEqual(snake_case		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case       ):
		lowercase       =    ['on_init_end', 'on_train_begin']
		lowercase       =    0
		lowercase       =    len(trainer.get_eval_dataloader()       )
		lowercase       =    ['on_prediction_step'] * len(trainer.get_eval_dataloader()       ) + ['on_log', 'on_evaluate']
		for _ in range(trainer.state.num_train_epochs       ):
			expected_events.append('on_epoch_begin'       )
			for _ in range(snake_case       ):
				step += 1
				expected_events += ["on_step_begin", "on_step_end"]
				if step % trainer.args.logging_steps == 0:
					expected_events.append('on_log'       )
				if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
					expected_events += evaluation_events.copy()
				if step % trainer.args.save_steps == 0:
					expected_events.append('on_save'       )
			expected_events.append('on_epoch_end'       )
			if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
				expected_events += evaluation_events.copy()
		expected_events += ["on_log", "on_train_end"]
		return expected_events





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_trainer()
		lowercase       =    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		# Callbacks passed at init are added to the default callbacks
		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]       )
		expected_callbacks.append(snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
		lowercase       =    self.get_trainer(disable_tqdm=snake_case       )
		lowercase       =    DEFAULT_CALLBACKS.copy() + [PrinterCallback]
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
		lowercase       =    self.get_trainer()

		# We can add, pop, or remove by class name
		trainer.remove_callback(snake_case       )
		expected_callbacks.remove(snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		lowercase       =    self.get_trainer()
		lowercase       =    trainer.pop_callback(snake_case       )
		self.assertEqual(cb.__class__		,    snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		trainer.add_callback(snake_case       )
		expected_callbacks.insert(0		,    snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		# We can also add, pop, or remove by instance
		lowercase       =    self.get_trainer()
		lowercase       =    trainer.callback_handler.callbacks[0]
		trainer.remove_callback(snake_case       )
		expected_callbacks.remove(snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		lowercase       =    self.get_trainer()
		lowercase       =    trainer.callback_handler.callbacks[0]
		lowercase       =    trainer.pop_callback(snake_case       )
		self.assertEqual(snake_case		,    snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )

		trainer.add_callback(snake_case       )
		expected_callbacks.insert(0		,    snake_case       )
		self.check_callbacks_equality(trainer.callback_handler.callbacks		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		import warnings

		# XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
		warnings.simplefilter(action='ignore'		,    category=snake_case       )

		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]       )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

		# Independent log/save/eval
		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]		,    logging_steps=5       )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]		,    save_steps=5       )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]		,    eval_steps=5		,    evaluation_strategy='steps'       )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]		,    evaluation_strategy='epoch'       )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

		# A bit of everything
		lowercase       =    self.get_trainer(
		    callbacks=[MyTestTrainerCallback]		,    logging_steps=3		,    save_steps=10		,    eval_steps=5		,    evaluation_strategy='steps'		,    )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

		# warning should be emitted for duplicated callbacks
		with patch('transformers.trainer_callback.logger.warning'       ) as warn_mock:
			lowercase       =    self.get_trainer(
			    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]		,    )
			assert str(snake_case       ) in warn_mock.call_args[0][0]



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


UpperCAmelCase	      = logging.get_logger(__name__)


if is_vision_available():
	import PIL







class        A_   (			__lowerCamelCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     str							 =       ["""pixel_values"""]





	def __init__(       self		,    snake_case = True		,    snake_case = None		,    snake_case = PILImageResampling.BICUBIC		,    snake_case = True		,    snake_case = None		,    snake_case = True		,    snake_case = 1 / 255		,    snake_case = True		,    snake_case = None		,    snake_case = None		,    snake_case = True		,    **snake_case		,    ):
		super().__init__(**snake_case       )
		lowercase       =    size if size is not None else {'shortest_edge': 224}
		lowercase       =    get_size_dict(snake_case		,    default_to_square=snake_case       )
		lowercase       =    crop_size if crop_size is not None else {'height': 224, 'width': 224}
		lowercase       =    get_size_dict(snake_case		,    default_to_square=snake_case		,    param_name='crop_size'       )

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





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case = PILImageResampling.BICUBIC		,    snake_case = None		,    **snake_case		,    ):
		lowercase       =    get_size_dict(snake_case		,    default_to_square=snake_case       )
		if "shortest_edge" not in size:
			raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''       )
		lowercase       =    get_resize_output_image_size(snake_case		,    size=size['shortest_edge']		,    default_to_square=snake_case       )
		return resize(snake_case		,    size=snake_case		,    resample=snake_case		,    data_format=snake_case		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case = None		,    **snake_case		,    ):
		lowercase       =    get_size_dict(snake_case       )
		if "height" not in size or "width" not in size:
			raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''       )
		return center_crop(snake_case		,    size=(size['height'], size['width'])		,    data_format=snake_case		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case = None		,    **snake_case		,    ):
		return rescale(snake_case		,    scale=snake_case		,    data_format=snake_case		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    snake_case = None		,    **snake_case		,    ):
		return normalize(snake_case		,    mean=snake_case		,    std=snake_case		,    data_format=snake_case		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = None		,    snake_case = ChannelDimension.FIRST		,    **snake_case		,    ):
		lowercase       =    do_resize if do_resize is not None else self.do_resize
		lowercase       =    size if size is not None else self.size
		lowercase       =    get_size_dict(snake_case		,    param_name='size'		,    default_to_square=snake_case       )
		lowercase       =    resample if resample is not None else self.resample
		lowercase       =    do_center_crop if do_center_crop is not None else self.do_center_crop
		lowercase       =    crop_size if crop_size is not None else self.crop_size
		lowercase       =    get_size_dict(snake_case		,    param_name='crop_size'		,    default_to_square=snake_case       )
		lowercase       =    do_rescale if do_rescale is not None else self.do_rescale
		lowercase       =    rescale_factor if rescale_factor is not None else self.rescale_factor
		lowercase       =    do_normalize if do_normalize is not None else self.do_normalize
		lowercase       =    image_mean if image_mean is not None else self.image_mean
		lowercase       =    image_std if image_std is not None else self.image_std
		lowercase       =    do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

		lowercase       =    make_list_of_images(snake_case       )

		if not valid_images(snake_case       ):
			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:
			lowercase       =    [convert_to_rgb(snake_case       ) for image in images]

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

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

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

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

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

		lowercase       =    [to_channel_dimension_format(snake_case		,    snake_case       ) for image in images]

		lowercase       =    {'pixel_values': images}
		return BatchFeature(data=snake_case		,    tensor_type=snake_case       )

