## 3 Proposed Deep-Learning-Based Defective Bean Inspection Scheme (DL-DBIS)
### Design of Defect-Sensitive Inspection Deep Network (DIDN)
For training a proper YOLOv3 model, the loss function is defined as follows [37]:  
\[Loss= \lambda_{coord}\sum_{i=0}^{S^{2}}\sum_{j=0}^{B}\mathds{1}_{ij}^{ obj}\left((X_{i,j}-\dot{X_{i,j}})^{2}+(Y_{i,j}-\dot{Y_{i,j}})^{2}\right)\] \[+\lambda_{coord}\sum_{i=0}^{S^{2}}\sum_{j=0}^{B}\mathds{1}_{ij}^{ obj}\left(\left(\sqrt{W_{i,j}}-\sqrt{\dot{W_{i,j}}}\right)^{2}+\left(\sqrt{h_{i,j}}- \sqrt{h_{i,j}^{*}}\right)^{2}\right)\] \[+\sum_{i=0}^{S^{2}}\sum_{j=0}^{B}\mathds{1}_{ij}^{obj}\left(- \log(\dot{p_{i,j}})\right)+\lambda_{noobj}\sum_{i=0}^{S^{2}}\sum_{j=0}^{B} \mathds{1}_{ij}^{noobj}\left(-\log(1-\dot{p_{i,j}})\right)\] \[+\sum_{i=0}^{S^{2}}\sum_{j=0}^{B}\mathds{1}_{ij}^{obj}\sum_{c\in classes }(-c\log(\dot{c})-(1-c)\log(1-\dot{c})), \tag{1}\]  
where \(\mathds{1}_{ij}^{obj}\) is an indicator function that determining whether \(obj\) is in cell \((i,j)\) and returning \(1\) if the condition holds; 0 otherwise, \(\lambda_{\blacksquare}\) are the pre-defined weighting values for each of the loss terms, \(S^{2}\) are the number of grids of an image used in YOLOv3, and \(B\) is the maximum number of bounding boxes in a grid. The HC transform is a feature extraction technique for detecting circles/eclipses from imperfect digital image inputs [38; 39]. Figure 6 shows the idea of the HC transform. In a two-dimensional space, a circle is expressed as follows:  
\[(x-a)^{2}+(y-b)^{2}=r^{2}, \tag{2}\]  
where \((a,b)\) is the center of the circle and \(r\) is the radius. If a point \((x,y)\) lies on the circle, then the parameters \((a,b,r)\) can be found according to Equation (2), and the three parameters form a three-dimensional Hough parameter space (see Figure 6b). And all the parameters \((a,b,r)\) that satisfy \((x,y)\) would lie on the surface of an inverted cone whose minimal point is at \((x,y,0)\). In the 3D Hough parameter space, the circle parameters can be determined by the intersection of multiple conic surfaces  
Figure 5: The DIDN architecture. Rectangles in light green are functional components, and ones in light blue are the data structures used in the associated components. YOLO = You Only Look Once; HC = Hough circle; GDV = grid-description vector; BBC = bounding box class. that are generated by all points of the 2D circle. In summary, the HCs provide bean outlines as the second references for precisely identifying beans. Two data structures used for data exchange inside the DIDN are presented as follows. A grid-description vector (GDV) shown in Figure 5, indicating prediction features of a grid partitioned by the YOLOv3 to an image, is a primitive data structure used in the components in the DIDN architecture. The GDV format includes two segments. The first segment, called the bounding box (BB) segment, contains five attributes: \(\hat{x},\hat{y},\hat{w},\hat{h},\hat{p}\), where \((\hat{x},\hat{y})\) indicates the center point, \(\hat{w},\hat{h}\) show the width and the height, respectively, and \(\hat{p}\) is the prediction confidence of the associated bounding box. The second segment, call class probability (CP) segment, contains probabilities of all classes. Let \(C\) be the number of classes. Then, the second segment contains \(C\) attributes, each of which stands for the prediction probability of the corresponding class. For example, 14 classes are used in our previous example in Figure 1, where class 0 stands for the normal beans and other classes 1\(\sim\)13 stand for different kinds of defective beans. For convenient study, the convention \(GDV_{i}(attribute)\) is used for accessing an attribute of \(GDV_{i}\). The bounding box class (BBC) is a data structure designed for maintaining classes of bounding boxes. The BBC also has two segments. The first segment is the bounding box (BB) segment, which is the same to the first segment of GDVs. The second segment is the class segment, which maintain the object class (i.e., label) of the bounding box. The BBC is the output format of the DIDN.