OpenV概述

发布时间:2023-05-18

OpenV是一个集成视觉和深度学习计算库的开源库,它基于OpenCV和OpenVX框架,并优化了在多种硬件上的性能表现,为计算机视觉应用提供了高效易用的解决方案。本文从多个方面介绍OpenV的特点和应用场景。

一、OpenVINO

OpenV根据应用场景提供了多个不同的接口,其中OpenVINO是它在视觉领域中最流行的接口之一。OpenVINO可以通过基于神经网络的推理加速计算机视觉应用的运行速度。若使用CPU,OpenVINO可以利用多线程或OpenMP进行优化,而使用GPU可以利用OpenCL和CUDA等计算框架进行优化。OpenVINO还支持基于自定义硬件的推理,如神经网络处理单元(NPU)。

二、OpenVINO扑克牌检测示例

下面是一个OpenVINO扑克牌检测示例,可以检测图片中的扑克牌,算法使用预训练的神经网络模型。

#include "opencv2/dnn.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/ocl.hpp>
#include <chrono>
using namespace cv; using namespace dnn; using namespace std; using namespace std::chrono;
int main(int argc, char** argv) {
    String modelTxt = "/path/to/model/deploy.prototxt";
    String modelBin = "/path/to/model/snapshot_iter_8910.caffemodel";
    // Initialize network
    Net net = readNetFromCaffe(modelTxt, modelBin);
    // Load image
    Mat frame = imread("/path/to/image.jpg");
    // Create 4D blob from input image
    Mat blob;
    blobFromImage(frame, blob, 1.0, Size(227, 227), Scalar(104, 117, 123), false, false);
    // Set input blob
    net.setInput(blob);
    // Forward pass through network
    Mat output = net.forward();
    // Determine label with highest confidence
    Point maxPt;
    minMaxLoc(output.reshape(1, 1), 0, 0, 0, &maxPt);
    // Read label names
    vector<string> classNames;
    ifstream classNamesFile("/path/to/class/names/file.txt");
    string className = "";
    while (getline(classNamesFile, className)) {
        classNames.push_back(className);
    }
    // Display results
    string label = classNames[maxPt.x];
    putText(frame, label, Point(10, 30), FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 0, 255), 2);
    imshow("Output", frame);
    waitKey(0);
    return 0;
}

三、OpenVPen

OpenVPen是OpenV在深度学习领域的另一个接口。它提供了基于GPU加速的代码生成和优化工具,能够优化和升级现有的深度学习模型,并将它们转化为高效的计算图,更好地运行于没有GPU的平台上。OpenVPen内置了一系列的优化器,可以自动处理数据流中的优化问题,例如图像归一化等操作,从而加速深度学习应用的推理和训练。

四、OpenVINO车辆检测示例

下面是一个OpenVINO车辆检测示例,可以检测视频中的汽车,算法使用预训练的神经网络模型。

#include <inference_engine.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
#include <chrono>
using namespace std;
using namespace cv;
using namespace InferenceEngine;
int main() {
    // Plugin initialization for specified device and load extensions library if specified
    PluginDispatcher dispatcher({ "" });
    auto plugin = dispatcher.getSuitablePlugin(TargetDevice::eCPU);
    // Read IR generated by ModelOptimizer (.xml and .bin files)
    CNNNetReader networkReader;
    networkReader.ReadNetwork("vehicle-detection-adas-0002.xml");
    networkReader.ReadWeights("vehicle-detection-adas-0002.bin");
    // Configure input & output
    auto inputInfo = networkReader.getNetwork().getInputInfo().begin()->second;
    inputInfo->setPrecision(Precision::FP16);
    inputInfo->setLayout(Layout::NHWC);
    auto outputInfo = networkReader.getNetwork().getOutputsInfo().begin()->second;
    outputInfo->setPrecision(Precision::FP16);
    outputInfo->setLayout(Layout::NCHW);
    // Load network to the plugin
    ExecutableNetwork executableNetwork = plugin.LoadNetwork(networkReader.getNetwork(), {});
    // Create inference request iterators
    auto inputBlob = executableNetwork.CreateBlobProxy(inputInfo);
    auto outputBlob = executableNetwork.GetOutputsInfo()[0]->GetBlob();
    // Read input frame
    Mat inFrame = cv::imread("/path/to/input/frame.jpg");
    // Resize to network's input size and assign to input blob
    Mat outFrame;
    Size size(inputInfo->getTensorDesc().getDims()[3], inputInfo->getTensorDesc().getDims()[2]);
    cv::resize(inFrame, outFrame, size);
    std::memcpy(inputBlob->buffer(), outFrame.ptr(), outFrame.total() * outFrame.elemSize());
    // Start inference
    auto t0 = chrono::high_resolution_clock::now();
    auto inferRequest = executableNetwork.CreateInferRequest();
    inferRequest.SetBlob(inputInfo->name(), inputBlob);
    inferRequest.Infer();
    auto t1 = chrono::high_resolution_clock::now();
    // Retrieve information from the output blob
    MemoryBlob::Ptr moutput = as<MemoryBlob>(outputBlob);
    const auto loutputs = outputBlob->getTensorDesc().getDims();
    const size_t lwidth = loutputs[3];
    const size_t lheight = loutputs[2];
    Mat detectionMat(lheight, lwidth, CV_32F, moutput->rmap().fptr());
    // Draw bounding boxes around detected cars
    float confidenceThreshold = 0.7f;
    for (int i = 0; i < detectionMat.rows; i++) {
        float confidence = detectionMat.at<float>(i, 2);
        if (confidence > confidenceThreshold) {
            int xmin = static_cast<int>(detectionMat.at<float>(i, 3) * inFrame.cols);
            int ymin = static_cast<int>(detectionMat.at<float>(i, 4) * inFrame.rows);
            int xmax = static_cast<int>(detectionMat.at<float>(i, 5) * inFrame.cols);
            int ymax = static_cast<int>(detectionMat.at<float>(i, 6) * inFrame.rows);
            rectangle(inFrame, Point(xmin, ymin), Point(xmax, ymax), Scalar(0, 255, 255), 2);
        }
    }
    // Show result
    namedWindow("Vehicle Detection", WINDOW_AUTOSIZE);
    imshow("Vehicle Detection", inFrame);
    waitKey(0);
    cout << "Inference time: " << chrono::duration<double>(t1 - t0).count() << " seconds" << endl;
    return 0;
}

五、OpenVPen Connect

OpenVPen Connect是一个基于Python的API,提供了为深度学习和计算机视觉应用编写高性能、易扩展的代码的能力。它允许用户可以更方便地打包和部署应用,以及在更大的计算资源上运行。它还支持在多个主机之间进行分布式训练。

六、OpenVINO硬件要求

OpenVINO支持各种平台和设备,尤其是CPU、GPU和FPGA等硬件设备。具体的要求取决于应用场景和硬件设备,通常可以满足大多数现代设备的需求。

七、OpenVINO证书有用吗?

OpenVINO提供了认证证书,它是一个针对OpenVINO的认证系统,考核申请人在OpenVINO框架的使用和应用上的技能和知识。OpenVINO证书可以证明一个人在OpenVINO技术上具有专业知识和技能,成为用户找工作、转行或证明自己的首选证明之一。

八、OpenVINO 多输出推理

OpenVINO支持多输出推理,这是在神经网络中使用的一种技术。神经网络通常包含多个输出,每个输出都用于区分不同的属性或特征。OpenVINO允许用户获取和处理多个输出,从而可以在单次推理中获得多个结果。

九、OpenVINO转的模型预测不对

如果OpenVINO转换模型所得到的预测结果不准确,可能是由于OpenVINO不支持模型中的某些特性导致的。这时需要对模型进行更深入的分析和调整,确保所用模型和参数与OpenVINO的要求相符合,以提高预测的准确度。