Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Add DepthwiseConv2d to subclassed model and efficient-net test cases #394

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .azure-pipelines/linux-CI-keras-applications-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ jobs:
pip install git+https://www.github.com/keras-team/keras-contrib.git
pip install keras-tcn==2.8.3
$(UNINSTALL_KERAS)
pip install git+https://github.com/qubvel/efficientnet
displayName: 'Install dependencies'

- script: |
Expand Down
1 change: 1 addition & 0 deletions .azure-pipelines/win32-CI-keras-applications-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ jobs:
pip install git+https://www.github.com/keras-team/keras-contrib.git
pip install keras-tcn==2.8.3
%UNINSTALL_KERAS%
pip install git+https://github.com/qubvel/efficientnet
displayName: 'Install dependencies'

- script: |
Expand Down
46 changes: 46 additions & 0 deletions applications/nightly_build/test_efn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
###############################################################################
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
###############################################################################
import os
import sys
import unittest
from os.path import dirname, abspath
from keras2onnx.proto import keras, is_keras_older_than

sys.path.insert(0, os.path.join(dirname(abspath(__file__)), '../../tests/'))
from test_utils import run_image

img_path = os.path.join(os.path.dirname(__file__), '../data', 'street.jpg')


# @unittest.skipIf(is_keras_older_than('2.2.0'), "efficientnet needs keras >= 2.2.0")
@unittest.skip("Minor discrepancy on the model output.")
class TestEfn(unittest.TestCase):

def setUp(self):
self.model_files = []

def tearDown(self):
for fl in self.model_files:
os.remove(fl)

def test_custom(self):
from efficientnet import keras as efn
keras.backend.set_learning_phase(0)
base_model = efn.EfficientNetB0(input_shape=(600, 600, 3), weights=None)
backbone = keras.Model(base_model.input, base_model.get_layer("top_activation").output)
res = run_image(backbone, self.model_files, img_path, target_size=(600, 600), rtol=1e-1)
self.assertTrue(*res)

def test_efn(self):
from efficientnet import keras as efn
keras.backend.set_learning_phase(0)
model = efn.EfficientNetB7(weights='imagenet')
res = run_image(model, self.model_files, img_path, target_size=(600, 600), rtol=1e-1)
self.assertTrue(*res)


if __name__ == "__main__":
unittest.main()
1 change: 0 additions & 1 deletion applications/nightly_build/test_gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import sys
import unittest
import keras2onnx
import onnx
import numpy as np
from keras2onnx.proto import keras
from os.path import dirname, abspath
Expand Down
5 changes: 4 additions & 1 deletion keras2onnx/_parse_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ def build_layer_outputs(model, graph, outputs):
output_dict[op_.name] = layer_dict[ln_]
else:
# fx_[1] is output node redirect function.
output_dict[fx_list[1](lobj, op_)] = layer_dict[ln_]
output_tensor = fx_list[1](lobj, op_)
assert graph.get_operation_by_name(output_tensor) is not None, "Parsing layer({}) failed.".format(
lobj)
output_dict[output_tensor] = layer_dict[ln_]

return output_dict

Expand Down
5 changes: 5 additions & 0 deletions keras2onnx/ke2onnx/layer_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def _simple_layer_name_extractor(fstr_list, node_name):


def _conv_layer_spec_outputs(layer, node):
if type(layer) == _layer.DepthwiseConv2D:
ri = node.name.rindex('/')
return node.name[:ri + 1] + 'BiasAdd'

activation_map = {
keras.activations.linear: '',
tf.nn.sigmoid: 'Sigmoid',
Expand Down Expand Up @@ -58,6 +62,7 @@ def _relu_like_spec_outputs(layer, node):
_layer.AveragePooling2D: (["{}/AvgPool"], [_default_layer_name_extractor]),
_layer.AveragePooling3D: (["{}/AvgPool"], [_default_layer_name_extractor]),
_layer.Conv2DTranspose: (["{}/conv2d_transpose"], [_simple_layer_name_extractor, _conv_layer_spec_outputs]),
_layer.DepthwiseConv2D: (["{}/depthwise"], [_simple_layer_name_extractor, _conv_layer_spec_outputs]),
_layer.LeakyReLU: (["{}/LeakyRelu"], [_default_layer_name_extractor]),
_adv_activations.PReLU: (["{}/Relu"], [_simple_layer_name_extractor, _relu_like_spec_outputs])
}
Expand Down