tf.image.random_brightness在TensorFlow中随机给出负值

时间:2018-02-16 06:27:40

标签: python tensorflow

'我正在尝试使用各种方法(如旋转,随机亮度,随机饱和度)在TensorFlow中进行图像数据增强。我观察到tf.image.random_brightness的输出不一致 - 有时会产生负值。我理解随机性,但产生负值是否正确?当我尝试使用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上使用的是带有Python 3.5.3的Jupyter笔记本和TensorFlow CPU版本1.5.0-rc0。'

1 个答案:

答案 0 :(得分:5)

您允许强度(delta)随机变化,介于-0.8和0.8之间:

Schema::create('clients', function (Blueprint $table) {
        $table->increments('id');
        $table->string("client_ref");
        $table->string('client_name');
        $table->enum('is_active', ['Y', 'N'])->default('Y');
        $table->timestamps();
        $table->softDeletes();
    });

请注意,图像的强度在[0-1]范围内,因为你做了:

Schema::create('clients_addresses', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('client_id')->comments('id from the clients table');
    $table->enum('address_type', ['Billing', 'Shipping'])->default('Billing');
    $table->enum('is_active', ['Y', 'N'])->default('Y');
    $table->string('address_line1');
    $table->string('address_line2')->nullable();
    $table->string('address_line3')->nullable();
    $table->string('city');
    $table->string('country')->default('UK');
    $table->string('postcode');
    $table->timestamps();
    $table->index(['address_type', 'is_active']);
 });

这意味着图像中的每个强度值 i 将更改为:

Schema::table('clients_addresses', function (Blueprint $table) {
    //
    if (Schema::hasColumn('clients_addresses', 'client_id')) {
        $table->foreign('client_id')->references('id')->on('clients');
    }
});

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

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

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

tf.image.random_brightness(params[0], 0.8, 1)