caffe图像分类教程_Caffe 图像分类

本文主要描述如何使用 CAFFE 进行图像分类。

开发环境要求: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 = newTrainer(projectPath);//创建训练数据(首次必须创建,后续可以不再重新创建)

bool bRebuildData = true;if(bRebuildData)

{//分类信息

ListlistLabel = new List();

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");//将用于训练的图像信息加入到列表中

ListlistTrain = new List();

listTrain.Add(new ImageSampleInfo() { LabelValue = 0, FileName = "4e1b2156a4d548b690b9630f6ca2f8aa.bmp"});//...//将用于测试的图像信息加入到列表中

ListlistTest = new List();

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 = newPredictor(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);

}

文章来源: www.cnblogs.com,作者:Wagwei,版权归原作者所有,如需转载,请联系作者。

原文链接:https://www.cnblogs.com/wang_xy/p/11972681.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值