(2) Comparison experiments between different models

In order to further verify the superiority of the proposed algorithm, the improved YOLOv5 algorithm was compared with several classical algorithms commonly used in the current deep learning field, including a single shot multibox detector (SSD) [33], a fast and superior generalization among One-Stage detectors, and YOLOv4 and YOLOv5s [34], which have better comprehensive performance. The comparison experiments selected accuracy, recall, mAP, and Fps as the evaluation metrics of each algorithm, and the models were trained and tested under the same initial conditions. The apple grading results are shown in Table 3 below.

**Figure 15.** Feature map visualization of the Im-YOLOv5 model. (**a**) CSP network output 56 × 56 feature map; (**b**) SPP network output 14 × 14 feature map.


**Table 3.** Comparison of different models.

As can be seen from the results in Table 3, the SSD model has lower accuracy and recall, with an average accuracy mAP of 0.789 and a real-time frame rate FPS of 34.78 for the Apple classification. As the model improves, its accuracy, recall, mAP, and FPS gradually increase, with the Im-YOLOv5 model having the highest mAP of 0.906, compared to the YOLOv5, YOLOv4, and SSD models by 14.8%, 11.1%, and 3.7%, respectively. The accuracy and recall of the grade-2 apple reached 0.806 and 0.751, respectively, which were 16.4% and 14.6% higher than the original YOLOv5 method. On the other hand, the real-time image frame rate of the Im-YOLOv5 method in this paper was improved, and the FPS of the improved model reached a maximum of 59.63, which has better real-time performance compared with the lightweight model YOLOv5s. The results show that the grading effect and real-time performance of the Im-YOLOv5 model proposed in this paper are better than those of the traditional deep learning model, proving the effectiveness of the proposed method.

### *3.2. System Solution Validation*

#### 3.2.1. Automatic Apple Grader Control System Set Up

The automatic apple grader designed and developed in this paper is shown in Figure 16, and its workflow is shown in Figure 17. When the automatic apple grader is started, the apples are lifted by a feeding and material handling lifting mechanism to the turnover detection conveyor. The turnover conveyor uses pairs of double conical sponge rollers to turn the apples. At this point, the automatic grading control system uses the improved YOLOv5 algorithm to grade the apples based on the surface information collected by the image acquisition device and sends the grading decision to the grading execution device [35,36]. The grading actuator automatically places the apples in the appropriate storage bin when they reach the appropriate grade based on the grading results assessed by the grading control system. The bins are equipped with cushioning material to reduce the impact of falling apples.

**Figure 16.** Physical view of the automatic apple grader.

**Figure 17.** Workflow diagram for automatic apple grading.

The hardware of the automatic apple grading control system includes a CCD industrial camera, IPC-610L industrial computer, PLC-1212 controller, AC contactor, inverter, and AC motor. The CCD industrial camera uses a GigE interface for data transmission and acquisition with an IPC-610L industrial computer. The industrial computer and PLC-1212 controller use the snap7 library to transmit information via a network cable. PLC controls the AC motor to drive a grading actuator through the AC contactor. The processor CPU of the IPC-610L is the same as that of the training computer, an Intel i7-9750k, an Intel i7-9750k with two graphics cards GTX1660Ti (6G), the operating system is Windows 10-x64, and the software environment is Python3.7, CUDA10.3, TIA Portal V15.1.

In order to facilitate debugging and observe the improvement effect of the model algorithm, the PyQt-based apple automatic grading control system software developed in this study is shown in Figure 18, which implements local video detection and real-time grading functions to achieve fast and accurate apple grading. The software designed in this paper sends the processed apple grade and location information to the TIA Portal V15.1 software through the snap7 library. After the grading actuator receives the apples in order, the grading operation is finally completed by the PLC controller in the corresponding grading lane [36,37].

#### 3.2.2. Results of the Grading Experiment

In order to verify the feasibility of the algorithm and the grading scheme of the apple automatic grading platform system in this paper, the designed and developed automatic apple grader was experimentally verified. One hundred apples of each quality grade were manually selected as samples, and the apple grades were determined based on the red Fuji GB/T 10651-2008 grading standard mentioned in Section 3.2. The experimental results are shown in Table 4.

**Figure 18.** Apple auto-grading software.

**Table 4.** Grading experimental data.


As can be seen from Table 4 of the experimental results, there was some grading error for Grade-1 and Grade-2 apples. Grade-1 apple sorting was 92% accurate, grade-2 apple sorting was 88% accurate, and grade-3 apple sorting was 100% accurate, with an average accuracy of 93%. The average classification accuracy for the three apple grades was 93% for the three apple grades, with an average classification speed of four apples/second. Both the real-time and accuracy rates are high enough to meet the grading requirements of small and medium-sized fruit farmers and to verify the effectiveness of the algorithm.

#### **4. Conclusions**

This paper proposes an improved apple grading model of YOLOv5, which better balances the grading accuracy and speed of apples, and also carries out experimental verification on the automatic apple grader designed and developed in this paper. The main conclusions of this study are as follows.

(1) In order to achieve more accurate apple grading and better real-time performance, the DIoU loss function and Mish loss function were chosen to replace the GIoU function and Relu activation function of the original algorithm model in terms of algorithm optimization, which improved the feature extraction capability and convergence speed of the model. The attention SE module is embedded in the Backbone structure to discard unnecessary features, which improves the training accuracy of the model without burdening the model. The experimental results show that the improved YOLOv5 has improved the average accuracy rate mAP by 3.1% compared to YOLOv5, 11% compared to YOLOv4, and 15% compared to SSD, and the real-time grading speed has reached 59.63 FPS, which is a large improvement in both the apple-grade grading accuracy rate and real-time performance. A portion of the improved YOLOv5 feature extraction layer was visualized to show the features extracted by different

convolutional layers, enhancing the interpretability of the apple grading model in this paper.

(2) An automatic apple grader was developed and designed, and the grading method in this paper was experimentally verified on an automatic apple grading machine platform. The experimental results showed that the grading accuracy of the grading method on the automatic apple grader reached 93%, with an average grading speed of four apples/sec. It has high accuracy and real-time performance, which can meet the grading needs of farmers and small and medium-sized enterprises in the field and has practical application in the apple grading industry.

**Author Contributions:** Conceptualization, B.X. and X.C.; methodology, W.J. and X.C.; software, X.C. and J.W.; validation, J.W.; formal analysis, X.C.; investigation, B.X.; data curation, X.C.; resources, B.X. and H.Y.; writing—original draft preparation, X.C.; writing—review and editing, W.J. and J.W.; visualization, B.X.; supervision, W.J.; project administration, W.J. and H.Y.; funding acquisition, B.X. and W.J. All authors have read and agreed to the published version of the manuscript.

**Funding:** This research was funded by the National Natural Science Foundation of China (No. 61973141 and 62173162), the Jiangsu agriculture science and technology innovation fund (No. CX(20)3059), and A Project Funded by the Priority Academic Program Development of Jiangsu Higher Education Institutions (No. PAPD-2018-87).

**Institutional Review Board Statement:** Not applicable.

**Data Availability Statement:** Not applicable.

**Conflicts of Interest:** The authors declare no conflict of interest.
