简体   繁体   中英

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 ?

General reason : The size of your h5 file is based only on the number of parameters your model has.

After constructing the model add the line model.summary() and look at the number of parameters the model has in general.

Steps to reduce model size : You have a LOT of filters in your conv layer. Since I don't know what you want to achieve with your model, I would still advise you to seperate the number of filters to different conv layers and add Pooling layers in between. The will scale down the image and will especially reduce the number of parameters for the Flatten layer. More information on Pooling layers can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM