{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-20T02:05:44.233261Z","iopub.execute_input":"2024-06-20T02:05:44.234306Z","iopub.status.idle":"2024-06-20T02:06:02.736820Z","shell.execute_reply.started":"2024-06-20T02:05:44.234251Z","shell.execute_reply":"2024-06-20T02:06:02.735999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport cv2\nimport os\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as  plt\nimport tensorflow as tf\nfrom tensorflow.keras import layers,models,optimizers\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout,Flatten\nfrom sklearn.metrics import accuracy_score\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img,img_to_array\nfrom tensorflow.keras.applications import VGG16,VGG19\nimport warnings\nwarnings.simplefilter('ignore')\nfrom PIL import Image\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:02.738749Z","iopub.execute_input":"2024-06-20T02:06:02.739068Z","iopub.status.idle":"2024-06-20T02:06:16.582833Z","shell.execute_reply.started":"2024-06-20T02:06:02.739041Z","shell.execute_reply":"2024-06-20T02:06:16.581844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the Dataset","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:16.584034Z","iopub.execute_input":"2024-06-20T02:06:16.584550Z","iopub.status.idle":"2024-06-20T02:06:16.630639Z","shell.execute_reply.started":"2024-06-20T02:06:16.584524Z","shell.execute_reply":"2024-06-20T02:06:16.629699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:16.631718Z","iopub.execute_input":"2024-06-20T02:06:16.632024Z","iopub.status.idle":"2024-06-20T02:06:16.647240Z","shell.execute_reply.started":"2024-06-20T02:06:16.631999Z","shell.execute_reply":"2024-06-20T02:06:16.646212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(df,x='label')","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:16.650784Z","iopub.execute_input":"2024-06-20T02:06:16.651148Z","iopub.status.idle":"2024-06-20T02:06:16.936442Z","shell.execute_reply.started":"2024-06-20T02:06:16.651121Z","shell.execute_reply":"2024-06-20T02:06:16.935387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nwith open('/kaggle/input/cassava-leaf-disease-classification/label_num_to_disease_map.json') as file:\n    print(json.dumps(json.loads(file.read()), indent=4))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:16.937538Z","iopub.execute_input":"2024-06-20T02:06:16.937797Z","iopub.status.idle":"2024-06-20T02:06:16.946304Z","shell.execute_reply.started":"2024-06-20T02:06:16.937773Z","shell.execute_reply":"2024-06-20T02:06:16.945275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"sample = df[df.label == 0].sample(9)\nplt.figure(figsize=(12,12))\n\nfor ind, (image_id,label) in enumerate(zip(sample.image_id,sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = load_img(os.path.join('/kaggle/input/cassava-leaf-disease-classification/train_images',image_id),color_mode='rgb',target_size=(224,224))\n    image = img_to_array(image)\n    image = image/255.\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:16.947504Z","iopub.execute_input":"2024-06-20T02:06:16.947845Z","iopub.status.idle":"2024-06-20T02:06:17.909162Z","shell.execute_reply.started":"2024-06-20T02:06:16.947820Z","shell.execute_reply":"2024-06-20T02:06:17.908019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = df[df.label == 1].sample(9)\nplt.figure(figsize=(12,12))\n\nfor ind, (image_id,label) in enumerate(zip(sample.image_id,sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = load_img(os.path.join('/kaggle/input/cassava-leaf-disease-classification/train_images',image_id),color_mode='rgb',target_size=(224,224))\n    image = img_to_array(image)\n    image = image/255.\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:17.910520Z","iopub.execute_input":"2024-06-20T02:06:17.910842Z","iopub.status.idle":"2024-06-20T02:06:18.977411Z","shell.execute_reply.started":"2024-06-20T02:06:17.910815Z","shell.execute_reply":"2024-06-20T02:06:18.976069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = df[df.label == 2].sample(9)\nplt.figure(figsize=(12,12))\n\nfor ind, (image_id,label) in enumerate(zip(sample.image_id,sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = load_img(os.path.join('/kaggle/input/cassava-leaf-disease-classification/train_images',image_id),color_mode='rgb',target_size=(224,224))\n    image = img_to_array(image)\n    image = image/255.\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:18.979056Z","iopub.execute_input":"2024-06-20T02:06:18.979429Z","iopub.status.idle":"2024-06-20T02:06:19.937559Z","shell.execute_reply.started":"2024-06-20T02:06:18.979400Z","shell.execute_reply":"2024-06-20T02:06:19.936206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = df[df.label == 3].sample(9)\nplt.figure(figsize=(12,12))\n\nfor ind, (image_id,label) in enumerate(zip(sample.image_id,sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = load_img(os.path.join('/kaggle/input/cassava-leaf-disease-classification/train_images',image_id),color_mode='rgb',target_size=(224,224))\n    image = img_to_array(image)\n    image = image/255.\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:19.938822Z","iopub.execute_input":"2024-06-20T02:06:19.939173Z","iopub.status.idle":"2024-06-20T02:06:20.909163Z","shell.execute_reply.started":"2024-06-20T02:06:19.939144Z","shell.execute_reply":"2024-06-20T02:06:20.908134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = df[df.label == 4].sample(9)\nplt.figure(figsize=(12,12))\n\nfor ind, (image_id,label) in enumerate(zip(sample.image_id,sample.label)):\n    plt.subplot(3,3,ind+1)\n    image = load_img(os.path.join('/kaggle/input/cassava-leaf-disease-classification/train_images',image_id),color_mode='rgb',target_size=(224,224))\n    image = img_to_array(image)\n    image = image/255.\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:20.910419Z","iopub.execute_input":"2024-06-20T02:06:20.911240Z","iopub.status.idle":"2024-06-20T02:06:22.222890Z","shell.execute_reply.started":"2024-06-20T02:06:20.911204Z","shell.execute_reply":"2024-06-20T02:06:22.221675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df.iloc[:20000,:]\ntest_df = df.iloc[20000:,:]","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:22.224226Z","iopub.execute_input":"2024-06-20T02:06:22.224585Z","iopub.status.idle":"2024-06-20T02:06:22.230013Z","shell.execute_reply.started":"2024-06-20T02:06:22.224555Z","shell.execute_reply":"2024-06-20T02:06:22.228981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augumentation","metadata":{}},{"cell_type":"code","source":"train_df['label'] = train_df['label'].astype('str')\ngen = ImageDataGenerator(\n    horizontal_flip = True,\n    vertical_flip = True,\n    shear_range = 0.2,\n    zoom_range = 0.2,\n    rescale = 1/255.,\n)\ntrain_datagen = gen.flow_from_dataframe(\n    train_df,\n    directory = \"/kaggle/input/cassava-leaf-disease-classification/train_images\",\n    batch_size = 32,\n    target_size = (224,224),\n    seed = 42,\n    x_col = 'image_id',\n    y_col = 'label',\n    class_mode = 'categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:22.231319Z","iopub.execute_input":"2024-06-20T02:06:22.231715Z","iopub.status.idle":"2024-06-20T02:06:30.984176Z","shell.execute_reply.started":"2024-06-20T02:06:22.231676Z","shell.execute_reply":"2024-06-20T02:06:30.983335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['label'] = test_df['label'].astype('str')\ngen = ImageDataGenerator(\n    rescale = 1/255.,\n)\ntest_datagen = gen.flow_from_dataframe(\n    test_df,\n    directory='/kaggle/input/cassava-leaf-disease-classification/train_images',\n    batch_size = 32,\n    target_size = (224,224),\n    seed = 42,\n    x_col = 'image_id',\n    y_col = 'label',\n    class_mode = 'categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:30.988805Z","iopub.execute_input":"2024-06-20T02:06:30.989198Z","iopub.status.idle":"2024-06-20T02:06:31.611043Z","shell.execute_reply.started":"2024-06-20T02:06:30.989168Z","shell.execute_reply":"2024-06-20T02:06:31.609926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modelling","metadata":{}},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(VGG16(include_top = False,weights = 'imagenet',input_shape=(224,224,3)))\nmodel.add(GlobalAveragePooling2D())\n\nmodel.add(Flatten())\n\nmodel.add(Dense(256,activation='elu'))\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(5,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:31.612378Z","iopub.execute_input":"2024-06-20T02:06:31.612784Z","iopub.status.idle":"2024-06-20T02:06:33.112988Z","shell.execute_reply.started":"2024-06-20T02:06:31.612744Z","shell.execute_reply":"2024-06-20T02:06:33.112224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = tf.keras.losses.CategoricalCrossentropy(\n    label_smoothing = 0.001,\n    name = 'categorical_crossentropy'\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.114025Z","iopub.execute_input":"2024-06-20T02:06:33.114294Z","iopub.status.idle":"2024-06-20T02:06:33.118704Z","shell.execute_reply.started":"2024-06-20T02:06:33.114271Z","shell.execute_reply":"2024-06-20T02:06:33.117669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(learning_rate = 1e-4)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.119875Z","iopub.execute_input":"2024-06-20T02:06:33.120209Z","iopub.status.idle":"2024-06-20T02:06:33.137847Z","shell.execute_reply.started":"2024-06-20T02:06:33.120178Z","shell.execute_reply":"2024-06-20T02:06:33.136958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = optimizer,loss=loss,metrics= ['categorical_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.139011Z","iopub.execute_input":"2024-06-20T02:06:33.139342Z","iopub.status.idle":"2024-06-20T02:06:33.148443Z","shell.execute_reply.started":"2024-06-20T02:06:33.139310Z","shell.execute_reply":"2024-06-20T02:06:33.147572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.149635Z","iopub.execute_input":"2024-06-20T02:06:33.150063Z","iopub.status.idle":"2024-06-20T02:06:33.172916Z","shell.execute_reply.started":"2024-06-20T02:06:33.150028Z","shell.execute_reply":"2024-06-20T02:06:33.172052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build((None))\ntf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.173936Z","iopub.execute_input":"2024-06-20T02:06:33.174250Z","iopub.status.idle":"2024-06-20T02:06:33.569092Z","shell.execute_reply.started":"2024-06-20T02:06:33.174225Z","shell.execute_reply":"2024-06-20T02:06:33.568021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"rlrong = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.2,\n    mode='min',\n    min_lr = 1e-5,\n    patience = 2,\n    verbose=1\n)\nestop = EarlyStopping(\n    monitor = 'val_loss',\n    mode= 'min',\n    patience = 3,\n    verbose = 1,\n    restore_best_weights = True\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.570440Z","iopub.execute_input":"2024-06-20T02:06:33.570768Z","iopub.status.idle":"2024-06-20T02:06:33.576511Z","shell.execute_reply.started":"2024-06-20T02:06:33.570741Z","shell.execute_reply":"2024-06-20T02:06:33.575459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_datagen,epochs = 5,verbose=1,validation_data = test_datagen,callbacks = [rlrong,estop])","metadata":{"execution":{"iopub.status.busy":"2024-06-20T02:06:33.577799Z","iopub.execute_input":"2024-06-20T02:06:33.578201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}