Bilateral identification method for imbalanced bovine body data based on MBN-Transformer model

By using the MBN-Transformer model, combined with random and balanced samplers, a dynamic fusion hybrid enhancement module and a visual Transformer were designed to solve the problem of imbalanced data in cattle body recognition, improve the recognition rate of cattle sheds and milking passages, and achieve more stable cattle body feature extraction and recognition.

CN116311357BActive Publication Date: 2026-01-30YICAI TIANCHENG (ZHENGZHOU) INFORMATION TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310117831.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-02-15
Publication Date
2026-01-30
Estimated Expiration
2043-02-15

AI Technical Summary

Technical Problem

Existing convolutional neural network-based cattle body recognition technology does not perform well under imbalanced data conditions, especially in cattle sheds and milking aisles, where data in cattle sheds is diverse while data in milking aisles is homogeneous, resulting in uneven recognition rates.

Method used

We employ the MBN-Transformer model, combining random samplers and balanced samplers, and design a dynamic fusion hybrid enhancement module and a visual Transformer. We process image data from different samplers through balanced and regular branches, and optimize the network using a dynamic balancing factor and label smoothing cross-entropy loss function to enhance global feature association and reduce overfitting.

Benefits of technology

It improves the accuracy of cow body recognition under imbalanced data conditions, especially the recognition rate in the milking passage, reduces overfitting, and enhances the model's generalization ability and recognition stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116311357B_ABST
    Figure CN116311357B_ABST
Patent Text Reader

Abstract

This invention discloses a bilateral recognition method for imbalanced cow body data based on the MBN-Transformer model. The invention increases the sampling rate of fewer cow classes by adding a balancing sampler, reduces overfitting issues caused by the balancing sampler through a dynamic fusion and hybrid enhancement module, and enhances the correlation of global information in the cow body images by selecting the Vision Transformer as the shared backbone network structure. A Transformer encoder is used to design balancing and regular branch modules to process the mixed enhanced image data from the random sampler in the cowshed and the balanced sampler in the milking aisle, respectively. The output features of the two branches are fused using a dynamic feature balancing factor. The multi-head self-attention mechanism of the Transformer encoder is used to mine the correlation and global information of the cow body data from the dynamic fusion and hybrid enhancement module. This invention solves the problem of poor recognition performance of single-pose images of cows in the milking aisle, while also having better focusing ability on key features of the cows.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of biometric identification, and provides a two-sided identification method for unbalanced cattle body data based on an MBN-Transformer model. Specifically, it is a biometric identification technology based on deep learning in the application scenario of unbalanced cattle body data. BACKGROUND

[0002] With the improvement of the production level of our country, the individual workshop breeding mode gradually disappears, and is replaced by the large-scale and fine breeding mode. A fast and efficient cattle individual identification technology becomes a key factor for improving the breeding scale. The traditional management mode adopts ear tags, labels, radio frequency detection technology (RFID), etc. However, the ear tags and labels are easy to be counterfeited and may cause harm to the cattle. With the application and development of deep learning in biometric identification technology, this non-contact identification method is not easy to cause physical harm to the cattle and can be quickly used in actual scenes. The deep learning technology does not need any artificially made features and can directly learn feature information from data, extracting more robust features for posture or background changes and illumination changes. Most methods are based on convolutional neural networks for cattle body identification. The convolutional neural network pays more attention to local features and does not plan the global features, resulting in poor performance of cattle body identification through the convolutional neural network. Moreover, the training and identification are based on balanced cattle data, and the effect is not ideal in the unbalanced sample situation. However, in the actual scene, the collected cattle data is usually unbalanced. The data set in this paper is collected in the actual scene, mainly through the cameras in the cowshed and the milking channel. The cattle in the cowshed can move freely, while the cattle in the channel are limited in movement. Therefore, the collected cattle images in the cowshed are diversified in posture and large in quantity, while the cattle in the milking channel are single in posture and small in quantity, resulting in that the training process is often biased towards the cattle in the cowshed, and the accuracy of the cattle in the milking channel is poor. SUMMARY

[0003] For the training recognition effect caused by the unbalanced data of cattle collected in the actual application scene is not ideal, the application provides a kind of unbalanced cattle body data bilateral recognition algorithm based on MBN-Transformer.The algorithm increases the balanced sampler to increase the sampling rate of the few categories of cattle, and reduces the overfitting problem caused by the balanced sampler by dynamically fusing the mixed enhancement module, simultaneously adopts Vision Transformer (ViT) as the backbone network, enhances the association of global information of cattle image, adopts the Transformer encoder to design the balance branch and the conventional branch module, respectively processes the image mixed enhancement data of the random sampler of the cowshed and the image mixed enhancement data of the balanced sampler of the milking channel, and fuses the output features of the two branches through the feature dynamic balance factor, the fusion feature changes with the training number, solves the problem of poor image recognition rate performance of single posture of cattle in the milking channel.

[0004] The technical scheme adopted by the application is:

[0005] S1, construct a data set, and divide the data set into a training set and a test set.

[0006] The data set adopted by the application is the cattle body image obtained by the cattle herd target detection segmentation network. The training set after division is expanded by translation, rotation and scaling operation to meet the different postures of cattle body, so as to obtain a more general model.

[0007] S1-1: collect image data of several types of cattle through the cattle herd target detection segmentation network.

[0008] S1-2: expand the training set by using translation, rotation and scaling operation. And normalize the image data of the expanded training set to the same resolution size.

[0009] S2, design the MBN-Transformer model.

[0010] S2-1: Select the Vision Transformer as the shared backbone network structure to increase the extraction of global information of the cow body, and use the Transformer encoder to design the regular branch and the balance branch to process the image mixed augmented data of the cowshed random sampler and the image mixed augmented data of the milking channel balance sampler respectively, and add the input features of the two branches through the dynamic balance factor, so that the final fusion features tend to the cow body data in the milking channel in the training process, solving the problem of poor image recognition performance of single posture of the cow in the milking channel. The random sampler and the balance sampler are designed to control the input images of different categories, and the dynamic fusion mixed augmented module is used to slow down the overfitting phenomenon caused by over-sampling the single posture data in the milking channel. In addition, the multi-head self-attention mechanism of the Transformer encoder is used to mine the correlation and global information of the cow body data of the dynamic fusion mixed augmented module.

[0011] The MBN-Transformer model includes a sampler module, a dynamic fusion image mixed augmented module, a shared backbone network, and two branch subnets. The sampler module includes a random sampler and a balance sampler. The random sampler is randomly sampled from all samples in the training set. However, there are more cow body images in the cowshed in the training set, so it tends to cow body images in the cowshed. The output image of the random sampler is X r . The balance sampler is over-sampled from the cow body data in the milking channel. The balance sampler tends to cow body images in the milking channel. The output image of the balance sampler is X p . The two branch subnets include a regular branch and a balance branch. The dynamic fusion mixed image augmented module mainly prevents over-sampling of single posture cow body images in the milking channel by dynamically fusing the output images of the random sampler and the balance sampler during the training process. The output of the dynamic fusion mixed image augmented module is X r ' and X p '. The shared backbone network uses the Vision Transformer, i.e., the ViT model. The ViT model includes image blocking and several layers of Transformer encoders. Image blocking divides the image into several identical blocks. After image blocking, position encoding representing cow body position information and global features learning cow body information are added to obtain an input sequence. The input sequence is input into the Transformer encoder to output features F r and F p . The two features F r and F p are transmitted to the regular branch and the balance branch, respectively. The regular branch and the balance branch both use the Transformer encoder to better focus on global features. The output features of the regular branch and the balance branch are F r ' and F ptwo output features F r ′ and F p ′ are fused, and the specific formula is shown in the following formula.

[0012] l = 1 - ((n-1) / T) (1)

[0013] F = lxF′ r + (1-l)xF p ′ (2)

[0014] In the formula, n is the current training round, T is the total number of training rounds, l is the fusion degree of the two output features, and F is the final output feature delivered to the classifier. By using the training round to control the fusion of the two input features, more attention is paid to the input of the training random sampler at the beginning of training, and as the training round increases, the input of the balanced sampler is focused on to increase the attention to the cows in the milking channel. And finally, the Euclidean distance between the cow features is calculated to recognize the cow.

[0015] S2-2: Designing a sampler module:

[0016] The traditional sampler uses a random sampler to sample according to the number of samples of each class, and the sampling probability calculation formula of the jth cow of the random sampler is shown in formula (3).

[0017]

[0018] In the formula, k is the total number of classes in the training set, S i is the number of cow images of the ith cow class in the training set, is the sampling probability of the jth cow class of the random sampler. As can be seen from the formula, the sampling probability of the cow image with a larger number of samples in the cowshed is larger, and the sampling probability of the cow image with a smaller number of samples in the milking channel is smaller. Therefore, in the case of sample imbalance, the cow image in the milking channel may be under-fitted.

[0019] The present application also uses a balanced sampler, and the sampling probability calculation formula of the jth cow of the balanced sampler is shown in the following formula.

[0020]

[0021]

[0022] In the formula, k is the total number of classes in the training set, N max is the largest sample number in the training set, N i is the number of cow images of the ith cow class, and W i is the sampling degree of the ith cow class. is the sampling probability of the jth cow category of the balanced sampler. As can be seen from equations (4) and (5), the cows in the milking channel with fewer samples are given more attention, while the cows in the cowshed with more samples are given less attention.

[0023] The present application simultaneously adopts a random sampler and a balanced sampler to train a conventional regular branch and a balanced branch, respectively. The random sampler collects more cow body image data in the cowshed, and the balanced sampler collects more cow body image data in the milking channel. This method can take into account the cow body image data collected in two different ways in the cow farm.

[0024] S2-3: Design a dynamic fusion mixed image enhancement module:

[0025] The cow body image data in the milking channel is less, and although the balanced sampler can increase the sampling probability of the cow body image in the milking channel, it is easy to over-sample the cow body image in the milking channel. Mixed image enhancement is to generalize the network model by synthesizing samples, but it is easy to not converge in the training process without restoring to real image data, which may bring new problems while solving the overfitting problem. Therefore, the present application proposes a dynamic fusion mixed image enhancement module, which reflects the image mixing degree by dynamically fusing parameters to reduce the overfitting of the cow body image data in the milking channel. The specific formula is as follows.

[0026] λ=1-((T+n) / 2×T) (6)

[0027] (X′ r ,Y′ r )=λ(X r ,Y r )+(1-λ)(X p ,Y p ) (7)

[0028] (X′ p ,Y′ p )=(1-λ)(X r ,Y r )+λ(X p ,Y p ) (8)

[0029] S3, design a loss function.

[0030] In network training, in order to enhance the performance of the modeling of the present application, a label smoothing cross-entropy loss is used to optimize the network, and the specific calculation formula is as follows.

[0031] L=l*L smoo (y p_true ,y pred )+(1-l)*L smoo (yr_true ,y pred (9)

[0032] L smoo (y p_true ,y pred )=∑[(1-ε)*y p_true +ε / N]ln y pred (10)

[0033] L smoo (y r_true ,y pred )=∑[(1-ε)*y r_true +ε / N]ln y pred (11)

[0034] In the formula, ε is the random noise coefficient, which is set to 0.1 in the algorithm of this chapter; l varies with the training batch, and the degree of variation adopts the method of formula (1); N is the total number of cattle categories in the cattle training set; y r_true These are the real labels obtained after dynamic fusion and image enhancement using a random sampler; y p_true It is the true label after the balanced sampler dynamically fuses and blends the image for enhancement; y pred It represents the predicted category output by the model; L represents the total loss, L smoo This is the label smoothing cross-entropy loss. As can be seen from the formula, when optimizing the network, the loss function tends to better balance the real labels after the sampler has been dynamically fused and mixed with the enhanced image, as the l parameter increases.

[0035] S4. Train the entire model, such as Figure 1 This continues until the entire training set is trained several times in succession.

[0036] S5. Input the test set images into the trained model to extract cow image features, and use cosine distance for 1:1 comparison and recognition.

[0037] The beneficial effects of this invention are as follows:

[0038] This paper proposes a bilateral recognition algorithm based on imbalanced cow body data using Transformer. It reduces overfitting caused by the balanced sampler by using a dynamic fusion and enhancement module. At the same time, it uses a Transformer encoder to design a regular branch and a balanced branch to process the image fusion and enhancement data of the random sampler in the cowshed and the image fusion and enhancement data of the balanced sampler in the milking lane, respectively. The input features of the two branches are added by a dynamic balancing factor so that the final features tend to be biased towards the cow body data in the milking lane during the training process, thereby solving the problem of imbalance in cow body images.

[0039] In the past recognition model such as LA Transformer has paid attention to some key features of the cattle, but still not all the key features of the cattle are paid attention to; BBN algorithm pays more attention to more key features than LA Transformer, but still pays attention to some noise; the algorithm (MBN-Transformer) in this paper not only reduces the attention to noise, but also pays more attention to the key features of the cattle, so the algorithm in this paper has better focusing ability for the key features of the cattle. BRIEF DESCRIPTION OF DRAWINGS

[0040] Figure 1 It is a bilateral recognition algorithm model diagram based on MBN-Transformer cattle body imbalance data.

[0041] Figure 2 It is a camera placement position actual scene diagram of the cowshed and the milking channel

[0042] Figure 3 It is different collection methods of cattle data set.

[0043] Figure 4 It is a visual display of different algorithms.

[0044] Figure 5 It is the ROC curve of different algorithms in the test set. DETAILED DESCRIPTION

[0045] The specific embodiments of the present application will be further described below in combination with the drawings.

[0046] The MBN-Transformer-based imbalance cattle body data bilateral recognition algorithm of the present embodiment includes the following steps:

[0047] S1, constructing a data set

[0048] S1-1: The data in this paper includes two collection methods, one is to collect by the camera located in the cowshed, the camera is placed on the top of the cowshed, and the camera is installed at an appropriate distance to shoot more cattle and avoid overlapping the same area; the other is to collect by the camera located in the milking channel, the camera is placed above the milking channel, and three cameras are placed at the same position to shoot the cattle from the left, right and directly above. Figure 2 The placement of the cowshed and milking camera is shown. The cattle body image shot by the camera located in the cowshed and milking channel is shown in Figure 3 .

[0049] Cows in the cowshed can move freely within the range of the cowshed, and each cow can be captured from different angles and poses. Cows in the milking channel can only move in one direction and cannot turn freely, and each cow can only be captured from a few angles and poses. In this paper, a cow body image dataset consisting of 389 cows was collected in two cowsheds. The cow body images were collected by cameras deployed in the cowshed and the milking channel. In the cowshed, there are two cases. In the first case, the cows can move freely, so the poses of the cow body images differ greatly. A total of 60 cows were collected, and the number of images for each cow was between 40 and 80. In the other case, the cows were tied to the cowshed by a rope, so the range of movement of the cows was small, and the poses of the cow body images changed little. A total of 249 cows were collected, and the number of images for each cow was between 20 and 50. In the milking channel, the cows move quickly and have a single pose. A total of 80 cows were collected, and the number of images for each cow was between 20 and 40. However, there were fewer effective images, and more images had the same pose. The number of 389 cows was approximately 17000.

[0050] The first 25 classes in the cowshed and the first 25 classes in the milking channel were combined as the test set, which had 50 classes and a total of approximately 1800 images. The remaining images were used as the training set, and the training set was expanded by image enhancement. After expansion, the number of cow body images in the cowshed was between 500 and 1000 for each class. However, because the poses of the cow body images in the milking channel were single, image enhancement was not ideal, so the number of cow body images in the milking channel was increased to 100. The final training set had 339 classes and a total of approximately 130000 images.

[0051] S2, design an unbalanced cow body data bilateral recognition algorithm based on MBN-Transformer;

[0052] S2-1: Sampler module design

[0053] The present application can take into account the cow body image data of two different collection methods in the cow farm by simultaneously using a random sampler and a balanced sampler to train a conventional network branch and a balanced network branch respectively, the random sampler collects more cow body image data in the cowshed, and the balanced sampler collects more cow body image data in the milking channel. By increasing the balanced sampler to increase the sampling rate of the few categories of cows, and by dynamically fusing the mixed enhancement module to reduce the overfitting problem that may be caused by the balanced sampler, a Vision Transformer (ViT) is used as the backbone network to enhance the correlation of global information of the cow body image, and a Transformer encoder is used to design the balanced branch and the conventional branch module to process the mixed enhanced data of the image of the random sampler in the cowshed and the mixed enhanced data of the image of the balanced sampler in the milking channel, and the output features of the two branches are fused through a feature dynamic balancing factor, which changes with the number of training times, to solve the problem of poor image recognition rate performance of the single posture of the cow in the milking channel.

[0054] S2-2: Dynamic fusion of mixed image enhancement

[0055] Due to the small amount of cow body image data in the milking channel, the sampling probability of the cow body image in the milking channel can be increased by the balanced sampler, but the cow body image in the milking channel is easy to be over-sampled. Mixed image enhancement is to generalize the network model by synthesizing samples, but it is easy to fail to converge in the training process without restoring to real image data, which may bring new problems while solving the overfitting problem. Therefore, the algorithm proposes a dynamic fusion mixed image enhancement module to reflect the image mixing degree through dynamic fusion parameters to reduce the overfitting of the cow body image data in the milking channel. The specific formula is as follows.

[0056] λ=1-((T+n) / 2×T) (6)

[0057] (X′ r ,Y′ r )=λ(X r ,Y r )+(1-λ)(X p ,Y p ) (7)

[0058] (X′ p ,Y′ p )=(1-λ)(X r ,Y r )+λ(X p ,Y p ) (8)

[0059] In the formula, X r and Y ris the output image and label of the random sampler, X p and Y p is the output image and label of the balanced sampler, X' r , Y' r and X' p , Y' p is the mixed enhanced image and label of the random sampler and the balanced sampler, and is the mixing degree of the two images, n is the current training batch, and T is the total number of training rounds. By using the parameter to control the mixing degree of the two samplers, the overfitting phenomenon of the cow body image in the milking channel is avoided. And the mixing degree is gradually reduced during the training to finally restore the real cow body image.

[0060] S3, design of loss function

[0061] In the present application, the label smoothing cross-entropy loss is used to guide the network training.

[0062] The random noise coefficient is set to 0.1.

[0063] S4, the whole model is trained, as shown in the formula. Figure 1 Iterate the whole training set several times until the total loss of the model decreases to about 0.01.

[0064] S5, the test set image is input into the trained model to extract the cow image surface feature and perform recognition comparison.

[0065] The present application adopts the cosine distance as the standard for measuring the similarity of the cow surface features. The cosine distance calculation formula is as follows:

[0066]

[0067] The greater the cosine distance, the higher the similarity of the two cow surface features, and vice versa. The combined normalized features extracted from the test set cow images after 6, 4, 2, etc. are compared in 1:1 different classes, and the model comparison threshold T is obtained. Then the images in the test set are compared in the same class, and when the comparison value of the combined features extracted in the same class is greater than T, it is considered as a successful comparison. Otherwise, it is considered as a failed comparison.

[0068] The GPU server used in the experiment of the present application is NVIDIA TITAN RTX 3090, the deep learning framework used for training is Pytorch, the training batch size is 64, the iteration number is 50, the initial learning rate is 0.0024, and the learning rate is reduced by 0.1 when the iteration reaches 10, 30 and 50. The whole model uses Adam as the optimizer.

[0069] The following is based on Score-Weighted Class Activation Mapping (Score-CAM) for visualization research, and the present application is visualized and analyzed with ResNet-50, ViT, BBN and LA Transformer.

[0070] In Figure 4 , two different poses of cattle are visualized respectively, cattle a is a cow photographed in a cowshed, and images of multiple different poses of cattle a can be collected in a cowshed, and only two poses of cattle a are visualized in this paper; cattle b is a cow photographed in a milking channel, and there are only a few poses, and only two poses of cattle b are visualized in this paper. As can be seen from Table 3, the ResNet algorithm is difficult to focus on the key features of the cattle, and the ViT algorithm begins to focus on the important features of the cattle, but still cannot find the real features of the cattle, the LA Transformer algorithm has focused on some key features of the cattle, but still cannot focus on all the key features of the cattle; the BBN algorithm focuses on more key features than the LA Transformer, but still focuses on some noise; the algorithm (MBN-Transformer) in this paper not only reduces the focus on noise, but also focuses more clearly on the key features of the cattle, so the algorithm in this paper has better focusing ability for the key features of the cattle.

[0071] To verify the performance of the algorithm model in this paper, the performance of ResNet-50, ViT, BBN, LA Transformer and the algorithm (MBN-Transformer) in this paper is compared in the same two test sets, all models are tested 3 times in this paper, and the model is selected according to the highest recognition rate of the test set, and the ROC curve is as shown in Figure 5 , in the ROC curve, the horizontal coordinate is the false acceptance rate (False Acceptance Rate, FAR), and the vertical coordinate is the false rejection rate (False Rejection Rate, FRR).

[0072] Figure 5 is the ROC curve of the test set, which represents the false rejection rate of the test set at different false acceptance rates, and the right side is the result of de-logarithm of the left side. The cattle body features extracted by the algorithm in this paper are more robust, and as can be seen from Figure 2 , when FAR is 0, the performance of the algorithm (MBN-Transformer) in this paper is the best in the test set, which is reduced by 8.25%, 5.82%, 4.61% and 1.47% respectively compared with ResNet-50, ViT, BBN and LA Transformer.

[0073] As can be seen from Table 1, the algorithm (MBN-Transformer) herein has the best Top1 ranking performance, can extract more stable cow body features in unbalanced cow body data, and respectively increases by 11.57%, 7.87%, 5.46% and 1.12% compared with ResNet-50, ViT, BBN and LA Transformer in the test set.

[0074] A bilateral recognition algorithm based on the Transformer unbalanced cow body data is proposed herein, a dynamic fusion mixed enhancement module is used to reduce the overfitting phenomenon caused by the balanced sampler, and a Transformer encoder is used to design a regular branch and a balanced branch to process the image mixed enhancement data of the cowshed random sampler and the image mixed enhancement data of the milking channel balanced sampler respectively, and the input features of the two branches are added through a dynamic balance factor, so that the final features tend to the cow body data in the milking channel in the training process, to solve the unbalanced problem of the cow body image.

[0075] The examples of the application are described in detail above in combination with the drawings, but the application is not limited to the above examples, and various changes can be made within the knowledge of those skilled in the art without departing from the purpose of the application, which should also be considered as the protection scope of the application.

Claims

1.A method for bilateral recognition of unbalanced cattle body data based on an MBN-Transformer model, characterized in that The method increases the sampling rate of the few categories of cattle by adding a balanced sampler, reduces the overfitting problem caused by the balanced sampler by dynamically fusing a mixed enhancement module, and enhances the correlation of global information of the cattle body image by selecting a Vision Transformer as a shared backbone network structure; the balanced branch and the conventional branch module are designed by using the Transformer encoder to process the mixed enhanced data of the image of the random sampler in the cowshed and the mixed enhanced data of the image of the balanced sampler in the milking channel respectively, and the output features of the two branches are fused by a feature dynamic balancing factor, and the correlation of the cattle body data of the dynamic fusion mixed enhancement module and the global information are mined by using the multi-head self-attention mechanism of the Transformer encoder, which specifically includes the following steps: S1, construct a data set, and divide the data set into a training set and a test set, and the data set used is the cattle body image obtained by the cow group target detection segmentation network: S1-1: acquire image data of several types of cattle by the cow group target detection segmentation network; S1-2: expand the training set by translation, rotation and scaling operations; and normalize the image data of the expanded training set to the same resolution size; S2, design the MBN-Transformer model, which is implemented as follows: S2-1: The MBN-Transformer model comprises a sampler module, a dynamic fusion image mixed enhancement module, a shared backbone network, and two branch subnetworks, wherein the sampler module comprises a random sampler and a balanced sampler, the random sampler is to randomly sample all samples in the training set, but there are more images of the body of the cow in the cowshed in the training set, so it is more inclined to the images of the body of the cow in the cowshed, and the output image of the random sampler is X r ; the balanced sampler is to oversample the body data of the cow in the milking channel, and the balanced sampler is more inclined to the images of the body of the cow in the milking channel, and the output image of the balanced sampler is X p ; the two branch subnetworks comprise a regular branch and a balanced branch; the dynamic fusion mixed image enhancement module mainly prevents oversampling of the single posture body image in the milking channel by dynamically fusing the output images of the random sampler and the balanced sampler with the training process, and the output of the dynamic fusion mixed image enhancement module is X r and X p ; the shared backbone network adopts a Vision Transformer, namely a ViT model; the ViT model comprises image blocking and a plurality of layers of Transformer encoders; the image blocking divides the image into a plurality of identical blocks, and after the image blocking, position encoding representing the position information of the body of the cow and global features of learnable body information are added to obtain an input sequence; the input sequence is input into the Transformer encoder to output features F r and F p ; the two features F r and F p are respectively transmitted to the regular branch and the balanced branch, and the regular branch and the balanced branch both adopt the Transformer encoder, so as to better focus on the global features, and the output features of the regular branch and the balanced branch are F r and F p ; the two output features F r and F p are fused, and the specific formula is shown in the following formula. l=1-((n-1) / T) (1) F = l x F r F = l x F + (1 - l) x F p F = l x F + (1 - l) x F In the formula, n is the current training round, T is the total number of training rounds, l is the fusion degree of the two output features, and F is the final output feature delivered to the classifier; S2-2: design the sampler module: The random sampler is used to sample according to the number of samples of each class, and the sampling probability calculation formula of the jth cow of the random sampler is as shown in formula (3); wherein k is the total number of classes of the training set, S i is the number of cow body images of the i-th class of the cow in the training set, is the sampling probability of the j-th class of the cow of the random sampler; The sampling probability calculation formula of the jth cow of the balanced sampler is as shown in the following formula; where k is the total number of classes in the training set, N max is the maximum number of samples in all the cows in the training set, N i is the number of cow body images of the i-th cow class, W i is the sampling degree of the i-th cow class, is the sampling probability of the j-th cow class of the balanced sampler; as can be seen from equations (4) and (5), the cows in the milking channel with fewer samples are given more attention, while the cows in the cowshed with more samples are given less attention; The sampler module uses a random sampler and a balanced sampler to train a traditional conventional branch and a balanced branch respectively, the random sampler collects more cattle body image data in the cowshed, and the balanced sampler collects more cattle body image data in the milking channel, which can take into account the cattle body image data collected in two different ways in the cowshed; S2-3: design a dynamic fusion mixed image enhancement module: A dynamic fusion mixed image enhancement module is proposed, which uses dynamic fusion parameters to represent the image mixing degree and reduce the overfitting of the cattle body image data in the milking channel, and the specific formula is as shown in the following formula; λ=1-((T+n) / 2×T) (6) (X r ,Y r ) = λ(X r ,Y r ) + (1 - λ)(X p ,Y p ) (7) (X p ,Y p ) = (1 - λ)(X r ,Y r ) + λ(X p ,Y p ) (8) S3, design the loss function, which is implemented as follows: The label smoothing cross entropy loss is used to optimize the network, and the specific calculation formula is as shown in the following formula; L = 1 * L smoo (y p_true ,y pred ) + (1 - 1) * L smoo (y r_true ,y pred ) (9) L smoo (y p_true ,y pred )=∑[(1-ε)*y p_true +ε / N]lny pred (10) L smoo (y r_true ,y pred )=∑[(1-ε)*y r_true +ε / N]lny pred (11) In the formula, ε is the random noise coefficient, which is set to 0.1 in the algorithm of this chapter; l is the change of the training batch, and the change degree adopts the formula (1) mode; N is the total number of cattle classes in the training set of the cattle; y r_true is the real label after the dynamic fusion mixed image enhancement of the random sampler; y p_true is the real label after the dynamic fusion mixed image enhancement of the balanced sampler; y pred is the predicted class after the model output; L represents the total loss, L smoo is the label smoothing cross-entropy loss; it can be seen from the formula that the loss function will also tend to the real label after the dynamic fusion mixed image enhancement of the balanced sampler when optimizing the network, and the l parameter S4, train the entire model until the entire training set is iterated several times; S5, input the test set image into the trained model to extract the cattle image features, and use the cosine distance to compare and identify 1:1.

Citation Information

Patent Citations

  • Double-branch early warning classification method for class imbalance

    CN114332750A