{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torch-1.7.0+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchvision-0.8.1+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchaudio-0.7.0-cp37-cp37m-linux_x86_64.whl' --no-deps","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:37:56.104609Z","iopub.execute_input":"2022-01-22T09:37:56.105216Z","iopub.status.idle":"2022-01-22T09:39:50.234679Z","shell.execute_reply.started":"2022-01-22T09:37:56.105139Z","shell.execute_reply":"2022-01-22T09:39:50.233848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/mmdetectionenv/mmdetectionenv/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionenv/mmdetectionenv/yapf-0.32.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionenv/mmdetectionenv/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionenv/mmdetectionenv/terminaltables-3.1.10-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetection-v2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:39:50.237118Z","iopub.execute_input":"2022-01-22T09:39:50.237668Z","iopub.status.idle":"2022-01-22T09:41:41.947729Z","shell.execute_reply.started":"2022-01-22T09:39:50.237610Z","shell.execute_reply":"2022-01-22T09:41:41.946706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/pycocotools/pycocotools-2.0.4-cp37-cp37m-linux_x86_64.whl' --no-deps","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:41:41.951053Z","iopub.execute_input":"2022-01-22T09:41:41.951306Z","iopub.status.idle":"2022-01-22T09:42:03.746034Z","shell.execute_reply.started":"2022-01-22T09:41:41.951274Z","shell.execute_reply":"2022-01-22T09:42:03.745171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf mmdetection\n\n!cp -r /kaggle/input/mmdetectionoffline /kaggle/working/\n!mv /kaggle/working/mmdetectionoffline /kaggle/working/mmdetection\n%cd /kaggle/working/mmdetection","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:42:03.748555Z","iopub.execute_input":"2022-01-22T09:42:03.748806Z","iopub.status.idle":"2022-01-22T09:42:12.188864Z","shell.execute_reply.started":"2022-01-22T09:42:03.748776Z","shell.execute_reply":"2022-01-22T09:42:12.188017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/mmdetection/mmdetection\n","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:42:12.190607Z","iopub.execute_input":"2022-01-22T09:42:12.190898Z","iopub.status.idle":"2022-01-22T09:42:12.197407Z","shell.execute_reply.started":"2022-01-22T09:42:12.190853Z","shell.execute_reply":"2022-01-22T09:42:12.196633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -e .","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:42:12.198824Z","iopub.execute_input":"2022-01-22T09:42:12.199818Z","iopub.status.idle":"2022-01-22T09:42:45.705805Z","shell.execute_reply.started":"2022-01-22T09:42:12.199674Z","shell.execute_reply":"2022-01-22T09:42:45.704973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.transforms as transforms\nimport torch.nn.functional as F\nimport sklearn\nimport torchvision\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport numpy as np\n# import cupy as cp\nimport gc\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport PIL\nimport json\nfrom PIL import Image, ImageEnhance\nimport albumentations as A\nimport mmdet\nimport mmcv\nfrom albumentations.pytorch import ToTensorV2\nimport seaborn as sns\nimport glob\nfrom pathlib import Path\nimport pycocotools\nfrom pycocotools import mask\nimport numpy.random\nimport random\nimport cv2\nimport re\nimport shutil\n","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:42:45.707779Z","iopub.execute_input":"2022-01-22T09:42:45.708106Z","iopub.status.idle":"2022-01-22T09:42:50.217236Z","shell.execute_reply.started":"2022-01-22T09:42:45.708053Z","shell.execute_reply":"2022-01-22T09:42:50.216164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\nfrom mmdet.apis import train_detector\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot, set_random_seed","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:42:50.219259Z","iopub.execute_input":"2022-01-22T09:42:50.219556Z","iopub.status.idle":"2022-01-22T09:43:06.217872Z","shell.execute_reply.started":"2022-01-22T09:42:50.219515Z","shell.execute_reply":"2022-01-22T09:43:06.217159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:06.219026Z","iopub.execute_input":"2022-01-22T09:43:06.219865Z","iopub.status.idle":"2022-01-22T09:43:06.225583Z","shell.execute_reply.started":"2022-01-22T09:43:06.219821Z","shell.execute_reply":"2022-01-22T09:43:06.224922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH = 1333\nIMG_HEIGHT = 800","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:06.226595Z","iopub.execute_input":"2022-01-22T09:43:06.227359Z","iopub.status.idle":"2022-01-22T09:43:06.236221Z","shell.execute_reply.started":"2022-01-22T09:43:06.227320Z","shell.execute_reply":"2022-01-22T09:43:06.235529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.append('/kaggle/input/tensorflow-great-barrier-reef')\nimport greatbarrierreef\n\nsys.path.append('/kaggle/working/mmdetection')\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:06.239787Z","iopub.execute_input":"2022-01-22T09:43:06.240027Z","iopub.status.idle":"2022-01-22T09:43:06.265859Z","shell.execute_reply.started":"2022-01-22T09:43:06.239997Z","shell.execute_reply":"2022-01-22T09:43:06.265204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.path.append('/kaggle/input/tensorflow-great-barrier-reef')\nenv = greatbarrierreef.make_env()\niter_test = env.iter_test() ","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:06.266943Z","iopub.execute_input":"2022-01-22T09:43:06.267584Z","iopub.status.idle":"2022-01-22T09:43:06.271811Z","shell.execute_reply.started":"2022-01-22T09:43:06.267550Z","shell.execute_reply":"2022-01-22T09:43:06.271153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm submission.csv","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:06.273278Z","iopub.execute_input":"2022-01-22T09:43:06.273975Z","iopub.status.idle":"2022-01-22T09:43:06.941435Z","shell.execute_reply.started":"2022-01-22T09:43:06.273940Z","shell.execute_reply":"2022-01-22T09:43:06.940663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_dir = '/kaggle/input/tensorflow-great-barrier-reef/test_images'\nconfig = '/kaggle/input/faster-rcnn-r50-fpn-1x-reef/faster_rcnn_r50_fpn_1x_reef.py'\ncheckpoint = '/kaggle/input/latestpth/latest.pth'\nmodel = init_detector(config, checkpoint, device='cuda:0')","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:06.943774Z","iopub.execute_input":"2022-01-22T09:43:06.944474Z","iopub.status.idle":"2022-01-22T09:43:14.544511Z","shell.execute_reply.started":"2022-01-22T09:43:06.944429Z","shell.execute_reply":"2022-01-22T09:43:14.543699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def color_correction_of_image_analysis(img):\n    \"\"\"\n    基于图像分析的偏色检测及颜色校正方法\n    :param img: cv2.imread读取的图片数据\n    :return: 返回的白平衡结果图片数据\n    \"\"\"\n    b, g, r = cv2.split(img)\n    # print(img.shape)\n    m, n = b.shape\n    # detection(img)\n    I_r_2 = np.zeros(r.shape)\n    I_b_2 = np.zeros(b.shape)\n    I_r_2 = (r.astype(np.float32) ** 2).astype(np.float32)\n    I_b_2 = (b.astype(np.float32) ** 2).astype(np.float32)\n    sum_I_r_2 = I_r_2.sum()\n    sum_I_b_2 = I_b_2.sum()\n    sum_I_g = g.sum()\n    sum_I_r = r.sum()\n    sum_I_b = b.sum()\n\n    max_I_r = r.max()\n    max_I_g = g.max()\n    max_I_b = b.max()\n    max_I_r_2 = I_r_2.max()\n    max_I_b_2 = I_b_2.max()\n    [u_b, v_b] = np.matmul(np.linalg.inv([[sum_I_b_2, sum_I_b], [max_I_b_2, max_I_b]]), [sum_I_g, max_I_g])\n    [u_r, v_r] = np.matmul(np.linalg.inv([[sum_I_r_2, sum_I_r], [max_I_r_2, max_I_r]]), [sum_I_g, max_I_g])\n    b_point = u_b * (b.astype(np.float32) ** 2) + v_b * b.astype(np.float32)\n    r_point = u_r * (r.astype(np.float32) ** 2) + v_r * r.astype(np.float32)\n    b_point[b_point > 255] = 255\n    b_point[b_point < 0] = 0\n    b = b_point.astype(np.uint8)\n    r_point[r_point > 255] = 255\n    r_point[r_point < 0] = 0\n    r = r_point.astype(np.uint8)\n    return cv2.merge([b, g, r])","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:14.545657Z","iopub.execute_input":"2022-01-22T09:43:14.545943Z","iopub.status.idle":"2022-01-22T09:43:14.560469Z","shell.execute_reply.started":"2022-01-22T09:43:14.545911Z","shell.execute_reply":"2022-01-22T09:43:14.559699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_thr = 0.25\nfor idx, (img, sample_prediction_df) in enumerate(iter_test):\n    print('idx', idx)\n    img = img[:, :, ::-1]\n    img = color_correction_of_image_analysis(img)\n    print(img.shape)\n    bboxes = inference_detector(model, img)\n    \n    predictions = []\n    detects = []\n    print(bboxes)\n    for bbox in bboxes[0]:\n        if bbox[-1] >= score_thr:\n            xmin, ymin, xmax, ymax = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])\n            if xmin < 0 or ymin < 0 or xmax > w or ymax > h:\n                continue\n            predictions.append('{:.2f} {} {} {} {}'.format(bbox[-1], xmin, ymin, xmax-xmin, ymax-ymin))\n\n    prediction_str = ' '.join(predictions)\n    print('1111111')\n    print(prediction_str)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:14.561912Z","iopub.execute_input":"2022-01-22T09:43:14.562480Z","iopub.status.idle":"2022-01-22T09:43:15.192328Z","shell.execute_reply.started":"2022-01-22T09:43:14.562433Z","shell.execute_reply":"2022-01-22T09:43:15.191622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nprint('2222222')\nsub_df = pd.read_csv('submission.csv')\nprint('3333333')\nsub_df.head()\nprint('4444444')","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:15.193639Z","iopub.execute_input":"2022-01-22T09:43:15.193902Z","iopub.status.idle":"2022-01-22T09:43:15.203540Z","shell.execute_reply.started":"2022-01-22T09:43:15.193867Z","shell.execute_reply":"2022-01-22T09:43:15.202721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:15.204779Z","iopub.execute_input":"2022-01-22T09:43:15.205563Z","iopub.status.idle":"2022-01-22T09:43:15.910804Z","shell.execute_reply.started":"2022-01-22T09:43:15.205521Z","shell.execute_reply":"2022-01-22T09:43:15.909628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prediction_str\nimport shutil\n!cp  /kaggle/working/mmdetection/submission.csv /kaggle/working/submission.csv\n\n# shutil.rmtree('/kaggle/working/build')\n# shutil.rmtree('/kaggle/working/filterpy')\n# shutil.rmtree('/kaggle/working/codefixres')\n# shutil.rmtree('/kaggle/working/common')\nshutil.rmtree('/kaggle/working/mmdetection')\n# shutil.rmtree('/kaggle/working/mmpycocotools')\n# shutil.rmtree('/kaggle/working/pycocotools')\n# shutil.rmtree('/kaggle/working/pycocotools.egg-info')\n!ls /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:15.912625Z","iopub.execute_input":"2022-01-22T09:43:15.912929Z","iopub.status.idle":"2022-01-22T09:43:17.376480Z","shell.execute_reply.started":"2022-01-22T09:43:15.912889Z","shell.execute_reply":"2022-01-22T09:43:17.375672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sub_df)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T09:43:17.378765Z","iopub.execute_input":"2022-01-22T09:43:17.379187Z","iopub.status.idle":"2022-01-22T09:43:17.391815Z","shell.execute_reply.started":"2022-01-22T09:43:17.379148Z","shell.execute_reply":"2022-01-22T09:43:17.390875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}