Possible to do heatmaps using tflite models? #100
-
Is it possible to do heatmaps on keras_cv_attention_models once they are converted to tflite format? To save server space I convert trained models to tflite format but I still also want to do heatmaps with new images. Is this possible? From what I've read you cannot convert a tflite model back to a keras model. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think it's possible but not easy refering Applying Grad-Cam to a model that was converted to Tensorflow Lite. Just we need the original keras model anyway, for exporting a TFLite model with |
Beta Was this translation helpful? Give feedback.
-
OK, I think it's workng now. Two functions from keras_cv_attention_models import efficientnet, test_images
mm = efficientnet.EfficientNetV2B0()
from keras_cv_attention_models.visualizing.gradcam_heatmap import ModelWithGradForTFLite, grads_to_heatmap, apply_heatmap
saved_model = mm.name
bb = ModelWithGradForTFLite(mm)
signatures = {'serving_default': bb.serving_default.get_concrete_function(), 'gradcam': bb.gradcam.get_concrete_function()}
tf.saved_model.save(bb, saved_model, signatures=signatures)
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model)
# enable TensorFlow and TensorFlow ops.
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open(mm.name + ".tflite", "wb").write(tflite_model) # Will save a much larger model than without these signatures
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
gradcam = interpreter.get_signature_runner("gradcam")
image = test_images.dog_cat()
gg = gradcam(inputs=tf.expand_dims(tf.image.resize(image , [224, 224]) / 255, 0))
grads, last_conv_layer_output, class_channel = gg['grads'], gg['last_conv_layer_output'], gg['class_channel']
# Show
heatmap = grads_to_heatmap(grads, last_conv_layer_output, class_channel=class_channel, use_v2=True)
superimposed_img = apply_heatmap(image, heatmap, alpha=0.8, plot=True) You may find the raw code test in kecam_test.ipynb TFLite Heatmap. But still it's not recommended, as the converted TFLite model will be much larger, almost |
Beta Was this translation helpful? Give feedback.
OK, I think it's workng now. Two functions
grads_to_heatmap
andapply_heatmap
are separated out from previousmake_and_apply_gradcam_heatmap
, and added a converting classModelWithGradForTFLite
withgradcam
signature.