Keras中的自定义图层用于idct

时间:2018-05-28 04:30:55

标签: python machine-learning keras computer-vision

我正在尝试在Keras中为IDCT(反向离散余弦变换)编写自定义图层,因为与DCT相比,Keras中没有用于IDCT的内置函数。所以当我把我的图层写成:

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很陌生,希望能帮助我指出正确的方向。

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

1 个答案:

答案 0 :(得分:0)

您正在运行一项操作,该操作需要在张量上使用NumPy ndarray。不幸的是,这不起作用。您需要使用张量运算符重新实现自定义操作。

话虽如此,直接使用Tensorflow中的函数也是可以的,比如来自import tensorflow,并且使用自定义图层中的函数可能比Keras后端提供更多功能。