[Python] ValueError: Unknown layer: 'GetItem'

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    Here is the full error message:


    ValueError: Unknown layer: 'GetItem'. Please ensure you are using a keras.utils.custom_object_scope and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
    ​

    The problem I am having here is that this GetItem layer is preventing me from loading my model. The GetItem layer comes from indexing the Keras input layer. The original input layer shape is (None, 256, 33), and this shape is split into (None, 256, 1) and (None, 256, 32), the former of which gets sliced to (None, 1) and the latter of which transform into (None, 8) after tensor operations.

    Because this layer is not a handmade custom layer by me but instead the result of tensor indexing, I have no idea how to pass the layer class like model = load_model('my_model.h5', custom_objects={'GetItem': GetItem}).

    Thus, I need help in either passing the GetItem layer as a custom layer to the load_model function or loading the model without this hassle.

    (My Tensorflow version is 2.17.0, and Keras version is 3.5.0.)

    Here is the code I used to load the model.

    import os

    import tensorflow as tf

    parent_path = os.path.join(os.getcwd(), 'Model')
    file_name = "2024_08_14_yunja_Cylinder_MDN_1.h5"
    file_path = os.path.join(parent_path, file_name)

    model = tf.keras.models.load_model(file_path)


    Here is the shape of the model architecture.

    ┌─────────────────────┬───────────────────┬────────────┬───────────────────┐
    │ Layer (type) │ Output Shape │ Param # │ Connected to │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ input_layer │ (None, 256, 33) │ 0 │ - │
    │ (InputLayer) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ get_item_1 │ (None, 256, 32) │ 0 │ input_layer[0][0] │
    │ (GetItem) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ gaussian_noise │ (None, 256, 32) │ 0 │ get_item_1[0][0] │
    │ (GaussianNoise) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d (Conv1D) │ (None, 256, 64) │ 18,496 │ gaussian_noise[0… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_1 (Conv1D) │ (None, 256, 64) │ 10,304 │ gaussian_noise[0… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate │ (None, 256, 128) │ 0 │ conv1d[0][0], │
    │ (Concatenate) │ │ │ conv1d_1[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_2 (Conv1D) │ (None, 128, 64) │ 41,024 │ concatenate[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ max_pooling1d │ (None, 128, 128) │ 0 │ concatenate[0][0] │
    │ (MaxPooling1D) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate_1 │ (None, 128, 192) │ 0 │ conv1d_2[0][0], │
    │ (Concatenate) │ │ │ max_pooling1d[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_3 (Conv1D) │ (None, 128, 32) │ 43,040 │ concatenate_1[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_4 (Conv1D) │ (None, 128, 32) │ 30,752 │ concatenate_1[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate_2 │ (None, 128, 64) │ 0 │ conv1d_3[0][0], │
    │ (Concatenate) │ │ │ conv1d_4[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_5 (Conv1D) │ (None, 64, 32) │ 10,272 │ concatenate_2[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ max_pooling1d_1 │ (None, 64, 64) │ 0 │ concatenate_2[0]… │
    │ (MaxPooling1D) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate_3 │ (None, 64, 96) │ 0 │ conv1d_5[0][0], │
    │ (Concatenate) │ │ │ max_pooling1d_1[… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_6 (Conv1D) │ (None, 64, 32) │ 15,392 │ concatenate_3[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_7 (Conv1D) │ (None, 64, 32) │ 9,248 │ concatenate_3[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate_4 │ (None, 64, 64) │ 0 │ conv1d_6[0][0], │
    │ (Concatenate) │ │ │ conv1d_7[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ conv1d_8 (Conv1D) │ (None, 32, 32) │ 10,272 │ concatenate_4[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ re_lu (ReLU) │ (None, 32, 32) │ 0 │ conv1d_8[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ max_pooling1d_2 │ (None, 32, 64) │ 0 │ concatenate_4[0]… │
    │ (MaxPooling1D) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate_5 │ (None, 32, 96) │ 0 │ re_lu[0][0], │
    │ (Concatenate) │ │ │ max_pooling1d_2[… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ flatten (Flatten) │ (None, 3072) │ 0 │ concatenate_5[0]… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dropout (Dropout) │ (None, 3072) │ 0 │ flatten[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ gaussian_noise_1 │ (None, 3072) │ 0 │ dropout[0][0] │
    │ (GaussianNoise) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dense (Dense) │ (None, 1536) │ 4,720,128 │ gaussian_noise_1… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dropout_1 (Dropout) │ (None, 1536) │ 0 │ dense[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ gaussian_noise_2 │ (None, 1536) │ 0 │ dropout_1[0][0] │
    │ (GaussianNoise) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dense_1 (Dense) │ (None, 256) │ 393,472 │ gaussian_noise_2… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dropout_2 (Dropout) │ (None, 256) │ 0 │ dense_1[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ gaussian_noise_3 │ (None, 256) │ 0 │ dropout_2[0][0] │
    │ (GaussianNoise) │ │ │ │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dense_2 (Dense) │ (None, 64) │ 16,448 │ gaussian_noise_3… │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dense_3 (Dense) │ (None, 8) │ 520 │ dense_2[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ get_item (GetItem) │ (None, 1) │ 0 │ input_layer[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ concatenate_6 │ (None, 9) │ 0 │ dense_3[0][0], │
    │ (Concatenate) │ │ │ get_item[0][0] │
    ├─────────────────────┼───────────────────┼────────────┼───────────────────┤
    │ dense_4 (Dense) │ (None, 4) │ 40 │ concatenate_6[0]… │
    └─────────────────────┴───────────────────┴────────────┴───────────────────┘


    Code for building the model:

    def build_MGNN(
    input_shape: tuple[int],
    output_num: int,
    num_given: int,
    last_activation: float
    ) -> tf.keras.models:

    input_ = Input(shape=input_shape)
    extra_ = input_[:, 0, :num_given]
    next_ = input_[:, :, num_given:]

    next_ = GaussianNoise(0.025)(next_)

    temp_0 = Conv1D(
    filters=64,
    kernel_size=9,
    strides=1,
    padding="same",
    activation="relu"
    )(next_)

    temp_1 = Conv1D(
    filters=64,
    kernel_size=5,
    strides=1,
    padding="same",
    activation="relu"
    )(next_)

    x = Concatenate(axis=2)([temp_0, temp_1])

    temp_0 = Conv1D(
    filters=64,
    kernel_size=5,
    strides=2,
    padding="same",
    activation="relu"
    )(x)

    temp_1 = MaxPooling1D()(x)

    x = Concatenate(axis=2)([temp_0, temp_1])

    temp_0 = Conv1D(
    filters=32,
    kernel_size=7,
    strides=1,
    padding="same",
    activation="relu"
    )(x)

    temp_1 = Conv1D(
    filters=32,
    kernel_size=5,
    strides=1,
    padding="same",
    activation="relu"
    )(x)

    x = Concatenate(axis=2)([temp_0, temp_1])

    temp_0 = Conv1D(
    filters=32,
    kernel_size=5,
    strides=2,
    padding="same",
    activation="relu"
    )(x)

    temp_1 = MaxPooling1D()(x)

    x = Concatenate(axis=2)([temp_0, temp_1])

    temp_0 = Conv1D(
    filters=32,
    kernel_size=5,
    strides=1,
    padding="same",
    activation="relu"
    )(x)

    temp_1 = Conv1D(
    filters=32,
    kernel_size=3,
    strides=1,
    padding="same",
    activation="relu"
    )(x)

    x = Concatenate(axis=2)([temp_0, temp_1])

    temp_0 = Conv1D(
    filters=32,
    kernel_size=5,
    strides=2,
    padding="same"
    )(x)

    temp_0 = ReLU()(temp_0)

    temp_1 = MaxPooling1D()(x)

    x = Concatenate(axis=2)([temp_0, temp_1])

    x = Flatten()(x)
    x = Dropout(0.25)(x)
    x = GaussianNoise(0.05)(x)

    x = Dense(units=1536, activation="relu")(x)

    x = Dropout(0.125)(x)
    x = GaussianNoise(0.025)(x)

    x = Dense(units=256, activation="relu")(x)

    x = Dropout(0.0625)(x)
    x = GaussianNoise(0.0125)(x)

    x = Dense(units=64, activation="relu")(x)
    x = Dense(units=8, activation="relu")(x)
    x = Concatenate(axis=1)([x, extra_])
    x = Dense(units=2*output_num, activation=last_activation)(x)

    model = Model(inputs=input_, outputs=x)

    return model

    Continue reading...

Compartilhe esta Página