





from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


_a						=   logging.get_logger(__name__)


# General docstring
_a						=   '''MobileNetV1Config'''

# Base docstring
_a						=   '''google/mobilenet_v1_1.0_224'''
_a						=   [1, 1_024, 7, 7]

# Image classification docstring
_a						=   '''google/mobilenet_v1_1.0_224'''
_a						=   '''tabby, tabby cat'''


_a						=   [
    '''google/mobilenet_v1_1.0_224''',
    '''google/mobilenet_v1_0.75_192''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case=None							)       ->   Any:







	'''simple docstring'''







	lowerCamelCase__              =	{}

	if isinstance(__UpperCamelCase ,__UpperCamelCase							):
		lowerCamelCase__              =	model.mobilenet_va
	else:
		lowerCamelCase__              =	model

	lowerCamelCase__              =	"""MobilenetV1/Conv2d_0/"""
	lowerCamelCase__              =	backbone.conv_stem.convolution.weight
	lowerCamelCase__              =	backbone.conv_stem.normalization.bias
	lowerCamelCase__              =	backbone.conv_stem.normalization.weight
	lowerCamelCase__              =	backbone.conv_stem.normalization.running_mean
	lowerCamelCase__              =	backbone.conv_stem.normalization.running_var

	for i in range(13							):
		lowerCamelCase__              =	i + 1
		lowerCamelCase__              =	i * 2

		lowerCamelCase__              =	backbone.layer[pt_index]
		lowerCamelCase__              =	F'MobilenetV1/Conv2d_{tf_index}_depthwise/'
		lowerCamelCase__              =	pointer.convolution.weight
		lowerCamelCase__              =	pointer.normalization.bias
		lowerCamelCase__              =	pointer.normalization.weight
		lowerCamelCase__              =	pointer.normalization.running_mean
		lowerCamelCase__              =	pointer.normalization.running_var

		lowerCamelCase__              =	backbone.layer[pt_index + 1]
		lowerCamelCase__              =	F'MobilenetV1/Conv2d_{tf_index}_pointwise/'
		lowerCamelCase__              =	pointer.convolution.weight
		lowerCamelCase__              =	pointer.normalization.bias
		lowerCamelCase__              =	pointer.normalization.weight
		lowerCamelCase__              =	pointer.normalization.running_mean
		lowerCamelCase__              =	pointer.normalization.running_var

	if isinstance(__UpperCamelCase ,__UpperCamelCase							):
		lowerCamelCase__              =	"""MobilenetV1/Logits/Conv2d_1c_1x1/"""
		lowerCamelCase__              =	model.classifier.weight
		lowerCamelCase__              =	model.classifier.bias

	return tf_to_pt_map





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case							)       ->   str:







	'''simple docstring'''







	try:
		import numpy as np
		import tensorflow as tf
	except ImportError:
		logger.error(
		    '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
		    '''https://www.tensorflow.org/install/ for installation instructions.'''							)
		raise

	# Load weights from TF model
	lowerCamelCase__              =	tf.train.list_variables(__UpperCamelCase							)
	lowerCamelCase__              =	{}
	for name, shape in init_vars:
		logger.info(F'Loading TF weight {name} with shape {shape}'							)
		lowerCamelCase__              =	tf.train.load_variable(__UpperCamelCase ,__UpperCamelCase							)
		lowerCamelCase__              =	array

	# Build TF to PyTorch weights loading map
	lowerCamelCase__              =	_build_tf_to_pytorch_map(__UpperCamelCase ,__UpperCamelCase ,__UpperCamelCase							)

	for name, pointer in tf_to_pt_map.items():
		logger.info(F'Importing {name}'							)
		if name not in tf_weights:
			logger.info(F'{name} not in tf pre-trained weights, skipping'							)
			continue

		lowerCamelCase__              =	tf_weights[name]

		if "depthwise_weights" in name:
			logger.info('''Transposing depthwise'''							)
			lowerCamelCase__              =	np.transpose(__UpperCamelCase ,(2, 3, 0, 1)							)
		elif "weights" in name:
			logger.info('''Transposing'''							)
			if len(pointer.shape							) == 2:  # copying into linear layer
				lowerCamelCase__              =	array.squeeze().transpose()
			else:
				lowerCamelCase__              =	np.transpose(__UpperCamelCase ,(3, 2, 0, 1)							)

		if pointer.shape != array.shape:
			raise ValueError(F'Pointer shape {pointer.shape} and array shape {array.shape} mismatched'							)

		logger.info(F'Initialize PyTorch weight {name} {array.shape}'							)
		lowerCamelCase__              =	torch.from_numpy(__UpperCamelCase							)

		tf_weights.pop(__UpperCamelCase ,__UpperCamelCase							)
		tf_weights.pop(name + '''/RMSProp''' ,__UpperCamelCase							)
		tf_weights.pop(name + '''/RMSProp_1''' ,__UpperCamelCase							)
		tf_weights.pop(name + '''/ExponentialMovingAverage''' ,__UpperCamelCase							)

	logger.info(F'Weights not copied to PyTorch model: {", ".join(tf_weights.keys()							)}'							)
	return model





def lowerCAmelCase__(__snake_case ,__snake_case							)       ->   List[Any]:







	'''simple docstring'''







	lowerCamelCase__              =	features.shape[-2:]
	lowerCamelCase__              =	conv_layer.stride
	lowerCamelCase__              =	conv_layer.kernel_size

	if in_height % stride_height == 0:
		lowerCamelCase__              =	max(kernel_height - stride_height ,0							)
	else:
		lowerCamelCase__              =	max(kernel_height - (in_height % stride_height) ,0							)

	if in_width % stride_width == 0:
		lowerCamelCase__              =	max(kernel_width - stride_width ,0							)
	else:
		lowerCamelCase__              =	max(kernel_width - (in_width % stride_width) ,0							)

	lowerCamelCase__              =	pad_along_width // 2
	lowerCamelCase__              =	pad_along_width - pad_left
	lowerCamelCase__              =	pad_along_height // 2
	lowerCamelCase__              =	pad_along_height - pad_top

	lowerCamelCase__              =	(pad_left, pad_right, pad_top, pad_bottom)
	return nn.functional.pad(__UpperCamelCase ,__UpperCamelCase ,'''constant''' ,0.0							)






class       __A					(					nn.Module				):

			'''simple docstring'''







			def __init__(					self ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase = 1 ,						__lowerCAmelCase = 1 ,						__lowerCAmelCase = False ,						__lowerCAmelCase = True ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				super().__init__()
				lowerCamelCase__              =	config

				if in_channels % groups != 0:
					raise ValueError(F'Input channels ({in_channels}) are not divisible by {groups} groups.'							)
				if out_channels % groups != 0:
					raise ValueError(F'Output channels ({out_channels}) are not divisible by {groups} groups.'							)

				lowerCamelCase__              =	0 if config.tf_padding else int((kernel_size - 1) / 2							)

				lowerCamelCase__              =	nn.Convad(
				    in_channels=_lowercase ,						out_channels=_lowercase ,						kernel_size=_lowercase ,						stride=_lowercase ,						padding=_lowercase ,						groups=_lowercase ,						bias=_lowercase ,						padding_mode='''zeros''' ,						)

				if use_normalization:
					lowerCamelCase__              =	nn.BatchNormad(
					    num_features=_lowercase ,						eps=config.layer_norm_eps ,						momentum=0.9997 ,						affine=_lowercase ,						track_running_stats=_lowercase ,						)
				else:
					lowerCamelCase__              =	None

				if use_activation:
					if isinstance(_lowercase ,						_lowercase							):
						lowerCamelCase__              =	ACTaFN[use_activation]
					elif isinstance(config.hidden_act ,						_lowercase							):
						lowerCamelCase__              =	ACTaFN[config.hidden_act]
					else:
						lowerCamelCase__              =	config.hidden_act
				else:
					lowerCamelCase__              =	None





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase							):



				'''simple docstring'''







				if self.config.tf_padding:
					lowerCamelCase__              =	apply_tf_padding(_lowercase ,						self.convolution							)
				lowerCamelCase__              =	self.convolution(_lowercase							)
				if self.normalization is not None:
					lowerCamelCase__              =	self.normalization(_lowercase							)
				if self.activation is not None:
					lowerCamelCase__              =	self.activation(_lowercase							)
				return features






class       __A					(					SCREAMING_SNAKE_CASE__				):

			'''simple docstring'''







			lowerCAmelCase_							  =		MobileNetVaConfig
			lowerCAmelCase_							  =		load_tf_weights_in_mobilenet_va
			lowerCAmelCase_							  =		"""mobilenet_v1"""
			lowerCAmelCase_							  =		"""pixel_values"""
			lowerCAmelCase_							  =		False





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase							):



				'''simple docstring'''







				if isinstance(_lowercase ,						(nn.Linear, nn.Convad)							):
					module.weight.data.normal_(mean=0.0 ,						std=self.config.initializer_range							)
					if module.bias is not None:
						module.bias.data.zero_()
				elif isinstance(_lowercase ,						nn.BatchNormad							):
					module.bias.data.zero_()
					module.weight.data.fill_(1.0							)



_a						=   R'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

_a						=   R'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`MobileNetV1ImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
'''






@add_start_docstrings(
    """The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."""    ,       SCREAMING_SNAKE_CASE__    ,       )
class       __A					(					SCREAMING_SNAKE_CASE__				):

			'''simple docstring'''







			def __init__(					self ,						__lowerCAmelCase ,						__lowerCAmelCase = True							):



				'''simple docstring'''







				super().__init__(_lowercase							)
				lowerCamelCase__              =	config

				lowerCamelCase__              =	3_2
				lowerCamelCase__              =	max(int(depth * config.depth_multiplier							) ,						config.min_depth							)

				lowerCamelCase__              =	MobileNetVaConvLayer(
				    _lowercase ,						in_channels=config.num_channels ,						out_channels=_lowercase ,						kernel_size=3 ,						stride=2 ,						)

				lowerCamelCase__              =	[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

				lowerCamelCase__              =	nn.ModuleList()
				for i in range(1_3							):
					lowerCamelCase__              =	out_channels

					if strides[i] == 2 or i == 0:
						depth *= 2
						lowerCamelCase__              =	max(int(depth * config.depth_multiplier							) ,						config.min_depth							)

					self.layer.append(
					    MobileNetVaConvLayer(
					        _lowercase ,						in_channels=_lowercase ,						out_channels=_lowercase ,						kernel_size=3 ,						stride=strides[i] ,						groups=_lowercase ,						)							)

					self.layer.append(
					    MobileNetVaConvLayer(
					        _lowercase ,						in_channels=_lowercase ,						out_channels=_lowercase ,						kernel_size=1 ,						)							)

				lowerCamelCase__              =	nn.AdaptiveAvgPoolad((1, 1)							) if add_pooling_layer else None

				# Initialize weights and apply final processing
				self.post_init()





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase							):



				'''simple docstring'''







				raise NotImplementedError





			@add_start_docstrings_to_model_forward(_lowercase							)
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC ,						output_type=_lowercase ,						config_class=_CONFIG_FOR_DOC ,						modality='''vision''' ,						expected_output=_EXPECTED_OUTPUT_SHAPE ,						)
			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						):



				'''simple docstring'''







				lowerCamelCase__              =	(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				lowerCamelCase__              =	return_dict if return_dict is not None else self.config.use_return_dict

				if pixel_values is None:
					raise ValueError('''You have to specify pixel_values'''							)

				lowerCamelCase__              =	self.conv_stem(_lowercase							)

				lowerCamelCase__              =	() if output_hidden_states else None

				for i, layer_module in enumerate(self.layer							):
					lowerCamelCase__              =	layer_module(_lowercase							)

					if output_hidden_states:
						lowerCamelCase__              =	all_hidden_states + (hidden_states,)

				lowerCamelCase__              =	hidden_states

				if self.pooler is not None:
					lowerCamelCase__              =	torch.flatten(self.pooler(_lowercase							) ,						start_dim=1							)
				else:
					lowerCamelCase__              =	None

				if not return_dict:
					return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None							)

				return BaseModelOutputWithPoolingAndNoAttention(
				    last_hidden_state=_lowercase ,						pooler_output=_lowercase ,						hidden_states=_lowercase ,						)






@add_start_docstrings(
    """
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """    ,       SCREAMING_SNAKE_CASE__    ,       )
class       __A					(					SCREAMING_SNAKE_CASE__				):

			'''simple docstring'''







			def __init__(					self ,						__lowerCAmelCase							):



				'''simple docstring'''







				super().__init__(_lowercase							)

				lowerCamelCase__              =	config.num_labels
				lowerCamelCase__              =	MobileNetVaModel(_lowercase							)

				lowerCamelCase__              =	self.mobilenet_va.layer[-1].convolution.out_channels

				# Classifier head
				lowerCamelCase__              =	nn.Dropout(config.classifier_dropout_prob ,						inplace=_lowercase							)
				lowerCamelCase__              =	nn.Linear(_lowercase ,						config.num_labels							) if config.num_labels > 0 else nn.Identity()

				# Initialize weights and apply final processing
				self.post_init()





			@add_start_docstrings_to_model_forward(_lowercase							)
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT ,						output_type=_lowercase ,						config_class=_CONFIG_FOR_DOC ,						expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT ,						)
			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						):



				'''simple docstring'''







				lowerCamelCase__              =	return_dict if return_dict is not None else self.config.use_return_dict

				lowerCamelCase__              =	self.mobilenet_va(_lowercase ,						output_hidden_states=_lowercase ,						return_dict=_lowercase							)

				lowerCamelCase__              =	outputs.pooler_output if return_dict else outputs[1]

				lowerCamelCase__              =	self.classifier(self.dropout(_lowercase							)							)

				lowerCamelCase__              =	None
				if labels is not None:
					if self.config.problem_type is None:
						if self.num_labels == 1:
							lowerCamelCase__              =	"""regression"""
						elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
							lowerCamelCase__              =	"""single_label_classification"""
						else:
							lowerCamelCase__              =	"""multi_label_classification"""

					if self.config.problem_type == "regression":
						lowerCamelCase__              =	MSELoss()
						if self.num_labels == 1:
							lowerCamelCase__              =	loss_fct(logits.squeeze() ,						labels.squeeze()							)
						else:
							lowerCamelCase__              =	loss_fct(_lowercase ,						_lowercase							)
					elif self.config.problem_type == "single_label_classification":
						lowerCamelCase__              =	CrossEntropyLoss()
						lowerCamelCase__              =	loss_fct(logits.view(-1 ,						self.num_labels							) ,						labels.view(-1							)							)
					elif self.config.problem_type == "multi_label_classification":
						lowerCamelCase__              =	BCEWithLogitsLoss()
						lowerCamelCase__              =	loss_fct(_lowercase ,						_lowercase							)

				if not return_dict:
					lowerCamelCase__              =	(logits,) + outputs[2:]
					return ((loss,) + output) if loss is not None else output

				return ImageClassifierOutputWithNoAttention(
				    loss=_lowercase ,						logits=_lowercase ,						hidden_states=outputs.hidden_states ,						)










from queue import PriorityQueue
from typing import Any

import numpy as np





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,)       ->   float | int:







	'''simple docstring'''







	for nxt, d in graph[v]:
		if nxt in visited_forward:
			continue
		lowerCamelCase__              =	cst_fwd.get(__snake_case ,np.inf							)
		lowerCamelCase__              =	cst_fwd[v] + d
		if new_cost_f < old_cost_f:
			queue.put((new_cost_f, nxt)							)
			lowerCamelCase__              =	new_cost_f
			lowerCamelCase__              =	v
		if nxt in visited_backward:
			if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
				lowerCamelCase__              =	cst_fwd[v] + d + cst_bwd[nxt]
	return shortest_distance





def lowerCAmelCase__(__snake_case ,__snake_case ,__snake_case ,__snake_case							)       ->   int:







	'''simple docstring'''







	lowerCamelCase__              =	-1

	lowerCamelCase__              =	set()
	lowerCamelCase__              =	set()
	lowerCamelCase__              =	{source: 0}
	lowerCamelCase__              =	{destination: 0}
	lowerCamelCase__              =	{source: None}
	lowerCamelCase__              =	{destination: None}
	lowerCamelCase__              =	PriorityQueue()
	lowerCamelCase__              =	PriorityQueue()

	lowerCamelCase__              =	np.inf

	queue_forward.put((0, source)							)
	queue_backward.put((0, destination)							)

	if source == destination:
		return 0

	while not queue_forward.empty() and not queue_backward.empty():
		lowerCamelCase__       ,			lowerCamelCase__              =	queue_forward.get()
		visited_forward.add(__snake_case							)

		lowerCamelCase__       ,			lowerCamelCase__              =	queue_backward.get()
		visited_backward.add(__snake_case							)

		lowerCamelCase__              =	pass_and_relaxation(
		    __snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,)

		lowerCamelCase__              =	pass_and_relaxation(
		    __snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,__snake_case ,)

		if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
			break

	if shortest_distance != np.inf:
		lowerCamelCase__              =	shortest_distance
	return shortest_path_distance


_a						=   {
    "B": [["C", 1]],
    "C": [["D", 1]],
    "D": [["F", 1]],
    "E": [["B", 1], ["G", 2]],
    "F": [],
    "G": [["F", 1]],
}
_a						=   {
    "B": [["E", 1]],
    "C": [["B", 1]],
    "D": [["C", 1]],
    "F": [["D", 1], ["G", 1]],
    "E": [[None, np.inf]],
    "G": [["E", 2]],
}

if __name__ == "__main__":
			import doctest

			doctest.testmod()



