Skip to content

Commit e39a533

Browse files
committed
commit
1 parent 024160b commit e39a533

23 files changed

+4223
-1
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/c++/*.sdf
2+
/libgfortran-3.dll
3+
/libopenblas.dll
4+
/libquadmath-0.dll
5+
/_iter_122659.caffemodel
6+
/classification_dll.dll
7+
/libgcc_s_sjlj-1.dll
8+
/yzm1
9+
/CaffeLSTM-OCR.rar

CC3.1-alpha.5.ec

27.3 KB
Binary file not shown.

CaffeLSTM-OCR.rar

22.3 MB
Binary file not shown.

README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
# CaffeLSTM-OCR
2-
基于caffe的LSTM OCR案例,能够利用该案例完成序列的识别,包括验证码、车牌、身份证号码、地址等长序列动长的内容识别
2+
基于caffe的LSTM CTC OCR案例,能够利用该案例完成序列的识别,包括验证码、车牌、身份证号码、地址等长序列动长的内容识别<br/>
3+
这是一个resnet+blstm的例子,blstm是双向lstm的意思,resnet也只是采用了其中的126部分,丢掉了一大半<br/>
4+
5+
这个最大的贡献,是<br/>
6+
能够训练长序列的ocr识别,可以使用这个技术完成比如身份证号码、地址、车牌等识别任务<br/>
7+
8+
9+
lstm网络设计注意事项:<br/>
10+
1.保证CNN得到的featuremap输入到lstm时的宽度至少大于等于最大字符数的3倍左右,即time_step大于等于最大字符数3倍,否则小了不行<br/>
11+
2.如果是配合完整的resnet精度应该能够更好<br/>
12+
3.这次训练的精度为100%停止的,测试精度是97%左右,算是对复杂验证码OCR的一个证明,证明能力<br/>
13+
4.对于自己衔接网络,只要保证最后的time_step能配的上就不会有错<br/>
14+
5.训练过程中,如果出现难以收敛,把dropout层的dropout_ratio调低到0.5或者更低比如0.3甚至0,如果过拟合了,就调高,甚至可以0.7、0.9。当然默认是不要修改他,除非你也在研究<br/>
15+
6.lstm的num_output个数也影响精度,还有所谓的多层lstm也是可以有的<br/>
16+
17+
18+
<br/>
19+
里面的C++程序是纯无依赖的,只依赖[CCDL](https://github.com/dlunion/CCDL) <br/>
20+
21+
# 下载
22+
模型、演示图片、和依赖项<br/>
23+
[CaffeLSTM-OCR.rar](http://www.zifuture.com/fs/12.github/CaffeLSTM-OCR/CaffeLSTM-OCR.rar)

c++/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Release

c++/c++.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
2+
//这是一个lstm+cnn的ocr例子
3+
//2017年7月14日 12:23:54
4+
//wish
5+
6+
#include <vector>
7+
#include <string>
8+
#include <algorithm>
9+
#include <fstream>
10+
#include "classification-c.h"
11+
using namespace std;
12+
13+
#pragma comment(lib, "classification_dll.lib")
14+
15+
vector<char> readFile(const char* file){
16+
vector<char> data;
17+
FILE* f = fopen(file, "rb");
18+
if (!f) return data;
19+
20+
int len = 0;
21+
fseek(f, 0, SEEK_END);
22+
len = ftell(f);
23+
fseek(f, 0, SEEK_SET);
24+
25+
if (len > 0){
26+
data.resize(len);
27+
fread(&data[0], 1, len, f);
28+
}
29+
fclose(f);
30+
return data;
31+
}
32+
33+
vector<string> loadCodeMap(const char* file){
34+
ifstream infile(file);
35+
string line;
36+
vector<string> out;
37+
while (std::getline(infile, line)){
38+
out.push_back(line);
39+
}
40+
return out;
41+
}
42+
43+
string getLabel(const vector<string>& labelMap, int index){
44+
if (index < 0 || index >= labelMap.size())
45+
return "*";
46+
47+
return labelMap[index];
48+
}
49+
50+
void main(){
51+
//禁止caffe输出信息
52+
disableErrorOutput();
53+
54+
//注意目录是相对工程上级目录的
55+
Classifier* classif = createClassifier("deploy.prototxt", "_iter_122659.caffemodel");
56+
const char* imageFile = "yzm1/MAGYZ_10991.png";
57+
58+
vector<string> labelMap = loadCodeMap("label-map.txt");
59+
vector<char> data = readFile(imageFile);
60+
if (data.empty()){
61+
printf("文件不存在么? %s\n", imageFile);
62+
63+
releaseClassifier(classif);
64+
return;
65+
}
66+
67+
forward(classif, &data[0], data.size());
68+
69+
70+
BlobData* premuted_fc = getBlobData(classif, "premuted_fc");
71+
float* ptr = premuted_fc->list;
72+
int blank_label = 32;
73+
int time_step = 19;
74+
int alphabet_size = 33;
75+
int prev_label = blank_label;
76+
string result, result_raw;
77+
78+
for (int i = 0; i < time_step; ++i){
79+
float* lin = ptr + i * alphabet_size;
80+
int predict_label = std::max_element(lin, lin + alphabet_size) - lin;
81+
float value = lin[predict_label];
82+
83+
if (predict_label != blank_label && predict_label != prev_label){
84+
result = result + getLabel(labelMap, predict_label);
85+
}
86+
87+
result_raw = result_raw + getLabel(labelMap, predict_label);
88+
prev_label = predict_label;
89+
}
90+
91+
releaseBlobData(premuted_fc);
92+
printf("识别的结果是:\n%s\n%s\n", result.c_str(), result_raw.c_str());
93+
releaseClassifier(classif);
94+
}

c++/c++.sln

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.21005.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "c++", "c++.vcxproj", "{A7463756-0F71-48B7-9D0D-98EAB717A8F7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Win32 = Debug|Win32
11+
Release|Win32 = Release|Win32
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{A7463756-0F71-48B7-9D0D-98EAB717A8F7}.Debug|Win32.ActiveCfg = Debug|Win32
15+
{A7463756-0F71-48B7-9D0D-98EAB717A8F7}.Debug|Win32.Build.0 = Debug|Win32
16+
{A7463756-0F71-48B7-9D0D-98EAB717A8F7}.Release|Win32.ActiveCfg = Release|Win32
17+
{A7463756-0F71-48B7-9D0D-98EAB717A8F7}.Release|Win32.Build.0 = Release|Win32
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

c++/c++.v12.suo

18.5 KB
Binary file not shown.

c++/c++.vcxproj

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{A7463756-0F71-48B7-9D0D-98EAB717A8F7}</ProjectGuid>
15+
<Keyword>Win32Proj</Keyword>
16+
<RootNamespace>c</RootNamespace>
17+
</PropertyGroup>
18+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
19+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
20+
<ConfigurationType>Application</ConfigurationType>
21+
<UseDebugLibraries>true</UseDebugLibraries>
22+
<PlatformToolset>v120</PlatformToolset>
23+
<CharacterSet>Unicode</CharacterSet>
24+
</PropertyGroup>
25+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
26+
<ConfigurationType>Application</ConfigurationType>
27+
<UseDebugLibraries>false</UseDebugLibraries>
28+
<PlatformToolset>v120</PlatformToolset>
29+
<WholeProgramOptimization>true</WholeProgramOptimization>
30+
<CharacterSet>Unicode</CharacterSet>
31+
</PropertyGroup>
32+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
33+
<ImportGroup Label="ExtensionSettings">
34+
</ImportGroup>
35+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
36+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
37+
</ImportGroup>
38+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
39+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
40+
</ImportGroup>
41+
<PropertyGroup Label="UserMacros" />
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
43+
<LinkIncremental>true</LinkIncremental>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
46+
<LinkIncremental>false</LinkIncremental>
47+
</PropertyGroup>
48+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
49+
<ClCompile>
50+
<PrecompiledHeader>
51+
</PrecompiledHeader>
52+
<WarningLevel>Level3</WarningLevel>
53+
<Optimization>Disabled</Optimization>
54+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
55+
<SDLCheck>true</SDLCheck>
56+
</ClCompile>
57+
<Link>
58+
<SubSystem>Console</SubSystem>
59+
<GenerateDebugInformation>true</GenerateDebugInformation>
60+
</Link>
61+
</ItemDefinitionGroup>
62+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
63+
<ClCompile>
64+
<WarningLevel>Level3</WarningLevel>
65+
<PrecompiledHeader>
66+
</PrecompiledHeader>
67+
<Optimization>MaxSpeed</Optimization>
68+
<FunctionLevelLinking>true</FunctionLevelLinking>
69+
<IntrinsicFunctions>true</IntrinsicFunctions>
70+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
71+
<SDLCheck>true</SDLCheck>
72+
</ClCompile>
73+
<Link>
74+
<SubSystem>Console</SubSystem>
75+
<GenerateDebugInformation>true</GenerateDebugInformation>
76+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
77+
<OptimizeReferences>true</OptimizeReferences>
78+
</Link>
79+
</ItemDefinitionGroup>
80+
<ItemGroup>
81+
<ClCompile Include="c++.cpp" />
82+
</ItemGroup>
83+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
84+
<ImportGroup Label="ExtensionTargets">
85+
</ImportGroup>
86+
</Project>

c++/c++.vcxproj.filters

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="源文件">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
</ItemGroup>
9+
<ItemGroup>
10+
<ClCompile Include="c++.cpp">
11+
<Filter>源文件</Filter>
12+
</ClCompile>
13+
</ItemGroup>
14+
</Project>

c++/c++.vcxproj.user

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4+
<LocalDebuggerWorkingDirectory>../</LocalDebuggerWorkingDirectory>
5+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
6+
</PropertyGroup>
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8+
<LocalDebuggerWorkingDirectory>../</LocalDebuggerWorkingDirectory>
9+
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
10+
</PropertyGroup>
11+
</Project>

0 commit comments

Comments
 (0)