votes up 0

`height_factor` cannot have upper bound less than lower bound, got (param1)

Package:
keras
github stars 51164
Exception Class:
ValueError

Raise code

 isinstance(height_factor, (tuple, list)):
      self.height_lower = height_factor[0]
      self.height_upper = height_factor[1]
    else:
      self.height_lower = -height_factor
      self.height_upper = height_factor
    if self.height_upper < self.height_lower:
      raise ValueError('`height_factor` cannot have upper bound less than '
                       'lower bound, got {}'.format(height_factor))
    if abs(self.height_lower) > 1. or abs(self.height_upper) > 1.:
      raise ValueError('`height_factor` must have values between [-1, 1], '
                       'got {}'.format(height_factor))

    self.width_factor = width_factor
    if isinstance(width_factor, (tuple, list)):
      

Ways to fix

votes up 2 votes down

The RandomTranslation layer is used to randomly translate each image during training.

usage:

layer = tf.keras.layers.experimental.preprocessing.RandomTranslation(
    height_factor, width_factor)

This initializes the layer object.

The arguments used are describes as follows:

  • height_factor: a float represented as fraction of value, or a tuple of size 2 representing lower and upper bound for shifting vertically. If a single number is given the lower and upper bounds are calculated as:
upper_bound = height_factor
lower_bound = -height_factor

However if a tuple or a list of size 2 is given, then the upper and lower bounds are defined as follows.

upper_bound = height_factor[1]
lower_bound = height_factor[0]
  • width_factor: a float represented as fraction of value, or a tuple of size 2 representing lower and upper bound for shifting horizontally. The upper and lower bounds are defined the same way as in the height_factor.

After initializing the layer the given image or number of images are modified by calling the layer object with the image.

original_image = np.random.randn(3,16,16,3)

modified_image = layer (original_image)

The error is raised when height factor is given as tuple or list and the the values of lower and upper bounds are reversed.

Reproducing the error:

  • Installation and environment setup
$ pip install --user pipenv 

$ mkdir test_folder & cd test_folder

  • Activate the virtual environment

$ pipenv shell

  • Install tensorflow

$ pipenv install tensorflow

  • Run the sample code

import tensorflow as tf
height_factor = (0.5,0.1) # notice, the cause of the error is here
width_factor =(0.3,0.4)

#initialize the layer
layer = tf.keras.layers.experimental.preprocessing.RandomTranslation(height_factor, width_factor)
#generate random numpy array for the input image,this should be 4D i.e. (samples, height, width, channels)
original_image = np.random.randn(3,16,16,3)
#modify the image using the layer
modified_image = layer (original_image)
print(image)

Fix: Make sure the elements of the tuple or list given to the height_factor argument are appropriately ordered. I.e. lower to upper.

Fixed version of the code:

import tensorflow as tf
height_factor = (0.1,0.5)
width_factor =(0.3,0.4)

#initialize the layer
layer = tf.keras.layers.experimental.preprocessing.RandomTranslation(height_factor, width_factor)
#generate random numpy array for the input image,this should be 4D i.e. (samples, height, width, channels)
original_image = np.random.randn(3,16,16,3)
#modify the image using the layer
modified_image = layer (original_image)
print(image)
Jun 11, 2021 kellemnegasi answer
kellemnegasi 2.7k

Add a possible fix

Please authorize to post fix