





from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve

def      a    (   _lowercase :  np.ndarray			,    _lowercase :  np.ndarray			,    _lowercase :  np.ndarray							)  ->np.ndarray:
		UpperCamelCase__								=						np.stack((horizontal_flow, vertical_flow)			,    2							)

		# Create a grid of all pixel coordinates and subtract the flow to get the
		# target pixels coordinates
		UpperCamelCase__								=						np.stack(
		    np.meshgrid(np.arange(0			,    image.shape[1]							)			,    np.arange(0			,    image.shape[0]							)							)			,    2							)
		UpperCamelCase__								=						np.round(grid - flow							).astype(np.intaa							)

		# Find the locations outside of the original image
		UpperCamelCase__								=						(grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]							))
		UpperCamelCase__								=						0

		UpperCamelCase__								=						image[grid[:, :, 1], grid[:, :, 0]]

		# Set pixels at invalid locations to 0
		UpperCamelCase__								=						0

		return warped

def      a    (   _lowercase :  np.ndarray			,    _lowercase :  np.ndarray			,    _lowercase :  SupportsIndex			,    _lowercase :  float | None = None			,    )  ->tuple[np.ndarray, np.ndarray]:
		if alpha is None:
				UpperCamelCase__								=						0.1

		# Initialize flow
		UpperCamelCase__								=						np.zeros_like(_lowercase							)
		UpperCamelCase__								=						np.zeros_like(_lowercase							)

		# Prepare kernels for the calculation of the derivatives and the average velocity
		UpperCamelCase__								=						np.array([[-1, 1], [-1, 1]]							) * 0.25
		UpperCamelCase__								=						np.array([[-1, -1], [1, 1]]							) * 0.25
		UpperCamelCase__								=						np.array([[1, 1], [1, 1]]							) * 0.25
		UpperCamelCase__								=						np.array(
		    [[1 / 1_2, 1 / 6, 1 / 1_2], [1 / 6, 0, 1 / 6], [1 / 1_2, 1 / 6, 1 / 1_2]]							)

		# Iteratively refine the flow
		for _ in range(_lowercase							):
				UpperCamelCase__								=						warp(_lowercase			,    _lowercase			,    _lowercase							)
				UpperCamelCase__								=						convolve(_lowercase			,    _lowercase							) + convolve(_lowercase			,    _lowercase							)
				UpperCamelCase__								=						convolve(_lowercase			,    _lowercase							) + convolve(_lowercase			,    _lowercase							)
				UpperCamelCase__								=						convolve(_lowercase			,    _lowercase							) + convolve(_lowercase			,    -kernel_t							)

				UpperCamelCase__								=						convolve(_lowercase			,    _lowercase							)
				UpperCamelCase__								=						convolve(_lowercase			,    _lowercase							)

				# This updates the flow as proposed in the paper (Step 12)
				UpperCamelCase__								=						(
				    derivative_x * avg_horizontal_velocity
				    + derivative_y * avg_vertical_velocity
				    + derivative_t
				)
				UpperCamelCase__								=						update / (alpha**2 + derivative_x**2 + derivative_y**2)

				UpperCamelCase__								=						avg_horizontal_velocity - derivative_x * update
				UpperCamelCase__								=						avg_vertical_velocity - derivative_y * update

		return horizontal_flow, vertical_flow


if __name__ == "__main__":
				import doctest

				doctest.testmod()












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCAmelCase 	=      {"configuration_visual_bert": ["VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "VisualBertConfig"]}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__lowerCAmelCase 	=      [
				    "VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "VisualBertForMultipleChoice",
				    "VisualBertForPreTraining",
				    "VisualBertForQuestionAnswering",
				    "VisualBertForRegionToPhraseAlignment",
				    "VisualBertForVisualReasoning",
				    "VisualBertLayer",
				    "VisualBertModel",
				    "VisualBertPreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_visual_bert import (
								    VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
								    VisualBertForMultipleChoice,
								    VisualBertForPreTraining,
								    VisualBertForQuestionAnswering,
								    VisualBertForRegionToPhraseAlignment,
								    VisualBertForVisualReasoning,
								    VisualBertLayer,
								    VisualBertModel,
								    VisualBertPreTrainedModel,
								)


else:
				import sys

				__lowerCAmelCase 	=      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





