{"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        if filename[-3:] != 'jpg':\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-09-29T21:50:35.77761Z","iopub.execute_input":"2021-09-29T21:50:35.777976Z","iopub.status.idle":"2021-09-29T21:50:35.803291Z","shell.execute_reply.started":"2021-09-29T21:50:35.777944Z","shell.execute_reply":"2021-09-29T21:50:35.802417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Examine Data","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if filename[-3:] == 'jpg':\n            print(os.path.join(dirname, filename))\n            break","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:39.274757Z","iopub.execute_input":"2021-09-29T21:50:39.275134Z","iopub.status.idle":"2021-09-29T21:50:39.289735Z","shell.execute_reply.started":"2021-09-29T21:50:39.2751Z","shell.execute_reply":"2021-09-29T21:50:39.288454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# examine train CSV\n\ndf_train = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\n\ndf_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:39.845175Z","iopub.execute_input":"2021-09-29T21:50:39.845606Z","iopub.status.idle":"2021-09-29T21:50:40.253213Z","shell.execute_reply.started":"2021-09-29T21:50:39.845567Z","shell.execute_reply":"2021-09-29T21:50:40.252045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:40.428972Z","iopub.execute_input":"2021-09-29T21:50:40.429295Z","iopub.status.idle":"2021-09-29T21:50:40.438606Z","shell.execute_reply.started":"2021-09-29T21:50:40.429265Z","shell.execute_reply":"2021-09-29T21:50:40.437204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.describe()\n\n# all images are the same size","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:41.013163Z","iopub.execute_input":"2021-09-29T21:50:41.01351Z","iopub.status.idle":"2021-09-29T21:50:41.058924Z","shell.execute_reply.started":"2021-09-29T21:50:41.01348Z","shell.execute_reply":"2021-09-29T21:50:41.057723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['source'].value_counts()\n\n# 7 different sources. Might be worthwhile to include this column in the model","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:41.614356Z","iopub.execute_input":"2021-09-29T21:50:41.614674Z","iopub.status.idle":"2021-09-29T21:50:41.644769Z","shell.execute_reply.started":"2021-09-29T21:50:41.614633Z","shell.execute_reply":"2021-09-29T21:50:41.643854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ex = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\n\ndf_ex.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:42.079837Z","iopub.execute_input":"2021-09-29T21:50:42.080189Z","iopub.status.idle":"2021-09-29T21:50:42.107192Z","shell.execute_reply.started":"2021-09-29T21:50:42.080157Z","shell.execute_reply":"2021-09-29T21:50:42.106199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ex.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:42.461186Z","iopub.execute_input":"2021-09-29T21:50:42.461579Z","iopub.status.idle":"2021-09-29T21:50:42.469587Z","shell.execute_reply.started":"2021-09-29T21:50:42.461543Z","shell.execute_reply":"2021-09-29T21:50:42.468289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dirnames = []\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    dirnames.append(dirname)\n    \ndirnames = set(dirnames)\n\ndirnames","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:42.885418Z","iopub.execute_input":"2021-09-29T21:50:42.885756Z","iopub.status.idle":"2021-09-29T21:50:42.902169Z","shell.execute_reply.started":"2021-09-29T21:50:42.885722Z","shell.execute_reply":"2021-09-29T21:50:42.901294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_uris = []\ntest_uris = []\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if dirname == '/kaggle/input/global-wheat-detection/train':\n            train_uris.append(os.path.join(dirname, filename))\n        elif dirname == '/kaggle/input/global-wheat-detection/test':\n            test_uris.append(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:43.285146Z","iopub.execute_input":"2021-09-29T21:50:43.285506Z","iopub.status.idle":"2021-09-29T21:50:43.308989Z","shell.execute_reply.started":"2021-09-29T21:50:43.285471Z","shell.execute_reply":"2021-09-29T21:50:43.307954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_uris)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:43.78998Z","iopub.execute_input":"2021-09-29T21:50:43.790376Z","iopub.status.idle":"2021-09-29T21:50:43.798508Z","shell.execute_reply.started":"2021-09-29T21:50:43.790337Z","shell.execute_reply":"2021-09-29T21:50:43.797343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_uris)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:44.258125Z","iopub.execute_input":"2021-09-29T21:50:44.258473Z","iopub.status.idle":"2021-09-29T21:50:44.265275Z","shell.execute_reply.started":"2021-09-29T21:50:44.258439Z","shell.execute_reply":"2021-09-29T21:50:44.264672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_uris[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:44.653499Z","iopub.execute_input":"2021-09-29T21:50:44.654538Z","iopub.status.idle":"2021-09-29T21:50:44.661627Z","shell.execute_reply.started":"2021-09-29T21:50:44.654498Z","shell.execute_reply":"2021-09-29T21:50:44.660374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import rasterio as rio","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:45.14165Z","iopub.execute_input":"2021-09-29T21:50:45.142183Z","iopub.status.idle":"2021-09-29T21:50:45.515042Z","shell.execute_reply.started":"2021-09-29T21:50:45.142148Z","shell.execute_reply":"2021-09-29T21:50:45.514258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with rio.open(train_uris[0]) as src:\n    rast = src.read()\n    \nrast.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:45.754687Z","iopub.execute_input":"2021-09-29T21:50:45.755192Z","iopub.status.idle":"2021-09-29T21:50:45.930064Z","shell.execute_reply.started":"2021-09-29T21:50:45.755158Z","shell.execute_reply":"2021-09-29T21:50:45.928806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rast[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:46.758073Z","iopub.execute_input":"2021-09-29T21:50:46.75939Z","iopub.status.idle":"2021-09-29T21:50:46.770043Z","shell.execute_reply.started":"2021-09-29T21:50:46.759319Z","shell.execute_reply":"2021-09-29T21:50:46.768622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nimg = mpimg.imread(train_uris[0])\nimgplot = plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:48.773047Z","iopub.execute_input":"2021-09-29T21:50:48.773422Z","iopub.status.idle":"2021-09-29T21:50:49.201645Z","shell.execute_reply.started":"2021-09-29T21:50:48.773366Z","shell.execute_reply":"2021-09-29T21:50:49.200808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train/Val/Test split on the training data","metadata":{}},{"cell_type":"code","source":"import random\n\n\ntrain_uris_2 = train_uris.copy()\n\nrandom.shuffle(train_uris_2)\n\nlen(train_uris_2)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:53.128866Z","iopub.execute_input":"2021-09-29T21:50:53.129876Z","iopub.status.idle":"2021-09-29T21:50:53.141275Z","shell.execute_reply.started":"2021-09-29T21:50:53.129809Z","shell.execute_reply":"2021-09-29T21:50:53.140199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"3422 * 0.6","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:53.785375Z","iopub.execute_input":"2021-09-29T21:50:53.785696Z","iopub.status.idle":"2021-09-29T21:50:53.794462Z","shell.execute_reply.started":"2021-09-29T21:50:53.785666Z","shell.execute_reply":"2021-09-29T21:50:53.793498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = train_uris_2[:2053]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:54.469902Z","iopub.execute_input":"2021-09-29T21:50:54.470823Z","iopub.status.idle":"2021-09-29T21:50:54.47616Z","shell.execute_reply.started":"2021-09-29T21:50:54.470787Z","shell.execute_reply":"2021-09-29T21:50:54.475006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"3422 * 0.2","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:56.325545Z","iopub.execute_input":"2021-09-29T21:50:56.326419Z","iopub.status.idle":"2021-09-29T21:50:56.33293Z","shell.execute_reply.started":"2021-09-29T21:50:56.326349Z","shell.execute_reply":"2021-09-29T21:50:56.331953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_set = train_uris_2[2053:2053+684]\n\ntest_set = train_uris_2[2053+684:]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:58.438269Z","iopub.execute_input":"2021-09-29T21:50:58.439257Z","iopub.status.idle":"2021-09-29T21:50:58.444097Z","shell.execute_reply.started":"2021-09-29T21:50:58.439212Z","shell.execute_reply":"2021-09-29T21:50:58.443497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_set)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:59.124967Z","iopub.execute_input":"2021-09-29T21:50:59.126121Z","iopub.status.idle":"2021-09-29T21:50:59.134052Z","shell.execute_reply.started":"2021-09-29T21:50:59.126056Z","shell.execute_reply":"2021-09-29T21:50:59.132928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(val_set)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:59.71146Z","iopub.execute_input":"2021-09-29T21:50:59.711757Z","iopub.status.idle":"2021-09-29T21:50:59.71859Z","shell.execute_reply.started":"2021-09-29T21:50:59.711728Z","shell.execute_reply":"2021-09-29T21:50:59.717281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:51:00.149669Z","iopub.execute_input":"2021-09-29T21:51:00.150562Z","iopub.status.idle":"2021-09-29T21:51:00.156879Z","shell.execute_reply.started":"2021-09-29T21:51:00.150521Z","shell.execute_reply":"2021-09-29T21:51:00.156167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert len(train_set) + len(val_set) + len(test_set) == len(train_uris_2)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:51:03.605507Z","iopub.execute_input":"2021-09-29T21:51:03.60605Z","iopub.status.idle":"2021-09-29T21:51:03.611966Z","shell.execute_reply.started":"2021-09-29T21:51:03.606002Z","shell.execute_reply":"2021-09-29T21:51:03.610775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Get Object Detection API stuff into this environment\n\nhttps://medium.com/analytics-vidhya/how-to-retrain-an-object-detection-model-with-a-custom-training-set-c827aa3eb796","metadata":{}},{"cell_type":"code","source":"import os\n\ndirnames = []\n\nfor dirname, _, filenames in os.walk('/kaggle/working'):\n    dirnames.append(dirname)\n    \ndirnames = set(dirnames)\n\ndirnames","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:46:57.397903Z","iopub.execute_input":"2021-09-29T21:46:57.398262Z","iopub.status.idle":"2021-09-29T21:46:57.407329Z","shell.execute_reply.started":"2021-09-29T21:46:57.398232Z","shell.execute_reply":"2021-09-29T21:46:57.406061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pathlib\n\n\n# Clone the tensorflow models repository if it doesn't already exist\nif \"models\" in pathlib.Path.cwd().parts:\n    while \"models\" in pathlib.Path.cwd().parts:\n        os.chdir('..')\nelif not pathlib.Path('models').exists():\n    !git clone --depth 1 https://github.com/tensorflow/models","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:47:03.233201Z","iopub.execute_input":"2021-09-29T21:47:03.233509Z","iopub.status.idle":"2021-09-29T21:47:07.879174Z","shell.execute_reply.started":"2021-09-29T21:47:03.23348Z","shell.execute_reply":"2021-09-29T21:47:07.878023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install the Object Detection API\nos.chdir('/kaggle/working/models/research/')\n!protoc object_detection/protos/*.proto --python_out=.\n!cp object_detection/packages/tf2/setup.py .\n!python -m pip install .","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:47:07.881522Z","iopub.execute_input":"2021-09-29T21:47:07.881813Z","iopub.status.idle":"2021-09-29T21:49:19.480319Z","shell.execute_reply.started":"2021-09-29T21:47:07.881775Z","shell.execute_reply":"2021-09-29T21:49:19.479114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import tensorflow libraries\n\nimport matplotlib\nimport matplotlib.pyplot as plt\n\nimport os\nimport random\nimport io\nimport imageio\nimport glob\nimport scipy.misc\nimport numpy as np\nfrom six import BytesIO\nfrom PIL import Image, ImageDraw, ImageFont\nfrom IPython.display import display, Javascript\nfrom IPython.display import Image as IPyImage\n\nimport tensorflow as tf\n\nfrom object_detection.utils import label_map_util\nfrom object_detection.utils import config_util\nfrom object_detection.utils import visualization_utils as viz_utils\n# from object_detection.utils import colab_utils ## not using colab so commented out\nfrom object_detection.builders import model_builder\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:49:19.482687Z","iopub.execute_input":"2021-09-29T21:49:19.483036Z","iopub.status.idle":"2021-09-29T21:49:23.584486Z","shell.execute_reply.started":"2021-09-29T21:49:19.482992Z","shell.execute_reply":"2021-09-29T21:49:23.583609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# test to ensure Object Detection API is installed properly\n\n!python /kaggle/working/models/research/object_detection/builders/model_builder_tf2_test.py","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:49:23.586275Z","iopub.execute_input":"2021-09-29T21:49:23.586555Z","iopub.status.idle":"2021-09-29T21:50:08.425425Z","shell.execute_reply.started":"2021-09-29T21:49:23.586525Z","shell.execute_reply":"2021-09-29T21:50:08.421847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copy images into these folders. then make TFRecord for them.\nos.mkdir('/kaggle/working/train/')\nos.mkdir('/kaggle/working/val/')","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:50:14.786045Z","iopub.execute_input":"2021-09-29T21:50:14.786546Z","iopub.status.idle":"2021-09-29T21:50:14.795653Z","shell.execute_reply.started":"2021-09-29T21:50:14.786497Z","shell.execute_reply":"2021-09-29T21:50:14.794205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:51:15.64684Z","iopub.execute_input":"2021-09-29T21:51:15.647202Z","iopub.status.idle":"2021-09-29T21:51:15.654686Z","shell.execute_reply.started":"2021-09-29T21:51:15.647172Z","shell.execute_reply":"2021-09-29T21:51:15.653732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[0].split('/')[-1]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:52:14.441918Z","iopub.execute_input":"2021-09-29T21:52:14.442261Z","iopub.status.idle":"2021-09-29T21:52:14.453058Z","shell.execute_reply.started":"2021-09-29T21:52:14.442219Z","shell.execute_reply":"2021-09-29T21:52:14.452388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(df_train)):\n    if df_train.loc[i, 'image_id'] == '824121036':\n        print(i)\n        break","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:53:47.794195Z","iopub.execute_input":"2021-09-29T21:53:47.794546Z","iopub.status.idle":"2021-09-29T21:53:48.532797Z","shell.execute_reply.started":"2021-09-29T21:53:47.794511Z","shell.execute_reply":"2021-09-29T21:53:48.532133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.loc[55452, 'image_id']","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:54:01.875769Z","iopub.execute_input":"2021-09-29T21:54:01.87706Z","iopub.status.idle":"2021-09-29T21:54:01.884631Z","shell.execute_reply.started":"2021-09-29T21:54:01.876996Z","shell.execute_reply":"2021-09-29T21:54:01.883069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shutil import copyfile\n\nbase = '/kaggle/working/train/'\n\nfor src in train_set:\n    filename = src.split('/')[-1]\n    dst = base + filename\n    copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:57:34.074632Z","iopub.execute_input":"2021-09-29T23:57:34.075001Z","iopub.status.idle":"2021-09-29T23:57:46.945561Z","shell.execute_reply.started":"2021-09-29T23:57:34.074967Z","shell.execute_reply":"2021-09-29T23:57:46.944952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_set = os.listdir('/kaggle/working/train')\n\nlen(new_train_set)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:14.205785Z","iopub.execute_input":"2021-09-29T23:59:14.206971Z","iopub.status.idle":"2021-09-29T23:59:14.213414Z","shell.execute_reply.started":"2021-09-29T23:59:14.206915Z","shell.execute_reply":"2021-09-29T23:59:14.212552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_set)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:18.435322Z","iopub.execute_input":"2021-09-29T23:59:18.436445Z","iopub.status.idle":"2021-09-29T23:59:18.442103Z","shell.execute_reply.started":"2021-09-29T23:59:18.4364Z","shell.execute_reply":"2021-09-29T23:59:18.441156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_set[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:55:58.659481Z","iopub.execute_input":"2021-09-29T21:55:58.660137Z","iopub.status.idle":"2021-09-29T21:55:58.667133Z","shell.execute_reply.started":"2021-09-29T21:55:58.660099Z","shell.execute_reply":"2021-09-29T21:55:58.66621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base = '/kaggle/working/val/'\n\nfor src in val_set:\n    filename = src.split('/')[-1]\n    dst = base + filename\n    copyfile(src, dst)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T21:56:28.83272Z","iopub.execute_input":"2021-09-29T21:56:28.833053Z","iopub.status.idle":"2021-09-29T21:56:34.688865Z","shell.execute_reply.started":"2021-09-29T21:56:28.833024Z","shell.execute_reply":"2021-09-29T21:56:34.687896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### create tfrecords file\n\nhttps://keras.io/examples/keras_recipes/creating_tfrecords/","metadata":{}},{"cell_type":"code","source":"def image_feature(value):\n    \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n    return tf.train.Feature(\n        bytes_list=tf.train.BytesList(value=[tf.io.encode_jpeg(value).numpy()])\n    )\n\n\ndef bytes_feature(value):\n    \"\"\"Returns a bytes_list from a string / byte.\"\"\"\n    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value.encode()]))\n\n\ndef float_feature(value):\n    \"\"\"Returns a float_list from a float / double.\"\"\"\n    return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))\n\n\ndef int64_feature(value):\n    \"\"\"Returns an int64_list from a bool / enum / int / uint.\"\"\"\n    return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))\n\n\ndef float_feature_list(value):\n    \"\"\"Returns a list of float_list from a float / double.\"\"\"\n    return tf.train.Feature(float_list=tf.train.FloatList(value=value))\n\n\n## changed below functions\n\ndef create_example(image, path, example):\n    feature = {\n        \"image\": image_feature(image),\n        \"path\": bytes_feature(path),\n        \"width\": float_feature(example[\"width\"]),\n        \"bbox\": float_feature_list(example[\"bbox\"]),\n        \"image_id\": int64_feature(example[\"image_id\"]),\n    }\n    return tf.train.Example(features=tf.train.Features(feature=feature))\n\n\ndef parse_tfrecord_fn(example):\n    feature_description = {\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"path\": tf.io.FixedLenFeature([], tf.string),\n        \"area\": tf.io.FixedLenFeature([], tf.float32),\n        \"bbox\": tf.io.VarLenFeature(tf.float32),\n        \"category_id\": tf.io.FixedLenFeature([], tf.int64),\n        \"id\": tf.io.FixedLenFeature([], tf.int64),\n        \"image_id\": tf.io.FixedLenFeature([], tf.int64),\n    }\n    example = tf.io.parse_single_example(example, feature_description)\n    example[\"image\"] = tf.io.decode_jpeg(example[\"image\"], channels=3)\n    example[\"bbox\"] = tf.sparse.to_dense(example[\"bbox\"])\n    return example","metadata":{"execution":{"iopub.status.busy":"2021-09-29T22:01:38.56148Z","iopub.execute_input":"2021-09-29T22:01:38.561851Z","iopub.status.idle":"2021-09-29T22:01:38.580402Z","shell.execute_reply.started":"2021-09-29T22:01:38.561819Z","shell.execute_reply":"2021-09-29T22:01:38.578918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:31.016209Z","iopub.execute_input":"2021-09-29T23:59:31.016498Z","iopub.status.idle":"2021-09-29T23:59:31.033179Z","shell.execute_reply.started":"2021-09-29T23:59:31.016471Z","shell.execute_reply":"2021-09-29T23:59:31.032309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T22:05:32.948208Z","iopub.execute_input":"2021-09-29T22:05:32.948646Z","iopub.status.idle":"2021-09-29T22:05:32.95666Z","shell.execute_reply.started":"2021-09-29T22:05:32.948608Z","shell.execute_reply":"2021-09-29T22:05:32.955421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = {'uri': new_train_set}\n\ndf_new_train = pd.DataFrame(d)\n\ndf_new_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:39.000301Z","iopub.execute_input":"2021-09-29T23:59:39.00071Z","iopub.status.idle":"2021-09-29T23:59:39.016832Z","shell.execute_reply.started":"2021-09-29T23:59:39.000678Z","shell.execute_reply":"2021-09-29T23:59:39.015953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new_train['uri'] = df_new_train['uri'].apply(lambda x: '/kaggle/working/train/' + x)\n\ndf_new_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:39.326594Z","iopub.execute_input":"2021-09-29T23:59:39.326891Z","iopub.status.idle":"2021-09-29T23:59:39.339709Z","shell.execute_reply.started":"2021-09-29T23:59:39.326863Z","shell.execute_reply":"2021-09-29T23:59:39.338885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_set[0][:-4]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:40.672122Z","iopub.execute_input":"2021-09-29T23:59:40.672753Z","iopub.status.idle":"2021-09-29T23:59:40.679595Z","shell.execute_reply.started":"2021-09-29T23:59:40.672716Z","shell.execute_reply":"2021-09-29T23:59:40.678788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_new_train['image_id'] = df_new_train['uri'].apply(lambda x: x.split('/')[-1][:-4])\n\ndf_new_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:41.935048Z","iopub.execute_input":"2021-09-29T23:59:41.935343Z","iopub.status.idle":"2021-09-29T23:59:41.96069Z","shell.execute_reply.started":"2021-09-29T23:59:41.935307Z","shell.execute_reply":"2021-09-29T23:59:41.959889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels = df_train.merge(df_new_train, on='image_id', how='right')\n\ndf_train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T23:59:42.743579Z","iopub.execute_input":"2021-09-29T23:59:42.743862Z","iopub.status.idle":"2021-09-29T23:59:42.791454Z","shell.execute_reply.started":"2021-09-29T23:59:42.743815Z","shell.execute_reply":"2021-09-29T23:59:42.790776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels.loc[0, 'bbox']","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:01:20.989584Z","iopub.execute_input":"2021-09-30T00:01:20.990327Z","iopub.status.idle":"2021-09-30T00:01:20.995905Z","shell.execute_reply.started":"2021-09-30T00:01:20.990279Z","shell.execute_reply":"2021-09-30T00:01:20.995202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def change_bbox_to_list(bbox):\n    values = bbox.split(',')\n    values[0] = values[0][1:]\n    values[-1] = values[-1][:-1]\n    for i in range(len(values)):\n        values[i] = float(values[i])\n        \n    return values\n\nl = change_bbox_to_list(df_train_labels.loc[0, 'bbox'])\n\nl","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:01:34.450931Z","iopub.execute_input":"2021-09-30T00:01:34.451567Z","iopub.status.idle":"2021-09-30T00:01:34.459499Z","shell.execute_reply.started":"2021-09-30T00:01:34.451527Z","shell.execute_reply":"2021-09-30T00:01:34.458359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(l[0])","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:01:42.444233Z","iopub.execute_input":"2021-09-30T00:01:42.444534Z","iopub.status.idle":"2021-09-30T00:01:42.451496Z","shell.execute_reply.started":"2021-09-30T00:01:42.444501Z","shell.execute_reply":"2021-09-30T00:01:42.450445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(l)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:01:46.980958Z","iopub.execute_input":"2021-09-30T00:01:46.981577Z","iopub.status.idle":"2021-09-30T00:01:46.988251Z","shell.execute_reply.started":"2021-09-30T00:01:46.981538Z","shell.execute_reply":"2021-09-30T00:01:46.987264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels['bbox'] = df_train_labels['bbox'].apply(lambda x: change_bbox_to_list(x) if not pd.isnull(x) else x)\n\ndf_train_labels.loc[0, 'bbox']","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:03:03.593544Z","iopub.execute_input":"2021-09-30T00:03:03.593843Z","iopub.status.idle":"2021-09-30T00:03:04.118976Z","shell.execute_reply.started":"2021-09-30T00:03:03.593815Z","shell.execute_reply":"2021-09-30T00:03:04.118106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:03:14.858454Z","iopub.execute_input":"2021-09-30T00:03:14.858731Z","iopub.status.idle":"2021-09-30T00:03:14.886276Z","shell.execute_reply.started":"2021-09-30T00:03:14.858705Z","shell.execute_reply":"2021-09-30T00:03:14.885392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:03:58.371864Z","iopub.execute_input":"2021-09-30T00:03:58.372169Z","iopub.status.idle":"2021-09-30T00:03:58.387127Z","shell.execute_reply.started":"2021-09-30T00:03:58.372138Z","shell.execute_reply":"2021-09-30T00:03:58.386288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations = {}\n\nfor i in range(len(df_train_labels)):\n    image_id = df_train_labels.loc[i, 'image_id']\n    if image_id in annotations:\n        annotations[image_id]['bbox'].append(df_train_labels.loc[i, 'bbox'])\n    else:\n        annotations[image_id] = {'bbox': []}\n        if not pd.isnull(df_train_labels.loc[i, 'width']):\n            for value in ['width', 'height']:\n                annotations[image_id][value] = df_train_labels.loc[i, value]\n            annotations[image_id]['bbox'].append(df_train_labels.loc[i, 'bbox'])","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:07:23.153833Z","iopub.execute_input":"2021-09-30T00:07:23.154137Z","iopub.status.idle":"2021-09-30T00:07:24.651034Z","shell.execute_reply.started":"2021-09-30T00:07:23.15411Z","shell.execute_reply":"2021-09-30T00:07:24.649897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(annotations)","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:07:35.037752Z","iopub.execute_input":"2021-09-30T00:07:35.03863Z","iopub.status.idle":"2021-09-30T00:07:35.046622Z","shell.execute_reply.started":"2021-09-30T00:07:35.038574Z","shell.execute_reply":"2021-09-30T00:07:35.045681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations['aedca9e2e']","metadata":{"execution":{"iopub.status.busy":"2021-09-30T00:07:49.399664Z","iopub.execute_input":"2021-09-30T00:07:49.400092Z","iopub.status.idle":"2021-09-30T00:07:49.408381Z","shell.execute_reply.started":"2021-09-30T00:07:49.400061Z","shell.execute_reply":"2021-09-30T00:07:49.407564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with tf.io.TFRecordWriter(\n    tfrecords_dir + \"/file_%.2i-%i.tfrec\" % (tfrec_num, len(samples))\n) as writer:\n    for sample in samples:\n        image_path = f\"{images_dir}/{sample['image_id']:012d}.jpg\"\n        image = tf.io.decode_jpeg(tf.io.read_file(image_path))\n        example = create_example(image, image_path, sample)\n        writer.write(example.SerializeToString())","metadata":{},"execution_count":null,"outputs":[]}]}