{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport json\n\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df= pd.read_csv('/kaggle/input/vinbigdata-512-image-dataset/vinbigdata/train.csv')\ndf.image_id= '/kaggle/input/vinbigdata-512-image-dataset/vinbigdata/train/'+df.image_id+'.png'\ndf = df[df.class_id!=14].reset_index(drop = True)\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['x_min']= (df.x_min/df.width)*512\ndf['x_max']= (df.x_max/df.width)*512\n\ndf['y_min']= (df.y_min/df.height)*512\ndf['y_max']= (df.y_max/df.height)*512","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['w']= df.x_max- df.x_min\ndf['h']= df.y_max- df.y_min\ndf_train= df[['image_id', 'x_min', 'y_min', 'w', 'h', 'class_id', 'class_name']].reset_index(drop=True)\ndf_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.isna().sum()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_ids = df_train['image_id'].unique()\nimage_dict = dict(zip(image_ids, range(len(image_ids))))\nlen(image_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"json_dict = {\"images\": [], \"type\": \"instances\", \"annotations\": [], \"categories\": []}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for image_id in image_ids:\n    image = {'file_name': image_id, \n             'height': 512, \n             'width': 512, \n             'id': image_dict[image_id]}\n    json_dict['images'].append(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for name, cls_id in zip(df_train.class_name.unique(), df_train.class_id.unique()):\n    print(name, cls_id)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for name, cls_id in zip(df_train.class_name.unique(), df_train.class_id.unique()):\n    categories = {'supercategory': 'master', 'id': cls_id, 'name': name}\n    json_dict['categories'].append(categories)\n\n# categories = {'supercategory': 'master', 'id': df_train.class_id.unique(), 'name': df_train.class_name.unique()}\n# json_dict['categories'].append(categories)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"############################## Need HELP ##########################################\nfor idx, row in tqdm(df_train.iterrows()): \n    image_id = image_dict[row['image_id']]\n    ann = {'area': row['w'] * row['h'], \n           'iscrowd': 0, \n           'image_id': image_id,                        \n           'bbox': [row['x_min'], row['y_min'], row['w'], row['h']],\n           'category_id': row.class_id, \n           'id': idx,\n           'segmentation': []}\n    \n    json_dict['annotations'].append(ann)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class NpEncoder(json.JSONEncoder):\n    def default(self, obj):\n        if isinstance(obj, np.integer):\n            return int(obj)\n        elif isinstance(obj, np.floating):\n            return float(obj)\n        elif isinstance(obj, np.ndarray):\n            return obj.tolist()\n        else:\n            return super(NpEncoder, self).default(obj)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"annFile='instances_Images.json'\n\njson_fp = open(annFile, 'w',encoding='utf-8')\njson_str = json.dumps(json_dict,cls=NpEncoder)\njson_fp.write(json_str)\njson_fp.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/kamauz/EfficientDet.git\n#https://github.com/zylo117/Yet-Another-EfficientDet-Pytorch.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd /kaggle/working/EfficientDet/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#%%capture\n!python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -r requirements.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from model import efficientdet\nfrom losses import smooth_l1, focal\nfrom efficientnet import BASE_WEIGHTS_PATH, WEIGHTS_HASHES\nfrom generators.common import Generator","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -U 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI' -q\n\nfrom pycocotools.coco import COCO","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocess_image(image):\n    image = image.astype(np.float32)\n    image /= 255.0\n#     mean = [0.485, 0.456, 0.406]\n#     std = [0.229, 0.224, 0.225]\n#     image -= mean\n#     image /= std\n\n    return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def postprocess_boxes(boxes, height, width):\n    c_boxes = boxes.copy()\n    c_boxes[:, 0] = np.clip(c_boxes[:, 0], 0, width - 1)\n    c_boxes[:, 1] = np.clip(c_boxes[:, 1], 0, height - 1)\n    c_boxes[:, 2] = np.clip(c_boxes[:, 2], 0, width - 1)\n    c_boxes[:, 3] = np.clip(c_boxes[:, 3], 0, height - 1)\n    return c_boxes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class CocoGenerator(Generator):\n    def __init__(self, data_dir, set_name, **kwargs):                                    \n        self.coco = COCO('/kaggle/working/instances_Images.json')                \n        self.image_ids = self.coco.getImgIds()\n        self.load_classes()\n\n        super(CocoGenerator, self).__init__(**kwargs)\n\n    def load_classes(self): \n        categories = self.coco.loadCats(self.coco.getCatIds())\n        categories.sort(key=lambda x: x['id'])\n\n        self.classes = {}\n        self.coco_labels = {}\n        self.coco_labels_inverse = {}\n        for c in categories:\n            self.coco_labels[len(self.classes)] = c['id']\n            self.coco_labels_inverse[c['id']] = len(self.classes)\n            self.classes[c['name']] = len(self.classes)\n\n        self.labels = {}\n        for key, value in self.classes.items():\n            self.labels[value] = key\n\n    def size(self):\n        return len(self.image_ids)\n\n    def num_classes(self):\n        return 1\n\n    def has_label(self, label):\n        return label in self.labels\n\n    def has_name(self, name):\n        return name in self.classes\n\n    def name_to_label(self, name):\n        return self.classes[name]\n\n    def label_to_name(self, label):\n        return self.labels[label]\n\n    def coco_label_to_label(self, coco_label):\n        return self.coco_labels_inverse[coco_label]\n\n    def coco_label_to_name(self, coco_label):\n        return self.label_to_name(self.coco_label_to_label(coco_label))\n\n    def label_to_coco_label(self, label):\n        return self.coco_labels[label]\n\n    def image_aspect_ratio(self, image_index):\n        image = self.coco.loadImgs(self.image_ids[image_index])[0]\n        return float(image['width']) / float(image['height'])\n\n    def load_image(self, image_index):        \n        image_info = self.coco.loadImgs(self.image_ids[image_index])[0]    \n        path = os.path.join('/kaggle/input/vinbigdata-512-image-dataset/vinbigdata/train/', image_info['file_name'])\n        print(path)\n        image = cv2.imread(path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        \n        image = preprocess_image(image)\n        \n        return image\n\n    def load_annotations(self, image_index):\n        annotations_ids = self.coco.getAnnIds(imgIds=self.image_ids[image_index], iscrowd=False)\n        annotations = {'labels': np.empty((0,), dtype=np.float32), 'bboxes': np.empty((0, 4), dtype=np.float32)}\n\n        if len(annotations_ids) == 0:\n            return annotations\n\n        coco_annotations = self.coco.loadAnns(annotations_ids)\n        for idx, a in enumerate(coco_annotations):\n            # some annotations have basically no width / height, skip them\n            if a['bbox'][2] < 1 or a['bbox'][3] < 1:\n                continue\n\n            annotations['labels'] = np.concatenate(\n                [annotations['labels'], [a['category_id'] - 1]], axis=0)\n            annotations['bboxes'] = np.concatenate([annotations['bboxes'], [[\n                a['bbox'][0],\n                a['bbox'][1],\n                a['bbox'][0] + a['bbox'][2],\n                a['bbox'][1] + a['bbox'][3],\n            ]]], axis=0)           \n\n        return annotations","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"phi = 4\nscore_threshold=0.4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = CocoGenerator(data_dir=None, set_name=None, batch_size = 2, phi = phi)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for jj in train_generator:\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"jj[0][0].shape, jj[1][0].shape#, jj[2][0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"jj[0][0][0].mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.imshow(jj[0][0][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}