当前位置:
首页 > Python基础教程 >
-
C#教程之Caffe任务池GPU模型图像识别
一开始我在网上找demo没有找到,在群里寻求帮助也没有得到结果,索性将网上的易语言模块反编译之后,提取出对应的dll以及代码,然后对照官方的c++代码,写出了下面的c#版本
/*** * @pName caffe_task_pool_demo * @name CC * @user wadezh * @date 2018/6/16 * @desc */ using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace caffe_task_pool_demo { class CC { public static int taskPool { get; set; } = 0; public static string prototxt { get; set; } public static ArrayList map { get; set; } public static int timeStep { get; set; } public static int alphabetSize { get; set; } /*Caffe_API TaskPool* __stdcall createTaskPoolByData( const void* prototxt_data, int prototxt_data_length, const void* caffemodel_data, int caffemodel_data_length, float scale_raw = 1, const char* mean_file = 0, int num_means = 0, float* means = 0, int gpu_id = -1, int batch_size = 3);*/ [DllImport("classification_dll.dll", EntryPoint = "createTaskPoolByData", CallingConvention = CallingConvention.StdCall)] public static extern int CreateTaskPoolByData(byte[] prototxt_data, int prototxt_data_length, byte[] caffemodel_data, int caffemodel_data_length, float scale_raw = 1, string mean_file = "", int num_means = 0, float means = 0, int gpu_id = -1, int cach_size = 1); /*Caffe_API BlobData* __stdcall forwardByTaskPool(TaskPool* pool, const void* img, int len, const char* blob_name);*/ [DllImport("classification_dll.dll", EntryPoint = "forwardByTaskPool", CallingConvention = CallingConvention.StdCall)] public static extern int ForwardByTaskPool(int poolHandle, byte[] image, int imageLen, string printBlobName); /*Caffe_API int __stdcall getBlobLength(BlobData* feature);*/ [DllImport("classification_dll.dll", EntryPoint = "getBlobLength", CallingConvention = CallingConvention.StdCall)] public static extern int GetBlobLength(int feature); /*Caffe_API void __stdcall cpyBlobData(void* buffer, BlobData* feature);*/ [DllImport("classification_dll.dll", EntryPoint = "cpyBlobData", CallingConvention = CallingConvention.StdCall)] public static extern int CpyBlobData(float[] buffer, int feature); /*Caffe_API void __stdcall releaseBlobData(BlobData* ptr);*/ [DllImport("classification_dll.dll", EntryPoint = "releaseBlobData", CallingConvention = CallingConvention.StdCall)] public static extern int ReleaseBlobData(int ptr); private static int Argmax(float[] arr, int begin, int end, ref float acc) { acc = -9999; int mxInd = 0; for (int i = begin; i < end; i++) { if (acc < arr[i]) { mxInd = i; acc = arr[i]; } } return mxInd - begin; } public static bool InitCaptcha(string prototxtPath, string modelPath, string mapPath, int gpuId, int batchSize) { byte[] deploy = Util.GetFileStream(prototxtPath); byte[] model = Util.GetFileStream(modelPath); CC.taskPool = CC.CreateTaskPoolByData(deploy, deploy.Length, model, model.Length, 1F, "", 0, 0F, gpuId, batchSize); CC.prototxt = System.Text.Encoding.Default.GetString(deploy); string[] mapFile = Util.LoadStringFromFile(mapPath).Trim().Split("\r\n".ToArray()); CC.map = new ArrayList(); for (int i = 0; i < mapFile.Length; i++) { if (mapFile[i].Length > 0) { CC.map.Add(mapFile[i]); } } string time_step = Util.GetMiddleString(CC.prototxt, "time_step:", "\r\n"); string layer = Util.GetMiddleString(CC.prototxt, "inner_product_param {", "{"); string alphabet_size = Util.GetMiddleString(layer, "num_output:", "\r\n"); CC.timeStep = int.Parse(time_step); CC.alphabetSize = int.Parse(alphabet_size); return CC.taskPool != 0; } public static string GetCaptcha(byte[] image) { // Get the prediction result handle int poolHandle = CC.ForwardByTaskPool(taskPool, image, image.Length, "premuted_fc"); // Get the tensor handle float[] permute_fc = new float[CC.GetBlobLength(poolHandle)]; // Copy the tensor data CpyBlobData(permute_fc, poolHandle); string code = string.Empty; if (permute_fc.Length > 0) { int o = 0; float acc = 0F; int emptyLabel = alphabetSize - 1; int prev = emptyLabel; for (int i = 1; i < timeStep; i++) { o = Argmax(permute_fc, (i - 1) * alphabetSize + 1, i * alphabetSize, ref acc); if (o != emptyLabel && prev != o) code += map[o + 1]; prev = o; } code = code.Replace("_", "").Trim(); } ReleaseBlobData(poolHandle); return code; } protected class Util { public static byte[] GetFileStream(string path) { FileStream fs = new FileStream(path, FileMode.Open); long size = fs.Length; byte[] array = new byte[size]; fs.Read(array, 0, array.Length); fs.Close(); return array; } public static string LoadStringFromFile(string fileName) { string content = string.Empty; StreamReader sr = null; try { sr = new StreamReader(fileName, System.Text.Encoding.UTF8); content = sr.ReadToEnd(); } catch (Exception ex) { throw ex; } if (sr != null) sr.Close(); return content; } public static string GetMiddleString(string SumString, string LeftString, string RightString) { if (string.IsNullOrEmpty(SumString)) return ""; if (string.IsNullOrEmpty(LeftString)) return ""; if (string.IsNullOrEmpty(RightString)) return ""; int LeftIndex = SumString.IndexOf(LeftString); if (LeftIndex == -1) return ""; LeftIndex = LeftIndex + LeftString.Length; int RightIndex = SumString.IndexOf(RightString, LeftIndex); if (RightIndex == -1) return ""; return SumString.Substring(LeftIndex, RightIndex - LeftIndex); } } } }
项目中我已经将caffemodel以及prototxt等文件都打包,可以直接运行
我封装的这个CC类只支持GPU任务池识别,识别速度比较快
链接:https://pan.baidu.com/s/17tSh3IE3Xv_YlJhSOhKddg 密码:ct5z
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式