1

导出经过训练的模型后,BatchNorm 层仍然存在。我在某处读过这些应该被删除的推理,原因有两个:

  1. 网络输出可能有误
  2. 网提速

好吧,我怀疑 1。但第二个事实听起来合乎逻辑,所以我的问题是:那么如何过滤掉图层?

环境:来自 Tensorflow GitHub 并在 Tensorflow 1.15.3 上训练的模型。

二手出口:

python deeplab/export_model.py \
--num_classes=2 --model_variant="mobilenet_v3_large_seg" \
--dataset="123" \
--checkpoint_path=training \
--crop_size=384 \
--crop_size=384 \
--export_path=graph.pb

摘录网络图:

(<tf.Tensor 'MobilenetV3/MobilenetV3/input:0' shape=(1, 768, 768, 3) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/weights:0' shape=(3, 3, 3, 16) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/weights/read:0' shape=(3, 3, 3, 16) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/Conv2D:0' shape=(1, 384, 384, 16) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/gamma:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/gamma/read:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/beta:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/beta/read:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/moving_mean:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/moving_mean/read:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/moving_variance:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/moving_variance/read:0' shape=(16,) dtype=float32>,)
(<tf.Tensor 'MobilenetV3/Conv/BatchNorm/FusedBatchNormV3:0' shape=(1, 384, 384, 16) dtype=float32>, <tf.Tensor 'MobilenetV3/Conv/BatchNorm/FusedBatchNormV3:1' shape=(16,) dtype=float32>, <tf.Tensor 'MobilenetV3/Conv/BatchNorm/FusedBatchNormV3:2' shape=(16,) dtype=float32>, <tf.Tensor 'MobilenetV3/Conv/BatchNorm/FusedBatchNormV3:3' shape=(16,) dtype=float32>, <tf.Tensor 'MobilenetV3/Conv/BatchNorm/FusedBatchNormV3:4' shape=(16,) dtype=float32>, <tf.Tensor 'MobilenetV3/Conv/BatchNorm/FusedBatchNormV3:5' shape=<unknown> dtype=float32>)
(<tf.Tensor 'MobilenetV3/Conv/hard_swish/add/y:0' shape=() dtype=float32>,)
4

1 回答 1

1

两者都是真实的。Batchnormalization 仅适用于训练期间,就像 Dropout 一样。您实际上不需要自己处理。

在推理过程中,只需使用 model.predict,库就会处理它。即,所有批标准化和丢失层都将被停用。

如果您需要做更多花哨的事情而不仅仅是预测,您还可以传递参数 Training=False。检查文档。https://www.tensorflow.org/api_docs/python/tf/keras/layers/BatchNormalization

于 2020-10-26T21:58:49.717 回答