ValueError: Quantizing a tf.keras Model inside another tf.keras Model is not supported
ValueError: Quantizing a tf.keras Model inside another tf.keras Model is not supported
我刚开始使用 Keras/Tensorflow,我正在尝试重新训练和量化为 int8 a MobileNetV2,但我收到此错误:
ValueError: Quantizing a tf.keras Model inside another tf.keras Model is not supported.
我按照这个 guide 来绕过量化步骤,但我不确定我到底做了什么不同。
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
在这种情况下,您的 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)
我刚开始使用 Keras/Tensorflow,我正在尝试重新训练和量化为 int8 a MobileNetV2,但我收到此错误:
ValueError: Quantizing a tf.keras Model inside another tf.keras Model is not supported.
我按照这个 guide 来绕过量化步骤,但我不确定我到底做了什么不同。
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
在这种情况下,您的 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)