通道交换需要与 caffe 中的输入通道错误具有相同的维数

Channel swap needs to have the same number of dimensions as the input channels error in caffe

我是 caffe 的新手,因此尝试使用 MNIST 数据集构建模型,我执行了以下说明:

./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
./examples/mnist/train_lenet.sh

在最后一条指令结束时,控制台上打印了一系列输出,最后几行是:

I0315 08:46:40.755457  2430 sgd_solver.cpp:106] Iteration 9900, lr = 0.00596843
I0315 08:46:48.754673  2430 solver.cpp:454] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodel
I0315 08:46:48.761627  2430 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate
I0315 08:46:48.832245  2430 solver.cpp:317] Iteration 10000, loss = 0.00262804
I0315 08:46:48.835681  2430 solver.cpp:337] Iteration 10000, Testing net (#0)
I0315 08:46:54.016222  2430 solver.cpp:404]     Test net output #0: accuracy = 0.991
I0315 08:46:54.016377  2430 solver.cpp:404]     Test net output #1: loss = 0.0300069 (* 1 = 0.0300069 loss)
I0315 08:46:54.016427  2430 solver.cpp:322] Optimization Done.
I0315 08:46:54.016474  2430 caffe.cpp:222] Optimization Done.

我现在尝试使用在 examples/mnist/lenet_iter_10000.caffemodel

中创建为 binay 原型文件的模型

i 运行 以下命令将我的图像作为测试模型的输入:

python python/classify.py --print_results --model_def examples/mnist/lenet.prototxt --pretrained_model examples/mnist/lenet_iter_10000.caffemodel --force_grayscale --center_only --labels_file data/mnist/mnist_words.txt --images_dim 28,28 /home/kishan/Desktop/gray1.jpg foo

我的图片是256*256的灰度图(256*256的RGB图我也试过)

但是出现错误说:

Traceback (most recent call last):
  File "python/classify.py", line 154, in <module>
    main(sys.argv)
  File "python/classify.py", line 126, in main
    channel_swap=channel_swap)
  File "/home/kishan/deep-learning/caffe/python/caffe/classifier.py", line 40, in __init__
    self.transformer.set_channel_swap(in_, channel_swap)
  File "/home/kishan/deep-learning/caffe/python/caffe/io.py", line 216, in set_channel_swap
    raise Exception('Channel swap needs to have the same number of '
Exception: Channel swap needs to have the same number of dimensions as the input channels.

我没有更改任何文件,除了 classify.py 我在其中添加了以下代码行:

 parser.add_argument(
        "--labels_file",
        default=os.path.join(pycaffe_dir,
        "../data/ilsvrc12/synset_words.txt"),
        help="Readable label definition file."
    )
    parser.add_argument(
        "--print_results",
        action='store_true',
        help="Write output text to stdout rather than serializing to a file."
    )
    parser.add_argument(
        "--force_grayscale",
        action='store_true',
        help="Converts RGB images down to single-channel grayscale versions useful for single-channel networks like MNIST."
    )

我的文件结构为 ./examples/mnist :

mnist/
|-- convert_mnist_data.cpp
|-- create_mnist.sh
|-- lenet_adadelta_solver.prototxt
|-- lenet_auto_solver.prototxt
|-- lenet_consolidated_solver.prototxt
|-- lenet_iter_10000.caffemodel
|-- lenet_iter_10000.solverstate
|-- lenet_iter_5000.caffemodel
|-- lenet_iter_5000.solverstate
|-- lenet_multistep_solver.prototxt
|-- lenet.prototxt
|-- lenet_solver_adam.prototxt
|-- lenet_solver.prototxt
|-- lenet_solver_rmsprop.prototxt
|-- lenet_train_test.prototxt
|-- mnist_autoencoder.prototxt
|-- mnist_autoencoder_solver_adadelta.prototxt
|-- mnist_autoencoder_solver_adagrad.prototxt
|-- mnist_autoencoder_solver_nesterov.prototxt
|-- mnist_autoencoder_solver.prototxt
|-- mnist_test_lmdb
|   |-- data.mdb
|   `-- lock.mdb
|-- mnist_train_lmdb
|   |-- data.mdb
|   `-- lock.mdb
|-- readme.md
|-- train_lenet_adam.sh
|-- train_lenet_consolidated.sh
|-- train_lenet_docker.sh
|-- train_lenet_rmsprop.sh
|-- train_lenet.sh
|-- train_mnist_autoencoder_adadelta.sh
|-- train_mnist_autoencoder_adagrad.sh
|-- train_mnist_autoencoder_nesterov.sh
`-- train_mnist_autoencoder.sh

./data/mnist 的文件结构:

mnist/
|-- get_mnist.sh
|-- t10k-images-idx3-ubyte
|-- t10k-labels-idx1-ubyte
|-- train-images-idx3-ubyte
`-- train-labels-idx1-ubyte

如有任何帮助,我们将不胜感激。

我想通了是什么问题,实际上caffe中的classify.py默认是为imagenet模型编写的,并以3维通道作为输入。然而MNIST模型需要一维通道作为输入。所以当我传递图像时,它向我显示了这个尺寸不匹配错误,因此必须编写必要的代码才能使这个 MNIST caffemodel 工作。

https://github.com/BVLC/caffe/pull/2359/commits/dd3a5f9268ca3bdf19a17760bd6f568e21c1b521

需要写的都在这个link里给了。希望对你有帮助。