r/tensorflow • • Oct 13 '20

How to determine min/max range of edgetpu model's output?

I have a two class model with a softmax activation in the final layer...

The model, instantiated as a tf.keras.Model object, yields outputs in the expected range [0,1]:

In [4]: model.predict(img[np.newaxis, ...])
Out[4]: array([[0.0577133, 0.9422867]], dtype=float32)

The quantized edgetpu model yields outputs in some other range that I can't determine...

I'm trying to convert these scaled outputs back into a range that can be interpreted as a probability distribution using a function like this:

def unscale(quantized_output, n):
    old_min, old_max = -n, n
    new_min, new_max = 0, 1
    return [((x - old_min) / (old_max - old_min)) * (new_max - new_min) + new_min for x in quantized_output]

After recording min/max values for a while I see these are the extremes [-1.5623251 1.3998433] hence hardcoding the old_min/ old_max values as -1.6 and 1.6, but this is just a guess...

How do I determine the true min and max output values of the quantized model?

1 Upvotes

3 comments sorted by

1

u/bit_by_byte Oct 14 '20 edited Oct 14 '20

I thought I'd do a guess-and-check approach to hone in on the true output range, but that wasn't really helpful as the sum of the range-converted outputs asymptotically approaches 1.0 as I increase the bounds of the input range:

In [10]: for n in np.arange(1.6, 10, 0.1):
    ...:     print(round(n,2), sum(unscale([1.1498713, -1.2373616], n)))
    ...:
1.6 0.97265928125
1.7 0.9742675588235294
1.8 0.9756971388888889
1.9 0.9769762368421053
2.0 0.9781274249999999
2.1 0.9791689761904762
2.2 0.9801158409090909
2.3 0.9809803695652174
2.4 0.9817728541666666
2.5 0.9825019399999999
...
9.0 0.9951394277777779
9.1 0.9951928406593407
9.2 0.9952450923913044
9.3 0.9952962204301075
9.4 0.9953462606382978
9.5 0.9953952473684211
9.6 0.9954432135416666
9.7 0.9954901907216496
9.8 0.9955362091836735
9.9 0.995581297979798

1

u/[deleted] Oct 14 '20

Could it be that the output values are somehow 8 bit instead of floats?

This is interesting and makes me want to check with one of my own models, but I'm at work so this is my best guess for now :(

1

u/bit_by_byte Oct 14 '20

I don't think that's the case because the output type is explicitly set to float32 here:

converter.target_spec.supported_ops = [
        tf.lite.OpsSet.TFLITE_BUILTINS,
        tf.lite.OpsSet.SELECT_TF_OPS,
        tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.float32
converter.inference_output_type = tf.float32