eval.py 3.8 KB
Newer Older
1 2 3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
D
Dun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
import os
os.environ['FLAGS_fraction_of_gpu_memory_to_use'] = '0.98'

import paddle
import paddle.fluid as fluid
import numpy as np
import argparse
from reader import CityscapeDataset
import reader
import models
import sys


def add_argument(name, type, default, help):
    parser.add_argument('--' + name, default=default, type=type, help=help)


def add_arguments():
    add_argument('total_step', int, -1,
                 "Number of the step to be evaluated, -1 for full evaluation.")
    add_argument('init_weights_path', str, None,
                 "Path of the weights to evaluate.")
    add_argument('dataset_path', str, None, "Cityscape dataset path.")
    add_argument('verbose', bool, False, "Print mIoU for each step if verbose.")
    add_argument('use_gpu', bool, True, "Whether use GPU or CPU.")
D
Dun 已提交
29
    add_argument('num_classes', int, 19, "Number of classes.")
D
Dun 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72


def mean_iou(pred, label):
    label = fluid.layers.elementwise_min(
        label, fluid.layers.assign(np.array(
            [num_classes], dtype=np.int32)))
    label_ignore = (label == num_classes).astype('int32')
    label_nignore = (label != num_classes).astype('int32')

    pred = pred * label_nignore + label_ignore * num_classes

    miou, wrong, correct = fluid.layers.mean_iou(pred, label, num_classes + 1)
    return miou, wrong, correct


def load_model():
    if args.init_weights_path.endswith('/'):
        fluid.io.load_params(
            exe, dirname=args.init_weights_path, main_program=tp)
    else:
        fluid.io.load_params(
            exe, dirname="", filename=args.init_weights_path, main_program=tp)


CityscapeDataset = reader.CityscapeDataset

parser = argparse.ArgumentParser()
add_arguments()

args = parser.parse_args()

models.clean()
models.is_train = False
deeplabv3p = models.deeplabv3p

image_shape = [1025, 2049]
eval_shape = [1024, 2048]

sp = fluid.Program()
tp = fluid.Program()
batch_size = 1
reader.default_config['crop_size'] = -1
reader.default_config['shuffle'] = False
D
Dun 已提交
73
num_classes = args.num_classes
D
Dun 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87

with fluid.program_guard(tp, sp):
    img = fluid.layers.data(name='img', shape=[3, 0, 0], dtype='float32')
    label = fluid.layers.data(name='label', shape=eval_shape, dtype='int32')
    img = fluid.layers.resize_bilinear(img, image_shape)
    logit = deeplabv3p(img)
    logit = fluid.layers.resize_bilinear(logit, eval_shape)
    pred = fluid.layers.argmax(logit, axis=1).astype('int32')
    miou, out_wrong, out_correct = mean_iou(pred, label)

tp = tp.clone(True)
fluid.memory_optimize(
    tp,
    print_log=False,
D
Dun 已提交
88
    skip_opt_set=set([pred.name, miou, out_wrong, out_correct]),
D
Dun 已提交
89 90 91 92 93 94 95 96 97
    level=1)

place = fluid.CPUPlace()
if args.use_gpu:
    place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(sp)

if args.init_weights_path:
98
    print("load from:", args.init_weights_path)
D
Dun 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    load_model()

dataset = CityscapeDataset(args.dataset_path, 'val')
if args.total_step == -1:
    total_step = len(dataset.label_files)
else:
    total_step = args.total_step

batches = dataset.get_batch_generator(batch_size, total_step)

sum_iou = 0
all_correct = np.array([0], dtype=np.int64)
all_wrong = np.array([0], dtype=np.int64)

for i, imgs, labels, names in batches:
    result = exe.run(tp,
                     feed={'img': imgs,
                           'label': labels},
                     fetch_list=[pred, miou, out_wrong, out_correct])
    wrong = result[2][:-1] + all_wrong
    right = result[3][:-1] + all_correct
    all_wrong = wrong.copy()
    all_correct = right.copy()
    mp = (wrong + right) != 0
    miou2 = np.mean((right[mp] * 1.0 / (right[mp] + wrong[mp])))
    if args.verbose:
125
        print('step: %s, mIoU: %s' % (i + 1, miou2))
D
Dun 已提交
126
    else:
127
        print('\rstep: %s, mIoU: %s' % (i + 1, miou2))
D
Dun 已提交
128
        sys.stdout.flush()