# A Review of Convolutional Neural Network Applied to Fruit Image Processing
## 8 Deep Learning Frameworks and CNN-Based Examples
### CNN-Based Examples for Fruit Classification
#### 8.2.2 Example of Fruit Quality Classification  
For fruit quality classification, we used the Apple-NDDA dataset [104], which consists of 1110 apple images from defective and non-defective category. As the objective of the proposed examples is to show the use of CNN in the simplest way, in this case, we use the same CNN architecture shown in Figure 10, modifying the output layer and verifying that the input layer has adequate dimensions. Unlike the classification example, in this case, due to the fact that the dataset is smaller, we adopted a data augmentation solution aiming to increase the number of training samples and also reduce the overfitting. Thus, in order to generate image batches in real-time during the training process, we randomly applied the following variations of the training images:  
* Rotation in the range of \(\pm 10\) degrees.
* Width and/or height shifting of \(\pm 0.1\) of the image dimensions.
* Zoom the image in the range of \(\pm 0.1x\).
* Horizontal and/or vertical flipping.  
We train the network by 20 epochs with the same settings of optimizer and parameters of the first example, which are described in Table 4. As Apple-NDDA dataset [104] do not has separated training and testing subsets, we randomly divided the dataset in 80% for training and 20% for testing. Besides, we select the 10% of the training images for validation. Figure 14 depicts the average loss and accuracy curves during the model training process for 10-fold cross-validation. In both curves, after the 12 th-epoch, the loss values keep stable and close to 0, and the accuracy results are above 80% for training and vary for validation obtaining 81.57% at 20 th-epoch.  
We evaluate our model on 222 testing images. Figure 15 shows the activation maps obtained after the 3 rd convolution layer for Non-defect and Defect sample images, where it is noticeable how the defects are highlighted in the second case. After we evaluate the model on the testing images, we obtain an average accuracy of 81.25% with an average F1-score of 0.87. The confusion matrix for classification results is shown in Figure 16. It is worth highlighting that the accuracy for the Defect class is 90%. These results show that our model misclassifies apples without defects to a greater extent than apples with defects, which would be good for controlling poor quality. However, the final average accuracy is not good enough for a real system of fruit quality control. This is mainly due to the quality and size of the database.  
Figure 14: Curves of (**a**) average loss and (**b**) average accuracy during the model training for fruit quality classification.  
Aiming to compare the performance between the proposed example and other pre-trained models, we implemented the same solutions as in Section 8.2.1 based on transfer learning with the Imagenet dataset [64]. We include the most used models in the literature for fruit quality control (see Figure 8b), with the exception of the LeNet architecture. In order to provide a simpler solution, we adopt the same transfer learning strategy by freezing the convolutional base, adding one FC layer, and modifying the number of outputs. Moreover, the parameters of the training process were as in the initial example.  
In Table 6, we compare the results obtained by the proposed example and pre-trained models. It should be noticed that classification results are slightly improved by AlexNet, VGG16, and MobileNet. However, for deeper models such as InceptionV3 and ResNet50, the accuracy decreases due to the overfitting caused by a lack of data. The results show that more complex models are more appropriate for more complex tasks and data, instead of shallow architectures as the proposed.  
Figure 16: Confusion matrix for classification results of fruit quality on Apple-NDDA dataset [104].  
Figure 15: Examples of activation maps of the 3 rd convolution layer for two images from Apple-NDDA dataset [104].