2017-08-31 38 views
0

您好我想为图像和它们的一个热门阵列标签创建一个tfrecord.Im能达到它的图像,但不是标签。我提到这个SOF link,但得到相同的错误。下面是我的代码。无法为图像标签创建张量流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'>) 

我不知道我哪里错了?任何帮助将是非常有用的。

回答

1

既然你定义的标签为_int64_feature,你必须使用一个int作为标签不是numpy array

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

,你可以将它们转换为one_hot格式,而读取数据。

如果你想要把它作为一个列表;修改你的函数定义

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

感谢您的回复Ishant。但是在上面的链接中,Steven已经通过了一个列表,看起来似乎是这样吗? – george

+0

是的!您需要修改您的函数,该函数,看到我更新的答案。 –

+0

感谢ü非常ishant..could你喂养的数据tensorflow管道培训提出了一些教程?; – george