//当前程序的根目录
string pathRoot =AppDomain.CurrentDomain.BaseDirectory;//demo项目的根目录
string projectPath = Path.Combine(pathRoot, "DemoProject");//图像分类的训练器
Trainer trainer = newTrainer(projectPath);//创建训练数据(首次必须创建,后续可以不再重新创建)
bool bRebuildData = true;if(bRebuildData)
{//分类信息
List listLabel = 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");//将用于训练的图像信息加入到列表中
List listTrain = new List();
listTrain.Add(new ImageSampleInfo() { LabelValue = 0, FileName = "4e1b2156a4d548b690b9630f6ca2f8aa.bmp"});//...//将用于测试的图像信息加入到列表中
List listTest = 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("训练完成!");