{"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":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nfrom scipy.ndimage.filters import maximum_filter\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Model, load_model\nfrom skimage import measure","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-20T09:37:20.588984Z","iopub.execute_input":"2022-02-20T09:37:20.589569Z","iopub.status.idle":"2022-02-20T09:37:26.384298Z","shell.execute_reply.started":"2022-02-20T09:37:20.589471Z","shell.execute_reply":"2022-02-20T09:37:26.383517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()  ","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:37:26.386041Z","iopub.execute_input":"2022-02-20T09:37:26.386307Z","iopub.status.idle":"2022-02-20T09:37:26.409429Z","shell.execute_reply.started":"2022-02-20T09:37:26.386273Z","shell.execute_reply":"2022-02-20T09:37:26.408774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('../input/cots-models/v5am2-004-0.674-0.167.h5')","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:37:26.410531Z","iopub.execute_input":"2022-02-20T09:37:26.410922Z","iopub.status.idle":"2022-02-20T09:37:30.79423Z","shell.execute_reply.started":"2022-02-20T09:37:26.410885Z","shell.execute_reply":"2022-02-20T09:37:30.793494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"thr = 0.2\nkernel = np.ones((7,7))\nmin_w=21\nmin_h=15\nratio = 1.6\n\nmodel_input = keras.Input(shape=(None,None,3))\nx = model_input/127.5-1.\nx = keras.layers.Resizing(int(np.round(720*ratio)), int(np.round(1280*ratio)))(x)\nx = model(x)\nmodel = keras.Model(inputs=model_input, outputs=x)\n\nfor i, (image_np, sample_prediction_df) in enumerate(iter_test):\n\n    p = model.predict(image_np[None, :,:,::-1])\n    hm = p['hm'][0,:,:,0]\n    reg = p['re'][0]\n\n    hm[:3] = hm[:3]*.6\n    hm[-3:] = hm[-3:]*.6\n    hm[:,:3] = hm[:,:3]*.6\n    hm[:,-3:] = hm[:,-3:]*.6\n    \n    local_maxi = hm*(hm == maximum_filter(hm,footprint=kernel))>thr\n    py, px = np.where(local_maxi)\n    conf = hm[py,px]\n    bb = reg[py,px]\n        \n    px = px + 0.5 + bb[:,0] - bb[:,2]/2.\n    py = py + 0.5 + bb[:,1] - bb[:,3]/2.\n    ww = bb[:,2]\n    hh = bb[:,3]\n    \n    bboxes = np.round(np.array([px,py,ww,hh]).T*8./ratio).astype('int')\n    #bboxes = np.array([px,py,ww,hh]).T*8./ratio\n    \n    bboxes[:,2:] = bboxes[:,2:] + np.minimum(bboxes[:,:2], 0)\n    bboxes[:,:2] = np.maximum(bboxes[:,:2], 0)\n    bboxes[:,2] = np.minimum(bboxes[:,0]+bboxes[:,2], 1279)-bboxes[:,0]\n    bboxes[:,3] = np.minimum(bboxes[:,1]+bboxes[:,3], 719)-bboxes[:,1]\n    #valid= np.min(bboxes[:,2:], axis=1)>0\n    valid= (bboxes[:,2]>(min_w/ratio))*(bboxes[:,3]>(min_h/ratio))\n    bboxes = bboxes[valid]\n    conf = conf[valid]\n        \n    predictions = []\n\n    for j, (x,y,w,h) in enumerate(bboxes):\n        predictions.append('{:.4f} {} {} {} {}'.format(conf[j], x, y, w, h))\n        #predictions.append('{:.4f} {:.2f} {:.2f} {:.2f} {:.2f}'.format(conf[j], x, y, w, h))\n\n    prediction_str = ' '.join(predictions)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n        \n    if i<3:\n        print('Prediction:', prediction_str)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:37:30.796395Z","iopub.execute_input":"2022-02-20T09:37:30.79662Z","iopub.status.idle":"2022-02-20T09:37:41.530872Z","shell.execute_reply.started":"2022-02-20T09:37:30.796596Z","shell.execute_reply":"2022-02-20T09:37:41.530125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(hm)","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:37:41.531977Z","iopub.execute_input":"2022-02-20T09:37:41.532209Z","iopub.status.idle":"2022-02-20T09:37:41.784353Z","shell.execute_reply.started":"2022-02-20T09:37:41.532176Z","shell.execute_reply":"2022-02-20T09:37:41.783657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hm.max()","metadata":{"execution":{"iopub.status.busy":"2022-02-20T09:37:41.785342Z","iopub.execute_input":"2022-02-20T09:37:41.785711Z","iopub.status.idle":"2022-02-20T09:37:41.793033Z","shell.execute_reply.started":"2022-02-20T09:37:41.785675Z","shell.execute_reply":"2022-02-20T09:37:41.79207Z"},"trusted":true},"execution_count":null,"outputs":[]}]}