forked from anuragranj/spynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPECriterion.lua
executable file
·68 lines (48 loc) · 1.82 KB
/
EPECriterion.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
-- Copyright 2016 Anurag Ranjan and the Max Planck Gesellschaft.
-- All rights reserved.
-- This software is provided for research purposes only.
-- By using this software you agree to the terms of the license file
-- in the root folder.
-- For commercial use, please contact [email protected].
local EPECriterion, parent = torch.class('nn.EPECriterion', 'nn.Criterion')
-- Computes average endpoint error for batchSize x ChannelSize x Height x Width
-- flow fields or general multidimensional matrices.
local eps = 1e-12
function EPECriterion:__init()
parent.__init(self)
self.sizeAverage = true
end
function EPECriterion:updateOutput(input, target)
assert( input:nElement() == target:nElement(),
"input and target size mismatch")
self.buffer = self.buffer or input.new()
local buffer = self.buffer
local output
local npixels
buffer:resizeAs(input)
npixels = input:nElement()/2 -- 2 channel flow fields
buffer:add(input, -1, target):pow(2)
output = torch.sum(buffer,2):sqrt() -- second channel is flow
output = output:sum()
output = output / npixels
self.output = output
return self.output
end
function EPECriterion:updateGradInput(input, target)
assert( input:nElement() == target:nElement(),
"input and target size mismatch")
self.buffer = self.buffer or input.new()
local buffer = self.buffer
local gradInput = self.gradInput
local npixels
local loss
buffer:resizeAs(input)
npixels = input:nElement()/2
buffer:add(input, -1, target):pow(2)
loss = torch.sum(buffer,2):sqrt():add(eps) -- forms the denominator
loss = torch.cat(loss, loss, 2) -- Repeat tensor to scale the gradients
gradInput:resizeAs(input)
gradInput:add(input, -1, target):cdiv(loss)
gradInput = gradInput / npixels
return gradInput
end