Lightweight insect recognition method based on knowledge distillation and customs real-time general device
Patent Information
- Application Number
- CN202511139546.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-14
- Publication Date
- 2026-10-20
- Estimated Expiration
- 2045-08-14
AI Technical Summary
不同终端采集的图像分辨率、色彩格式差异显著,缺乏统一预处理机制,导致模型跨设备泛化能力弱
1、针对不同终端采集的图像分辨率、色彩格式差异显著,缺乏统一预处理机制,导致模型跨设备泛化能力弱的问题,本发明通过图像输入处理模块对待识别昆虫图像进行本地预处理,获得预处理后的图像,解决了泛化能力弱的问题。
Smart Images

Figure CN120997882B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of insect image recognition technology, and in particular to a lightweight insect recognition method based on knowledge distillation and a real-time general-purpose equipment for customs. Background Technology
[0002] With the rapid development of global trade, the demand for pest quarantine of inbound goods and personal belongings at customs ports has increased dramatically.
[0003] Traditional insect identification mainly relies on manual visual inspection or laboratory microscopic observation, which suffers from low efficiency, high subjectivity, and a scarcity of expert resources. In recent years, deep learning-based image recognition technology has been introduced into this field, but it still faces the following key bottlenecks: Existing high-precision identification models (such as ResNet and VGG) have large numbers of parameters and are computationally complex, making them difficult to run in real time on edge devices (such as customs tablets and portable microscope terminals). Their reliance on cloud-based inference leads to high network latency, failing to meet the real-time judgment needs in port environments with no or weak network coverage. Insect identification relies on subtle anatomical structures such as wing veins and antennae; traditional convolutional networks have limited ability to perceive local features, resulting in a high misclassification rate for morphologically similar species (such as insects that mimic other insects). Furthermore, the significant differences in image resolution and color format between different terminals, coupled with the lack of a unified preprocessing mechanism, result in weak cross-device generalization ability of the models.
[0004] Therefore, there is an urgent need to provide a lightweight insect identification method based on knowledge distillation and a real-time universal device for customs, in order to solve at least one of the above problems. Summary of the Invention
[0005] Therefore, it is necessary to provide a lightweight insect identification method based on knowledge distillation to address at least one existing problem.
[0006] The first aspect of this application provides a lightweight insect identification method based on knowledge distillation, characterized by comprising: The image input processing module performs local preprocessing on the image of the insect to be identified, and obtains the preprocessed image. A lightweight student model is used to perform local inference on the preprocessed image input, and the top-k candidate insect species and their corresponding confidence scores are output. The lightweight student model is obtained from the teacher model through knowledge distillation. The local inference includes: extracting features and expanding channels of the preprocessed image through a front-end convolution module to obtain a convolutional feature map; dividing the convolutional feature map into multiple fixed-size image blocks through a Patch Embedding module, mapping each image block to a corresponding token to obtain a token sequence for a local self-attention mechanism; applying self-attention to the local regions of the token sequence for the local self-attention mechanism through a local self-attention mechanism module to model the global relationship between local regions in the image, obtaining a token sequence for classification; converting and fusing the token sequence for classification into a global image representation through a fully connected classification output module and outputting the Softmax probability vector corresponding to the candidate insect species; and sorting the Softmax probability vector in descending order through a candidate output generation module, selecting the top k categories as candidate results, and outputting a top-k candidate list, which includes the confidence scores corresponding to the top-k candidate insect species.
[0007] In some implementations, the step of transferring the lightweight student model from the teacher model through knowledge distillation includes: The lightweight student model is optimized using a joint loss function composed of KL divergence and cross-entropy. The expression for the joint loss function is as follows: ; in, The output of the lightweight student model is based on the corresponding input image. Let y be the softmax probability vector output by the teacher model based on the input image, KL be the Kullback-Leibler divergence, CE be the cross-entropy loss function, y be the true label of the image, and α be the distillation balance coefficient, where 0.7 ≤ α ≤ 0.9.
[0008] In some implementations, the output of the teacher model is probabilistically softened based on a temperature parameter before calculating the KL divergence. This probabilistic softening is expressed in normalized form as follows: ; in, This is the raw prediction output of the teacher model, where j is the index of the class, representing the j-th class, and its value range is equal to the number of classes. Let be the original probability output of the teacher model in the j-th category, T be the temperature parameter, and exp be the exponential function with the natural constant e as the base, used to map the real number of the input to a positive number, and the value increases exponentially with the increase of the input.
[0009] In some implementations, the Softmax probability vector is represented as: ; Where N is the total number of species that support identification, Pi represents the confidence level of the i-th species that supports identification, and i represents the i-th species that supports identification. The top-k candidate list is represented as follows: ; in, For the sorted number The category index with the highest probability, This represents the corresponding confidence level.
[0010] In some implementations, the step of extracting features and expanding channels of the preprocessed image using a front-end convolution module to obtain a convolutional feature map includes: Using the preprocessed image as the initial input, feature extraction and channel expansion are performed sequentially through three layers of depthwise separable convolutional blocks. The output of the previous depthwise separable convolutional block is used as the input of the next depthwise separable convolutional block. The depthwise separable convolutional block includes a convolution that performs 3×3 convolution on each input channel without channel fusion, and a convolution that uses 1×1 convolution to linearly combine the channels to achieve feature interaction and channel expansion. Each depthwise separable convolutional block is followed by BatchNorm and GELU activation, resulting in a tensor dimension of the final output convolutional feature map: X0∈R 1×128×H′×W′ .
[0011] In some implementations, the step of dividing the convolutional feature map into multiple fixed-size image blocks using the Patch Embedding module, and mapping each image block to a corresponding token to obtain a token sequence for the local self-attention mechanism, includes: Convolutional feature maps Divided into by flattening and rearranging operations There are image patches, where P is the set of image patches; Each image patch is mapped to a token using a linear mapping layer, and the token is a d-dimensional embedding vector. The token sequence used for the local self-attention mechanism is obtained, and the token sequence used for the local self-attention mechanism is represented as follows: ; Among them, t i This represents the i-th token.
[0012] In some implementations, the step of applying self-attention to local regions of the token sequence used for the local self-attention mechanism through a local self-attention mechanism module to model the global relationships between local regions in the image and obtain a token sequence for classification includes: By applying self-attention to the local token regions of the token sequence used for the local self-attention mechanism through a two-layer lightweight Transformer encoder, a token sequence for classification is obtained. The step of applying self-attention to the local token region of the token sequence used for the local self-attention mechanism includes: Each layer of the Transformer encoder performs multi-head self-attention calculation, feedforward network processing, residual connection and layer normalization processing on the input, and finally the local self-attention mechanism module outputs a token sequence for classification. The token sequence used for classification is represented as follows: ; The multi-head self-attention calculation is expressed as follows: ; Q, K, and V are obtained by linear mapping of the input tokens, using h=2 Attention Heads, with each Head having a dimension of d / h; The feedforward network is represented as follows: ; .
[0013] In some implementations, the step of converting and fusing the token sequence used for classification into a global image representation and outputting the softmax probability vector corresponding to the candidate insect species through a fully connected classification output module includes: The global image representation vector is obtained by pooling all tokens in the token sequence used for classification using mean pooling through Token Pooling. The global representation vector of the image is processed by a two-layer fully connected classifier to determine the category and corresponding confidence score of the candidate insect species; Based on the category of the candidate insect species and the corresponding confidence score, output a softmax probability vector; The global image representation vector is represented as follows: ; The fully connected classifier is represented as follows: ; The softmax probability vector is represented as follows: ; N represents the number of categories; In the knowledge distillation stage, the Softmax probability vector... Used for distillation alignment with the teacher model output.
[0014] In some embodiments, before the image input processing module performs local preprocessing on the image of the insect to be identified to obtain the preprocessed image, the method further includes acquiring the image of the insect to be identified; and / or, the local preprocessing includes scaling or cropping the image of the insect to be identified at any resolution to a uniform input size; normalizing the image pixel values to the range of [0,1] or [-1,1]; changing the image format from H×W×N to N×H×W; constructing a standard tensor using NumPy or PyTorch backends; and / or, the lightweight student model obtains the corresponding Softmax probability vector by transferring knowledge distillation from the teacher model to both the teacher model and the lightweight student model to be trained, which involve feeding the same input image into a frozen state. and The process involves calculating the weighted total loss of KL divergence and cross-entropy, and backpropagating this weighted total loss as the joint loss function to all parameters of the lightweight student model. After training stabilizes and converges, the weights of the lightweight student model are exported in deployment format to adapt to the local inference environment of mobile devices. During training, the teacher model does not participate in parameter updates but only serves as a source of guidance signals. The training process of the teacher model includes: selecting the MobileNet model as the model structure of the teacher model; using multiple insect images obtained from the Customs Laboratory as a dataset and employing image-level standard classification labels for supervised learning; using cross-entropy as the loss function during training, combining the Adam optimizer, and introducing a warm-up and learning rate decay strategy for optimization; and finally, improving the teacher model's accuracy on the validation set to at least 90% Top-1 accuracy to complete the training.
[0015] The second aspect of this application provides customs real-time universal equipment, including: The image input processing module is used to perform local preprocessing on the image of the insect to be identified, and obtain the preprocessed image. A lightweight student model, obtained from a teacher model through knowledge distillation, includes: The front-end convolution module is used to extract features and expand channels in the preprocessed image to obtain a convolutional feature map; The Patch Embedding module is used to divide the convolutional feature map into multiple fixed-size image blocks, and map each image block to a corresponding token to obtain a token sequence for the local self-attention mechanism. The local self-attention mechanism module is used to apply self-attention to local regions of the token sequence used for the local self-attention mechanism in order to model the global relationship between local regions in the image and obtain the token sequence for classification. The fully connected classification output module is used to transform and fuse the token sequence used for classification into a global image representation and output the Softmax probability vector corresponding to the candidate insect species; The candidate output generation module is used to sort the Softmax probability vector in descending order, select the top k categories as candidate results, and output a top-k candidate list, which includes the confidence scores of the top-k candidate insect species.
[0016] The beneficial effects of this invention are: 1. To address the problem of weak generalization ability of models due to significant differences in image resolution and color format among images collected from different terminals and the lack of a unified preprocessing mechanism, this invention solves the problem of weak generalization ability by performing local preprocessing on the images of insects to be identified through an image input processing module to obtain preprocessed images.
[0017] 2. This invention uses a lightweight student model to perform local inference on the preprocessed image input. The lightweight student model is obtained from the teacher model through knowledge distillation. The local inference includes: extracting features and expanding channels of the preprocessed image through a front-end convolution module to obtain a convolutional feature map; dividing the convolutional feature map into multiple fixed-size image blocks through a Patch Embedding module, mapping each image block to a corresponding token to obtain a token sequence for a local self-attention mechanism; applying self-attention to the local regions of the token sequence for the local self-attention mechanism through a local self-attention mechanism module to model the global relationship between local regions in the image, obtaining a token sequence for classification; converting and fusing the token sequence for classification into a global image representation through a fully connected classification output module and outputting the Softmax probability vector corresponding to the candidate insect species; and sorting the Softmax probability vector in descending order through a candidate output generation module, selecting the top k categories as candidate results, and outputting a top-k candidate list, which includes the confidence scores corresponding to the top-k candidate insect species. It achieves insect recognition through a lightweight student model, eliminating the need for high-precision recognition models with large parameters, complex calculations, and difficulty in running in real time on edge devices.
[0018] 3. The lightweight student model of the present invention has a teacher model obtained through knowledge distillation. The local self-attention mechanism module of the lightweight student model applies self-attention to the local regions of the token sequence used for the local self-attention mechanism to model the global relationship of each local region in the image. It has high perception ability of local features and low misjudgment rate of morphologically similar species (such as mimicking insects).
[0019] 4. It achieves real-time insect species identification through local inference of a lightweight student model, which can meet the instant judgment needs in port environments with no or weak network coverage. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other embodiments can be obtained based on these drawings without creative effort.
[0021] Figure 1 A flowchart illustrating the steps of a lightweight insect identification method based on knowledge distillation provided for embodiments of this application.
[0022] Figure 2 A flowchart illustrating the steps of performing local inference on a preprocessed image input, provided for embodiments of this application; Figure 3 A comparative flowchart illustrating the lightweight model distillation method, insect image recognition method, and operation method on a general terminal device provided for the embodiments of this application; Figure 4 A schematic diagram of the network structure of a lightweight academic model provided for the implementation of this application; Figure 5 A schematic diagram of a customs real-time general-purpose device provided for an embodiment of this application. Detailed Implementation
[0023] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings. Many specific details are set forth in the following description to provide a thorough understanding of the present invention. However, the present invention can be practiced in many other ways different from those described herein, and those skilled in the art can make similar modifications without departing from the spirit of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0024] In the description of this invention, it should be understood that the terms "center," "longitudinal," "lateral," "length," "width," "thickness," "upper," "lower," "front," "rear," "left," "right," "vertical," "horizontal," "top," "bottom," "inner," "outer," "clockwise," "counterclockwise," "axial," "radial," and "circumferential" indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are used only for the convenience of describing this invention and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this invention.
[0025] Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one of that feature. In the description of this invention, "a plurality of" means at least two, such as two, three, etc., unless otherwise explicitly specified.
[0026] In this invention, unless otherwise explicitly specified and limited, the terms "installation," "connection," "linking," and "fixing," etc., should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral part; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; they can refer to the internal communication of two components or the interaction between two components, unless otherwise explicitly limited. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.
[0027] In this invention, unless otherwise explicitly specified and limited, a feature "above" or "below" the second feature may mean that the feature is in direct contact with the second feature or indirect contact with the second feature through an intermediate medium. Furthermore, "above," "over," and "on top" of the second feature may mean that the feature is directly above or diagonally above the second feature, or simply indicates that the feature is at a higher horizontal level than the second feature. Similarly, "below," "below," and "beneath" of the second feature may mean that the feature is directly below or diagonally below the second feature, or simply indicates that the feature is at a lower horizontal level than the second feature.
[0028] It should be noted that when an element is referred to as being "fixed to" or "set on" another element, it can be directly on the other element or there may be an intervening element. When an element is considered to be "connected to" another element, it can be directly connected to the other element or there may be an intervening element. The terms "vertical," "horizontal," "upper," "lower," "left," "right," and similar expressions used herein are for illustrative purposes only and do not represent the only possible implementation.
[0029] Please see Figures 1-4 , Figure 1 A flowchart illustrating the steps of a lightweight insect identification method based on knowledge distillation provided for embodiments of this application. Figure 2 A flowchart illustrating the steps of performing local inference on a preprocessed image input, provided for embodiments of this application; Figure 3 A comparative flowchart illustrating the lightweight model distillation method, insect image recognition method, and operation method on a general terminal device provided for the embodiments of this application; Figure 4 This is a schematic diagram of the network structure of the lightweight academic model provided for the implementation of this application. The lightweight model is the lightweight student model; image input processing corresponds to the image input processing module; lightweight recognition and inference corresponds to the front-end convolution module, patch embedding module, local self-attention mechanism module, and fully connected classification output module; candidate output generation corresponds to the candidate output generation module.
[0030] See Figures 1-5 The embodiments of this application provide a lightweight insect identification method based on knowledge distillation, which includes: Step 200: The image of the insect to be identified is preprocessed locally through the image input processing module to obtain the preprocessed image; Step 400: Perform local inference on the preprocessed image input using a lightweight student model, and output the top-k candidate insect species and their corresponding confidence scores; Among them, see Figures 1-5 The lightweight student model is obtained from the teacher model through knowledge distillation, and the local reasoning includes: Step 410: Perform feature extraction and channel expansion on the preprocessed image using the front-end convolution module to obtain a convolutional feature map; Step 420: Divide the convolutional feature map into multiple fixed-size image blocks using the Patch Embedding module, and map each image block to a corresponding Token to obtain a Token sequence for the local self-attention mechanism; interpretably, an image block is a Patch. Step 430: Apply self-attention to the local regions of the token sequence used for the local self-attention mechanism through the local self-attention mechanism module to model the global relationship between the local regions in the image and obtain the token sequence for classification. Step 440: The token sequence used for classification is converted and fused into a global image representation through the fully connected classification output module, and the Softmax probability vector corresponding to the candidate insect species is output. Step 450: Sort the Softmax probability vector in descending order through the candidate output generation module, select the top k categories as candidate results, and output the top-k candidate list, which includes the confidence scores of the top-k candidate insect species.
[0031] The advantages of the implementation method of this application are as follows: By performing local preprocessing on the image of the insect to be identified through the image input processing module, a preprocessed image is obtained. This solves the problem of significant differences in image resolution and color format between different terminals, the lack of a unified preprocessing mechanism, and the resulting weak cross-device generalization ability of the model. The lightweight student model has a teacher model obtained through knowledge distillation. The local self-attention mechanism module of the lightweight student model applies self-attention to the local regions of the token sequence used for the local self-attention mechanism to model the global relationship between various local regions in the image. It has high perception ability of local features and low misclassification rate for morphologically similar species (such as mimicry insects). Insect identification is achieved through the lightweight student model, eliminating the need for high-precision identification models with large parameters, complex calculations, and difficulty in real-time operation on edge devices. Real-time insect species identification is achieved through local inference of the lightweight student model, which can meet the instant judgment requirements in port environments with no or weak network coverage.
[0032] Specifically, the implementation methods of this application are particularly applicable to general-purpose terminal equipment deployed at port customs, such as quarantine laboratory workstations connected to microscope camera modules, on-site administrative law enforcement Pads equipped with camera capabilities, and portable customs equipment. In these devices, traditional deep learning models are difficult to deploy and run directly due to practical constraints such as unstable networks, limited computing resources, and strict response time requirements. This invention constructs a high-precision teacher model and uses knowledge distillation to obtain a lightweight student model, embedding it as a local model into the terminal device, thus achieving efficient insect identification functionality even offline.
[0033] Furthermore, the implementation method of this application is specifically designed for business scenarios such as customs animal and plant quarantine and inspection of inbound goods, possessing strong practicality and industry adaptability. Compared to traditional identification methods that rely on manual image retrieval or expert judgment, the implementation method of this application, by deploying it on general-purpose equipment such as administrative tablets or microscope image acquisition terminals used by customs frontline staff, achieves an intelligent identification process that requires no network connection, provides instant response, and features enhanced structure, bringing the following significant benefits in actual business operations: Firstly, in terms of work efficiency, it significantly reduces the time spent on manual retrieval and waiting for expert feedback. It supports customs officers to instantly identify and judge samples at the physical inspection site, making it particularly suitable for peak inspection periods and scenarios requiring rapid pre-screening of large batches of samples. Even in environments with limited or no network conditions at the port, it can still operate independently, ensuring uninterrupted frontline inspection work.
[0034] Secondly, regarding recognition accuracy, it not only retains the discriminative power of deep learning models but also introduces structural perception capabilities through knowledge distillation and local attention mechanisms, significantly improving the accuracy of identifying insects with similar morphology, belonging to the same genus but different species, or those using mimicry and camouflage. It can automatically focus on key anatomical features such as wing veins, antennae, and intersegments, enhancing the discriminative and interpretable nature of the recognition results while maintaining computational efficiency. This provides technical support for the precise interception of high-risk invasive species or pests of important economic crops.
[0035] Furthermore, its lightweight structure and stable operating mechanism support long-term residence in mobile administrative terminals or professional microscope platforms. Combined with regularly updated knowledge distillation model versions, it can achieve low-cost, high-coverage intelligent inspection capability deployment, thereby reducing the customs' reliance on high-level human experts and improving the level of business intelligence at the grassroots level.
[0036] In summary, the implementation method of this application not only solves the limitations of model deployment and computing resources, but also provides a fast, reliable, and interpretable identification support tool in customs inspection scenarios, significantly enhancing the efficiency and accuracy of risk screening of inbound insect samples at ports of entry, and has obvious industry promotion value and social benefits.
[0037] In some implementations, see Figures 1-5 Before step 200, where the image input processing module performs local preprocessing on the image of the insect to be identified to obtain the preprocessed image, the method further includes: Step 100: Obtain an image of the insect to be identified.
[0038] Specifically, the images of the insects to be identified can come from the industrial camera in the microscope workstation, the built-in camera on the customs administration Pad, or externally uploaded image files.
[0039] See Figures 1-5 In some embodiments, the local preprocessing in step 200 includes: Step 210: Scale or crop the images of insects to be identified at any resolution to a uniform input size. Step 220: Normalize the image pixel values to the range of [0,1] or [-1,1]; Step 230: Convert the image format from H×W×N to N×H×W format; Step 240: Construct standard tensors using NumPy and PyTorch backends.
[0040] As described above, steps 210-240 support unified preprocessing and standardization of images captured by microscopes and Pads, automatically adapting to different input data formats and image sharpness to ensure consistency and robustness in subsequent identification. The insect images to be identified can come from industrial cameras in microscope workstations, built-in cameras on customs administrative Pads, or externally uploaded image files. During the local preprocessing stage, the insect images (i.e., the original images) undergo size resampling (supporting two resolutions: 224×224 and 256×256), color normalization, and channel adjustment, converting them into standard tensor format, commonly such as [1, 3, 256, 256]. This processing flow is entirely executed locally, without relying on external networks or servers.
[0041] NumPy is an open-source scientific computing library that provides efficient multidimensional array objects and a rich set of mathematical functions for numerical computation and data processing. It supports array creation, manipulation, linear algebra, random number generation, and other functionalities, making it a crucial foundational component of the Python scientific computing ecosystem. PyTorch is an open-source deep learning framework based on dynamic graph mechanisms, supporting tensor computation and automatic differentiation, facilitating the construction and training of neural network models. PyTorch offers a rich set of neural network modules, optimizers, and utility libraries, and is widely used in machine learning and artificial intelligence.
[0042] The following section introduces the specific methods and processes of knowledge distillation.
[0043] See Figures 1-5 In some implementations, the lightweight student model in step 400 is obtained from the teacher model through knowledge distillation, including: Step 000: Feed the same input image into the frozen teacher model and the lightweight student model to be trained respectively, to obtain the corresponding softmax probability vector. and ; Step 010: Calculate the weighted total loss of KL divergence and cross-entropy, and backpropagate only this weighted total loss as the joint loss function to all parameters of the lightweight student model; Step 020: After the training has stabilized and converged, export the lightweight student model weights in the deployment format to adapt to the local inference environment of mobile devices; during the training process, the teacher model does not participate in parameter updates, but only serves as a source of guidance signals.
[0044] Knowledge distillation training can be performed on fully labeled datasets and also supports few-shot scenarios. As described in steps 000-020 above, to enable the lightweight student model to achieve recognition performance close to or even partially surpassing the teacher model on terminal devices, this application employs a knowledge distillation method based on probability output alignment. The core idea is to guide the lightweight student model to mimic the teacher model's judgment behavior on class probability distributions, enabling it to capture rich inter-class boundary information even with small-scale structures. This method can adapt to the local inference environment of mobile devices, achieving low-latency, high-accuracy real-time insect recognition capabilities.
[0045] In some implementations, see Figures 1-5 The step 400, in which the lightweight student model is transferred from the teacher model through knowledge distillation, includes: The lightweight student model is optimized by using a joint loss function composed of KL divergence and cross-entropy. Specifically, step 010 involves calculating the weighted total loss of KL divergence and cross-entropy, and then using this weighted total loss as the joint loss function to backpropagate to all parameters of the lightweight student model.
[0046] Specifically, the expression for the joint loss function is: ; in, The output of the lightweight student model is based on the corresponding input image. Let y be the Softmax probability vector output by the teacher model based on the input image, KL be the Kullback-Leibler divergence, CE be the cross-entropy loss function, y be the true label of the image, and α be the distillation balance coefficient, where 0.7 ≤ α ≤ 0.9.
[0047] As shown above, the knowledge distillation process is optimized using a joint loss function, which consists of two parts: first, the Kullback-Leibler (KL) divergence between the student model output and the teacher model output, which measures the similarity between the two probability distributions; and second, the cross-entropy (CE) between the student model output and the true label, which ensures that it does not deviate from the basic supervision signal.
[0048] In addition, see Figures 1-5 In some implementations, the KL divergence needs to be calculated before calculating the weighted total loss of KL divergence and cross-entropy in step 010. Further, before calculating the KL divergence, a probability softening process is performed on the output of the teacher model based on the temperature parameter, wherein the probability softening process is expressed as: ; in, This is the raw prediction output of the teacher model, where j is the index of the category, representing the j-th category, and its value range is equal to the number of categories. Let T be the original probability output of the teacher model for the j-th category, T be the temperature parameter, and exp be the exponential function with the natural constant e as the base, used to map the real number of the input to a positive number, and the value increases exponentially with the input. A higher temperature will make the softmax probability vector output of the teacher model smoother, revealing the fine-grained distinguishing relationships between non-target categories, thereby helping the student model to better learn the "hidden knowledge" of the teacher model.
[0049] For training high-precision teacher models, see [link / reference]. Figures 1-5 In some implementations, the training process of the teacher model includes: Step 001: Select the MobileNet model as the model structure for the teacher model; Step 002: Use multiple insect images obtained from the customs laboratory as a dataset and perform supervised learning using standard classification labels at the image level; for example, use 600 insect images as a dataset. Step 003: During training, cross-entropy is used as the loss function, combined with the Adam optimizer, and Warm-up and learning rate decay strategies are introduced for optimization. Step 004: Continue training until the teacher model achieves a Top-1 accuracy of at least 90% on the validation set.
[0050] The following describes the lightweight student model and its execution steps using specific implementation methods.
[0051] As the core recognition model ultimately deployed on general-purpose terminal devices (such as mobile devices, embedded AI chips, etc.), the lightweight student model needs to maintain good insect classification and recognition accuracy while minimizing model size and computational complexity. Therefore, the lightweight student model in this application is a lightweight deep neural network structure that can achieve smooth forward inference in environments with limited computing power, such as mobile CPUs or NNAPI, and supports user-defined input image sizes; the size used in this embodiment is 256×256.
[0052] In some implementations, see Figures 1-5 Step 410, obtaining a convolutional feature map by performing feature extraction and channel expansion on the preprocessed image through the front-end convolution module, includes: Using the preprocessed image as the initial input, feature extraction and channel expansion are performed sequentially through three layers of depthwise separable convolutional blocks. The output of the previous depthwise separable convolutional block is used as the input of the next depthwise separable convolutional block. The depthwise separable convolutional block includes a depthwise convolution that performs 3×3 convolution on each input channel without channel fusion, and a pointwise convolution that uses 1×1 convolution to linearly combine the channels to achieve feature interaction and channel expansion. Each depthwise separable convolutional block is followed by BatchNorm and GELU activation, resulting in a tensor dimension of the final output convolutional feature map: X0∈R 1×128×H′×W′ For example, if H′=64, W′=64.
[0053] Specifically, if the initial input is an image of size [1, 3, H, W], after passing through the three convolutional layers of the front-end convolutional module, the number of image channels changes from 3→32→64→128, and the feature map size is gradually downsampled.
[0054] For X0∈R 1×128×H′×W′ Explanation: X0 represents the convolutional feature map obtained after processing through three depthwise separable convolutional blocks and subsequent batch normalization (BatchNorm) and GELU activation function. 1 represents the batch size, meaning the tensor contains features from one sample. 128 represents the number of feature channels, meaning the feature map has 128 channels after channel expansion by the depthwise separable convolutional blocks. H' represents the height of the feature map, i.e., the spatial height dimension obtained after convolution and possible downsampling of the input image. W' represents the width of the feature map, i.e., the spatial width dimension obtained after convolution and possible downsampling of the input image.
[0055] BatchNorm is a method used to accelerate training and stabilize deep neural networks. It standardizes the activation values of neurons within each mini-batch, making their mean close to 0 and variance close to 1, and then adjusts these values using learnable scaling and translation parameters. GELU (Gaussian Error Linear Unit) is a non-linear activation function that combines the probability distribution characteristics of the input values to smoothly activate neurons.
[0056] See Figures 1-5 In some implementations, step 420, which involves dividing the convolutional feature map into multiple fixed-size image blocks using the Patch Embedding module and mapping each image block to a corresponding token to obtain a token sequence for the local self-attention mechanism, includes: Convolutional feature maps Divided into by flattening and rearranging operations There are image patches, where P is the set of image patches; Each image patch is mapped to a token using a linear mapping layer, and the token is a d-dimensional embedding vector. The token sequence used for the local self-attention mechanism is obtained, and the token sequence used for the local self-attention mechanism is represented as follows: ; where t i This represents the i-th token.
[0057] To introduce the Transformer mechanism, the Patch Embedding module divides the convolutional feature map into multiple fixed-size image patches, such as 16×16. Step 420 effectively transforms the two-dimensional spatial information into a sequence input format processed by the Transformer.
[0058] In some implementations, see Figures 1-5 Step 430, applying self-attention to the local regions of the token sequence used for the local self-attention mechanism through the local self-attention mechanism module to model the global relationship between local regions in the image, and obtaining the token sequence for classification, includes: By applying self-attention to the local token regions of the token sequence used for the local self-attention mechanism through a two-layer lightweight Transformer encoder, a token sequence for classification is obtained. The step of applying self-attention to the local token region of the token sequence used for the local self-attention mechanism includes: Each layer of the Transformer encoder performs multi-head self-attention calculation, feedforward network processing, residual connection and layer normalization processing on the input, and finally the local self-attention mechanism module outputs a token sequence for classification. The token sequence used for classification is represented as follows: The multi-head self-attention calculation is expressed as follows: ; Q, K, and V are obtained by linear mapping of the input tokens, using h=2 Attention Heads, each with a dimension of d / h; the feedforward network is represented as: ; .
[0059] As mentioned above, step 430 introduces a local self-attention mechanism. In order to retain the powerful local modeling capabilities of the Transformer while avoiding computational bottlenecks, the local self-attention mechanism module in this application uses a two-layer lightweight Transformer encoder, applying self-attention only to local token regions. Finally, the local self-attention mechanism module outputs a token sequence for classification, compressing the computational scope of context modeling while preserving the sequence structure.
[0060] See Figures 1-5 In some implementations, step 440, which involves converting and fusing the token sequence used for classification into a global image representation and outputting the softmax probability vector corresponding to the candidate insect species through a fully connected classification output module, includes: The global image representation vector is obtained by pooling all tokens in the token sequence used for classification using mean pooling through Token Pooling. The global representation vector of the image is processed by a two-layer fully connected classifier to determine the category and corresponding confidence score of the candidate insect species. Based on the category of the candidate insect species and the corresponding confidence score, output a softmax probability vector; The global image representation vector is represented as follows: ; The fully connected classifier is represented as follows: ; The softmax probability vector is represented as follows: ; N is the number of species categories; Where N represents the number of minimum embedding units (Tokens) used for classification, i.e., the length of the token sequence. ti represents the embedding vector of the i-th token, with dimension d. v represents the global representation vector of the image. d represents the dimension of the token, which is also the dimension of the global representation vector. d' represents the hidden layer dimension, which is the dimension of the output of the first fully connected layer. R represents the set of real numbers. W1 represents the weight matrix of the first layer in the fully connected classifier, mapping the global representation vector from dimension d to the hidden layer dimension d'. b1 represents the bias vector of the first layer of the fully connected classifier. b2 is the bias vector of the second layer of the fully connected classifier. W2 is the weight matrix of the second layer of the fully connected classifier, mapping the hidden layer output to the class space, with N classes. GELU is the Gaussian error linear unit activation function, performing a non-linear transformation on the linear transformation result. z is the final linear output vector of the fully connected classifier, representing the original score corresponding to each class. P sIt is a probability vector obtained by applying the softmax function to z, where each element represents the confidence probability of the corresponding class.
[0061] In the knowledge distillation stage, the softmax probability vector... Used for distillation alignment with the teacher model output.
[0062] By combining convolution and Transformer, it balances the ability to capture local details with the ability to understand contextual semantics. Under the guidance of knowledge distillation, it can achieve recognition capabilities similar to those of the teacher model and has excellent mobile deployment characteristics. It is easy to deploy to formats such as TFLite, ONNX, and CoreML and supports on-device CPU, GPU, NNAPI, and NPU acceleration.
[0063] Explained, Token Pooling refers to the aggregation operation on an input sequence of tokens to obtain a global representation vector of the sequence. It is commonly used in tasks such as image classification to fuse the features of multiple local tokens into a single global feature, thereby simplifying the input to subsequent classifiers. Mean Pooling is a commonly used aggregation method that calculates the arithmetic mean of the corresponding elements of a set of vectors to obtain a new vector representing the overall features of the input vector.
[0064] In some implementations, see Figures 1-5 In step 450, the softmax probability vector is sorted in descending order by the candidate output generation module, and the top k categories are selected as candidate results. A top-k candidate list is output, which includes the softmax probability vector in the confidence scores of the top-k candidate insect species. The representation of the top-k candidate list is as follows: The softmax probability vector is represented as follows: ; Where N is the total number of species that support identification, and p i This represents the confidence level of the i-th species that supports identification; i represents the i-th species that supports identification. Specifically, p i The confidence level of the model for the i-th supported species is represented, with a value range of [0,1], and the sum of the confidence levels of all species is 1. i represents the i-th species in the set of supported species, where i is the index number, with a value range of {1,2,…,N}, and N is the total number of species supported by the system.
[0065] The top-k candidate list is represented as follows: ; in, For the sorted number The category index with the highest probability, This represents the corresponding confidence level.
[0066] Specifically, C represents the top-k candidate list, which contains the top k categories sorted by confidence level from highest to lowest and their corresponding confidence values. i represents the index of the category with the highest probability in the sorted results, where i∈{1,2,…,k}.
[0067] Based on the above, the following describes how this application operates on a general-purpose terminal device.
[0068] The lightweight insect identification method based on knowledge distillation proposed in this application is specifically designed for import and export quarantine inspection scenarios in the port customs field. The system is particularly suitable for two types of terminal platforms: one is the video acquisition and analysis terminal on the microscope side (such as microscope interface computing modules and embedded edge recognition boxes); the other is the administrative tablets (such as domestic Android devices) issued to front-line customs inspectors. The implementation method of this application can operate independently offline, and features fast response speed, high accuracy, and low resource consumption, meeting the multiple requirements of actual business processes for mobility, stability, low latency, and offline operation.
[0069] The overall operation follows a modular pipeline logic, divided into an initialization phase, a data input phase, an inference and calculation phase, and a result output phase. The specific process is as follows: Initialization phase: After startup, the lightweight student model optimized by knowledge distillation is automatically loaded. The structure is compressed and the weight volume is small, so it can be directly run on: embedded computing modules; domestic government equipment; microscope intelligent recognition terminals, etc.
[0070] Data Input Stage: The system receives the image input source (images of insects to be identified) and performs localized preprocessing. The input format can be a single image or a continuous image stream. For example, in a microscope terminal, the image source is a continuous observation frame, acquired frame by frame, and clear frames are selected for processing; while on an administrative tablet, the image source is usually a camera photo or a close-up scan. The input image undergoes size resampling (256×256), pixel value normalization (between 0 and 1), RGB channel adjustment, and format conversion to a standard tensor (float32 type, dimensions [1,3,H,W]) by the image input processing module.
[0071] Inference computation phase: The image tensor (the preprocessed image) is fed into a distilled and optimized lightweight student model for a complete forward propagation. This inference computation phase is completed by a front-end convolution module, a patch embedding module, a local self-attention mechanism module, a fully connected classification output module, and a candidate output generation module, which can output a softmax probability vector corresponding to the target species. The inference phase is executed entirely locally on the terminal device and does not depend on a network connection. Subsequently, the top-k species names and confidence scores with the highest scores are selected from the probability vectors to form a top-k candidate list, which has good interpretability and practicality and is suitable for customs quarantine identification tasks with a large number of insect species and complex distribution of different species within the same genus.
[0072] In the results output stage, the top-k candidate list is organized into structured data objects, including candidate species names, corresponding confidence scores, inference time, current timestamps, and model versions used. This result is output through a unified interface, facilitating integration with front-end display systems, data acquisition control programs, or back-end logging systems. In practical applications, different presentation methods and interaction logics can be designed according to requirements.
[0073] It should be noted that this application only involves improvements to the recognition method itself and does not include specific front-end UI, graphical interface interaction, result management system or other external adaptation parts; the output is only structured data, which users can freely integrate and develop upper-layer application systems that meet the needs of the scenario.
[0074] Through the collaborative work of the above modules, this application provides a knowledge distillation-based insect identification method applicable to customs quarantine scenarios. It can be widely deployed on customs real-time general-purpose equipment such as microscope terminals and administrative pads to achieve efficient, accurate, low-latency, and offline operation on-site identification capabilities, providing technical support for tasks such as import and export animal and plant quarantine, invasive species screening, and preliminary judgment of high-risk specimens.
[0075] The following are two application examples: one deployed in a microscopic image recognition system in a port quarantine laboratory, and the other deployed in a real-time on-site judgment system on frontline customs administrative tablets. Application Case 1: Deployment in the Microscopic Image Recognition System of Port Quarantine Laboratory This application case demonstrates how this application is used as a tool for microscopic insect image recognition in a customs port insect quarantine laboratory. The system consists of a microscopic imaging device, an image acquisition module, and a workstation PC, running the lightweight insect recognition method based on knowledge distillation proposed in this application.
[0076] After the program starts, the system loads the distilled and optimized lightweight student model, which resides in local memory, ready for input. Quarantine personnel place the suspected sample under a microscope for observation, capture images through the acquisition module, and transmit them to the image input processing module. The image input processing module automatically resizes the image (to a uniform 256×256 resolution), normalizes the color channels, and converts it into a three-channel tensor.
[0077] After processing, the preprocessed image is input into a lightweight student model, performs one forward inference, and outputs probability vectors for various insects. The program then selects the top-3 candidate results and returns the candidate species name, confidence score, inference delay, and current timestamp. All of the above processes are completed locally on a PC, requiring no internet connection and without interrupting the microscopic observation process.
[0078] The final results are fed back to the laboratory's information system via a local standard interface, and can be used to generate electronic sample reports and assist experts in secondary interpretation. Laboratory staff can also save the identification results into a database for specimen archiving. This application case demonstrates that this application can serve as an automated supplement to traditional manual visual quarantine, greatly improving the efficiency of high-throughput sample processing.
[0079] Application Case 2: Application of the On-site Real-time Judgment System Deployed on Frontline Customs Administrative Pads This application case demonstrates how this application is used as a mobile tool for rapid insect identification on an administrative tablet used by frontline customs inspectors. The terminal device is a portable customs-specific tablet that supports camera image acquisition and local model deployment.
[0080] After deploying the relevant software for the lightweight insect identification method based on knowledge distillation of this invention on the Pad, the lightweight student model is loaded upon the first system startup. Customs officers can take pictures of the insects they carry or smuggle using the in-app photo function. After the images are uploaded, they automatically enter the preprocessing flow of the image input processing module to unify the image format and generate input tensors.
[0081] The lightweight student model's front-end convolutional module executes the forward propagation process locally on the Pad. The inference results return candidate species' Chinese and Latin names, probability values, current time, and other attribute information in top-k format, and support automatic recording of images and recognition results to form a local record.
[0082] This application can be integrated with the on-site handling system within the Pad, supporting subsequent operations such as: retrieving historical identification records, marking uncertain samples for re-examination, matching existing prohibited and restricted lists, and generating case records. This application case demonstrates the practical deployability of this application in non-experimental environments, low computing power, and highly mobile scenarios, possessing high practical value and promising prospects for widespread adoption.
[0083] Furthermore, as mentioned above, in contrast to the lightweight insect identification method based on knowledge distillation described above, see [link to relevant documentation]. Figure 1-5 The embodiments of this application also provide a customs real-time general-purpose device, which includes: The image input processing module is used to perform local preprocessing on the image of the insect to be identified, and obtain the preprocessed image. A lightweight student model, obtained from a teacher model through knowledge distillation, includes: The front-end convolution module is used to extract features and expand channels in the preprocessed image to obtain a convolutional feature map; The Patch Embedding module is used to divide the convolutional feature map into multiple fixed-size image blocks, and map each image block to a corresponding token to obtain a token sequence for the local self-attention mechanism. The local self-attention mechanism module is used to apply self-attention to local regions of the token sequence used for the local self-attention mechanism in order to model the global relationship between local regions in the image and obtain the token sequence for classification. The fully connected classification output module is used to transform and fuse the token sequence used for classification into a global image representation and output the softmax probability vector corresponding to the candidate insect species; The candidate output generation module is used to sort the softmax probability vector in descending order, select the top k categories as candidate results, and output a top-k candidate list, which includes the confidence scores of the top-k candidate insect species.
[0084] The interpretable Patch Embedding module is used to convert the convolutional feature maps output by the front-end convolutional module into token sequences that are adapted for local self-attention mechanisms.
[0085] The working process of the Patch Embedding module includes: Feature map segmentation: Divide the convolutional feature map into multiple non-overlapping image patches according to a preset fixed spatial size; Flattening and rearranging: Flatten the two-dimensional features of each image patch into a one-dimensional vector, and rearrange them in order to form a Patch vector sequence; Linear mapping: Each Patch vector is mapped to a unified d-dimensional embedding space through a fully connected layer (or an equivalent 1×1 convolution) to obtain the corresponding Token; Output Token Sequence: All mapped tokens are arranged in the original tile order to form a token sequence, which serves as input to the local self-attention mechanism to model the global relationships of local regions in the image.
[0086] In an interpretive sense, a Token (embedding vector unit) is a fixed-dimensional vector unit obtained by segmenting, flattening, and mapping an image or convolutional feature map. It serves as the basic input unit for feature modeling in local self-attention mechanisms or Transformer modules. Each Token corresponds to a local region in the image or feature map and represents the feature information of that region in the embedding space.
[0087] The softmax function (normalized exponential function) is a function that transforms a set of real number vectors into a probability distribution. Its calculation process involves performing an exponential operation on each element of the input vector, and then dividing that exponential value by the sum of the exponential values of all elements to obtain the normalized probability value of the corresponding element.
[0088] Finally, it should be noted that the technical features of the above embodiments can be combined arbitrarily. For the sake of brevity, not all possible combinations of the technical features in the above embodiments have been described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0089] The embodiments described above are merely examples of implementations of the present invention, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of the invention. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of the present invention, and these all fall within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the appended claims.
Claims
1. A lightweight insect identification method based on knowledge distillation, characterized in that, include: Acquire images of the insects to be identified. These images may come from the industrial camera in the microscope workstation, the built-in camera on the customs administration Pad, or externally uploaded image files. The image input processing module performs local preprocessing on the image of the insect to be identified to obtain a preprocessed image; the local preprocessing includes scaling or cropping the image of the insect to be identified at any resolution to a uniform input size. A lightweight student model is used to perform local inference on the preprocessed image input, and the top-k candidate insect species and their corresponding confidence scores are output. The lightweight student model is obtained from the teacher model through knowledge distillation. The local inference includes: extracting features and expanding channels of the preprocessed image through a front-end convolution module to obtain a convolutional feature map; dividing the convolutional feature map into multiple fixed-size image blocks through a Patch Embedding module, mapping each image block to a corresponding token to obtain a token sequence for a local self-attention mechanism; applying self-attention to the local regions of the token sequence for the local self-attention mechanism through a local self-attention mechanism module to model the global relationship between local regions in the image, obtaining a token sequence for classification; converting and fusing the token sequence for classification into a global image representation through a fully connected classification output module and outputting the softmax probability vector corresponding to the candidate insect species; and sorting the softmax probability vector in descending order through a candidate output generation module, selecting the top k categories as candidate results, and outputting a top-k candidate list, which includes the confidence scores corresponding to the top-k candidate insect species. The step of applying self-attention to local regions of the token sequence used for local self-attention mechanism through a local self-attention mechanism module to model the global relationship between local regions in the image and obtain a token sequence for classification includes: By applying self-attention to the local token regions of the token sequence used for the local self-attention mechanism through a two-layer lightweight Transformer encoder, a token sequence for classification is obtained. The step of applying self-attention to the local token region of the token sequence used for the local self-attention mechanism includes: Each layer of the Transformer encoder performs multi-head self-attention calculation, feedforward network processing, residual connection and layer normalization processing on the input, and finally the local self-attention mechanism module outputs a token sequence for classification. The lightweight student model is obtained through knowledge distillation from the teacher model, comprising a teacher model that has been fed into a frozen state and a lightweight student model to be trained, respectively, resulting in corresponding softmax probability vectors. and The weighted total loss of KL divergence and cross-entropy is calculated, and this weighted total loss is used as the joint loss function to backpropagate to all parameters of the lightweight student model. After training is stable and converged, the weights of the lightweight student model are exported in deployment format to adapt to the local inference environment of mobile devices. During training, the teacher model does not participate in parameter updates and only serves as a source of guidance signals. The lightweight student model is embedded in the mobile device as a local model. A token is a vector unit with fixed dimensions obtained by segmenting, flattening and mapping an image or convolutional feature map. It is the basic input unit for feature modeling in local self-attention mechanisms or Transformer modules. Each token corresponds to a local region in the image or feature map and represents the feature information of that local region in the embedding space. The step of extracting features and expanding channels in the preprocessed image through a front-end convolution module to obtain a convolutional feature map includes: Using the preprocessed image as the initial input, feature extraction and channel expansion are performed sequentially through three layers of depthwise separable convolutional blocks. The output of the previous depthwise separable convolutional block is used as the input of the next depthwise separable convolutional block. The depthwise separable convolutional block includes a convolution that performs 3×3 convolution on each input channel without channel fusion, and a convolution that uses 1×1 convolution to linearly combine the channels to achieve feature interaction and channel expansion. Each depthwise separable convolutional block is followed by BatchNorm and GELU activation, resulting in a tensor dimension of the final output convolutional feature map: X0∈R 1×128×64×64 R represents the set of real numbers.
2. The lightweight insect identification method based on knowledge distillation according to claim 1, characterized in that, The step of transferring the lightweight student model from the teacher model through knowledge distillation includes: The lightweight student model is optimized using a joint loss function composed of KL divergence and cross-entropy. The expression for the joint loss function is as follows: ; in, The output of the lightweight student model is based on the corresponding input image. Let y be the softmax probability vector output by the teacher model based on the input image, KL be the Kullback-Leibler divergence, CE be the cross-entropy loss function, y be the true label of the image, and α be the distillation balance coefficient, where 0.7 ≤ α ≤ 0.
9.
3. The lightweight insect identification method based on knowledge distillation according to claim 2, characterized in that, Before calculating the KL divergence, a probabilistic softening process is applied to the output of the teacher model based on the temperature parameter. This probabilistic softening process is expressed in normalized form as follows: ; in, This is the raw prediction output of the teacher model, where j is the index of the category, representing the j-th category, and its value range is equal to the number of categories. Let be the original probability output of the teacher model in the j-th category, T be the temperature parameter, and exp be the exponential function with the natural constant e as the base, used to map the real number of the input to a positive number, and the value increases exponentially with the increase of the input.
4. The lightweight insect identification method based on knowledge distillation according to claim 1, characterized in that, The step of dividing the convolutional feature map into multiple fixed-size image blocks using the Patch Embedding module, and mapping each image block to a corresponding token to obtain a token sequence for the local self-attention mechanism, includes: Convolutional feature maps Divided into segments by flattening and rearranging operations Image blocks; Each image patch is mapped to a token using a linear mapping layer, and the token is a d-dimensional embedding vector. The token sequence used for the local self-attention mechanism is obtained, and the token sequence used for the local self-attention mechanism is represented as follows: ; where t q Indicates the first Tokens.
5. The lightweight insect identification method based on knowledge distillation according to claim 4, characterized in that, The step of converting and fusing the token sequence used for classification into a global image representation and outputting the softmax probability vector corresponding to the candidate insect species through the fully connected classification output module includes: The global image representation vector is obtained by pooling all tokens in the token sequence used for classification using mean pooling through Token Pooling. The global representation vector of the image is processed by a two-layer fully connected classifier to determine the category and corresponding confidence score of the candidate insect species; Based on the category of the candidate insect species and the corresponding confidence score, output a softmax probability vector; The global image representation vector is represented as follows: The fully connected classifier is represented as: The softmax probability vector is represented as: ; N is the number of species categories; N t The minimum number of embedding units used for classification, i.e., the length of the token sequence, t. q Let v represent the embedding vector of the q-th token, v represent the global representation vector of the image, d represent the dimension of the token, d' represent the dimension of the hidden layer, W1 represent the weight matrix of the first layer in the fully connected classifier, mapping the global representation vector from dimension d to the hidden layer dimension d', b1 represent the bias vector of the first layer of the fully connected classifier, b2 represent the bias vector of the second layer of the fully connected classifier, W2 represent the weight matrix of the second layer of the fully connected classifier, mapping the hidden layer output to the class space, z represent the final linear output vector of the fully connected classifier, representing the original score corresponding to each class, and P s It is a probability vector obtained by applying the softmax function to z, where each element represents the confidence probability of the corresponding class; In the knowledge distillation stage, the softmax probability vector... Used for distillation alignment with the teacher model output.
6. The lightweight insect identification method based on knowledge distillation according to any one of claims 1-5, characterized in that, The training process of the teacher model includes: selecting the MobileNet model as the model structure of the teacher model; using multiple insect images obtained from the customs laboratory as the dataset, and performing supervised learning using image-level standard classification labels; using cross-entropy as the loss function during training, combining the Adam optimizer, and introducing a warm-up and learning rate decay strategy for optimization; until the teacher model's accuracy on the validation set is finally improved to a Top-1 accuracy of no less than 90%, thus completing the training.
7. Customs real-time universal equipment, characterized in that, include: The image input processing module is used to acquire images of insects to be identified. These images come from industrial cameras in the microscope workstation, built-in cameras on the customs administration Pad, or externally uploaded image files. The module performs local preprocessing on the images of the insects to be identified to obtain preprocessed images. The local preprocessing includes scaling or cropping images of insects to be identified at any resolution to a uniform input size. A lightweight student model is used to perform local inference on the preprocessed image input and output top-k candidate insect species and their corresponding confidence scores. The lightweight student model is obtained from the teacher model through knowledge distillation and includes a front-end convolutional module and a patch module. The module comprises an Embedding module, a local self-attention mechanism module, a fully connected classification output module, and a candidate output generation module. The front-end convolutional module is used to extract features and expand channels in the preprocessed image to obtain a convolutional feature map. This extraction and expansion involves using the preprocessed image as the initial input, sequentially performing feature extraction and channel expansion through three depthwise separable convolutional blocks. The output of the previous depthwise separable convolutional block serves as the input to the next. Each depthwise separable convolutional block includes a 3×3 convolution performed on each input channel without channel fusion, and a 1×1 convolution that linearly combines the channels to achieve feature interaction and channel expansion. Each depthwise separable convolutional block is followed by BatchNorm and GELU activation. The tensor dimension of the final output convolutional feature map is: X0∈R. 1×128×64×64 R represents the set of real numbers; the Patch Embedding module is used to divide the convolutional feature map into multiple fixed-size image patches, and map each image patch to a corresponding token to obtain a token sequence for the local self-attention mechanism; the local self-attention mechanism module is used to apply self-attention to the local regions of the token sequence for the local self-attention mechanism to model the global relationship between local regions in the image, and obtain a token sequence for classification; applying self-attention to the local regions of the token sequence for the local self-attention mechanism to model the global relationship between local regions in the image, and obtain a token sequence for classification, includes applying self-attention to the local token regions of the token sequence for the local self-attention mechanism through a two-layer lightweight Transformer encoder to obtain... A token sequence for classification is obtained; wherein, applying self-attention to the local token region of the token sequence for the local self-attention mechanism includes each layer of the Transformer encoder performing multi-head self-attention calculation, feedforward network processing, residual connection and layer normalization processing on the input, and finally the local self-attention mechanism module outputs the token sequence for classification; the fully connected classification output module is used to convert and fuse the token sequence for classification into a global image representation and output the softmax probability vector corresponding to the candidate insect species; the candidate output generation module is used to sort the softmax probability vector in descending order, select the top k categories as candidate results, and output a top-k candidate list, wherein the top-k candidate list includes the confidence scores corresponding to the top-k candidate insect species; The lightweight student model is obtained through knowledge distillation from the teacher model, comprising a teacher model that has been fed into a frozen state and a lightweight student model to be trained, respectively, resulting in corresponding softmax probability vectors. and The weighted total loss of KL divergence and cross-entropy is calculated, and this weighted total loss is used as the joint loss function to backpropagate to all parameters of the lightweight student model. After training stabilizes and converges, the weights of the lightweight student model are exported in deployment format to adapt to the local inference environment of mobile devices. During training, the teacher model does not participate in parameter updates and only serves as a source of guidance signals. The lightweight student model is embedded in the mobile device as a local model. A Token is a vector unit with a fixed dimension obtained by segmenting, flattening, and mapping an image or convolutional feature map. It is the basic input unit for feature modeling by the local self-attention mechanism or the Transformer module. Each Token corresponds to a local region in the image or feature map and represents the feature information of that local region in the embedding space.
Citation Information
Patent Citations
Fine-grained image classification method based on feature fusion and semantic enhancement
CN118799646A
Lightweight ViT based on image feature cutting and cloud edge knowledge distillation
CN120375050A