


'''simple docstring'''







import argparse
import json
import os.path
from collections import OrderedDict

import numpy as np
import requests
import torch
from flax.training.checkpoints import restore_checkpoint
from huggingface_hub import hf_hub_download

from transformers import VivitConfig, VivitForVideoClassification, VivitImageProcessor
from transformers.image_utils import PILImageResampling



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				__magic_name__				:					Tuple						       =       "https://storage.googleapis.com/scenic-bucket/vivit/kinetics_400/vivit_base_16x2_unfactorized/checkpoint"

				with open(UpperCamelCase__     ,   "wb"		) as f:
								with requests.get(UpperCamelCase__     ,   stream=UpperCamelCase__		) as req:
												for chunk in req.iter_content(chunk_size=2048		):
																f.write(UpperCamelCase__		)



def 						_UpperCamelCase							( ):






				"""simple docstring"""



				__magic_name__				:					int						       =       VivitConfig()

				__magic_name__				:					List[str]						       =       400
				__magic_name__				:					Dict						       =       "huggingface/label-files"
				__magic_name__				:					Union[str, Any]						       =       "kinetics400-id2label.json"

				__magic_name__				:					int						       =       json.load(open(hf_hub_download(UpperCamelCase__     ,   UpperCamelCase__     ,   repo_type="dataset"		)     ,   "r"		)		)
				__magic_name__				:					str						       =       {int(UpperCamelCase__		): v for k, v in idalabel.items()}
				__magic_name__				:					Any						       =       idalabel
				__magic_name__				:					int						       =       {v: k for k, v in idalabel.items()}
				return config



def 						_UpperCamelCase							( ):






				"""simple docstring"""



				__magic_name__				:					Any						       =       hf_hub_download(
				    repo_id="hf-internal-testing/spaghetti-video"     ,   filename="eating_spaghetti_32_frames.npy"     ,   repo_type="dataset"		)
				__magic_name__				:					List[Any]						       =       np.load(UpperCamelCase__		)
				return list(UpperCamelCase__		)



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				if np.ndim(UpperCamelCase__		) == 2:
								return transform_attention_bias(UpperCamelCase__		)

				elif np.ndim(UpperCamelCase__		) == 3:
								return transform_attention_kernel(UpperCamelCase__		)

				else:
								raise Exception(F"""Invalid number of dimesions: {np.ndim(UpperCamelCase__		)}"""		)



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				return current.flatten()



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				return np.reshape(UpperCamelCase__     ,   (current.shape[0], current.shape[1] * current.shape[2])		).T



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				return np.reshape(UpperCamelCase__     ,   (current.shape[0] * current.shape[1], current.shape[2])		).T



def 						_UpperCamelCase							( UpperCamelCase__     ,   UpperCamelCase__		):






				"""simple docstring"""



				__magic_name__				:					Any						       =       state_dict["optimizer"]["target"]["Transformer"][F"""encoderblock_{i}"""]

				__magic_name__				:					List[str]						       =       F"""encoder.layer.{i}."""
				__magic_name__				:					Any						       =       {
				    prefix + "intermediate.dense.bias": state["MlpBlock_0"]["Dense_0"]["bias"],
				    prefix + "intermediate.dense.weight": np.transpose(state["MlpBlock_0"]["Dense_0"]["kernel"]		),
				    prefix + "output.dense.bias": state["MlpBlock_0"]["Dense_1"]["bias"],
				    prefix + "output.dense.weight": np.transpose(state["MlpBlock_0"]["Dense_1"]["kernel"]		),
				    prefix + "layernorm_before.bias": state["LayerNorm_0"]["bias"],
				    prefix + "layernorm_before.weight": state["LayerNorm_0"]["scale"],
				    prefix + "layernorm_after.bias": state["LayerNorm_1"]["bias"],
				    prefix + "layernorm_after.weight": state["LayerNorm_1"]["scale"],
				    prefix
				    + "attention.attention.query.bias": transform_attention(
				        state["MultiHeadDotProductAttention_0"]["query"]["bias"]		),
				    prefix
				    + "attention.attention.query.weight": transform_attention(
				        state["MultiHeadDotProductAttention_0"]["query"]["kernel"]		),
				    prefix
				    + "attention.attention.key.bias": transform_attention(state["MultiHeadDotProductAttention_0"]["key"]["bias"]		),
				    prefix
				    + "attention.attention.key.weight": transform_attention(
				        state["MultiHeadDotProductAttention_0"]["key"]["kernel"]		),
				    prefix
				    + "attention.attention.value.bias": transform_attention(
				        state["MultiHeadDotProductAttention_0"]["value"]["bias"]		),
				    prefix
				    + "attention.attention.value.weight": transform_attention(
				        state["MultiHeadDotProductAttention_0"]["value"]["kernel"]		),
				    prefix + "attention.output.dense.bias": state["MultiHeadDotProductAttention_0"]["out"]["bias"],
				    prefix
				    + "attention.output.dense.weight": transform_attention_output_weight(
				        state["MultiHeadDotProductAttention_0"]["out"]["kernel"]		),
				}

				return new_state



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				return sum([1 if "encoderblock_" in k else 0 for k in state_dict["optimizer"]["target"]["Transformer"].keys()]		)



def 						_UpperCamelCase							( UpperCamelCase__     ,   UpperCamelCase__=False		):






				"""simple docstring"""



				__magic_name__				:					int						       =       get_n_layers(UpperCamelCase__		)

				__magic_name__				:					int						       =       OrderedDict()

				__magic_name__				:					str						       =       state_dict["optimizer"]["target"]["Transformer"]["encoder_norm"]["bias"]
				__magic_name__				:					int						       =       state_dict["optimizer"]["target"]["Transformer"]["encoder_norm"]["scale"]

				__magic_name__				:					Optional[Any]						       =       np.transpose(
				    state_dict["optimizer"]["target"]["embedding"]["kernel"]     ,   (4, 3, 0, 1, 2)		)
				__magic_name__				:					List[Any]						       =       state_dict["optimizer"]["target"]["embedding"]["bias"]

				__magic_name__				:					Union[str, Any]						       =       state_dict["optimizer"]["target"]["cls"]
				__magic_name__				:					Any						       =       state_dict["optimizer"]["target"]["Transformer"]["posembed_input"][
				    "pos_embedding"
				]

				for i in range(UpperCamelCase__		):
								new_state.update(transform_state_encoder_block(UpperCamelCase__     ,   UpperCamelCase__		)		)

				if classification_head:
								__magic_name__				:					Optional[int]						       =       {"vivit." + k: v for k, v in new_state.items()}
								__magic_name__				:					List[Any]						       =       np.transpose(state_dict["optimizer"]["target"]["output_projection"]["kernel"]		)
								__magic_name__				:					Dict						       =       np.transpose(state_dict["optimizer"]["target"]["output_projection"]["bias"]		)

				return {k: torch.tensor(UpperCamelCase__		) for k, v in new_state.items()}



def 						_UpperCamelCase							( ):






				"""simple docstring"""



				__magic_name__				:					Dict						       =       VivitImageProcessor()

				assert extractor.do_resize is True
				assert extractor.size == {"shortest_edge": 256}
				assert extractor.do_center_crop is True
				assert extractor.crop_size == {"width": 224, "height": 224}
				assert extractor.resample == PILImageResampling.BILINEAR

				# here: https://github.com/deepmind/dmvr/blob/master/dmvr/modalities.py
				# one can seen that add_image has default values for normalization_mean and normalization_std set to 0 and 1
				# which effectively means no normalization (and ViViT does not overwrite those when calling this func)
				assert extractor.do_normalize is False
				assert extractor.do_rescale is True
				assert extractor.rescale_factor == 1 / 255

				# zero-centering = True in original implementation
				assert extractor.do_zero_centering is True

				return extractor



def 						_UpperCamelCase							( UpperCamelCase__		):






				"""simple docstring"""



				__magic_name__				:					Dict						       =       "checkpoint"

				if not os.path.exists(UpperCamelCase__		):
								download_checkpoint(UpperCamelCase__		)

				__magic_name__				:					Optional[Any]						       =       restore_checkpoint(UpperCamelCase__     ,   UpperCamelCase__		)
				__magic_name__				:					List[str]						       =       transform_state(UpperCamelCase__     ,   classification_head=UpperCamelCase__		)

				__magic_name__				:					int						       =       get_vivit_config()

				assert config.image_size == 224
				assert config.num_frames == 32

				__magic_name__				:					List[str]						       =       VivitForVideoClassification(UpperCamelCase__		)
				model.load_state_dict(UpperCamelCase__		)
				model.eval()

				__magic_name__				:					Optional[int]						       =       get_processor()

				__magic_name__				:					Union[str, Any]						       =       prepare_video()
				__magic_name__				:					Union[str, Any]						       =       extractor(UpperCamelCase__     ,   return_tensors="pt"		)

				__magic_name__				:					List[Any]						       =       model(**UpperCamelCase__		)

				__magic_name__				:					Optional[Any]						       =       torch.Size([1, 400]		)
				__magic_name__				:					List[str]						       =       torch.tensor([-1.0543, 2.0764, -0.2104, 0.4439, -0.9658]		)

				assert outputs.logits.shape == expected_shape
				assert torch.allclose(outputs.logits[0, :5]     ,   UpperCamelCase__     ,   atol=1E-4		), outputs.logits[0, :5]

				model.save_pretrained(UpperCamelCase__		)
				extractor.save_pretrained(UpperCamelCase__		)


if __name__ == "__main__":
		_SCREAMING_SNAKE_CASE       :   Dict        = argparse.ArgumentParser()

		parser.add_argument("--output_model_name", "-o", type=str, help="Output path for the converted HuggingFace model")

		_SCREAMING_SNAKE_CASE       :   Tuple        = parser.parse_args()
		convert(args.output_model_name)



'''simple docstring'''







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


if is_vision_available():
		import PIL


_SCREAMING_SNAKE_CASE       :   int        = logging.get_logger(__name__)






class 		_snake_case    (				snake_case_		):






						'''simple docstring'''







						__snake_case   =      ["pixel_values"]







						def __init__(  self:	Union[str, Any]      ,							__UpperCamelCase:	bool = True      ,							__UpperCamelCase:	Dict[str, int] = None      ,							__UpperCamelCase:	bool = True      ,							__UpperCamelCase:	Dict[str, int] = None      ,							__UpperCamelCase:	PILImageResampling = PILImageResampling.BICUBIC      ,							__UpperCamelCase:	bool = True      ,							__UpperCamelCase:	Union[int, float] = 1 / 255      ,							__UpperCamelCase:	bool = True      ,							__UpperCamelCase:	Optional[Union[float, List[float]]] = None      ,							__UpperCamelCase:	Optional[Union[float, List[float]]] = None      ,							**__UpperCamelCase:	Union[str, Any]      ,							)							->	None:
										super().__init__(**__UpperCamelCase     )
										__magic_name__				:					Optional[Any]						       =       crop_size if crop_size is not None else {"height": 256, "width": 256}
										__magic_name__				:					Optional[int]						       =       get_size_dict(__UpperCamelCase      ,							param_name="crop_size"     )
										__magic_name__				:					Union[str, Any]						       =       size if size is not None else {"height": 224, "width": 224}
										__magic_name__				:					Tuple						       =       get_size_dict(__UpperCamelCase     )

										__magic_name__				:					Tuple						       =       do_center_crop
										__magic_name__				:					int						       =       crop_size
										__magic_name__				:					Dict						       =       do_resize
										__magic_name__				:					Optional[Any]						       =       size
										__magic_name__				:					Optional[Any]						       =       resample
										__magic_name__				:					str						       =       do_rescale
										__magic_name__				:					int						       =       rescale_factor
										__magic_name__				:					Dict						       =       do_normalize
										__magic_name__				:					Any						       =       image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
										__magic_name__				:					str						       =       image_std if image_std is not None else IMAGENET_DEFAULT_STD







						def lowerCAmelCase__ (  self:	str      ,							__UpperCamelCase:	np.ndarray      ,							__UpperCamelCase:	Dict[str, int]      ,							__UpperCamelCase:	Optional[int] = None      ,							__UpperCamelCase:	Optional[Union[str, ChannelDimension]] = None      ,							**__UpperCamelCase:	List[str]      ,							)							->	np.ndarray:
										__magic_name__				:					List[str]						       =       self.size if size is None else size
										__magic_name__				:					Dict						       =       get_size_dict(__UpperCamelCase     )
										__magic_name__				:					Dict						       =       get_size_dict(__UpperCamelCase      ,							param_name="crop_size"     )

										__magic_name__       ,     __magic_name__				:					Dict						       =       get_image_size(__UpperCamelCase     )
										__magic_name__				:					int						       =       min(__UpperCamelCase      ,							__UpperCamelCase     )
										__magic_name__				:					Dict						       =       (size["height"] / crop_size["height"]) * min_dim
										__magic_name__				:					str						       =       (size["width"] / crop_size["width"]) * min_dim
										return center_crop(__UpperCamelCase      ,							size=(cropped_height, cropped_width)      ,							data_format=__UpperCamelCase      ,							**__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	int      ,							__UpperCamelCase:	np.ndarray      ,							__UpperCamelCase:	Dict[str, int]      ,							__UpperCamelCase:	PILImageResampling = PIL.Image.BICUBIC      ,							__UpperCamelCase:	Optional[Union[str, ChannelDimension]] = None      ,							**__UpperCamelCase:	Any      ,							)							->	np.ndarray:
										__magic_name__				:					Optional[Any]						       =       get_size_dict(__UpperCamelCase     )
										if "height" not in size or "width" not in size:
														raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}"""     )
										return resize(
										    __UpperCamelCase      ,							size=(size["height"], size["width"])      ,							resample=__UpperCamelCase      ,							data_format=__UpperCamelCase      ,							**__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	Optional[int]      ,							__UpperCamelCase:	np.ndarray      ,							__UpperCamelCase:	Union[int, float]      ,							__UpperCamelCase:	Optional[Union[str, ChannelDimension]] = None      ,							**__UpperCamelCase:	List[Any]      ,							)							->	List[Any]:
										return rescale(__UpperCamelCase      ,							scale=__UpperCamelCase      ,							data_format=__UpperCamelCase      ,							**__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	Any      ,							__UpperCamelCase:	np.ndarray      ,							__UpperCamelCase:	Union[float, List[float]]      ,							__UpperCamelCase:	Union[float, List[float]]      ,							__UpperCamelCase:	Optional[Union[str, ChannelDimension]] = None      ,							**__UpperCamelCase:	str      ,							)							->	np.ndarray:
										return normalize(__UpperCamelCase      ,							mean=__UpperCamelCase      ,							std=__UpperCamelCase      ,							data_format=__UpperCamelCase      ,							**__UpperCamelCase     )







						def lowerCAmelCase__ (  self:	str      ,							__UpperCamelCase:	ImageInput      ,							__UpperCamelCase:	Optional[bool] = None      ,							__UpperCamelCase:	Optional[Dict[str, int]] = None      ,							__UpperCamelCase:	Optional[bool] = None      ,							__UpperCamelCase:	Optional[Dict[str, int]] = None      ,							__UpperCamelCase:	PILImageResampling = None      ,							__UpperCamelCase:	Optional[bool] = None      ,							__UpperCamelCase:	Optional[float] = None      ,							__UpperCamelCase:	Optional[bool] = None      ,							__UpperCamelCase:	Optional[Union[float, List[float]]] = None      ,							__UpperCamelCase:	Optional[Union[float, List[float]]] = None      ,							__UpperCamelCase:	Optional[Union[str, TensorType]] = None      ,							__UpperCamelCase:	ChannelDimension = ChannelDimension.FIRST      ,							**__UpperCamelCase:	Dict      ,							)							->	PIL.Image.Image:
										__magic_name__				:					Optional[Any]						       =       do_center_crop if do_center_crop is not None else self.do_center_crop
										__magic_name__				:					Optional[Any]						       =       crop_size if crop_size is not None else self.crop_size
										__magic_name__				:					Tuple						       =       get_size_dict(__UpperCamelCase      ,							param_name="crop_size"     )
										__magic_name__				:					int						       =       do_resize if do_resize is not None else self.do_resize
										__magic_name__				:					Optional[Any]						       =       size if size is not None else self.size
										__magic_name__				:					Any						       =       get_size_dict(__UpperCamelCase     )
										__magic_name__				:					Optional[int]						       =       resample if resample is not None else self.resample
										__magic_name__				:					int						       =       do_rescale if do_rescale is not None else self.do_rescale
										__magic_name__				:					int						       =       rescale_factor if rescale_factor is not None else self.rescale_factor
										__magic_name__				:					Any						       =       do_normalize if do_normalize is not None else self.do_normalize
										__magic_name__				:					Dict						       =       image_mean if image_mean is not None else self.image_mean
										__magic_name__				:					Tuple						       =       image_std if image_std is not None else self.image_std

										__magic_name__				:					List[Any]						       =       make_list_of_images(__UpperCamelCase     )

										if not valid_images(__UpperCamelCase     ):
														raise ValueError(
														    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
														    "torch.Tensor, tf.Tensor or jax.ndarray."     )

										if do_center_crop and crop_size is None:
														raise ValueError("If `do_center_crop` is set to `True`, `crop_size` must be provided."     )

										if do_resize and size is None:
														raise ValueError("Size must be specified if do_resize is True."     )

										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 image standard deviation must be specified if do_normalize is True."     )

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

										if do_center_crop:
														__magic_name__				:					Union[str, Any]						       =       [self.center_crop(__UpperCamelCase      ,							__UpperCamelCase      ,							size=__UpperCamelCase     ) for image in images]

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

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

										if do_normalize:
														__magic_name__				:					Optional[int]						       =       [self.normalize(image=__UpperCamelCase      ,							mean=__UpperCamelCase      ,							std=__UpperCamelCase     ) for image in images]

										__magic_name__				:					List[Any]						       =       [to_channel_dimension_format(__UpperCamelCase      ,							__UpperCamelCase     ) for image in images]

										__magic_name__				:					Optional[int]						       =       {"pixel_values": images}
										return BatchFeature(data=__UpperCamelCase      ,							tensor_type=__UpperCamelCase     )