2017-08-31 74 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