我剛剛開始使用 Keras/Tensorflow,我正在嘗試重新訓練并量化為 int8 a MobileNetV2,但我收到了這個錯誤:
ValueError: Quantizing a tf.keras Model inside another tf.keras Model is not supported.
我按照本指南來繞過量化步驟,但我不確定我到底在做什么不同。
IMG_SHAPE = (224, 224, 3)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
base_model.trainable = False
model = tf.keras.Sequential([
base_model,
tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.MaxPool2D(pool_size=(2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(units=2, activation='softmax')
])
quantize_model = tfmot.quantization.keras.quantize_model
q_aware_model = quantize_model(model)
堆疊跟蹤:
ValueError Traceback (most recent call last)
<ipython-input-34-b724ad4872a5> in <module>()
9
10 quantize_model = tfmot.quantization.keras.quantize_model
---> 11 q_aware_model = quantize_model(model)
4 frames
/usr/local/lib/python3.7/dist-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py in _add_quant_wrapper(layer)
217 if isinstance(layer, tf.keras.Model):
218 raise ValueError(
--> 219 'Quantizing a tf.keras Model inside another tf.keras Model is not supported.'
220 )
221
uj5u.com熱心網友回復:
在這種情況下,您的base_model行為就好像它是一個圖層。為了擴展它,你需要使用Functional API,而不是Sequential API:
IMG_SHAPE = (224, 224, 3)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
base_model.trainable = False
x = tf.keras.layers.Conv2D(filters=32, kernel_size=3, activation='relu')(base_model.output)
x = tf.keras.layers.Dropout(0.5)(x)
x = tf.keras.layers.MaxPool2D(pool_size=(2, 2))(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(units=2, activation='softmax')(x)
model = tf.keras.Model(base_model.input, x)
model.summary()
請注意,模型摘要顯示了所有層,包括base_model's. 然后你可以申請:
quantize_model = tfmot.quantization.keras.quantize_model
q_aware_model = quantize_model(model)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/476643.html
標籤:Python 张量流 喀拉斯 张量流精简版 量化感知训练
上一篇:tensorflow中pytorchmodule.register_parameter(name,param)的替代方法是什么?
下一篇:如何在自定義層張量流中修改張量?
