A YOLOv5s lightweight sheep breed identification method and system based on knowledge distillation
By employing a lightweight sheep breed identification method based on knowledge distillation in YOLOv5s, which utilizes a teacher network to transfer knowledge to a student network, the problems of large background interference and large model parameters in sheep breed identification are solved, achieving high-precision and low-cost sheep breed identification.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- INNER MONGOLIA UNIV OF SCI & TECH
- Filing Date
- 2023-01-04
- Publication Date
- 2026-04-24
AI Technical Summary
Existing sheep breed identification methods suffer from problems such as large non-contact background interference, large number of model parameters, and low model generalization ability, resulting in high identification difficulty and high cost.
A lightweight sheep breed identification method based on knowledge distillation in YOLOv5s is adopted. Knowledge is transferred from the teacher network YOLOv5x to the student network YOLOv5s to construct a lightweight sheep breed identification neural network. Knowledge distillation technology is used to transfer the knowledge learned by the teacher network with a large number of parameters and high recognition accuracy to the lightweight student network. Combined with the target detection algorithm, sheep facial image features are extracted.
It achieves improved recognition accuracy without increasing the number of model parameters, effectively removes background interference, reduces recognition costs, and improves the accuracy of sheep breed identification and the model's generalization ability.
Smart Images

Figure CN116052211B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of modern intelligent animal husbandry technology, specifically to a lightweight sheep breed identification method and system based on knowledge distillation using YOLOv5s. Background Technology
[0002] The sheep industry is a crucial component of modern animal husbandry and holds a vital position in the national economy. Developing optimal grazing times, supplementary feed amounts, and disease prevention medications for different breeds can effectively save costs, improve farm economic efficiency, and enhance flock productivity through the breeding of superior breeds. Therefore, identifying sheep breeds is a prerequisite and foundation, holding immense significance. Currently, sheep breed identification in actual production primarily relies on manual identification, anthropometric methods, and DNA testing. With the promotion of crossbreeding technology, the phenotypes of individuals after several generations of crossbreeding are highly similar to their parents, making manual identification difficult and prone to errors. Anthropometric methods are not only time-consuming and labor-intensive but also prone to errors. While DNA testing can achieve accurate identification, it is costly and time-consuming, and blood draws can cause stress in sheep, negatively impacting their health. In recent years, as animal husbandry has shifted from traditional models to intelligent, precise, and large-scale operations, non-contact breed identification methods based on computer vision have gained increasing attention, offering advantages such as cost savings and improved identification accuracy. Yan Rui'an et al. published a patent entitled "A Method, Device, and Computer-Readable Storage Medium for Pig Breed Identification," which provides a method for identifying pig breeds. However, the constructed two-step identification network model is complex, resulting in long network inference time, which is not conducive to practical deployment. Yu Wenbo et al. published a patent entitled "A Method and System for Individual Sheep Identification in a Sheepfold Based on YOLOv4," which provides a method for identifying individual sheep in a sheepfold. However, the constructed network has low identification accuracy. Therefore, how to quickly and accurately identify sheep breeds in a non-contact manner has become an urgent problem to be solved.
[0003] In view of the above-mentioned defects, the inventors of this invention have finally obtained this invention after a long period of research and practice. Summary of the Invention
[0004] The purpose of this invention is to solve the problems of large background interference, large number of model parameters, and low model generalization ability in non-contact technology, and thus proposes a lightweight sheep breed identification method and system based on knowledge distillation in YOLOv5s.
[0005] A lightweight sheep breed identification method based on knowledge distillation in YOLOv5s includes the following steps:
[0006] S1: Collect videos of multiple breeds of sheep inside the farm;
[0007] S2: Extract image frames from the video as raw images, perform data preprocessing on the raw images, and divide the processed images into training set, validation set and test set;
[0008] S3: The sheep breed identification neural network includes a teacher network YOLOv5x and a student network YOLOv5s. Knowledge distillation is used to transfer the knowledge learned by the teacher network YOLOv5x, which has a large number of parameters and high recognition accuracy, to the lightweight student network YOLOv5s, thus constructing a lightweight sheep breed identification neural network model based on knowledge distillation YOLOv5s.
[0009] S4: Train the sheep breed identification neural network model using training and validation set data to obtain the optimal weights;
[0010] S5: Input the test set into the trained sheep breed recognition network model, input sheep images to predict breeds, and evaluate the performance of the model based on the test results.
[0011] Furthermore, step S1 includes the following:
[0012] Different breeds of sheep were numbered, and cameras were placed at the exit of the sheepfold passage to film the sheep passing through the passage at different times and under different weather conditions. The video files were then transmitted to the server using WIFI technology.
[0013] Furthermore, step S2 includes the following steps:
[0014] S21: Extract video frames from the saved video file as the original image, set the frame capture frequency to a preset value, and remove duplicates from the original image;
[0015] S22: Label the sheep's face region and breed category on the deduplicated image. During the labeling process, add label boxes and generate a label file. The label file contains the location information of the labeled face region and the corresponding breed category.
[0016] S23: Divide the labeled images into training set, validation set and test set according to a preset ratio;
[0017] S24: Perform data augmentation on the images in the training and validation sets. Data augmentation includes brightness enhancement, brightness reduction, horizontal mirroring, vertical mirroring, multi-angle rotation, and noise superposition operations.
[0018] Furthermore, step S3 includes the following steps:
[0019] S31: Input the augmented training set into the teacher network YOLOv5x and the student network YOLOv5s respectively, and obtain high-dimensional features B by extracting features from the training set through the backbone convolutional neural network. 1t and B 2s ;
[0020] The teacher network YOLOv5x and the student network YOLOv5s respectively input feature vectors into their respective PANet networks. They use bottom-up and top-down methods to superimpose features at different scales, complete multi-scale feature fusion, and obtain multiple feature vectors S and P.
[0021] Perform global max pooling and channel concatenation on multiple feature vectors S and P respectively to obtain a one-dimensional vector T. i F i One-dimensional vector T i F i Feature vectors are obtained by feature fusion between channels. and
[0022] eigenvectors The soft labels of the teacher network are obtained by inputting into a multi-class softmax function at temperature t. Knowledge distillation guides the student network to learn about variety characteristics and the relationships between different varieties through these soft labels.
[0023] S32: Construct a loss function for a YOLOv5s sheep breed identification neural network based on knowledge distillation. The loss function includes target detection loss and relative entropy loss. The overall loss function is obtained by adding the target detection loss and the relative entropy loss.
[0024] Furthermore, the high-dimensional feature B in step S31 1t and B 2s The formula for expressing this is as follows:
[0025]
[0026] Where f t f represents the backbone convolutional neural network in the teacher network. s The backbone convolutional neural network representing the student network, where H, W, and C represent the length, width, and number of channels of the sheep images in the input training set, respectively, and m (i,j,k) This represents the pixel values of the input sheep image.
[0027] Furthermore, in step S31, multiple feature vectors S i and P i Perform global max pooling and channel concatenation separately to obtain a one-dimensional vector T. i Fi One-dimensional vector T i F i The formula for expressing this is as follows:
[0028]
[0029] F i =[Max(P i H,W,1 ),Max(P i H,W,2 ),……,Max(P i H,W,j )]
[0030] Among them, T i and F i Feature fusion between channels is performed using 1×1 convolution to obtain feature vectors. and and The vector can be decomposed into N+M, where N represents the number of sheep breeds and M represents the index and coordinates of the specific anchor frame.
[0031] Furthermore, step S31 will convert the feature vector The input is fed into a softmax multi-class classification function at temperature t to obtain the soft labels of the teacher network. The definition of the soft label is as follows:
[0032]
[0033] Z i The final output of the trained teacher network is a feature vector for each variety, including the anchor box index, coordinates, and variety category.
[0034] Furthermore, step S32 includes the following specific steps:
[0035] S321: Construction of object detection loss:
[0036]
[0037] Where L box The bounding box regression loss is calculated for each target; L obj The loss for the target object is calculated for each grid cell; L cls For the classification loss, it is also calculated for each target; λ1, λ2, and λ3 are the weights of the three types of loss, respectively;
[0038] S322: Calculate the relative entropy loss based on the soft labels obtained from the teacher network and the prediction vectors obtained from the student network in step S31. The relative entropy loss is calculated using the following formula:
[0039]
[0040] in, q(t) represents the soft label of the teacher's network output, q(t) represents the prediction result of the student's network output, and N represents the number of varieties;
[0041] S323: During the training of the knowledge distillation network model, the object detection loss and the relative entropy loss are weighted and added together to obtain the overall knowledge distillation loss.
[0042]
[0043] Where μ is the hyperparameter of the sum of the two loss functions, t 2 Used to adjust the weight of the two loss functions.
[0044] Furthermore, step S4 specifically includes the following steps:
[0045] S41: Pre-train the teacher network YOLOv5x using the training set to obtain soft labels for each image data;
[0046] S42: The training process of the student network YOLOv5s is supervised using soft labels. During the training process, the parameters of the student network YOLOv5s are continuously updated. When the loss value reaches a stable state or oscillates continuously within a preset range, the student network YOLOv5s completes the parameter update and obtains a trained neural network model for sheep breed recognition.
[0047] A lightweight sheep breed identification system based on knowledge distillation in YOLOv5s, which applies the aforementioned sheep breed identification method, includes the following modules:
[0048] The dataset acquisition module is used to collect facial information of sheep passing through the sheepfold passage in the farm, preprocess the facial images, and label the sheep faces with annotation boxes to obtain a sheep breed image dataset, which is divided into training set, validation set and test set.
[0049] The model building module constructs a lightweight sheep breed recognition network model based on knowledge distillation using YOLOv5s. The network model includes a teacher network YOLOv5x and a student network YOLOv5s. Knowledge distillation is used to transfer the knowledge learned by the teacher network YOLOv5x, which has a large number of parameters and high recognition accuracy, to the lightweight student network YOLOv5s, thus constructing a lightweight sheep breed recognition neural network model based on knowledge distillation using YOLOv5s.
[0050] The model training module is used to construct a loss function, perform pre-training using a training set, use the model parameters obtained after pre-training as the initial parameters of the sheep breed recognition neural network model, adjust the parameters using the validation set, and obtain a trained sheep breed recognition neural network model; finally, the test set is input into the trained neural network model, and its performance is evaluated.
[0051] Compared with existing technologies, the beneficial effects of this invention are as follows: This invention discloses a lightweight sheep breed identification method based on knowledge distillation using YOLOv5s. It eliminates the need for manual feature extraction, instead repeatedly extracting and stacking features through a convolutional neural network-based target detection method, thereby obtaining richer essential features related to the breed. Compared to existing deep learning-based breed identification methods, the method provided by this invention directly extracts sheep facial images through a target detection algorithm, effectively removing background interference. Simultaneously, it utilizes knowledge distillation to transfer effective features learned by the teacher network (which has a large number of parameters and high recognition accuracy) to the student network, thus improving the recognition accuracy of the breed identification network while achieving the goal of small and lightweight network model parameters. Attached Figure Description
[0052] Figure 1 This is a flowchart of the steps of a lightweight sheep breed identification method based on knowledge distillation in YOLOv5s according to the present invention.
[0053] Figure 2 This is a flowchart illustrating a lightweight sheep breed identification method based on knowledge distillation in YOLOv5s according to the present invention.
[0054] Figure 3 This is a flowchart of the perceptual hash algorithm of the present invention;
[0055] Figure 4 These are the original image and the image-enhanced sample of this invention;
[0056] Figure 5 This is a schematic diagram of the structure of a lightweight sheep breed identification neural network model based on knowledge distillation in YOLOv5s according to the present invention.
[0057] Figure 6 This is the teacher network training verification loss curve for this invention;
[0058] Figure 7 This invention introduces a YOLOv5s network training and verification loss curve based on knowledge distillation.
[0059] Figure 8 The mAP index is the test result of this invention;
[0060] Figure 9This is a structural block diagram of a lightweight sheep breed identification system based on knowledge distillation, YOLOv5s, according to the present invention. Detailed Implementation
[0061] This invention provides a lightweight sheep breed identification method based on knowledge distillation using YOLOv5s, which solves the problems of large background interference, large number of model parameters, and low model generalization ability in existing non-contact technologies.
[0062] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below through specific implementation and in conjunction with the accompanying drawings.
[0063] Example 1
[0064] like Figure 1 , Figure 2 As shown in the embodiment of the present invention, the lightweight sheep breed identification method based on knowledge distillation in YOLOv5s includes the following steps:
[0065] S1: Collect videos of multiple breeds of sheep inside the farm;
[0066] S2: Extract image frames from the video as raw images, perform data preprocessing on the raw images, and divide the processed images into training set, validation set and test set;
[0067] S3: The sheep breed identification neural network includes a teacher network YOLOv5x and a student network YOLOv5s. Knowledge distillation is used to transfer the knowledge learned by the teacher network YOLOv5x, which has a large number of parameters and high recognition accuracy, to the lightweight student network YOLOv5s, thus constructing a lightweight sheep breed identification neural network model based on knowledge distillation YOLOv5s.
[0068] S4: Train the sheep breed identification neural network model using the training set to obtain the optimal weights;
[0069] S5: Input the test set into the trained sheep breed recognition network model, input sheep images to predict breeds, and evaluate the performance of the model based on the test results.
[0070] In one embodiment, step S1 specifically includes: numbering sheep of different breeds so that the sheep image dataset can be classified by breed later; placing a camera at the exit of the sheepfold passage, 5-10cm above the height of the sheep, setting the camera to 24 frames per second, and taking videos of the sheep passing through the passage at different times and under different weather conditions, and transmitting the video files to the server using WIFI technology.
[0071] In one embodiment, in step S2 above, the video information is cropped according to a preset frame-cutting frequency, and the obtained sheep image is used as the input raw image. The raw image is then preprocessed, specifically including the following steps:
[0072] S21: Using the OpenCV open-source library in Python, extract video frames from the saved video, setting the frame rate to 24, and save as JPG format. The acquired sheep images are first cleaned to remove images that do not contain sheep faces or have large areas of face occlusion. Then, the cleaned images are filtered for similarity using a perceptual hash algorithm. After filtering, 400 images are retained for each breed. The perceptual hash algorithm process is as follows: Figure 3 As shown;
[0073] S22: First, based on the sheep breed labels, save all images of the same breed of sheep into the same folder, with the folder name matching the sheep label name. Then, label the facial regions and breed categories of all sheep breeds. During the labeling process, use labelimg to add label boxes and generate corresponding label files for the sheep facial regions in the images. The files mainly contain the coordinates of the four vertices of the labeled sheep face rectangle and the corresponding sheep breed category within the region;
[0074] S23: Divide the labeled images into training, validation, and test sets according to a 7:2:1 ratio;
[0075] S24: Perform data augmentation on the images in the training and validation sets to improve the generalization ability of the sheep breed recognition model. Data augmentation includes operations such as brightness enhancement and reduction, mirroring, multi-angle rotation, and noise superposition. After augmentation, the number of images in the training and validation sets increased by 7 times. Figure 4 As shown, Figure 4 Examples include the original image and the image-enhanced sample.
[0076] In one embodiment, such as Figure 5 As shown, in step S3, knowledge distillation is used to transfer the knowledge learned by the teacher network, which has a large number of parameters and high recognition accuracy, to the lightweight student network YOLOv5s. This enhances the feature extraction capability of the PANet part in the student network, achieving the goal of lightweight network model and accurate recognition. The construction of a lightweight sheep breed recognition neural network model based on knowledge distillation in YOLOv5s includes the following steps in step S3:
[0077] S31: Construct a YOLOv5s sheep breed identification neural network model and loss function based on knowledge distillation; Step S31 specifically includes the following steps:
[0078] First, the augmented training set images with a pixel size of 640×640×3 are input into the teacher network and student network respectively. After feature extraction through the backbone convolutional neural network, high-dimensional features B are obtained respectively. 1t and B 2s .
[0079]
[0080] Where f t f represents the backbone convolutional neural network in the teacher network. s The backbone convolutional neural network representing the student network, where H, W, and C represent the length, width, and number of channels of the sheep images in the input training set, respectively, and m (i,j,k) This represents the pixel values of the input sheep image.
[0081] High-dimensional feature vector B 1t It includes three scales: (80, 80, 320), (40, 40, 640), and (20, 20, 1280); high-dimensional feature vector B 1s It includes three scales: (80, 80, 128), (40, 40, 256), and (20, 20, 512). In the feature extraction stage of the backbone convolutional neural network, the teacher network YOLOv5x inputs feature vectors of size (80, 80, 320), (40, 40, 640), and (20, 20, 1280) into the PANet network. It uses bottom-up and top-down methods to superimpose features of different scales, completes multi-scale feature fusion, and obtains feature vectors S1(80, 80, 320), S2(40, 40, 640), and S3(20, 20, 1280). Similarly, the student network YOLOv5s inputs feature vectors of size (80, 80, 128), (40, 40, 256), and (20, 20, 512) into the PANet network, and obtains feature vectors P1(80, 80, 128), P2(40, 40, 256), and P3(20, 20, 512).
[0082] Then, global max pooling and channel concatenation are performed on the feature vectors S1, S2, S3 and P1, P2, P3 respectively to obtain a one-dimensional vector T. i F i :
[0083]
[0084] F i =[Max(P i H,W,1 ),Max(P i H,W,2 ),……,Max(P iH,W,j )]
[0085] Among them, T i and F i Feature fusion between channels is performed using 1×1 convolution to obtain feature vectors. and and The vector can be decomposed into N+M, where N represents the number of sheep breeds and M represents the index and coordinates of the specific anchor frame.
[0086] Finally The soft labels of the teacher network are obtained by inputting them into a softmax (multi-class classification) function at temperature t. The calculation formula is as follows:
[0087]
[0088] Soft labels representing teacher networks, The output vector of the teacher network represents t, and the temperature represents Z. i Represents the predicted probability of the i-th category
[0089] Knowledge distillation guides students to learn about varietal characteristics and relationships between different varieties through soft tags on the teacher network.
[0090] The student network's prediction vector includes the index and coordinates of specific anchor boxes, obtained through feature extraction from sheep images. The relative entropy loss is calculated using Kullback-Leibler divergence (KL divergence) based on the soft labels obtained by the teacher network and the prediction vectors obtained by the student network. This relative entropy loss is added to the object detection loss to obtain the overall knowledge distillation loss. The student network is guided by the optimal weights obtained during the teacher network's pre-training process, ultimately enabling it to achieve recognition accuracy close to that of the teacher network. During backpropagation to update the weights, only the parameters of the student network model are updated; the teacher network's model parameters are not updated.
[0091] S32: Construct the loss function for a YOLOv5s sheep breed identification neural network based on knowledge distillation. The overall distillation loss includes two parts: target detection loss and relative entropy loss, specifically including the following:
[0092] Object detection loss function:
[0093] Where L box The bounding box regression loss is calculated for each target; L obj The loss for the target object is calculated for each grid cell; L clsFor the classification loss, it is also calculated for each target; λ1, λ2, and λ3 are the weights of the three types of loss, respectively.
[0094] Relative entropy loss function:
[0095] in, q(t) represents the soft label of the teacher's network output, q(t) represents the prediction result of the student's network output, and N represents the number of varieties.
[0096] During the training of the knowledge distillation network model, the overall knowledge distillation loss is obtained by weighted summing of the object detection loss function and the relative entropy loss function:
[0097] Where μ is the hyperparameter of the sum of the two loss functions, t 2 This is used to adjust the weighting of the two loss functions. Distillation loss is used as a feature that supervises student network learning and teacher network learning.
[0098] In one embodiment, step S4 involves training a lightweight sheep breed identification network based on knowledge distillation YOLOv5s to obtain optimal weights. These optimal weights are obtained by comparing loss values, which can achieve the best identification results. The specific steps include:
[0099] S41: First, the teacher network YOLOv5x in the sheep breed identification neural network model is pre-trained using the training set to obtain soft labels for each data point; then, the soft labels are used to supervise the training process of the output student network YOLOv5s.
[0100] Specifically, this invention collected facial images of 823 sheep from 10 breeds, totaling 25,200 images after data augmentation, and conducted 500 rounds of training. After pre-training with a large number of sheep facial images, the teacher network can effectively extract essential features of sheep faces that are closely related to breed.
[0101] Teacher network training validation loss validation curve as shown Figure 6 As shown, the horizontal axis Epoch represents the number of training rounds, and the vertical axis Loss represents the loss value. The train loss curve represents the loss value of the teacher network on the training set, and the val loss curve represents the loss value on the validation set. The training and validation loss curves of the teacher network indicate that the network model training has reached a stable state, and further training will not achieve better results. After the teacher network training is completed, the optimal weights are obtained by comparing the loss values, and the optimal weights obtained by the network model during the training process are saved.
[0102] S42: Then, fix the optimal weights of the obtained teacher network YOLOv5x, and use the training set to pre-train the knowledge distillation-based YOLOv5s sheep breed recognition neural network model. In the knowledge distillation network model, the teacher network YOLOv5x does not participate in the weight update, while the parameters of the student network YOLOv5s are continuously updated. When the loss value reaches a stable state or oscillates continuously within a preset range, the student network YOLOv5s completes the parameter update, and the trained sheep breed recognition neural network model is obtained.
[0103] The parameters selected for the knowledge distillation network model are as follows: distillation temperature t = 4, and loss function balance coefficient μ = 0.2. The entire network model was trained for a total of 500 rounds, and the training and validation loss curves are shown below. Figure 7 As shown in the figure, the loss function curve indicates that the network model has not overfitted and is convergent.
[0104] In this embodiment of the invention, 400 photos from the test set are input into a trained neural network model for sheep breed recognition, and the results are as follows. Figure 8 As shown, the vertical axis represents different sheep breeds, and the horizontal axis represents the mAP index. The final average mAP value is 94.7%, which indicates that the model achieved a high detection accuracy without increasing the number of network model parameters.
[0105] Example 2
[0106] like Figure 9 As shown, this embodiment of the invention provides a lightweight sheep breed identification system based on knowledge distillation, using YOLOv5s, comprising the following modules:
[0107] The dataset acquisition module is used to collect facial information of sheep passing through the sheepfold passage, preprocess the facial images, and label the sheep faces with bounding boxes to obtain a sheep breed image dataset, which is then divided into training set, validation set and test set.
[0108] The model building module constructs a lightweight sheep breed recognition network model based on knowledge distillation using YOLOv5s. This network model includes a teacher network (YOLOv5x) and a student network (YOLOv5s). The test set is input into the teacher network and the student network respectively, and high-dimensional features B are obtained through feature extraction via a backbone convolutional neural network. 1t and B 2s The teacher and student networks respectively input feature vectors into their respective PANet networks. Using bottom-up and top-down methods, features at different scales are superimposed to achieve multi-scale feature fusion and obtain feature vectors. Global max pooling and channel concatenation are then performed on the feature vectors to obtain one-dimensional vectors. and Feature vectors of teacher networks The soft label is obtained by inputting into the softmax function at temperature t. The soft label guides students to learn about the characteristics of varieties and the relationships between different varieties online.
[0109] The model training module is used to construct a loss function, pre-train the model using a sheep face data training set, use the pre-trained model parameters as the initial parameters of the sheep breed recognition neural network model, adjust the parameters using the validation set, and obtain a trained sheep breed recognition neural network model. Finally, the test set is input into the trained neural network model to evaluate its performance.
[0110] This invention discloses a lightweight sheep breed identification method based on knowledge distillation using YOLOv5s. This method is a non-contact identification method that does not cause stress to the sheep. Unlike traditional machine learning methods that extract feature points for classification, the method provided by this invention does not require manual feature extraction. Instead, it repeatedly extracts and stacks features through a target detection method based on convolutional neural networks, thereby obtaining richer essential features related to the breed. Compared with existing deep learning-based methods for breed identification, the method provided by this invention directly extracts sheep facial images through a target detection algorithm, which can effectively remove background interference. At the same time, it uses knowledge distillation to transfer effective features learned by the teacher network with large parameters and high recognition accuracy to the student network. This not only improves the recognition accuracy of the breed identification network, but also achieves the goal of small and lightweight network model parameters.
[0111] The above description is merely a preferred embodiment of the present invention and is illustrative rather than restrictive. Those skilled in the art will understand that many changes, modifications, and even equivalents can be made within the spirit and scope defined by the claims of the present invention, all of which will fall within the protection scope of the present invention.
Claims
1. A lightweight sheep breed identification method based on knowledge distillation in YOLOv5s, characterized in that, It includes the following steps: S1: Collect videos of multiple breeds of sheep inside the farm; S2: Extract image frames from the video as raw images, perform data preprocessing on the raw images, and divide the processed images into training set, validation set and test set; S3: The sheep breed identification neural network includes a teacher network YOLOv5x and a student network YOLOv5s. Knowledge distillation is used to transfer the knowledge learned by the teacher network YOLOv5x, which has a large number of parameters and high recognition accuracy, to the lightweight student network YOLOv5s. A lightweight sheep breed identification neural network model and loss function based on knowledge distillation YOLOv5s are constructed. S4: Train the sheep breed identification neural network model using training and validation set data to obtain the optimal weights; S5: Input the test set into the trained sheep breed recognition network model, input sheep images to predict breeds, and evaluate the performance of the model based on the test results. Step S2 includes the following steps: S21: Extract video frames from the saved video file as the original image, set the frame capture frequency to a preset value, and remove duplicates from the original image; S22: Label the sheep's face region and breed category on the deduplicated image. During the labeling process, add label boxes and generate a label file. The label file contains the location information of the labeled face region and the corresponding breed category. S23: Divide the labeled images into training set, validation set and test set according to a preset ratio; S24: Perform data augmentation on the images in the training and validation sets. Data augmentation includes brightness enhancement, brightness reduction, horizontal mirroring, vertical mirroring, multi-angle rotation, and noise superposition operations. Step S3 includes the following steps: S31: Input the augmented training set into the teacher network YOLOv5x and the student network YOLOv5s respectively, and obtain high-dimensional features by extracting features from the training set through the backbone convolutional neural network. and ; The teacher network YOLOv5x and the student network YOLOv5s respectively incorporate high-dimensional features and The features are fed into their respective PANet networks, and the features at different scales are superimposed using bottom-up and top-down methods to complete the multi-scale fusion of features and obtain multiple feature vectors S and feature vector P. Perform global max pooling and channel concatenation on multiple feature vectors S and P respectively to obtain a one-dimensional vector. , One-dimensional vector , Feature vectors are obtained by feature fusion between channels. and ; eigenvectors Input temperature is The soft labels of the teacher network are obtained through the multi-class softmax function, and knowledge distillation guides the student network to learn variety characteristic information and the correlation information between different varieties through the soft labels; S32: Construct a loss function for a YOLOv5s sheep breed identification neural network based on knowledge distillation. The loss function includes target detection loss and relative entropy loss. The overall loss function is obtained by adding the target detection loss and the relative entropy loss.
2. The lightweight sheep breed identification method based on knowledge distillation in YOLOv5s as described in claim 1, step S1 includes the following: Different breeds of sheep were numbered, and cameras were placed at the exit of the sheepfold passage to film the sheep passing through the passage at different times and under different weather conditions. The video files were then transmitted to the server using WIFI technology.
3. The lightweight sheep breed identification method based on knowledge distillation in YOLOv5s as described in claim 1, characterized in that, High-dimensional features in step S31 and The formula for expressing this is as follows: ; in The backbone convolutional neural network in the teacher network, The backbone convolutional neural network representing the student network, , , These represent the length, width, and number of channels of the sheep images in the input training set, respectively. This represents the pixel values of the input sheep image.
4. The lightweight sheep breed identification method based on knowledge distillation in YOLOv5s as described in claim 1, characterized in that, In step S31, multiple feature vectors are processed. and Perform global max pooling and channel concatenation separately to obtain a one-dimensional vector. , One-dimensional vector , The formula for expressing this is as follows: ; ; in, and Feature fusion between channels is performed using 1×1 convolution to obtain feature vectors. and , and The vector can be decomposed into N+M, where N represents the number of sheep breeds and M represents the index and coordinates of the specific anchor frame.
5. The lightweight sheep breed identification method based on knowledge distillation in YOLOv5s as described in claim 1, characterized in that, Step S31 converts the feature vector Input temperature is In the softmax multi-class classification function, the soft labels of the teacher network are obtained. The definition of the soft label is as follows: ; Z i The final output of the trained teacher network is a feature vector for each variety, including the anchor box index, coordinates, and variety category.
6. The lightweight sheep breed identification method based on knowledge distillation in YOLOv5s as described in claim 1, characterized in that, Step S32 includes the following specific steps: S321: Construction of object detection loss: ; in The bounding box regression loss is calculated for each target. The loss for the target object is calculated for each grid cell. For classification loss, the same calculation is performed for each target; , , These are the weights for the three types of losses; S322: Calculate the relative entropy loss based on the soft labels obtained from the teacher network and the prediction vectors obtained from the student network in step S31. The relative entropy loss is calculated using the following formula: ; in, Soft tags representing teachers' online output; The predicted results represent the output of the student network, where N represents the number of varieties; S323: During the training of the knowledge distillation network model, the object detection loss and the relative entropy loss are weighted and added together to obtain the overall knowledge distillation loss. ; in It is a hyperparameter of the sum of two loss functions. Used to adjust the weight of the two loss functions.
7. The lightweight sheep breed identification method based on knowledge distillation in YOLOv5s as described in claim 1, characterized in that, Step S4 specifically includes the following steps: S41: Pre-train the teacher network YOLOv5x using the training set to obtain soft labels for each image data; S42: The training process of the student network YOLOv5s is supervised using soft labels. During the training process, the parameters of the student network YOLOv5s are continuously updated. When the loss value reaches a stable state or oscillates continuously within a preset range, the student network YOLOv5s completes the parameter update and obtains a trained neural network model for sheep breed recognition.
8. A lightweight sheep breed identification system based on knowledge distillation using YOLOv5s, which applies the sheep breed identification method as described in any one of claims 1-7, characterized in that... Includes the following modules: The dataset acquisition module is used to collect facial information of sheep passing through the sheepfold passage in the farm, preprocess the facial images, and label the sheep faces with bounding boxes to obtain a sheep breed image dataset, which is divided into training set, validation set and test set. The model building module is used to build a lightweight sheep breed recognition network model based on knowledge distillation in YOLOv5s. The network model includes a teacher network YOLOv5x and a student network YOLOv5s. Knowledge distillation is used to transfer the knowledge learned by the teacher network YOLOv5x, which has a large number of parameters and high recognition accuracy, to the lightweight student network YOLOv5s, thereby building a lightweight sheep breed recognition neural network model based on knowledge distillation in YOLOv5s. The model training module is used to construct a loss function, perform pre-training using a training set, use the model parameters obtained after pre-training as the initial parameters of the sheep breed recognition neural network model, adjust the parameters using the validation set, and obtain a trained sheep breed recognition neural network model; finally, the test set is input into the trained neural network model, and its performance is evaluated.
Citation Information
Patent Citations
Lightweight cattle herd detection method and device based on DC-SMKD
CN114494923A