【问题标题】:Transforming a tf.data.dataset转换 tf.data.dataset
【发布时间】:2020-03-16 18:51:13
【问题描述】:

假设我有一个 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 生成的。因此,正如预期的那样,打印会打印与预序列化相同的图像。但是,我可以以某种方式转换我的序列化数据集,而不是逐个元素地执行此操作马上变成原来的uint8一个?

【问题讨论】:

  • “立即”是什么意思?将所有内容加载到内存中?这通常不是一个好主意。也许分批可以吗?
  • 能否提供可重现的代码来生成示例序列化数据集

标签: python tensorflow machine-learning


【解决方案1】:

您可以按照以下步骤获取 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)

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

【讨论】:

  • @user13072350 - 如果回答您的问题,请投票并接受答案。谢谢。
猜你喜欢
  • 2021-12-15
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2018-12-22
相关资源
最近更新 更多