Commit b5d53231 authored by remi cresson's avatar remi cresson
Browse files

ENH: better exception messages

parent 5c0ec1a9
......@@ -108,6 +108,8 @@ protected:
TensorflowMultisourceModelBase();
virtual ~TensorflowMultisourceModelBase() {};
virtual std::stringstream GenerateDebugReport(DictListType & inputs, TensorListType & outputs);
virtual void RunSession(DictListType & inputs, TensorListType & outputs);
private:
......
......@@ -32,6 +32,40 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage>
m_InputPlaceholdersNames.push_back(placeholder);
}
template <class TInputImage, class TOutputImage>
std::stringstream
TensorflowMultisourceModelBase<TInputImage, TOutputImage>
::GenerateDebugReport(DictListType & inputs, TensorListType & outputs)
{
// Create a debug report
std::stringstream debugReport;
// Describe the output buffered region
ImagePointerType outputPtr = this->GetOutput();
const RegionType outputReqRegion = outputPtr->GetRequestedRegion();
debugReport << "Output image buffered region: " << outputReqRegion << "\n";
// Describe inputs
for (unsigned int i = 0 ; i < this->GetNumberOfInputs() ; i++)
{
const ImagePointerType inputPtr = const_cast<TInputImage*>(this->GetInput(i));
const RegionType reqRegion = inputPtr->GetRequestedRegion();
debugReport << "Input #" << i << ":\n";
debugReport << "Requested region: " << reqRegion << "\n";
debugReport << "Tensor shape (\"" << inputs[i].first << "\": " << tf::PrintTensorShape(inputs[i].second.shape()) << "\n";
}
// Show user placeholders
debugReport << "User placeholders:\n" ;
for (auto& dict: this->GetUserPlaceholders())
{
debugReport << dict.first << " " << tf::PrintTensorInfos(dict.second) << "\n" << std::endl;
}
return debugReport;
}
template <class TInputImage, class TOutputImage>
void
TensorflowMultisourceModelBase<TInputImage, TOutputImage>
......@@ -52,29 +86,7 @@ TensorflowMultisourceModelBase<TInputImage, TOutputImage>
if (!status.ok()) {
// Create a debug report
std::stringstream debugReport;
// Describe the output buffered region
ImagePointerType outputPtr = this->GetOutput();
const RegionType outputReqRegion = outputPtr->GetRequestedRegion();
debugReport << "Output image buffered region: " << outputReqRegion << "\n";
// Describe inputs
for (unsigned int i = 0 ; i < this->GetNumberOfInputs() ; i++)
{
const ImagePointerType inputPtr = const_cast<TInputImage*>(this->GetInput(i));
const RegionType reqRegion = inputPtr->GetRequestedRegion();
debugReport << "Input #" << i << ":\n";
debugReport << "Requested region: " << reqRegion << "\n";
debugReport << "Tensor shape (\"" << inputs[i].first << "\": " << tf::PrintTensorShape(inputs[i].second.shape()) << "\n";
}
// Show user placeholders
debugReport << "User placeholders:\n" ;
for (auto& dict: this->GetUserPlaceholders())
{
debugReport << dict.first << " " << tf::PrintTensorInfos(dict.second) << "\n" << std::endl;
}
std::stringstream debugReport = GenerateDebugReport(inputs, outputs);
// Throw an exception with the report
itkExceptionMacro("Can't run the tensorflow session !\n" <<
......
......@@ -454,7 +454,18 @@ TensorflowMultisourceModelFilter<TInputImage, TOutputImage>
// The offset (i.e. the starting index of the channel for the output tensor) is updated
// during this call
// TODO: implement a generic strategy enabling FOE copy in patch-based mode (see tf::CopyTensorToImageRegion)
tf::CopyTensorToImageRegion<TOutputImage> (outputs[i], outputAlignedReqRegion, outputPtr, outputReqRegion, bandOffset);
try
{
tf::CopyTensorToImageRegion<TOutputImage> (outputs[i],
outputAlignedReqRegion, outputPtr, outputReqRegion, bandOffset);
}
catch( itk::ExceptionObject & err )
{
std::stringstream debugMsg = this->GenerateDebugReport(inputs, outputs);
itkExceptionMacro("Error occured during tensor to image conversion.\n"
<< "Context: " << debugMsg.str()
<< "Error:" << err);
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment