Skip to content

Convert model to tflite model #152

Closed

Description

@MrRiahi

I want to run this code on raspberry pi 4. In the first step, I should convert this model to tflite model. I use the following link https://www.tensorflow.org/lite/convert/index to convert the model but it raises the following error.
OSError: SavedModel file does not exist at: ./models/mobilenet-float16-stride16/{saved_model.pbtxt|saved_model.pb}

I also use the following code for model conversion
python -m tf_bodypix \ convert-to-tflite \ --model-path \ "https://storage.googleapis.com/tfjs-models/savedmodel/bodypix/mobilenet/float/075/model-stride16.json" \ --optimize \ --quantization-type=float16 \ --output-model-file "./mobilenet-float16-stride16.tflite"
it works and the model convert to tflite but when I want to use it in tensorflow lite based on this link https://www.tensorflow.org/lite/guide/inference, I found that the input model dimension is array([1, 1, 1, 3], dtype=int32). Can you help me to solve this problem?

Activity

  1. de-code commented on Nov 23, 2021

    @de-code
    Owner

    What if you try to use the tflite model with tf-bodypix?
    If you pass a file ending with .tflite to the load_model method, then it should use the tflite API.
    You should also be able to use the CLI.

    You can find the implementation in the code: https://github.com/de-code/python-tf-bodypix/blob/v0.3.8/tf_bodypix/model.py#L455-L504

    The ranks of the input shapes are not always the same, and sometimes it is a 4D tensor, sometimes a 3D tensor (depending on the conversion method).
    If it is a 4D tensor and you can simply resize the tensor to make it 4D. In that case the first dimension will typically be the batch size.
    The other feature of tflite or the model is that the width and height is dynamic. With tflite you need to resize the actual tensor (see linked code block).

  2. MrRiahi commented on Nov 23, 2021

    @MrRiahi
    ContributorAuthor

    Thanks for your response. Your answer really helps me. I need the location of each pose from this model. When I feed an image into it, the output result is a dictionary with these values: float_short_offsets, float_segments, float_part_heatmaps, float_long_offsets, float_heatmaps, MobilenetV1/displacement_fwd_2/BiasAdd, MobilenetV1/displacement_bwd_2/BiasAdd, and float_part_offsets. I can not figure out which ones of these values are corresponds to the poses locations.

  3. de-code commented on Nov 23, 2021

    @de-code
    Owner

    I would say that implementing the pose detection isn't trivial. You can look at the related changes to this project to add support for it:
    https://github.com/de-code/python-tf-bodypix/pull/74/files
    (Looking at the source code should also give clues as to what model outputs are relevant)

    I would be hesitant though to re-implement the pose detection unless you really need to.

    I don't know what you want to achieve. Can you not use tf-bodypix on the Raspberry Pi? (I must admit I haven't tried it myself)

  4. MrRiahi commented on Nov 24, 2021

    @MrRiahi
    ContributorAuthor

    The reason I want to use the TensorFlow lite is that Tensorflow v2x is not supported for Raspberry pi 4.

  5. de-code commented on Nov 24, 2021

    @de-code
    Owner

    It seems there are non-official wheels to install the full TensorFlow v2.x. Perhaps something to try anyway. Or build and cross-compile from source.

    When you are using TensorFlow Lite though, what API / language are you using? Are you using a Python API or do you implement it in C?

  6. MrRiahi commented on Nov 24, 2021

    @MrRiahi
    ContributorAuthor

    Exactly. I use python but if there are not no other solutions, I can switch to C.

  7. de-code commented on Nov 24, 2021

    @de-code
    Owner

    This is what I can suggest:

    • If you are using a Python version of TensorFlow Lite, then we could work together on making tf-bodypix work with that
    • If you are not using Python TensorFlow Lite, but you do use Python to interpret the results, then we can work together on refactoring tf-bodypix to make it easier to re-use the code that interprets the results (e.g. in case some imports get in the way)
    • If you are not using Python at all, then I can only offer the code as a reference and perhaps debugging the Python version will help you to understand it better
  8. MrRiahi commented on Nov 24, 2021

    @MrRiahi
    ContributorAuthor

    I think the first suggestion is the best and can help other people. I try to make changes in tf-bodypix so that it can work with tflite model.
    In the first step, I should figure out what is its output means. I would be grateful if you help me to understand that.

  9. de-code commented on Nov 24, 2021

    @de-code
    Owner

    I think the first suggestion is the best and can help other people. I try to make changes in tf-bodypix so that it can work with tflite model. In the first step, I should figure out what is its output means. I would be grateful if you help me to understand that.

    There is already TensorFlow Lite support, at least using the TensorFlow Lite provided via the full TensorFlow.

  10. MrRiahi commented on Dec 5, 2021

    @MrRiahi
    ContributorAuthor

    Hi again.
    Following our previous conversations, I release the tflite inference from TensorFlow. It would be my pleasure to share my code with you and contribute to this project. If you are ok, please give me access to add a folder for tflite inference only using PIL, numpy, tflite, and cv2.
    Thanks

  11. de-code commented on Dec 5, 2021

    @de-code
    Owner

    Hi again. Following our previous conversations, I release the tflite inference from TensorFlow. It would be my pleasure to share my code with you and contribute to this project. If you are ok, please give me access to add a folder for tflite inference only using PIL, numpy, tflite, and cv2. Thanks

    That is great news.
    The best will be to fork the repo and submit a PR with the changes. It will give me a chance to review and test it.

  12. MrRiahi commented on Dec 5, 2021

    @MrRiahi
    ContributorAuthor

    Thanks. I will do it in the near future.

  13. MrRiahi commented on Dec 12, 2021

    @MrRiahi
    ContributorAuthor

    Hi again. I fork the repo and put my codes in the bodypix_tflite folder.

  14. de-code commented on Dec 13, 2021

    @de-code
    Owner

    Hi again. I fork the repo and put my codes in the bodypix_tflite folder.

    Great, thank you. Can you create a PR please?

  15. MrRiahi commented on Dec 15, 2021

    @MrRiahi
    ContributorAuthor

    I created a pull request in the past few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions