This repository has been archived by the owner on Jul 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
doDetect.lua
187 lines (156 loc) · 5.05 KB
/
doDetect.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require('torch')
require('cunn')
require('image')
local minSize = 224
local threshold = 0.75
local className = {
'aeroplane',
'bicycle',
'bird',
'boat',
'bottle',
'bus',
'car',
'cat',
'chair',
'cow',
'diningtable',
'dog',
'horse',
'motorbike',
'person',
'pottedplant',
'sheep',
'sofa',
'train',
'tvmonitor'
}
local processImage = function(fileName)
local img2caffe = function(img)
local mean_pixel = torch.Tensor({103.939, 116.779, 123.68})
local perm = torch.LongTensor{3, 2, 1}
img = img:index(1, perm):mul(256.0)
mean_pixel = mean_pixel:view(3, 1, 1):expandAs(img)
img:add(-1, mean_pixel)
return img
end
local img = image.loadJPG(fileName)
local wid = img:size()[3]
local hei = img:size()[2]
local scale = 0
if ( wid > hei ) then
scale = minSize / hei
else
scale = minSize / wid
end
local newWid = scale * wid
local newHei = scale * hei
if ( newWid < minSize) then
newWid = minSize
end
if ( newHei < minSize ) then
newHei = minSize
end
newWid = newWid - (newWid % 32)
newHei = newHei - (newHei % 32)
local scaledImg = image.scale(img, newWid, newHei)
local targetImg = img2caffe(scaledImg)
return targetImg, scaledImg
end
local jaccardOverlap = function(b1, b2)
local xmin = math.min(b1.xmin, b2.xmin)
local ymin = math.min(b1.ymin, b2.ymin)
local xmax = math.max(b1.xmax, b2.xmax)
local ymax = math.max(b1.ymax, b2.ymax)
local w1 = b1.xmax - b1.xmin
local h1 = b1.ymax - b1.ymin
local w2 = b2.xmax - b2.xmin
local h2 = b2.ymax - b2.ymin
-- no overlap
if ( (xmax - xmin) >= ( w1 + w2) or
(ymax - ymin) >= ( h1 + h2) ) then
return 0
end
local andw = w1 + w2 - (xmax - xmin)
local andh = h1 + h2 - (ymax - ymin)
local andArea = andw * andh
local orArea = w1 * h1 + w2*h2 - andArea
return andArea / orArea
end
-- init
torch.setdefaulttensortype('torch.FloatTensor')
cutorch.setDevice(2)
local _ = require('./model.lua')
local modelInfo = _.info
local fixedCNN = _.fixedCNN
local boxSampling = require('boxsampling')
local xinput, origin = processImage( arg[2])
_ = xinput:size()
local targetWidth = _[3]
local targetHeight = _[2]
local predBoxes = boxSampling( modelInfo, targetWidth, targetHeight)
local featureCNN = torch.load(arg[1])
featureCNN:evaluate()
fixedCNN:cuda()
featureCNN:cuda()
local xfixed = fixedCNN:forward(xinput:cuda())
local yout = featureCNN:forward(xfixed)
local maxBoxes = {}
local _ = modelInfo.getSize(targetWidth, targetHeight)
local lastWidth = _[1]
local lastHeight = _[2]
for i = 1, #modelInfo.boxes do
local wid = lastWidth - (modelInfo.boxes[i][1] - 1)
local hei = lastHeight - (modelInfo.boxes[i][2] - 1)
local conf = yout[(i-1)*2 + 1]:float()
local loc = yout[(i-1)*2 + 2]:float()
for h = 1,hei do
for w = 1,wid do
local ii = "_" .. i .. "_" .. h .. "_" .. w
local pbox = predBoxes[ii]
local smf = conf[{{}, h, w}]:reshape(modelInfo.classNumber)
local v,_ = smf:max(1)
if ( _[1] ~= modelInfo.classNumber and math.exp(v[1]) > threshold ) then
local box = {}
box.v = math.exp(v[1])
box.c = _[1]
box.xmin = loc[1][h][w]*16 + pbox.xmin
box.ymin = loc[2][h][w]*16 + pbox.ymin
box.xmax = loc[3][h][w]*16 + pbox.xmax
box.ymax = loc[4][h][w]*16 + pbox.ymax
local isNew = true
local removed = {}
for j = 1, #maxBoxes do
if ( maxBoxes[j].c == box.c ) then
overlap = jaccardOverlap(box, maxBoxes[j])
if ( overlap > 0.4) then
if ( box.v > maxBoxes[j].v ) then
table.insert(removed, j)
else
isNew = false
break
end
end
end
end
if isNew then
box.xmin = math.max(box.xmin, 0)
box.xmax = math.min(box.xmax, targetWidth)
box.ymin = math.max(box.ymin, 0)
box.ymax = math.min(box.ymax, targetHeight)
table.insert(maxBoxes, box)
for j = 1,#removed do
table.remove(maxBoxes, removed[j] - j + 1)
end
end
end
end
end
end
print(maxBoxes)
local img = origin
for i = 1, #maxBoxes do
img = image.drawRect(img, maxBoxes[i].xmin, maxBoxes[i].ymin, maxBoxes[i].xmax, maxBoxes[i].ymax)
img = image.drawText(img, className[maxBoxes[i].c], maxBoxes[i].xmin+10, (maxBoxes[i].ymin + maxBoxes[i].ymax) / 2)
end
image.save('result.jpg', img);