首页 > 解决方案 > 我如何使用 tensorflow-transform 来处理图像,例如定义一个操作来降低图像的平均值

问题描述

如何使用 tensorflow-transform 来降低图像的平均值并使其在服务期间也能正常工作

标签: pythontensorflow-transform

解决方案


如果我正确理解你的问题,你想减少图像的平均值,即你想将图像从颜色转换为灰度(将像素的值除以 255,因此它们的平均值将减少)

我们可以将其定义为 Tensorflow 操作,tf.divide(outputs[key], 255)preprocessing_fnTensorflow Transform,以便这些更改可以在训练期间和服务中应用。你可以试试 Tensorflow 2.0 Alpha,这样就不需要创建会话了。

def preprocessing_fn(inputs):
  """Preprocess input columns into transformed columns."""
  # Since we are modifying some features and leaving others unchanged, we
  # start by setting `outputs` to a copy of `inputs.
  outputs = inputs.copy()

  # Convert the Image from Color to Grey Scale. 
  # NUMERIC_FEATURE_KEYS is the names of Columns of Values of Pixels
  for key in NUMERIC_FEATURE_KEYS:
    outputs[key] = tf.divide(outputs[key], 255)

  outputs[LABEL_KEY] = outputs[LABEL_KEY]

  return outputs

让我知道这是否回答了您的问题。


推荐阅读