This section provides instruction on how to support a custom Caffe operation written only in Python. For example, the Faster-R-CNN model implemented in Caffe contains a custom layer Proposal written in Python. The layer is described in the Faster-R-CNN protoxt the following way:
This section describes only a procedure on how to extract operator attributes in the Model Optimizer. The rest of the operation enabling pipeline and documentation on how to support other Caffe operations (written in C++) is described in the main document Customize_Model_Optimizer.
Custom Caffe Python layers have an attribute type
(defining the type of the operation) equal to Python
and two mandatory attributes module
and layer
in the python_param
dictionary. The module
defines the Python module name with the layer implementation, while layer
value is an operation type defined by an user. In order to extract attributes for such an operation it is necessary to implement extractor class inherited from the CaffePythonFrontExtractorOp
class instead of FrontExtractorOp
class used for standard framework layers. The op
class attribute value should be set to the module + "." + layer
value so the extractor is triggered for this kind of operation.
Here is a simplified example of the extractor for the custom operation Proposal from Faster-R-CNN model mentioned above. The full code with additional checks is provided in the <INSTALL_DIR>/deployment_tools/model_optimizer/extensions/front/caffe/proposal_python_ext.py
. The sample code uses operation ProposalOp
which corresponds to Proposal
operation described in the Available Operations Sets document. Refer to the source code below for a detailed explanation of the extractor.