






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


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import BlipImageProcessor


class 			__SCREAMING_SNAKE_CASE    (      unittest.TestCase):






						def __init__(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=7   ,  _UpperCamelCase=3   ,  _UpperCamelCase=18   ,  _UpperCamelCase=30   ,  _UpperCamelCase=4_00   ,  _UpperCamelCase=True   ,  _UpperCamelCase=None   ,  _UpperCamelCase=True   ,  _UpperCamelCase=False   ,  _UpperCamelCase=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]   ,  _UpperCamelCase=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]   ,  _UpperCamelCase=True   ,  ):






												"""simple docstring"""
												lowerCAmelCase__			  =  size if size is not None else {'height': 20, 'width': 20}
												lowerCAmelCase__			  =  parent
												lowerCAmelCase__			  =  batch_size
												lowerCAmelCase__			  =  num_channels
												lowerCAmelCase__			  =  image_size
												lowerCAmelCase__			  =  min_resolution
												lowerCAmelCase__			  =  max_resolution
												lowerCAmelCase__			  =  do_resize
												lowerCAmelCase__			  =  size
												lowerCAmelCase__			  =  do_normalize
												lowerCAmelCase__			  =  image_mean
												lowerCAmelCase__			  =  image_std
												lowerCAmelCase__			  =  do_pad
												lowerCAmelCase__			  =  do_convert_rgb






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												return {
												    "do_resize": self.do_resize,
												    "size": self.size,
												    "do_normalize": self.do_normalize,
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_convert_rgb": self.do_convert_rgb,
												    "do_pad": self.do_pad,
												}







						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase=False   ,  _UpperCamelCase=False   ,  _UpperCamelCase=False      ):






												"""simple docstring"""
												assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

												if equal_resolution:
																		lowerCAmelCase__			  =  []
																		for i in range(self.batch_size      ):
																								image_inputs.append(
																								    np.random.randint(
																								        2_55   ,  size=(self.num_channels, self.max_resolution, self.max_resolution)   ,  dtype=np.uinta      )      )
												else:
																		lowerCAmelCase__			  =  []
																		for i in range(self.batch_size      ):
																								lowerCAmelCase__  ,				lowerCAmelCase__			  =  np.random.choice(np.arange(self.min_resolution   ,  self.max_resolution      )   ,  2      )
																								image_inputs.append(np.random.randint(2_55   ,  size=(self.num_channels, width, height)   ,  dtype=np.uinta      )      )

												if not numpify and not torchify:
																		# PIL expects the channel dimension as last dimension
																		lowerCAmelCase__			  =  [Image.fromarray(np.moveaxis(_UpperCamelCase   ,  0   ,  -1      )      ) for x in image_inputs]

												if torchify:
																		lowerCAmelCase__			  =  [torch.from_numpy(_UpperCamelCase      ) for x in image_inputs]

												return image_inputs




@require_torch
@require_vision
class 			__SCREAMING_SNAKE_CASE    (      __lowercase						,    unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:				List[str]            =    BlipImageProcessor if is_vision_available() else None






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  BlipImageProcessingTester(self      )






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												return self.image_processor_tester.prepare_image_processor_dict()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.image_processing_class(**self.image_processor_dict      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'do_resize'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'size'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'do_normalize'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'image_mean'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'image_std'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'do_convert_rgb'      )      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# Initialize image_processor
												lowerCAmelCase__			  =  self.image_processing_class(**self.image_processor_dict      )
												# create random PIL images
												lowerCAmelCase__			  =  self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCamelCase      )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase   ,  Image.Image      )

												# Test not batched input
												lowerCAmelCase__			  =  image_processor(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
												lowerCAmelCase__			  =  image_processor(_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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# Initialize image_processor
												lowerCAmelCase__			  =  self.image_processing_class(**self.image_processor_dict      )
												# create random numpy tensors
												lowerCAmelCase__			  =  self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCamelCase   ,  numpify=_UpperCamelCase      )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase   ,  np.ndarray      )

												# Test not batched input
												lowerCAmelCase__			  =  image_processor(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
												lowerCAmelCase__			  =  image_processor(_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 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# Initialize image_processor
												lowerCAmelCase__			  =  self.image_processing_class(**self.image_processor_dict      )
												# create random PyTorch tensors
												lowerCAmelCase__			  =  self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCamelCase   ,  torchify=_UpperCamelCase      )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase   ,  torch.Tensor      )

												# Test not batched input
												lowerCAmelCase__			  =  image_processor(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
												lowerCAmelCase__			  =  image_processor(_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'],
												    )   ,  )




@require_torch
@require_vision
class 			__SCREAMING_SNAKE_CASE    (      __lowercase						,    unittest.TestCase):
						_SCREAMING_SNAKE_CASE		:				List[str]            =    BlipImageProcessor if is_vision_available() else None






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  BlipImageProcessingTester(self   ,  num_channels=4      )
												lowerCAmelCase__			  =  3






						@property
						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												return self.image_processor_tester.prepare_image_processor_dict()






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self.image_processing_class(**self.image_processor_dict      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'do_resize'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'size'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'do_normalize'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'image_mean'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'image_std'      )      )
												self.assertTrue(hasattr(_UpperCamelCase   ,  'do_convert_rgb'      )      )






						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												pass







						def 					UpperCamelCase__					(		self      ):






												"""simple docstring"""
												# Initialize image_processor
												lowerCAmelCase__			  =  self.image_processing_class(**self.image_processor_dict      )
												# create random PIL images
												lowerCAmelCase__			  =  self.image_processor_tester.prepare_inputs(equal_resolution=_UpperCamelCase      )
												for image in image_inputs:
																		self.assertIsInstance(_UpperCamelCase   ,  Image.Image      )

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

												# Test batched
												lowerCAmelCase__			  =  image_processor(_UpperCamelCase   ,  return_tensors='pt'      ).pixel_values
												self.assertEqual(
												    encoded_images.shape   ,  (
												        self.image_processor_tester.batch_size,
												        self.expected_encoded_image_num_channels,
												        self.image_processor_tester.size['height'],
												        self.image_processor_tester.size['width'],
												    )   ,  )













import logging
from pathlib import Path

import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.utilities import rank_zero_only

from utils import save_json







def 		_UpperCamelCase      (			UpperCamelCase_				:    Tuple						)     ->      str:

						"""simple docstring"""



						lowerCAmelCase__			  =  filter(lambda UpperCamelCase_						: p.requires_grad			,				model.parameters()						)
						lowerCAmelCase__			  =  sum([np.prod(p.size()						) for p in model_parameters]						)
						return params


__snake_case			:			Dict			   =   logging.getLogger(__name__)


class 			__SCREAMING_SNAKE_CASE    (      pl.Callback):






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  {F"lr_group_{i}": param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups      )}
												pl_module.logger.log_metrics(_UpperCamelCase      )






						@rank_zero_only
						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase=True      ):






												"""simple docstring"""
												logger.info(F"***** {type_path} results at step {trainer.global_step:05d} *****"      )
												lowerCAmelCase__			  =  trainer.callback_metrics
												trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']}      )
												# Log results
												lowerCAmelCase__			  =  Path(pl_module.hparams.output_dir      )
												if type_path == "test":
																		lowerCAmelCase__			  =  od / 'test_results.txt'
																		lowerCAmelCase__			  =  od / 'test_generations.txt'
												else:
																		# this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json
																		# If people want this it will be easy enough to add back.
																		lowerCAmelCase__			  =  od / F"{type_path}_results/{trainer.global_step:05d}.txt"
																		lowerCAmelCase__			  =  od / F"{type_path}_generations/{trainer.global_step:05d}.txt"
																		results_file.parent.mkdir(exist_ok=_UpperCamelCase      )
																		generations_file.parent.mkdir(exist_ok=_UpperCamelCase      )
												with open(_UpperCamelCase   ,  'a+'      ) as writer:
																		for key in sorted(_UpperCamelCase      ):
																								if key in ["log", "progress_bar", "preds"]:
																														continue
																								lowerCAmelCase__			  =  metrics[key]
																								if isinstance(_UpperCamelCase   ,  torch.Tensor      ):
																														lowerCAmelCase__			  =  val.item()
																								lowerCAmelCase__			  =  F"{key}: {val:.6f}\n"
																								writer.write(_UpperCamelCase      )

												if not save_generations:
																		return

												if "preds" in metrics:
																		lowerCAmelCase__			  =  '\n'.join(metrics['preds']      )
																		generations_file.open('w+'      ).write(_UpperCamelCase      )






						@rank_zero_only
						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												try:
																		lowerCAmelCase__			  =  pl_module.model.model.num_parameters()
												except AttributeError:
																		lowerCAmelCase__			  =  pl_module.model.num_parameters()

												lowerCAmelCase__			  =  count_trainable_parameters(_UpperCamelCase      )
												# mp stands for million parameters
												trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6}      )






						@rank_zero_only
						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												save_json(pl_module.metrics   ,  pl_module.metrics_save_path      )
												return self._write_logs(_UpperCamelCase   ,  _UpperCamelCase   ,  'test'      )







						@rank_zero_only
						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase      ):






												"""simple docstring"""
												save_json(pl_module.metrics   ,  pl_module.metrics_save_path      )
												# Uncommenting this will save val generations
												# return self._write_logs(trainer, pl_module, "valid")







def 		_UpperCamelCase      (			UpperCamelCase_				:    Union[str, Any]			,				UpperCamelCase_				:    Union[str, Any]			,				UpperCamelCase_				:    List[Any]=1			,				UpperCamelCase_				:    List[str]=False						)     ->      Optional[Any]:

						"""simple docstring"""



						if metric == "rouge2":
												lowerCAmelCase__			  =  '{val_avg_rouge2:.4f}-{step_count}'
						elif metric == "bleu":
												lowerCAmelCase__			  =  '{val_avg_bleu:.4f}-{step_count}'
						elif metric == "loss":
												lowerCAmelCase__			  =  '{val_avg_loss:.4f}-{step_count}'
						else:
												raise NotImplementedError(
												    F"seq2seq callbacks only support rouge2, bleu and loss, got {metric}, You can make your own by adding to"
												    ' this function.'						)

						lowerCAmelCase__			  =  ModelCheckpoint(
						    dirpath=UpperCamelCase_			,				filename=UpperCamelCase_			,				monitor=F"val_{metric}"			,				mode='min' if 'loss' in metric else 'max'			,				save_top_k=UpperCamelCase_			,				)
						return checkpoint_callback







def 		_UpperCamelCase      (			UpperCamelCase_				:    Union[str, Any]			,				UpperCamelCase_				:    int						)     ->      Dict:

						"""simple docstring"""



						return EarlyStopping(
						    monitor=F"val_{metric}"			,				mode='min' if 'loss' in metric else 'max'			,				patience=UpperCamelCase_			,				verbose=UpperCamelCase_			,				)





