-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckSalient.lua
More file actions
164 lines (146 loc) · 4.35 KB
/
checkSalient.lua
File metadata and controls
164 lines (146 loc) · 4.35 KB
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
require 'image';
require 'torch';
require 'nn';
require 'cutorch';
require 'cunn';
cmd = torch.CmdLine()
cmd:option('-max', 10)
cmd:option('-device', 1)
params = cmd:parse(arg)
cutorch.setDevice(params['device'])
cutorch.synchronize()
testset = torch.load('../FaceScrub/FaceScrub_testset_128x128')
trainset = torch.load('../FaceScrub/FaceScrub_trainset_128x128')
generator = torch.load('./generatorFace.net')
discriminator = torch.load('./discriminatorFace.net')
salientTest = torch.Tensor( params['max'] ):zero()
salientTrain = torch.Tensor( params['max'] ):zero()
lamda_rec = 0.30
lamda_adv = 1 - lamda_rec
criterion_rec = nn.MSECriterion():cuda()
criterion_adv = nn.BCECriterion():cuda()
M = torch.CudaTensor( 2, 3, 128, 128 ):fill(1) -- filling mask
M[{{},{},{33, 96},{33, 96}}]:zero()
maskedInput = torch.CudaTensor( 2, 3, 128, 128 )
targetImage = torch.CudaTensor( 2, 3, 64, 64 )
stitch = function( small, large )
large[{{}, {}, {33, 96}, {33, 96}}] = small[{{}, {}, {1, 64}, {1, 64}}]
return large
end
function insert( err, tree, x )
if tree == nil then
tree = {
index = x,
value = err,
left = nil,
right = nil
}
return tree
elseif tree['value'] < err then
tree['right'] = insert( err, tree['right'], x )
return tree
else
tree['left'] = insert( err, tree['left'], x )
return tree
end
end
function removeMin( tree )
if tree == nil then
return nil
end
if tree['left'] == nil then
local temp = tree['right']
tree = nil
collectgarbage()
return temp
else
tree['left'] = removeMin( tree['left'] )
return tree
end
end
function fillArr( a, tree )
if tree == nil then
return
end
if tree['left'] ~= nil then
fillArr( a, tree['left'] )
end
a[ current_index ] = tree['index']
current_index = current_index + 1
if tree['right'] ~= nil then
fillArr( a, tree['right'] )
end
return
end
root = nil
current_size = 0
for i = 1, trainset.size do
collectgarbage()
targetImage[1] = trainset.data[{{ i }, {}, {33, 96}, {33, 96}}]
targetImage[2] = targetImage[1]
maskedInput[1] = trainset.data[i]
maskedInput[2] = maskedInput[1]
targetLabel = torch.CudaTensor( 2 ):fill(1)
maskedInput:cuda()
targetImage:cuda()
maskedInput:cmul(M)
outputImage = generator:forward( maskedInput )
print( i )
err_rec = criterion_rec:forward( outputImage, targetImage )
err_adv = criterion_adv:forward( discriminator:forward( stitch( outputImage, maskedInput ) ), targetLabel )
err = err_rec * lamda_rec + err_adv * lamda_adv
if current_size == 0 then
current_size = 1
root = insert( err, root, i )
elseif current_size == params['max'] then
root = removeMin( root )
root = insert( err, root, i )
else
root = insert( err, root, i )
current_size = current_size + 1
end
end
current_index = 1
fillArr( salientTrain, root )
i = params['max']
while i > 0 do
name = params['max'] + 1 - i .. '_point_3_rankedTrainSalient.jpg'
image.save( name, trainset.data[salientTrain[i]] )
i = i - 1
end
root = nil
current_size = 0
for i = 1, testset.size do
collectgarbage()
targetImage[1] = testset.data[{{ i }, {}, {33, 96}, {33, 96}}]
targetImage[2] = targetImage[1]
maskedInput[1] = testset.data[i]
maskedInput[2] = maskedInput[1]
targetLabel = torch.CudaTensor( 2 ):fill(1)
maskedInput:cuda()
targetImage:cuda()
maskedInput:cmul(M)
outputImage = generator:forward( maskedInput )
print( i )
err_rec = criterion_rec:forward( outputImage, targetImage )
err_adv = criterion_adv:forward( discriminator:forward( stitch( outputImage, maskedInput ) ), targetLabel )
err = err_rec * lamda_rec + err_adv * lamda_adv
if current_size == 0 then
current_size = 1
root = insert( err, root, i )
elseif current_size == params['max'] then
root = removeMin( root )
root = insert( err, root, i )
else
root = insert( err, root, i )
current_size = current_size + 1
end
end
current_index = 1
fillArr( salientTest, root )
i = params['max']
while i > 0 do
name = params['max'] - i + 1 .. '_point_3_rankedTestSalient.jpg'
image.save( name, testset.data[salientTest[i]] )
i = i - 1
end