-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-implement caffe old-style extractors with extractor extensions (#3675
) * move crop extractor * Add concat_ext.py * Add roipooling_ext.py * Add roipooling_ext * Add scale extractor * Add scale extractor * Add bn_ext.py and dropout_ext.py * Add bn_ext.py and dropout_ext.py * Add bn_ext.py and dropout_ext.py * Fix bn.ext.py * Sort fix * Fix bn_test.py * rename to batchnorm_ext * Add bn_ext * Fix batchnorm_ext.py * small fix * Small fix
- Loading branch information
Showing
18 changed files
with
253 additions
and
491 deletions.
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,53 @@ | ||
""" | ||
Copyright (C) 2018-2021 Intel Corporation | ||
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 numpy as np | ||
|
||
from extensions.ops.BatchNormInference import BatchNormInference | ||
from mo.front.caffe.extractors.utils import embed_input | ||
from mo.front.extractor import FrontExtractorOp | ||
|
||
|
||
class BatchNormalizationExtractor(FrontExtractorOp): | ||
op = 'batchnorm' | ||
enabled = True | ||
|
||
@classmethod | ||
def extract(cls, node): | ||
eps = node.pb.batch_norm_param.eps | ||
attrs = { | ||
'eps': eps | ||
} | ||
pb_model = None if not node.soft_get('model_pb', None) else node.model_pb | ||
if pb_model: | ||
blobs = pb_model.blobs | ||
assert len(blobs) >= 2, 'BatchNorm accepts not less then two input blobs' | ||
mean = np.array(blobs[0].data) | ||
variance = np.array(blobs[1].data) | ||
|
||
if len(blobs) == 3: | ||
scale = blobs[2].data[0] | ||
if scale != 0: | ||
scale = 1.0 / scale | ||
mean *= scale | ||
variance *= scale | ||
|
||
embed_input(attrs, 1, 'gamma', np.ones(mean.shape), 'gamma') | ||
embed_input(attrs, 2, 'beta', np.zeros(variance.shape), 'beta') | ||
embed_input(attrs, 3, 'mean', mean, 'biases') | ||
embed_input(attrs, 4, 'variance', variance, 'weights') | ||
|
||
BatchNormInference.update_node_stat(node, attrs) | ||
return cls.enabled |
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
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,32 @@ | ||
""" | ||
Copyright (C) 2018-2021 Intel Corporation | ||
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. | ||
""" | ||
|
||
from mo.front.extractor import FrontExtractorOp | ||
from mo.ops.concat import Concat | ||
|
||
|
||
class ConcatFrontExtractor(FrontExtractorOp): | ||
op = 'concat' | ||
enabled = True | ||
|
||
@classmethod | ||
def extract(cls, node): | ||
pb = node.pb | ||
mapping_rule = { | ||
'axis': pb.concat_param.axis, | ||
} | ||
Concat.update_node_stat(node, mapping_rule) | ||
return cls.enabled |
2 changes: 1 addition & 1 deletion
2
...timizer/mo/front/caffe/extractors/crop.py → ...imizer/extensions/front/caffe/crop_ext.py
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
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,35 @@ | ||
""" | ||
Copyright (C) 2018-2021 Intel Corporation | ||
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. | ||
""" | ||
|
||
from mo.front.extractor import FrontExtractorOp | ||
from mo.ops.roipooling import ROIPooling | ||
|
||
|
||
class ROIPoolingFrontExtractor(FrontExtractorOp): | ||
op = 'roipooling' | ||
enabled = True | ||
|
||
@classmethod | ||
def extract(cls, node): | ||
param = node.pb.roi_pooling_param | ||
attrs = { | ||
'pooled_h': param.pooled_h, | ||
'pooled_w': param.pooled_w, | ||
'spatial_scale': param.spatial_scale, | ||
} | ||
|
||
ROIPooling.update_node_stat(node, attrs) | ||
return cls.enabled |
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,55 @@ | ||
""" | ||
Copyright (C) 2018-2021 Intel Corporation | ||
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 numpy as np | ||
|
||
from mo.front.caffe.extractors.utils import embed_input, weights_biases | ||
from mo.front.common.partial_infer.elemental import copy_shape_infer | ||
from mo.front.extractor import FrontExtractorOp | ||
from mo.ops.scale_shift import ScaleShiftOp | ||
from mo.utils.utils import NamedAttrsClass | ||
|
||
|
||
class ScaleFrontExtractor(FrontExtractorOp): | ||
op = 'scale' | ||
enabled = True | ||
|
||
@classmethod | ||
def extract(cls, node): | ||
pb = node.pb | ||
model = node.model_pb | ||
param = pb.scale_param | ||
attrs = { | ||
'axis': param.axis, | ||
} | ||
|
||
if model is None and len(pb.bottom) == 1: | ||
# default weights and biases for scale layer if the caffemodel file doesn't contain them | ||
model = NamedAttrsClass({'blobs': np.array([NamedAttrsClass({'data': np.array([1])}), | ||
NamedAttrsClass({'data': np.array([0])})])}) | ||
# scale with 1 input and 1 or 2 blobs | ||
if model and len(model.blobs) != 0 and len(pb.bottom) == 1: | ||
attrs.update(weights_biases(param.bias_term, model)) | ||
# 2 inputs + bias | ||
elif len(pb.bottom) == 2 and param.bias_term: | ||
if model is None or len(model.blobs) == 0: | ||
# default bias for scale layer with 2 inputs if the caffemodel file doesn't contain them | ||
model = NamedAttrsClass({'blobs': np.array([NamedAttrsClass({'data': np.array([0])})])}) | ||
|
||
embed_input(attrs, 1, 'biases', model.blobs[0].data) | ||
ScaleShiftOp.update_node_stat(node, attrs) | ||
return cls.enabled | ||
|
Oops, something went wrong.