一开始我在网上找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模型图像识别的更多相关文章

  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. 学习记录:CONCAT()

    连接多个字符串 SELECT * from t_info where phone = CONCAT('12345','678900')

  2. leetcode807

    class Solution { public: int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) { ...

  3. GridEh排序

    添加pas文件 EhLibADO.pas EhLibCDS.pas EhLibFireDAC.pas 支持FireDAC 设置GridEh的属性 DBGridEh1->SortLocal = t ...

  4. Java路程

    Java学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每 ...

  5. VS2015 C#6.0 中的没有实现/支持的特性

      VS2015 C#6.0 中的没有实现/支持的特性   .数组增强:赋值 维数组 Int[] numbers: numbers = {2,3,4,5}; 维数组 Int[,] numbers2; ...

  6. CHEMISTS DISCOVER A SAFE, GREEN METHOD TO PROCESS RED PHOSPHORUS

                   When it comes to making phosphorus compounds, chemists have traditionally relied on w ...

  7. 41. First Missing Positive (HashTable)

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  8. 单源最短路:Bellman-Ford算法 及 证明

    描述: 求图中某一点到其他任一点的最短距离. 操作: 1. 初始化 结果保存在一个dist数组里,源点的结果初始化为0,其他初始化为无穷大(如INT32_MAX). 2. 计算: 两重for循环,第一 ...

  9. 在线编辑器CKeditor,CKfinder

    在线编辑器的分类: 常见的在线编辑器有很多,比较常用的有FCKeditor(在线编辑器——Ajax 浏览器 端服务器文件管理器),CKeditor(在线编辑器与服务器端文件管理器的分离,) 其中CKe ...

  10. Python爬虫利器一之Requests库的用法

    前言 之前我们用了 urllib 库,这个作为入门的工具还是不错的,对了解一些爬虫的基本理念,掌握爬虫爬取的流程有所帮助.入门之后,我们就需要学习一些更加高级的内容和工具来方便我们的爬取.那么这一节来 ...