-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.lua
executable file
·172 lines (138 loc) · 3.93 KB
/
utils.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
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
165
166
167
168
169
170
171
require 'torch'
utils = {}
function utils.ind2sub(matsize, ndx)
matsize_tmp = torch.LongTensor(im_wl:size())
for i = 1,im_wl:size():size() do
matsize_tmp[i] = matsize[i]
end
matsize = matsize_tmp
cp = torch.cumprod(matsize)
sub = torch.zeros(matsize:size())
for i = cp:size()[1], 2, -1 do
vi = ((ndx-1) % cp[i-1]) + 1
vj = (ndx - vi)/cp[i-1] + 1
sub[i] = vj
ndx = vi
end
sub[1] = ndx;
return sub
end
function utils.alphanumsort(o)
-- Shamelessley lifted from
-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
-- grj 5/16/16
local function padnum(d) return ("%012d"):format(d) end
table.sort(o, function(a,b)
return tostring(a):gsub("%d+",padnum) < tostring(b):gsub("%d+",padnum) end)
return o
end
function utils.meshgrid(x, y)
local xx = torch.repeatTensor(x, y:size(1),1)
local yy = torch.repeatTensor(y:view(-1,1), 1, x:size(1))
return xx, yy
end
function utils.unique(input)
local b = {}
local range
-- print(type(input))
if type(input) == 'table' then
range = #input
else
range = input:numel()
end
local c = 0
for i = 1, range do
if b[input[i]] == nil then
c = c+1
b[input[i]] = c
end
end
local u_vals = {}
for i in pairs(b) do
table.insert(u_vals,i)
end
local inds = torch.zeros(range)
for i = 1, range do
inds[i] = b[input[i]]
end
u_vals_tmp = {}
for i = 1,#u_vals do
ind = torch.nonzero(torch.eq(inds, torch.FloatTensor(inds:size()[1]):fill(i)))[1][1]
u_vals_tmp[i] = input[ind]
end
u_vals = u_vals_tmp
return u_vals, inds
end
function utils.table2float(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[orig_key] = utils.table2float(orig_value)
end
setmetatable(copy, utils.table2float(getmetatable(orig)))
elseif orig_type == 'userdata' then -- number, string, boolean, etc
copy = orig:float()
end
return copy
end
function utils.table2cuda(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[orig_key] = utils.table2cuda(orig_value)
end
setmetatable(copy, utils.table2cuda(getmetatable(orig)))
elseif orig_type == 'userdata' then -- number, string, boolean, etc
copy = orig:cuda()
end
return copy
end
function utils.split(inputstr, sep)
-- liberated from http://stackoverflow.com/questions/1426954/split-string-in-lua
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function utils.normalize(tensor)
mu = torch.mean(tensor)
std = torch.std(tensor)
tensor_out = (torch.Tensor(tensor:size()):copy(tensor)-mu)/std
return tensor_out
end
function utils.shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value:clone()
end
else -- number, string, boolean, etc
copy = orig:clone()
end
return copy
end
function utils.deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[utils.deepcopy(orig_key)] = utils.deepcopy(orig_value)
end
setmetatable(copy, utils.deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig:clone()
end
return copy
end