【问题标题】:Keras/Tensorflow calculate mean_iou for batchesKeras/Tensorflow 为批次计算 mean_iou
【发布时间】:2018-02-10 11:43:50
【问题描述】:

我正在尝试计算 mean_iou 并更新每个批次的混淆矩阵。但是经过 30 步后,我得到了一个 SIGKILL 事件。我在生成器中使用的图像的分辨率为 2048x1024,因此我的 batch_size 为 2。似乎在一步完成后我无法释放内存。我在迭代所有图像时测试了生成器,但一切正常。

我在 GTX 1080 上使用 Keras 2.1.2 和 Tensorflow 1.4.1 作为后端。如果有人有建议,那就太好了。

def calculate_iou_tf(model, generator, steps, num_classes):
    conf_m = K.tf.zeros((num_classes, num_classes), dtype=K.tf.float64)
    generator.reset()
    pb = Progbar(steps)
    for i in range(0, steps):
        x, y_true = generator.next()
        y_pred = model.predict_on_batch(x)

        # num_classes = K.int_shape(y_pred)[-1]
        y_pred = K.flatten(K.argmax(y_pred, axis=-1))
        y_true = K.reshape(y_true, (-1,))

        mask = K.less_equal(y_true, num_classes - 1)
        y_true = K.tf.to_int32(K.tf.boolean_mask(y_true, mask))
        y_pred = K.tf.to_int32(K.tf.boolean_mask(y_pred, mask))

        mIoU, up_op = K.tf.contrib.metrics.streaming_mean_iou(y_pred, y_true, num_classes, updates_collections=[conf_m])
        K.get_session().run(K.tf.local_variables_initializer())
        with K.tf.control_dependencies([up_op]):
            score = K.eval(mIoU)
            print(score)

        pb.update(i + 1)

    conf_m = K.eval(conf_m)
    return conf_m, K.eval(mIoU)

【问题讨论】:

  • 你为什么使用K函数而不是numpy
  • 我正在使用 Tensorflow(K 只是 Keras 后端)在 GPU 上进行计算
  • 但是每次你定义张量时 - 它都被分配了,不幸的是没有与当前的tensorflow 实现一起发布。此外 - 我预计将计算推送到 gpu 不会有太大的提升。
  • 好吧,很遗憾,但感谢您的帮助! CPU版本运行没有问题。
  • 那么我可以制定一个答案吗?

标签: python tensorflow keras deep-learning metrics


【解决方案1】:

问题在于使用keras.backend 函数而不是numpy 函数。每次调用函数时 - 都会创建一个新张量。不幸的是——在tf 的当前实现中——没有张量的系统垃圾收集——所以这导致内存满错误。切换到numpy 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2021-09-18
    • 2020-09-25
    • 2021-05-22
    • 1970-01-01
    相关资源
    最近更新 更多