Custom layer definition script - DigitalMediaProfessionals/dv-sdk GitHub Wiki
One needs to provide the definition of custom layers in order for the conversion tool to recognize those custom layers. The definition must be provided as a Python script.
For each type of custom layer, one needs to provide the list of the names of necessary parameters that should be exported to the converted network; and needs to implement a function to tell the tool how to calculate the output dimensions of this kind of layer. Finally a dictionary with the name custom_layer
should be provided to map the custom layer type to the defined function and parameter list.
The list of names of necessary parameters is a list of parameter names. The tool will retrieve the parameters within the layer of those names:
custom_params = ['name1', 'name2', ... , 'nameN']
The function to calculate the output dimensions has the following prototype:
def compute_output_shape(params, input_shape):
# ...
# output_shape is a tuple of the output shape
return output_shape
params
is a dictionary of parameters of the custom layer which is passed in by the tool.
input_shape
is a tuple of input dimensions.
The function should return a tuple of output dimensions that calculated from the given input dimensions and parameters.
The dictionary custom_layer
should map each custom type to the tuple of the parameter list and output dimensions calculating function:
custom_layer = {'Type1': (custom_params_1, compute_output_shape_1),
'Type2': (custom_params_2, compute_output_shape_2),
...
'TypeN': (custom_params_N, compute_output_shape_N)}
Finally in the tool input config, one needs to specify this custom layer definition script by specify the custom_layer
parameter:
[INPUT]
; Other parameters
; ...
custom_layer = custom_layer_def.py ; Optional parameter for custom layers
; ...
Example: PriorBox layer definition script
The PriorBox layer definition script looks like this:
priorbox_params = ['img_size', 'min_size', 'max_size', 'aspect_ratios', 'variances', 'clip']
def compute_priorbox_output_shape(params, input_shape):
num_priors_ = len(params['aspect_ratios'])
layer_width = input_shape[0]
layer_height = input_shape[1]
num_boxes = num_priors_ * layer_width * layer_height
return (num_boxes, 8)
custom_layer = {'PriorBox': (priorbox_params, compute_priorbox_output_shape)}
Since PriorBox
is the only one kind of custom layer in the SSD network, these is only one pair of mapping in the custom_layer
dictionary.
If one compares the implementation of compute_priorbox_output_shape
and the original implementation of compute_output_shape
in the Keras layer, one can find that they look very similar. In fact, only the ways to retrieve parameters are different since the implementation in the custom script does not resides in the class itself, so one cannot use the self
variable to access these parameters. But besides that, it should be really easy to convert the output dimensions calculation function from the original implementation.