当前位置:   article > 正文

【Tensorflow】keras模型预测+keras模型转tflite模型(post-training模型量化)+tflite模型预测+对比量化模型和原始模型的推理速度和文件大小_experimental_new_quantizer

experimental_new_quantizer

本文python程序在在tensorflow2.4.1上运行,CUDA11.0,cuDNN8.0.5

 

之前已经在keras上用多种数据输入方式训练得到keras模型

【Tensorflow】训练keras模型+keras的数据生成器ImageDataGenerator+jpg图像数据格式的MNIST数据集+对比flow和flow_from_directory

【Tensorflow】训练keras模型+tensorflow-V2的数据集tf.data.Dataset+jpg图像数据格式的MNIST数据集+MobileNet

 

博主在某个6分类的数据集上训练下面的keras模型 :

  1. import tensorflow as tf
  2. def single_net_block(inputs,classes,postfix='r1'):
  3. h1 = tf.keras.layers.Conv2D(32, 3, activation='relu', name='conv1_' + postfix)(inputs)
  4. h1 = tf.keras.layers.Conv2D(64, 3, activation='relu', name='conv2_' + postfix)(h1)
  5. block1_out = tf.keras.layers.MaxPooling2D(3, name='pool1_' + postfix)(h1)
  6. h2 = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same', name='conv3_' + postfix)(block1_out)
  7. h2 = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same', name='conv4_' + postfix)(h2)
  8. block2_out = tf.keras.layers.add([h2, block1_out], name='add1_' + postfix)
  9. h3 = tf.keras.layers.Conv2D(64, 3, activation='relu', padding='same', name='conv5_' + postfix)(block2_out)
  10. # h3 = tf.keras.layers.Conv2D(16, 3, activation='relu', padding='same', name='conv6_' + postfix)(h3)
  11. h3 = tf.keras.layers.Conv2D(64, 1, activation='relu', padding='same', name='conv6_' + postfix)(h3)
  12. block3_out = tf.keras.layers.add([h3, block2_out], name='add2_' + postfix)
  13. h4 = tf.keras.layers.Conv2D(64, 3, activation='relu', name='conv7_' + postfix)(block3_out)
  14. return h4
  15. def single_net(classes=10,im_w=256,im_h=256,im_channels=3,postfix='r1'):
  16. inputs = tf.keras.Input(shape=(im_w, im_h, im_channels), name='input_'+postfix)
  17. h4 = single_net_block(inputs, classes, postfix)
  18. h4 = tf.keras.layers.GlobalMaxPool2D(name='pool2_' + postfix)(h4)
  19. h4 = tf.keras.layers.Dense(256, activation='relu', name='dense1_' + postfix)(h4)
  20. outputs = tf.keras.layers.Dense(classes, activation='softmax', name='dense2_' + postfix)(h4)
  21. model = tf.keras.Model(inputs, outputs, name=postfix)
  22. model.summary()
  23. tf.keras.utils.plot_model(model, 'architecture_' + postfix + '.png', show_shapes=True)
  24. return model

网络结构图

训练过程:

测试集结果:99.0%,fps为23ms

test time (ms/frame):  23.402893940607708
test accuracy: 0.990000

模型大小:1557KB

 

接下来,

用tf.lite的相关api将keras模型转换为tflite模型(模型量化)

这里需要用到:tf.lite.TFLiteConverter

这里支持savedmodel(https://www.alibabacloud.com/help/zh/doc-detail/111030.htm)、keras模型或者其他的具体的模型(可以自定义构造方法,没有试过。。。)

 

tf.lite.TFLiteConverter.from_keras_model会返回一个tf.lite.TFLiteConverter对象,其属性如上所示:

常见的post_training_quantize属性(在tf2.0版本被舍弃了)

之前博主参考https://www.cnblogs.com/jiangxinyang/p/12056209.html看到的。

查看lite.py (https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/lite/python/lite.py#L456-L1034)

  1. post_training_quantize: Deprecated. Please specify `[Optimize.DEFAULT]` for
  2. `optimizations` instead. Boolean indicating whether to quantize the
  3. weights of the converted float model. Model size will be reduced and
  4. there will be latency improvements (at the cost of accuracy). (default
  5. False)

这个属性会被舍弃,请用属性optimizations

 

属性optimizations的值是tf.lite.Optimize

optimizations有三种选择:

  1. DEFAULT
  2. OPTIMIZE_FOR_LATENCY
  3. OPTIMIZE_FOR_SIZE

https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/lite/python/lite.py#L79-L111

  1. class Optimize(enum.Enum):
  2. """Enum defining the optimizations to apply when generating tflite graphs.
  3. Some optimizations may come at the cost of accuracy.
  4. """
  5. # Default optimization strategy.
  6. #
  7. # Converter will do its best to improve size and latency based on the
  8. # information provided.
  9. # Enhanced optimizations can be gained by providing a representative_dataset.
  10. # This is recommended, and is currently equivalent to the modes below.
  11. # Currently, weights will be quantized and if representative_dataset is
  12. # provided, activations for quantizable operations will also be quantized.
  13. DEFAULT = "DEFAULT"
  14. # Optimize for size.
  15. #
  16. # Optimizations that reduce the size of the model.
  17. # The model size will be reduced.
  18. # Currently, weights will be quantized and if representative_dataset is
  19. # provided, activations for quantizable operations will also be quantized.
  20. OPTIMIZE_FOR_SIZE = "OPTIMIZE_FOR_SIZE"
  21. # Optimize for latency.
  22. #
  23. # Optimizations that reduce the latency of the model.
  24. # Currently, weights will be quantized and if representative_dataset is
  25. # provided, activations for quantizable operations will also be quantized.
  26. OPTIMIZE_FOR_LATENCY = "OPTIMIZE_FOR_LATENCY"
  27. def __str__(self):
  28. return self.value

通过源码可以看到,

DEFAULT 模式的作用是:

转换器将根据所提供的信息尽力改进大小和延迟。

权重默认会被量化,如果提供了代表性数据集,可量化操作的激活函数也将被量化。

OPTIMIZE_FOR_SIZE 模式的作用是:重点缩小模型的大小

OPTIMIZE_FOR_LATENCY 模式的作用是:重点减少模型的前向传播延迟,也就是提升速度

 

两个新属性experimental_new_converter和experimental_new_quantizer

https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/lite/python/lite.py#L266-L471

  1. experimental_new_converter: Experimental flag, subject to change.
  2. Enables MLIR-based conversion instead of TOCO conversion.
  3. experimental_new_quantizer: Experimental flag, subject to change.
  4. Enables MLIR-based post-training quantization.

experimental_new_converter默认开启(True),experimental_new_quantizer默认关闭(False)

新量化方法基于MLIR

 

量化代码(tflite模型转换)

  1. import tensorflow as tf
  2. from tensorflow.keras.models import load_model
  3. model = load_model('models_resnet.hdf5')
  4. converter = tf.lite.TFLiteConverter.from_keras_model(model)
  5. # converter.experimental_new_converter=False#旧的量化方法
  6. converter.experimental_new_quantizer=True#新的量化方法,基于MLIR
  7. #上面两行代码,选择一行注释,注释第一行为新方法,注释第二行为旧方法
  8. converter.optimizations = ["DEFAULT"]
  9. tflite_model = converter.convert()
  10. open("quantize_models_resnet_new_DEFAULT.tflite", "wb").write(tflite_model)
  11. converter.optimizations = ["OPTIMIZE_FOR_SIZE"]
  12. tflite_model = converter.convert()
  13. open("quantize_models_resnet_new_OPTIMIZE_FOR_SIZE.tflite", "wb").write(tflite_model)
  14. #
  15. converter.optimizations = ["OPTIMIZE_FOR_LATENCY"]
  16. tflite_model = converter.convert()
  17. open("quantize_models_resnet_new_OPTIMIZE_FOR_LATENCY.tflite", "wb").write(tflite_model)

结果转换之后这几个模型的大小几乎都一样:旧的方法比新的方法小了1kb

对比原始keras模型,压缩率的定义如下:

压缩率(Compression rate),描述压缩文件的效果名,是文件压缩后的大小与压缩前的大小之比,例如:把100m的文件压缩后是90m,压缩率为90/100*100%=90%,压缩率一般是越小越好,但是压得越小,解压时间越长。

https://baike.baidu.com/item/%E5%8E%8B%E7%BC%A9%E7%8E%87/6435712?fr=aladdin

tensorflow量化方法的模型压缩率为47.9%,量化模型的大小约为原始模型的1/2

 

tflite模型预测

预测代码(tflite模型预测)

  1. import time
  2. import tensorflow as tf
  3. import gc
  4. gc.collect()
  5. tf.keras.backend.clear_session()
  6. tf.compat.v1.reset_default_graph()
  7. import numpy as np
  8. from get_RSM_data import get_data_list,test_preprocess_image,preprocess_label
  9. class_name=['Cr','In','Pa','PS','RS','Sc']
  10. file_dir='../database/'
  11. log_dir='./logs/'
  12. classes=len(class_name)
  13. train_ratio = 2 / 3
  14. val_ratio= 2 / 10
  15. nums_of_each_classes = 300
  16. nums_for_training=int(len(class_name)*nums_of_each_classes*train_ratio)
  17. nums_for_validation=int(float(nums_for_training)*val_ratio)
  18. im_w=256
  19. im_h=256
  20. im_channels=3
  21. divide = 8
  22. batch_size=32
  23. epochs=500
  24. _,_,test_image_list,test_label_list,_,_=get_data_list(file_dir,class_name,nums_of_each_classes,nums_for_training,nums_for_validation)
  25. test_image = []
  26. test_label = []
  27. for image, label in zip(test_image_list, test_label_list):
  28. r_image = test_preprocess_image(image,im_w=im_w, im_h=im_h, im_channels=im_channels)
  29. r_label = preprocess_label(label,classes=classes,one_hot=False)
  30. test_image.append(r_image)
  31. test_label.append(r_label)
  32. test_images = np.array(test_image)
  33. test_labels = np.array(test_label)
  34. print(test_images.shape)
  35. print(test_labels.shape)
  36. interpreter = tf.lite.Interpreter(model_path="quantize_models_resnet_OPTIMIZE_FOR_SIZE.tflite")
  37. interpreter.allocate_tensors()
  38. input_tensor_index = interpreter.get_input_details()[0]["index"]
  39. output = interpreter.tensor(interpreter.get_output_details()[0]["index"])
  40. start_time = time.time()
  41. prediction_digits = []
  42. for test_image in test_images:
  43. # Pre-processing: add batch dimension and convert to float32 to match with
  44. # the model's input data format.
  45. test_image = np.expand_dims(test_image, axis=0).astype(np.float32)
  46. interpreter.set_tensor(input_tensor_index, test_image)
  47. # Run inference.
  48. interpreter.invoke()
  49. # Post-processing: remove batch dimension and find the digit with highest
  50. # probability.
  51. digit = np.argmax(output()[0])
  52. prediction_digits.append(digit)
  53. end_time = time.time()
  54. print('test time (ms/frame): ',(end_time - start_time) / len(prediction_digits) * 1000)
  55. accurate_count = 0
  56. for index in range(len(prediction_digits)):
  57. if prediction_digits[index] == test_labels[index]:
  58. accurate_count += 1
  59. accuracy = float(accurate_count) * 1.0 / float(len(prediction_digits))
  60. print('test accuracy: %.6f'%accuracy)
  61. gc.collect()
  62. tf.keras.backend.clear_session()
  63. tf.compat.v1.reset_default_graph()

 

预测代码(keras模型)

  1. import time
  2. import tensorflow as tf
  3. import gc
  4. gc.collect()
  5. tf.keras.backend.clear_session()
  6. tf.compat.v1.reset_default_graph()
  7. import numpy as np
  8. from get_RSM_data import get_data_list,test_preprocess_image,preprocess_label
  9. class_name=['Cr','In','Pa','PS','RS','Sc']
  10. file_dir='../database/'
  11. log_dir='./logs/'
  12. classes=len(class_name)
  13. train_ratio = 2 / 3
  14. val_ratio= 2 / 10
  15. nums_of_each_classes = 300
  16. nums_for_training=int(len(class_name)*nums_of_each_classes*train_ratio)
  17. nums_for_validation=int(float(nums_for_training)*val_ratio)
  18. im_w=256
  19. im_h=256
  20. im_channels=3
  21. divide = 8
  22. batch_size=32
  23. epochs=500
  24. _,_,test_image_list,test_label_list,_,_=get_data_list(file_dir,class_name,nums_of_each_classes,nums_for_training,nums_for_validation)
  25. test_image = []
  26. test_label = []
  27. for image, label in zip(test_image_list, test_label_list):
  28. r_image = test_preprocess_image(image,im_w=im_w, im_h=im_h, im_channels=im_channels)
  29. r_label = preprocess_label(label,classes=classes,one_hot=False)
  30. test_image.append(r_image)
  31. test_label.append(r_label)
  32. test_images = np.array(test_image)
  33. test_labels = np.array(test_label)
  34. print(test_images.shape)
  35. print(test_labels.shape)
  36. # interpreter = tf.lite.Interpreter(model_path="quantize_models_resnet_OPTIMIZE_FOR_SIZE.tflite")
  37. # interpreter.allocate_tensors()
  38. # input_tensor_index = interpreter.get_input_details()[0]["index"]
  39. # output = interpreter.tensor(interpreter.get_output_details()[0]["index"])
  40. from tensorflow.keras.models import load_model
  41. model = load_model('models_resnet.hdf5')
  42. # cost = model.evaluate(test_images,test_labels)
  43. # print('test loss: ', cost)
  44. start_time = time.time()
  45. prediction_digits = []
  46. for test_image in test_images:
  47. test_image = np.expand_dims(test_image, axis=0).astype(np.float32)
  48. res=model.predict(test_image, batch_size=1)
  49. digit = np.argmax(res[0])
  50. prediction_digits.append(digit)
  51. #
  52. end_time = time.time()
  53. print('test time (ms/frame): ',(end_time - start_time) / len(prediction_digits) * 1000)
  54. #
  55. accurate_count = 0
  56. for index in range(len(prediction_digits)):
  57. # print(prediction_digits[index],test_labels[index])
  58. if prediction_digits[index] == test_labels[index]:
  59. accurate_count += 1
  60. accuracy = float(accurate_count) * 1.0 / float(len(prediction_digits))
  61. print('test accuracy: %.6f'%accuracy)
  62. gc.collect()
  63. tf.keras.backend.clear_session()
  64. tf.compat.v1.reset_default_graph()

tflite模型预测测试结果

  • 新量化方法的结果

quantize_models_resnet_new_DEFAULT.tflite

test time (ms/frame):  43.77090573310852
test accuracy: 0.990000

quantize_models_resnet_new_OPTIMIZE_FOR_LATENCY

test time (ms/frame):  45.00792781511942
test accuracy: 0.990000

quantize_models_resnet_new_OPTIMIZE_FOR_SIZE

test time (ms/frame):  44.728724559148155
test accuracy: 0.990000

 

  • 旧方法的结果

quantize_models_resnet_DEFAULT

test time (ms/frame):  44.802682399749756
test accuracy: 0.990000

quantize_models_resnet_OPTIMIZE_FOR_LATENCY

test time (ms/frame):  44.54366246859232
test accuracy: 0.990000

quantize_models_resnet_OPTIMIZE_FOR_SIZE

 test time (ms/frame):  44.73690390586853
test accuracy: 0.990000

 

测试结果表明基于post-training的模型量化,不论旧方法或者新方法在测试准确率上均没有变化,和原始模型的测试准确率几乎相等,可以说是没有损失的压缩,

但是,压缩模型的预测速度慢了接近一倍(压缩模型44ms,原始模型23ms),

新量化方法的DEFAULT模式可能会稍微快一点,但还是43ms而已,我本来还以为会加速呢,结果只是模型变小了而已(失望!!!)

 

当然也可能和我的网络模型有关,量化一般是做权重偏置和激活函数的输出值,

博主设计的网络中,用的激活函数都是relu,也没有做BN归一化中间特征图的数据分布,

所以不好做量化,可能会在量化和反量化之间浪费了很多时间,后面再做验证!

 

——————分割线——————

 

后面我又继续用tensorflow_model_optimization对keras模型进行量化,进行Quantization aware training

  1. import tensorflow_model_optimization as tfmot
  2. quantize_model = tfmot.quantization.keras.quantize_model
  3. # q_aware stands for for quantization aware.
  4. q_aware_model = quantize_model(model)
  5. optimizer=tf.keras.optimizers.RMSprop()
  6. q_aware_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
  7. q_aware_model.summary()
  8. q_aware_model.fit(train_ds,batch_size=batch_size,epochs=epochs,validation_data=val_ds)
  9. converter = tf.lite.TFLiteConverter.from_keras_model(q_aware_model)
  10. converter.optimizations = ["DEFAULT"]
  11. tflite_model = converter.convert()
  12. open("quantize_models_resnet_tflite.tflite", "wb").write(tflite_model)
  13. cost = q_aware_model.evaluate(test_ds)
  14. print('test loss: ', cost)

但是生成的keras模型或者转化的tflite模型都不能用上面的读取方法读取测试,暂时没有找到解决方法,

 keras模型读取失败

  1. from tensorflow.keras.models import load_model
  2. model = load_model('quantize_models_resnet.hdf5')

报错:ValueError: Unknown layer: QuantizeLayer

  1. Traceback (most recent call last):
  2. File "H:/Leon/CODE/python_projects/master_ImRecognition/dataset/NEU/ThesisTest_Leon/test_keras.py", line 52, in <module>
  3. model = load_model('quantize_models_resnet.hdf5')
  4. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\saving\save.py", line 207, in load_model
  5. compile)
  6. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 184, in load_model_from_hdf5
  7. custom_objects=custom_objects)
  8. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\saving\model_config.py", line 64, in model_from_config
  9. return deserialize(config, custom_objects=custom_objects)
  10. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 177, in deserialize
  11. printable_module_name='layer')
  12. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 358, in deserialize_keras_object
  13. list(custom_objects.items())))
  14. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 669, in from_config
  15. config, custom_objects)
  16. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1275, in reconstruct_from_config
  17. process_layer(layer_data)
  18. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 1257, in process_layer
  19. layer = deserialize_layer(layer_data, custom_objects=custom_objects)
  20. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\layers\serialization.py", line 177, in deserialize
  21. printable_module_name='layer')
  22. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 347, in deserialize_keras_object
  23. config, module_objects, custom_objects, printable_module_name)
  24. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 296, in class_and_config_for_serialized_keras_object
  25. raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
  26. ValueError: Unknown layer: QuantizeLayer
  27. Process finished with exit code 1

 

tflite模型读取失败

  1. interpreter = tf.lite.Interpreter(model_path="quantize_models_resnet_tflite.tflite")
  2. interpreter.allocate_tensors()
  3. input_tensor_index = interpreter.get_input_details()[0]["index"]
  4. output = interpreter.tensor(interpreter.get_output_details()[0]["index"])

报错:RuntimeError: tensorflow/lite/kernels/quantize.cc:113 affine_quantization->scale->size == 1 was not true.Node number 0 (QUANTIZE) failed to prepare.

  1. Traceback (most recent call last):
  2. File "H:/Leon/CODE/python_projects/master_ImRecognition/dataset/NEU/ThesisTest_Leon/test_tflite.py", line 48, in <module>
  3. interpreter.allocate_tensors()
  4. File "D:\Users\Leon_PC\Anaconda3\envs\tf2_4_1\lib\site-packages\tensorflow\lite\python\interpreter.py", line 259, in allocate_tensors
  5. return self._interpreter.AllocateTensors()
  6. RuntimeError: tensorflow/lite/kernels/quantize.cc:113 affine_quantization->scale->size == 1 was not true.Node number 0 (QUANTIZE) failed to prepare.

后面继续跟进这个bug!应该不是不能解决的!

 

 

但是为了测试,我把训练代码和测试代码写到一起然后测试了速度和准确率:

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Jan 29 20:36:16 2021
  4. @author: Leon_PC
  5. """
  6. import tensorflow as tf
  7. import random
  8. import pathlib
  9. from tensorflow import keras
  10. import os
  11. import numpy as np
  12. class_name=['Cr','In','Pa','PS','RS','Sc']
  13. file_dir='../database/'
  14. postfix='resnet'
  15. model_save_dir='quantize_models_'+postfix+'.hdf5'
  16. log_dir='./logs/'
  17. classes=len(class_name)
  18. train_ratio = 2 / 3
  19. val_ratio= 2 / 10
  20. nums_of_each_classes = 300
  21. nums_for_training=int(len(class_name)*nums_of_each_classes*train_ratio)
  22. nums_for_validation=int(float(nums_for_training)*val_ratio)
  23. im_w=256
  24. im_h=256
  25. im_channels=3
  26. batch_size=32
  27. epochs=50
  28. from get_Normal_data import get_tfdataset
  29. train_ds,test_ds,val_ds=get_tfdataset(file_dir=file_dir, \
  30. class_name=class_name,\
  31. nums_of_each_classes=nums_of_each_classes,\
  32. nums_for_training=nums_for_training,\
  33. nums_for_validation=nums_for_validation,\
  34. im_w=im_w, im_h=im_h, im_channels=im_channels,batch_size=batch_size,is_aug=True)
  35. from RSM_CascadeNet import single_net,cascade_net
  36. model = single_net(classes=classes,im_w=im_w,im_h=im_h,im_channels=im_channels,postfix=postfix)
  37. import tensorflow_model_optimization as tfmot
  38. quantize_model = tfmot.quantization.keras.quantize_model
  39. # q_aware stands for for quantization aware.
  40. q_aware_model = quantize_model(model)
  41. optimizer=tf.keras.optimizers.RMSprop()
  42. q_aware_model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
  43. q_aware_model.summary()
  44. import gc
  45. #callbacks
  46. tensorboard = tf.keras.callbacks.TensorBoard(log_dir=log_dir)
  47. import callbacks_history
  48. history = callbacks_history.LossHistory()
  49. cp_callback = tf.keras.callbacks.ModelCheckpoint(model_save_dir,verbose=1,monitor='val_accuracy', save_best_only=True, mode='max')
  50. q_aware_model.fit(train_ds,batch_size=batch_size,epochs=epochs,validation_data=val_ds,callbacks=[history,tensorboard,cp_callback])
  51. converter = tf.lite.TFLiteConverter.from_keras_model(q_aware_model)
  52. converter.optimizations = ["DEFAULT"]
  53. # converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
  54. tflite_model = converter.convert()
  55. open("quantize_models_resnet_tflite.tflite", "wb").write(tflite_model)
  56. # from tensorflow.keras.models import load_model
  57. # test_model = load_model(model_save_dir)
  58. cost = q_aware_model.evaluate(test_ds)
  59. print('test loss: ', cost)
  60. from get_RSM_data import get_data_list,test_preprocess_image,preprocess_label
  61. _,_,test_image_list,test_label_list,_,_=get_data_list(file_dir,class_name,nums_of_each_classes,nums_for_training,nums_for_validation)
  62. test_image = []
  63. test_label = []
  64. for image, label in zip(test_image_list, test_label_list):
  65. r_image = test_preprocess_image(image,im_w=im_w, im_h=im_h, im_channels=im_channels)
  66. r_label = preprocess_label(label,classes=classes,one_hot=False)
  67. test_image.append(r_image)
  68. test_label.append(r_label)
  69. test_images = np.array(test_image)
  70. test_labels = np.array(test_label)
  71. print(test_images.shape)
  72. print(test_labels.shape)
  73. import time
  74. start_time = time.time()
  75. prediction_digits = []
  76. for test_image in test_images:
  77. test_image = np.expand_dims(test_image, axis=0).astype(np.float32)
  78. res=q_aware_model.predict(test_image, batch_size=1)
  79. digit = np.argmax(res[0])
  80. prediction_digits.append(digit)
  81. #
  82. end_time = time.time()
  83. print('test time (ms/frame): ',(end_time - start_time) / len(prediction_digits) * 1000)
  84. #
  85. accurate_count = 0
  86. for index in range(len(prediction_digits)):
  87. # print(prediction_digits[index],test_labels[index])
  88. if prediction_digits[index] == test_labels[index]:
  89. accurate_count += 1
  90. accuracy = float(accurate_count) * 1.0 / float(len(prediction_digits))
  91. print('test accuracy: %.6f'%accuracy)
  92. history.loss_plot('epoch')
  93. gc.collect()
  94. tf.keras.backend.clear_session()
  95. tf.compat.v1.reset_default_graph()

基于Quantization aware training的量化keras模型的测试结果:

test time (ms/frame):  25.569610993067425
test accuracy: 0.971667

速度也没有提升,准确率也没有提升,模型大小还变大了。。。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/154250?site
推荐阅读
相关标签
  

闽ICP备14008679号