Skip to content

Commit

Permalink
Separate custom layers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elad cohen committed Oct 29, 2023
1 parent d2b3ffe commit 23256fc
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 25 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/run_keras_sony_custom_layers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run Keras Sony Custom Layers Tests
on:
workflow_dispatch: # Allow manual triggers
schedule:
- cron: 0 0 * * *
pull_request:
branches:
- main

jobs:
run-tensorflow-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Python 3
uses: actions/setup-python@v1
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install tensorflow==2.13.*
pip install sony-custom-layers
- name: Run unittests
run: |
python -m unittest discover tests/keras_tests/custom_layers_tests -v
1 change: 0 additions & 1 deletion .github/workflows/run_keras_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install sony-custom-layers
pip install tensorflow==${{ inputs.tf-version }}
- name: Run unittests
# Some tests are sensitive to memory because we use tf gradients on a multi-thread/process
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run_tests_suite_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
pip install -r requirements.txt
pip install coverage
- name: Prepare TF env
run: pip install tensorflow==2.13.* sony-custom-layers
run: pip install tensorflow==2.13.*
- name: Run tensorflow testsuite
run: coverage run --parallel-mode -m --omit "./tests*,*__init__.py" unittest tests/test_suite.py -v
- name: Prepare Torch env
Expand Down
14 changes: 14 additions & 0 deletions tests/keras_tests/custom_layers_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2023 Sony Semiconductor Israel, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import unittest

import numpy as np
import tensorflow as tf

import model_compression_toolkit as mct
from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess

keras = tf.keras
layers = keras.layers


def get_rep_dataset(n_iters, in_shape):
def rep_dataset():
for _ in range(n_iters):
yield [np.random.randn(*in_shape)]

return rep_dataset


class TestSonySsdPostProcessLayer(unittest.TestCase):

def test_custom_layer(self):
inputs = layers.Input(shape=(8, 8, 3))
x = layers.Conv2D(32, 4)(inputs)
x = layers.BatchNormalization()(x)
x = layers.Conv2D(32, 4)(x)
x = layers.ReLU()(x)
x = layers.Reshape((32, 4))(x)
ssd_pp = SSDPostProcess(tf.constant(np.random.random(size=list(x.shape[1:])), dtype=tf.float32), [1, 1, 1, 1],
[8, 8], 'sigmoid', score_threshold=0.001,
iou_threshold=0.5, max_detections=10)
outputs = ssd_pp((x, x))
model = keras.Model(inputs=inputs, outputs=outputs)

core_config = mct.core.CoreConfig(
mixed_precision_config=mct.core.MixedPrecisionQuantizationConfigV2(
use_grad_based_weights=False))
q_model, _ = mct.ptq.keras_post_training_quantization_experimental(model,
get_rep_dataset(2, (1, 8, 8, 3)),
core_config=core_config,
target_kpi=mct.KPI(weights_memory=6000))

# verify the custom layer is in the quantized model
self.assertTrue(isinstance(q_model.layers[-1], SSDPostProcess), 'Custom layer should be in the quantized model')
# verify mixed-precision
self.assertTrue(any([q_model.layers[2].weights_quantizers['kernel'].num_bits < 8,
q_model.layers[4].weights_quantizers['kernel'].num_bits < 8]))
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from model_compression_toolkit.core.common.user_info import UserInformation
from tests.keras_tests.tpc_keras import get_weights_only_mp_tpc_keras
from tests.keras_tests.utils import get_layers_from_model_by_type
from sony_custom_layers.keras.object_detection.ssd_post_process import SSDPostProcess

keras = tf.keras
layers = keras.layers
Expand All @@ -41,12 +40,12 @@ def __init__(self, unit_test, val_batch_size=1):

def get_quantization_config(self):
return mct.core.QuantizationConfig(mct.core.QuantizationErrorMethod.MSE,
mct.core.QuantizationErrorMethod.MSE,
relu_bound_to_power_of_2=True,
weights_bias_correction=True,
weights_per_channel_threshold=True,
input_scaling=True,
activation_channel_equalization=True)
mct.core.QuantizationErrorMethod.MSE,
relu_bound_to_power_of_2=True,
weights_bias_correction=True,
weights_per_channel_threshold=True,
input_scaling=True,
activation_channel_equalization=True)

def get_mixed_precision_v2_config(self):
return mct.core.MixedPrecisionQuantizationConfigV2(num_of_images=1,
Expand Down Expand Up @@ -224,9 +223,7 @@ def compare(self, quantized_model, float_model, input_x=None, quantization_info=


class MixedPercisionSearchKPI4BitsAvgTestCombinedNMS(MixedPercisionBaseTest):
def __init__(self, unit_test, layer_type):
assert layer_type in ['combined_nms', 'SSDPostProcess']
self.layer_type = layer_type
def __init__(self, unit_test):
super().__init__(unit_test)

def get_kpi(self):
Expand All @@ -239,17 +236,8 @@ def create_networks(self):
x = layers.BatchNormalization()(x)
x = layers.Conv2D(32, 4)(x)
x = layers.ReLU()(x)
if self.layer_type == 'combined_nms':
x = layers.Reshape((160, 5, 4))(x)
outputs = tf.image.combined_non_max_suppression(x, tf.reduce_mean(x, 3), 10, 10)
elif self.layer_type == 'SSDPostProcess':
x = layers.Reshape((800, 4))(x)
ssd_pp = SSDPostProcess(tf.constant(np.random.random(size=list(x.shape[1:])), dtype=tf.float32), [1, 1, 1, 1],
[*self.get_input_shapes()[0][1:3]], 'sigmoid', score_threshold=0.001,
iou_threshold=0.5, max_detections=10)
outputs = ssd_pp((x, x))
else:
raise NotImplemented
x = layers.Reshape((160, 5, 4))(x)
outputs = tf.image.combined_non_max_suppression(x, tf.reduce_mean(x, 3), 10, 10)
model = keras.Model(inputs=inputs, outputs=outputs)
return model

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ def test_mixed_precision_search_kpi_4bits_avg(self):
MixedPercisionSearchKPI4BitsAvgTest(self).run_test()

def test_mixed_precision_search_kpi_4bits_avg_nms(self):
MixedPercisionSearchKPI4BitsAvgTestCombinedNMS(self, 'SSDPostProcess').run_test()
MixedPercisionSearchKPI4BitsAvgTestCombinedNMS(self, 'combined_nms').run_test()
MixedPercisionSearchKPI4BitsAvgTestCombinedNMS(self).run_test()

def test_mixed_precision_search(self):
MixedPercisionSearchTest(self).run_test()
Expand Down

0 comments on commit 23256fc

Please sign in to comment.