`Hello I am in the same error as the title

Below is the code I used

from keras.layers import Input



class PositionalEmbedding(layers.Layer):
    def __init__(self, sequence_length, output_dim, **kwargs):
        super().__init__(**kwargs)
        self.position_embeddings = layers.Embedding(
            input_dim=sequence_length, output_dim=output_dim
    )
        self.sequence_length = sequence_length
        self.output_dim = output_dim
        self.feature_extractor = build_feature_extractor()
    @tf.function
    def prepare_feature(self,inputs, root_dir):
        num_samples = 5
        frame_features = np.zeros(
        shape=(num_samples, MAX_SEQ_LENGTH, NUM_FEATURES), dtype="float32"
    )
            # Initialize placeholder to store the features of the current video.
        for idx, frames  in inputs:
            temp_frame_features = np.zeros(
              shape=(1, MAX_SEQ_LENGTH, NUM_FEATURES), dtype="float32"
        )

            # Extract features from the frames of the current video.
            for i, batch in enumerate(frames):
                video_length = batch.shape[0]
                length = min(MAX_SEQ_LENGTH, video_length)
                for j in range(length):
                    if np.mean(batch[j, :]) > 0.0:
                        temp_frame_features[i, j, :] = self.feature_extractor.predict(
                            batch[None, j, :]
                    )

                    else:
                        temp_frame_features[i, j, :] = 0.0

            frame_features[idx,] = temp_frame_features.squeeze()
        return frame_features     # (1229,90,1664)

    def call(self, inputs):
        inputs=self.prepare_feature(inputs,"train")
        # The inputs are of shape: `(batch_size, frames, num_features)`
        length = tf.shape(inputs)[1]
        positions = tf.range(start=0, limit=length, delta=1)
        embedded_positions = self.position_embeddings(positions)
        return inputs + embedded_positions

    #def compute_mask(self, inputs, mask=None):
    #    mask = tf.reduce_any(tf.cast(inputs, "bool"), axis=-1)
    #    return mask

class TransformerEncoder(layers.Layer):
    def __init__(self, embed_dim, dense_dim, num_heads, **kwargs):
        super().__init__(**kwargs)
        self.embed_dim = embed_dim
        self.dense_dim = dense_dim
        self.num_heads = num_heads
        self.attention = layers.MultiHeadAttention(
            num_heads=num_heads, key_dim=embed_dim, dropout=0.3
        )
        self.dense_proj = keras.Sequential(
            [layers.Dense(dense_dim, activation=tf.nn.gelu), layers.Dense(embed_dim),]
        )
        self.layernorm_1 = layers.LayerNormalization()
        self.layernorm_2 = layers.LayerNormalization()
  
    def call(self, inputs, mask=None):
       # if mask is not None:
       #     mask = mask[:, tf.newaxis, :]
    
        attention_output = self.attention(inputs, inputs)
        proj_input = self.layernorm_1(inputs + attention_output)
        proj_output = self.dense_proj(proj_input)
        out = self.layernorm_2(proj_input + proj_output)
        return out
    
def get_compiled_model():
    sequence_length = MAX_SEQ_LENGTH
    embed_dim = NUM_FEATURES
    dense_dim = 4
    num_heads = 1
    classes = len(label_processor.get_vocabulary())
    inputs = keras.Input(shape=(None,None,))

    x1 = PositionalEmbedding(
        sequence_length, embed_dim, name="frame_position_embedding"
    )(inputs)
    x = TransformerEncoder(embed_dim, dense_dim, num_heads, name="transformer_layer")(x1)
    x = layers.GlobalMaxPooling1D()(x)
    x = layers.Dropout(0.5)(x)
    outputs = layers.Dense(classes, activation="softmax")(x)
    model = keras.Model(inputs, outputs)

    model.compile(
       optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),                 loss="sparse_categorical_crossentropy", metrics=["accuracy"]
    )
    return model


 def run_experiment():
    filepath = "/tmp/video_classifier"

    checkpoint = keras.callbacks.ModelCheckpoint(
        filepath, save_weights_only=True, save_best_only=True, verbose=1
    )

    model = get_compiled_model()
    history = model.fit(
        train_video,
        train_labels,
        batch_size=64,
        validation_split=0.18,
        epochs=13,
        callbacks=[checkpoint],
    )

    model.load_weights(filepath)


    return model

Below is the error message

ValueError Traceback (most recent call last) in <cell line: 1>() ----> 1 trained_model = run_experiment()

6 frames /usr/local/lib/python3.10/dist-packages/keras/engine/functional.py in _validate_graph_inputs_and_outputs(self) 870 if not hasattr(x, "_keras_history"): 871 cls_name = self.class.name --> 872 raise ValueError( 873 f"Output tensors of a {cls_name} model must be " 874 "the output of a TensorFlow Layer "

ValueError: Output tensors of a Functional model must be the output of a TensorFlow Layer (thus holding past layer metadata). Found: [[0.9923929 0.00760708]


The input of the model is of type numpy.ndarray and inserts video data of the form (5,90,240,240,3). Every time I try, I get several types of errors. can you help me?`

If I don't set inputs = keras.Input(shape=(None,None,)) but set inputs = np.shape(None) An error occurs with the phrase 'ValueError: Output tensors of a Functional model must be the output of a TensorFlow Layer when using custom callback to plot conv layer feature maps'.

0

There are 0 best solutions below