
from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve




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

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

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

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

      # Set pixels at invalid locations to 0
      _lowercase									=				0

      return warped




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

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

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

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

            _lowercase									=				convolve(SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_					)
            _lowercase									=				convolve(SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_					)

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

            _lowercase									=				avg_horizontal_velocity - derivative_x * update
            _lowercase									=				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, is_vision_available


A		:   int         =    {'''configuration_vilt''': ['''VILT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViltConfig''']}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A		:   int         =    ['''ViltFeatureExtractor''']
		A		:   List[Any]         =    ['''ViltImageProcessor''']
		A		:   Dict         =    ['''ViltProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A		:   Tuple         =    [
		    '''VILT_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ViltForImageAndTextRetrieval''',
		    '''ViltForImagesAndTextClassification''',
		    '''ViltForTokenClassification''',
		    '''ViltForMaskedLM''',
		    '''ViltForQuestionAnswering''',
		    '''ViltLayer''',
		    '''ViltModel''',
		    '''ViltPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_vilt import VILT_PRETRAINED_CONFIG_ARCHIVE_MAP, ViltConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_vilt import ViltFeatureExtractor
				from .image_processing_vilt import ViltImageProcessor
				from .processing_vilt import ViltProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vilt import (
				    VILT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViltForImageAndTextRetrieval,
				    ViltForImagesAndTextClassification,
				    ViltForMaskedLM,
				    ViltForQuestionAnswering,
				    ViltForTokenClassification,
				    ViltLayer,
				    ViltModel,
				    ViltPreTrainedModel,
				)


else:
		import sys

		A		:   Dict         =    _LazyModule(__name__, globals()['''__file__'''], _import_structure)