I. A
I. A

Reputation: 2312

Subtract Image' mean from itself for a batch of images in Tensorflow

I would like to subtract the mean of the image from itself given a batch of images.

Obviously, the tf.image.per_image_standardization is not what I want since I don't want to divide by the standard deviation.

And, frames_normalized = tf.map_fn(lambda frame: tf.reduce_mean(frame, axis=[2]), frames_contrast_adjust) is not what I want since that will reduce the dimensions of the original image. That is, if the dimension of a single image is [112, 112, 3] the result of the tf.reduce_mean image will has a shape of [112, 112]. Therefore, the size of frame_normalized will become: [?, ?, 112, 3] when frames_contrast_adjust is a batch of images of size: [?, 112, 112, 3].

Please note that I do want to use queues in the mean.

Any help is much appreciated!!

Upvotes: 0

Views: 1118

Answers (2)

I. A
I. A

Reputation: 2312

The problem was solved by setting the keep_dims parameter in tf.reduce_mean to true. Now: tf.map_fn(lambda frame: tf.reduce_mean(frame, axis=[2], keep_dims=True), frames_contrast_adjust)

And to subtract the mean of image from the image itself, I did the following:

frames_normalized = tf.map_fn(lambda frame: frame - tf.reduce_mean(frame, axis=[2], keep_dims=True), input_frames)

Upvotes: 0

Salvador Dali
Salvador Dali

Reputation: 222561

If you want the op which behaves almost like per_image_standadization but without variance, you can just look how tf.image.per_image_standadization is implemented and remove all the stuff related to variance (I commented it away):

image = ops.convert_to_tensor(image, name='image')
image = control_flow_ops.with_dependencies(_Check3DImage(image, require_static=False), image)
num_pixels = math_ops.reduce_prod(array_ops.shape(image))

image = math_ops.cast(image, dtype=dtypes.float32)
image_mean = math_ops.reduce_mean(image)

#variance = (math_ops.reduce_mean(math_ops.square(image)) -math_ops.square(image_mean))
#variance = gen_nn_ops.relu(variance)
#stddev = math_ops.sqrt(variance)

# Apply a minimum normalization that protects us against uniform images.
#min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, dtypes.float32))
#pixel_value_scale = math_ops.maximum(stddev, min_stddev)
pixel_value_offset = image_mean

image = math_ops.subtract(image, pixel_value_offset)
#image = math_ops.div(image, pixel_value_scale)
return image

Upvotes: 1

Related Questions