{"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":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install albumentations==0.4.5","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:50:51.228453Z","iopub.execute_input":"2022-08-31T07:50:51.229957Z","iopub.status.idle":"2022-08-31T07:51:04.911825Z","shell.execute_reply.started":"2022-08-31T07:50:51.229876Z","shell.execute_reply":"2022-08-31T07:51:04.910134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/facebookresearch/detr.git   #cloning github repo of detr to import its unique loss","metadata":{"execution":{"iopub.status.busy":"2022-08-31T06:20:21.243463Z","iopub.execute_input":"2022-08-31T06:20:21.245065Z","iopub.status.idle":"2022-08-31T06:20:25.040505Z","shell.execute_reply.started":"2022-08-31T06:20:21.244996Z","shell.execute_reply":"2022-08-31T06:20:25.038792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\n... IMPORTS STARTING ...\\n\")\n\nprint(\"\\n\\tVERSION INFORMATION\")\n\n# Machine Learning and Data Science Imports\nimport pandas as pd; pd.options.mode.chained_assignment = None;\nimport numpy as np; print(f\"\\t\\t– NUMPY VERSION: {np.__version__}\");\nimport sklearn; print(f\"\\t\\t– SKLEARN VERSION: {sklearn.__version__}\");\nfrom sklearn.preprocessing import RobustScaler, PolynomialFeatures\nfrom pandarallel import pandarallel; pandarallel.initialize();\nfrom sklearn.model_selection import GroupKFold, StratifiedKFold\nfrom scipy.spatial import cKDTree\n\n# # RAPIDS\n# import cudf, cupy, cuml\n# from cuml.neighbors import NearestNeighbors\n# from cuml.manifold import TSNE, UMAP\n# from cuml import PCA\n\n# Built In Imports\nfrom kaggle_datasets import KaggleDatasets\nfrom collections import Counter\nfrom datetime import datetime\nfrom zipfile import ZipFile\nfrom glob import glob\nimport openslide\nimport warnings\nimport requests\nimport hashlib\nimport imageio\nimport IPython\nimport sklearn\nimport urllib\nimport zipfile\nimport pickle\nimport random\nimport shutil\nimport string\nimport json\nimport math\nimport time\nimport gzip\nimport ast\nimport sys\nimport io\nimport os\nimport gc\nimport re\n\n# Visualization Imports\nfrom matplotlib.colors import ListedColormap\nfrom matplotlib.patches import Rectangle\nimport matplotlib.patches as patches\nimport plotly.graph_objects as go\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm; tqdm.pandas();\nimport plotly.express as px\nimport tifffile as tif\nimport seaborn as sns\nfrom PIL import Image, ImageEnhance; Image.MAX_IMAGE_PIXELS = 5_000_000_000;\nimport matplotlib; print(f\"\\t\\t– MATPLOTLIB VERSION: {matplotlib.__version__}\");\nfrom matplotlib import animation, rc; rc('animation', html='jshtml')\nimport plotly\nimport PIL\nimport cv2\n\nimport os\nimport numpy as np \nimport pandas as pd \nfrom datetime import datetime\nimport time\nimport random\nfrom tqdm.autonotebook import tqdm\n\n\n#Torch\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset,DataLoader\nfrom torch.utils.data.sampler import SequentialSampler, RandomSampler\n\n#sklearn\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.preprocessing import LabelEncoder\n\n#CV\nimport cv2\n\n################# DETR FUCNTIONS FOR LOSS######################## \nimport sys\nsys.path.append('./detr/')\n\nfrom detr.models.matcher import HungarianMatcher\nfrom detr.models.detr import SetCriterion\n#################################################################\n\n#Albumenatations\nimport albumentations as A\nimport matplotlib.pyplot as plt\nfrom albumentations.pytorch.transforms import ToTensorV2\n\n#Glob\nfrom glob import glob\n\nimport plotly.io as pio\nprint(pio.renderers)\n\ndef flatten_l_o_l(nested_list):\n    \"\"\" Flatten a list of lists \"\"\"\n    return [item for sublist in nested_list for item in sublist]\n\ndef load_json_to_dict(json_path):\n    \"\"\" tbd \"\"\"\n    with open(json_path) as json_file:\n        data = json.load(json_file)\n    return data\n    \nprint(\"\\n\\n... IMPORTS COMPLETE ...\\n\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-31T09:02:21.898817Z","iopub.status.idle":"2022-08-31T09:02:21.900405Z","shell.execute_reply.started":"2022-08-31T09:02:21.899769Z","shell.execute_reply":"2022-08-31T09:02:21.899833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_coco2017(dir_path):\n    annot_dir = os.path.join(dir_path, \"annotations\")\n    \n    coco_instances_train = load_json_to_dict(os.path.join(annot_dir, \"instances_train2017.json\"))\n    \n    COCO_I2S = {c_map[\"id\"]:c_map[\"name\"] for c_map in coco_instances_train[\"categories\"]}\n    COCO_S2I = {v:k for k,v in COCO_I2S.items()}\n    \n    train_license_map = {_l[\"id\"]:_l[\"name\"] for _l in coco_instances_train[\"licenses\"]}\n    train_images, train_annots = coco_instances_train[\"images\"], coco_instances_train[\"annotations\"]\n    train_df = pd.merge(pd.DataFrame(train_annots), pd.DataFrame(train_images), left_on=\"image_id\", right_on=\"id\").drop(columns=[\"id_x\", \"id_y\"]).sort_values(by=\"image_id\")\n    train_df[\"license\"] = train_df[\"license\"].map(train_license_map)\n    train_df[\"img_path\"] = os.path.join(dir_path, \"train2017\")+\"/\"+train_df[\"file_name\"]\n    train_df[\"str_category\"] = train_df[\"category_id\"].map(COCO_I2S)\n    train_df = train_df[['image_id', 'file_name', 'img_path', 'category_id',\n                         'str_category', 'segmentation', 'area', 'iscrowd', 'bbox', 'license', \n                         'coco_url', 'height', 'width', 'date_captured','flickr_url']].reset_index(drop=True)\n    \n    coco_instances_val = load_json_to_dict(os.path.join(annot_dir, \"instances_val2017.json\"))\n    val_license_map = {_l[\"id\"]:_l[\"name\"] for _l in coco_instances_val[\"licenses\"]}\n    val_images, val_annots = coco_instances_val[\"images\"], coco_instances_val[\"annotations\"]\n    val_df = pd.merge(pd.DataFrame(val_annots), pd.DataFrame(val_images), left_on=\"image_id\", right_on=\"id\").drop(columns=[\"id_x\", \"id_y\"]).sort_values(by=\"image_id\")\n    val_df[\"license\"] = val_df[\"license\"].map(val_license_map)\n    val_df[\"img_path\"] = os.path.join(dir_path, \"val2017\")+\"/\"+val_df[\"file_name\"]\n    val_df[\"str_category\"] = val_df[\"category_id\"].map(COCO_I2S)\n    val_df = val_df[['image_id', 'file_name', 'img_path', 'category_id', \n                     'str_category', 'segmentation', 'area', 'iscrowd', 'bbox', 'license', \n                     'coco_url', 'height', 'width', 'date_captured','flickr_url']].reset_index(drop=True)\n    \n    # Get test dataframe images\n    test_df = pd.DataFrame({\"img_path\":glob(os.path.join(dir_path, \"test2017\", \"*.jpg\"))})\n    test_df.insert(0, \"image_id\", test_df[\"img_path\"].apply(lambda x: x.rsplit(\"/\", 1)[-1][:-4]))\n\n    # Cleanup\n    gc.collect(); gc.collect(); gc.collect();\n    \n    return train_df, val_df, test_df\n    \ntrain_df, val_df, test_df = load_coco2017(\"../input/coco2017/annotations_trainval2017\")\nN_TRAIN, N_VAL, N_TEST = len(train_df), len(val_df), len(test_df)\n\nprint(f\"\\n\\n... COCO TRAINING DATAFRAME ({N_TRAIN} EXAMPLES OVER {train_df.image_id.nunique()} IMAGES) ...\\n\")\ndisplay(train_df)\n\nprint(f\"\\n\\n\\n... COCO VALIDATION DATAFRAME ({N_VAL} EXAMPLES OVER {val_df.image_id.nunique()} IMAGES) ...\\n\")\ndisplay(val_df)\n\nprint(f\"\\n\\n\\n... COCO VALIDATION DATAFRAME ({N_TEST} EXAMPLES OVER {test_df.image_id.nunique()} IMAGES) ...\\n\")\ndisplay(test_df)\n\nfig = px.histogram(train_df, \"str_category\", color=\"str_category\", title=\"<b>COCO CLASS DISTRIBUTION</b>\")\nfig.update_layout(showlegend=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T07:50:05.461245Z","iopub.execute_input":"2022-08-31T07:50:05.461718Z","iopub.status.idle":"2022-08-31T07:50:51.224470Z","shell.execute_reply.started":"2022-08-31T07:50:05.461686Z","shell.execute_reply":"2022-08-31T07:50:51.222513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = train_df[['file_name', 'bbox', 'str_category']].iloc[:50000]\ncolumns = ['x', 'y', 'w', 'h']\nboxes = np.stack(label['bbox'].values)\nfor i, column in enumerate(columns):\n    label[column] = boxes[:,i]\nlabel.set_index('file_name', inplace=True)\nlb_en = LabelEncoder()\nlabel['label'] = lb_en.fit_transform(label['str_category'])\nlabel.drop(['str_category', 'bbox'], axis=1, inplace=True)\nlabel","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:50:16.163595Z","iopub.execute_input":"2022-08-31T08:50:16.164008Z","iopub.status.idle":"2022-08-31T08:50:16.609519Z","shell.execute_reply.started":"2022-08-31T08:50:16.163978Z","shell.execute_reply":"2022-08-31T08:50:16.608139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = label.groupby(label.index)['label'].count()\nsample_df","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:50:18.485082Z","iopub.execute_input":"2022-08-31T08:50:18.485577Z","iopub.status.idle":"2022-08-31T08:50:18.514962Z","shell.execute_reply.started":"2022-08-31T08:50:18.485544Z","shell.execute_reply":"2022-08-31T08:50:18.513552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label = pd.read_csv('../input/global-wheat-detection/train.csv')\n# label['image'] = label['image_id'] + '.jpg'\n# label.drop('image_id', axis=1, inplace=True)\n# label.set_index('image', inplace=True)\n# label['bbox'] = label['bbox'].apply(lambda x: np.fromstring(x[1:-1], sep=','))\n\n# columns = ['x', 'y', 'w', 'h']\n# for i, column in enumerate(columns):\n#     label[column] = np.stack(label['bbox'].values)[:,i]\n# label.drop(['width','height', 'bbox'], axis=1, inplace=True)\n\n# lb_en = LabelEncoder()\n# label['label'] = lb_en.fit_transform(label['source'])\n# label","metadata":{"execution":{"iopub.status.busy":"2022-08-31T06:21:46.654359Z","iopub.execute_input":"2022-08-31T06:21:46.658204Z","iopub.status.idle":"2022-08-31T06:21:48.073978Z","shell.execute_reply.started":"2022-08-31T06:21:46.658155Z","shell.execute_reply":"2022-08-31T06:21:48.072381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label = pd.read_csv('../input/car-object-detection/data/train_solution_bounding_boxes (1).csv')\n# label['x'] = (label['xmin'] + label['xmax'])/2\n# label['y'] = (label['ymin'] + label['ymax'])/2\n# label['w'] = (label['xmax'] - label['xmin'])\n# label['h'] = (label['ymax'] - label['ymin'])\n# label.drop(['xmin','ymin','xmax','ymax'], axis=1,inplace=True)\n# label['source'] = 'car'\n# label.set_index('image',inplace=True)\n# label","metadata":{"execution":{"iopub.status.busy":"2022-08-31T06:20:30.376561Z","iopub.execute_input":"2022-08-31T06:20:30.377501Z","iopub.status.idle":"2022-08-31T06:20:30.438982Z","shell.execute_reply.started":"2022-08-31T06:20:30.377453Z","shell.execute_reply":"2022-08-31T06:20:30.437484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Augmentations\n\n* Augmentations will play a major role and hence took her up advice and use awesome augmentations, cut-mix.","metadata":{}},{"cell_type":"code","source":"def get_train_transforms():\n    return A.Compose([A.OneOf([\n                     A.HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit= 0.2, val_shift_limit=0.2, p=0.9),\n                               \n                      A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.9)]\n                    ,p=0.9),\n                      A.ToGray(p=0.01),\n                      \n                      A.HorizontalFlip(p=0.5),\n                      \n                      A.Resize(height=512, width=512, p=1),\n                      \n                      A.Cutout(num_holes=8, max_h_size=64, max_w_size=64, fill_value=0, p=0.5),\n                      \n                      ToTensorV2(p=1.0)],\n                      \n                      p=1.0,\n                     \n                      bbox_params=A.BboxParams(format='coco',min_area=0, min_visibility=0,label_fields=['labels'])\n                      )\n\ndef get_valid_transforms():\n    return A.Compose([A.Resize(height=512, width=512, p=1.0),\n                      ToTensorV2(p=1.0)], \n                      p=1.0, \n                      bbox_params=A.BboxParams(format='coco',min_area=0, min_visibility=0,label_fields=['labels'])\n                      )","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:36.622647Z","iopub.execute_input":"2022-08-31T08:37:36.623090Z","iopub.status.idle":"2022-08-31T08:37:36.636027Z","shell.execute_reply.started":"2022-08-31T08:37:36.623059Z","shell.execute_reply":"2022-08-31T08:37:36.634589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DIR_TRAIN = '../input/coco2017/train2017/train2017'\n\nclass CarDataset(Dataset):\n    \n    def __init__(self, image_ids, dataframe, transforms=None):\n        self.image_ids = image_ids\n        self.df = dataframe\n        self.transforms = transforms\n        \n    def __len__(self):\n        return self.image_ids.shape[0]\n    \n    def __getitem__(self, index):\n        image_id = self.image_ids[index]\n        records = self.df.loc[image_id]\n        \n        image = cv2.imread(f'{DIR_TRAIN}/{image_id}', cv2.IMREAD_COLOR)\n#         image = cv2.imread(image_id, cv2.IMREAD_COLOR)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).astype(np.float32)\n        image /= 255.0\n        \n        # DETR takes in data in coco format\n        boxes = records[['x', 'y', 'w', 'h']].values\n        if len(boxes.shape) == 1:\n            boxes = boxes[np.newaxis, ...]\n        \n        # area of bounding boxes\n        area = boxes[:, 2] * boxes[:, 3]\n        area = torch.as_tensor(area.astype(np.float32), dtype=torch.float32)\n        \n#         labels = np.zeros(len(boxes), dtype=np.int32)\n        labels = label.label.values\n        if self.transforms:\n            sample = {\n                'image': image,\n                'bboxes': boxes,\n                'labels': labels\n            }\n            sample = self.transforms(**sample)\n            image = sample['image']\n            boxes = sample['bboxes']\n            labels = sample['labels']\n            \n        # Normalizing bounding boxes\n        _,h,w = image.shape\n        boxes = A.augmentations.bbox_utils.normalize_bboxes(sample['bboxes'],rows=h,cols=w)\n\n        target = {}\n        target['boxes'] = torch.as_tensor(boxes,dtype=torch.float32)\n        target['labels'] = torch.as_tensor(labels,dtype=torch.long)\n        target['image_id'] = torch.tensor([index])\n        target['area'] = area\n        \n        return image, target, image_id","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.228602Z","iopub.execute_input":"2022-08-31T08:37:37.230705Z","iopub.status.idle":"2022-08-31T08:37:37.246239Z","shell.execute_reply.started":"2022-08-31T08:37:37.230658Z","shell.execute_reply":"2022-08-31T08:37:37.244906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = label.iloc[:400]\n\ntrain_dataset = CarDataset(\nimage_ids=df_train.index.values,\ndataframe=label,\ntransforms=get_train_transforms()\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.548884Z","iopub.execute_input":"2022-08-31T08:37:37.550635Z","iopub.status.idle":"2022-08-31T08:37:37.557873Z","shell.execute_reply.started":"2022-08-31T08:37:37.550591Z","shell.execute_reply":"2022-08-31T08:37:37.556367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image, target, image_id in train_dataset:\n    print(image.shape)\n    print(target)\n    print(image_id)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:37.755284Z","iopub.execute_input":"2022-08-31T08:37:37.756686Z","iopub.status.idle":"2022-08-31T08:37:37.795382Z","shell.execute_reply.started":"2022-08-31T08:37:37.756641Z","shell.execute_reply":"2022-08-31T08:37:37.793737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model\n* initial DETR model is trained on coco dataset, which has 91 classes + 1 background class, hence we need to modify it to take our own number of classes.\n* Also DETR model takes in 100 queries ie, it outputs total of 100 bboxes for every image, we can very well change that too.","metadata":{}},{"cell_type":"code","source":"class DETRModel(nn.Module):\n    def __init__(self,num_classes,num_queries):\n        super(DETRModel,self).__init__()\n        self.num_classes = num_classes\n        self.num_queries = num_queries\n        \n        self.model = torch.hub.load('facebookresearch/detr', 'detr_resnet50', pretrained=True)\n        self.in_features = self.model.class_embed.in_features\n        \n        self.model.class_embed = nn.Linear(in_features=self.in_features,out_features=self.num_classes)\n#         self.output_activate = nn.Softmax(dim=1)\n        self.model.num_queries = self.num_queries\n        \n    def forward(self,images):\n        output = self.model(images)\n#         output['pred_logits'] = self.output_activate(output['pred_logits'])\n        return output","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:38.149950Z","iopub.execute_input":"2022-08-31T08:37:38.150561Z","iopub.status.idle":"2022-08-31T08:37:38.159947Z","shell.execute_reply.started":"2022-08-31T08:37:38.150529Z","shell.execute_reply":"2022-08-31T08:37:38.158498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\ncode taken from github repo detr , 'code present in engine.py'\n'''\n\nmatcher = HungarianMatcher()\n\nweight_dict = weight_dict = {'loss_ce': 1, 'loss_bbox': 1 , 'loss_giou': 1}\n\nlosses = ['labels', 'boxes', 'cardinality']","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:41.242525Z","iopub.execute_input":"2022-08-31T08:37:41.244669Z","iopub.status.idle":"2022-08-31T08:37:41.253896Z","shell.execute_reply.started":"2022-08-31T08:37:41.244609Z","shell.execute_reply":"2022-08-31T08:37:41.252240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_fn(data_loader,model,criterion,optimizer,device,scheduler,epoch):\n    model.train()\n    criterion.train()\n    \n    summary_loss = AverageMeter()\n    \n    tk0 = tqdm(data_loader, total=len(data_loader))\n    \n    for step, (images, targets, image_ids) in enumerate(tk0):\n        \n        images = list(image.to(device) for image in images)\n        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n        \n\n        output = model(images)\n        \n        loss_dict = criterion(output, targets)\n        weight_dict = criterion.weight_dict\n        \n        losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)\n        \n        optimizer.zero_grad()\n\n        losses.backward()\n        optimizer.step()\n        if scheduler is not None:\n            scheduler.step()\n        \n        summary_loss.update(losses.item(),BATCH_SIZE)\n        tk0.set_postfix(loss=summary_loss.avg)\n\n    return summary_loss","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:41.519282Z","iopub.execute_input":"2022-08-31T08:37:41.520681Z","iopub.status.idle":"2022-08-31T08:37:41.532368Z","shell.execute_reply.started":"2022-08-31T08:37:41.520623Z","shell.execute_reply":"2022-08-31T08:37:41.530877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def eval_fn(data_loader, model,criterion, device):\n    model.eval()\n    criterion.eval()\n    summary_loss = AverageMeter()\n    \n    with torch.no_grad():\n        \n        tk0 = tqdm(data_loader, total=len(data_loader))\n        for step, (images, targets, image_ids) in enumerate(tk0):\n            \n            images = list(image.to(device) for image in images)\n            targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n\n            output = model(images)\n        \n            loss_dict = criterion(output, targets)\n            weight_dict = criterion.weight_dict\n        \n            losses = sum(loss_dict[k] * weight_dict[k] for k in loss_dict.keys() if k in weight_dict)\n            \n            summary_loss.update(losses.item(),BATCH_SIZE)\n            tk0.set_postfix(loss=summary_loss.avg)\n    \n    return summary_loss","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:41.796244Z","iopub.execute_input":"2022-08-31T08:37:41.797567Z","iopub.status.idle":"2022-08-31T08:37:41.808293Z","shell.execute_reply.started":"2022-08-31T08:37:41.797523Z","shell.execute_reply":"2022-08-31T08:37:41.806778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def collate_fn(batch):\n    return tuple(zip(*batch))","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:42.093804Z","iopub.execute_input":"2022-08-31T08:37:42.094149Z","iopub.status.idle":"2022-08-31T08:37:42.102366Z","shell.execute_reply.started":"2022-08-31T08:37:42.094122Z","shell.execute_reply":"2022-08-31T08:37:42.099610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class AverageMeter(object):\n    \"\"\"Computes and stores the average and current value\"\"\"\n    def __init__(self):\n        self.reset()\n\n    def reset(self):\n        self.val = 0\n        self.avg = 0\n        self.sum = 0\n        self.count = 0\n\n    def update(self, val, n=1):\n        self.val = val\n        self.sum += val * n\n        self.count += n\n        self.avg = self.sum / self.count","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:37:42.403628Z","iopub.execute_input":"2022-08-31T08:37:42.404631Z","iopub.status.idle":"2022-08-31T08:37:42.415035Z","shell.execute_reply.started":"2022-08-31T08:37:42.404571Z","shell.execute_reply":"2022-08-31T08:37:42.413689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run():\n    \n    df_train = sample_df.iloc[:6000]\n    df_valid = sample_df.iloc[6000:]\n    \n    train_dataset = CarDataset(\n    image_ids=df_train.index.values,\n    dataframe=label,\n    transforms=get_train_transforms()\n    )\n\n    valid_dataset = CarDataset(\n    image_ids=df_valid.index.values,\n    dataframe=label,\n    transforms=get_valid_transforms()\n    )\n    \n    train_data_loader = DataLoader(\n    train_dataset,\n    batch_size=BATCH_SIZE,\n    shuffle=False,\n    num_workers=4,\n    collate_fn=collate_fn\n    )\n\n    valid_data_loader = DataLoader(\n    valid_dataset,\n    batch_size=BATCH_SIZE,\n    shuffle=False,\n    num_workers=4,\n    collate_fn=collate_fn\n    )\n    \n    device = torch.device('cuda')\n    model = DETRModel(num_classes=num_classes,num_queries=num_queries)\n    model = model.to(device)\n    criterion = SetCriterion(num_classes-1, matcher, weight_dict, eos_coef = null_class_coef, losses=losses)\n    criterion = criterion.to(device)\n    \n\n    optimizer = torch.optim.AdamW(model.parameters(), lr=LR)\n    \n    best_loss = 10**5\n    for epoch in range(EPOCHS):\n        train_loss = train_fn(train_data_loader, model,criterion, optimizer,device,scheduler=None,epoch=epoch)\n        valid_loss = eval_fn(valid_data_loader, model,criterion, device)\n        \n        print('|EPOCH {}| TRAIN_LOSS {}| VALID_LOSS {}|'.format(epoch+1,train_loss.avg,valid_loss.avg))\n        \n        if valid_loss.avg < best_loss:\n            best_loss = valid_loss.avg\n            print('Best model found in Epoch {}........Saving Model'.format(epoch+1))\n            torch.save(model.state_dict(), f'detr_best.pth')","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:50:35.269675Z","iopub.execute_input":"2022-08-31T08:50:35.270091Z","iopub.status.idle":"2022-08-31T08:50:35.284733Z","shell.execute_reply.started":"2022-08-31T08:50:35.270060Z","shell.execute_reply":"2022-08-31T08:50:35.282628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_folds = 5\nseed = 42\nnum_classes = label.label.nunique()\nnum_queries = 100\nnull_class_coef = 0.5\nBATCH_SIZE = 8\nLR = 2e-5\nEPOCHS = 3","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:50:35.666187Z","iopub.execute_input":"2022-08-31T08:50:35.666608Z","iopub.status.idle":"2022-08-31T08:50:35.674861Z","shell.execute_reply.started":"2022-08-31T08:50:35.666572Z","shell.execute_reply":"2022-08-31T08:50:35.673210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"run()","metadata":{"execution":{"iopub.status.busy":"2022-08-31T09:05:16.387952Z","iopub.execute_input":"2022-08-31T09:05:16.388400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_sample(df_valid,sample_id, model,device):\n    '''\n    Code taken from Peter's Kernel \n    https://www.kaggle.com/pestipeti/pytorch-starter-fasterrcnn-train\n    '''\n    valid_dataset = CarDataset(image_ids=df_valid.index.values,\n                                 dataframe=label,\n                                 transforms=get_valid_transforms()\n                                )\n     \n    valid_data_loader = DataLoader(\n                                    valid_dataset,\n                                    batch_size=BATCH_SIZE,\n                                    shuffle=False,\n                                   num_workers=4,\n                                   collate_fn=collate_fn)\n    \n    images, targets, image_ids = next(iter(valid_data_loader))\n    _,h,w = images[sample_id].shape # for de normalizing images\n    \n    images = list(img.to(device) for img in images)\n    targets = [{k: v.to(device) for k, v in t.items()} for t in targets]\n    \n    boxes = targets[sample_id]['boxes'].cpu().numpy()\n    labels = targets[sample_id]['labels'].cpu().numpy()\n    boxes = [np.array(box).astype(np.int32) for box in A.augmentations.bbox_utils.denormalize_bboxes(boxes,h,w)]\n    sample = images[sample_id].permute(1,2,0).cpu().numpy()\n    \n    model.eval()\n    model.to(device)\n    cpu_device = torch.device(\"cpu\")\n    \n    with torch.no_grad():\n        outputs = model(images)\n        \n    outputs = [{k: v.to(cpu_device) for k, v in outputs.items()}]\n    \n    fig, ax = plt.subplots(1, 1, figsize=(16, 8))\n\n    for box in boxes:\n        cv2.rectangle(sample,\n                      (box[0], box[1]),\n                      (box[2]+box[0], box[3]+box[1]),\n                      (220, 0, 0), 1)\n        \n\n    oboxes = outputs[0]['pred_boxes'][sample_id].detach().cpu().numpy()\n    oboxes = [np.array(box).astype(np.int32) for box in A.augmentations.bbox_utils.denormalize_bboxes(oboxes,h,w)]\n    prob = outputs[0]['pred_logits'][sample_id].softmax(1).detach().cpu().numpy()\n    pred_label = np.argmax(prob, axis=1)\n    prob = np.max(prob,axis=1)\n    print(prob)\n    print(labels)\n    \n    for box,p in zip(oboxes,prob):\n        if p >0.9:\n            color = (0,0,220) #if p>0.5 else (0,0,0)\n            cv2.rectangle(sample,\n                  (box[0], box[1]),\n                  (box[2]+box[0], box[3]+box[1]),\n                  color, 1)\n    \n    ax.set_axis_off()\n    ax.imshow(sample)","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:43:41.916425Z","iopub.execute_input":"2022-08-31T08:43:41.916907Z","iopub.status.idle":"2022-08-31T08:43:41.937266Z","shell.execute_reply.started":"2022-08-31T08:43:41.916867Z","shell.execute_reply":"2022-08-31T08:43:41.935533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = DETRModel(num_classes=num_classes,num_queries=num_queries)\nmodel.load_state_dict(torch.load(\"./detr_best.pth\"))\nview_sample(sample_df[:100], 0, model=model, device=torch.device('cuda'))","metadata":{"execution":{"iopub.status.busy":"2022-08-31T08:43:42.627107Z","iopub.execute_input":"2022-08-31T08:43:42.628769Z","iopub.status.idle":"2022-08-31T08:43:47.828817Z","shell.execute_reply.started":"2022-08-31T08:43:42.628724Z","shell.execute_reply":"2022-08-31T08:43:47.827314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}