forked from onnx/models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duc-postprocess.py
70 lines (57 loc) · 2.45 KB
/
duc-postprocess.py
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
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import cv2 as cv
from PIL import Image
import cityscapes_labels
def get_palette():
# get train id to color mappings from file
trainId2colors = {label.trainId: label.color for label in cityscapes_labels.labels}
# prepare and return palette
palette = [0] * 256 * 3
for trainId in trainId2colors:
colors = trainId2colors[trainId]
if trainId == 255:
colors = (0, 0, 0)
for i in range(3):
palette[trainId * 3 + i] = colors[i]
return palette
def colorize(labels):
# generate colorized image from output labels and color palette
result_img = Image.fromarray(labels).convert('P')
result_img.putpalette(get_palette())
return np.array(result_img.convert('RGB'))
def postprocess(labels,img_shape,result_shape):
'''
Postprocessing function for DUC
input : output labels from the network as numpy array, input image shape, desired output image shape
output : confidence score, segmented image, blended image, raw segmentation labels
'''
ds_rate = 8
label_num = 19
cell_width = 2
img_height,img_width = img_shape
result_height,result_width = result_shape
# re-arrange output
test_width = int((int(img_width) / ds_rate) * ds_rate)
test_height = int((int(img_height) / ds_rate) * ds_rate)
feat_width = int(test_width / ds_rate)
feat_height = int(test_height / ds_rate)
labels = labels.reshape((label_num, 4, 4, feat_height, feat_width))
labels = np.transpose(labels, (0, 3, 1, 4, 2))
labels = labels.reshape((label_num, int(test_height / cell_width), int(test_width / cell_width)))
labels = labels[:, :int(img_height / cell_width),:int(img_width / cell_width)]
labels = np.transpose(labels, [1, 2, 0])
labels = cv.resize(labels, (result_width, result_height), interpolation=cv.INTER_LINEAR)
labels = np.transpose(labels, [2, 0, 1])
# get softmax output
softmax = labels
# get classification labels
results = np.argmax(labels, axis=0).astype(np.uint8)
raw_labels = results
# comput confidence score
confidence = float(np.max(softmax, axis=0).mean())
# generate segmented image
result_img = Image.fromarray(colorize(raw_labels)).resize(result_shape[::-1])
# generate blended image
blended_img = Image.fromarray(cv.addWeighted(im[:, :, ::-1], 0.5, np.array(result_img), 0.5, 0))
return confidence, result_img, blended_img, raw_labels