Skip to content

Commit 86d5e05

Browse files
committed
Let onnxRuntime handles output memory
1 parent dd64da8 commit 86d5e05

File tree

2 files changed

+16
-65
lines changed

2 files changed

+16
-65
lines changed

src/aliceVision/segmentation/segmentation.cpp

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ bool Segmentation::initialize()
6767
_ortSession = std::make_unique<Ort::Session>(*_ortEnvironment, _parameters.modelWeights.c_str(), ortSessionOptions);
6868
#endif
6969

70-
Ort::MemoryInfo memInfoCuda("Cuda", OrtAllocatorType::OrtArenaAllocator, 0, OrtMemType::OrtMemTypeDefault);
71-
Ort::Allocator cudaAllocator(*_ortSession, memInfoCuda);
72-
7370
_output.resize(_parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
74-
_cudaInput = cudaAllocator.Alloc(_output.size() * sizeof(float));
7571
#endif
7672
}
7773
else
@@ -87,20 +83,6 @@ bool Segmentation::initialize()
8783
return true;
8884
}
8985

90-
bool Segmentation::terminate()
91-
{
92-
if (_parameters.useGpu)
93-
{
94-
#if ALICEVISION_IS_DEFINED(ALICEVISION_HAVE_ONNX_GPU)
95-
Ort::MemoryInfo mem_info_cuda("Cuda", OrtAllocatorType::OrtArenaAllocator, 0, OrtMemType::OrtMemTypeDefault);
96-
Ort::Allocator cudaAllocator(*_ortSession, mem_info_cuda);
97-
cudaAllocator.Free(_cudaInput);
98-
#endif
99-
}
100-
101-
return true;
102-
}
103-
10486
bool Segmentation::processImage(image::Image<IndexT>& labels, const image::Image<image::RGBfColor>& source)
10587
{
10688
// Todo : handle orientation and small images smaller than model input
@@ -299,34 +281,23 @@ bool Segmentation::processTile(image::Image<ScoredLabel>& labels, const image::I
299281
return false;
300282
}
301283

302-
std::vector<float> output(_parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
303-
int idx = 0;
304-
for (int ch = 0; ch < _parameters.classes.size(); ch++)
305-
{
306-
for (int i = 0; i < _parameters.modelHeight; i++)
307-
{
308-
for (int j = 0; j < _parameters.modelWidth; j++)
309-
{
310-
const std::vector<int64_t> coords = {0, ch, i, j};
311-
output[idx++] = outTensor[0].At<float>(coords);
312-
}
313-
}
314-
}
315-
316284
if (!labelsFromOutputTensor(labels, outTensor[0]))
317285
{
318286
return false;
319287
}
320288

289+
std::vector<float> output(_parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
290+
auto *outTData = outTensor.front().GetTensorMutableData<float>();
291+
output.assign(outTData, outTData + _parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
292+
321293
return true;
322294
}
323295

324296
bool Segmentation::processTileGPU(image::Image<ScoredLabel>& labels, const image::Image<image::RGBfColor>::Base& source)
325297
{
326298
ALICEVISION_LOG_TRACE("Process tile using gpu");
327299
#if ALICEVISION_IS_DEFINED(ALICEVISION_HAVE_CUDA)
328-
Ort::MemoryInfo mem_info_cuda("Cuda", OrtAllocatorType::OrtArenaAllocator, 0, OrtMemType::OrtMemTypeDefault);
329-
Ort::Allocator cudaAllocator(*_ortSession, mem_info_cuda);
300+
Ort::MemoryInfo memInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
330301

331302
std::vector<const char*> inputNames{"input"};
332303
std::vector<const char*> outputNames{"output"};
@@ -335,41 +306,33 @@ bool Segmentation::processTileGPU(image::Image<ScoredLabel>& labels, const image
335306
std::vector<float> transformedInput;
336307
imageToPlanes(transformedInput, source);
337308

338-
cudaMemcpy(_cudaInput, transformedInput.data(), sizeof(float) * transformedInput.size(), cudaMemcpyHostToDevice);
339-
340-
Ort::Value inputTensors = Ort::Value::CreateTensor<float>(
341-
mem_info_cuda, reinterpret_cast<float*>(_cudaInput), transformedInput.size(), inputDimensions.data(), inputDimensions.size());
309+
std::vector<Ort::Value> inputTensors;
310+
inputTensors.emplace_back(Ort::Value::CreateTensor<float>(memInfo,
311+
transformedInput.data(),
312+
transformedInput.size(),
313+
inputDimensions.data(),
314+
inputDimensions.size()));
342315

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

345318
try
346319
{
347-
outTensor = _ortSession->Run(Ort::RunOptions{nullptr}, inputNames.data(), &inputTensors, 1, outputNames.data(), 1);
320+
outTensor = _ortSession->Run(Ort::RunOptions{nullptr}, inputNames.data(), inputTensors.data(), 1, outputNames.data(), 1);
348321
}
349322
catch (const Ort::Exception& exception)
350323
{
351324
ALICEVISION_LOG_ERROR("ERROR running model inference: " << exception.what());
352325
return false;
353326
}
354327

355-
int idx = 0;
356-
for (int ch = 0; ch < _parameters.classes.size(); ch++)
357-
{
358-
for (int i = 0; i < _parameters.modelHeight; i++)
359-
{
360-
for (int j = 0; j < _parameters.modelWidth; j++)
361-
{
362-
const std::vector<int64_t> coords = {0, ch, i, j};
363-
_output[idx++] = outTensor[0].At<float>(coords);
364-
}
365-
}
366-
}
367-
368328
if (!labelsFromOutputTensor(labels, outTensor[0]))
369329
{
370330
return false;
371331
}
372332

333+
auto *outTData = outTensor.front().GetTensorMutableData<float>();
334+
_output.assign(outTData, outTData + _parameters.classes.size() * _parameters.modelHeight * _parameters.modelWidth);
335+
373336
#endif
374337

375338
return true;

src/aliceVision/segmentation/segmentation.hpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Segmentation
5757
}
5858
}
5959

60-
virtual ~Segmentation() { terminate(); }
60+
virtual ~Segmentation() {}
6161

6262
/**
6363
* Process an input image to estimate segmentation
@@ -72,25 +72,13 @@ class Segmentation
7272
*/
7373
bool initialize();
7474

75-
/**
76-
* Onnx destruction code
77-
*/
78-
bool terminate();
79-
8075
/**
8176
* Assume the source image is the correct size
8277
* @param labels the output label image
8378
* @param source the input image to process
8479
*/
8580
bool tiledProcess(image::Image<IndexT>& labels, const image::Image<image::RGBfColor>& source);
8681

87-
/**
88-
* Transform model output to a label image
89-
* @param labels the output labels image
90-
* @param modeloutput the model output vector
91-
*/
92-
bool labelsFromModelOutput(image::Image<ScoredLabel>& labels, const std::vector<float>& modelOutput);
93-
9482
/**
9583
* Transform model output to a label image
9684
* @param labels the output labels image

0 commit comments

Comments
 (0)