通道交换需要与 caffe 中的输入通道错误具有相同的维数
Posted
技术标签:
【中文标题】通道交换需要与 caffe 中的输入通道错误具有相同的维数【英文标题】:Channel swap needs to have the same number of dimensions as the input channels error in caffe 【发布时间】:2016-03-15 03:52:25 【问题描述】:我是 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 proto文件的模型
我运行了以下命令,将我的图像作为测试模型的输入:
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.
我没有更改任何文件,除了我添加了这些代码行的分类.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
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:我发现问题出在哪里,实际上caffe中的classify.py是默认为imagenet模型编写的,并以3维通道作为输入。然而MNIST 模型需要一维通道作为输入。所以当我传递图像时,它向我显示了这个尺寸不匹配错误,因此必须编写必要的代码才能使这个 MNIST caffemodel 工作。
https://github.com/BVLC/caffe/pull/2359/commits/dd3a5f9268ca3bdf19a17760bd6f568e21c1b521
所有需要编写的内容都已在此链接中给出。希望对您有所帮助。
【讨论】:
以上是关于通道交换需要与 caffe 中的输入通道错误具有相同的维数的主要内容,如果未能解决你的问题,请参考以下文章