Tensorflow 需要类似字节的对象,而不是';张量';在急切模式下调用字符串张量上的映射时

Tensorflow 需要类似字节的对象,而不是';张量';在急切模式下调用字符串张量上的映射时,tensorflow,tensorflow-datasets,Tensorflow,Tensorflow Datasets,我正试图使用TF.dataset.map来移植这段旧代码,因为我收到了一个弃用警告 从TFRecord文件中读取一组自定义协议的旧代码: record\u iterator=tf.python\u io.tf\u record\u iterator(path=filename) 对于记录迭代器中的记录: 示例=MyProto() 示例.ParseFromString(记录) 我试图使用渴望模式和地图,但我得到了这个错误 def parse_proto(字符串): proto_object=My

我正试图使用TF.dataset.map来移植这段旧代码,因为我收到了一个弃用警告

从TFRecord文件中读取一组自定义协议的旧代码:

record\u iterator=tf.python\u io.tf\u record\u iterator(path=filename)
对于记录迭代器中的记录:
示例=MyProto()
示例.ParseFromString(记录)
我试图使用渴望模式和地图,但我得到了这个错误

def parse_proto(字符串):
proto_object=MyProto()
proto_object.ParseFromString(字符串)
dataset=tf.data.TFRecordDataset(数据集\u路径)
parsed_protos=raw_tf_dataset.map(parse_proto)
此代码适用于:

对于原始数据集中的原始记录:
proto_object=MyProto()
proto_object.ParseFromString(原始记录.numpy())
但地图给了我一个错误:

TypeError: a bytes-like object is required, not 'Tensor'


使用参数映射函数结果并将其视为字符串的正确方法是什么?

您需要从张量中提取字符串并在
映射
函数中使用。下面是代码中要实现的步骤

  • 您必须使用
    tf.py\u函数(get\u path[x],[tf.float32])
    来装饰map函数。您可以找到更多关于tf.py_函数的信息。在
    tf.py_函数
    中,第一个参数是
    map
    函数的名称,第二个参数是要传递给
    map
    函数的元素,最后一个参数是返回类型
  • 您可以使用map函数中的
    bytes.decode(file\u path.numpy())
    获取字符串部分
  • 因此,请按如下所示修改您的程序

    parsed_protos = raw_tf_dataset.map(parse_proto)
    
    def parse_proto(string):
          proto_object = MyProto()
          proto_object.ParseFromString(string)
    

    同时修改
    parse_proto
    ,如下所示:

    parsed_protos = raw_tf_dataset.map(parse_proto)
    
    def parse_proto(string):
          proto_object = MyProto()
          proto_object.ParseFromString(string)
    

    在下面的简单程序中,我们使用
    tf.data.Dataset.list\u文件来读取图像的路径。接下来在
    map
    功能中,我们使用
    load\u img
    读取图像,然后执行
    tf.image.central\u crop
    功能来裁剪图像的中心部分

    代码-

    %tensorflow_version 2.x
    import tensorflow as tf
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array, array_to_img
    from matplotlib import pyplot as plt
    import numpy as np
    
    def load_file_and_process(path):
        image = load_img(bytes.decode(path.numpy()), target_size=(224, 224))
        image = img_to_array(image)
        image = tf.image.central_crop(image, np.random.uniform(0.50, 1.00))
        return image
    
    train_dataset = tf.data.Dataset.list_files('/content/bird.jpg')
    train_dataset = train_dataset.map(lambda x: tf.py_function(load_file_and_process, [x], [tf.float32]))
    
    for f in train_dataset:
      for l in f:
        image = np.array(array_to_img(l))
        plt.imshow(image)
    
    输出-

    %tensorflow_version 2.x
    import tensorflow as tf
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array, array_to_img
    from matplotlib import pyplot as plt
    import numpy as np
    
    def load_file_and_process(path):
        image = load_img(bytes.decode(path.numpy()), target_size=(224, 224))
        image = img_to_array(image)
        image = tf.image.central_crop(image, np.random.uniform(0.50, 1.00))
        return image
    
    train_dataset = tf.data.Dataset.list_files('/content/bird.jpg')
    train_dataset = train_dataset.map(lambda x: tf.py_function(load_file_and_process, [x], [tf.float32]))
    
    for f in train_dataset:
      for l in f:
        image = np.array(array_to_img(l))
        plt.imshow(image)
    


    希望这能回答你的问题。快乐学习。

    @Kartik Ayyar-希望我们已经回答了您的问题。如果你对答案感到满意,请接受并投票。