我使用的是tensorflow和keras 2.8.0版本。
我有以下网络:
#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)
关键是,现在我试图以".h5“格式保存模型,但是如果我将其训练为100个历元或1个历元,我将得到一个4.61Gb文件模型。
为什么这个文件这么大?我怎样才能缩小这个型号的尺寸?
发布于 2022-10-13 09:14:33
我发现(经过5个月的经验),为了缩小模型尺寸、提高精度评分和降低损失值,需要采取的步骤如下:
tf.data.Dataset.from_tensor_slices((x, y)).batch(32 , drop_remainder=True)
,当然,它应该用于培训、测试、验证。希望这是有帮助的
发布于 2022-05-11 09:59:34
通用原因:h5文件的大小仅基于模型的参数数。
在构造模型之后,添加行model.summary()
,并查看模型一般具有的参数数。
减少模型大小的步骤--:您的conv层中有很多过滤器。由于我不知道您想用您的模型实现什么,所以我仍然建议您将过滤器的数量分离到不同的conv层,并在其中添加Pooling
层。将缩小图像,特别是减少Flatten
层的参数数。有关Pooling
层的更多信息可以找到这里。
https://stackoverflow.com/questions/72197835
复制相似问题