-
Notifications
You must be signed in to change notification settings - Fork 53
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
Switch to Keras Mish implementation for TfLite compatibility #60
Open
m-romanenko
wants to merge
9
commits into
master
Choose a base branch
from
mish-layer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a8c40e
use tf.keras Mish implementation
m-romanenko 9e6128f
add script for tflite conversion
m-romanenko 3367bda
use absolute path in import
m-romanenko a4ac9d7
remove unused __init__
m-romanenko 144afd5
rename main() -> convert_tflite()
m-romanenko f33a4f7
add entry point to setup
m-romanenko 4e4f2e4
add docstrings
m-romanenko d1ae5de
add import test
m-romanenko 32863e6
refactor and add test
m-romanenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from tf2_yolov4.tools.convert_tflite import create_tflite_model | ||
|
||
HEIGHT, WIDTH = (640, 960) | ||
|
||
|
||
def test_import_convert_tflite_script_does_not_fail(): | ||
from tf2_yolov4.tools.convert_tflite import convert_tflite | ||
|
||
|
||
def test_create_tflite_model_returns_correct_type(yolov4_inference): | ||
tflite_model = create_tflite_model(yolov4_inference) | ||
assert isinstance(tflite_model, bytes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"""Activations layers""" | ||
|
||
from tf2_yolov4.activations.mish import Mish | ||
|
||
__all__ = ["Mish"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Tensorflow-Keras Implementation of Mish | ||
Source: https://github.com/digantamisra98/Mish/blob/master/Mish/TFKeras/mish.py | ||
""" | ||
import tensorflow as tf | ||
from tensorflow.keras.layers import Layer | ||
|
||
|
||
class Mish(Layer): | ||
""" | ||
Mish Activation Function. | ||
.. math:: | ||
mish(x) = x * tanh(softplus(x)) = x * tanh(ln(1 + e^{x})) | ||
Shape: | ||
- Input: Arbitrary. Use the keyword argument `input_shape` | ||
(tuple of integers, does not include the samples axis) | ||
when using this layer as the first layer in a model. | ||
- Output: Same shape as the input. | ||
Examples: | ||
>>> X = Mish()(X_input) | ||
""" | ||
|
||
def call(self, inputs, **kwargs): | ||
return inputs * tf.math.tanh(tf.math.softplus(inputs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Script used to create a TfLite YOLOv4 model from previously trained weights. | ||
""" | ||
|
||
import click | ||
import tensorflow as tf | ||
|
||
from tf2_yolov4.anchors import YOLOV4_ANCHORS | ||
from tf2_yolov4.model import YOLOv4 | ||
|
||
HEIGHT, WIDTH = (640, 960) | ||
|
||
TFLITE_MODEL_PATH = "yolov4.tflite" | ||
|
||
|
||
def create_tflite_model(model): | ||
"""Converts a YOLOv4 model to a TfLite model | ||
|
||
Args: | ||
model (tensorflow.python.keras.engine.training.Model): YOLOv4 model | ||
|
||
Returns: | ||
(bytes): a binary TfLite model | ||
""" | ||
converter = tf.lite.TFLiteConverter.from_keras_model(model) | ||
converter.optimizations = [tf.lite.Optimize.DEFAULT] | ||
converter.target_spec.supported_ops = [ | ||
tf.lite.OpsSet.TFLITE_BUILTINS, | ||
tf.lite.OpsSet.SELECT_TF_OPS, | ||
] | ||
|
||
converter.allow_custom_ops = True | ||
return converter.convert() | ||
|
||
|
||
@click.command() | ||
@click.option("--num_classes", default=80, help="Number of classes") | ||
@click.option( | ||
"--weights_path", default=None, help="Path to .h5 file with model weights" | ||
) | ||
def convert_tflite(num_classes, weights_path): | ||
"""Creates a .tflite file with a trained YOLOv4 model | ||
|
||
Args: | ||
num_classes (int): Number of classes | ||
weights_path (str, optional): Path to .h5 pre-trained weights file | ||
""" | ||
model = YOLOv4( | ||
input_shape=(HEIGHT, WIDTH, 3), | ||
anchors=YOLOV4_ANCHORS, | ||
num_classes=num_classes, | ||
training=False, | ||
yolo_max_boxes=100, | ||
yolo_iou_threshold=0.4, | ||
yolo_score_threshold=0.1, | ||
) | ||
|
||
if weights_path: | ||
model.load_weights(weights_path) | ||
|
||
tflite_model = create_tflite_model(model) | ||
|
||
with tf.io.gfile.GFile(TFLITE_MODEL_PATH, "wb") as file: | ||
file.write(tflite_model) | ||
|
||
|
||
if __name__ == "__main__": | ||
convert_tflite() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Height and width are parametrizable, is it an argument stored in the tflite model or is it just used for the conversion? We want to make sure users can proceed the inference on any size