28 Build a single attention classification model.
31 n_classes (int): The number of output classes.
32 weights (str): Path to the pre-trained model weights.
33 feature_vector_dim (int): The dimensionality of the input feature vectors.
34 Must be 2560; otherwise, a ValueError is raised.
35 samples (int, optional): The number of samples in the input sequence. Defaults to None.
38 Model: A Keras model implementing attention-based classification.
41 ValueError: If the feature vector dimension is not 2560.
43 if TF_AVAILABLE
is False:
44 raise RuntimeError(
"Cannot create engagement model because tensorflow is not installed")
45 if feature_vector_dim != 2560:
46 raise ValueError(
"Unsupported feature vector dim. Maybe you use unsupported model.")
48 shape=(samples, feature_vector_dim), name=
"image_set"
50 e = Dense(1, activation=
"linear", name=
"e")(inputs)
51 e = Reshape([samples], name=
"alignment")(e)
52 alpha = Activation(
"softmax", name=
"alpha")(e)
54 alpha_repeated = Permute([2, 1], name=
"alpha_repeated")(
55 RepeatVector(feature_vector_dim, name=
"repeat")(alpha)
58 c = Multiply(name=
"c")([inputs, alpha_repeated])
59 x = Lambda(
lambda xin: K.sum(xin, axis=1), output_shape=(feature_vector_dim,), name=
"context")(
63 x = Dense(units=512, activation=
"relu", name=
"hidden_FC")(x)
65 pred = Dense(n_classes, activation=
"softmax")(x)
66 model = Model(inputs=inputs, outputs=pred)
68 model.load_weights(weights)