**1. Introduction**

Food safety has always been a social health issue of great concern to people. Impurity pollution accounts for a large proportion of food pollution and is difficult to avoid [1]. Impurity pollution refers to the presence of other substances or foreign substances in food other than the food itself [2], which will cause physical and psychological harm to consumers. Taking walnut kernels as an example, impurities of walnut kernels can be divided into exogenous impurities (stones, metal parts, and plastic fragments) and endogenous impurities (walnut shells; spoiled walnut kernels) [3], which will seriously affect consumers satisfaction. Therefore, impurity detection is one of the important links to ensure the high quality of nut food [4]. In recent years, many researchers have tried to use imaging detection technology to detect impurities in food, including X imaging technology, terahertz detection technology, spectral detection analysis technology, machine vision detection technology, etc.

In the field of machine vision detection, object detection methods based on deep learning are developing rapidly. According to the nature of the algorithm stages, the current mainstream algorithms can be divided into two categories: one of them is the twostage algorithm R-CNN series, and the representative algorithms include R-CNN, SPP-Net,

**Citation:** Yu, L.; Qian, M.; Chen, Q.; Sun, F.; Pan, J. An Improved YOLOv5 Model: Application to Mixed Impurities Detection for Walnut Kernels. *Foods* **2023**, *12*, 624. https:// doi.org/10.3390/foods12030624

Academic Editor: Antonello Santini

Received: 26 October 2022 Revised: 17 January 2023 Accepted: 22 January 2023 Published: 1 February 2023

**Copyright:** © 2023 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https:// creativecommons.org/licenses/by/ 4.0/).

and Faster R-CNN [5]. The series of algorithms first generate regions and then classify samples through convolutional neural networks. Xie et al. used the Fsater-RCNN + VGG16 neural network model to detect bone impurities in salmon meat [6]. Wang et al. used the Faster RCNN ResNet101 for detecting the potato surface defects and verified the high recognition accuracy of the model [7]. The point of the R-CNN series of algorithms is that the detection accuracy is high, but the detection speed is defective. The other type is a singlestage algorithm, and the representative algorithm is the YOLO series. The YOLO (You Only Look Once) algorithm directly inputs the entire image into the model network and returns the classification category and location of the bounding box at the output, so it can extract all features in the image and predict all objects [8,9]. Based on the results of various studies, the YOLOv5 algorithm in the YOLO series has better comprehensive detection ability than other YOLO models due to its accuracy and detection accuracy [10–12]. Many researchers have applied the YOLOv5 model or the improved YOLOv5 model to the food safety field for object detection [13]. Jubayer et al. used the YOLOv5 model to detect molds on food surfaces and successfully identified the types of molds on food surfaces [14]. On the basis of the original YOLOv5 network model, Chen et al. added a new involution bottleneck module, which reduced the parameters and calculation amount, and introduced the SE module to improve the sensitivity of the model to channel features, establishing a plant pest identification model [15]. Qi et al. borrowed the human visual attention mechanism and added the squeeze-and-excitation module to the YOLOv5 model to achieve a key feature extraction [16]; the trained network model was evaluated on the tomato virus disease test set, and the accuracy rate reached 91.07%. Han et al. adopted the YOLOv5 model based on the flood filling method to achieve cherry quality detection [17].

This paper takes walnut impurities as the detection target. There is a high requirement for the real-time detection rate of impurities for the fast running speed of the walnut processing line [18]. The YOLOv5 model can maintain a higher detection accuracy, while maintaining a higher detection rate [19], so this paper chooses YOLOv5 as the detection model. However, the original YOLOv5 model is challenging to extract image features of impurities in walnut kernels under complex backgrounds. It is hard to detect small impurities such as broken shells, resulting in a low impurity recognition rate. In order to solve the above problems, we take the pursuit of a balance between detection performance and detection rate as the goal and improve the original YOLOv5 network, so that it can more accurately detect the impurities in the image without losing the detection rate. Firstly, a small target detection layer is added to the neck part to improve the model's ability to detect small impurities. Secondly, the Tans-E module is proposed to replace some of the convolution blocks in the original network. Thirdly, the CBAM module is added to improve the sensitivity of the model to channel features, which is convenient for finding prediction regions in dense objects. Finally, the GhostNet module is introduced to make the model lighter and improve the model detection rate.

### **2. Materials and Methods**
