0

我正在尝试对 caffe2 模型进行预测,使用 caffe2 附带的脚本从 caffe 转换。脚本正确终止,给了我“init_net.pb”和“predict_net.pb”。

当我尝试调用 Predictor 方法时run(),我收到以下错误消息:

Traceback (most recent call last):
  File "caffe2Inference.py", line 23, in <module>
    results = p.run(input)
RuntimeError: [enforce fail at conv_op_impl.h:21] X.ndim() == filter.ndim(). 3 vs 4 Error from operator: 
input: "data" input: "conv1/time_w" input: "conv1/time_b" output: "conv1/time" type: "Conv" arg { name: "stride" i: 1 } arg { name: "pad" i: 0 } arg { name: "kernel_h" i: 7 } arg { name: "kernel_w" i: 1 }

这是我执行推理的代码:

# load up the caffe2 workspace
from caffe2.python import workspace, core
import numpy as np

# helper image processing functions
#import caffe2.python.tutorials.helpers as helpers
name= "scnn+fc/"
INIT_NET = name+"/init_net.pb"
PREDICT_NET = name+"/predict_net.pb"
#read protobuf
with open(INIT_NET,"rb") as f:
    init_net = f.read()
with open(PREDICT_NET,"rb") as f:
    predict_net = f.read()

# you must name it something
workspace.RunNetOnce(init_net)
workspace.CreateNet(predict_net)
p = workspace.Predictor(init_net, predict_net)

input = np.ones((1,1,63,13))
# submit the image to net and get a tensor of results
results = p.run(input)
res= np.asarray(results)
#response = helpers.parseResults(results)
# and lookup our result from the list

这是我原来的 .prototxt

name: "UNIPINET"
#input:"data"
#input_shape: { dim: 1 dim: 1 dim: 63 dim: 13 } 
layer { name: "data" 
type: "Input" 
top: "data" 
input_param
{ 
  shape 
  {
    dim: 1 
    dim: 3 
    dim: 224 
    dim: 224 
   } 
  } 
}
layer {
  name: "conv1/time"
  type: "Convolution"
  bottom: "data"
  top: "conv1/time"
  convolution_param {
    num_output: 1
    bias_term: true
    kernel_h: 7 
    kernel_w: 1
    #engine: CAFFE
    stride: 1
  }
}

layer {
  name: "conv1/freq"
  type: "Convolution"
  bottom: "conv1/time"
  top: "conv1/freq"
  convolution_param {
    num_output: 32
    bias_term: true
    kernel_h:1
    kernel_w:3
    stride: 1
  }
}
layer {
  name: "conv1/freq/bn"
  type: "BatchNorm"
  bottom: "conv1/freq"
  top: "conv1/freq/bn"
}
layer {
  name: "conv1/freq/bn/scale"
  type: "Scale"
  bottom: "conv1/freq/bn"
  top: "conv1/freq/bn/scale"
  scale_param{
    bias_term: true
  }
}
layer {
  name: "relu1"
  type: "ReLU"
  bottom: "conv1/freq/bn/scale"
  top: "relu1"
}
layer {
  name: "conv2/time"
  type: "Convolution"
  bottom: "relu1"
  top: "conv2/time"
  convolution_param {
    num_output: 32
    bias_term: true
    kernel_h: 15
      kernel_w: 1
    pad_h:7
    #engine: CAFFE
    stride: 1
  }
}

layer {
  name: "conv2/freq"
  type: "Convolution"
  bottom: "conv2/time"
  top: "conv2/freq"
  convolution_param {
    num_output: 32
    bias_term: true
    kernel_h:1
      kernel_w:3
    pad_w:1
    stride: 1
  }
}
layer {
  name: "conv2/freq/bn"
  type: "BatchNorm"
  bottom: "conv2/freq"
  top: "conv2/freq/bn"
}
layer {
  name: "conv2/freq/bn/scale"
  type: "Scale"
  bottom: "conv2/freq/bn"
  top: "conv2/freq/bn/scale"
  scale_param{
    bias_term: true
  }
}
layer {
  name: "relu2"
  type: "ReLU"
  bottom: "conv2/freq/bn/scale"
  top: "relu2"
}
layer {
  name: "conv3/time"
  type: "Convolution"
  bottom: "relu2"
  top: "conv3/time"
  convolution_param {
    num_output: 32
    bias_term: true
    kernel_h: 13
      kernel_w: 1
    pad_h:6
    #engine: CAFFE
    stride: 1
  }
}

layer {
  name: "conv3/freq"
  type: "Convolution"
  bottom: "conv3/time"
  top: "conv3/freq"
  convolution_param {
    num_output: 128
    bias_term: true
    kernel_h:1
      kernel_w:3
    pad_w:1
    stride: 1
  }
}
layer {
  name: "conv3/freq/bn"
  type: "BatchNorm"
  bottom: "conv3/freq"
  top: "conv3/freq/bn"
}
layer {
  name: "conv3/freq/bn/scale"
  type: "Scale"
  bottom: "conv3/freq/bn"
  top: "conv3/freq/bn/scale"
  scale_param{
    bias_term: true
  }
}
layer {
  name: "relu3"
  type: "ReLU"
  bottom: "conv3/freq/bn/scale"
  top: "relu3"
}

layer {
  name: "avg_pool"
  type: "Pooling"
  bottom: "relu3"
  top: "avg_pool"
  pooling_param {
    pool: MAX
    global_pooling: true
  }
}
layer {
  name: "fc"
  type: "InnerProduct"
  bottom: "avg_pool"
  top: "fc"
  inner_product_param {
    num_output: 12
  }
}

layer {
  name: "output"
  type: "Softmax"
  bottom: "fc"
  top: "output"
}
4

1 回答 1

0

p.run预期类型是输入列表。

改变:

p.run(input)

经过:

p.run([input.astype(np.float32)])
于 2018-06-29T12:45:03.130 回答