-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhspDemo.lua
190 lines (153 loc) · 7.25 KB
/
hspDemo.lua
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'image'
require 'torch'
require 'nn'
require 'cutorch'
require 'cunn'
require 'optim'
require 'cudnn'
dofile('utils/instanceNormalization.lua')
local hsp = require 'hsp'
cutorch.setDevice(arg[1])
local networkFileName = arg[2]
local imageFileName = arg[3]
local numLevels = 5
local splitThreshold = 0.08
local featBlockBoundary1, featBlockBoundary2, featBlockBoundary3
local featBlockBoundary1 = 12
local featBlockBoundary2 = 9
local featBlockBoundary3 = 20
-- load the snapshot
net = torch.load(networkFileName)
net:evaluate()
-- load the image
local img = image.load(imageFileName)
img = image.scale(img, 128, 128)
if (img:size()[1] == 4) then
local alphaMask = img[4]:repeatTensor(3,1,1)
img = torch.cmul(img:narrow(1,1,3),alphaMask) + 1 - alphaMask
end
img = img:view(1,3,128,128):cuda()
local volSoftMax = cudnn.VolumetricSoftMax():cuda()
-- forward pass through the network
outputs = {}
outputMasks = {}
outputs[1] = torch.Tensor(1,3,16,16,16):cuda():zero()
outputMasks[1] = torch.Tensor(1,1,16,16,16):cuda():zero()
for l=2,numLevels-1 do
currSize = outputs[l-1]:size()
outputs[l] = torch.Tensor(currSize[1],currSize[2],2*currSize[3],2*currSize[4],2*currSize[5]):cuda():zero()
outputMasks[l] = torch.Tensor(currSize[1],1,2*currSize[3],2*currSize[4],2*currSize[5]):cuda():zero()
end
currSize = outputs[numLevels-1]:size()
outputs[numLevels] = torch.Tensor(currSize[1],1,2*currSize[3],2*currSize[4],2*currSize[5]):cuda():zero()
outputMasks[numLevels] = torch.Tensor(currSize[1],1,2*currSize[3],2*currSize[4],2*currSize[5]):cuda():zero()
-- run first stage
result = net:get(1):forward(img)
local function evaluateFull(output, x, y, z, level)
outputs[level][{{1,1},{},{16*x+1,16*(x+1)},{16*y+1,16*(y+1)},{16*z+1,16*(z+1)}}]:copy(output)
outputMasks[level][{{1,1},{},{16*x+1,16*(x+1)},{16*y+1,16*(y+1)},{16*z+1,16*(z+1)}}]:fill(1)
end
local function evaluateIntermediate(output, feature, x, y, z, level)
local outputSoftMax = volSoftMax:forward(output)
-- copy output
outputs[level][{{1,1},{},{16*x+1,16*(x+1)},{16*y+1,16*(y+1)},{16*z+1,16*(z+1)}}]:copy(output[{1,{1,3},{},{},{}}])
outputMasks[level][{{1,1},{},{16*x+1,16*(x+1)},{16*y+1,16*(y+1)},{16*z+1,16*(z+1)}}]:fill(1)
-- compute thresholds for splits
local maxVal1 = torch.max(outputSoftMax[{1,3,{1,8},{1,8},{1,8}}])
local maxVal2 = torch.max(outputSoftMax[{1,3,{1,8},{1,8},{9,16}}])
local maxVal3 = torch.max(outputSoftMax[{1,3,{1,8},{9,16},{1,8}}])
local maxVal4 = torch.max(outputSoftMax[{1,3,{1,8},{9,16},{9,16}}])
local maxVal5 = torch.max(outputSoftMax[{1,3,{9,16},{1,8},{1,8}}])
local maxVal6 = torch.max(outputSoftMax[{1,3,{9,16},{1,8},{9,16}}])
local maxVal7 = torch.max(outputSoftMax[{1,3,{9,16},{9,16},{1,8}}])
local maxVal8 = torch.max(outputSoftMax[{1,3,{9,16},{9,16},{9,16}}])
if (maxVal1 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{1,featBlockBoundary1},{1,featBlockBoundary1},{1,featBlockBoundary1}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x, 2*y, 2*z, level+1)
else
evaluateFull(result, 2*x, 2*y, 2*z, level+1)
end
end
if (maxVal2 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{1,featBlockBoundary1},{1,featBlockBoundary1},{featBlockBoundary2,featBlockBoundary3}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x, 2*y, 2*z+1, level+1)
else
evaluateFull(result, 2*x, 2*y, 2*z+1, level+1)
end
end
if (maxVal3 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{1,featBlockBoundary1},{featBlockBoundary2,featBlockBoundary3},{1,featBlockBoundary1}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x, 2*y+1, 2*z, level+1)
else
evaluateFull(result, 2*x, 2*y+1, 2*z, level+1)
end
end
if (maxVal4 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{1,featBlockBoundary1},{featBlockBoundary2,featBlockBoundary3},{featBlockBoundary2,featBlockBoundary3}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x, 2*y+1, 2*z+1, level+1)
else
evaluateFull(result, 2*x, 2*y+1, 2*z+1, level+1)
end
end
if (maxVal5 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{featBlockBoundary2,featBlockBoundary3},{1,featBlockBoundary1},{1,featBlockBoundary1}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x+1, 2*y, 2*z, level+1)
else
evaluateFull(result, 2*x+1, 2*y, 2*z, level+1)
end
end
if (maxVal6 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{featBlockBoundary2,featBlockBoundary3},{1,featBlockBoundary1},{featBlockBoundary2,featBlockBoundary3}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x+1, 2*y, 2*z+1, level+1)
else
evaluateFull(result, 2*x+1, 2*y, 2*z+1, level+1)
end
end
if (maxVal7 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{featBlockBoundary2,featBlockBoundary3},{featBlockBoundary2,featBlockBoundary3},{1,featBlockBoundary1}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x+1, 2*y+1, 2*z, level+1)
else
evaluateFull(result, 2*x+1, 2*y+1, 2*z, level+1)
end
end
if (maxVal8 > splitThreshold) then
local result = net:get(level+1):forward(feature[{{1,1},{},{featBlockBoundary2,featBlockBoundary3},{featBlockBoundary2,featBlockBoundary3},{featBlockBoundary2,featBlockBoundary3}}])
if (level < numLevels-1) then
evaluateIntermediate(result[1],result[2], 2*x+1, 2*y+1, 2*z+1, level+1)
else
evaluateFull(result, 2*x+1, 2*y+1, 2*z+1, level+1)
end
end
end
resultSingleOutput = result[1][{{1,1},{},{},{},{}}]
resultSingleFeature = result[2][{{1,1},{},{},{},{}}]
evaluateIntermediate(resultSingleOutput, resultSingleFeature, 0, 0, 0, 1)
local softMaxLayer = cudnn.VolumetricSoftMax():cuda()
outputsCompiled = {}
local upSampleLayer = nn.VolumetricFullConvolution(1,1,2,2,2,2,2,2,0,0,0)
upSampleLayer.bias:zero()
upSampleLayer.weight:fill(1)
upSampleLayer = upSampleLayer:cuda()
outputsCompiled = {}
outputsCompiled[1] = outputs[1]:clone()
local outputsCompiledColor = {}
for l=2,numLevels-1 do
outputsCompiled[l] = outputs[l]:clone()
local copyMask = torch.eq(outputMasks[l], 0):cuda()
outputsCompiled[l][{{},{1},{},{},{}}]:add(upSampleLayer:forward(outputsCompiled[l-1][{{},{1},{},{},{}}]):cmul(copyMask))
outputsCompiled[l][{{},{2},{},{},{}}]:add(upSampleLayer:forward(outputsCompiled[l-1][{{},{2},{},{},{}}]):cmul(copyMask))
outputsCompiled[l][{{},{3},{},{},{}}]:add(upSampleLayer:forward(outputsCompiled[l-1][{{},{3},{},{},{}}]):cmul(copyMask))
end
outputsCompiled[numLevels] = outputs[numLevels]:clone()
local copyMask = torch.eq(outputMasks[numLevels], 0):cuda()
local smOutputBefore = softMaxLayer:forward(outputsCompiled[numLevels-1])
local outputsCompiledTwoLabel = torch.add(smOutputBefore[{{},{2},{},{},{}}], smOutputBefore[{{},{3},{},{},{}}])
outputsCompiled[numLevels]:add(upSampleLayer:forward(outputsCompiledTwoLabel):cmul(copyMask))
hsp.saveMeshAsObj(outputsCompiled[numLevels][{1,1,{},{},{}}]:double(),0.2, "output.obj")