Conversion of TensorFlow Classification Models and Launch with OpenCV Python
Примеры с cv2.imshow требуют сборки OpenCV с GUI. В ноутбуке с headless-версией используйте вывод изображений через Matplotlib.
Conversion of TensorFlow Classification Models and Launch with OpenCV Python
Original author
Anastasia Murzova
Compatibility
OpenCV >= 4.5
In this tutorial you will learn how to:
obtain frozen graphs of TensorFlow (TF) classification models
run converted TensorFlow model with OpenCV Python API
obtain an evaluation of the TensorFlow and OpenCV DNN models
We will explore the above-listed points by the example of MobileNet architecture.
Introduction
Let's briefly view the key concepts involved in the pipeline of TensorFlow models transition with OpenCV API. The initial step in conversion of TensorFlow models into cv.dnn.Net is obtaining the frozen TF model graph. Frozen graph defines the combination of the model graph structure with kept values of the required variables, for example, weights. Usually the frozen graph is saved in protobuf (.pb) files. After the model .pb file was generated it can be read with cv.dnn.readNetFromTensorflow function.
Requirements
To be able to experiment with the below code you will need to install a set of libraries. We will use a virtual environment with python3.7+ for this:
For OpenCV-Python building from source, follow the corresponding instructions from the Introduction to OpenCV.
Before you start the installation of the libraries, you can customize the requirements.txt, excluding or including (for example, opencv-python) some dependencies. The below line initiates requirements installation into the previously activated virtual environment:
pipinstall -rrequirements.txt
Practice
In this part we are going to cover the following points:
create a TF classification model conversion pipeline and provide the inference
evaluate and test TF classification models
If you'd like merely to run evaluation or test model pipelines, the "Model Conversion Pipeline" tutorial part can be skipped.
Model Conversion Pipeline
The code in this subchapter is located in the dnn_model_runner module and can be executed with the line:
Please, pay attention at the preprocessing order in the cv2.dnn.blobFromImage function. Firstly, the mean value is subtracted and only then pixel values are multiplied by the defined scale. Therefore, to reproduce the image preprocessing pipeline from the TF mobilenet.preprocess_input function, we multiply mean by 127.5.
As a result, 4-dimensional input_blob was obtained:
As it can be seen from the experiments OpenCV and TF inference results are equal.
Evaluation of the Models
The proposed in dnn/samplesdnn_model_runner module allows to run the full evaluation pipeline on the ImageNet dataset and test execution for the following TensorFlow classification models:
vgg16
vgg19
resnet50
resnet101
resnet152
densenet121
densenet169
densenet201
inceptionresnetv2
inceptionv3
mobilenet
mobilenetv2
nasnetlarge
nasnetmobile
xception
This list can be also extended with further appropriate evaluation pipeline configuration.
Evaluation Mode
To below line represents running of the module in the evaluation mode:
Chosen from the list classification model will be read into OpenCV cv.dnn_Net object. Evaluation results of TF and OpenCV models (accuracy, inference time, L1) will be written into the log file. Inference time values will be also depicted in a chart to generalize the obtained model information.
Necessary evaluation configurations are defined in the test_config.py and can be modified in accordance with actual paths of data location::
@dataclassclassTestClsConfig:
batch_size: int = 50frame_size: int = 224img_root_dir: str = "./ILSVRC2012_img_val"
# locationofimage-classmatchingimg_cls_file: str = "./val.txt"bgr_to_rgb: bool = True
The values from TestClsConfig can be customized in accordance with chosen model.
To initiate the evaluation of the TensorFlow MobileNet, run the following line:
Here default_img_preprocess key defines whether you'd like to parametrize the model test process with some particular values or use the default values, for example, scale, mean or std.
Test configuration is represented in test_config.pyTestClsModuleConfig class:
The basis of the model testing is represented in samples/dnn/classification.py. classification.py can be executed autonomously with provided converted model in --input and populated parameters for cv.dnn.blobFromImage.
To reproduce from scratch the described in "Model Conversion Pipeline" OpenCV steps with dnn_model_runner execute the below line: