{"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNGqlg8SWFn/otEqL+KDrff"},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":9988,"databundleVersionId":868324,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# L'importation des dépandences\n","metadata":{"id":"xnFS4XLFTDNg"}},{"cell_type":"code","source":"import os\nfrom skimage.io import imread\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom skimage.segmentation import mark_boundaries\nimport numpy as np\nfrom skimage.morphology import label\n","metadata":{"id":"FaLNdt_v58pV","execution":{"iopub.status.busy":"2023-11-18T17:54:51.53886Z","iopub.execute_input":"2023-11-18T17:54:51.539431Z","iopub.status.idle":"2023-11-18T17:54:54.198738Z","shell.execute_reply.started":"2023-11-18T17:54:51.539398Z","shell.execute_reply":"2023-11-18T17:54:54.197706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis (EDA)","metadata":{"id":"1yPhUbhjTrt-"}},{"cell_type":"code","source":"train_image_dir = os.path.join('../input/airbus-ship-detection', 'train_v2')\ntest_image_dir = os.path.join('../input/airbus-ship-detection', 'test_v2')","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:54.200763Z","iopub.execute_input":"2023-11-18T17:54:54.201265Z","iopub.status.idle":"2023-11-18T17:54:54.206341Z","shell.execute_reply.started":"2023-11-18T17:54:54.201233Z","shell.execute_reply":"2023-11-18T17:54:54.204829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = pd.read_csv(os.path.join('../input/airbus-ship-detection', 'train_ship_segmentations_v2.csv'))\nprint(masks.shape[0], 'masks found')\nprint(masks['ImageId'].value_counts().shape[0])\nmasks.head()","metadata":{"id":"D_nC_MV6GjSG","executionInfo":{"status":"error","timestamp":1700226043104,"user_tz":-60,"elapsed":6,"user":{"displayName":"Abderahmen Dhaouadi","userId":"01851801202066046338"}},"outputId":"37bd0df6-146e-482e-c289-9ba59952088b","execution":{"iopub.status.busy":"2023-11-18T17:54:54.207923Z","iopub.execute_input":"2023-11-18T17:54:54.208261Z","iopub.status.idle":"2023-11-18T17:54:55.511737Z","shell.execute_reply.started":"2023-11-18T17:54:54.208234Z","shell.execute_reply":"2023-11-18T17:54:55.51073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks.describe()","metadata":{"id":"Ph8jX2keT7zB","execution":{"iopub.status.busy":"2023-11-18T17:54:55.513133Z","iopub.execute_input":"2023-11-18T17:54:55.51353Z","iopub.status.idle":"2023-11-18T17:54:55.775695Z","shell.execute_reply.started":"2023-11-18T17:54:55.513494Z","shell.execute_reply":"2023-11-18T17:54:55.774963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks.tail()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:55.778693Z","iopub.execute_input":"2023-11-18T17:54:55.779826Z","iopub.status.idle":"2023-11-18T17:54:55.790737Z","shell.execute_reply.started":"2023-11-18T17:54:55.779785Z","shell.execute_reply":"2023-11-18T17:54:55.789398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:55.79208Z","iopub.execute_input":"2023-11-18T17:54:55.792388Z","iopub.status.idle":"2023-11-18T17:54:55.835799Z","shell.execute_reply.started":"2023-11-18T17:54:55.792363Z","shell.execute_reply":"2023-11-18T17:54:55.834643Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def verifie_repetition_image_id(masque):\n    repetitions = masque['ImageId'].duplicated().any()\n\n    if repetitions:\n        print(\"Il y a des 'ImageId' qui se répètent dans le masque.\")\n    else:\n        print(\"Aucun 'ImageId' ne se répète dans le masque.\")\nverifie_repetition_image_id(masks)","metadata":{"id":"nNL_w7QPG7M3","executionInfo":{"status":"ok","timestamp":1699651262894,"user_tz":-60,"elapsed":291,"user":{"displayName":"Abderahmen Dhaouadi","userId":"01851801202066046338"}},"outputId":"52a1593c-9a90-4376-c073-2308d6ed5db5","execution":{"iopub.status.busy":"2023-11-18T17:54:55.837248Z","iopub.execute_input":"2023-11-18T17:54:55.837652Z","iopub.status.idle":"2023-11-18T17:54:55.865794Z","shell.execute_reply.started":"2023-11-18T17:54:55.837623Z","shell.execute_reply":"2023-11-18T17:54:55.86445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks.dtypes","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:55.867491Z","iopub.execute_input":"2023-11-18T17:54:55.867867Z","iopub.status.idle":"2023-11-18T17:54:55.877993Z","shell.execute_reply.started":"2023-11-18T17:54:55.867835Z","shell.execute_reply":"2023-11-18T17:54:55.876909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import io\n\nimage = io.imread('../input/airbus-ship-detection/test_v2/002a943bf.jpg')\n\n\nio.imshow(image)\nio.show()\n\nio.imshow(np.transpose(image,(1,0,2)))\nio.show()\n\n\nprint(label(image))\nprint(image.shape)\nprint(\"\\n*************************************************************************\\n\")\nprint(image[:,:,2])\nprint(\"\\n*************************************************************************\\n\")\n\nprint(\"\\n*************************************************************************\\n\")\nlabels=label(image[:,:,0])\nprint(len(np.unique(labels[labels>0])))\nprint(\"\\n*************************************************************************\\n\")\nprint(image[:,:,2].shape)\n\n","metadata":{"id":"XfOGI3E7AbM1","executionInfo":{"status":"ok","timestamp":1699651064261,"user_tz":-60,"elapsed":1060,"user":{"displayName":"Abderahmen Dhaouadi","userId":"01851801202066046338"}},"outputId":"d1ff4bf6-6cef-4087-a3dc-7752e15011cb","execution":{"iopub.status.busy":"2023-11-18T17:54:55.879499Z","iopub.execute_input":"2023-11-18T17:54:55.87984Z","iopub.status.idle":"2023-11-18T17:54:56.935182Z","shell.execute_reply.started":"2023-11-18T17:54:55.879811Z","shell.execute_reply":"2023-11-18T17:54:56.934036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = io.imread('/kaggle/input/airbus-ship-detection/test_v2/004946ceb.jpg')\nlabels = label(image[:, :, 0])\nprint(labels==2)\nprint(image)\nprint(\"\\n***************************************\\n\")\nprint(label(image))","metadata":{"id":"_S-udibSGagz","execution":{"iopub.status.busy":"2023-11-18T17:54:56.936969Z","iopub.execute_input":"2023-11-18T17:54:56.937725Z","iopub.status.idle":"2023-11-18T17:54:57.017691Z","shell.execute_reply.started":"2023-11-18T17:54:56.937692Z","shell.execute_reply":"2023-11-18T17:54:57.016803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = masks.assign(ships=masks['EncodedPixels'].map(lambda row: 1 if isinstance(row, str) else 0))\nmasks_with_unique_ids = masks.groupby('ImageId').agg({'ships': 'sum'}).reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:57.018816Z","iopub.execute_input":"2023-11-18T17:54:57.019115Z","iopub.status.idle":"2023-11-18T17:54:57.306815Z","shell.execute_reply.started":"2023-11-18T17:54:57.01909Z","shell.execute_reply":"2023-11-18T17:54:57.305923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks_with_unique_ids.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:57.307928Z","iopub.execute_input":"2023-11-18T17:54:57.308284Z","iopub.status.idle":"2023-11-18T17:54:57.317968Z","shell.execute_reply.started":"2023-11-18T17:54:57.308257Z","shell.execute_reply":"2023-11-18T17:54:57.316901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks_with_unique_ids['ships'].hist()","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:54:57.319321Z","iopub.execute_input":"2023-11-18T17:54:57.319696Z","iopub.status.idle":"2023-11-18T17:54:57.609668Z","shell.execute_reply.started":"2023-11-18T17:54:57.31966Z","shell.execute_reply":"2023-11-18T17:54:57.608651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataframe = masks_with_unique_ids.groupby('ships').apply(lambda x: x.sample(2000) if len(x) > 2000 else x)\ndataframe['ships'].hist(bins=dataframe['ships'].max()+1)\nprint(dataframe.shape[0], 'masks')","metadata":{"execution":{"iopub.status.busy":"2023-11-18T17:56:31.42994Z","iopub.execute_input":"2023-11-18T17:56:31.430385Z","iopub.status.idle":"2023-11-18T17:56:31.753051Z","shell.execute_reply.started":"2023-11-18T17:56:31.430352Z","shell.execute_reply":"2023-11-18T17:56:31.751834Z"},"trusted":true},"execution_count":null,"outputs":[]}]}