【问题标题】:TypeError: Value passed to parameter 'input' has DataType bool not in list of allowed values: float32, float64, int32, uint8, int16, int8TypeError:传递给参数“输入”的值的 DataType bool 不在允许值列表中:float32、float64、int32、uint8、int16、int8
【发布时间】:2020-09-29 10:46:23
【问题描述】:

我有一个包含 5 个标签的数据集

def get_label(file_path):
  # convert the path to a list of path components
  parts = tf.strings.split(file_path, os.path.sep)
  class_names = ['daisy' 'dandelion' 'roses' 'sunflowers' 'tulips']
  # The second to last is the class-directory
  one_hot = parts[-2] == class_names
  # Integer encode the label
  return tf.argmax(one_hot)

def decode_img(img):
  # convert the compressed string to a 3D uint8 tensor
  img = tf.image.decode_jpeg(img, channels=3)
  # resize the image to the desired size
  return tf.image.resize(img, [img_height, img_width])

def process_path(file_path):
  label = get_label(file_path)
  # load the raw data from the file as a string
  img = tf.io.read_file(file_path)
  img = decode_img(img)
  return img, label

train_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE)

如果我用其他具有 2 个标签的数据集更改此代码,class_names = ['dog', 'cat'] 我发现此错误 TypeError: Value passed to parameter 'input' has DataType bool not in list of allowed values: float32, float64, int32, uint8, int16, int8, complex64, int64, qint8, quint8, qint32, bfloat16, uint16, complex128, float16, uint32, uint64 那么我该如何更新 def get_label(file_path)

【问题讨论】:

    标签: python tensorflow2.x


    【解决方案1】:

    我遇到了同样的问题。按照上一篇的思路:

    one_hot = tf.dtypes.cast(parts[-2] == class_names, dtype = tf.int16)
    

    【讨论】:

      【解决方案2】:

      我的猜测是 tf.argmax 需要其中一种数据类型(我现在无法测试)

      float32, float64, int32, uint8, int16, int8, complex64, int64, qint8, quint8, qint32, bfloat16, uint16, complex128, float16, uint32, uint64
      

      所以你需要做的就是转换输出

      one_hot = parts[-2] == class_names
      

      对于 int,“==”的计算结果为 True/False,这可能是不允许的。

      【讨论】:

        【解决方案3】:

        我认为在这一行 img = tf.io.read_file(file_path) 中,img 是图像名称而不是实际图像。 解决这个问题可以参考here

        它对我有用。告诉我!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-30
          • 1970-01-01
          相关资源
          最近更新 更多