Using Machine Learning Approaches for Food Quality Detection
Food quality detection is an important method for ensuring food safety. Efficient quality detection methods can improve the efficiency of food circulation and reduce storage and labor costs. Traditional methods use instrumentation, testing reagents, or manual labor. These methods take a long time to detect, are time-consuming and labor-intensive, and require professionals to operate. Fruit, as a high-value food that provides essential nutrition for human beings, is susceptible to spoilage during packaging, transportation, and sales, so the freshness and safety assurance of fruit are a hot and difficult area of current research. Therefore, for the detection of fruit freshness, this paper proposes an efficient and nondestructive way to detect fruit freshness by using the machine learning algorithm convolutional neural network (CNN). This paper shows that convolutional neural networks have good performance in identifying the freshness of fruits through extensive experimental results and discusses the overfitting of machine learning based on the experimental results.
Food safety is the main factor affecting public health and social security  and the primary goal of food analysis . Especially in some underdeveloped countries, frequent food safety incidents can reduce consumer confidence, affect the normal order of the market, and even cause social problems, so issues related to food safety have attracted increasing social attention , and related research has become particularly important.
According to web research data (from ourworldindata.org), these kinds of fruits’ growth in production from 1961 to 2018 are shown in Table 1. The production of these fruits increased dramatically over 57 years, the average of the absolute change increased by about 240 million tons, and the average of the relative change increased by about 403%. With a dramatic increase in the production of fruits, fruit selection, transportation, and quality control have become an important issue.
Fresh fruits are an important part of the human diet and contain essential minerals, vitamins, and dietary fibers ; therefore, finding an efficient and nondestructive method to identify the quality of fruits has become a hot research direction in recent years. According to the current research, there are about three directions.
1.1. Based on the Supply Chain
Wang et al.  proposed a food traceability system, which not only enables quality tracking but also allows quality monitoring and information sharing in real time.
Pal and Kant  used big data collected by the Internet of things to quickly remove poor-quality food from the supply chain while reducing food waste and improving transportation efficiency.
Due to the increasing demand for fresh fruit, Elavarasi et al.  suggested that by using the Internet of things, fresh fruits can be monitored to avoid wastage during transportation.
Wu and Defraeye  researched thermal heterogeneity and associated differences in quality evolution for large collections of packed fruits and investigated the thermal behavior of an entire pallet of fruit throughout the entire cold chain. Therefore, three cold chain options were evaluated with the expectation of achieving the elimination of heat to improve the quality of cold chain transportation.
Dora et al.  adopted artificial intelligence in the food supply chain to improve transparency and traceability and address challenges in food safety, quality, and waste.
1.2. Based on Hyperspectral
Zhang et al.  developed an algorithm for detecting early rottenness in apples using a hyperspectral reflectance imaging system combined with spectral analysis and image processing, and chemometrics and pattern recognition methods were performed for spectral analysis in the spectral domain. Finally, the results with 98% overall detection accuracy were obtained.
Wang et al.  applied hyperspectral transmittance data combined with convolutional neural networks to detection of internal mechanical damage in blueberries and achieved an average accuracy and F1 score of 0.8844/0.8784 and 0.8952/0.8905, respectively.
Tao et al.  designed a low-cost, cloud-based, portable near-infrared (NIR) system and achieved an F1 score of 0.89.
1.3. Based on New Equipment
Weng and Neethirajan  reported portable microfluidic devices for food safety and quality control, which are used for the determination of food-borne pathogens (microorganisms), food allergens, biotoxins, heavy metal ions, and other chemical components that may be present in food.
Yousefi et al.  suggested implanting sensors into the packaging material of food products and monitoring microbial contamination in food products in real time using this sensor.
Alfian et al.  proposed a proposal for controlling temperature and humidity during food storage and transportation using radio frequency identification and Internet of things technology, which can track perishable foods.
Gonzalez-Viejo et al.  developed a low-cost e-nose and combined it with machine learning modeling to predict aroma in beer for food quality monitoring purposes.
The supply chain focuses more on improving traceability and fast delivery. Hyperspectral and new equipment are the methods to detect the quality of fruit. However, these three methods have drawbacks such as not touching end sales, having high investment costs, and needing specialized domain knowledge, and moreover, the hyperspectral imaging system is mainly composed of five parts: an array camera, a light splitter, a light source, a transmission mechanism, and computer software and hardware, so large datasets of hyperspectral images are difficult to establish. Therefore, this paper uses a convolutional neural network to classify fruit freshness RGB-images and confirm the freshness of fruits based on classification results.
Some damages or lesions on the surface of fruit are visible to the naked eye, and features can also be clearly reflected in RGB images. The convolutional neural network has been widely used as efficient and nondestructive fruit quality detection.
Azizah et al.  and Suistika et al.  studied CNN models for automatic classification of mangosteen and strawberry, respectively. Ciocca et al.  researched convolutional neural networks to classify food productions and studied CNN in food information retrieval. Hameed et al.  proposed that the classification of fresh produce, such as fruits and vegetables, has become a complex problem, and convolutional neural networks are considered a promising approach for its application. Research by JahanBakhshi et al.  shows that image processing as a nondestructive method plays a key role in the quality assessment of agricultural products and experimentally demonstrates that convolutional neural networks have significant advantages in processing sour lemon images.
These surveyed studies indicated that the types of fruits and some physical and chemical indicators of them will be reflected in the RGB image or spectral image. Deep learning methods showed better performance than traditional data analysis methods and deserve further study in the future for the quality detection of fruits.
2. Convolutional Neural Networks
The convolutional neural network is a kind of feedforward neural network that includes convolutional computation and has a deep structure. It is one of the representative algorithms of deep learning .
A typical architecture of the CNN model is displayed in Figure 1. CNN is one of the most popular neural network models for images, including convolutional layers, pooling layers, and fully connected layers . Convolutional layers can be understood as filters for image feature extraction, which is implemented by traversing input images with convolution kernel channels, kernel size, strides, padding, and other parameters, and these parameters are set and optimized according to the problem. Pooling layers are used for sampling (upsampling, downsampling, and average sampling), and we get more obvious features by sampling. Fully connected layers composed of fully connected neural units are used to generate numerical output for regression or classification problems.
In a typical CNN model, the feature is only extracted from an individual layer and not transported to the next layer, and features are concatenated from different layers. To achieve better results, residual deep residual networks (ResNets) and densely connected convolutional networks (DenseNets) are used in this paper.
2.1. Deep Residual Networks (ResNets)
In convolutional neural network, when deeper networks are able to start converging, a degradation problem has been exposed: with the network depth increasing, accuracy gets saturated and then degrades rapidly . This problem is called degradation.
He et al.  developed an improved version of CNN called ResNets. In ResNets, a deep residual learning framework to solve the degradation problem is proposed and can be realized by “shortcut connections” [23–25]. Figure 2 shows connections by simply performing identity mapping, and their outputs are added to the outputs of stacked layers; pure identity mappings are used as bypassing paths. It means that ResNets can improve their performance, provided depth is sufficient, and combine image features through summation before they are passed into a layer.
2.2. Dense Convolutional Networks (DenseNets)
Huang et al.  designed a model named dense convolutional network. In a feedforward fashion, Figure 3 shows that DenseNets concatenate feature maps learned by different layers, so for each layer, the feature maps of the input are the output of all preceding layers’ feature maps.
2.3. Evaluation Metrics and Parameters
In the machine learning field, almost all researchers use precision, recall, and the F1 score to evaluate models.
TP represents true positive; i.e., the number of positive samples which are classified correctly.
FP represents false positive; i.e., the number of negative samples which are classified incorrectly as positive ones.
FN represents false negative; i.e., the number of positive samples which are classified incorrectly as negative ones .
Table 2 shows parameters used in this paper's methods.
To train a good network, the dataset used in this paper was collected from Kaggle (kaggle.com) and other websites. All datasets are RGB images, containing three types of freshness (fresh, medium, and rotten), and six kinds of fruit (apples, bananas, cucumber, lemon, orange, and tomatoes). The number of these RGB images is more than 40,000, as shown in Table 3.
3.2. Three-Classification Experiment
This three-classification experiment aims to detect freshness without considering types of fruit. Therefore, the dataset was divided into three parts: fresh, medium, and rotten, 70% of all images are used for training, and the other 30% for testing. The results are shown in Figure 4.
Compared with other CNNs, DenseNets have several compelling advantages, such as adding dense blocks to enhance feature reuse and propagation, being easy to train due to improved flow of information and gradients, alleviating the vanishing gradient problem of the deep network, and reducing model parameters.
3.3. Eighteen-Classification Experiment
The eighteen-classification experiment aims to detect each kind of fruit whether it is fresh or not. Therefore, the dataset was divided into eighteen parts: fresh apple, medium apple, rotten apple, fresh banana, medium banana, rotten banana, fresh cucumber, medium cucumber, rotten cucumber, fresh lemon, medium lemon, rotten lemon, fresh orange, medium orange, rotten orange, fresh tomato, medium tomato, and rotten tomato, 70% of all images are used for training, and the other 30% for testing. The results are shown in Figure 5.
Table 4 shows an overview of recent articles where machine learning is adopted for fruit quality detection. As can be seen in the table, approaches used in this paper (ResNets and DenseNets) perform better than others. First, ResNets and DenseNets only use RGB images as the input data, and several other methods used hyperspectral, laser backscattering, or infrared videos to improve the evaluation precision of models. Second, other research studies have a binary classification issue, while the experiments in this paper increase classification numbers to three and eighteen, so the machine learning model needs to have a better classification effect. Last, this research lists key parameters and their values: precision, recall, and F1 score, and these values show this paper’s methods are better than others.
In the machine learning area, another key issue to evaluating a model whether it is good or not is overfitting. Overfitting is a modeling error in statistics that occurs when a function is too close to a finite set of data points. Therefore, the model is only applicable to its initial dataset and not to any other dataset.
Figure 6 shows the loss function values of ResNets and DenseNets for training and testing. These figures illustrate that ResNets and DenseNets do not show overfitting and have good performance on both the training and test sets.
5. Conclusions and Future Work
In this paper, we introduce two machine learning models (ResNets and DenseNets) to solve rapid and nondestructive detection of fruit. We selected a big dataset and conducted experiments within the dataset. From the results of the experiments, the key parameters and their values show that the models used are better than others and show consistent improvement in precision, recall, and the F1 score, without any signs of overfitting.
However, in any machine learning model, data imbalance is an important point, which means that the number of images for each class is not the same. It may result in classification bias towards the majority class. Therefore, our future research is to use the data augmentation technique and generative adversarial networks to solve data imbalance, and we hope that machine learning models could perform better than now. On the other hand, solving the limitation of RGB images, such as illumination and occlusion, is also our future research.
The data used to support the findings of this study are available from the corresponding author upon request.
Conflicts of Interest
The authors declare no conflicts of interest.
This work was supported in part by the National Natural Science Foundation of China under Grant 32101611, the Youth Project of Basic Research Program of Yunnan Province under Grant 202101AU070096, the Scientific Research Fund Project of the Yunnan Provincial Department of Education under Grant 2020J0239, and the Major Project of Science and Technology of Yunnan Province under Grant 202002AE090010 and 202002AD080002.
H. Yousefi, H.-M. Su, S. M. Imani, K.M. C. D. Alkhaldi, and T. F. Didar, “Intelligent food packaging: a review of smart sensing technologies for monitoring food quality,” ACS Sensors, vol. 4, no. 4, pp. 808–821, 2019.View at: Publisher Site | Google Scholar
X. Weng and S. Neethirajan, “Ensuring food safety: quality monitoring using microfluidics,” Trends in Food Science & Technology, vol. 65, pp. 10–22, 2017.View at: Publisher Site | Google Scholar
J. Wang, H. Yue, and Z. Zhou, “An improved traceability system for food quality assurance and evaluation based on fuzzy classification and neural network,” Food Control, vol. 79, pp. 363–370, 2017.View at: Publisher Site | Google Scholar
C. Leneveu-Jenvrin, F. Charles, F. J. Barba, and F. Remize, “Role of biological control agents and physical treatments in maintaining the quality of fresh and minimally-processed fruit and vegetables,” Critical Reviews in Food Science and Nutrition, vol. 60, no. 17, pp. 2837–2855, 2020.View at: Publisher Site | Google Scholar
A. Pal and K. Kant, “IoT-based sensing and communications infrastructure for the fresh food supply chain,” Computer, vol. 51, no. 2, pp. 76–80, 2018.View at: Publisher Site | Google Scholar
G. Elavarasi, G. Murugaboopathi, and S. Kathirvel, “Fresh fruit supply chain sensing and transaction using iot,” in Proceedings of the 2019 IEEE International Conference on Intelligent Techniques in Control, Optimization and Signal Processing (INCOS), pp. 1–4, IEEE, Tamilnadu, India, January 2019.View at: Google Scholar
W. Wu and T. Defraeye, “Identifying heterogeneities in cooling and quality evolution for a pallet of packed fresh fruit by using virtual cold chains,” Applied Thermal Engineering, vol. 133, pp. 407–417, 2018.View at: Publisher Site | Google Scholar
M. Dora, A. Kumar, S. K. Mangla, A. Pant, and M. M. Kamal, “Critical success factors influencing artificial intelligence adoption in food supply chains,” International Journal of Production Research, vol. 60, no. 14, pp. 4621–4640, 2021.View at: Publisher Site | Google Scholar
B. Zhang, S. Fan, J. Li et al., “Detection of early rottenness on apples by using hyperspectral imaging combined with spectral analysis and image processing,” Food Analytical Methods, vol. 8, no. 8, pp. 2075–2086, 2015.View at: Publisher Site | Google Scholar
Z. Wang, M. Hu, and G. Zhai, “Application of deep learning architectures for accurate and rapid detection of internal mechanical damage of blueberry using hyperspectral transmittance data,” Sensors, vol. 18, no. 4, Article ID 1126, p. 1126, 2018.View at: Publisher Site | Google Scholar
T. Wang, J. Chen, Y. Fan, Z. Qiu, and Y. He, “Seefruits: design and evaluation of a cloud-based ultra-portable nirs system for sweet cherry quality detection,” Computers and Electronics in Agriculture, vol. 152, pp. 302–313, 2018.View at: Publisher Site | Google Scholar
H. Yousefi, M. M. Ali, H.-M. Su, C. D. Filipe, and T. F. Didar, “Sentinel wraps: real-time monitoring of food contamination by printing dnazyme probes on food packaging,” ACS Nano, vol. 12, no. 4, pp. 3287–3294, 2018.View at: Publisher Site | Google Scholar
G. Alfian, M. Syafrudin, U. Farooq et al., “Improving efficiency of rfid-based traceability system for perishable food by utilizing iot sensors and machine learning model,” Food Control, vol. 110, Article ID 107016, 2020.View at: Publisher Site | Google Scholar
C. Gonzalez Viejo, S. Fuentes, A. Godbole, B. Widdicombe, and R. R. Unnithan, “Development of a low-cost E-nose to assess aroma profiles: an artificial intelligence application to assess beer quality,” Sensors and Actuators B: Chemical, vol. 308, Article ID 127688, 2020.View at: Publisher Site | Google Scholar
L. M. r. Azizah, S. F. Umayah, S. Riyadi, C. Damarjati, and N. A. Utama, “Deep learning implementation using convolutional neural network in mangosteen surface defect detection,” in Proceedings of the 2017 7th IEEE international conference on control system, computing and engineering (ICCSCE), pp. 242–246, IEEE, Penang, Malaysia, November 2017.View at: Google Scholar
R. Sustika, A. Subekti, H. F. Pardede, E. Suryawati, O. Mahendra, and S. Yuwana, “Evaluation of deep convolutional neural network architectures for strawberry quality inspection,” International Journal of Engineering and Technologies, vol. 7, no. 4, pp. 75–80, 2018.View at: Google Scholar
G. Ciocca, P. Napoletano, and R. Schettini, “Learning cnn-based features for retrieval of food images,” in Proceedings of the International Conference on Image Analysis and Processing, pp. 426–434, Springer, Lecce, Italy, May 2017.View at: Google Scholar
K. Hameed, D. Chai, and A. Rassau, “A comprehensive review of fruit and vegetable classification techniques,” Image and Vision Computing, vol. 80, pp. 24–44, 2018.View at: Publisher Site | Google Scholar
A. Jahanbakhshi, M. Momeny, M. Mahmoudi, and Y. D. Zhang, “Classification of sour lemons based on apparent defects using stochastic pooling mechanism in deep convolutional neural networks,” Scientia Horticulturae, vol. 263, Article ID 109133, 2020.View at: Publisher Site | Google Scholar
I. Goodfellow, Y. Bengio, and A. Courville, Deep Learning, MIT press, 2016.
L. Zhou, C. Zhang, F. Liu, Z. Qiu, and Y. He, “Application of deep learning in food: a review,” Comprehensive Reviews in Food Science and Food Safety, vol. 18, no. 6, pp. 1793–1811, 2019.View at: Publisher Site | Google Scholar
I. He, X. Zhang, S. Ren, and J. Sun, “Deep residual learning for image recognition,” in Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pp. 770–778, Las Vegas, NV, USA, June 2016.View at: Google Scholar
C. M. Bishop, Neural Networks for Pattern Recognition, Oxford University Press, Oxford, UK, 1995.
K. Kafadar, J. R. Koehler, W. N. Venables, and B. D. Ripley, “Modern applied statistics with S-plus,” The American Statistician, vol. 53, no. 1, p. 86, 1999.View at: Publisher Site | Google Scholar
B. D. Ripley, Pattern Recognition and Neural Networks, Cambridge University Press, Cambridge, UK, 2007.
G. Huang, Z. Liu, L. Van Der Maaten, and K. Q. Weinberger, “Densely connected convolutional networks,” in Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, pp. 4700–4708, Honolulu, HI, USA, July 2017.View at: Google Scholar
G. Kimutai, A. Ngenzi, R. N. Said, A. Kiprop, and A. Forster, “An optimum tea fermentation detection model based on deep convolutional neural networks,” Data, vol. 5, pp. 44–52, 2020.View at: Publisher Site | Google Scholar
H. Lee, M. S. Kim, H.-S. Lim, E. Park, W. H. Lee, and B. K. Cho, “Detection of cucumber green mottle mosaic virus-infected watermelon seeds using a near-infrared (nir) hyperspectral imaging system: application to seeds of the “sambok honey” cultivar,” Biosystems Engineering, vol. 148, pp. 138–147, 2016.View at: Publisher Site | Google Scholar
W. H. Su and D. W. Sun, “Potential of hyperspectral imaging for visual authentication of sliced organic potatoes from potato and sweet potato tubers and rapid grading of the tubers according to moisture proportion,” Computers and Electronics in Agriculture, vol. 125, pp. 113–124, 2016.View at: Publisher Site | Google Scholar
B. Zhang, J. Li, S. Fan et al., “Hyperspectral imaging combined with multivariate analysis and band math for detection of common defects on peaches (prunus persica),” Computers and Electronics in Agriculture, vol. 114, pp. 14–24, 2015.View at: Publisher Site | Google Scholar
F. J. Rodríguez, A. García, P. J. Pardo, F. Chavez, and R. M. Luque-Baena, “Study and classification of plum varieties using image analysis and deep learning techniques,” Progress in Artificial Intelligence, vol. 7, no. 2, pp. 119–127, 2018.View at: Publisher Site | Google Scholar
B. Mithun, S. Shinde, K. Bhavsar et al., “Non-destructive method to detect artificially ripened banana using hyperspectral sensing and rgb imaging,” Sensing for Agriculture and Food Quality and Safety X, vol. 10665, International Society for Optics and Photonics, Bellingham, WA, USA, 2018.View at: Google Scholar
A. Wu, J. Zhu, and T. Ren, “Detection of apple defect using laser-induced light backscattering imaging and convolutional neural network,” Computers & Electrical Engineering, vol. 81, Article ID 106454, 2020.View at: Publisher Site | Google Scholar
I. Munasingha, H. Gunasinghe, and W. Dhanapala, “Identification of papaya fruit diseases using deep learning approach,” in Proceedings of the 4th International Conference on Advances in Computing and Technology (ICACT‒2019), Faculty of Computing and Technology, University of Kelaniya, Kelaniya, Sri Lanka, February 2019.View at: Google Scholar
T.-T. Tran, J.-W. Choi, T.-T. H. Le, and J. W. Kim, “A comparative study of deep cnn in forecasting and classifying the macronutrient deficiencies on development of tomato plant,” Applied Sciences, vol. 9, no. 8, p. 1601, 2019.View at: Publisher Site | Google Scholar
W. Tan, C. Zhao, and H. Wu, “Intelligent alerting for fruit-melon lesion image based on momentum deep learning,” Multimedia Tools and Applications, vol. 75, no. 24, pp. 16741–16761, 2016.View at: Publisher Site | Google Scholar