{"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 sys\nsys.path.append('../input/cowboy-detection')\n\n!pip install pycocotools\n!nvidia-smi","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:28:12.954120Z","iopub.execute_input":"2021-08-04T16:28:12.954595Z","iopub.status.idle":"2021-08-04T16:28:28.469950Z","shell.execute_reply.started":"2021-08-04T16:28:12.954548Z","shell.execute_reply":"2021-08-04T16:28:28.468918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\n\nimport torch\nfrom tqdm import tqdm\nimport os\n\n\nimport modeling\nfrom coco_eval import coco_eval\nfrom data_process import CocoDataLoader\nfrom evaluate import Pipeline, preds2coco_eval_result\nfrom train import Trainer\n","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:28:28.473289Z","iopub.execute_input":"2021-08-04T16:28:28.473579Z","iopub.status.idle":"2021-08-04T16:28:28.734899Z","shell.execute_reply.started":"2021-08-04T16:28:28.473549Z","shell.execute_reply":"2021-08-04T16:28:28.734026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Args:\n    num_epochs = 1\n    device = 'cuda:0'\n    model_name = 'm28'\n    best_epoch = 0\n    lr = 0.005\n    momentum = 0.9\n    weight_decay = 0.01\n    \n    data_dir = '../input/cowboyoutfits'\n    model_out = f'/kaggle/working/models/faster_rcnn/{model_name}'\n    log_dir = f'/kaggle/working/runs/faster_rcnn/{model_name}'\n\n    best_model_path = f'{model_out}/e{best_epoch}.pt'\n    outfile_name = f'faster_rcnn_{model_name}e{best_epoch}.json'\n    eval_coco_path = f'/kaggle/working/{outfile_name}'\n    \nargs = Args()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:28:28.736886Z","iopub.execute_input":"2021-08-04T16:28:28.737229Z","iopub.status.idle":"2021-08-04T16:28:28.745492Z","shell.execute_reply.started":"2021-08-04T16:28:28.737201Z","shell.execute_reply":"2021-08-04T16:28:28.742494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## train","metadata":{}},{"cell_type":"code","source":"\n\ncdl = CocoDataLoader(args.data_dir, from_cache=False)\ntrain_data = cdl.train_95\ndev_data = cdl.dev_05\n\nmodel = modeling.get_fasterrcnn_resnet50_model(num_classes=6)\n\nparams = [p for p in model.parameters() if p.requires_grad]\noptimizer = torch.optim.SGD(params, lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)\n\ntrainer = Trainer(args=args,\n                  model=model,\n                  optimizer=optimizer,\n                  train_data=train_data,\n                  dev_data=dev_data)\ntrainer.train()\n","metadata":{"_uuid":"03c6a6cf-ca6e-491f-bac3-8b9f24d6f2bd","_cell_guid":"842c45eb-0e18-41f2-a669-2c57929e7c0c","execution":{"iopub.status.busy":"2021-08-04T16:28:28.747293Z","iopub.execute_input":"2021-08-04T16:28:28.747749Z","iopub.status.idle":"2021-08-04T16:54:02.966147Z","shell.execute_reply.started":"2021-08-04T16:28:28.747710Z","shell.execute_reply":"2021-08-04T16:54:02.965283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## validation","metadata":{}},{"cell_type":"code","source":"pipeline = Pipeline(args.best_model_path, args.device)\n\ntest_data = cdl.test_all\nidx2str = cdl.idx2str\nidx2label = cdl.idx2label\n\npreds = []\nimage_ids = []\n\nfor batched_imgs, batched_targets in tqdm(test_data):\n    batched_preds = pipeline(batched_imgs)\n\n    preds += batched_preds\n    image_ids += [_[0]['image_id'] for _ in batched_targets]\n\ncoco_result = preds2coco_eval_result(preds, image_ids, idx2label)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:54:02.967599Z","iopub.execute_input":"2021-08-04T16:54:02.968150Z","iopub.status.idle":"2021-08-04T17:00:46.832800Z","shell.execute_reply.started":"2021-08-04T16:54:02.968111Z","shell.execute_reply":"2021-08-04T17:00:46.831000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nwith open(args.eval_coco_path, 'w') as fout:\n    json.dump(coco_result, fout)\n\nfrom pycocotools.coco import COCO\nfrom pycocotools.cocoeval import COCOeval\n\n\ndef coco_eval(res_path):\n    coco_gt = COCO( '../input/cowboyoutfits/train.json')\n    coco_dt = coco_gt.loadRes(res_path)\n    ce = COCOeval(coco_gt, coco_dt, \"bbox\")\n    ce.evaluate()\n    ce.accumulate()\n    ce.summarize()\n\ncoco_eval(args.eval_coco_path)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:04:33.510222Z","iopub.execute_input":"2021-08-04T17:04:33.510742Z","iopub.status.idle":"2021-08-04T17:04:35.689709Z","shell.execute_reply.started":"2021-08-04T17:04:33.510698Z","shell.execute_reply":"2021-08-04T17:04:35.688901Z"},"trusted":true},"execution_count":null,"outputs":[]}]}