編輯:更新所有的代碼來組織這個問題,雖然相同的問題和問題。爲什麼這個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專家都在乎重量? :)
空的生成器仍然可迭代,如空列表,但不是無。 –
(1)在第一個代碼中的for循環中是否執行了任何操作? (2)你從'list(islice(generate_batch_from_hdf5(),3))'得到了什麼? (3)你是否顯示了'generate_batch_from_hdf5'的完整代碼? –
1.是的,在第一次迭代時,它會打印出像(100,1,224,224)(100,112,50176),然後是Epoch 1/5那樣的形狀,然後是回溯,將這些形狀添加到問題中。 2.這似乎是行得通的 - 如果加上它也是一樣3.還有問題。 – BigBoy1337