


import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
			import torch

if is_tf_available():
			import tensorflow as tf

@require_vision
@require_torchvision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    Tuple							)      ->       Optional[Any]:
														__snake_case						:     Dict									=  tempfile.mkdtemp()
														__snake_case						:     Any									=  SamImageProcessor()
														__snake_case						:     Optional[int]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    Optional[Any]       ,      **lowerCamelCase			:    Optional[int]							)      ->       Optional[Any]:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    int							)      ->       List[Any]:
														__snake_case						:     List[str]									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     int									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							def        __snake_case   (				self			:    List[Any]							)      ->       Dict:
														__snake_case						:     int									=  SamProcessor(image_processor=self.get_image_processor()							)
														processor.save_pretrained(self.tmpdirname							)

														__snake_case						:     Dict									=  self.get_image_processor(do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														__snake_case						:     Optional[Any]									=  SamProcessor.from_pretrained(self.tmpdirname       ,      do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														self.assertEqual(processor.image_processor.to_json_string()       ,      image_processor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.image_processor       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    List[str]							)      ->       Tuple:
														__snake_case						:     int									=  self.get_image_processor()

														__snake_case						:     str									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Optional[int]									=  self.prepare_image_inputs()

														__snake_case						:     List[str]									=  image_processor(lowerCamelCase       ,      return_tensors="np"							)
														__snake_case						:     Dict									=  processor(images=lowerCamelCase       ,      return_tensors="np"							)

														input_feat_extract.pop("original_sizes"							)  # pop original_sizes as it is popped in the processor
														input_feat_extract.pop("reshaped_input_sizes"							)  # pop original_sizes as it is popped in the processor

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()       ,      input_processor[key].sum()       ,      delta=1E-2							)






							@require_torch
							def        __snake_case   (				self			:    Optional[Any]							)      ->       Dict:
														__snake_case						:     Tuple									=  self.get_image_processor()

														__snake_case						:     List[Any]									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     List[str]									=  [torch.ones((1, 3, 5, 5)							)]

														__snake_case						:     Tuple									=  [[1764, 2646]]

														__snake_case						:     Optional[int]									=  [[683, 1024]]
														__snake_case						:     int									=  processor.post_process_masks(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Optional[Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      torch.tensor(lowerCamelCase							)       ,      torch.tensor(lowerCamelCase							)							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														# should also work with np
														__snake_case						:     List[str]									=  [np.ones((1, 3, 5, 5)							)]
														__snake_case						:     Optional[int]									=  processor.post_process_masks(lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)							)

														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     str									=  [[1, 0], [0, 1]]
														with self.assertRaises(lowerCamelCase							):
																					__snake_case						:     Optional[int]									=  processor.post_process_masks(lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)							)

@require_vision
@require_tf
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    List[Any]							)      ->       Union[str, Any]:
														__snake_case						:     int									=  tempfile.mkdtemp()
														__snake_case						:     str									=  SamImageProcessor()
														__snake_case						:     List[Any]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    str       ,      **lowerCamelCase			:    Any							)      ->       Tuple:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[int]							)      ->       Any:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    str							)      ->       List[Any]:
														__snake_case						:     int									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     Dict									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							def        __snake_case   (				self			:    int							)      ->       List[str]:
														__snake_case						:     List[Any]									=  SamProcessor(image_processor=self.get_image_processor()							)
														processor.save_pretrained(self.tmpdirname							)

														__snake_case						:     Optional[Any]									=  self.get_image_processor(do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														__snake_case						:     Tuple									=  SamProcessor.from_pretrained(self.tmpdirname       ,      do_normalize=lowerCamelCase       ,      padding_value=1.0							)

														self.assertEqual(processor.image_processor.to_json_string()       ,      image_processor_add_kwargs.to_json_string()							)
														self.assertIsInstance(processor.image_processor       ,      lowerCamelCase							)

							def        __snake_case   (				self			:    Union[str, Any]							)      ->       List[Any]:
														__snake_case						:     str									=  self.get_image_processor()

														__snake_case						:     Union[str, Any]									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Dict									=  self.prepare_image_inputs()

														__snake_case						:     int									=  image_processor(lowerCamelCase       ,      return_tensors="np"							)
														__snake_case						:     List[str]									=  processor(images=lowerCamelCase       ,      return_tensors="np"							)

														input_feat_extract.pop("original_sizes"							)  # pop original_sizes as it is popped in the processor
														input_feat_extract.pop("reshaped_input_sizes"							)  # pop reshaped_input_sizes as it is popped in the processor

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()       ,      input_processor[key].sum()       ,      delta=1E-2							)






							@require_tf
							def        __snake_case   (				self			:    Any							)      ->       Optional[int]:
														__snake_case						:     List[str]									=  self.get_image_processor()

														__snake_case						:     Dict									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     Union[str, Any]									=  [tf.ones((1, 3, 5, 5)							)]

														__snake_case						:     List[Any]									=  [[1764, 2646]]

														__snake_case						:     Dict									=  [[683, 1024]]
														__snake_case						:     List[str]									=  processor.post_process_masks(lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="tf"							)
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Optional[Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      tf.convert_to_tensor(lowerCamelCase							)       ,      tf.convert_to_tensor(lowerCamelCase							)       ,      return_tensors="tf"       ,      )
														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														# should also work with np
														__snake_case						:     Union[str, Any]									=  [np.ones((1, 3, 5, 5)							)]
														__snake_case						:     List[str]									=  processor.post_process_masks(
														    lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)       ,      return_tensors="tf"							)

														self.assertEqual(masks[0].shape       ,      (1, 3, 1764, 2646)							)

														__snake_case						:     Tuple									=  [[1, 0], [0, 1]]
														with self.assertRaises(tf.errors.InvalidArgumentError							):
																					__snake_case						:     Dict									=  processor.post_process_masks(
																					    lowerCamelCase       ,      np.array(lowerCamelCase							)       ,      np.array(lowerCamelCase							)       ,      return_tensors="tf"							)

@require_vision
@require_torchvision
class 							a						(unittest.TestCase ):




							"""simple docstring"""


							def        __snake_case   (				self			:    List[str]							)      ->       str:
														__snake_case						:     Optional[int]									=  tempfile.mkdtemp()
														__snake_case						:     str									=  SamImageProcessor()
														__snake_case						:     List[Any]									=  SamProcessor(lowerCamelCase							)
														processor.save_pretrained(self.tmpdirname							)

							def        __snake_case   (				self			:    List[str]       ,      **lowerCamelCase			:    Any							)      ->       Dict:
														return AutoProcessor.from_pretrained(self.tmpdirname       ,      **lowerCamelCase							).image_processor

							def        __snake_case   (				self			:    Optional[int]							)      ->       List[Any]:
														shutil.rmtree(self.tmpdirname							)

							def        __snake_case   (				self			:    Optional[int]							)      ->       Optional[int]:
														__snake_case						:     str									=  [np.random.randint(255       ,      size=(3, 30, 400)       ,      dtype=np.uinta							)]

														__snake_case						:     List[Any]									=  [Image.fromarray(np.moveaxis(lowerCamelCase       ,      0       ,      -1							)							) for x in image_inputs]

														return image_inputs

							@is_pt_tf_cross_test
							def        __snake_case   (				self			:    Union[str, Any]							)      ->       List[str]:
														__snake_case						:     str									=  self.get_image_processor()

														__snake_case						:     str									=  SamProcessor(image_processor=lowerCamelCase							)
														__snake_case						:     List[Any]									=  np.random.randint(0       ,      2       ,      size=(1, 3, 5, 5)							).astype(np.floataa							)
														__snake_case						:     Dict									=  [tf.convert_to_tensor(lowerCamelCase							)]
														__snake_case						:     List[Any]									=  [torch.tensor(lowerCamelCase							)]

														__snake_case						:     Optional[Any]									=  [[1764, 2646]]

														__snake_case						:     Optional[int]									=  [[683, 1024]]
														__snake_case						:     Union[str, Any]									=  processor.post_process_masks(
														    lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="tf"							)
														__snake_case						:     Dict									=  processor.post_process_masks(
														    lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      return_tensors="pt"							)

														self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)






							@is_pt_tf_cross_test
							def        __snake_case   (				self			:    List[Any]							)      ->       List[str]:
														__snake_case						:     Any									=  self.get_image_processor()

														__snake_case						:     List[Any]									=  SamProcessor(image_processor=lowerCamelCase							)

														__snake_case						:     Dict									=  self.prepare_image_inputs()

														__snake_case						:     Any									=  image_processor(lowerCamelCase       ,      return_tensors="pt"							)["pixel_values"].numpy()
														__snake_case						:     Optional[Any]									=  processor(images=lowerCamelCase       ,      return_tensors="pt"							)["pixel_values"].numpy()

														__snake_case						:     Tuple									=  image_processor(lowerCamelCase       ,      return_tensors="tf"							)["pixel_values"].numpy()
														__snake_case						:     List[Any]									=  processor(images=lowerCamelCase       ,      return_tensors="tf"							)["pixel_values"].numpy()

														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)
														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)
														self.assertTrue(np.allclose(lowerCamelCase       ,      lowerCamelCase							)							)







'''simple docstring'''


import math
import random



def  _SCREAMING_SNAKE_CASE     (      UpperCamelCase__    :					float ,    UpperCamelCase__    :					bool = False				):




     """simple docstring"""


     if deriv:
          return value * (1 - value)
     return 1 / (1 + math.exp(-value				))


# Initial Value
lowerCAmelCase_      :  Optional[Any]				     =  0.02



def  _SCREAMING_SNAKE_CASE     (      UpperCamelCase__    :					int ,    UpperCamelCase__    :					int				):




     """simple docstring"""


     a_     :							Tuple     						=  float(2 * (random.randint(1 ,    100				)) - 1				)

     for _ in range(UpperCamelCase__				):
          # Forward propagation
          a_     :							Dict     						=  sigmoid_function(INITIAL_VALUE * weight				)
          # How much did we miss?
          a_     :							Dict     						=  (expected / 100) - layer_a
          # Error delta
          a_     :							Optional[Any]     						=  layer_1_error * sigmoid_function(UpperCamelCase__ ,    UpperCamelCase__				)
          # Update weight
          weight += INITIAL_VALUE * layer_1_delta

     return layer_a * 100


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 lowerCAmelCase_      :  str				     =  int(input('Expected value: '))
 lowerCAmelCase_      :  str				     =  int(input('Number of propagations: '))
 print(forward_propagation(expected, number_propagations))
