Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于tfkan中Conv3DKAN函数的使用 #15

Open
JunXinVan opened this issue Sep 3, 2024 · 4 comments
Open

关于tfkan中Conv3DKAN函数的使用 #15

JunXinVan opened this issue Sep 3, 2024 · 4 comments

Comments

@JunXinVan
Copy link

您好!在使用Conv3DKAN函数的过程中,我发现五维的输入经过函数处理后变为了四维,请问这是为什么?您辛苦了!

@ZPZhou-lab
Copy link
Owner

I'm not sure how you configure your Conv3DKAN layer.

The following is my code:

import tensorflow as tf
from tfkan.layers import Conv3DKAN

# x has (batch, spatial_1, spatial_2, spatial_3, channels) shape
x = tf.random.normal(shape=(1, 10, 32, 32, 3))
layer = Conv3DKAN(filters=8, kernel_size=3, strides=1)
y = layer(x)
print(y.shape)

and the output is

TensorShape([1, 8, 30, 30, 8])

which has 5D

@JunXinVan
Copy link
Author

当我运行您的示例时,我能得到同样的结果,但当我自己编写代码时,却总会遇到维度下降的问题:

def TokKANBlock(input_tensor, filters, kernel_size=(3, 3, 3), strides=(1, 1, 1)):
    # 第一层标准 3D 卷积
    x = tf.keras.layers.Conv3D(filters=filters, kernel_size=kernel_size, strides=strides, padding='same')(input_tensor)
    print(x.shape)

    # 第二层使用 KAN 的 3D 卷积
    x = Conv3DKAN(filters=filters, kernel_size=3, strides=1, padding="same")(x)
    print(x.shape)

    x = tf.keras.layers.Conv3D(filters=filters, kernel_size=kernel_size, strides=strides, padding='same')(x)
    print(x.shape)

    return x


def autoKAN(n_filters=16, n_classes=1, dropout_rate=0, grid_size=3, spline_order=3):
    inputs = tf.keras.layers.Input(shape=[None, None, None, 1])
    kan = TokKANBlock(inputs, filters=n_filters * 8, kernel_size=(3, 3, 3), strides=(1, 1, 1))


model1 = autoKAN()

#以下为运行结果

(None, None, None, None, 128)
(None, None, None, 128)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-9423babc311f> in <cell line: 21>()
     19 
     20 
---> 21 model1 = autoKAN()

3 frames
/usr/local/lib/python3.10/dist-packages/keras/src/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    251             ndim = x.shape.rank
    252             if ndim is not None and ndim < spec.min_ndim:
--> 253                 raise ValueError(
    254                     f'Input {input_index} of layer "{layer_name}" '
    255                     "is incompatible with the layer: "
    
ValueError: Input 0 of layer "conv3d_40" is incompatible with the layer: expected min_ndim=5, found ndim=4. Full shape received: (None, None, None, 128)

@ZPZhou-lab
Copy link
Owner

I have find where the problem is, we use the API of tensorflow to extract patches when processing 3D inputs(with (spatial_1, spatial_2, spatial_3) as follows:

patches = tf.extract_volume_patches(
    inputs,
    ksizes=[1, *self.kernel_size, 1],
    strides=[1, *self.strides, 1],
    padding=self.padding
)

I found that this API reduces one dimension when encountering undetermined tensors (virtual inputs created through tf.keras.layers.Input), resulting in the final output dimension not meeting expectations.😫

I will consider how to fix this issue. I'm sorry, before that, you can only use tensors of a certain size to initialize your model.

@JunXinVan
Copy link
Author

感谢您百忙中抽出时间进行回答,我理解了,您辛苦了!谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants