当前位置:首页 » 《关于电脑》 » 正文

【C++】和【预训练模型】实现【机器学习】【图像分类】的终极指南

13 人参与  2024年10月22日 16:01  分类 : 《关于电脑》  评论

点击全文阅读


目录

?1. 准备工作和环境配置?

?安装OpenCV?

?安装Dlib?

下载并编译TensorFlow C++ API?

?2. 下载和配置预训练模型?

?2.1 下载预训练的ResNet-50模型?

?2.2 配置TensorFlow C++ API?

?2.3 加载和使用模型?

?3.编写代码进行图像分类?

?CMakeLists.txt?

?main.cpp?

?4. 代码分析和推导?

?初始化TensorFlow会话?

?读取和导入模型?

?读取输入图像?

?创建输入Tensor?

?运行会话并处理输出?

?5. 进阶优化与性能提升?

?多线程处理?

?GPU加速?

?模型优化?

?6. 问题与解决方案?

?问题1:内存不足?

?问题2:推理速度慢?

?问题3:模型兼容性问题?

 


 

在现代机器学习和人工智能应用中,图像分类是一个非常常见且重要的任务。通过使用预训练模型,我们可以显著减少训练时间并提高准确性。C++作为一种高效的编程语言,特别适用于需要高性能计算的任务。226b4e959a0c4b4ca7e72460c6008eb7.png

?1. 准备工作和环境配置?

首先,我们需要配置开发环境。这里我们将使用以下工具和库:

C++ 编译器 (如GCC)CMake 构建系统OpenCV 库Dlib 库下载并编译C++版本的TensorFlow

?安装OpenCV?

在Linux系统上,可以通过以下命令安装OpenCV:

sudo apt-get updatesudo apt-get install libopencv-dev

?安装Dlib?

Dlib是一个现代C++工具包,包含了机器学习算法和工具。可以通过以下命令安装:

git clone https://github.com/davisking/dlib.gitcd dlibmkdir buildcd buildcmake ..cmake --build .sudo make install

下载并编译TensorFlow C++ API?

下载TensorFlow的C++库并编译,可以参考TensorFlow官方文档进行详细的步骤。确保下载的版本与您当前的环境兼容。

?2. 下载和配置预训练模型?

使用ResNet-50模型,这是一个用于图像分类的深度卷积神经网络。在TensorFlow中,可以轻松地获取预训练的ResNet-50模型。以下是下载和配置ResNet-50模型的详细步骤:

?2.1 下载预训练的ResNet-50模型?

首先,我们需要下载预训练的ResNet-50模型。TensorFlow提供了很多预训练模型,您可以从TensorFlow的模型库中获取ResNet-50。

1.访问TensorFlow模型库: 打开浏览器,访问TensorFlow模型库的GitHub页面:TensorFlow Model Garden

2.选择预训练模型: 在模型库中找到ResNet-50模型。通常在tensorflow/models/official/vision/image_classification目录下可以找到相关的预训练模型。

3.下载模型文件: 下载模型文件,模型文件通常是一个.pb文件(TensorFlow模型的protobuf格式)。如果直接下载预训练模型文件不方便,可以使用TensorFlow的tf.keras.applications模块直接加载ResNet-50,并保存为.pb文件。

使用Python脚本下载并保存ResNet-50模型:

import tensorflow as tfmodel = tf.keras.applications.ResNet50(weights='imagenet')model.save('resnet50_saved_model', save_format='tf')

运行此脚本将会在当前目录生成一个名为resnet50_saved_model的文件夹,其中包含了模型的.pb文件。

?2.2 配置TensorFlow C++ API?

在下载模型文件后,我们需要配置TensorFlow的C++ API来加载和使用该模型。以下是配置步骤:

1.安装TensorFlow C++库: 从TensorFlow的官方网站下载适用于您的平台的TensorFlow C++库。如果没有现成的二进制包,可以从源代码编译TensorFlow C++库。

git clone https://github.com/tensorflow/tensorflow.gitcd tensorflow./configurebazel build //tensorflow:libtensorflow_cc.so

编译完成后,库文件位于bazel-bin/tensorflow目录下。

2.设置环境变量: 将TensorFlow C++库的包含路径和库文件路径添加到环境变量中。

export TF_CPP_INCLUDE_DIR=/path/to/tensorflow/includeexport TF_CPP_LIB_DIR=/path/to/tensorflow/lib

3.配置CMakeLists.txt: 更新项目的CMakeLists.txt文件,包含TensorFlow C++库的路径。

cmake_minimum_required(VERSION 3.10)project(ImageClassification)set(CMAKE_CXX_STANDARD 14)find_package(OpenCV REQUIRED)find_package(Dlib REQUIRED)include_directories(${OpenCV_INCLUDE_DIRS})include_directories(${Dlib_INCLUDE_DIRS})include_directories(${TF_CPP_INCLUDE_DIR})link_directories(${TF_CPP_LIB_DIR})add_executable(ImageClassification main.cpp)target_link_libraries(ImageClassification ${OpenCV_LIBS} dlib::dlib tensorflow_cc)

?2.3 加载和使用模型?

在完成上述配置后,可以在C++代码中加载和使用ResNet-50模型。下面是示例代码,演示如何加载和使用该模型进行图像分类:

#include <iostream>#include <opencv2/opencv.hpp>#include <dlib/dnn.h>#include <tensorflow/core/public/session.h>#include <tensorflow/core/protobuf/meta_graph.pb.h>using namespace std;using namespace cv;using namespace tensorflow;// 定义图像分类函数void classifyImage(const std::string& model_path, const std::string& image_path) {    // 初始化TensorFlow会话    Session* session;    Status status = NewSession(SessionOptions(), &session);    if (!status.ok()) {        std::cerr << "Error creating TensorFlow session: " << status.ToString() << std::endl;        return;    }    // 读取模型    GraphDef graph_def;    status = ReadBinaryProto(Env::Default(), model_path, &graph_def);    if (!status.ok()) {        std::cerr << "Error reading graph definition from " << model_path << ": " << status.ToString() << std::endl;        return;    }    // 将模型导入会话    status = session->Create(graph_def);    if (!status.ok()) {        std::cerr << "Error creating graph: " << status.ToString() << std::endl;        return;    }    // 读取输入图像    Mat img = imread(image_path);    if (img.empty()) {        std::cerr << "Error reading image: " << image_path << std::endl;        return;    }    // 预处理图像    Mat img_resized;    resize(img, img_resized, Size(224, 224));    img_resized.convertTo(img_resized, CV_32FC3);    img_resized = img_resized / 255.0;    // 创建输入Tensor    Tensor input_tensor(DT_FLOAT, TensorShape({1, 224, 224, 3}));    auto input_tensor_mapped = input_tensor.tensor<float, 4>();    // 将图像数据复制到输入Tensor    for (int y = 0; y < 224; ++y) {        for (int x = 0; x < 224; ++x) {            for (int c = 0; c < 3; ++c) {                input_tensor_mapped(0, y, x, c) = img_resized.at<Vec3f>(y, x)[c];            }        }    }    // 运行会话    std::vector<Tensor> outputs;    status = session->Run({{"input_tensor", input_tensor}}, {"output_tensor"}, {}, &outputs);    if (!status.ok()) {        std::cerr << "Error during inference: " << status.ToString() << std::endl;        return;    }    // 处理输出    auto output_tensor = outputs[0].tensor<float, 2>();    int best_label = std::distance(output_tensor(0).data(), std::max_element(output_tensor(0).data(), output_tensor(0).data() + output_tensor.dim_size(1)));    std::cout << "Predicted label: " << best_label << std::endl;    // 清理    session->Close();    delete session;}int main(int argc, char** argv) {    if (argc != 3) {        std::cerr << "Usage: " << argv[0] << " <model_path> <image_path>" << std::endl;        return 1;    }    const std::string model_path = argv[1];    const std::string image_path = argv[2];    classifyImage(model_path, image_path);    return 0;}

?3.编写代码进行图像分类?

使用预训练的ResNet-50模型进行图像分类。

?CMakeLists.txt?

cmake_minimum_required(VERSION 3.10)project(ImageClassification)set(CMAKE_CXX_STANDARD 14)find_package(OpenCV REQUIRED)find_package(Dlib REQUIRED)include_directories(${OpenCV_INCLUDE_DIRS})include_directories(${Dlib_INCLUDE_DIRS})include_directories(/path/to/tensorflow/include)link_directories(/path/to/tensorflow/lib)add_executable(ImageClassification main.cpp)target_link_libraries(ImageClassification ${OpenCV_LIBS} dlib::dlib tensorflow)

?main.cpp?

#include <iostream>#include <opencv2/opencv.hpp>#include <dlib/dnn.h>#include <tensorflow/core/public/session.h>#include <tensorflow/core/protobuf/meta_graph.pb.h>using namespace std;using namespace cv;using namespace tensorflow;// 定义图像分类函数void classifyImage(const std::string& model_path, const std::string& image_path) {    // 初始化TensorFlow会话    Session* session;    Status status = NewSession(SessionOptions(), &session);    if (!status.ok()) {        std::cerr << "Error creating TensorFlow session: " << status.ToString() << std::endl;        return;    }    // 读取模型    GraphDef graph_def;    status = ReadBinaryProto(Env::Default(), model_path, &graph_def);    if (!status.ok()) {        std::cerr << "Error reading graph definition from " << model_path << ": " << status.ToString() << std::endl;        return;    }    // 将模型导入会话    status = session->Create(graph_def);    if (!status.ok()) {        std::cerr << "Error creating graph: " << status.ToString() << std::endl;        return;    }    // 读取输入图像    Mat img = imread(image_path);    if (img.empty()) {        std::cerr << "Error reading image: " << image_path << std::endl;        return;    }    // 预处理图像    Mat img_resized;    resize(img, img_resized, Size(224, 224));    img_resized.convertTo(img_resized, CV_32FC3);    img_resized = img_resized / 255.0;    // 创建输入Tensor    Tensor input_tensor(DT_FLOAT, TensorShape({1, 224, 224, 3}));    auto input_tensor_mapped = input_tensor.tensor<float, 4>();    // 将图像数据复制到输入Tensor    for (int y = 0; y < 224; ++y) {        for (int x = 0; x < 224; ++x) {            for (int c = 0; c < 3; ++c) {                input_tensor_mapped(0, y, x, c) = img_resized.at<Vec3f>(y, x)[c];            }        }    }    // 运行会话    std::vector<Tensor> outputs;    status = session->Run({{"input_tensor", input_tensor}}, {"output_tensor"}, {}, &outputs);    if (!status.ok()) {        std::cerr << "Error during inference: " << status.ToString() << std::endl;        return;    }    // 处理输出    auto output_tensor = outputs[0].tensor<float, 2>();    int best_label = std::distance(output_tensor(0).data(), std::max_element(output_tensor(0).data(), output_tensor(0).data() + output_tensor.dim_size(1)));    std::cout << "Predicted label: " << best_label << std::endl;    // 清理    session->Close();    delete session;}int main(int argc, char** argv) {    if (argc != 3) {        std::cerr << "Usage: " << argv[0] << " <model_path> <image_path>" << std::endl;        return 1;    }    const std::string model_path = argv[1];    const std::string image_path = argv[2];    classifyImage(model_path, image_path);    return 0;}

?4. 代码分析和推导?

?初始化TensorFlow会话?

首先,我们初始化一个TensorFlow会话。这个会话将用于执行图中的操作。

Session* session;Status status = NewSession(SessionOptions(), &session);if (!status.ok()) {    std::cerr << "Error creating TensorFlow session: " << status.ToString() << std::endl;    return;}

?读取和导入模型?

使用ReadBinaryProto函数读取二进制格式的模型文件,并将其导入会话。

GraphDef graph_def;status = ReadBinaryProto(Env::Default(), model_path, &graph_def);if (!status.ok()) {    std::cerr << "Error reading graph definition from " << model_path << ": " << status.ToString() << std::endl;    return;}status = session->Create(graph_def);if (!status.ok()) {    std::cerr << "Error creating graph: " << status.ToString() << std::endl;    return;}

?读取输入图像?

我们使用OpenCV读取图像,并将其大小调整为224x224,这是ResNet-50模型所需的输入尺寸。

Mat img = imread(image_path);if (img.empty()) {    std::cerr << "Error reading image: " << image_path << std::endl;    return;}Mat img_resized;resize(img, img_resized, Size(224, 224));img_resized.convertTo(img_resized, CV_32FC3);img_resized = img_resized / 255.0;

?创建输入Tensor?

接下来,创建一个TensorFlow的Tensor,并将图像数据复制到该Tensor中。

Tensor input_tensor(DT_FLOAT, TensorShape({1, 224, 224, 3}));auto input_tensor_mapped = input_tensor.tensor<float, 4>();for (int y = 0; y < 224; ++y) {    for (int x = 0; x < 224; ++x) {        for (int c = 0; c < 3; ++c) {            input_tensor_mapped(0, y, x, c) = img_resized.at<Vec3f>(y, x)[c];        }    }}

?运行会话并处理输出?

使用会话运行模型,并获取输出结果。

std::vector<Tensor> outputs;status = session->Run({{"input_tensor", input_tensor}}, {"output_tensor"}, {}, &outputs);if (!status.ok()) {    std::cerr << "Error during inference: " << status.ToString() << std::endl;    return;}auto output_tensor = outputs[0].tensor<float, 2>();int best_label = std::distance(output_tensor(0).data(), std::max_element(output_tensor(0).data(), output_tensor(0).data() + output_tensor.dim_size(1)));std::cout << "Predicted label: " << best_label << std::endl;

?5. 进阶优化与性能提升?

在这部分中,我们将探讨如何进一步优化代码以提高性能和效率。这些技巧和方法包括多线程处理、GPU加速、模型优化等。

?多线程处理?

在处理大量图像时,利用多线程可以显著提高处理速度。C++中的std::thread库使得多线程编程更加方便。多线程处理:

#include <thread>#include <vector>// 定义一个处理图像的函数void processImage(const std::string& model_path, const std::string& image_path) {    classifyImage(model_path, image_path);}int main(int argc, char** argv) {    if (argc < 3) {        std::cerr << "Usage: " << argv[0] << " <model_path> <image_paths...>" << std::endl;        return 1;    }    const std::string model_path = argv[1];    std::vector<std::string> image_paths;    for (int i = 2; i < argc; ++i) {        image_paths.push_back(argv[i]);    }    std::vector<std::thread> threads;    for (const auto& image_path : image_paths) {        threads.emplace_back(processImage, model_path, image_path);    }    for (auto& t : threads) {        if (t.joinable()) {            t.join();        }    }    return 0;}

通过这种方式,我们可以同时处理多个图像,从而提高整体处理效率。

?GPU加速?

GPU在处理大规模并行计算任务时具有显著优势。TensorFlow的C++ API支持GPU加速,只需在创建会话时指定GPU设备即可:

SessionOptions options;options.config.mutable_gpu_options()->set_allow_growth(true);Session* session;Status status = NewSession(options, &session);if (!status.ok()) {    std::cerr << "Error creating TensorFlow session: " << status.ToString() << std::endl;    return;}

在配置好CUDA和cuDNN后,TensorFlow会自动利用GPU进行计算,从而显著提高计算速度。

?模型优化?

模型优化是提升推理速度和减少内存占用的重要手段。常用的方法包括模型量化和裁剪。可以使用TensorFlow的模型优化工具进行这些优化。

使用TensorFlow的模型优化API进行量化:

import tensorflow as tffrom tensorflow_model_optimization.quantization.keras import vitis_quantizemodel = tf.keras.models.load_model('model.h5')quantized_model = vitis_quantize.quantize_model(model)quantized_model.save('quantized_model.h5')

将量化后的模型加载到C++项目中,可以显著减少模型的计算量,从而提高推理速度。

?6. 问题与解决方案?

在实际应用中,可能会遇到各种问题。以下是一些常见问题及其解决方案,具体分析每种问题的可能原因和详细的解决步骤。

?问题1:内存不足?

解决方案:

1.减少批处理大小: 批处理大小(batch size)是指一次性送入模型进行处理的数据样本数。如果批处理大小过大,可能会导致内存溢出。可以通过减小批处理大小来减少内存使用。例如,将批处理大小从32减小到16甚至更小。

// 将批处理大小设置为1Tensor input_tensor(DT_FLOAT, TensorShape({1, 224, 224, 3}));

2.使用模型量化技术: 模型量化通过将浮点数转换为低精度整数来减少模型大小和内存占用。TensorFlow提供了量化工具,可以在训练后对模型进行量化。

import tensorflow as tffrom tensorflow_model_optimization.quantization.keras import vitis_quantizemodel = tf.keras.models.load_model('model.h5')quantized_model = vitis_quantize.quantize_model(model)quantized_model.save('quantized_model.h5')

3.更高效的数据预处理方法: 使用OpenCV或其他图像处理库进行高效的数据预处理,尽量减少在内存中的图像副本。在读取图像后立即进行缩放和归一化处理。

Mat img = imread(image_path);resize(img, img_resized, Size(224, 224));img_resized.convertTo(img_resized, CV_32FC3);img_resized = img_resized / 255.0;

?问题2:推理速度慢?

解决方案:

1.使用GPU加速: GPU在处理大规模并行计算任务时具有显著优势。在TensorFlow中可以通过指定GPU设备来加速推理。

SessionOptions options;options.config.mutable_gpu_options()->set_allow_growth(true);Session* session;Status status = NewSession(options, &session);if (!status.ok()) {    std::cerr << "Error creating TensorFlow session: " << status.ToString() << std::endl;    return;}

2.利用多线程并行处理: 使用C++的多线程库(如std::thread)来并行处理多个图像,充分利用多核CPU的计算能力。

#include <thread>#include <vector>void processImage(const std::string& model_path, const std::string& image_path) {    classifyImage(model_path, image_path);}int main(int argc, char** argv) {    if (argc < 3) {        std::cerr << "Usage: " << argv[0] << " <model_path> <image_paths...>" << std::endl;        return 1;    }    const std::string model_path = argv[1];    std::vector<std::string> image_paths;    for (int i = 2; i < argc; ++i) {        image_paths.push_back(argv[i]);    }    std::vector<std::thread> threads;    for (const auto& image_path : image_paths) {        threads.emplace_back(processImage, model_path, image_path);    }    for (auto& t : threads) {        if (t.joinable()) {            t.join();        }    }    return 0;}

3.优化模型结构: 优化模型结构,例如减少模型层数、使用更小的卷积核等,可以提高推理速度。具体方法包括剪枝、合并卷积层等。

4.使用模型量化和裁剪技术: 量化可以显著减少模型大小和计算量,从而提高推理速度。模型裁剪(pruning)通过去除不重要的权重来优化模型。

import tensorflow_model_optimization as tfmotprune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitudepruning_params = {    'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.30,                                                             final_sparsity=0.70,                                                             begin_step=2000,                                                             end_step=10000)}model_for_pruning = prune_low_magnitude(model, **pruning_params)model_for_pruning.compile(optimizer='adam',                          loss=tf.keras.losses.categorical_crossentropy,                          metrics=['accuracy'])model_for_pruning.fit(train_data, train_labels, epochs=2, validation_split=0.1)

?问题3:模型兼容性问题?

解决方案:

确保模型文件和库版本匹配: 在不同平台上使用模型时,确保模型文件与库版本匹配非常重要。例如,TensorFlow模型的版本和TensorFlow库的版本必须一致。

重新训练和导出模型: 如果遇到兼容性问题,尝试在目标平台上重新训练并导出模型。这样可以确保模型和运行环境的完全兼容。

import tensorflow as tf# 重新训练模型model = tf.keras.models.Sequential([    tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)),    tf.keras.layers.MaxPooling2D((2, 2)),    tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),    tf.keras.layers.MaxPooling2D((2, 2)),    tf.keras.layers.Flatten(),    tf.keras.layers.Dense(64, activation='relu'),    tf.keras.layers.Dense(10, activation='softmax')])model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])model.fit(train_images, train_labels, epochs=10)# 导出模型model.save('retrained_model.h5')

3.使用中间格式进行转换: 使用ONNX(开放神经网络交换)格式,可以在不同的深度学习框架之间转换模型。可以使用tf2onnx将TensorFlow模型转换为ONNX格式,然后在目标平台上加载ONNX模型。

import tf2onnximport tensorflow as tfmodel = tf.keras.models.load_model('model.h5')spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name="input"),)output_path = "model.onnx"model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=13)with open(output_path, "wb") as f:    f.write(model_proto.SerializeToString())

然后在C++中使用ONNX Runtime加载和推理ONNX模型:

#include <onnxruntime/core/providers/cpu/cpu_provider_factory.h>#include <onnxruntime/core/providers/tensorrt/tensorrt_provider_factory.h>#include <onnxruntime/core/session/onnxruntime_cxx_api.h>int main() {    Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "ONNXModel");    Ort::SessionOptions session_options;    session_options.AppendExecutionProvider_TensorRT();    session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);    Ort::Session session(env, "model.onnx", session_options);    // 输入、输出和推理代码略...        return 0;}

 

2abe820ccf5e4af399c6049449f1dd1e.png

 


点击全文阅读


本文链接:http://zhangshiyu.com/post/175577.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1