Reputation: 45
I am working on a binary classification problem using DenseNet121 on Keras. The original model yields an accuracy of ~93% and when float16 quantized (PTQ), accuracy acceptably drops to ~90%. I was experimenting with INT8 quantization when I realized the preprocess module of DenseNet scales input images between 0 and 1 and each channel is normalized with respect to the ImageNet dataset (as in here). Preprocessing my input images is essential but INT8 input results in all the pixel values becoming 0.
Here's my converter:
converter = tf.lite.TFLiteConverter.from_keras_model(trained_densenet_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
model_lite = converter.convert()
And here's how I've defined the representative_data_gen
:
def representative_data_gen():
for images, _ in train_ds.take(100):
yield[images]
I have tried explicitly defining input and output tensors as FLOAT16, but that still results in predictions 0.
Upvotes: 0
Views: 58
Reputation: 45
representative_dataset()
should actually have images with preprocessor if there is one like in this case. The correct function for DenseNet121 should be defined as:
def representative_data_gen():
for images, _ in train_ds.take(100):
tf.keras.applications.densenet.preprocess_input(yield[images])
Upvotes: 0