# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="rIJcPwCF9tNT" colab_type="text"
# # Training Mask R-CNN
# **Protip**: Activate GPU by `Menu > Runtime > Change runtime type` and selecting `GPU` from `Hardware accelerator` dropdown.

# + [markdown] id="OwDwdmKI65WT" colab_type="text"
# ## Clone repository from github

# + id="xah6lNgACW-N" colab_type="code" colab={}
# import getpass
# password = <PASSWORD>()
# password = ":" + password
password = ""

# + id="aez9bS3Nsm6K" colab_type="code" outputId="4a63d4ff-179e-408d-9be5-ff555ab4285e" colab={"base_uri": "https://localhost:8080/", "height": 168}
# %cd /content
# !rm -rf Mask-RCNN
repo = "https://subtleseeker" + password + "@github.com/subtleseeker/Mask-RCNN"
# !git clone $repo

# + [markdown] id="Rx-Jccdk6_WY" colab_type="text"
# ## Mount your google drive
# - Mounting will require authorization code. Follow the link after executing the cell below. 
# - Login with your google account.
# - Approve for the required permissions.
# - Copy the generated code and paste it in the cell input below.
# Congrats! You have mounted your google drive.
#
# You can view the files of the mounted drive, by using the `>` arrow on top-left part of your screen just below `+ Code` button, and then clicking `Files` tab.   
#
# **Note**: You will have to do this for every instance on google colab, as the instance alloted to you is temporary.

# + id="3J070rjbtL1E" colab_type="code" outputId="1d2271d9-5766-42c2-b232-ed9c360c2ea4" colab={"base_uri": "https://localhost:8080/", "height": 121}
from google.colab import drive
drive.mount('/content/drive')

# + [markdown] id="SZ120mr97Hmv" colab_type="text"
# ## Downloading necessary files
# Copy the required files from your google drive.
#
# **These files should be present in the following directory in your Google Drive:
# `DRIVE_ROOT/SLAM/`.**  
#     
#     
#
# **Required files**
# - `mask_rcnn_coco.h5`: COCO Dataset pretrained weights. Download `mask_rcnn_coco.h5` from [this](https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5) link. If this link fails, open [this](https://github.com/matterport/Mask_RCNN/releases) link and download the file under `v2.0` tag.
# - `dataset.tar.gz`: Dataset containing images and their masks in the required directory structure as mentioned in README.md in the github link above
#
#

# + id="G45FCfO7uHXs" colab_type="code" colab={}
# !cp /content/drive/My\ Drive/SLAM/mask_rcnn_coco.h5 /content/Mask-RCNN

# + id="M9EFGWjCtMsI" colab_type="code" outputId="a678e8fc-b682-4b76-83ac-77bb4e12ee9a" colab={"base_uri": "https://localhost:8080/", "height": 50}
# !tar -zxf /content/drive/My\ Drive/SLAM/dataset.tar.gz -C /content/Mask-RCNN/samples/auto/

# + id="POv-03EUwCYe" colab_type="code" outputId="8b26288b-41f9-4bf6-b627-24338cda1575" colab={"base_uri": "https://localhost:8080/", "height": 34}
# %cd /content/Mask-RCNN/samples/auto

# + [markdown] id="7Y08OFk5-t0t" colab_type="text"
# ## Training
# Execute the command to begin training   
# `!python3 auto.py train --dataset=<path to dataset root>/images --weights=coco`

# + id="Vw4AaKufw9zg" colab_type="code" outputId="629df2e6-7d87-41b6-a3ad-711b042ceb54" colab={"base_uri": "https://localhost:8080/", "height": 1000}
# !python3 auto.py train --dataset=./dataset/images --weights=coco

# + [markdown] id="5ErH68FNAwJF" colab_type="text"
# The last `auto_xxxxxx_yy.h5` file is the trained model weights after `yy` epochs. Save these weights in your Google Drive to use for your overlaying and evaluating the dataset. 

# + id="6BFnInz9Bajw" colab_type="code" colab={}
# !cp /content/Mask-RCNN/logs/auto20191110T1347/mask_rcnn_balloon_0030.h5 /content/drive/My\ Drive/SLAM

# + [markdown] id="sFx8gK74BqbU" colab_type="text"
# The trained weights are now saved in your Google Drive. Download it from `DRIVE_ROOT/SLAM`

# + [markdown] id="B7uutjCc_nPM" colab_type="text"
# ## Visualize results with Tensorboard
# Load tensorboard with *magic* command

# + id="lAZUEiPpynvX" colab_type="code" colab={}
# %load_ext tensorboard

# + [markdown] id="TcR-CHMkAiBA" colab_type="text"
# Logs are stored in `ROOT_DIR/logs` for every epoch. Visualize the training with:   
# `%tensorboard --logdir <path/to/logs>`

# + id="-ZUHfBJhxdtF" colab_type="code" outputId="1c6cae5a-f553-4f8b-9a3a-e37c5023a242" colab={"base_uri": "https://localhost:8080/", "height": 50}
# %tensorboard --logdir /content/Mask-RCNN/logs

StarcoderdataJupyterScriptsDedupFiltered