





'''simple docstring'''


import inspect
import unittest

from transformers import YolosConfig
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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import YolosForObjectDetection, YolosModel
    from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor






class     lowerCAmelCase__ :

       '''simple docstring'''

       def __init__( self					:				str       ,     a__					:				List[Any]       ,     a__					:				str=13       ,     a__					:				Tuple=[30, 30]       ,     a__					:				int=2       ,     a__					:				Any=3       ,     a__					:				Any=True       ,     a__					:				int=True       ,     a__					:				List[Any]=32       ,     a__					:				int=5       ,     a__					:				int=4       ,     a__					:				Optional[Any]=37       ,     a__					:				Optional[int]="gelu"       ,     a__					:				Tuple=0.1       ,     a__					:				Any=0.1       ,     a__					:				Union[str, Any]=10       ,     a__					:				Any=0.02       ,     a__					:				str=3       ,     a__					:				Tuple=None       ,     a__					:				str=8       ,     a__					:				List[str]=10       ,     ):
              UpperCAmelCase  =						parent
              UpperCAmelCase  =						batch_size
              UpperCAmelCase  =						image_size
              UpperCAmelCase  =						patch_size
              UpperCAmelCase  =						num_channels
              UpperCAmelCase  =						is_training
              UpperCAmelCase  =						use_labels
              UpperCAmelCase  =						hidden_size
              UpperCAmelCase  =						num_hidden_layers
              UpperCAmelCase  =						num_attention_heads
              UpperCAmelCase  =						intermediate_size
              UpperCAmelCase  =						hidden_act
              UpperCAmelCase  =						hidden_dropout_prob
              UpperCAmelCase  =						attention_probs_dropout_prob
              UpperCAmelCase  =						type_sequence_label_size
              UpperCAmelCase  =						initializer_range
              UpperCAmelCase  =						num_labels
              UpperCAmelCase  =						scope
              UpperCAmelCase  =						n_targets
              UpperCAmelCase  =						num_detection_tokens
              # we set the expected sequence length (which is used in several tests)
              # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
              UpperCAmelCase  =						(image_size[1] // patch_size) * (image_size[0] // patch_size)
              UpperCAmelCase  =						num_patches + 1 + self.num_detection_tokens

       def 			__snake_case      ( self					:				List[str]						):
              UpperCAmelCase  =						floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]						)

              UpperCAmelCase  =						None
              if self.use_labels:
                     # labels is a list of Dict (each Dict being the labels for a given example in the batch)
                     UpperCAmelCase  =						[]
                     for i in range(self.batch_size						):
                            UpperCAmelCase  =						{}
                            UpperCAmelCase  =						torch.randint(
                                high=self.num_labels       ,     size=(self.n_targets,)       ,     device=a__						)
                            UpperCAmelCase  =						torch.rand(self.n_targets       ,     4       ,     device=a__						)
                            labels.append(a__						)

              UpperCAmelCase  =						self.get_config()

              return config, pixel_values, labels

       def 			__snake_case      ( self					:				Any						):
              return YolosConfig(
                  image_size=self.image_size       ,     patch_size=self.patch_size       ,     num_channels=self.num_channels       ,     hidden_size=self.hidden_size       ,     num_hidden_layers=self.num_hidden_layers       ,     num_attention_heads=self.num_attention_heads       ,     intermediate_size=self.intermediate_size       ,     hidden_act=self.hidden_act       ,     hidden_dropout_prob=self.hidden_dropout_prob       ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,     is_decoder=a__       ,     initializer_range=self.initializer_range       ,     num_detection_tokens=self.num_detection_tokens       ,     num_labels=self.num_labels       ,     )

       def 			__snake_case      ( self					:				int       ,     a__					:				Tuple       ,     a__					:				Optional[int]       ,     a__					:				str						):
              UpperCAmelCase  =						YolosModel(config=a__						)
              model.to(a__						)
              model.eval()
              UpperCAmelCase  =						model(a__						)
              self.parent.assertEqual(
                  result.last_hidden_state.shape       ,     (self.batch_size, self.expected_seq_len, self.hidden_size)						)

       def 			__snake_case      ( self					:				Optional[int]       ,     a__					:				Union[str, Any]       ,     a__					:				str       ,     a__					:				int						):
              UpperCAmelCase  =						YolosForObjectDetection(a__						)
              model.to(a__						)
              model.eval()

              UpperCAmelCase  =						model(pixel_values=a__						)
              UpperCAmelCase  =						model(a__						)

              self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_detection_tokens, self.num_labels + 1)						)
              self.parent.assertEqual(result.pred_boxes.shape       ,     (self.batch_size, self.num_detection_tokens, 4)						)

              UpperCAmelCase  =						model(pixel_values=a__       ,     labels=a__						)

              self.parent.assertEqual(result.loss.shape       ,     ()						)
              self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_detection_tokens, self.num_labels + 1)						)
              self.parent.assertEqual(result.pred_boxes.shape       ,     (self.batch_size, self.num_detection_tokens, 4)						)



       def 			__snake_case      ( self					:				Optional[int]						):
              UpperCAmelCase  =						self.prepare_config_and_inputs()
              UpperCAmelCase,					UpperCAmelCase,					UpperCAmelCase  =						config_and_inputs
              UpperCAmelCase  =						{'''pixel_values''': pixel_values}
              return config, inputs_dict






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

       '''simple docstring'''
       _lowerCamelCase								=(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
       _lowerCamelCase								=(
           {"feature-extraction": YolosModel, "object-detection": YolosForObjectDetection} if is_torch_available() else {}
       )

       _lowerCamelCase								=False
       _lowerCamelCase								=False
       _lowerCamelCase								=False
       _lowerCamelCase								=False

       def 			__snake_case      ( self					:				List[Any]       ,     a__					:				List[str]       ,     a__					:				Dict       ,     a__					:				Dict=False						):
              UpperCAmelCase  =						super()._prepare_for_class(a__       ,     a__       ,     return_labels=a__						)

              if return_labels:
                     if model_class.__name__ == "YolosForObjectDetection":
                            UpperCAmelCase  =						[]
                            for i in range(self.model_tester.batch_size						):
                                   UpperCAmelCase  =						{}
                                   UpperCAmelCase  =						torch.ones(
                                       size=(self.model_tester.n_targets,)       ,     device=a__       ,     dtype=torch.long						)
                                   UpperCAmelCase  =						torch.ones(
                                       self.model_tester.n_targets       ,     4       ,     device=a__       ,     dtype=torch.float						)
                                   labels.append(a__						)
                            UpperCAmelCase  =						labels

              return inputs_dict

       def 			__snake_case      ( self					:				Dict						):
              UpperCAmelCase  =						YolosModelTester(self						)
              UpperCAmelCase  =						ConfigTester(self       ,     config_class=a__       ,     has_text_modality=a__       ,     hidden_size=37						)

       def 			__snake_case      ( self					:				Optional[Any]						):
              self.config_tester.run_common_tests()

       def 			__snake_case      ( self					:				Dict						):
              # YOLOS does not use inputs_embeds
              pass

       def 			__snake_case      ( self					:				int						):
              UpperCAmelCase,					UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs_for_common()

              for model_class in self.all_model_classes:
                     UpperCAmelCase  =						model_class(a__						)
                     self.assertIsInstance(model.get_input_embeddings()       ,     (nn.Module)						)
                     UpperCAmelCase  =						model.get_output_embeddings()
                     self.assertTrue(x is None or isinstance(a__       ,     nn.Linear						)						)

       def 			__snake_case      ( self					:				Union[str, Any]						):
              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 			__snake_case      ( self					:				List[str]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*a__						)

       def 			__snake_case      ( self					:				Dict						):
              UpperCAmelCase,					UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs_for_common()
              UpperCAmelCase  =						True

              # in YOLOS, the seq_len is different
              UpperCAmelCase  =						self.model_tester.expected_seq_len
              for model_class in self.all_model_classes:
                     UpperCAmelCase  =						True
                     UpperCAmelCase  =						False
                     UpperCAmelCase  =						True
                     UpperCAmelCase  =						model_class(a__						)
                     model.to(a__						)
                     model.eval()
                     with torch.no_grad():
                            UpperCAmelCase  =						model(**self._prepare_for_class(a__       ,     a__						)						)
                     UpperCAmelCase  =						outputs.attentions
                     self.assertEqual(len(a__						)       ,     self.model_tester.num_hidden_layers						)

                     # check that output_attentions also work using config
                     del inputs_dict["output_attentions"]
                     UpperCAmelCase  =						True
                     UpperCAmelCase  =						model_class(a__						)
                     model.to(a__						)
                     model.eval()
                     with torch.no_grad():
                            UpperCAmelCase  =						model(**self._prepare_for_class(a__       ,     a__						)						)
                     UpperCAmelCase  =						outputs.attentions
                     self.assertEqual(len(a__						)       ,     self.model_tester.num_hidden_layers						)

                     self.assertListEqual(
                         list(attentions[0].shape[-3:]						)       ,     [self.model_tester.num_attention_heads, seq_len, seq_len]       ,     )
                     UpperCAmelCase  =						len(a__						)

                     # Check attention is always last and order is fine
                     UpperCAmelCase  =						True
                     UpperCAmelCase  =						True
                     UpperCAmelCase  =						model_class(a__						)
                     model.to(a__						)
                     model.eval()
                     with torch.no_grad():
                            UpperCAmelCase  =						model(**self._prepare_for_class(a__       ,     a__						)						)

                     UpperCAmelCase  =						1
                     self.assertEqual(out_len + added_hidden_states       ,     len(a__						)						)

                     UpperCAmelCase  =						outputs.attentions

                     self.assertEqual(len(a__						)       ,     self.model_tester.num_hidden_layers						)
                     self.assertListEqual(
                         list(self_attentions[0].shape[-3:]						)       ,     [self.model_tester.num_attention_heads, seq_len, seq_len]       ,     )

       def 			__snake_case      ( self					:				Union[str, Any]						):
              def check_hidden_states_output(a__					:				Optional[Any]       ,     a__					:				Dict       ,     a__					:				Optional[Any]						):
                     UpperCAmelCase  =						model_class(a__						)
                     model.to(a__						)
                     model.eval()

                     with torch.no_grad():
                            UpperCAmelCase  =						model(**self._prepare_for_class(a__       ,     a__						)						)

                     UpperCAmelCase  =						outputs.hidden_states

                     UpperCAmelCase  =						getattr(
                         self.model_tester       ,     '''expected_num_hidden_layers'''       ,     self.model_tester.num_hidden_layers + 1						)
                     self.assertEqual(len(a__						)       ,     a__						)

                     # YOLOS has a different seq_length
                     UpperCAmelCase  =						self.model_tester.expected_seq_len

                     self.assertListEqual(
                         list(hidden_states[0].shape[-2:]						)       ,     [seq_length, self.model_tester.hidden_size]       ,     )

              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 			__snake_case      ( self					:				Optional[Any]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_object_detection(*a__						)



       @slow
       def 			__snake_case      ( self					:				str						):
              for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     UpperCAmelCase  =						YolosModel.from_pretrained(a__						)
                     self.assertIsNotNone(a__						)


def       __snake_case		( )							->     Optional[int]:

       """simple docstring"""


       UpperCAmelCase  =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
       return image






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

       '''simple docstring'''

       @cached_property
       def 			__snake_case      ( self					:				List[str]						):
              return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''						) if is_vision_available() else None



       @slow
       def 			__snake_case      ( self					:				Union[str, Any]						):
              UpperCAmelCase  =						YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''						).to(a__						)

              UpperCAmelCase  =						self.default_image_processor
              UpperCAmelCase  =						prepare_img()
              UpperCAmelCase  =						image_processor(images=a__       ,     return_tensors='''pt'''						).to(a__						)

              # forward pass
              with torch.no_grad():
                     UpperCAmelCase  =						model(inputs.pixel_values						)

              # verify outputs
              UpperCAmelCase  =						torch.Size((1, 100, 92)						)
              self.assertEqual(outputs.logits.shape       ,     a__						)

              UpperCAmelCase  =						torch.tensor(
                  [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]]       ,     device=a__       ,     )
              UpperCAmelCase  =						torch.tensor(
                  [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]]       ,     device=a__						)
              self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]       ,     a__       ,     atol=1e-4						)						)
              self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]       ,     a__       ,     atol=1e-4						)						)

              # verify postprocessing
              UpperCAmelCase  =						image_processor.post_process_object_detection(
                  a__       ,     threshold=0.3       ,     target_sizes=[image.size[::-1]]						)[0]
              UpperCAmelCase  =						torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861]						).to(a__						)
              UpperCAmelCase  =						[75, 75, 17, 63, 17]
              UpperCAmelCase  =						torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495]						).to(a__						)

              self.assertEqual(len(results['''scores''']						)       ,     5						)
              self.assertTrue(torch.allclose(results['''scores''']       ,     a__       ,     atol=1e-4						)						)
              self.assertSequenceEqual(results['''labels'''].tolist()       ,     a__						)
              self.assertTrue(torch.allclose(results['''boxes'''][0, :]       ,     a__						)						)










from sklearn.metrics import fa_score

import datasets


lowerCamelCase      :       Union[str, Any]    =						'''
The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:
F1 = 2 * (precision * recall) / (precision + recall)
'''


lowerCamelCase      :       Optional[Any]    =						'''
Args:
    predictions (`list` of `int`): Predicted labels.
    references (`list` of `int`): Ground truth labels.
    labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. Labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in `predictions` and `references` are used in sorted order. Defaults to None.
    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.
    average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.

        - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.
        - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives.
        - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
        - \'weighted\': Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. This option can result in an F-score that is not between precision and recall.
        - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
    sample_weight (`list` of `float`): Sample weights Defaults to None.

Returns:
    f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.

Examples:

    Example 1-A simple binary example
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])
        >>> print(results)
        {\'f1\': 0.5}

    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)
        >>> print(round(results[\'f1\'], 2))
        0.67

    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])
        >>> print(round(results[\'f1\'], 2))
        0.35

    Example 4-A multiclass example, with different values for the `average` input.
        >>> predictions = [0, 2, 1, 0, 0, 1]
        >>> references = [0, 1, 2, 0, 1, 2]
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro")
        >>> print(round(results[\'f1\'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro")
        >>> print(round(results[\'f1\'], 2))
        0.33
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted")
        >>> print(round(results[\'f1\'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)
        >>> print(results)
        {\'f1\': array([0.8, 0. , 0. ])}
'''


lowerCamelCase      :       Optional[Any]    =						'''
@article{scikit-learn,
    title={Scikit-learn: Machine Learning in {P}ython},
    author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
           and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
           and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
           Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
    journal={Journal of Machine Learning Research},
    volume={12},
    pages={2825--2830},
    year={2011}
}
'''



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,		_KWARGS_DESCRIPTION      )
class 				_UpperCamelCase      (datasets.Metric      ):


      def 			__UpperCAmelCase		(						self						)->						Tuple:
           return datasets.MetricInfo(
               description=_DESCRIPTION       ,    citation=_CITATION       ,    inputs_description=_KWARGS_DESCRIPTION       ,    features=datasets.Features(
                   {
                       "predictions": datasets.Sequence(datasets.Value("int32"						)						),
                       "references": datasets.Sequence(datasets.Value("int32"						)						),
                   }
                   if self.config_name == "multilabel"
                   else {
                       "predictions": datasets.Value("int32"						),
                       "references": datasets.Value("int32"						),
                   }						)       ,    reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html"]       ,    )


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase=None       ,    __UpperCamelCase=1       ,    __UpperCamelCase="binary"       ,    __UpperCamelCase=None						)->						Optional[int]:
           __lowerCAmelCase             =       fa_score(
               __UpperCamelCase       ,    __UpperCamelCase       ,    labels=__UpperCamelCase       ,    pos_label=__UpperCamelCase       ,    average=__UpperCamelCase       ,    sample_weight=__UpperCamelCase						)
           return {"f1": float(__UpperCamelCase						) if score.size == 1 else score}






