Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image segmentation cpu bugfix and onnx output update #1783

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions src/aliceVision/segmentation/segmentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ bool Segmentation::initialize()

_output.resize(_parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
_cudaInput = cudaAllocator.Alloc(_output.size() * sizeof(float));
_cudaOutput = cudaAllocator.Alloc(_output.size() * sizeof(float));
#endif
}
else
Expand All @@ -90,12 +89,14 @@ bool Segmentation::initialize()

bool Segmentation::terminate()
{
if (_parameters.useGpu)
{
#if ALICEVISION_IS_DEFINED(ALICEVISION_HAVE_ONNX_GPU)
Ort::MemoryInfo mem_info_cuda("Cuda", OrtAllocatorType::OrtArenaAllocator, 0, OrtMemType::OrtMemTypeDefault);
Ort::Allocator cudaAllocator(*_ortSession, mem_info_cuda);
cudaAllocator.Free(_cudaInput);
cudaAllocator.Free(_cudaOutput);
Ort::MemoryInfo mem_info_cuda("Cuda", OrtAllocatorType::OrtArenaAllocator, 0, OrtMemType::OrtMemTypeDefault);
Ort::Allocator cudaAllocator(*_ortSession, mem_info_cuda);
cudaAllocator.Free(_cudaInput);
demoulinv marked this conversation as resolved.
Show resolved Hide resolved
#endif
}

return true;
}
Expand Down Expand Up @@ -244,7 +245,7 @@ bool Segmentation::mergeLabels(image::Image<ScoredLabel>& labels, image::Image<S
return true;
}

bool Segmentation::labelsFromModelOutput(image::Image<ScoredLabel>& labels, const std::vector<float>& modelOutput)
bool Segmentation::labelsFromOutputTensor(image::Image<ScoredLabel>& labels, Ort::Value& modelOutput)
{
for (int outputY = 0; outputY < _parameters.modelHeight; outputY++)
{
Expand All @@ -255,10 +256,8 @@ bool Segmentation::labelsFromModelOutput(image::Image<ScoredLabel>& labels, cons

for (int classe = 0; classe < _parameters.classes.size(); classe++)
{
int classPos = classe * _parameters.modelWidth * _parameters.modelHeight;
int pos = classPos + outputY * _parameters.modelWidth + outputX;

float val = modelOutput[pos];
const std::vector<int64_t> coords = {0,classe,outputY,outputX};
const float val = modelOutput.At<float>(coords);
if (val > maxVal)
{
maxVal = val;
Expand All @@ -281,29 +280,40 @@ bool Segmentation::processTile(image::Image<ScoredLabel>& labels, const image::I
std::vector<const char*> inputNames{"input"};
std::vector<const char*> outputNames{"output"};
std::vector<int64_t> inputDimensions = {1, 3, _parameters.modelHeight, _parameters.modelWidth};
std::vector<int64_t> outputDimensions = {1, static_cast<int64_t>(_parameters.classes.size()), _parameters.modelHeight, _parameters.modelWidth};

std::vector<float> output(_parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
Ort::Value outputTensors =
Ort::Value::CreateTensor<float>(memInfo, output.data(), output.size(), outputDimensions.data(), outputDimensions.size());

std::vector<float> transformedInput;
imageToPlanes(transformedInput, source);

Ort::Value inputTensors =
Ort::Value::CreateTensor<float>(memInfo, transformedInput.data(), transformedInput.size(), inputDimensions.data(), inputDimensions.size());

std::vector<Ort::Value> outTensor;

try
{
_ortSession->Run(Ort::RunOptions{nullptr}, inputNames.data(), &inputTensors, 1, outputNames.data(), &outputTensors, 1);
outTensor = _ortSession->Run(Ort::RunOptions{nullptr}, inputNames.data(), &inputTensors, 1, outputNames.data(), 1);
}
catch (const Ort::Exception& exception)
{
ALICEVISION_LOG_ERROR("ERROR running model inference: " << exception.what());
return false;
}

if (!labelsFromModelOutput(labels, output))
std::vector<float> output(_parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
int idx = 0;
for (int ch = 0; ch < _parameters.classes.size(); ch++)
{
for (int i = 0; i < _parameters.modelHeight; i++)
{
for (int j = 0; j < _parameters.modelWidth; j++)
{
const std::vector<int64_t> coords = {0, ch, i, j};
output[idx++] = outTensor[0].At<float>(coords);
}
}
}

if (!labelsFromOutputTensor(labels, outTensor[0]))
demoulinv marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}
Expand All @@ -321,10 +331,6 @@ bool Segmentation::processTileGPU(image::Image<ScoredLabel>& labels, const image
std::vector<const char*> inputNames{"input"};
std::vector<const char*> outputNames{"output"};
std::vector<int64_t> inputDimensions = {1, 3, _parameters.modelHeight, _parameters.modelWidth};
std::vector<int64_t> outputDimensions = {1, static_cast<int64_t>(_parameters.classes.size()), _parameters.modelHeight, _parameters.modelWidth};

Ort::Value outputTensors = Ort::Value::CreateTensor<float>(
mem_info_cuda, reinterpret_cast<float*>(_cudaOutput), _output.size(), outputDimensions.data(), outputDimensions.size());

std::vector<float> transformedInput;
imageToPlanes(transformedInput, source);
Expand All @@ -334,19 +340,32 @@ bool Segmentation::processTileGPU(image::Image<ScoredLabel>& labels, const image
Ort::Value inputTensors = Ort::Value::CreateTensor<float>(
mem_info_cuda, reinterpret_cast<float*>(_cudaInput), transformedInput.size(), inputDimensions.data(), inputDimensions.size());
demoulinv marked this conversation as resolved.
Show resolved Hide resolved

std::vector<Ort::Value> outTensor;

try
{
_ortSession->Run(Ort::RunOptions{nullptr}, inputNames.data(), &inputTensors, 1, outputNames.data(), &outputTensors, 1);
outTensor = _ortSession->Run(Ort::RunOptions{nullptr}, inputNames.data(), &inputTensors, 1, outputNames.data(), 1);
}
catch (const Ort::Exception& exception)
{
ALICEVISION_LOG_ERROR("ERROR running model inference: " << exception.what());
return false;
}

cudaMemcpy(_output.data(), _cudaOutput, sizeof(float) * _output.size(), cudaMemcpyDeviceToHost);
int idx = 0;
for (int ch = 0; ch < _parameters.classes.size(); ch++)
{
for (int i = 0; i < _parameters.modelHeight; i++)
{
for (int j = 0; j < _parameters.modelWidth; j++)
{
const std::vector<int64_t> coords = {0, ch, i, j};
_output[idx++] = outTensor[0].At<float>(coords);
}
}
}

if (!labelsFromModelOutput(labels, _output))
if (!labelsFromOutputTensor(labels, outTensor[0]))
demoulinv marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}
Expand Down
9 changes: 8 additions & 1 deletion src/aliceVision/segmentation/segmentation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ class Segmentation

/**
* Transform model output to a label image
* @param labels the output labels imaage
* @param labels the output labels image
* @param modeloutput the model output vector
*/
bool labelsFromModelOutput(image::Image<ScoredLabel>& labels, const std::vector<float>& modelOutput);
demoulinv marked this conversation as resolved.
Show resolved Hide resolved

/**
* Transform model output to a label image
* @param labels the output labels image
* @param modeloutput the model output tensor
*/
bool labelsFromOutputTensor(image::Image<ScoredLabel>& labels, Ort::Value& modelOutput);

/**
* Process effectively a buffer of the model input size
* param labels the output labels
Expand Down
Loading