From 280b1b192daaa9de7767ce48dfb66decfe6e4b06 Mon Sep 17 00:00:00 2001 From: judgeeeeee Date: Thu, 26 Nov 2020 11:46:31 +0800 Subject: [PATCH 1/5] feat(example): rename graphdef model --- .../model/{graphdef.pb => model.graphdef} | Bin 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/GraphDef-model/model/{graphdef.pb => model.graphdef} (100%) diff --git a/examples/GraphDef-model/model/graphdef.pb b/examples/GraphDef-model/model/model.graphdef similarity index 100% rename from examples/GraphDef-model/model/graphdef.pb rename to examples/GraphDef-model/model/model.graphdef From 5d732f04517ff74a5dfce1faf71b49780c1465b5 Mon Sep 17 00:00:00 2001 From: judgeeeeee Date: Thu, 26 Nov 2020 11:51:47 +0800 Subject: [PATCH 2/5] feat(graphdef): update graphdef model checker: --- pkg/model/format.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/model/format.go b/pkg/model/format.go index 2aba3617..bd7c54af 100644 --- a/pkg/model/format.go +++ b/pkg/model/format.go @@ -210,13 +210,13 @@ func (f Format) validateForTorchScript(modelPath string, files []os.FileInfo) er func (f Format) validateForGraphDef(modelPath string, files []os.FileInfo) error { var pbFileFlag bool for _, file := range files { - if path.Ext(file.Name()) == ".pb" { + if path.Ext(file.Name()) == ".graphdef" { pbFileFlag = true break } } if !pbFileFlag { - return fmt.Errorf("there are no *.pb file in %v directory", modelPath) + return fmt.Errorf("there are no *.graphdef file in %v directory", modelPath) } return nil } From 8a6bd83cc7c11d7b8a0b8719cee2b0c372237067 Mon Sep 17 00:00:00 2001 From: judgeeeeee Date: Thu, 26 Nov 2020 14:38:08 +0800 Subject: [PATCH 3/5] feat(validate): update framerwork validate --- pkg/model/format.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/model/format.go b/pkg/model/format.go index bd7c54af..ba8e15f7 100644 --- a/pkg/model/format.go +++ b/pkg/model/format.go @@ -81,7 +81,7 @@ func (f Format) validateForSavedModel(modelPath string, files []os.FileInfo) err var pbFileFlag bool var variablesDirFlag bool for _, file := range files { - if path.Ext(file.Name()) == ".pb" { + if file.Name() == "saved_model.pb" { pbFileFlag = true } if file.IsDir() && file.Name() == "variables" { @@ -89,7 +89,7 @@ func (f Format) validateForSavedModel(modelPath string, files []os.FileInfo) err } } if !pbFileFlag { - return fmt.Errorf("there are no *.pb file in %v directory", modelPath) + return fmt.Errorf("there are no saved_model.pb file in %v directory", modelPath) } if !variablesDirFlag { return fmt.Errorf("there are no variables dir in %v directory", modelPath) @@ -178,7 +178,7 @@ func (f Format) validateForMXNETParams(modelPath string, files []os.FileInfo) er var jsonFileFlag bool var paramsFileFlag bool for _, file := range files { - if path.Ext(file.Name()) == ".json" { + if path.Ext(file.Name()) == "symbol.json" { jsonFileFlag = true } if path.Ext(file.Name()) == ".params" { @@ -186,7 +186,7 @@ func (f Format) validateForMXNETParams(modelPath string, files []os.FileInfo) er } } if !jsonFileFlag { - return fmt.Errorf("there are no *.json file in %v directory", modelPath) + return fmt.Errorf("there are no *symbol.json file in %v directory", modelPath) } if !paramsFileFlag { return fmt.Errorf("there are no *.params file in %v directory", modelPath) @@ -224,12 +224,12 @@ func (f Format) validateForGraphDef(modelPath string, files []os.FileInfo) error func (f Format) validateForTensorRT(modelPath string, files []os.FileInfo) error { var tensorrtFileFlag bool for _, file := range files { - if path.Ext(file.Name()) == ".plan" { + if path.Ext(file.Name()) == ".plan" || path.Ext(file.Name()) == ".engine" { tensorrtFileFlag = true } } if !tensorrtFileFlag { - return fmt.Errorf("there are no *.plan file in %v directory", modelPath) + return fmt.Errorf("there are no *.plan or *.engine file in %v directory", modelPath) } return nil } From e794ca838bd18d5efa159a1fe9a87632994055ad Mon Sep 17 00:00:00 2001 From: judgeeeeee Date: Thu, 26 Nov 2020 14:51:26 +0800 Subject: [PATCH 4/5] fix(validate): validate suffix func --- pkg/model/format.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/model/format.go b/pkg/model/format.go index ba8e15f7..53fb5a35 100644 --- a/pkg/model/format.go +++ b/pkg/model/format.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path" + "strings" ) // Format is the definition of model format. @@ -178,7 +179,7 @@ func (f Format) validateForMXNETParams(modelPath string, files []os.FileInfo) er var jsonFileFlag bool var paramsFileFlag bool for _, file := range files { - if path.Ext(file.Name()) == "symbol.json" { + if strings.HasSuffix(file.Name(), "symbol.json") { jsonFileFlag = true } if path.Ext(file.Name()) == ".params" { From c2f95d670744f8e4bce348130ef110d70e6fbede Mon Sep 17 00:00:00 2001 From: judgeeeeee Date: Thu, 26 Nov 2020 15:38:12 +0800 Subject: [PATCH 5/5] feat(validate): validate ML format for exactly one legal file --- pkg/model/format.go | 136 +++++++++++++++++++++++--------------------- 1 file changed, 71 insertions(+), 65 deletions(-) diff --git a/pkg/model/format.go b/pkg/model/format.go index 53fb5a35..554fb14e 100644 --- a/pkg/model/format.go +++ b/pkg/model/format.go @@ -78,48 +78,55 @@ func (f Format) ValidateDirectory(rootPath string) error { return nil } +func ValidateError(modelPath string, modelName string, modelNum int32) error { + if modelNum != 1 { + return fmt.Errorf("Expected one %v file in %v directory, but found %v .", modelName, modelPath, modelNum) + } + return nil +} + func (f Format) validateForSavedModel(modelPath string, files []os.FileInfo) error { - var pbFileFlag bool - var variablesDirFlag bool + var pbFileNum int32 + var variablesDirNum int32 for _, file := range files { if file.Name() == "saved_model.pb" { - pbFileFlag = true + pbFileNum++ } if file.IsDir() && file.Name() == "variables" { - variablesDirFlag = true + variablesDirNum++ } } - if !pbFileFlag { - return fmt.Errorf("there are no saved_model.pb file in %v directory", modelPath) + if e := ValidateError(modelPath, "saved_model.pb", pbFileNum); e != nil { + return e } - if !variablesDirFlag { - return fmt.Errorf("there are no variables dir in %v directory", modelPath) + if e := ValidateError(modelPath, "variables", variablesDirNum); e != nil { + return e } return nil } func (f Format) validateForONNX(modelPath string, files []os.FileInfo) error { - var onnxFileFlag bool + var onnxFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".onnx" { - onnxFileFlag = true + onnxFileNum++ } } - if !onnxFileFlag { - return fmt.Errorf("there are no *.onnx file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.onnx", onnxFileNum); e != nil { + return e } return nil } func (f Format) validateForH5(modelPath string, files []os.FileInfo) error { - var h5FileFlag bool + var h5FileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".h5" { - h5FileFlag = true + h5FileNum++ } } - if !h5FileFlag { - return fmt.Errorf("there are no *.h5 file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.h5", h5FileNum); e != nil { + return e } return nil } @@ -136,141 +143,140 @@ func (f Format) validateForPMML(modelPath string, files []os.FileInfo) error { } func (f Format) validateForCaffeModel(modelPath string, files []os.FileInfo) error { - var caffeModelFileFlag bool - var prototxtFileFlag bool + var caffeModelFileNum int32 + var prototxtFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".caffemodel" { - caffeModelFileFlag = true + caffeModelFileNum++ } if path.Ext(file.Name()) == ".prototxt" { - prototxtFileFlag = true + prototxtFileNum++ } } - if !caffeModelFileFlag { - return fmt.Errorf("there are no *.caffemodel file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.caffemodel", caffeModelFileNum); e != nil { + return e } - if !prototxtFileFlag { - return fmt.Errorf("there are no *.prototxt file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.prototxt", prototxtFileNum); e != nil { + return e } return nil } func (f Format) validateForNetDef(modelPath string, files []os.FileInfo) error { - var initFileFlag bool - var predictFileFlag bool + var initFileNum int32 + var predictFileNum int32 for _, file := range files { if file.Name() == "init_net.pb" { - initFileFlag = true + initFileNum++ } if file.Name() == "predict_net.pb" { - predictFileFlag = true + predictFileNum++ } } - if !initFileFlag { - return fmt.Errorf("there are no init_net.pb file in %v directory", modelPath) + if e := ValidateError(modelPath, "init_net.pb", initFileNum); e != nil { + return e } - if !predictFileFlag { - return fmt.Errorf("there are no predict_net.pb file in %v directory", modelPath) + if e := ValidateError(modelPath, "predict_net.pb", predictFileNum); e != nil { + return e } return nil } func (f Format) validateForMXNETParams(modelPath string, files []os.FileInfo) error { - var jsonFileFlag bool - var paramsFileFlag bool + var jsonFileNum int32 + var paramsFileNum int32 for _, file := range files { if strings.HasSuffix(file.Name(), "symbol.json") { - jsonFileFlag = true + jsonFileNum++ } if path.Ext(file.Name()) == ".params" { - paramsFileFlag = true + paramsFileNum++ } } - if !jsonFileFlag { - return fmt.Errorf("there are no *symbol.json file in %v directory", modelPath) + if e := ValidateError(modelPath, "*symbol.json", jsonFileNum); e != nil { + return e } - if !paramsFileFlag { - return fmt.Errorf("there are no *.params file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.params", paramsFileNum); e != nil { + return e } return nil } func (f Format) validateForTorchScript(modelPath string, files []os.FileInfo) error { - var ptFileFlag bool + var ptFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".pt" { - ptFileFlag = true + ptFileNum++ } } - if !ptFileFlag { - return fmt.Errorf("there are no *.pt file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.pt", ptFileNum); e != nil { + return e } return nil } func (f Format) validateForGraphDef(modelPath string, files []os.FileInfo) error { - var pbFileFlag bool + var graphdefFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".graphdef" { - pbFileFlag = true - break + graphdefFileNum++ } } - if !pbFileFlag { - return fmt.Errorf("there are no *.graphdef file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.graphdef", graphdefFileNum); e != nil { + return e } return nil } func (f Format) validateForTensorRT(modelPath string, files []os.FileInfo) error { - var tensorrtFileFlag bool + var tensorrtFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".plan" || path.Ext(file.Name()) == ".engine" { - tensorrtFileFlag = true + tensorrtFileNum++ } } - if !tensorrtFileFlag { - return fmt.Errorf("there are no *.plan or *.engine file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.plan or *.engine", tensorrtFileNum); e != nil { + return e } return nil } func (f Format) validateForSKLearn(modelPath string, files []os.FileInfo) error { - var sklearnFileFlag bool + var sklearnFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".joblib" { - sklearnFileFlag = true + sklearnFileNum++ } } - if !sklearnFileFlag { - return fmt.Errorf("there are no *.joblib file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.joblib", sklearnFileNum); e != nil { + return e } return nil } func (f Format) validateForXGBoost(modelPath string, files []os.FileInfo) error { - var xgboostFileFlag bool + var xgboostFileNum int32 for _, file := range files { if path.Ext(file.Name()) == ".xgboost" { - xgboostFileFlag = true + xgboostFileNum++ } } - if !xgboostFileFlag { - return fmt.Errorf("there are no *.xgboost file in %v directory", modelPath) + if e := ValidateError(modelPath, "*.xgboost", xgboostFileNum); e != nil { + return e } return nil } func (f Format) validateForMLflow(modelPath string, files []os.FileInfo) error { - var isMLflowFile bool + var MLflowFileNum int32 for _, file := range files { if file.Name() == "MLmodel" { // assuming that user would not fool the tool - isMLflowFile = true + MLflowFileNum++ } } - if !isMLflowFile { - return fmt.Errorf("there are no MLmodel file in %v, directory", modelPath) + if e := ValidateError(modelPath, "MLmodel", MLflowFileNum); e != nil { + return e } return nil }