XiaoMi / mace-models

Mobile AI Compute Engine Model Zoo
Apache License 2.0
368 stars 72 forks source link

Support for tf.keras models(models from micro-models) #30

Closed madhu-korada closed 3 years ago

madhu-korada commented 3 years ago

I was exploring some of the models given in this repository. But I can't seem to convert the micro-models/keras folder's model. As given in the readme I am using TensorFlow >= 2.0.0. I tried various approaches, trained a small model in colab, but there seem to be issues anyways.

When I am directly using mnist.yml from the folder I am facing this error.

  File "tools/python/convert.py", line 282, in <module>
    convert(conf, flags.output, flags.enable_micro)
  File "tools/python/convert.py", line 89, in convert
    net_def_with_Data = convert_net(net_name, net_conf, enable_micro)
  File "tools/python/convert.py", line 226, in convert_net
    option, conf["model_file_path"])
  File "/home/hitech/aware/mace/tools/python/transform/keras_converter.py", line 205, in __init__
    compile=False)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/saving/save.py", line 184, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 178, in load_model_from_hdf5
    custom_objects=custom_objects)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/saving/model_config.py", line 55, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 109, in deserialize
    printable_module_name='layer')
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 373, in deserialize_keras_object
    list(custom_objects.items())))
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/engine/sequential.py", line 398, in from_config
    custom_objects=custom_objects)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 109, in deserialize
    printable_module_name='layer')
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 375, in deserialize_keras_object
    return cls.from_config(cls_config)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize_wrapper.py", line 206, in from_config
    layer = tf.keras.layers.deserialize(config.pop('layer'))
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/layers/serialization.py", line 109, in deserialize
    printable_module_name='layer')
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 375, in deserialize_keras_object
    return cls.from_config(cls_config)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 655, in from_config
    return cls(**config)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 599, in __init__
    **kwargs)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/layers/convolutional.py", line 125, in __init__
    **kwargs)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/training/tracking/base.py", line 456, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 294, in __init__
    generic_utils.validate_kwargs(kwargs, allowed_kwargs)
  File "/home/hitech/anaconda3/envs/mace/lib/python3.6/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 792, in validate_kwargs
    raise TypeError(error_message, kwarg)
TypeError: ('Keyword argument not understood:', 'groups')

The colab link has the same code as the code given in the micro-models/keras folder :- https://colab.research.google.com/drive/1_GhO4805M_aSk1kIOVDFlVA-b1nGusBi?usp=sharing

When I am using the model from the above link I am getting the following error :-

File "tools/python/convert.py", line 282, in <module>
    convert(conf, flags.output, flags.enable_micro)
File "tools/python/convert.py", line 89, in convert
    net_def_with_Data = convert_net(net_name, net_conf, enable_micro)
File "tools/python/convert.py", line 238, in convert_net
    output_graph_def, quantize_activation_info = mace_transformer.run()
File "/home/hitech/aware/mace/tools/python/transform/transformer.py", line 166, in run
    changed = transformer()
File "/home/hitech/aware/mace/tools/python/transform/transformer.py", line 1623, in sort_by_execution
    "output_tensor %s not existed in model" % output_node)
File "/home/hitech/aware/mace/tools/python/utils/util.py", line 76, in mace_check
    for line in traceback.format_stack():
ERROR: /home/hitech/aware/mace/tools/python/transform/transformer.py:1623: output_tensor output not existed in model

I verified my model using netron, and everything seems fine. my yml file

library_name: mnist_madhu
target_abis: [arm64-v8a]
model_graph_format: file
model_data_format: file
models:
  mnist:
    platform: keras
    model_file_path: /home/hitech/aware/mace/examples/android-custom-demo-MACE/models/keras/mnist_madhu/mnist_layers_named.h5
    model_sha256_checksum: d64999fc571fe5725fb87427dfbcf8e757010883039b06728f5929e7088f447d
    subgraphs:
      - input_tensors:
          - input
        input_shapes:
          - 1,28,28
        input_ranges:
          - 0,1
        output_tensors:
          - output
        output_shapes:
          - 1,10
    runtime: cpu+gpu
    limit_opencl_kernel_time: 0
    nnlib_graph_mode: 0
    obfuscate: 0
    winograd: 0

Can anyone confirm if keras models are working properly in their systems ?? If yes, please share your environments. And please let me know what I am doing wrong.

Any suggestions are appreciated.

madhu-korada commented 3 years ago

Keras Models seems to be working with Tensorflow 2.3.0, but in documentation, it is written as TF >=2. I tried with all other versions which were throwing some errors.