1 Star 0 Fork 0

IeatCW/gocv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
dnn_test.go 15.38 KB
一键复制 编辑 原始数据 按行查看 历史
deadprogram 提交于 2021-03-10 16:24 . dnn: increase test coverage
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
package gocv
import (
"image"
"io/ioutil"
"os"
"path/filepath"
"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))
}
if len(ids) == 1 && ids[0] != 142 {
t.Errorf("Invalid unconnected output layers in ReadNet test: %d\n", ids[0])
}
lnames := net.GetLayerNames()
if len(lnames) != 142 {
t.Errorf("Invalid len layer names in ReadNet test: %d\n", len(lnames))
}
if len(lnames) == 142 && lnames[1] != "conv1/relu_7x7" {
t.Errorf("Invalid layer name in ReadNet test: %s\n", lnames[1])
}
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")
}
for _, bl := range prob {
bl.Close()
}
}
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)
}
_, err = ReadNetBytes("caffe", nil, nil)
if err == nil {
t.Errorf("Should have error for reading nil model bytes")
}
bConfig, err := ioutil.ReadFile(path + "/bvlc_googlenet.prototxt")
if err != nil {
t.Errorf("Failed to load config from file: %v", err)
}
_, err = ReadNetBytes("caffe", bModel, nil)
if err == nil {
t.Errorf("Should have error for reading nil config bytes")
}
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")
}
_, err := ReadNetFromCaffeBytes(nil, nil)
if err == nil {
t.Errorf("Should have error for reading nil model bytes")
}
bPrototxt, err := ioutil.ReadFile(path + "/bvlc_googlenet.prototxt")
if err != nil {
t.Errorf("Failed to load Caffe prototxt from file: %v", err)
}
_, err = ReadNetFromCaffeBytes(bPrototxt, nil)
if err == nil {
t.Errorf("Should have error for reading nil config bytes")
}
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 TestOnnxMemory(t *testing.T) {
path := os.Getenv("GOCV_ONNX_TEST_FILES")
if path == "" {
t.Skip("Unable to locate ONNX model file for tests")
}
b, err := ioutil.ReadFile(filepath.Join(path, "googlenet-9.onnx"))
if err != nil {
t.Errorf("Failed to load ONNX from file: %v", err)
}
net, err := ReadNetFromONNXBytes(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()
checkONNXNet(t, net)
}
func TestOnnxDisk(t *testing.T) {
path := os.Getenv("GOCV_ONNX_TEST_FILES")
if path == "" {
t.Skip("Unable to locate ONNX model file for tests")
}
net := ReadNetFromONNX(filepath.Join(path, "googlenet-9.onnx"))
if net.Empty() {
t.Errorf("Unable to load ONNX model")
}
defer net.Close()
checkONNXNet(t, net)
}
func checkONNXNet(t *testing.T, net Net) {
img := IMRead("images/space_shuttle.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in ONNX 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 ONNX test")
}
defer blob.Close()
net.SetInput(blob, "data_0")
prob := net.Forward("prob_1")
defer prob.Close()
if prob.Empty() {
t.Error("Invalid output in ONNX test")
}
probMat := prob.Reshape(1, 1)
defer probMat.Close()
_, maxVal, minLoc, maxLoc := MinMaxLoc(probMat)
if round(float64(maxVal), 0.0005) != 0.9965 {
t.Errorf("ONNX maxVal incorrect: %v\n", round(float64(maxVal), 0.0005))
}
if minLoc.X != 955 || minLoc.Y != 0 {
t.Errorf("ONNX minLoc incorrect: %v\n", minLoc)
}
if maxLoc.X != 812 || maxLoc.Y != 0 {
t.Errorf("ONNX maxLoc incorrect: %v\n", maxLoc)
}
}
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("vulkan")
if val != NetBackendVKCOM {
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("vulkan")
if val != NetTargetVulkan {
t.Errorf("ParseNetTarget invalid")
}
val = ParseNetTarget("fpga")
if val != NetTargetFPGA {
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))
}
}
func TestNMSBoxes(t *testing.T) {
img := IMRead("images/face.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in NMSBoxes test")
}
defer img.Close()
img.ConvertTo(&img, MatTypeCV32F)
bboxes := []image.Rectangle{
image.Rect(53, 47, 589, 451),
image.Rect(118, 54, 618, 450),
image.Rect(53, 66, 605, 480),
image.Rect(111, 65, 630, 480),
image.Rect(156, 51, 640, 480),
}
scores := []float32{0.82094115, 0.7998236, 0.9809663, 0.99717456, 0.89628726}
indices := make([]int, 10)
scoreThreshold := float32(0.5)
nmsThreshold := float32(0.4)
NMSBoxes(bboxes, scores, scoreThreshold, nmsThreshold, indices)
if indices[0] != 3 {
t.Errorf("Invalid NMSBoxes test indices: %v", indices)
}
}
func TestNMSBoxesWithParams(t *testing.T) {
img := IMRead("images/face.jpg", IMReadColor)
if img.Empty() {
t.Error("Invalid Mat in NMSBoxesWithParams test")
}
defer img.Close()
img.ConvertTo(&img, MatTypeCV32F)
bboxes := []image.Rectangle{
image.Rect(53, 47, 589, 451),
image.Rect(118, 54, 618, 450),
image.Rect(53, 66, 605, 480),
image.Rect(111, 65, 630, 480),
image.Rect(156, 51, 640, 480),
}
scores := []float32{0.82094115, 0.7998236, 0.9809663, 0.99717456, 0.89628726}
indices := make([]int, 10)
scoreThreshold := float32(0.5)
nmsThreshold := float32(0.4)
NMSBoxesWithParams(bboxes, scores, scoreThreshold, nmsThreshold, indices, float32(1.0), 0)
if indices[0] != 3 {
t.Errorf("Invalid NMSBoxesWithParams test indices: %v", indices)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ieat/gocv.git
git@gitee.com:ieat/gocv.git
ieat
gocv
gocv
release

搜索帮助

344bd9b3 5694891 D2dac590 5694891