【问题标题】:TypeError: __call__() takes from 1 to 2 positional arguments but 3 were givenTypeError: __call__() 接受 1 到 2 个位置参数,但给出了 3 个
【发布时间】:2020-09-10 11:11:24
【问题描述】:

我正在尝试进行多标签图像分类,并且正在尝试将图像数据集转换为 TFRecords 格式。

这是我的代码:

        def _bytes_feature(value):
          if isinstance(value,type(tf.cosntant(0))):
            value = value.numpy()
          return tf.train.Feature(byte_list=tf.train.BytesList(value=[value]))
        
        def _float_feature(value):
          return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
        
        def _int64_feature(value):
          return tf.train.Feature(int64_list = tf.train.Int64List(value=[value]))
        
        def image_example(image_string,image_name, df):
          image_shape = tf.image.decode_png(image_string).shape
          
          feature = {
          'height': _int64_feature(image_shape[0]),
          'width': _int64_feature(image_shape[1]),
          'depth': _int64_feature(image_shape[2]),
          'label_1': _int64_feature(df.loc(image_name,'Atelectasis')),
          'label_2': _int64_feature(df.loc(image_name,'Cardiomegaly')),
          'label_3': _int64_feature(df.loc(image_name,'Consolidation')),
          'label_4': _int64_feature(df.loc(image_name,'Edema')),
          'label_5': _int64_feature(df.loc(image_name,'Effusion')),
          'label_6': _int64_feature(df.loc(image_name,'Emphysema')),
          'label_7': _int64_feature(df.loc(image_name,'Fibrosis')),
          'label_8': _int64_feature(df.loc(image_name,'Hernia')),
          'label_9': _int64_feature(df.loc(image_name,'Infiltration')),
          'label_10': _int64_feature(df.loc(image_name,'Mass')),
          'label_11': _int64_feature(df.loc(image_name,'Nodule')),
          'label_12': _int64_feature(df.loc(image_name,'Pleural_Thickening')),
          'label_13': _int64_feature(df.loc(image_name,'Pneumonia')),
          'label_14': _int64_feature(df.loc(image_name,'Pneumothorax')),
          'label_15': _int64_feature(df.loc(image_name,'No Finding')),
          'image_raw': _bytes_feature(image_string),
          }
          return tf.train.example(features = tf.train.Features(feature=feature))


  

record_image = 'image.tfrecords'
with tf.io.TFRecordWriter(record_image) as write:
  for row in df.index:
    full_path = '/content/images/'+df['Image Index'][row]
    image_string = tf.io.read_file(full_path)
    image_name = pd.Series(df['Image Index'])[row]
    tf_example = image_example(image_string, image_name, df)
    write.write(tf_example.SerializeToString())

但它会抛出错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-66-f42cc357d799> in <module>()
      5     image_string = tf.io.read_file(full_path)
      6     image_name = pd.Series(df['Image Index'])[row]
----> 7     tf_example = image_example(image_string,image_name,df)
      8     write.write(tf_example.SerializeToString())

<ipython-input-64-de0476ade753> in image_example(image_string, image_name, df)
     17   'width': _int64_feature(image_shape[1]),
     18   'depth': _int64_feature(image_shape[2]),
---> 19   'label_1': _int64_feature(df.loc(image_name,'Atelectasis')),
     20   'label_2': _int64_feature(df.loc(image_name,'Cardiomegaly')),
     21   'label_3': _int64_feature(df.loc(image_name,'Consolidation')),

TypeError: __call__() takes from 1 to 2 positional arguments but 3 were given

Q1) 我可以这样存储特征吗?来自 tensorflowHub 的模型无法理解它。我可以“告诉”模型期望什么作为输入吗?

Q2) 为什么会出现该错误? Image_example 显然需要 3 个参数。

【问题讨论】:

  • Q1 我有更普遍的疑问。 Q2 是关于我在代码中遇到的这个错误。

标签: python tensorflow computer-vision tensorflow-datasets tfrecord


【解决方案1】:
df.loc(image_name,'X') 

需要:

df.loc[image_name,'X']

【讨论】:

  • 这可能有助于为其他问题打开一个单独的问题,提供更多信息和上下文(尽管它可能过于开放而无法得到回答)。
  • 现在我得到了一个 Keyerror: (with name of the first image)
  • @VaibhavJha 哦,我看到了问题,您需要实际索引系列而不是数据框。试试image_name.loc['X']。 (您可能需要从创建 Series 对象的行中删除 [row])。
  • 我会按照你说的去做。但是你能再解释一下吗。我是一个非常初学者。谢谢
  • 由于您似乎有多个错误,因此添加一个最小的可重现示例stackoverflow.com/help/minimal-reproducible-example 可能会有所帮助
猜你喜欢
  • 1970-01-01
  • 2019-06-28
  • 2023-03-03
  • 2021-02-02
  • 2017-10-09
  • 2020-07-14
  • 2015-07-13
  • 2015-09-01
  • 2017-01-12
相关资源
最近更新 更多