KerasConverter

class webdnn.frontend.keras.KerasConverter(batch_size=1)[source]

Converter for Keras.

Limitations

  • Only Keras v2+ is supported.
  • Only tensorflow backend is supported.
  • Only data_format="channel_last" is supported.

If you want to implement custom handler for your custom Keras Layer, please see Use your custom operator.

Parameters:
  • batch_size (int or None) – input batch size. As default, keras handle the batch size as place holder (undetermined) value. If None is passed, converter handles the batch size as placeholder named “N”.
  • use_tensorflow_converter (bool) – If True, KerasConverter first tries to convert model by TensorFlowConverter. Then if it failed, retry conversion with KerasConverter itself.
convert(model, input_orders=None)[source]

Convert kerasmodel into WebDNN IR Graph. First, WebDNN try to convert backend TensorFlow graph by TensorFlowConverter. If TensorFlowConverter failed to convert, then KerasConverter converts model by itself

Parameters:model (keras.models.Model) – keras model

example

Convert pre-trained keras ResNet model.

import keras
from webdnn.frontend.keras import KerasConverter

model = keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet')
graph = KerasConverter(batch_size=1).convert(model)
Returns:WebDNN IR Graph
Return type:(Graph)
convert_to_constant_variable(tf_var, order)[source]

Convert TensorFlow variable (parameter of kerasmodel) into ConstantVariable.

This method also registers the mapping information between TensorFlow variable and WebDNN constant variable. If specified TensorFlow variable is already registered into converter, converter checks that the shape and order is valid

This method is provided only for implementing custom converter handler.

Parameters:
  • tf_var (tensorflow.Variable) – TensorFlow variable
  • order – (Order) data order
Returns:

converted variable.

Return type:

(ConstantVariable)

get_input_tensor(k_op)[source]

Return input tensor(s) of specified keras layer.

This method is provided only for implementing custom converter handler.

Parameters:k_op (keras.layers.Layer) – keras operator
Returns:list of input tensor(s). Even if only one element, it’s wrapped in a list.
Return type:(list of tensorflow.Tensor)
get_output_tensor(k_op)[source]

Return output tensor(s) of specified keras layer.

This method is provided only for implementing custom converter handler.

Parameters:k_op (keras.layers.Layer) – keras operator
Returns:list of output tensor(s). Even if only one element, it’s wrapped in a list.
Return type:(list of tensorflow.Tensor)