-
Notifications
You must be signed in to change notification settings - Fork 52
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 2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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" | ||
|
||
|
||
@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 main(num_classes, weights_path): | ||
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) | ||
|
||
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 | ||
tflite_model = converter.convert() | ||
|
||
with tf.io.gfile.GFile(TFLITE_MODEL_PATH, "wb") as f: | ||
f.write(tflite_model) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 .mish import Mish | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mets un path absolu plutot que relatif |
||
|
||
__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,27 @@ | ||
""" | ||
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 __init__(self, **kwargs): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pas besoin de définir l'init si tu ne fais rien de plus |
||
super(Mish, self).__init__(**kwargs) | ||
|
||
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
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.
Est-ce que tu peux :