| During capsule production,surface defects in the capsules can occur due to uncontrollable factors such as equipment and environment.These defects can affect the quality of medications.Therefore,capsule defect detection technology is significant for screening unqualified medications.Traditional methods for detecting medication surface defects,including manual inspection and machine vision inspection,are challenging to meet automated production needs.In recent years,due to the rapid development of artificial intelligence,deep learning-based target detection technology has been widely used in various domains.Among these technologies,You Only Look Once series algorithms perform well in defect detection processing.In this paper,we build a surface defect detection model of capsules based on the YOLOv5 algorithm and achieve accurate classification and localization of 5 types of defects on the capsule surface.The main contributions and innovations are as follows.(1)To address the problem of the small number of samples in the MVTec AD capsule dataset,single-data deformation methods such as geometric transformation,color gamut transformation,and noise injection are used to batch enhancement of the capsule dataset without changing the image semantics.This aims to increase the data volume of samples while reducing the number of difference between different types of samples.A Finetune single migration training strategy is adopted for target detection model training.This strategy first trains the whole network using the publicly available COCO2017 dataset to obtain the initial model.Then it uses the parameters of the initial model as pre-training weights for migration,thaws,and freezes the training capsule dataset in batches to obtain the final capsule defect detection model.(2)Aiming at the problem that the surface defects of capsules have large-scale differences and are not obvious,a method for detecting surface defects of capsules based on Trans B-YOLOv5 is proposed.Using the Transformer to establish a global image dependency mechanism.The Trans encoder module is applied to feature images with low,medium,and high resolutions so that the model pays more attention to the relationship between image blocks.On this basis,a fast normalized weighted bidirectional BiFPN feature fusion network is constructed,which combines the feature maps of the backbone network with the same level of the bottom-up branch in the network to reduce the problem of invisible information loss.The ablation and comparison experiments were conducted on the model,and the experimental results show that the mAP value of the improved Trans B-YOLOv5 model on the capsule test set reaches 92.9%,which has a good detection accuracy.(3)Given that the increased number of parameters in the proposed Trans B-YOLOv5 model may lead to inconvenient deployment on end-terminals,a lightweight Ghost CSP-YOLOv5 method for capsule surface defect detection is proposed.First,the CSP1_X module was reconstructed based on Trans B-YOLOv5,with the lightweight Ghost module being introduced into the Bottleneck and the convolution module on the side branch being removed.Then,a random pooling SPP is used.The feature extraction process will assign an activation rate according to the feature value size to retain more image information.Finally,the Focall EIoU Loss function is introduced to reduce the difference between the prediction frame and the Real Frame by optimizing the bounding box regression to accelerate the model’s convergence speed and detection accuracy.The experimental results show that the optimized Ghost CSP-YOLOv5 model achieved an mAP of 94.5% on the capsule test set,with a parameter count of 6.5M,which effectively reduces the model parameter and facilitates practical deployment. |