A Pig Face Recognition Method Based on Median Attention Residual Network
Through the median attention residual network MAM_ResNet50, the problems of high individual similarity, complex environment and difficulty in multi-angle recognition in pig face recognition are solved, and the accuracy and robustness of pig face recognition are improved.
Patent Information
- Application Number
- CN202210953320.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-09
- Publication Date
- 2025-08-08
- Estimated Expiration
- 2042-08-09
AI Technical Summary
The prior art has problems such as high individual similarity, complex environment, easy to conceal facial features, and difficulty in multi-angle recognition in pig face recognition, resulting in low recognition accuracy and poor reliability.
The median attention residual network MAM_ResNet50 is used to train the model through the training set, and an attention mechanism of median compression is added to the residual network to assign different channel weights to improve the accuracy of feature extraction and recognition.
It improves the recognition accuracy of pig face recognition in multiple angles and complex environments, enhances the robustness and accuracy of recognition, and adapts to practical application needs.
Smart Images

Figure CN115188029B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of pattern recognition technology, and in particular to a pig face recognition method based on a median attention residual network. Background Art
[0002] Pattern recognition technology has been widely used in the pig farming industry. Pig face recognition and classification using image processing and deep learning methods has become a hot research topic in recent years. Pig face recognition is also a key component of intelligent pig farming and pig disease prevention and control, playing a decisive role in promoting AI-powered pig farming.
[0003] Pigs are common poultry, and there are certain differences between different individuals. In theory, the pattern recognition methods in deep learning can be directly transferred to pig face recognition. However, pig face recognition has some special problems, including the following:
[0004] First, pigs often give birth to multiple children from one litter, and the similarity between individual pigs is relatively high. When traditional pattern recognition methods are applied to similar individuals, the recognition accuracy drops sharply, and a pattern recognition method with better classification effect is needed.
[0005] Second, the environment in which pigs live is relatively complex. When performing pig face recognition, changes in the surrounding environment will to some extent affect the reliability and accuracy of recognition. Therefore, a robust algorithm is required to complete pig face recognition.
[0006] Third, pig faces are often dirty, which can obscure some facial features, interfere with feature extraction, and increase the difficulty of recognition. Better pattern recognition algorithms are needed to solve this problem.
[0007] Fourth, when performing pig face recognition, pigs will not cooperate in data collection, so multi-angle feature extraction and recognition should be considered when performing pig face recognition. This issue should be considered when collecting databases to better meet actual application needs. At the same time, multi-angle recognition also has higher requirements for pattern recognition algorithms.
[0008] These issues can lead to low accuracy and reliability when directly transferring deep learning pattern recognition methods to pig face recognition. Therefore, a better pig face recognition method is urgently needed to address these issues and better perform pig face recognition in practical application scenarios, laying the foundation for AI-powered pig farming and pig disease prevention and control. Summary of the Invention
[0009] The purpose of the present invention is to solve the problems existing in the above-mentioned prior art. Specifically, a median attention residual network (MAM_ResNet50) is used as a recognition model. The training set is used to train MAM_ResNet50. The test set is used to select the MAM_ResNet50 with the best performance as the pig face recognition model to complete the pig face recognition task. Compared with the traditional method, the method of the present invention adds an attention mechanism based on median compression to the residual network, assigns different channel weights to correct the channel values, and increases the degree of attention to key tasks, thereby improving the accuracy of pig face recognition from multiple angles, dirty pig faces, and complex environments.
[0010] To achieve the above objectives, the present invention provides a pig face recognition method based on a median attention residual network, comprising obtaining a pig face recognition model with optimal recognition performance by training a median attention residual network, and using the pig face recognition model to recognize pig face images, specifically comprising the following steps:
[0011] Step 1: Build training and test sets for pig face recognition
[0012] Collect M types of pig face images, where M is the number of pig face categories;
[0013] Randomly select A pig face pictures from each category of M types of pig face pictures to obtain M×A pig face pictures, then perform image enhancement on the M×A pig face pictures using B image preprocessing methods, and retain the pig face pictures obtained by each image processing method, that is, a total of A×M×(B+1) pig face pictures are obtained, and the A×M×(B+1) pig face pictures constitute the training set;
[0014] Select C additional pig face pictures from each of the M categories of pig face pictures to obtain M×C pig face pictures. The M×C pig face pictures constitute a test set, A≠C;
[0015] Step 2: Train the median attention residual network to obtain the optimal classification effect model, and record the optimal classification effect model as the pig face recognition model. The specific steps are as follows:
[0016] Step 2.1: Select the residual network ResNet50 as the backbone network, add the attention mechanism based on median compression to the backbone network, assign different channel weights to correct the channel values, and obtain the median attention residual network, which is recorded as MAM_ResNet50.
[0017] The structure of the MAM_ResNet50 includes: a convolution layer Ω1 with a convolution matrix size of 7×7, a convolution stride of 2, and a channel number of 64, a maximum pooling layer Λ1 with a convolution matrix size of 3×3 and a convolution stride of 2, a first-level median attention residual unit Φ1 obtained by connecting three identical median attention residual structures 1 in series along the input-output direction of MAM_ResNet50, a second-level median attention residual unit Φ2 obtained by connecting four identical median attention residual structures 2 in series along the input-output direction of MAM_ResNet50, a third-level median attention residual unit Φ3 obtained by connecting six identical median attention residual structures 3 in series along the input-output direction of MAM_ResNet50, a fourth-level median attention residual unit Φ4 obtained by connecting three identical median attention residual structures 4 in series along the input-output direction of MAM_ResNet50, and a global average pooling layer Λ2;
[0018] The input of MAM_ResNet50 is the convolutional layer Ω1, the maximum pooling layer Λ1, the first-level median attention residual unit Φ1, the second-level median attention residual unit Φ2, the third-level median attention residual unit Φ3, the fourth-level median attention residual unit Φ4 and the global average pooling layer Λ2;
[0019] The median attention residual structure 1 is obtained by serially connecting the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 64 channels, a convolutional layer with a convolution matrix size of 3×3 and 64 channels, and a convolutional layer with a convolution matrix size of 1×1 and 256 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 256 channels.
[0020] The median attention residual structure 2 is obtained by cascading the following three convolutional layers in the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 128 channels, a convolutional layer with a convolution matrix size of 3×3 and 128 channels, and a convolutional layer with a convolution matrix size of 1×1 and 512 channels. The median compression-based attention mechanism is inserted after the convolutional layer with a convolution matrix size of 1×1 and 512 channels.
[0021] The median attention residual structure 3 is obtained by cascading the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 256 channels, a convolutional layer with a convolution matrix size of 3×3 and 256 channels, and a convolutional layer with a convolution matrix size of 1×1 and 1024 channels. The median compression-based attention mechanism is inserted after the convolutional layer with a convolution matrix size of 1×1 and 1024 channels.
[0022] The median attention residual structure 4 is obtained by cascading the following three convolutional layers in the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 512 channels, a convolutional layer with a convolution matrix size of 3×3 and 512 channels, and a convolutional layer with a convolution matrix size of 1×1 and 2048 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 2048 channels.
[0023] The number of channels of the convolutional layer before the median compression-based attention mechanism is inserted is denoted by δ, where δ is equal to 256, 512, 1024, or 2048.
[0024] Step 2.2: Use the training set to optimize the MAM_ResNet50 described in step 2.1. The specific steps are as follows:
[0025] Step 2.2.1: Use image normalization to uniformly adjust the pixels of all pig face images in the training set to I×I;
[0026] Step 2.2.2, randomly select J pig face images from the training set, and record the sequence number of any pig face image as x, x = 1, 2, ..., J, and record the true one-hot encoding sequence of the x-th pig face image as H x , Among them, q is the real one-hot encoding sequence H x The serial number of each true one-hot encoded value in , q = 1, 2, ..., M, Indicates that the pig face image with serial number x is in the real one-hot encoding sequence H x The qth true one-hot encoding value in ;
[0027] The true one-hot encoding sequence H x The value of is defined as: And at this time All other true one-hot encoding values in are equal to 0;
[0028] Step 2.2.3: Input the J pig face images obtained in step 2.2.2 into MAM_ResNet50 and output the predicted one-hot encoding sequence X for each pig face image. x , Where p is the predicted one-hot encoding sequence X x The number of each predicted one-hot encoding value in, p = 1, 2, ..., M, Indicates that the pig face image with serial number x is predicting the one-hot encoding sequence Xx The p-th predicted one-hot encoding value in ;
[0029] Step 2.2.4, according to the real one-hot encoding sequence H x and predict one-hot encoding sequence X x , through the back-propagation operation, the network weights of MAM_ResNet50 are updated and optimized as follows:
[0030] Calculate the true one-hot encoding sequence H x and predict one-hot encoding sequence X x The loss function between Among them, ||·||2 represents the Euclidean norm;
[0031] The loss function is minimized through gradient descent, and the weights of the connections between neural network elements are updated through backpropagation to complete the training of MAM_ResNet50.
[0032] Step 2.2.5: Repeat steps 2.2.2 to 2.2.4 until all pig face images in the training set have been selected. If the number of pig face images remaining in the training set is less than J in the last round of selection, then randomly select pig face images from the already selected pig face images to supplement the training set.
[0033] The MAM_ResNet50 trained through steps 2.2.2 to 2.2.5 is called the MAM_ResNet50 of the Lth round and is recorded as network T L , where L is the round number, L = 1, 2, ... G, G is the number of repetitions;
[0034] Step 2.3, calculate the network T through the test set L The accuracy of the test set classification P L , For network T L The correct value for the classification of pig face images in the test set,
[0035] Step 2.4, repeat steps 2.2 to 2.3 G times to obtain a network set T and an accuracy set P, T = {T1, T2, ..., T L ,...,T G}, P = {P1, P2, ..., P L ,...,P G};
[0036] Step 2.5: Take the maximum accuracy value in the accuracy set P and record it as the maximum accuracy Pmax , take the maximum accuracy P in the network set T max The corresponding MAM_ResNet50 network is taken as the MAM_ResNet50 network with the best recognition performance and is recorded as the pig face recognition model;
[0037] Step 3: Implement pig face recognition through the pig face recognition model. The specific steps are as follows:
[0038] Step 3.1: Input the pig face image to be identified into the pig face recognition model. The pig face recognition model extracts the features of the pig face image to be identified and generates a feature sequence K of the pig face image to be identified, where K = (k1, k2, ..., k w ,...,kz), w represents the dimension of the feature sequence of the pig face image to be identified, k w represents the value of the w-th dimension of the pig face image feature sequence to be identified, w = 1, 2, ..., Z;
[0039] Step 3.2, take k τ =max{k1, k2, ..., k w ,...,k Z}, the dimension of the pig face image feature sequence K corresponding to τ is the number of categories of pig faces to be identified, k τ The value of is the confidence level of the category number.
[0040] Preferably, the B image preprocessing methods in step 1 are 9 methods, that is, B=9, specifically including: contrast enhancement, noise perturbation, mirroring, histogram equalization, image sharpening, image rotation, illumination reduction, illumination enhancement, and random color.
[0041] Preferably, the steps for implementing the attention mechanism based on median compression in step 2.1 and assigning different channel weights to correct channel values are as follows:
[0042] According to step 2.1, the input of the attention mechanism based on median compression is the two-dimensional channel feature vector of δ channels. For each two-dimensional channel feature vector, the end and end are connected and flattened to obtain (2λ+1) one-dimensional channel feature vectors, where γ represents the sequence number of the input channel, γ = 1, 2, ..., δ;
[0043] Any two-dimensional channel feature vector in the δ channels is recorded as the two-dimensional channel feature vector η γ , and its corresponding flattened vector is S γ , i represents the one-dimensional channel feature vector value in the flattened vector S γ The serial number in Represents the flattened vector S γThe i-th one-dimensional channel feature vector value of the γ-th channel in is recorded as the one-dimensional channel feature vector value
[0044] Flatten the vector S γ The one-dimensional channel feature vector value in Sort in ascending order to get the ordered flattened vector U γ , j represents the one-dimensional channel feature vector value in the ordered flattened vector U γ The serial number in represents the ordered flattened vector U γ The j-th one-dimensional channel feature vector value of the γ-th channel in , j = 1, 2, ..., λ+1, ..., 2λ+1;
[0045] For the ordered flattened vector U γ , take its median value As the channel descriptor θ of the γth channel γ , the channel descriptor θ γ Input the fully connected V-shaped module to obtain the median compression-based attention mechanism ρ of the γth channel γ ; The attention mechanism ρ based on median compression of the γth channel γ and the two-dimensional channel feature vector η of the γth channel γ Performing point multiplication achieves the purpose of assigning different channel weights to correct the channel values;
[0046] The specific implementation of the fully connected V-shaped module is as follows:
[0047] For channel descriptor θ γ , first passes through a fully connected layer with δ nodes, then passes through a fully connected layer with δ / β nodes, then passes through the ReLU function, then passes through a fully connected layer with δ nodes, and finally passes through the Sigmoid function, where β is an integer greater than 0;
[0048] The expression of the ReLU function is as follows:
[0049]
[0050] in, Represents the input of the ReLU function, Represents the output of the ReLU function;
[0051] The expression of the Sigmoid function is as follows:
[0052]
[0053] Where e represents the exponential function, ω represents the input of the Sigmoid function, and y(ω) represents the output of the Sigmoid function.
[0054] Compared with the prior art, the present invention has the following beneficial effects:
[0055] 1. This paper takes into account that the traditional residual network does not pay enough attention to the relationship between channels, resulting in insufficient feature extraction. This paper proposes to add an attention mechanism to the residual network and assign weights to different channels to achieve more sufficient feature extraction.
[0056] 2. The attention mechanism proposed in this invention adopts a more effective data compression method, namely, the median data compression method, to obtain a channel descriptor that can better represent the channel information. This compression method can improve the performance of the attention mechanism, thereby better improving the accuracy of network classification when inserted into the residual network.
[0057] 3. The attention residual network proposed in this invention can deal with the problems of individual pig similarity and multi-angle problems in pig face recognition, and has good classification capabilities for these tasks. BRIEF DESCRIPTION OF THE DRAWINGS
[0058] Figure 1 is a flow chart of the pig face recognition method of the present invention;
[0059] Figure 2 It is a schematic block diagram of the pig face recognition method of the present invention;
[0060] Figure 3 Schematic diagram of the steps for implementing the attention mechanism based on median compression in an embodiment of the present invention;
[0061] Figure 4 3 is a comparison chart of the accuracy of the pig face recognition method of the present invention and other methods in an embodiment of the present invention. DETAILED DESCRIPTION
[0062] The technical solution of the present invention will be clearly and completely described below with reference to the accompanying drawings and specific implementation methods.
[0063] Figure 1 is a flow chart of the pig face recognition method of the present invention, Figure 2 is a schematic block diagram of the pig face recognition method of the present invention, which consists of Figure 1 and Figure 2 It can be seen that the present invention provides a pig face recognition method based on the median attention residual network, including obtaining a pig face recognition model with optimal recognition performance by training the median attention residual network, and using the pig face recognition model to realize the recognition of pig face images, specifically comprising the following steps:
[0064] Step 1: Build training and test sets for pig face recognition
[0065] Collect M types of pig face images, where M is the number of pig face categories;
[0066] Randomly select A pig face pictures from each category of M types of pig face pictures to obtain M×A pig face pictures, then perform image enhancement on the M×A pig face pictures using B image preprocessing methods, and retain the pig face pictures obtained by each image processing method, that is, a total of A×M×(B+1) pig face pictures are obtained, and the A×M×(B+1) pig face pictures constitute the training set;
[0067] Select C pig face pictures from each category of M types of pig face pictures to obtain M×C pig face pictures. The M×C pig face pictures constitute a test set, A≠C.
[0068] In this example, M=30; A=5; C=10.
[0069] In this example, the B image processing methods are 9 processing methods, that is, B=9, specifically including: contrast enhancement, noise perturbation, mirroring, histogram equalization, image sharpening, image rotation, illumination reduction, illumination enhancement, and random color.
[0070] Step 2: Train the median attention residual network to obtain the optimal classification effect model, and record the optimal classification effect model as the pig face recognition model. The specific steps are as follows:
[0071] In step 2.1, the residual network ResNet50 is selected as the backbone network. The attention mechanism based on median compression is added to the backbone network. Different channel weights are assigned to correct the channel values to obtain the median attention residual network, which is recorded as MAM_ResNet50.
[0072] The structure of the MAM_ResNet50 includes: a convolution layer Ω1 with a convolution matrix size of 7×7, a convolution stride of 2, and a channel number of 64, a maximum pooling layer Λ1 with a convolution matrix size of 3×3 and a convolution stride of 2, a first-level median attention residual unit Φ1 obtained by connecting three identical median attention residual structures 1 in series along the input-output direction of MAM_ResNet50, a second-level median attention residual unit Φ2 obtained by connecting four identical median attention residual structures 2 in series along the input-output direction of MAM_ResNet50, a third-level median attention residual unit Φ3 obtained by connecting six identical median attention residual structures 3 in series along the input-output direction of MAM_ResNet50, a fourth-level median attention residual unit Φ4 obtained by connecting three identical median attention residual structures 4 in series along the input-output direction of MAM_ResNet50, and a global average pooling layer Λ2.
[0073] The input of MAM-ResNet50 is the convolutional layer Ω1, the maximum pooling layer Λ1, the first-level median attention residual unit Φ1, the second-level median attention residual unit Φ2, the third-level median attention residual unit Φ3, the fourth-level median attention residual unit Φ4 and the global average pooling layer Λ2.
[0074] The median attention residual structure 1 is obtained by serially connecting the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 64 channels, a convolutional layer with a convolution matrix size of 3×3 and 64 channels, and a convolutional layer with a convolution matrix size of 1×1 and 256 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 256 channels.
[0075] The median attention residual structure 2 is obtained by serially connecting the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 128 channels, a convolutional layer with a convolution matrix size of 3×3 and 128 channels, and a convolutional layer with a convolution matrix size of 1×1 and 512 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 512 channels.
[0076] The median attention residual structure 3 is obtained by serially connecting the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 256 channels, a convolutional layer with a convolution matrix size of 3×3 and 256 channels, and a convolutional layer with a convolution matrix size of 1×1 and 1024 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 1024 channels.
[0077] The median attention residual structure 4 is obtained by connecting the following three convolutional layers in series along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 512 channels, a convolutional layer with a convolution matrix size of 3×3 and 512 channels, and a convolutional layer with a convolution matrix size of 1×1 and 2048 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 2048 channels.
[0078] The number of channels of the convolutional layer before the median compression-based attention mechanism is inserted is denoted as δ, where δ is equal to 256, 512, 1024, or 2048.
[0079] Figure 3 This is a schematic diagram of the steps for implementing the attention mechanism based on median compression in an embodiment of the present invention. Figure 3It can be seen that in the example of the present invention, the steps for implementing the attention mechanism based on median compression in step 2.1 to correct channel values by assigning different channel weights are as follows:
[0080] According to step 2.1, the input of the attention mechanism based on median compression is a two-dimensional channel feature vector of δ channels. For each two-dimensional channel feature vector, the end to end is connected and flattened to obtain a one-dimensional channel feature vector of (2λ+1), where γ represents the sequence number of the input channel, γ=1, 2, ..., δ.
[0081] Any two-dimensional channel feature vector in the δ channels is recorded as the two-dimensional channel feature vector η γ , and its corresponding flattened vector is S γ , i represents the one-dimensional channel feature vector value in the flattened vector S γ The serial number in Represents the flattened vector S γ The i-th one-dimensional channel feature vector value of the γ-th channel in is recorded as the one-dimensional channel feature vector value
[0082] Flatten the vector S γ The one-dimensional channel feature vector value in Sort in ascending order to get the ordered flattened vector U γ , j represents the one-dimensional channel feature vector value in the ordered flattened vector U γ The serial number in represents the ordered flattened vector U γ The j-th one-dimensional channel feature vector value of the γ-th channel in , j = 1, 2, ..., λ+1, ..., 2λ+1.
[0083] For the ordered flattened vector U γ , take its median value As the channel descriptor θ of the γth channel γ , the channel descriptor θ γ Input the fully connected V-shaped module to obtain the median compression-based attention mechanism ρ of the γth channel γ ; The attention mechanism ρ based on median compression of the γth channel γ and the two-dimensional channel feature vector η of the γth channel γ By performing point multiplication, the purpose of assigning different channel weights to correct the channel values is achieved.
[0084] The specific implementation of the fully connected V-shaped module is as follows:
[0085] For channel descriptor θγ , first passes through a fully connected layer with δ nodes, then passes through a fully connected layer with δ / β nodes, then passes through the ReLU function, then passes through a fully connected layer with δ nodes, and finally passes through the Sigmoid function, where β is an integer greater than 0;
[0086] The expression of the ReLU function is as follows:
[0087]
[0088] in, Represents the input of the ReLU function, Represents the output of the ReLU function;
[0089] The expression of the Sigmoid function is as follows:
[0090]
[0091] Where e represents the exponential function, ω represents the input of the Sigmoid function, and y(ω) represents the output of the Sigmoid function.
[0092] Step 2.2: Use the training set to optimize the MAM_ResNet50 described in step 2.1. The specific steps are as follows:
[0093] Step 2.2.1: Use image normalization to uniformly adjust the pixels of all pig face images in the training set to I×I;
[0094] Step 2.2.2, randomly select J pig face images from the training set, and record the sequence number of any pig face image as x, x = 1, 2, ..., J, and record the true one-hot encoding sequence of the x-th pig face image as H x , Among them, q is the real one-hot encoding sequence H x The serial number of each true one-hot encoded value in , q = 1, 2, ..., M, Indicates that the pig face image with serial number x is in the real one-hot encoding sequence H x The qth true one-hot encoding value in ;
[0095] The true one-hot encoding sequence H x The value of is defined as: And at this time All other true one-hot encoding values in are equal to 0;
[0096] Step 2.2.3: Input the J pig face images obtained in step 2.2.2 into MAM_ResNet50 and output the predicted one-hot encoding sequence X for each pig face image. x , Where p is the predicted one-hot encoding sequence X x The number of each predicted one-hot encoding value in, p = 1, 2, ..., M, Indicates that the pig face image with serial number x is predicting the one-hot encoding sequence X x The p-th predicted one-hot encoding value in ;
[0097] Step 2.2.4, according to the real one-hot encoding sequence H x and predict one-hot encoding sequence X x , through the back-propagation operation, the network weights of MAM_ResNet50 are updated and optimized as follows:
[0098] Calculate the true one-hot encoding sequence H x and predict one-hot encoding sequence X x The loss function between Among them, ||·||2 represents the Euclidean norm;
[0099] The loss function is minimized through gradient descent, and the weights of the connections between neural network elements are updated through backpropagation to complete the training of MAM_ResNet50.
[0100] Step 2.2.5: Repeat steps 2.2.2 to 2.2.4 until all pig face images in the training set have been selected. If the number of pig face images remaining in the training set is less than J in the last round of selection, then randomly select pig face images from the already selected pig face images to supplement the training set.
[0101] The MAM_ResNet50 trained through steps 2.2.2 to 2.2.5 is called the MAM_ResNet50 of the Lth round and is recorded as network T L , where L is the round number, L = 1, 2, ... G, G is the number of repetitions;
[0102] Step 2.3, calculate the network T through the test set L The accuracy of the test set classification P L , For network T L The correct value for the classification of pig face images in the test set,
[0103] Step 2.4, repeat steps 2.2 to 2.3 G times to obtain a network set T and an accuracy set P, T = {T1, T2, ..., T L ,...,T G}, P = {P1, P2, ..., P L ,...,P G};
[0104] Step 2.5: Take the maximum accuracy value in the accuracy set P and record it as the maximum accuracy P max , take the maximum accuracy P in the network set T mzx The corresponding MAM_ResNet50 network is taken as the MAM_ResNet50 network with the best recognition performance and is recorded as the pig face recognition model.
[0105] In this example, J=12; G=100.
[0106] Step 3: Implement pig face recognition through the pig face recognition model. The specific steps are as follows:
[0107] Step 3.1: Input the pig face image to be identified into the pig face recognition model. The pig face recognition model extracts the features of the pig face image to be identified and generates a feature sequence K of the pig face image to be identified, where K = (k1, k2, ..., k w ,...,k Z ), w represents the dimension of the feature sequence of the pig face image to be identified, k w represents the value of the w-th dimension of the pig face image feature sequence to be identified, w = 1, 2, ..., Z;
[0108] Step 3.2, take k τ =max{k1, k2, ..., k w ,...,k Z}, the dimension of the pig face image feature sequence K corresponding to τ is the number of categories of pig faces to be identified, k τ The value of is the confidence level of the category number.
[0109] Figure 4 This is a comparison chart of the accuracy of a pig face recognition method based on a median attention residual network in an embodiment of the present invention and other latest recognition algorithms, including ResNet50, MobileNet V2, RegNet, ShuffleNet V2 and MobileNet V3. Figure 4 It can be seen that the accuracy curve corresponding to the method of the present invention is better than other latest recognition algorithms, which illustrates the superiority of the recognition method proposed by the present invention in the field of pig face recognition.
Claims
1. A pig face recognition method based on median attention residual network, characterized in that: The method includes obtaining a pig face recognition model with optimal recognition performance by training a median attention residual network, and using the pig face recognition model to recognize pig face images, specifically including the following steps: Step 1: Build training and test sets for pig face recognition Collect M types of pig face images, where M is the number of pig face categories; Randomly select A pig face pictures from each category of M types of pig face pictures to obtain M×A pig face pictures, then perform image enhancement on the M×A pig face pictures using B image preprocessing methods, and retain the pig face pictures obtained by each image processing method, that is, a total of A×M×(B+1) pig face pictures are obtained, and the A×M×(B+1) pig face pictures constitute the training set; Select C additional pig face pictures from each of the M categories of pig face pictures to obtain M×C pig face pictures. The M×C pig face pictures constitute a test set, A≠C; Step 2: Train the median attention residual network to obtain the optimal classification effect model, and record the optimal classification effect model as the pig face recognition model. The specific steps are as follows: Step 2.1: Select the residual network ResNet50 as the backbone network, add the attention mechanism based on median compression to the backbone network, assign different channel weights to correct the channel values, and obtain the median attention residual network, which is recorded as MAM_ResNet50. The structure of the MAM_ResNet50 includes: a convolution layer Ω1 with a convolution matrix size of 7×7, a convolution stride of 2, and a channel number of 64, a maximum pooling layer Λ1 with a convolution matrix size of 3×3 and a convolution stride of 2, a first-level median attention residual unit Φ1 obtained by connecting three identical median attention residual structures 1 in series along the input-output direction of MAM_ResNet50, a second-level median attention residual unit Φ2 obtained by connecting four identical median attention residual structures 2 in series along the input-output direction of MAM_ResNet50, a third-level median attention residual unit Φ3 obtained by connecting six identical median attention residual structures 3 in series along the input-output direction of MAM_ResNet50, a fourth-level median attention residual unit Φ4 obtained by connecting three identical median attention residual structures 4 in series along the input-output direction of MAM_ResNet50, and a global average pooling layer Λ2; The input of MAM_ResNet50 is the convolutional layer Ω1, the maximum pooling layer Λ1, the first-level median attention residual unit Φ1, the second-level median attention residual unit Φ2, the third-level median attention residual unit Φ3, the fourth-level median attention residual unit Φ4 and the global average pooling layer Λ2; The median attention residual structure 1 is obtained by serially connecting the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 64 channels, a convolutional layer with a convolution matrix size of 3×3 and 64 channels, and a convolutional layer with a convolution matrix size of 1×1 and 256 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 256 channels. The median attention residual structure 2 is obtained by cascading the following three convolutional layers in the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 128 channels, a convolutional layer with a convolution matrix size of 3×3 and 128 channels, and a convolutional layer with a convolution matrix size of 1×1 and 512 channels. The median compression-based attention mechanism is inserted after the convolutional layer with a convolution matrix size of 1×1 and 512 channels. The median attention residual structure 3 is obtained by cascading the following three convolutional layers along the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 256 channels, a convolutional layer with a convolution matrix size of 3×3 and 256 channels, and a convolutional layer with a convolution matrix size of 1×1 and 1024 channels. The median compression-based attention mechanism is inserted after the convolutional layer with a convolution matrix size of 1×1 and 1024 channels. The median attention residual structure 4 is obtained by cascading the following three convolutional layers in the input-output direction of MAM_ResNet50: a convolutional layer with a convolution matrix size of 1×1 and 512 channels, a convolutional layer with a convolution matrix size of 3×3 and 512 channels, and a convolutional layer with a convolution matrix size of 1×1 and 2048 channels. The attention mechanism based on median compression is inserted after the convolutional layer with a convolution matrix size of 1×1 and 2048 channels. The number of channels of the convolutional layer before the median compression-based attention mechanism is inserted is denoted by δ, where δ is equal to 256, 512, 1024, or 2048. Step 2.2: Use the training set to optimize the MAM_ResNet50 described in step 2.
1. The specific steps are as follows: Step 2.2.1: Use image normalization to uniformly adjust the pixels of all pig face images in the training set to I×I; Step 2.2.2, randomly select J pig face images from the training set, and record the sequence number of any pig face image as x, x = 1, 2, ..., J, and record the true one-hot encoding sequence of the x-th pig face image as H x , Among them, q is the real one-hot encoding sequence H x The serial number of each true one-hot encoded value in , q = 1, 2, ..., M, Indicates that the pig face image with serial number x is in the real one-hot encoding sequence H x The qth true one-hot encoding value in ; The true one-hot encoding sequence H x The value of is defined as: And at this time All other true one-hot encoding values in are equal to 0; Step 2.2.3: Input the J pig face images obtained in step 2.2.2 into MAM_ResNet50 and output the predicted one-hot encoding sequence X for each pig face image. x , Where p is the predicted one-hot encoding sequence X x The number of each predicted one-hot encoding value in, p = 1, 2, ..., M, Indicates that the pig face image with serial number x is predicting the one-hot encoding sequence X x The p-th predicted one-hot encoding value in ; Step 2.2.4, according to the real one-hot encoding sequence H x and predict one-hot encoding sequence X x , through the back-propagation operation, the network weights of MAM_ResNet50 are updated and optimized as follows: Calculate the true one-hot encoding sequence H x and predict one-hot encoding sequence X x The loss function between Among them, ||·||2 represents the Euclidean norm; The loss function is minimized through gradient descent, and the weights of the connections between neural network elements are updated through backpropagation to complete the training of MAM_ResNet50. Step 2.2.5: Repeat steps 2.2.2 to 2.2.4 until all pig face images in the training set have been selected. If the number of pig face images remaining in the training set is less than J in the last round of selection, then randomly select pig face images from the already selected pig face images to supplement the training set. The MAM_ResNet50 trained through steps 2.2.2 to 2.2.5 is called the MAM_ResNet50 of the Lth round and is recorded as network T L , where L is the round number, L = i, 2, ... G, G is the number of repetitions; Step 2.3, calculate the network T through the test set L The accuracy of the test set classification P L , For network T L The correct value for the classification of pig face images in the test set, Step 2.4, repeat steps 2.2 to 2.3 G times to obtain a network set T and an accuracy set P, T = {T1, T2, ..., T L ,...,T G }, P = {P1, P2, ..., P L ,...,P G }; Step 2.5: Take the maximum accuracy value in the accuracy set P and record it as the maximum accuracy P max , take the maximum accuracy P in the network set T mzx The corresponding MAM_ResNet50 network is taken as the MAM_ResNet50 network with the best recognition performance and is recorded as the pig face recognition model; Step 3: Implement pig face recognition through the pig face recognition model. The specific steps are as follows: Step 3.1: Input the pig face image to be identified into the pig face recognition model. The pig face recognition model extracts the features of the pig face image to be identified and generates a feature sequence K of the pig face image to be identified, where K = (k1, k2, ..., k w ,...,k Z ), w represents the dimension of the feature sequence of the pig face image to be identified, k w represents the value of the w-th dimension of the pig face image feature sequence to be identified, w = 1, 2, ..., Z; Step 3.2, take k τ =max{k1, k2, ..., k w ,...,k Z }, the dimension of the pig face image feature sequence K corresponding to τ is the number of categories of pig faces to be identified, k τ The value of is the confidence level of the category number.
2. A pig face recognition method based on median attention residual network according to claim 1, characterized in that, The B image preprocessing methods described in step 1 are 9 methods, that is, B=9, specifically including: contrast enhancement, noise perturbation, mirroring, histogram equalization, image sharpening, image rotation, illumination reduction, illumination enhancement, and random color.
3. A pig face recognition method based on median attention residual network according to claim 1, characterized in that, The steps for implementing the attention mechanism based on median compression in step 2.1 and assigning different channel weights to correct channel values are as follows: According to step 2.1, the input of the attention mechanism based on median compression is the two-dimensional channel feature vector of δ channels. For each two-dimensional channel feature vector, the end and end are connected and flattened to obtain (2λ+1) one-dimensional channel feature vectors, where γ represents the sequence number of the input channel, γ = 1, 2, ..., δ; Any two-dimensional channel feature vector in the δ channels is recorded as the two-dimensional channel feature vector η γ , and its corresponding flattened vector is S γ , i represents the one-dimensional channel feature vector value in the flattened vector S γ The serial number in Represents the flattened vector S γ The i-th one-dimensional channel feature vector value of the γ-th channel in is recorded as the one-dimensional channel feature vector value Flatten the vector S γ The one-dimensional channel feature vector value in Sort in ascending order to get the ordered flattened vector U γ , j represents the one-dimensional channel feature vector value in the ordered flattened vector U γ The serial number in represents the ordered flattened vector U γ The j-th one-dimensional channel feature vector value of the γ-th channel in , j = 1, 2, ..., λ+1, ..., 2λ+1; For the ordered flattened vector U γ , take its median value As the channel descriptor θ of the γth channel γ , the channel descriptor θ γ Input the fully connected V-shaped module to obtain the median compression-based attention mechanism ρ of the γth channel γ ; The attention mechanism ρ based on median compression of the γth channel γ and the two-dimensional channel feature vector η of the γth channel γ Performing point multiplication achieves the purpose of assigning different channel weights to correct the channel values; The specific implementation of the fully connected V-shaped module is as follows: For channel descriptor θ γ , first passes through a fully connected layer with δ nodes, then passes through a fully connected layer with δ / β nodes, then passes through the ReLU function, then passes through a fully connected layer with δ nodes, and finally passes through the Sigmoid function, where β is an integer greater than 0; The expression of the ReLU function is as follows: in, Represents the input of the ReLU function, Represents the output of the ReLU function; The expression of the Sigmoid function is as follows: Where e represents the exponential function, ω represents the input of the Sigmoid function, and y(ω) represents the output of the Sigmoid function.