Spaces:
Sleeping
Sleeping
Update tools/ai/demo_utils.py
Browse files- tools/ai/demo_utils.py +110 -110
tools/ai/demo_utils.py
CHANGED
@@ -1,111 +1,111 @@
|
|
1 |
-
import cv2
|
2 |
-
import random
|
3 |
-
import numpy as np
|
4 |
-
|
5 |
-
from PIL import Image
|
6 |
-
|
7 |
-
def get_strided_size(orig_size, stride):
|
8 |
-
return ((orig_size[0]-1)//stride+1, (orig_size[1]-1)//stride+1)
|
9 |
-
|
10 |
-
def get_strided_up_size(orig_size, stride):
|
11 |
-
strided_size = get_strided_size(orig_size, stride)
|
12 |
-
return strided_size[0]*stride, strided_size[1]*stride
|
13 |
-
|
14 |
-
def imshow(image, delay=0, mode='RGB', title='show'):
|
15 |
-
if mode == 'RGB':
|
16 |
-
demo_image = image[..., ::-1]
|
17 |
-
else:
|
18 |
-
demo_image = image
|
19 |
-
|
20 |
-
cv2.imshow(title, demo_image)
|
21 |
-
if delay >= 0:
|
22 |
-
cv2.waitKey(delay)
|
23 |
-
|
24 |
-
def transpose(image):
|
25 |
-
return image.transpose((1, 2, 0))
|
26 |
-
|
27 |
-
def denormalize(image, mean=None, std=None, dtype=np.uint8, tp=True):
|
28 |
-
if tp:
|
29 |
-
image = transpose(image)
|
30 |
-
|
31 |
-
if mean is not None:
|
32 |
-
image = (image * std) + mean
|
33 |
-
|
34 |
-
if dtype == np.uint8:
|
35 |
-
image *= 255.
|
36 |
-
return image.astype(np.uint8)
|
37 |
-
else:
|
38 |
-
return image
|
39 |
-
|
40 |
-
def colormap(cam, shape=None, mode=cv2.COLORMAP_JET):
|
41 |
-
if shape is not None:
|
42 |
-
h, w, c = shape
|
43 |
-
cam = cv2.resize(cam, (w, h))
|
44 |
-
cam = cv2.applyColorMap(cam, mode)
|
45 |
-
return cam
|
46 |
-
|
47 |
-
def decode_from_colormap(data, colors):
|
48 |
-
ignore = (data == 255).astype(np.int32)
|
49 |
-
|
50 |
-
mask = 1 - ignore
|
51 |
-
data *= mask
|
52 |
-
|
53 |
-
h, w = data.shape
|
54 |
-
image = colors[data.reshape((h * w))].reshape((h, w, 3))
|
55 |
-
|
56 |
-
ignore = np.concatenate([ignore[..., np.newaxis], ignore[..., np.newaxis], ignore[..., np.newaxis]], axis=-1)
|
57 |
-
image[ignore.astype(
|
58 |
-
return image
|
59 |
-
|
60 |
-
def normalize(cam, epsilon=1e-5):
|
61 |
-
cam = np.maximum(cam, 0)
|
62 |
-
max_value = np.max(cam, axis=(0, 1), keepdims=True)
|
63 |
-
return np.maximum(cam - epsilon, 0) / (max_value + epsilon)
|
64 |
-
|
65 |
-
def crf_inference(img, probs, t=10, scale_factor=1, labels=21):
|
66 |
-
import pydensecrf.densecrf as dcrf
|
67 |
-
from pydensecrf.utils import unary_from_softmax
|
68 |
-
|
69 |
-
h, w = img.shape[:2]
|
70 |
-
n_labels = labels
|
71 |
-
|
72 |
-
d = dcrf.DenseCRF2D(w, h, n_labels)
|
73 |
-
|
74 |
-
unary = unary_from_softmax(probs)
|
75 |
-
unary = np.ascontiguousarray(unary)
|
76 |
-
|
77 |
-
d.setUnaryEnergy(unary)
|
78 |
-
d.addPairwiseGaussian(sxy=3/scale_factor, compat=3)
|
79 |
-
d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
|
80 |
-
Q = d.inference(t)
|
81 |
-
|
82 |
-
return np.array(Q).reshape((n_labels, h, w))
|
83 |
-
|
84 |
-
def crf_with_alpha(ori_image, cams, alpha):
|
85 |
-
# h, w, c -> c, h, w
|
86 |
-
# cams = cams.transpose((2, 0, 1))
|
87 |
-
|
88 |
-
bg_score = np.power(1 - np.max(cams, axis=0, keepdims=True), alpha)
|
89 |
-
bgcam_score = np.concatenate((bg_score, cams), axis=0)
|
90 |
-
|
91 |
-
cams_with_crf = crf_inference(ori_image, bgcam_score, labels=bgcam_score.shape[0])
|
92 |
-
# return cams_with_crf.transpose((1, 2, 0))
|
93 |
-
return cams_with_crf
|
94 |
-
|
95 |
-
def crf_inference_label(img, labels, t=10, n_labels=21, gt_prob=0.7):
|
96 |
-
import pydensecrf.densecrf as dcrf
|
97 |
-
from pydensecrf.utils import unary_from_labels
|
98 |
-
|
99 |
-
h, w = img.shape[:2]
|
100 |
-
|
101 |
-
d = dcrf.DenseCRF2D(w, h, n_labels)
|
102 |
-
|
103 |
-
unary = unary_from_labels(labels, n_labels, gt_prob=gt_prob, zero_unsure=False)
|
104 |
-
|
105 |
-
d.setUnaryEnergy(unary)
|
106 |
-
d.addPairwiseGaussian(sxy=3, compat=3)
|
107 |
-
d.addPairwiseBilateral(sxy=50, srgb=5, rgbim=np.ascontiguousarray(np.copy(img)), compat=10)
|
108 |
-
|
109 |
-
q = d.inference(t)
|
110 |
-
|
111 |
return np.argmax(np.array(q).reshape((n_labels, h, w)), axis=0)
|
|
|
1 |
+
import cv2
|
2 |
+
import random
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
def get_strided_size(orig_size, stride):
|
8 |
+
return ((orig_size[0]-1)//stride+1, (orig_size[1]-1)//stride+1)
|
9 |
+
|
10 |
+
def get_strided_up_size(orig_size, stride):
|
11 |
+
strided_size = get_strided_size(orig_size, stride)
|
12 |
+
return strided_size[0]*stride, strided_size[1]*stride
|
13 |
+
|
14 |
+
def imshow(image, delay=0, mode='RGB', title='show'):
|
15 |
+
if mode == 'RGB':
|
16 |
+
demo_image = image[..., ::-1]
|
17 |
+
else:
|
18 |
+
demo_image = image
|
19 |
+
|
20 |
+
cv2.imshow(title, demo_image)
|
21 |
+
if delay >= 0:
|
22 |
+
cv2.waitKey(delay)
|
23 |
+
|
24 |
+
def transpose(image):
|
25 |
+
return image.transpose((1, 2, 0))
|
26 |
+
|
27 |
+
def denormalize(image, mean=None, std=None, dtype=np.uint8, tp=True):
|
28 |
+
if tp:
|
29 |
+
image = transpose(image)
|
30 |
+
|
31 |
+
if mean is not None:
|
32 |
+
image = (image * std) + mean
|
33 |
+
|
34 |
+
if dtype == np.uint8:
|
35 |
+
image *= 255.
|
36 |
+
return image.astype(np.uint8)
|
37 |
+
else:
|
38 |
+
return image
|
39 |
+
|
40 |
+
def colormap(cam, shape=None, mode=cv2.COLORMAP_JET):
|
41 |
+
if shape is not None:
|
42 |
+
h, w, c = shape
|
43 |
+
cam = cv2.resize(cam, (w, h))
|
44 |
+
cam = cv2.applyColorMap(cam, mode)
|
45 |
+
return cam
|
46 |
+
|
47 |
+
def decode_from_colormap(data, colors):
|
48 |
+
ignore = (data == 255).astype(np.int32)
|
49 |
+
|
50 |
+
mask = 1 - ignore
|
51 |
+
data *= mask
|
52 |
+
|
53 |
+
h, w = data.shape
|
54 |
+
image = colors[data.reshape((h * w))].reshape((h, w, 3))
|
55 |
+
|
56 |
+
ignore = np.concatenate([ignore[..., np.newaxis], ignore[..., np.newaxis], ignore[..., np.newaxis]], axis=-1)
|
57 |
+
image[ignore.astype(bool)] = 255
|
58 |
+
return image
|
59 |
+
|
60 |
+
def normalize(cam, epsilon=1e-5):
|
61 |
+
cam = np.maximum(cam, 0)
|
62 |
+
max_value = np.max(cam, axis=(0, 1), keepdims=True)
|
63 |
+
return np.maximum(cam - epsilon, 0) / (max_value + epsilon)
|
64 |
+
|
65 |
+
def crf_inference(img, probs, t=10, scale_factor=1, labels=21):
|
66 |
+
import pydensecrf.densecrf as dcrf
|
67 |
+
from pydensecrf.utils import unary_from_softmax
|
68 |
+
|
69 |
+
h, w = img.shape[:2]
|
70 |
+
n_labels = labels
|
71 |
+
|
72 |
+
d = dcrf.DenseCRF2D(w, h, n_labels)
|
73 |
+
|
74 |
+
unary = unary_from_softmax(probs)
|
75 |
+
unary = np.ascontiguousarray(unary)
|
76 |
+
|
77 |
+
d.setUnaryEnergy(unary)
|
78 |
+
d.addPairwiseGaussian(sxy=3/scale_factor, compat=3)
|
79 |
+
d.addPairwiseBilateral(sxy=80/scale_factor, srgb=13, rgbim=np.copy(img), compat=10)
|
80 |
+
Q = d.inference(t)
|
81 |
+
|
82 |
+
return np.array(Q).reshape((n_labels, h, w))
|
83 |
+
|
84 |
+
def crf_with_alpha(ori_image, cams, alpha):
|
85 |
+
# h, w, c -> c, h, w
|
86 |
+
# cams = cams.transpose((2, 0, 1))
|
87 |
+
|
88 |
+
bg_score = np.power(1 - np.max(cams, axis=0, keepdims=True), alpha)
|
89 |
+
bgcam_score = np.concatenate((bg_score, cams), axis=0)
|
90 |
+
|
91 |
+
cams_with_crf = crf_inference(ori_image, bgcam_score, labels=bgcam_score.shape[0])
|
92 |
+
# return cams_with_crf.transpose((1, 2, 0))
|
93 |
+
return cams_with_crf
|
94 |
+
|
95 |
+
def crf_inference_label(img, labels, t=10, n_labels=21, gt_prob=0.7):
|
96 |
+
import pydensecrf.densecrf as dcrf
|
97 |
+
from pydensecrf.utils import unary_from_labels
|
98 |
+
|
99 |
+
h, w = img.shape[:2]
|
100 |
+
|
101 |
+
d = dcrf.DenseCRF2D(w, h, n_labels)
|
102 |
+
|
103 |
+
unary = unary_from_labels(labels, n_labels, gt_prob=gt_prob, zero_unsure=False)
|
104 |
+
|
105 |
+
d.setUnaryEnergy(unary)
|
106 |
+
d.addPairwiseGaussian(sxy=3, compat=3)
|
107 |
+
d.addPairwiseBilateral(sxy=50, srgb=5, rgbim=np.ascontiguousarray(np.copy(img)), compat=10)
|
108 |
+
|
109 |
+
q = d.inference(t)
|
110 |
+
|
111 |
return np.argmax(np.array(q).reshape((n_labels, h, w)), axis=0)
|