-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathptest.lua
71 lines (58 loc) · 1.64 KB
/
ptest.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
--[[ precision-test
Compare precision of hardware and software implementation
run with: qlua precision-network.lua
--]]
require 'nn'
require 'pl'
require 'image'
gl = require 'libglconv'
torch.setdefaulttensortype('torch.FloatTensor')
local iC = 3 -- input channel
local iH = 128 -- input size
local iW = iH
local kC = 4 -- nb kernels
local kH = 3 -- kernel size
local kW = kH
local pH = 2 -- pool size
local pW = pH
-- define network and weights
network = nn.Sequential()
network:add(nn.SpatialConvolutionMM(iC, kC, kH, kW))
for i = 1, kC do
network.modules[1].weight[i]:fill(0.01*i)
end
network.modules[1].bias:fill(0)
-- use lena as an input
local lena = image.scale(image.lena(),iW,iH)
-- parse network
dst_hw = torch.Tensor(kC, iH, iW)
filt = torch.Tensor(kC, 3, 3, 3)
for i = 1, kC do
filt[i]:fill(0.01 * i)
end
local dst_sw = network:forward(lena)
function nn.SpatialConvolutionMM:updateOutput(input)
if self.weight:dim() == 2 then
self.weight = self.weight:view(self.nOutputPlane, self.nInputPlane, self.kH, self.kW)
end
gl.precision(1)
gl.logging(1)
gl.conv(input, self.weight, self.output, self.bias)
return self.output
end
dst_hw = network:forward(lena)
-- print output
print('==> Precision test')
local precision = 5
local coordinate = 20
local function trunc(x)
return math.floor(x*math.pow(10,precision)+.5)/math.pow(10,precision)
end
for i = 1, kC do
local sw = trunc(dst_sw[i][coordinate][coordinate])
local hw = trunc(dst_hw[i][coordinate][coordinate])
local diff = trunc(math.abs(sw-hw))
print('output['..i..']: ', 'CPU = ', sw, 'GPU = ', hw, 'DIFF = ', diff)
end
-- display output
image.display(dst_hw)