forked from edward-zhu/umaru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslider.lua
76 lines (58 loc) · 1.34 KB
/
slider.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
Slider = {
pos = 0,
total = 0,
stride = 0,
win_width = 0,
height = 0,
width = 0,
im = nil
}
setmetatable(Slider, {
__call =
function (cls, ...)
return cls:new(...)
end
})
function Slider:new(o, win_width, stride)
local o = o or {}
self.win_width = win_width or 10
self.stride = stride or self.win_width / 2
setmetatable(o, self)
self.__index = self
return o
end
function Slider:load(im)
assert(im:dim() == 2, "[Slider] a 2-dimensional tensor expected.")
self.height = im:size(1)
self.width = im:size(2)
self.total = math.ceil(self.width / self.stride) -- total # of windows
self.pos = 0
self.im = im
end
function Slider:slide()
assert(self.im, "[Slider] need to load a image before slide")
if self.pos >= self.total then
return nil
end
local _start = self.pos * self.stride + 1
local _end = _start + self.win_width
local ret
if _end > self.width then
ret = torch.zeros(self.height, self.win_width)
ret[{{1, self.height}, {1, self.width - _start + 1}}] = self.im[{{1, self.height}, {_start, self.width}}]
else
ret = self.im[{{1, self.height}, {_start, _start + self.win_width - 1}}]
end
self.pos = self.pos + 1
return ret
end
function Slider:genSequence()
local seq = {}
local s = self:slide()
while s do
s = s:reshape(s:nElement())
table.insert(seq, s)
s = self:slide()
end
return seq
end