@@ -298,59 +298,55 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
298298 << " : Required parameter `model_path` not found in configuration" ;
299299 throw std::invalid_argument (buffer.str ());
300300 }
301-
302- const vsdk::ProtoValue& model_path_val = model_path->second ;
303- if (!model_path_val.is_a <std::string>() ||
304- model_path_val.get_unchecked <std::string>().empty ()) {
301+ const auto * const model_path_string = model_path->second .get <std::string>();
302+ if (!model_path_string || model_path_string->empty ()) {
305303 std::ostringstream buffer;
306304 buffer << service_name
307305 << " : Required non-empty string parameter `model_path` is either not a string "
308306 " or is an empty string" ;
309307 throw std::invalid_argument (buffer.str ());
310308 }
311- const std::string& model_path_string = model_path_val.get_unchecked <std::string>();
312309
313310 // Process any tensor name remappings provided in the config.
314311 auto remappings = attributes.find (" tensor_name_remappings" );
315312 if (remappings != attributes.end ()) {
316- if (!remappings->second .is_a <vsdk::ProtoStruct>()) {
313+ const auto remappings_attributes = remappings->second .get <vsdk::ProtoStruct>();
314+ if (!remappings_attributes) {
317315 std::ostringstream buffer;
318316 buffer << service_name
319317 << " : Optional parameter `tensor_name_remappings` must be a dictionary" ;
320318 throw std::invalid_argument (buffer.str ());
321319 }
322- const auto remappings_attributes =
323- remappings->second .get_unchecked <vsdk::ProtoStruct>();
324-
325320 const auto populate_remappings = [](const vsdk::ProtoValue& source, auto & target) {
326- if (!source.is_a <vsdk::ProtoStruct>()) {
321+ const auto source_attributes = source.get <vsdk::ProtoStruct>();
322+ if (!source_attributes) {
327323 std::ostringstream buffer;
328324 buffer << service_name
329- << " : Fields `inputs` and `outputs` of `tensor_name_remappings` "
330- " must be "
325+ << " : Fields `inputs` and `outputs` of `tensor_name_remappings` must be "
331326 " dictionaries" ;
332327 throw std::invalid_argument (buffer.str ());
333328 }
334- for (const auto & kv : source. get_unchecked <vsdk::ProtoStruct>() ) {
329+ for (const auto & kv : *source_attributes ) {
335330 const auto & k = kv.first ;
336- if (!kv.second .is_a <std::string>()) {
331+ const auto * const kv_string = kv.second .get <std::string>();
332+ if (!kv_string) {
337333 std::ostringstream buffer;
338- buffer << service_name
339- << " : Fields `inputs` and `outputs` of `tensor_name_remappings` "
340- " must "
341- " be dictionaries with string values" ;
334+ buffer
335+ << service_name
336+ << " : Fields `inputs` and `outputs` of `tensor_name_remappings` must "
337+ " be dictionaries with string values" ;
342338 throw std::invalid_argument (buffer.str ());
343339 }
344- target[kv.first ] = kv. second . get_unchecked <std::string>() ;
340+ target[kv.first ] = *kv_string ;
345341 }
346342 };
347343
348- const auto inputs_where = remappings_attributes. find (" inputs" );
349- if (inputs_where != remappings_attributes. end ()) {
344+ const auto inputs_where = remappings_attributes-> find (" inputs" );
345+ if (inputs_where != remappings_attributes-> end ()) {
350346 populate_remappings (inputs_where->second , state->input_name_remappings );
351347 }
352- const auto outputs_where = remappings_attributes. find (" outputs" );
353- if (outputs_where != remappings_attributes. end ()) {
348+ const auto outputs_where = remappings_attributes-> find (" outputs" );
349+ if (outputs_where != remappings_attributes-> end ()) {
354350 populate_remappings (outputs_where->second , state->output_name_remappings );
355351 }
356352 }
@@ -366,11 +362,11 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
366362 // buffer which we can use with `TfLiteModelCreate`. That
367363 // still requires that the buffer be kept valid, but that's
368364 // more easily done.
369- const std::ifstream in (model_path_string, std::ios::in | std::ios::binary);
365+ const std::ifstream in (* model_path_string, std::ios::in | std::ios::binary);
370366 if (!in) {
371367 std::ostringstream buffer;
372368 buffer << service_name << " : Failed to open file for `model_path` "
373- << model_path_string;
369+ << * model_path_string;
374370 throw std::invalid_argument (buffer.str ());
375371 }
376372 std::ostringstream model_path_contents_stream;
@@ -405,27 +401,21 @@ class MLModelServiceTFLite : public vsdk::MLModelService,
405401 // object to carry that information.
406402 auto num_threads = attributes.find (" num_threads" );
407403 if (num_threads != attributes.end ()) {
408- auto throwError = [&] {
404+ const auto * num_threads_double = num_threads->second .get <double >();
405+ if (!num_threads_double || !std::isnormal (*num_threads_double) ||
406+ (*num_threads_double < 0 ) ||
407+ (*num_threads_double >= std::numeric_limits<std::int32_t >::max ()) ||
408+ (std::trunc (*num_threads_double) != *num_threads_double)) {
409409 std::ostringstream buffer;
410410 buffer << service_name
411- << " : Value for field `num_threads` is not a positive integer" ;
411+ << " : Value for field `num_threads` is not a positive integer: "
412+ << *num_threads_double;
412413 throw std::invalid_argument (buffer.str ());
413- };
414-
415- if (!num_threads->second .is_a <double >()) {
416- throwError ();
417- }
418-
419- double num_threads_double = num_threads->second .get_unchecked <double >();
420- if (!std::isnormal (num_threads_double) || (num_threads_double < 0 ) ||
421- (num_threads_double >= std::numeric_limits<std::int32_t >::max ()) ||
422- (std::trunc (num_threads_double) != num_threads_double)) {
423- throwError ();
424414 }
425415
426416 state->interpreter_options .reset (TfLiteInterpreterOptionsCreate ());
427417 TfLiteInterpreterOptionsSetNumThreads (state->interpreter_options .get (),
428- static_cast <int32_t >(num_threads_double));
418+ static_cast <int32_t >(* num_threads_double));
429419 }
430420
431421 // Build the single interpreter.
0 commit comments