


"""simple docstring"""






import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length
def 					A_      (      _lowerCAmelCase   :    str				):






     """simple docstring"""

     _a					= int(_lowerCAmelCase				)
     _a					= t // 36_00, (t // 60) % 60, t % 60
     return f'{h}:{m:02d}:{s:02d}' if h != 0 else f'{m:02d}:{s:02d}'
def 					A_      (      _lowerCAmelCase   :    int,   _lowerCAmelCase   :    Union[str, Any],   _lowerCAmelCase   :    Union[str, Any],   _lowerCAmelCase   :    Optional[Any],   _lowerCAmelCase   :    int=3_00				):






     """simple docstring"""

     return f'\n    <div>\n      {prefix}\n      <progress value=\'{value}\' max=\'{total}\' style=\'width:{width}px; height:20px; vertical-align: middle;\'></progress>\n      {label}\n    </div>\n    '
def 					A_      (      _lowerCAmelCase   :    Tuple				):






     """simple docstring"""

     _a					= """<table border=\"1\" class=\"dataframe\">\n"""
     html_code += """  <thead>\n <tr style="text-align: left;">\n"""
     for i in items[0]:
          html_code += f'      <th>{i}</th>\n'
     html_code += "    </tr>\n  </thead>\n  <tbody>\n"
     for line in items[1:]:
          html_code += "    <tr>\n"
          for elt in line:
               _a					= f'{elt:.6f}' if isinstance(_lowerCAmelCase,   _lowerCAmelCase				) else str(_lowerCAmelCase				)
               html_code += f'      <td>{elt}</td>\n'
          html_code += "    </tr>\n"
     html_code += "  </tbody>\n</table><p>"
     return html_code


class 			__lowerCamelCase     :

     '''simple docstring'''
     A_				:		List[Any]			=						5
     A_				:		Dict			=						0.2


     def __init__(					self			, __UpperCAmelCase			, __UpperCAmelCase = None			, __UpperCAmelCase = True			, __UpperCAmelCase = None			, __UpperCAmelCase = 300			, )				->							Tuple:
          _a					= total
          _a					= """""" if prefix is None else prefix
          _a					= leave
          _a					= parent
          _a					= width
          _a					= None
          _a					= None
          _a					= None


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase = False			, __UpperCAmelCase = None							)				->							Tuple:
          _a					= value
          if comment is not None:
               _a					= comment
          if self.last_value is None:
               _a					= time.time()
               _a					= value
               _a					= None
               _a					= self.warmup
               _a					= 1
               self.update_bar(__UpperCAmelCase							)
          elif value <= self.last_value and not force_update:
               return
          elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for			, self.total							):
               if self.first_calls > 0:
                    self.first_calls -= 1
               _a					= time.time()
               _a					= current_time - self.start_time
               # We could have value = self.start_value if the update is called twixe with the same start value.
               if value > self.start_value:
                    _a					= self.elapsed_time / (value - self.start_value)
               else:
                    _a					= None
               if value >= self.total:
                    _a					= self.total
                    _a					= None
                    if not self.leave:
                         self.close()
               elif self.average_time_per_item is not None:
                    _a					= self.average_time_per_item * (self.total - value)
               self.update_bar(__UpperCAmelCase							)
               _a					= value
               _a					= current_time
               if self.average_time_per_item is None:
                    _a					= 1
               else:
                    _a					= max(int(self.update_every / self.average_time_per_item							)			, 1							)


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase=None							)				->							List[str]:
          _a					= """ """ * (len(str(self.total							)							) - len(str(__UpperCAmelCase							)							)) + str(__UpperCAmelCase							)
          if self.elapsed_time is None:
               _a					= F'[{spaced_value}/{self.total} : < :'
          elif self.predicted_remaining is None:
               _a					= F'[{spaced_value}/{self.total} {format_time(self.elapsed_time							)}'
          else:
               _a					= (
                   F'[{spaced_value}/{self.total} {format_time(self.elapsed_time							)} <'
                   F' {format_time(self.predicted_remaining							)}'
               )
               self.label += F', {1/self.average_time_per_item:.2f} it/s'
          self.label += "]" if self.comment is None or len(self.comment							) == 0 else F', {self.comment}]'
          self.display()


     def 							_UpperCAmelCase					(					self							)				->							List[Any]:
          _a					= html_progress_bar(self.value			, self.total			, self.prefix			, self.label			, self.width							)
          if self.parent is not None:
               # If this is a child bar, the parent will take care of the display.
               self.parent.display()
               return
          if self.output is None:
               _a					= disp.display(disp.HTML(self.html_code							)			, display_id=__UpperCAmelCase							)
          else:
               self.output.update(disp.HTML(self.html_code							)							)





     def 							_UpperCAmelCase					(					self							)				->							Tuple:
          if self.parent is None and self.output is not None:
               self.output.update(disp.HTML(''''''							)							)







class 			__lowerCamelCase     (    __SCREAMING_SNAKE_CASE ):

     '''simple docstring'''


     def __init__(					self			, __UpperCAmelCase			, __UpperCAmelCase=None							)				->							Optional[int]:
          super().__init__(__UpperCAmelCase							)
          _a					= None if column_names is None else [column_names]
          _a					= None


     def 							_UpperCAmelCase					(					self							)				->							str:
          _a					= html_progress_bar(self.value			, self.total			, self.prefix			, self.label			, self.width							)
          if self.inner_table is not None:
               self.html_code += text_to_html_table(self.inner_table							)
          if self.child_bar is not None:
               self.html_code += self.child_bar.html_code
          if self.output is None:
               _a					= disp.display(disp.HTML(self.html_code							)			, display_id=__UpperCAmelCase							)
          else:
               self.output.update(disp.HTML(self.html_code							)							)


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase							)				->							List[Any]:
          if self.inner_table is None:
               _a					= [list(values.keys()							), list(values.values()							)]
          else:
               _a					= self.inner_table[0]
               if len(self.inner_table							) == 1:
                    # We give a chance to update the column names at the first iteration
                    for key in values.keys():
                         if key not in columns:
                              columns.append(__UpperCAmelCase							)
                    _a					= columns
               self.inner_table.append([values[c] for c in columns]							)


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase=None			, __UpperCAmelCase=300							)				->							Union[str, Any]:
          _a					= NotebookProgressBar(__UpperCAmelCase			, prefix=__UpperCAmelCase			, parent=self			, width=__UpperCAmelCase							)
          return self.child_bar





     def 							_UpperCAmelCase					(					self							)				->							Tuple:
          _a					= None
          self.display()







class 			__lowerCamelCase     (    __SCREAMING_SNAKE_CASE ):

     '''simple docstring'''


     def __init__(					self							)				->							Union[str, Any]:
          _a					= None
          _a					= None
          _a					= False


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, **__UpperCAmelCase							)				->							str:
          _a					= """Epoch""" if args.evaluation_strategy == IntervalStrategy.EPOCH else """Step"""
          _a					= 0
          _a					= 0
          _a					= [self.first_column] + ["""Training Loss"""]
          if args.evaluation_strategy != IntervalStrategy.NO:
               column_names.append('''Validation Loss'''							)
          _a					= NotebookTrainingTracker(state.max_steps			, __UpperCAmelCase							)


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, **__UpperCAmelCase							)				->							str:
          _a					= int(state.epoch							) if int(state.epoch							) == state.epoch else F'{state.epoch:.2f}'
          self.training_tracker.update(
              state.global_step + 1			, comment=F'Epoch {epoch}/{state.num_train_epochs}'			, force_update=self._force_next_update			, )
          _a					= False


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase=None			, **__UpperCAmelCase							)				->							List[Any]:
          if not has_length(__UpperCAmelCase							):
               return
          if self.prediction_bar is None:
               if self.training_tracker is not None:
                    _a					= self.training_tracker.add_child(len(__UpperCAmelCase							)							)
               else:
                    _a					= NotebookProgressBar(len(__UpperCAmelCase							)							)
               self.prediction_bar.update(1							)
          else:
               self.prediction_bar.update(self.prediction_bar.value + 1							)


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, **__UpperCAmelCase							)				->							Any:
          if self.prediction_bar is not None:
               self.prediction_bar.close()
          _a					= None


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase=None			, **__UpperCAmelCase							)				->							str:
          # Only for when there is no evaluation
          if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
               _a					= {"""Training Loss""": logs["""loss"""]}
               # First column is necessarily Step sine we're not in epoch eval strategy
               _a					= state.global_step
               self.training_tracker.write_line(__UpperCAmelCase							)


     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase=None			, **__UpperCAmelCase							)				->							Dict:
          if self.training_tracker is not None:
               _a					= {"""Training Loss""": """No log""", """Validation Loss""": """No log"""}
               for log in reversed(state.log_history							):
                    if "loss" in log:
                         _a					= log["""loss"""]
                         break

               if self.first_column == "Epoch":
                    _a					= int(state.epoch							)
               else:
                    _a					= state.global_step
               _a					= """eval"""
               for k in metrics:
                    if k.endswith('''_loss'''							):
                         _a					= re.sub(r'''\_loss$'''			, ''''''			, __UpperCAmelCase							)
               _a					= metrics.pop('''total_flos'''			, __UpperCAmelCase							)
               _a					= metrics.pop('''epoch'''			, __UpperCAmelCase							)
               _a					= metrics.pop(F'{metric_key_prefix}_runtime'			, __UpperCAmelCase							)
               _a					= metrics.pop(F'{metric_key_prefix}_samples_per_second'			, __UpperCAmelCase							)
               _a					= metrics.pop(F'{metric_key_prefix}_steps_per_second'			, __UpperCAmelCase							)
               _a					= metrics.pop(F'{metric_key_prefix}_jit_compilation_time'			, __UpperCAmelCase							)
               for k, v in metrics.items():
                    if k == F'{metric_key_prefix}_loss':
                         _a					= v
                    else:
                         _a					= k.split('''_'''							)
                         _a					= """ """.join([part.capitalize() for part in splits[1:]]							)
                         _a					= v
               self.training_tracker.write_line(__UpperCAmelCase							)
               self.training_tracker.remove_child()
               _a					= None
               # Evaluation takes a long time so we should force the next update.
               _a					= True





     def 							_UpperCAmelCase					(					self			, __UpperCAmelCase			, __UpperCAmelCase			, __UpperCAmelCase			, **__UpperCAmelCase							)				->							Union[str, Any]:
          self.training_tracker.update(
              state.global_step			, comment=F'Epoch {int(state.epoch							)}/{state.num_train_epochs}'			, force_update=__UpperCAmelCase							)
          _a					= None

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,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
      import PIL


_lowerCamelCase					   =logging.get_logger(__name__)





class 							A__		(	__SCREAMING_SNAKE_CASE):
      _UpperCAmelCase				:    Union[str, Any]           =			["""pixel_values"""]
      def __init__(      self    ,  __magic_name__ = True    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = PILImageResampling.BILINEAR    ,  __magic_name__ = True    ,  __magic_name__ = 1 / 2_5_5    ,  __magic_name__ = True    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  **__magic_name__    ,  ):
       super().__init__(**__magic_name__      )
       lowerCamelCase		:	Dict							=      size if size is not None else {"""shortest_edge""": 3_8_4}
       lowerCamelCase		:	Tuple							=      get_size_dict(__magic_name__    ,  default_to_square=__magic_name__      )

       lowerCamelCase		:	Dict							=      do_resize
       lowerCamelCase		:	List[Any]							=      size
       # Default value set here for backwards compatibility where the value in config is None
       lowerCamelCase		:	Any							=      crop_pct if crop_pct is not None else 2_2_4 / 2_5_6
       lowerCamelCase		:	Union[str, Any]							=      resample
       lowerCamelCase		:	str							=      do_rescale
       lowerCamelCase		:	Union[str, Any]							=      rescale_factor
       lowerCamelCase		:	Tuple							=      do_normalize
       lowerCamelCase		:	Optional[Any]							=      image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       lowerCamelCase		:	int							=      image_std if image_std is not None else IMAGENET_STANDARD_STD
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__ = PILImageResampling.BICUBIC    ,  __magic_name__ = None    ,  **__magic_name__    ,  ):
       lowerCamelCase		:	Union[str, Any]							=      get_size_dict(__magic_name__    ,  default_to_square=__magic_name__      )
       if "shortest_edge" not in size:
        raise ValueError(F'''Size dictionary must contain \'shortest_edge\' key. Got {size.keys()}'''      )
       lowerCamelCase		:	str							=      size["""shortest_edge"""]

       if shortest_edge < 3_8_4:
        # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct
        lowerCamelCase		:	List[str]							=      int(shortest_edge / crop_pct      )
        lowerCamelCase		:	Optional[Any]							=      get_resize_output_image_size(__magic_name__    ,  size=__magic_name__    ,  default_to_square=__magic_name__      )
        lowerCamelCase		:	Optional[int]							=      resize(image=__magic_name__    ,  size=__magic_name__    ,  resample=__magic_name__    ,  data_format=__magic_name__    ,  **__magic_name__      )
        # then crop to (shortest_edge, shortest_edge)
        return center_crop(image=__magic_name__    ,  size=(shortest_edge, shortest_edge)    ,  data_format=__magic_name__    ,  **__magic_name__      )
       else:
        # warping (no cropping) when evaluated at 384 or larger
        return resize(
            __magic_name__    ,  size=(shortest_edge, shortest_edge)    ,  resample=__magic_name__    ,  data_format=__magic_name__    ,  **__magic_name__      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__ = None    ,  **__magic_name__    ,  ):
       return rescale(__magic_name__    ,  scale=__magic_name__    ,  data_format=__magic_name__    ,  **__magic_name__      )
      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__    ,  __magic_name__ = None    ,  **__magic_name__    ,  ):
       return normalize(__magic_name__    ,  mean=__magic_name__    ,  std=__magic_name__    ,  data_format=__magic_name__    ,  **__magic_name__      )






      def 						UpperCamelCase__							(      self    ,  __magic_name__    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = None    ,  __magic_name__ = ChannelDimension.FIRST    ,  **__magic_name__    ,  ):
       lowerCamelCase		:	str							=      do_resize if do_resize is not None else self.do_resize
       lowerCamelCase		:	Optional[Any]							=      crop_pct if crop_pct is not None else self.crop_pct
       lowerCamelCase		:	Optional[int]							=      resample if resample is not None else self.resample
       lowerCamelCase		:	Optional[int]							=      do_rescale if do_rescale is not None else self.do_rescale
       lowerCamelCase		:	List[str]							=      rescale_factor if rescale_factor is not None else self.rescale_factor
       lowerCamelCase		:	Tuple							=      do_normalize if do_normalize is not None else self.do_normalize
       lowerCamelCase		:	Optional[Any]							=      image_mean if image_mean is not None else self.image_mean
       lowerCamelCase		:	Tuple							=      image_std if image_std is not None else self.image_std

       lowerCamelCase		:	Dict							=      size if size is not None else self.size
       lowerCamelCase		:	str							=      get_size_dict(__magic_name__    ,  default_to_square=__magic_name__      )

       lowerCamelCase		:	List[str]							=      make_list_of_images(__magic_name__      )

       if not valid_images(__magic_name__      ):
        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 or resample is None:
        raise ValueError("""Size and resample must be specified if do_resize is True."""      )

       if do_resize and size["shortest_edge"] < 3_8_4 and crop_pct is None:
        raise ValueError("""crop_pct must be specified if size < 384."""      )

       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."""      )

       # All transformations expect numpy arrays.
       lowerCamelCase		:	Optional[Any]							=      [to_numpy_array(__magic_name__      ) for image in images]

       if do_resize:
        lowerCamelCase		:	List[Any]							=      [self.resize(image=__magic_name__    ,  size=__magic_name__    ,  crop_pct=__magic_name__    ,  resample=__magic_name__      ) for image in images]

       if do_rescale:
        lowerCamelCase		:	Union[str, Any]							=      [self.rescale(image=__magic_name__    ,  scale=__magic_name__      ) for image in images]

       if do_normalize:
        lowerCamelCase		:	List[Any]							=      [self.normalize(image=__magic_name__    ,  mean=__magic_name__    ,  std=__magic_name__      ) for image in images]

       lowerCamelCase		:	Optional[int]							=      [to_channel_dimension_format(__magic_name__    ,  __magic_name__      ) for image in images]

       lowerCamelCase		:	List[str]							=      {"""pixel_values""": images}
       return BatchFeature(data=__magic_name__    ,  tensor_type=__magic_name__      )




