Caffe:训练网络准确度 = 1 常数!精度问题
Caffe : train network accuracy = 1 constant ! Accuracy issue
现在,我正在使用 2 class 数据训练网络...但第一次迭代后准确度保持不变!
输入数据为灰度图像。 class 图像都是在创建 HDF5Data 时随机选择的。
为什么会这样?哪里不对或错在哪里!
network.prototxt :
name: "brainMRI"
layer {
name: "data"
type: "HDF5Data"
top: "data"
top: "label"
include: {
phase: TRAIN
}
hdf5_data_param {
source: "/home/shivangpatel/caffe/brainMRI1/train_file_location.txt"
batch_size: 10
}
}
layer {
name: "data"
type: "HDF5Data"
top: "data"
top: "label"
include: {
phase: TEST
}
hdf5_data_param {
source: "/home/shivangpatel/caffe/brainMRI1/test_file_location.txt"
batch_size: 10
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 2
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "softmax"
type: "Softmax"
bottom: "ip2"
top: "smip2"
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "smip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
输出:
I0217 17:41:07.912580 2913 net.cpp:270] This network produces output loss
I0217 17:41:07.912607 2913 net.cpp:283] Network initialization done.
I0217 17:41:07.912739 2913 solver.cpp:60] Solver scaffolding done.
I0217 17:41:07.912789 2913 caffe.cpp:212] Starting Optimization
I0217 17:41:07.912813 2913 solver.cpp:288] Solving brainMRI
I0217 17:41:07.912832 2913 solver.cpp:289] Learning Rate Policy: inv
I0217 17:41:07.920737 2913 solver.cpp:341] Iteration 0, Testing net (#0)
I0217 17:41:08.235076 2913 solver.cpp:409] Test net output #0: accuracy = 0.98
I0217 17:41:08.235194 2913 solver.cpp:409] Test net output #1: loss = 0.0560832 (* 1 = 0.0560832 loss)
I0217 17:41:35.831647 2913 solver.cpp:341] Iteration 100, Testing net (#0)
I0217 17:41:36.140849 2913 solver.cpp:409] Test net output #0: accuracy = 1
I0217 17:41:36.140949 2913 solver.cpp:409] Test net output #1: loss = 0.00757247 (* 1 = 0.00757247 loss)
I0217 17:42:05.465395 2913 solver.cpp:341] Iteration 200, Testing net (#0)
I0217 17:42:05.775877 2913 solver.cpp:409] Test net output #0: accuracy = 1
I0217 17:42:05.776000 2913 solver.cpp:409] Test net output #1: loss = 0.0144996 (* 1 = 0.0144996 loss)
.............
.............
从评论中总结了一些信息:
- 您 运行 以 test_interval:100
次迭代的间隔进行测试。
- 每个测试间隔超过 test_iter:5
* batch_size:10
= 50 个样本。
- 你的训练集和测试集似乎非常nit:所有负样本(label=0)都在所有正样本之前组合在一起。
考虑一下您的 SGD 迭代求解器,您在训练期间将 batch_size:10
分批输入它。在任何正样本之前,您的训练集有 14,746 个负样本(即 1474 个批次)。因此,对于前 1474 次迭代,您的求解器只有 "sees" 个负样本,没有正样本。
您希望这个求解器学到什么?
问题
你的求解器只看到反例,因此知道无论输入是什么,它都应该输出“0”。您的测试集也以相同的方式排序,因此每个 test_interval 只测试 50 个样本,您只测试测试集中的负样本,结果准确率为 1.
但正如您所说,您的网络实际上什么也没学到。
解决方案
我想您现在已经猜到解决方案应该是什么了。您需要洗牌您的训练集,并在您的 整个 测试集上测试您的网络。
现在,我正在使用 2 class 数据训练网络...但第一次迭代后准确度保持不变!
输入数据为灰度图像。 class 图像都是在创建 HDF5Data 时随机选择的。
为什么会这样?哪里不对或错在哪里!
network.prototxt :
name: "brainMRI"
layer {
name: "data"
type: "HDF5Data"
top: "data"
top: "label"
include: {
phase: TRAIN
}
hdf5_data_param {
source: "/home/shivangpatel/caffe/brainMRI1/train_file_location.txt"
batch_size: 10
}
}
layer {
name: "data"
type: "HDF5Data"
top: "data"
top: "label"
include: {
phase: TEST
}
hdf5_data_param {
source: "/home/shivangpatel/caffe/brainMRI1/test_file_location.txt"
batch_size: 10
}
}
layer {
name: "conv1"
type: "Convolution"
bottom: "data"
top: "conv1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 20
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool1"
type: "Pooling"
bottom: "conv1"
top: "pool1"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "conv2"
type: "Convolution"
bottom: "pool1"
top: "conv2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
convolution_param {
num_output: 50
kernel_size: 5
stride: 1
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "pool2"
type: "Pooling"
bottom: "conv2"
top: "pool2"
pooling_param {
pool: MAX
kernel_size: 2
stride: 2
}
}
layer {
name: "ip1"
type: "InnerProduct"
bottom: "pool2"
top: "ip1"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 500
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "relu1"
type: "ReLU"
bottom: "ip1"
top: "ip1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "ip1"
top: "ip2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 2
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "softmax"
type: "Softmax"
bottom: "ip2"
top: "smip2"
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "ip2"
bottom: "label"
top: "loss"
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "smip2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
输出:
I0217 17:41:07.912580 2913 net.cpp:270] This network produces output loss I0217 17:41:07.912607 2913 net.cpp:283] Network initialization done. I0217 17:41:07.912739 2913 solver.cpp:60] Solver scaffolding done. I0217 17:41:07.912789 2913 caffe.cpp:212] Starting Optimization I0217 17:41:07.912813 2913 solver.cpp:288] Solving brainMRI I0217 17:41:07.912832 2913 solver.cpp:289] Learning Rate Policy: inv I0217 17:41:07.920737 2913 solver.cpp:341] Iteration 0, Testing net (#0) I0217 17:41:08.235076 2913 solver.cpp:409] Test net output #0: accuracy = 0.98 I0217 17:41:08.235194 2913 solver.cpp:409] Test net output #1: loss = 0.0560832 (* 1 = 0.0560832 loss) I0217 17:41:35.831647 2913 solver.cpp:341] Iteration 100, Testing net (#0) I0217 17:41:36.140849 2913 solver.cpp:409] Test net output #0: accuracy = 1 I0217 17:41:36.140949 2913 solver.cpp:409] Test net output #1: loss = 0.00757247 (* 1 = 0.00757247 loss) I0217 17:42:05.465395 2913 solver.cpp:341] Iteration 200, Testing net (#0) I0217 17:42:05.775877 2913 solver.cpp:409] Test net output #0: accuracy = 1 I0217 17:42:05.776000 2913 solver.cpp:409] Test net output #1: loss = 0.0144996 (* 1 = 0.0144996 loss) ............. .............
从评论中总结了一些信息:
- 您 运行 以 test_interval:100
次迭代的间隔进行测试。
- 每个测试间隔超过 test_iter:5
* batch_size:10
= 50 个样本。
- 你的训练集和测试集似乎非常nit:所有负样本(label=0)都在所有正样本之前组合在一起。
考虑一下您的 SGD 迭代求解器,您在训练期间将 batch_size:10
分批输入它。在任何正样本之前,您的训练集有 14,746 个负样本(即 1474 个批次)。因此,对于前 1474 次迭代,您的求解器只有 "sees" 个负样本,没有正样本。
您希望这个求解器学到什么?
问题
你的求解器只看到反例,因此知道无论输入是什么,它都应该输出“0”。您的测试集也以相同的方式排序,因此每个 test_interval 只测试 50 个样本,您只测试测试集中的负样本,结果准确率为 1.
但正如您所说,您的网络实际上什么也没学到。
解决方案
我想您现在已经猜到解决方案应该是什么了。您需要洗牌您的训练集,并在您的 整个 测试集上测试您的网络。