Machine learning Caffe:如何使用已经训练好的模型一次检查多个数据集的准确性?

Machine learning Caffe:如何使用已经训练好的模型一次检查多个数据集的准确性?,machine-learning,neural-network,deep-learning,caffe,Machine Learning,Neural Network,Deep Learning,Caffe,在Caffe框架中使用2类10k图像对LeNet模型进行训练后,得到了包含权重和偏差的LeNet_iter_4000.CAFFEM模型。我想一次检查5k新测试图像(训练时不属于测试图像的一部分)的准确性。我为所有这些5k图像创建了lmdb文件。我知道如何使用以下方法测试图像 ./build/tools/caffe test --model=examples/mnist/lenet_train_test.prototxt --weights=examples/mnist/lenet_iter_40

在Caffe框架中使用2类10k图像对LeNet模型进行训练后,得到了包含权重和偏差的LeNet_iter_4000.CAFFEM模型。我想一次检查5k新测试图像(训练时不属于测试图像的一部分)的准确性。我为所有这些5k图像创建了lmdb文件。我知道如何使用以下方法测试图像

./build/tools/caffe test --model=examples/mnist/lenet_train_test.prototxt --weights=examples/mnist/lenet_iter_4000.caffemodel
但是我无法在训练时获得准确度,如果我们将测试间隔
3000
,我们将在
3000
迭代后获得所有测试图像的准确度。如果我想在训练后的某个时间测试准确性,我必须对prototxt进行更改。
我的问题是:在使用经过训练的模型进行训练之后,如何获得多个数据集的精度

一个快速解决方案是在训练时获得多个数据集的精确度。您可以通过如下方式修改solver.prototxt和net.prototxt来实现这一点,更具体地说,就是使用solver.prototxt中的多个“test_state”和net.prototxt中具有不同“include:{stage:“xxx”}”的多个数据层来测试多个数据集:

solver.prototxt:

net: "lenet_train_test.prototxt"
#testing stage for your orininal test images
test_state: { stage: "original test" }
#testing stage for your new test images
test_state: { stage: "new 5k test" }
#iterations for original testing
test_iter: xxx
#iterations for your new 5k testing
test_iter: xxx
#Those 2 testings use the same test_interval
test_interval: 500
name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"
    batch_size: 32
    backend: LMDB
    shuffle: true
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
    stage: "original test"
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
    stage: "new 5k test"
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "path/to/your/5k_images_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
.
.
.
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}
对应的net.prototxt:

net: "lenet_train_test.prototxt"
#testing stage for your orininal test images
test_state: { stage: "original test" }
#testing stage for your new test images
test_state: { stage: "new 5k test" }
#iterations for original testing
test_iter: xxx
#iterations for your new 5k testing
test_iter: xxx
#Those 2 testings use the same test_interval
test_interval: 500
name: "LeNet"
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"
    batch_size: 32
    backend: LMDB
    shuffle: true
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
    stage: "original test"
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST
    stage: "new 5k test"
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "path/to/your/5k_images_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
.
.
.
layer {
  name: "accuracy"
  type: "Accuracy"
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}
并将其作为一种微调方式:

./build/tools/caffe train --solver=examples/mnist/solver.prototxt --weights=examples/mnist/lenet_iter_4000.caffemodel

迭代0时,解算器将测试多个数据集,您将获得它们的多个精度,然后可以停止解算器。

只需使用caffe的测试工具,检查

我在这里晚了一点,但是你们中那些想知道如何使用经过训练的模型进行测试的人可以使用caffe的测试工具。如果您训练了您的模型,并计划在以后使用它,那么您应该在每次迭代时保存权重。让我们将最终保存的权重模型命名为\u iter\u xxxx.caffemodel

  • 将模型复制到model_test.prototxt(例如)

  • 添加测试阶段需要考虑的层,即测试数据的输入层和准确性层

  • 运行:
    caffe测试-模型路径/to/model\u test.prototxt-权重路径/to/model\u iter\u xxxx.caffemodel-迭代次数100

  • 我想在使用经过训练的模型进行训练后获得多个数据集的精度。您的问题的快速解决方案可以是这样的:/build/tools/caffe train--solver=examples/mnist/lenet_solver.prototxt--weights=examples/mnist/lenet_iter\u 4000.caffemodel。lenet_solver.prototxt和相应的net.prototxt应该像我的答案中那样进行修改。我理解,但我已经训练了数小时的模型。我想使用该模型进行测试,为了节省时间,我给了LeNet一个例子。但是我已经训练了AlexNet模型一天。我想使用训练过的AlexNet模型进行测试。在迭代0时,解算器将测试你的2个数据集,包括你的5k图像,你将得到它们的精度。然后你可以阻止它。这是我能想到的最快的方法。否则,您应该修改caffe.cpp中的test()函数,就像Solver::Init()所做的那样,以维护多个网络进行测试,这将有点琐碎@BHAV247用于测试的Alexnet模型的解决方案与lenet相同,只需通过添加“test_state”修改sovler.prototxt,并通过为5k测试图像再添加一个数据层修改Alexnet.prototxt即可。测试时间一点也不长@BHAV247