c++ - Caffe classifocation.cpp 始终返回 100% 概率

标签 c++ neural-network deep-learning caffe conv-neural-network

我正在尝试使用 Caffe C++ classification示例(这里是 code)用手写数字对图像进行分类(我在 MNIST 数据库上训练我的模型),但它总是返回像

这样的概率
[0, 0, 0, 1.000, 0, 0, 0, 0, 0]  (1.000 can be on different position)

即使图像上没有数字。我觉得应该是这样的

[0.01, 0.043, ... 0.9834, ... ]

此外,例如对于“9”,它总是预测错误的数字。
我在 classification.cpp 中唯一改变的是我一直在使用 CPU

//#ifdef CPU_ONLY    
  Caffe::set_mode(Caffe::CPU); // <----- always CPU
//#else
//  Caffe::set_mode(Caffe::GPU);
//#endif

这就是我的 deploy.prototxt 的样子

name: "LeNet"
layer {
  name: "data"
  type: "ImageData"
  top: "data"
  top: "label"
  image_data_param {
    source: "D:\\caffe-windows\\examples\\mnist\\test\\file_list.txt"
  }
}
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: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "loss"
  type: "Softmax"
  bottom: "ip2"
  top: "loss"
}    

file_list.txt 是

D:\caffe-windows\examples\mnist\test\test1.jpg 0

而tests1.jpg是这样的

enter image description here

(黑白 28*28 图像保存在 paint 中,我尝试了不同的尺寸但没关系,Preprocces() 无论如何都会调整它的大小)

为了训练网络,我使用 this教程,这里是prototxt

那么为什么它总是以 100% 的概率预测错误的数字?

(我使用的是 windows 7,VS13)

最佳答案

在您的“ImageData”层中,您应该通过“缩放”将 test1.jpg 数据从 [0, 255] 归一化为 [0, 1],以保持训练和测试之间预处理方式的一致性,如下所示:

image_data_param {
    source: "D:\\caffe-windows\\examples\\mnist\\test\\file_list.txt"
    scale: 0.00390625
  }

关于c++ - Caffe classifocation.cpp 始终返回 100% 概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38188578/

相关文章:

c++ - 使用结构化绑定(bind)定义类成员

c++ - 如何将 "mean_file_proto"读入 OpenCV Mat 以便从均值中减去图像以用于 Caffe 模型?

neural-network - 使用 Gekko 的大脑模块,我如何确定使用多少层和什么类型的层来解决深度学习问题?

opencv - 为 Caffe 缩放 OpenCV Mat

C++ 在处理视频数据和视频设备方面的优势

c++ - 为什么bazel不搜索系统包含路径

machine-learning - 如何实现数字识别?

python - 没有所有连接的神经网络层

deep-learning - Faster-RCNN bbox/图像标准化

python - 相对于重复函数的梯度