{"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":30887,"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-14T14:30:45.558445Z","iopub.execute_input":"2025-02-14T14:30:45.558735Z","iopub.status.idle":"2025-02-14T14:30:45.563166Z","shell.execute_reply.started":"2025-02-14T14:30:45.558713Z","shell.execute_reply":"2025-02-14T14:30:45.562297Z"}},"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-14T14:30:45.564526Z","iopub.execute_input":"2025-02-14T14:30:45.564824Z","iopub.status.idle":"2025-02-14T14:30:45.586127Z","shell.execute_reply.started":"2025-02-14T14:30:45.564801Z","shell.execute_reply":"2025-02-14T14:30:45.585097Z"}},"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/train.csv')\n\n# Get the counts for each class\ncases_count = df['label'].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()\ndel cases_count","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:45.588114Z","iopub.execute_input":"2025-02-14T14:30:45.588383Z","iopub.status.idle":"2025-02-14T14:30:45.765283Z","shell.execute_reply.started":"2025-02-14T14:30:45.588353Z","shell.execute_reply":"2025-02-14T14:30:45.764511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:45.766491Z","iopub.execute_input":"2025-02-14T14:30:45.766713Z","iopub.status.idle":"2025-02-14T14:30:45.774182Z","shell.execute_reply.started":"2025-02-14T14:30:45.766693Z","shell.execute_reply":"2025-02-14T14:30:45.773281Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:45.774974Z","iopub.execute_input":"2025-02-14T14:30:45.775217Z","iopub.status.idle":"2025-02-14T14:30:45.790281Z","shell.execute_reply.started":"2025-02-14T14:30:45.775170Z","shell.execute_reply":"2025-02-14T14:30:45.789419Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:45.791190Z","iopub.execute_input":"2025-02-14T14:30:45.791484Z","iopub.status.idle":"2025-02-14T14:30:45.797219Z","shell.execute_reply.started":"2025-02-14T14:30:45.791455Z","shell.execute_reply":"2025-02-14T14:30:45.796412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:45.798061Z","iopub.execute_input":"2025-02-14T14:30:45.798300Z","iopub.status.idle":"2025-02-14T14:30:45.812420Z","shell.execute_reply.started":"2025-02-14T14:30:45.798281Z","shell.execute_reply":"2025-02-14T14:30:45.811682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['label'].value_counts().sort_index().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:45.814969Z","iopub.execute_input":"2025-02-14T14:30:45.815170Z","iopub.status.idle":"2025-02-14T14:30:45.951648Z","shell.execute_reply.started":"2025-02-14T14:30:45.815143Z","shell.execute_reply":"2025-02-14T14:30:45.951003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"validate_df['label'].value_counts().sort_index().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:45.952939Z","iopub.execute_input":"2025-02-14T14:30:45.953173Z","iopub.status.idle":"2025-02-14T14:30:46.092048Z","shell.execute_reply.started":"2025-02-14T14:30:45.953154Z","shell.execute_reply":"2025-02-14T14:30:46.091398Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:46.092760Z","iopub.execute_input":"2025-02-14T14:30:46.092988Z","iopub.status.idle":"2025-02-14T14:30:46.096603Z","shell.execute_reply.started":"2025-02-14T14:30:46.092956Z","shell.execute_reply":"2025-02-14T14:30:46.095811Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:46.097435Z","iopub.execute_input":"2025-02-14T14:30:46.097669Z","iopub.status.idle":"2025-02-14T14:30:46.738817Z","shell.execute_reply.started":"2025-02-14T14:30:46.097650Z","shell.execute_reply":"2025-02-14T14:30:46.738160Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:46.739547Z","iopub.execute_input":"2025-02-14T14:30:46.739781Z","iopub.status.idle":"2025-02-14T14:30:47.845562Z","shell.execute_reply.started":"2025-02-14T14:30:46.739761Z","shell.execute_reply":"2025-02-14T14:30:47.844725Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:47.846457Z","iopub.execute_input":"2025-02-14T14:30:47.846741Z","iopub.status.idle":"2025-02-14T14:30:48.004147Z","shell.execute_reply.started":"2025-02-14T14:30:47.846714Z","shell.execute_reply":"2025-02-14T14:30:48.003340Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:48.005046Z","iopub.execute_input":"2025-02-14T14:30:48.005372Z","iopub.status.idle":"2025-02-14T14:30:48.281207Z","shell.execute_reply.started":"2025-02-14T14:30:48.005339Z","shell.execute_reply":"2025-02-14T14:30:48.279890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:48.282161Z","iopub.execute_input":"2025-02-14T14:30:48.282438Z","iopub.status.idle":"2025-02-14T14:30:48.285949Z","shell.execute_reply.started":"2025-02-14T14:30:48.282415Z","shell.execute_reply":"2025-02-14T14:30:48.285083Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:48.286679Z","iopub.execute_input":"2025-02-14T14:30:48.286945Z","iopub.status.idle":"2025-02-14T14:30:48.300257Z","shell.execute_reply.started":"2025-02-14T14:30:48.286908Z","shell.execute_reply":"2025-02-14T14:30:48.299394Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile the model\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:30:48.301089Z","iopub.execute_input":"2025-02-14T14:30:48.301599Z","iopub.status.idle":"2025-02-14T14:30:48.312586Z","shell.execute_reply.started":"2025-02-14T14:30:48.301555Z","shell.execute_reply":"2025-02-14T14:30:48.311789Z"}},"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":{"iopub.status.busy":"2025-02-14T14:30:48.313331Z","iopub.execute_input":"2025-02-14T14:30:48.313574Z","iopub.status.idle":"2025-02-14T14:32:59.749629Z","shell.execute_reply.started":"2025-02-14T14:30:48.313548Z","shell.execute_reply":"2025-02-14T14:32:59.748927Z"}},"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":{"iopub.status.busy":"2025-02-14T14:32:59.750574Z","iopub.execute_input":"2025-02-14T14:32:59.750838Z","iopub.status.idle":"2025-02-14T14:33:00.130783Z","shell.execute_reply.started":"2025-02-14T14:32:59.750817Z","shell.execute_reply":"2025-02-14T14:33:00.129934Z"}},"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":{"iopub.status.busy":"2025-02-14T14:33:00.131760Z","iopub.execute_input":"2025-02-14T14:33:00.132059Z","iopub.status.idle":"2025-02-14T14:33:00.138551Z","shell.execute_reply.started":"2025-02-14T14:33:00.132028Z","shell.execute_reply":"2025-02-14T14:33:00.137536Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:33:00.139378Z","iopub.execute_input":"2025-02-14T14:33:00.139651Z","iopub.status.idle":"2025-02-14T14:33:00.151720Z","shell.execute_reply.started":"2025-02-14T14:33:00.139629Z","shell.execute_reply":"2025-02-14T14:33:00.150988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.tail()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:33:00.152426Z","iopub.execute_input":"2025-02-14T14:33:00.152629Z","iopub.status.idle":"2025-02-14T14:33:00.167122Z","shell.execute_reply.started":"2025-02-14T14:33:00.152611Z","shell.execute_reply":"2025-02-14T14:33:00.166422Z"}},"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":{"iopub.status.busy":"2025-02-14T14:33:00.170068Z","iopub.execute_input":"2025-02-14T14:33:00.170306Z","iopub.status.idle":"2025-02-14T14:33:00.538269Z","shell.execute_reply.started":"2025-02-14T14:33:00.170286Z","shell.execute_reply":"2025-02-14T14:33:00.537644Z"}},"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":{"iopub.status.busy":"2025-02-14T14:33:00.539125Z","iopub.execute_input":"2025-02-14T14:33:00.539364Z","iopub.status.idle":"2025-02-14T14:33:00.787229Z","shell.execute_reply.started":"2025-02-14T14:33:00.539332Z","shell.execute_reply":"2025-02-14T14:33:00.786567Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Predict\npredict = model.predict(test_generator)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:33:00.788047Z","iopub.execute_input":"2025-02-14T14:33:00.788374Z","iopub.status.idle":"2025-02-14T14:33:07.083954Z","shell.execute_reply.started":"2025-02-14T14:33:00.788343Z","shell.execute_reply":"2025-02-14T14:33:07.083065Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['predict'] = np.argmax(predict, axis=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:33:07.084915Z","iopub.execute_input":"2025-02-14T14:33:07.085235Z","iopub.status.idle":"2025-02-14T14:33:07.089178Z","shell.execute_reply.started":"2025-02-14T14:33:07.085179Z","shell.execute_reply":"2025-02-14T14:33:07.088530Z"}},"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":{"iopub.status.busy":"2025-02-14T14:33:07.090047Z","iopub.execute_input":"2025-02-14T14:33:07.090308Z","iopub.status.idle":"2025-02-14T14:33:07.106106Z","shell.execute_reply.started":"2025-02-14T14:33:07.090287Z","shell.execute_reply":"2025-02-14T14:33:07.105418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df['predict'].value_counts().plot.bar()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:33:07.106895Z","iopub.execute_input":"2025-02-14T14:33:07.107358Z","iopub.status.idle":"2025-02-14T14:33:07.493032Z","shell.execute_reply.started":"2025-02-14T14:33:07.107324Z","shell.execute_reply":"2025-02-14T14:33:07.492290Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_df.head()) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-02-14T14:33:07.493959Z","iopub.execute_input":"2025-02-14T14:33:07.494296Z","iopub.status.idle":"2025-02-14T14:33:07.500155Z","shell.execute_reply.started":"2025-02-14T14:33:07.494264Z","shell.execute_reply":"2025-02-14T14:33:07.499407Z"}},"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":{"iopub.status.busy":"2025-02-14T14:33:07.501146Z","iopub.execute_input":"2025-02-14T14:33:07.501458Z","iopub.status.idle":"2025-02-14T14:33:07.516542Z","shell.execute_reply.started":"2025-02-14T14:33:07.501421Z","shell.execute_reply":"2025-02-14T14:33:07.515846Z"}},"outputs":[],"execution_count":null}]}