{"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\nimport contextlib2\nimport io\nimport IPython\nimport json\nimport pathlib\nimport sys\nimport tensorflow as tf\nimport time\nimport pandas as pd\nimport numpy as np\nimport argparse\nfrom PIL import Image, ImageDraw\nimport os\n\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n        \n        \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":"2022-02-11T04:30:33.023863Z","iopub.execute_input":"2022-02-11T04:30:33.024408Z","iopub.status.idle":"2022-02-11T04:30:37.141655Z","shell.execute_reply.started":"2022-02-11T04:30:33.024311Z","shell.execute_reply":"2022-02-11T04:30:37.140933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf \ngpus = tf.config.experimental.list_physical_devices('GPU') \nfor gpu in gpus:\n    tf.config.experimental.set_memory_growth(gpu, True)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:30:37.144978Z","iopub.execute_input":"2022-02-11T04:30:37.145613Z","iopub.status.idle":"2022-02-11T04:30:37.336673Z","shell.execute_reply.started":"2022-02-11T04:30:37.145581Z","shell.execute_reply":"2022-02-11T04:30:37.335500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model():\n    model = tf.compat.v2.saved_model.load(r\"../input/resnet50-fpn/resnet50fpn\")\n#     model = model.signatures['serving_default']\n\n    return model\ndetect_fn1 =load_model()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:30:37.338150Z","iopub.execute_input":"2022-02-11T04:30:37.338648Z","iopub.status.idle":"2022-02-11T04:30:53.076196Z","shell.execute_reply.started":"2022-02-11T04:30:37.338607Z","shell.execute_reply":"2022-02-11T04:30:53.075440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_into_numpy_array(image):\n    (im_width, im_height) = image.size\n    return np.array(image.getdata()).reshape((im_height, im_width, 3)).astype(np.uint8)\n\ndef predict(image):\n    return detect_fn1(np.expand_dims(image, axis=0)) ","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:30:53.078753Z","iopub.execute_input":"2022-02-11T04:30:53.079276Z","iopub.status.idle":"2022-02-11T04:30:53.084709Z","shell.execute_reply.started":"2022-02-11T04:30:53.079238Z","shell.execute_reply":"2022-02-11T04:30:53.083968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\nenv = greatbarrierreef.make_env()\niter_test = env.iter_test() ","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:30:53.087529Z","iopub.execute_input":"2022-02-11T04:30:53.087721Z","iopub.status.idle":"2022-02-11T04:30:53.128670Z","shell.execute_reply.started":"2022-02-11T04:30:53.087697Z","shell.execute_reply":"2022-02-11T04:30:53.128072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def format_output(image,pred):\n    annot = []\n    for i in range(100):\n        if pred['detection_scores'][0][i].numpy()>0.25:\n            ls=pred['detection_boxes'][0][i].numpy()\n            height,width,chal=image_np.shape\n            xmin=ls[1]*width\n            x2=ls[3]*width\n            ymin=ls[0]*height\n            y2=ls[2]*height\n            height_img=x2-xmin\n            width_img=y2-ymin\n#             arr={'x':xmin, 'y':ymin, 'width':width_img , 'height':height_img}\n            annot.append('{:.2f} {} {} {} {}'.format(pred['detection_scores'][0][i], int(xmin), int(ymin), int(width_img), int(height_img)))\n    return annot","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:30:53.131190Z","iopub.execute_input":"2022-02-11T04:30:53.131379Z","iopub.status.idle":"2022-02-11T04:30:53.140102Z","shell.execute_reply.started":"2022-02-11T04:30:53.131355Z","shell.execute_reply":"2022-02-11T04:30:53.139380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for (image_np, sample_prediction_df) in iter_test:\n    pred = predict(image_np)\n    predictions=format_output(image_np,pred)\n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:30:53.141407Z","iopub.execute_input":"2022-02-11T04:30:53.141651Z","iopub.status.idle":"2022-02-11T04:31:01.122644Z","shell.execute_reply.started":"2022-02-11T04:30:53.141616Z","shell.execute_reply":"2022-02-11T04:31:01.121921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# BASE_DIR = '/kaggle/input/tensorflow-great-barrier-reef'\n# testing_data = pd.read_csv(f'{BASE_DIR}/test.csv')\n# testing_data.head()\nsub=pd.read_csv('./submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T04:31:35.758217Z","iopub.execute_input":"2022-02-11T04:31:35.758470Z","iopub.status.idle":"2022-02-11T04:31:35.776227Z","shell.execute_reply.started":"2022-02-11T04:31:35.758442Z","shell.execute_reply":"2022-02-11T04:31:35.775604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# f'{BASE_DIR}/train_images/video_{video_id}/{video_frame}.jpg'\n# testing_data.info()\n# for i in range(3):\n#     print(testing_data.iloc[i])\n#     image_path=f'{BASE_DIR}/t_images/video_{video_id}/{video_frame}.jpg'\n# import matplotlib.pyplot as plt\n# %matplotlib inline\n# plt.imshow(ls[0])\n# pred=predict(ls[0])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:09:28.42333Z","iopub.execute_input":"2022-02-10T04:09:28.423633Z","iopub.status.idle":"2022-02-10T04:09:28.435712Z","shell.execute_reply.started":"2022-02-10T04:09:28.423597Z","shell.execute_reply":"2022-02-10T04:09:28.435019Z"},"trusted":true},"execution_count":null,"outputs":[]}]}