简体   繁体   English

无法为图像标签创建张量流tfrecord

[英]Unable to create a tensorflow tfrecord for an image label

Hi I want to create a tfrecord for images and their one hot array labels.Im able to acheive it for the images,but not for the labels.I referred to this SOF link ,but getting the same error.Below is my code. 嗨,我想为图像及其一个热阵列标签创建一个tfrecord。我能够为图像而不是标签实现它。我引用了此SOF链接 ,但遇到了同样的错误。下面是我的代码。

def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))


def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))


for i in range(len(train_addrs)):

  print('reading image no {0} : and image address {1}'.format(i,train_addrs[i]))

  img = load_image(train_addrs[i])#loading the preprocessed image

  label = train_labels[i]#loading associated one-hot array

  print('label is ',label) #array([0, 1]) of type uint8 ,I tried with int64,int32 also;but no use

  feature = {'train/label':_int64_feature(label),
             'train/image':_bytes_feature(tf.compat.as_bytes(img.tostring())) #this part works 
           }    


  example = tf.train.Example(features=tf.train.Features(feature=feature))

  serToString = example.SerializeToString()

  writer.write(serToString)

When I execute this code,Im getting the following error. 当我执行此代码时,出现以下错误。

 TypeError: array([0, 1]) has type <type 'numpy.ndarray'>, but expected one of: (<type 'int'>, <type 'long'>)

Im not sure where am I going wrong?Any help would be really helpful. 我不确定我要去哪里错了吗?任何帮助都会非常有帮助。

Since you defined label as _int64_feature , you have to use a int for label not a numpy array 由于您将标签定义为_int64_feature ,因此必须对标签使用int而不是numpy array

  label = train_labels[i]#loading associated one-hot array
  label = np.argmax(label)

you can convert them to one_hot format while reading the data. 您可以在读取数据时将其转换为one_hot格式。

If you want to pass it as a list; 如果您希望将其作为列表传递; modify your function definition 修改您的功能定义

def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM