{"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":"none","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\nfrom IPython.display import clear_output\nclear_output()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-16T02:50:05.807387Z","iopub.execute_input":"2024-06-16T02:50:05.807991Z","iopub.status.idle":"2024-06-16T02:50:11.457428Z","shell.execute_reply.started":"2024-06-16T02:50:05.807947Z","shell.execute_reply":"2024-06-16T02:50:11.456281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image\n\nimport random\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers , models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing.image import load_img , img_to_array\n\nfrom sklearn.metrics import confusion_matrix , accuracy_score\n","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:01:18.416379Z","iopub.execute_input":"2024-06-16T03:01:18.416971Z","iopub.status.idle":"2024-06-16T03:01:18.423637Z","shell.execute_reply.started":"2024-06-16T03:01:18.416929Z","shell.execute_reply":"2024-06-16T03:01:18.422423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Image reading","metadata":{}},{"cell_type":"code","source":"def plot_img(path , num_imgs = 6):\n    img_filenames = os.listdir(path)\n    \n    selected_images = random.sample(img_filenames , num_imgs)\n    \n    fig , axs = plt.subplots( 3 , 2, figsize = (8 , 8))\n    axs = axs.ravel()\n    \n    for i ,img_file in enumerate(selected_images):\n        img_path = os.path.join(path , img_file)\n        img = Image.open(img_path)\n        axs[i].imshow(img)\n        \n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:03:44.924197Z","iopub.execute_input":"2024-06-16T03:03:44.924625Z","iopub.status.idle":"2024-06-16T03:03:44.934334Z","shell.execute_reply.started":"2024-06-16T03:03:44.924589Z","shell.execute_reply":"2024-06-16T03:03:44.933078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:03:45.368907Z","iopub.execute_input":"2024-06-16T03:03:45.369327Z","iopub.status.idle":"2024-06-16T03:03:45.374342Z","shell.execute_reply.started":"2024-06-16T03:03:45.369294Z","shell.execute_reply":"2024-06-16T03:03:45.372972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_img(path)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:03:45.940834Z","iopub.execute_input":"2024-06-16T03:03:45.941239Z","iopub.status.idle":"2024-06-16T03:03:47.734532Z","shell.execute_reply.started":"2024-06-16T03:03:45.941211Z","shell.execute_reply":"2024-06-16T03:03:47.733326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model building","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:16:17.478550Z","iopub.execute_input":"2024-06-16T03:16:17.479016Z","iopub.status.idle":"2024-06-16T03:16:17.513098Z","shell.execute_reply.started":"2024-06-16T03:16:17.478979Z","shell.execute_reply":"2024-06-16T03:16:17.511414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = []\nlabels = []","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:16:35.650039Z","iopub.execute_input":"2024-06-16T03:16:35.650437Z","iopub.status.idle":"2024-06-16T03:16:35.657745Z","shell.execute_reply.started":"2024-06-16T03:16:35.650407Z","shell.execute_reply":"2024-06-16T03:16:35.655351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\n\nfor index , row in df.iterrows():\n    img_path = os.path.join(path , row['image_id'])\n    image = load_img(img_path , target_size = (150 ,150))\n    image = img_to_array(image)\n    images.append(image)\n    labels.append(row['label'])\n    \nimages = np.array(images)\nlabels = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:21:59.879458Z","iopub.execute_input":"2024-06-16T03:21:59.880038Z","iopub.status.idle":"2024-06-16T03:25:33.605460Z","shell.execute_reply.started":"2024-06-16T03:21:59.879991Z","shell.execute_reply":"2024-06-16T03:25:33.604072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\nrescale = 1.0/255.0, validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:31:26.679443Z","iopub.execute_input":"2024-06-16T03:31:26.680150Z","iopub.status.idle":"2024-06-16T03:31:26.688101Z","shell.execute_reply.started":"2024-06-16T03:31:26.680094Z","shell.execute_reply":"2024-06-16T03:31:26.686731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow(\nimages ,\ny = labels , \nbatch_size = 20  ,\nsubset = 'training')\n\nval_generator = train_datagen.flow(\nimages , \ny = labels , \nbatch_size = 20,\nsubset = 'validation')","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:35:15.293913Z","iopub.execute_input":"2024-06-16T03:35:15.294375Z","iopub.status.idle":"2024-06-16T03:35:15.303785Z","shell.execute_reply.started":"2024-06-16T03:35:15.294336Z","shell.execute_reply":"2024-06-16T03:35:15.301791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:36:53.698613Z","iopub.execute_input":"2024-06-16T03:36:53.699075Z","iopub.status.idle":"2024-06-16T03:36:53.710143Z","shell.execute_reply.started":"2024-06-16T03:36:53.699039Z","shell.execute_reply":"2024-06-16T03:36:53.708609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    \n    Conv2D(128, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    \n    Flatten(),\n    Dense(512, activation='relu'),\n    Dropout(0.5),\n    Dense(1, activation='softmax')  \n])","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:36:55.329508Z","iopub.execute_input":"2024-06-16T03:36:55.329981Z","iopub.status.idle":"2024-06-16T03:36:55.572889Z","shell.execute_reply.started":"2024-06-16T03:36:55.329944Z","shell.execute_reply":"2024-06-16T03:36:55.571429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam',\nloss = 'categorical_crossentropy',\nmetrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:40:20.546227Z","iopub.execute_input":"2024-06-16T03:40:20.546675Z","iopub.status.idle":"2024-06-16T03:40:20.567304Z","shell.execute_reply.started":"2024-06-16T03:40:20.546639Z","shell.execute_reply":"2024-06-16T03:40:20.565984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-16T03:41:00.058442Z","iopub.execute_input":"2024-06-16T03:41:00.058890Z","iopub.status.idle":"2024-06-16T03:41:00.098597Z","shell.execute_reply.started":"2024-06-16T03:41:00.058837Z","shell.execute_reply":"2024-06-16T03:41:00.097345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch = 20\nsteps_per_epoch = (df.shape[0]*0.8)/batch\nprint(steps_per_epoch)\n\nval_steps = (df.shape[0]*0.2)/batch\nprint(val_steps)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T04:52:24.416338Z","iopub.execute_input":"2024-06-16T04:52:24.416835Z","iopub.status.idle":"2024-06-16T04:52:24.424559Z","shell.execute_reply.started":"2024-06-16T04:52:24.416795Z","shell.execute_reply":"2024-06-16T04:52:24.423334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mod_fit = model.fit(\ntrain_generator , \nsteps_per_epoch = 850,\nepochs = 10,\nvalidation_data = val_generator ,\nvalidation_steps = 210)","metadata":{"execution":{"iopub.status.busy":"2024-06-16T04:06:04.428668Z","iopub.execute_input":"2024-06-16T04:06:04.429120Z","iopub.status.idle":"2024-06-16T04:43:16.010224Z","shell.execute_reply.started":"2024-06-16T04:06:04.429083Z","shell.execute_reply":"2024-06-16T04:43:16.008960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}