{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-17T13:12:08.742387Z","iopub.execute_input":"2021-10-17T13:12:08.74267Z","iopub.status.idle":"2021-10-17T13:12:08.751949Z","shell.execute_reply.started":"2021-10-17T13:12:08.74264Z","shell.execute_reply":"2021-10-17T13:12:08.750921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport keras.backend as K\nfrom zipfile import ZipFile\nimport zipfile","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:08.762794Z","iopub.execute_input":"2021-10-17T13:12:08.763092Z","iopub.status.idle":"2021-10-17T13:12:15.009408Z","shell.execute_reply.started":"2021-10-17T13:12:08.763062Z","shell.execute_reply":"2021-10-17T13:12:15.008301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_zip_path = \"/kaggle/input/carvana-image-masking-challenge/train.zip\"\nwith zipfile.ZipFile(train_zip_path, \"r\") as z_:\n    z_.extractall(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:15.013355Z","iopub.execute_input":"2021-10-17T13:12:15.013621Z","iopub.status.idle":"2021-10-17T13:12:27.745315Z","shell.execute_reply.started":"2021-10-17T13:12:15.01359Z","shell.execute_reply":"2021-10-17T13:12:27.744507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks_zip_path = \"/kaggle/input/carvana-image-masking-challenge/train_masks.zip\"\nwith zipfile.ZipFile(masks_zip_path, \"r\") as z_:\n    z_.extractall(\"/kaggle/working\")","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:27.746297Z","iopub.execute_input":"2021-10-17T13:12:27.746649Z","iopub.status.idle":"2021-10-17T13:12:28.814325Z","shell.execute_reply.started":"2021-10-17T13:12:27.746619Z","shell.execute_reply":"2021-10-17T13:12:28.813577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"car_ids = []\npaths = []\n\nfor dirname, _, filenames in os.walk(\"/kaggle/working/train\"):\n    for filename in filenames:\n        path = os.path.join(dirname, filename)\n        paths.append(path)\n        \n        car_id = filename.split(\".\")[0]\n        car_ids.append(car_id)\n        \ndf = pd.DataFrame({\"id\": car_ids, \"car_path\": paths})\ndf = df.set_index(\"id\")","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:28.816183Z","iopub.execute_input":"2021-10-17T13:12:28.816414Z","iopub.status.idle":"2021-10-17T13:12:28.857941Z","shell.execute_reply.started":"2021-10-17T13:12:28.816386Z","shell.execute_reply":"2021-10-17T13:12:28.85702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"car_ids = []\nmask_path = []\n\nfor dirname, _,filenames in os.walk(\"/kaggle/working/train_masks\"):\n    for filename in filenames:\n        path = os.path.join(dirname, filename)\n        mask_path.append(path)\n        \n        car_id = filename.split(\".\")[0]\n        car_id = car_id.split(\"_mask\")[0]\n        car_ids.append(car_id)\n        \n        \nmask_df = pd.DataFrame({\"id\": car_ids, \"mask_path\": mask_path})\nmask_df = mask_df.set_index(\"id\")","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:28.859636Z","iopub.execute_input":"2021-10-17T13:12:28.859972Z","iopub.status.idle":"2021-10-17T13:12:28.893298Z","shell.execute_reply.started":"2021-10-17T13:12:28.859928Z","shell.execute_reply":"2021-10-17T13:12:28.892526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"mask_path\"] = mask_df[\"mask_path\"]\ndf = df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:28.894925Z","iopub.execute_input":"2021-10-17T13:12:28.895214Z","iopub.status.idle":"2021-10-17T13:12:28.909853Z","shell.execute_reply.started":"2021-10-17T13:12:28.895176Z","shell.execute_reply":"2021-10-17T13:12:28.908944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:08:54.381554Z","iopub.execute_input":"2021-10-17T13:08:54.381847Z","iopub.status.idle":"2021-10-17T13:08:54.388314Z","shell.execute_reply.started":"2021-10-17T13:08:54.381817Z","shell.execute_reply":"2021-10-17T13:08:54.387129Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ntrain_df, valid_df = train_test_split(df, random_state=42, test_size=0.25)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:12:45.623697Z","iopub.execute_input":"2021-10-17T13:12:45.62404Z","iopub.status.idle":"2021-10-17T13:12:45.630503Z","shell.execute_reply.started":"2021-10-17T13:12:45.623998Z","shell.execute_reply":"2021-10-17T13:12:45.629811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display(display_list):\n    plt.figure(figsize=(15,15))\n    \n    title = [\"Input image\", \"True mask\", \"Predicted_mask\"]\n    \n    for i in range(len(display_list)):\n        plt.subplot(1, len(display_list), i + 1)\n        plt.title(title[i])           \n        plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]))\n        plt.axis(\"off\")\n                   \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:13:14.742839Z","iopub.execute_input":"2021-10-17T13:13:14.743663Z","iopub.status.idle":"2021-10-17T13:13:14.750826Z","shell.execute_reply.started":"2021-10-17T13:13:14.743609Z","shell.execute_reply":"2021-10-17T13:13:14.749679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image, mask in train_df.take(1):\n    sample_image, sample_mask = image, mask\n    display([sample_image, sample_mask])","metadata":{"execution":{"iopub.status.busy":"2021-10-17T13:13:45.62823Z","iopub.execute_input":"2021-10-17T13:13:45.628498Z","iopub.status.idle":"2021-10-17T13:13:45.670401Z","shell.execute_reply.started":"2021-10-17T13:13:45.62847Z","shell.execute_reply":"2021-10-17T13:13:45.669181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_kg_hide-input":false},"execution_count":null,"outputs":[]}]}