我是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.
我没有更改任何文件,除了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
任何帮助都将不胜感激。
答案 0 :(得分:1)
我弄清楚问题是什么,实际上caffe中的classify.py是默认为imagenet模型编写的,并采用3维通道作为输入。但是 MNIST模型需要一维通道作为输入 。因此,当我传递图像时,它向我显示了这个尺寸不匹配错误,因此必须编写必要的代码才能使这个MNIST caffemodel工作。
https://github.com/BVLC/caffe/pull/2359/commits/dd3a5f9268ca3bdf19a17760bd6f568e21c1b521
此链接中提供了所有需要编写的必要内容。希望它有所帮助。