基于Keras的模型量化(PTQ、QAT)

12 篇文章 1 订阅
8 篇文章 26 订阅

对PTQ和QAT的详细解释在这篇哦:
《模型量化(三)—— 量化感知训练QAT(pytorch)》

本文给的代码是基于tensorflow

PTQ

只量化权重

只是优化了模型大小,对于模型的计算没什么优化,因为W * X时,W要反量化为浮点进行运算,相当于还增加了反量化这一累赘操作…

import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()

权重和激活值全量化

通过量化权重和激活,可以同时改善功耗和时延,因为最关键的密集部分W * X使用 8 位而不是浮点数进行计算。这需要一个较小的calibration数据集来计算激活值反量化时的S和Z操作。

import tensorflow as tf

def representative_dataset_gen():
  for _ in range(num_calibration_steps):
    # Get sample input data as a numpy array in a method of your choosing.
    yield [input]

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
tflite_quant_model = converter.convert()

详细内容参考官方的说明:
https://www.tensorflow.org/model_optimization/guide/quantization/post_training?hl=zh-cn

 

QAT

套路创建和训练模型

!pip install -q tensorflow
!pip install -q tensorflow-model-optimization

import tempfile
import os
import tensorflow as tf
from tensorflow_model_optimization.python.core.keras.compat import keras

# Load MNIST dataset
mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Normalize the input image so that each pixel value is between 0 to 1.
train_images = train_images / 255.0
test_images = test_images / 255.0

# Define the model architecture.
model = keras.Sequential([
  keras.layers.InputLayer(input_shape=(28, 28)),
  keras.layers.Reshape(target_shape=(28, 28, 1)),
  keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation='relu'),
  keras.layers.MaxPooling2D(pool_size=(2, 2)),
  keras.layers.Flatten(),
  keras.layers.Dense(10)
])

# Train the digit classification model
model.compile(optimizer='adam',
              loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(
  train_images,
  train_labels,
  epochs=1,
  validation_split=0.1,
)

 

用QAT克隆和微调预训练模型

import tensorflow_model_optimization as tfmot

quantize_model = tfmot.quantization.keras.quantize_model

# q_aware stands for for quantization aware.
q_aware_model = quantize_model(model)

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

q_aware_model.summary()

##############################################################
train_images_subset = train_images[0:1000] # out of 60000
train_labels_subset = train_labels[0:1000]

q_aware_model.fit(train_images_subset, train_labels_subset,
                  batch_size=500, epochs=1, validation_split=0.1)

##############################################################
_, baseline_model_accuracy = model.evaluate(
    test_images, test_labels, verbose=0)

_, q_aware_model_accuracy = q_aware_model.evaluate(
   test_images, test_labels, verbose=0)

print('Baseline test accuracy:', baseline_model_accuracy)
print('Quant test accuracy:', q_aware_model_accuracy)
#现在模型还是float32,不是int8

 

量化模型

用上面介绍的PTQ方法

converter = tf.lite.TFLiteConverter.from_keras_model(q_aware_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

quantized_tflite_model = converter.convert()

 

评估TF和TFLite

import numpy as np

def evaluate_model(interpreter):
  input_index = interpreter.get_input_details()[0]["index"]
  output_index = interpreter.get_output_details()[0]["index"]

  # Run predictions on every image in the "test" dataset.
  prediction_digits = []
  for i, test_image in enumerate(test_images):
    if i % 1000 == 0:
      print('Evaluated on {n} results so far.'.format(n=i))
    # Pre-processing: add batch dimension and convert to float32 to match with
    # the model's input data format.
    test_image = np.expand_dims(test_image, axis=0).astype(np.float32)
    interpreter.set_tensor(input_index, test_image)

    # Run inference.
    interpreter.invoke()

    # Post-processing: remove batch dimension and find the digit with highest
    # probability.
    output = interpreter.tensor(output_index)
    digit = np.argmax(output()[0])
    prediction_digits.append(digit)

  print('\n')
  # Compare prediction results with ground truth labels to calculate accuracy.
  prediction_digits = np.array(prediction_digits)
  accuracy = (prediction_digits == test_labels).mean()
  return accuracy



###################################################################
interpreter = tf.lite.Interpreter(model_content=quantized_tflite_model)
interpreter.allocate_tensors()

test_accuracy = evaluate_model(interpreter)

print('Quant TFLite test_accuracy:', test_accuracy)
print('Quant TF test accuracy:', q_aware_model_accuracy)
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,针对您的问题,我可以给您提供一些思路和代码示例。 首先,我们需要准备好房价数据集,可以使用Scikit-learn库中的波士顿房价数据集进行实验。数据集包含506个样本,每个样本有13个特征,目标是预测房屋价格。可以使用以下代码导入数据集: ```python from sklearn.datasets import load_boston boston = load_boston() X = boston.data y = boston.target ``` 然后,我们需要对数据进行预处理,包括特征缩放和数据划分。可以使用以下代码: ```python from sklearn.preprocessing import StandardScaler from sklearn.model_selection import train_test_split scaler = StandardScaler() X = scaler.fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) ``` 接下来,我们可以使用Keras构建线性回归模型。线性回归模型是一个简单的前馈神经网络,只有一个输出层。可以使用以下代码: ```python from keras.models import Sequential from keras.layers import Dense model = Sequential() model.add(Dense(1, input_dim=13)) model.compile(loss='mean_squared_error', optimizer='adam') ``` 在模型编译之后,我们可以使用训练数据对模型进行训练。可以使用以下代码: ```python model.fit(X_train, y_train, epochs=100, batch_size=10) ``` 最后,我们可以使用测试数据集对模型进行评估和预测。可以使用以下代码: ```python from sklearn.metrics import r2_score, mean_squared_error import numpy as np y_pred = model.predict(X_test) print("R2 score: ", r2_score(y_test, y_pred)) print("MSE: ", mean_squared_error(y_test, y_pred)) ``` 这就是使用Keras构建线性回归模型对房价数据进行训练和预测的大致流程。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈O-Jay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值