





import inspect
import unittest

from huggingface_hub import hf_hub_download

from transformers import ConvNextConfig, UperNetConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

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


if is_torch_available():
    import torch

    from transformers import UperNetForSemanticSegmentation
    from transformers.models.upernet.modeling_upernet import UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor

class     snake_case_		:






 '''simple docstring'''





 def __init__(  self,		A_,		A_=13,		A_=32,		A_=3,		A_=4,		A_=[10, 20, 30, 40],		A_=[2, 2, 3, 2],		A_=True,		A_=True,		A_=37,		A_="gelu",		A_=10,		A_=0.02,		A_=["stage2", "stage3", "stage4"],		A_=3,		A_=None,		)	->       int:
    UpperCAmelCase__						=parent
    UpperCAmelCase__						=batch_size
    UpperCAmelCase__						=image_size
    UpperCAmelCase__						=num_channels
    UpperCAmelCase__						=num_stages
    UpperCAmelCase__						=hidden_sizes
    UpperCAmelCase__						=depths
    UpperCAmelCase__						=is_training
    UpperCAmelCase__						=use_labels
    UpperCAmelCase__						=intermediate_size
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=type_sequence_label_size
    UpperCAmelCase__						=initializer_range
    UpperCAmelCase__						=out_features
    UpperCAmelCase__						=num_labels
    UpperCAmelCase__						=scope
    UpperCAmelCase__						=num_stages

 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    UpperCAmelCase__						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

    UpperCAmelCase__						=None
    if self.use_labels:
       UpperCAmelCase__						=ids_tensor([self.batch_size],		self.type_sequence_label_size		)

    UpperCAmelCase__						=self.get_config()

    return config, pixel_values, labels

 def  __UpperCAmelCase						(  self		)	->       Union[str, Any]:
    return ConvNextConfig(
        num_channels=self.num_channels,		num_stages=self.num_stages,		hidden_sizes=self.hidden_sizes,		depths=self.depths,		is_training=self.is_training,		intermediate_size=self.intermediate_size,		hidden_act=self.hidden_act,		out_features=self.out_features,		)

 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    return UperNetConfig(
        backbone_config=self.get_backbone_config(),		hidden_size=512,		pool_scales=[1, 2, 3, 6],		use_auxiliary_head=A_,		auxiliary_loss_weight=0.4,		auxiliary_in_channels=40,		auxiliary_channels=256,		auxiliary_num_convs=1,		auxiliary_concat_input=A_,		loss_ignore_index=255,		num_labels=self.num_labels,		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_		)	->       str:
    UpperCAmelCase__						=UperNetForSemanticSegmentation(config=A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_		)
    self.parent.assertEqual(
        result.logits.shape,		(self.batch_size, self.num_labels, self.image_size, self.image_size)		)






 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    UpperCAmelCase__						=self.prepare_config_and_inputs()
    (
        (
        UpperCAmelCase__
    )			,							(
        UpperCAmelCase__
    )			,							(
        UpperCAmelCase__
    )			,							
    )						=config_and_inputs
    UpperCAmelCase__						={"pixel_values": pixel_values}
    return config, inputs_dict

@require_torch
class     snake_case_		(   a,		a,		unittest.TestCase							):






 '''simple docstring'''





 __UpperCamelCase       =      (UperNetForSemanticSegmentation,) if is_torch_available() else ()
 __UpperCamelCase       =      {'image-segmentation': UperNetForSemanticSegmentation} if is_torch_available() else {}
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=UperNetModelTester(self		)
    UpperCAmelCase__						=ConfigTester(self,		config_class=A_,		has_text_modality=A_,		hidden_size=37		)

 def  __UpperCAmelCase						(  self		)	->       Dict:
    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		)	->       int:
    return

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

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

       UpperCAmelCase__						=["pixel_values"]
       self.assertListEqual(arg_names[:1],		A_		)

 def  __UpperCAmelCase						(  self		)	->       str:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_semantic_segmentation(*A_		)

 @unittest.skip(reason="UperNet does not use inputs_embeds"		)
 def  __UpperCAmelCase						(  self		)	->       int:
    pass

 @unittest.skip(reason="UperNet does not support input and output embeddings"		)
 def  __UpperCAmelCase						(  self		)	->       Any:
    pass

 @unittest.skip(reason="UperNet does not have a base model"		)
 def  __UpperCAmelCase						(  self		)	->       List[str]:
    pass

 @unittest.skip(reason="UperNet does not have a base model"		)
 def  __UpperCAmelCase						(  self		)	->       str:
    pass

 @require_torch_multi_gpu
 @unittest.skip(reason="UperNet has some layers using `add_module` which doesn't work well with `nn.DataParallel`"		)
 def  __UpperCAmelCase						(  self		)	->       str:
    pass

 @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."		)
 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    pass

 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    def check_hidden_states_output(A_,		A_,		A_		):
       UpperCAmelCase__						=model_class(A_		)
       model.to(A_		)
       model.eval()

       with torch.no_grad():
          UpperCAmelCase__						=model(**self._prepare_for_class(A_,		A_		)		)

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

       UpperCAmelCase__						=self.model_tester.num_stages
       self.assertEqual(len(A_		),		expected_num_stages + 1		)

       # ConvNext'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],		)

    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       UpperCAmelCase__						=True
       check_hidden_states_output(A_,		A_,		A_		)

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

       check_hidden_states_output(A_,		A_,		A_		)

 def  __UpperCAmelCase						(  self		)	->       Any:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

    UpperCAmelCase__						=_config_zero_init(A_		)
    UpperCAmelCase__						=_config_zero_init(configs_no_init.backbone_config		)
    for model_class in self.all_model_classes:
       UpperCAmelCase__						=model_class(config=A_		)
       for name, param in model.named_parameters():
          if param.requires_grad:
             self.assertIn(
                 ((param.data.mean() * 1E9).round() / 1E9).item(),		[0.0, 1.0],		msg=f"""Parameter {name} of model {model_class} seems not properly initialized""",		)

 @unittest.skip(reason="UperNet does not have tied weights"		)
 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    pass






 @slow
 def  __UpperCAmelCase						(  self		)	->       Tuple:
    for model_name in UPERNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase__						=UperNetForSemanticSegmentation.from_pretrained(A_		)
       self.assertIsNotNone(A_		)






def 				_UpperCAmelCase			(    ):




   '''simple docstring'''

   UpperCAmelCase__						=hf_hub_download(
       repo_id="hf-internal-testing/fixtures_ade20k"				,							repo_type="dataset"				,							filename="ADE_val_00000001.jpg"     )
   UpperCAmelCase__						=Image.open(A     ).convert("RGB"     )
   return image




@require_torch
@require_vision
@slow
class     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=AutoImageProcessor.from_pretrained("openmmlab/upernet-swin-tiny"		)
    UpperCAmelCase__						=UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-swin-tiny"		).to(A_		)

    UpperCAmelCase__						=prepare_img()
    UpperCAmelCase__						=processor(images=A_,		return_tensors="pt"		).to(A_		)

    with torch.no_grad():
       UpperCAmelCase__						=model(**A_		)

    UpperCAmelCase__						=torch.Size((1, model.config.num_labels, 512, 512)		)
    self.assertEqual(outputs.logits.shape,		A_		)

    UpperCAmelCase__						=torch.tensor(
        [[-7.59_58, -7.59_58, -7.43_02], [-7.59_58, -7.59_58, -7.43_02], [-7.47_97, -7.47_97, -7.30_68]]		).to(A_		)
    self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3],		A_,		atol=1E-4		)		)






 def  __UpperCAmelCase						(  self		)	->       List[str]:
    UpperCAmelCase__						=AutoImageProcessor.from_pretrained("openmmlab/upernet-convnext-tiny"		)
    UpperCAmelCase__						=UperNetForSemanticSegmentation.from_pretrained("openmmlab/upernet-convnext-tiny"		).to(A_		)

    UpperCAmelCase__						=prepare_img()
    UpperCAmelCase__						=processor(images=A_,		return_tensors="pt"		).to(A_		)

    with torch.no_grad():
       UpperCAmelCase__						=model(**A_		)

    UpperCAmelCase__						=torch.Size((1, model.config.num_labels, 512, 512)		)
    self.assertEqual(outputs.logits.shape,		A_		)

    UpperCAmelCase__						=torch.tensor(
        [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]		).to(A_		)
    self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3],		A_,		atol=1E-4		)		)










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 				_UpperCAmelCase			(    A     ):




   '''simple docstring'''

   UpperCAmelCase__						=int(A     )
   UpperCAmelCase__			,							UpperCAmelCase__			,							UpperCAmelCase__						=t // 3600, (t // 60) % 60, t % 60
   return F"""{h}:{m:02d}:{s:02d}""" if h != 0 else F"""{m:02d}:{s:02d}"""






def 				_UpperCAmelCase			(    A				,							A				,							A				,							A				,							A=300     ):




   '''simple docstring'''

   return F"""
    <div>
      {prefix}
      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>
      {label}
    </div>
    """






def 				_UpperCAmelCase			(    A     ):




   '''simple docstring'''

   UpperCAmelCase__						="<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:
         UpperCAmelCase__						=F"""{elt:.6f}""" if isinstance(A				,							A     ) else str(A     )
         html_code += F"""      <td>{elt}</td>\n"""
      html_code += "    </tr>\n"
   html_code += "  </tbody>\n</table><p>"
   return html_code

class     snake_case_		:






 '''simple docstring'''





 __UpperCamelCase       =      5
 __UpperCamelCase       =      0.2

 def __init__(  self,		A_,		A_ = None,		A_ = True,		A_ = None,		A_ = 300,		)	->       Optional[Any]:
    UpperCAmelCase__						=total
    UpperCAmelCase__						="" if prefix is None else prefix
    UpperCAmelCase__						=leave
    UpperCAmelCase__						=parent
    UpperCAmelCase__						=width
    UpperCAmelCase__						=None
    UpperCAmelCase__						=None
    UpperCAmelCase__						=None

 def  __UpperCAmelCase						(  self,		A_,		A_ = False,		A_ = None		)	->       Any:
    UpperCAmelCase__						=value
    if comment is not None:
       UpperCAmelCase__						=comment
    if self.last_value is None:
       UpperCAmelCase__						=UpperCAmelCase__						=time.time()
       UpperCAmelCase__						=UpperCAmelCase__						=value
       UpperCAmelCase__						=UpperCAmelCase__						=None
       UpperCAmelCase__						=self.warmup
       UpperCAmelCase__						=1
       self.update_bar(A_		)
    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
       UpperCAmelCase__						=time.time()
       UpperCAmelCase__						=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:
          UpperCAmelCase__						=self.elapsed_time / (value - self.start_value)
       else:
          UpperCAmelCase__						=None
       if value >= self.total:
          UpperCAmelCase__						=self.total
          UpperCAmelCase__						=None
          if not self.leave:
             self.close()
       elif self.average_time_per_item is not None:
          UpperCAmelCase__						=self.average_time_per_item * (self.total - value)
       self.update_bar(A_		)
       UpperCAmelCase__						=value
       UpperCAmelCase__						=current_time
       if self.average_time_per_item is None:
          UpperCAmelCase__						=1
       else:
          UpperCAmelCase__						=max(int(self.update_every / self.average_time_per_item		),		1		)

 def  __UpperCAmelCase						(  self,		A_,		A_=None		)	->       Dict:
    UpperCAmelCase__						=" " * (len(str(self.total		)		) - len(str(A_		)		)) + str(A_		)
    if self.elapsed_time is None:
       UpperCAmelCase__						=f"""[{spaced_value}/{self.total} : < :"""
    elif self.predicted_remaining is None:
       UpperCAmelCase__						=f"""[{spaced_value}/{self.total} {format_time(self.elapsed_time		)}"""
    else:
       UpperCAmelCase__						=(
           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		)	->       Tuple:
    UpperCAmelCase__						=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:
       UpperCAmelCase__						=disp.display(disp.HTML(self.html_code		),		display_id=A_		)
    else:
       self.output.update(disp.HTML(self.html_code		)		)






 def  __UpperCAmelCase						(  self		)	->       str:
    if self.parent is None and self.output is not None:
       self.output.update(disp.HTML(""		)		)

class     snake_case_		(   a							):






 '''simple docstring'''





 def __init__(  self,		A_,		A_=None		)	->       Dict:
    super().__init__(A_		)
    UpperCAmelCase__						=None if column_names is None else [column_names]
    UpperCAmelCase__						=None

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__						=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:
       UpperCAmelCase__						=disp.display(disp.HTML(self.html_code		),		display_id=A_		)
    else:
       self.output.update(disp.HTML(self.html_code		)		)

 def  __UpperCAmelCase						(  self,		A_		)	->       Tuple:
    if self.inner_table is None:
       UpperCAmelCase__						=[list(values.keys()		), list(values.values()		)]
    else:
       UpperCAmelCase__						=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(A_		)
          UpperCAmelCase__						=columns
       self.inner_table.append([values[c] for c in columns]		)

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






 def  __UpperCAmelCase						(  self		)	->       Tuple:
    UpperCAmelCase__						=None
    self.display()

class     snake_case_		(   a							):






 '''simple docstring'''





 def __init__(  self		)	->       Optional[int]:
    UpperCAmelCase__						=None
    UpperCAmelCase__						=None
    UpperCAmelCase__						=False

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

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_,		**A_		)	->       str:
    UpperCAmelCase__						=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,		)
    UpperCAmelCase__						=False

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_,		A_=None,		**A_		)	->       int:
    if not has_length(A_		):
       return
    if self.prediction_bar is None:
       if self.training_tracker is not None:
          UpperCAmelCase__						=self.training_tracker.add_child(len(A_		)		)
       else:
          UpperCAmelCase__						=NotebookProgressBar(len(A_		)		)
       self.prediction_bar.update(1		)
    else:
       self.prediction_bar.update(self.prediction_bar.value + 1		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_,		**A_		)	->       Optional[Any]:
    if self.prediction_bar is not None:
       self.prediction_bar.close()
    UpperCAmelCase__						=None

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

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

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






 def  __UpperCAmelCase						(  self,		A_,		A_,		A_,		**A_		)	->       List[str]:
    self.training_tracker.update(
        state.global_step,		comment=f"""Epoch {int(state.epoch		)}/{state.num_train_epochs}""",		force_update=A_		)
    UpperCAmelCase__						=None



