tf.strings.split

本文介绍如何使用TensorFlow中的tf.strings.split方法将字符串按指定分隔符进行拆分。通过示例展示输入为list格式时,如何得到SparseTensorValue对象,并解析其结构,最后获取所需的拆分结果。

tf.strings.split

lines = tf.strings.split(line, '\n')

输入是list格式

输出是一个SparseTensorValue对象

SparseTensorValue(indices,

values=array( ,dtype), 

dense_shape)

拿结果

lines.values

 

import os import matplotlib.pylab as plt import tensorflow as tf import pathlib # 在URL上下载数据集 flowers_root = tf.keras.utils.get_file( 'flower_photos', 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz', untar=True) # untar=True表示自动解压下载的压缩包 flowers_root = pathlib.Path(flowers_root) print("看一下flowers_root的路径:", flowers_root) # 获取每个类下的文件数据 list_ds = tf.data.Dataset.list_files(str(flowers_root / '*/*')) def parse_image(filename): parts = tf.strings.split(filename, os.sep) # 分割数据,使用 tf.strings.split 函数将文件路径 filename 按照操作系统的路径分隔符 os.sep 进行分割 label = parts[-2] image = tf.io.read_file(filename) # 读取并输出输入文件名的全部内容 image = tf.image.decode_jpeg(image) # 编码解码处理 image = tf.image.convert_image_dtype(image, tf.float32) # 转换为float类型 image = tf.image.resize(image, [128, 128]) # 尺寸调整为128*128 return image, label file_path = next(iter(list_ds)) # 找到文件路径 image, label = parse_image(file_path) print('image的内容', image) print('***********************************************') print('label的内容', label) # 自定义函数绘制图像图形 plt.figure() plt.imshow(image) plt.title(label.numpy().decode('utf-8')) plt.axis('off') plt.show() images_ds = list_ds.map(parse_image) for image, label in images_ds.take(1): plt.figure() plt.imshow(image) plt.title(label.numpy().decode('utf-8')) plt.axis('off') plt.show() 检查错误
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值