Caffe执行


梦中会飞的鱼
2025-03-27 05:38:19 (16天前)


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

我见过我可以加载我的网络……

3 条回复
  1. 0# 雪浴冰灵 | 2019-08-31 10-32



    尝试理解附加的python代码行并根据您的需要进行调整。这不是我的代码,但我写了一篇类似的文章来测试我的模型。

    来源是:

    1. <a href="https://www.cc.gatech.edu/~zk15/deep_learning/classify_test.py" rel="nofollow noreferrer">
    2. https://www.cc.gatech.edu/~zk15/deep_learning/classify_test.py
    3. </A>



    如果您不想微调预先训练过的模型,很明显您不需要求解器。求解器是优化模型的原因。如果你想预测图像的类概率,你实际上只需要进行正向传递。请记住,deploy.prototxt必须具有适当的最后一层,该层使用softmax或sigmoid函数(取决于体系结构)。您不能使用train_val.prototxt中的loss函数。




    1. import numpy as np
      import matplotlib.pyplot as plt

    2. 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’)

    3. import caffe

    4. 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

    5. 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)

    6. 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()

    7. </code>

  2. 1# 烏鴉喝酒 | 2019-08-31 10-32



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




    1. import caffe

    2. caffe.set_mode_cpu() #If you are using CPU

    3. caffe.set_mode_gpu() #or if you are using GPU

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

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

    5. Lets forward a single image (lets say inputImg)

      data is the name of my input blob

      net.blobs[“data”].data[0] = inputImg
      out = net.forward()

    6. 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

    7. </code>


    在这个例子中,

    inputImg

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


登录 后才能参与评论