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

Export error with saved_model, pb, tflite #7091

Closed
2 tasks done
ShubhamAtPhilips opened this issue Mar 21, 2022 · 6 comments
Closed
2 tasks done

Export error with saved_model, pb, tflite #7091

ShubhamAtPhilips opened this issue Mar 21, 2022 · 6 comments
Labels
bug Something isn't working Stale Stale and schedule for closing soon

Comments

@ShubhamAtPhilips
Copy link

ShubhamAtPhilips commented Mar 21, 2022

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Export

Bug

I trained a model using this models/hub/yolov3-tiny.yaml configuration. I am trying to convert it to TFlite.

I found out that I need to implement TFMaxPool2d and TFZeroPad2d in models/tf.py from this issue #7033.

I implemented it as follows in models/tf.py

class TFMaxPool2d(keras.layers.Layer):
    def __init__(self, k=(2, 2), s=None, p='same', w=None):
        super().__init__()
        self.m = keras.layers.MaxPool2D(pool_size=k, strides=s, padding='SAME')

    def call(self, inputs):
        return self.m(inputs)

class TFZeroPad2d(keras.layers.Layer):
    def __init__(self, p=(1, 1), w=None):
        super().__init__()
        print(p)
        self.z = keras.layers.ZeroPadding2D(padding=p)

    def call(self, inputs):
        return self.z(inputs)

Now, I am getting the error

TensorFlow SavedModel: starting export with tensorflow 2.7.1...
                 from  n    params  module                                  arguments                        0                -1  1       464  models.common.Conv                      [3, 16, 3, 1]                    1                -1  1         0  torch.nn.modules.pooling.MaxPool2d      [2, 2, 0]                        2                -1  1      4672  models.common.Conv                      [16, 32, 3, 1]                   3                -1  1         0  torch.nn.modules.pooling.MaxPool2d      [2, 2, 0]                        4                -1  1     18560  models.common.Conv                      [32, 64, 3, 1]                   5                -1  1         0  torch.nn.modules.pooling.MaxPool2d      [2, 2, 0]                        6                -1  1     73984  models.common.Conv                      [64, 128, 3, 1]                  7                -1  1         0  torch.nn.modules.pooling.MaxPool2d      [2, 2, 0]                        8                -1  1    295424  models.common.Conv                      [128, 256, 3, 1]                 9                -1  1         0  torch.nn.modules.pooling.MaxPool2d      [2, 2, 0]                       10                -1  1   1180672  models.common.Conv                      [256, 512, 3, 1]               

TensorFlow SavedModel: export failure: `padding` should have two elements. Received: [0, 1, 
0, 1].

TensorFlow Lite: starting export with tensorflow 2.7.1...

TensorFlow Lite: export failure: 'NoneType' object has no attribute 'call'

Can someone help me implement 4-tuple padding TF/Keras equivalent of torch.nn.ZeroPad2d?

Thank you.

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@ShubhamAtPhilips ShubhamAtPhilips added the bug Something isn't working label Mar 21, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 21, 2022

👋 Hello @ShubhamAtPhilips, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@ShubhamAtPhilips ShubhamAtPhilips changed the title Export error Export error with saved_model, pb, tflite Mar 21, 2022
@ShubhamAtPhilips
Copy link
Author

Update,

I updated the function definitions, and it seems to work now.

class TFMaxPool2d(keras.layers.Layer):
    def __init__(self, k=(2, 2), s=None, p='same', w=None):
        super().__init__()
        if p == 0:
            padding = 'valid'
        else:
            padding = 'same'
        self.m = keras.layers.MaxPool2D(pool_size=k, strides=s, padding=padding)
        
    def call(self, inputs):
        return self.m(inputs)

class TFZeroPad2d(keras.layers.Layer):
    def __init__(self, p=(1, 1), w=None):
        super().__init__()
        if hasattr(p, '__len__'):
            if len(p) == 4:
                p = ((p[2], p[3]), (p[0], p[1]))
        self.z = keras.layers.ZeroPadding2D(padding=p)

    def call(self, inputs):
        return self.z(inputs)

Can someone verify if it's correct? If yes, I can submit a PR.

@glenn-jocher
Copy link
Member

glenn-jocher commented Mar 23, 2022

@ShubhamAtPhilips oh cool! Yes go ahead and submit a PR. To verify your implementation export yolov3-tiny.pt to TFLite or SavedModel format and then run val.py on your exported model and compare against PyTorch model. Both should have identical mAP to several decimal places.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 23, 2022

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Apr 23, 2022
@wakasu
Copy link

wakasu commented Jun 13, 2022

This works, i was able to convert yolov3-tiny to tflite 👍

@glenn-jocher
Copy link
Member

@wakasu that's great to hear! 🎉 If you have any further questions or need assistance with anything else, feel free to ask. Good luck with your project!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

3 participants