EmotiEffLib
Python implementation of Emotion Efficient Library
Loading...
Searching...
No Matches
engagement_classification_model.py
Go to the documentation of this file.
1"""
2Engagement classification model implementation
3"""
4
5# pylint: disable=no-name-in-module,import-error
6from .utils import get_engagement_classification_weights
7
8TF_AVAILABLE = True
9try:
10 from tensorflow.keras import backend as K
11 from tensorflow.keras.layers import (
12 Activation,
13 Dense,
14 Input,
15 Lambda,
16 Multiply,
17 Permute,
18 RepeatVector,
19 Reshape,
20 )
21 from tensorflow.keras.models import Model
22except ImportError:
23 TF_AVAILABLE = False
24
25
26def _single_attention_model(n_classes, weights, feature_vector_dim, samples=None):
27 """
28 Build a single attention classification model.
29
30 Args:
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.
36
37 Returns:
38 Model: A Keras model implementing attention-based classification.
39
40 Raises:
41 ValueError: If the feature vector dimension is not 2560.
42 """
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.")
47 inputs = Input(
48 shape=(samples, feature_vector_dim), name="image_set"
49 ) # (batch, samples, features)
50 e = Dense(1, activation="linear", name="e")(inputs)
51 e = Reshape([samples], name="alignment")(e)
52 alpha = Activation("softmax", name="alpha")(e)
53
54 alpha_repeated = Permute([2, 1], name="alpha_repeated")(
55 RepeatVector(feature_vector_dim, name="repeat")(alpha)
56 )
57
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")(
60 c
61 )
62
63 x = Dense(units=512, activation="relu", name="hidden_FC")(x) # (batch, units) #128 64
64
65 pred = Dense(n_classes, activation="softmax")(x) # (batch, classes)
66 model = Model(inputs=inputs, outputs=pred)
67
68 model.load_weights(weights)
69
70 return model
71
72
73def get_engagement_model(feature_vector_dim, number_of_frames):
74 """
75 Load and return the engagement classification model.
76
77 Args:
78 feature_vector_dim (int): The dimensionality of the input feature vectors.
79 number_of_frames (int): The number of frames in the input sequence.
80
81 Returns:
82 Model: A Keras model for engagement classification.
83 """
84 weights_path = get_engagement_classification_weights()
85 return _single_attention_model(2, weights_path, feature_vector_dim, number_of_frames)
_single_attention_model(n_classes, weights, feature_vector_dim, samples=None)
get_engagement_model(feature_vector_dim, number_of_frames)