@@ -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-
10486bool 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
324296bool 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 ;
0 commit comments