google/qkeras

QSeparableConv2D: 'Keyword argument not understood:', 'depthwise_activation'

EnriqueNueve opened this issue · 1 comments

For the QSeparableConv2D layer, a error is being prompted saying that the keyword argument for depthwise_activation is not understood. Following the sample code from the readme, this error is prompted. Below is the code that prompted this error.

tensorflow==2.5.0
Qkeras==0.9.0
from tensorflow.keras.layers import *
from qkeras import *

def getQModel(INPUT_SHAPE,N_CLASSES):
    inputs = Input(INPUT_SHAPE)

    x = QConv2D(18, (3, 3),
            kernel_quantizer="stochastic_ternary",
            bias_quantizer="ternary", name="first_conv2d")(inputs)
    x = QActivation("quantized_relu(3)")(x)
    x = QSeparableConv2D(32, (3, 3),
            depthwise_quantizer=quantized_bits(4, 0, 1),
            pointwise_quantizer=quantized_bits(3, 0, 1),
            bias_quantizer=quantized_bits(3),
            depthwise_activation=quantized_tanh(6, 2, 1))(x)
    x = QActivation("quantized_relu(3)")(x)
    x = Flatten()(x)
    x = QDense(NB_CLASSES,
            kernel_quantizer=quantized_bits(3),
            bias_quantizer=quantized_bits(3))(x)
    x = QActivation("quantized_bits(20, 5)")(x)
    yh = Activation("softmax")(x)
    
    model = tf.keras.Model(inputs, yh)
    print(model.summary())
    return model

qmodel = getQModel(INPUT_SHAPE,N_CLASSES)

The error itself.

TypeError                                 Traceback (most recent call last)
/var/folders/1l/1j39gqlj2373rny0fddzmbgw0000gn/T/ipykernel_35211/3806686309.py in <module>
     26     return model
     27 
---> 28 qmodel = getQModel(INPUT_SHAPE,N_CLASSES)

/var/folders/1l/1j39gqlj2373rny0fddzmbgw0000gn/T/ipykernel_35211/3806686309.py in getQModel(INPUT_SHAPE, N_CLASSES)
      9             bias_quantizer="ternary", name="first_conv2d")(inputs)
     10     x = QActivation("quantized_relu(3)")(x)
---> 11     x = QSeparableConv2D(32, (3, 3),
     12             depthwise_quantizer=quantized_bits(4, 0, 1),
     13             pointwise_quantizer=quantized_bits(3, 0, 1),

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/qkeras/qconvolutional.py in __init__(self, filters, kernel_size, strides, padding, data_format, dilation_rate, depth_multiplier, activation, use_bias, depthwise_initializer, pointwise_initializer, bias_initializer, depthwise_regularizer, pointwise_regularizer, bias_regularizer, activity_regularizer, depthwise_constraint, pointwise_constraint, bias_constraint, depthwise_quantizer, pointwise_quantizer, bias_quantizer, **kwargs)
    766       activation = get_quantizer(activation)
    767 
--> 768     super(QSeparableConv2D, self).__init__(
    769         filters=filters,
    770         kernel_size=kernel_size,

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py in __init__(self, filters, kernel_size, strides, padding, data_format, dilation_rate, depth_multiplier, activation, use_bias, depthwise_initializer, pointwise_initializer, bias_initializer, depthwise_regularizer, pointwise_regularizer, bias_regularizer, activity_regularizer, depthwise_constraint, pointwise_constraint, bias_constraint, **kwargs)
   2205                bias_constraint=None,
   2206                **kwargs):
-> 2207     super(SeparableConv2D, self).__init__(
   2208         rank=2,
   2209         filters=filters,

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py in __init__(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, depth_multiplier, activation, use_bias, depthwise_initializer, pointwise_initializer, bias_initializer, depthwise_regularizer, pointwise_regularizer, bias_regularizer, activity_regularizer, depthwise_constraint, pointwise_constraint, bias_constraint, trainable, name, **kwargs)
   1784                name=None,
   1785                **kwargs):
-> 1786     super(SeparableConv, self).__init__(
   1787         rank=rank,
   1788         filters=filters,

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/tensorflow/python/keras/layers/convolutional.py in __init__(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, trainable, name, conv_op, **kwargs)
    127                conv_op=None,
    128                **kwargs):
--> 129     super(Conv, self).__init__(
    130         trainable=trainable,
    131         name=name,

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    520     self._self_setattr_tracking = False  # pylint: disable=protected-access
    521     try:
--> 522       result = method(self, *args, **kwargs)
    523     finally:
    524       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
    345     }
    346     # Validate optional keyword arguments.
--> 347     generic_utils.validate_kwargs(kwargs, allowed_kwargs)
    348 
    349     # Mutable properties

~/opt/anaconda3/envs/golden/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py in validate_kwargs(kwargs, allowed_kwargs, error_message)
   1135   for kwarg in kwargs:
   1136     if kwarg not in allowed_kwargs:
-> 1137       raise TypeError(error_message, kwarg)
   1138 
   1139 

TypeError: ('Keyword argument not understood:', 'depthwise_activation')

Current QSeparableConv2D layer follows Keras implementation and doesn't apply activation after the depthwise step. The readme refers to the old implementation based on MobileNet that does this. That implementation is still available as QMobileNetSeparableConv2D. We'll fix the readme, thanks for reporting!