这个错误意味着什么“无法将<class'tuple'>类型的对象转换为张量”?

2024-03-29 00:00:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Keras中有一个网络,我尝试对一个层的输出进行一些更改,然后将其馈送给剩余的网络。在apply function中,它接受一个具有形状(batch size,1,32,32)的张量,并且所有张量的格式都是channel_first。然后用形状为(8,8,1,64)的张量卷积得到(?)?,64,4,4),但当我想将形状更改为(8,8,4,4)时,会产生以下错误。为什么会发生这种错误?因为当我尝试在没有网络的情况下用张量检查代码时,它是有效的!我也在这里加上了代码。你能告诉我有什么问题吗?在

    def apply_conv(self, image, filter_type: str):

        if filter_type == 'dct':
            filters = self.dct_conv_weights
        elif filter_type == 'idct':
            filters = self.idct_conv_weights
        else:
            raise('Unknown filter_type value.')
        print(image.shape)

        image_conv_channels = []
        for channel in range(image.shape[1]):
            print(image.shape)
            print(channel)
            image_yuv_ch = K.expand_dims(image[:, channel, :, :],1)
            print( image_yuv_ch.shape)
            print(filters.shape)
            image_conv = Kr.backend.conv2d(image_yuv_ch,filters,strides=(8,8),data_format='channels_first')
            print(image_conv.shape)
            print('salam')
#            image_conv = Kr.backend.permute_dimensions(image_conv,(0, 2, 3, 1))
            image_conv = Kr.backend.reshape(image_conv,(image_conv.shape[0], 8,8,image_conv.shape[2], image_conv.shape[3]))
            print(image_conv.shape)
            image_conv =  Kr.backend.permute_dimensions(image_conv,(0, 1, 3, 2, 4))
            image_conv = Kr.backend.reshape(image_conv,(image_conv.shape[0],
                                                  image_conv.shape[1]*image_conv.shape[2],
                                                  image_conv.shape[3]*image_conv.shape[4]))

#            Kr.backend.expand_dims(image_conv,1)

            # image_conv = F.conv2d()
            image_conv_channels.append(image_conv)

        image_conv_stacked = Kr.backend.concatenate(image_conv_channels, axis=1)

        return image_conv_stacked

Traceback (most recent call last):

File "", line 376, in decoded_noise=JpegCompression()(act11)#16

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", line 457, in call output = self.call(inputs, **kwargs)

File "", line 162, in call image_dct = self.apply_conv(noised_image, 'dct')

File "", line 126, in apply_conv image_conv = Kr.backend.reshape(image_conv,(image_conv.shape[0], 8,8,image_conv.shape[2], image_conv.shape[3]))

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\backend\tensorflow_backend.py", line 1969, in reshape return tf.reshape(x, shape)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 6482, in reshape "Reshape", tensor=tensor, shape=shape, name=name)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 513, in _apply_op_helper raise err

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 510, in _apply_op_helper preferred_dtype=default_dtype)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\ops.py", line 1146, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 229, in _constant_tensor_conversion_function return constant(v, dtype=dtype, name=name)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant value, dtype=dtype, shape=shape, verify_shape=verify_shape))

File "D:\software\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 531, in make_tensor_proto "supported type." % (type(values), values))

TypeError: Failed to convert object of type to Tensor. Contents: (Dimension(None), 8, 8, Dimension(4), Dimension(4)). Consider casting elements to a supported type.


Tags: inpyimagebackendlibpackageslinesite