# Copyright 2021 Huawei Technologies Co., Ltd # # 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. # ============================================================================ """post process for 310 inference""" import os import argparse import numpy as np import cv2 from eval import cal_hist, pre_process def parse_args(): parser = argparse.ArgumentParser(description="deeplabv3 accuracy calculation") parser.add_argument('--data_root', type=str, default='', help='root path of val data') parser.add_argument('--data_lst', type=str, default='', help='list of val data') parser.add_argument('--batch_size', type=int, default=1, help='batch size') parser.add_argument('--crop_size', type=int, default=513, help='crop size') parser.add_argument('--scales', type=float, action='append', help='scales of evaluation') parser.add_argument('--flip', action='store_true', help='perform left-right flip') parser.add_argument('--ignore_label', type=int, default=255, help='ignore label') parser.add_argument('--num_classes', type=int, default=21, help='number of classes') parser.add_argument('--result_path', type=str, default='./result_Files', help='result Files path') args, _ = parser.parse_known_args() return args def eval_batch(args, result_file, img_lst, crop_size=513, flip=True): result_lst = [] batch_size = len(img_lst) batch_img = np.zeros((args.batch_size, 3, crop_size, crop_size), dtype=np.float32) resize_hw = [] for l in range(batch_size): img_ = img_lst[l] img_, resize_h, resize_w = pre_process(args, img_, crop_size) batch_img[l] = img_ resize_hw.append([resize_h, resize_w]) batch_img = np.ascontiguousarray(batch_img) net_out = np.fromfile(result_file, np.float32).reshape(args.batch_size, args.num_classes, crop_size, crop_size) for bs in range(batch_size): probs_ = net_out[bs][:, :resize_hw[bs][0], :resize_hw[bs][1]].transpose((1, 2, 0)) ori_h, ori_w = img_lst[bs].shape[0], img_lst[bs].shape[1] probs_ = cv2.resize(probs_, (ori_w, ori_h)) result_lst.append(probs_) return result_lst def eval_batch_scales(args, eval_net, img_lst, scales, base_crop_size=513, flip=True): sizes_ = [int((base_crop_size - 1) * sc) + 1 for sc in scales] probs_lst = eval_batch(args, eval_net, img_lst, crop_size=sizes_[0], flip=flip) print(sizes_) for crop_size_ in sizes_[1:]: probs_lst_tmp = eval_batch(args, eval_net, img_lst, crop_size=crop_size_, flip=flip) for pl, _ in enumerate(probs_lst): probs_lst[pl] += probs_lst_tmp[pl] result_msk = [] for i in probs_lst: result_msk.append(i.argmax(axis=2)) return result_msk def acc_cal(): args = parse_args() args.image_mean = [103.53, 116.28, 123.675] args.image_std = [57.375, 57.120, 58.395] # data list with open(args.data_lst) as f: img_lst = f.readlines() # evaluate hist = np.zeros((args.num_classes, args.num_classes)) batch_img_lst = [] batch_msk_lst = [] bi = 0 image_num = 0 for i, line in enumerate(img_lst): img_path, msk_path = line.strip().split(' ') result_file = os.path.join(args.result_path, os.path.basename(img_path).split('.jpg')[0] + '_0.bin') img_path = os.path.join(args.data_root, img_path) msk_path = os.path.join(args.data_root, msk_path) img_ = cv2.imread(img_path) msk_ = cv2.imread(msk_path, cv2.IMREAD_GRAYSCALE) batch_img_lst.append(img_) batch_msk_lst.append(msk_) bi += 1 if bi == args.batch_size: batch_res = eval_batch_scales(args, result_file, batch_img_lst, scales=args.scales, base_crop_size=args.crop_size, flip=args.flip) for mi in range(args.batch_size): hist += cal_hist(batch_msk_lst[mi].flatten(), batch_res[mi].flatten(), args.num_classes) bi = 0 batch_img_lst = [] batch_msk_lst = [] print('processed {} images'.format(i+1)) image_num = i if bi > 0: batch_res = eval_batch_scales(args, result_file, batch_img_lst, scales=args.scales, base_crop_size=args.crop_size, flip=args.flip) for mi in range(bi): hist += cal_hist(batch_msk_lst[mi].flatten(), batch_res[mi].flatten(), args.num_classes) print('processed {} images'.format(image_num + 1)) print(hist) iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist)) print('per-class IoU', iu) print('mean IoU', np.nanmean(iu)) if __name__ == '__main__': acc_cal()