c++ - 了解 Caffe 中的输入维度、SoftmaxWithLoss 和标签

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

我正在尝试将我自己训练的网络与我自己的 C++ 数据结合使用。我使用 ImageData 层在“.jpg”数据上训练和测试了网络,然后实现了基本的 caffe 示例“classification.cpp”以通过内存一张一张地传递图像。结果我需要知道 2 个类的概率:
1 - 对象,
2 - 环境。

我的常规输入层看起来像:

layer {
    name: "data"
    top:  "data"
    top:  "label"
    type: "Input"
    input_param { shape: { dim: 1 dim: 3 dim: 256 dim: 256 }}
}

输出层:

layer {
    name: "fc6"
    top:  "fc6"
    type: "InnerProduct"
    bottom: "drop5"
    inner_product_param {
        num_output: 2
        weight_filler {
            type: "xavier"
            std: 0.1
        }
    }
}

layer {
    name: "prob"
    top:  "prob"
    type: "SoftmaxWithLoss"
    bottom: "fc6"
    bottom: "label"
}

layer {
    name: "accuracy"
    top:  "accuracy"
    type: "Accuracy"
    bottom: "fc6"
    bottom: "label"
    include {
        phase: TEST
    }
}

在测试阶段,网络已经达到accuracy=0.93,但现在在C++中正常使用,我无法弄清楚一些基本概念,并在解析模型时出错。

Check failure stack trace:
...
caffe::SoftmaxWithLossLayer<>::Reshape()
caffe::Net<>::Init()
caffe::Net<>::Net()
...
Check failed: outer_num_ * inner_num_ == bottom[1]->count() (1 vs. 196608) Number of labels must match number of predictions; e.g., if softmax axis == 1 and prediction shape is (N, C, H, W), label count (number of labels) must be N*H*W, with integer values in {0, 1, ..., C-1}.

好的,1x3x256x256 = 196608,但为什么我需要这个标签计数? 我有一个文件“labels.txt”,如示例“classification.cpp”:

environment
object

为什么标签 != 类? 我应该如何处理 SoftmaxWithLoss 和输入维度?

最佳答案

您没有为标签定义 shape,我假设您每张图片只有一个标签。因此

layer {
  name: "data"
  top:  "data"
  top:  "label"
  type: "Input"
  input_param { shape: { dim: 1 dim: 3 dim: 256 dim: 256 }
                shape: { dim: 1 dim: 1 }}  # one label per image
}

关于c++ - 了解 Caffe 中的输入维度、SoftmaxWithLoss 和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38371118/

相关文章:

c++ - 为什么在这种情况下使用范围 for 循环会产生与使用常规 for 循环不同的输出?

python - 如何更改A3C Tensorflow示例来玩Atari游戏?

python - 递归 Python 神经网络 - Reshape () 错误

c++ - Qt 资源中的 QML 文件未找到文件错误

c++ - 标准容器元素的常量正确性

c++ - 在NTFS压缩目录中,如何读取文件压缩和未压缩的大小?

matlab - 在 Matlab/Octave 中实现神经网络

python - TensorFlow:EVAL 和 INFER 之间的区别

python - 是否可以在多输入神经网络中使用 sklearn 中的 StratifiedKFold?

python - 如何在 Google Colab 中的另一个虚拟机上拍摄和恢复模型训练的快照?