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; } = ;
- 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 = ,
- string mean_file = "",
- int num_means = ,
- float means = ,
- int gpu_id = -,
- int cach_size = );
- /*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 = -;
- int mxInd = ;
- 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, "", , 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 = ; i < mapFile.Length; i++)
- {
- if (mapFile[i].Length > )
- {
- 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 != ;
- }
- 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 > )
- {
- int o = ;
- float acc = 0F;
- int emptyLabel = alphabetSize - ;
- int prev = emptyLabel;
- for (int i = ; i < timeStep; i++)
- {
- o = Argmax(permute_fc, (i - ) * alphabetSize + , i * alphabetSize, ref acc);
- if (o != emptyLabel && prev != o) code += map[o + ];
- 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, , 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 == -) return "";
- LeftIndex = LeftIndex + LeftString.Length;
- int RightIndex = SumString.IndexOf(RightString, LeftIndex);
- if (RightIndex == -) return "";
- return SumString.Substring(LeftIndex, RightIndex - LeftIndex);
- }
- }
- }
- }
项目中我已经将caffemodel以及prototxt等文件都打包,可以直接运行
我封装的这个CC类只支持GPU任务池识别,识别速度比较快
链接:https://pan.baidu.com/s/17tSh3IE3Xv_YlJhSOhKddg 密码:ct5z
Caffe任务池GPU模型图像识别的更多相关文章
- Caffe学习笔记(一):Caffe架构及其模型解析
Caffe学习笔记(一):Caffe架构及其模型解析 写在前面:关于caffe平台如何快速搭建以及如何在caffe上进行训练与预测,请参见前面的文章<caffe平台快速搭建:caffe+wind ...
- Caffe框架GPU与MLU计算结果不一致请问如何调试?
Caffe框架GPU与MLU计算结果不一致请问如何调试? 某一检测模型移植到Cambricon Caffe上时,发现无法检测出结果,于是将GPU和MLU的运行结果输出并保存后进行对比,发现二者计算结果 ...
- 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 ...
- 在Caffe中实现模型融合
模型融合 有的时候我们手头可能有了若干个已经训练好的模型,这些模型可能是同样的结构,也可能是不同的结构,训练模型的数据可能是同一批,也可能不同.无论是出于要通过ensemble提升性能的目的,还是要设 ...
- pycaffe︱caffe中fine-tuning模型三重天(函数详解、框架简述)
本文主要参考caffe官方文档[<Fine-tuning a Pretrained Network for Style Recognition>](http://nbviewer.jupy ...
- 基于Caffe训练AlexNet模型
数据集 1.准备数据集 1)下载训练和验证图片 ImageNet官网地址:http://www.image-net.org/signup.php?next=download-images (需用邮箱注 ...
- Caffe学习笔记2--Ubuntu 14.04 64bit 安装Caffe(GPU版本)
0.检查配置 1. VMWare上运行的Ubuntu,并不能支持真实的GPU(除了特定版本的VMWare和特定的GPU,要求条件严格,所以我在VMWare上搭建好了Caffe环境后,又重新在Windo ...
- windows+caffe(四)——创建模型并编写配置文件+训练和测试
1.模型就用程序自带的caffenet模型,位置在 models/bvlc_reference_caffenet/文件夹下, 将需要的两个配置文件,复制到myfile文件夹内 2. 修改solver. ...
- caffe 无GPU 环境搭建
root@k-Lenovo:/home/k# sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-d ...
随机推荐
- 解决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 ...
- 分享一个jquery插件,弥补一下hover事件的小小不足
hover事件有一个缺点:当你的鼠标无意划过一个dom元素(瞬间划过,这个时候用户可能不想触发hover事件),会触发hover事件 应该设置一个时差来控制hover事件的触发 比如jd左边的菜单 你 ...
- ES6系列_9之对象
1.对象赋值 es5中的对象赋值方式如下: let name="小明"; let skill= 'es6开发'; var obj= {name:name,skill:skill}; ...
- Matlab信号处理工具箱函数
波形产生和绘图chirp 产生扫描频率余弦diric 产生Dirichlet函数或周期Sinc函数gauspuls 产生高斯调制正弦脉冲pulstran 产生脉冲串rectpuls 产生非周期矩形信号 ...
- 通过 NPOI 生成 Excel
HSSFWorkbook hssfworkbook; ISheet sheet1; public void BuildExcel() { hssfworkbook = new HSSFWorkbook ...
- 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主程序访问网络.
- fft 远程服务器返回错误 550返回码
"远程服务器返回错误:(550) 文件不可用(例如,未找到文件,无法访问文件)"时,可能是如下原因: 1.URL路径不对,看看有没有多加空格,或者大小写问题 2.权限是否足 3.需 ...
- TI davinci DM6467通过串口0将UBL和u-boot写入NAND flash
TI的davinci系列一般支持好几种启动模式,如下图TMS320DM6467的datasheet可以查到所有的BOOTMODE LVS301和LW9226的开发板上有一个选择bootmode的拨码开 ...
- 录制手机的视频,转换成gif
Android: 通过adb命令去进行录屏,然后将录制的视频转换成gif图片: 前提:确保电脑上安装了adb,且Android的level高于19. 1.adb shell screenrecord ...
- 关于ErrorPage
JSP里创建一个网页test.jsp, 让这个网页上面出现一个错误, 再创建一个切换页面error.jsp, 使test.jsp如果出现错误就切换到error.jsp上, 但是怎么试都是出现一个网页上 ...