为什么这个 python 生成器根据 keras 没有输出?

Why does this python generator have no output according to 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 分类标签等。拟合生成器正在尝试获取批次,但为此做了大量工作。也许它太多了,所以它只是将 return 值视为空,因为它占用了太多 memory/bandwidth。

我知道例如张量流有一个完整的排队系统来解决这个问题。很高兴知道这是否是我正在经历的——而不是实施错误。那里的任何 keras 专家都关心重量??? :)

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))

我在 theano 后端遇到了完全相同的问题。 我通过发现当我增加 "max_q_size" 更多时,这个错误出现得更早来探究问题。说明是队列问题,跟入队操作有关!!!

事实上,在我的例子中,batch_generator 中缺少 "while True" 会在一个时期内进行此 bug:when 训练,该时期接近生成器中所有可用训练样本的时间点全部加载到队列中,然后生成器将不得不将 "None" 作为 "next_sample" 加入队列,而 fit_generator 最终会遇到此 "None" 并报告您提到的错误.