IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> CNN卷积网络图像前处理 -> 正文阅读

[人工智能]CNN卷积网络图像前处理

CNN卷积网络图像前处理

说明

  • PreprocessBase: 抽象基类,子类需要实现operator(),实现图像前处理运算
  • CHWFP32Preprocess: 一个实现用例
  • test_preprocess.cpp : 测试代码(来源ResNet50)

源码

PreprocessBase

  • Preprocess.h
    #pragma once
    #include <opencv2/opencv.hpp>
    #include <string>
    #include <stdint.h>
    
    class PreprocessBase{
    public:
        static inline void resize(cv::Mat &img, int size);
    	static inline void center_crop(cv::Mat &img, int crop_size, cv::Mat &oimg);
    	static inline float half_to_float(const uint16_t x);
    	static inline uint16_t float_to_half(const float x);
    public:
    	virtual void operator()(const std::string &imgfile, void *img_out)=0;
    	virtual ~PreprocessBase()=0;
    };
    
    inline uint32_t 
    as_uint(const float x) 
    {
        return *(uint32_t*)&x;
    }
    inline float 
    as_float(const uint32_t x) {
        return *(float*)&x;
    }
    
    //https://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion
    inline float 
    PreprocessBase::half_to_float(const uint16_t x) 
    {
    	// IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
        const uint e = (x&0x7C00)>>10; // exponent
        const uint m = (x&0x03FF)<<13; // mantissa
        const uint v = as_uint((float)m)>>23; // evil log2 bit hack to count leading zeros in denormalized format
        return as_float((x&0x8000)<<16 | (e!=0)*((e+112)<<23|m) | ((e==0)&(m!=0))*((v-37)<<23|((m<<(150-v))&0x007FE000))); // sign : normalized : denormalized
    }
    
    inline uint16_t 
    PreprocessBase::float_to_half(const float x)
    { 
    	// IEEE-754 16-bit floating-point format (without infinity): 1-5-10, exp-15, +-131008.0, +-6.1035156E-5, +-5.9604645E-8, 3.311 digits
        const uint b = as_uint(x)+0x00001000; // round-to-nearest-even: add last bit after truncated mantissa
        const uint e = (b&0x7F800000)>>23; // exponent
        const uint m = b&0x007FFFFF; // mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
        return (b&0x80000000)>>16 | (e>112)*((((e-112)<<10)&0x7C00)|m>>13) | ((e<113)&(e>101))*((((0x007FF000+m)>>(125-e))+1)>>1) | (e>143)*0x7FFF; // sign : normalized : denormalized : saturate
    }
    typedef struct HalfFloat{
    	uint16_t x;
    	inline explicit operator float() 
    	{
    		return PreprocessBase::half_to_float(x);
    	}
    } float16;
    
    inline void
    PreprocessBase::resize(cv::Mat &img, int size)
    {
        auto w=img.cols;
        auto h=img.rows;
    
        int ow,oh;
    
        if(w<h)
        {
            ow=size;
            oh=int(size*(double)h/w);
        }
        else
        {
            oh=size;
            ow=int(size*(double)w/h);
        }
    
        cv::resize(img, img, cv::Size(ow,oh));
    
    }
    
    inline void
    PreprocessBase::center_crop(cv::Mat &img, int crop_size, cv::Mat &oimg)
    {
      
        CV_Assert(crop_size<=img.cols);
        CV_Assert(crop_size<=img.rows);
    	
        auto roi = img(cv::Rect((img.cols - crop_size) / 2, (img.rows - crop_size) / 2, crop_size, crop_size));
        roi.copyTo(oimg, cv::Mat(roi.rows, roi.cols, roi.depth(), cv::Scalar(1)));
    
        return;
    }
    
    
  • Preprocess.cpp
    #include "PreprocessBase.h"
    PreprocessBase::~PreprocessBase(){}
    
  • CHWFP32Preprocess.h
    #include "PreprocessBase.h"
    
    #include<vector>
    #include<omp.h>
    
    class CHWFP32Preprocess: public PreprocessBase
    {
    public:
    	using ResultType=float;
    	using ImageType=uint8_t;
    private:
    	int m_crop_size;
    	int m_size;
    	float m_scale;
    	float m_mean_bgr[3];
    private:
    	void resize();
    	void center_crop();
    	
    	template<int channel>
    	void apply_scale_and_mean(float *out) const;
    	
    private:
    	cv::Mat m_resized_img;
    	cv::Mat m_cropped_img;
    	std::vector<cv::Mat> m_bgr;
    	
    public:
    	CHWFP32Preprocess(int crop_size=224, float scale=1/128.0f, float mean_b=128.0f, float mean_g=128.0f, float mean_r=128.0f):
    	m_crop_size(crop_size),m_scale(scale),m_mean_bgr{mean_b,mean_g,mean_r}
    	{
    		CV_Assert(crop_size>100 && crop_size<256);
    		CV_Assert(scale>0 && scale<=1);
    		CV_Assert(mean_b>0 && mean_b<256);
    		CV_Assert(mean_g>0 && mean_g<256);
    		CV_Assert(mean_r>0 && mean_r<256);
    		
    		m_size=m_crop_size*m_crop_size;
    		
    	}
    public:
    	void operator()(const std::string &imgfile,void *img_out) override;
    };
    
    template<int channel>
    void CHWFP32Preprocess::apply_scale_and_mean(ResultType *out) const
    {
    	static_assert(channel>=0 && channel<=2);
    	
    	float mean=m_mean_bgr[channel];
    	uint8_t *data=m_bgr[channel].data;
    	
    	for(int i=0; i<m_size; i++)
    	{
    		out[i]=ResultType((data[i]-mean)*m_scale);
    	}
    	
    	return;
    }
    
  • CHWFP32Preprocess.cpp
    #include "CHWFP32Preprocess.h"
    
    void CHWFP32Preprocess::operator()(const std::string &imgfile, void *img_out)
    {
    	CV_Assert(img_out!=nullptr);
    	
    	//读图
    	m_resized_img=cv::imread(imgfile);
    	
    	CV_Assert(m_resized_img.data!=nullptr);
    	
    	//放大
    	PreprocessBase::resize(m_resized_img, 256);
    	
    	//抠图
    	PreprocessBase::center_crop(m_resized_img, m_crop_size, m_cropped_img);
    	
    	//BGR通道分离	
    	cv::split(m_cropped_img,m_bgr);
    
    	ResultType *out=static_cast<ResultType*>(img_out);
    	
    	//减去均值,乘比例因子
    	#pragma omp parallel sections num_threads(3)
    	{	
    		apply_scale_and_mean<0>(out+0*m_size); //B
    		
    		#pragma omp section
    		apply_scale_and_mean<1>(out+1*m_size); //G
    		
    		#pragma omp section
    		apply_scale_and_mean<2>(out+2*m_size); //R
    	}
    }
    
  • test_preprocess.cpp
    #include "CHWFP32Preprocess.h"
    #include <memory> //std::unique_ptr
    
    void test()
    {
    	std::string imgfile{"1.jpg"};
    	
    	int crop_size=224;
    	size_t size=crop_size*crop_size;
    	
    	std::unique_ptr<PreprocessBase> preprocessor{new CHWFP32Preprocess{crop_size,1.0f,103.939f,116.779f,123.68f}};
    	
    	using ResultType=CHWFP32Preprocess::ResultType;
    	
    	auto *oimg=new ResultType[3*crop_size*crop_size];
    	
    	(*preprocessor)(imgfile,oimg);
    	
    	for(size_t i=0; i<crop_size;i+=50)
    		for(size_t j=0; j<crop_size;j+=50)
    		{
    			size_t k=i*crop_size+j;
    			
    			float b=float(oimg[k]);
    			float g=float(oimg[k+size]);
    			float r=float(oimg[k+2*size]);
    			
    			printf("img[%d,%d]=(%g,%g,%g)\n",(int)i,(int)j,b,g,r);
    		}
    	
    	size_t k=size-1;
    
    	float b=float(oimg[k]);
    	float g=float(oimg[k+size]);
    	float r=float(oimg[k+2*size]);
    	
    	printf("img[%d,%d]=(%g,%g,%g)\n",(int)crop_size-1,(int)crop_size-1, b,g,r);
    	
    	delete []oimg;
    	
    }
    
    int 
    main(int argc, char **argv)
    {
    	test();
    	
    	return(0);
    }
    
  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-08-04 11:12:44  更:2021-08-04 11:12:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 10:19:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码