Age prediction method, model training method, medium and equipment
By introducing gated units and hybrid expert modules in the age prediction model, extracting features of different ages, the problem of low adaptability of age prediction models to new face images in the prior art is solved, and a higher accuracy of age prediction is achieved.
Patent Information
- Application Number
- CN202411706369.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-26
- Publication Date
- 2025-04-04
AI Technical Summary
In the prior art, the age prediction model has low adaptability when facing new face images, resulting in low accuracy.
An age prediction model training method including basic feature extraction module, gated unit, hybrid expert module and age prediction module is adopted. This method uses a gated unit and a hybrid expert module to specifically extract features of different ages to improve adaptability to new images.
By introducing gated units and hybrid expert modules, the adaptability of the age prediction model to new face images is improved, and the accuracy of age prediction is improved.
Smart Images

Figure CN119763167A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of face recognition, and in particular to an age prediction method, a model training method, a medium and a device. Background Art
[0002] Age is a very important feature information in face images and is widely used in the field of face recognition. Different people of the same age have some similar features. Therefore, in existing technologies, age is usually used as a separate label information. During the model training process, multiple training face images are established with a one-to-one correspondence between age and the model, and training is performed.
[0003] However, each person's facial features are unique, which results in the trained age prediction model being tested. Each time a new face image is tested, the new face image is a new type of image relative to the model, and the model itself has only learned the categories of the training face images. In order to accurately identify the age of new face images, the model needs to be retrained. Therefore, it is difficult for the trained model to adapt to this unique feature, which may lead to low accuracy in actual application. Summary of the invention
[0004] Based on this, it is necessary to provide age prediction methods, model training methods, media and equipment to solve the problem of low adaptability to new face images in age prediction scenarios.
[0005] A training method for an age prediction model, the age prediction model comprising a basic feature extraction module, a gate control unit, a hybrid expert module and an age prediction module connected in sequence, the gate control unit comprising N branches, the hybrid expert module comprising N expert units, one branch being connected to one expert unit, N being the total number of ages within a preset age range, the method comprising:
[0006] Acquire multiple face training images with age labels, wherein the age labels are used to indicate the actual ages of the faces in the face training images, and the multiple face training images cover all ages within a preset age range;
[0007] Inputting N face training images of different actual ages into the basic feature extraction module to extract basic features, and obtaining output basic feature vectors of N different actual ages;
[0008] Inputting N basic feature vectors of different actual ages into the gate control unit for output selection, and obtaining the basic feature vector of the target age output by the gate control unit in the target branch; wherein the target branch is any one of the N branches, and different branches output basic feature vectors of different preset ages during training;
[0009] Inputting the basic feature vector of the target age into the target expert unit to extract the feature representation of the target age, obtaining the output age feature vector, and obtaining N age feature vectors; wherein the target expert unit is the expert unit connected to the target branch in the hybrid expert module;
[0010] Inputting the N age feature vectors into the age prediction module for prediction respectively, and obtaining output N predicted ages;
[0011] Through a preset loss function, a total loss value of each face training image is calculated according to the difference between N actual ages, N predicted ages and the age feature vectors, and the parameters of the age prediction model are iteratively adjusted according to the total loss value and a preset adjustment strategy until the age prediction model converges; wherein the preset adjustment strategy is to reduce the difference between the actual age and the predicted age of each face training image and to increase the difference between the basic feature vectors of different actual ages.
[0012] In one embodiment, the preset loss function includes an age difference loss function and a cross entropy loss function, and the preset loss function is used to calculate the total loss value of each face training image according to the difference between N actual ages, N predicted ages and age feature vectors, including:
[0013] By using the age difference loss function, an age loss value is calculated according to the actual age of each face training image and the corresponding output predicted age; wherein the size of the age loss value is positively correlated with the age difference between the actual age and the predicted age;
[0014] The cross entropy loss function is used to calculate the cross entropy loss value according to the vector difference between the age feature vector of each face training image and the age feature vectors of M adjacent actual ages; wherein the size of the cross entropy loss value is negatively correlated with the size of the vector difference, M≥2;
[0015] The sum of the age loss value and the cross entropy loss value is taken as the total loss value.
[0016] In one embodiment, the age difference loss function is expressed as:
[0017]
[0018] Among them, CELoss represents the age loss value, y represents the actual age, represents the predicted age;
[0019] The cross entropy loss function is expressed as:
[0020]
[0021] Wherein, AdaLoss represents the cross entropy loss value, represents the age feature vector of the face training image with actual age i, Represents the age feature vector of the face training image with actual age j.
[0022] In one embodiment, the basic feature extraction module includes I1 basic feature extraction units and a fully connected layer connected in sequence, and inputting N face training images of different actual ages into the basic feature extraction module to obtain N basic feature vectors of different actual ages output by the basic feature extraction module includes:
[0023] Input the (i-1)th layer feature map to the i-th basic feature extraction unit in the basic feature extraction module for feature extraction, and obtain the i-th layer feature map as output; wherein, 1≤i≤I1, when i=1, the (i-1)th layer feature map is a target face training image, and the target face training image is any one of N face training images of different actual ages;
[0024] The output I1th layer feature map is converted into a basic feature vector of the target face training image through a fully connected layer and output, so as to obtain basic feature vectors of N different actual ages.
[0025] In one embodiment, the age prediction model further includes I2 layers of hybrid attention modules connected in sequence, each layer of the hybrid attention module includes a multi-head attention mechanism layer, a forward layer module and a normalization layer connected in sequence, and the multi-layer hybrid attention module is arranged between the basic feature extraction module and the gating unit, and the method further includes:
[0026] Input the (i-1)th layer feature vector into the multi-head attention mechanism layer of the i-th layer hybrid attention module to dynamically allocate attention weights, and obtain the output i-th layer attention feature vector; where 1≤i≤I2, when i=1, the (i-1)th layer feature vector is any one of the N basic feature vectors;
[0027] Input the i-th layer attention feature vector into the forward layer module of the i-th layer hybrid attention module for nonlinear transformation and weight update, and obtain the output i-th layer forward feature vector;
[0028] The i-th layer forward feature vector is input into the normalization layer of the i-th layer hybrid attention module for normalization calculation to obtain the output i-th layer normalized vector; wherein the i-th layer normalized vector has the same feature size as the (i-1)-th layer feature vector.
[0029] In one embodiment, a branch includes a gate-controlled switch and a branch number, the branch number of a branch is any one of N different actual ages, and the branch numbers of different branches are different, and the basic feature vectors of N different actual ages are input into the gate control unit for output selection, and the basic feature vector of the target age output by the gate control unit in the target branch is obtained, including:
[0030] When a basic feature vector is input into the target branch, determining whether the actual age of the currently input basic feature vector is the same as the branch number of the target branch;
[0031] If the actual age of the currently input basic feature vector is different from the branch number of the target branch, the gated switch of the target branch is controlled to be closed;
[0032] If the actual age of the currently input basic feature vector is the same as the branch number of the target branch, the gated switch of the target branch is controlled to be turned on, and the currently input basic feature vector is output as the basic feature vector of the target age.
[0033] In one embodiment, the age prediction module includes an input layer, multiple hidden layers and an output layer connected in sequence, and the N age feature vectors are respectively input into the age prediction module for prediction to obtain the output N predicted ages, including:
[0034] Inputting the age feature vector of the target age into the input layer to obtain an output initial feature vector;
[0035] Input the (i-1)th intermediate feature vector into the i-th hidden layer for nonlinear transformation and feature extraction, and obtain the i-th intermediate feature vector as output; wherein 1≤i≤h, h is the number of hidden layers, and when i=1, the (i-1)th intermediate feature vector is the initial feature vector;
[0036] The hth intermediate feature vector is input into the output layer for conversion, and the predicted age of the corresponding output is obtained to obtain N predicted ages.
[0037] An age prediction method is applied to the age prediction model trained by the above method, the method comprising:
[0038] Acquire a face image to be detected, input the face image to be detected into the basic feature extraction module to extract basic features, and obtain an output basic feature vector of the face image to be detected;
[0039] Input the basic feature vector of the face image to be detected into the gate control unit, and obtain N basic feature vectors output from all branches in the gate control unit; wherein all branches in the gate control unit are currently opened, each branch has a branch number, and each branch number is used to indicate the actual age of the basic feature vector input during training;
[0040] Inputting the basic feature vector output from the target branch into the corresponding target expert unit, obtaining the age feature vector output by the target expert unit, so as to obtain N age feature vectors;
[0041] Inputting the obtained N age feature vectors into the age prediction module respectively to obtain N predicted ages output by the age prediction module;
[0042] Among the N predicted ages, the predicted age with the smallest difference from the corresponding branch number is used as the prediction result of the face image to be detected.
[0043] A computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the processor executes the training method of the age prediction model and the steps of the age prediction method.
[0044] A terminal device includes a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the training method of the age prediction model and the steps of the age prediction method.
[0045] The present invention provides an age prediction method, a model training method, a medium and a device. First, face training images with age labels are obtained, and these images cover all ages within a preset age range. These images are then input into a basic feature extraction module to extract basic features and generate corresponding basic feature vectors. The basic feature vectors are then input into a gating unit, and a basic feature vector of a target age is selected and output from a target branch. The output target feature vector enters a corresponding target expert unit to further extract the age features of the target age and generate an age feature vector. The age feature vector then passes through an age prediction module to obtain a final predicted age. Finally, the total loss value of each face training image is calculated through a loss function based on the difference between the predicted and actual ages and the difference between the age feature vectors, and the model parameters are iteratively adjusted until the model converges. By introducing a gating unit and a hybrid expert module, this method can specifically extract features for different ages and improve the adaptability to new images. The preset adjustment strategy is to reduce the difference between N different actual ages and N predicted ages, and increase the difference between the basic feature vectors of N different actual ages. Therefore, as the training progresses, while paying attention to its own age feature vector, the basic feature vectors of N different actual ages are distinguished, which further improves the adaptability to new face images in the age prediction scenario, thereby improving the accuracy of age prediction. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0047] in:
[0048] Figure 1 is a flow chart of a method for training an age prediction model in one embodiment;
[0049] Figure 2 is a first structural schematic diagram of an age prediction model;
[0050] Figure 3 is a second structural schematic diagram of the age prediction model;
[0051] Figure 4 is a schematic flow chart of an age prediction method in one embodiment;
[0052] Figure 5 This is a structural block diagram of the terminal equipment. DETAILED DESCRIPTION
[0053] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0054] The terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0055] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0056] like Figure 1 As shown, Figure 1 FIG. 1 is a flow chart of a method for training an age prediction model in an embodiment. Figure 2 As shown, the age prediction model includes a basic feature extraction module, a gating unit, a hybrid expert module and an age prediction module connected in sequence. Among them, the gating unit includes N branches, the hybrid expert module includes N expert units, one branch is connected to one expert unit, and N is the total number of ages in the preset age range. Exemplarily, the preset age range in this embodiment is set to 1-100, so N here is 100. Of course, N here can also be set to other values according to needs.
[0057] The training method of the age prediction model in this embodiment provides the following steps:
[0058] S101, obtaining a plurality of face training images with age labels.
[0059] The age label is used to indicate the actual age of the face in the face training image, and the multiple face training images cover all ages within a preset age range.
[0060] Optionally, a plurality of face training images are taken by a camera device, and age labels are marked according to the actual age. Alternatively, some face training images with age labels are retrieved from a database. Of course, multiple face training images with age labels can also be obtained by other means, which are not specifically limited here. In this embodiment, the face training images obtained should be sufficient and cover all ages within the preset age range. For example, if the preset age range is 1-100, 100 face training images with age labels of 1-100 can be used as a training set, and 100 such sets can be obtained. Of course, more sets can be obtained.
[0061] It is understandable that there are differences between different face training images. These differences include image size, image brightness, angle of the face in the image, etc. These differences will cause certain errors in subsequent age prediction. Therefore, it is necessary to perform certain preprocessing operations on these obtained face training images. The preprocessing operations include:
[0062] (1) Rotate the faces in multiple face training images until the face angles in different face training images are consistent. Specifically, according to the face key point algorithm, for example, the face 68-point landmark model algorithm of the Dlib library is used to obtain the center positions of the two eyeballs and the center position of the nose, and then connect the two center positions and combine the vertical line to calculate the angle θ of the face rotation to the left and right. Finally, the rotation change matrix is used to adjust the face image with the nose coordinate information as the center. The specific calculation formula is as follows:
[0063]
[0064] Where x and y are the two-dimensional coordinates of the pixels in the original face training image. ′ and ′ The two-dimensional coordinates of the adjusted pixels in the face training image. This allows the angle of the crooked face training image to be corrected.
[0065] (2) Adjust the brightness of multiple face training images until the brightness of different face training images is consistent. Specifically, tools such as Photoshop can be used to adjust the brightness.
[0066] (3) Normalize the sizes of multiple face training images to a preset size. In addition, the effective face area can be intercepted based on the coordinates of the face key points, with the nose center coordinates as the center and the maximum distance between the face key points as the length, so that only the effective face image part is predicted later. Then perform a scale normalization operation to obtain face training images of the same size. In this embodiment, the normalized size is unified to 224*224*3, but of course it can also be other sizes.
[0067] S102, inputting N face training images of different actual ages into a basic feature extraction module to extract basic features, and obtaining output basic feature vectors of N different actual ages.
[0068] Among them, the basic feature extraction module is a part used to extract the underlying basic features from the face image. It can automatically extract basic features such as edges, textures, shapes, etc. from the image. These features help the model better understand the structure and differences of the face. After each image passes through the basic feature extraction module, a feature vector will be generated. Since there are N training images of faces of different actual ages, N corresponding basic feature vectors can be obtained.
[0069] Optionally, the constructed basic feature extraction module includes I1 basic feature extraction units and a fully connected layer connected in sequence, and S102 specifically includes the following steps:
[0070] A1. Input the (i-1)th layer feature map into the i-th basic feature extraction unit in the basic feature extraction module for feature extraction, and obtain the output i-th layer feature map.
[0071] Among them, 1≤i≤I1, when i=1, the (i-1)th layer feature map is the target face training image, and the target face training image is any one of the N face training images of different actual ages. Each basic feature extraction unit uses a 3*3 convolution kernel for convolution operation, with a step size of 2, and then connects a Relu activation function to reset the negative numbers in the output value to zero. In this way, feature maps of different scales are gradually extracted from shallow to deep layers. The shallow feature map mainly captures low-level features in the image, such as edges and textures, while the deep feature map extracts higher-level semantic features, such as the overall structure and shape of the face, and finally obtains a feature map of size 7*7*256.
[0072] It is understandable that there is no restriction on the feature extraction part here, and the existing structure can be used for replacement, such as Mobilenet, Shufflenet and other structures.
[0073] A2. Convert the output I1th layer feature map into the basic feature vector of the target face training image through the fully connected layer and output it to obtain the basic feature vectors of N different actual ages.
[0074] After obtaining the final feature map of size 7*7*256, a fully connected layer is used to convert the feature map into a basic feature vector F of size 16*96. Since there are N training images of faces of different actual ages, N corresponding basic feature vectors can be obtained.
[0075] S103, inputting N basic feature vectors of different actual ages into a gating unit for output selection, and obtaining a basic feature vector of a target age outputted by the gating unit in a target branch.
[0076] Among them, the target branch is any one of the N branches, for example, branch 1 is a target branch. It can be understood that the gating unit here plays the role of output selection. During the training stage, the gating unit uses actual age as the basis for distinction, and divides the basic feature vectors of N different actual ages that were originally "gathered" together into "dispersed" N basic feature vectors of different actual ages and outputs them, and one branch outputs one basic feature vector, and different branches output basic feature vectors of different preset ages during training. For example, branch 1 only outputs basic feature vectors with actual age of 1, and branch 2 only outputs basic feature vectors with actual age of 2.
[0077] Optional, such as Figure 2 As shown, the gate control unit includes N branches, each branch includes a gate-controlled switch α and a branch number n, and the branch number n of a branch is any one of N different actual ages, that is, n∈N, and the branch numbers of different branches are different. The gate control unit uses the gate-controlled switch α i (i=1, ..., N) control output, its value is 0 or 1, 0 means this branch is in the closed state, 1 means this branch is in the open state. The structure of the gate control unit can be expressed as:
[0078]
[0079] Wherein, F is the basic feature vector obtained by S102. represents a fully connected layer. i represents the i-th gate-controlled switch. Z represents the output vector.
[0080] The basic feature vector carries an age label to indicate the actual age of the basic feature vector. Optionally, the process of dividing the basic feature vector specifically includes the following steps:
[0081] B1. When a basic feature vector is input into the target branch, it is determined whether the actual age of the currently input basic feature vector is the same as the branch number of the target branch.
[0082] B2. If the actual age of the currently input basic feature vector is different from the branch number of the target branch, the gated switch controlling the target branch is closed.
[0083] For example, when a basic feature vector with an actual age of 2 is input to a branch with a branch number of 1, the gated switch controlling the target branch is closed.
[0084] B3. If the actual age of the currently input basic feature vector is the same as the branch number of the target branch, the gated switch of the target branch is controlled to be turned on, and the currently input basic feature vector is output as the basic feature vector of the target age.
[0085] For example, when a basic feature vector with an actual age of 1 is input to a branch with a branch number of 1, the gated switch of branch 1 is controlled to be turned on, and the basic feature vector with an actual age of 1 is output.
[0086] S104, inputting the basic feature vector of the target age into the target expert unit to extract the feature representation of the target age, obtaining the output age feature vector, and obtaining N age feature vectors.
[0087] Among them, the target expert unit is the expert unit connected to the target branch in the hybrid expert module. In the hybrid expert module, each expert unit is responsible for the feature extraction of a specific age. Each expert unit can be regarded as a more refined model component that specializes in dealing with a certain type of problem (in this case, a specific age). After the gating unit selects the target branch, the corresponding target expert unit is responsible for further extracting the deep features of the branch, thereby obtaining the corresponding age feature vector, which contains deeper and more representative features. Optionally, the structure of these expert units can be a simple fully connected layer, or a more complex convolutional neural network (CNN), recurrent neural network (RNN), etc. This ensures that the model can be more accurate in feature extraction for different age groups.
[0088] Optionally, if the input face training images have an age imbalance problem, you can also increase the load balancing weight to adjust the model's learning process, expressed as:
[0089] G σ (x) = Softmax9x·W g +Softplus(x·W noise ))
[0090] Among them, W g represents the network learning parameters of the gate control unit, W noise represents the age parameter weight, and x is the input feature vector.
[0091] S105, inputting N age feature vectors into an age prediction module for prediction respectively, and obtaining output N predicted ages.
[0092] Among them, the age prediction module is a network module used to output a specific age value from the age feature vector. The design of this module can be implemented in different ways, such as a regression model or a classification model, depending on the specific prediction requirements. During the training and testing process of the model, the input N face training images will output N predicted ages respectively. This means that for each face image, there is a corresponding predicted age value.
[0093] Optionally, the age prediction module is a three-layer perceptron, including an input layer, multiple hidden layers and an output layer connected in sequence, wherein the input layer contains K neural units, the hidden layer contains Q neural units, and the output layer contains N (N=100) units. Assume that the input layer uses vector The output of the hidden layer is W 1 is the weight, b 1 is the bias, and the function f can be the commonly used sigmoid function or tanh function. The hidden layer to the output layer can be regarded as a multi-category logistic regression, that is, softmax regression, so the output of the output layer is The network structure formula of the multi-layer perceptron is expressed as follows:
[0094]
[0095] Among them, G represents the softmax activation function, h represents the number of hidden layers, and W i and b i represents the weight and bias of the i-th hidden layer, Represents the input feature vector. W 1 and b 1 Represents the weight and bias of the input layer. S represents the activation function.
[0096] Accordingly, S105 specifically includes the following steps:
[0097] C1. Input the target age feature vector into the input layer to obtain the output initial feature vector.
[0098] In this step, the input is the age feature vector of the target age obtained from the expert unit. The function of the input layer is to perform preliminary linear transformation and processing on the vector and convert it into an initial feature vector.
[0099] C2. Input the (i-1)th intermediate feature vector into the i-th hidden layer for nonlinear transformation and feature extraction to obtain the i-th intermediate feature vector as output.
[0100] Where 1≤i≤h, h is the number of hidden layers, and when i=1, the (i-1)th intermediate feature vector is the initial feature vector. In B2, each hidden layer in the network receives the intermediate feature vector output by the previous layer (i.e., the (i-1)th) and processes it. This process includes nonlinear transformation and feature extraction, that is, nonlinear mapping is performed through weight matrices and activation functions (such as ReLU) to further extract the input feature vector into a more meaningful intermediate feature vector. This process is repeated h times, and each layer extracts a new feature vector, gradually enhancing the model's understanding of the target age characteristics.
[0101] C3. Input the hth intermediate feature vector into the output layer for conversion, and obtain the predicted age of the corresponding output to obtain N predicted ages.
[0102] In the last step, the hth intermediate feature vector after being processed by h hidden layers will be input to the output layer. The output layer will convert the high-dimensional feature vector into a specific prediction result through linear transformation. Because N age feature vectors are input, the output layer will process each feature vector separately and finally obtain N predicted ages.
[0103] S106, using a preset loss function, calculate the total loss value of each face training image according to the difference between N actual ages, N predicted ages and the age feature vector, and iteratively adjust the parameters of the age prediction model according to the total loss value and a preset adjustment strategy until the age prediction model converges.
[0104] The preset adjustment strategy is to reduce the difference between the actual age and the predicted age of each face training image, and to increase the difference between the basic feature vectors of different actual ages. Therefore, as the training progresses, while paying attention to the own age feature vector, the basic feature vectors of N different actual ages are distinguished, which further improves the generalization ability of age prediction and thus improves the accuracy of age prediction.
[0105] Optionally, the preset loss function includes an age difference loss function and a cross entropy loss function. In S106, the total loss value of each face training image is calculated according to the difference between the N actual ages, the N predicted ages and the age feature vectors by using the preset loss function, and specifically includes the following steps:
[0106] D1. The age difference loss function is used to calculate the age loss value based on the actual age of each face training image and the corresponding output predicted age.
[0107] The age loss value is positively correlated with the age difference between the actual age and the predicted age. Optionally, the age difference loss function is expressed as:
[0108]
[0109] Among them, CELoss represents the age loss value, y represents the actual age, Represents the predicted age.
[0110] D2. The cross entropy loss function is used to calculate the cross entropy loss value according to the vector difference between the age feature vector of each face training image and the age feature vectors of M adjacent actual ages.
[0111] Among them, the size of the cross entropy loss value is negatively correlated with the size of the vector difference, M ≥ 2. Optionally, the cross entropy loss function is expressed as:
[0112]
[0113] Among them, AdaLoss represents the cross entropy loss value, represents the age feature vector of the face training image with actual age i, represents the age feature vector of the face training image with actual age j. L1 represents the L1 norm. Since the facial features of adjacent ages are usually close, the model is adjusted here by measuring the difference between the feature vectors of adjacent ages, so that the feature distribution of adjacent ages has stronger distinguishability.
[0114] D3. The sum of the age loss value and the cross entropy loss value is taken as the total loss value.
[0115] So it is expressed as:
[0116]
[0117] Furthermore, the above result is the total loss value of each face training image. The target total loss value of the current batch of images can be calculated by averaging or other methods, and the parameters in the age prediction model are adjusted using the adaptive matrix estimation algorithm (ADAM algorithm) based on the total loss value. In this embodiment, the number of iterations of the ADAM algorithm is set to 500 times, the initialization learning rate is set to 0.001, and the weight decay is set to 0.0005. Every 50 iterations, the learning rate decays to 1 / 10 of the original. Model training is performed until the model converges.
[0118] The training method of the above-mentioned age prediction model first obtains face training images with age labels, which cover all ages within the preset age range. Then these images are input into the basic feature extraction module to extract the basic features and generate the corresponding basic feature vector. Then the basic feature vector is input into the gating unit, the basic feature vector of the target age is selected and output from the target branch, and the output target feature vector enters the corresponding target expert unit to further extract the age features of the target age and generate the age feature vector. Then the age feature vector passes through the age prediction module to obtain the final predicted age. Finally, the total loss value of each face training image is calculated through the loss function according to the difference between the predicted and actual age and the difference between the age feature vectors, and the model parameters are iteratively adjusted until the model converges. By introducing a gating unit and a hybrid expert module, this method can specifically extract features for different ages and improve the adaptability to new images. The preset adjustment strategy is to reduce the difference between N different actual ages and N predicted ages, and increase the difference between the basic feature vectors of N different actual ages. Therefore, as the training progresses, while paying attention to its own age feature vector, the basic feature vectors of N different actual ages are distinguished, which further improves the adaptability to new face images in the age prediction scenario, thereby improving the accuracy of age prediction.
[0119] Optional, such as Figure 3 As shown, the age prediction model also includes I2 layers of hybrid attention modules connected in sequence, for example, I2 is set to 5 layers, each layer of hybrid attention module includes a multi-head attention mechanism layer, a forward layer module and a normalization layer connected in sequence, and the multi-layer hybrid attention module is set between the basic feature extraction module and the gating unit. The above method also performs the following steps:
[0120] E1. Input the (i-1)th layer feature vector into the multi-head attention mechanism layer of the i-th layer hybrid attention module to dynamically allocate attention weights and obtain the output i-th layer attention feature vector.
[0121] Among them, 1≤i≤I2, when i=1, the (i-1)th layer feature vector is any one of the N basic feature vectors.
[0122] The multi-head attention mechanism layer is formulated as:
[0123] Q=G σ (x)*W Q
[0124] K=G σ (x)*W K
[0125] V=G σ (x)*W V
[0126]
[0127] W Q , W k , W v Represent the weight matrices of query, key, and value of the multi-head attention mechanism layer. k is the vector dimension. Through the query, key, and value (Q, K, V) matrices, the input basic feature vector is dynamically assigned different weights, thereby highlighting the model's attention to different parts. The multi-head attention mechanism can capture the correlation between different dimensions, thereby improving the flexibility and accuracy of feature extraction.
[0128] E2. Input the i-th layer attention feature vector into the forward layer module of the i-th layer hybrid attention module for nonlinear transformation and weight update to obtain the output i-th layer forward feature vector.
[0129] The forward layer module consists of two fully connected layers and a residual connection. The residual connection adds the input directly to the output of the module. The fully connected layer includes a linear transformation and an activation function, where the linear transformation multiplies the input X by a weight matrix W1 and adds a bias b1, and then the result is input into the activation function for nonlinear transformation. The activation function is usually ReLU. The forward layer module is expressed as:
[0130] FFN i (X) = τ i (max(0,XW1+b1)W2+b2+X)
[0131]
[0132] Among them, W1, W2 represent the weights of the forward module, b1, b2 are the bias values of the forward module, τ i represents the adaptive weight value of the i-th expert module, and N is the age value.
[0133] The forward layer further abstracts and refines the input features through linear transformation and activation function. Residual connections are introduced to avoid the gradient vanishing problem and ensure that the model maintains effective feature transfer during deep training. This step enhances the nonlinear representation ability of feature extraction, allowing the model to better express complex age characteristics.
[0134] E3. Input the i-th layer forward feature vector into the normalization layer of the i-th layer hybrid attention module for normalization calculation to obtain the output i-th layer normalized vector.
[0135] Among them, the normalized vector of the i-th layer has the same feature size as the feature vector of the (i-1)-th layer.
[0136] The normalization layer is expressed as:
[0137]
[0138] Where y represents the input, y+sublayer(y) represents the residual connection between the input y and the result sublayer(y) after sublayer processing, and LayerNorm represents the normalization operation.
[0139] The normalization layer is used to stabilize the training process and reduce the offset of internal covariates. This process makes the features of each layer output consistent between different feature scales through normalization operations, further improving the convergence speed of the model and the stability of prediction.
[0140] After adding the hybrid attention module to the above embodiment, the age prediction model can have stronger dynamic attention ability when processing facial age features, further deepen the learning ability of age features, and improve the robustness of the model and the accuracy of prediction.
[0141] like Figure 4 As shown, the present invention provides an age prediction method, which is applied to the age prediction model trained by the above method. The steps provided by the age prediction method in this embodiment include:
[0142] S401, obtaining a face image to be detected, inputting the face image to be detected into a basic feature extraction module to extract basic features, and obtaining a basic feature vector of the output face image to be detected.
[0143] This step is basically the same as S101-S102 in the training method of the age prediction model. The only difference is that here, the face image to be detected without an age label is obtained, and the age prediction of the face image to be detected needs to be performed.
[0144] S402, inputting the basic feature vector of the face image to be detected into a gating unit, and obtaining N basic feature vectors output from all branches in the gating unit.
[0145] Among them, a branch has a branch number, and each branch number is used to indicate the actual age of the basic feature vector input during training. For example, the branch number of branch 1 is 1, indicating that the actual age of the basic feature vector input during training is 1, and the branch number of branch 2 is 2, indicating that the actual age of the basic feature vector input during training is 2.
[0146] Since the face image to be detected does not carry an age label, the gating unit does not have the function of dividing features in the prediction stage. All branches in the gating unit are currently turned on. After the basic feature vector of the face image to be detected is input into the gating unit, all branches output the basic feature vector of the face image to be detected, so there are N basic feature vectors.
[0147] S403, inputting the basic feature vector output from the target branch into the corresponding target expert unit, acquiring the age feature vector output by the target expert unit, to obtain N age feature vectors.
[0148] In this step, each basic feature vector output from the gating unit is input into the corresponding expert unit respectively. The expert unit is used to further process the basic features, extract more specific age features, and finally obtain N age feature vectors.
[0149] S404: Input the obtained N age feature vectors into the age prediction module respectively to obtain N predicted ages output by the age prediction module.
[0150] S405: Among the N predicted ages, the predicted age with the smallest difference with the corresponding branch number is used as the prediction result of the face image to be detected.
[0151] For example, assuming N=3, if the unit prediction result output by the branch with branch number 15 is 16, the unit prediction result output by the branch with branch number 16 is 18, and the unit prediction result output by the branch with branch number 17 is 19, then because the difference between the unit prediction result 16 and the corresponding branch number 15 is the smallest, it is considered that the prediction result output by the age prediction unit connected to the branch with branch number 15 is closest to the true result and is the most reliable, and the predicted age of the face image to be detected is determined to be 16.
[0152] It can be seen that the above-mentioned gated age prediction method selects prediction results in a targeted manner and selects the most reliable results from all unit prediction results. The age prediction model and age prediction method have good generalization ability and can effectively improve the accuracy of age prediction.
[0153] Figure 5 FIG. 2 shows an internal structure diagram of a terminal device in an embodiment. Figure 5 As shown, the terminal device includes a processor, a memory and a network interface connected via a system bus. The memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium of the terminal device stores an operating system and may also store a computer program. When the computer program is executed by the processor, the processor can implement the age prediction method. The internal memory may also store a computer program. When the computer program is executed by the processor, the processor can implement the age prediction method. Those skilled in the art will understand that Figure 5The structure shown in the figure is only a block diagram of a partial structure related to the scheme of the present application, and does not constitute a limitation on the terminal device to which the scheme of the present application is applied. The specific terminal device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0154] A computer-readable storage medium stores a computer program, which implements the following steps when executed by a processor: obtaining multiple face training images with age labels; wherein the age labels are used to indicate the actual age of the faces in the face training images, and the multiple face training images cover all ages within a preset age range; inputting N face training images of different actual ages into a basic feature extraction module to extract basic features, and obtaining output basic feature vectors of N different actual ages; inputting the N basic feature vectors of different actual ages into a gating unit for output selection, and obtaining a basic feature vector of a target age output by the gating unit in a target branch; wherein the target branch is any one of the N branches, and different branches output basic feature vectors of different preset ages during training; and inputting the target branch into a basic feature extraction module to extract basic features, and obtaining output basic feature vectors of N different actual ages; and obtaining output basic feature vectors of N different actual ages; and obtaining output basic feature vectors of a target age output by the gating unit in a target branch; wherein the target branch is any one of the N branches, and different branches output basic feature vectors of different preset ages during training; and The basic feature vector of the marked age is input into the target expert unit to extract the feature representation of the target age, and the output age feature vector is obtained to obtain N age feature vectors; wherein the target expert unit is the expert unit connected to the target branch in the hybrid expert module; the N age feature vectors are respectively input into the age prediction module for prediction, and the output N predicted ages are obtained; through the preset loss function, the total loss value of each face training image is calculated according to the difference between the N actual ages, the N predicted ages and the age feature vectors, and the parameters of the age prediction model are iteratively adjusted according to the total loss value and the preset adjustment strategy until the age prediction model converges; wherein the preset adjustment strategy is to reduce the difference between the actual age and the predicted age of each face training image, and to increase the difference between the basic feature vectors of different actual ages.
[0155] And perform the following steps: obtain a face image to be detected, input the face image to be detected into a basic feature extraction module to extract basic features, and obtain the basic feature vector of the face image to be detected output; input the basic feature vector of the face image to be detected into a gate control unit, and obtain N basic feature vectors output from all branches in the gate control unit; wherein all branches in the gate control unit are currently opened, and one branch has a branch number, and each branch number is used to indicate the actual age of the basic feature vector input during training; input the basic feature vector output from the target branch into the corresponding target expert unit, and obtain the age feature vector output by the target expert unit to obtain N age feature vectors; respectively input the obtained N age feature vectors into the age prediction module to obtain N predicted ages output by the age prediction module; among the N predicted ages, the predicted age with the smallest difference with the corresponding branch number is used as the prediction result of the face image to be detected.
[0156] A terminal device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the following steps when executing the computer program: obtaining a plurality of face training images with age labels; wherein the age labels are used to indicate the actual ages of faces in the face training images, and the plurality of face training images cover all ages within a preset age range; inputting N face training images of different actual ages into a basic feature extraction module to extract basic features, and obtaining output basic feature vectors of N different actual ages; inputting the N basic feature vectors of different actual ages into a gating unit for output selection, and obtaining a basic feature vector of a target age output by the gating unit in a target branch; wherein the target branch is any one of the N branches, and different branches output basic feature vectors of different preset ages during training. The invention relates to a method for obtaining a face prediction model of an age prediction model. The method comprises the following steps: inputting the basic feature vector of the target age into the target expert unit to extract the feature representation of the target age, obtaining the output age feature vector, and obtaining N age feature vectors; wherein the target expert unit is an expert unit connected to the target branch in the hybrid expert module; respectively inputting the N age feature vectors into the age prediction module for prediction, and obtaining the output N predicted ages; calculating the total loss value of each face training image according to the difference between the N actual ages, the N predicted ages and the age feature vectors through a preset loss function, iteratively adjusting the parameters of the age prediction model according to the total loss value and the preset adjustment strategy until the age prediction model converges; wherein the preset adjustment strategy is to reduce the difference between the actual age and the predicted age of each face training image, and to increase the difference between the basic feature vectors of different actual ages.
[0157] And perform the following steps: obtain a face image to be detected, input the face image to be detected into a basic feature extraction module to extract basic features, and obtain the basic feature vector of the face image to be detected output; input the basic feature vector of the face image to be detected into a gate control unit, and obtain N basic feature vectors output from all branches in the gate control unit; wherein all branches in the gate control unit are currently opened, and one branch has a branch number, and each branch number is used to indicate the actual age of the basic feature vector input during training; input the basic feature vector output from the target branch into the corresponding target expert unit, and obtain the age feature vector output by the target expert unit to obtain N age feature vectors; respectively input the obtained N age feature vectors into the age prediction module to obtain N predicted ages output by the age prediction module; among the N predicted ages, the predicted age with the smallest difference with the corresponding branch number is used as the prediction result of the face image to be detected.
[0158] It should be noted that the above-mentioned age prediction method, model training method, medium and device belong to a general inventive concept, and the contents in the embodiments of the age prediction method, model training method, medium and device are applicable to each other.
[0159] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a non-volatile computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0160] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0161] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A training method for an age prediction model, characterized in that: The age prediction model comprises a basic feature extraction module, a gate control unit, a hybrid expert module and an age prediction module which are connected in sequence, the gate control unit comprises N branches, the hybrid expert module comprises N expert units, one branch is connected to one expert unit, N is the total number of ages in a preset age range, and the method comprises: Acquire multiple face training images with age labels, wherein the age labels are used to indicate the actual ages of the faces in the face training images, and the multiple face training images cover all ages within a preset age range; Inputting N face training images of different actual ages into the basic feature extraction module to extract basic features, and obtaining output basic feature vectors of N different actual ages; Inputting N basic feature vectors of different actual ages into the gate control unit for output selection, and obtaining the basic feature vector of the target age output by the gate control unit in the target branch; wherein the target branch is any one of the N branches, and different branches output basic feature vectors of different preset ages during training; Inputting the basic feature vector of the target age into the target expert unit to extract the feature representation of the target age, obtaining the output age feature vector, and obtaining N age feature vectors; wherein the target expert unit is the expert unit connected to the target branch in the hybrid expert module; Inputting the N age feature vectors into the age prediction module for prediction respectively, and obtaining output N predicted ages; Through a preset loss function, a total loss value of each face training image is calculated according to the difference between N actual ages, N predicted ages and the age feature vectors, and the parameters of the age prediction model are iteratively adjusted according to the total loss value and a preset adjustment strategy until the age prediction model converges; wherein the preset adjustment strategy is to reduce the difference between the actual age and the predicted age of each face training image and to increase the difference between the basic feature vectors of different actual ages.
2. The method according to claim 1, characterized in that The preset loss function includes an age difference loss function and a cross entropy loss function. The preset loss function is used to calculate the total loss value of each face training image according to the difference between N actual ages, N predicted ages and age feature vectors, including: By using the age difference loss function, an age loss value is calculated according to the actual age of each face training image and the corresponding output predicted age; wherein the size of the age loss value is positively correlated with the age difference between the actual age and the predicted age; The cross entropy loss function is used to calculate the cross entropy loss value according to the vector difference between the age feature vector of each face training image and the age feature vectors of M adjacent actual ages; wherein the size of the cross entropy loss value is negatively correlated with the size of the vector difference, M≥2; The sum of the age loss value and the cross entropy loss value is taken as the total loss value.
3. The method according to claim 2, characterized in that The age difference loss function is expressed as: Among them, CELoss represents the age loss value, y represents the actual age, represents the predicted age; The cross entropy loss function is expressed as: Wherein, AdaLoss represents the cross entropy loss value, represents the age feature vector of the face training image with actual age i, Represents the age feature vector of the face training image with actual age j.
4. The method according to claim 1, characterized in that: The basic feature extraction module includes I1 basic feature extraction units and a fully connected layer connected in sequence, and inputting N face training images of different actual ages into the basic feature extraction module to obtain N basic feature vectors of different actual ages output by the basic feature extraction module, including: Input the (i-1)th layer feature map to the i-th basic feature extraction unit in the basic feature extraction module for feature extraction, and obtain the i-th layer feature map as output; wherein, 1≤i≤I1, when i=1, the (i-1)th layer feature map is a target face training image, and the target face training image is any one of N face training images of different actual ages; The output I1th layer feature map is converted into a basic feature vector of the target face training image through a fully connected layer and output, so as to obtain basic feature vectors of N different actual ages.
5. The method according to claim 1, characterized in that The age prediction model also includes I2 layers of hybrid attention modules connected in sequence, each layer of the hybrid attention module includes a multi-head attention mechanism layer, a forward layer module and a normalization layer connected in sequence, and the multi-layer hybrid attention module is arranged between the basic feature extraction module and the gating unit. The method also includes: Input the (i-1)th layer feature vector into the multi-head attention mechanism layer of the i-th layer hybrid attention module to dynamically allocate attention weights, and obtain the output i-th layer attention feature vector; where 1≤i≤I2, when i=1, the (i-1)th layer feature vector is any one of the N basic feature vectors; Input the i-th layer attention feature vector into the forward layer module of the i-th layer hybrid attention module for nonlinear transformation and weight update, and obtain the output i-th layer forward feature vector; The i-th layer forward feature vector is input into the normalization layer of the i-th layer hybrid attention module for normalization calculation to obtain the output i-th layer normalized vector; wherein the i-th layer normalized vector has the same feature size as the (i-1)-th layer feature vector.
6. The method according to claim 1, characterized in that A branch includes a gate-controlled switch and a branch number, the branch number of a branch is any one of N different actual ages, and the branch numbers of different branches are different. The basic feature vectors of N different actual ages are input into the gate control unit for output selection, and the basic feature vector of the target age output by the gate control unit in the target branch is obtained, including: When a basic feature vector is input into the target branch, determining whether the actual age of the currently input basic feature vector is the same as the branch number of the target branch; If the actual age of the currently input basic feature vector is different from the branch number of the target branch, the gated switch of the target branch is controlled to be closed; If the actual age of the currently input basic feature vector is the same as the branch number of the target branch, the gated switch of the target branch is controlled to be turned on, and the currently input basic feature vector is output as the basic feature vector of the target age.
7. The method according to claim 1, characterized in that The age prediction module comprises an input layer, a plurality of hidden layers and an output layer connected in sequence, and the N age feature vectors are respectively input into the age prediction module for prediction to obtain the output N predicted ages, including: Inputting the age feature vector of the target age into the input layer to obtain an output initial feature vector; Input the (i-1)th intermediate feature vector into the i-th hidden layer for nonlinear transformation and feature extraction, and obtain the i-th intermediate feature vector as output; wherein 1≤i≤h, h is the number of hidden layers, and when i=1, the (i-1)th intermediate feature vector is the initial feature vector; The hth intermediate feature vector is input into the output layer for conversion, and the predicted age of the corresponding output is obtained to obtain N predicted ages.
8. An age prediction method, characterized in that: An age prediction model trained by the method according to any one of claims 1 to 7, the method comprising: Acquire a face image to be detected, input the face image to be detected into the basic feature extraction module to extract basic features, and obtain an output basic feature vector of the face image to be detected; Input the basic feature vector of the face image to be detected into the gate control unit, and obtain N basic feature vectors output from all branches in the gate control unit; wherein all branches in the gate control unit are currently opened, each branch has a branch number, and each branch number is used to indicate the actual age of the basic feature vector input during training; Inputting the basic feature vector output from the target branch into the corresponding target expert unit, obtaining the age feature vector output by the target expert unit, so as to obtain N age feature vectors; Inputting the obtained N age feature vectors into the age prediction module respectively to obtain N predicted ages output by the age prediction module; Among the N predicted ages, the predicted age with the smallest difference from the corresponding branch number is used as the prediction result of the face image to be detected.
9. A computer-readable storage medium, characterized in that: A computer program is stored, and when the computer program is executed by a processor, the processor is caused to perform the steps of the method according to any one of claims 1 to 8.
10. A terminal device, characterized in that: The method comprises a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Gated age prediction method, model training method, equipment and medium
CN115359538A
Lifelong pretraining of mixture-of-experts neural networks
WO2024159132A1
Cited By
Face deep counterfeiting detection method and system based on hybrid expert network
CN122473829A