forked from hybridgroup/gocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnn_test.go
130 lines (104 loc) · 3.18 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
package gocv
import (
"image"
"os"
"testing"
)
func TestCaffe(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()
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(104, 117, 123, 0), false, false)
if blob.Empty() {
t.Error("Invalid blob in Caffe test")
}
defer blob.Close()
net.SetInput(blob, "data")
prob := net.Forward("prob")
if prob.Empty() {
t.Error("Invalid prob in Caffe test")
}
probMat := prob.Reshape(1, 1)
_, maxVal, minLoc, maxLoc := MinMaxLoc(probMat)
if round(float64(maxVal), 0.00005) != 0.99995 {
t.Errorf("Caffe maxVal incorrect: %v\n", round(float64(maxVal), 0.00005))
}
if minLoc.X != 793 || 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 TestTensorflow(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()
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")
if prob.Empty() {
t.Error("Invalid softmax2 in Tensorflow test")
}
probMat := prob.Reshape(1, 1)
_, 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 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")
}
}