简体   繁体   English

如何减小“.h5”中的模型文件大小

[英]How to reduce model file size in ".h5"

I'm using tensorflow and keras 2.8.0 version.我正在使用 tensorflow 和 keras 2.8.0 版本。

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.关键是现在我正在尝试将模型保存为“.h5”格式,但如果我将它训练 100 个 epoch 或 1 个 epoch,我将得到一个 4.61Gb 的文件模型。

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.一般原因:h5 文件的大小仅取决于模型的参数数量。

After constructing the model add the line model.summary() and look at the number of parameters the model has in general.构建模型后,添加行model.summary()并查看模型通常具有的参数数量。

Steps to reduce model size : You have a LOT of filters in your conv layer.减小模型大小的步骤:你的 conv 层中有很多过滤器。 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.由于我不知道你想用你的模型实现什么,我仍然建议你将过滤器的数量分开到不同的卷积层,并在它们之间添加Pooling化层。 The will scale down the image and will especially reduce the number of parameters for the Flatten layer.这将缩小图像,尤其会减少Flatten层的参数数量。 More information on Pooling layers can be found here .可以在此处找到有关Pooling化层的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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