我正在使用tensorflow-transform处理图像预处理管道。在管道的先前步骤中,我将图像文件(带有 tf.io.gfile.GFile )下载为字节数组。我现在想用 tf.image 操作处理字节数组。
import tensorflow as tf
import tensorflow_transform as tfx
...
def preprocessing_fn(inputs: dict) -> dict:
# inputs
size = 448
channels = 3
x = inputs['image']
y = inputs['label']
print('x: {}'.format(x))
print('y: {}'.format(y))
# create labels in model assets
tfx.vocabulary(y, vocab_filename='labels')
# load and normalize image
# x is a byte array (as string)
img_full = tf.image.decode_image(x, channels=channels)
img_norm = tf.image.convert_image_dtype(img_full, tf.float32)
img_resize = tf.image.resize(img_norm, [size, size])
x = img_resize
y = tf.cast(y.decode('utf-8'), dtype=tf.string)
# output
outputs = {'x': x,
'y': y}
return outputs
...
transformed_dataset, transform_fn = (
(downloaded_dataset, INPUT_SCHEMA)
| '{}_AnalyseAndTransform'.format(step) >> tfx_beam.AnalyzeAndTransformDataset(preprocessing_fn)
)
但是,我无法弄清楚形状不匹配,如何解决。
ValueError: Shape must be rank 0 but is rank 1 for 'DecodeJpeg' (op: 'DecodeJpeg') with input shapes: [?].
x: Tensor("inputs/inputs/image_copy:0", shape=(None,), dtype=string)
y: Tensor("inputs/inputs/label_copy:0", shape=(None,), dtype=string)
tf.reshape(), as_bytes(), x [0] 都无法转换 x 到用于图像操作的有效字符串。 使用 beam.Map()在apache-beam上运行它不会引起任何问题,因为字节数组是一个简单的字符串,而不是没有形状的批处理张量。 我缺少任何转换,还是我的架构不正确?
INPUT_SCHEMA = dataset_metadata.DatasetMetadata(schema_utils.schema_from_feature_spec({
'image': tf.io.FixedLenFeature(shape=[], dtype=tf.string),
'label': tf.io.FixedLenFeature(shape=[], dtype=tf.string),
}))
我没有在 AnalyzeAndTransformDataset 中找到任何 tf.image 操作的示例。我不确定它是否可以那样工作。我对每一个想法都很感谢,因为这是我第一次在 tfx 上进行图像预处理。谢谢:)