2017-05-05 637 views
0

我很難找到這個轉換的資源。我看到一些示例代碼中的輸入數據爲.pkl格式,而mnist數據集的格式爲.idx3-ubyte。用於計算視覺的數據集格式各不相同。我不熟悉任何格式,如果能夠解決這個問題,我們將不勝感激。謝謝。如何將jpg數據集快速轉換爲.pkl用於CNN?


更新:現在我成功地加載我的圖片使用下面的代碼.tfrecords格式,但作爲這樣的格式爲CNN似乎無法閱讀,我仍然在試圖修改.pkl格式的代碼。但是,我的跑步都失敗了。

  cwd='/Users/Downloads/tflearn_train/' 
      classes={'0','1'} #classify into 2 types 
      writer= tf.python_io.TFRecordWriter("train.tfrecords") #file to be produced 

      for index,name in enumerate(classes): 
       class_path=cwd+name+'/' 
       for img_name in os.listdir(class_path): 
        if (not img_name.startswith('.') and img_name != 'Thumbs.db'): 
         img_path=class_path+img_name #the path of every pic 
         img=Image.open(img_path,"r") 
         img= img.resize((224,224)) 
         img_raw=img.tobytes()#transform pic into binary 
         example = tf.train.Example(features=tf.train.Features(feature={ 
          "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])), 
          'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw])) 
         })) 
         writer.write(example.SerializeToString()) 
      writer.close() 

上面的工作正常。但我試圖把

 write_file = open('train.pkl', 'wb') 
     cPickle.dump(example, write_file, -1) 
     cPickle.dump(example.features.feature['label'].int64_list.value, write_file, -1) 
     write_file.close() 

內部和外部的循環。到目前爲止,我使用cPickle.load時未能創建與其他.pkl文件類似的.pkl文件。

感謝您的每一個輸入。

回答

0

Pickle存儲有關python對象的結構以及數據的信息。對於簡單的張量,這可能不是必需的。

取而代之,通常的方法是將二進制格式的矩陣數據轉儲到文件中並直接重新加載到內存中。我相信用於MNIST圖形數據集的「.idx3-ubyte」就是這樣一個例子。

如果你使用python和numpy,你最好使用numpy的.npy格式,它可以簡化np.load和np.dump函數的一些過程:https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.load.html

如果您需要加載二進制數據轉儲,看看https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html

+0

感謝您的輸入。我已經成功地以.tfrecords的形式加載了這些圖像和標籤,但是我找不到任何以這種格式加載文件來訓練cnn的代碼。 –

+0

假設tf代表張量流,我沒有經驗,對不起。 – pixelou