Export custom layer parameters - DigitalMediaProfessionals/dv-sdk GitHub Wiki
Many CNN frameworks can be easily extended to support non-standard layers. But when using models with these layers, the original implementation may lack parameters when the network is serialized to the offline network model data. Thus one may need to fix the implementation of these layers to export these parameters when serializing.
Example: Keras PriorBox layer of SSD network
The original implementation of the PriorBox layer looks like this:
class PriorBox(Layer):
def __init__(self, img_size, min_size, max_size=None, aspect_ratios=None,
flip=True, variances=[0.1], clip=True, **kwargs):
# initialization
# ...
def compute_output_shape(self, input_shape):
# implementation
# ...
def call(self, x, mask=None):
# implementation
# ...
The original implementation can work correctly when run as online model, but when the model is saved to a offline network model file, the necessary parameters are not stored. So when loading an offline network model back, the network will not work properly; and the conversion tool also cannot handle them correctly because lack of information.
Fortunately in Keras it is easy to fix this problem. One only needs to implement the get_config
member function; then the Keras framework will automatically store these parameters to the offline network model file and can load them back and initialize those layers. For PriorBox
layer, the example implementation looks like this:
class PriorBox(Layer):
# ...
def get_config(self):
config = {
'img_size': self.img_size,
'min_size': self.min_size,
'max_size': self.max_size,
'flip': False,
'aspect_ratios': self.aspect_ratios,
'variances': list(self.variances),
'clip': self.clip
}
base_config = super(PriorBox, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
# ...
Since the complete details of serialization of custom layers lies outside the scope of this document, please see Keras documents for details on how to implement serialization of custom layers. One just needs to make sure that all necessary parameters are serialized to the offline model before feeding it to the conversion tool.