-
Notifications
You must be signed in to change notification settings - Fork 4
/
crf.py
49 lines (38 loc) · 1.34 KB
/
crf.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
import numpy as np
import pydensecrf.densecrf as dcrf
import pydensecrf.utils as utils
import torch
import torch.nn.functional as F
import torchvision.transforms.functional as VF
from scipy import ndimage
MAX_ITER = 10
POS_W = 7
POS_XY_STD = 3
Bi_W = 10
Bi_XY_STD = 50
Bi_RGB_STD = 5
def dense_crf(image, mask):
h, w = mask.shape
mask = mask.reshape(1, h, w)
fg = mask.astype(np.float)
bg = 1 - fg
output_logits = torch.from_numpy(np.concatenate((bg,fg), axis=0))
#image = np.array(VF.to_pil_image(image_tensor))[:, :, ::-1]
H, W = image.shape[:2]
image = np.ascontiguousarray(image)
output_logits = F.interpolate(output_logits.unsqueeze(0), size=(H, W), mode="bilinear",
align_corners=False).squeeze()
output_probs = F.softmax(output_logits, dim=0).cpu().numpy()
c = output_probs.shape[0]
h = output_probs.shape[1]
w = output_probs.shape[2]
U = utils.unary_from_softmax(output_probs)
U = np.ascontiguousarray(U)
d = dcrf.DenseCRF2D(w, h, c)
d.setUnaryEnergy(U)
d.addPairwiseGaussian(sxy=POS_XY_STD, compat=POS_W)
d.addPairwiseBilateral(sxy=Bi_XY_STD, srgb=Bi_RGB_STD, rgbim=image, compat=Bi_W)
Q = d.inference(MAX_ITER)
Q = np.array(Q).reshape((c, h, w))
MAP = np.argmax(Q, axis=0).reshape((h,w)).astype(np.float32)
return MAP