首页 > 解决方案 > 使用 OpenCV 时如何在 NN 中使用 keras Lambda

问题描述

我有一个功能

def preprocess(image):
  image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
  image = cv2.GaussianBlur(image,  (3, 3), 0)
  image = cv2.resize(image, (200, 66))
  return image

我想在这样的 NN 的 Lambda 层中使用这个函数

  model=Sequential()

  model.add(Lambda(preprocess,input_shape=(160,320,3)))
  #model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160,320,3)))
  model.add(Lambda(lambda x: (x / 255.0) - 0.5))
  model.add(Cropping2D(cropping=((60,25), (0,0))))

但是我得到了错误

model = nvidia_model()
print(model.summary())

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-9f6554917809> in <module>()
----> 1 model = nvidia_model()
      2 print(model.summary())

6 frames
<ipython-input-9-5e5ec9bc70c2> in preprocess(image)
      1 def preprocess(image):
      2   
----> 3   image = cv2.cvtColor(image, cv2.COLOR_RGB2YUV)
      4   image = cv2.GaussianBlur(image,  (3, 3), 0)
      5   image = cv2.resize(image, (200, 66))

TypeError: Expected Ptr<cv::UMat> for argument '%s'

如何将此功能用作NN的一层?(请注意,仅使用 lambda x:(x/255.0) 效果很好。

注意:我不想在外部这样做。(因为我已经这样做了,所以效果很好,但现在我正在尝试在内部进行)

标签: pythonopencvkeraskeras-layer

解决方案


推荐阅读