python-Keras中的idct自定义层
作者:互联网
我试图在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并不陌生,希望获得一些帮助,将我的方向指向正确.
预先感谢您的时间和帮助…
解决方法:
您正在运行一个期望张量为NumPy ndarray的操作.不幸的是,这是行不通的.您只需要使用张量运算符即可重新实现自定义操作.
话虽如此,直接使用Tensorflow的功能也可以,例如从import tensorflow导入并在自定义层中使用这些功能可能比单独的Keras后端给您更多的功能.
标签:python,keras,machine-learning,computer-vision 来源: https://codeday.me/bug/20191014/1912400.html