一开始我在网上找demo没有找到,在群里寻求帮助也没有得到结果,索性将网上的易语言模块反编译之后,提取出对应的dll以及代码,然后对照官方的c++代码,写出了下面的c#版本

  1. /***
  2. * @pName caffe_task_pool_demo
  3. * @name CC
  4. * @user wadezh
  5. * @date 2018/6/16
  6. * @desc
  7. */
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Runtime.InteropServices;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16.  
  17. namespace caffe_task_pool_demo
  18. {
  19. class CC
  20. {
  21.  
  22. public static int taskPool { get; set; } = ;
  23. public static string prototxt { get; set; }
  24. public static ArrayList map { get; set; }
  25. public static int timeStep { get; set; }
  26. public static int alphabetSize { get; set; }
  27.  
  28. /*Caffe_API TaskPool* __stdcall createTaskPoolByData(
  29.  
  30. const void* prototxt_data,
  31.  
  32. int prototxt_data_length,
  33.  
  34. const void* caffemodel_data,
  35.  
  36. int caffemodel_data_length,
  37.  
  38. float scale_raw = 1,
  39.  
  40. const char* mean_file = 0,
  41.  
  42. int num_means = 0,
  43.  
  44. float* means = 0,
  45.  
  46. int gpu_id = -1,
  47.  
  48. int batch_size = 3);*/
  49.  
  50. [DllImport("classification_dll.dll", EntryPoint = "createTaskPoolByData", CallingConvention = CallingConvention.StdCall)]
  51. public static extern int CreateTaskPoolByData(byte[] prototxt_data,
  52. int prototxt_data_length,
  53. byte[] caffemodel_data,
  54. int caffemodel_data_length,
  55. float scale_raw = ,
  56. string mean_file = "",
  57. int num_means = ,
  58. float means = ,
  59. int gpu_id = -,
  60. int cach_size = );
  61.  
  62. /*Caffe_API BlobData* __stdcall forwardByTaskPool(TaskPool* pool, const void* img, int len, const char* blob_name);*/
  63.  
  64. [DllImport("classification_dll.dll", EntryPoint = "forwardByTaskPool", CallingConvention = CallingConvention.StdCall)]
  65. public static extern int ForwardByTaskPool(int poolHandle, byte[] image, int imageLen, string printBlobName);
  66.  
  67. /*Caffe_API int __stdcall getBlobLength(BlobData* feature);*/
  68. [DllImport("classification_dll.dll", EntryPoint = "getBlobLength", CallingConvention = CallingConvention.StdCall)]
  69. public static extern int GetBlobLength(int feature);
  70.  
  71. /*Caffe_API void __stdcall cpyBlobData(void* buffer, BlobData* feature);*/
  72. [DllImport("classification_dll.dll", EntryPoint = "cpyBlobData", CallingConvention = CallingConvention.StdCall)]
  73. public static extern int CpyBlobData(float[] buffer, int feature);
  74.  
  75. /*Caffe_API void __stdcall releaseBlobData(BlobData* ptr);*/
  76. [DllImport("classification_dll.dll", EntryPoint = "releaseBlobData", CallingConvention = CallingConvention.StdCall)]
  77. public static extern int ReleaseBlobData(int ptr);
  78.  
  79. private static int Argmax(float[] arr, int begin, int end, ref float acc)
  80. {
  81. acc = -;
  82. int mxInd = ;
  83. for (int i = begin; i < end; i++)
  84. {
  85. if (acc < arr[i])
  86. {
  87. mxInd = i;
  88. acc = arr[i];
  89. }
  90. }
  91. return mxInd - begin;
  92. }
  93.  
  94. public static bool InitCaptcha(string prototxtPath, string modelPath, string mapPath, int gpuId, int batchSize) {
  95. byte[] deploy = Util.GetFileStream(prototxtPath);
  96. byte[] model = Util.GetFileStream(modelPath);
  97. CC.taskPool = CC.CreateTaskPoolByData(deploy, deploy.Length, model, model.Length, 1F, "", , 0F, gpuId, batchSize);
  98. CC.prototxt = System.Text.Encoding.Default.GetString(deploy);
  99. string[] mapFile = Util.LoadStringFromFile(mapPath).Trim().Split("\r\n".ToArray());
  100. CC.map = new ArrayList();
  101. for (int i = ; i < mapFile.Length; i++)
  102. {
  103. if (mapFile[i].Length > )
  104. {
  105. CC.map.Add(mapFile[i]);
  106. }
  107. }
  108. string time_step = Util.GetMiddleString(CC.prototxt, "time_step:", "\r\n");
  109. string layer = Util.GetMiddleString(CC.prototxt, "inner_product_param {", "{");
  110. string alphabet_size = Util.GetMiddleString(layer, "num_output:", "\r\n");
  111. CC.timeStep = int.Parse(time_step);
  112. CC.alphabetSize = int.Parse(alphabet_size);
  113. return CC.taskPool != ;
  114. }
  115.  
  116. public static string GetCaptcha(byte[] image) {
  117. // Get the prediction result handle
  118. int poolHandle = CC.ForwardByTaskPool(taskPool, image, image.Length, "premuted_fc");
  119.  
  120. // Get the tensor handle
  121. float[] permute_fc = new float[CC.GetBlobLength(poolHandle)];
  122.  
  123. // Copy the tensor data
  124. CpyBlobData(permute_fc, poolHandle);
  125. string code = string.Empty;
  126.  
  127. if (permute_fc.Length > )
  128. {
  129. int o = ;
  130. float acc = 0F;
  131. int emptyLabel = alphabetSize - ;
  132. int prev = emptyLabel;
  133. for (int i = ; i < timeStep; i++)
  134. {
  135. o = Argmax(permute_fc, (i - ) * alphabetSize + , i * alphabetSize, ref acc);
  136. if (o != emptyLabel && prev != o) code += map[o + ];
  137. prev = o;
  138. }
  139. code = code.Replace("_", "").Trim();
  140. }
  141.  
  142. ReleaseBlobData(poolHandle);
  143. return code;
  144. }
  145.  
  146. protected class Util
  147. {
  148.  
  149. public static byte[] GetFileStream(string path)
  150. {
  151. FileStream fs = new FileStream(path, FileMode.Open);
  152. long size = fs.Length;
  153. byte[] array = new byte[size];
  154. fs.Read(array, , array.Length);
  155. fs.Close();
  156. return array;
  157. }
  158.  
  159. public static string LoadStringFromFile(string fileName)
  160. {
  161. string content = string.Empty;
  162.  
  163. StreamReader sr = null;
  164. try
  165. {
  166. sr = new StreamReader(fileName, System.Text.Encoding.UTF8);
  167. content = sr.ReadToEnd();
  168. }
  169. catch (Exception ex)
  170. {
  171. throw ex;
  172. }
  173.  
  174. if (sr != null)
  175. sr.Close();
  176.  
  177. return content;
  178. }
  179.  
  180. public static string GetMiddleString(string SumString, string LeftString, string RightString)
  181. {
  182. if (string.IsNullOrEmpty(SumString)) return "";
  183. if (string.IsNullOrEmpty(LeftString)) return "";
  184. if (string.IsNullOrEmpty(RightString)) return "";
  185.  
  186. int LeftIndex = SumString.IndexOf(LeftString);
  187. if (LeftIndex == -) return "";
  188. LeftIndex = LeftIndex + LeftString.Length;
  189. int RightIndex = SumString.IndexOf(RightString, LeftIndex);
  190. if (RightIndex == -) return "";
  191. return SumString.Substring(LeftIndex, RightIndex - LeftIndex);
  192. }
  193.  
  194. }
  195.  
  196. }
  197.  
  198. }

项目中我已经将caffemodel以及prototxt等文件都打包,可以直接运行

我封装的这个CC类只支持GPU任务池识别,识别速度比较快

链接:https://pan.baidu.com/s/17tSh3IE3Xv_YlJhSOhKddg 密码:ct5z

Caffe任务池GPU模型图像识别的更多相关文章

  1. Caffe学习笔记(一):Caffe架构及其模型解析

    Caffe学习笔记(一):Caffe架构及其模型解析 写在前面:关于caffe平台如何快速搭建以及如何在caffe上进行训练与预测,请参见前面的文章<caffe平台快速搭建:caffe+wind ...

  2. Caffe框架GPU与MLU计算结果不一致请问如何调试?

    Caffe框架GPU与MLU计算结果不一致请问如何调试? 某一检测模型移植到Cambricon Caffe上时,发现无法检测出结果,于是将GPU和MLU的运行结果输出并保存后进行对比,发现二者计算结果 ...

  3. Error when Building GPU docker image for caffe: Unsupported gpu architecture 'compute_60'

    issue: Error when Building GPU docker image for caffe: Unsupported gpu architecture 'compute_60' rea ...

  4. 在Caffe中实现模型融合

    模型融合 有的时候我们手头可能有了若干个已经训练好的模型,这些模型可能是同样的结构,也可能是不同的结构,训练模型的数据可能是同一批,也可能不同.无论是出于要通过ensemble提升性能的目的,还是要设 ...

  5. pycaffe︱caffe中fine-tuning模型三重天(函数详解、框架简述)

    本文主要参考caffe官方文档[<Fine-tuning a Pretrained Network for Style Recognition>](http://nbviewer.jupy ...

  6. 基于Caffe训练AlexNet模型

    数据集 1.准备数据集 1)下载训练和验证图片 ImageNet官网地址:http://www.image-net.org/signup.php?next=download-images (需用邮箱注 ...

  7. Caffe学习笔记2--Ubuntu 14.04 64bit 安装Caffe(GPU版本)

    0.检查配置 1. VMWare上运行的Ubuntu,并不能支持真实的GPU(除了特定版本的VMWare和特定的GPU,要求条件严格,所以我在VMWare上搭建好了Caffe环境后,又重新在Windo ...

  8. windows+caffe(四)——创建模型并编写配置文件+训练和测试

    1.模型就用程序自带的caffenet模型,位置在 models/bvlc_reference_caffenet/文件夹下, 将需要的两个配置文件,复制到myfile文件夹内 2. 修改solver. ...

  9. caffe 无GPU 环境搭建

    root@k-Lenovo:/home/k# sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-d ...

随机推荐

  1. 解决Linux下MySQL登录ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using passwor)问题

    两种方案: 一: 1.修改mysql配置文件:vim /etc/my.cnf 2.找到[mysqld]下添加 skip-grant-tables 配置 3.重启服务service mysqld res ...

  2. 分享一个jquery插件,弥补一下hover事件的小小不足

    hover事件有一个缺点:当你的鼠标无意划过一个dom元素(瞬间划过,这个时候用户可能不想触发hover事件),会触发hover事件 应该设置一个时差来控制hover事件的触发 比如jd左边的菜单 你 ...

  3. ES6系列_9之对象

    1.对象赋值 es5中的对象赋值方式如下: let name="小明"; let skill= 'es6开发'; var obj= {name:name,skill:skill}; ...

  4. Matlab信号处理工具箱函数

    波形产生和绘图chirp 产生扫描频率余弦diric 产生Dirichlet函数或周期Sinc函数gauspuls 产生高斯调制正弦脉冲pulstran 产生脉冲串rectpuls 产生非周期矩形信号 ...

  5. 通过 NPOI 生成 Excel

    HSSFWorkbook hssfworkbook; ISheet sheet1; public void BuildExcel() { hssfworkbook = new HSSFWorkbook ...

  6. A configuration error occurred during startup.Please verify the preference filed with the prompt:Connect to VM

    1. 检查JDK,及Tomcat是否正确可用.2. Tomcat,myeclipse使用的是不是同一个jdk.3. 检查系统的防火墙是不是阻止了MyEclipse主程序访问网络.

  7. fft 远程服务器返回错误 550返回码

    "远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)"时,可能是如下原因: 1.URL路径不对,看看有没有多加空格,或者大小写问题 2.权限是否足 3.需 ...

  8. TI davinci DM6467通过串口0将UBL和u-boot写入NAND flash

    TI的davinci系列一般支持好几种启动模式,如下图TMS320DM6467的datasheet可以查到所有的BOOTMODE LVS301和LW9226的开发板上有一个选择bootmode的拨码开 ...

  9. 录制手机的视频,转换成gif

    Android: 通过adb命令去进行录屏,然后将录制的视频转换成gif图片: 前提:确保电脑上安装了adb,且Android的level高于19. 1.adb shell screenrecord ...

  10. 关于ErrorPage

    JSP里创建一个网页test.jsp, 让这个网页上面出现一个错误, 再创建一个切换页面error.jsp, 使test.jsp如果出现错误就切换到error.jsp上, 但是怎么试都是出现一个网页上 ...