{"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":"# **Detectron Half For Great Barrier Reef**","metadata":{}},{"cell_type":"markdown","source":"#### One of the most challenges now a days in Kaggle Competetion is that no Internet is provided while submission of the Notebook. To handle complex models such as Detectron2 it might be tricky without internet access as it requires lots of dependencies and we also have to register out dataset. In this Notebook I utilized Detectron2 version 0.5. Hope you like this Notebook.","metadata":{}},{"cell_type":"markdown","source":"##  Please up-vote to keep me motivated and Enthusiastic 😁 Thanks!\n- Follow me on Linkedin [Link](https://www.linkedin.com/in/owaiskhan9654/)\n- Also see my Portfolio [Link](https://owaiskhan9654.github.io/)","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport json\nimport ast\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nfrom sklearn.model_selection import GroupKFold\nimport os, json, cv2, random\nimport matplotlib.pyplot as plt\n%matplotlib inline\nfrom PIL import Image\nfrom string import Template\nfrom IPython.display import display\nfrom shutil import *","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-20T08:27:56.736955Z","iopub.execute_input":"2022-01-20T08:27:56.737972Z","iopub.status.idle":"2022-01-20T08:27:57.85973Z","shell.execute_reply.started":"2022-01-20T08:27:56.737859Z","shell.execute_reply":"2022-01-20T08:27:57.858973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_path(row):\n    \n    row['image_path'] = f'../input/tensorflow-great-barrier-reef/train_images/video_{row.video_id}/{row.video_frame}.jpg'\n    return row\n\ndef get_bbox(annots):\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:27:57.863104Z","iopub.execute_input":"2022-01-20T08:27:57.863307Z","iopub.status.idle":"2022-01-20T08:27:57.870087Z","shell.execute_reply.started":"2022-01-20T08:27:57.863282Z","shell.execute_reply":"2022-01-20T08:27:57.869431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\ndf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:27:57.87272Z","iopub.execute_input":"2022-01-20T08:27:57.872962Z","iopub.status.idle":"2022-01-20T08:27:57.954897Z","shell.execute_reply.started":"2022-01-20T08:27:57.872933Z","shell.execute_reply":"2022-01-20T08:27:57.95389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"num_bbox\"] = df['annotations'].progress_apply(lambda x: str.count(x, 'x'))\ndf_train = df[df[\"num_bbox\"]>0]\n\n\ndf_train['annotations'] = df_train['annotations'].progress_apply(lambda x: ast.literal_eval(x))\ndf_train['bboxes'] = df_train.annotations.progress_apply(get_bbox)\n\n\ndf_train[\"width\"] = 1280\ndf_train[\"height\"] = 720\n\n\ndf_train = df_train.progress_apply(get_path, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:27:57.956892Z","iopub.execute_input":"2022-01-20T08:27:57.957357Z","iopub.status.idle":"2022-01-20T08:28:02.594758Z","shell.execute_reply.started":"2022-01-20T08:27:57.957313Z","shell.execute_reply":"2022-01-20T08:28:02.593626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.reset_index(drop=True,inplace=True)\ndf_train","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:28:02.596303Z","iopub.execute_input":"2022-01-20T08:28:02.596676Z","iopub.status.idle":"2022-01-20T08:28:02.634035Z","shell.execute_reply.started":"2022-01-20T08:28:02.596636Z","shell.execute_reply":"2022-01-20T08:28:02.633037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kf = GroupKFold(n_splits = 5) \ndf_train['fold'] = -1\nfor fold, (train_idx, val_idx) in enumerate(kf.split(df_train, y = df_train.video_id.tolist(), groups=df_train.sequence)):\n    df_train.loc[val_idx, 'fold'] = fold\n\ndf_train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:28:02.635872Z","iopub.execute_input":"2022-01-20T08:28:02.636163Z","iopub.status.idle":"2022-01-20T08:28:02.666283Z","shell.execute_reply.started":"2022-01-20T08:28:02.636125Z","shell.execute_reply":"2022-01-20T08:28:02.665447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir dataset\n!mkdir dataset/images\n!mkdir dataset/images/train\n!mkdir dataset/images/val\n!mkdir dataset/annotations","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:28:02.667821Z","iopub.execute_input":"2022-01-20T08:28:02.668147Z","iopub.status.idle":"2022-01-20T08:28:05.953075Z","shell.execute_reply.started":"2022-01-20T08:28:02.668109Z","shell.execute_reply":"2022-01-20T08:28:05.952103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SELECTED_FOLD = 4\n\nannotion_id=0\nfor i in tqdm(range(len(df_train))):\n    row = df_train.loc[i]\n    if row.fold != SELECTED_FOLD:\n        copyfile(f'{row.image_path}', f'dataset/images/train/{row.image_id}.jpg')\n    else:\n        copyfile(f'{row.image_path}', f'dataset/images/val/{row.image_id}.jpg') ","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:28:05.95644Z","iopub.execute_input":"2022-01-20T08:28:05.957019Z","iopub.status.idle":"2022-01-20T08:29:05.940999Z","shell.execute_reply.started":"2022-01-20T08:28:05.95698Z","shell.execute_reply":"2022-01-20T08:29:05.940309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Number of training files:', len(os.listdir(f\"dataset/images/train/\")))\nprint(f'Number of validation files:', len(os.listdir(f\"dataset/images/val/\")))","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:05.943191Z","iopub.execute_input":"2022-01-20T08:29:05.943811Z","iopub.status.idle":"2022-01-20T08:29:05.955194Z","shell.execute_reply.started":"2022-01-20T08:29:05.943779Z","shell.execute_reply":"2022-01-20T08:29:05.954432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_annot_json(json_annotation, filename):\n    with open(filename, 'w') as f:\n        output_json = json.dumps(json_annotation)\n        f.write(output_json)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:05.959118Z","iopub.execute_input":"2022-01-20T08:29:05.959331Z","iopub.status.idle":"2022-01-20T08:29:05.963592Z","shell.execute_reply.started":"2022-01-20T08:29:05.959306Z","shell.execute_reply":"2022-01-20T08:29:05.962686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def dataset2coco(df, dest_path):\n    \n    global annotion_id\n    \n    annotations_json = {\n        \"info\": [],\n        \"licenses\": [],\n        \"categories\": [],\n        \"images\": [],\n        \"annotations\": []\n    }\n    \n    info = {\n        \"year\": \"2022\",\n        \"version\": \"1\",\n        \"description\": \"COTS dataset - COCO format\",\n        \"contributor\": \"Owais Ahmad\",\n        \"url\": \"https://kaggle.com\",\n        \"date_created\": \"2022-01-18T16:40:06+00:00\"\n    }\n    annotations_json[\"info\"].append(info)\n    \n    lic = {\n            \"id\": 1,\n            \"url\": \"\",\n            \"name\": \"Unknown\"\n        }\n    annotations_json[\"licenses\"].append(lic)\n\n    classes = {\"id\": 0, \"name\": \"starfish\", \"supercategory\": \"none\"}\n\n    annotations_json[\"categories\"].append(classes)\n\n    \n    for ann_row in df.itertuples():\n            \n        images = {\n            \"id\": ann_row[0],\n            \"license\": 1,\n            \"file_name\": ann_row.image_id + '.jpg',\n            \"height\": ann_row.height,\n            \"width\": ann_row.width,\n            \"date_captured\": \"2022-01-18T16:43:26+00:00\"\n        }\n        \n        annotations_json[\"images\"].append(images)\n        \n        bbox_list = ann_row.bboxes\n        \n        for bbox in bbox_list:\n            b_width = bbox[2]\n            b_height = bbox[3]\n            \n            # some boxes in COTS are outside the image height and width\n            if (bbox[0] + bbox[2] > 1280):\n                b_width = bbox[0] - 1280 \n            if (bbox[1] + bbox[3] > 720):\n                b_height = bbox[1] - 720 \n                \n            image_annotations = {\n                \"id\": annotion_id,\n                \"image_id\": ann_row[0],\n                \"category_id\": 0,\n                \"bbox\": [bbox[0], bbox[1], b_width, b_height],\n                \"area\": bbox[2] * bbox[3],\n                \"segmentation\": [],\n                \"iscrowd\": 0\n            }\n            \n            annotion_id += 1\n            annotations_json[\"annotations\"].append(image_annotations)\n        \n        \n    print(f\"Dataset COTS annotation to COCO json format completed! Files: {len(df)}\")\n    return annotations_json","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:05.964972Z","iopub.execute_input":"2022-01-20T08:29:05.965336Z","iopub.status.idle":"2022-01-20T08:29:05.980171Z","shell.execute_reply.started":"2022-01-20T08:29:05.965296Z","shell.execute_reply":"2022-01-20T08:29:05.979511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_annot_json = dataset2coco(df_train[df_train.fold != SELECTED_FOLD], f\"dataset/images/train/\")","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:05.981118Z","iopub.execute_input":"2022-01-20T08:29:05.981318Z","iopub.status.idle":"2022-01-20T08:29:06.155003Z","shell.execute_reply.started":"2022-01-20T08:29:05.981293Z","shell.execute_reply":"2022-01-20T08:29:06.154088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Json files for annotation is saved in seprate annotations folder which I will use further while register our dataset into Detectron2","metadata":{}},{"cell_type":"code","source":"train_annot_json = dataset2coco(df_train[df_train.fold != SELECTED_FOLD], f\"dataset/images/train/\")\nval_annot_json = dataset2coco(df_train[df_train.fold == SELECTED_FOLD], f\"dataset/images/valid\")\n\n\nsave_annot_json(train_annot_json, f\"dataset/annotations/train.json\")\nsave_annot_json(val_annot_json, f\"dataset/annotations/valid.json\")","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:06.156583Z","iopub.execute_input":"2022-01-20T08:29:06.15686Z","iopub.status.idle":"2022-01-20T08:29:06.265029Z","shell.execute_reply.started":"2022-01-20T08:29:06.156823Z","shell.execute_reply":"2022-01-20T08:29:06.264276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Helper functions, used these for debugging purposes\n### Detector2 build only succeeds if CUDA version is correct","metadata":{}},{"cell_type":"code","source":"!nvidia-smi\n!nvcc --version","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:06.266391Z","iopub.execute_input":"2022-01-20T08:29:06.266808Z","iopub.status.idle":"2022-01-20T08:29:07.629446Z","shell.execute_reply.started":"2022-01-20T08:29:06.26677Z","shell.execute_reply":"2022-01-20T08:29:07.628459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:07.632101Z","iopub.execute_input":"2022-01-20T08:29:07.632337Z","iopub.status.idle":"2022-01-20T08:29:09.402555Z","shell.execute_reply.started":"2022-01-20T08:29:07.632308Z","shell.execute_reply":"2022-01-20T08:29:09.401657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* ### The submission notebooks don't have access to the internet, in order to install detectron2 we need to download dependecies with pip download, copy them into Output Directory and Install them as followed it in this notebook","metadata":{}},{"cell_type":"code","source":"!cp  -r ../input/detectron-05/ ./detectron-05/","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:09.404037Z","iopub.execute_input":"2022-01-20T08:29:09.404265Z","iopub.status.idle":"2022-01-20T08:29:16.929227Z","shell.execute_reply.started":"2022-01-20T08:29:09.404238Z","shell.execute_reply":"2022-01-20T08:29:16.928247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Standard procedure to install Detectron2. Install with this if you are facing Issue with the Offline Version","metadata":{}},{"cell_type":"code","source":"#!python -m pip install 'git+https://github.com/facebookresearch/detectron2.git' #\n#!git clone https://github.com/facebookresearch/detectron2.git #","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:16.931767Z","iopub.execute_input":"2022-01-20T08:29:16.932045Z","iopub.status.idle":"2022-01-20T08:29:16.937413Z","shell.execute_reply.started":"2022-01-20T08:29:16.932005Z","shell.execute_reply":"2022-01-20T08:29:16.936566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/working/detectron-05/whls/pycocotools-2.0.2/dist/pycocotools-2.0.2.tar --no-index --find-links ../input/detectron-05/whls \n!pip install /kaggle/working/detectron-05/whls/fvcore-0.1.5.post20211019/fvcore-0.1.5.post20211019 --no-index --find-links ../input/detectron-05/whls \n!pip install /kaggle/working/detectron-05/whls/antlr4-python3-runtime-4.8/antlr4-python3-runtime-4.8 --no-index --find-links ../input/detectron-05/whls \n!pip install /kaggle/working/detectron-05/whls/detectron2-0.5/detectron2 --no-index --find-links ../input/detectron-05/whls ","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:29:16.938867Z","iopub.execute_input":"2022-01-20T08:29:16.939116Z","iopub.status.idle":"2022-01-20T08:32:39.802763Z","shell.execute_reply.started":"2022-01-20T08:29:16.939079Z","shell.execute_reply":"2022-01-20T08:32:39.801811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Base setup For Detectron2 Training","metadata":{}},{"cell_type":"code","source":"# detectron2 logger\nimport detectron2\nfrom detectron2.utils.logger import setup_logger\nsetup_logger()\n\n# detectron2 utilities\nfrom detectron2 import model_zoo\nfrom detectron2.engine import DefaultPredictor\nfrom detectron2.config import get_cfg\nfrom detectron2.utils.visualizer import Visualizer\nfrom detectron2.data import MetadataCatalog, DatasetCatalog\nfrom detectron2.structures import BoxMode\nfrom detectron2.data.datasets import register_coco_instances\n#import shutil\n#shutil.rmtree('detectron_clone')","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:39.805517Z","iopub.execute_input":"2022-01-20T08:32:39.806104Z","iopub.status.idle":"2022-01-20T08:32:40.071679Z","shell.execute_reply.started":"2022-01-20T08:32:39.80606Z","shell.execute_reply":"2022-01-20T08:32:40.070884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python -m detectron2.utils.collect_env","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:40.072963Z","iopub.execute_input":"2022-01-20T08:32:40.073388Z","iopub.status.idle":"2022-01-20T08:32:46.026414Z","shell.execute_reply.started":"2022-01-20T08:32:40.073323Z","shell.execute_reply":"2022-01-20T08:32:46.025414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### In order to Use Detectron2 We need to Register Out Dataset to Detectron2. While Processing the Dataset I generated corresponding Train.json and Valid.json which I will utilize Now.","metadata":{}},{"cell_type":"code","source":"register_coco_instances( 'Train_Great_Barrier',{},'/kaggle/working/dataset/annotations/train.json','/kaggle/working/dataset/images/train/')# os.path.join(dataset_dir,train_dir))\nregister_coco_instances( 'Valid_Great_Barrier',{},'/kaggle/working/dataset/annotations/valid.json','/kaggle/working/dataset/images/val/')# os.path.join(dataset_dir,train_dir))\n","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:46.028498Z","iopub.execute_input":"2022-01-20T08:32:46.028822Z","iopub.status.idle":"2022-01-20T08:32:46.034477Z","shell.execute_reply.started":"2022-01-20T08:32:46.028775Z","shell.execute_reply":"2022-01-20T08:32:46.033697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_dicts = DatasetCatalog.get(\"Train_Great_Barrier\")\nmetadata_dicts = MetadataCatalog.get(\"Train_Great_Barrier\")","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:46.036156Z","iopub.execute_input":"2022-01-20T08:32:46.036444Z","iopub.status.idle":"2022-01-20T08:32:46.314559Z","shell.execute_reply.started":"2022-01-20T08:32:46.03641Z","shell.execute_reply":"2022-01-20T08:32:46.313819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from detectron2.utils.visualizer import ColorMode\nfrom detectron2.engine import DefaultTrainer","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:46.315903Z","iopub.execute_input":"2022-01-20T08:32:46.316144Z","iopub.status.idle":"2022-01-20T08:32:46.319766Z","shell.execute_reply.started":"2022-01-20T08:32:46.316107Z","shell.execute_reply":"2022-01-20T08:32:46.319121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2, 2, figsize =(20,14))\nindices=[ax[0][0],ax[1][0],ax[0][1],ax[1][1] ]\ni=-1\nfor d in random.sample(dataset_dicts, 4):\n    i=i+1    \n    img = cv2.imread(d[\"file_name\"])\n    v = Visualizer(img[:, :, ::-1],\n                   metadata=metadata_dicts, \n                   scale=0.4, \n                   instance_mode=None  # remove the colors of unsegmented pixels. This option is only available for segmentation models\n    )\n    out = v.draw_dataset_dict(d)\n    indices[i].grid(False)\n    indices[i].axis('off')\n    indices[i].imshow(out.get_image()[:, :, ::-1])","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:46.321056Z","iopub.execute_input":"2022-01-20T08:32:46.321483Z","iopub.status.idle":"2022-01-20T08:32:47.438559Z","shell.execute_reply.started":"2022-01-20T08:32:46.32144Z","shell.execute_reply":"2022-01-20T08:32:47.435435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augumentation","metadata":{}},{"cell_type":"code","source":"from detectron2.data import DatasetCatalog, MetadataCatalog, build_detection_test_loader, build_detection_train_loader\nfrom detectron2.data import detection_utils as utils\nimport detectron2.data.transforms as T\n\ndef custom_mapper(dataset_dict):\n    \n    dataset_dict = copy.deepcopy(dataset_dict)\n    image = utils.read_image(dataset_dict[\"file_name\"], format=\"BGR\")\n    transform_list = [T.Resize((640,640)),\n                      T.RandomBrightness(0.8, 1.1),\n                      T.RandomFlip(prob=0.4, horizontal=False, vertical=True),\n                      T.RandomFlip(prob=0.4, horizontal=True, vertical=False),\n                      T.RandomCrop(\"absolute\", (640, 640))\n                      ]\n    image, transforms = T.apply_transform_gens(transform_list, image)\n    dataset_dict[\"image\"] = torch.as_tensor(image.transpose(2, 0, 1).astype(\"float32\"))\n\n    annos = [\n        utils.transform_instance_annotations(obj, transforms, image.shape[:2])\n        for obj in dataset_dict.pop(\"annotations\")\n        if obj.get(\"iscrowd\", 0) == 0\n    ]\n    instances = utils.annotations_to_instances(annos, image.shape[:2])\n    dataset_dict[\"instances\"] = utils.filter_empty_instances(instances)\n    return dataset_dict\nclass AugTrainer(DefaultTrainer):\n    \n    @classmethod\n    def build_train_loader(cls, cfg):\n        return build_detection_train_loader(cfg, mapper=custom_mapper)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:47.439553Z","iopub.execute_input":"2022-01-20T08:32:47.439771Z","iopub.status.idle":"2022-01-20T08:32:47.453065Z","shell.execute_reply.started":"2022-01-20T08:32:47.439741Z","shell.execute_reply":"2022-01-20T08:32:47.452413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg = get_cfg()\nconfig_name = \"COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml\" \n#config_name = \"COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml\"\ncfg.merge_from_file(model_zoo.get_config_file(config_name))\n\ncfg.DATASETS.TRAIN = (\"Train_Great_Barrier\",)\ncfg.DATASETS.TEST = (\"Valid_Great_Barrier\",)\ncfg.DATALOADER.NUM_WORKERS = 2\ncfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(config_name)\n\ncfg.SOLVER.IMS_PER_BATCH = 4\ncfg.SOLVER.BASE_LR = 0.00025\n\ncfg.SOLVER.WARMUP_ITERS = 1000\ncfg.SOLVER.MAX_ITER = 3500 #adjust up if val mAP is still rising, adjust down if overfit\ncfg.SOLVER.STEPS = (100, 500) # must be less than  MAX_ITER \ncfg.SOLVER.GAMMA = 0.05\n\n\ncfg.SOLVER.CHECKPOINT_PERIOD = 10000  # Small value=Frequent save need a lot of storage.\ncfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 128\ncfg.MODEL.ROI_HEADS.NUM_CLASSES = 1\n\n\nos.makedirs(cfg.OUTPUT_DIR, exist_ok=True)\n\n\n#Training using custom trainer defined above\ntrainer = AugTrainer(cfg) \ntrainer = DefaultTrainer(cfg) \n#trainer.resume_or_load(resume=False)\ntrainer.train()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-20T08:32:47.454398Z","iopub.execute_input":"2022-01-20T08:32:47.455298Z","iopub.status.idle":"2022-01-20T09:08:34.465634Z","shell.execute_reply.started":"2022-01-20T08:32:47.45526Z","shell.execute_reply":"2022-01-20T09:08:34.464743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Dumping config files variables as we may need them while Inferencing ","metadata":{}},{"cell_type":"code","source":"metrics_df = pd.read_json(\"./output/metrics.json\", orient=\"records\", lines=True)\nmdf = metrics_df.sort_values(\"iteration\")\nmdf.T","metadata":{"execution":{"iopub.status.busy":"2022-01-20T09:08:34.468238Z","iopub.execute_input":"2022-01-20T09:08:34.468564Z","iopub.status.idle":"2022-01-20T09:08:34.540867Z","shell.execute_reply.started":"2022-01-20T09:08:34.468519Z","shell.execute_reply":"2022-01-20T09:08:34.540015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Loss curve\n","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\n\nmdf1 = mdf[~mdf[\"total_loss\"].isna()]\nax.plot(mdf1[\"iteration\"], mdf1[\"total_loss\"], c=\"C0\", label=\"train\")\nif \"validation_loss\" in mdf.columns:\n    mdf2 = mdf[~mdf[\"validation_loss\"].isna()]\n    ax.plot(mdf2[\"iteration\"], mdf2[\"validation_loss\"], c=\"C1\", label=\"validation\")\n\n#ax.set_ylim([0, 0.5])\nax.legend()\nax.set_title(\"Loss curve\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-01-20T09:08:34.546449Z","iopub.execute_input":"2022-01-20T09:08:34.546647Z","iopub.status.idle":"2022-01-20T09:08:34.754531Z","shell.execute_reply.started":"2022-01-20T09:08:34.546623Z","shell.execute_reply":"2022-01-20T09:08:34.753802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Loss curve","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots()\n\nmdf1 = mdf[~mdf[\"fast_rcnn/cls_accuracy\"].isna()]\nax.plot(mdf1[\"iteration\"], mdf1[\"fast_rcnn/cls_accuracy\"], c=\"C0\", label=\"train\")\n# ax.set_ylim([0, 0.5])\nax.legend()\nax.set_title(\"cls_accuracy\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-20T09:08:34.75823Z","iopub.execute_input":"2022-01-20T09:08:34.758639Z","iopub.status.idle":"2022-01-20T09:08:34.94925Z","shell.execute_reply.started":"2022-01-20T09:08:34.7586Z","shell.execute_reply":"2022-01-20T09:08:34.948588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictor\nA predictor is defined with 0.5 threshold score which gives bounding box and label for the test images","metadata":{}},{"cell_type":"code","source":"!zip -r  output.zip output  ","metadata":{"execution":{"iopub.status.busy":"2022-01-20T09:08:34.950375Z","iopub.execute_input":"2022-01-20T09:08:34.95119Z","iopub.status.idle":"2022-01-20T09:08:44.738113Z","shell.execute_reply.started":"2022-01-20T09:08:34.951135Z","shell.execute_reply":"2022-01-20T09:08:44.737193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\nwith open(\"cfg.pkl\", \"wb\") as f:\n    pickle.dump(cfg, f)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T09:08:44.739733Z","iopub.execute_input":"2022-01-20T09:08:44.740018Z","iopub.status.idle":"2022-01-20T09:08:44.747853Z","shell.execute_reply.started":"2022-01-20T09:08:44.739979Z","shell.execute_reply":"2022-01-20T09:08:44.746856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, \"model_final.pth\")\npredictor = DefaultPredictor(cfg)","metadata":{"execution":{"iopub.status.busy":"2022-01-20T09:08:44.749416Z","iopub.execute_input":"2022-01-20T09:08:44.749909Z","iopub.status.idle":"2022-01-20T09:08:45.645441Z","shell.execute_reply.started":"2022-01-20T09:08:44.749871Z","shell.execute_reply":"2022-01-20T09:08:45.644642Z"},"trusted":true},"execution_count":null,"outputs":[]}]}