【问题标题】:How to perform pruning with a transfer learning model?如何使用迁移学习模型进行修剪?
【发布时间】:2021-06-25 09:28:50
【问题描述】:

基本上,我想对我的迁移学习模型进行修剪。

我使用efficientnetb0 对微生物进行分类。

import tensorflow_model_optimization as tfmot

prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude

# Compute end step to finish pruning after 2 epochs.
batch_size = 32
epochs = 25

end_step = np.ceil(len(training_set) / batch_size).astype(np.int32) * epochs

# Define model for pruning.
pruning_params = {
      'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(
                                        initial_sparsity = 0.40,                                                                 
                                        final_sparsity = 0.90,                                                                   
                                        begin_step = 0,                                                                
                                        end_step = end_step
                                        )
                  }

model_for_pruning = prune_low_magnitude(
                         efficientnetb0_transfer_model, **pruning_params)

# `prune_low_magnitude` requires a recompile.
efficientnetb0_transfer_model_for_pruning.compile(optimizer=optim,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

efficientnetb0_transfer_model_for_pruning.summary()

但是,我收到以下错误:

ValueError: Please initialize `Prune` with a supported layer. Layers should either be supported by the PruneRegistry (built-in keras layers) or should be a `PrunableLayer` instance, or should has a customer defined `get_prunable_weights` method. You passed: <class 'tensorflow.python.keras.layers.preprocessing.image_preprocessing.Rescaling'>

我做错了什么?

【问题讨论】:

    标签: tensorflow keras tensorflow-lite transfer-learning pruning


    【解决方案1】:

    你点击了这个error

    修剪 API 不够灵活。它目前期望模型中的所有层都是可修剪的(逻辑here)。理想情况下,它应该能够跳过图像重新缩放等层。您可以提交一个 github 问题,我们将进行修复。谢谢!

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 2016-05-07
      • 1970-01-01
      相关资源
      最近更新 更多