forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnn_test.go
453 lines (376 loc) · 11.1 KB
/
dnn_test.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package gocv
import (
"image"
"io/ioutil"
"os"
"testing"
)
func checkNet(t *testing.T, net Net) {
net.SetPreferableBackend(NetBackendDefault)
net.SetPreferableTarget(NetTargetCPU)
img := IMRead("images/space_shuttle.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in ReadNet test")
}
defer img.Close()
blob := BlobFromImage(img, 1.0, image.Pt(224, 224), NewScalar(0, 0, 0, 0), false, false)
if blob.Empty() {
t.Error("Invalid blob in ReadNet test")
}
defer blob.Close()
net.SetInput(blob, "data")
layer := net.GetLayer(0)
defer layer.Close()
if layer.InputNameToIndex("notthere") != -1 {
t.Error("Invalid layer in ReadNet test")
}
if layer.OutputNameToIndex("notthere") != -1 {
t.Error("Invalid layer in ReadNet test")
}
if layer.GetName() != "_input" {
t.Errorf("Invalid layer name in ReadNet test: %s\n", layer.GetName())
}
if layer.GetType() != "" {
t.Errorf("Invalid layer type in ReadNet test: %s\n", layer.GetType())
}
ids := net.GetUnconnectedOutLayers()
if len(ids) != 1 {
t.Errorf("Invalid len output layers in ReadNet test: %d\n", len(ids))
}
lnames := net.GetLayerNames()
if len(lnames) != 142 {
t.Errorf("Invalid len layer names in ReadNet test: %d\n", len(lnames))
}
prob := net.ForwardLayers([]string{"prob"})
if len(prob) == 0 {
t.Error("Invalid len prob in ReadNet test")
}
if prob[0].Empty() {
t.Error("Invalid prob[0] in ReadNet test")
}
probMat := prob[0].Reshape(1, 1)
defer probMat.Close()
_, maxVal, minLoc, maxLoc := MinMaxLoc(probMat)
if round(float64(maxVal), 0.00005) != 0.9998 {
t.Errorf("ReadNet maxVal incorrect: %v\n", round(float64(maxVal), 0.00005))
}
if minLoc.X != 955 || minLoc.Y != 0 {
t.Errorf("ReadNet minLoc incorrect: %v\n", minLoc)
}
if maxLoc.X != 812 || maxLoc.Y != 0 {
t.Errorf("ReadNet maxLoc incorrect: %v\n", maxLoc)
}
perf := net.GetPerfProfile()
if perf == 0 {
t.Error("ReadNet GetPerfProfile error")
}
}
func TestReadNetDisk(t *testing.T) {
path := os.Getenv("GOCV_CAFFE_TEST_FILES")
if path == "" {
t.Skip("Unable to locate Caffe model files for tests")
}
net := ReadNet(path+"/bvlc_googlenet.caffemodel", path+"/bvlc_googlenet.prototxt")
if net.Empty() {
t.Errorf("Unable to load Caffe model using ReadNet")
}
defer net.Close()
checkNet(t, net)
}
func TestReadNetMemory(t *testing.T) {
path := os.Getenv("GOCV_CAFFE_TEST_FILES")
if path == "" {
t.Skip("Unable to locate Caffe model files for tests")
}
bModel, err := ioutil.ReadFile(path + "/bvlc_googlenet.caffemodel")
if err != nil {
t.Errorf("Failed to load model from file: %v", err)
}
bConfig, err := ioutil.ReadFile(path + "/bvlc_googlenet.prototxt")
if err != nil {
t.Errorf("Failed to load config from file: %v", err)
}
net, err := ReadNetBytes("caffe", bModel, bConfig)
if err != nil {
t.Errorf("Failed to read net bytes: %v", err)
}
if net.Empty() {
t.Errorf("Unable to load Caffe model using ReadNetBytes")
}
defer net.Close()
checkNet(t, net)
}
func checkCaffeNet(t *testing.T, net Net) {
img := IMRead("images/space_shuttle.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in Caffe test")
}
defer img.Close()
blob := BlobFromImage(img, 1.0, image.Pt(224, 224), NewScalar(0, 0, 0, 0), false, false)
if blob.Empty() {
t.Error("Invalid blob in Caffe test")
}
defer blob.Close()
net.SetInput(blob, "data")
prob := net.Forward("prob")
defer prob.Close()
if prob.Empty() {
t.Error("Invalid prob in Caffe test")
}
probMat := prob.Reshape(1, 1)
defer probMat.Close()
_, maxVal, minLoc, maxLoc := MinMaxLoc(probMat)
if round(float64(maxVal), 0.00005) != 0.9998 {
t.Errorf("Caffe maxVal incorrect: %v\n", round(float64(maxVal), 0.00005))
}
if minLoc.X != 955 || minLoc.Y != 0 {
t.Errorf("Caffe minLoc incorrect: %v\n", minLoc)
}
if maxLoc.X != 812 || maxLoc.Y != 0 {
t.Errorf("Caffe maxLoc incorrect: %v\n", maxLoc)
}
}
func TestCaffeDisk(t *testing.T) {
path := os.Getenv("GOCV_CAFFE_TEST_FILES")
if path == "" {
t.Skip("Unable to locate Caffe model files for tests")
}
net := ReadNetFromCaffe(path+"/bvlc_googlenet.prototxt", path+"/bvlc_googlenet.caffemodel")
if net.Empty() {
t.Errorf("Unable to load Caffe model")
}
defer net.Close()
checkCaffeNet(t, net)
}
func TestCaffeMemory(t *testing.T) {
path := os.Getenv("GOCV_CAFFE_TEST_FILES")
if path == "" {
t.Skip("Unable to locate Caffe model files for tests")
}
bPrototxt, err := ioutil.ReadFile(path + "/bvlc_googlenet.prototxt")
if err != nil {
t.Errorf("Failed to load Caffe prototxt from file: %v", err)
}
bCaffeModel, err := ioutil.ReadFile(path + "/bvlc_googlenet.caffemodel")
if err != nil {
t.Errorf("Failed to load Caffe caffemodel from file: %v", err)
}
net, err := ReadNetFromCaffeBytes(bPrototxt, bCaffeModel)
if err != nil {
t.Errorf("Error reading caffe from bytes: %v", err)
}
if net.Empty() {
t.Errorf("Unable to load Caffe model")
}
defer net.Close()
checkCaffeNet(t, net)
}
func checkTensorflowNet(t *testing.T, net Net) {
img := IMRead("images/space_shuttle.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in Tensorflow test")
}
defer img.Close()
blob := BlobFromImage(img, 1.0, image.Pt(224, 224), NewScalar(0, 0, 0, 0), true, false)
if blob.Empty() {
t.Error("Invalid blob in Tensorflow test")
}
defer blob.Close()
net.SetInput(blob, "input")
prob := net.Forward("softmax2")
defer prob.Close()
if prob.Empty() {
t.Error("Invalid softmax2 in Tensorflow test")
}
probMat := prob.Reshape(1, 1)
defer probMat.Close()
_, maxVal, minLoc, maxLoc := MinMaxLoc(probMat)
if round(float64(maxVal), 0.00005) != 1.0 {
t.Errorf("Tensorflow maxVal incorrect: %v\n", round(float64(maxVal), 0.00005))
}
if minLoc.X != 481 || minLoc.Y != 0 {
t.Errorf("Tensorflow minLoc incorrect: %v\n", minLoc)
}
if maxLoc.X != 234 || maxLoc.Y != 0 {
t.Errorf("Tensorflow maxLoc incorrect: %v\n", maxLoc)
}
}
func TestTensorflowDisk(t *testing.T) {
path := os.Getenv("GOCV_TENSORFLOW_TEST_FILES")
if path == "" {
t.Skip("Unable to locate Tensorflow model file for tests")
}
net := ReadNetFromTensorflow(path + "/tensorflow_inception_graph.pb")
if net.Empty() {
t.Errorf("Unable to load Tensorflow model")
}
defer net.Close()
checkTensorflowNet(t, net)
}
func TestTensorflowMemory(t *testing.T) {
path := os.Getenv("GOCV_TENSORFLOW_TEST_FILES")
if path == "" {
t.Skip("Unable to locate Tensorflow model file for tests")
}
b, err := ioutil.ReadFile(path + "/tensorflow_inception_graph.pb")
if err != nil {
t.Errorf("Failed to load tensorflow model from file: %v", err)
}
net, err := ReadNetFromTensorflowBytes(b)
if err != nil {
t.Errorf("Failed to load Tensorflow model from bytes: %v", err)
}
if net.Empty() {
t.Errorf("Unable to load Tensorflow model")
}
defer net.Close()
checkTensorflowNet(t, net)
}
func TestBlobFromImages(t *testing.T) {
imgs := make([]Mat, 0)
img := IMRead("images/space_shuttle.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in BlobFromImages test")
}
defer img.Close()
imgs = append(imgs, img)
imgs = append(imgs, img)
blob := NewMat()
BlobFromImages(imgs, &blob, 1.0, image.Pt(25, 25), NewScalar(0, 0, 0, 0), false, false, MatTypeCV32F)
defer blob.Close()
sz := GetBlobSize(blob)
if sz.Val1 != 2 || sz.Val2 != 3 || sz.Val3 != 25 || sz.Val4 != 25 {
t.Errorf("GetBlobSize in BlobFromImages retrieved wrong values")
}
}
func TestBlobFromImageGreyscale(t *testing.T) {
img := IMRead("images/space_shuttle.jpg", IMReadGrayScale)
if img.Empty() {
t.Error("Invalid Mat in TestBlobFromImageGreyscale test")
}
defer img.Close()
blob := BlobFromImage(img, 1.0, image.Pt(100, 100), NewScalar(0, 0, 0, 0), false, false)
defer blob.Close()
if blob.Empty() {
t.Errorf("BlobFromImageGreyscale failed to create blob")
}
}
func TestImagesFromBlob(t *testing.T) {
imgs := make([]Mat, 0)
img := IMRead("images/space_shuttle.jpg", IMReadGrayScale)
if img.Empty() {
t.Error("Invalid Mat in BlobFromImages test")
}
defer img.Close()
imgs = append(imgs, img)
imgs = append(imgs, img)
blob := NewMat()
defer blob.Close()
BlobFromImages(imgs, &blob, 1.0, image.Pt(img.Size()[0], img.Size()[1]), NewScalar(0, 0, 0, 0), false, false, MatTypeCV32F)
imgsFromBlob := make([]Mat, len(imgs))
ImagesFromBlob(blob, imgsFromBlob)
for i := 0; i < len(imgs); i++ {
func() {
imgFromBlob := NewMat()
defer imgFromBlob.Close()
imgsFromBlob[i].ConvertTo(&imgFromBlob, imgs[i].Type())
diff := NewMat()
defer diff.Close()
Compare(imgs[i], imgFromBlob, &diff, CompareNE)
nz := CountNonZero(diff)
if nz != 0 {
t.Error("imgFromBlob is different from img!")
}
}()
}
}
func TestGetBlobChannel(t *testing.T) {
img := NewMatWithSize(100, 100, 5+16)
defer img.Close()
blob := BlobFromImage(img, 1.0, image.Pt(0, 0), NewScalar(0, 0, 0, 0), true, false)
defer blob.Close()
ch2 := GetBlobChannel(blob, 0, 1)
defer ch2.Close()
if ch2.Empty() {
t.Errorf("GetBlobChannel failed to retrieve 2nd chan of a 3channel blob")
}
if ch2.Rows() != img.Rows() || ch2.Cols() != img.Cols() {
t.Errorf("GetBlobChannel: retrieved image size does not match original")
}
}
func TestGetBlobSize(t *testing.T) {
img := NewMatWithSize(100, 100, 5+16)
defer img.Close()
blob := BlobFromImage(img, 1.0, image.Pt(0, 0), NewScalar(0, 0, 0, 0), true, false)
defer blob.Close()
sz := GetBlobSize(blob)
if sz.Val1 != 1 || sz.Val2 != 3 || sz.Val3 != 100 || sz.Val4 != 100 {
t.Errorf("GetBlobSize retrieved wrong values")
}
}
func TestParseNetBackend(t *testing.T) {
val := ParseNetBackend("halide")
if val != NetBackendHalide {
t.Errorf("ParseNetBackend invalid")
}
val = ParseNetBackend("openvino")
if val != NetBackendOpenVINO {
t.Errorf("ParseNetBackend invalid")
}
val = ParseNetBackend("opencv")
if val != NetBackendOpenCV {
t.Errorf("ParseNetBackend invalid")
}
val = ParseNetBackend("cuda")
if val != NetBackendCUDA {
t.Errorf("ParseNetBackend invalid")
}
val = ParseNetBackend("crazytrain")
if val != NetBackendDefault {
t.Errorf("ParseNetBackend invalid")
}
}
func TestParseNetTarget(t *testing.T) {
val := ParseNetTarget("cpu")
if val != NetTargetCPU {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("fp32")
if val != NetTargetFP32 {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("fp16")
if val != NetTargetFP16 {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("vpu")
if val != NetTargetVPU {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("cuda")
if val != NetTargetCUDA {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("cudafp16")
if val != NetTargetCUDAFP16 {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("idk")
if val != NetTargetCPU {
t.Errorf("ParseNetTarget invalid")
}
}
func TestFP16BlobFromImage(t *testing.T) {
img := NewMatWithSize(100, 100, 5+16)
defer img.Close()
data := FP16BlobFromImage(img, 1.0, image.Pt(100, 100), 0, false, false)
if len(data) != 60000 {
t.Errorf("FP16BlobFromImage incorrect length: %v\n", len(data))
}
img2 := NewMatWithSize(100, 50, 5+16)
defer img2.Close()
data = FP16BlobFromImage(img2, 2.0, image.Pt(50, 100), -0.1, true, false)
if len(data) != 30000 {
t.Errorf("FP16BlobFromImage incorrect length: %v\n", len(data))
}
}