forked from shenjianbing/PDB-ConvLSTM
-
Notifications
You must be signed in to change notification settings - Fork 4
/
davis_applyCRF.py
63 lines (47 loc) · 2.1 KB
/
davis_applyCRF.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
import pydensecrf.densecrf as dcrf
import numpy as np
import sys
from skimage.io import imread, imsave
from pydensecrf.utils import unary_from_labels, create_pairwise_bilateral, create_pairwise_gaussian, unary_from_softmax
from os import listdir, makedirs
from os.path import isfile, join
davis_path = '/your path/datasets/davis16-test/'
setting = '/your path/results/davis/'
out_folder = '/your path/results/davis-crf/'
for d in listdir(setting):
vidDir = join(davis_path, d)
resDir = join(out_folder, d)
if not os.path.exists(resDir):
os.makedirs(resDir)
for f in listdir(vidDir):
img = imread(join(vidDir, f))
segDir = join(setting, d)
frameName = str.split(f, '.')[0]
anno_rgb = imread(segDir + '/' + frameName + '.png').astype(np.uint32)
min_val = np.min(anno_rgb.ravel())
max_val = np.max(anno_rgb.ravel())
out = (anno_rgb.astype('float') - min_val) / (max_val - min_val)
labels = np.zeros((2, img.shape[0], img.shape[1]))
labels[1, :, :] = out
labels[0, :, :] = 1 - out
colors = [0, 255]
colorize = np.empty((len(colors), 1), np.uint8)
colorize[:,0] = colors
n_labels = 2
crf = dcrf.DenseCRF(img.shape[1] * img.shape[0], n_labels)
U = unary_from_softmax(labels)
crf.setUnaryEnergy(U)
feats = create_pairwise_gaussian(sdims=(3, 3), shape=img.shape[:2])
crf.addPairwiseEnergy(feats, compat=3,
kernel=dcrf.DIAG_KERNEL,
normalization=dcrf.NORMALIZE_SYMMETRIC)
feats = create_pairwise_bilateral(sdims=(60, 60), schan=(5, 5, 5),
img=img, chdim=2)
crf.addPairwiseEnergy(feats, compat=5,
kernel=dcrf.DIAG_KERNEL,
normalization=dcrf.NORMALIZE_SYMMETRIC)
Q = crf.inference(5)
MAP = np.argmax(Q, axis=0)
MAP = colorize[MAP]
imsave(resDir + '/' + frameName + '.png', MAP.reshape(anno_rgb.shape))
print ("Saving: " + resDir + '/' + frameName + '.png')