Assume we have images Tensor A
with shape (None, 200, 200, 1)
. where None
is the batch size, and (200, 200, 1)
is the image size.
How to perform normalization (0 to 1) on each image (not using for
iteration)?
That is:
A[0] = (A[0] - A[0].min()) / (A[0].max() - A[0].min())
A[1] = (A[1] - A[1].min()) / (A[1].max() - A[1].min())
...
A[n] = (A[n] - A[n].min()) / (A[n].max() - A[n].min())
if I just directly use A = (A - A.min()) / (A.max() - A.min())
, it will normalize all the images by the global max
and min
. I wish to normalize each image with their own max
and min
.
In other words, how to achieve a max
or min
operation, that results in a shape of: (None, 1, 1, 1)
, where each (1, 1, 1)
contains the max or min of each image.
You can use tf.reduce_max
and tf.reduce_min
.
import tensorflow as tf
A = tf.random_normal(shape=(-1, 200, 200, 1))
B = tf.reduce_max(A, axis=(1, 2, 3))
C = tf.reduce_min(A, axis=(1, 2, 3))
print(B.shape)
print(C.shape)
Output:
(?,)
(?,)
Also, the output in your case needs to be of the shape (None, 1, 1, 1)
, not (None, 1, 1)
, as you have included the last channel dimension.
B = tf.reshape(B, (-1, 1, 1, 1))
C = tf.reshape(C, (-1, 1, 1, 1))
print(B.shape)
print(C.shape)
The above code gives the following output:
(?, 1, 1, 1)
(?, 1, 1, 1)
Finally, why are you subtracting the max
from each pixel, do you mean to subtract the min
for normalizing each image between 0-1 ?
D = (A - C) / (B - C)
print(D.shape)
gives
(?, 200, 200, 1)
as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.