/crop_disease_classification

2022 business analytics term project

Primary LanguageJupyter Notebook

Business Analytics

This git repository is for 2022 term project of business analytics. Our task is the classification for plant and disease

Our theme is a project to enable farmers to diagnose pests to efficiently manage crops on smart farms. We make models using image data of plants and time series data of plants.


Method

Model

model
We ensemble Vision Transformer and LSTM by concatenating the final features that go through the Head, to handel multi -Image and Time Series- data both.

Data-Preprocessing

We split the source data and label.csv to train and test in data_preprocessing.ipynb

Next, to select the features that we use in our analysis, we aggregate the all values of our time-series data and calculate min, max of them.
The detail of it is in: https://github.com/dripdropdr/business_analytics/blob/main/plant_main.py#L32

def csv_features():
    # 분석에 사용할 feature 선택
    csv_features = ['내부 온도 1 평균', '내부 온도 1 최고', '내부 온도 1 최저', '내부 이슬점 평균', '내부 이슬점 최고', '내부 이슬점 최저']
    csv_files = sorted(glob('./data/*/*.csv'))
    temp_csv = pd.read_csv(csv_files[0])[csv_features]
    max_arr, min_arr = temp_csv.max().to_numpy(), temp_csv.min().to_numpy()

    # feature 별 최대값, 최솟값 계산
    for csv_path in tqdm(csv_files[1:], desc='feature minmax calculating'):
        
        ...
    
    return {csv_features[i]:[min_arr[i], max_arr[i]] for i in range(len(csv_features))}

Training Implementations

Epoch : 10, Batch size : 32
Loss function : Cross-entropy, Optimizer : Adam
NVIDIA RTX A4000 GPU, 32GB RAM, Ubuntu 18.04

Run model

Training


python plant_main.py --epochs {epoch} --batch_size {batch_size} --workers {data_worker} --resdir {output_directory}

Testing


python predict.py --batch_size {batch_size} --workers {data_worker} --resdir {output_directory}

When training Vision Transformer, we use jx_vit_base_p16_224-80ecf9dd.pth pretrained model on ImaegNet-21K We cannot find the pretrained model that trained with the crop & disease data which similar with our tasks, so we used this to get general features
To get pretrained model : https://www.kaggle.com/datasets/abhinand05/vit-base-models-pretrained-pytorch

Result

Train Result

f1-score
We use validation set to confirm the training status of our model in each epoch. The learning curve shows the fitting of our model.
Also, we employed F1-score macro for metric because this dataset are class imbalanced.
The detail of our dataset: https://github.com/dripdropdr/business_analytics/blob/main/EDA.ipynb

loss
This is the formulation of f1-score. In Imbalanced dataset, accuracy isn't reflected the performance of the model.
F1-score is the harmonic mean of precision and recall to represent the performance more correctly.



loss

We use Cross-Entropy Loss.

equation

softmax

label

f1-score


Cross-Entropy Loss usually used on Classification model. The class labels that are represented to one-hot vector is multiplied with the probability distribution that is output of the model

Test result & Ablation Study

Metric ViT+LSTM ViT only LSTM only
F1 score 0.9870 0.9655 0.7043
Precision 0.9875 0.9680 0.8001
Recall 0.9869 0.9650 0.7101

The results shows the fusion model that use time series data and image data both performed best result.
F1 score of LSTM only is relatively lower than ViT only. We assume the reason is that time series data, including environment information of the crops, didn't represent the features of the crop and disease directly than the image data.
Environment information may include about disease information of each crops, but cannot represent the information of the crop itself.

Data Description

Data : 57673
Train : Valid : Test = 7 : 1 : 2
Label : 20 classes

Distribution of classes

image

Label

'1_00' : '딸기_정상'
'2_00' : '토마토_정상'
'2_a5' : '토마토_흰가루병'
'3_00' : '파프리카_정상'
'3_a9' : '파프리카_흰가루병'
'3_b3' : '파프리카_칼슘결핍'
'3_b6' : '파프리카_다량원소결핍(N)'
'3_b7' : '파프리카_다량원소결핍(P)'
'3_b8' : '파프리카_다량원소결핍(K)'
'4_00' : '오이_정상'
'5_00' : '고추_정상'
'5_a7' : '고추_탄저병'
'5_b6' : '고추_다량원소결핍(N)'
'5_b7' : '고추_다량원소결핍(P)'
'5_b8' : '고추_다량원소결핍(K)'
'6_00' : '시설포도_정상'
'6_a11' : '시설포도_탄저병'
'6_a12' : '시설포도_노균병'
'6_b4' : '시설포도_일소피해'
'6_b5' : '시설포도_축과병'

Visualize images by class

image image image image image

Visualize environment variables by class

image image image image