用faster rcnn对celebA的bbox重新标注,c++实现筛选,过滤无效bbox

      在对MTCNN进行landmark训练前,发现celebA数据集人脸bbox的标注不准确(landmark标注准确,可以使用),所以需要对人脸bbox进行重新标注,选择用faster rcnn对celebA数据集的bbox重新标注。参考博客:

http://blog.csdn.net/xzzppp/article/details/52071546


      用faster rcnn对celebA数据集的bbox重新标注后,发现有不少误报,误报主要表现为把手,眼睛,耳朵或背景当成了人脸。 所以我对bbox的刷选条件为:“如果celebA数据集的5个landmark都在bbox里面,认为这个bbox是有效的,其他的都无效” 

      celebA数据集中的标注文件,一个图片中只有一个人脸bbox,5个landmark;但faster rcnn标注的bbox中,一个图片中,可能存在多个bbox,用c++实现刷选,过滤和排除无效bbox的代码如下:


#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>  
using namespace cv;
using namespace std;

int main()
{
	ifstream infile_bbox, infile_align;
	ofstream outfile;
	string Imagedir = "E:/face_alignment/data/CelebA/Img/img_celeba.7z/img_celeba/";
	infile_bbox.open("E:/face_alignment/data/CelebA/Anno/celebA_frcnn_annot.txt");  //输入fasterrcnn标注的bbox
	infile_align.open("E:/face_alignment/data/CelebA/Anno/list_landmarks_celeba_modfy.txt");  //输入celebA的landmark
	outfile.open("E:/face_alignment/data/CelebA/Anno/celebA_bbox_landmark.txt");  //输出文件,包含bbox和landmark

	string img_bbox, img_align;
	int x1, y1, x2, y2, w, h, i = 0;
	int pst1_x, pst1_y, pst2_x, pst2_y, pst3_x, pst3_y, pst4_x, pst4_y, pst5_x, pst5_y;

	infile_bbox >> img_bbox >> x1 >> y1 >> x2 >> y2;

	while (infile_align)
	{
		infile_align >> img_align >> pst1_x >> pst1_y >> pst2_x >> pst2_y >> pst3_x >> pst3_y >> pst4_x >> pst4_y >> pst5_x >> pst5_y;

		while (infile_bbox)
		{
			// 判断是否为同一个图片
			if (strcmp(img_bbox.c_str(), img_align.c_str()) == 0)
			{
				//判断五个点是否都在bbox中
				int xx = (x1 < pst1_x && pst1_x < x2) && (x1 < pst2_x && pst2_x < x2) && (x1 < pst3_x && pst3_x < x2) && (x1 < pst4_x && pst4_x < x2) && (x1 < pst5_x && pst5_x < x2);
				int yy = (y1 < pst1_y && pst1_y < y2) && (y1 < pst2_y && pst2_y < y2) && (y1 < pst3_y && pst3_y < y2) && (y1 < pst4_y && pst4_y < y2) && (y1 < pst5_y && pst5_y < y2);

				if (xx == 1 && yy == 1)
				{
					outfile << img_align << " " << x1 << " " << y1 << " " << x2 << " " << y2 << " ";
					outfile << pst1_x << " " << pst1_y << " " << pst2_x << " " << pst2_y << " " << pst3_x << " " << pst3_y << " " << pst4_x << " " << pst4_y << " " << pst5_x << " " << pst5_y << endl;

					//string image_dir = Imagedir + img_align;
					//Mat image = imread(image_dir, -1);
					//w = x2 - x1;
					//h = y2 - y1;
					//Rect bbox(x1, y1, w, h);
					//rectangle(image, bbox, cv::Scalar(255, 0, 0), 2);
					//cv::circle(image, cv::Point(pst1_x, pst1_y), 5, cv::Scalar(255, 255, 0), 2);
					//cv::circle(image, cv::Point(pst2_x, pst2_y), 5, cv::Scalar(255, 255, 0), 2);
					//cv::circle(image, cv::Point(pst3_x, pst3_y), 5, cv::Scalar(255, 255, 0), 2);
					//cv::circle(image, cv::Point(pst4_x, pst4_y), 5, cv::Scalar(255, 255, 0), 2);
					//cv::circle(image, cv::Point(pst5_x, pst5_y), 5, cv::Scalar(255, 255, 0), 2);

					i++;
					cout << i << endl;
					//imshow("bbox", image);
					//waitKey(1000);
				}

			}
			else
				break;

			infile_bbox >> img_bbox >> x1 >> y1 >> x2 >> y2;
		}


	}

	infile_bbox.close();
	infile_align.close();
	outfile.close();

	return 0;
}


验证刷选效果是否可行的可视化程序:


#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>  
using namespace cv;
using namespace std;


int main()
{
	ifstream infile;
	string Imagedir = "E:/face_alignment/data/CelebA/Img/img_celeba.7z/img_celeba/";
	infile.open("E:/face_alignment/data/CelebA/Anno/celebA_bbox_landmark.txt");

	string img;
	int x1, y1, x2, y2, w, h, i = 0;
	int pst1_x, pst1_y, pst2_x, pst2_y, pst3_x, pst3_y, pst4_x, pst4_y, pst5_x, pst5_y;

	while (infile)
	{
		infile >> img >> x1 >> y1 >> x2 >> y2 >> pst1_x >> pst1_y >> pst2_x >> pst2_y >> pst3_x >> pst3_y >> pst4_x >> pst4_y >> pst5_x >> pst5_y;

		string image_dir = Imagedir + img;
		Mat image = imread(image_dir, -1);
		w = x2 - x1;
		h = y2 - y1;
		Rect bbox(x1, y1, w, h);
		rectangle(image, bbox, cv::Scalar(255, 0, 0), 2);
		cv::circle(image, cv::Point(pst1_x, pst1_y), 5, cv::Scalar(255, 255, 0), 2);
		cv::circle(image, cv::Point(pst2_x, pst2_y), 5, cv::Scalar(255, 255, 0), 2);
		cv::circle(image, cv::Point(pst3_x, pst3_y), 5, cv::Scalar(255, 255, 0), 2);
		cv::circle(image, cv::Point(pst4_x, pst4_y), 5, cv::Scalar(255, 255, 0), 2);
		cv::circle(image, cv::Point(pst5_x, pst5_y), 5, cv::Scalar(255, 255, 0), 2);

		i++;
		cout << i << endl;
		imshow("bbox", image);
		waitKey(1000);

	}

	infile.close();


	return 0;
}


标注效果如下图所示:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 可以使用 Python 实现 Faster RCNN 算法。Faster RCNN 是一种目标检测算法,它使用了一种叫做区域建议网络 (Region Proposal Network, RPN) 的方法来提出候选区域,然后使用其他神经网络来分类和定位目标。 使用 Python 实现 Faster RCNN 算法可以使用一些已有的库,例如: - TensorFlow Object Detection API:这是由 Google 开发的一个开源框架,可以用于训练和部署计算机视觉模型,包括 Faster RCNN 算法。 - mmdetection:这是由阿里巴巴搜索算法团队开发的一个开源目标检测库,它支持 Faster RCNN 算法。 如果你想手动实现 Faster RCNN 算法,你可以参考论文 "Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks" 中的方法。你也可以参考其他资源,例如博客文章或教程。 ### 回答2: Faster RCNN是一种在目标检测中表现出色的深度学习模型,它结合了Fast RCNN和RPN(Region Proposal Network)的思想。要使用Python来实现Faster RCNN,我们需要以下几个步骤: 1. 数据准备:首先,准备训练和测试所需的图像数据集,并标注每个图像中目标的位置和对应的类别。 2. 搭建基础模型:我们可以使用Python中的深度学习框架(如TensorFlow、PyTorch等)来搭建Faster RCNN的基础模型。一般而言,我们可以选择使用预训练的CNN模型(如VGG、ResNet等)作为Faster RCNN的主干网络。 3. Region Proposal Network(RPN):在Faster RCNN中,首先需要使用RPN来生成候选的目标框。RPN是一个简单的卷积神经网络,它可以从图像中提取一系列的候选目标框。 4. ROI Pooling:通过ROI Pooling,我们可以将RPN生成的目标框提取出来,并将其调整为固定的大小。这一步骤是为了保证每个目标框的特征长度一致,方便后续的分类和回归。 5. 目标分类和回归:最后,我们使用CNN模型对提取出的目标框进行分类和回归。分类可以使用softmax函数,而回归可以使用线性回归等方法。 在实现Faster RCNN的过程中,我们还需要进行模型训练和调参等步骤,以获得更好的检测效果。此外,还可以利用一些其他的技巧,如数据增强、多尺度训练等来进一步提升模型性能。 总结起来,实现Faster RCNN主要包括数据准备、搭建基础模型、RPN生成目标框、ROI Pooling和目标分类与回归等步骤。同时,合理的训练和调参过程也是实现一个高性能的Faster RCNN模型的关键。 ### 回答3: Faster R-CNN(Region Convolutional Neural Network)是目标检测领域的经典算法,结合了区域建议网络(Region Proposal Networks)和卷积神经网络(Convolutional Neural Networks)。下面我将简要说明如何使用Python来实现Faster R-CNN。 首先,我们需要导入相应的Python库,如numpy、torch和torchvision等。接下来,需要定义我们的模型架构。Faster R-CNN的模型由提取特征的主干网络和两个子网络组成,即Region Proposal Network(RPN)和Fast R-CNN Network。 主干网络通常使用预训练的卷积神经网络,如ResNet、VGG等。我们可以使用torchvision中的这些预训练模型来作为主干网络。 接下来,我们需要定义RPN网络。RPN是一个用来生成候选检测框的网络,它通过在主干网络的特征图上滑动一个小窗口,并预测窗口内是否存在目标,并生成一组候选框。 然后,我们需要定义Fast R-CNN网络,该网络用于对RPN生成的候选框进行分类和回归。这个网络类似于一个分类器,根据候选框的特征来进行目标的分类和位置的回归。 在训练过程中,我们需要定义损失函数,通常包括分类损失和边界框回归损失。我们使用训练集来调整网络参数,以使模型能够准确地预测目标的类别和位置。 在测试过程中,我们可以使用模型来检测输入图像中的目标。我们先使用RPN网络生成一组候选框,然后使用Fast R-CNN网络对这些候选框进行分类和位置回归,得到最终的检测结果。 最后,我们可以根据需要对模型进行优化和调整,例如调整超参数、增加数据增强等,以提升模型的性能和泛化能力。 综上所述,使用Python来实现Faster R-CNN,我们需要导入库,定义模型架构、网络和损失函数,然后进行训练和测试。这只是一个简单的概述,实际实现中会涉及更多的细节和代码。具体的实现可以参考相关的开源实现和教程。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值