From e4261bbd41619dcf0b34a7ba43b662a28b5c137e Mon Sep 17 00:00:00 2001 From: Alexander Chernikov Date: Sun, 26 Apr 2020 03:51:37 +0300 Subject: [PATCH 1/4] Defer close before checks on empty --- cmd/tf-classifier/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/tf-classifier/main.go b/cmd/tf-classifier/main.go index 86f4d630..89447df1 100644 --- a/cmd/tf-classifier/main.go +++ b/cmd/tf-classifier/main.go @@ -70,11 +70,11 @@ func main() { // open DNN classifier net := gocv.ReadNet(model, "") + defer net.Close() if net.Empty() { fmt.Printf("Error reading network model : %v\n", model) return } - defer net.Close() net.SetPreferableBackend(gocv.NetBackendType(backend)) net.SetPreferableTarget(gocv.NetTargetType(target)) From 7be86e171735e4a21f319a88ae6142ca7599866c Mon Sep 17 00:00:00 2001 From: Alexander Chernikov Date: Sun, 26 Apr 2020 03:56:02 +0300 Subject: [PATCH 2/4] Defer close before checks on empty --- cmd/caffe-classifier/main.go | 2 +- cmd/dnn-detection/main.go | 2 +- cmd/dnn-pose-detection/main.go | 2 +- cmd/dnn-style-transfer/main.go | 2 +- cmd/ssd-facedetect/main.go | 2 +- dnn_test.go | 12 ++++++------ 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cmd/caffe-classifier/main.go b/cmd/caffe-classifier/main.go index be359d3a..99b041bc 100644 --- a/cmd/caffe-classifier/main.go +++ b/cmd/caffe-classifier/main.go @@ -77,11 +77,11 @@ func main() { // open DNN classifier net := gocv.ReadNet(model, config) + defer net.Close() if net.Empty() { fmt.Printf("Error reading network model from : %v %v\n", model, config) return } - defer net.Close() net.SetPreferableBackend(gocv.NetBackendType(backend)) net.SetPreferableTarget(gocv.NetTargetType(target)) diff --git a/cmd/dnn-detection/main.go b/cmd/dnn-detection/main.go index 24d8426f..fbff056e 100644 --- a/cmd/dnn-detection/main.go +++ b/cmd/dnn-detection/main.go @@ -74,11 +74,11 @@ func main() { // open DNN object tracking model net := gocv.ReadNet(model, config) + defer net.Close() if net.Empty() { fmt.Printf("Error reading network model from : %v %v\n", model, config) return } - defer net.Close() net.SetPreferableBackend(gocv.NetBackendType(backend)) net.SetPreferableTarget(gocv.NetTargetType(target)) diff --git a/cmd/dnn-pose-detection/main.go b/cmd/dnn-pose-detection/main.go index 1b414b65..a61753b4 100644 --- a/cmd/dnn-pose-detection/main.go +++ b/cmd/dnn-pose-detection/main.go @@ -76,11 +76,11 @@ func main() { // open OpenPose model n := gocv.ReadNet(model, proto) net = &n + defer net.Close() if net.Empty() { fmt.Printf("Error reading network model from : %v %v\n", model, proto) return } - defer net.Close() net.SetPreferableBackend(gocv.NetBackendType(backend)) net.SetPreferableTarget(gocv.NetTargetType(target)) diff --git a/cmd/dnn-style-transfer/main.go b/cmd/dnn-style-transfer/main.go index 98c81cb9..95337e30 100644 --- a/cmd/dnn-style-transfer/main.go +++ b/cmd/dnn-style-transfer/main.go @@ -59,11 +59,11 @@ func main() { // open DNN style transfer model net := gocv.ReadNet(model, "") + defer net.Close() if net.Empty() { fmt.Printf("Error reading network model from : %v\n", model) return } - defer net.Close() net.SetPreferableBackend(gocv.NetBackendType(backend)) net.SetPreferableTarget(gocv.NetTargetType(target)) diff --git a/cmd/ssd-facedetect/main.go b/cmd/ssd-facedetect/main.go index 6819db52..63470232 100644 --- a/cmd/ssd-facedetect/main.go +++ b/cmd/ssd-facedetect/main.go @@ -67,11 +67,11 @@ func main() { // open DNN classifier net := gocv.ReadNetFromCaffe(proto, model) + defer net.Close() if net.Empty() { fmt.Printf("Error reading network model from : %v %v\n", proto, model) return } - defer net.Close() green := color.RGBA{0, 255, 0, 0} fmt.Printf("Start reading device: %v\n", deviceID) diff --git a/dnn_test.go b/dnn_test.go index f7da01b1..4e087d6e 100644 --- a/dnn_test.go +++ b/dnn_test.go @@ -85,10 +85,10 @@ func TestReadNet(t *testing.T) { t.Run("net from disk", func(t *testing.T) { net := ReadNet(path+"/bvlc_googlenet.caffemodel", path+"/bvlc_googlenet.prototxt") + defer net.Close() if net.Empty() { t.Errorf("Unable to load Caffe model using ReadNet") } - defer net.Close() checkNet(t, net) }) @@ -103,13 +103,13 @@ func TestReadNet(t *testing.T) { t.Errorf("Failed to load config from file: %v", err) } net, err := ReadNetBytes("caffe", bModel, bConfig) + defer net.Close() 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) }) @@ -160,10 +160,10 @@ func TestCaffe(t *testing.T) { t.Run("net from disk", func(t *testing.T) { net := ReadNetFromCaffe(path+"/bvlc_googlenet.prototxt", path+"/bvlc_googlenet.caffemodel") + defer net.Close() if net.Empty() { t.Errorf("Unable to load Caffe model") } - defer net.Close() checkNet(t, net) }) @@ -178,13 +178,13 @@ func TestCaffe(t *testing.T) { t.Errorf("Failed to load Caffe caffemodel from file: %v", err) } net, err := ReadNetFromCaffeBytes(bPrototxt, bCaffeModel) + defer net.Close() 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() checkNet(t, net) }) @@ -235,10 +235,10 @@ func TestTensorflow(t *testing.T) { t.Run("net from disk", func(t *testing.T) { net := ReadNetFromTensorflow(path + "/tensorflow_inception_graph.pb") + defer net.Close() if net.Empty() { t.Errorf("Unable to load Tensorflow model") } - defer net.Close() checkNet(t, net) }) @@ -249,13 +249,13 @@ func TestTensorflow(t *testing.T) { t.Errorf("Failed to load tensorflow model from file: %v", err) } net, err := ReadNetFromTensorflowBytes(b) + defer net.Close() 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() checkNet(t, net) }) From c0d3183f212550eaaea5accb8d3c459980a58a65 Mon Sep 17 00:00:00 2001 From: Alexander Chernikov Date: Sun, 26 Apr 2020 04:05:56 +0300 Subject: [PATCH 3/4] fix --- dnn_test.go | 475 ++++++++++++++++++++++++++++------------------------ 1 file changed, 258 insertions(+), 217 deletions(-) diff --git a/dnn_test.go b/dnn_test.go index 4e087d6e..01f31e67 100644 --- a/dnn_test.go +++ b/dnn_test.go @@ -7,268 +7,282 @@ import ( "testing" ) -func TestReadNet(t *testing.T) { - checkNet := func(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(104, 117, 123, 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)) - } - - 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.99995 { - t.Errorf("ReadNet maxVal incorrect: %v\n", round(float64(maxVal), 0.00005)) - } - - if minLoc.X != 793 || 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 checkNet(t *testing.T, net Net) { + net.SetPreferableBackend(NetBackendDefault) + net.SetPreferableTarget(NetTargetCPU) + + img := IMRead("images/space_shuttle.jpg", IMReadColor) + defer img.Close() + if img.Empty() { + t.Error("Invalid Mat in ReadNet test") + } + + blob := BlobFromImage(img, 1.0, image.Pt(224, 224), NewScalar(0, 0, 0, 0), false, false) + defer blob.Close() + if blob.Empty() { + t.Error("Invalid blob in ReadNet test") + } + + 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") + defer net.Close() + if net.Empty() { + t.Errorf("Unable to load Caffe model using ReadNet") + } + + 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") } - t.Run("net from disk", func(t *testing.T) { - net := ReadNet(path+"/bvlc_googlenet.caffemodel", path+"/bvlc_googlenet.prototxt") - defer net.Close() - if net.Empty() { - t.Errorf("Unable to load Caffe model using ReadNet") - } - - checkNet(t, net) - }) - - t.Run("net from memory", func(t *testing.T) { - 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) - defer net.Close() - if err != nil { - t.Errorf("Failed to read net bytes: %v", err) - } - if net.Empty() { - t.Errorf("Unable to load Caffe model using ReadNetBytes") - } - - checkNet(t, net) - }) + 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) + defer net.Close() + if err != nil { + t.Errorf("Failed to read net bytes: %v", err) + } + if net.Empty() { + t.Errorf("Unable to load Caffe model using ReadNetBytes") + } + + checkNet(t, net) } -func TestCaffe(t *testing.T) { - checkNet := func(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() +func checkCaffeNet(t *testing.T, net Net) { + img := IMRead("images/space_shuttle.jpg", IMReadColor) + defer img.Close() + if img.Empty() { + t.Error("Invalid Mat in Caffe test") + } - 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() + blob := BlobFromImage(img, 1.0, image.Pt(224, 224), NewScalar(0, 0, 0, 0), false, false) + defer blob.Close() + if blob.Empty() { + t.Error("Invalid blob in Caffe test") + } - net.SetInput(blob, "data") - prob := net.Forward("prob") - defer prob.Close() - if prob.Empty() { - t.Error("Invalid prob in Caffe test") - } + 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) + probMat := prob.Reshape(1, 1) + defer probMat.Close() + _, 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 round(float64(maxVal), 0.00005) != 0.9998 { + 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 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) - } + 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") } - t.Run("net from disk", func(t *testing.T) { - net := ReadNetFromCaffe(path+"/bvlc_googlenet.prototxt", path+"/bvlc_googlenet.caffemodel") - defer net.Close() - if net.Empty() { - t.Errorf("Unable to load Caffe model") - } - - checkNet(t, net) - }) - - t.Run("net from memory", func(t *testing.T) { - 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) - defer net.Close() - if err != nil { - t.Errorf("Error reading caffe from bytes: %v", err) - } - if net.Empty() { - t.Errorf("Unable to load Caffe model") - } - - checkNet(t, net) - }) + net := ReadNetFromCaffe(path+"/bvlc_googlenet.prototxt", path+"/bvlc_googlenet.caffemodel") + defer net.Close() + if net.Empty() { + t.Errorf("Unable to load Caffe model") + } + + 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) + defer net.Close() + if err != nil { + t.Errorf("Error reading caffe from bytes: %v", err) + } + if net.Empty() { + t.Errorf("Unable to load Caffe model") + } + + checkCaffeNet(t, net) } -func TestTensorflow(t *testing.T) { - checkNet := func(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() +func checkTensorflowNet(t *testing.T, net Net) { + img := IMRead("images/space_shuttle.jpg", IMReadColor) + defer img.Close() + if img.Empty() { + t.Error("Invalid Mat in Tensorflow test") + } + + blob := BlobFromImage(img, 1.0, image.Pt(224, 224), NewScalar(0, 0, 0, 0), true, false) + defer blob.Close() + if blob.Empty() { + t.Error("Invalid blob in Tensorflow test") + } + + net.SetInput(blob, "input") + prob := net.Forward("softmax2") + defer prob.Close() + if prob.Empty() { + t.Error("Invalid softmax2 in Tensorflow test") + } - 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() + probMat := prob.Reshape(1, 1) + defer probMat.Close() + _, maxVal, minLoc, maxLoc := MinMaxLoc(probMat) - net.SetInput(blob, "input") - prob := net.Forward("softmax2") - defer prob.Close() - if prob.Empty() { - t.Error("Invalid softmax2 in Tensorflow test") - } + if round(float64(maxVal), 0.00005) != 1.0 { + t.Errorf("Tensorflow maxVal incorrect: %v\n", round(float64(maxVal), 0.00005)) + } - probMat := prob.Reshape(1, 1) - defer probMat.Close() - _, maxVal, minLoc, maxLoc := MinMaxLoc(probMat) + if minLoc.X != 481 || minLoc.Y != 0 { + t.Errorf("Tensorflow minLoc incorrect: %v\n", minLoc) + } - if round(float64(maxVal), 0.00005) != 1.0 { - t.Errorf("Tensorflow maxVal incorrect: %v\n", round(float64(maxVal), 0.00005)) - } + if maxLoc.X != 234 || maxLoc.Y != 0 { + t.Errorf("Tensorflow maxLoc incorrect: %v\n", maxLoc) + } +} - if minLoc.X != 481 || minLoc.Y != 0 { - t.Errorf("Tensorflow minLoc incorrect: %v\n", minLoc) - } +func TestTensorflowDisk(t *testing.T) { + path := os.Getenv("GOCV_TENSORFLOW_TEST_FILES") + if path == "" { + t.Skip("Unable to locate Tensorflow model file for tests") + } - if maxLoc.X != 234 || maxLoc.Y != 0 { - t.Errorf("Tensorflow maxLoc incorrect: %v\n", maxLoc) - } + net := ReadNetFromTensorflow(path + "/tensorflow_inception_graph.pb") + defer net.Close() + if net.Empty() { + t.Errorf("Unable to load Tensorflow model") } + 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") } - t.Run("net from disk", func(t *testing.T) { - net := ReadNetFromTensorflow(path + "/tensorflow_inception_graph.pb") - defer net.Close() - if net.Empty() { - t.Errorf("Unable to load Tensorflow model") - } - - checkNet(t, net) - }) - - t.Run("net from memory", func(t *testing.T) { - 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) - defer net.Close() - if err != nil { - t.Errorf("Failed to load Tensorflow model from bytes: %v", err) - } - if net.Empty() { - t.Errorf("Unable to load Tensorflow model") - } - - checkNet(t, net) - }) + 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) + defer net.Close() + if err != nil { + t.Errorf("Failed to load Tensorflow model from bytes: %v", err) + } + if net.Empty() { + t.Errorf("Unable to load Tensorflow model") + } + + checkTensorflowNet(t, net) } func TestBlobFromImages(t *testing.T) { imgs := make([]Mat, 0) img := IMRead("images/space_shuttle.jpg", IMReadColor) + defer img.Close() if img.Empty() { t.Error("Invalid Mat in BlobFromImages test") } - defer img.Close() imgs = append(imgs, img) imgs = append(imgs, img) @@ -283,14 +297,29 @@ func TestBlobFromImages(t *testing.T) { } } +func TestBlobFromImageGreyscale(t *testing.T) { + img := IMRead("images/space_shuttle.jpg", IMReadGrayScale) + defer img.Close() + if img.Empty() { + t.Error("Invalid Mat in TestBlobFromImageGreyscale test") + } + + 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) + defer img.Close() if img.Empty() { t.Error("Invalid Mat in BlobFromImages test") } - defer img.Close() imgs = append(imgs, img) imgs = append(imgs, img) @@ -362,6 +391,10 @@ func TestParseNetBackend(t *testing.T) { 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") @@ -385,6 +418,14 @@ func TestParseNetTarget(t *testing.T) { 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") From 52693f7aa9c95e13a5c688b96d3459c778cdf0e1 Mon Sep 17 00:00:00 2001 From: Alexander Chernikov Date: Sun, 26 Apr 2020 04:12:01 +0300 Subject: [PATCH 4/4] fix --- dnn_test.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/dnn_test.go b/dnn_test.go index 01f31e67..704a956d 100644 --- a/dnn_test.go +++ b/dnn_test.go @@ -12,16 +12,16 @@ func checkNet(t *testing.T, net Net) { net.SetPreferableTarget(NetTargetCPU) img := IMRead("images/space_shuttle.jpg", IMReadColor) - defer img.Close() 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) - defer blob.Close() if blob.Empty() { t.Error("Invalid blob in ReadNet test") } + defer blob.Close() net.SetInput(blob, "data") @@ -89,10 +89,10 @@ func TestReadNetDisk(t *testing.T) { } net := ReadNet(path+"/bvlc_googlenet.caffemodel", path+"/bvlc_googlenet.prototxt") - defer net.Close() if net.Empty() { t.Errorf("Unable to load Caffe model using ReadNet") } + defer net.Close() checkNet(t, net) } @@ -112,29 +112,29 @@ func TestReadNetMemory(t *testing.T) { t.Errorf("Failed to load config from file: %v", err) } net, err := ReadNetBytes("caffe", bModel, bConfig) - defer net.Close() 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) - defer img.Close() 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) - defer blob.Close() if blob.Empty() { t.Error("Invalid blob in Caffe test") } + defer blob.Close() net.SetInput(blob, "data") prob := net.Forward("prob") @@ -167,10 +167,10 @@ func TestCaffeDisk(t *testing.T) { } net := ReadNetFromCaffe(path+"/bvlc_googlenet.prototxt", path+"/bvlc_googlenet.caffemodel") - defer net.Close() if net.Empty() { t.Errorf("Unable to load Caffe model") } + defer net.Close() checkCaffeNet(t, net) } @@ -190,29 +190,29 @@ func TestCaffeMemory(t *testing.T) { t.Errorf("Failed to load Caffe caffemodel from file: %v", err) } net, err := ReadNetFromCaffeBytes(bPrototxt, bCaffeModel) - defer net.Close() 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) - defer img.Close() 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) - defer blob.Close() if blob.Empty() { t.Error("Invalid blob in Tensorflow test") } + defer blob.Close() net.SetInput(blob, "input") prob := net.Forward("softmax2") @@ -245,10 +245,10 @@ func TestTensorflowDisk(t *testing.T) { } net := ReadNetFromTensorflow(path + "/tensorflow_inception_graph.pb") - defer net.Close() if net.Empty() { t.Errorf("Unable to load Tensorflow model") } + defer net.Close() checkTensorflowNet(t, net) } @@ -264,13 +264,13 @@ func TestTensorflowMemory(t *testing.T) { t.Errorf("Failed to load tensorflow model from file: %v", err) } net, err := ReadNetFromTensorflowBytes(b) - defer net.Close() 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) } @@ -279,10 +279,10 @@ func TestBlobFromImages(t *testing.T) { imgs := make([]Mat, 0) img := IMRead("images/space_shuttle.jpg", IMReadColor) - defer img.Close() if img.Empty() { t.Error("Invalid Mat in BlobFromImages test") } + defer img.Close() imgs = append(imgs, img) imgs = append(imgs, img) @@ -299,10 +299,10 @@ func TestBlobFromImages(t *testing.T) { func TestBlobFromImageGreyscale(t *testing.T) { img := IMRead("images/space_shuttle.jpg", IMReadGrayScale) - defer img.Close() 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() @@ -316,10 +316,10 @@ func TestImagesFromBlob(t *testing.T) { imgs := make([]Mat, 0) img := IMRead("images/space_shuttle.jpg", IMReadGrayScale) - defer img.Close() if img.Empty() { t.Error("Invalid Mat in BlobFromImages test") } + defer img.Close() imgs = append(imgs, img) imgs = append(imgs, img)