0

我用自己的数据训练了我的网络,这不是图像,而是矢量(20×128×128)。现在我想使用训练好的网络进行预测。但是,当我尝试代码时,无论输入向量是什么,我都会得到相同的输出。我将附上我用过的文件。任何意见,将不胜感激。

import cv2
import numpy as np
import os
import caffe
import lmdb
import leveldb
from caffe.proto import caffe_pb2

caffe_root = '../'
import sys
sys.path.insert(0, caffe_root + 'python')

def read_txt(file_path):
    X = np.empty((20,128,128),dtype=np.float)
    if os.path.exists(file_path) == False:
       return X
    f = open(file_path)
    lines2 = f.readlines()
    f.close()

    count = -1
    for line in lines2:
       count = count + 1
       i = count % 130
       k = count//130
       if i <= 127 and k<20:
           words = line.split(',')
           for j in range(0,128):
           X[k,i,j]=float(words[j])/500000

   return X

MODEL_FILE = '************/try_bn_deploy.prototxt'
PRETRAINED = '*********/bn__iter_20000.caffemodel'
base_path = '*************/database/txt/'

f = open('*****************/val.txt')
line = f.readline()
caffe.set_mode_cpu()
net = caffe.Net(MODEL_FILE, PRETRAINED,caffe.TEST)

count = 0
correct = 0
while line:
  count += 1

  # load feature vector
  pair = line.strip().split()
  image_path = pair[0]
  label = pair[1]
  X=np.empty((20,128,128),dtype=np.float)
  X[:,:,:] = read_txt(base_path + image_path)
  net.blobs['data'].data[...] = X
  # forcast
  out = net.forward()
  plabel = int(out['prob'][0].argmax())
  print count, label, plabel
  line = f.readline()
  f.close`

这是我的部署文件

  name: "Net"
  input: "data"
  input_shape {
     dim: 1
     dim: 20
     dim: 128
     dim: 128
  }
  ...
  layer {
     name: "prob"
     type: "Softmax"
     bottom: "fc8"
     top: "prob"
  }
4

1 回答 1

0

当您调用net.forward()它时,它将所有层替换为 prototxt 指定的数据层,从而使您的调用net.blobs['data'].data[...] = X无效。要修复此运行:

net.forward(data=np.asarray(X))

这应该会给你正确的预测。

于 2016-09-08T14:07:39.930 回答