2016-03-11 74 views
0

我嘗試製作與CIFAR10類似的數據集。我發現這個教程: How to create dataset similar to cifar-10如何使用幾張圖像製作與CIFAR10相似的數據集

我已經可以做一個數據集1倍的圖像,但是當我嘗試使用一些圖片,我得到這個錯誤:

tensorflow.python.framework.errors.InvalidArgumentError: Indices are not valid: not lexicographically sorted or containing repeats. 

誰能幫我解決這個問題呢?

這是我的代碼:

from PIL import Image 
import numpy as np 

out =np.empty([20,7501]) 
    for j in xrange(0, 10): 
     im = Image.open('%d_receipt.jpg' % j) 
     im = (np.array(im)) 
     r = im[:,:,0].flatten() 
     g = im[:,:,1].flatten() 
     b = im[:,:,2].flatten() 
     label = [0] 
     out[j] = np.array(list(label) + list(r) + list(g) + list(b),np.uint8) 


    for i in xrange(0, 10): 
     im = Image.open('%d_news.jpg' % i) 
     im = (np.array(im)) 
     r = im[:,:,0].flatten() 
     g = im[:,:,1].flatten() 
     b = im[:,:,2].flatten() 
     label = [1] 
     j = i + 10 
     out[j] = np.array(list(label) + list(r) + list(g) + list(b),np.uint8) 


out.tofile("data_batch.bin") 

回答

0

我不喜歡這樣寫道:

import numpy as np 
import scipy.io 

mat = scipy.io.loadmat('train_32x32.mat') 
data = mat['X'] 
label = mat['y'] 

R_data = data[:,:,0,:] 
G_data = data[:,:,1,:] 
B_data = data[:,:,2,:] 

R_data = np.transpose(R_data, (2,0,1)) 
G_data = np.transpose(G_data, (2,0,1)) 
B_data = np.transpose(B_data, (2,0,1)) 

R_data = np.reshape(R_data,(73257,32*32)) 
G_data = np.reshape(G_data,(73257,32*32)) 
B_data = np.reshape(B_data,(73257,32*32)) 

outdata = np.concatenate((label,R_data,G_data,B_data), axis = 1) 
step = 10000 
for i in range(1,6): 
    temp = outdata[i*step:(i+1)*step,:] 
    temp.tofile('SVHN_train_data_batch%d.bin' % i) 
    print('save data %d' % i) 

然後,只需把它直接在Cifar10 tensorflow例如列車代碼。

+0

我可能聽起來很愚蠢,但你能告訴你如何獲得「train_32x32.mat」文件嗎?這些是我們想要訓練的數據集的提取特徵嗎? – shivisuper

0

我也試着按照你在問題中發佈的教程,但是我無法讓它工作,所以我做了我自己的解決方案。它可以在這裏找到我的github:https://github.com/jdeepee/machine_learning/tree/master

該代碼被評論,所以應該很容易遵循。我應該注意它通過包含多個包含圖像的文件夾的主目錄迭代。

0

下面的代碼片段是我爲了使CIFAR-10適應GTSRB而做的。更多細節在這裏。 https://github.com/hashkanna/traffic-signs/blob/master/Traffic_Signs_Recognition_binFiles.ipynb

out = {} 

for i in range(5): 
    bin_val = (i%5) + 1 
    #im = Image.open(X_train[i]) 
    #im = np.array(im) 
    im = X_train[i] 
    r = im[:,:,0].flatten() 
    g = im[:,:,1].flatten() 
    b = im[:,:,2].flatten() 
    label = [y_train[i]] 
    out[bin_val] = np.array(list(label) + list(r) + list(g) + list(b),np.uint8) 

for i in range(5,len(X_train)): 
    bin_val = (i%5) + 1 
    #im = Image.open(X_train[i]) 
    #im = np.array(im) 
    im = X_train[i] 
    r = im[:,:,0].flatten() 
    g = im[:,:,1].flatten() 
    b = im[:,:,2].flatten() 
    label = [y_train[i]] 
    new_array = np.array(list(label) + list(r) + list(g) + list(b),np.uint8) 
    out[bin_val] = np.append(out[bin_val], new_array, 0) 

for bin_val in range(1,6): 
    out[bin_val].tofile("/Users/kanna/Downloads/data_batch_%s.bin"%bin_val) 
相關問題