Keras中的IDct自定义图层 [英] Custom Layer in Keras for idct

查看:93
本文介绍了Keras中的IDct自定义图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Keras中为IDCT(逆余弦逆变换)编写一个自定义层,因为与DCT相比,在Keras中没有IDCT的内置功能.因此,当我将图层写为:

I am trying to write a custom layer in Keras for IDCT (Inverse Discrete Cosine Transform) as there is no built-in function in Keras for IDCT as compared to DCT. So when I write my layer as:

model = Sequential()
model.add(Conv2D(512,1,activation='relu', input_shape= (8,8,64) ))
model.add(Lambda( lambda x: get_2d_idct_tensor(x) ) )

我的功能定义为:

def get_2d_idct_tensor(coefficients):
   return fftpack.idct(K.transpose(fftpack.idct(K.transpose(coefficients), norm='ortho')), norm='ortho')

我收到以下错误:

----> 9 model.add(Lambda( lambda x: get_2d_idct_tensor(x) ) )
 10 
 11 #model.add(Lambda(lambda x: K.tf.spectral.dct(K.transpose(K.tf.spectral.dct(K.transpose(x), type=2, norm='ortho')), norm='ortho'),input_shape=(8, 8, 512),output_shape=(8, 8, 1) ))

/usr/local/lib/python3.6/dist-packages/keras/models.py in add(self, layer)
520                           output_shapes=[self.outputs[0]._keras_shape])
521         else:
--> 522             output_tensor = layer(self.outputs[0])
523             if isinstance(output_tensor, list):
524                 raise TypeError('All layers in a Sequential model '

/usr/local/lib/python3.6/dist-packages/keras/engine/topology.py in __call__(self, inputs, **kwargs)
617 
618             # Actually call the layer, collecting output(s), mask(s), and shape(s).
--> 619             output = self.call(inputs, **kwargs)
620             output_mask = self.compute_mask(inputs, previous_mask)
621 

/usr/local/lib/python3.6/dist-packages/keras/layers/core.py in call(self, inputs, mask)
683         if has_arg(self.function, 'mask'):
684             arguments['mask'] = mask
--> 685         return self.function(inputs, **arguments)
686 
687     def compute_mask(self, inputs, mask=None):

<ipython-input-14-dae1f7021aae> in <lambda>(x)
  7 model.add(Conv2D(512,1,activation='relu', input_shape= (8,8,64) ))
  8 
----> 9 model.add(Lambda( lambda x: get_2d_idct_tensor(x) ) )
 10 
 11 #model.add(Lambda(lambda x: K.tf.spectral.dct(K.transpose(K.tf.spectral.dct(K.transpose(x), type=2, norm='ortho')), norm='ortho'),input_shape=(8, 8, 512),output_shape=(8, 8, 1) ))

<ipython-input-7-9ac404754077> in get_2d_idct_tensor(coefficients)
 12     """ Get 2D Inverse Cosine Transform of Image
 13     """
---> 14     return fftpack.idct(K.transpose(fftpack.idct(K.transpose(coefficients), norm='ortho')), norm='ortho')
 15 
 16 def get_reconstructed_image(img):

/usr/local/lib/python3.6/dist-packages/scipy/fftpack/realtransforms.py in idct(x, type, n, axis, norm, overwrite_x)
200     # Inverse/forward type table
201     _TP = {1:1, 2:3, 3:2}
--> 202     return _dct(x, _TP[type], n, axis, normalize=norm, overwrite_x=overwrite_x)
203 
204 

/usr/local/lib/python3.6/dist-packages/scipy/fftpack/realtransforms.py in _dct(x, type, n, axis, overwrite_x, normalize)
279 
280     """
--> 281     x0, n, copy_made = __fix_shape(x, n, axis, 'DCT')
282     if type == 1 and n < 2:
283         raise ValueError("DCT-I is not defined for size < 2")

/usr/local/lib/python3.6/dist-packages/scipy/fftpack/realtransforms.py in __fix_shape(x, n, axis, dct_or_dst)
224 
225 def __fix_shape(x, n, axis, dct_or_dst):
--> 226     tmp = _asfarray(x)
227     copy_made = _datacopied(tmp, x)
228     if n is None:

/usr/local/lib/python3.6/dist-packages/scipy/fftpack/basic.py in _asfarray(x)
125     already an array with a float dtype, and do not cast complex types to
126     real."""
--> 127     if hasattr(x, "dtype") and x.dtype.char in numpy.typecodes["AllFloat"]:
128         # 'dtype' attribute does not ensure that the
129         # object is an ndarray (e.g. Series class

AttributeError: 'DType' object has no attribute 'char'

有人可以解释一下试图说什么错误以及为什么引起该错误吗?我对Keras并不陌生,希望获得一些帮助,使我朝正确的方向发展.

Can someone please explain what is the error trying to say and why is it caused? I am pretty new to Keras and would like some help to point me in the right direction.

提前感谢您的时间和帮助...

Thanks in advance for your time and help...

推荐答案

您正在运行一个期望张量为NumPy ndarray的操作.不幸的是,这是行不通的.您只需要使用 张量运算符来重新实现自定义操作.

You are running an operation which expects a NumPy ndarray on tensors. Unfortunately this will not work. You need to reimplement the custom operation using only tensor operators.

说过,直接使用Tensorflow中的函数也可以,例如import tensorflow中的函数,并在自定义层中使用这些函数,可能会比单独提供Keras后端给您更多的功能.

Having said that, using functions from Tensorflow directly is OK as well, say from import tensorflow and use those inside a custom layer might give you more functions than Keras backend alone.

这篇关于Keras中的IDct自定义图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆