zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#,人工智能,深度学习,OpenCV目标检测,《训练集“负样本”批量生成器》源代码

2023-09-11 14:15:48 时间

一、正样本Positive Examples与负样本Negative Examples

正样本就是任务所要检测的目标物,比如在人脸识别中不同种族年龄的人脸、不同表情的人脸、戴不同装饰的人脸等等情况下的人脸;而负样本则是目标物所处的不同背景(注意:此背景不包含人脸),比如人脸会出现在不同环境中,街道、室内总之所有能想到的环境中都有可能出现人脸哈哈,负样本就是这些不包含人脸的图片,如果需要很多负样本,则可以把这些不包含人脸的图片裁剪成所需图片大小。

Positive samples are the targets to be detected by the task, such as faces of different races and ages, faces with different expressions, faces with different decorations, and so on in face recognition; Negative samples are the different backgrounds of the target object (Note: this background does not contain faces). For example, faces will appear in different environments, streets and rooms. In short, faces may appear in all conceivable environments. Haha, negative samples are these images that do not contain faces. If you need many negative samples, you can cut these images that do not contain faces to the required image size.

二、关于样本的概要性描述 About examples

1、样品数量。

根据经验法则:当你训练一个探测器时,你需要大约几个

每个阶段有数千个正样本和负样本。典型的探测器有10-20级,每级减少2倍的负片量。所以你需要大约3000-10000个正样本例子和大约5000000到100000000个负样本。

2、采取哪些消极措施。

经验法则:你需要在给定的环境中找到一张脸。所以

你需要将这种环境视为负样本。例如,如果你试图检测坐在教室里的学生的脸,而不是将教室里的图像(墙壁、窗户、人体、衣服等)作为负样本。拍摄月亮或天空的照片可能对你没有帮助。如果你不了解你的环境,那就尽量拍摄不同的自然图像(在不同的光照条件下)。

3、你应该把面部(如眼睛或鼻子)作为负样本吗?

你可以,但这是绝对不是enouah(只考虑那些负面因素)。探测器的真实强度将来自代表人脸典型背景的负图像。

4、如何收集/生成负样本你实际上不需要很多整洁的图像。

您可以拍摄1000张图像,并从中生成10000000个负样本。这是你怎么做的。假设你拍了一张100万像素分辨率1000x1000像素的汽车照片。假设您想训练人脸检测器,使其分辨率达到20x20像素(就像openCV一样)。所以你把你的1000x1000大图像切成20x20块。您可以获得2500件(50x50)。这就是如何从一张大图中产生2500个负样本。现在你可以把同样的大图像切成10x10像素的大小。现在,您将有额外的10000个负面示例。每个示例的大小为10x10像素,您可以将其放大2倍,以强制所有样本具有相同的大小。您可以根据需要重复此过程(将输入图像切割成不同大小的片段)。从数学上讲,如果你的图像大小为NxN,你可以通过取其中每个可能的矩形来生成O(n4)个反例。

5、在第4步中,我描述了如何拍摄一张大图像,并将其剪切成大量负面示例。

我必须警告你们,负样本不应该有高协方差,所以我不建议只拍摄一张图像,并从中生成100万个负样本。根据经验,创建一个包含1000个图像的库(或者从Gooqle下载随机图像)。确认所有图像均不包含面。从每张图片中裁剪出大约10000个负样本,现在你得到了相当可观的10000000个负样本。训练你的探测器。在下一步中,您可以将每张图像剪切到约50000张(部分重叠),从而将负片数量扩大到5000万张。你会开始有很好的结果

6、探测器的最终增强步骤。

当你已经有了一个相当好的探测器,运行它出现在许多图像上。它将产生错误检测(检测没有人脸的人脸)收集所有这些错误检测并将其添加到负片集中。现在再次对探测器进行重新训练。这样的迭代次数越多,检测器就越好。

7、实数

今天最好的人脸检测器(如FaceBook)使用数亿正样本的例子和几十亿负面的东西。作为积极的例子,他们不仅采用正样本表情,而且采用了多种方向的面部表情,不同的面部表情(微笑、喊叫、愤怒……),不同年龄段、不同性别、不同种族(高加索人、黑人、泰国人、中国人等),无论戴不戴眼镜/帽子/太阳镜/化妆等。你都无法与最好的人竞争,所以如果你的探测器漏掉了一些脸,不要生气。

原文:

1. Amount of samples. As a rule of thumbs: When you train a detector you need roughly few
thousands positive and negative examples per stage. Typical detector has 10-20 stages,Each stage reduces the amount of negative by a factor of 2. So you will need roughly 3,000-10,000 positive examples and ~5,000,000 to 100,000,000 negative examples.
2. Which negatives to take.A rule of thumb: You need to find a face in a given environment. So
you need to take that environment as negative examples. For instance, if you try to detect faces of students sitting in a classroom than take as negative examples images from the classroom (walls, windows, human body, clothes etc). Taking images of the moon or of the sky will probably not help you. If you don't know your environment than just take as much as possible different natural images (under different light conditions).
3. Should you take facial parts (like an eye, or a nose) as negative? You can but this is
definitely not enouah (to take only those negatives). The real strenath of the detector will come from the negative images which represent the typical background of the faces
4.How to collect/generate negative samples-You don't actualy need many neative images.
You can take 1000 images and generate 10,000,000 negative samples from them. Here is how you do it. Suppose you take a photo of a car of 1 mega pixel resolution 1000x1000 pixels. Suppose than you want to train face detector to work on resolution of 20x20 pixels (like openCV did). So you take your 1000x1000 big image and cut it to pieces of 20x20. You can get 2,500 pieces (50x50). So this is how from a single big image you generated 2,500 negative examples. Now you can take the same big image and cut it to pieces of size 10x10 pixels. You will now have additional 10,000 negative examples.Each example is of size 10x10 pixels and you can enlarge it by factor of 2 to force all the sample to have the same size. You can repeat this process as much as you want (cutting the input image to pieces of different size). Mathematically speaking, if your image is of size NxN-You can generate O(N^4) negative examples from it by taking each possible rectangle inside it.
5.In step 4,I described how to take a single big image and cut it to a large amount of negative examples. I must warn you that negative examples should not have high co-variance so I don't recommend taking only one image and generating 1 million negative examples from it. As a rule of thumb-create a library of 1000 images (or download random images from Gooqle). Verify than none of the images contains faces. Crop about 10,000 negative examples from each image and now you have got a decent 10,000,000 negative examples. Train your detector. In the next step you can cut each image to ~50,000 (partially overlapping pieces) and thus enlarge your amount of negatives to 50 millions. You will start having very good results with it
6. Final enhancement step of the detector. When you already have a rather good detector, run
It on many images.It will produce false detections (detect face where there is no face) Gather all those false detections and add them to your negative set. Now retrain the detector once again. The more such iterations you do the better your detector becomes
7. Real numbers- The best face detectors today (like Facebooks)use hundreds of millions of
positive examples and billions of negatives. As positive examples they take not only frontal faces but faces in many orientations, different facial expressions (smiling, shouting, angry…..), different age groups, different genders, different races (Caucasians, blacks, Thai, Chinese,…..), with or without glasses/hat/sunglasses/make-up etc. You will not be able to compete with the best, so don't get angry if your detector misses some faces. Good luck!

三、负样本的随机生成需求

寻找并制作千万亿级别的负样本,是一件受累的事情。不如自己花半小时写个程序自动生成,保证不会含有正样本!!!!!!!

(1)、背景颜色随机;

(2)、添加特效:椒盐点;线段;圆;矩形;白点;黑点;

(3)、高斯模糊:单词高斯模糊,多次高斯模糊;不同核大小的高斯模糊;

深度混淆 继续给大家分享从垃圾桶里面翻出来的不值钱的代码。

1、如果你是 C# 小白,请先安装 Visual Studio 2022,下是 图文(动画)教程:

C#,入门教程—— Visual Studio 2022开发环境搭建图文教程https://blog.csdn.net/beijinghorn/article/details/123434181

2、如果你是 C# OpenCV 开发小白,请先添加 OpenCvSharp 库:

C#,人工智能,深度学习,OpenCV,C#开发环境OpenCvSharp的安装、搭建与可视化教程https://blog.csdn.net/beijinghorn/article/details/125528673

3、OpenCV目标检测《千、万、亿级负样本图片一键生成器》源代码

     代码不多(POWER BY 315SOFT.COM)!

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using OpenCvSharp;

namespace Legalsoft.NegativeGenerator
{
    public partial class Form1 : Form
    {
        Random rnd = new Random((int)DateTime.Now.Ticks);

        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "人工智能,目标识别,负样本图片一键生成器(Negative Images Generator) —— BEIJING LEGAL SOFTWARE Ltd.";

            button1.Text = "Truffer"; button1.Cursor = Cursors.Hand;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num = 10000;
            if (radioButton7.Checked) num = 100000;
            if (radioButton5.Checked) num = 1000000;
            if (radioButton10.Checked) num = 10000000;

            string ext = ".jpg";
            if (radioButton7.Checked) ext = ".png";
            if (radioButton5.Checked) ext = ".gif";
            if (radioButton10.Checked) ext = ".bmp";

            int w = int.Parse(textBox1.Text);
            int h = int.Parse(textBox2.Text);

            try
            {
                string folder = Path.Combine(Application.StartupPath, DateTime.Now.ToString("yyyy-MM-dd-HH"));
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }

                progressBar1.Style = ProgressBarStyle.Continuous;
                progressBar1.Maximum = num;
                progressBar1.Value = 0;
                for (int k = 0; k < num; k++)
                {
                    progressBar1.Value = k;
                    progressBar1.Refresh();

                    using (Mat img = new Mat(h, w, (radioButton1.Checked) ? MatType.CV_8UC3 : MatType.CV_8UC1, (checkBox5.Checked) ? Scalar.RandomColor() : Scalar.White))
                    {
                        for (int i = 0; i < w * h / 10; i++)
                        {
                            int x1 = rnd.Next(w);
                            int y1 = rnd.Next(h);
                            int x2 = rnd.Next(w);
                            int y2 = rnd.Next(h);
                            int w1 = rnd.Next(w / 5) + 1;
                            int h1 = rnd.Next(h / 5) + 1;

                            // 椒盐
                            if (checkBox1.Checked)
                            {
                                if (img.Type() == MatType.CV_8UC1)
                                {
                                    int cx = (byte)rnd.Next(256);
                                    Cv2.Line(img, new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x1 + 1, y1), new Scalar(cx, cx, cx));
                                }
                                else if (img.Type() == MatType.CV_8UC3)
                                {
                                    Cv2.Line(img, new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x1 + 1, y1), Scalar.RandomColor());
                                }
                            }
                            // 线段
                            if (checkBox2.Checked)
                            {
                                Cv2.Line(img, new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x2, y2), Scalar.RandomColor());
                            }
                            // 圆
                            if (checkBox3.Checked && rnd.Next(10) > 6)
                            {
                                Cv2.Circle(img, (x1 + x2) / 2, (y1 + y2) / 2, w1, Scalar.RandomColor(), -1);
                            }
                            // 矩形
                            if (checkBox3.Checked && rnd.Next(10) > 6)
                            {
                                Cv2.Rectangle(img, new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x2, y2), Scalar.RandomColor(), -1);
                            }
                            // 白点
                            if (checkBox7.Checked)
                            {
                                Cv2.Line(img, new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x1 + 1, y1), Scalar.White);
                            }
                            // 黑点
                            if (checkBox8.Checked)
                            {
                                Cv2.Line(img, new OpenCvSharp.Point(x1, y1), new OpenCvSharp.Point(x1 + 1, y1), Scalar.Black);
                            }
                        }

                        // 高斯模糊
                        string filename = Path.Combine(folder, String.Format("{0:D8}", k + 1) + ext);
                        if (checkBox6.Checked)
                        {
                            int ksize = Math.Min(w / 10, h / 10);
                            if ((ksize % 2) == 0) ksize++;

                            Mat gas = new Mat();
                            img.CopyTo(gas);
                            for (int j = 0; j < 20; j++)
                            {
                                Cv2.GaussianBlur(gas, gas, new OpenCvSharp.Size(ksize, ksize), 0);
                            }
                            gas.SaveImage(filename);
                        }
                        else
                        {
                            img.SaveImage(filename);
                        }
                    }
                }
                progressBar1.Value = 0;
                MessageBox.Show("Put down coffee cup!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void radioButton10_Click(object sender, EventArgs e)
        {
            radioButton8.Checked = true;
            radioButton10.Checked = false;
            MessageBox.Show("你的硬盘表示反对!");
        }
    }
}

 请自行改进之处,包括但不限于:

(1)如果数量巨大,比如,10000000000 或更多,应该创建一些子目录!!!

(2)可以修改或增加 高斯混淆的 ksize ;

(3)。。。