{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":93309,"databundleVersionId":11102982,"sourceType":"competition"}],"dockerImageVersionId":30886,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import library","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom skimage.io import imread\n\nfrom keras.models import Sequential, Model, load_model\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, SeparableConv2D","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T07:14:17.703331Z","iopub.execute_input":"2025-02-15T07:14:17.703636Z","iopub.status.idle":"2025-02-15T07:14:17.797982Z","shell.execute_reply.started":"2025-02-15T07:14:17.703612Z","shell.execute_reply":"2025-02-15T07:14:17.796769Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T07:14:41.892606Z","iopub.execute_input":"2025-02-15T07:14:41.893075Z","iopub.status.idle":"2025-02-15T07:14:41.901761Z","shell.execute_reply.started":"2025-02-15T07:14:41.893041Z","shell.execute_reply":"2025-02-15T07:14:41.900704Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/durian-hackathon-sugarcane-disease-classification/submit.csv')\n\n# Print the available columns to check the actual column name\nprint(df.columns)\n\n# Assuming the column name is 'predict' based on the provided DataFrame info\ncases_count = df['predict'].value_counts() \nprint(cases_count)\n\n# Plot the results \nplt.figure(figsize=(10,8))\nsns.barplot(x=cases_count.index, y= cases_count.values)\nplt.title('Number of images', fontsize=14)\nplt.xlabel('class', fontsize=12)\nplt.ylabel('Count', fontsize=12)\n# plt.xticks(range(len(cases_count.index)), ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T07:21:27.437697Z","iopub.execute_input":"2025-02-15T07:21:27.438013Z","iopub.status.idle":"2025-02-15T07:21:27.585694Z","shell.execute_reply.started":"2025-02-15T07:21:27.437991Z","shell.execute_reply":"2025-02-15T07:21:27.584992Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-15T07:13:28.243509Z","iopub.execute_input":"2025-02-15T07:13:28.243843Z","iopub.status.idle":"2025-02-15T07:13:28.262805Z","shell.execute_reply.started":"2025-02-15T07:13:28.243813Z","shell.execute_reply":"2025-02-15T07:13:28.261737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_df, validate_df = train_test_split(df, test_size=0.20, stratify=df['label'])\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['label'] = train_df['label'].astype(str)\nvalidate_df['label'] = validate_df['label'].astype(str)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['label'].value_counts().sort_index().plot.bar()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"validate_df['label'].value_counts().sort_index().plot.bar()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_size = 224\ntarget_size = (image_size, image_size)\ninput_shape = (image_size, image_size, 3)\n\nbatch_size = 16\nnum_class = 5\nepochs = 5","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=15,\n    shear_range=0.1,\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/input/durian-hackathon-sugarcane-disease-classification/Images\", \n    x_col='id',\n    y_col='label',\n    target_size=target_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"batch=next(train_generator)  # returns the next batch of images and labels \nprint(batch[0].shape) # batch[0] is the images, batch[1] are the labels\n\nfor i in range(0,4):\n    img=batch[0][i] \n    plt.imshow(img)\n    plt.show()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/input/durian-hackathon-sugarcane-disease-classification/Images\", \n    x_col='id',\n    y_col='label',\n    target_size=target_size,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nimport os\n\nbase_model = tf.keras.applications.VGG16(\n    input_shape=input_shape,\n    include_top=False,\n    weights='imagenet'\n)\n\nbase_model.trainable = False\n\nx = base_model.output\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Dropout(0.2)(x)\noutput = tf.keras.layers.Dense(num_class, activation=\"softmax\")(x)\n\nmodel = keras.Model(inputs = base_model.input,\n                    outputs = output)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model.summary()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n# Define the optimizer\nadam = Adam(learning_rate=0.001)\n\nearlyStopping = EarlyStopping(monitor='val_loss', patience=30, verbose=0, mode='min')\nmcp_save = ModelCheckpoint('best_model.keras', save_best_only=True, monitor='val_loss', mode='min')","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile the model\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(train_generator, \n                validation_data = validation_generator,\n                epochs = epochs,\n                batch_size = batch_size,\n                callbacks=[earlyStopping, mcp_save])","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\nax1.plot(history.history['loss'], color='b', label=\"Training loss\")\nax1.plot(history.history['val_loss'], color='r', label=\"validation loss\")\n\nax2.plot(history.history['accuracy'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_accuracy'], color='r',label=\"Validation accuracy\")\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#test data\ntest_df = pd.read_csv('/kaggle/input/durian-hackathon-sugarcane-disease-classification/submit.csv')","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.tail()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# create test generator\n\ntest_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/input/durian-hackathon-sugarcane-disease-classification/Images\", \n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=target_size,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load best model from saved file\nmodel = load_model('/kaggle/working/best_model.keras')","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict\npredict = model.predict(test_generator)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['predict'] = np.argmax(predict, axis=-1)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['predict'] = test_df['predict'].replace(label_map)","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['predict'].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.head()) ","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.to_csv('../working/submit.csv', index=False)\nfrom IPython.display import FileLink\nFileLink(r'submit.csv')","metadata":{"trusted":true,"execution":{"execution_failed":"2025-02-15T07:10:13.489Z"}},"outputs":[],"execution_count":null}]}