2016-06-13 106 views
2

編輯:更新所有的代碼來組織這個問題,雖然相同的問題和問題。爲什麼這個python生成器根據keras沒有輸出?

def extract_hypercolumn(model, layer_indexes, instance): 
    layers = [model.layers[li].output for li in layer_indexes] 
    get_feature = K.function([model.layers[0].input],layers) 
    assert instance.shape == (1,3,224,224) 
    feature_maps = get_feature([instance]) 
    hypercolumns = [] 
    for convmap in feature_maps: 
     for fmap in convmap[0]: 
      upscaled = sp.misc.imresize(fmap, size=(224, 224), 
             mode="F", interp='bilinear') 
      hypercolumns.append(upscaled) 

    return np.asarray(hypercolumns) 

def get_arrays(each_file): 
    img = color.rgb2lab(io.imread(each_file)[..., :3]) 
    X = img[:,:,:1] 
    y = img[:,:,1:] 
    X_rows,X_columns,X_channels=X.shape 
    y_rows,y_columns,y_channels=y.shape 
    X_channels_first = np.transpose(X,(2,0,1)) 
    X_sample = np.expand_dims(X_channels_first,axis=0) 
    X_3d = np.tile(X_sample,(1,3,1,1)) 
    hc = extract_hypercolumn(model,[3,8],X_3d) 
    hc_expand_dims = np.expand_dims(hc,axis=0) 
    y_reshaped = np.reshape(y,(y_rows*y_columns,y_channels)) 
    classed_pixels_first = KNN.predict_proba(y_reshaped) 
    classed_classes_first = np.transpose(classed_pixels_first,(1,0)) 
    classed_expand_dims = np.expand_dims(classed_classes_first,axis=0) 
    print "hypercolumn shape: ",hc_expand_dims.shape,"classified output color shape: ",classed_expand_dims.shape 
    return hc_expand_dims,classed_expand_dims 


def generate_batch(): 
    files = glob.glob('../manga-resized/sliced/*.png') 
    while True: 
     random.shuffle(files) 
     for fl in files: 
      yield get_arrays(fl) 

colorize = Colorize() 
colorize.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=["accuracy"]) 


colorize.fit_generator(generate_batch(),samples_per_epoch=1,nb_epoch=5) 

這裏是(使用Tensorflow)回溯:

Using TensorFlow backend. 
output shape: (None, 112, 228, 228) 
output_shape after reshaped: (None, 112, 51984) 
Epoch 1/5 
Traceback (most recent call last): 
    File "load.py", line 152, in <module> 
    colorize.fit_generator(generate_batch(),samples_per_epoch=1,nb_epoch=5) 
    File "/Users/alex/anaconda2/lib/python2.7/site-packages/keras/models.py", line 651, in fit_generator 
    max_q_size=max_q_size) 
    File "/Users/alex/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 1358, in fit_generator 
    'or (x, y). Found: ' + str(generator_output)) 
Exception: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: None 
Exception in thread Thread-1: 
Traceback (most recent call last): 
    File "/Users/alex/anaconda2/lib/python2.7/threading.py", line 801, in __bootstrap_inner 
    self.run() 
    File "/Users/alex/anaconda2/lib/python2.7/threading.py", line 754, in run 
    self.__target(*self.__args, **self.__kwargs) 
    File "/Users/alex/anaconda2/lib/python2.7/site-packages/keras/engine/training.py", line 404, in data_generator_task 
    generator_output = next(generator) 
StopIteration 

並採用theano - 注意這裏超柱和入級標籤打印成功 - 好像這是接近的工作:

更新:它使用theano!我很滿意。然而,問題仍然與張量流代表我猜

現在,當我嘗試:

for a, b in generate_batch(): print(a, b) 

print list(islice(generate_batch(), 3)) 

編輯:新發展 - 他們的工作!

這個工作完美,至少打印出numpy數組而不是錯誤。然而,Keras問題仍然存在

這讓我想知道我是否僅僅遇到了keras的限制 - 因爲數據有太多的預處理 - 將圖像提供給VGG,提取超列,執行KNN分類標籤等。適配發生器試圖獲得批次,但要做很多工作。也許它太多了,所以它只是將返回值視爲空,因爲它佔用了很多內存/帶寬。

我知道張量流例如有一個完整的排隊系統爲這個確切的問題構建出來。知道這是我所經歷的 - 而不是執行錯誤,這將是非常棒的。在那裏的任何keras專家都在乎重量? :)

+0

空的生成器仍然可迭代,如空列表,但不是無。 –

+0

(1)在第一個代碼中的for循環中是否執行了任何操作? (2)你從'list(islice(generate_batch_from_hdf5(),3))'得到了什麼? (3)你是否顯示了'generate_batch_from_hdf5'的完整代碼? –

+0

1.是的,在第一次迭代時,它會打印出像(100,1,224,224)(100,112,50176),然後是Epoch 1/5那樣的形狀,然後是回溯,將這些形狀添加到問題中。 2.這似乎是行得通的 - 如果加上它也是一樣3.還有問題。 – BigBoy1337

回答

3

發電機在fit_generator中應該是無限的(通過數據循環)。

c.f. keras documentation on fit_generator

The generator is expected to loop over its data indefinitely.

試着改變你的函數generate_batch到:

def generate_batch(): 
    files = glob.glob('../manga-resized/sliced/*.png') 
    while True: 
     random.shuffle(files) 
     for fl in files: 
      yield get_arrays(fl) 

另外:

我覺得你的代碼的問題來自於線

y_reshaped = (y,(y_rows*y_columns,y_channels)) 

此行沒有按」似乎完全沒有重塑。它只是創建一個包含2個元素的元組:numpy數組y和元組(y_rows*y_columns,y_channels)

我想你應該寫類似

y_reshaped = np.reshape(y,(y_rows*y_columns,y_channels)) 
+0

很好找!然而,上述相同的問題仍然存在:( – BigBoy1337

+0

哇!另一個好的發現!現在我的打印列表(islice(generate_batch(),3))聲明響起切片 - 因此它被放棄。但是,keras錯誤仍然存​​在:(我開始懷疑它是否是keras的限制 - 我意識到有很多預處理 - 正在發生 - 它通過VGG運行圖像,在提供標籤之前在標籤上執行KNN分類器。例如,Tensorflow爲這個問題建立了一個排隊系統,知道這是否是我遇到的具體問題真是太棒了 – BigBoy1337

+0

Personnally我在Theano後端使用keras,它的功能就像一個魅力(我使用'fit_generator',產量,CPU/GPU ...)你是否改變過你的函數'generate_batch'?你能更新你的代碼和trackback輸出嗎?'get_arrays'返回的'hc'和'classed_classes_first'的形狀是什麼? – sytrus

0

我遇到了正好與theano後端同樣的問題。 我通過發現當我更多地增加「max_q_size」來解決這個問題時,這個錯誤來得更早。這意味着它是一個隊列問題,並與排隊操作有關!實際上,在我的情況下,在batch_generator中缺少「while True」引發了這個bug:當在一個時代訓練接近發生器中所有可用訓練樣本全部加載到隊列中時,生成器必須將「None」作爲「next_sample」排入隊列,並且fit_generator將最終滿足此「None」並報告您提到的錯誤。

相關問題