Automatic plant counting method and system based on deep learning
By employing multi-scale backbone feature extraction, bi-branch cross-attention fusion, and multi-scale density map comparison learning, this method addresses the issues of multi-scale adaptability, complex backgrounds, and unstable density map prediction in existing plant counting techniques, achieving high-precision and robust plant counting.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-30
- Publication Date
- 2026-04-03
AI Technical Summary
Existing plant counting techniques struggle to account for multi-scale plant structures, suffer from significant interference from complex backgrounds, lack sufficient feature fusion mechanisms, lack structural consistency in density map predictions, and lack global modeling capabilities adapted to agricultural scenarios, leading to missed detections, false detections, and unstable counting.
A multi-scale backbone feature extraction module, a dual-branch cross-attention fusion module, and a multi-scale density map contrastive learning constraint module are adopted. Through multi-scale convolutional branches and Mamba state space modeling, combined with feature interaction fusion mechanism, a stable density map is generated and plant counts are performed.
It achieves high-precision and robust plant counting under different growth stages and complex backgrounds, reduces computational costs, improves the stability and real-time performance of counting, and solves the problems of multi-scale adaptability and unstable density map prediction in traditional methods.
Smart Images

Figure CN121789062A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of agricultural intelligent monitoring technology, and particularly relates to an automated plant counting method and system based on deep learning. More specifically, it relates to a plant counting method and system that combines a multi-scale convolutional neural network structure with a Mamba state space modeling structure, and is further supported by a feature interaction fusion mechanism and a multi-scale density map comparison constraint mechanism. Background Technology
[0002] With the development of digital agriculture, smart farms, and precision planting technologies, image-based automated plant counting technology has become one of the core requirements for agricultural production management. Plant count is an important indicator for measuring sowing quality, seedling condition, growth changes, and field management level, and plays a key role in tasks such as pest and disease monitoring, yield prediction, and crop condition analysis.
[0003] Current plant counting methods mainly rely on manual surveys or semi-automatic identification based on traditional visual algorithms. Manual counting is labor-intensive, significantly influenced by human experience, and difficult to meet the real-time monitoring needs of large-scale farmland. Traditional image algorithms typically segment or count based on color, texture, or edge features, which have poor robustness and are prone to misjudgment or omission when faced with complex field backgrounds such as weeds, soil texture, and changes in light intensity. In addition, crops exhibit significant morphological differences at different growth stages, from small target structures in the seedling stage to mesoscale leaves in the vigorous growth stage, and then to large-scale canopy structures in the mature stage. The scale variation is large, and traditional single-scale models are difficult to adapt to the multi-stage and multi-scale feature differences.
[0004] In recent years, deep learning methods have achieved remarkable results in object detection and density estimation tasks, but their direct application in agricultural scenarios still has several shortcomings. On the one hand, models that only utilize convolutional structures have limited receptive fields, making it difficult to fully capture long-distance relationships between plants, overall arrangement patterns, and macroscopic structural information. On the other hand, agricultural scenarios contain a large number of irregular occlusions, repetitive structures, and complex backgrounds, resulting in insufficient feature fusion and unreasonable feature weight allocation, making it difficult for the model to focus on the main plant. In addition, although density regression-based methods can handle densely planted areas, their training usually relies on simple loss functions and lacks constraints on the structural consistency and spatial distribution relationship of the density map, resulting in a relatively blurry density map that significantly affects the counting results.
[0005] In summary, existing technologies generally suffer from the following problems: (1) It is difficult to take into account the plant structure at multiple scales at the same time, resulting in missed or false detections at different growth stages. (2) Complex background interference is large and feature fusion mechanism is insufficient, which is not conducive to the model focusing on key areas of the plant; (3) Density map prediction lacks structural consistency constraints, and the expression of dense and sparse regions is unstable; (4) Lacking the ability to model the global landscape in agricultural scenarios, it is difficult to capture the macroscopic patterns of plant arrangement in the field.
[0006] To address the aforementioned problems, this invention proposes an automated plant counting method and system based on deep learning. Summary of the Invention
[0007] The purpose of this invention is to provide an automated plant counting method and system based on deep learning to solve the prominent problems of existing plant counting techniques, such as insufficient scale adaptability, significant interference from complex backgrounds, and unstable density map prediction.
[0008] To achieve the above objectives, the present invention adopts the following technical solution: An automated plant counting method based on deep learning includes the following steps: S1. Construct a multi-scale backbone feature extraction module. Use the multi-scale backbone feature extraction module to preprocess the farmland input image and then extract and fuse features to obtain the fused multi-scale backbone features. S2. Construct a dual-branch cross-attention fusion module to perform bidirectional cross-attention modulation and fusion on the multi-scale backbone features obtained in S1, and output unified fused features. S3. Construct a multi-scale density map contrast learning constraint module. Based on the unified fusion features output in S2, generate a multi-scale intermediate density map. Introduce contrast loss and density regression loss to jointly optimize the model and process the multi-scale intermediate density map to obtain the optimized fusion features. S4. Based on the optimized fusion features obtained in S3, a final density map with the same resolution as the input image is generated through the density regression module, and pixel integration is performed on the final density map to obtain the final plant count estimate, thereby achieving the goal of end-to-end automated plant counting.
[0009] Preferably, S1 specifically includes the following: S1.1 Perform size normalization, brightness standardization and basic noise reduction on the farmland input image, and then input it into the basic feature construction module composed of a convolutional encoder to extract the low-level structure including edge texture and obtain the basic feature map. S1.2. Set up multiple CNN convolutional branches of different scales on the basic feature map, including small-scale branches for capturing seedling features, medium-scale branches for characterizing the structure during the vigorous growth period, and large-scale branches for expressing the overall layout of the canopy during the mature period, so as to obtain multi-scale convolutional local features. S1.3. Input the obtained multi-scale convolutional local features into the Mamba global modeling branch, and obtain the Mamba global structural features through convolution enhancement, serialization, state space modeling and spatial reconstruction. S1.4 Align the multi-scale convolutional local features with the Mamba global structural features in the spatial dimension through upsampling, interpolation, or transposed convolution, and maintain the consistency of details by using residual connections to output the fused multi-scale backbone features.
[0010] Preferably, the small-scale branch captures fine leaf structures by maintaining high spatial resolution; the medium-scale branch expresses the complete plant structure through moderate downsampling; and the large-scale branch expresses the overall canopy layout through deeper convolution stacking. The small-scale, medium-scale, and large-scale branches all employ residual connections to preserve low-level details and alleviate the difficulty of training deep networks.
[0011] Preferably, the Mamba global modeling branch specifically performs the following operations: Improve feature discriminativeness using convolutional enhancement modules; The two-dimensional features are flattened into a one-dimensional sequence of length L=H×W and sequence normalization is performed. The long-range dependencies of plants in sparse, dense, and inter-row regions are captured through a selective state-space mechanism. The sequence is restored to a two-dimensional feature map and structural consistency is ensured through convolutional smoothing.
[0012] Preferably, S2 specifically includes the following: S2.1 Perform channel compression, spatial size alignment and numerical normalization on the input multi-scale convolutional local features, and perform channel compression and residual enhancement on the Mamba global structural features; S2.2. Using the processed multi-scale convolutional local features as the query vector and the processed Mamba global structural features as the key vector and value vector, the first attention weight map is obtained through the "local → global" cross attention calculation, and the Mamba global structural features are then subjected to position-by-position and channel-by-channel weighted modulation accordingly. S2.3. Using the processed Mamba global structural features as the query vector and the processed multi-scale convolutional local features as the key vector and value vector, the second attention weight map is obtained through the cross-attention calculation of "global → local", and the multi-scale convolutional local features are weighted and modulated accordingly. S2.4 The local and global features after weighted modulation in S2.2 and S2.3 are weighted and fused to obtain two sets of feature representations that are aligned in spatial structure and coordinated in semantic information. The two sets of feature representations are then concatenated in the channel dimension. The information is re-integrated by compressing the number of channels through convolutional mapping and nonlinear activation to output a unified fused feature.
[0013] Preferably, the first attention weight map is used to enhance the response of global features in dense plant areas, overlapping leaf areas, and local high-confidence areas; the second attention weight map is used to suppress the response of local features in noisy areas and background areas, and to enhance the expression of local details in areas consistent with the overall plant row and column structure and density trend.
[0014] Preferably, S3 specifically includes the following: S3.1 Generate intermediate density maps of multiple spatial scales based on unified fusion features; S3.2 Map the intermediate density maps to the same spatial size; S3.3 Construct positive sample pairs by taking the feature vectors corresponding to the same spatial location on intermediate density maps at different scales and constraining them to maintain high similarity in the feature space; wherein, the positive sample pairs are derived from density segments at different scales but the same spatial location, in order to maintain cross-scale density consistency; S3.4 Construct negative sample pairs from feature vectors corresponding to different spatial locations and constrain them to maintain their differences in the feature space; wherein, the negative sample pairs are derived from density segments of plant regions and background regions, and are used to enhance the ability to distinguish background noise; S3.5. The model is optimized by combining the contrast loss function and the density regression loss function. Different contrast loss weights are applied to the dense plant area, the edge transition area and the sparse area to make the generated density map have scale stability and structural consistency.
[0015] An automated plant counting system based on deep learning, comprising: A multi-scale backbone feature extraction module is used to perform size normalization, brightness standardization, and basic denoising on the farmland input image. This image is then input into a basic feature construction module composed of a convolutional encoder to extract the underlying structure, including edge textures, to obtain a basic feature map. Multiple CNN convolutional branches of different scales are set on the basic feature map, including a small-scale branch for capturing seedling features, a medium-scale branch for characterizing the vigorous growth stage structure, and a large-scale branch for expressing the overall canopy layout at maturity, thus obtaining multi-scale convolutional local features. These multi-scale convolutional local features are then input into the Mamba global modeling branch, where Mamba global structural features are obtained through convolutional enhancement, serialization, state space modeling, and spatial reconstruction. Finally, the multi-scale convolutional local features and Mamba global structural features are aligned spatially through upsampling, interpolation, or transposed convolution, and residual connections are used to maintain detail consistency, outputting the fused multi-scale backbone features. The dual-branch cross-attention fusion module performs channel compression, spatial size alignment, and numerical normalization on the input multi-scale convolutional local features, and channel compression and residual enhancement on the Mamba global structural features. Using the processed multi-scale convolutional local features as the query vector and the processed Mamba global structural features as the key and value vectors, a first attention weight map is calculated through "local → global" cross-attention, and the Mamba global structural features are then weighted and modulated positionally and channel-wise. Using the processed Mamba global structural features as the query vector and the processed multi-scale convolutional local features as the key and value vectors, a second attention weight map is calculated through "global → local" cross-attention, and the multi-scale convolutional local features are then weighted and modulated. The weighted and modulated local and global features are then weighted and fused to obtain two sets of spatially aligned and semantically coordinated feature representations. These two sets of feature representations are then concatenated along the channel dimension, and the information is re-integrated through convolutional mapping and nonlinear activation to compress the number of channels, outputting a unified fused feature. The multi-scale density map contrastive learning constraint module generates intermediate density maps at multiple spatial scales based on unified fusion features and maps them uniformly to the same spatial size. It constructs positive sample pairs by constructing feature vectors corresponding to the same spatial location on intermediate density maps at different scales, and constrains them to maintain high similarity in the feature space. These positive sample pairs originate from density segments at different scales but the same spatial location, used to maintain cross-scale density consistency. It also constructs negative sample pairs by constructing feature vectors corresponding to different spatial locations, and constrains them to maintain differences in the feature space. These negative sample pairs originate from density segments of plant regions and background regions, used to enhance the ability to distinguish background noise. The model is optimized by jointly using a contrastive loss function and a density regression loss function, applying different contrastive loss weights to dense plant regions, edge transition regions, and sparse regions, so that the generated density maps possess scale stability and structural consistency. The density map generation and plant counting inference module is used to input the optimized fusion features into the multi-scale density map contrast learning constraint module, and generate a final density map with the same resolution as the input image through convolutional mapping; pixel integration is performed on the density map to obtain the final plant number estimate, thereby achieving the goal of end-to-end automated plant counting.
[0016] The present invention further protects a computer device, the computer device including a processor and a memory, the memory storing at least one instruction, at least one program, code set or instruction set, the instruction, program, code set or instruction set being loaded and executed by the processor to implement the above-mentioned automated plant counting method based on deep learning.
[0017] The present invention further provides a computer-readable storage medium storing at least one instruction, at least one program, code set, or instruction set, wherein the instruction, program, code set, or instruction set is loaded and executed by a processor to implement the aforementioned automated plant counting method based on deep learning.
[0018] Compared with the prior art, the beneficial effects of the present invention are as follows: (1) A method for using the Mamba state-space model to plant counting scenarios was proposed. This invention replaces the quadratic complexity of traditional Transformers with long-range dependency modeling of linear complexity, enabling the model to maintain efficient inference capabilities even in high-resolution farmland images. This design significantly reduces computational costs while obtaining global plant arrangement and density trends, resolving the trade-off between global modeling accuracy and real-time performance.
[0019] (2) A hybrid skeleton with multi-scale convolutional branches and global modeling branches was constructed. The multi-scale convolution path proposed in this invention covers the scale span from seedling stage to vigorous growth stage to maturity stage. Different levels of plant structure are extracted through differentiated downsampling ratios and convolution depths. The Mamba branch compensates for the limited receptive field of convolution, realizing a global characterization of field row structure, plant density patterns and overall spatial layout. The combination of the two overcomes the shortcomings of the limited perception range of traditional CNNs.
[0020] (3) A dual-branch cross-attention fusion mechanism was proposed. This invention achieves dynamic spatial and semantic interaction of features at different scales through bidirectional gating weight generation of local and global features, solving the problem of insufficient adaptability of traditional fixed fusion strategies in complex backgrounds. This mechanism enables the network to focus on local leaf details while maintaining the consistency of the overall plant distribution, significantly improving the ability of fused features to distinguish densely planted areas, shadow interference areas, and background noise areas.
[0021] (4) A multi-scale density map contrastive learning constraint was designed. This invention optimizes the density map at both structural and numerical levels through positive and negative sample construction and spatial consistency constraints, improving the stability and coherence of the density map at different scales and reducing noise activation and density fragmentation. This design enhances the reliability of density estimation, improves the interpretability of results, and provides a more accurate density basis for the final integral counting.
[0022] (5) An end-to-end architecture of multi-scale input, feature fusion, density constraint, and counting inference has been formed. This invention constructs a fully integrated counting system from input to output by covering different plant sizes through multi-scale input, achieving local and global balance through a fusion mechanism, maintaining structural continuity through density constraints, and outputting the final count through density integration. This avoids the problems of structural complexity and cumbersome post-processing in traditional detection or segmentation methods. Attached Figure Description
[0023] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings involved in the embodiments are now briefly described. Obviously, the drawings in the following description are merely illustrative of some embodiments of the present invention. For those skilled in the art, other forms of drawings can be constructed based on these drawings without creative effort.
[0024] Figure 1 This is a schematic diagram of the overall structure of the end-to-end plant counting network based on multi-scale hybrid backbone and Mamba global modeling proposed in Embodiment 2 of the present invention. Figure 2 This is a schematic diagram of the data flow and loss calculation process of the end-to-end plant counting network mentioned in Embodiment 2 of the present invention during the training and inference phases. Detailed Implementation
[0025] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0026] This invention proposes an automated plant counting method and system based on deep learning. With an overall framework design of "multi-scale structural expression + global dependency modeling + bidirectional interactive fusion + multi-scale density consistency constraint", it can efficiently analyze the morphological scale features of plants in different growth cycles and maintain stable counting performance in field scenarios such as severe shading, large light changes, and complex backgrounds, thereby outputting high-precision and robust plant number estimation results.
[0027] The core idea of this invention is to decompose the plant counting problem into three highly related sub-tasks: "local structure enhancement," "global relationship modeling," and "density distribution consistency constraint." First, multi-scale convolutional branches are used to capture the structural differences of plants from the seedling stage to the vigorous growth stage and maturity stage, thus solving the problem that traditional single-scale models cannot cover the scale span. Then, the Mamba state space modeling structure is used to encode the spatial arrangement and long-distance dependence of plants in the whole image, making up for the deficiency of convolutional models in perceiving global structure. On this basis, a two-branch cross-attention fusion mechanism is used to achieve bidirectional guidance and interactive enhancement of local and global information, enabling the network to actively focus on the main body of the plant while suppressing the interference of invalid areas such as soil texture, weeds, and shadows. Finally, multi-scale density map contrast constraint is introduced to stabilize the spatial consistency of the density map by controlling the positive and negative sample structure, so that the final density estimate remains smooth in sparse areas and clearly expressed in dense areas, further improving the overall counting accuracy. Through the above structural combination and optimization strategies, this invention constructs an end-to-end optimizable plant counting process that can provide reliable, stable and deployable counting capabilities under various crop categories, different shooting conditions and different spatial scales. It can be widely used in crop growth monitoring, farmland inspection, yield assessment and intelligent agricultural machinery equipment, and belongs to the cross-technology direction of agricultural informatization and artificial intelligence.
[0028] The following description, in conjunction with the accompanying drawings and specific examples, illustrates an automated plant counting method and system based on deep learning proposed in this invention.
[0029] Example 1: This example presents an automated plant counting method and system based on deep learning, including the following: Step 1: Construct a hybrid multi-scale backbone feature extraction module This step constructs a hybrid multi-scale backbone network that can adapt to different growth stages and target size differences of plants. It comprehensively utilizes the local modeling capabilities of convolutional networks and the global modeling capabilities of the Mamba state-space model to form a multi-level plant representation from fine-grained leaf texture to the overall canopy structure. This provides a unified feature foundation for subsequent cross-attention fusion and density estimation. Specifically, it includes the following: Basic Feature Construction Module: This invention first performs size normalization, brightness normalization, and basic noise reduction on the input farmland image, mapping images from different devices, shooting distances, and lighting conditions to a more consistent numerical range. The preprocessed image is represented as follows: X∈R H×W×Cin Among them, H, W and C in These represent the spatial size and number of channels of the feature map, respectively. The input features are then fed into a basic convolutional encoder consisting of multiple convolutional layers, normalization layers, and nonlinear activation functions to extract low-level structural information such as edges, textures, and color variations, while suppressing background noise unrelated to plant counting. Multi-scale convolutional feature path: On the basic feature map, this invention constructs a multi-scale convolutional feature path, setting multiple convolutional branches with different receptive fields in parallel on the same basic feature map. Specifically, the fine-scale branch uses the convolution kernel parameter W... fine By performing a two-dimensional convolution operation on the input feature X and applying a nonlinear mapping, fine-scale local features are obtained: F fine =σ (W fine X+b fine ) This branch preserves high spatial resolution to capture the local texture structure of seedlings and small leaves. Meanwhile, the mesoscale branch employs a convolution kernel with parameter W. mid Convolutional computation is performed on the input features to generate a mesoscale feature representation: F mid =σ (W mid X+b mid ) This branch, through moderate downsampling and receptive field expansion, focuses on characterizing the medium-scale texture and plant structure of vigorous plants. Furthermore, the large-scale branch uses convolution kernels W with larger receptive fields. coarse Modeling the input features yields coarse-scale features: F coarse =σ (W coarse X+b coarse ) This branch focuses on perceiving the continuous distribution characteristics and overall row and column structure information of the canopy of mature plants. Through parallel modeling of the above-mentioned convolution branches at different scales, this invention, while maintaining a unified convolution calculation form, achieves the extraction of features from different receptive field ranges only by varying the convolution kernel size and parameter configuration. Subsequently, the fine-scale features F... fine Mesoscale features F mid and coarse-scale feature F coarse By fusing the data, we obtain multi-scale local feature representations: F local =C (F fine ,F mid ,F coarse ) Among them, C( The symbol ") represents a feature fusion operation, used to integrate local structural information at different scales along the channel or spatial dimensions. In the convolution operation described above, the symbol " " represents a two-dimensional convolution process, and its computational form at the s-th scale branch, k-th output channel, and spatial location (x,y) can be expressed as:
[0030] This calculation, by weighted summing of multi-channel information within the local neighborhood of the input features, ensures that branches at different scales maintain consistency in numerical computation while obtaining complementary spatial structural representations at the feature semantic level. Through the aforementioned multi-scale convolutional modeling method, this invention can simultaneously capture fine-grained leaf texture, medium-scale plant structure, and large-scale canopy distribution features within the same backbone structure, providing a stable and structurally rich local feature foundation for subsequent global modeling, cross-attention fusion, and density regression.
[0031] Mamba Global Modeling Branch: To compensate for the limited receptive field of convolution, this invention introduces a Mamba-based global modeling branch into the backbone. High-semantic and representative intermediate feature maps are selected and enhanced using one or two layers of 3×3 convolutions and normalization. Then, the two-dimensional feature maps are flattened in space into a feature sequence of length L=H×W, and layer normalization is applied to the sequence to obtain the input that meets the requirements of state-space modeling. Let the fused feature representation of the multi-scale convolutional backbone network output be: F local ∈R H×W×C Where H and W represent the spatial resolution of the feature map, and C represents the number of channels. For global modeling, the two-dimensional feature map is expanded spatially into a one-dimensional feature sequence of length L = H × W: X=Flatten(F local)∈R L×C Through this mapping operation, features originally located at different positions in two-dimensional space are uniformly represented as a sequence, enabling subsequent models to model the relationships between arbitrary spatial positions along the sequence dimension. In the sequence modeling phase, Mamba employs a recursive mechanism based on a state-space model to globally model the feature sequence. For the t-th feature vector x in the sequence... t ∈R C Define the corresponding hidden state h t ∈R D Its state update process can be represented as:
[0032]
[0033] Among them, h t Let y represent the hidden state at position t. t This represents the output features after global modeling. , Let C be the learnable parameter matrix. Based on the above recursive relationship, the current hidden state h... t Simultaneously dependent on the current input feature x t Including historical state information accumulated from all previous positions, this enables long-range dependency modeling in the sequence dimension. When plant distribution is relatively stable, the model relies more on historical state information; when density or spatial structure changes abruptly, the model correspondingly increases the response weight to the current input feature, effectively capturing large-scale plant distribution patterns and overall density change trends while maintaining linear computational complexity. Since sequence X originates from the unfolding of two-dimensional spatial features, this process is equivalent to modeling the dependency relationship between any two spatial locations in the feature map. After completing the global modeling in the sequence dimension, the output sequence is remapped back to the two-dimensional spatial structure to obtain the global feature representation: F global =Reshape(Y)∈R H×W×C Among them, F global This represents the sequence features after global modeling. Through this reconstruction operation, the model obtains global semantic features consistent with the spatial structure of the original feature map, which facilitates further fusion with local convolutional features or direct use in subsequent density regression prediction.
[0034] Structural alignment and residual enhancement mechanisms: Since the features output by multi-scale convolutional branches and Mamba branches differ in spatial resolution and semantic level, this invention uses bilinear interpolation, upsampling, or transposed convolution to uniformly map features at each scale to the target spatial size, ensuring consistent coordinates of the same physical location across different feature maps. Simultaneously, residual connections are introduced within each convolutional branch, adding the convolutional output to the input features. This provides a shortcut path for the gradient, alleviating the training difficulties of deep networks, and preserves low-level details in high-level semantics, preventing small target plants from being completely erased during multiple convolutions and downsampling processes.
[0035] Multi-scale backbone feature output: After basic feature construction, multi-scale convolutional modeling, Mamba global modeling, and structural alignment and residual enhancement, this step finally outputs a set of backbone feature maps with uniform spatial dimensions and complementary semantic levels. These maps simultaneously encode fine-grained leaf texture information and the overall plant distribution pattern in the field. These backbone features will serve as inputs to the subsequent bi-branch cross-attention fusion module and multi-scale density contrast constraint module, forming the feature foundation for the end-to-end plant counting network.
[0036] In summary, step 1 covers different structural scales of the plant from seedling to maturity through multi-scale convolutional paths. It supplements long-range dependencies that convolution struggles to capture by introducing a Mamba global modeling branch and enhances feature stability through scale alignment and residual enhancement mechanisms. This step establishes a complementary relationship between local structural details and the overall plant arrangement pattern, enabling the backbone network to effectively perceive morphological changes at different scales. This avoids the problems of traditional single-path feature extraction, such as losing details at small scales and lacking semantic consistency at large scales, providing a hierarchical, complete, and structurally rich feature input foundation for subsequent fusion modules.
[0037] Step 2: Dual-branch cross-attention fusion module This step aims to address the inherent differences between local convolution and global modeling branches in terms of semantic abstraction level, receptive field coverage, and feature focus. By constructing a bidirectional cross-attention mechanism, the two feature streams can achieve deep semantic alignment, structural complementarity, and dynamic weight adjustment before fusion, thereby significantly enhancing the model's ability to recognize dense vegetation, complex backgrounds, and regions with abrupt scale changes. Specifically, it includes the following: Local branch feature compression module: Local convolutional features contain rich fine-grained structural information, but are accompanied by redundant dimensions and background noise. Therefore, this invention compresses local features through lightweight convolution, forming a more compact local feature representation that can be used for attention interactions while preserving leaf details, plant boundaries, and local density gradients. Let the local features output by the multi-scale convolution module be: F local ∈RH×W×C Where H and W represent the spatial resolution of the feature map, C represents the number of channels, and F represents the number of channels. local This represents the local feature representation after multi-scale convolution extraction and scale alignment. After compression and mapping, the local features are linearly transformed into a query, key, and value vector, where the local features participate in subsequent interactions as queries and values. Its form can be expressed as:
[0038] in, , and These represent the query vector, key vector, and value vector generated from local features, respectively. For the corresponding learnable linear mapping parameters, the subscript l indicates a local branch.
[0039] During compression, normalization and nonlinear activation are used to suppress distribution shifts caused by changes in lighting, shooting angle, or background, ensuring sufficient stability of local features in subsequent cross-attention modules. The processed local features not only retain the texture details of the plant but also possess strong discriminative power, which is beneficial for guiding the attention calculation of global branches.
[0040] Global Branch Feature Compression Module: The Mamba global modeling branch can capture the overall distribution relationship, row and column structure, and density trend of plants in the field, but its original feature channels still contain redundant or noisy semantics. To improve its effectiveness in cross-branch interactions, this invention also uses a combination of convolution and activation to compress the global features, making them more focused on expressing semantic information related to the overall plant structure. Let the features output by the global modeling branch be: F global ∈R H×W×C Here, Fglobal represents the global semantic feature representation obtained after Mamba global modeling. Similarly, the global features are mapped to corresponding query, key, and value vectors:
[0041] in, , and These represent the query vector, key vector, and value vector generated from global features, respectively. , , The parameters are learnable linear mappings, and the subscript g denotes the global branch. After compression and mapping, the global features maintain the ability to model long-range dependencies while avoiding weight dispersion due to too many channels in attention calculation, so that local and global features are in a more matching feature space before fusion.
[0042] Cross-attention weight generation module: This module is the core of the entire fusion mechanism. By constructing a bidirectional query-key-value interaction structure, it enables local and global features to achieve deep correlation before fusion.
[0043] In the "local → global" direction, the local branch uses its high-precision texture and structural features as the query vector. This key-value mapping applied to global features gives higher attention weights to global branches in densely planted areas, seedling concentration areas, and overlapping leaf areas, avoiding the over-smoothing problem that occurs in Mamba global modeling. Query vectors generated from local features. Key vector generated with global features Calculate the attention weight matrix:
[0044] in, This represents the global attention weight matrix generated by local features, where d is the feature dimension of the query and key vectors, used for scaling the inner product result. Based on this attention weight, the global value vector... A weighted update is performed to obtain a global feature representation enhanced by local features:
[0045] in, This represents the global features guided by local features.
[0046] In the "global → local" direction, global features use their overall distribution patterns as guiding information to assign new attention weights to local features. This causes local branches to reduce their response in noisy regions that do not conform to the overall density trend, while being strengthened in locations with higher structural consistency. This allows local features to break free from the limitations of being purely detail-driven.
[0047] The query vector Qg generated from global features is used to generate the key vector K from local features. l Perform attention calculations:
[0048] in, This represents the local attention weight matrix generated under the guidance of global semantics. Using this weight matrix, the local value vector Vl is updated with weights to obtain the enhanced local feature representation under global semantic constraints.
[0049] in, This represents local features guided by global semantics.
[0050] Through the aforementioned bidirectional cross-attention mechanism, local features can suppress background noise and highlight key plant areas under the constraints of the global context, while global features can obtain a more refined spatial response under the guidance of local salient areas, thereby establishing a stable complementary relationship between detail perception and overall structural understanding.
[0051] This bidirectional interactive structure borrows from the "modal mutual guidance mechanism" in multimodal cross-attention networks, enabling the establishment of stable associations between local features and global semantics, thereby enhancing the robustness of the model in complex scenarios.
[0052] Feature weighting and spatial fusion module: After generating bidirectional attention weights, this module updates local and global features with weights, enabling deep fusion of the two in terms of spatial continuity, structural consistency and semantic interpretability.
[0053] Guided by global semantics, local features automatically suppress background noise and illumination interference areas, and enhance responses at plant edges, densely planted areas, and key structural transition zones. Global features, supplemented by local details, gain stronger local sensitivity, enabling them to more accurately represent local changes within the overall density structure.
[0054] The weighted features achieve pixel-by-pixel semantic alignment on the same spatial dimension, making the fused output more recognizable, continuous and locally consistent.
[0055] Feature concatenation and unified mapping module: After completing the bidirectional cross-attention enhancement, this module processes the updated local features. With global features The two types of features are fused to form a unified feature representation for subsequent density regression. Specifically, the two types of features are first concatenated along the channel dimension to obtain a composite feature representation containing information from multiple sources. Then, a convolutional mapping layer is used to integrate the number of channels after concatenation, so that the fused feature has a channel structure suitable for use by the density regression head.
[0056] From an overall perspective, the fusion process can be uniformly represented as:
[0057] Among them, F fused This represents the final feature representation after the two-branch cross-attention fusion. ( ) is a feature fusion function used to achieve element-wise weighted summation, or to perform unified integration by concatenating along the channel dimension and then performing linear mapping (such as convolution transformation).
[0058] In the specific implementation process, the combined effect of nonlinear activation functions and convolutional mapping is used to further enhance the upper limit of the expressive power of the fused features, resulting in fused features that are ultimately obtained. It possesses the ability to describe fine-grained structures, model global dependencies, and maintain cross-branch consistency. Specifically, the texture and edge information preserved by the convolutional branches are... It provides precise local detail awareness; the long-range dependencies built by the Mamba global modeling branch enable... It can understand the overall distribution structure and density trend of plants in the field; and the dynamic weight adjustment relationship established between the two feature branches by the bidirectional cross-attention mechanism further enhances the fusion features in terms of spatial consistency, semantic coordination and responsiveness to key plant areas.
[0059] Through the aforementioned feature splicing and unified mapping module, this invention can achieve complementarity in three aspects: local detail perception, overall spatial layout understanding, and dynamic adaptive adjustment, thus enabling the fusion of features. It serves as the core input to the density map estimation module, providing a solid feature foundation for achieving high-precision plant counting under conditions of dense plant growth, significant scale variations, and complex backgrounds.
[0060] In summary, step 2 constructs a bidirectional cross-attention structure with local and global branches. This allows local leaf textures and microstructures to be guided by the overall planting pattern, while global density trends, aided by local responses, enable more precise localization of plant areas. Compared to traditional unidirectional attention or simple splicing fusion methods, this mechanism achieves bidirectional dynamic guidance between feature dimensions. This allows the model to automatically adjust the importance of local and global information for different density regions and plant arrangements, avoiding the limitations of fixed fusion weights that cannot adapt to different scenarios, and significantly improving feature discrimination capabilities in complex backgrounds.
[0061] Step 3: Multi-scale density map comparison constraint mechanism This step aims to improve the structural stability of density maps in scale-varying scenarios. By constructing multi-scale positive and negative samples, spatial consistency constraints, and multiple contrastive losses, the network can maintain consistent density predictions at different scales, thereby obtaining a density map representation with clear structure and less noise. Specifically, it includes the following: Multi-scale density map generation module: in obtaining fused features Subsequently, this invention not only generates a final high-resolution density map for counting, but also generates several intermediate density maps in the intermediate layers of the network using convolutional heads of different resolutions. These intermediate density maps correspond to the plant density distribution at coarse, meso, and fine scales, respectively, reflecting the spatial layout of plants in the farmland from different observation scales. Let the density maps predicted by the network at different scales be as follows:
[0062] In this context, the superscripts f, m, and c represent the density prediction results at fine, medium, and coarse scales, respectively.
[0063] To facilitate pixel-by-pixel comparisons across different scales, this invention upsamples the aforementioned density maps to the same spatial resolution:
[0064] Among them, Up( ) indicates bilinear interpolation and other upsampling operations, which strictly align density maps at different scales in spatial coordinates.
[0065] Positive Sample Construction Module (Cross-Scale Consistency): For each spatial location, this invention treats corresponding density fragments from density maps at different scales as multiple "views" describing the same plant region, thus requiring high similarity in the feature space. That is, for the same spatial location (x, y), density predictions at different scales should remain consistent.
[0066] Based on the above consistency assumption, this invention further defines a multi-scale density consistency constraint loss term:
[0067] Here, S={(f,m),(f,c),(m,c)} represents the set of scale pairs. This loss, by constraining the consistency of predictions at the same spatial location across different scales, forces the model to form stable judgments about the same plant region under scale-varying conditions.
[0068] Negative Sample Construction Module (Cross-Regional Difference): To enhance the model's ability to distinguish between plant regions and background regions, as well as regions with different density levels, this invention also explicitly constructs negative sample pairs. Specifically, for a density segment marked as a densely planted region at a certain scale, this invention samples the corresponding segment from the density map of the background region or the sparsely planted region as a negative sample; and vice versa.
[0069] At the feature level, to further enhance this discriminative ability, this invention maps multi-scale density maps to the embedding space:
[0070] in, ( ) represents the feature embedding mapping function. For the same spatial location, the embedding representations obtained at different scales ( Considered as positive sample pairs, while embedding representations from different spatial locations ( Then they constitute a negative sample pair.
[0071] Spatial Structure Consistency Constraint Module: In addition to comparing the density of individual points or small segments, this invention also considers the overall spatial structure consistency of the density map. By prioritizing neighborhood relationships in dense plant areas and transition regions at the edges during comparative sampling, this invention enables the model to place greater emphasis on the continuity of density distribution and the smooth transition of boundaries during training. For example, at the boundary between inter-row gaps and intra-row dense areas, the design of positive and negative samples at these locations allows the network to correctly learn the structural pattern of "smooth transition from high density to low density," avoiding a large number of unrealistic isolated high-response points or abrupt density breaks in the density map. This spatial structure consistency constraint further improves the interpretability of the density map, making it closer to the actual plant distribution.
[0072] Contrastive Loss Fusion Module: In the design of the loss function, this invention weighted and fused the contrastive learning loss of the multi-scale density map with the traditional density regression loss (such as L2 loss or L1 loss). The density regression loss mainly constrains the numerical difference between the predicted density value and the true density, ensuring the accuracy of the overall count; while the contrastive loss constrains the density map from the perspective of feature distribution and spatial structure, making it semantically clearer in terms of inter-class separation and intra-class compactness. By simultaneously minimizing the weighted sum of the two types of losses during training, this invention achieves a balance between numerical accuracy and structural rationality, avoiding the problem of density map blurring caused by optimizing only the regression loss, and avoiding the situation of focusing only on the contrastive structure while ignoring the absolute counting accuracy.
[0073] Based on the above construction of positive and negative samples, this invention further introduces a multi-scale contrastive consistency loss to enhance the consistency of density expression at the same spatial location at different scales. Its definition is as follows:
[0074] Where, sim( ) represents the feature similarity function, used to measure the similarity between features embedded at different densities; τ is a temperature coefficient used to adjust the smoothness of the similarity distribution in contrastive learning; P and N represent the sets of positive and negative sample pairs, respectively. By minimizing this contrastive loss, the model can bring the density representations of the same plant region closer together at different scales in the feature space, while widening the feature distance between different spatial locations or different semantic regions, thereby enhancing the discriminative power of the density map at the structural level.
[0075] During the training phase, this invention combines the multi-scale density consistency constraint loss, the contrastive consistency loss, and the density regression loss to form an overall optimization objective, which takes the following form: L=L reg + λ1 L ms + λ2 L con Among them, L reg Used to constrain the numerical error between predicted density values and actual density, ensuring the accuracy of overall plant count results; L ms With L con The density map is then supplemented with constraints from the perspectives of cross-scale numerical consistency and feature distribution discriminability; λ1 and λ2 are weight coefficients used to balance the relative contributions of each loss term during training.
[0076] In summary, step 3 generates density representations covering different spatial granularities through multi-scale density maps. By constructing positive samples (different scales at the same location) and negative samples (different semantics in different regions), the model establishes more stable density consistency constraints during training. Simultaneously, spatial structure consistency design strengthens the continuous representation of densely planted areas, sparse areas, and boundary transition areas, avoiding density breaks, isolated high-response points, or background false activations common in traditional density regression. This module achieves dual-level "structure-numerical" supervision of the density map, making the final density map closer to the actual plant distribution.
[0077] Step 4: Density map generation and plant counting inference This step is used to input the aforementioned features, which have undergone multi-scale feature fusion and contrast constraint optimization, into the density regression network to generate the final density map for counting inference, and to obtain the number of plants through spatial integration, thereby realizing an end-to-end automated plant counting process.
[0078] Density Regression Module: After completing feature fusion and constraint optimization, the fused feature representation obtained by the network is denoted as: F fused ∈R H×W×C Where H and W represent the spatial resolution of the feature map, and C represents the number of channels.
[0079] The density regression module uses the density regression mapping function ψ( The fused features are mapped to a prediction density map. D^(x,y)=ψ(F fused )∈R H×W Where D^(x,y) represents the predicted plant density value at spatial location (x,y).
[0080] In its specific implementation, the density regression mapping function ψ(•) is implemented by a convolution operation, and its computational form can be expressed as:
[0081] Among them, W c,i,jLet represent the parameters of the density regression convolution kernel, b be the bias term, and k be the kernel size. Through this convolutional regression operation, the local structural information and global semantic information in the fused features are mapped into a continuous spatial density distribution.
[0082] Gaussian Modeling of True Density Maps: To supervise the density regression process, this invention constructs true density maps based on manually labeled plant center points. Suppose an image contains N plants, and their corresponding labeled locations are:
[0083] Based on the above point annotations, the true density map D(x,y) is constructed as follows:
[0084] Where g(•) represents the two-dimensional Gaussian kernel function, which is defined as:
[0085] parameter The smoothness of the density distribution is controlled. Through the Gaussian modeling method described above, discrete point labels are transformed into a continuous, differentiable density distribution, providing a stable supervision signal for the training of the density regression network.
[0086] Density regression loss function: During the training phase, this invention constrains the numerical consistency between the predicted density map and the true density map through a density regression loss function, which is defined as follows:
[0087] in, This represents the density regression loss, which measures the difference between the predicted density map and the true density map at the pixel level. By minimizing this loss, the network can learn a more accurate density mapping, thereby ensuring the numerical accuracy of the overall plant count results.
[0088] Density integration counting module: After completing the density map prediction, this invention obtains the final plant quantity estimate by spatially integrating the predicted density map. The calculation form is as follows:
[0089] in, This represents the predicted total number of plants. Because the density map depicts the spatial distribution of plants in a continuous distribution form, this integration operation can still stably accumulate local density information even when plants are shading, overlapping, or densely planted, thus obtaining reliable counting results.
[0090] In summary, step 3 maps the fused features to a density map with the same resolution as the input image using a density regression head, and achieves unified counting of non-overlapping and occluded plants through integration. Because the density map contains continuous distribution information, this step can still output stable counting results even in areas with dense vegetation, complex backgrounds, or overlapping areas. This step simplifies the cumbersome post-processing operations in traditional detection- or segmentation-based methods, forming an efficient, unified, end-to-end counting inference process.
[0091] Example 2: Based on Embodiment 1, but with some differences, the present invention will be further described below with reference to the accompanying drawings, specifically including the following: This example proposes an end-to-end plant counting network based on a multi-scale hybrid backbone and Mamba global modeling. It is an improvement addressing three core pain points of current plant counting techniques: poor scale adaptability, strong interference from complex backgrounds, and unstable density estimation. Existing methods often struggle to maintain counting accuracy for both small-scale seedlings and large-scale mature canopies when dealing with different growth stages, crop varieties, and shooting heights. Furthermore, complex background interference such as weeds, soil textures, agricultural machinery tracks, and shadows commonly exist in field scenes, easily leading to missed or incorrect counts. In addition, density estimation in dense, occluded, and sparse plant areas is prone to structural breaks and noise responses. This invention, through a collaborative architecture of "hybrid multi-scale backbone feature extraction + bi-branch cross-attention fusion + multi-scale density map comparison constraints + end-to-end density regression and counting inference," achieves stable plant counting across all scenes while ensuring controllable computational complexity. This provides crucial automated counting technology support for applications such as farmland inspection, yield assessment, and intelligent agricultural machinery control.
[0092] like Figure 1 As shown, the overall structure of the end-to-end plant counting network based on multi-scale hybrid backbone and Mamba global modeling proposed in this invention mainly includes the following four key stages: (1) Hybrid multi-scale backbone feature extraction: A hybrid backbone network is constructed by combining multi-scale convolutional paths with the Mamba global modeling branch to extract structural information at different levels, such as seedling stage, small plants, densely planted rows and columns, and large-scale canopy layout, forming backbone features that contain both local texture and global distribution patterns, providing a foundation for subsequent fusion.
[0093] (2) Design of dual-branch cross-attention fusion module: Through the cross-attention mechanism between local and global branches, the convolutional features and Mamba features are bidirectionally guided and weighted to fuse, so that the network can adaptively adjust the importance of local and global information according to the plant density and background complexity of different regions, forming more discriminative fusion features.
[0094] (3) Multi-scale density map contrast constraint mechanism design: Based on the fusion features, intermediate density maps of multiple scales are generated. Density segments at the same spatial location but different scales are used as positive samples, and density segments at different locations or background areas are used as negative samples. Contrast learning loss and spatial structure consistency constraint are introduced to make the density map have a more stable and continuous structural expression at different scales.
[0095] (4) Density map generation and plant counting inference: The fused features are mapped to a density map with the same resolution as the input image through the density regression module, and the final plant number estimate is obtained by integrating the density map, thus forming an end-to-end automatic counting process.
[0096] Through the synergistic effect of the above four stages, the network framework proposed in this invention achieves efficient global modeling, fine local perception, adaptive feature fusion, and structurally stable density estimation. The final output is a prediction result that combines counting accuracy and robustness, and can be widely applied to plant counting tasks of different crop types and different shooting platforms (drones, ground cameras, etc.).
[0097] In the hybrid multi-scale backbone feature extraction stage, the multi-scale convolution branch and the Mamba global modeling branch undertake the modeling tasks of local and global features, respectively. The multi-scale convolution branch models the leaf expansion structure during the seedling stage, the leaf expansion structure during the vigorous growth stage, and the overall canopy layout during the mature stage by setting convolution paths with different downsampling ratios and receptive field sizes. The Mamba module focuses on the row and column structure, dense planting zone and gap zone distribution patterns of the farmland as a whole, enabling the network to understand the local leaf texture while also capturing the planting pattern and density trend of the "whole plot". The two complement each other to form a multi-scale backbone feature covering the "local-global" perspective.
[0098] Specifically, the Mamba module, as the core of global modeling, innovates by introducing a state-space model into the plant counting task, overcoming the limitation that the traditional Transformer self-attention mechanism has a computational complexity of O(L) when the feature map is large. 2This addresses the bottleneck of traditional methods. By flattening the two-dimensional feature map into a one-dimensional sequence with a sequence length L = H × W, where H is the feature map height and W is the feature map width, and then using a selective state-space mechanism to process the sequence with a linear time complexity of O(L), the network maintains good inference efficiency when processing high-resolution farmland images, avoiding the computational burden of global modeling. Simultaneously, the Mamba module front-end preprocesses the input features through convolutional residual blocks, enhancing the local structural representation and discriminative power of the features without changing the channel dimension. The back-end reconstructs the one-dimensional sequence into a two-dimensional feature map through spatial reconstruction and eliminates the spatial offset caused by serialization through convolutional smoothing, ensuring that the global features remain aligned with the convolutional branches in the spatial dimension, providing a high-quality global semantic prior for subsequent dual-branch fusion.
[0099] The multi-scale convolution module focuses on multi-scale perception of local details. By setting independent convolution branches for different scales, this invention can extract features from small target plants, medium-scale structures between rows, and large-scale canopy block regions. Higher-resolution branches can capture leaf outlines, sparse distribution of a few plants, and small gaps; medium-resolution branches are suitable for densely planted areas and areas with gaps; and low-resolution branches cover plant arrangement patterns in entire rows and areas. In each branch, the separability of features is improved by combining convolution, normalization, and nonlinear activation, and residual connections are used to avoid the difficulties of training deep networks. Through multi-scale parallel design, this invention effectively compensates for the shortcomings of traditional single-scale backbones in scale generalization ability, enabling the network to adapt to significant scale changes brought about by different growth stages and different crop varieties.
[0100] The bi-branch cross-attention fusion module is a crucial link connecting the hybrid backbone and the density estimation module. Its core function is to adaptively fuse local convolutional features with Mamba global features. The local and global branches are transformed into compact feature representations suitable for attention computation through channel compression and lightweight mapping, respectively. Subsequently, the local branch generates a set of attention weights based on its fine-grained structural information and applies them to the global features, enhancing the global feature responses in dense leaf areas and in-row clustering regions. The global branch, on the other hand, uses the overall density trend to generate another set of weights and applies them to the local features, suppressing local background noise responses from a large-scale layout perspective. Through this bi-directional cross-attention mechanism, the network can adaptively balance the weights of "looking at details" and "looking at the whole" in both spatial and channel dimensions, allowing different regions to obtain different fusion strategies based on their own plant density and background complexity, thereby effectively improving the feature discriminability and robustness in the density estimation stage.
[0101] The multi-scale density map contrast constraint mechanism addresses the structural stability and cross-scale consistency issues of density estimation. During density regression, this invention not only generates the final high-resolution density map but also generates several intermediate density maps through multi-scale branches. These intermediate density maps are spatially aligned but differ in semantic granularity. This invention treats density segments from different scales at the same spatial location as semantically consistent positive sample pairs and density segments from different locations or background regions as negative sample pairs. By constructing a contrastive learning loss, positive samples become closer in the feature space, and negative samples are more separated. Furthermore, by setting different contrast weights for densely planted regions, boundary transition regions, and sparse regions, this invention makes the model pay more attention to the spatial continuity and structural rationality of the density map during training, effectively reducing density breaks, isolated high-response points, and background false activations. This module works in conjunction with the density regression loss to optimize both the numerical accuracy and structural representation of the final density map.
[0102] like Figure 2 As shown, the "training-validation-testing" process of the end-to-end plant counting network proposed in this invention includes stages such as data collection, annotation generation, dataset partitioning, model training, validation tuning, and final testing. In specific implementation, image samples containing different crop types, growth stages, and shooting heights can be collected from real farmland scenes. Each visible plant is labeled as a point, and a corresponding ground truth density map is generated based on the point labels. Subsequently, the training set, validation set, and test set can be divided in an 8:1:1 ratio for network training and performance evaluation. During the training phase, the parameters of the backbone network, cross-attention module, and contrast constraint module are jointly optimized; during the validation phase, indicators such as counting error and density map structure quality are monitored to select the optimal model; during the testing phase, the model's generalization ability is evaluated on images not used in training.
[0103] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. An automated plant counting method based on deep learning, characterized in that, Includes the following steps: S1. Construct a multi-scale backbone feature extraction module. Use the multi-scale backbone feature extraction module to preprocess the farmland input image and then extract and fuse features to obtain the fused multi-scale backbone features. S2. Construct a dual-branch cross-attention fusion module to perform bidirectional cross-attention modulation and fusion on the multi-scale backbone features obtained in S1, and output unified fused features. S3. Construct a multi-scale density map contrast learning constraint module. Based on the unified fusion features output in S2, generate a multi-scale intermediate density map. Introduce contrast loss and density regression loss to jointly optimize the model and process the multi-scale intermediate density map to obtain the optimized fusion features. S4. Based on the optimized fusion features obtained in S3, a final density map with the same resolution as the input image is generated through the density regression module. Pixel integration is then performed on the final density map to obtain the final plant count estimate, thus achieving end-to-end automated plant counting.
2. The method as described in claim 1, characterized in that, S1 specifically includes the following: S1.1 Perform size normalization, brightness standardization and basic noise reduction on the farmland input image, and then input it into the basic feature construction module composed of a convolutional encoder to extract the low-level structure including edge texture and obtain the basic feature map. S1.
2. Set up multiple CNN convolutional branches of different scales on the basic feature map, including small-scale branches for capturing seedling features, medium-scale branches for characterizing the structure during the vigorous growth period, and large-scale branches for expressing the overall layout of the canopy during the mature period, so as to obtain multi-scale convolutional local features. S1.3 Input the obtained multi-scale convolutional local features into the Mamba global modeling branch, and obtain the Mamba global structural features through convolution enhancement, serialization, state space modeling and spatial reconstruction; S1.4 Align the multi-scale convolutional local features with the Mamba global structural features in the spatial dimension through upsampling, interpolation, or transposed convolution, and maintain the consistency of details by using residual connections to output the fused multi-scale backbone features.
3. The method according to claim 2, characterized in that, The small-scale branch captures fine leaf structures by maintaining high spatial resolution; the medium-scale branch expresses the complete plant structure through moderate downsampling; and the large-scale branch expresses the overall canopy layout through deeper convolution stacking. The small-scale, medium-scale, and large-scale branches all employ residual connections to preserve low-level details and alleviate the training difficulty of deep networks.
4. The method according to claim 3, characterized in that, The Mamba global modeling branch performs the following operations: Improve feature discriminativeness using convolutional enhancement modules; The two-dimensional features are flattened into a one-dimensional sequence of length L=H×W and sequence normalization is performed. The long-range dependencies of plants in sparse, dense, and inter-row regions are captured through a selective state-space mechanism. The sequence is restored to a two-dimensional feature map and structural consistency is ensured through convolutional smoothing.
5. The method according to claim 4, characterized in that, S2 specifically includes the following: S2.1 Perform channel compression, spatial size alignment and numerical normalization on the input multi-scale convolutional local features, and perform channel compression and residual enhancement on the Mamba global structural features; S2.
2. Using the processed multi-scale convolutional local features as the query vector and the processed Mamba global structural features as the key vector and value vector, the first attention weight map is obtained through the "local → global" cross attention calculation, and the Mamba global structural features are then subjected to position-by-position and channel-by-channel weighted modulation accordingly. S2.
3. Using the processed Mamba global structural features as the query vector and the processed multi-scale convolutional local features as the key vector and value vector, the second attention weight map is obtained through the cross-attention calculation of "global → local", and the multi-scale convolutional local features are weighted and modulated accordingly. S2.4 The local and global features after weighted modulation in S2.2 and S2.3 are weighted and fused to obtain two sets of feature representations that are aligned in spatial structure and coordinated in semantic information. The two sets of feature representations are then concatenated in the channel dimension. The information is re-integrated by compressing the number of channels through convolutional mapping and nonlinear activation to output a unified fused feature.
6. The method according to claim 5, characterized in that, The first attention weight map is used to enhance the response of global features in dense plant areas, overlapping leaf areas, and local high-confidence areas; the second attention weight map is used to suppress the response of local features in noisy areas and background areas, and to enhance the expression of local details in areas consistent with the overall plant row and column structure and density trend.
7. The method according to claim 6, characterized in that, S3 specifically includes the following: S3.1 Generate intermediate density maps of multiple spatial scales based on unified fusion features; S3.2 Map the intermediate density maps to the same spatial size; S3.3 Construct positive sample pairs by taking the feature vectors corresponding to the same spatial location on intermediate density maps at different scales and constraining them to maintain high similarity in the feature space; wherein, the positive sample pairs are derived from density segments at different scales but the same spatial location, in order to maintain cross-scale density consistency; S3.4 Construct negative sample pairs from feature vectors corresponding to different spatial locations and constrain them to maintain their differences in the feature space; wherein, the negative sample pairs are derived from density segments of plant regions and background regions, and are used to enhance the ability to distinguish background noise; S3.
5. The model is optimized by combining the contrast loss function and the density regression loss function. Different contrast loss weights are applied to the dense plant area, the edge transition area and the sparse area to make the generated density map have scale stability and structural consistency.
8. An automated plant counting system based on deep learning that applies the method described in any one of claims 1-7, characterized in that, include: A multi-scale backbone feature extraction module is used to perform size normalization, brightness standardization, and basic denoising on the farmland input image. This image is then input into a basic feature construction module composed of a convolutional encoder to extract the underlying structure, including edge textures, to obtain a basic feature map. Multiple CNN convolutional branches of different scales are set on the basic feature map, including a small-scale branch for capturing seedling features, a medium-scale branch for characterizing the vigorous growth stage structure, and a large-scale branch for expressing the overall canopy layout at maturity, thus obtaining multi-scale convolutional local features. These multi-scale convolutional local features are then input into the Mamba global modeling branch, where Mamba global structural features are obtained through convolutional enhancement, serialization, state space modeling, and spatial reconstruction. Finally, the multi-scale convolutional local features and Mamba global structural features are aligned spatially through upsampling, interpolation, or transposed convolution, and residual connections are used to maintain detail consistency, outputting the fused multi-scale backbone features. The dual-branch cross-attention fusion module performs channel compression, spatial size alignment, and numerical normalization on the input multi-scale convolutional local features, and channel compression and residual enhancement on the Mamba global structural features. Using the processed multi-scale convolutional local features as the query vector and the processed Mamba global structural features as the key and value vectors, a first attention weight map is calculated through "local → global" cross-attention, and the Mamba global structural features are then weighted and modulated positionally and channel-wise. Using the processed Mamba global structural features as the query vector and the processed multi-scale convolutional local features as the key and value vectors, a second attention weight map is calculated through "global → local" cross-attention, and the multi-scale convolutional local features are then weighted and modulated. The weighted and modulated local and global features are then weighted and fused to obtain two sets of spatially aligned and semantically coherent feature representations. These two sets of feature representations are then concatenated along the channel dimension, and the information is re-integrated through convolutional mapping and nonlinear activation to compress the number of channels, outputting a unified fused feature. The multi-scale density map contrastive learning constraint module generates intermediate density maps at multiple spatial scales based on unified fusion features and maps them uniformly to the same spatial size. It constructs positive sample pairs by constructing feature vectors corresponding to the same spatial location on intermediate density maps at different scales, and constrains them to maintain high similarity in the feature space. These positive sample pairs originate from density segments at different scales but the same spatial location, used to maintain cross-scale density consistency. It also constructs negative sample pairs by constructing feature vectors corresponding to different spatial locations, and constrains them to maintain differences in the feature space. These negative sample pairs originate from density segments of plant regions and background regions, used to enhance the ability to distinguish background noise. The model is optimized by jointly using a contrastive loss function and a density regression loss function, applying different contrastive loss weights to dense plant regions, edge transition regions, and sparse regions, so that the generated density maps possess scale stability and structural consistency. The density map generation and plant counting inference module is used to input the optimized fusion features into the multi-scale density map contrast learning constraint module, and generate a final density map with the same resolution as the input image through convolutional mapping; pixel integration is performed on the density map to obtain the final plant number estimate, thereby achieving the goal of end-to-end automated plant counting.
9. A computer device, characterized in that, The computer device includes a processor and a memory, wherein the memory stores at least one instruction, at least one program, code set, or instruction set, and the instruction, program, code set, or instruction set is loaded and executed by the processor to implement an automated plant counting method based on deep learning as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores at least one instruction, at least one program, code set, or instruction set, which is loaded and executed by a processor to implement an automated plant counting method based on deep learning as described in any one of claims 1-7.