我刚刚开始使用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 发布于 2022-05-16 21:24:27
在这种情况下,您的base_model行为就好像它是一个层一样。为了扩展它,您需要使用Functional,而不是顺序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://stackoverflow.com/questions/72264191
复制相似问题