caffe训练(2)生成train.txt、val.txt及test.txt文件的分类命令流

在进行caffe训练前需要生成train.txt文件,caffe图像分类训练流程见博客,本文提供了另外一种生成txt文件的命令流。

#!/usr/bin/env sh
DATA=examples/myfile4/data    #数据来源的路径 
MY=examples/myfile4/data      #生成的txt文件存储位置
 
echo "Create train.txt..."
rm -rf $MY/train.txt          #先把文件中原有的同名文件删除
	
find $DATA/train -name 00000*.jpg | cut -d '/' -f4-5 | sed "s/$/ 0/">>$MY/train.txt    
find $DATA/train -name 00001*.jpg | cut -d '/' -f4-5 | sed "s/$/ 1/">>$MY/train.txt
find $DATA/train -name 00002*.jpg | cut -d '/' -f4-5 | sed "s/$/ 2/">>$MY/train.txt
find $DATA/train -name 00003*.jpg | cut -d '/' -f4-5 | sed "s/$/ 3/">>$MY/train.txt
find $DATA/train -name 00004*.jpg | cut -d '/' -f4-5 | sed "s/$/ 4/">>$MY/train.txt
find $DATA/train -name 00005*.jpg | cut -d '/' -f4-5 | sed "s/$/ 5/">>$MY/train.txt
 
echo "Create val.txt..."
rm -rf $MY/val.txt
 
find $DATA/val -name 00000*.jpg | cut -d '/' -f4-5 | sed "s/$/ 0/">>$MY/val.txt
find $DATA/val -name 00001*.jpg | cut -d '/' -f4-5 | sed "s/$/ 1/">>$MY/val.txt
find $DATA/val -name 00002*.jpg | cut -d '/' -f4-5 | sed "s/$/ 2/">>$MY/val.txt
find $DATA/val -name 00003*.jpg | cut -d '/' -f4-5 | sed "s/$/ 3/">>$MY/val.txt
find $DATA/val -name 00004*.jpg | cut -d '/' -f4-5 | sed "s/$/ 4/">>$MY/val.txt
find $DATA/val -name 00005*.jpg | cut -d '/' -f4-5 | sed "s/$/ 5/">>$MY/val.txt
 
echo "All done"
  • find:$DATA/val表示数据来源的路径文件夹
  • name:其中15001、15059等表示图片名称前缀,初始命名时表示某类
  • cut -d ‘/’ -f4-5:表示先按符号“/”分段,分段后结果中,-f 表示取第几个字段
  • 例子:
    其中例如一个图片的绝对路径为‘/home/ww/xc/newbuild/train/00001_0.696860780225.jpg’
    -f1 在.txt文件中显示 0
    -f3-4 在.txt中显示 ww/xc 0
    -f6-7 在.txt中显示 train/00001_0.683245042607.jpg 0
    因此-f后面数字如何确定基本应该清楚了,第一个符号“/”前定位符为1。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中使用MyCaffe调用train.caffemodel和prototxt文件的步骤与使用mean.binaryproto文件类似。您可以使用MyCaffe提供的BlobProto和NetParameter类来加载这些文件,并将它们转换为C#中的对象。 以下是一个示例代码片段,展示了如何在C#中使用MyCaffe加载train.caffemodel和prototxt文件: ```csharp using MyCaffe; using MyCaffe.basecode; using MyCaffe.common; using MyCaffe.param; using MyCaffe.proto; using System.Collections.Generic; namespace MyCaffeTest { class Program { static void Main(string[] args) { // Load the prototxt file NetParameter net_param = new NetParameter(); net_param.CopyFrom(LayerParameter.ParseFrom(File.ReadAllBytes("train.prototxt"))); // Load the caffemodel file BlobProto blob_proto = new BlobProto(); blob_proto.Load("train.caffemodel"); // Convert the BlobProto object to a Blob object BlobCollection<float> colBlobs = new BlobCollection<float>(); Blob<float> blob = new Blob<float>(new List<int> { 1, 3, 256, 256 }); blob.FromProto(blob_proto); colBlobs.Add(blob); // Create the solver SolverParameter solver_param = new SolverParameter(); solver_param.net_param = net_param; solver_param.test_iter.Add(0); solver_param.test_interval = 1; solver_param.base_lr = 0.01; solver_param.momentum = 0.9; solver_param.weight_decay = 5e-4; solver_param.lr_policy = "inv"; solver_param.gamma = 0.1; solver_param.power = 0.75; solver_param.display = 1; solver_param.max_iter = 1; solver_param.snapshot = 1; solver_param.snapshot_prefix = "snapshot"; solver_param.type = SolverParameter.SolverType.SGD; // Create the solver and solve the net MyCaffeControl<float> mycaffe = new MyCaffeControl<float>(null, null, null, colBlobs, null, solver_param, null, null, null, null); mycaffe.Solve(); System.Console.ReadKey(); } } } ``` 在上面的代码中,我们使用了MyCaffe提供的NetParameter和BlobProto类,分别加载了train.prototxttrain.caffemodel文件。我们还创建了一个SolverParameter对象,并使用NetParameter对象初始化它。最后,我们使用这些对象创建了一个MyCaffeControl对象,并使用Solve函数来训练网络。 希望这可以帮助您解决问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值