[Python] Increasing dimensions in middle of keras model structure. 2D to 3D

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm basically trying to teach a CNN to detect simple wavelets (and their offsets) in low-res-ish images (30x48px).

    The results of my model were not great, so I tried to apply some heuristics into the model design, and I came up with an idea, where I'd first have a conv2D (layer 1) with big kernel, lets say 5x15, which should in theory fit one wavelet, therefore be able to kinda detect the wavelet. (mby bold assumption). But to detect the wavelets, since they are not always exactly same, I made number of the kernels to be 30. I'd like to follow this with another conv. layer, however I dont think the number of different kernels does not need to be 30, I think 5 should suffice.

    So my idea was, to have conv3D layer (layer 2), which would take values of the different filtered layers after L1 and combine them, but it could have different kernel for each of the filtered layers.

    One flaw would be, that the filtered layers in new dimension, lets call it z (og image having X,Y ) no longer have any "positional information". Because Z dim is just stack of filters, and I have no control about the order of the filters. However I still want to keep the X,Y information. My workaround for this is to have conv3D layer with kernel (26,34,30), so there is only one valid position for this kernel in L1 Z index.

    model = keras.Sequential(
    [
    keras.layers.Input(shape=input_shape),
    keras.layers.Conv2D(filters=30, kernel_size=(5,15), activation='relu'),
    keras.layers.Conv3D(filters=5, kernel_size=(26,34,30), activation='relu'),
    keras.layers.MaxPool2D((1,5)),
    keras.layers.Flatten(),
    keras.layers.Dense(15, activation = 'relu')
    ...


    However this does not work, as the conv3D needs 5D input, and I've only 4D input. I failed to figure out, how, or even if it is possible to add reshape or upscale layer into the model stackup.

    This does not seem to work, as I need the array "x" (input data), which i do not (or cannot) have at the time of defining the model. See bellow:

    model = keras.Sequential(
    [
    keras.layers.Input(shape=input_shape),
    keras.layers.Conv2D(filters=30, kernel_size=(5,15), activation='relu'),
    keras.layers.Reshape(-1,26,34,30) #(x)
    keras.layers.Conv3D(filters=5, kernel_size=(26,34,30), activation='relu'),
    keras.layers.MaxPool2D((1,5)),
    keras.layers.Flatten(),
    keras.layers.Dense(15, activation = 'relu')
    ...


    TLDR: Can I somehow use Reshape layer in model stackup to increase the array size from 4D to 5D in middle of the model?

    (honestly any good sources about CNN desing are welcome, I feel like I'm missing something)

    Thank you

    Continue reading...

Compartilhe esta Página