{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# import torch as th\n# import torchvision.transforms as T\nimport pandas as pd\n# from PIL import Image, ImageDraw, ImageFont\nimport os\nfrom tqdm import tqdm\nimport numpy as np\nfrom sklearn import model_selection\nimport json\nimport shutil\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size = 512\nAnnotations = './vinbigdata/annotations'\nVAL_IMAGES_PATH = './vinbigdata/val2017'\nTRAIN_IMAGES_PATH = './vinbigdata/train2017' #12000\nos.makedirs(Annotations, exist_ok = True)\nos.makedirs(VAL_IMAGES_PATH, exist_ok = True)\nos.makedirs(TRAIN_IMAGES_PATH, exist_ok = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/vinbigdata-512-image-dataset/vinbigdata/train.csv')\ndf = df.fillna(0)\ndf['x_min'] = df.apply(lambda row: (row.x_min)/row.width, axis = 1)*float(size)\ndf['y_min'] = df.apply(lambda row: (row.y_min)/row.height, axis = 1)*float(size)\ndf['x_max'] = df.apply(lambda row: (row.x_max)/row.width, axis =1)*float(size)\ndf['y_max'] = df.apply(lambda row: (row.y_max)/row.height, axis =1)*float(size)\n\ndf['w'] = df.apply(lambda row: (row.x_max-row.x_min), axis =1)\ndf['h'] = df.apply(lambda row: (row.y_max-row.y_min), axis =1)\n\ndf['w'] /= float(size)\ndf['h'] /= float(size)\n\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes = [ 'No finding',\n            'Aortic enlargement',\n            'Atelectasis',\n            'Calcification',\n            'Cardiomegaly',\n            'Consolidation',\n            'ILD',\n            'Infiltration',\n            'Lung Opacity',\n            'Nodule/Mass',\n            'Other lesion',\n            'Pleural effusion',\n            'Pleural thickening',\n            'Pneumothorax',\n            'Pulmonary fibrosis']\n\ncategories = []\n\nfor i in range(len(classes)):\n    categories.append({\"supercategory\": \"none\", \"name\": classes[i], \"id\":i})\n\n# print(categories)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preproccess_data(df):\n    annot_count = 0\n    image_id = 0\n    res_file = {\n    \"categories\": categories,\n#     \"categories\": [],\n    \"images\": [],\n    \"annotations\": []\n    }\n    for column, row in tqdm(df.iterrows(), total=len(df)):\n        attributes = row[['image_id', 'class_id','x_min','y_min','x_max', 'y_max', 'w','h']].values\n#         print(attributes)\n\n  \n\n        img_elem = {\"file_name\": row['image_id'] + '.png',\n                    \"height\": row['h'],\n                    \"width\": row['w'],\n                    \"id\": image_id}\n\n        res_file[\"images\"].append(img_elem)\n\n        annot_elem = {\n            \"id\": annot_count,\n            \"bbox\": [\n                float(row['x_min']),\n                float(row['y_min']),\n                float(row['w']),\n                float(row['h'])\n            ],\n            \"segmentation\": [],\n            \"image_id\": image_id,\n            \"ignore\": 0,\n            \"category_id\": 0,\n            \"iscrowd\": 0,\n            \"area\": float(row['w'] * row['h'])\n        }\n#         if image_id == 3:\n#             break\n        res_file[\"annotations\"].append(annot_elem)\n        annot_count+=1\n        image_id+=1\n\n    return res_file\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train, df_valid = model_selection.train_test_split(df, test_size=0.2, random_state=42, shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_json = preproccess_data(df_train)\nvalid_json = preproccess_data(df_valid)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd ..","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nwith open('./vinbigdata/annotations/instances_train2017.json', \"w\") as f:\n    json_str = json.dumps(train_json)\n    f.write(json_str)\n      \n\nwith open('./vinbigdata/annotations/instances_val2017.json', \"w\") as f:\n    json_str = json.dumps(valid_json)\n    f.write(json_str)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = open('./vinbigdata/annotations/instances_train2017.json', 'r')\nprint(f.read())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def copy_images(df, images_path):\n    for column, row in tqdm(df.iterrows(), total=len(df)):\n        shutil.copy(os.path.join('/kaggle/input/vinbigdata-512-image-dataset/vinbigdata/train', f\"{row['image_id']}.png\"),images_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"copy_images(df_train, './vinbigdata/train2017')\ncopy_images(df_valid, './vinbigdata/val2017')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir('./vinbigdata/train2017')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/facebookresearch/detr.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd ./detr","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":"!python -m main --coco_path ../vinbigdata","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}