Python tf.image.random_亮度在TensorFlow中随机给出负值

Python tf.image.random_亮度在TensorFlow中随机给出负值,python,tensorflow,Python,Tensorflow,我正在尝试使用旋转、随机亮度、随机饱和等多种方法在TensorFlow中增强图像数据。我观察到tf.image.random_亮度的输出不一致-有时会产生负值。我理解随机性,但产生负值是否正确?当我尝试使用matplotlib.pyplot打印图像时,它无法显示ValueError:浮点图像RGB值必须在0..1范围内 下面是一些代码示例:' # Function which reads file and converts to image array def read_images_from_

我正在尝试使用旋转、随机亮度、随机饱和等多种方法在TensorFlow中增强图像数据。我观察到tf.image.random_亮度的输出不一致-有时会产生负值。我理解随机性,但产生负值是否正确?当我尝试使用matplotlib.pyplot打印图像时,它无法显示ValueError:浮点图像RGB值必须在0..1范围内 下面是一些代码示例:'

# Function which reads file and converts to image array
def read_images_from_file (input_queue):
    label = input_queue[1]

    file_content = tf.read_file(input_queue[0])
    image = tf.image.decode_jpeg(file_content, channels=NUM_CHANNELS)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32, saturate=True)
    image = tf.image.resize_images(image, [IMAGE_HEIGHT, IMAGE_WIDTH])

.....
    #inside a function which applies various augmentations - code shown only for brightness

    X_init = tf.placeholder(tf.float32, shape=images.shape)
    X = tf.Variable(X_init)

    sess.run(tf.variables_initializer([X]), feed_dict={X_init: images})
    aug_images, aug_labels = (sess.run(tf.map_fn(lambda params: (tf.image.random_brightness(params[0], 0.8, 1), params[1]), (X, labels))))

    #inside a loop after calling above function - output of function is returned to aug_train_images

    print (aug_train_images[i])


'Some sample output:'
    [[[-0.18852733 -0.27872342 -0.31009597]
      [-0.18059228 -0.2786315  -0.3060825 ]
      [-0.1765788  -0.27461803 -0.302069  ]
      ...

      [-0.20366213 -0.19974056 -0.18405429]
      [-0.22792684 -0.22437292 -0.20458125]
      [-0.24324547 -0.23166458 -0.21205674]]

“我正在Ubuntu 16.10上使用Jupyter笔记本电脑和Python 3.5.3和TensorFlow CPU版本1.5.0-rc0。”

您允许强度(增量)在-0.8和0.8之间随机变化:

tf.image.random_brightness(params[0], 0.8, 1)
请注意,图像的强度在[0-1]范围内,因为您执行了以下操作:

image = tf.image.convert_image_dtype(image, dtype=tf.float32, saturate=True)
这意味着图像中的每个强度值i将更改为介于以下之间的值:

[i-0.8, i+0.8]
超出图像的[0-1]范围。换句话说,将有负值和大于1的值

第一条评论是0.8的增量似乎太大(当然这取决于问题)。我建议大约0.1(即允许10%的变化)

第二件事是,您必须确保在更改亮度后,图像仍然是图像,也就是说,将强度剪辑到[0-1]范围内。您可以通过以下方式实现:

image = tf.clip_by_value(image, 0.0, 1.0)

keras已经内置了图像数据增强功能。为什么不用呢?谢谢米奇的建议。现在我正在学习TensorFlow和deep learning,因此试图将我的代码限制在TFAPI上。我觉得使用高级API会更容易,效果也会更好,但学习(即我学习深度学习!)会更少。此外,我在Keras的数据增强库中找不到改变亮度/饱和度的方法。但改变亮度/饱和度是否会给convNet带来更多的信息?我想不是。