{"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\nbase_dir = '/kaggle/input/carvana-image-masking-challenge'\n\nos.listdir(base_dir)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-11T03:41:22.826562Z","iopub.execute_input":"2022-03-11T03:41:22.826961Z","iopub.status.idle":"2022-03-11T03:41:22.865008Z","shell.execute_reply.started":"2022-03-11T03:41:22.826868Z","shell.execute_reply":"2022-03-11T03:41:22.864078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimg = plt.imread(os.path.join(base_dir, '29bb3ece3180_11.jpg'))\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:41:25.956414Z","iopub.execute_input":"2022-03-11T03:41:25.956760Z","iopub.status.idle":"2022-03-11T03:41:26.547913Z","shell.execute_reply.started":"2022-03-11T03:41:25.956725Z","shell.execute_reply":"2022-03-11T03:41:26.546585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -q ../input/carvana-image-masking-challenge/train.zip -d train/","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:41:28.399675Z","iopub.execute_input":"2022-03-11T03:41:28.399974Z","iopub.status.idle":"2022-03-11T03:41:37.363642Z","shell.execute_reply.started":"2022-03-11T03:41:28.399942Z","shell.execute_reply":"2022-03-11T03:41:37.362722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip -q ../input/carvana-image-masking-challenge/train_masks.zip -d train_masks/","metadata":{"execution":{"iopub.status.busy":"2022-03-11T03:41:37.365389Z","iopub.execute_input":"2022-03-11T03:41:37.365775Z","iopub.status.idle":"2022-03-11T03:41:38.768123Z","shell.execute_reply.started":"2022-03-11T03:41:37.365727Z","shell.execute_reply":"2022-03-11T03:41:38.766845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_imgs = os.listdir('train/train/')\ntrain_masks = os.listdir('train_masks/train_masks')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T04:48:43.441327Z","iopub.execute_input":"2022-03-11T04:48:43.441611Z","iopub.status.idle":"2022-03-11T04:48:43.453029Z","shell.execute_reply.started":"2022-03-11T04:48:43.441582Z","shell.execute_reply":"2022-03-11T04:48:43.452166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_imgs)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T04:48:44.373713Z","iopub.execute_input":"2022-03-11T04:48:44.374002Z","iopub.status.idle":"2022-03-11T04:48:44.379515Z","shell.execute_reply.started":"2022-03-11T04:48:44.373961Z","shell.execute_reply":"2022-03-11T04:48:44.378785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_masks)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T04:48:45.136589Z","iopub.execute_input":"2022-03-11T04:48:45.136892Z","iopub.status.idle":"2022-03-11T04:48:45.143014Z","shell.execute_reply.started":"2022-03-11T04:48:45.136863Z","shell.execute_reply":"2022-03-11T04:48:45.141959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = []\ntrain_img_masks = []\nids1 = []\nids2 = []\n\nfor i in train_imgs:\n    ids1.append(i.split('.')[0])\n    train_images.append('train/train/'+i)\n    \nfor i in train_masks:\n    car_id = i.split(\".\")[0]\n    ids2.append(car_id.split(\"_mask\")[0])\n    train_img_masks.append('train_masks/train_masks/'+i)\n    \nprint(ids1[0])\nprint(ids2[0])\nprint(train_images[0])\nprint(train_img_masks[0])","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:16:59.670570Z","iopub.execute_input":"2022-03-11T05:16:59.670923Z","iopub.status.idle":"2022-03-11T05:16:59.688992Z","shell.execute_reply.started":"2022-03-11T05:16:59.670891Z","shell.execute_reply":"2022-03-11T05:16:59.687986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d1 = {'id':ids1, 'img':train_images}\ndf1 = pd.DataFrame(data=d1)\n\nd2 = {'id':ids2, 'mask':train_img_masks}\ndf2 = pd.DataFrame(data=d2)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:00.650154Z","iopub.execute_input":"2022-03-11T05:17:00.650444Z","iopub.status.idle":"2022-03-11T05:17:00.660049Z","shell.execute_reply.started":"2022-03-11T05:17:00.650413Z","shell.execute_reply":"2022-03-11T05:17:00.659334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:01.397595Z","iopub.execute_input":"2022-03-11T05:17:01.398297Z","iopub.status.idle":"2022-03-11T05:17:01.409048Z","shell.execute_reply.started":"2022-03-11T05:17:01.398248Z","shell.execute_reply":"2022-03-11T05:17:01.408251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:02.024827Z","iopub.execute_input":"2022-03-11T05:17:02.025129Z","iopub.status.idle":"2022-03-11T05:17:02.036781Z","shell.execute_reply.started":"2022-03-11T05:17:02.025100Z","shell.execute_reply":"2022-03-11T05:17:02.035597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.merge(df1, df2, on='id')","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:02.819250Z","iopub.execute_input":"2022-03-11T05:17:02.820212Z","iopub.status.idle":"2022-03-11T05:17:02.831446Z","shell.execute_reply.started":"2022-03-11T05:17:02.820163Z","shell.execute_reply":"2022-03-11T05:17:02.830709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:03.408596Z","iopub.execute_input":"2022-03-11T05:17:03.409363Z","iopub.status.idle":"2022-03-11T05:17:03.420407Z","shell.execute_reply.started":"2022-03-11T05:17:03.409321Z","shell.execute_reply":"2022-03-11T05:17:03.419535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nds = tf.data.Dataset.from_tensor_slices((df['img'].values, df['mask'].values))","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:32.145650Z","iopub.execute_input":"2022-03-11T05:17:32.146227Z","iopub.status.idle":"2022-03-11T05:17:32.153591Z","shell.execute_reply.started":"2022-03-11T05:17:32.146187Z","shell.execute_reply":"2022-03-11T05:17:32.152842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_img(img_name, mask_name):\n    img = tf.io.read_file(img_name)\n    img = tf.io.decode_image(img)\n    mask = tf.io.read_file(mask_name)\n    mask = tf.io.decode_image(mask)\n    mask = tf.squeeze(mask)\n    mask = mask[:,:,:1]\n    \n    return img, mask","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:32.946187Z","iopub.execute_input":"2022-03-11T05:17:32.946951Z","iopub.status.idle":"2022-03-11T05:17:32.952488Z","shell.execute_reply.started":"2022-03-11T05:17:32.946902Z","shell.execute_reply":"2022-03-11T05:17:32.951609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = ds.map(load_img, tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:33.658775Z","iopub.execute_input":"2022-03-11T05:17:33.659679Z","iopub.status.idle":"2022-03-11T05:17:33.745036Z","shell.execute_reply.started":"2022-03-11T05:17:33.659596Z","shell.execute_reply":"2022-03-11T05:17:33.744344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\none = ds.take(5)\nfor a, b in one:\n    plt.imshow(a)\n    plt.show()\n    plt.imshow(b)\n    plt.show()\n    print(a.shape)\n    print(b.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:34.577263Z","iopub.execute_input":"2022-03-11T05:17:34.578143Z","iopub.status.idle":"2022-03-11T05:17:39.705915Z","shell.execute_reply.started":"2022-03-11T05:17:34.578097Z","shell.execute_reply":"2022-03-11T05:17:39.704944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_img(img, mask):\n    img = tf.image.resize(img, [256, 256])\n    mask = tf.image.resize(mask, [256, 256])\n    \n    return img, mask","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:39.707595Z","iopub.execute_input":"2022-03-11T05:17:39.707868Z","iopub.status.idle":"2022-03-11T05:17:39.712806Z","shell.execute_reply.started":"2022-03-11T05:17:39.707837Z","shell.execute_reply":"2022-03-11T05:17:39.712069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = ds.map(resize_img, tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-03-11T05:17:39.714118Z","iopub.execute_input":"2022-03-11T05:17:39.714905Z","iopub.status.idle":"2022-03-11T05:17:39.769941Z","shell.execute_reply.started":"2022-03-11T05:17:39.714870Z","shell.execute_reply":"2022-03-11T05:17:39.768898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}