val.py 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import numpy as np
import tqdm
import cv2
from paddle.fluid.dygraph.base import to_variable
import paddle.fluid as fluid
C
chenguowei01 已提交
22 23
import paddle.nn.functional as F
import paddle
24

C
chenguowei01 已提交
25
import dygraph.utils.logger as logger
C
chenguowei01 已提交
26 27
from dygraph.utils import ConfusionMatrix
from dygraph.utils import Timer, calculate_eta
28 29 30 31 32 33 34


def evaluate(model,
             eval_dataset=None,
             model_dir=None,
             num_classes=None,
             ignore_index=255,
C
chenguowei01 已提交
35
             iter_id=None):
36 37 38 39 40
    ckpt_path = os.path.join(model_dir, 'model')
    para_state_dict, opti_state_dict = fluid.load_dygraph(ckpt_path)
    model.set_dict(para_state_dict)
    model.eval()

C
chenguowei01 已提交
41
    total_iters = len(eval_dataset)
42 43
    conf_mat = ConfusionMatrix(num_classes, streaming=True)

C
chenguowei01 已提交
44
    logger.info(
C
chenguowei01 已提交
45 46
        "Start to evaluating(total_samples={}, total_iters={})...".format(
            len(eval_dataset), total_iters))
47 48
    timer = Timer()
    timer.start()
C
chenguowei01 已提交
49 50
    for iter, (im, im_info, label) in tqdm.tqdm(
            enumerate(eval_dataset), total=total_iters):
51
        im = to_variable(im)
C
chenguowei01 已提交
52 53 54
        # pred, _ = model(im)
        logits = model(im)
        pred = paddle.argmax(logits[0], axis=1)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        pred = pred.numpy().astype('float32')
        pred = np.squeeze(pred)
        for info in im_info[::-1]:
            if info[0] == 'resize':
                h, w = info[1][0], info[1][1]
                pred = cv2.resize(pred, (w, h), cv2.INTER_NEAREST)
            elif info[0] == 'padding':
                h, w = info[1][0], info[1][1]
                pred = pred[0:h, 0:w]
            else:
                raise Exception("Unexpected info '{}' in im_info".format(
                    info[0]))
        pred = pred[np.newaxis, :, :, np.newaxis]
        pred = pred.astype('int64')
        mask = label != ignore_index

        conf_mat.calculate(pred=pred, label=label, ignore=mask)
        _, iou = conf_mat.mean_iou()

C
chenguowei01 已提交
74 75
        time_iter = timer.elapsed_time()
        remain_iter = total_iters - iter - 1
C
chenguowei01 已提交
76
        logger.debug(
C
chenguowei01 已提交
77 78 79
            "[EVAL] iter_id={}, iter={}/{}, iou={:4f}, sec/iter={:.4f} | ETA {}"
            .format(iter_id, iter + 1, total_iters, iou, time_iter,
                    calculate_eta(remain_iter, time_iter)))
80 81 82 83
        timer.restart()

    category_iou, miou = conf_mat.mean_iou()
    category_acc, macc = conf_mat.accuracy()
C
chenguowei01 已提交
84
    logger.info("[EVAL] #Images={} mAcc={:.4f} mIoU={:.4f}".format(
85
        len(eval_dataset), macc, miou))
C
chenguowei01 已提交
86 87 88
    logger.info("[EVAL] Category IoU: " + str(category_iou))
    logger.info("[EVAL] Category Acc: " + str(category_acc))
    logger.info("[EVAL] Kappa:{:.4f} ".format(conf_mat.kappa()))
89
    return miou, macc