如何在Keras Lambda层中使用OpenCV函数? [英] How to use OpenCV functions in Keras Lambda Layer?
问题描述
我正在尝试使用在图像上使用某些OpenCV函数的函数.但是我获取的数据是张量,我无法将其转换为图像.
I am trying to use a function that uses some OpenCV function on the image. But the data I am getting is a tensor and I am not able to convert it into an image.
def image_func(img):
img=cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
img=cv2.resize(img,(200,66))
return img
model=Sequential()
model.add(Lambda(get_ideal_img,input_shape=(r,c,ch),output_shape=(r,c,ch)))
当我运行此代码片段时,它会在cvtColor
函数中引发错误,指出img
不是numpy数组.我打印出img
,它似乎是张量.
When I run this snippet it throws an error in the cvtColor
function saying that img
is not a numpy array. I printed out img
and it seemed to be a tensor.
我不知道如何将张量更改为图像,然后再返回张量.我希望模型具有这一层.
I do not know how to change the tensor to an image and then return the tensor as well. I want the model to have this layer.
如果我不能通过lambda层实现此目的,我还能做什么?
If I cannot achieve this with a lambda layer what else can I do?
推荐答案
您将Lambda
层中的符号运算与python函数中的数值运算混淆了.
You confused with the symbolic operation in the Lambda
layer with the numerical operation in a python function.
基本上,您的自定义操作接受数字输入,但不接受符号输入.要解决此问题,您需要的是py_func
>
Basically, your custom operation accepts numerical inputs but not symbolic ones. To fix this, what you need is something like py_func
in tensorflow
此外,您尚未考虑反向传播.简而言之,尽管该层是非参数且不可学习的,但您还需要注意其梯度.
In addition, you have not considered the backpropagation. In short, although this layer is non-parametric and non-learnable, you need to take care of its gradient as well.
import tensorflow as tf
from keras.layers import Input, Conv2D, Lambda
from keras.models import Model
from keras import backend as K
import cv2
def image_func(img):
img=cv2.cvtColor(img,cv2.COLOR_BGR2YUV)
img=cv2.resize(img,(200,66))
return img.astype('float32')
def image_tensor_func(img4d) :
results = []
for img3d in img4d :
rimg3d = image_func(img3d )
results.append( np.expand_dims( rimg3d, axis=0 ) )
return np.concatenate( results, axis = 0 )
class CustomLayer( Layer ) :
def call( self, xin ) :
xout = tf.py_func( image_tensor_func,
[xin],
'float32',
stateful=False,
name='cvOpt')
xout = K.stop_gradient( xout ) # explicitly set no grad
xout.set_shape( [xin.shape[0], 66, 200, xin.shape[-1]] ) # explicitly set output shape
return xout
def compute_output_shape( self, sin ) :
return ( sin[0], 66, 200, sin[-1] )
x = Input(shape=(None,None,3))
f = CustomLayer(name='custom')(x)
y = Conv2D(1,(1,1), padding='same')(x)
model = Model( inputs=x, outputs=y )
print model.summary()
现在,您可以使用一些虚拟数据来测试该层.
Now you can test this layer with some dummy data.
a = np.random.randn(2,100,200,3)
b = model.predict(a)
print b.shape
model.compile('sgd',loss='mse')
model.fit(a,b)
这篇关于如何在Keras Lambda层中使用OpenCV函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!