



'''simple docstring'''




import argparse
import os
import re

import numpy as np
import PIL
import torch
from timm import create_model
from torch.optim.lr_scheduler import OneCycleLR
from torch.utils.data import DataLoader, Dataset
from torchvision.transforms import Compose, RandomResizedCrop, Resize, ToTensor

from accelerate import Accelerator


def 				lowerCAmelCase_   (	_lowerCamelCase:   List[str]					):
		__SCREAMING_SNAKE_CASE       :		Dict           =		fname.split(os.path.sep					)[-1]
		return re.search(r"""^(.*)_\d+\.jpg$"""       ,      _lowerCamelCase					).groups()[0]






class 						_UpperCamelCase  (							lowerCamelCase__    ):


			'''simple docstring'''

			def __init__(							self     : Union[str, Any]							,     lowerCAmelCase__     : Tuple							,     lowerCAmelCase__     : Optional[Any]=None							,     lowerCAmelCase__     : Dict=None						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[Any]           =		file_names
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		image_transform
					__SCREAMING_SNAKE_CASE       :		Dict           =		label_to_id

			def __len__(							self     : Union[str, Any]						):





					"""simple docstring"""






					return len(self.file_names						)



			def __getitem__(							self     : Optional[int]							,     lowerCAmelCase__     : List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		self.file_names[idx]
					__SCREAMING_SNAKE_CASE       :		Tuple           =		PIL.Image.open(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		raw_image.convert("""RGB"""						)
					if self.image_transform is not None:
							__SCREAMING_SNAKE_CASE       :		List[str]           =		self.image_transform(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		str           =		extract_label(lowerCAmelCase__						)
					if self.label_to_id is not None:
							__SCREAMING_SNAKE_CASE       :		Optional[int]           =		self.label_to_id[label]
					return {"image": image, "label": label}


def 				lowerCAmelCase_   (	_lowerCamelCase:   Optional[Any]       ,      _lowerCamelCase:   Optional[int]					):
		# Initialize accelerator
		if args.with_tracking:
				__SCREAMING_SNAKE_CASE       :		List[str]           =		Accelerator(
				    cpu=args.cpu       ,      mixed_precision=args.mixed_precision       ,      log_with="""all"""       ,      project_dir=args.project_dir					)
		else:
				__SCREAMING_SNAKE_CASE       :		List[Any]           =		Accelerator(cpu=args.cpu       ,      mixed_precision=args.mixed_precision					)

		# Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
		__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		config["""lr"""]
		__SCREAMING_SNAKE_CASE       :		int           =		int(config["""num_epochs"""]					)
		__SCREAMING_SNAKE_CASE       :		List[Any]           =		int(config["""seed"""]					)
		__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		int(config["""batch_size"""]					)
		__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		config["""image_size"""]
		if not isinstance(_lowerCamelCase       ,      (list, tuple)					):
				__SCREAMING_SNAKE_CASE       :		Tuple           =		(image_size, image_size)

		# Parse out whether we are saving every epoch or after a certain number of batches
		if hasattr(args.checkpointing_steps       ,      """isdigit"""					):
				if args.checkpointing_steps == "epoch":
						__SCREAMING_SNAKE_CASE       :		Tuple           =		args.checkpointing_steps
				elif args.checkpointing_steps.isdigit():
						__SCREAMING_SNAKE_CASE       :		List[str]           =		int(args.checkpointing_steps					)
				else:
						raise ValueError(
						    F"Argument `checkpointing_steps` must be either a number or `epoch`. `{args.checkpointing_steps}` passed."					)
		else:
				__SCREAMING_SNAKE_CASE       :		str           =		None

		# We need to initialize the trackers we use, and also store our configuration
		if args.with_tracking:
				__SCREAMING_SNAKE_CASE       :		Tuple           =		os.path.split(_lowerCamelCase					)[-1].split("""."""					)[0]
				accelerator.init_trackers(_lowerCamelCase       ,      _lowerCamelCase					)

		# Grab all the image filenames
		__SCREAMING_SNAKE_CASE       :		Any           =		[os.path.join(args.data_dir       ,      _lowerCamelCase					) for fname in os.listdir(args.data_dir					) if fname.endswith(""".jpg"""					)]

		# Build the label correspondences
		__SCREAMING_SNAKE_CASE       :		str           =		[extract_label(_lowerCamelCase					) for fname in file_names]
		__SCREAMING_SNAKE_CASE       :		Dict           =		list(set(_lowerCamelCase					)					)
		id_to_label.sort()
		__SCREAMING_SNAKE_CASE       :		Dict           =		{lbl: i for i, lbl in enumerate(_lowerCamelCase					)}

		# Set the seed before splitting the data.
		np.random.seed(_lowerCamelCase					)
		torch.manual_seed(_lowerCamelCase					)
		torch.cuda.manual_seed_all(_lowerCamelCase					)

		# Split our filenames between train and validation
		__SCREAMING_SNAKE_CASE       :		Optional[int]           =		np.random.permutation(len(_lowerCamelCase					)					)
		__SCREAMING_SNAKE_CASE       :		Optional[int]           =		int(0.8 * len(_lowerCamelCase					)					)
		__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		random_perm[:cut]
		__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		random_perm[cut:]

		# For training we use a simple RandomResizedCrop
		__SCREAMING_SNAKE_CASE       :		str           =		Compose([RandomResizedCrop(_lowerCamelCase       ,      scale=(0.5, 1.0)					), ToTensor()]					)
		__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		PetsDataset(
		    [file_names[i] for i in train_split]       ,      image_transform=_lowerCamelCase       ,      label_to_id=_lowerCamelCase					)

		# For evaluation, we use a deterministic Resize
		__SCREAMING_SNAKE_CASE       :		List[str]           =		Compose([Resize(_lowerCamelCase					), ToTensor()]					)
		__SCREAMING_SNAKE_CASE       :		int           =		PetsDataset([file_names[i] for i in eval_split]       ,      image_transform=_lowerCamelCase       ,      label_to_id=_lowerCamelCase					)

		# Instantiate dataloaders.
		__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		DataLoader(_lowerCamelCase       ,      shuffle=_lowerCamelCase       ,      batch_size=_lowerCamelCase       ,      num_workers=4					)
		__SCREAMING_SNAKE_CASE       :		Optional[int]           =		DataLoader(_lowerCamelCase       ,      shuffle=_lowerCamelCase       ,      batch_size=_lowerCamelCase       ,      num_workers=4					)

		# Instantiate the model (we build the model here so that the seed also control new weights initialization)
		__SCREAMING_SNAKE_CASE       :		Any           =		create_model("""resnet50d"""       ,      pretrained=_lowerCamelCase       ,      num_classes=len(_lowerCamelCase					)					)

		# We could avoid this line since the accelerator is set with `device_placement=True` (default value).
		# Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
		# creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
		__SCREAMING_SNAKE_CASE       :		Tuple           =		model.to(accelerator.device					)

		# Freezing the base model
		for param in model.parameters():
				__SCREAMING_SNAKE_CASE       :		int           =		False
		for param in model.get_classifier().parameters():
				__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		True

		# We normalize the batches of images to be a bit faster.
		__SCREAMING_SNAKE_CASE       :		Optional[int]           =		torch.tensor(model.default_cfg["""mean"""]					)[None, :, None, None].to(accelerator.device					)
		__SCREAMING_SNAKE_CASE       :		List[str]           =		torch.tensor(model.default_cfg["""std"""]					)[None, :, None, None].to(accelerator.device					)

		# Instantiate optimizer
		__SCREAMING_SNAKE_CASE       :		Tuple           =		torch.optim.Adam(params=model.parameters()       ,      lr=lr / 25					)

		# Instantiate learning rate scheduler
		__SCREAMING_SNAKE_CASE       :		str           =		OneCycleLR(optimizer=_lowerCamelCase       ,      max_lr=_lowerCamelCase       ,      epochs=_lowerCamelCase       ,      steps_per_epoch=len(_lowerCamelCase					)					)

		# Prepare everything
		# There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
		# prepare method.
		__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		Dict           =		accelerator.prepare(
		    _lowerCamelCase       ,      _lowerCamelCase       ,      _lowerCamelCase       ,      _lowerCamelCase       ,      _lowerCamelCase					)
		# We need to keep track of how many total steps we have iterated over
		__SCREAMING_SNAKE_CASE       :		str           =		0
		# We also need to keep track of the starting epoch so files are named properly
		__SCREAMING_SNAKE_CASE       :		List[Any]           =		0

		# Potentially load in the weights and states from a previous save
		if args.resume_from_checkpoint:
				if args.resume_from_checkpoint is not None or args.resume_from_checkpoint != "":
						accelerator.print(F"Resumed from checkpoint: {args.resume_from_checkpoint}"					)
						accelerator.load_state(args.resume_from_checkpoint					)
						__SCREAMING_SNAKE_CASE       :		int           =		os.path.basename(args.resume_from_checkpoint					)
				else:
						# Get the most recent checkpoint
						__SCREAMING_SNAKE_CASE       :		List[str]           =		[f.name for f in os.scandir(os.getcwd()					) if f.is_dir()]
						dirs.sort(key=os.path.getctime					)
						__SCREAMING_SNAKE_CASE       :		str           =		dirs[-1]  # Sorts folders by date modified, most recent checkpoint is the last
				# Extract `epoch_{i}` or `step_{i}`
				__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		os.path.splitext(_lowerCamelCase					)[0]

				if "epoch" in training_difference:
						__SCREAMING_SNAKE_CASE       :		int           =		int(training_difference.replace("""epoch_"""       ,      """"""					)					) + 1
						__SCREAMING_SNAKE_CASE       :		Tuple           =		None
				else:
						__SCREAMING_SNAKE_CASE       :		Optional[int]           =		int(training_difference.replace("""step_"""       ,      """"""					)					)
						__SCREAMING_SNAKE_CASE       :		Any           =		resume_step // len(_lowerCamelCase					)
						resume_step -= starting_epoch * len(_lowerCamelCase					)

    # Now we train the model
		for epoch in range(_lowerCamelCase       ,      _lowerCamelCase					):
				model.train()
				if args.with_tracking:
						__SCREAMING_SNAKE_CASE       :		List[str]           =		0
				if args.resume_from_checkpoint and epoch == starting_epoch and resume_step is not None:
						# We need to skip steps until we reach the resumed step
						__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		accelerator.skip_first_batches(_lowerCamelCase       ,      _lowerCamelCase					)
						overall_step += resume_step
				else:
						# After the first iteration though, we need to go back to the original dataloader
						__SCREAMING_SNAKE_CASE       :		List[Any]           =		train_dataloader
				for batch in active_dataloader:
						# We could avoid this line since we set the accelerator with `device_placement=True`.
						__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		{k: v.to(accelerator.device					) for k, v in batch.items()}
						__SCREAMING_SNAKE_CASE       :		str           =		(batch["""image"""] - mean) / std
						__SCREAMING_SNAKE_CASE       :		List[Any]           =		model(_lowerCamelCase					)
						__SCREAMING_SNAKE_CASE       :		List[Any]           =		torch.nn.functional.cross_entropy(_lowerCamelCase       ,      batch["""label"""]					)
						# We keep track of the loss at each epoch
						if args.with_tracking:
								total_loss += loss.detach().float()
						accelerator.backward(_lowerCamelCase					)
						optimizer.step()
						lr_scheduler.step()
						optimizer.zero_grad()
						overall_step += 1
						if isinstance(_lowerCamelCase       ,      _lowerCamelCase					):
								__SCREAMING_SNAKE_CASE       :		str           =		F"step_{overall_step}"
								if overall_step % checkpointing_steps == 0:
										if args.output_dir is not None:
												__SCREAMING_SNAKE_CASE       :		List[Any]           =		os.path.join(args.output_dir       ,      _lowerCamelCase					)
										accelerator.save_state(_lowerCamelCase					)
				model.eval()
				__SCREAMING_SNAKE_CASE       :		List[str]           =		0
				__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		0
				for step, batch in enumerate(_lowerCamelCase					):
						# We could avoid this line since we set the accelerator with `device_placement=True`.
						__SCREAMING_SNAKE_CASE       :		int           =		{k: v.to(accelerator.device					) for k, v in batch.items()}
						__SCREAMING_SNAKE_CASE       :		Dict           =		(batch["""image"""] - mean) / std
						with torch.no_grad():
								__SCREAMING_SNAKE_CASE       :		List[str]           =		model(_lowerCamelCase					)
						__SCREAMING_SNAKE_CASE       :		List[Any]           =		outputs.argmax(dim=-1					)
						__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		str           =		accelerator.gather_for_metrics((predictions, batch["""label"""])					)
						__SCREAMING_SNAKE_CASE       :		Dict           =		predictions == references
						num_elems += accurate_preds.shape[0]
						accurate += accurate_preds.long().sum()

				__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		accurate.item() / num_elems
				# Use accelerator.print to print only on the main process.
				accelerator.print(F"epoch {epoch}: {1_00 * eval_metric:.2f}"					)
				if args.with_tracking:
						accelerator.log(
						    {
						        """accuracy""": 1_00 * eval_metric,
						        """train_loss""": total_loss.item() / len(_lowerCamelCase					),
						        """epoch""": epoch,
						    }       ,      step=_lowerCamelCase       ,      )
				if checkpointing_steps == "epoch":
						__SCREAMING_SNAKE_CASE       :		Any           =		F"epoch_{epoch}"
						if args.output_dir is not None:
								__SCREAMING_SNAKE_CASE       :		Tuple           =		os.path.join(args.output_dir       ,      _lowerCamelCase					)
						accelerator.save_state(_lowerCamelCase					)

		if args.with_tracking:
				accelerator.end_training()


def 				lowerCAmelCase_   (	):
		__SCREAMING_SNAKE_CASE       :		Any           =		argparse.ArgumentParser(description="""Simple example of training script."""					)
		parser.add_argument("""--data_dir"""       ,      required=_lowerCamelCase       ,      help="""The data folder on disk."""					)
		parser.add_argument("""--fp16"""       ,      action="""store_true"""       ,      help="""If passed, will use FP16 training."""					)
		parser.add_argument(
		    """--mixed_precision"""       ,      type=_lowerCamelCase       ,      default=_lowerCamelCase       ,      choices=["""no""", """fp16""", """bf16""", """fp8"""]       ,      help="""Whether to use mixed precision. Choose"""
		    """between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10."""
		    """and an Nvidia Ampere GPU."""       ,      )
		parser.add_argument("""--cpu"""       ,      action="""store_true"""       ,      help="""If passed, will train on the CPU."""					)
		parser.add_argument(
		    """--checkpointing_steps"""       ,      type=_lowerCamelCase       ,      default=_lowerCamelCase       ,      help="""Whether the various states should be saved at the end of every n steps, or 'epoch' for each epoch."""       ,      )
		parser.add_argument(
		    """--output_dir"""       ,      type=_lowerCamelCase       ,      default="""."""       ,      help="""Optional save directory where all checkpoint folders will be stored. Default is the current working directory."""       ,      )
		parser.add_argument(
		    """--resume_from_checkpoint"""       ,      type=_lowerCamelCase       ,      default=_lowerCamelCase       ,      help="""If the training should continue from a checkpoint folder."""       ,      )
		parser.add_argument(
		    """--with_tracking"""       ,      action="""store_true"""       ,      help="""Whether to load in all available experiment trackers from the environment and use them for logging."""       ,      )
		parser.add_argument(
		    """--project_dir"""       ,      type=_lowerCamelCase       ,      default="""logs"""       ,      help="""Location on where to store experiment tracking logs` and relevent project information"""       ,      )
		__SCREAMING_SNAKE_CASE       :		Dict           =		parser.parse_args()
		__SCREAMING_SNAKE_CASE       :		Tuple           =		{"""lr""": 3E-2, """num_epochs""": 3, """seed""": 42, """batch_size""": 64, """image_size""": 2_24}
		training_function(_lowerCamelCase       ,      _lowerCamelCase					)


if __name__ == "__main__":
						main()




'''simple docstring'''




import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
						from PIL import Image
else:






						class 						_UpperCamelCase  :


									'''simple docstring'''

									@staticmethod
									def UpperCamelCase__					(							*lowerCAmelCase__     : Any							,     **lowerCAmelCase__     : Any						):





											"""simple docstring"""






											pass






@is_pipeline_test
@require_vision
@require_torch
class 						_UpperCamelCase  (							unittest.TestCase    ):


			'''simple docstring'''

			_A      :		Optional[int]			    =		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

			def UpperCamelCase__					(							self     : str							,     lowerCAmelCase__     : Dict							,     lowerCAmelCase__     : Tuple							,     lowerCAmelCase__     : Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		pipeline(
					    """zero-shot-object-detection"""							,     model="""hf-internal-testing/tiny-random-owlvit-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		Any           =		[
					    {
					        """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
					        """candidate_labels""": ["""cat""", """remote""", """couch"""],
					    }
					]
					return object_detector, examples

			def UpperCamelCase__					(							self     : int							,     lowerCAmelCase__     : Optional[Any]							,     lowerCAmelCase__     : List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		object_detector(examples[0]							,     threshold=0.0						)

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		len(lowerCAmelCase__						)
					self.assertGreater(lowerCAmelCase__							,     0						)
					self.assertEqual(
					    lowerCAmelCase__							,     [
					        {
					            """score""": ANY(lowerCAmelCase__						),
					            """label""": ANY(lowerCAmelCase__						),
					            """box""": {"""xmin""": ANY(lowerCAmelCase__						), """ymin""": ANY(lowerCAmelCase__						), """xmax""": ANY(lowerCAmelCase__						), """ymax""": ANY(lowerCAmelCase__						)},
					        }
					        for i in range(lowerCAmelCase__						)
					    ]							,     )

			@require_tf
			@unittest.skip("""Zero Shot Object Detection not implemented in TF"""						)
			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					pass

			@require_torch
			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		int           =		pipeline(
					    """zero-shot-object-detection"""							,     model="""hf-internal-testing/tiny-random-owlvit-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		int           =		object_detector(
					    """./tests/fixtures/tests_samples/COCO/000000039769.png"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     threshold=0.64							,     )

					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					        {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					        {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					        {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					        {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					        {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					        {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					        {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
					        {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		List[Any]           =		object_detector(
					    [
					        {
					            """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
					            """candidate_labels""": ["""cat""", """remote""", """couch"""],
					        }
					    ]							,     threshold=0.64							,     )

					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        [
					            {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					            {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					            {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					            {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					            {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					            {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					            {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					            {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
					            {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					        ]
					    ]							,     )

			@require_torch
			@slow
			def UpperCamelCase__					(							self     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		pipeline("""zero-shot-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		List[str]           =		object_detector(
					    """http://images.cocodataset.org/val2017/000000039769.jpg"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					        {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					        {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					        {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
					        {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		Dict           =		object_detector(
					    [
					        {
					            """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
					            """candidate_labels""": ["""cat""", """remote""", """couch"""],
					        },
					        {
					            """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
					            """candidate_labels""": ["""cat""", """remote""", """couch"""],
					        },
					    ]							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        [
					            {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					            {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					            {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					            {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
					            {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
					        ],
					        [
					            {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					            {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					            {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					            {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
					            {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
					        ],
					    ]							,     )

			@require_tf
			@unittest.skip("""Zero Shot Object Detection not implemented in TF"""						)
			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					pass

			@require_torch
			@slow
			def UpperCamelCase__					(							self     : List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Dict           =		0.2
					__SCREAMING_SNAKE_CASE       :		Dict           =		pipeline("""zero-shot-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		Any           =		object_detector(
					    """http://images.cocodataset.org/val2017/000000039769.jpg"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     threshold=lowerCAmelCase__							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					        {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					        {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					    ]							,     )



			@require_torch
			@slow
			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		str           =		2
					__SCREAMING_SNAKE_CASE       :		int           =		pipeline("""zero-shot-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		int           =		object_detector(
					    """http://images.cocodataset.org/val2017/000000039769.jpg"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     top_k=lowerCAmelCase__							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					        {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					    ]							,     )