{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import Library","metadata":{"_uuid":"fe76d1d1ded592430e7548feacfa38dc42f085d9"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom keras.preprocessing.image import ImageDataGenerator, load_img\nfrom keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport random\nimport os\nprint(os.listdir(\"../input\"))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-21T14:11:09.915831Z","iopub.execute_input":"2023-05-21T14:11:09.916189Z","iopub.status.idle":"2023-05-21T14:11:09.923602Z","shell.execute_reply.started":"2023-05-21T14:11:09.916128Z","shell.execute_reply":"2023-05-21T14:11:09.922616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Constants","metadata":{}},{"cell_type":"code","source":"FAST_RUN = False\nIMAGE_WIDTH=128\nIMAGE_HEIGHT=128\nIMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\nIMAGE_CHANNELS=3","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:11:11.955621Z","iopub.execute_input":"2023-05-21T14:11:11.955953Z","iopub.status.idle":"2023-05-21T14:11:11.960251Z","shell.execute_reply.started":"2023-05-21T14:11:11.955894Z","shell.execute_reply":"2023-05-21T14:11:11.959229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Traning Data","metadata":{"_uuid":"7335a579cc0268fba5d34d6f7558f33c187eedb3"}},{"cell_type":"code","source":"folder_path = \"/kaggle/input/checkbox\"\n\n# Get a list of all files in the folder\nfiles = os.listdir(folder_path)\n\n# Create a dictionary to store the filenames and marked/unmarked status\ndata = {\"filename\": [], \"marked\": []}\n\n# Loop through all the files and add them to the dictionary\nfor filename in files:\n    if filename.startswith(\"marked_\"):\n        data[\"filename\"].append(filename)\n        data[\"marked\"].append(1)\n    elif filename.startswith(\"unmarked_\"):\n        data[\"filename\"].append(filename)\n        data[\"marked\"].append(0)\n\n# Create a pandas DataFrame from the dictionary\ndf = pd.DataFrame(data)\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:11:14.98551Z","iopub.execute_input":"2023-05-21T14:11:14.985823Z","iopub.status.idle":"2023-05-21T14:11:14.994593Z","shell.execute_reply.started":"2023-05-21T14:11:14.985763Z","shell.execute_reply":"2023-05-21T14:11:14.993727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"_uuid":"915bb9ba7063ab4d5c07c542419ae119003a5f98","execution":{"iopub.status.busy":"2023-05-21T14:11:15.727108Z","iopub.execute_input":"2023-05-21T14:11:15.727437Z","iopub.status.idle":"2023-05-21T14:11:15.742986Z","shell.execute_reply.started":"2023-05-21T14:11:15.72738Z","shell.execute_reply":"2023-05-21T14:11:15.742094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.tail()","metadata":{"_uuid":"72bf69e817f67f5a2eaff8561217e22077248553","execution":{"iopub.status.busy":"2023-05-21T14:11:17.598951Z","iopub.execute_input":"2023-05-21T14:11:17.599594Z","iopub.status.idle":"2023-05-21T14:11:17.618839Z","shell.execute_reply.started":"2023-05-21T14:11:17.599536Z","shell.execute_reply":"2023-05-21T14:11:17.618152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np\n# import pandas as pd \n# from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\n# from keras.utils import to_categorical\n# from sklearn.model_selection import train_test_split\n# import matplotlib.pyplot as plt\n# import random\n# import os\n# print(os.listdir(\"../input\"))\n\n# FAST_RUN = False\n# IMAGE_WIDTH=128\n# IMAGE_HEIGHT=128\n# IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)\n# IMAGE_CHANNELS=3\n\n# folder_path = \"/kaggle/input/checkbox\"\n\n# # Get a list of all files in the folder\n# files = os.listdir(folder_path)\n\n# # Create a dictionary to store the filenames and marked/unmarked status\n# data = {\"filename\": [], \"marked\": []}\n\n# # Loop through all the files and add them to the dictionary\n# for filename in files:\n#     if filename.startswith(\"marked_\"):\n#         data[\"filename\"].append(filename)\n#         data[\"marked\"].append(1)\n#     elif filename.startswith(\"unmarked_\"):\n#         data[\"filename\"].append(filename)\n#         data[\"marked\"].append(0)\n\n# # Create a pandas DataFrame from the dictionary\n# df = pd.DataFrame(data)\n\n# # Balance the data by amplifying the marked images\n# marked_df = df[df['marked'] == 1]\n# unmarked_df = df[df['marked'] == 0]\n\n# # # Amplify the marked images by creating new augmented images\n# # datagen = ImageDataGenerator(\n# #     rotation_range=20,\n# #     width_shift_range=0.2,\n# #     height_shift_range=0.2,\n# #     zoom_range=0.2,\n# #     horizontal_flip=True,\n# #     vertical_flip=True,\n# #     fill_mode='nearest'\n# # )\n\n# # augmented_images = []\n# # for i in range(0, len(marked_df)):\n# #     img_path = folder_path + '/' + marked_df.iloc[i]['filename']\n# #     img = load_img(img_path)\n# #     x = img_to_array(img)\n# #     x = x.reshape((1,) + x.shape)\n# #     j = 0\n# #     for batch in datagen.flow(x, batch_size=1):\n# #         augmented_images.append(batch[0])\n# #         j += 1\n# #         if j == 5: # Number of augmented images per original image\n# #             break\n\n# # # Create a new DataFrame with the amplified marked images\n# # augmented_df = pd.DataFrame({\"filename\": [f\"augmented_{i}.jpg\" for i in range(len(augmented_images))], \"marked\": [1]*len(augmented_images)})\n# # augmented_df.to_csv(\"augmented.csv\", index=False)\n\n# # Combine the original and amplified dataframes\n# df = pd.concat([df, augmented_df], axis=0)\n\n# # Shuffle the data\n# df = df.sample(frac=1).reset_index(drop=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:10:55.483499Z","iopub.execute_input":"2023-05-21T14:10:55.48386Z","iopub.status.idle":"2023-05-21T14:10:55.516396Z","shell.execute_reply.started":"2023-05-21T14:10:55.483792Z","shell.execute_reply":"2023-05-21T14:10:55.515621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['marked'].value_counts().plot.bar()","metadata":{"_uuid":"fa26f0bc7a6d835a24989790b20f3c6f32946f45","execution":{"iopub.status.busy":"2023-05-21T14:11:26.851908Z","iopub.execute_input":"2023-05-21T14:11:26.852218Z","iopub.status.idle":"2023-05-21T14:11:27.111688Z","shell.execute_reply.started":"2023-05-21T14:11:26.852162Z","shell.execute_reply":"2023-05-21T14:11:27.110574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = random.choice(df['filename'])\nimage = load_img(\"/kaggle/input/checkbox/\"+sample)\nplt.imshow(image)","metadata":{"_uuid":"602b40f7353871cb161c60b5237f0da0096b2f47","execution":{"iopub.status.busy":"2023-05-21T14:12:21.809635Z","iopub.execute_input":"2023-05-21T14:12:21.809979Z","iopub.status.idle":"2023-05-21T14:12:22.104586Z","shell.execute_reply.started":"2023-05-21T14:12:21.80992Z","shell.execute_reply":"2023-05-21T14:12:22.103398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\n\nmodel = Sequential()\n\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(IMAGE_WIDTH, IMAGE_HEIGHT, IMAGE_CHANNELS)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(BatchNormalization())\nmodel.add(Dense(2, activation='softmax')) # 2 because we have marked and unmarked classes\n\nmodel.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])\n\nmodel.summary()","metadata":{"_uuid":"8c9f833c1441b657c779844912d0b8028218d454","execution":{"iopub.status.busy":"2023-05-21T14:12:24.841549Z","iopub.execute_input":"2023-05-21T14:12:24.842031Z","iopub.status.idle":"2023-05-21T14:12:25.599708Z","shell.execute_reply.started":"2023-05-21T14:12:24.84182Z","shell.execute_reply":"2023-05-21T14:12:25.599023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Callbacks","metadata":{"_uuid":"bd496f6c65888a969be3703135b0b03a8a1190c8"}},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ReduceLROnPlateau","metadata":{"_uuid":"9aa032f0f6da539d23918890d2d131cc3aac8c7a","execution":{"iopub.status.busy":"2023-05-18T11:51:25.334294Z","iopub.execute_input":"2023-05-18T11:51:25.334617Z","iopub.status.idle":"2023-05-18T11:51:25.339635Z","shell.execute_reply.started":"2023-05-18T11:51:25.334559Z","shell.execute_reply":"2023-05-18T11:51:25.338669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"earlystop = EarlyStopping(patience=10)","metadata":{"_uuid":"3421c5ec428da6c0d8cc1184179a9caff1e01d1c","execution":{"iopub.status.busy":"2023-05-18T11:51:25.509607Z","iopub.execute_input":"2023-05-18T11:51:25.509899Z","iopub.status.idle":"2023-05-18T11:51:25.516857Z","shell.execute_reply.started":"2023-05-18T11:51:25.509846Z","shell.execute_reply":"2023-05-18T11:51:25.515946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Learning Rate Reduction**\n\nWe will reduce the learning rate when then accuracy not increase for 2 steps","metadata":{"_uuid":"51d3fe52e911286433cedf6e47332948a253361e"}},{"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n                                            patience=2, \n                                            verbose=1, \n                                            factor=0.5, \n                                            min_lr=0.00001)","metadata":{"_uuid":"8010a5661ad8924d2db24af0f3c00b1593b38901","execution":{"iopub.status.busy":"2023-05-18T11:51:26.130852Z","iopub.execute_input":"2023-05-18T11:51:26.131147Z","iopub.status.idle":"2023-05-18T11:51:26.139729Z","shell.execute_reply.started":"2023-05-18T11:51:26.131095Z","shell.execute_reply":"2023-05-18T11:51:26.138587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [earlystop, learning_rate_reduction]","metadata":{"_uuid":"a79cc604199469789f183096d863f7248e5f6aab","execution":{"iopub.status.busy":"2023-05-18T11:51:26.569199Z","iopub.execute_input":"2023-05-18T11:51:26.569542Z","iopub.status.idle":"2023-05-18T11:51:26.573941Z","shell.execute_reply.started":"2023-05-18T11:51:26.569479Z","shell.execute_reply":"2023-05-18T11:51:26.572974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare data","metadata":{}},{"cell_type":"code","source":"df[\"marked\"] = df[\"marked\"].replace({0: 'unmarked', 1: 'marked'}) ","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:12:33.026632Z","iopub.execute_input":"2023-05-21T14:12:33.026956Z","iopub.status.idle":"2023-05-21T14:12:33.03439Z","shell.execute_reply.started":"2023-05-21T14:12:33.026897Z","shell.execute_reply":"2023-05-21T14:12:33.033403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, validate_df = train_test_split(df, test_size=0.20, random_state=42)\ntrain_df = train_df.reset_index(drop=True)\nvalidate_df = validate_df.reset_index(drop=True)","metadata":{"_uuid":"4eeb7af8dcf02c4ef5ca744c8305c51a2f5cedef","execution":{"iopub.status.busy":"2023-05-21T14:12:35.573919Z","iopub.execute_input":"2023-05-21T14:12:35.574233Z","iopub.status.idle":"2023-05-21T14:12:35.581703Z","shell.execute_reply.started":"2023-05-21T14:12:35.574176Z","shell.execute_reply":"2023-05-21T14:12:35.580657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['marked'].value_counts().plot.bar()","metadata":{"_uuid":"b84836337441705eda9d2e655665ffa14d9feead","execution":{"iopub.status.busy":"2023-05-21T14:12:37.725473Z","iopub.execute_input":"2023-05-21T14:12:37.725774Z","iopub.status.idle":"2023-05-21T14:12:37.976823Z","shell.execute_reply.started":"2023-05-21T14:12:37.725719Z","shell.execute_reply":"2023-05-21T14:12:37.97605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validate_df['marked'].value_counts().plot.bar()","metadata":{"_uuid":"19cf03f9a3c39532d6e2d06bd30be49a5afd9d57","execution":{"iopub.status.busy":"2023-05-21T14:12:42.331523Z","iopub.execute_input":"2023-05-21T14:12:42.331864Z","iopub.status.idle":"2023-05-21T14:12:42.600056Z","shell.execute_reply.started":"2023-05-21T14:12:42.331786Z","shell.execute_reply":"2023-05-21T14:12:42.599054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total_train = train_df.shape[0]\ntotal_validate = validate_df.shape[0]\nbatch_size=15","metadata":{"_uuid":"ae3dec0361f0443132d0309d3b883ee80070cf9f","execution":{"iopub.status.busy":"2023-05-21T14:12:57.956882Z","iopub.execute_input":"2023-05-21T14:12:57.957201Z","iopub.status.idle":"2023-05-21T14:12:57.963696Z","shell.execute_reply.started":"2023-05-21T14:12:57.957146Z","shell.execute_reply":"2023-05-21T14:12:57.962766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Traning Generator","metadata":{"_uuid":"ff760be9104f7d9492467b8d9d3405011aa77d11"}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=15,\n    rescale=1./255,\n    shear_range=0.1,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    width_shift_range=0.1,\n    height_shift_range=0.1\n)\n\ntrain_generator = train_datagen.flow_from_dataframe(\n    train_df, \n    \"/kaggle/input/checkbox/\", \n    x_col='filename',\n    y_col='marked',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"_uuid":"4d1c7818703a8a4bac5c036fdea45972aa9e5e9e","execution":{"iopub.status.busy":"2023-05-21T14:13:03.16912Z","iopub.execute_input":"2023-05-21T14:13:03.169426Z","iopub.status.idle":"2023-05-21T14:13:03.248461Z","shell.execute_reply.started":"2023-05-21T14:13:03.169372Z","shell.execute_reply":"2023-05-21T14:13:03.24761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Validation Generator","metadata":{"_uuid":"859c7b2857939c19fd2e3bb32839c9f7deb5aa3f"}},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255)\nvalidation_generator = validation_datagen.flow_from_dataframe(\n    validate_df, \n    \"/kaggle/input/checkbox/\", \n    x_col='filename',\n    y_col='marked',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical',\n    batch_size=batch_size\n)","metadata":{"_uuid":"7925e16bcacc89f4484fb6fe47e54d6420af732e","execution":{"iopub.status.busy":"2023-05-21T14:13:10.534403Z","iopub.execute_input":"2023-05-21T14:13:10.534713Z","iopub.status.idle":"2023-05-21T14:13:10.568736Z","shell.execute_reply.started":"2023-05-21T14:13:10.534657Z","shell.execute_reply":"2023-05-21T14:13:10.568052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# See how our generator work","metadata":{"_uuid":"6e17fc1f002fedd60febb78fee5e81770640b909"}},{"cell_type":"code","source":"example_df = train_df.sample(n=1).reset_index(drop=True)\nexample_generator = train_datagen.flow_from_dataframe(\n    example_df, \n    \"/kaggle/input/checkbox/\", \n    x_col='filename',\n    y_col='marked',\n    target_size=IMAGE_SIZE,\n    class_mode='categorical'\n)","metadata":{"_uuid":"4252cce168ab65f88e44a8ebc2672607bc852af4","execution":{"iopub.status.busy":"2023-05-21T14:13:12.215208Z","iopub.execute_input":"2023-05-21T14:13:12.215512Z","iopub.status.idle":"2023-05-21T14:13:12.230597Z","shell.execute_reply.started":"2023-05-21T14:13:12.215455Z","shell.execute_reply":"2023-05-21T14:13:12.22964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(0, 15):\n    plt.subplot(5, 3, i+1)\n    for X_batch, Y_batch in example_generator:\n        image = X_batch[0]\n        plt.imshow(image)\n        print(Y_batch)\n        break\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"23d923dba747f8b47dc75569244cecc6f70df321","execution":{"iopub.status.busy":"2023-05-21T14:13:35.48053Z","iopub.execute_input":"2023-05-21T14:13:35.480833Z","iopub.status.idle":"2023-05-21T14:13:38.279739Z","shell.execute_reply.started":"2023-05-21T14:13:35.480776Z","shell.execute_reply":"2023-05-21T14:13:38.278953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Seem to be nice ","metadata":{"_uuid":"810ddf1373d9db470ed48da4f30ca5a6c1274435"}},{"cell_type":"markdown","source":"# Fit Model","metadata":{"_uuid":"5cd8df64e794ed17de326b613a9819e7da977a0e"}},{"cell_type":"code","source":"epochs=3 if FAST_RUN else 50\nhistory = model.fit_generator(\n    train_generator, \n    epochs=epochs,\n    validation_data=validation_generator,\n    validation_steps=total_validate//batch_size,\n    steps_per_epoch=total_train//batch_size,\n    #callbacks=callbacks\n)","metadata":{"_uuid":"0836a4cc8aa0abf603e0f96573c0c4ff383ad56b","execution":{"iopub.status.busy":"2023-05-21T14:13:45.213267Z","iopub.execute_input":"2023-05-21T14:13:45.213567Z","iopub.status.idle":"2023-05-21T14:14:16.782535Z","shell.execute_reply.started":"2023-05-21T14:13:45.213511Z","shell.execute_reply":"2023-05-21T14:14:16.78121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Model","metadata":{"_uuid":"aa1fbc4081ae0de2993188b2bf658a0be5bc0687"}},{"cell_type":"code","source":"# model.save_weights(\"model_checkBox.h5\")\nfrom tensorflow import keras\nmodel.save('model_checkBox.h5')\n","metadata":{"_uuid":"67575a4decdaf79a915d23151626b784ffa82642","execution":{"iopub.status.busy":"2023-05-18T10:45:27.849326Z","iopub.execute_input":"2023-05-18T10:45:27.849631Z","iopub.status.idle":"2023-05-18T10:45:28.116363Z","shell.execute_reply.started":"2023-05-18T10:45:27.849576Z","shell.execute_reply":"2023-05-18T10:45:28.115282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('/kaggle/working/model_checkBox.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-18T10:48:10.634122Z","iopub.execute_input":"2023-05-18T10:48:10.634449Z","iopub.status.idle":"2023-05-18T10:48:13.560798Z","shell.execute_reply.started":"2023-05-18T10:48:10.634391Z","shell.execute_reply":"2023-05-18T10:48:13.560042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-18T11:52:40.567572Z","iopub.execute_input":"2023-05-18T11:52:40.567868Z","iopub.status.idle":"2023-05-18T11:52:40.58281Z","shell.execute_reply.started":"2023-05-18T11:52:40.567817Z","shell.execute_reply":"2023-05-18T11:52:40.582104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Virtualize Training","metadata":{"_uuid":"1b76c0a9040bc0babf0a453e567e41e22f8a1e0e"}},{"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\")\nax1.set_xticks(np.arange(1, epochs, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\n\nax2.plot(history.history['acc'], color='b', label=\"Training accuracy\")\nax2.plot(history.history['val_acc'], color='r',label=\"Validation accuracy\")\nax2.set_xticks(np.arange(1, epochs, 1))\n\nlegend = plt.legend(loc='best', shadow=True)\nplt.tight_layout()\nplt.show()","metadata":{"_uuid":"79055f2dc3e2abb47bea758e0464c86ca42ab431","execution":{"iopub.status.busy":"2023-05-21T14:15:26.873107Z","iopub.execute_input":"2023-05-21T14:15:26.873544Z","iopub.status.idle":"2023-05-21T14:15:28.596328Z","shell.execute_reply.started":"2023-05-21T14:15:26.873481Z","shell.execute_reply":"2023-05-21T14:15:28.59552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare Testing Data","metadata":{"_uuid":"764dc66e4b2bc558f3a0f90b80bb802f5b3d45a8"}},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/input/checkbox\")\ntest_df = pd.DataFrame({\n    'filename': test_filenames\n})\nnb_samples = test_df.shape[0]","metadata":{"_uuid":"c35e70d3e1e4834dbbf840fa0ea08c049bfcd915","execution":{"iopub.status.busy":"2023-05-21T14:15:37.153775Z","iopub.execute_input":"2023-05-21T14:15:37.154268Z","iopub.status.idle":"2023-05-21T14:15:37.160978Z","shell.execute_reply.started":"2023-05-21T14:15:37.154057Z","shell.execute_reply":"2023-05-21T14:15:37.159938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Testing Generator","metadata":{"_uuid":"291bc3996dce8d05e174b27d64f03996d3e8038e"}},{"cell_type":"code","source":"test_gen = ImageDataGenerator(rescale=1./255)\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"/kaggle/input/checkbox\", \n    x_col='filename',\n    y_col=None,\n    class_mode=None,\n    target_size=IMAGE_SIZE,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"_uuid":"52249ec1c35fb1be3adef386be245de3794e55aa","execution":{"iopub.status.busy":"2023-05-21T14:15:41.225651Z","iopub.execute_input":"2023-05-21T14:15:41.225983Z","iopub.status.idle":"2023-05-21T14:15:41.316487Z","shell.execute_reply.started":"2023-05-21T14:15:41.225922Z","shell.execute_reply":"2023-05-21T14:15:41.315627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predict","metadata":{"_uuid":"2fa580afca2931ec5ce374e732d8c1789d03f2ed"}},{"cell_type":"code","source":"predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size))","metadata":{"_uuid":"4782eb23fa7d003f0e2415d995894017edb2d896","execution":{"iopub.status.busy":"2023-05-21T14:15:46.251544Z","iopub.execute_input":"2023-05-21T14:15:46.252052Z","iopub.status.idle":"2023-05-21T14:15:46.633381Z","shell.execute_reply.started":"2023-05-21T14:15:46.251799Z","shell.execute_reply":"2023-05-21T14:15:46.632004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"For categoral classication the prediction will come with probability of each category. So we will pick the category that have the highest probability with numpy average max","metadata":{}},{"cell_type":"code","source":"test_df['marked'] = np.argmax(predict, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:15:48.565443Z","iopub.execute_input":"2023-05-21T14:15:48.565756Z","iopub.status.idle":"2023-05-21T14:15:48.571402Z","shell.execute_reply.started":"2023-05-21T14:15:48.565697Z","shell.execute_reply":"2023-05-21T14:15:48.570505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We will convert the predict category back into our generator classes by using `train_generator.class_indices`. It is the classes that image generator map while converting data into computer vision","metadata":{}},{"cell_type":"code","source":"label_map = dict((v,k) for k,v in train_generator.class_indices.items())\ntest_df['marked'] = test_df['marked'].replace(label_map)","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:15:50.890945Z","iopub.execute_input":"2023-05-21T14:15:50.891264Z","iopub.status.idle":"2023-05-21T14:15:50.897641Z","shell.execute_reply.started":"2023-05-21T14:15:50.891209Z","shell.execute_reply":"2023-05-21T14:15:50.896894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From our prepare data part. We map data with `{1: 'dog', 0: 'cat'}`. Now we will map the result back to dog is 1 and cat is 0","metadata":{}},{"cell_type":"code","source":"test_df['marked'] = test_df['marked'].replace({ 'marked': 1, 'unmarked': 0 })","metadata":{"execution":{"iopub.status.busy":"2023-05-21T14:15:52.847282Z","iopub.execute_input":"2023-05-21T14:15:52.847583Z","iopub.status.idle":"2023-05-21T14:15:52.856227Z","shell.execute_reply.started":"2023-05-21T14:15:52.847527Z","shell.execute_reply":"2023-05-21T14:15:52.855424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Virtaulize Result","metadata":{"_uuid":"b00add65fe765529e637c3a9904d710bb7eff1d8"}},{"cell_type":"code","source":"test_df['marked'].value_counts().plot.bar()","metadata":{"_uuid":"d0bf6dd5ff344092fa0121f70bdd60fa5a40e29c","execution":{"iopub.status.busy":"2023-05-21T14:15:54.774908Z","iopub.execute_input":"2023-05-21T14:15:54.775234Z","iopub.status.idle":"2023-05-21T14:15:55.015036Z","shell.execute_reply.started":"2023-05-21T14:15:54.775177Z","shell.execute_reply":"2023-05-21T14:15:55.014007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### See predicted result with images","metadata":{"_uuid":"ce72a83f80d6e012b12b82c8ee3365d671a3b307"}},{"cell_type":"code","source":"sample_test = test_df.head(18)\nsample_test.head()\nplt.figure(figsize=(12, 24))\nfor index, row in sample_test.iterrows():\n    filename = row['filename']\n    category = row['marked']\n    img = load_img(\"/kaggle/input/checkbox/\"+filename, target_size=IMAGE_SIZE)\n    plt.subplot(6, 3, index+1)\n    plt.imshow(img)\n    plt.xlabel(filename + '(' + \"{}\".format(category) + ')' )\nplt.tight_layout()\nplt.show()\n\n\n","metadata":{"_uuid":"98b41dc83075e6297137fb45bf703c313dd4ae28","execution":{"iopub.status.busy":"2023-05-21T14:15:57.205423Z","iopub.execute_input":"2023-05-21T14:15:57.205726Z","iopub.status.idle":"2023-05-21T14:16:01.692161Z","shell.execute_reply.started":"2023-05-21T14:15:57.205669Z","shell.execute_reply":"2023-05-21T14:16:01.691234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{"_uuid":"d1ca25943e73aa20a37f9fb8670ee430caeaaf1f"}},{"cell_type":"code","source":"submission_df = test_df.copy()\nsubmission_df['id'] = submission_df['filename'].str.split('.').str[0]\nsubmission_df['label'] = submission_df['marked']\nsubmission_df.drop(['filename', 'marked'], axis=1, inplace=True)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"_uuid":"cce9f3e2ffff0693d79d84590ed71fbbca7c3c7c","execution":{"iopub.status.busy":"2023-05-11T09:39:38.830449Z","iopub.execute_input":"2023-05-11T09:39:38.830918Z","iopub.status.idle":"2023-05-11T09:39:38.843298Z","shell.execute_reply.started":"2023-05-11T09:39:38.83087Z","shell.execute_reply":"2023-05-11T09:39:38.842242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ncsv_path = '/kaggle/input/original1/original1_annotations.csv'\ndef get_checkbox_labels(csv_path):\n    print(csv_path)\n    data = pd.read_csv(csv_path)\n    checkbox_labels = []\n    for index, row in data.iterrows():\n        if row['label_name'].endswith('CheckBox'):\n            checkbox_labels.append(row['label_name'])\n    return checkbox_labels\n\nlabels = get_checkbox_labels(csv_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-11T09:39:38.844779Z","iopub.execute_input":"2023-05-11T09:39:38.84535Z","iopub.status.idle":"2023-05-11T09:39:38.884264Z","shell.execute_reply.started":"2023-05-11T09:39:38.845167Z","shell.execute_reply":"2023-05-11T09:39:38.883397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-05-11T09:39:38.885584Z","iopub.execute_input":"2023-05-11T09:39:38.885867Z","iopub.status.idle":"2023-05-11T09:39:38.895048Z","shell.execute_reply.started":"2023-05-11T09:39:38.88582Z","shell.execute_reply":"2023-05-11T09:39:38.894248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport numpy as np\nimport pandas as pd\n\n# Load the annotations CSV file\nannotations = pd.read_csv(\"/kaggle/input/original1/original1_annotations.csv\")\n\n# Define the labels you are interested in detecting\nlabels_of_interest = [\"PL_CheckBox\", \"BL_CheckBox\", \"CV_CheckBox\"]\n\n# Filter the annotations to only include the labels of interest\nannotations = annotations[annotations[\"label_name\"].isin(labels_of_interest)]\n\n# Load the image\nimage = cv2.imread(\"/kaggle/input/original1/original1.jpg\")\nimage = cv2.resize(image, (128, 128))\n\n# Convert the annotations to a tensor\nannotation_tensor = np.zeros((len(annotations), 5))\nfor i, annotation in enumerate(annotations.values):\n    annotation_tensor[i, 0] = labels_of_interest.index(annotation[0])\n    annotation_tensor[i, 1:] = annotation[1:5]\n\n# Make a prediction\nprediction_tensor = model.predict(np.array([image]))\n\n# Define the classes for visualization\nclasses = [\"background\"] + labels_of_interest\n\n# Visualize the results\nif prediction_tensor.size > 0:\n    for i, prediction in enumerate(prediction_tensor):\n        class_id = np.argmax(prediction[5:]) + 5\n        class_name = classes[class_id]\n        if class_name not in labels_of_interest:\n            continue\n        confidence = prediction[class_id]\n        bbox = prediction[1:5]\n\n        if confidence > 0.5:\n            x1, y1, x2, y2 = bbox * np.array([image.shape[1], image.shape[0], image.shape[1], image.shape[0]])\n            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)\n            cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)\n            cv2.putText(image, f\"{class_name}: {confidence:.2f}\", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)\n\ncv2.imshow(\"Image with Predictions\", image)\ncv2.waitKey(0)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-11T10:04:32.830623Z","iopub.execute_input":"2023-05-11T10:04:32.830949Z","iopub.status.idle":"2023-05-11T10:04:32.910226Z","shell.execute_reply.started":"2023-05-11T10:04:32.830892Z","shell.execute_reply":"2023-05-11T10:04:32.909155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}