tesorflow c ++ gpu call maskrcnn

opencv latest version has support gpu call, you need to compile it, temporarily did not take the time to compile actual use has been a tensorflow c ++ gpu version of the call model, faster than the cpu much faster, the code following results (several tests out, there are insufficient please indicate):

#define COMPILER_MSVC
#define NOMINMAX
#define PLATFORM_WINDOWS   // 指定使用tensorflow/core/platform/windows/cpu_info.h


#include<iostream>
#include<opencv2/opencv.hpp>
#include"tensorflow/core/public/session.h"
#include "tensorflow/core/platform/env.h"
#include<iostream>
#include<string>
#include<io.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#pragma warning (disable:4996)

using namespace tensorflow;
using namespace cv;
using namespace std;
using std::cout;
using std::endl;




int main() {
	    const std::string model_path = "D:/frozen_inference_graph.pb";// tensorflow模型文件,注意不能含有中文
	    std::vector<tensorflow::Tensor> outputs;
	    Session* session;
	    Status status = NewSession(SessionOptions(), &session);
	    tensorflow::GraphDef graph_def;
	    status = ReadBinaryProto(Env::Default(), model_path, &graph_def);
	    status = session->Create(graph_def);

		//设置ouput节点
	    std::vector<std::string> output_nodes;
	    output_nodes.push_back("num_detections:0");
	    output_nodes.push_back("detection_boxes:0");
	    output_nodes.push_back("detection_scores:0");
	    output_nodes.push_back("detection_classes:0");
	    output_nodes.push_back("detection_masks:0");


		clock_t start = clock();
		Mat img = imread("E:/1.png");
		cv::cvtColor(img, img, cv::COLOR_BGR2RGB);//做一个红蓝通道值互换,对识别效果有一定的提升
		Mat img2 = imread("E:/1.png");//img已被修改,再读取一张原图作显示
		int height = img.rows;
		int width = img.cols;
		int depth = img.channels();
		

		//将图像转换未input数据
		tensorflow::Tensor input_tensor(DT_UINT8, TensorShape({ 1, height, width, depth }));
		const uint8* source_data = img.data;
		auto input_tensor_mapped = input_tensor.tensor<uint8, 4>();

		for (int i = 0; i < height; i++) {
			const uint8* source_row = source_data + (i * width * depth);
			for (int j = 0; j < width; j++) {
				const uint8* source_pixel = source_row + (j * depth);
				for (int c = 0; c < depth; c++) {
					const uint8* source_value = source_pixel + c;
					input_tensor_mapped(0, i, j, c) = *source_value;
				}
			}
		}
		std::vector<std::pair<std::string, tensorflow::Tensor>> inputs = {
			{ "image_tensor:0", input_tensor },
		};

		//开始识别计算
		status = session->Run(inputs, { output_nodes }, {}, &outputs);

		//获取识别的数据
		auto mapp = outputs[0].tensor<float, 1>();
		auto detec = outputs[1].tensor<float, 3>();
		auto score = outputs[2].tensor<float, 2>();
		auto mask = outputs[4].tensor<float, 4>();
		auto class1 = outputs[3].tensor<float, 2>();
		
		//显示边界框和轮廓
		for (int i = 0; i < outputs[0].tensor<float, 1>()(0); i++) {
			if (score(0, i) > 0.5) {
				Rect  box = Rect(detec(0, i, 1) * (img.cols - 1), detec(0, i, 0) * (img.rows - 1), detec(0, i, 3) * img.cols - detec(0, i, 1) * (img.cols - 1) + 1, detec(0, i, 2) * img.rows - detec(0, i, 0) * (img.rows - 1) + 1);
				Mat objectMask(33, 33, CV_32F);//根据不同模型提取的轮廓设定v2(15,15),resnet50、resnet101(33,33),具体根据配置文件
				for (int r = 0; r < 33; r++) {
					for (int c = 0; c < 33; c++) {
						objectMask.at<float>(r, c) = mask(0, i, r, c);
					}
				}
				resize(objectMask, objectMask, Size(box.width, box.height));
				Mat mask = (objectMask);
				Mat coloredRoi = img2(box);
				coloredRoi.convertTo(coloredRoi, CV_8UC3);
				std::vector<Mat> contours;
				Mat hierarchy;
				mask.convertTo(mask, CV_8U);
				findContours(mask, contours, hierarchy, RETR_CCOMP, 1); // 提取轮廓
				drawContours(coloredRoi, contours, -1, Scalar(0, 205, 0), 2, LINE_8, hierarchy, 100); // 绘制轮廓
				coloredRoi.copyTo(img2(box), mask);
			}
		}
		cout << clock() - start << endl;
		namedWindow("img", 0);
		imshow("img", img2);
		cv::waitKey(0);
	return 0;
}

These header files can be deleted, made a deal with something else, it is the first document a bit more.

Necessary to configure tensorflow c ++ environment, available for free download here: https://download.csdn.net/download/qq_39731633/12208694

Be arranged on vs2015, the specific configuration is as follows:

1, includes directory

2, the library catalog

3, additional dependencies

qq: 2036511045 welcomed the discussion

 

 

 

 

 

Released two original articles · won praise 0 · Views 105

Guess you like

Origin blog.csdn.net/qq_39731633/article/details/104634507
Recommended