Search code examples
pythontensorflowmachine-learningkerasdeep-learning

How to reduce model file size in ".h5"


I'm using tensorflow and keras 2.8.0 version.

I have the following network:

#defining model
  model=Sequential()
  #adding convolution layer
  model.add(Conv2D(256,(3,3),activation='relu',input_shape=(256,256,3)))
  #adding pooling layer
  model.add(MaxPool2D(2,2))
  #adding fully connected layer
  model.add(Flatten())
  model.add(Dense(100,activation='relu'))
  #adding output layer
  model.add(Dense(len(classes),activation='softmax'))

  #compiling the model
  model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

  #fitting the model
  model.fit(x_tr,y_tr,epochs=epochs, ) 
  # Alla 12-esima epoca, va a converge a 1
  # batch size è 125 credo, non so il motivo

  #evaluting the model
  loss_value, accuracy = model.evaluate(x_te, y_te)
  #loss_value, accuracy, top_k_accuracy = model.evaluate(x_te, y_te, batch_size=batch_size)
  print("loss_value: " + str(loss_value))
  print("acuracy: " + str(accuracy))

  #predict first 4 images in the test set
  ypred = model.predict(x_te)

The point is that now i'm trying to save the model in ".h5" format but if i train it for 100 epochs or for 1 epochs i will get a 4.61Gb file model.

Why the size of this file is that big? How can i reduce this model size ?


Solution

  • What I find out, after 5 months of experience, is that the steps to do in order to reduce the model size, improve the accuracy score and reduce the loss value are the following:

    1. categorize the labels and then change the loss function of the model
    2. normalize data in values in the range [-1,1]
    3. use the dense layer increase the parameters and then the dimension of the model: is not even helpful sometimes. Having more parameters doesn't mean have more accuracy. In order to find a solution you have to do several try changing the network, using different activation function and optimizer such as SGD or Adam.
    4. Choose good parameters for learning_rate, decay_rate, decay_values and so on. These parameters give you a better or worse result.
    5. Use batch_size = 32 or 64
    6. Use function that load the dataset step by step and not all in one time in RAM because it makes the process slower and is not even needed: if you are using keras/tensorflow then you can use tf.data.Dataset.from_tensor_slices((x, y)).batch(32 , drop_remainder=True) of course it should be done for train,test,validation

    Hope that it helps