Kaggle competition: Cassava Leaf Disease Classification
all_data.isnull().sum()<feature_engineering>
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import plotly.express as px
import os
import cv2
from PIL import Image
import keras
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import tensorflow as tf
from tensorflow.keras import models, layers
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from tensorflow.keras.applications import EfficientNetB0, Xception
from tensorflow.keras.optimizers import Adam
all_data['datetime']=pd.to_datetime(all_data['datetime'])
 all_data['year']=all_data['datetime'].dt.year
all_data['month']=all_data['datetime'].dt.month
all_data['day']=all_data['datetime'].dt.day
all_data['hour']=all_data['datetime'].dt.hour
all_data['dayofweek']=all_data['datetime'].dt.dayofweek
all_data=all_data.drop(columns='datetime' )<drop_column>
input_dir = ".. /input/cassava-leaf-disease-classification"
train_images_path = os.path.join(input_dir,"train_images")
 test_images_path = os.path.join(input_dir,'test_images' )
all_data=all_data.drop(columns=['month', 'day'] )<feature_engineering>
train = pd.read_csv('.. /input/cassava-leaf-disease-classification/train.csv')
 train.head(10 )
all_data['weekend']=(all_data['dayofweek']==5)|(all_data['dayofweek']==6 )<categorify>
image_list = train['image_id'].to_list()
label_list = train['label'].to_list()
season_encoded=pd.get_dummies(all_data['season'], prefix='season_')
 all_data=pd.concat(( all_data, season_encoded), axis=1)
 all_data=all_data.drop(columns='season' )<categorify>
with open(".. /input/cassava-leaf-disease-classification/label_num_to_disease_map.json")as f:
class_mapping = json.load(f)
 class_mapping2 ={int(k):v for k,v in class_mapping.items() }
class_mapping2
weather_encoded=pd.get_dummies(all_data['weather'], prefix='weather_')
 all_data=pd.concat(( all_data, weather_encoded), axis=1)
 all_data=all_data.drop(columns='weather' )<import_modules>
BATCH_SIZE =8
STEPS_PER_EPOCH = len(train)*0.8 / BATCH_SIZE
VALIDATION_STEPS = len(train)*0.2 / BATCH_SIZE
EPOCHS = 20
TARGET_SIZE = 350
from sklearn.decomposition import PCA<train_model>
train.label = train.label.astype('str')
 train_datagen = ImageDataGenerator(validation_split = 0.2,
rotation_range = 45,
zoom_range = 0.3,
horizontal_flip = True,
vertical_flip = True,
fill_mode = 'nearest',
shear_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
featurewise_center = True,
featurewise_std_normalization = True)
 train_generator = train_datagen.flow_from_dataframe(train,
directory = os.path.join('.. /input/cassava-leaf-disease-classification/train_images'),
subset = "training",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse",
shuffle= True)
 validation_datagen = ImageDataGenerator(validation_split = 0.2)
 validation_generator = validation_datagen.flow_from_dataframe(train,
directory = os.path.join('.. /input/cassava-leaf-disease-classification/train_images'),
subset = "validation",
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse" )
pca=PCA(n_components=1)
 pca.fit(all_data[['temp', 'atemp']])
 print(f" Variance explained after PCA : {pca.explained_variance_ratio_}" )<drop_column>
img_path = os.path.join('.. /input/cassava-leaf-disease-classification/train_images/1003442061.jpg')
 img = image.load_img(img_path, target_size =(TARGET_SIZE, TARGET_SIZE))
img_tensor = image.img_to_array(img)
 img_tensor = np.expand_dims(img_tensor, axis = 0)
 img_tensor /= 255.
 plt.imshow(img_tensor[0])
 plt.show()
all_data=all_data.drop(columns=['temp', 'atemp'] )<feature_engineering>
generator = train_datagen.flow_from_dataframe(train.iloc[17:18],
directory = os.path.join('.. /input/cassava-leaf-disease-classification/train_images'),
x_col = "image_id",
y_col = "label",
target_size =(TARGET_SIZE, TARGET_SIZE),
batch_size = BATCH_SIZE,
class_mode = "sparse")
 aug_images = [generator[0][0][0]/255 for i in range(10)]
fig, axes = plt.subplots(2, 5, figsize =(20, 10))
axes = axes.flatten()
for img, ax in zip(aug_images, axes):
ax.imshow(img)
 plt.tight_layout()
plt.show()
all_data.loc[all_data['windspeed']==0, 'windspeed']=all_data['windspeed'].mean()<train_model>
def create_model() :
conv_base = Xception(include_top=False, input_tensor=None,
pooling=None, input_shape=(TARGET_SIZE, TARGET_SIZE, 3), classifier_activation='softmax')
 model = conv_base.output
model = layers.GlobalAveragePooling2D()(model)
 model = layers.Dense(5, activation = "softmax" )(model)
 model = models.Model(conv_base.input, model)
 model.compile(optimizer = Adam(lr = 0.001),
loss = "sparse_categorical_crossentropy",
metrics = ["acc"])
 return model
Xtrain=all_data[:len(train)]
Xtest=all_data[len(train):]
Ytrain=np.log(Ytrain+1)
 print(Xtrain.shape, Ytrain.shape, Xtest.shape )<import_modules>
model = keras.models.load_model('.. /input/xception-best-weights/Xception_best_weights.h5' )
from lightgbm import LGBMRegressor
from sklearn.model_selection import cross_val_score<choose_model_class>
submission_file = pd.read_csv(os.path.join('.. /input/cassava-leaf-disease-classification/sample_submission.csv'))
submission_file
model=LGBMRegressor(boosting_type='gbdt', class_weight=None,
colsample_bytree=0.6746393485503049, importance_type='split',
learning_rate=0.03158974434726661, max_bin=55, max_depth=-1,
min_child_samples=159, min_child_weight=0.001, min_split_gain=0.0,
n_estimators=1458, n_jobs=-1, num_leaves=196, objective=None,
random_state=18, reg_alpha=0.23417614793823338,
reg_lambda=0.33890027779706655, silent=False,
subsample=0.5712459474269626, subsample_for_bin=200000,
subsample_freq=1 )<save_to_csv>
preds = []
for image_id in submission_file.image_id:
image = Image.open(os.path.join(f'.. /input/cassava-leaf-disease-classification/test_images/{image_id}'))
image = image.resize(( TARGET_SIZE, TARGET_SIZE))
image = np.expand_dims(image, axis = 0)
 preds.append(np.argmax(model.predict(image)))
 submission_file['label'] = preds
submission_file
<load_from_csv><EOS> 
submission_file.to_csv('submission.csv', index = False )