-
Notifications
You must be signed in to change notification settings - Fork 7
/
dismap.py
156 lines (124 loc) · 6.6 KB
/
dismap.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
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
import torch
import getopt
import math
import numpy
import os
import PIL
import PIL.Image
import sys
import torch.nn.functional as F
import torchvision.transforms as transforms
import skimage.io as io
import numpy as np
from skimage import morphology
from scipy import ndimage
import math
transform = transforms.Compose([transforms.ToTensor()])
revtransf = transforms.Compose([transforms.ToPILImage()])
class Network(torch.nn.Module):
def __init__(self, gpu=None):
super(Network, self).__init__()
self.moduleVggOne = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggTwo = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggThr = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggFou = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=256, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleVggFiv = torch.nn.Sequential(
torch.nn.MaxPool2d(kernel_size=2, stride=2),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False),
torch.nn.Conv2d(in_channels=512, out_channels=512, kernel_size=3, stride=1, padding=1),
torch.nn.ReLU(inplace=False)
)
self.moduleScoreOne = torch.nn.Conv2d(in_channels=64, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreTwo = torch.nn.Conv2d(in_channels=128, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreThr = torch.nn.Conv2d(in_channels=256, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreFou = torch.nn.Conv2d(in_channels=512, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleScoreFiv = torch.nn.Conv2d(in_channels=512, out_channels=1, kernel_size=1, stride=1, padding=0)
self.moduleCombine = torch.nn.Sequential(
torch.nn.Conv2d(in_channels=5, out_channels=1, kernel_size=1, stride=1, padding=0),
torch.nn.Sigmoid()
)
self.gpu = gpu
if gpu is not None:
self.cuda(gpu)
# end
def forward(self, tensorInput):
if self.gpu is not None:
tensorInput = tensorInput.cuda(self.gpu)
tensorBlue = (tensorInput[:, 0:1, :, :] * 255.0) - 127.5
tensorGreen = (tensorInput[:, 1:2, :, :] * 255.0) - 127.5
tensorRed = (tensorInput[:, 2:3, :, :] * 255.0) - 127.5
tensorInput = torch.cat([ tensorBlue, tensorGreen, tensorRed ], 1)
vggOne = self.moduleVggOne(tensorInput)
vggTwo = self.moduleVggTwo(vggOne)
vggThr = self.moduleVggThr(vggTwo)
vggFou = self.moduleVggFou(vggThr)
vggFiv = self.moduleVggFiv(vggFou)
scoreOne = self.moduleScoreOne(vggOne)
scoreTwo = self.moduleScoreTwo(vggTwo)
scoreThr = self.moduleScoreThr(vggThr)
scoreFou = self.moduleScoreFou(vggFou)
scoreFiv = self.moduleScoreFiv(vggFiv)
H = tensorInput.size(2)
W = tensorInput.size(3)
scoreOne = torch.nn.functional.interpolate(input=scoreOne, size=(H, W), mode='bilinear', align_corners=False)
scoreTwo = torch.nn.functional.interpolate(input=scoreTwo, size=(H, W), mode='bilinear', align_corners=False)
scoreThr = torch.nn.functional.interpolate(input=scoreThr, size=(H, W), mode='bilinear', align_corners=False)
scoreFou = torch.nn.functional.interpolate(input=scoreFou, size=(H, W), mode='bilinear', align_corners=False)
scoreFiv = torch.nn.functional.interpolate(input=scoreFiv, size=(H, W), mode='bilinear', align_corners=False)
scoreFin = self.moduleCombine(torch.cat([ scoreOne, scoreTwo, scoreThr, scoreFou, scoreFiv ], 1))
return F.sigmoid(1 - scoreTwo)
##########################################################
torch.set_grad_enabled(False) # make sure to not compute gradients for computational performance
torch.backends.cudnn.enabled = True # make sure to use cudnn for computational performance
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
EdgeDet = Network(gpu = 0)
EdgeDet.load_state_dict(torch.load('./checkpoints/hed.pkl'))
framDir = './dataset/frame'
saveDir = './dataset/dismap'
for clipDir in sorted(os.listdir(framDir)):
clipPath = os.path.join(framDir, clipDir)
savePath = os.path.join(saveDir, clipDir)
if not os.path.exists(savePath):
os.mkdir(savePath)
for index, frame in enumerate(sorted(os.listdir(clipPath))):
inDir = '%s%s%s%s%s' % (framDir, '/', clipDir, '/', frame)
ouDir = '%s%s%s%s%s%s' % (saveDir, '/', clipDir, '/', frame.split('.')[0], '.npy')
imgt = io.imread(inDir).astype(np.float32)/255.0
imgt = transform(imgt)
imgt = imgt.unsqueeze(0).to(device)
edgmap = np.asarray(revtransf(EdgeDet(imgt).cpu()[0]))
edgmap = edgmap / 255.0
newmap = np.zeros(edgmap.shape)
newmap[edgmap > 1.0/(1 + math.exp(-0.5))] = 1.0
dismap = ndimage.distance_transform_edt(newmap)
np.save(ouDir, dismap)