[Python] Why isn't my keras model throwing and error when different sizes are passed to the...

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a dynamic time series multi-class segmentation problem with keras (tensorflow version 2.12.0), and I wanted to see what would happen when I dropped in a dense layer into the network architecture. My expectation is that for any situation where the input size changes (e.g., dynamic time series), you'll need to include some kind of pooling layer that will maintain a fixed input shape into the dense layer. I managed to get it working without any pooling layers, and I'm wondering why I am NOT getting an error at the dense layer when passing dynamically sized inputs into the network.

    Note: I am using 2d operations to maintain compatibility with opencv

    Model code:

    def residual_block(input_layer, filters=64, kernel_size=(1, 2), strides=1):
    conv_x = keras.layers.Conv2D(
    filters=filters, kernel_size=kernel_size, padding="same"
    )(input_layer)
    conv_x = keras.layers.Activation("relu")(conv_x)

    conv_y = keras.layers.Conv2D(
    filters=filters, kernel_size=kernel_size, padding="same"
    )(conv_x)
    conv_y = keras.layers.Activation("relu")(conv_y)

    conv_z = keras.layers.Conv2D(
    filters=filters, kernel_size=kernel_size, padding="same"
    )(conv_y)

    # expand channels for the sum
    shortcut_y = keras.layers.Conv2D(
    filters=filters, kernel_size=(1, 1), padding="same"
    )(input_layer)

    output_block = keras.layers.add([shortcut_y, conv_z])
    output_block = keras.layers.Activation("relu")(output_block)

    return output_block


    def residual_blocks(input_layer, num_blocks=8):
    x = input_layer

    for block in range(num_blocks):
    x = residual_block(x)

    return x


    def classifier_block(ecoder_results, num_cats):
    conv_y = keras.layers.Conv2D(filters=num_cats, kernel_size=(1, 2), padding="same")(
    ecoder_results
    )
    conv_y = keras.layers.Activation("softmax")(conv_y)

    return conv_y

    def dense_connection_block(ecoder_results, units=64):

    return keras.layers.Dense(units=64)(ecoder_results)

    def residual_cnn_w_dense(input_shape, num_categories=2):
    inputs = tf.keras.layers.Input(shape=input_shape)

    encoder = residual_blocks(inputs)

    denseout = dense_connection_block(encoder)

    outputs = classifier_block(denseout, num_categories)

    model = tf.keras.Model(inputs=inputs, outputs=outputs, name="residual_cnn_w_dense")

    return model



    Compile the model for input 3 feature channels and 2 segmentation classes

    num_time_series_channels = 3

    model = residual_cnn_w_dense(
    input_shape=(None, None, num_time_series_channels),
    )

    model.compile(
    run_eagerly=True,
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
    loss=focal_loss,
    metrics=dice_coef_multiclass,
    )


    Create some different sized time-series inputs with shape (batch, 1, time, feature-channels), where the time-series length changes.

    data_input_0 = np.random.randn(12,1,32,3)
    data_input_1 = np.random.randn(17,1,12,3)


    Run the model on each different shaped input

    model(data_input_0).shape, model(data_input_1).shape


    This works and returns (TensorShape([12, 1, 32, 2]), TensorShape([17, 1, 12, 2]))

    Training the model also works and does not throw an error when the time-series length changes.

    What is happening here? Why am I not getting an error at the dense layer?

    Continue reading...

Compartilhe esta Página