1

I am building a model which should classify flowers. So I created a model with Tensorflow:

  keras.layers.Conv2D(128, (3,3), activation='relu', input_shape=(imageShape[0], imageShape[1],3)),
  keras.layers.MaxPooling2D(2,2),
  keras.layers.Dropout(0.5),
  
  keras.layers.Conv2D(256, (3,3), activation='relu'),
  
  keras.layers.MaxPooling2D(2,2), 
 
  keras.layers.Conv2D(512, (3,3), activation='relu'),
  
  keras.layers.MaxPooling2D(2,2),
 
  keras.layers.Flatten(),
      
  keras.layers.Dropout(0.3),      
  
  keras.layers.Dense(280, activation='relu'),
  
  keras.layers.Dense(4, activation='softmax')

  opt = tf.keras.optimizers.RMSprop()
  model.compile(loss='categorical_crossentropy',
              optimizer= opt,
              metrics=['accuracy'])

While training I save checkpoints as .h5

checkpoint = ModelCheckpoint("preSaved"+str(time.time())+".h5", monitor='val_loss', verbose=1, 
save_best_only=True, save_weights_only=False, mode='auto', period=1)

Now I got an epoch with a pretty low loss and want to convert it to .tflite to upload it to Firebase (use it in an Android Studio App).

import tensorflow as tf

new_model= tf.keras.models.load_model(filepath="model.h5")
tflite_converter = tf.lite.TFLiteConverter.from_keras_model(new_model)
tflite_converter.inference_type=tf.uint8
tflite_converter.default_ranges_stats=[min_value,max_value]
tflite_converter.quantized_input_stats={"conv2d_6_input_6:0"[mean,std]}
tflite_converter.post_training_quantize=True
tflite_model = tflite_converter.convert()
open("tf_lite_model.tflite", "wb").write(tflite_model)

The .h5 has about 335mb and the final .tflite got 160mb.But Firebase only allows .tflite to 60 mb and if I use a local model it needs minutes to load. I read that .tflite are usually smaller. Is there a problem in my model or when I convert it to .tflite?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mare Seestern
  • 335
  • 1
  • 3
  • 13

1 Answers1

0

The model size is largely determined by your model architecture (the different layers that make up the model and the number of parameters in each layer). You can experiment changing those to get a smaller model.

Here is much simpler architecture for an image classification model. Keep in mind, of course, that going with a smaller model might have lower accuracy than a more sophisticated version.

Sachin K
  • 339
  • 4
  • 14