Tablet surface defect detection method and system based on improved YOLOv7

CN116228730BActive Publication Date: 2026-08-21BEIJING INFORMATION SCI & TECH UNIV
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310252153.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-03-16
Publication Date
2026-08-21
Estimated Expiration
2043-03-16

AI Technical Summary

Technical Problem

[0002]随着人们对于医药的品质要求在不断提高且需求的数量在不断加大的情况,导致药片在生产的过程中,药片质量检测的工作越来越繁重

Benefits of technology

[0028]本申请采用融合SimAM注意力机制的MP-S结构,提高对于小目标缺陷的检测精度和目标定位能力,减少目标的候选框数量,置信度更高,提高网络模型的收敛速度;采用SIoU的损失函数,减小网络模型预测框与真实框之间差距,提高目标检测精度;采用Soft-NMS方法优化候选框选择方法,减少了由于误删候选框导致的目标误检的情况。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116228730B_ABST
    Figure CN116228730B_ABST
Patent Text Reader

Abstract

The application discloses a tablet surface defect detection method and system based on improved YOLOv7, wherein the method steps comprise: constructing a YOLOv7 model; the input of the YOLOv7 model is a drug surface image; the constructed YOLOv7 model is improved to obtain a detection model; and the detection model is used for defect detection on a tablet surface to be detected. The application adopts an MP-S structure fused with a SimAM attention mechanism, improves the detection precision and target positioning capability for small target defects, reduces the number of target candidate frames, has higher confidence, and improves the convergence speed of the network model; a SIoU loss function is used to reduce the gap between the network model prediction frame and the real frame, improve the target detection precision; and a Soft-NMS method is used to optimize the candidate frame selection method, and the target mis-detection caused by the misdeletion of the candidate frame is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of image processing, specifically to a method and system for detecting defects on the surface of tablets based on an improved YOLOv7. Background Technology

[0002] As people's demands for pharmaceutical quality continue to rise and the quantity of demand increases, the quality inspection of tablets during production is becoming increasingly demanding. Due to factors such as manufacturing technology and the physical characteristics of the tablets themselves, surface defects such as scratches, cracks, peeling, breakage, and oil stains are prone to occur, significantly reducing the therapeutic effect of the tablets and negatively impacting the company's brand image. Traditional manual inspection methods are inefficient, costly, prone to secondary contamination, and susceptible to false positives and false negatives, which is inconsistent with the development trend of industrial automation. Therefore, researchers have proposed many research methods. Traditional inspection techniques include surface defect detection methods based on the principle of optical reflection. This method takes multiple images of the tablet under different environmental conditions, calculates the Von Leigh illumination model based on the surface reflection of the tablet, and then uses the least squares method to estimate the hemispherical reflectivity and illumination model parameters to determine the tablet's reflection threshold. If the reflection of the tablet is detected outside the threshold, it is considered a defective tablet. This method has high requirements for lenses and lighting, and changes in the environment can significantly affect the results. In addition, there is an image processing-based method for detecting circular defects. This method first enhances the brightness and contrast of the image through preprocessing, and then further analyzes the circles present in the image using a circular Hough transform. However, this method can only detect circular tablets and cannot detect tablets of other shapes, thus having limited applicability. With the continuous development of deep learning technology, pharmaceutical packaging defect detection technology using deep learning has made some progress. Summary of the Invention

[0003] Based on the YOLOv7 model, this application improves the original model in three aspects: frame, loss function, and candidate target boxes. The resulting detection model has a very good effect on the detection of tablet surfaces.

[0004] To achieve the above objectives, this application provides a method for detecting defects on the surface of tablets based on an improved YOLOv7, comprising the following steps:

[0005] A YOLOv7 model is constructed; the input of the YOLOv7 model is a drug surface image; the input of the YOLOv7 model is used for image preprocessing and data augmentation. First, the file is read into an RGB color image, then normalization is performed, and the image is scaled to a specified size.

[0006] The constructed YOLOv7 model is improved to obtain a detection model;

[0007] The aforementioned detection model is used to detect defects on the surface of the tablet to be tested.

[0008] Preferably, the method for obtaining the detection model includes: optimizing the original architecture in the YOLOv7 model; replacing the loss function in the YOLOv7 model; and optimizing the method of the YOLOv7 model when selecting bounding boxes.

[0009] Preferably, the method for optimizing the original architecture includes: introducing the SimAM attention mechanism into the YOLOv7 model; and replacing the MP1 structure in the YOLOv7 model with an MP-S structure based on the SimAM attention mechanism.

[0010] Preferably, the method for replacing the loss function includes: replacing the CIoU loss function in the YOLOv7 model with the SIoU loss function; the expression of the SIoU loss function includes:

[0011]

[0012] Where Ω represents shape loss; Δ represents distance loss; and IoU represents the intersection-union ratio of the target box and the candidate box.

[0013] Preferably, the method for optimizing the target box selection includes: using the Soft-NMS module to replace the NMS module in the YOLOv7 model for target box selection.

[0014] This application also provides a tablet surface defect detection system based on the improved YOLOv7, including: a building module, an improvement module, and a detection module;

[0015] The building module is used to build a YOLOv7 model; the input of the YOLOv7 model is a drug surface image; the input end of the YOLOv7 model is used for image preprocessing and data augmentation, first reading the file into an RGB color image, then performing normalization operations, and scaling the image to a specified size;

[0016] The improvement module is used to improve the constructed YOLOv7 model to obtain a detection model;

[0017] The detection module is used to detect defects on the surface of the tablet to be tested using the detection model.

[0018] Preferably, the improvement module includes: an original frame improvement unit, a loss function improvement unit, and a target box selection improvement unit;

[0019] The original framework improvement unit is used to optimize the original architecture in the YOLOv7 model;

[0020] The loss function improvement unit is used to replace the loss function in the YOLOv7 model;

[0021] The target box selection improvement unit is used to optimize the method of the YOLOv7 model when selecting target boxes.

[0022] Preferably, the workflow of the original framework improvement unit includes: introducing the SimAM attention mechanism into the YOLOv7 model; and replacing the MP1 structure in the YOLOv7 model with the MP-S structure based on the SimAM attention mechanism.

[0023] Preferably, the workflow of the loss function improvement unit includes: replacing the CIoU loss function in the YOLOv7 model with the SIoU loss function; the expression of the SIoU loss function includes:

[0024]

[0025] Where Ω represents shape loss; Δ represents distance loss; and IoU represents the intersection-union ratio of the target box and the candidate box.

[0026] Preferably, the workflow of the target box selection improvement unit includes: using the Soft-NMS module to replace the NMS module in the YOLOv7 model to select the target box.

[0027] Compared with the prior art, the beneficial effects of this application are as follows:

[0028] This application adopts an MP-S structure that integrates the SimAM attention mechanism to improve the detection accuracy and target localization capability for small target defects, reduce the number of candidate boxes for targets, increase confidence, and improve the convergence speed of the network model. It uses the SIoU loss function to reduce the gap between the network model's predicted boxes and the ground truth boxes, thereby improving the target detection accuracy. It also uses the Soft-NMS method to optimize the candidate box selection method, reducing the occurrence of false detections due to the deletion of candidate boxes. Attached Figure Description

[0029] To more clearly illustrate the technical solutions of this application, the drawings used in the embodiments are briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0030] Figure 1 This is a schematic diagram of the YOLOv7 network model structure according to an embodiment of this application;

[0031] Figure 2 This is a schematic diagram of the detection model in an embodiment of this application;

[0032] Figure 3 This is a schematic diagram of the MP1 structure in the SimAM improved network model of this application embodiment;

[0033] Figure 4 This is a schematic diagram illustrating the SimAM principle of an embodiment of this application;

[0034] Figure 5 This is a schematic diagram of the system structure according to an embodiment of this application. Detailed Implementation

[0035] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0036] To make the above-mentioned objectives, features and advantages of this application more apparent and understandable, the application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0037] Example 1

[0038] First, construct a YOLOv7 network model. For example... Figure 1As shown, the YOLOv7 network consists of three parts: the input layer, the backbone, and the head. Conv (Convolution) is a convolutional layer; CBS (Conv+Batchnormalization+Silu) consists of convolutional layers, batch normalization, and the Silu loss function; the Conv network layers and CBS are used for feature extraction; MP1 and MP2 consist of max pooling layers and CBS; UP uses nearest neighbor interpolation for upsampling; MP and UP are used together for feature fusion and dimensionality reduction, accelerating network training; Cat (Concat) is a connection layer used for multi-branch feature fusion; ELAN (Efficient Layer Aggregation)... Network (ELAN) and ELAN2 consist of multiple CBSs. By controlling the shortest and longest gradient paths, the network can learn more features while keeping the input and output feature sizes constant. SPPC is a spatial pyramid structure that uses different max pooling parameters to obtain different receptive fields for detecting targets of different sizes in images. The REP structure consists of RepConv, which borrows the structural ideas of RepVGG. RepConv combines a 3x3 convolution, a 1x1 convolution, and an identity mapping into a single convolution. The network contains three REP branches for feature extraction, feature smoothing, and feature propagation, respectively.

[0039] The YOLOv7 model described above uses image preprocessing and data augmentation at its input. First, the file is read as an RGB color image, then normalized, and the image is scaled to a specified size. During network training, YOLOv7 determines whether to use Mosaic stitching for data augmentation. The Mosaic method combines several images to be trained into a single image. This method significantly improves network training speed and reduces model memory requirements, showing good performance for small object detection tasks such as pill defect detection. The backbone network uses convolutional neural networks to extract features at different image granularities. It employs a top-down and bottom-up joint feature fusion approach to recombine image features from different levels and pass them to the detection head. The detection head predicts bounding boxes and categories based on the received image features. Non-Maximum Suppression (NMS) eliminates redundant predicted boxes, ultimately outputting the predicted category and bounding box coordinates with the highest confidence.

[0040] Based on the YOLOv7 network model constructed above, it is optimized and improved. The steps include: optimizing the original architecture of the YOLOv7 model; replacing the loss function in the YOLOv7 model; and optimizing the method of the YOLOv7 model when selecting bounding boxes. The improved detection model backbone obtained in this embodiment is as follows: Figure 2 As shown in the figure, the MP-S structure in the original YOLOv7 model architecture is replaced with the MP1 structure. MP-S, combined with the SimAM attention mechanism, can improve the detection accuracy of small target defects in pills. Simultaneously, this detection model uses the SIoU loss function, considering the angular relationship between the predicted and ground truth boxes when calculating the difference, reducing the network model training loss and improving training accuracy. Furthermore, this embodiment also employs Soft-NMS to optimize predicted box selection, reducing the erroneous removal of predicted boxes in cases of overlap.

[0041] This embodiment proposes an MP-S (Maxpooling + SimAM + Conv as the main network layer) structure based on the SimAM attention mechanism, such as... Figure 3 As shown, compared to MP1, MP-S retains its dimensionality reduction and feature fusion capabilities while enhancing its ability to locate salient small targets, thus improving the target localization ability of defect features during detection, especially for small target defects. The SimAM attention mechanism (Simple, Parameter-Free Attention Module, SimAM) differs from existing 1-D channel attention mechanisms and 2-D spatial attention mechanisms. SimAM considers both spatial and channel dimensional correlations, inferring 3-D attention weights through feature mapping in the feature layer. It only requires a single energy function for calculation, without the need for additional parameters, making it a plug-and-play attention mechanism. Therefore, it does not require adjustments to the network model structure, demonstrating good flexibility and effectiveness in improving the representation capabilities of convolutional networks.

[0042] The principle of SimAM attention mechanism is as follows: Figure 4 As shown in the figure. X represents the input feature, C represents the current number of feature channels, and W and H represent the current feature width and height, respectively. The output features obtained by 3-D weight calculation through SimAM attention mechanism are more global. The data processing includes both spatial and channel dimensions, and the reference information is more comprehensive. It can better locate small targets in the image. The SimAM calculation process is shown in formula (1).

[0043]

[0044] In the formula, X represents the input feature; E represents the output feature calculated by the energy function, where E is the energy function for each channel; ⊙ represents the dot product calculation. The sigmoid function is used to avoid the problem of model non-convergence caused by the E value deviating too much from the center position, and the calculation method of E is shown in formula (2).

[0045]

[0046] In the formula, t represents the input feature value, t∈X; λ represents a constant, the value of which varies for different datasets; μ and σ 2 Let μ and σ represent the mean and variance of the X channel, respectively. 2 The calculation method is shown in formula (3), where M = H × W, which represents the number of features in each channel.

[0047]

[0048] In the formula, x i Let i represent the feature value of the i-th input.

[0049] The improved network has fewer candidate boxes containing the target when locating candidate boxes, and these candidate boxes are closer to the labeled target boxes, resulting in higher confidence. The MP-S structure can improve the convergence speed of the network model.

[0050] Then, the loss function was improved based on the above.

[0051] In this embodiment, SIoU (Scylla-IoU, SIoU) is used instead of CIoU (Complete-IoU, CIoU) as the loss function for training.

[0052] The SIoU loss function used in this embodiment takes into account the issue that different angles between the ground truth box and the candidate box will affect the confidence of the candidate box. The angle loss is defined as shown in formula (4), where σ is defined as the distance between the center points of the ground truth box and the candidate box.

[0053]

[0054] In the formula, Λ represents the angle loss; α represents the horizontal angle between the ground truth bounding box and the candidate bounding box; C h The vertical distance between the ground truth bounding box and the candidate bounding box is represented by ; x represents the sine of the horizontal angle between the ground truth bounding box and the candidate bounding box.

[0055] To address the issue that existing loss functions do not consider the angular relationship between boxes, this embodiment adopts the SIoU loss function, as shown in formula (5), where SIoU consists of angular loss, distance loss, shape loss, and IoU loss.

[0056]

[0057] In the formula, Ω represents the shape loss; Δ represents the distance loss; and IoU represents the intersection-union ratio of the target box and the candidate box. When calculating the distance loss Δ, γ needs to be calculated first, and γ is obtained by first calculating the angle loss Λ, as shown in formula (6):

[0058]

[0059] In the formula, W GT W and H represent the widths of the ground truth bounding box and the predicted bounding box, respectively; GT H and H are the heights of the ground truth bounding box and the predicted bounding box, respectively; and These represent the x-coordinates of the center points of the ground truth bounding box and the predicted bounding box, respectively. and C represents the ordinate of the center point of the ground truth bounding box and the center point of the predicted bounding box, respectively; w and C h represents the horizontal and vertical lengths of the minimum bounding rectangles of the ground truth bounding box and the candidate bounding box, respectively; γ, ρ (including all different subscripts), and ω (including all different subscripts) represent different intermediate variables for calculating the angle loss, obtained by the distance between the center points of the candidate and ground truth bounding boxes and the aspect ratio of the minimum bounding rectangle; max represents the maximum value function; Σ is used for accumulation; m and n represent the range of values ​​when calculating the distance loss; p and q represent the range of values ​​when calculating the shape loss; t represents the intermediate variable; θ represents the special parameters calculated by the genetic algorithm for different datasets, used to calculate the shape loss.

[0060] Finally, Soft-NMS is used instead of NMS to improve the YOLOv7 model. The steps for removing candidate boxes are compared, and the traditional NMS and Soft-NMS approaches are shown in equations (7) and (8).

[0061]

[0062]

[0063] In the formula, iou(M, b) i The calculations are for the candidate boxes M and b with the highest confidence. i The overlap rate between them, through the threshold N t Compare and assign candidate box b i Confidence level S i The assignment is performed. Soft-NMS follows the same algorithm execution flow as NMS, but in calculating the detection confidence score, NMS directly assigns the candidate box confidence score S. iSetting it to 0 causes Soft-NMS to reduce the confidence score through computation. Soft-NMS calculates the confidence score linearly, optimizing for target detection failures and reduced average detection rates caused by target overlap without significantly increasing computational cost. Soft-NMS is well-suited for situations where the candidate box overlap threshold cannot be determined. NMS is a special form of Soft-NMS; when the score reset function uses a binarization function, Soft-NMS and NMS are equivalent.

[0064] After the above steps, a detection model is obtained. Finally, the detection model is used to detect defects on the surface of the tablet.

[0065] Example 2

[0066] like Figure 5 The diagram shown illustrates the system structure of this embodiment, including a construction module, an improvement module, and a detection module. The construction module is used to construct a YOLOv7 model. The input to the YOLOv7 model is an image of the drug surface. The improvement module is used to improve the constructed YOLOv7 model to obtain a detection model. The detection module is used to perform defect detection on the surface of the drug tablet using the detection model. The improvement module includes an original framework improvement unit, a loss function improvement unit, and a target box selection improvement unit. The original framework improvement unit optimizes the original architecture of the YOLOv7 model. The loss function improvement unit replaces the loss function in the YOLOv7 model. The target box selection improvement unit optimizes the method of the YOLOv7 model when selecting target boxes.

[0067] The following will describe in detail, with reference to this embodiment, how this application solves technical problems in real life.

[0068] First, use the building blocks to construct a YOLOv7 network model. For example... Figure 1As shown, the YOLOv7 network consists of three parts: the input, the backbone, and the head. In this network, Conv is a convolutional layer, CBS consists of a convolutional layer, batch normalization, and a Silu loss function. The Conv network layer and CBS are used for feature extraction. MP1 and MP2 consist of a max pooling layer and CBS. UP uses the nearest neighbor interpolation algorithm for upsampling. MP and UP are used together for feature fusion and dimensionality reduction to accelerate network training. Cat is a connection layer used for multi-branch feature fusion. ELAN and ELAN2 consist of multiple CBSs. By controlling the shortest and longest gradient paths, the network can learn more features while keeping the input and output feature sizes constant. SPPCSPC is a spatial pyramid structure that uses different max pooling parameters to obtain different receptive fields for detecting targets of different sizes in images. The REP structure consists of RepConv, which borrows the structural ideas of RepVGG. RepConv combines a 3x3 convolution, a 1x1 convolution, and an identity mapping into a single convolution. The network contains three REP branches, used for feature extraction, feature smoothing, and feature propagation, respectively.

[0069] The YOLOv7 input is used for image preprocessing and data augmentation. First, the file is read as an RGB color image, then normalized, and the image is scaled to a specified size. During network training, YOLOv7 determines whether to use Mosaic stitching for data augmentation. The Mosaic method combines several images to be trained into a single image. This method significantly improves network training speed and reduces model memory requirements, showing good performance for small object detection tasks such as pill defect detection. The backbone network uses convolutional neural networks to extract features at different image granularities. It employs a top-down and bottom-up joint feature fusion approach to recombine image features from different levels and pass them to the detection head. The detection head predicts bounding boxes and categories based on the received image features. Non-Maximum Suppression (NMS) eliminates redundant predicted boxes, ultimately outputting the predicted category and bounding box coordinates with the highest confidence.

[0070] Based on the YOLOv7 network model constructed above, an improvement module is used to optimize and improve it. The process includes: optimizing the original architecture of the YOLOv7 model; replacing the loss function in the YOLOv7 model; and optimizing the method of the YOLOv7 model when selecting bounding boxes. The improved detection model backbone obtained in this embodiment is as follows: Figure 2As shown in the figure, the MP-S structure in the original YOLOv7 model architecture is replaced with the MP1 structure. MP-S, combined with the SimAM attention mechanism, can improve the detection accuracy of small target defects in pills. Simultaneously, this detection model uses the SIoU loss function, considering the angular relationship between the predicted and ground truth boxes when calculating the difference, reducing the network model training loss and improving training accuracy. Furthermore, this embodiment also employs Soft-NMS to optimize predicted box selection, reducing the erroneous removal of predicted boxes in cases of overlap.

[0071] First, the original architecture in the YOLOv7 model is optimized using the original framework improvement unit. This embodiment proposes an MP-S (Max pooling + SimAM + Conv as the main network layer structure) structure based on the SimAM attention mechanism, such as... Figure 3 As shown, compared to MP1, MP-S retains its dimensionality reduction and feature fusion capabilities while enhancing its ability to locate salient small targets, thus improving the target localization ability of defect features during detection, especially for small target defects. The SimAM attention mechanism (Simple, Parameter-Free Attention Module, SimAM) differs from existing 1-D channel attention mechanisms and 2-D spatial attention mechanisms. SimAM considers both spatial and channel dimensional correlations, inferring 3-D attention weights through feature mapping in the feature layer. It only requires a single energy function for calculation, without the need for additional parameters, making it a plug-and-play attention mechanism. Therefore, it does not require adjustments to the network model structure, demonstrating good flexibility and effectiveness in improving the representation capabilities of convolutional networks.

[0072] The principle of SimAM attention mechanism is as follows: Figure 4 As shown in the figure. X represents the input feature, C represents the current number of feature channels, and W and H represent the current feature width and height, respectively. The output features obtained by 3-D weight calculation through SimAM attention mechanism are more global. The data processing includes both spatial and channel dimensions, and the reference information is more comprehensive. It can better locate small targets in the image. The SimAM calculation process is shown in formula (9).

[0073]

[0074] In the formula, X represents the input features; E represents the output feature calculated by the energy function, where E is the energy function for each channel; ⊙ represents the dot product calculation. The sigmoid function is used to avoid the problem of model non-convergence caused by the E value deviating too much from the center position, and the calculation method of E is shown in formula (10).

[0075]

[0076] In the formula, t represents the input feature value, t∈X, λ represents a constant (the value of λ varies for different datasets), and μ and σ 2 Let μ and σ represent the mean and variance of the X channel, respectively. 2 The calculation method is shown in formula (11), where M = H × W, which represents the number of features in each channel.

[0077]

[0078] In the formula, x i Let i represent the feature value of the i-th input.

[0079] The improved network has fewer candidate boxes containing the target when locating candidate boxes, and these candidate boxes are closer to the labeled target boxes, resulting in higher confidence. The MP-S structure can improve the convergence speed of the network model.

[0080] Then, the loss function is improved based on the above by using the loss function improvement unit.

[0081] In this embodiment, SIoU (Scylla-IoU, SIoU) is used instead of CIoU (Complete-IoU, CIoU) as the loss function for training.

[0082] The SIoU loss function used in this embodiment takes into account the issue that different angles between the ground truth box and the candidate box will affect the confidence of the candidate box. The angle loss is defined as shown in formula (12), where σ is defined as the distance between the center points of the ground truth box and the candidate box.

[0083]

[0084] In the formula, Λ represents the angle loss; α represents the angle between the ground truth bounding box and the candidate bounding box; C h The vertical distance between the ground truth bounding box and the candidate bounding box is represented by ; x represents the sine of the horizontal angle between the ground truth bounding box and the candidate bounding box.

[0085] To address the issue that existing loss functions do not consider the angular relationship between boxes, this embodiment adopts the SIoU loss function, as shown in formula (13), where SIoU consists of angular loss, distance loss, shape loss, and IoU loss.

[0086]

[0087] In the formula, Ω represents the shape loss; Δ represents the distance loss; and IoU represents the intersection-union ratio of the target box and the candidate box. When calculating the distance loss Δ, γ needs to be calculated first, and γ needs to be obtained by first calculating the angle loss Λ, as shown in formula (14):

[0088]

[0089] In the formula, W GT W and H represent the widths of the ground truth bounding box and the predicted bounding box, respectively; GT H and H are the heights of the ground truth bounding box and the predicted bounding box, respectively; and These represent the x-coordinates of the center points of the ground truth bounding box and the predicted bounding box, respectively. and C represents the ordinate of the center point of the ground truth bounding box and the center point of the predicted bounding box, respectively; w and C h represents the horizontal and vertical lengths of the minimum bounding rectangles of the ground truth bounding box and the candidate bounding box, respectively; γ, ρ (including all different subscripts), and ω (including all different subscripts) represent different intermediate variables for calculating the angle loss, obtained by the distance between the center points of the candidate and ground truth bounding boxes and the aspect ratio of the minimum bounding rectangle; max represents the maximum value function, and Σ is used for accumulation; m and n represent the range of values ​​when calculating the distance loss; p and q represent the range of values ​​when calculating the shape loss; t represents an intermediate variable; θ represents specific parameters calculated by a genetic algorithm for different datasets, used to calculate the shape loss.

[0090] Finally, the target box selection improvement unit is used to optimize the method of YOLOv7 model in target box selection. The workflow includes: using Soft-NMS instead of NMS to improve the YOLOv7 model. The steps of removing candidate boxes are compared, and the traditional NMS and Soft-NMS approaches are shown in equations (15) and (16).

[0091]

[0092]

[0093] In the formula, iou(M, b) i The calculations are for the candidate boxes M and b with the highest confidence. i The overlap rate between them, through the threshold N t Compare and assign candidate box b i Confidence level S i The assignment is performed. Soft-NMS follows the same algorithm execution flow as NMS, but in calculating the detection confidence score, NMS directly assigns the candidate box confidence score S. iSetting it to 0 causes Soft-NMS to reduce the confidence score through computation. Soft-NMS calculates the confidence score linearly, optimizing for target detection failures and reduced average detection rates caused by target overlap without significantly increasing computational cost. Soft-NMS is well-suited for situations where the candidate box overlap threshold cannot be determined. NMS is a special form of Soft-NMS; when the score reset function uses a binarization function, Soft-NMS and NMS are equivalent.

[0094] After the above steps, a detection model is obtained. Finally, the detection module uses the detection model to detect defects on the surface of the tablet.

[0095] The embodiments described above are merely preferred embodiments of this application and are not intended to limit the scope of this application. Any modifications and improvements made to the technical solutions of this application by those skilled in the art without departing from the spirit of this application shall fall within the protection scope defined by the claims of this application.

Claims

1. A method for detecting surface defects of tablets based on an improved YOLOv7, characterized in that the steps are as follows: include: Construct a YOLOv7 model; the input to the YOLOv7 model is an image of the drug surface; The input of the YOLOv7 model is used for image preprocessing and data augmentation. First, the file is read into an RGB color image, then normalization is performed, and the image is scaled to a specified size. The constructed YOLOv7 model is improved to obtain a detection model; the SimAM attention mechanism is introduced into the YOLOv7 model; the MP1 structure in the YOLOv7 model is replaced by the MP-S structure based on the SimAM attention mechanism; the MP-S structure includes: Max pooling, SimAM, and Conv. The aforementioned detection model is used to detect defects on the surface of the tablet to be tested.

2. The method for detecting tablet surface defects based on improved YOLOv7 according to claim 1, characterized in that, The method for obtaining the detection model includes: optimizing the original architecture in the YOLOv7 model; replacing the loss function in the YOLOv7 model; and optimizing the method of the YOLOv7 model when selecting bounding boxes.

3. The method for detecting tablet surface defects based on improved YOLOv7 according to claim 2, characterized in that, The method for replacing the loss function includes: replacing the CIoU loss function in the YOLOv7 model with the SIoU loss function; the expression of the SIoU loss function includes: Where Ω represents shape loss; ∆ represents distance loss; and IoU represents the intersection-union ratio of the target box and the candidate box.

4. The method for detecting tablet surface defects based on improved YOLOv7 according to claim 2, characterized in that, The method for optimizing the target box selection includes: using the Soft-NMS module to replace the NMS module in the YOLOv7 model for target box selection.

5. A tablet surface defect detection system based on an improved YOLOv7, said system being used to implement the method according to any one of claims 1-4, characterized in that, include: Modules for building, improving, and detecting; The building module is used to build a YOLOv7 model; the input of the YOLOv7 model is a drug surface image; the input end of the YOLOv7 model is used for image preprocessing and data augmentation, first reading the file into an RGB color image, then performing normalization operations, and scaling the image to a specified size; The improvement module is used to improve the constructed YOLOv7 model to obtain a detection model; The detection module is used to detect defects on the surface of the tablet to be tested using the detection model.

Citation Information

Patent Citations

  • Insulator defect detection method based on improved YOLOv7

    CN115731164A