python - Keras/Tensorflow 计算批处理的mean_iou

标签 python tensorflow keras deep-learning metrics

我正在尝试计算 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)

最佳答案

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

关于python - Keras/Tensorflow 计算批处理的mean_iou,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48720388/

相关文章:

python - 导入 OpenCV python 模块时出错(使用 Qt 和 QtOpenGL 构建时)

python - 在 python 上使用请求发布图像

python - M2Crypto:验证 DSA 签名

python - 使用 Keras 进行 OR-Lambda 层操作

python - 在 Keras 和 Tensorflow 中为多线程设置复制模型

python - Keras - .flow_from_directory(目录)

python - 为什么 python 不重新引用这个现有对象?

tensorflow - tensorflow 中的外积

python - 拟合 TensorForestEstimator 时 TensorFlow 崩溃

python - Tensorflow估计器输入函数: defining each feature or not?