# 2nd Place Solution for the CSC532 Fruits Categorical Classification 2024 Competition

Introduction
First of all, I would like to thank the competition-host for hosting this competition along with CSC532 course. It really helped us to apply the concepts that we learned from the class. This competition is to classify "fresh" vs "rotten" apples, bananas, and oranges. It looks like an elementary competition but the huge variation of data between training set and test set makes the competition challenging.
Overview of the Approach
I implemented a two-stage ensemble approach. As the name suggests, each image has to go through two stages of classification. Given an image,
Classify whether it is Apple or Banana or Orange
Classify whether it is Rotten or Fresh
I used an ensemble of models for both classification stages. The ensemble includes:
MobileNetV2
InceptionV3
Xception
InceptionV3
![Ensemble Model](assets/2e7ebd47e021ab95bf7d60cb.png)
Data Augmentation for Training
I applied the following augmentation to all fruits:
horizontal flip
vertical flip
samplewise center
zoom
width shift
height shift
rotation
Apples are the most challenging because of the variations in color due the type (Gala, Granny Smith, Delicious Red, etc). Among all apples, most of my models tend to misclassify fresh Gala apples as rotten ones due to its texture. Therefore, I set epochs to 25 for apples (10 for other fruits) and added Gaussian noise to apples using the following function:
():
    row, col, ch = img.shape
    mean = 
    var = 
    sigma = var ** 
    gauss = np.random.normal(mean, sigma, (row, col, ch))
    gauss = gauss.reshape(row, col, ch)
    noisy = img + gauss
     noisy
Pre-Processing for Submission
All of the images for submission go through background remover as pre-processing step to remove the noisy background. This is because of the difference between training and validation(submission) sets. The training images are sharp and clean, while the validation images are crowd-sourced images as mentioned in the competition data description.
The validation data was mostly crowdsourced from the participants of the DLAI5 hackathon.
First I used self-defined YOLO-based function to remove the background for submission images (special thanks to Khine Eaindray Htun for the amazing idea
[https://www.kaggle.com/code/khineaindrayhtun/segmentation-assisted-transfer-learning)](https://www.kaggle.com/code/khineaindrayhtun/segmentation-assisted-transfer-learning))
. However, self-defined function tends to remove the whole fruit in some cases as I discussed earlier in
[https://www.kaggle.com/competitions/csc532-fruits-categorical-classification-2024/discussion/483413](https://www.kaggle.com/competitions/csc532-fruits-categorical-classification-2024/discussion/483413)
. Therefore, I used an alternative way to do so by using rembg library. Based on my observations, it is safer to use rembg as it rarely misclassifies and removes the fruit.
Final submissions
Model
Private Score
Public Score
Two-stage model without Gaussian noise in Apples
0.77402
0.64384
Two-stage model with Gaussian noise in Apples
0.80527
0.59920
I believed in leaderboard overfitting (based on my Kaggler's instinct) and therefore, I didn't rely on public score a lot. As expected, a huge shakeup happened once the private score were released.
One of my non-final submissions could make me get the 1st place. However, I didn't select it as I lost track of its notebook.
Lesson learned : Always keep everything documented.
![](assets/b3899410fb5a2a6ab168084c.png)
Conclusion
The two-stage ensemble model proved to be effective, especially when Gaussian noise was added to the training images of apples to address their variability. The data augmentation techniques and pre-processing steps were crucial in improving the model's performance and generalization.
Overall, the competition was a great learning experience, enhancing my understanding of ensemble learning, pre-processing and data augmentation.
I've shared my two-stage model inference here :
[https://www.kaggle.com/code/kaungmyatkyaw/2-stage-ensemble-inference](https://www.kaggle.com/code/kaungmyatkyaw/2-stage-ensemble-inference)