# Automatic Fruits Freshness Classification Using CNN and Transfer Learning
## 3 Methodology
### SoftMax Classifier  
Softmax or multinomial logistic regression has a unique advantage to deal the N-dimensional vectors. The Softmax is widely used in diverse fields including deep learning for various objects classification [22]. The Softmax classifier determines the probability of extracted vectors for classification. For the same data set, it gives the sum of the probability equal to 1 for all vectors as indicated by Equation (3).  
\[f_{j}(z)=\frac{e^{zj}}{\sum_{k}e^{zk}}, \tag{3}\]  
where \(z\) is the input vector obtained from fine-tuned network in previous stage. These results are mapped to the probability domain from the exponential domain. We select Softmax loss function for fruits classification. Our study indicates that this Softmax function has good performance and converges quickly. Mathematically, the Softmax loss function is modelled as shown in Equation (4).  
\[Loss_{Softmax}=\frac{1}{N}{\sum_{i}}-log\frac{e^{zyi}}{\sum_{j}e^{zj}}, \tag{4}\]  
where \(N\) represents the output value of last fully connected layer of the correct class \((yi)\). Moreover, \(zj\) is the output value of the last fully connected layer of the \(jth\) class.  
Equations (3) and (4) indicate that the \(Loss_{So_{ftimax}}\) function uses the data from correct labels and maximizes the possibility of data. Meanwhile, it also ignores the information from the prevailing incorrect labels. Algorithm 1 shows the pseudo cod of our developed method. Algorithm 1 indicates that the fruits images, which are obtained from the Kaggle dataset, are pre-processed as described in the above section. In Algorithm 1, from lines (2) to (8) the gathered data is processed, which is later fed to the deep learning models. The data processed in initial stage is now processed by the AlexNet model as indicated in lines (9) to (15). During the AlexNet operation, all the eight layers are used. For first five convolution layers, the ReLU activation function is used. The other layers are utilized as described in above section. From lines (15) to (20), transfer learning of the CNN is performed. Meanwhile, the CNN is fine tuned in which epochs, batch sizes, and learning rate is set as shown in lines (18) and (19) of Algorithm 1.  
```
1. Input: Obtain colored fruits images \(\blacktriangleright\) Data Collection from Kaggle
2. do:
3. Process collected data obtained in step (1). \(\blacktriangleright\) Initial stage.
4. Perform pre-processing operations as listed in Section 3.2.
5. Process uni-channel images and make uniform image resizing.
6. Perform image augmentation and image labeling.
7. Perform data manipulation. \(\blacktriangleright\) Isolate into validation, train, and test set.
8. end
9. begin
10. Load AlexNet model
11. Activate 8-layers including 5 conv layers and 3 fully connected layers.
12. Use ReLU for first 5 conv layers.
13. Process and utilize 8-layers as depicted in Section 3.4.
14. end
15. begin
16. Transfer learning of the CNN
17. Apply fine tuning and set the parameters as:
18. Epochs = 10 and Batch size = 32,
19. Set the learn rate = \(10^{-5}\)
20. end
21. Apply softmax classifier:
22. Use Equations (1) and (2) for classification tasks.
23. Output: Final classification result:
24. Fresh fruit or Rotten fruit
```  
**Algorithm 1**Pseudocode of fruits freshness classification method  
In the final stages, the softmax classifier is used to predict the final status of the fruit. It will be shown in next section that our developed method is robust and accurately classifies the fruit condition instantly. Moreover, the steps shown in Algorithm 1 are simple and easy for readers to follow. In section below, we detail our findings along with useful images. In addition, we also discuss our observations during algorithm development stages.