为何剪枝后的TensorFlow模型文件体积比初始模型更大?
神经网络剪枝后模型体积异常增大问题
我正在参考TensorFlow官方的剪枝示例探索神经网络剪枝,基于预训练模型的剪枝代码如下:
prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude # Compute end step to finish pruning after 2 epochs. batch_size = 64 epochs = 3 validation_split = 0.1 # 10% of training set will be used for validation set. num_images = 114 * (1 - validation_split) end_step = np.ceil(num_images / batch_size).astype(np.int32) * epochs # Define model for pruning. pruning_params = {'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity = 0.50, final_sparsity = 0.80, begin_step = 0, end_step = end_step) } pruned_model = prune_low_magnitude(model, **pruning_params) # `prune_low_magnitude` requires a recompile. pruned_model.compile(optimizer = 'adam', loss = keras.losses.SparseCategoricalCrossentropy(from_logits = True), metrics = ['accuracy']) logdir = tempfile.mkdtemp() callbacks = [ tfmot.sparsity.keras.UpdatePruningStep(), tfmot.sparsity.keras.PruningSummaries(log_dir = logdir), ] pruned_model.fit(train_dataset, batch_size=batch_size, epochs=epochs, validation_data=valid_dataset, callbacks=callbacks)
通过pruned_model.evaluate(train_dataset, verbose=0)验证,剪枝模型准确率略有下降,符合预期,测试结果如下:
Baseline test accuracy: 0.9197102189064026 Pruned model test accuracy: 0.8976686000823975
我使用model.save()将初始模型和剪枝模型分别保存为.h5和.keras格式:
- 初始模型大小约为60.7-60.9MB
- 剪枝模型的.h5格式为85.4MB,.keras格式更是达到110MB
我在Keras文档中未找到保存剪枝模型时需指定优化的相关说明。
内容的提问来源于stack exchange,提问作者Barney_Dinosaur
相关产品推荐
相关产品推荐

