ValueError:`to_quantize`只能是 keras 顺序模型或函数模型

问题描述 投票:0回答:1

我目前正在使用 Tensorflow Lite 和 Tensorflow Lite for Micro 学习 TinyML。 我正在研究 R. Banerjee 的《Hands-on TinyML》一书。

我正在尝试量化模型,但它产生了错误:

q_aware_model = tfmot.quantization.keras.quantize_model(basemodel)

量化模型中的文件/usr/local/lib/python3.12/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize.py:135 引发值错误(

ValueError:

to_quantize
只能是keras顺序模型或功能模型。

这是我练习的Python源代码:

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf

from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense

def base_model():

  model = Sequential()      
  model.add(Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape = (28,28,1)))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Conv2D(64, kernel_size=(3, 3), activation="relu"))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Flatten())
  model.add(Dropout(0.5))
  model.add(Dense(100, activation = "relu"))
  model.add(Dense(10, activation="softmax"))
  
  return model

basemodel = base_model()
basemodel.summary()

import tensorflow_model_optimization as tfmot

quantized_model = tfmot.quantization.keras.quantize_model

# q_aware stands for for quantization aware.
q_aware_model = tfmot.quantization.keras.quantize_model(basemodel)

# `quantize_model` requires a recompile.
q_aware_model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

q_aware_model.summary()

引发的错误位于以下行:q_aware_model = tfmot.quantization.keras.quantize_model(basemodel)

我不知道为什么。 你能帮我吗?

我的环境:spyder 5.5.6,Python 3.12.6 64位,tensorflow 2.17.0,tensorflow-model-optimization 0.8.0,keras 3.5.0。

谢谢。 埃迪33

tensorflow-lite quantization
1个回答
0
投票

我通过重写人工智能模型解决了自己的问题:

import numpy as np
import matplotlib.pyplot as plt

import tensorflow as tf

from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dropout, Dense
from tensorflow_model_optimization.python.core.keras.compat import keras
#import tf_keras as keras

def base_model():

  model = keras.Sequential([
  keras.layers.Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape = (28,28,1)),
  keras.layers.MaxPooling2D(pool_size=(2, 2)),
  keras.layers.Conv2D(64, kernel_size=(3, 3), activation="relu"),
  keras.layers.MaxPooling2D(pool_size=(2, 2)),
  keras.layers.Flatten(),
  keras.layers.Dropout(0.5),
  keras.layers.Dense(100, activation = "relu"),
  keras.layers.Dense(10, activation="softmax") 
])
  
  return model

basemodel = base_model()
basemodel.summary()

import tensorflow_model_optimization as tfmot

quantized_model = tfmot.quantization.keras.quantize_model

# q_aware stands for for quantization aware.
q_aware_model = tfmot.quantization.keras.quantize_model(basemodel)

# `quantize_model` requires a recompile.
q_aware_model.compile(optimizer='adam',
              loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

q_aware_model.summary()

谢谢。 埃迪33

© www.soinside.com 2019 - 2024. All rights reserved.