首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换tf.data.dataset

转换tf.data.dataset
EN

Stack Overflow用户
提问于 2020-03-17 02:51:13
回答 1查看 100关注 0票数 2

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

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

在序列化我得到的数据之后:

代码语言:javascript
复制
<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}>

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

代码语言:javascript
复制
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))

在原始dataset.So中使用get_next生成e1的情况下,print会打印出与序列化前的图像相同的图像。但是,我可以通过某种方式将序列化的数据集立即转换为原始的uint8数据集吗?

EN

回答 1

Stack Overflow用户

发布于 2020-05-09 23:16:06

您可以通过以下步骤在uint8中获取镜像。

创建序列化数据。

代码语言:javascript
复制
list_ds = tf.data.Dataset.list_files("img_dir_path/*")

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

代码语言:javascript
复制
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对象中的所有项。

代码语言:javascript
复制
processed_images = list_ds.map(process_img)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60711659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档