PaddlePaddle在预测图像时出现错误EnforceNotMet: Conv intput should be 4-D or 5-D tensor

12.问题:在预测图像时出现错误EnforceNotMet: Conv intput should be 4-D or 5-D tensor

  • 关键字:数据预处理预测图片

  • 问题描述:使用训练好的模型,预测图片。图片经过预处理之后,再调用预测接口infer()对图片进行预测,出现输入数据维度不正确。错误提示:EnforceNotMet: Conv intput should be 4-D or 5-D tensor

  • 报错信息:

EnforceNotMet                             Traceback (most recent call last)
<ipython-input-17-27415360cecb> in <module>
     23     place=place)
     24 
---> 25 results = inferencer.infer({'img': img})
     26 lab = np.argsort(results)
     27 print("Inference result of image/infer_3.png is: %d" % lab[0][0][-1])

/usr/local/lib/python3.5/dist-packages/paddle/fluid/contrib/inferencer.py in infer(self, inputs, return_numpy)
    102             results = self.exe.run(feed=inputs,
    103                                    fetch_list=[self.predict_var.name],
--> 104                                    return_numpy=return_numpy)
    105 
    106         return results

/usr/local/lib/python3.5/dist-packages/paddle/fluid/executor.py in run(self, program, feed, fetch_list, feed_var_name, fetch_var_name, scope, return_numpy, use_program_cache)
    468 
    469         self._feed_data(program, feed, feed_var_name, scope)
--> 470         self.executor.run(program.desc, scope, 0, True, True)
    471         outs = self._fetch_data(fetch_list, fetch_var_name, scope)
    472         if return_numpy:

EnforceNotMet: Conv intput should be 4-D or 5-D tensor. at [/paddle/paddle/fluid/operators/conv_op.cc:47]
PaddlePaddle Call Stacks: 
  • 问题复现:使用训练好保存的模型和网络结构创建一个预测器,然后图片经过预处理,再使用预处理后的图片进行预测,然后就出现错误。错误代码如下:
params_dirname = "recognize_digits_network.inference.model"
use_cuda = False  # set to True if training with GPU
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    
def load_image(file):
    im = Image.open(file).convert('L')
    im = im.resize((28, 28), Image.ANTIALIAS)
    im = np.array(im).astype(np.float32).flatten()
    im = im / 255.0
    return im
    
inferencer = Inferencer(
    infer_func=convolutional_neural_network,
    param_path=params_dirname,
    place=place)

img = load_image('infer_3.png')
results = inferencer.infer({'img': img})
lab = np.argsort(results)
print("Inference result of image/infer_3.png is: %d" % lab[0][0][-1])
  • 问题解决:上面错误的原因是数据预处理出错,上面数据预处理后的数据维度是(784,),但实际需要的数据维度是(1, 1, 28, 28),结构分别是Batch大小,图片通道数,图片的宽和图片的高。所以要通过reshape(1, 1, 28, 28)函数对图片维度进行变换。正确代码如下:
params_dirname = "recognize_digits_network.inference.model"
use_cuda = False  # set to True if training with GPU
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()

def load_image(file):
    im = Image.open(file).convert('L')
    im = im.resize((28, 28), Image.ANTIALIAS)
    im = np.array(im).reshape(1, 1, 28, 28).astype(np.float32)
    im = im / 255.0 * 2.0 - 1.0
    return im

img = load_image('infer_3.png')
inferencer = Inferencer(
    infer_func=convolutional_neural_network,
    param_path=params_dirname,
    place=place)

results = inferencer.infer({'img': img})
lab = np.argsort(results)
print("Inference result of image/infer_3.png is: %d" % lab[0][0][-1])
  • 问题拓展:在预测时的数据预处理,有些用户使用训练好的模型,在预测的时候没能预测正确的结果,这通常时数据预处理的问题。预测时对数据预处理的方式必须要跟训练的时候对数据预处理一样,这样模型才能正确预测数据。

猜你喜欢

转载自blog.csdn.net/PaddlePaddle/article/details/87359448