开发环境要求:windows 10 64位、Visual Studio 2017、.NET framework 4.6.1




分类
在一个项目的图像分类中,存在N个分类,每个分类需要有足量训练样本图像和测试样本图像。
训练
定义分类(标签)
分类的标签值 LabelValue 要求从0开始且连续增量为1

样本管理
用于管理每个分类的样本图像。

配置训练参数
主要用于修改基本配置、训练参数等。


注意:loss值越小代表训练效果越好,上图的loss=1.09699表示训练非常失败


//当前程序的根目录
string pathRoot = AppDomain.CurrentDomain.BaseDirectory;
//demo项目的根目录
string projectPath = Path.Combine(pathRoot, "DemoProject");
//图像分类的训练器
Trainer trainer = new Trainer(projectPath);
//创建训练数据(首次必须创建,后续可以不再重新创建)
bool bRebuildData = true;
if (bRebuildData)
{
//分类信息
List<LabelInfo> listLabel = new List<LabelInfo>();
listLabel.Add(new LabelInfo() { LabelValue = 0, LabelName = "b黑色" });
listLabel.Add(new LabelInfo() { LabelValue = 1, LabelName = "w白色" });
listLabel.Add(new LabelInfo() { LabelValue = 2, LabelName = "n无螺丝" });
//...
//存储样本图像的根目录
string imagePath = Path.Combine(pathRoot, "DemoProject", "Images");
//将用于训练的图像信息加入到列表中
List<ImageSampleInfo> listTrain = new List<ImageSampleInfo>();
listTrain.Add(new ImageSampleInfo() { LabelValue = 0, FileName = "4e1b2156a4d548b690b9630f6ca2f8aa.bmp" });
//...
//将用于测试的图像信息加入到列表中
List<ImageSampleInfo> listTest = new List<ImageSampleInfo>();
listTest.Add(new ImageSampleInfo() { LabelValue = 0, FileName = "0e48c295e2ab4eac85429ba94efa12c7.bmp" });
//...
//调用创建方法
trainer.CreateImageDataFile(
listLabel,
imagePath,
listTrain,
listTest,
false);
}
//设置基本参数
trainer.ProjectSettings.resize_width = 71;
trainer.ProjectSettings.resize_height = 71;
trainer.ProjectSettings.gray = true;
//...
trainer.SaveProjectSettings();
//设置训练参数
trainer.solver_args.max_iter = 10;
trainer.solver_args.snapshot = 10;
trainer.solver_args.type = type.SGD;
trainer.solver_args.solver_mode = solver_mode.CPU;
//...
trainer.SaveSolver();
//调用训练方法
trainer.Train();
//等待训练进程
Thread.Sleep(1000 * 3);
string trainProcessName = "caffe";
while (true)
{
var array = Process.GetProcessesByName(trainProcessName);
if (array.Length > 0)
{
Debug.WriteLine($"进程还存在 {trainProcessName}");
//Application.DoEvents();
Thread.Sleep(500);
}
else
{
Debug.WriteLine($"进程已杀死 {trainProcessName}");
break;
}
}//end while
MessageBox.Show("训练完成!");

预测
当训练完成之后,根据训练得到的一些模型等文件,就可对后续的图像进行预测分类。



//当前程序的根目录
string pathRoot = AppDomain.CurrentDomain.BaseDirectory;
//demo项目的根目录
string projectPath = Path.Combine(pathRoot, "DemoProject");
string fileDeploy = Path.Combine(projectPath, "current_deploy.prototxt");
string fileCaffeModel = Directory.GetFiles(projectPath, "*.caffemodel").FirstOrDefault();
string fileLabel = Path.Combine(projectPath, "labels.txt");
string filePredictionArgs = Path.Combine(projectPath, "prediction_args.json");
//预测器
Predictor predictor = new Predictor(fileDeploy, fileCaffeModel, fileLabel, filePredictionArgs);
//待预测图像,图像数据需要转到Mat中,来源可以是:(1)图像文件(2)相机采集
Mat matImage = null;
//来源于图像文件
string fileImage = Path.Combine(projectPath, "demo.bmp");
matImage = Cv2.ImRead(fileImage, ImreadModes.Grayscale);
//预测结果
PredictResults results = null;
results = predictor.Predict(matImage);
//或者 results = predictor.Predict(fileImage);
if (results.Highest != null)
{
string show = $"LabelValue={results.Highest.LabelValue}, LabelName={results.Highest.LabelName}, Score={results.Highest.Score}";
Debug.WriteLine(show);
}
