繁体   English   中英

转换 tf.data.dataset

[英]Transforming a tf.data.dataset

假设我有一个 32*32*3 类型图像的数据集作为源数据:

<DatasetV1Adapter shapes: {coarse_label: (), image: (32, 32, 3), label: ()}, types: {coarse_label: tf.int64, image: tf.uint8, label: tf.int64}>

序列化数据后,我得到:

<MapDataset shapes: {depth: (), height: (), image_raw: (), label: (), width: ()}, types: {depth: tf.int64, height: tf.int64, image_raw: tf.string, label: tf.int64, width: tf.int64}>

我可以使用这段代码访问每个元素:

for i in parsed_image_dataset.take(1):
  j=i['image_raw']
array_shape = e1['image'].numpy().shape
print(np.frombuffer(j.numpy(), dtype = 'uint8').reshape(array_shape))

其中e1是在原始数据get_next使用get_next生成的。 get_next ,正如预期的那样,打印打印出与预序列化相同的图像。但是,我可以以某种方式将序列化数据集立即转换为原始uint8数据集,而不是逐个元素执行此操作吗?

您可以按照以下步骤在 uint8 中获取图像。

创建序列化数据。

list_ds = tf.data.Dataset.list_files("img_dir_path/*")

创建一个函数,它将 file_path 作为参数并以 uint8 格式返回图像。

def process_img(file_path):
  img = tf.io.read_file(file_path)

  img = tf.image.decode_jpeg(img, channels=3)
  return img

使用 map 函数将上述函数应用于 list_ds 对象中的所有项。

processed_images = list_ds.map(process_img)

processing_images将包含给定图像目录的 uint8 格式的图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM