Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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 无法为图像标签创建tensorflow tfrecord_Python_Numpy_Tensorflow - Fatal编程技术网

Python 无法为图像标签创建tensorflow tfrecord

Python 无法为图像标签创建tensorflow tfrecord,python,numpy,tensorflow,Python,Numpy,Tensorflow,您好,我想为图像及其一个热数组标签创建一个tfrecord。我可以为图像创建tfrecord,但不能为标签创建tfrecord。我参考了这个,但得到了相同的错误。下面是我的代码 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=

您好,我想为图像及其一个热数组标签创建一个tfrecord。我可以为图像创建tfrecord,但不能为标签创建tfrecord。我参考了这个,但得到了相同的错误。下面是我的代码

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)
当我执行这段代码时,我得到了以下错误

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

我不知道我哪里出错了?任何帮助都会非常有用。

因为您将标签定义为_int64_功能,所以必须使用int作为标签,而不是numpy数组

您可以在读取数据时将其转换为一个热格式

如果您想将其作为列表传递;修改函数定义

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

谢谢你的回复。但是在上面的链接中,史蒂文似乎传递了一个列表。这可能吗?是的!为此,您需要修改您的函数,请参阅我的更新答案。非常感谢ishant。您能否建议一些将数据提供给tensorflow管道进行培训的教程?;你可以参考
def _int64_feature(value):
    return tf.train.Feature(int64_list=tf.train.Int64List(value=value))