{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":13836,"databundleVersionId":1718836,"sourceType":"competition"}],"dockerImageVersionId":30715,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Set Up Environment","metadata":{}},{"cell_type":"code","source":"import math, re, os\nimport cv2\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom kaggle_datasets import KaggleDatasets\nfrom tensorflow import keras\nfrom functools import partial\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nprint(\"Tensorflow version \" + tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:21:17.284428Z","iopub.execute_input":"2024-06-06T14:21:17.284677Z","iopub.status.idle":"2024-06-06T14:21:36.551556Z","shell.execute_reply.started":"2024-06-06T14:21:17.284650Z","shell.execute_reply":"2024-06-06T14:21:36.550740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Detect TPU","metadata":{}},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas: ', strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:23:16.789561Z","iopub.execute_input":"2024-06-06T14:23:16.790275Z","iopub.status.idle":"2024-06-06T14:23:26.817631Z","shell.execute_reply.started":"2024-06-06T14:23:16.790238Z","shell.execute_reply":"2024-06-06T14:23:26.816854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"/kaggle/input/cassava-leaf-disease-classification/\"","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:23:32.797834Z","iopub.execute_input":"2024-06-06T14:23:32.798667Z","iopub.status.idle":"2024-06-06T14:23:32.802144Z","shell.execute_reply.started":"2024-06-06T14:23:32.798632Z","shell.execute_reply":"2024-06-06T14:23:32.801135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/cassava-leaf-disease-classification/train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:23:39.739766Z","iopub.execute_input":"2024-06-06T14:23:39.740369Z","iopub.status.idle":"2024-06-06T14:23:39.775023Z","shell.execute_reply.started":"2024-06-06T14:23:39.740314Z","shell.execute_reply":"2024-06-06T14:23:39.774123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(train_df, x='label')","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:23:42.987728Z","iopub.execute_input":"2024-06-06T14:23:42.988095Z","iopub.status.idle":"2024-06-06T14:23:43.206545Z","shell.execute_reply.started":"2024-06-06T14:23:42.988063Z","shell.execute_reply":"2024-06-06T14:23:43.205640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = train_df[train_df.label == 0].sample(1)\nfor i in range(1,5):\n    sample = train_df[train_df.label == i].sample(1).values[0]\n    sample_df.loc[len(sample_df.index)] = sample\nsample_df","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:23:46.297668Z","iopub.execute_input":"2024-06-06T14:23:46.298020Z","iopub.status.idle":"2024-06-06T14:23:46.318775Z","shell.execute_reply.started":"2024-06-06T14:23:46.297990Z","shell.execute_reply":"2024-06-06T14:23:46.317754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = os.path.join(path, \"train_images\")\nplt.figure(figsize=(12,12))\nfor i, (img_id, label) in enumerate(zip(sample_df.image_id, sample_df.label)):\n    plt.subplot(3,3, i+1)\n    image = cv2.imread(os.path.join(train_path, img_id))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    plt.imshow(image)\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:23:48.427367Z","iopub.execute_input":"2024-06-06T14:23:48.427686Z","iopub.status.idle":"2024-06-06T14:23:48.938906Z","shell.execute_reply.started":"2024-06-06T14:23:48.427660Z","shell.execute_reply":"2024-06-06T14:23:48.937991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation ","metadata":{}},{"cell_type":"code","source":"train_df[\"label\"] = train_df[\"label\"].astype('str')\ntrain_gen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,\n    horizontal_flip=True,\n    vertical_flip=True,\n    shear_range=0.2,\n    fill_mode='nearest',\n    validation_split = 0.25\n)\n\nval_gen = ImageDataGenerator(\n    rescale=1./255,\n    validation_split = 0.25\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:24:49.279615Z","iopub.execute_input":"2024-06-06T14:24:49.280009Z","iopub.status.idle":"2024-06-06T14:24:49.290512Z","shell.execute_reply.started":"2024-06-06T14:24:49.279980Z","shell.execute_reply":"2024-06-06T14:24:49.289453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = train_gen.flow_from_dataframe(\n    train_df,\n    directory = train_path,\n    batch_size = 16,\n    x_col = \"image_id\",\n    y_col = \"label\",\n    target_size = (500,500),\n    subset = \"training\",\n    class_mode = \"categorical\",\n    seed = \"42\"\n)\n\nval_datagen = val_gen.flow_from_dataframe(\n    train_df,\n    directory = train_path,\n    batch_size = 16,\n    x_col = \"image_id\",\n    y_col = \"label\",\n    target_size = (500,500),\n    subset = \"validation\",\n    class_mode = \"categorical\",\n    seed = \"42\"\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:24:52.278004Z","iopub.execute_input":"2024-06-06T14:24:52.278745Z","iopub.status.idle":"2024-06-06T14:26:03.632522Z","shell.execute_reply.started":"2024-06-06T14:24:52.278707Z","shell.execute_reply":"2024-06-06T14:26:03.631693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(float(train_datagen.n/train_datagen.batch_size))","metadata":{"execution":{"iopub.status.busy":"2024-06-06T13:31:22.474472Z","iopub.execute_input":"2024-06-06T13:31:22.474898Z","iopub.status.idle":"2024-06-06T13:31:22.481290Z","shell.execute_reply.started":"2024-06-06T13:31:22.474864Z","shell.execute_reply":"2024-06-06T13:31:22.480183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Built Model","metadata":{}},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Conv2D(16, 3, activation=\"relu\", input_shape=(500,500,5)),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(32, 3, activation=\"relu\"),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(64, 3, activation=\"relu\"),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, 3, activation=\"relu\"),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(128, 3, activation=\"relu\"),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.Conv2D(256, 3, activation=\"relu\"),\n    tf.keras.layers.MaxPooling2D(2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dropout(0.5),\n    tf.keras.layers.Dense(128, activation=\"relu\"),\n    tf.keras.layers.Dense(5, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:40:53.809603Z","iopub.execute_input":"2024-06-06T14:40:53.810455Z","iopub.status.idle":"2024-06-06T14:40:53.904663Z","shell.execute_reply.started":"2024-06-06T14:40:53.810418Z","shell.execute_reply":"2024-06-06T14:40:53.903744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=tf.optimizers.Adam(),\n    loss=\"categorical_crossentropy\",\n    metrics=[\"categorical_accuracy\"]\n             )\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:40:56.480608Z","iopub.execute_input":"2024-06-06T14:40:56.480932Z","iopub.status.idle":"2024-06-06T14:40:56.510135Z","shell.execute_reply.started":"2024-06-06T14:40:56.480903Z","shell.execute_reply":"2024-06-06T14:40:56.509424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_datagen,\n    epochs = 25,\n    steps_per_epoch = 41,\n    validation_data = val_datagen,\n    validation_steps = 27,\n    verbose=0,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-06T14:41:05.513500Z","iopub.execute_input":"2024-06-06T14:41:05.513816Z","iopub.status.idle":"2024-06-06T14:41:05.553655Z","shell.execute_reply.started":"2024-06-06T14:41:05.513789Z","shell.execute_reply":"2024-06-06T14:41:05.552705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}