Python Caffe:尽可能简单地从快照预测图像
有没有办法轻松地使用Caffe快照来预测新图像 我所说的“容易”是指:Python Caffe:尽可能简单地从快照预测图像,python,neural-network,deep-learning,caffe,conv-neural-network,Python,Neural Network,Deep Learning,Caffe,Conv Neural Network,有没有办法轻松地使用Caffe快照来预测新图像 我所说的“容易”是指: 不得手动将train.prototxt更改为deploy.prototxt 无需将文件转换为LMDB(或其他类似格式),只需使用简单的图像文件(无任何文件名列表或内容) 仅使用python代码(不使用CLI) 我已经基于Caffe MNIST示例训练了一个二进制分类网络。我将MNIST改为用于分类2个类,它训练得非常好。但现在,我已经完成了网络培训并生成了快照(包含“Snapshot.caffemodel”和“solver.
train.prototxt
更改为deploy.prototxt
File "C:\Anaconda2\lib\site-packages\caffe\classifier.py", line 29, in __init__ in_ = self.inputs[0]
IndexError: list index out of range
搜索之后,我发现我不应该使用“train.prototxt”,而应该使用“deploy.prototxt”
Caffe目前处理事情的方式有时似乎过于复杂,特别是对于一些琐碎的任务,如使用快照预测图像。。。
也许我做错了…您确实需要手动将train\u val.prototxt
更改为deploy.protoxt
然而,这种改变比你想象的要容易 将
train_val.prototxt
复制到新的deploy.prototxt
并按照以下步骤编辑deploy.prototxt
:
第一个变化:输入。
您需要告诉caffe为稍后手动提供的图像分配内存,而不是使用培训/验证数据集(通常表示为“数据”
/“HDF5Data”
/“图像数据”
层)。为此,您需要删除现有的输入层(用于训练和测试阶段),并将其替换为:
layer {
name: "input"
type: "Input"
top: "data" # or whatever was the name of the "top" of the training input. no need for "label" top - you do not have ground truth labels in test.
input_param { shape: { dim: 1 dim: 3 dim: 100 dim: 100 } } # give caffe the expected dimensions of your input for memory allocation
}
第二个变化:净产出。
在培训过程中,您的净输出是损失,而不是预测。因此,首先删除所有损失层(特别是希望将
标记为“底部”的任何层)。这包括“SoftmaxWithLoss”
/“准确性”
/“SigmoidCrossEntropyLoss”
等。
您需要用适当的预测层替换损耗层。例如,“SoftmaxWithLoss”
层应替换为简单的“Softmax”
层,“SigmoidCrossEntropy”
层应替换为“Sigmoid”
层等等。
因此,如果你有
layer {
type: "SoftmaxWithLoss"
name: "loss"
bottom: "fc8" # we need this name !
bottom: "label"
...
}
替换为:
layer {
name: "prob"
type: "Softmax"
bottom: "fc8" # the SAME as the train loss layer!
top: "prob"
}
保存更改,现在您就有了一个正确的deploy.prototxt
有关更多信息,请参阅帖子。谢谢您的回答。。。我还没有尝试过,但我最终会尝试,并将发生的事情发布在这里。。。祝你好运。。。
layer {
name: "prob"
type: "Softmax"
bottom: "fc8" # the SAME as the train loss layer!
top: "prob"
}