【问题标题】:How to handle negative values before CNNCNN之前如何处理负值
【发布时间】:2021-02-23 17:26:28
【问题描述】:

我将从代表体积值的图像生成我的训练和测试数据集。此图像包含 -25 到 75 的范围。我想忽略预处理步骤中的负值。谁能告诉我应该如何对待负值?有什么方法可以在不改变正像素值的情况下将负值转换为零或无数据?

【问题讨论】:

    标签: python tensorflow keras conv-neural-network image-preprocessing


    【解决方案1】:

    我不建议如果应该这样做,但是如果你想将所有负值变为 0,你可以使用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]])>
    

    这就是魔法:

    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]])>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2020-06-27
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多