From 71750b271573a5ad328e974761c004ea31d95f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Wed, 27 Oct 2021 15:48:57 +0200 Subject: [PATCH 01/42] WIP: add the possibility for the user not to specify the input/output placeholder names + Some refactoring --- app/otbTensorflowModelServe.cxx | 1 + include/otbTensorflowGraphOperations.cxx | 127 ++++++++++++++---- include/otbTensorflowMultisourceModelBase.h | 5 +- include/otbTensorflowMultisourceModelBase.hxx | 41 ++---- 4 files changed, 121 insertions(+), 53 deletions(-) diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index f381ea66..2ec4fab5 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -123,6 +123,7 @@ public: AddParameter(ParameterType_Int, ss_key_dims_y.str(), ss_desc_dims_y.str()); SetMinimumParameterIntValue (ss_key_dims_y.str(), 1); AddParameter(ParameterType_String, ss_key_ph.str(), ss_desc_ph.str()); + MandatoryOff (ss_key_ph.str(); // Add a new bundle ProcessObjectsBundle bundle; diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 3a4a4402..2324951a 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -71,11 +71,73 @@ void LoadModel(const tensorflow::tstring path, tensorflow::SavedModelBundle & bu } } + +// Get the "real" name of the specified tensors, i.e. the names of the layer inside the model +void GetLayerNamesFromUserNames(const tensorflow::protobuf::Map<std::string, tensorflow::TensorInfo> layers, std::vector<std::string> & tensorsNames, + std::vector<std::string> & layerNames) +{ + itkDebugMacro("Nodes contained in the model: "); + int i = 0; + for (auto const & layer : layers) + { + itkDebugMacro("Node "<< i << " inside the model: " << layer.first); + i+=1; + } + + // Get infos + int k = 0; // counter used for tensorsNames + for (std::vector<std::string>::iterator nameIt = tensorsNames.begin(); + nameIt != tensorsNames.end(); ++nameIt) + { + bool found = false; + + // If the user didn't specify the placeholdername, choose the kth layer inside the model + if (nameIt->size() == 0) + { + found = true; + itkDebugMacro("Input " << k << "corresponds to" << layers[k].first << " in the model"); + tensorsNames.push_back(layers[k].first); + } + + // Else, if the user specified the placeholdername, find the corresponding layer inside the model + else + { + itkDebugMacro("Searching for corresponding node of: " << (*nameIt) << "... "); + for (auto const & layer : layers) + { + // layer is a pair (name, tensor_info) + // cf https://stackoverflow.com/questions/63181951/how-to-get-graph-or-graphdef-from-a-given-model + std::string layername = layer.first; + if (layername.substr(0, layername.find(":")).compare((*nameIt)) == 0) + { + found = true; + itkDebugMacro("Found: " << layername << " in the model"); + tensorsNames.push_back(layers[k].first);} + } + } // next layer + } //end else + + k+=1; + + if (!found) + { + itkGenericExceptionMacro("Tensor name \"" << (*nameIt) << "\" not found. \n" << + "You can list all inputs/outputs of your SavedModel by " << + "running: \n\t `saved_model_cli show --dir your_model_dir --all`"); + + } + } +} + + + + // Get the following attributes of the specified tensors (by name) of a graph: +// - layer name, as specified in the model // - shape // - datatype void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::TensorInfo> layers, std::vector<std::string> & tensorsNames, - std::vector<tensorflow::TensorShapeProto> & shapes, std::vector<tensorflow::DataType> & dataTypes) + std::vector<std::string> & layerNames, std::vector<tensorflow::TensorShapeProto> & shapes, std::vector<tensorflow::DataType> & dataTypes) { // Allocation shapes.clear(); @@ -92,36 +154,41 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow } // Get infos + int k = 0; // counter used for tensorsNames for (std::vector<std::string>::iterator nameIt = tensorsNames.begin(); nameIt != tensorsNames.end(); ++nameIt) { bool found = false; - itkDebugMacro("Searching for corresponding node of: " << (*nameIt) << "... "); - for (auto const & layer : layers) - { - // layer is a pair (name, tensor_info) - // cf https://stackoverflow.com/questions/63181951/how-to-get-graph-or-graphdef-from-a-given-model - std::string layername = layer.first; - if (layername.substr(0, layername.find(":")).compare((*nameIt)) == 0) - { - found = true; - const tensorflow::TensorInfo& tensor_info = layer.second; - itkDebugMacro("Found: " << layername << " in the model"); - - // Set default to DT_FLOAT - tensorflow::DataType ts_dt = tensorflow::DT_FLOAT; + // If the user didn't specify the placeholdername, choose the kth layer inside the model + if (nameIt->size() == 0) + { + found = true; + layerNames.push_back(layers[k].first); // TODO modifier ce layers[k] + const tensorflow::TensorInfo& tensor_info = layers[k].second; + itkDebugMacro("Input " << k << "corresponds to" << layers[k].first << " in the model"); + } - // Default (input?) tensor type - ts_dt = tensor_info.dtype(); - dataTypes.push_back(ts_dt); + // Else, if the user specified the placeholdername, find the corresponding layer inside the model + else + { + itkDebugMacro("Searching for corresponding node of: " << (*nameIt) << "... "); + for (auto const & layer : layers) + { + // layer is a pair (name, tensor_info) + // cf https://stackoverflow.com/questions/63181951/how-to-get-graph-or-graphdef-from-a-given-model + std::string layername = layer.first; + if (layername.substr(0, layername.find(":")).compare((*nameIt)) == 0) + { + found = true; + layerNames.push_back(layername); + const tensorflow::TensorInfo& tensor_info = layer.second; + itkDebugMacro("Found: " << layername << " in the model"); + } + } // next layer + } //end else - // Get the tensor's shape - // Here we assure it's a tensor, with 1 shape - tensorflow::TensorShapeProto ts_shp = tensor_info.tensor_shape(); - shapes.push_back(ts_shp); - } - } // next layer + k+=1; if (!found) { @@ -130,6 +197,18 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow "running: \n\t `saved_model_cli show --dir your_model_dir --all`"); } + + // Set default to DT_FLOAT + tensorflow::DataType ts_dt = tensorflow::DT_FLOAT; + + // Default (input?) tensor type + ts_dt = tensor_info.dtype(); + dataTypes.push_back(ts_dt); + + // Get the tensor's shape + // Here we assure it's a tensor, with 1 shape + tensorflow::TensorShapeProto ts_shp = tensor_info.tensor_shape(); + shapes.push_back(ts_shp); } // next tensor name } diff --git a/include/otbTensorflowMultisourceModelBase.h b/include/otbTensorflowMultisourceModelBase.h index 693a150d..02f121e6 100644 --- a/include/otbTensorflowMultisourceModelBase.h +++ b/include/otbTensorflowMultisourceModelBase.h @@ -175,8 +175,9 @@ private: TensorShapeProtoList m_InputTensorsShapes; // Input tensors shapes TensorShapeProtoList m_OutputTensorsShapes; // Output tensors shapes - // Tensor names mapping - std::map<std::string, std::string> m_UserNameToLayerNameMapping; + // Input/output layer names inside the model + StringList m_InputLayers; // List of input names, as contained in the model + StringList m_OutputLayers; // List of output names, as contained in the model }; // end class diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index a79236d5..fd555ad5 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -111,25 +111,19 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> // Run the TF session here // The session will initialize the outputs - // Inputs corresponds to the names of placeholder, as specified when calling TensorFlowModelServe application - // Decloud example: For TF1 model, it is specified by the user as "tower_0:s2_t". For TF2 model, it must be specified by the user as "s2_t" - // Thus, for TF2, we must transorm that to "serving_default_s2_t" + // `inputs` corresponds to a mapping {name, tensor}, with the name being specified by the user when calling TensorFlowModelServe + // we must adapt it to `inputs_new`, that corresponds to a mapping {layerName, tensor}, with the layerName being from the model DictType inputs_new; + int k = 0; for (auto& dict: inputs) { - DictElementType element = {m_UserNameToLayerNameMapping[dict.first], dict.second}; + DictElementType element = {m_InputLayers[k], dict.second}; inputs_new.push_back(element); - } - - StringList m_OutputTensors_new; - for (auto& name: m_OutputTensors) - { - m_OutputTensors_new.push_back(m_UserNameToLayerNameMapping[name]); + k+=1; } // Run the session, evaluating our output tensors from the graph - auto status = this->GetSavedModel()->session.get()->Run(inputs_new, m_OutputTensors_new, m_TargetNodesNames, &outputs); - + auto status = this->GetSavedModel()->session.get()->Run(inputs_new, m_OutputLayers, m_TargetNodesNames, &outputs); if (!status.ok()) { @@ -176,22 +170,15 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> ////////////////////////////////////////////////////////////////////////////////////////// // Set all subelement of the model auto signaturedef = this->GetSignatureDef(); - for (auto& output: signaturedef.outputs()) - { - std::string userName = output.first.substr(0, output.first.find(":")); - std::string layerName = output.second.name(); - m_UserNameToLayerNameMapping[userName] = layerName; - } - for (auto& input: signaturedef.inputs()) - { - std::string userName = input.first.substr(0, input.first.find(":")); - std::string layerName = input.second.name(); - m_UserNameToLayerNameMapping[userName] = layerName; - } - // Get input and output tensors datatypes and shapes - tf::GetTensorAttributes(signaturedef.inputs(), m_InputPlaceholders, m_InputTensorsShapes, m_InputTensorsDataTypes); - tf::GetTensorAttributes(signaturedef.outputs(), m_OutputTensors, m_OutputTensorsShapes, m_OutputTensorsDataTypes); + // Given the inputs/outputs names that the user specified, get the names of the inputs/outputs contained in the model + // and other infos (shapes, dtypes) + // For example, for output names specified by the user m_OutputTensors = ['s2t', 's2t_pad'], + // this will return m_OutputLayers = ['PartitionedCall:0', 'PartitionedCall:1'] + // In case the user hasn't named the output, e.g. m_OutputTensors = [''], + // this will return the first output m_OutputLayers = ['PartitionedCall:0'] + tf::GetTensorAttributes(signaturedef.inputs(), m_InputPlaceholders, m_InputLayers, m_InputTensorsShapes, m_InputTensorsDataTypes); + tf::GetTensorAttributes(signaturedef.outputs(), m_OutputTensors, m_OutputLayers, m_OutputTensorsShapes, m_OutputTensorsDataTypes); } -- GitLab From 124e3a31622b180d40efb12c411506434ce90f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Wed, 27 Oct 2021 16:27:08 +0200 Subject: [PATCH 02/42] FIX: syntax --- include/otbTensorflowGraphOperations.cxx | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 2324951a..7a5364d4 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -159,14 +159,24 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow nameIt != tensorsNames.end(); ++nameIt) { bool found = false; + const tensorflow::TensorInfo& tensor_info; // If the user didn't specify the placeholdername, choose the kth layer inside the model if (nameIt->size() == 0) { found = true; - layerNames.push_back(layers[k].first); // TODO modifier ce layers[k] - const tensorflow::TensorInfo& tensor_info = layers[k].second; - itkDebugMacro("Input " << k << "corresponds to" << layers[k].first << " in the model"); + // select the k-th element of `layers` + int j = 0; + for (auto const & layer : layers) + { + if (j == k) + { + layerNames.push_back(layer.first); + tensor_info = layer.second; + itkDebugMacro("Input " << k << "corresponds to" << layer.first << " in the model"); + } + j+=1; + } } // Else, if the user specified the placeholdername, find the corresponding layer inside the model @@ -182,7 +192,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow { found = true; layerNames.push_back(layername); - const tensorflow::TensorInfo& tensor_info = layer.second; + tensor_info = layer.second; itkDebugMacro("Found: " << layername << " in the model"); } } // next layer -- GitLab From 65d44d591655f1a72428f5ff86c461b5d98d2300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Wed, 27 Oct 2021 16:30:01 +0200 Subject: [PATCH 03/42] COMP: syntax --- include/otbTensorflowGraphOperations.cxx | 59 ------------------------ 1 file changed, 59 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 7a5364d4..0ac19ab4 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -72,65 +72,6 @@ void LoadModel(const tensorflow::tstring path, tensorflow::SavedModelBundle & bu } -// Get the "real" name of the specified tensors, i.e. the names of the layer inside the model -void GetLayerNamesFromUserNames(const tensorflow::protobuf::Map<std::string, tensorflow::TensorInfo> layers, std::vector<std::string> & tensorsNames, - std::vector<std::string> & layerNames) -{ - itkDebugMacro("Nodes contained in the model: "); - int i = 0; - for (auto const & layer : layers) - { - itkDebugMacro("Node "<< i << " inside the model: " << layer.first); - i+=1; - } - - // Get infos - int k = 0; // counter used for tensorsNames - for (std::vector<std::string>::iterator nameIt = tensorsNames.begin(); - nameIt != tensorsNames.end(); ++nameIt) - { - bool found = false; - - // If the user didn't specify the placeholdername, choose the kth layer inside the model - if (nameIt->size() == 0) - { - found = true; - itkDebugMacro("Input " << k << "corresponds to" << layers[k].first << " in the model"); - tensorsNames.push_back(layers[k].first); - } - - // Else, if the user specified the placeholdername, find the corresponding layer inside the model - else - { - itkDebugMacro("Searching for corresponding node of: " << (*nameIt) << "... "); - for (auto const & layer : layers) - { - // layer is a pair (name, tensor_info) - // cf https://stackoverflow.com/questions/63181951/how-to-get-graph-or-graphdef-from-a-given-model - std::string layername = layer.first; - if (layername.substr(0, layername.find(":")).compare((*nameIt)) == 0) - { - found = true; - itkDebugMacro("Found: " << layername << " in the model"); - tensorsNames.push_back(layers[k].first);} - } - } // next layer - } //end else - - k+=1; - - if (!found) - { - itkGenericExceptionMacro("Tensor name \"" << (*nameIt) << "\" not found. \n" << - "You can list all inputs/outputs of your SavedModel by " << - "running: \n\t `saved_model_cli show --dir your_model_dir --all`"); - - } - } -} - - - // Get the following attributes of the specified tensors (by name) of a graph: // - layer name, as specified in the model -- GitLab From 86f25e7063d79dc45af887c5ff0204cdd2903284 Mon Sep 17 00:00:00 2001 From: Narcon Nicolas <nicolas.narcon@inrae.fr> Date: Wed, 27 Oct 2021 17:04:13 +0200 Subject: [PATCH 04/42] COMP: fix const + minor syntax --- app/otbTensorflowModelServe.cxx | 2 +- include/otbTensorflowGraphOperations.cxx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index 2ec4fab5..69587c5d 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -123,7 +123,7 @@ public: AddParameter(ParameterType_Int, ss_key_dims_y.str(), ss_desc_dims_y.str()); SetMinimumParameterIntValue (ss_key_dims_y.str(), 1); AddParameter(ParameterType_String, ss_key_ph.str(), ss_desc_ph.str()); - MandatoryOff (ss_key_ph.str(); + MandatoryOff (ss_key_ph.str()); // Add a new bundle ProcessObjectsBundle bundle; diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 0ac19ab4..95643af4 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -100,7 +100,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow nameIt != tensorsNames.end(); ++nameIt) { bool found = false; - const tensorflow::TensorInfo& tensor_info; + tensorflow::TensorInfo tensor_info; // If the user didn't specify the placeholdername, choose the kth layer inside the model if (nameIt->size() == 0) -- GitLab From 19b79f1009ed1dea5f5120036786f40ca7501fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Wed, 27 Oct 2021 17:18:06 +0200 Subject: [PATCH 05/42] FIX: change layer.first to layer.second.name() --- include/otbTensorflowGraphOperations.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 95643af4..05d32634 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -112,7 +112,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow { if (j == k) { - layerNames.push_back(layer.first); + layerNames.push_back(layer.second.name()); tensor_info = layer.second; itkDebugMacro("Input " << k << "corresponds to" << layer.first << " in the model"); } @@ -132,9 +132,9 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow if (layername.substr(0, layername.find(":")).compare((*nameIt)) == 0) { found = true; - layerNames.push_back(layername); + layerNames.push_back(layer.second.name()); tensor_info = layer.second; - itkDebugMacro("Found: " << layername << " in the model"); + itkDebugMacro("Found: " << layer.second.name() << " in the model"); } } // next layer } //end else -- GitLab From 4810a7138e33786738f66f9000487c742e16fc05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Wed, 27 Oct 2021 17:25:11 +0200 Subject: [PATCH 06/42] ENH: make the output names not mandatory --- app/otbTensorflowModelServe.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index 69587c5d..124dc27c 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -185,7 +185,7 @@ public: SetDefaultParameterFloat ("output.spcscale", 1.0); SetParameterDescription ("output.spcscale", "The output image size/scale and spacing*scale where size and spacing corresponds to the first input"); AddParameter(ParameterType_StringList, "output.names", "Names of the output tensors"); - MandatoryOn ("output.names"); + MandatoryOff ("output.names"); // Output Field of Expression AddParameter(ParameterType_Int, "output.efieldx", "The output expression field (width)"); -- GitLab From 5c103924650184958f84a974a4a587229ae48d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 28 Oct 2021 10:11:23 +0200 Subject: [PATCH 07/42] FIX: check only if output.names if specified --- include/otbTensorflowMultisourceModelBase.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index fd555ad5..071c5dbd 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -156,10 +156,10 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> " and the number of input tensors names is " << m_InputPlaceholders.size()); } - // Check that the number of the following is the same + // When the user specifies the output names, check that the number of the following is the same // - output tensors names // - output expression fields - if (m_OutputExpressionFields.size() != m_OutputTensors.size()) + if (m_OutputTensors.size() != 0) and (m_OutputExpressionFields.size() != m_OutputTensors.size())) { itkExceptionMacro("Number of output tensors names is " << m_OutputTensors.size() << " but the number of output fields of expression is " << m_OutputExpressionFields.size()); -- GitLab From 30a6bfc399e9eefb0a80492a7c68d2cf3ef04428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 28 Oct 2021 11:56:36 +0200 Subject: [PATCH 08/42] FIX: default output.names to "" --- app/otbTensorflowModelServe.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index 124dc27c..0082e15e 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -185,7 +185,7 @@ public: SetDefaultParameterFloat ("output.spcscale", 1.0); SetParameterDescription ("output.spcscale", "The output image size/scale and spacing*scale where size and spacing corresponds to the first input"); AddParameter(ParameterType_StringList, "output.names", "Names of the output tensors"); - MandatoryOff ("output.names"); + SetDefaultParameterString ("output.names", ""); // Output Field of Expression AddParameter(ParameterType_Int, "output.efieldx", "The output expression field (width)"); -- GitLab From 50c530beccb3a2d0d25b94664ebecb633a204f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 28 Oct 2021 13:36:52 +0200 Subject: [PATCH 09/42] FIX: change default output.names from empty string to list containing an empty string --- include/otbTensorflowMultisourceModelBase.hxx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index 071c5dbd..700b9df3 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -32,8 +32,7 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> { auto signatures = this->GetSavedModel()->GetSignatures(); tensorflow::SignatureDef signature_def; - // If serving_default key exists (which is the default for TF saved model), choose it as signature - // Else, choose the first one + if (signatures.size() == 0) { itkExceptionMacro("There are no available signatures for this tag-set. \n" << @@ -41,6 +40,8 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> "`saved_model_cli show --dir your_model_dir --all`"); } + // If serving_default key exists (which is the default for TF saved model), choose it as signature + // Else, choose the first one if (signatures.contains(tensorflow::kDefaultServingSignatureDefKey)) { signature_def = signatures.at(tensorflow::kDefaultServingSignatureDefKey); @@ -159,7 +160,7 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> // When the user specifies the output names, check that the number of the following is the same // - output tensors names // - output expression fields - if (m_OutputTensors.size() != 0) and (m_OutputExpressionFields.size() != m_OutputTensors.size())) + if ((m_OutputTensors.size() != 0) and (m_OutputExpressionFields.size() != m_OutputTensors.size())) { itkExceptionMacro("Number of output tensors names is " << m_OutputTensors.size() << " but the number of output fields of expression is " << m_OutputExpressionFields.size()); @@ -171,6 +172,12 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> // Set all subelement of the model auto signaturedef = this->GetSignatureDef(); + // When the user doesn't specify output.names, m_OutputTensors defaults to an empty string "". We change it to a + // list containing an empty string [""] + if (m_OutputTensors.size() == 0) + m_OutputTensors = {""}; + + // Given the inputs/outputs names that the user specified, get the names of the inputs/outputs contained in the model // and other infos (shapes, dtypes) // For example, for output names specified by the user m_OutputTensors = ['s2t', 's2t_pad'], -- GitLab From 9bbb24ebfa7acaf40c220f9227b699b941f45557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 28 Oct 2021 13:40:37 +0200 Subject: [PATCH 10/42] FIX: remove obsolete piece of code --- app/otbTensorflowModelServe.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index 0082e15e..702030c0 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -185,7 +185,7 @@ public: SetDefaultParameterFloat ("output.spcscale", 1.0); SetParameterDescription ("output.spcscale", "The output image size/scale and spacing*scale where size and spacing corresponds to the first input"); AddParameter(ParameterType_StringList, "output.names", "Names of the output tensors"); - SetDefaultParameterString ("output.names", ""); + MandatoryOff ("output.names"); // Output Field of Expression AddParameter(ParameterType_Int, "output.efieldx", "The output expression field (width)"); -- GitLab From 1d5ca172045ed0d4c46423d8e90811d4c820b9d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 28 Oct 2021 13:55:27 +0200 Subject: [PATCH 11/42] STYLE: minor comment --- include/otbTensorflowMultisourceModelBase.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/otbTensorflowMultisourceModelBase.h b/include/otbTensorflowMultisourceModelBase.h index 02f121e6..6f3619a8 100644 --- a/include/otbTensorflowMultisourceModelBase.h +++ b/include/otbTensorflowMultisourceModelBase.h @@ -175,7 +175,7 @@ private: TensorShapeProtoList m_InputTensorsShapes; // Input tensors shapes TensorShapeProtoList m_OutputTensorsShapes; // Output tensors shapes - // Input/output layer names inside the model + // Layer names inside the model corresponding to inputs and outputs StringList m_InputLayers; // List of input names, as contained in the model StringList m_OutputLayers; // List of output names, as contained in the model -- GitLab From c7f9521bf14713833f1914b642df41ac3248a416 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Tue, 2 Nov 2021 21:49:16 +0100 Subject: [PATCH 12/42] WIP: fix cppcheck warnings --- app/otbDensePolygonClassStatistics.cxx | 21 ++++--------- app/otbImageClassifierFromDeepFeatures.cxx | 11 ++----- app/otbLabelImageSampleSelection.cxx | 2 +- app/otbPatchesExtraction.cxx | 14 +++++---- app/otbPatchesSelection.cxx | 31 +++++++------------ app/otbTensorflowModelServe.cxx | 11 ++++--- app/otbTensorflowModelTrain.cxx | 4 +-- app/otbTrainClassifierFromDeepFeatures.cxx | 7 +---- include/otbTensorflowCommon.cxx | 2 +- include/otbTensorflowCommon.h | 2 +- include/otbTensorflowCopyUtils.cxx | 2 +- include/otbTensorflowCopyUtils.h | 2 +- include/otbTensorflowDataTypeBridge.cxx | 2 +- include/otbTensorflowDataTypeBridge.h | 2 +- include/otbTensorflowGraphOperations.cxx | 7 ++--- include/otbTensorflowGraphOperations.h | 2 +- include/otbTensorflowMultisourceModelBase.h | 2 +- include/otbTensorflowMultisourceModelBase.hxx | 2 +- include/otbTensorflowMultisourceModelFilter.h | 2 +- .../otbTensorflowMultisourceModelFilter.hxx | 2 +- ...tbTensorflowMultisourceModelLearningBase.h | 2 +- ...TensorflowMultisourceModelLearningBase.hxx | 2 +- include/otbTensorflowMultisourceModelTrain.h | 2 +- .../otbTensorflowMultisourceModelTrain.hxx | 2 +- .../otbTensorflowMultisourceModelValidate.h | 2 +- .../otbTensorflowMultisourceModelValidate.hxx | 2 +- include/otbTensorflowSampler.h | 2 +- include/otbTensorflowSampler.hxx | 2 +- include/otbTensorflowSamplingUtils.cxx | 2 +- include/otbTensorflowSamplingUtils.h | 2 +- include/otbTensorflowSource.h | 2 +- include/otbTensorflowSource.hxx | 2 +- include/otbTensorflowStreamerFilter.h | 2 +- include/otbTensorflowStreamerFilter.hxx | 2 +- test/otbTensorflowCopyUtilsTests.cxx | 2 +- test/otbTensorflowTests.cxx | 2 +- 36 files changed, 67 insertions(+), 95 deletions(-) diff --git a/app/otbDensePolygonClassStatistics.cxx b/app/otbDensePolygonClassStatistics.cxx index 62f9eea9..dc80a95c 100644 --- a/app/otbDensePolygonClassStatistics.cxx +++ b/app/otbDensePolygonClassStatistics.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -37,15 +37,14 @@ class DensePolygonClassStatistics : public Application { public: /** Standard class typedefs. */ - typedef DensePolygonClassStatistics Self; + typedef DensePolygonClassStatistics Self; typedef Application Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; /** Standard macro */ itkNewMacro(Self); - - itkTypeMacro(DensePolygonClassStatistics, otb::Application); + itkTypeMacro(DensePolygonClassStatistics, Application); /** DataObjects typedef */ typedef UInt32ImageType LabelImageType; @@ -67,14 +66,7 @@ public: typedef otb::StatisticsXMLFileWriter<FloatVectorImageType::PixelType> StatWriterType; - -private: - DensePolygonClassStatistics() - { - - } - - void DoInit() override + void DoInit() { SetName("DensePolygonClassStatistics"); SetDescription("Computes statistics on a training polygon set."); @@ -118,7 +110,7 @@ private: SetOfficialDocLink(); } - void DoUpdateParameters() override + void DoUpdateParameters() { if ( HasValue("vec") ) { @@ -164,7 +156,7 @@ private: } } - void DoExecute() override + void DoExecute() { // Filters @@ -261,7 +253,6 @@ private: statWriter->Update(); } - }; diff --git a/app/otbImageClassifierFromDeepFeatures.cxx b/app/otbImageClassifierFromDeepFeatures.cxx index 98763559..f3ffd273 100644 --- a/app/otbImageClassifierFromDeepFeatures.cxx +++ b/app/otbImageClassifierFromDeepFeatures.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -61,7 +61,6 @@ private: // Populate group ShareParameter(ss_key_group.str(), "tfmodel." + ss_key_group.str(), ss_desc_group.str()); - } @@ -107,10 +106,8 @@ private: ShareParameter("out" , "classif.out" , "Output image" , "Output image" ); ShareParameter("confmap" , "classif.confmap" , "Confidence map image", "Confidence map image"); ShareParameter("ram" , "classif.ram" , "Ram" , "Ram" ); - } - void DoUpdateParameters() { UpdateInternalParameters("classif"); @@ -122,12 +119,8 @@ private: GetInternalApplication("classif")->SetParameterInputImage("in", GetInternalApplication("tfmodel")->GetParameterOutputImage("out")); UpdateInternalParameters("classif"); ExecuteInternal("classif"); - } // DOExecute() - - void AfterExecuteAndWriteOutputs() - { - // Nothing to do } + }; } // namespace Wrapper } // namespace otb diff --git a/app/otbLabelImageSampleSelection.cxx b/app/otbLabelImageSampleSelection.cxx index 40591b9c..6b395e64 100644 --- a/app/otbLabelImageSampleSelection.cxx +++ b/app/otbLabelImageSampleSelection.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/app/otbPatchesExtraction.cxx b/app/otbPatchesExtraction.cxx index 11914866..7b0ce456 100644 --- a/app/otbPatchesExtraction.cxx +++ b/app/otbPatchesExtraction.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -136,10 +136,6 @@ public: } } - void DoUpdateParameters() - { - } - void DoInit() { @@ -237,6 +233,12 @@ public: } } + + + void DoUpdateParameters() + { + } + private: std::vector<SourceBundle> m_Bundles; @@ -245,4 +247,4 @@ private: } // end namespace wrapper } // end namespace otb -OTB_APPLICATION_EXPORT( otb::Wrapper::PatchesExtraction ) +OTB_APPLICATION_EXPORT(otb::Wrapper::PatchesExtraction) diff --git a/app/otbPatchesSelection.cxx b/app/otbPatchesSelection.cxx index eb6a31a8..5cbdc82a 100644 --- a/app/otbPatchesSelection.cxx +++ b/app/otbPatchesSelection.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -68,7 +68,7 @@ class PatchesSelection : public Application { public: /** Standard class typedefs. */ - typedef PatchesSelection Self; + typedef PatchesSelection Self; typedef Application Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; @@ -100,11 +100,6 @@ public: typedef itk::MaskImageFilter<UInt8ImageType, UInt8ImageType, UInt8ImageType> MaskImageFilterType; - void DoUpdateParameters() - { - } - - void DoInit() { @@ -167,22 +162,15 @@ public: { public: SampleBundle(){} - SampleBundle(unsigned int nClasses){ - dist = DistributionType(nClasses); - id = 0; + explicit SampleBundle(unsigned int nClasses): dist(DistributionType(nClasses)), id(0), black(true){ (void) point; - black = true; (void) index; } ~SampleBundle(){} - SampleBundle(const SampleBundle & other){ - dist = other.GetDistribution(); - id = other.GetSampleID(); - point = other.GetPosition(); - black = other.GetBlack(); - index = other.GetIndex(); - } + SampleBundle(const SampleBundle & other): dist(other.GetDistribution()), id(other.GetSampleID()), + point(other.GetPosition()), black(other.GetBlack()), index(other.GetIndex()) + {} DistributionType GetDistribution() const { @@ -539,7 +527,7 @@ public: PopulateVectorData(seed); } - void PopulateVectorData(std::vector<SampleBundle> & samples) + void PopulateVectorData(const std::vector<SampleBundle> & samples) { // Get data tree DataTreeType::Pointer treeTrain = m_OutVectorDataTrain->GetDataTree(); @@ -657,6 +645,11 @@ public: } + + void DoUpdateParameters() + { + } + private: RadiusType m_Radius; IsNoDataFilterType::Pointer m_NoDataFilter; diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index f381ea66..af260a1e 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -62,10 +62,6 @@ public: /** Typedefs for images */ typedef FloatVectorImageType::SizeType SizeType; - void DoUpdateParameters() - { - } - // // Store stuff related to one source // @@ -329,6 +325,11 @@ public: SetParameterOutputImage("out", m_TFFilter->GetOutput()); } } + + + void DoUpdateParameters() + { + } private: diff --git a/app/otbTensorflowModelTrain.cxx b/app/otbTensorflowModelTrain.cxx index ffb88e4c..e7901998 100644 --- a/app/otbTensorflowModelTrain.cxx +++ b/app/otbTensorflowModelTrain.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -362,7 +362,7 @@ public: // // Get user placeholders // - TrainModelFilterType::DictType GetUserPlaceholders(const std::string key) + TrainModelFilterType::DictType GetUserPlaceholders(const std::string & key) { TrainModelFilterType::DictType dict; TrainModelFilterType::StringList expressions = GetParameterStringList(key); diff --git a/app/otbTrainClassifierFromDeepFeatures.cxx b/app/otbTrainClassifierFromDeepFeatures.cxx index ada83c51..39ac4189 100644 --- a/app/otbTrainClassifierFromDeepFeatures.cxx +++ b/app/otbTrainClassifierFromDeepFeatures.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -121,11 +121,6 @@ private: GetInternalApplication("train")->AddImageToParameterInputImageList("io.il", GetInternalApplication("tfmodel")->GetParameterOutputImage("out")); UpdateInternalParameters("train"); ExecuteInternal("train"); - } // DOExecute() - - void AfterExecuteAndWriteOutputs() - { - // Nothing to do } }; diff --git a/include/otbTensorflowCommon.cxx b/include/otbTensorflowCommon.cxx index 300f4b5a..662c9d3e 100644 --- a/include/otbTensorflowCommon.cxx +++ b/include/otbTensorflowCommon.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowCommon.h b/include/otbTensorflowCommon.h index 512ff5d9..fbd72810 100644 --- a/include/otbTensorflowCommon.h +++ b/include/otbTensorflowCommon.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowCopyUtils.cxx b/include/otbTensorflowCopyUtils.cxx index 63f4a98c..353d37f8 100644 --- a/include/otbTensorflowCopyUtils.cxx +++ b/include/otbTensorflowCopyUtils.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowCopyUtils.h b/include/otbTensorflowCopyUtils.h index 91cddef5..c369febc 100644 --- a/include/otbTensorflowCopyUtils.h +++ b/include/otbTensorflowCopyUtils.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowDataTypeBridge.cxx b/include/otbTensorflowDataTypeBridge.cxx index 0a9eded8..a510cb4e 100644 --- a/include/otbTensorflowDataTypeBridge.cxx +++ b/include/otbTensorflowDataTypeBridge.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowDataTypeBridge.h b/include/otbTensorflowDataTypeBridge.h index bce9791f..af6be18d 100644 --- a/include/otbTensorflowDataTypeBridge.h +++ b/include/otbTensorflowDataTypeBridge.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 3a4a4402..18d6f08f 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even @@ -109,11 +109,8 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow itkDebugMacro("Found: " << layername << " in the model"); - // Set default to DT_FLOAT - tensorflow::DataType ts_dt = tensorflow::DT_FLOAT; - // Default (input?) tensor type - ts_dt = tensor_info.dtype(); + tensorflow::DataType ts_dt = tensor_info.dtype(); dataTypes.push_back(ts_dt); // Get the tensor's shape diff --git a/include/otbTensorflowGraphOperations.h b/include/otbTensorflowGraphOperations.h index 61c92411..f731c8d8 100644 --- a/include/otbTensorflowGraphOperations.h +++ b/include/otbTensorflowGraphOperations.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelBase.h b/include/otbTensorflowMultisourceModelBase.h index 693a150d..f9ded7f6 100644 --- a/include/otbTensorflowMultisourceModelBase.h +++ b/include/otbTensorflowMultisourceModelBase.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index fd3cefc7..8476e3aa 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelFilter.h b/include/otbTensorflowMultisourceModelFilter.h index 74200eb6..0a0078cd 100644 --- a/include/otbTensorflowMultisourceModelFilter.h +++ b/include/otbTensorflowMultisourceModelFilter.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelFilter.hxx b/include/otbTensorflowMultisourceModelFilter.hxx index 22be795b..b0db5386 100644 --- a/include/otbTensorflowMultisourceModelFilter.hxx +++ b/include/otbTensorflowMultisourceModelFilter.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelLearningBase.h b/include/otbTensorflowMultisourceModelLearningBase.h index 6e4c571d..161dc2d4 100644 --- a/include/otbTensorflowMultisourceModelLearningBase.h +++ b/include/otbTensorflowMultisourceModelLearningBase.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelLearningBase.hxx b/include/otbTensorflowMultisourceModelLearningBase.hxx index 49ba00df..28b2328b 100644 --- a/include/otbTensorflowMultisourceModelLearningBase.hxx +++ b/include/otbTensorflowMultisourceModelLearningBase.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelTrain.h b/include/otbTensorflowMultisourceModelTrain.h index 58d02d25..8ec4c38c 100644 --- a/include/otbTensorflowMultisourceModelTrain.h +++ b/include/otbTensorflowMultisourceModelTrain.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelTrain.hxx b/include/otbTensorflowMultisourceModelTrain.hxx index 23bb4e57..272dd639 100644 --- a/include/otbTensorflowMultisourceModelTrain.hxx +++ b/include/otbTensorflowMultisourceModelTrain.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelValidate.h b/include/otbTensorflowMultisourceModelValidate.h index ce9fd45c..322f6a24 100644 --- a/include/otbTensorflowMultisourceModelValidate.h +++ b/include/otbTensorflowMultisourceModelValidate.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowMultisourceModelValidate.hxx b/include/otbTensorflowMultisourceModelValidate.hxx index d264ebb1..8ec685ba 100644 --- a/include/otbTensorflowMultisourceModelValidate.hxx +++ b/include/otbTensorflowMultisourceModelValidate.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowSampler.h b/include/otbTensorflowSampler.h index ffd6af61..bd363bc8 100644 --- a/include/otbTensorflowSampler.h +++ b/include/otbTensorflowSampler.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowSampler.hxx b/include/otbTensorflowSampler.hxx index cdcbf1fe..8c0ea745 100644 --- a/include/otbTensorflowSampler.hxx +++ b/include/otbTensorflowSampler.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowSamplingUtils.cxx b/include/otbTensorflowSamplingUtils.cxx index 5a68cdb2..5cf88f6b 100644 --- a/include/otbTensorflowSamplingUtils.cxx +++ b/include/otbTensorflowSamplingUtils.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowSamplingUtils.h b/include/otbTensorflowSamplingUtils.h index ab808a25..d81d7161 100644 --- a/include/otbTensorflowSamplingUtils.h +++ b/include/otbTensorflowSamplingUtils.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowSource.h b/include/otbTensorflowSource.h index ddec24b5..a262b170 100644 --- a/include/otbTensorflowSource.h +++ b/include/otbTensorflowSource.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowSource.hxx b/include/otbTensorflowSource.hxx index deb8abd2..3ed4588a 100644 --- a/include/otbTensorflowSource.hxx +++ b/include/otbTensorflowSource.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowStreamerFilter.h b/include/otbTensorflowStreamerFilter.h index 6aba8656..4730d369 100644 --- a/include/otbTensorflowStreamerFilter.h +++ b/include/otbTensorflowStreamerFilter.h @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/include/otbTensorflowStreamerFilter.hxx b/include/otbTensorflowStreamerFilter.hxx index b323047b..59904a54 100644 --- a/include/otbTensorflowStreamerFilter.hxx +++ b/include/otbTensorflowStreamerFilter.hxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/test/otbTensorflowCopyUtilsTests.cxx b/test/otbTensorflowCopyUtilsTests.cxx index 249f16e6..5b958646 100644 --- a/test/otbTensorflowCopyUtilsTests.cxx +++ b/test/otbTensorflowCopyUtilsTests.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even diff --git a/test/otbTensorflowTests.cxx b/test/otbTensorflowTests.cxx index c4989121..340b421d 100644 --- a/test/otbTensorflowTests.cxx +++ b/test/otbTensorflowTests.cxx @@ -1,7 +1,7 @@ /*========================================================================= Copyright (c) 2018-2019 IRSTEA - Copyright (c) 2020-2020 INRAE + Copyright (c) 2020-2021 INRAE This software is distributed WITHOUT ANY WARRANTY; without even -- GitLab From 7ee0bde585eb4138c934dc16ded7d02edc0e126d Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 13:23:34 +0100 Subject: [PATCH 13/42] WIP: fix cppcheck warnings (use stc::copy and std::transform, add override and init. NULL pointers) --- include/otbTensorflowMultisourceModelBase.h | 2 +- include/otbTensorflowMultisourceModelBase.hxx | 24 +++++++------------ ...tbTensorflowMultisourceModelLearningBase.h | 2 +- include/otbTensorflowSamplingUtils.h | 22 +++++++---------- include/otbTensorflowSource.h | 6 ++--- include/otbTensorflowSource.hxx | 11 ++++++++- 6 files changed, 32 insertions(+), 35 deletions(-) diff --git a/include/otbTensorflowMultisourceModelBase.h b/include/otbTensorflowMultisourceModelBase.h index f9ded7f6..fe866733 100644 --- a/include/otbTensorflowMultisourceModelBase.h +++ b/include/otbTensorflowMultisourceModelBase.h @@ -131,7 +131,7 @@ public: itkGetMacro(OutputExpressionFields, SizeListType); /** User placeholders */ - void SetUserPlaceholders(DictType dict) {m_UserPlaceholders = dict;} + void SetUserPlaceholders(const DictType & dict) {m_UserPlaceholders = dict;} DictType GetUserPlaceholders() {return m_UserPlaceholders;} /** Target nodes names */ diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index 8476e3aa..b399d97e 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -23,6 +23,8 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> { Superclass::SetCoordinateTolerance(itk::NumericTraits<double>::max() ); Superclass::SetDirectionTolerance(itk::NumericTraits<double>::max() ); + + m_SavedModel = NULL; } template <class TInputImage, class TOutputImage> @@ -103,10 +105,7 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> { // Add the user's placeholders - for (auto& dict: this->GetUserPlaceholders()) - { - inputs.push_back(dict); - } + std::copy(this->GetUserPlaceholders().begin(), this->GetUserPlaceholders().end(), inputs.begin()); // Run the TF session here // The session will initialize the outputs @@ -115,20 +114,15 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> // Decloud example: For TF1 model, it is specified by the user as "tower_0:s2_t". For TF2 model, it must be specified by the user as "s2_t" // Thus, for TF2, we must transform that to "serving_default_s2_t" DictType inputs_new; - for (auto& dict: inputs) - { - DictElementType element = {m_UserNameToLayerNameMapping[dict.first], dict.second}; - inputs_new.push_back(element); - } + std::transform(inputs.begin(), inputs.end(), inputs_new.begin(), + [this](DictElementType dict) -> DictElementType {return {this->m_UserNameToLayerNameMapping[dict.first], dict.second}; }); - StringList m_OutputTensors_new; - for (auto& name: m_OutputTensors) - { - m_OutputTensors_new.push_back(m_UserNameToLayerNameMapping[name]); - } + StringList outputTensors_new; + std::transform(m_OutputTensors.begin(), m_OutputTensors.end(), outputTensors_new.begin(), + [this](std::string name) -> std::string {return this->m_UserNameToLayerNameMapping[name]; }); // Run the session, evaluating our output tensors from the graph - auto status = this->GetSavedModel()->session.get()->Run(inputs_new, m_OutputTensors_new, m_TargetNodesNames, &outputs); + auto status = this->GetSavedModel()->session.get()->Run(inputs_new, outputTensors_new, m_TargetNodesNames, &outputs); if (!status.ok()) diff --git a/include/otbTensorflowMultisourceModelLearningBase.h b/include/otbTensorflowMultisourceModelLearningBase.h index 161dc2d4..0663f17a 100644 --- a/include/otbTensorflowMultisourceModelLearningBase.h +++ b/include/otbTensorflowMultisourceModelLearningBase.h @@ -100,7 +100,7 @@ protected: TensorflowMultisourceModelLearningBase(); virtual ~TensorflowMultisourceModelLearningBase() {}; - virtual void GenerateOutputInformation(void); + virtual void GenerateOutputInformation(void) override; virtual void GenerateInputRequestedRegion(); diff --git a/include/otbTensorflowSamplingUtils.h b/include/otbTensorflowSamplingUtils.h index d81d7161..585f9013 100644 --- a/include/otbTensorflowSamplingUtils.h +++ b/include/otbTensorflowSamplingUtils.h @@ -27,23 +27,17 @@ public: typedef typename TImage::PixelType ValueType; typedef vnl_vector<float> CountsType; - Distribution(unsigned int nClasses){ - m_NbOfClasses = nClasses; - m_Dist = CountsType(nClasses, 0); - + explicit Distribution(unsigned int nClasses): m_NbOfClasses(nClasses), m_Dist(CountsType(nClasses, 0)) + { } - Distribution(unsigned int nClasses, float fillValue){ - m_NbOfClasses = nClasses; - m_Dist = CountsType(nClasses, fillValue); - + Distribution(unsigned int nClasses, float fillValue): m_NbOfClasses(nClasses), m_Dist(CountsType(nClasses, fillValue)) + { } - Distribution(){ - m_NbOfClasses = 2; - m_Dist = CountsType(m_NbOfClasses, 0); + Distribution(): m_NbOfClasses(2), m_Dist(CountsType(m_NbOfClasses, 0)) + { } - Distribution(const Distribution & other){ - m_Dist = other.Get(); - m_NbOfClasses = m_Dist.size(); + Distribution(const Distribution & other): m_Dist(other.Get()), m_NbOfClasses(m_Dist.size()) + { } ~Distribution(){} diff --git a/include/otbTensorflowSource.h b/include/otbTensorflowSource.h index a262b170..1556997f 100644 --- a/include/otbTensorflowSource.h +++ b/include/otbTensorflowSource.h @@ -25,9 +25,9 @@ namespace otb { /* - * This is a simple helper to create images concatenation. + * This is a helper for images concatenation. * Images must have the same size. - * This is basically the common input type used in every OTB-TF applications. + * This is the common input type used in every OTB-TF applications. */ template<class TImage> class TensorflowSource @@ -60,7 +60,7 @@ public: // Get the source output FloatVectorImagePointerType Get(); - TensorflowSource(){}; + TensorflowSource(); virtual ~TensorflowSource (){}; private: diff --git a/include/otbTensorflowSource.hxx b/include/otbTensorflowSource.hxx index 3ed4588a..2ad57586 100644 --- a/include/otbTensorflowSource.hxx +++ b/include/otbTensorflowSource.hxx @@ -17,12 +17,21 @@ namespace otb { +// +// Constructor +// +template <class TImage> +TensorflowSource<TImage> +::TensorflowSource() +{} + // // Prepare the big stack of images // template <class TImage> void -TensorflowSource<TImage>::Set(FloatVectorImageListType * inputList) +TensorflowSource<TImage> +::Set(FloatVectorImageListType * inputList) { // Create one stack for input images list m_Concatener = ListConcatenerFilterType::New(); -- GitLab From 9a46d2df308fe8912d463f1a04514cee4636faf3 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 14:27:48 +0100 Subject: [PATCH 14/42] COMP: add include dir in cppcheck --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 418c9052..8fbef75a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -54,7 +54,7 @@ cppcheck: allow_failure: true script: - sudo apt update && sudo apt install cppcheck -y - - cppcheck --enable=all --error-exitcode=1 $OTBTF_SRC/ + - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ . ctest: stage: Test -- GitLab From 5e22acebfa96b71669df92a84d08c5202412c051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Wed, 3 Nov 2021 16:12:48 +0100 Subject: [PATCH 15/42] ENH: set default values for receptive fields to 1 --- app/otbTensorflowModelServe.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/otbTensorflowModelServe.cxx b/app/otbTensorflowModelServe.cxx index 702030c0..7cce77f5 100644 --- a/app/otbTensorflowModelServe.cxx +++ b/app/otbTensorflowModelServe.cxx @@ -120,8 +120,10 @@ public: AddParameter(ParameterType_InputImageList, ss_key_in.str(), ss_desc_in.str() ); AddParameter(ParameterType_Int, ss_key_dims_x.str(), ss_desc_dims_x.str()); SetMinimumParameterIntValue (ss_key_dims_x.str(), 1); + SetDefaultParameterInt (ss_key_dims_x.str(), 1); AddParameter(ParameterType_Int, ss_key_dims_y.str(), ss_desc_dims_y.str()); SetMinimumParameterIntValue (ss_key_dims_y.str(), 1); + SetDefaultParameterInt (ss_key_dims_y.str(), 1); AddParameter(ParameterType_String, ss_key_ph.str(), ss_desc_ph.str()); MandatoryOff (ss_key_ph.str()); -- GitLab From aacae9bcfa497530e6171c7e06ebe7846d178b31 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 16:52:40 +0100 Subject: [PATCH 16/42] FIX: use std::back_inserter for placeholders --- include/otbTensorflowMultisourceModelBase.hxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index b399d97e..70cd94ce 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -105,7 +105,7 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> { // Add the user's placeholders - std::copy(this->GetUserPlaceholders().begin(), this->GetUserPlaceholders().end(), inputs.begin()); + std::copy(this->GetUserPlaceholders().begin(), this->GetUserPlaceholders().end(), std::back_inserter(inputs)); // Run the TF session here // The session will initialize the outputs @@ -114,11 +114,11 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> // Decloud example: For TF1 model, it is specified by the user as "tower_0:s2_t". For TF2 model, it must be specified by the user as "s2_t" // Thus, for TF2, we must transform that to "serving_default_s2_t" DictType inputs_new; - std::transform(inputs.begin(), inputs.end(), inputs_new.begin(), + std::transform(inputs.begin(), inputs.end(), std::back_inserter(inputs_new), [this](DictElementType dict) -> DictElementType {return {this->m_UserNameToLayerNameMapping[dict.first], dict.second}; }); StringList outputTensors_new; - std::transform(m_OutputTensors.begin(), m_OutputTensors.end(), outputTensors_new.begin(), + std::transform(m_OutputTensors.begin(), m_OutputTensors.end(), std::back_inserter(outputTensors_new), [this](std::string name) -> std::string {return this->m_UserNameToLayerNameMapping[name]; }); // Run the session, evaluating our output tensors from the graph -- GitLab From f4db15e14767b7afb2fa7502d676864b72428828 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 17:00:51 +0100 Subject: [PATCH 17/42] ENH: use --logging-format-style=new in pylint --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8fbef75a..893694ba 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -41,7 +41,7 @@ pylint: allow_failure: true script: - sudo apt update && sudo apt install pylint -y - - pylint --disable=too-many-nested-blocks,too-many-locals,too-many-statements,too-few-public-methods,too-many-instance-attributes,too-many-arguments --ignored-modules=tensorflow --max-line-length=120 $OTBTF_SRC/python + - pylint --disable=too-many-nested-blocks,too-many-locals,too-many-statements,too-few-public-methods,too-many-instance-attributes,too-many-arguments --ignored-modules=tensorflow --max-line-length=120 --logging-format-style=new $OTBTF_SRC/python codespell: stage: Static Analysis -- GitLab From ae08833c3057a67747a59a08b27254354d616628 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 17:01:11 +0100 Subject: [PATCH 18/42] TEST: comply with PEP --- python/ckpt2savedmodel.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/ckpt2savedmodel.py b/python/ckpt2savedmodel.py index 1892757e..117203ba 100755 --- a/python/ckpt2savedmodel.py +++ b/python/ckpt2savedmodel.py @@ -28,6 +28,7 @@ keras in Tensorflow 2). import argparse from tricks import ckpt_to_savedmodel + def main(): """ Main function @@ -48,5 +49,6 @@ def main(): savedmodel_path=params.model, clear_devices=params.clear_devices) + if __name__ == "__main__": main() -- GitLab From 9e652f12e6c8e1802de4c6ae03a02541c0c8cebe Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 17:52:15 +0100 Subject: [PATCH 19/42] STYLE: remove trailing spaces --- app/otbLabelImageSampleSelection.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/otbLabelImageSampleSelection.cxx b/app/otbLabelImageSampleSelection.cxx index 6b395e64..50396fa0 100644 --- a/app/otbLabelImageSampleSelection.cxx +++ b/app/otbLabelImageSampleSelection.cxx @@ -35,7 +35,7 @@ class LabelImageSampleSelection : public Application { public: /** Standard class typedefs. */ - typedef LabelImageSampleSelection Self; + typedef LabelImageSampleSelection Self; typedef Application Superclass; typedef itk::SmartPointer<Self> Pointer; typedef itk::SmartPointer<const Self> ConstPointer; @@ -385,4 +385,4 @@ private: } // end namespace wrapper } // end namespace otb -OTB_APPLICATION_EXPORT( otb::Wrapper::LabelImageSampleSelection ) +OTB_APPLICATION_EXPORT(otb::Wrapper::LabelImageSampleSelection) -- GitLab From 452626faf1bed59c00cb5b2acd848df1775a664e Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 17:52:50 +0100 Subject: [PATCH 20/42] STYLE: add cppcheck-suppress unusedFunction --- test/otbTensorflowTests.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/otbTensorflowTests.cxx b/test/otbTensorflowTests.cxx index 340b421d..9ed0d11a 100644 --- a/test/otbTensorflowTests.cxx +++ b/test/otbTensorflowTests.cxx @@ -11,6 +11,7 @@ =========================================================================*/ #include "otbTestMain.h" +// cppcheck-suppress unusedFunction void RegisterTests() { REGISTER_TEST(floatValueToTensorTest); @@ -21,4 +22,3 @@ void RegisterTests() REGISTER_TEST(boolVecValueToTensorTest); } - -- GitLab From f1ec4475c1ef032c199f2627f5ffb75a6a2c7a0e Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 17:53:11 +0100 Subject: [PATCH 21/42] STYLE: add cppcheck-suppress unusedFunction --- include/otbTensorflowGraphOperations.cxx | 1 + 1 file changed, 1 insertion(+) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 18d6f08f..0e89fe77 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -133,6 +133,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow // // Print a lot of stuff about the specified nodes of the graph // +// cppcheck-suppress unusedFunction void PrintNodeAttributes(const tensorflow::GraphDef & graph, std::vector<std::string> & nodesNames) { std::cout << "Go through graph:" << std::endl; -- GitLab From 788058ee6772242bb9cf0cd54d15f458ebc43f0b Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 18:47:01 +0100 Subject: [PATCH 22/42] DOC: remove unused DocSeeAlso --- app/otbDensePolygonClassStatistics.cxx | 136 +++++++++++++------------ 1 file changed, 72 insertions(+), 64 deletions(-) diff --git a/app/otbDensePolygonClassStatistics.cxx b/app/otbDensePolygonClassStatistics.cxx index dc80a95c..fa7c2701 100644 --- a/app/otbDensePolygonClassStatistics.cxx +++ b/app/otbDensePolygonClassStatistics.cxx @@ -9,18 +9,24 @@ PURPOSE. See the above copyright notices for more information. =========================================================================*/ -#include "otbWrapperApplication.h" +#include "itkFixedArray.h" +#include "itkObjectFactory.h" #include "otbWrapperApplicationFactory.h" +// Application engine +#include "otbStandardFilterWatcher.h" +#include "itkFixedArray.h" + +// Filters #include "otbStatisticsXMLFileWriter.h" #include "otbWrapperElevationParametersHandler.h" - #include "otbVectorDataToLabelImageFilter.h" #include "otbImageToNoDataMaskFilter.h" #include "otbStreamingStatisticsMapFromLabelImageFilter.h" #include "otbVectorDataIntoImageProjectionFilter.h" #include "otbImageToVectorImageCastFilter.h" +// OGR #include "otbOGR.h" namespace otb @@ -80,7 +86,6 @@ public: " - number of samples per geometry\n"); SetDocLimitations("None"); SetDocAuthors("Remi Cresson"); - SetDocSeeAlso(" "); AddDocTag(Tags::Learning); @@ -107,67 +112,11 @@ public: SetDocExampleParameterValue("field", "label"); SetDocExampleParameterValue("out","polygonStat.xml"); - SetOfficialDocLink(); - } - - void DoUpdateParameters() - { - if ( HasValue("vec") ) - { - std::string vectorFile = GetParameterString("vec"); - ogr::DataSource::Pointer ogrDS = - ogr::DataSource::New(vectorFile, ogr::DataSource::Modes::Read); - ogr::Layer layer = ogrDS->GetLayer(0); - ogr::Feature feature = layer.ogr().GetNextFeature(); - - ClearChoices("field"); - - for(int iField=0; iField<feature.ogr().GetFieldCount(); iField++) - { - std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef(); - key = item; - std::string::iterator end = std::remove_if(key.begin(),key.end(),IsNotAlphaNum); - std::transform(key.begin(), end, key.begin(), tolower); - - OGRFieldType fieldType = feature.ogr().GetFieldDefnRef(iField)->GetType(); - - if(fieldType == OFTString || fieldType == OFTInteger || fieldType == OFTInteger64) - { - std::string tmpKey="field."+key.substr(0, end - key.begin()); - AddChoice(tmpKey,item); - } - } - } - - // Check that the extension of the output parameter is XML (mandatory for - // StatisticsXMLFileWriter) - // Check it here to trigger the error before polygons analysis - - if ( HasValue("out") ) - { - // Store filename extension - // Check that the right extension is given : expected .xml - const std::string extension = itksys::SystemTools::GetFilenameLastExtension(this->GetParameterString("out")); - - if (itksys::SystemTools::LowerCase(extension) != ".xml") - { - otbAppLogFATAL( << extension << " is a wrong extension for parameter \"out\": Expected .xml" ); - } - } } void DoExecute() { - // Filters - VectorDataReprojFilterType::Pointer m_VectorDataReprojectionFilter; - RasterizeFilterType::Pointer m_RasterizeFIDFilter; - RasterizeFilterType::Pointer m_RasterizeClassFilter; - NoDataMaskFilterType::Pointer m_NoDataFilter; - CastFilterType::Pointer m_NoDataCastFilter; - StatsFilterType::Pointer m_FIDStatsFilter; - StatsFilterType::Pointer m_ClassStatsFilter; - // Retrieve the field name std::vector<int> selectedCFieldIdx = GetSelectedItems("field"); @@ -246,14 +195,73 @@ public: fidMap.erase(intNoData); classMap.erase(intNoData); - StatWriterType::Pointer statWriter = StatWriterType::New(); - statWriter->SetFileName(this->GetParameterString("out")); - statWriter->AddInputMap<StatsFilterType::LabelPopulationMapType>("samplesPerClass",classMap); - statWriter->AddInputMap<StatsFilterType::LabelPopulationMapType>("samplesPerVector",fidMap); - statWriter->Update(); + m_StatWriter = StatWriterType::New(); + m_StatWriter->SetFileName(this->GetParameterString("out")); + m_StatWriter->AddInputMap<StatsFilterType::LabelPopulationMapType>("samplesPerClass", classMap); + m_StatWriter->AddInputMap<StatsFilterType::LabelPopulationMapType>("samplesPerVector", fidMap); + m_StatWriter->Update(); + + } + + void DoUpdateParameters() + { + if (HasValue("vec")) + { + std::string vectorFile = GetParameterString("vec"); + ogr::DataSource::Pointer ogrDS = + ogr::DataSource::New(vectorFile, ogr::DataSource::Modes::Read); + ogr::Layer layer = ogrDS->GetLayer(0); + ogr::Feature feature = layer.ogr().GetNextFeature(); + + ClearChoices("field"); + for(int iField=0; iField<feature.ogr().GetFieldCount(); iField++) + { + std::string key, item = feature.ogr().GetFieldDefnRef(iField)->GetNameRef(); + key = item; + std::string::iterator end = std::remove_if(key.begin(),key.end(),IsNotAlphaNum); + std::transform(key.begin(), end, key.begin(), tolower); + + OGRFieldType fieldType = feature.ogr().GetFieldDefnRef(iField)->GetType(); + + if(fieldType == OFTString || fieldType == OFTInteger || fieldType == OFTInteger64) + { + std::string tmpKey="field."+key.substr(0, end - key.begin()); + AddChoice(tmpKey,item); + } + } + } + + // Check that the extension of the output parameter is XML (mandatory for + // StatisticsXMLFileWriter) + // Check it here to trigger the error before polygons analysis + + if (HasValue("out")) + { + // Store filename extension + // Check that the right extension is given : expected .xml + const std::string extension = itksys::SystemTools::GetFilenameLastExtension(this->GetParameterString("out")); + + if (itksys::SystemTools::LowerCase(extension) != ".xml") + { + otbAppLogFATAL( << extension << " is a wrong extension for parameter \"out\": Expected .xml" ); + } + } } + + +private: + // Filters + VectorDataReprojFilterType::Pointer m_VectorDataReprojectionFilter; + RasterizeFilterType::Pointer m_RasterizeFIDFilter; + RasterizeFilterType::Pointer m_RasterizeClassFilter; + NoDataMaskFilterType::Pointer m_NoDataFilter; + CastFilterType::Pointer m_NoDataCastFilter; + StatsFilterType::Pointer m_FIDStatsFilter; + StatsFilterType::Pointer m_ClassStatsFilter; + StatWriterType::Pointer m_StatWriter; + }; } // end of namespace Wrapper -- GitLab From 6bc39fd6515375f0a73f5a2ecc04193709cd4765 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 18:47:39 +0100 Subject: [PATCH 23/42] COMP: soften cppcheck on unused functions --- .gitlab-ci.yml | 2 +- test/otbTensorflowTests.cxx | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 893694ba..8baac130 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -54,7 +54,7 @@ cppcheck: allow_failure: true script: - sudo apt update && sudo apt install cppcheck -y - - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ . + - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ --suppress=missingInclude --inline-suppr --force . ctest: stage: Test diff --git a/test/otbTensorflowTests.cxx b/test/otbTensorflowTests.cxx index 9ed0d11a..50e9a91a 100644 --- a/test/otbTensorflowTests.cxx +++ b/test/otbTensorflowTests.cxx @@ -11,7 +11,6 @@ =========================================================================*/ #include "otbTestMain.h" -// cppcheck-suppress unusedFunction void RegisterTests() { REGISTER_TEST(floatValueToTensorTest); -- GitLab From 526193f2ae030f05fc9a4dce3a01982476440ab1 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 18:50:23 +0100 Subject: [PATCH 24/42] COMP: remove // cppcheck-suppress unusedFunction --- include/otbTensorflowGraphOperations.cxx | 1 - 1 file changed, 1 deletion(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 0e89fe77..18d6f08f 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -133,7 +133,6 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow // // Print a lot of stuff about the specified nodes of the graph // -// cppcheck-suppress unusedFunction void PrintNodeAttributes(const tensorflow::GraphDef & graph, std::vector<std::string> & nodesNames) { std::cout << "Go through graph:" << std::endl; -- GitLab From f0889331a148a0f1c921227f08381128ebe59e98 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 19:07:44 +0100 Subject: [PATCH 25/42] COMP: soften cppcheck on unused functions --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8baac130..f5b4f944 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -54,7 +54,7 @@ cppcheck: allow_failure: true script: - sudo apt update && sudo apt install cppcheck -y - - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ --suppress=missingInclude --inline-suppr --force . + - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ --suppress=missingInclude --suppress=unusedFunction --inline-suppr --force . ctest: stage: Test -- GitLab From cc763ce3c6c2294e2d6ebf18d983a6b207eac075 Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Wed, 3 Nov 2021 19:14:56 +0100 Subject: [PATCH 26/42] COMP: remove --inline-suppr --force in cppcheck --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f5b4f944..ef933451 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -54,7 +54,7 @@ cppcheck: allow_failure: true script: - sudo apt update && sudo apt install cppcheck -y - - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ --suppress=missingInclude --suppress=unusedFunction --inline-suppr --force . + - cd $OTBTF_SRC/ && cppcheck --enable=all --error-exitcode=1 -I include/ --suppress=missingInclude --suppress=unusedFunction . ctest: stage: Test -- GitLab From 9d6f4343db379426b263d706169ddd6f0692552c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 12:26:01 +0100 Subject: [PATCH 27/42] ENH: change itkDebugMacro to otbLogMacro --- include/otbTensorflowCopyUtils.cxx | 2 +- include/otbTensorflowCopyUtils.h | 3 +++ include/otbTensorflowGraphOperations.cxx | 10 +++++----- include/otbTensorflowGraphOperations.h | 3 +++ include/otbTensorflowMultisourceModelFilter.h | 3 +++ include/otbTensorflowMultisourceModelFilter.hxx | 2 +- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/otbTensorflowCopyUtils.cxx b/include/otbTensorflowCopyUtils.cxx index 4ac1cf2e..27f1433f 100644 --- a/include/otbTensorflowCopyUtils.cxx +++ b/include/otbTensorflowCopyUtils.cxx @@ -405,7 +405,7 @@ ValueToTensor(std::string value) } idx++; } - itkDebugMacro("Returning tensor: "<< out.DebugString()); + otbLogMacro(Debug, "Returning tensor: "<< out.DebugString()); return out; } diff --git a/include/otbTensorflowCopyUtils.h b/include/otbTensorflowCopyUtils.h index 91cddef5..e9e59a7e 100644 --- a/include/otbTensorflowCopyUtils.h +++ b/include/otbTensorflowCopyUtils.h @@ -15,6 +15,9 @@ // ITK exception #include "itkMacro.h" +// OTB log +#include "otbMacro.h" + // ITK image iterators #include "itkImageRegionIterator.h" #include "itkImageRegionConstIterator.h" diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 05d32634..5d3d42f0 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -86,11 +86,11 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow dataTypes.clear(); dataTypes.reserve(tensorsNames.size()); - itkDebugMacro("Nodes contained in the model: "); + otbLogMacro(Debug, "Nodes contained in the model: "); int i = 0; for (auto const & layer : layers) { - itkDebugMacro("Node "<< i << " inside the model: " << layer.first); + otbLogMacro(Debug, "Node "<< i << " inside the model: " << layer.first); i+=1; } @@ -114,7 +114,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow { layerNames.push_back(layer.second.name()); tensor_info = layer.second; - itkDebugMacro("Input " << k << "corresponds to" << layer.first << " in the model"); + otbLogMacro(Debug, "Input " << k << "corresponds to" << layer.first << " in the model"); } j+=1; } @@ -123,7 +123,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow // Else, if the user specified the placeholdername, find the corresponding layer inside the model else { - itkDebugMacro("Searching for corresponding node of: " << (*nameIt) << "... "); + otbLogMacro(Debug, "Searching for corresponding node of: " << (*nameIt) << "... "); for (auto const & layer : layers) { // layer is a pair (name, tensor_info) @@ -134,7 +134,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow found = true; layerNames.push_back(layer.second.name()); tensor_info = layer.second; - itkDebugMacro("Found: " << layer.second.name() << " in the model"); + otbLogMacro(Debug, "Found: " << layer.second.name() << " in the model"); } } // next layer } //end else diff --git a/include/otbTensorflowGraphOperations.h b/include/otbTensorflowGraphOperations.h index 61c92411..f83e04de 100644 --- a/include/otbTensorflowGraphOperations.h +++ b/include/otbTensorflowGraphOperations.h @@ -24,6 +24,9 @@ // ITK exception #include "itkMacro.h" +// OTB log +#include "otbMacro.h" + namespace otb { namespace tf { diff --git a/include/otbTensorflowMultisourceModelFilter.h b/include/otbTensorflowMultisourceModelFilter.h index 74200eb6..aa74c4ff 100644 --- a/include/otbTensorflowMultisourceModelFilter.h +++ b/include/otbTensorflowMultisourceModelFilter.h @@ -26,6 +26,9 @@ #include "itkMetaDataObject.h" #include "otbMetaDataKey.h" +// OTB log +#include "otbMacro.h" + namespace otb { diff --git a/include/otbTensorflowMultisourceModelFilter.hxx b/include/otbTensorflowMultisourceModelFilter.hxx index 43b6ed76..3453ad56 100644 --- a/include/otbTensorflowMultisourceModelFilter.hxx +++ b/include/otbTensorflowMultisourceModelFilter.hxx @@ -333,7 +333,7 @@ TensorflowMultisourceModelFilter<TInputImage, TOutputImage> if (!OutputRegionToInputRegion(requestedRegion, inRegion, inputImage) ) { // Image does not overlap requested region: set requested region to null - itkDebugMacro( << "Image #" << i << " :\n" << inRegion << " is outside the requested region"); + otbLogMacro(Debug, << "Image #" << i << " :\n" << inRegion << " is outside the requested region"); inRegion.GetModifiableIndex().Fill(0); inRegion.GetModifiableSize().Fill(0); } -- GitLab From 575671696521801ff31a380d239adff36ed23195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 12:28:52 +0100 Subject: [PATCH 28/42] ENH: change itkDebugMacro to otbLogMacro --- include/otbTensorflowCopyUtils.cxx | 2 +- include/otbTensorflowGraphOperations.cxx | 10 +++++----- include/otbTensorflowMultisourceModelFilter.hxx | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/otbTensorflowCopyUtils.cxx b/include/otbTensorflowCopyUtils.cxx index 27f1433f..497d0ae9 100644 --- a/include/otbTensorflowCopyUtils.cxx +++ b/include/otbTensorflowCopyUtils.cxx @@ -405,7 +405,7 @@ ValueToTensor(std::string value) } idx++; } - otbLogMacro(Debug, "Returning tensor: "<< out.DebugString()); + otbLogMacro(Debug, << "Returning tensor: "<< out.DebugString()); return out; } diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 5d3d42f0..56993075 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -86,11 +86,11 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow dataTypes.clear(); dataTypes.reserve(tensorsNames.size()); - otbLogMacro(Debug, "Nodes contained in the model: "); + otbLogMacro(Debug, << << "Nodes contained in the model: "); int i = 0; for (auto const & layer : layers) { - otbLogMacro(Debug, "Node "<< i << " inside the model: " << layer.first); + otbLogMacro(Debug, << << "Node "<< i << " inside the model: " << layer.first); i+=1; } @@ -114,7 +114,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow { layerNames.push_back(layer.second.name()); tensor_info = layer.second; - otbLogMacro(Debug, "Input " << k << "corresponds to" << layer.first << " in the model"); + otbLogMacro(Debug, << "Input " << k << "corresponds to" << layer.first << " in the model"); } j+=1; } @@ -123,7 +123,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow // Else, if the user specified the placeholdername, find the corresponding layer inside the model else { - otbLogMacro(Debug, "Searching for corresponding node of: " << (*nameIt) << "... "); + otbLogMacro(Debug, << "Searching for corresponding node of: " << (*nameIt) << "... "); for (auto const & layer : layers) { // layer is a pair (name, tensor_info) @@ -134,7 +134,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow found = true; layerNames.push_back(layer.second.name()); tensor_info = layer.second; - otbLogMacro(Debug, "Found: " << layer.second.name() << " in the model"); + otbLogMacro(Debug, << "Found: " << layer.second.name() << " in the model"); } } // next layer } //end else diff --git a/include/otbTensorflowMultisourceModelFilter.hxx b/include/otbTensorflowMultisourceModelFilter.hxx index 3453ad56..b7e4f873 100644 --- a/include/otbTensorflowMultisourceModelFilter.hxx +++ b/include/otbTensorflowMultisourceModelFilter.hxx @@ -333,7 +333,7 @@ TensorflowMultisourceModelFilter<TInputImage, TOutputImage> if (!OutputRegionToInputRegion(requestedRegion, inRegion, inputImage) ) { // Image does not overlap requested region: set requested region to null - otbLogMacro(Debug, << "Image #" << i << " :\n" << inRegion << " is outside the requested region"); + otbLogMacro(Debug, << << "Image #" << i << " :\n" << inRegion << " is outside the requested region"); inRegion.GetModifiableIndex().Fill(0); inRegion.GetModifiableSize().Fill(0); } -- GitLab From af779ef792d64c1114df3ae470b03106ae0feb3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 12:30:56 +0100 Subject: [PATCH 29/42] FIX: syntax --- include/otbTensorflowGraphOperations.cxx | 4 ++-- include/otbTensorflowMultisourceModelFilter.hxx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 56993075..578ba24e 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -86,11 +86,11 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow dataTypes.clear(); dataTypes.reserve(tensorsNames.size()); - otbLogMacro(Debug, << << "Nodes contained in the model: "); + otbLogMacro(Debug, << "Nodes contained in the model: "); int i = 0; for (auto const & layer : layers) { - otbLogMacro(Debug, << << "Node "<< i << " inside the model: " << layer.first); + otbLogMacro(Debug, << "Node "<< i << " inside the model: " << layer.first); i+=1; } diff --git a/include/otbTensorflowMultisourceModelFilter.hxx b/include/otbTensorflowMultisourceModelFilter.hxx index b7e4f873..7a80102e 100644 --- a/include/otbTensorflowMultisourceModelFilter.hxx +++ b/include/otbTensorflowMultisourceModelFilter.hxx @@ -333,7 +333,7 @@ TensorflowMultisourceModelFilter<TInputImage, TOutputImage> if (!OutputRegionToInputRegion(requestedRegion, inRegion, inputImage) ) { // Image does not overlap requested region: set requested region to null - otbLogMacro(Debug, << << "Image #" << i << " :\n" << inRegion << " is outside the requested region"); + otbLogMacro(Debug, << "Image #" << i << " :\n" << inRegion << " is outside the requested region"); inRegion.GetModifiableIndex().Fill(0); inRegion.GetModifiableSize().Fill(0); } -- GitLab From 6800a00d1fd9fc3a75ad9187657c85df72e7515f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 12:37:05 +0100 Subject: [PATCH 30/42] STYLE --- include/otbTensorflowGraphOperations.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 578ba24e..d677963e 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -114,7 +114,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow { layerNames.push_back(layer.second.name()); tensor_info = layer.second; - otbLogMacro(Debug, << "Input " << k << "corresponds to" << layer.first << " in the model"); + otbLogMacro(Debug, << "Input " << k << " corresponds to " << layer.first << " in the model"); } j+=1; } -- GitLab From c8eef12d6bde314a11f41ac4b68fda70dcdbc13c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 14:03:52 +0100 Subject: [PATCH 31/42] ENH: remove check "m_OutputExpressionFields.size() != m_OutputTensors.size()" + moving a piece of code --- include/otbTensorflowGraphOperations.cxx | 7 ++++++- include/otbTensorflowMultisourceModelBase.hxx | 15 --------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index d677963e..587448fb 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -90,10 +90,15 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow int i = 0; for (auto const & layer : layers) { - otbLogMacro(Debug, << "Node "<< i << " inside the model: " << layer.first); + otbLogMacro(Debug, << "\tNode "<< i << " inside the model: " << layer.first); i+=1; } + // When the user doesn't specify output.names, m_OutputTensors defaults to an empty list that we can not iterate over. + // We change it to a list containing an empty string [""] + if (tensorsNames.size() == 0) + tensorsNames.push_back(""); + // Get infos int k = 0; // counter used for tensorsNames for (std::vector<std::string>::iterator nameIt = tensorsNames.begin(); diff --git a/include/otbTensorflowMultisourceModelBase.hxx b/include/otbTensorflowMultisourceModelBase.hxx index 700b9df3..ca7ec5d1 100644 --- a/include/otbTensorflowMultisourceModelBase.hxx +++ b/include/otbTensorflowMultisourceModelBase.hxx @@ -157,27 +157,12 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage> " and the number of input tensors names is " << m_InputPlaceholders.size()); } - // When the user specifies the output names, check that the number of the following is the same - // - output tensors names - // - output expression fields - if ((m_OutputTensors.size() != 0) and (m_OutputExpressionFields.size() != m_OutputTensors.size())) - { - itkExceptionMacro("Number of output tensors names is " << m_OutputTensors.size() << - " but the number of output fields of expression is " << m_OutputExpressionFields.size()); - } - ////////////////////////////////////////////////////////////////////////////////////////// // Get tensors information ////////////////////////////////////////////////////////////////////////////////////////// // Set all subelement of the model auto signaturedef = this->GetSignatureDef(); - // When the user doesn't specify output.names, m_OutputTensors defaults to an empty string "". We change it to a - // list containing an empty string [""] - if (m_OutputTensors.size() == 0) - m_OutputTensors = {""}; - - // Given the inputs/outputs names that the user specified, get the names of the inputs/outputs contained in the model // and other infos (shapes, dtypes) // For example, for output names specified by the user m_OutputTensors = ['s2t', 's2t_pad'], -- GitLab From 1b72a6c490c7bf836a53e10bf43ff6c35f3f1031 Mon Sep 17 00:00:00 2001 From: Narcon Nicolas <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 14:15:05 +0100 Subject: [PATCH 32/42] WIP: Update CMakeLists.txt to include DEBUG messages --- test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index f7716a13..730b5925 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -49,6 +49,7 @@ set(MODEL4_FC_OUT apTvClTensorflowModelServeFCNN64x64to32x32.tif) set(MODEL1_SAVED model1_updated) #----------- Model training : 1-branch CNN (16x16) Patch-Based ---------------- +set(ENV{OTB_LOGGER_LEVEL} DEBUG) otb_test_application(NAME TensorflowModelTrainCNN16x16PB APP TensorflowModelTrain OPTIONS -- GitLab From 693114c393c384a445bcf09dc0ba13d14fb1bd2a Mon Sep 17 00:00:00 2001 From: Narcon Nicolas <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 14:24:25 +0100 Subject: [PATCH 33/42] WIP: Update CMakeLists.txt to include DEBUG message --- test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 730b5925..29481cda 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,6 +69,7 @@ otb_test_application(NAME TensorflowModelTrainCNN16x16PB -training.targetnodes "optimizer" -validation.mode "class" ) + set_tests_properties(TensorflowModelTrainCNN16x16PB PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG;$ENV{OTB_LOGGER_LEVEL}") #----------- Model serving : 1-branch CNN (16x16) Patch-Based ---------------- otb_test_application(NAME TensorflowModelServeCNN16x16PB -- GitLab From a709919e527a44f5a304f7ec67ada9c17aeb3c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 15:55:12 +0100 Subject: [PATCH 34/42] FIX: add a clearance of layers --- include/otbTensorflowGraphOperations.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 587448fb..1491b775 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -82,9 +82,8 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow { // Allocation shapes.clear(); - shapes.reserve(tensorsNames.size()); dataTypes.clear(); - dataTypes.reserve(tensorsNames.size()); + layers.clear(); otbLogMacro(Debug, << "Nodes contained in the model: "); int i = 0; -- GitLab From c7a16d466158a949754db241e16d96decf95cb11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 15:58:01 +0100 Subject: [PATCH 35/42] FIX: oops .clear applied to wrong variable --- include/otbTensorflowGraphOperations.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index 1491b775..fdcd475c 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -83,7 +83,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow // Allocation shapes.clear(); dataTypes.clear(); - layers.clear(); + layerNames.clear(); otbLogMacro(Debug, << "Nodes contained in the model: "); int i = 0; -- GitLab From 60faccc626b929435b13375192d5af4b588bbdbc Mon Sep 17 00:00:00 2001 From: remi cresson <remi.cresson@inrae.fr> Date: Thu, 4 Nov 2021 16:28:23 +0100 Subject: [PATCH 36/42] COMP: avoid redundant initialization of 'ts_dt' --- include/otbTensorflowGraphOperations.cxx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index b4b7d573..c6075205 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -164,11 +164,8 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten << "running: \n\t `saved_model_cli show --dir your_model_dir --all`"); } - // Set default to DT_FLOAT - tensorflow::DataType ts_dt = tensorflow::DT_FLOAT; - - // Default (input?) tensor type - ts_dt = tensor_info.dtype(); + // Default tensor type + tensorflow::DataType ts_dt = tensor_info.dtype(); dataTypes.push_back(ts_dt); // Get the tensor's shape -- GitLab From 4ff2292b8975bea1d53eec3a886d2581eb2162b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 16:35:55 +0100 Subject: [PATCH 37/42] WIP: set debug for logs of tests --- test/CMakeLists.txt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 29481cda..0f5a38c0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,7 +69,7 @@ otb_test_application(NAME TensorflowModelTrainCNN16x16PB -training.targetnodes "optimizer" -validation.mode "class" ) - set_tests_properties(TensorflowModelTrainCNN16x16PB PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG;$ENV{OTB_LOGGER_LEVEL}") +set_tests_properties(TensorflowModelTrainCNN16x16PB PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG;$ENV{OTB_LOGGER_LEVEL}") #----------- Model serving : 1-branch CNN (16x16) Patch-Based ---------------- otb_test_application(NAME TensorflowModelServeCNN16x16PB @@ -81,6 +81,7 @@ otb_test_application(NAME TensorflowModelServeCNN16x16PB VALID --compare-image ${EPSILON_6} ${DATADIR}/${MODEL1_PB_OUT} ${TEMP}/${MODEL1_PB_OUT}) +set_tests_properties(TensorflowModelServeCNN16x16PB PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG}") #----------- Model serving : 2-branch CNN (8x8, 32x32) Patch-Based ---------------- otb_test_application(NAME apTvClTensorflowModelServeCNN8x8_32x32PB @@ -94,7 +95,8 @@ otb_test_application(NAME apTvClTensorflowModelServeCNN8x8_32x32PB VALID --compare-image ${EPSILON_6} ${DATADIR}/${MODEL2_PB_OUT} ${TEMP}/${MODEL2_PB_OUT}) -set_tests_properties(apTvClTensorflowModelServeCNN8x8_32x32PB PROPERTIES ENVIRONMENT "OTB_TF_NSOURCES=2;$ENV{OTB_TF_NSOURCES}") +set_tests_properties(apTvClTensorflowModelServeCNN8x8_32x32PB PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG;OTB_TF_NSOURCES=2;$ENV{OTB_TF_NSOURCES}") + #----------- Model serving : 2-branch CNN (8x8, 32x32) Fully-Conv ---------------- set(ENV{OTB_TF_NSOURCES} 2) @@ -109,7 +111,7 @@ otb_test_application(NAME apTvClTensorflowModelServeCNN8x8_32x32FC VALID --compare-image ${EPSILON_6} ${DATADIR}/${MODEL2_FC_OUT} ${TEMP}/${MODEL2_FC_OUT}) -set_tests_properties(apTvClTensorflowModelServeCNN8x8_32x32FC PROPERTIES ENVIRONMENT "OTB_TF_NSOURCES=2;$ENV{OTB_TF_NSOURCES}") +set_tests_properties(apTvClTensorflowModelServeCNN8x8_32x32FC PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG;OTB_TF_NSOURCES=2;$ENV{OTB_TF_NSOURCES}") #----------- Model serving : 1-branch FCNN (16x16) Patch-Based ---------------- set(ENV{OTB_TF_NSOURCES} 1) @@ -122,6 +124,8 @@ otb_test_application(NAME apTvClTensorflowModelServeFCNN16x16PB VALID --compare-image ${EPSILON_6} ${DATADIR}/${MODEL3_PB_OUT} ${TEMP}/${MODEL3_PB_OUT}) +set_tests_properties(apTvClTensorflowModelServeFCNN16x16PB PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG}") + #----------- Model serving : 1-branch FCNN (16x16) Fully-conv ---------------- set(ENV{OTB_TF_NSOURCES} 1) @@ -134,6 +138,7 @@ otb_test_application(NAME apTvClTensorflowModelServeFCNN16x16FC VALID --compare-image ${EPSILON_6} ${DATADIR}/${MODEL3_FC_OUT} ${TEMP}/${MODEL3_FC_OUT}) +set_tests_properties(apTvClTensorflowModelServeFCNN16x16FC PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG}") #----------- Model serving : 1-branch FCNN (64x64)-->(32x32), Fully-conv ---------------- set(ENV{OTB_TF_NSOURCES} 1) @@ -147,5 +152,6 @@ otb_test_application(NAME apTvClTensorflowModelServeFCNN64x64to32x32.tif VALID --compare-image ${EPSILON_6} ${DATADIR}/${MODEL4_FC_OUT} ${TEMP}/${MODEL4_FC_OUT}) +set_tests_properties(apTvClTensorflowModelServeFCNN64x64to32x32 PROPERTIES ENVIRONMENT "OTB_LOGGER_LEVEL=DEBUG}") -- GitLab From 3576dc6fcb1419ddc8b1974cf97f7265f52405dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Nar=C3=A7on?= <nicolas.narcon@inrae.fr> Date: Thu, 4 Nov 2021 16:40:54 +0100 Subject: [PATCH 38/42] FIX: typo --- test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0f5a38c0..eaf6dbd7 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -142,7 +142,7 @@ set_tests_properties(apTvClTensorflowModelServeFCNN16x16FC PROPERTIES ENVIRONMEN #----------- Model serving : 1-branch FCNN (64x64)-->(32x32), Fully-conv ---------------- set(ENV{OTB_TF_NSOURCES} 1) -otb_test_application(NAME apTvClTensorflowModelServeFCNN64x64to32x32.tif +otb_test_application(NAME apTvClTensorflowModelServeFCNN64x64to32x32 APP TensorflowModelServe OPTIONS -source1.il ${IMAGEPXS2} -source1.rfieldx 64 -source1.rfieldy 64 -source1.placeholder x -- GitLab From f22afc1537cc72a35c739099b4c6574eed71e850 Mon Sep 17 00:00:00 2001 From: remi cresson <remi.cresson@inrae.fr> Date: Thu, 4 Nov 2021 17:21:32 +0100 Subject: [PATCH 39/42] ENH: simplify select the k-th element of --- include/otbTensorflowGraphOperations.cxx | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index c6075205..f6d29bb2 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -122,18 +122,11 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten { found = true; // select the k-th element of `layers` - int j = 0; - for (auto const & layer : layers) - { - - if (j == k) - { - layerNames.push_back(layer.second.name()); - tensor_info = layer.second; - otbLogMacro(Debug, << "Input " << k << " corresponds to " << layer.first << " in the model"); - } - j += 1; - } + auto it = layers.begin(); + std::advance(it, k); + layerNames.push_back(it->second.name()); + tensor_info = it->second; + otbLogMacro(Debug, << "Input " << k << " corresponds to " << it->first << " in the model"); } // Else, if the user specified the placeholdername, find the corresponding layer inside the model -- GitLab From e59843437812e519e6287313678c900f1b9655af Mon Sep 17 00:00:00 2001 From: remi cresson <remi.cresson@inrae.fr> Date: Thu, 4 Nov 2021 17:39:11 +0100 Subject: [PATCH 40/42] REFAC: simplifications --- include/otbTensorflowGraphOperations.cxx | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index f6d29bb2..f2d0f7fb 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -94,13 +94,10 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten dataTypes.clear(); layerNames.clear(); + // Debug infos otbLogMacro(Debug, << "Nodes contained in the model: "); - int i = 0; for (auto const & layer : layers) - { - otbLogMacro(Debug, << "\tNode " << i << " inside the model: " << layer.first); - i += 1; - } + otbLogMacro(Debug, << "\t" << layer.first); // When the user doesn't specify output.names, m_OutputTensors defaults to an empty list that we can not iterate over. // We change it to a list containing an empty string [""] @@ -110,15 +107,15 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten tensorsNames.push_back(""); } - // Get infos + // Next, we fill layerNames int k = 0; // counter used for tensorsNames - for (std::vector<std::string>::iterator nameIt = tensorsNames.begin(); nameIt != tensorsNames.end(); ++nameIt) + for (auto const & name: tensorsNames) { bool found = false; tensorflow::TensorInfo tensor_info; // If the user didn't specify the placeholdername, choose the kth layer inside the model - if (nameIt->size() == 0) + if (name.size() == 0) { found = true; // select the k-th element of `layers` @@ -132,13 +129,13 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten // Else, if the user specified the placeholdername, find the corresponding layer inside the model else { - otbLogMacro(Debug, << "Searching for corresponding node of: " << (*nameIt) << "... "); + otbLogMacro(Debug, << "Searching for corresponding node of: " << name << "... "); for (auto const & layer : layers) { // layer is a pair (name, tensor_info) // cf https://stackoverflow.com/questions/63181951/how-to-get-graph-or-graphdef-from-a-given-model std::string layername = layer.first; - if (layername.substr(0, layername.find(":")).compare((*nameIt)) == 0) + if (layername.substr(0, layername.find(":")).compare(name) == 0) { found = true; layerNames.push_back(layer.second.name()); @@ -146,7 +143,7 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten otbLogMacro(Debug, << "Found: " << layer.second.name() << " in the model"); } } // next layer - } // end else + } // end else k += 1; -- GitLab From a7416e3c907373191c531f9d3f0602d3924e6ff9 Mon Sep 17 00:00:00 2001 From: remi cresson <remi.cresson@inrae.fr> Date: Thu, 4 Nov 2021 17:41:37 +0100 Subject: [PATCH 41/42] REFAC: simplifications --- include/otbTensorflowGraphOperations.cxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index f2d0f7fb..c53d3883 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -149,7 +149,7 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten if (!found) { - itkGenericExceptionMacro("Tensor name \"" << (*nameIt) << "\" not found. \n" + itkGenericExceptionMacro("Tensor name \"" << name << "\" not found. \n" << "You can list all inputs/outputs of your SavedModel by " << "running: \n\t `saved_model_cli show --dir your_model_dir --all`"); } @@ -178,9 +178,9 @@ PrintNodeAttributes(const tensorflow::GraphDef & graph, std::vector<std::string> tensorflow::NodeDef node = graph.node(i); std::cout << i << "\t" << node.name() << std::endl; - for (std::vector<std::string>::iterator nameIt = nodesNames.begin(); nameIt != nodesNames.end(); ++nameIt) + for (auto const & name: nodesNames) { - if (node.name().compare((*nameIt)) == 0) + if (node.name().compare(name) == 0) { std::cout << "Node " << i << " : " << std::endl; std::cout << "\tName: " << node.name() << std::endl; -- GitLab From 247e71f0287b514f46b3a6818158fe0192ca123f Mon Sep 17 00:00:00 2001 From: Remi Cresson <remi.cresson@irstea.fr> Date: Thu, 4 Nov 2021 19:57:14 +0100 Subject: [PATCH 42/42] COMP: PrintNodeAttributes() has const variables --- include/otbTensorflowGraphOperations.cxx | 2 +- include/otbTensorflowGraphOperations.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/otbTensorflowGraphOperations.cxx b/include/otbTensorflowGraphOperations.cxx index c53d3883..d40c4da6 100644 --- a/include/otbTensorflowGraphOperations.cxx +++ b/include/otbTensorflowGraphOperations.cxx @@ -169,7 +169,7 @@ GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow::Ten // Print a lot of stuff about the specified nodes of the graph // void -PrintNodeAttributes(const tensorflow::GraphDef & graph, std::vector<std::string> & nodesNames) +PrintNodeAttributes(const tensorflow::GraphDef & graph, const std::vector<std::string> & nodesNames) { std::cout << "Go through graph:" << std::endl; std::cout << "#\tname" << std::endl; diff --git a/include/otbTensorflowGraphOperations.h b/include/otbTensorflowGraphOperations.h index 1d823cfe..6ad4a4e2 100644 --- a/include/otbTensorflowGraphOperations.h +++ b/include/otbTensorflowGraphOperations.h @@ -47,7 +47,7 @@ void GetTensorAttributes(const tensorflow::protobuf::Map<std::string, tensorflow std::vector<tensorflow::TensorShapeProto> & shapes, std::vector<tensorflow::DataType> & dataTypes); // Print a lot of stuff about the specified nodes of the graph -void PrintNodeAttributes(const tensorflow::GraphDef & graph, std::vector<std::string> & nodesNames); +void PrintNodeAttributes(const tensorflow::GraphDef & graph, const std::vector<std::string> & nodesNames); } // end namespace tf } // end namespace otb -- GitLab