infer.py 3.3 KB
Newer Older
W
wuyefeilin 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# coding: utf8
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
# 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.

W
wuyefeilin 已提交
16 17 18 19 20 21 22
import argparse
import os
import os.path as osp
import cv2
import numpy as np
import tqdm

23 24 25
import utils
import models
import transforms
W
wuyefeilin 已提交
26 27 28 29 30 31


def parse_args():
    parser = argparse.ArgumentParser(
        description='HumanSeg inference and visualization')
    parser.add_argument(
32 33 34
        '--model_dir',
        dest='model_dir',
        help='Model path for inference',
W
wuyefeilin 已提交
35 36 37 38
        type=str)
    parser.add_argument(
        '--data_dir',
        dest='data_dir',
39
        help='The root directory of dataset',
W
wuyefeilin 已提交
40 41
        type=str)
    parser.add_argument(
42 43 44 45
        '--test_list',
        dest='test_list',
        help='Test list file of dataset',
        type=str)
W
wuyefeilin 已提交
46 47 48
    parser.add_argument(
        '--save_dir',
        dest='save_dir',
49
        help='The directory for saving the inference results',
W
wuyefeilin 已提交
50
        type=str,
51
        default='./output/result')
C
chenguowei01 已提交
52 53 54 55 56 57 58
    parser.add_argument(
        "--image_shape",
        dest="image_shape",
        help="The image shape for net inputs.",
        nargs=2,
        default=[192, 192],
        type=int)
W
wuyefeilin 已提交
59 60 61 62 63 64 65 66 67
    return parser.parse_args()


def mkdir(path):
    sub_dir = osp.dirname(path)
    if not osp.exists(sub_dir):
        os.makedirs(sub_dir)


68 69
def infer(args):
    test_transforms = transforms.Compose(
C
chenguowei01 已提交
70
        [transforms.Resize(args.image_shape),
71 72
         transforms.Normalize()])
    model = models.load_model(args.model_dir)
W
wuyefeilin 已提交
73 74 75 76
    added_saveed_path = osp.join(args.save_dir, 'added')
    mat_saved_path = osp.join(args.save_dir, 'mat')
    scoremap_saved_path = osp.join(args.save_dir, 'scoremap')

77
    with open(args.test_list, 'r') as f:
W
wuyefeilin 已提交
78 79 80 81 82 83
        files = f.readlines()

    for file in tqdm.tqdm(files):
        file = file.strip()
        im_file = osp.join(args.data_dir, file)
        im = cv2.imread(im_file)
84
        result = model.predict(im, transforms=test_transforms)
W
wuyefeilin 已提交
85 86

        # save added image
87
        added_image = utils.visualize(im_file, result, weight=0.6)
W
wuyefeilin 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        added_image_file = osp.join(added_saveed_path, file)
        mkdir(added_image_file)
        cv2.imwrite(added_image_file, added_image)

        # save score map
        score_map = result['score_map'][:, :, 1]
        score_map = (score_map * 255).astype(np.uint8)
        score_map_file = osp.join(scoremap_saved_path, file)
        mkdir(score_map_file)
        cv2.imwrite(score_map_file, score_map)

        # save mat image
        score_map = np.expand_dims(score_map, axis=-1)
        mat_image = np.concatenate([im, score_map], axis=2)
        mat_file = osp.join(mat_saved_path, file)
        ext = osp.splitext(mat_file)[-1]
        mat_file = mat_file.replace(ext, '.png')
        mkdir(mat_file)
        cv2.imwrite(mat_file, mat_image)


if __name__ == '__main__':
    args = parse_args()
111
    infer(args)