qkeras.qpooling
Classes
|
Computes the quantized version of AveragePooling2D. |
|
Computes the quantized version of GlobalAveragePooling2D. |
- class qkeras.qpooling.QAveragePooling2D(*args, **kwargs)[source]
Bases:
AveragePooling2DComputes the quantized version of AveragePooling2D.
- call(inputs)[source]
Performs quantized AveragePooling followed by QActivation.
Since there is no specific parameter for averaging op, we couldn’t apply averaging quantizer to the averaging op. We have two options: 1. we perform our own average as sum first then multiply with the
inversion of the division factor: sum(x) * quantize(1/pool_area)
first, we call keras version of averaging first: y1 = keras_average(x) then multiply it with pool_size^2: y2 = y1 * pool_area Last, y3 = y2 * quantize(1/ pool_area)
Improved based on #2, but multiply x with pool_area before averaging so that we don’t lose precision during averaging. The order now becomes: first, multiply x with pool_area: y1 = x * pool_area then we call keras version of averaging: y2 = keras_average(y1) Last, y3 = y2 * quantize(1/ pool_area)
Since there is sum_pooling operation, another solution is to use depthwise_conv2d with kernel weights = 1 to get the pooling sum. In this case we don’t lose precision due to averaging. However, this solution will introduce extra weights to the layer, which might break our code elsewhere.
Since we need to match software and hardware inference numerics, we are now using #3 in the implementation.
- class qkeras.qpooling.QGlobalAveragePooling2D(*args, **kwargs)[source]
Bases:
GlobalAveragePooling2DComputes the quantized version of GlobalAveragePooling2D.
- call(inputs)[source]
Performs quantized GlobalAveragePooling followed by QActivation.
Since there is no specific parameter for averaging op, we couldn’t apply averaging quantizer to the averaging op. We have two options: 1. we perform our own average as sum first then multiply with the
inversion of the division factor: sum(x) * quantize(1/pool_area)
first, we call keras version of averaging first: y1 = keras_global_average(x) then multiply it with the denominator(pool_area) used by averaging: y2 = y1 * pool_area Last, y3 = y2 * quantize(1/ pool_area)
we perform pooling sum, and then multiply the sum with the quantized inverse multiplication factor to get the average value.
Our previous implementation uses option #2. Yet we observed minor numerical mismatch between software and hardware inference. Therefore we use #3 as the current implementation.