DNN系列5_FCN模型实现图像分割

本例程用到的模型文件、源码和图片素材

贾志刚OpenCV3.3深度神经网络DNN模块系列学习资料整理

5 FCN模型图像分割

5.1 FCN模型模型与数据介绍

FCN模型
支持20个分割标签


 使用模型实现图像分割

5.2 模型文件

 二进制模型
- fcn8s-heavy-pascal.caffemodel   官网下载
 网络描述
- fcn8s-heavy-pascal.prototxt
 分割信息
- pascal-classes.txt
- 20个分类

5.3 使用模型实现图像分割

 编码处理
- 加载Caffem模型
- 使用模型预测

实例5:FCN模型实现图像分割

#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>

using namespace cv;
using namespace cv::dnn;
using namespace std;

const size_t width = 300;
const size_t height = 300;
String labelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/pascal-classes.txt";
String modelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.caffemodel";
String model_text_file = "D:/opencv3.3/opencv/sources/samples/data/dnn/fcn8s-heavy-pascal.prototxt";

vector<Vec3b> readColors();
int main(int argc, char** argv) {
	Mat frame = imread("rgb.jpg");
	if (frame.empty()) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", frame);
	resize(frame, frame, Size(500, 500));//改变尺寸
	vector<Vec3b> colors = readColors();
//
	// init net  初始化网络
	Net net = readNetFromCaffe(model_text_file, modelFile);
	Mat blobImage = blobFromImage(frame);

	// use net   使用网络
	float time = getTickCount();
	net.setInput(blobImage, "data");
	Mat score = net.forward("score");
	float tt = getTickCount() - time;
	printf("time consume: %.2f ms \n", (tt / getTickFrequency()) * 1000);
	
	// segmentation and display   分割并显示
	const int rows = score.size[2];
	const int cols = score.size[3];
	const int chns = score.size[1];
	Mat maxCl(rows, cols, CV_8UC1);
	Mat maxVal(rows, cols, CV_32FC1);

	// setup LUT  LUT查找
	for (int c = 0; c < chns; c++) {
		for (int row = 0; row < rows; row++) {
			const float *ptrScore = score.ptr<float>(0, c, row);
			uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
			float *ptrMaxVal = maxVal.ptr<float>(row);
			for (int col = 0; col < cols; col++) {
				if(ptrScore[col] > ptrMaxVal[col]) {
					ptrMaxVal[col] = ptrScore[col];
					ptrMaxCl[col] = (uchar)c;
				}
			}
		}
	}

	// look up colors 找到对应颜色
	Mat result = Mat::zeros(rows, cols, CV_8UC3);
	for (int row = 0; row < rows; row++) {
		const uchar *ptrMaxCl = maxCl.ptr<uchar>(row);
		Vec3b *ptrColor = result.ptr<Vec3b>(row);
		for (int col = 0; col < cols; col++) {
			ptrColor[col] = colors[ptrMaxCl[col]];
		}
	}
	Mat dst;
	imshow("FCN-demo1", result);
	addWeighted(frame, 0.3, result, 0.7, 0, dst);//增加宽度
	imshow("FCN-demo", dst);

	waitKey(0);
	return 0;
}

vector<Vec3b> readColors() {
	vector<Vec3b> colors;
	ifstream fp(labelFile);
	if (!fp.is_open()) {
		printf("could not open the file...\n");
		exit(-1);
	}
	string line;
	while (!fp.eof()) {
		getline(fp, line);
		if (line.length()) {
			stringstream ss(line);
			string name;
			ss >> name;
			int temp;
			Vec3b color;
			ss >> temp;
			color[0] = (uchar)temp;
			ss >> temp;
			color[1] = (uchar)temp;
			ss >> temp;
			color[2] = (uchar)temp;
			colors.push_back(color);
		}
	}
	return colors;
}

                                   

 

 pascal可实现分割的种类和显示颜色(BGR)可见 pascal-classes.txt文件

                       

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个表达式是计算二分类(通常涉及机器学习中的预测模型,如DNN,即深度神经网络)中模型的准确率(Accuracy)。其中: - TP (True Positive): 真正例,预测为正且实际为正的样本数。 - TN (True Negative): 真负例,预测为负且实际为负的样本数。 - FP (False Positive): 假正例,预测为正但实际为负的样本数。 - FN (False Negative): 假负例,预测为负但实际为正的样本数。 公式 `(TP_DNN + TN_DNN) / (TP_DNN + TN_DNN + FP_DNN + FN_DNN)` 就是将正确预测(TP和TN)的总数除以所有预测总数(包括正确和错误预测),得到分类器在给定数据集上的准确率。 为了增加一个固定的阈值为0.5(通常用于二元分类问题中的概率阈值决策),我们通常会这样操作: 1. 对于模型输出的概率或预测概率进行比较。如果预测概率大于等于0.5,我们可以将其分类为正例(例如1),否则为负例(例如0)。 2. 计算新的TP、TN、FP和FN,基于新的分类标准。 这是一个示例代码片段: ```python # 假设model_output是DNN模型的输出概率列表 threshold = 0.5 new_predictions = [1 if prob >= threshold else 0 for prob in model_output] # 更新计数 TP_new = sum([1 for pred, actual in zip(new_predictions, labels) if pred == 1 and actual == 1]) TN_new = sum([1 for pred, actual in zip(new_predictions, labels) if pred == 0 and actual == 0]) FP_new = sum([1 for pred, actual in zip(new_predictions, labels) if pred == 1 and actual == 0]) FN_new = sum([1 for pred, actual in zip(new_predictions, labels) if pred == 0 and actual == 1]) # 新的精度计算 accuracy_DNN_thresholded = (TP_new + TN_new) / (TP_new + TN_new + FP_new + FN_new) print(f"Accuracy with 0.5 threshold: {accuracy_DNN_thresholded}") ``` 这里`labels`是对应的实际标签列表,`model_output`是模型预测的概率输出。这个新精度值就是基于0.5阈值后的模型性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦我飞也

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值