votes up 8

`width_factor` must have values between [-1, 1], got (param1)

Package:
Exception Class:
ValueError

Raise code

se:
      self.width_lower = -width_factor
      self.width_upper = width_factor
    if self.width_upper < self.width_lower:
      raise ValueError('`width_factor` cannot have upper bound less than '
                       'lower bound, got {}'.format(width_factor))
    if abs(self.width_lower) > 1. or abs(self.width_upper) > 1.:
      raise ValueError('`width_factor` must have values between [-1, 1], '
                       'got {}'.format(width_factor))

    check_fill_mode_and_interpolation(fill_mode, interpolation)

    self.fill_mode = fill_mode
    self.fill_value = fill_value
    self.interpolation = interpolation
    se
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 2 votes down

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

Usage:

 
layer_translator = RandomTranslation(height_factor, 
                          width_factor, 
                          fill_mode='reflect',
                          interpolation='bilinear', 
                          seed=None, 
                          fill_value=0.0,)

output_image = layer_translator(input_image)

The parameters

  • height_factor: a float represented as fraction of value, or a tuple of size 2 representing lower and upper bound for shifting vertically. A negative value means shifting image up, while a positive value means shifting image down
  • width_factor: a float represented as fraction of value, or a tuple of size 2 representing lower and upper bound for shifting horizontally. A negative value means shifting image left, while a positive value means shifting image right

If a lower bound less than -1 or an upper bound greater than 1 is given to width_factor parameter this error is raised.

from tensorflow.keras.layers.experimental.preprocessing import RandomTranslation
import tensorflow as tf
original_image = tf.random.uniform((10,56,56,3))
layer = RandomTranslation(height_factor=[-1,0.2], width_factor=[-1.5,1])
output_image = layer(original_image)
print(output_image.shape)

The raised error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-38-6ff5e27581be> in <module>()
      2 import tensorflow as tf
      3 original_image = tf.random.uniform((10,56,56,3))
----> 4 layer = RandomTranslation(height_factor=[-1,0.2], width_factor=[-1.5,1])
      5 output_image = layer(original_image)
      6 print(output_image.shape)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/preprocessing/image_preprocessing.py in __init__(self, height_factor, width_factor, fill_mode, interpolation, seed, fill_value, **kwargs)
    503     if abs(self.width_lower) > 1. or abs(self.width_upper) > 1.:
    504       raise ValueError('`width_factor` must have values between [-1, 1], '
--> 505                        'got {}'.format(width_factor))
    506 
    507     check_fill_mode_and_interpolation(fill_mode, interpolation)

ValueError: `width_factor` must have values between [-1, 1], got [-1.5, 1]

Fix:

The value of the width_factor should be with in the range [-1, 1].

from tensorflow.keras.layers.experimental.preprocessing import RandomTranslation
import tensorflow as tf
original_image = tf.random.uniform((10,56,56,3))
layer = RandomTranslation(height_factor=[-1,0.2], width_factor=[-0.5,1])
output_image = layer(original_image)
print(output_image.shape)

Output:

(10, 56, 56, 3)

Jul 14, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix