Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接string和tf.string以获取路径_Python_Tensorflow_Tensorflow2.0 - Fatal编程技术网

Python 连接string和tf.string以获取路径

Python 连接string和tf.string以获取路径,python,tensorflow,tensorflow2.0,Python,Tensorflow,Tensorflow2.0,我正在使用tensorflow.DataAPI处理csv文件。csv中的一个功能是图像名称。为了加载图像,我需要构建一个将基本文件夹与图像名称相结合的路径。但是,由于图像名称是张量,基本文件夹是字符串,因此我无法使用os.path.join将它们连接起来。我包括下面的代码 def process_csv_data(folder_path, image_dimensions): width, height, channels = image_dimensions def map_f

我正在使用
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

只需将字符串与
+
连接起来:

image_path = folder_path + os.sep + raw_data['image_name']

如果确实需要路径分隔符(如果它未包含在
文件夹\u path
),并且不希望显式使用
/
\

,从而导致另一个错误,请使用该分隔符:
ValueError:Shape必须是秩0,但对于输入形状为[1]的“ReadFile”(op:'ReadFile'),它是秩1
第行
image=tf.io.read\u文件(image\u路径)
@magomar这是另一个不相关的错误
raw_data['image_name']
应该是一个“标量”字符串(不是字符串数组),否则不能传递到
read_file
。这取决于数据本身(映射函数在原始数据中接收的内容)。如果它是一个带有单个字符串的数组(例如,
['my_file.csv']
),您可以使用
image=tf.io.read_file(tf.squese(image_path))
对其进行修复。原始数据是一个有序字典,其中“image_name”是一个功能(csv中的一列)。无论如何,我用
tf.reformate(image\u path,[])
修复了这个错误,在本例中它相当于tf.squence。到目前为止,问题已经解决了,谢谢。