Implementation of a Convolutional Neural Network Algorithm in Classifying Vegetable Freshness Based on Image

The purpose of this work is to apply CNN algorithm to a real problem of vegetable freshness identification using image data. Quantitative approach was used for this study and the data source was obtained from Kaggle; it is referred to as Fresh and Stale Images of Fruits and Vegetables with 2,604 images, four categories in total. The CNN model architecture consisted of a basic organization of four successive convolutional layers with associated max-pooling layers that aimed at capturing hierarchical feature representations of the input images. This model was trained using the Adam’s optimizer for 20 iterations with the batch size of 32. Pre-processing of data included image augmentations such as scaling, rotation, flipping which improved the performance of the model. The assessment was done using Confusion Matrix approach and the results show that the proposed system achieved an accuracy of 95%, with a precision of 94%, recall of 93% and F1-score of 93%. From this it can be concluded that the CNN model proposed has achieved the objective of distinguishing fresh and non-fresh vegetables with enough precision to assist in the automation of quality control in agriculture. The conclusion that can be drawn from this study is that AI especially CNNs could be of big help in increasing accuracy and decreasing human factors in the large scale production of food.


Introduction
Freshness vegetable is very important factor in guard quality, taste and benefits nutrition from vegetables.When fresh vegetables are consumed, we can feel tenderness, richer taste, and gain benefit optimal nutrition.Apart from that, fresh vegetables also provide life and energy new to in life We.For guard freshness vegetable, important For choose fresh vegetables with signs quality like color bright, firm texture, and fresh aroma.Not a little Difficult buyers differentiate type, type, and freshness vegetable.So, buyers only follow instruction from seller (Lubis, 2020).
Artificial Intelligence (AI) especially technology machine learning, has interesting attention big in a number of year final with aim For possible computer For simulate intelligence human and helpful handle tasks in the real world.Learning process specific and detailed known as Deep Learning.Deep Learning is a learning process that uses algorithms that refer to law the math works like brain.Deep Learning used For various type work like predict opportunity or events, recognize object or object, and also can diagnose disease (Xie et al., 2021).Deep learning own a number of one method Convolutional Neural Network (CNN) (Efendi et al., 2024).Convolutional Neural Networks can used or implemented For introduction image with rivaling accuracy humans on a certain dataset and Convolutional Neural Network can learn ISSN: 2716-3865 (Print), 2721-1290 (Online) Copyright © 2024, Journal La Multiapp, Under the license CC BY-SA 4.0 type features that produce more accuracy high and therefore use it in the classification process ( Paraijun et al., 2022) .
Convolutional Neural Network (CNN) incl in kind of deep learning because depth the network (Ketkar et al., 2021).Deep learning is branch from machine learning that can teach computer For do work appropriately human, like computer can Study from the training process.CNN is operation combining convolutions a number of layer processing , using a number of operating elements in a way parallel and inspired by the system nerve biological (Romario et al., 2020).Development Technology Continuous current information develop, can help overcome various problem (Chen, 2017).Because related matters with progress Technology Information has spread to almost all over layer Indonesian society.One of them can used in matter classification freshness vegetable.Where is one the problem that is in classify freshness vegetables, vegetable distributor need system that can classify freshness vegetable with amount Vegetables are plentiful and needed fast time (El-Ramady et al., 2015).
In Indonesia itself quality vegetable national seen from side consistency size, color, and freshness Still Far from hope consumer Because many vegetables that don't can classified its freshness in a way whole (Paraijun et al., 2022).According to Hidayat & Lusiana (2022), CNN was used For classify type vegetables based on features that have been extracted.After through layer convolution and pooling, features the submitted to layers connected full (fully connected layers) which allows the model to learn connection between features these and categories the right vegetables.This allows the model to predict with accurate type vegetables from given image.

Based on description above, research This aim For apply algorithm Convolutional Neural
Networks in classification freshness in the vegetables represented in something pictures, and for prove level success method Convolutional Neural Networks in do classification object.Researcher hope that with exists study this, got it lighten up work public in introduction quality vegetable based on image with big amount For know freshness vegetable is vegetable is it fresh or not fresh (Paraijun et al., 2022).

Methods
This research work adopted the quantitative research design whereby the primary data source was in the form of numbers, statistics, and quantitative measures to investigate the classification of vegetable freshness with the CNN algorithm.The research design and methodology was well guided to enable smooth running of the research process through data collection and preprocessing, model implementation, and evaluation.The data for this study were collected from Kaggle site and the data is called Fresh and Stale Images of Fruits and Vegetables.It contains 12 different classes of fruit and vegetable images and from that, 4 classes were chosen for the best performance of the system.The selected classes were bitter gourdraw and cooked and capsicumraw and cooked.The training dataset included 2082 images while 522 images were used in the testing dataset and the complete dataset was 2604 images.This division of data was made in a systematic way so as to maintain proportionality of each class as the models were being trained and tested.This preprocessing was used in enhancing the image data in readiness for the model training exercise.The images were then adjusted to a standard target dimension of 150x150 pixels so as to have a uniform input size.Several augmentations were performed to improve the model performance by feeding it with different images hence making it more adaptive.The specific augmentation parameters she found is shown in the Table 1 below

Horizontal Flip
Images were randomly flipped horizontally to increase variability in the dataset.

Fill Mode
Nearest neighbor interpolation was used to fill in any missing pixels created during transformations.
CNN was used by establishing an architecture that followed a deep learning format comprised of four convolutional layers and subsequently followed by max-pooling layers.The convolutional layers were utilized to obtain the hierarchical features from the input images and the feature maps were downsampled by the max-pooling layers to reduce computational cost and to avoid overfitting.CNN architecture details are shown in Table 2: CNN was set up by designing an architectural model that was inherently a deep learning model consisting four convolutional layers that in turn were followed by max-pooling layers.
An application of the convolutional layers was used to obtain the hierarchical features of the input images and the feature maps are downsampled by the max-pooling layers to reduce the computations and to prevent overfitting.The CNN architecture of details are presented in table two below.The data was formed by inserting a random shift into training data and evaluated by performing a random crop into the training data; the mini-batches with 32 samples for the training data were made by the Adam optimizer; the total numbers of epochs for the training and validation data were set as 20.The specifically chosen optimizer is known as the Adam optimizer namely for the convenience that originates in the capacity to automatically manage the learning rates during the training process, thus giving a boost to convergence and enhancing the overall performance.For the purpose of evaluation, such fundamental where there are 4 classes type vegetable where the total number of vegetables that will be tested is 2604 vegetable images to see the images of the vegetables being tested as following:

Pre -processing
In this research, the pre-processing process was carried out by randomly dividing the raw/original vegetable image data into two parts, 80% for training data and 20% for testing data.The raw data used totaled 2604 images.Thus, 2082 images were used as training data, while the remaining 522 images were used as testing data.This random distribution aims to ensure that the model developed has sufficient data for learning and sufficient data for testing and evaluating the model's performance in a representative manner (Efendi, 2024).Following This is division of training data and testing data used in this research : After dividing the training and testing data, the data will be processed to implement the use of Tensorflow/Keras to load image data from the directory and use it as input for training and testing the model.

Convolutional Neural Network Modeling
After the preprocessing process is carried out, the image data will then be processed in the convolutional neural network model by going through 2 stages, including:

Feature Learning
Stages First from this CNN model is feature learning, in research This this learning feature itself Alone consists of 2 parts namely convolutional layers and max pooling layers.Following This is the code used For perform feature learning:

Convolutional
Layer 2 In convolutional layer 2, the input data will be processed in Convolutional layer 2 originate from max pooling layer 1, in convolutional layer 2 is image data will return done processing with using a filter of 64, the kernel size is 3 x 3 matrix , and using stride size of 0 .Apart from that, convolutional layer 2 this will too used function Activation form ReLU .

Max Pooling layer 4
After passes through convolutional layer 3, then image data will return processed in max pooling layer 3 with the goal is that the CNN model can understand form image which will processed.Based on the vegetable image classification code in Figure 8, it can be explained as follows:

Process Name Information
Flatten Flatten layers are used For make input that has Lots dimensions become A vector, so that later results from flatten layer can used as mark input from fully connected layer.

Dense
After fully connected layer has input data, next process is connected input that is on the flatten layer to in layer classification, with use as dense as 128 units and function Activation that is ReLU.Then at the final stage of classification because there are 4 classes to be tested, the output layer is a softmax function Activation softmax will displays results based output probability biggest from results classification.

Dropouts
Before enter output layer, moreover formerly results from fully connected layer will pass dropout, dropout this Alone used For delete overlifting occurs happen during the fully connected layer process.

Model Evaluation
Based on results tests carried out from the Convolutional Neural Network model on image data of fresh and non-fresh vegetables , obtained results for training data as following:

Confusion Matrix
After we train the model, then we evaluate the performance of the model on the test set.
Evaluation is done to get the probability of failure of an image object that is read in the classification process, then the accuracy value with the highest probability will be obtained from the entire test model.The obtained high accuracy also proves that the application of CNNs, which incorporate the mechanism of hierarchical feature extraction, is relevant for tasks connected with image classification in the sphere of agriculture.This finding is consistent with other current studies indicating that deep learning is efficient in agriculture thus improve precision and automation of quality control.This brings operability CNNs help to reduce human errors and labor costs which are paramount in large-scale food production and distribution by facilitating the assessment of the freshness of produces in question.
However, this study's use of CNNs demonstrates AI's capacity to be incorporated into the vast concept of smart agriculture.Recent developments in AI and ML have indicated that such technologies can improve the living standards and the productivity of agriculture through efficiency in the usage of such resources and reduction of wastage (Gill et al., 2022;Zhang & Aslan, 2021;Cioffi et al. 2020;Li et al., 2021).Accordingly, the practical contribution of the current study goes far beyond the particular context of vegetable classification and can be viewed as the conceptualisation of how the design of an informative space of AI models may provide solutions for decision-making agents in the agricultural sector.
The first and major advantage of this study relates to the methodological part, including preprocessing and data augmentation of image data.Besides, when implemented the model scaling, rotation and flipping techniques not only improved its ability to generalize from previous training data but also helped train a more robust CNN on images that were filtered distorted in various ways.This kind of approach is in line with general practice in deep learning, wherein authors tend to make use of data augmentation so as to emulate actual use and reducing overfitting (Zhou et al., 2021;Chaudhari et al., 2021;Kiyasseh et al., 2021).
ISSN: 2716-3865 (Print), 2721-1290 (Online) Copyright © 2024, Journal La Multiapp, Under the license CC BY-SA 4.0 However, the following are the limitations of the study.The given dataset, though good enough for the purposes of the current research, is not diverse enough.The consideration of only two vegetable varieties, namely the bitter gourd and the capsicum, also leads to query the model's applicability to other kinds of produce.This can be a serious limitation especially since there are so many types of vegetables and fruits which may have different colours and textures.has rightly pointed out that how effective CNN models are in the applications of agriculture all depends with the data used for training these models.Future research should, therefore, try to use a wider variety of produce to ensure that the model can be applied in different conditions because of its usefulness (Alzubaidi et al., 2021;Li et al., 2021;Taye, 2023;Kattenborn et al., 2021;Salehi et al., 2023).
However, there are two conspicuous drawbacks regarding the stimulus materials; the first is that the images were acquired in a highly controlled manner.Though, the given work successfully showcased the viability of the model, real-world applications would prove much more complex and include such factors as inconsistent illumination, differences in background, and quality of the captured image (Eller et al., 2022;Elhanashi et al., 2023;Akande et al, 2022;Zhu et al., 2024).These factors could go a long way in affecting the model in question.According to Shaikh et al. (2022), to be practical in farming AI models, they should be checked beneath variable environmental situations.Therefore, future research should focus on applying the model under different situations with the goal of establishing its practical usability.

Conclusion
The image classification process in this study researchers separated 2 types of fresh and nonfresh vegetable categories, then randomly separated the raw dataset from 2064 vegetable images into 80% training and 20% testing.Then enter data preprocessing, data augmentation used in the form of scaling, rotation, translation, and flipping.Then enter the CNN model stage, then the researcher compiles the model with Adam's optimizer and then trains the training model, after entering the training model stage, the researcher evaluates the model with a graph plot and confusion matrix.On research This is the model process that occurs in CNN algorithm consists of 2 parts, namely feature learning and classification.The data has been done feature extraction will process at convolution layer 1, after goes through the feature learning process at convolutional layer 1, then processes the image data will done processing back in the max pooling layer aiming for zoom out image data size, so that the CNN model is designed can Can understand more details regarding data on fresh and non-fresh vegetables.The feature learning process is carried out until 4 layer, after pass feature learning then image will changed become A vector in flatten layer with aim for results from feature learning can used as mark input on stage classification.To measure loss and accuracy performance, training and testing of vegetable image datasets using the CNN model is quite good.Based on the CNN model designed, using the number of batch_size as much as 32 and a total of 20 epochs, the accuracy of fresh vegetables and non-fresh vegetables is 93% with a loss of 0.2399.The test results using the Confusion Matrix method obtained an accuracy value of 95%, an average precision value of 94%, an average recall value of 93%, and an f1-score value of 93%.

Figure 6 .
Figure 6.Code for Vegetable Data Augmentation and Feature ExtractionBased on code feature extraction on rass data, it can be explained as follows:

Figure 7 .
Figure 7. Code in Feature Learning Following This explanation about code For learning features:Table 7. Explanation of the Code in Feature Learning

Figure 8 .
Figure 8. Code at the Vegetable Image Classification Stages

Figure 9 .
Figure 9. Train Data Evaluation Results Based on results training data training the so can We shown in the plot/ graph of course in Figure 10 below.

Figure 10 .
Figure 10.Accuracy and Loss Data Train Plot From figure 10, we can analyze exists connection between mark accuracy and loss value on training data with number of epochs or iterations carried out.Observed relationship to grades accuracy show correlation positive, which means that the more Lots number of epochs used in training, grades accuracy of training data tends increase.On the other hand, the loss value shows correlation negative with number of epochs, meaning the more many epochs are used, the loss value in the training data tends to be decrease.Based on observation this, got it concluded that for reduce desired loss value, add number of epochs in the training process Can be one solution effective.
The classification results obtained from training data and test data are shown in Figure11showing the prediction results for training data.Vegetable images that were successfully classified were all Fresh Bitter Gourd images, namely 61 out of 64 images, Fresh Capsicum images that were successfully classified were 197 out of 206 images used, Satle Bitter Gourd ISSN: 2716-3865 (Print), 2721-1290 (Online) Copyright © 2024, Journal La Multiapp, Under the license CC BY-SA 4.0 images that were successfully classified were 60 out of 72 images used and Stale Capsicum images that were successfully classified were 178 out of 180 images.

Figure 11 .
Figure 11.Confusion Testing Data Matrix From the confusion matrix can obtained results form mark predictions recall and precision of the CNN model, below This results accuracy , recall and precision of vegetable data : Accuracy From the results of the confusion matrix, the overall average accuracy results are:  =  +   +  +  +  x 100%  = 61 + 197 + 60 + 178 61 + 197 + 60 + 178 + 1 + 2 + 9 + 12 + 2 x 100%  = 95% Recall From the results confusion matrix can is known results recall , which is where mark this recall used For know how much the precision of the model when matched return with use different image .Following This results calculation mark recall for data on fresh vegetables and nonfresh vegetables : Fresh Bitter Gourd  =   +   = 61 61 + 1 + 2  = 0.953

Figure
Figure 12.Classification Results

Table 2 .
CNN Model Architecture

Table 3 .
Model Training and Evaluation Parameters

Table 4 .
Vegetable Image Data

Table 6
. Explanation of Code for Augmentation and Feature Extraction of Vegetable Data Process Name Information rescale rescale will do image data normalization becomes 0 -1, with method share image data with amount maximum The RGB color pixel size is 255.rotation_range Augmentation rotation_range This used For rotate picture in a way random with mark rotation Can from 0 ˚ -180 ˚.However in study This rotation value used that is 4 0. So Later picture will be rotated in a way random from (-4 0˚) to 4 0˚.ISSN: 2716-3865 (Print), 2721-1290 (Online) Copyright © 2024, Journal La Multiapp, Under the license CC BY-SA 4.0

Table 8 .
Explanation of Vegetable Image Classification Stages

Table 9 .
Accuracy and Loss Results with Adam Optimization on Training Data training data testing using Adam optimization, with an epoch size of 20 epochs, and a batch size of 32 batches, the accuracy was 93% and the loss was 0.2399.ISSN: 2716-3865 (Print), 2721-1290 (Online) Copyright © 2024, Journal La Multiapp, Under the license CC BY-SA 4.0