【发布时间】:2019-07-26 16:43:47
【问题描述】:
我正在使用tensorflow.Data API 处理一个 csv 文件。 csv 中的一个特征是图像名称。为了加载图像,我需要构建一个将基本文件夹与图像名称相结合的路径。但是,由于图像名称是张量并且基本文件夹是字符串,因此我无法使用 os.path.join 将它们连接起来。我包括下面的代码。
def process_csv_data(folder_path, image_dimensions):
width, height, channels = image_dimensions
def map_function(raw_data):
image_path = os.path.join(folder_path,raw_data['image_name'].numpy().decode('utf-8'))
image = tf.io.read_file(image_path)
image = tf.image.decode_jpeg(image, channels=channels)
image = tf.image.resize(image, [width, height])
image /= 255.0 # normalize to [0,1] range
return image
return map_function
前一个函数使用如下:
raw_csv_dataset = tf.data.experimental.make_csv_dataset(
csv_path,
batch_size=1,
column_names=CSV_COLUMNS,
shuffle=False)
dataset = raw_csv_dataset.map(
process_csv_data(folder_path, image_dimensions, mode),
num_parallel_calls=tf.data.experimental.AUTOTUNE)
上面的代码产生了这个错误:
AttributeError: 'Tensor' 对象没有属性 'numpy'
我尝试了几种方法都没有成功,比如将文件夹名称转换为张量并使用tf.strings.join,或者将tf.string 转换为标准python string。那么,什么是正确的方法?
我正在使用 tensorflow 2.0
【问题讨论】:
标签: python tensorflow tensorflow2.0