Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test_warp.lua « test - github.com/torch/image.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb0a88e253f1e11e4c2686e20f3d8b3527ef3c3d (plain)
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
require 'image'
torch.setdefaulttensortype('torch.FloatTensor')
torch.setnumthreads(16)

im = image.lena()
-- Subsample lena like crazy
im = image.scale(im, im:size()[3] / 8, im:size()[2] / 8, 'bilinear')

width = im:size()[3]  -- 512 / 8
height = im:size()[2]  -- 512 / 8
nchan = im:size()[1]  -- 3
upscale = 8
width_up = width * upscale
height_up = height * upscale

-- ******************************************
-- COMPARE RESULTS OF UPSCALE (INTERPOLATION)
-- ******************************************

-- x/y grids
grid_y = torch.ger( torch.linspace(-1,1,height_up), torch.ones(width_up) )
grid_x = torch.ger( torch.ones(height_up), torch.linspace(-1,1,width_up) )

flow = torch.FloatTensor()
flow:resize(2,height_up,width_up)
flow:zero()

-- Apply scale
flow_scale = torch.FloatTensor()
flow_scale:resize(2,height_up,width_up)
flow_scale[1] = grid_y
flow_scale[2] = grid_x
flow_scale[1]:add(1):mul(0.5) -- 0 to 1
flow_scale[2]:add(1):mul(0.5) -- 0 to 1
flow_scale[1]:mul(height-1)
flow_scale[2]:mul(width-1)
flow:add(flow_scale)

t0 = sys.clock()
im_simple = image.warp(im, flow, 'simple', false)
t1 = sys.clock()
print("Upscale Time simple = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_simple, zoom = 1, legend = 'upscale simple'}

t0 = sys.clock()
im_bilinear = image.warp(im, flow, 'bilinear', false)
t1 = sys.clock()
print("Upscale Time bilinear = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_bilinear, zoom = 1, legend = 'upscale bilinear'}

t0 = sys.clock()
im_bicubic = image.warp(im, flow, 'bicubic', false)
t1 = sys.clock()
print("Upscale Time bicubic = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_bicubic, zoom = 1, legend = 'upscale bicubic'}

t0 = sys.clock()
im_lanczos = image.warp(im, flow, 'lanczos', false)
t1 = sys.clock()
print("Upscale Time lanczos = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_lanczos, zoom = 1, legend = 'upscale lanczos'}

-- *********************************************
-- NOW TRY A ROTATION AT THE STANDARD RESOLUTION
-- *********************************************

im = image.lena()
-- Subsample lena a little bit
im = image.scale(im, im:size()[3] / 4, im:size()[2] / 4, 'bilinear')

width = im:size()[3]  -- 512 / 4
height = im:size()[2]  -- 512 / 4
nchan = im:size()[1]  -- 3

grid_y = torch.ger( torch.linspace(-1,1,height), torch.ones(width) )
grid_x = torch.ger( torch.ones(height), torch.linspace(-1,1,width) )

flow = torch.FloatTensor()
flow:resize(2,height,width)
flow:zero()

-- Apply uniform scale
flow_scale = torch.FloatTensor()
flow_scale:resize(2,height,width)
flow_scale[1] = grid_y
flow_scale[2] = grid_x
flow_scale[1]:add(1):mul(0.5) -- 0 to 1
flow_scale[2]:add(1):mul(0.5) -- 0 to 1
flow_scale[1]:mul(height-1)
flow_scale[2]:mul(width-1)
flow:add(flow_scale)

flow_rot = torch.FloatTensor()
flow_rot:resize(2,height,width)
flow_rot[1] = grid_y * ((height-1)/2) * -1
flow_rot[2] = grid_x * ((width-1)/2) * -1
view = flow_rot:reshape(2,height*width)
function rmat(deg)
  local r = deg/180*math.pi
  return torch.FloatTensor{{math.cos(r), -math.sin(r)}, {math.sin(r), math.cos(r)}}
end
rot_angle = 360/7  -- a nice non-integer value
rotmat = rmat(rot_angle)
flow_rotr = torch.mm(rotmat, view)
flow_rot = flow_rot - flow_rotr:reshape( 2, height, width )
flow:add(flow_rot)

t0 = sys.clock()
im_simple = image.warp(im, flow, 'simple', false)
t1 = sys.clock()
print("Rotation Time simple = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_simple, zoom = 4, legend = 'rotation simple'}

t0 = sys.clock()
im_bilinear = image.warp(im, flow, 'bilinear', false)
t1 = sys.clock()
print("Rotation Time bilinear = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_bilinear, zoom = 4, legend = 'rotation bilinear'}

t0 = sys.clock()
im_bicubic = image.warp(im, flow, 'bicubic', false)
t1 = sys.clock()
print("Rotation Time bicubic = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_bicubic, zoom = 4, legend = 'rotation bicubic'}

t0 = sys.clock()
im_lanczos = image.warp(im, flow, 'lanczos', false)
t1 = sys.clock()
print("Rotation Time lanczos = " .. (t1 - t0))  -- Not a robust measure (should average)
image.display{image = im_lanczos, zoom = 4, legend = 'rotation lanczos'}

im_lanczos = image.warp(im, flow, 'lanczos', false, 'pad')
image.display{image = im_lanczos, zoom = 4, legend = 'rotation lanczos (default pad)'}

im_lanczos = image.warp(im, flow, 'lanczos', false, 'pad', 1)
image.display{image = im_lanczos, zoom = 4, legend = 'rotation lanczos (pad 1)'}

image.display{image = im, zoom = 4, legend = 'source image'}

-- ***********************************************
-- NOW MAKE SURE BOTH FLOAT AND DOUBLE INPUTS WORK
-- ***********************************************

mtx = torch.zeros(2, 32, 32)
out = image.warp(im:float(), mtx:float(), 'bilinear')
out = image.warp(im:double(), mtx:double(), 'bilinear')