File size: 6,855 Bytes
c2ba1ac c139485 c2ba1ac c139485 c2ba1ac c139485 c2ba1ac |
1 2 3 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 29 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
import os
import argparse
import random
import onnxruntime
import numpy as np
import torch
from torch.nn import functional as F
from torch.utils import data
import cv2
from PIL import Image
from tqdm import tqdm
from utils import input_transform, pad_image, resize_image, preprocess, get_confusion_matrix
parser = argparse.ArgumentParser(description='HRNet')
parser.add_argument('-m', '--onnx-model', default='',
type=str, help='Path to onnx model.')
parser.add_argument('-r', '--root', default='',
type=str, help='Path to dataset root.')
parser.add_argument('-l', '--list_path', default='',
type=str, help='Path to dataset list.')
parser.add_argument("--ipu", action="store_true", help="Use IPU for inference.")
parser.add_argument("--provider_config", type=str,
default="vaip_config.json", help="Path of the config file for seting provider_options.")
args = parser.parse_args()
INPUT_SIZE = [512, 1024]
NUM_CLASSES = 19
IGNORE_LABEL = 255
class Cityscapes(data.Dataset):
def __init__(self,
root,
list_path,
num_classes=19,
downsample_rate=8,
ignore_label=-1):
self.root = root
self.list_path = list_path
self.num_classes = num_classes
self.downsample_rate = downsample_rate
self.img_list = [line.strip().split() for line in open(root+list_path)]
self.files = self.read_files()
self.label_mapping = {-1: ignore_label, 0: ignore_label,
1: ignore_label, 2: ignore_label,
3: ignore_label, 4: ignore_label,
5: ignore_label, 6: ignore_label,
7: 0, 8: 1, 9: ignore_label,
10: ignore_label, 11: 2, 12: 3,
13: 4, 14: ignore_label, 15: ignore_label,
16: ignore_label, 17: 5, 18: ignore_label,
19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11,
25: 12, 26: 13, 27: 14, 28: 15,
29: ignore_label, 30: ignore_label,
31: 16, 32: 17, 33: 18}
def read_files(self):
files = []
for item in self.img_list:
image_path, label_path = item
name = os.path.splitext(os.path.basename(label_path))[0]
files.append({
"img": image_path,
"label": label_path,
"name": name,
})
return files
def __len__(self):
return len(self.files)
def convert_label(self, label, inverse=False):
temp = label.copy()
if inverse:
for v, k in self.label_mapping.items():
label[temp == k] = v
else:
for k, v in self.label_mapping.items():
label[temp == k] = v
return label
def __getitem__(self, index):
item = self.files[index]
image = cv2.imread(os.path.join(self.root, item["img"]),
cv2.IMREAD_COLOR)
label = cv2.imread(os.path.join(self.root, item["label"]),
cv2.IMREAD_GRAYSCALE)
label = self.convert_label(label)
image, label = self.gen_sample(image, label)
return image.copy(), label.copy()
def gen_sample(self, image, label):
label = self.label_transform(label)
# image = image.transpose((2, 0, 1))
if self.downsample_rate != 1:
label = cv2.resize(
label,
None,
fx=self.downsample_rate,
fy=self.downsample_rate,
interpolation=cv2.INTER_NEAREST
)
return image, label
def label_transform(self, label):
return np.array(label).astype('int32')
def run_onnx_inference(ort_session, img):
"""Infer an image with onnx seession
Args:
ort_session: Onnx session
img (ndarray): Image to be infered.
Returns:
ndarray: Model inference result.
"""
pre_img, pad_h, pad_w = preprocess(img)
# transform chw into hwc format
img = np.expand_dims(pre_img, 0)
img = np.transpose(img, (0,2,3,1))
ort_inputs = {ort_session.get_inputs()[0].name: img}
o1 = ort_session.run(None, ort_inputs)[0]
h, w = o1.shape[-2:]
h_cut = int(h / INPUT_SIZE[0] * pad_h)
w_cut = int(w / INPUT_SIZE[1] * pad_w)
o1 = o1[..., :h - h_cut, :w - w_cut]
return o1
def testval(ort_session, root, list_path):
test_dataset = Cityscapes(
root=root,
list_path=list_path,
num_classes=NUM_CLASSES,
ignore_label=IGNORE_LABEL,
downsample_rate=1)
testloader = torch.utils.data.DataLoader(
test_dataset,
batch_size=1,
shuffle=False,
num_workers=4,
pin_memory=True)
confusion_matrix = np.zeros(
(NUM_CLASSES, NUM_CLASSES))
for index, batch in enumerate(tqdm(testloader)):
image, label = batch
image = image.numpy()[0]
out = run_onnx_inference(ort_session, image)
size = label.size()
# for hwc output
out = out.transpose(0, 3, 1, 2)
if out.shape[2] != size[1] or out.shape[3] != size[2]:
out = torch.from_numpy(out).cpu()
pred = F.interpolate(
out, size=size[1:],
mode='bilinear'
)
confusion_matrix += get_confusion_matrix(
label,
pred,
size,
NUM_CLASSES,
IGNORE_LABEL)
pos = confusion_matrix.sum(1)
res = confusion_matrix.sum(0)
tp = np.diag(confusion_matrix)
pixel_acc = tp.sum()/pos.sum()
mean_acc = (tp/np.maximum(1.0, pos)).mean()
IoU_array = (tp / np.maximum(1.0, pos + res - tp))
mean_IoU = IoU_array.mean()
return mean_IoU, IoU_array, pixel_acc, mean_acc
if __name__ == "__main__":
onnx_path = args.onnx_model
root = args.root
list_path = args.list_path
if args.ipu:
providers = ["VitisAIExecutionProvider"]
provider_options = [{"config_file": args.provider_config}]
else:
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider']
provider_options = None
ort_session = onnxruntime.InferenceSession(onnx_path, providers=providers, provider_options=provider_options)
mean_IoU, IoU_array, pixel_acc, mean_acc = testval(ort_session, root, list_path)
msg = 'MeanIU: {: 4.4f}, Pixel_Acc: {: 4.4f}, Mean_Acc: {: 4.4f}'.format(mean_IoU, \
pixel_acc, mean_acc)
print(msg)
|