【问题标题】:Caffe execution咖啡执行
【发布时间】:2023-03-03 21:28:01
【问题描述】:

我开始使用caffe 进行深度学习。我有.caffemodel 文件,其中包含我训练的权重和特定的神经网络。我正在使用python接口。

我发现我可以通过这样做来加载我的网络和我的权重:

solver=caffe.get_solver('prototxtfile.prototxt')
solver.net.copy_from('weights.caffemodel')

但我不想微调我的应用程序。我只想使用这些权重。我想执行网络,并且对于 Imagenet 数据集中的每个图像,我想获得分类的结果(而不是整个批次的准确性)。我该怎么做?

非常感谢。

【问题讨论】:

  • 首先加载您的网络和源网络,然后将源权重分配给您的网络并保存您修改后的网络(像surgery 示例一样,然后只需将您的输入加载到网络和转发网络( net.forward)

标签: python deep-learning caffe


【解决方案1】:

尝试理解附加的 Python 代码行并根据您的需要进行调整。这不是我的代码,但我写了一篇类似的文章来测试我的模型。
来源是: https://www.cc.gatech.edu/~zk15/deep_learning/classify_test.py

如果您不想微调预训练模型,显然您不需要求解器。求解器是优化模型的东西。如果要预测图像的类别概率,实际上只需要进行前向传递。请记住,您的 deploy.prototxt 必须有一个适当的最后一层,该层使用 softmax 或 sigmoid 函数(取决于架构)。您不能为此使用 train_val.prototxt 中的损失函数。

import numpy as np
import matplotlib.pyplot as plt

# Make sure that caffe is on the python path:
caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + 'python')

import caffe

# Set the right path to your model definition file, pretrained model weights,
# and the image you would like to classify.
MODEL_FILE = '../models/bvlc_reference_caffenet/deploy.prototxt'
PRETRAINED = '../models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'
IMAGE_FILE = 'images/cat.jpg'

caffe.set_mode_cpu()
net = caffe.Classifier(MODEL_FILE, PRETRAINED,
                       mean=np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1),
                       channel_swap=(2,1,0),
                       raw_scale=255,
                       image_dims=(256, 256))
input_image = caffe.io.load_image(IMAGE_FILE)
plt.imshow(input_image)

prediction = net.predict([input_image])  # predict takes any number of images, and formats them for the Caffe net automatically
print 'prediction shape:', prediction[0].shape
plt.plot(prediction[0])
print 'predicted class:', prediction[0].argmax()
plt.show()

【讨论】:

  • 感谢您的回复。图像尺寸会发生什么?在该代码中,参数 image_dims 为 256x256,如果图像不符合代码会怎样?
  • 您只需使用名为 SciImage 的包调整它们的大小。不要为此使用 caffe 的操作。您还可以更改输入尺寸,只需确保您的模型针对您的尺寸进行了训练并在传递到网络之前调整它们的大小。
  • 如果它解决了您的问题,请您接受答案吗?
  • 他/她可以使用 Caffe 操作来调整大小。事实上,Caffe 有很多有用的预处理操作可以应用于输入数据(参见Transformer classhere)。当您说 SciImage 时,您是指 scikit-image 吗?
【解决方案2】:

这是我需要通过网络转发图像时使用的代码:

import caffe

caffe.set_mode_cpu()             #If you are using CPU
#caffe.set_mode_gpu()            #or if you are using GPU

model_def = "path/to/deploy.prototxt"         #architecture
model_weights = "path/to/weights.caffemodel"  #weights

net = caffe.Net(model_def,      # defines the structure of the model
                model_weights,
                caffe.TEST)     # use test mode (e.g., don't perform dropout)


#Let's forward a single image (let's say inputImg)
#'data' is the name of my input blob
net.blobs["data"].data[0] = inputImg
out = net.forward()

# to get the final softmax probability
# in my case, 'prob' is the name of our last blob
# a softmax layer that will output the score/probability for our problem
outputScore = net.blobs["prob"].data[0]   #[0] here because we forwarded a single image

在此示例中,inputImg 尺寸必须与训练期间使用的图像尺寸相匹配,以及所有预处理完成。

【讨论】:

    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 2013-08-04
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2015-09-24
    • 2013-01-09
    相关资源
    最近更新 更多