@@ -80,10 +80,12 @@ namespace BindingUtilities
8080 IBuffer buffer = dataWriter.DetachBuffer ();
8181
8282 // Create the software bitmap
83- return SoftwareBitmap::CreateCopyFromBuffer (buffer, TypeHelper::GetBitmapPixelFormat (inputDataType), static_cast <int32_t >(width), static_cast <int32_t >(height));
83+ return SoftwareBitmap::CreateCopyFromBuffer (buffer, TypeHelper::GetBitmapPixelFormat (inputDataType),
84+ static_cast <int32_t >(width), static_cast <int32_t >(height));
8485 }
8586
86- SoftwareBitmap LoadImageFile (const TensorFeatureDescriptor& imageDescriptor, InputDataType inputDataType, const hstring& filePath, const CommandLineArgs& args, uint32_t iterationNum)
87+ SoftwareBitmap LoadImageFile (const TensorFeatureDescriptor& imageDescriptor, InputDataType inputDataType,
88+ const hstring& filePath, const CommandLineArgs& args, uint32_t iterationNum)
8789 {
8890 assert (inputDataType != InputDataType::Tensor);
8991
@@ -103,12 +105,11 @@ namespace BindingUtilities
103105 BitmapDecoder decoder = BitmapDecoder::CreateAsync (stream).get ();
104106
105107 // If input dimensions are different from tensor input, then scale / crop while reading
106- if (args.IsAutoScale () &&
107- ( decoder.PixelHeight () != height ||
108- decoder.PixelWidth () != width))
108+ if (args.IsAutoScale () && (decoder.PixelHeight () != height || decoder.PixelWidth () != width))
109109 {
110110 if (!args.TerseOutput () || iterationNum == 0 )
111- std::cout << std::endl << " Binding Utilities: AutoScaling input image to match model input dimensions..." ;
111+ std::cout << std::endl
112+ << " Binding Utilities: AutoScaling input image to match model input dimensions..." ;
112113
113114 // Create a transform object with default parameters (no transform)
114115 auto transform = BitmapTransform ();
@@ -117,34 +118,43 @@ namespace BindingUtilities
117118 transform.InterpolationMode (args.AutoScaleInterpMode ());
118119
119120 // get the bitmap
120- return decoder. GetSoftwareBitmapAsync ( TypeHelper::GetBitmapPixelFormat (inputDataType),
121- BitmapAlphaMode::Ignore,
122- transform,
123- ExifOrientationMode::RespectExifOrientation,
124- ColorManagementMode::DoNotColorManage) .get ();
121+ return decoder
122+ . GetSoftwareBitmapAsync ( TypeHelper::GetBitmapPixelFormat (inputDataType), BitmapAlphaMode::Ignore,
123+ transform, ExifOrientationMode::RespectExifOrientation ,
124+ ColorManagementMode::DoNotColorManage)
125+ .get ();
125126 }
126127 else
127128 {
128129 // get the bitmap
129- return decoder.GetSoftwareBitmapAsync (TypeHelper::GetBitmapPixelFormat (inputDataType), BitmapAlphaMode::Ignore).get ();
130+ return decoder
131+ .GetSoftwareBitmapAsync (TypeHelper::GetBitmapPixelFormat (inputDataType), BitmapAlphaMode::Ignore)
132+ .get ();
130133 }
131134 }
132135 catch (...)
133136 {
134- std::cout << " BindingUtilities: could not open image file, make sure you are using fully qualified paths." << std::endl;
137+ std::cout << " BindingUtilities: could not open image file, make sure you are using fully qualified paths."
138+ << std::endl;
135139 return nullptr ;
136140 }
137141 }
138142
139- VideoFrame CreateVideoFrame (const SoftwareBitmap& softwareBitmap, InputBindingType inputBindingType, InputDataType inputDataType, const IDirect3DDevice winrtDevice)
143+ VideoFrame CreateVideoFrame (const SoftwareBitmap& softwareBitmap, InputBindingType inputBindingType,
144+ InputDataType inputDataType, const IDirect3DDevice winrtDevice)
140145 {
141146 VideoFrame inputImage = VideoFrame::CreateWithSoftwareBitmap (softwareBitmap);
142147
143148 if (inputBindingType == InputBindingType::GPU)
144149 {
145- VideoFrame gpuImage = winrtDevice
146- ? VideoFrame::CreateAsDirect3D11SurfaceBacked (TypeHelper::GetDirectXPixelFormat (inputDataType), softwareBitmap.PixelWidth (), softwareBitmap.PixelHeight (), winrtDevice)
147- : VideoFrame::CreateAsDirect3D11SurfaceBacked (TypeHelper::GetDirectXPixelFormat (inputDataType), softwareBitmap.PixelWidth (), softwareBitmap.PixelHeight ());
150+ VideoFrame gpuImage =
151+ winrtDevice
152+ ? VideoFrame::CreateAsDirect3D11SurfaceBacked (TypeHelper::GetDirectXPixelFormat (inputDataType),
153+ softwareBitmap.PixelWidth (),
154+ softwareBitmap.PixelHeight (), winrtDevice)
155+ : VideoFrame::CreateAsDirect3D11SurfaceBacked (TypeHelper::GetDirectXPixelFormat (inputDataType),
156+ softwareBitmap.PixelWidth (),
157+ softwareBitmap.PixelHeight ());
148158
149159 inputImage.CopyToAsync (gpuImage).get ();
150160
@@ -182,7 +192,7 @@ namespace BindingUtilities
182192 throw hresult_invalid_argument (L" CSV Input is size/shape is different from what model expects" );
183193 }
184194 T* data = binding.GetData ();
185- for (const auto & elementString : elementStrings)
195+ for (const auto & elementString : elementStrings)
186196 {
187197 T value;
188198 std::stringstream (elementString) >> value;
@@ -206,10 +216,8 @@ namespace BindingUtilities
206216 }
207217
208218 template <TensorKind T>
209- static ITensor CreateTensor (
210- const CommandLineArgs& args,
211- std::vector<std::string>& tensorStringInput,
212- TensorFeatureDescriptor& tensorDescriptor)
219+ static ITensor CreateTensor (const CommandLineArgs& args, std::vector<std::string>& tensorStringInput,
220+ TensorFeatureDescriptor& tensorDescriptor)
213221 {
214222 using TensorValue = typename TensorKindToValue<T>::Type;
215223 using DataType = typename TensorKindToType<T>::Type;
@@ -220,7 +228,7 @@ namespace BindingUtilities
220228 WriteDataToBinding<DataType>(tensorStringInput, binding);
221229 return TensorValue::CreateFromArray (binding.GetShapeBuffer (), binding.GetDataBuffer ());
222230 }
223- else if (args.IsGarbageInput ())
231+ else if (args.IsGarbageInput ())
224232 {
225233 std::vector<int64_t > vecShape = {};
226234 auto tensorDescriptorShape = tensorDescriptor.Shape ();
@@ -255,7 +263,7 @@ namespace BindingUtilities
255263 }
256264 else
257265 {
258- // Creating Tensors for Input Images haven't been added yet.
266+ // Creating Tensors for Input Images haven't been added yet.
259267 throw hresult_not_implemented (L" Creating Tensors for Input Images haven't been implemented yet!" );
260268 }
261269 }
@@ -345,16 +353,10 @@ namespace BindingUtilities
345353 throw hresult_not_implemented ();
346354 }
347355
348- ImageFeatureValue CreateBindableImage (
349- const ILearningModelFeatureDescriptor&
350- featureDescriptor,
351- const std::wstring& imagePath,
352- InputBindingType inputBindingType,
353- InputDataType inputDataType,
354- const IDirect3DDevice winrtDevice,
355- const CommandLineArgs& args,
356- uint32_t iterationNum
357- )
356+ ImageFeatureValue CreateBindableImage (const ILearningModelFeatureDescriptor& featureDescriptor,
357+ const std::wstring& imagePath, InputBindingType inputBindingType,
358+ InputDataType inputDataType, const IDirect3DDevice winrtDevice,
359+ const CommandLineArgs& args, uint32_t iterationNum)
358360 {
359361 auto imageDescriptor = featureDescriptor.try_as <TensorFeatureDescriptor>();
360362
@@ -364,16 +366,16 @@ namespace BindingUtilities
364366 throw ;
365367 }
366368
367- auto softwareBitmap = imagePath. empty ()
368- ? GenerateGarbageImage (imageDescriptor, inputDataType)
369- : LoadImageFile (imageDescriptor, inputDataType, imagePath.c_str (), args, iterationNum);
369+ auto softwareBitmap =
370+ imagePath. empty () ? GenerateGarbageImage (imageDescriptor, inputDataType)
371+ : LoadImageFile (imageDescriptor, inputDataType, imagePath.c_str (), args, iterationNum);
370372
371373 auto videoFrame = CreateVideoFrame (softwareBitmap, inputBindingType, inputDataType, winrtDevice);
372374
373375 return ImageFeatureValue::CreateFromVideoFrame (videoFrame);
374376 }
375377
376- template <typename K, typename V>
378+ template <typename K, typename V>
377379 void OutputSequenceBinding (IMapView<hstring, winrt::Windows::Foundation::IInspectable> results, hstring name)
378380 {
379381 auto map = results.Lookup (name).as <IVectorView<IMap<K, V>>>().GetAt (0 );
@@ -395,11 +397,9 @@ namespace BindingUtilities
395397 std::cout << " " << maxKey << " " << maxVal << std::endl;
396398 }
397399
398- void PrintOrSaveEvaluationResults (const LearningModel& model,
399- const CommandLineArgs& args,
400+ void PrintOrSaveEvaluationResults (const LearningModel& model, const CommandLineArgs& args,
400401 const IMapView<hstring, winrt::Windows::Foundation::IInspectable>& results,
401- OutputHelper& output,
402- int iterationNum)
402+ OutputHelper& output, int iterationNum)
403403 {
404404 for (auto && desc : model.OutputFeatures ())
405405 {
@@ -425,59 +425,63 @@ namespace BindingUtilities
425425 if (args.IsSaveTensor ())
426426 {
427427 fout.open (output.getCsvFileNamePerIterationResult (), std::ios_base::app);
428- fout << " Index" << " ," << " Value" << std::endl;
428+ fout << " Index"
429+ << " ,"
430+ << " Value" << std::endl;
429431 }
430432 TensorFeatureDescriptor tensorDescriptor = desc.as <TensorFeatureDescriptor>();
431433 TensorKind tensorKind = tensorDescriptor.TensorKind ();
432434 switch (tensorKind)
433435 {
434- case TensorKind::String:
435- {
436- if (!args.IsGarbageInput ())
436+ case TensorKind::String:
437437 {
438- auto resultVector = results.Lookup (desc.Name ()).as <TensorString>().GetAsVectorView ();
439- auto output = resultVector.GetAt (0 ).data ();
440- std::wcout << " Result: " << output << std::endl;
438+ if (!args.IsGarbageInput ())
439+ {
440+ auto resultVector = results.Lookup (desc.Name ()).as <TensorString>().GetAsVectorView ();
441+ auto output = resultVector.GetAt (0 ).data ();
442+ std::wcout << " Result: " << output << std::endl;
443+ }
441444 }
442- }
443- break ;
444- case TensorKind::Float16:
445- {
446- output.ProcessTensorResult <HALF>(args, tensor, uCapacity, maxValue, maxIndex, fout);
447- }
448- break ;
449- case TensorKind::Float:
450- {
451- output.ProcessTensorResult <float >(args, tensor, uCapacity, maxValue, maxIndex, fout);
452- }
453- break ;
454- case TensorKind::Int64:
455- {
456- auto resultVector = results.Lookup (desc.Name ()).as <TensorInt64Bit>().GetAsVectorView ();
457- if (!args.IsGarbageInput ())
445+ break ;
446+ case TensorKind::Float16:
458447 {
459- auto output = resultVector.GetAt (0 );
460- std::wcout << " Result: " << output << std::endl;
448+ output.ProcessTensorResult <HALF>(args, tensor, uCapacity, maxValue, maxIndex, fout);
461449 }
462- }
463- break ;
464- default :
465- {
466- std::cout << " BindingUtilities: output type not implemented." ;
467- }
468- break ;
450+ break ;
451+ case TensorKind::Float:
452+ {
453+ output.ProcessTensorResult <float >(args, tensor, uCapacity, maxValue, maxIndex, fout);
454+ }
455+ break ;
456+ case TensorKind::Int64:
457+ {
458+ auto resultVector = results.Lookup (desc.Name ()).as <TensorInt64Bit>().GetAsVectorView ();
459+ if (!args.IsGarbageInput ())
460+ {
461+ auto output = resultVector.GetAt (0 );
462+ std::wcout << " Result: " << output << std::endl;
463+ }
464+ }
465+ break ;
466+ default :
467+ {
468+ std::cout << " BindingUtilities: output type not implemented." ;
469+ }
470+ break ;
469471 }
470472 if (args.IsSaveTensor ())
471473 {
472474 fout.close ();
473- std::string iterationResult = " Index: " + std::to_string (maxIndex) + " ; Value: " + std::to_string (maxValue);
475+ std::string iterationResult =
476+ " Index: " + std::to_string (maxIndex) + " ; Value: " + std::to_string (maxValue);
474477 output.SaveResult (iterationNum, iterationResult, static_cast <int >(hash_data (tensor, uCapacity)));
475478 }
476479 if (!args.IsGarbageInput () && iterationNum == 0 )
477480 {
478481 std::cout << " Outputting results.. " << std::endl;
479482 std::cout << " Feature Name: " << name << std::endl;
480- std::wcout << " resultVector[" << maxIndex << " ] has the maximal value of " << maxValue << std::endl;
483+ std::wcout << " resultVector[" << maxIndex << " ] has the maximal value of " << maxValue
484+ << std::endl;
481485 }
482486 }
483487 else if (desc.Kind () == LearningModelFeatureKind::Sequence)
@@ -489,18 +493,18 @@ namespace BindingUtilities
489493 auto tensorKind = valueKind.as <TensorFeatureDescriptor>().TensorKind ();
490494 switch (keyKind)
491495 {
492- case TensorKind::Int64:
493- {
494- OutputSequenceBinding<int64_t , float >(results, desc.Name ());
495- }
496- break ;
497- case TensorKind::Float:
498- {
499- OutputSequenceBinding<float , float >(results, desc.Name ());
500- }
501- break ;
496+ case TensorKind::Int64:
497+ {
498+ OutputSequenceBinding<int64_t , float >(results, desc.Name ());
499+ }
500+ break ;
501+ case TensorKind::Float:
502+ {
503+ OutputSequenceBinding<float , float >(results, desc.Name ());
504+ }
505+ break ;
502506 }
503507 }
504508 }
505509 }
506- };
510+ }; // namespace BindingUtilities
0 commit comments