{"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":"markdown","source":"# Installing Keras-RetinaNet","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/fizyr/keras-retinanet.git\n%cd keras-retinanet/\n!python setup.py build_ext --inplace","metadata":{"execution":{"iopub.status.busy":"2022-11-09T09:41:59.470474Z","iopub.execute_input":"2022-11-09T09:41:59.471056Z","iopub.status.idle":"2022-11-09T09:42:06.186084Z","shell.execute_reply.started":"2022-11-09T09:41:59.471011Z","shell.execute_reply":"2022-11-09T09:42:06.184809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport time\nimport pandas as pd\nimport keras\nimport tensorflow\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport cv2\nimport tensorflow as tf\n","metadata":{"execution":{"iopub.status.busy":"2022-11-09T09:42:06.188209Z","iopub.execute_input":"2022-11-09T09:42:06.188619Z","iopub.status.idle":"2022-11-09T09:42:06.200946Z","shell.execute_reply.started":"2022-11-09T09:42:06.188582Z","shell.execute_reply":"2022-11-09T09:42:06.199158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import keras\nfrom keras_retinanet import models\nfrom keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image\nfrom keras_retinanet.utils.visualization import draw_box, draw_caption\nfrom keras_retinanet.utils.colors import label_color\nfrom keras_retinanet import models","metadata":{"execution":{"iopub.status.busy":"2022-11-09T09:42:06.202680Z","iopub.execute_input":"2022-11-09T09:42:06.203266Z","iopub.status.idle":"2022-11-09T09:42:06.222875Z","shell.execute_reply.started":"2022-11-09T09:42:06.203233Z","shell.execute_reply":"2022-11-09T09:42:06.221848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading data","metadata":{}},{"cell_type":"code","source":"# Turn annotations from strings into lists of dictionaries\ndf_train = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\ndf_train=df_train.loc[df_train[\"annotations\"].astype(str) != \"[]\"]\ndf_train['annotations'] = df_train['annotations'].apply(eval)\n\n# Create the image path for the row\ndef image_path(r):\n    video_id = r['video_id']\n    video_frame = r['video_frame']\n    return \"video_\" + str(video_id) + \"/\" + str(video_frame) + \".jpg\"\ndf_train['image_path'] = df_train.apply(lambda x: image_path(x), axis=1)\n\n#make annotation to each row\ndf_extrain=df_train.explode('annotations') \ndf_extrain.reset_index(inplace=True)\ndf_extrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-09T09:42:06.225602Z","iopub.execute_input":"2022-11-09T09:42:06.226895Z","iopub.status.idle":"2022-11-09T09:42:06.534112Z","shell.execute_reply.started":"2022-11-09T09:42:06.226855Z","shell.execute_reply":"2022-11-09T09:42:06.532522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create a new dataframe in df_extrain_main\ndf_extrain_main=pd.DataFrame(pd.json_normalize(df_extrain['annotations']), columns=['x', 'y', 'width', 'height']).join(df_extrain)\ndf_extrain_main['class']='Starfish'\ndf_extrain_main.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-09T09:42:06.536053Z","iopub.execute_input":"2022-11-09T09:42:06.536683Z","iopub.status.idle":"2022-11-09T09:42:06.649678Z","shell.execute_reply.started":"2022-11-09T09:42:06.536635Z","shell.execute_reply":"2022-11-09T09:42:06.647936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade git+https://github.com/broadinstitute/keras-resnet\nimport keras\nimport keras_resnet\nimport urllib.request\nPRETRAINED_MODEL = './snapshots/_pretrained_model.h5'\n#### OPTION 1: DOWNLOAD INITIAL PRETRAINED MODEL FROM FIZYR ####\nURL_MODEL = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5'\nurllib.request.urlretrieve(URL_MODEL, PRETRAINED_MODEL)","metadata":{"execution":{"iopub.status.busy":"2022-11-09T09:42:06.652016Z","iopub.execute_input":"2022-11-09T09:42:06.652761Z","iopub.status.idle":"2022-11-09T09:42:30.489004Z","shell.execute_reply.started":"2022-11-09T09:42:06.652713Z","shell.execute_reply":"2022-11-09T09:42:30.486989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_tf_example(rowss,data_df):\n    \"\"\"Create a tf.Example entry for a given training image.\"\"\"\n    full_path = os.path.join(rowss.image_path)\n    with tf.io.gfile.GFile(full_path, 'rb') as fid:\n        encoded_jpg = fid.read()\n    encoded_jpg_io = io.BytesIO(encoded_jpg)\n    image = Image.open(encoded_jpg_io)\n    if image.format != 'JPEG':\n        raise ValueError('Image format not JPEG')\n\n    height = image.size[1] # Image height\n    width = image.size[0] # Image width\n    #print(width,height)\n    filename = f'{rowss.video_id}:{rowss.video_frame}'.encode('utf8') # Unique id of the image.\n    encoded_image_data = None # Encoded image bytes\n    image_format = 'jpeg'.encode('utf8') # b'jpeg' or b'png'\n\n    xmins = [] \n    xmaxs = [] \n    ymins = [] \n    ymaxs = [] \n    \n    # Convert ---> [xmin,ymin,width,height] to [xmins,xmaxs,ymins,ymaxs]\n    xmin = rowss['x']\n    xmax = rowss['x']+rowss['width']\n    ymin = rowss['y']\n    ymax = rowss['y']+rowss['height']\n    \n\n    #main_data.append((rowss['image_path'],xmins,xmaxs,ymins,ymaxs))\n    return rowss['image_path'],xmin,ymin,xmax,ymax","metadata":{},"execution_count":null,"outputs":[]}]}