python - 我应该如何在 tensorflow 中规范化图像?

标签 python tensorflow

目前我正在使用 tf.image.per_image_standardization(image) 但它似乎比使用以下方法需要更长的时间来收敛:

image = image - image_mean 

数据集的 image_mean = [meanR, meanG, meanB]。我做错了什么?

最佳答案

该函数执行不同的过程。您只需减去平均值,但 tf.image.per_image_standardization() 还会除以方差。来自API docs :

This op computes (x - mean) / adjusted_stddev, where mean is the average of all values in image, and adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements())).

这是来自 here 的完整实现:

def per_image_standardization(image):
  """Linearly scales `image` to have zero mean and unit norm.
  This op computes `(x - mean) / adjusted_stddev`, where `mean` is the average
  of all values in image, and
  `adjusted_stddev = max(stddev, 1.0/sqrt(image.NumElements()))`.
  `stddev` is the standard deviation of all values in `image`. It is capped
  away from zero to protect against division by 0 when handling uniform images.
  Args:
    image: 3-D tensor of shape `[height, width, channels]`.
  Returns:
    The standardized image with same shape as `image`.
  Raises:
    ValueError: if the shape of 'image' is incompatible with this function.
  """
  image = ops.convert_to_tensor(image, name='image')
  _Check3DImage(image, require_static=False)
  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

关于python - 我应该如何在 tensorflow 中规范化图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44571306/

相关文章:

python - Django / python : error when get value from dictionary

python - 洗牌如何与机器学习中的 ImageDataGenerator 一起工作?

python - 如何逐行分析cython功能

python - pydoop vs hadoopy - hadoop python 客户端

javascript - 如何在tensorflow js中找到张量的形状?

python - 加载卡住的 tf 模型 - 无占位符张量值

python - Django 加载 MySqlDB 模块时出错

python - 用神经网络预测圆的半径

python - TensorFlow:将 tf.Dataset 转换为 tf.Tensor

python - Keras:将模型对象作为参数传递给损失函数