I am going to generate my train and test datasets from an image representing volume values. This image contains a range of -25 to 75. I want to ignore the negative values in preprocessing step. Could anyone tell me how I should treat negative values? Is there any way to transfer the negative values to zero or no-data without changing the positive pixel values?
I can't advise on if this should be done, but if you want to turn all your negative values to 0 you can use tf.maximum
:
import tensorflow as tf
x = tf.random.uniform((10, 10), -25, 75, dtype=tf.int32)
<tf.Tensor: shape=(10, 10), dtype=int32, numpy=
array([[ 57, -11, 48, 43, 29, 21, 15, 42, -9, 12],
[ 18, 67, -9, -21, 6, 27, 50, -1, 72, 51],
[ 2, 22, 70, 49, 50, -10, 67, 4, 59, -10],
[-13, 39, 60, -20, -15, -17, 51, 73, -23, 21],
[ 28, 8, 48, 66, -13, -3, 44, 35, 23, 45],
[-24, 30, 16, 25, 34, -13, 24, 49, 50, -10],
[-24, 25, -1, 35, 67, 45, 27, 6, 65, 4],
[ 20, -5, 41, -14, -10, 40, 21, 69, 13, 14],
[ 53, -2, 6, 0, -13, 28, 11, -11, 29, 17],
[ 15, 40, 61, 56, 3, 56, 12, -12, 19, 0]])>
Here's the magic:
tf.maximum(x, 0)
<tf.Tensor: shape=(10, 10), dtype=int32, numpy=
array([[57, 0, 48, 43, 29, 21, 15, 42, 0, 12],
[18, 67, 0, 0, 6, 27, 50, 0, 72, 51],
[ 2, 22, 70, 49, 50, 0, 67, 4, 59, 0],
[ 0, 39, 60, 0, 0, 0, 51, 73, 0, 21],
[28, 8, 48, 66, 0, 0, 44, 35, 23, 45],
[ 0, 30, 16, 25, 34, 0, 24, 49, 50, 0],
[ 0, 25, 0, 35, 67, 45, 27, 6, 65, 4],
[20, 0, 41, 0, 0, 40, 21, 69, 13, 14],
[53, 0, 6, 0, 0, 28, 11, 0, 29, 17],
[15, 40, 61, 56, 3, 56, 12, 0, 19, 0]])>
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.