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

Error in converting retinaface mobilenetv2 backend to CoreML #53

Open
N-Harish opened this issue May 8, 2023 · 0 comments
Open

Error in converting retinaface mobilenetv2 backend to CoreML #53

N-Harish opened this issue May 8, 2023 · 0 comments

Comments

@N-Harish
Copy link

N-Harish commented May 8, 2023

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/framework/importer.py:496, in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, producer_op_list)
    495 try:
--> 496   results = c_api.TF_GraphImportGraphDefWithResults(
    497       graph._c_graph, serialized, options)  # pylint: disable=protected-access
    498   results = c_api_util.ScopedTFImportGraphDefResults(results)

InvalidArgumentError: Input 0 of node RetinaFaceModel/FPN/ConvBN/bn/AssignNewValue was passed float from RetinaFaceModel/FPN/ConvBN/bn/FusedBatchNormV3/ReadVariableOp/resource:0 incompatible with expected resource.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
Cell In[19], line 1
----> 1 ct.convert(model, source='tensorflow')

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/_converters_entry.py:492, in convert(model, source, inputs, outputs, classifier_config, minimum_deployment_target, convert_to, compute_precision, skip_model_load, compute_units, package_dir, debug, pass_pipeline)
    489 if specification_version is None:
    490     specification_version = _set_default_specification_version(exact_target)
--> 492 mlmodel = mil_convert(
    493     model,
    494     convert_from=exact_source,
    495     convert_to=exact_target,
    496     inputs=inputs,
    497     outputs=outputs_as_tensor_or_image_types,  # None or list[ct.ImageType/ct.TensorType]
    498     classifier_config=classifier_config,
    499     skip_model_load=skip_model_load,
    500     compute_units=compute_units,
    501     package_dir=package_dir,
    502     debug=debug,
    503     specification_version=specification_version,
    504     main_pipeline=pass_pipeline,
    505 )
    507 if exact_target == 'milinternal':
    508     return mlmodel  # Returns the MIL program

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/converter.py:188, in mil_convert(model, convert_from, convert_to, compute_units, **kwargs)
    149 @_profile
    150 def mil_convert(
    151     model,
   (...)
    155     **kwargs
    156 ):
    157     """
    158     Convert model from a specified frontend `convert_from` to a specified
    159     converter backend `convert_to`.
   (...)
    186         See `coremltools.converters.convert`
    187     """
--> 188     return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/converter.py:212, in _mil_convert(model, convert_from, convert_to, registry, modelClass, compute_units, **kwargs)
    209     weights_dir = _tempfile.TemporaryDirectory()
    210     kwargs["weights_dir"] = weights_dir.name
--> 212 proto, mil_program = mil_convert_to_proto(
    213                         model,
    214                         convert_from,
    215                         convert_to,
    216                         registry,
    217                         **kwargs
    218                      )
    220 _reset_conversion_state()
    222 if convert_to == 'milinternal':

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/converter.py:285, in mil_convert_to_proto(model, convert_from, convert_to, converter_registry, main_pipeline, **kwargs)
    280 frontend_pipeline, backend_pipeline = _construct_other_pipelines(
    281     main_pipeline, convert_from, convert_to
    282 )
    284 frontend_converter = frontend_converter_type()
--> 285 prog = frontend_converter(model, **kwargs)
    286 PipelineManager.apply_pipeline(prog, frontend_pipeline)
    288 PipelineManager.apply_pipeline(prog, main_pipeline)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/converter.py:98, in TensorFlow2Frontend.__call__(self, *args, **kwargs)
     95 from .frontend.tensorflow2.load import TF2Loader
     97 tf2_loader = TF2Loader(*args, **kwargs)
---> 98 return tf2_loader.load()

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/load.py:61, in TFLoader.load(self)
     59 outputs = self.kwargs.get("outputs", None)
     60 output_names = get_output_names(outputs)
---> 61 self._graph_def = self._graph_def_from_model(output_names)
     63 if self._graph_def is not None and len(self._graph_def.node) == 0:
     64     msg = "tf.Graph should have at least 1 node, Got empty graph."

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py:133, in TF2Loader._graph_def_from_model(self, output_names)
    131 def _graph_def_from_model(self, output_names=None):
    132     """Overwrites TFLoader._graph_def_from_model()"""
--> 133     cfs, graph_def = self._get_concrete_functions_and_graph_def()
    134     if isinstance(self.model, _tf.keras.Model) and self.kwargs.get("outputs", None) is None:
    135         # For the keras model, check if the outputs is provided by the user.
    136         # If not, we make sure the coreml model outputs order is the same as
    137         # the original keras model
    138         cf = cfs[0]

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py:127, in TF2Loader._get_concrete_functions_and_graph_def(self)
    124 else:
    125     raise NotImplementedError(msg.format(self.model))
--> 127 graph_def = self._graph_def_from_concrete_fn(cfs)
    129 return cfs, graph_def

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py:328, in TF2Loader._graph_def_from_concrete_fn(self, cfs)
    325     raise NotImplementedError("Only a single concrete function is supported.")
    327 if _get_version(_tf.__version__) >= _StrictVersion("2.2.0"):
--> 328     frozen_fn = _convert_variables_to_constants_v2(cfs[0], lower_control_flow=False, aggressive_inlining=True)
    329 else:
    330     frozen_fn = _convert_variables_to_constants_v2(cfs[0], lower_control_flow=False)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/framework/convert_to_constants.py:1083, in convert_variables_to_constants_v2(func, lower_control_flow, aggressive_inlining)
   1075 converter_data = _FunctionConverterData(
   1076     func=func,
   1077     lower_control_flow=lower_control_flow,
   1078     aggressive_inlining=aggressive_inlining)
   1080 output_graph_def, converted_input_indices = _replace_variables_by_constants(
   1081     converter_data=converter_data)
-> 1083 return _construct_concrete_function(func, output_graph_def,
   1084                                     converted_input_indices)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/framework/convert_to_constants.py:1008, in _construct_concrete_function(func, output_graph_def, converted_input_indices)
   1005   if context.context().has_function(f.signature.name):
   1006     context.context().remove_function(f.signature.name)
-> 1008 new_func = wrap_function.function_from_graph_def(output_graph_def,
   1009                                                  new_input_names,
   1010                                                  new_output_names)
   1012 # Manually propagate shape for input tensors where the shape is not correctly
   1013 # propagated. Scalars shapes are lost when wrapping the function.
   1014 for input_tensor in new_func.inputs:

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/eager/wrap_function.py:650, in function_from_graph_def(graph_def, inputs, outputs)
    647 def _imports_graph_def():
    648   importer.import_graph_def(graph_def, name="")
--> 650 wrapped_import = wrap_function(_imports_graph_def, [])
    651 import_graph = wrapped_import.graph
    652 return wrapped_import.prune(
    653     nest.map_structure(import_graph.as_graph_element, inputs),
    654     nest.map_structure(import_graph.as_graph_element, outputs))

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/eager/wrap_function.py:621, in wrap_function(fn, signature, name)
    618 if name is not None:
    619   func_graph_name = "wrapped_function_" + name
    620 return WrappedFunction(
--> 621     func_graph.func_graph_from_py_func(
    622         func_graph_name,
    623         holder,
    624         args=None,
    625         kwargs=None,
    626         signature=signature,
    627         add_control_dependencies=False,
    628         collections={}),
    629     variable_holder=holder,
    630     signature=signature)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/framework/func_graph.py:999, in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    996 else:
    997   _, original_func = tf_decorator.unwrap(python_func)
--> 999 func_outputs = python_func(*func_args, **func_kwargs)
   1001 # invariant: `func_outputs` contains only Tensors, CompositeTensors,
   1002 # TensorArrays and `None`s.
   1003 func_outputs = nest.map_structure(convert, func_outputs,
   1004                                   expand_composites=True)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/eager/wrap_function.py:87, in VariableHolder.__call__(self, *args, **kwargs)
     86 def __call__(self, *args, **kwargs):
---> 87   return self.call_with_variable_creator_scope(self._fn)(*args, **kwargs)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/eager/wrap_function.py:93, in VariableHolder.call_with_variable_creator_scope.<locals>.wrapped(*args, **kwargs)
     91 def wrapped(*args, **kwargs):
     92   with variable_scope.variable_creator_scope(self.variable_creator_scope):
---> 93     return fn(*args, **kwargs)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/eager/wrap_function.py:648, in function_from_graph_def.<locals>._imports_graph_def()
    647 def _imports_graph_def():
--> 648   importer.import_graph_def(graph_def, name="")

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/util/deprecation.py:535, in deprecated_args.<locals>.deprecated_wrapper.<locals>.new_func(*args, **kwargs)
    527         _PRINTED_WARNING[(func, arg_name)] = True
    528       logging.warning(
    529           'From %s: calling %s (from %s) with %s is deprecated and will '
    530           'be removed %s.\nInstructions for updating:\n%s',
   (...)
    533           'in a future version' if date is None else ('after %s' % date),
    534           instructions)
--> 535 return func(*args, **kwargs)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/framework/importer.py:400, in import_graph_def(***failed resolving arguments***)
    357 """Imports the graph from `graph_def` into the current default `Graph`.
    358 
    359 This function provides a way to import a serialized TensorFlow
   (...)
    397     it refers to an unknown tensor).
    398 """
    399 del op_dict
--> 400 return _import_graph_def_internal(
    401     graph_def,
    402     input_map=input_map,
    403     return_elements=return_elements,
    404     name=name,
    405     producer_op_list=producer_op_list)

File ~/SageMaker/envs/coreml_env/lib64/python3.8/site-packages/tensorflow/python/framework/importer.py:501, in _import_graph_def_internal(graph_def, input_map, return_elements, validate_colocation_constraints, name, producer_op_list)
    498     results = c_api_util.ScopedTFImportGraphDefResults(results)
    499   except errors.InvalidArgumentError as e:
    500     # Convert to ValueError for backwards compatibility.
--> 501     raise ValueError(str(e))
    503 # Create _DefinedFunctions for any imported functions.
    504 #
    505 # We do this by creating _DefinedFunctions directly from `graph_def`, and
   (...)
    510 # TODO(skyewm): fetch the TF_Functions directly from the TF_Graph
    511 # TODO(skyewm): avoid sending serialized FunctionDefs back to the TF_Graph
    513 _ProcessNewOps(graph)

ValueError: Input 0 of node RetinaFaceModel/FPN/ConvBN/bn/AssignNewValue was passed float from RetinaFaceModel/FPN/ConvBN/bn/FusedBatchNormV3/ReadVariableOp/resource:0 incompatible with expected resource.

While trying to convert model to CoreMl format, We are getting this error and the conversion process stops @peteryuX can you let us know how to fix this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant