Skip to content

Commit

Permalink
Add ported effdet keras tutorial (sony#839)
Browse files Browse the repository at this point in the history
* Add Keras EffDet-lite0 notebook and resources

This PR adds the code for porting the EfficientDet-lite0 pretrained model from the efficientdet-pytorch repo to a keras model with a custom post-processing layer from sony-custom-layer.

Also added a tutorail notebook for porting the model and quantizing it with MCT

---------

Co-authored-by: elad cohen <[email protected]>
  • Loading branch information
elad-c and elad cohen authored Oct 25, 2023
1 parent 25570a0 commit 7030635
Show file tree
Hide file tree
Showing 11 changed files with 2,343 additions and 64 deletions.
1 change: 1 addition & 0 deletions model_compression_toolkit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
FOUND_TORCH = importlib.util.find_spec("torch") is not None
FOUND_ONNX = importlib.util.find_spec("onnx") is not None
FOUND_ONNXRUNTIME = importlib.util.find_spec("onnxruntime") is not None
FOUND_SONY_CUSTOM_LAYERS = importlib.util.find_spec('sony_custom_layers') is not None

WEIGHTS_SIGNED = True
# Minimal threshold to use for quantization ranges:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# ==============================================================================
import tensorflow as tf
from packaging import version
from model_compression_toolkit.constants import FOUND_SONY_CUSTOM_LAYERS

if FOUND_SONY_CUSTOM_LAYERS:
from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess

if version.parse(tf.__version__) >= version.parse("2.13"):
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
Expand Down Expand Up @@ -54,29 +57,34 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
tf.reshape,
Permute,
tf.transpose,
Flatten,
Cropping2D,
ZeroPadding2D,
Dropout,
MaxPooling2D,
tf.split,
tf.quantization.fake_quant_with_min_max_vars,
tf.math.argmax,
tf.shape,
tf.math.equal,
tf.gather,
tf.cast,
tf.unstack,
tf.compat.v1.gather,
tf.nn.top_k,
tf.__operators__.getitem,
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

with keras_tpc:
tp.OperationsSetToLayers("NoQuantization", [Reshape,
tf.reshape,
Permute,
tf.transpose,
Flatten,
Cropping2D,
ZeroPadding2D,
Dropout,
MaxPooling2D,
tf.split,
tf.quantization.fake_quant_with_min_max_vars,
tf.math.argmax,
tf.shape,
tf.math.equal,
tf.gather,
tf.cast,
tf.unstack,
tf.compat.v1.gather,
tf.nn.top_k,
tf.__operators__.getitem,
tf.image.combined_non_max_suppression,
tf.compat.v1.shape])
tp.OperationsSetToLayers("NoQuantization", no_quant_list)

tp.OperationsSetToLayers("Conv", [Conv2D,
DepthwiseConv2D,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
# ==============================================================================
import tensorflow as tf
from packaging import version
from model_compression_toolkit.constants import FOUND_SONY_CUSTOM_LAYERS

if FOUND_SONY_CUSTOM_LAYERS:
from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess

if version.parse(tf.__version__) >= version.parse("2.13"):
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
Expand Down Expand Up @@ -53,28 +57,34 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
tf.reshape,
Permute,
tf.transpose,
Flatten,
Cropping2D,
ZeroPadding2D,
Dropout,
MaxPooling2D,
tf.split,
tf.quantization.fake_quant_with_min_max_vars,
tf.math.argmax,
tf.shape,
tf.math.equal,
tf.gather,
tf.cast,
tf.unstack,
tf.compat.v1.gather,
tf.nn.top_k,
tf.__operators__.getitem,
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

with keras_tpc:
tp.OperationsSetToLayers("NoQuantization", [Reshape,
tf.reshape,
Permute,
tf.transpose,
Flatten,
Cropping2D,
ZeroPadding2D,
Dropout,
MaxPooling2D,
tf.split,
tf.quantization.fake_quant_with_min_max_vars,
tf.math.argmax,
tf.shape,
tf.math.equal,
tf.gather,
tf.cast,
tf.unstack,
tf.compat.v1.gather,
tf.nn.top_k,
tf.__operators__.getitem,
tf.compat.v1.shape])
tp.OperationsSetToLayers("NoQuantization", no_quant_list)

tp.OperationsSetToLayers("Conv", [Conv2D,
DepthwiseConv2D,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
# ==============================================================================
import tensorflow as tf
from packaging import version
from model_compression_toolkit.constants import FOUND_SONY_CUSTOM_LAYERS

if FOUND_SONY_CUSTOM_LAYERS:
from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess

if version.parse(tf.__version__) >= version.parse("2.13"):
from keras.src.layers import Conv2D, DepthwiseConv2D, Dense, Reshape, ZeroPadding2D, Dropout, \
Expand Down Expand Up @@ -54,28 +57,34 @@ def generate_keras_tpc(name: str, tp_model: tp.TargetPlatformModel):

keras_tpc = tp.TargetPlatformCapabilities(tp_model, name=name, version=TPC_VERSION)

no_quant_list = [Reshape,
tf.reshape,
Permute,
tf.transpose,
Flatten,
Cropping2D,
ZeroPadding2D,
Dropout,
MaxPooling2D,
tf.split,
tf.quantization.fake_quant_with_min_max_vars,
tf.math.argmax,
tf.shape,
tf.math.equal,
tf.gather,
tf.cast,
tf.unstack,
tf.compat.v1.gather,
tf.nn.top_k,
tf.__operators__.getitem,
tf.image.combined_non_max_suppression,
tf.compat.v1.shape]

if FOUND_SONY_CUSTOM_LAYERS:
no_quant_list.append(SSDPostProcess)

with keras_tpc:
tp.OperationsSetToLayers("NoQuantization", [Reshape,
tf.reshape,
Permute,
tf.transpose,
Flatten,
Cropping2D,
ZeroPadding2D,
Dropout,
MaxPooling2D,
tf.split,
tf.quantization.fake_quant_with_min_max_vars,
tf.math.argmax,
tf.shape,
tf.math.equal,
tf.gather,
tf.cast,
tf.unstack,
tf.compat.v1.gather,
tf.nn.top_k,
tf.__operators__.getitem,
tf.compat.v1.shape])
tp.OperationsSetToLayers("NoQuantization", no_quant_list)

tp.OperationsSetToLayers("Conv", [Conv2D,
DepthwiseConv2D,
Expand Down
Loading

0 comments on commit 7030635

Please sign in to comment.