{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport sys\nos.listdir(\"/kaggle/input/kaggle-monk-efficientdet/kaggle_monk_efficientdet\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cp -r /kaggle/input/kaggle-monk-efficientdet/kaggle_monk_efficientdet .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cd kaggle_monk_efficientdet/cocoapi/PythonAPI/ && pip install -e .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sys.path.append(\"kaggle_monk_efficientdet/cocoapi/PythonAPI/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cd kaggle_monk_efficientdet/EfficientNet-PyTorch/ && pip install -e .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sys.path.append(\"kaggle_monk_efficientdet/EfficientNet-PyTorch/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cd kaggle_monk_efficientdet/ && pip install xmltodict-0.12.0-py2.py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cd kaggle_monk_efficientdet/dicttoxml-1.7.4/ && pip install -e .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sys.path.append(\"kaggle_monk_efficientdet/dicttoxml-1.7.4/\")","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":"import os\nimport sys\nimport pandas as pd\nimport cv2\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"/kaggle/input/global-wheat-detection\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\");\n\ncombined = [];\nimg_name_current = df.iloc[0][\"image_id\"] + \".jpg\";\nwr = \"\";\nfrom tqdm import tqdm\nfor i in tqdm(range(len(df))):\n    img_name = df.iloc[i][\"image_id\"] + \".jpg\";\n    if(img_name_current != img_name):\n        wr = wr[:len(wr)-1];\n        combined.append([img_name_current, wr]);\n        img_name_current = img_name;\n        wr = \"\";\n        #break;\n    bbox = df.iloc[i][\"bbox\"]\n    bbox = bbox[1:len(bbox)-1].split(\",\");\n    x1 = int(float(bbox[0]));\n    y1 = int(float(bbox[1]));\n    x2 = int(float(bbox[0])) + int(float(bbox[2]));\n    y2 = int(float(bbox[1])) + int(float(bbox[3]));\n    \n    wr += str(x1) + \" \" + str(y1) + \" \" + str(x2) + \" \" + str(y2) + \" wheat \";","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(combined, columns = ['Id', 'Labels']);\ndf.to_csv(\"train_labels.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport numpy as np \nimport cv2\nfrom tqdm import tqdm\nimport shutil\nimport json\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir(\"wheat_dataset\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cp -r /kaggle/input/global-wheat-detection/train wheat_dataset/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"root = \"wheat_dataset/\";\nimg_dir = \"train/\";\nanno_file = \"train_labels.csv\";\n\n\n\ndataset_path = root;\nimages_folder = root + \"/\" + img_dir;\nannotations_path = \"/annotations/\";\n\nif not os.path.isdir(annotations_path):\n    os.mkdir(annotations_path)\n    \ninput_images_folder = images_folder;\ninput_annotations_path = anno_file;\n\noutput_dataset_path = root;\noutput_image_folder = input_images_folder;\noutput_annotation_folder = root + \"/\" + annotations_path;\n\ntmp = img_dir.replace(\"/\", \"\");\noutput_annotation_file = output_annotation_folder + \"/instances_\" + tmp + \".json\";\noutput_classes_file = output_annotation_folder + \"/classes.txt\";\n\nif not os.path.isdir(output_annotation_folder):\n    os.mkdir(output_annotation_folder);\n    \ndf = pd.read_csv(input_annotations_path);\ncolumns = df.columns\n\ndelimiter = \" \";\n\nlist_dict = [];\nanno = [];\nfor i in range(len(df)):\n    img_name = df[columns[0]][i];\n    labels = df[columns[1]][i];\n    tmp = labels.split(delimiter);\n    for j in range(len(tmp)//5):\n        label = tmp[j*5+4];\n        if(label not in anno):\n            anno.append(label);\n    anno = sorted(anno)\n    \nfor i in tqdm(range(len(anno))):\n    tmp = {};\n    tmp[\"supercategory\"] = \"master\";\n    tmp[\"id\"] = i;\n    tmp[\"name\"] = anno[i];\n    list_dict.append(tmp);\n\nanno_f = open(output_classes_file, 'w');\nfor i in range(len(anno)):\n    anno_f.write(anno[i] + \"\\n\");\nanno_f.close();\n\ncoco_data = {};\ncoco_data[\"type\"] = \"instances\";\ncoco_data[\"images\"] = [];\ncoco_data[\"annotations\"] = [];\ncoco_data[\"categories\"] = list_dict;\nimage_id = 0;\nannotation_id = 0;\n\n\nfor i in tqdm(range(len(df))):\n    img_name = df[columns[0]][i];\n    labels = df[columns[1]][i];\n    tmp = labels.split(delimiter);\n    image_in_path = input_images_folder + \"/\" + img_name;\n    img = cv2.imread(image_in_path, 1);\n    h, w, c = img.shape;\n\n    images_tmp = {};\n    images_tmp[\"file_name\"] = img_name;\n    images_tmp[\"height\"] = h;\n    images_tmp[\"width\"] = w;\n    images_tmp[\"id\"] = image_id;\n    coco_data[\"images\"].append(images_tmp);\n    \n\n    for j in range(len(tmp)//5):\n        x1 = int(tmp[j*5+0]);\n        y1 = int(tmp[j*5+1]);\n        x2 = int(tmp[j*5+2]);\n        y2 = int(tmp[j*5+3]);\n        label = tmp[j*5+4];\n        annotations_tmp = {};\n        annotations_tmp[\"id\"] = annotation_id;\n        annotation_id += 1;\n        annotations_tmp[\"image_id\"] = image_id;\n        annotations_tmp[\"segmentation\"] = [];\n        annotations_tmp[\"ignore\"] = 0;\n        annotations_tmp[\"area\"] = (x2-x1)*(y2-y1);\n        annotations_tmp[\"iscrowd\"] = 0;\n        annotations_tmp[\"bbox\"] = [x1, y1, x2-x1, y2-y1];\n        annotations_tmp[\"category_id\"] = anno.index(label);\n\n        coco_data[\"annotations\"].append(annotations_tmp)\n    image_id += 1;\n\noutfile =  open(output_annotation_file, 'w');\njson_str = json.dumps(coco_data, indent=4);\noutfile.write(json_str);\noutfile.close();","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":"import os\nimport sys\nsys.path.append(\"kaggle_monk_efficientdet/Monk_Object_Detection/4_efficientdet/lib/\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from train_detector import Detector\ngtf = Detector();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"root_dir = \"wheat_dataset/\";\ncoco_dir = \"\";\nimg_dir = \"\";\nset_dir = \"train\";","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Train_Dataset(root_dir, coco_dir, img_dir, set_dir, batch_size=8, image_size=512, use_gpu=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! mkdir /root/.cache/torch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! mkdir /root/.cache/torch/checkpoints","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cp kaggle_monk_efficientdet/efficientnet-b0-355c32eb.pth /root/.cache/torch/checkpoints/efficientnet-b0-355c32eb.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Model();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Set_Hyperparams(lr=0.0001, val_interval=1, es_min_delta=0.0, es_patience=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Train(num_epochs=50, model_output_dir=\"trained/\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport sys\nsys.path.append(\"Monk_Object_Detection/4_efficientdet/lib/\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from infer_detector import Infer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf = Infer();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Model(model_dir=\"trained/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = open(\"wheat_dataset/annotations/classes.txt\", 'r');\nclass_list = f.readlines();\nf.close();\nfor i in range(len(class_list)):\n    class_list[i] = class_list[i][:-1]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_list = os.listdir(\"/kaggle/input/global-wheat-detection/test/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"combined = [];\nfor i in tqdm(range(len(img_list))):\n    img_id = img_list[i].split(\".\")[0];\n    img_path = \"/kaggle/input/global-wheat-detection/test/\" + img_list[i];\n    scores, labels, boxes = gtf.Predict(img_path, class_list, vis_threshold=0.4);\n    #print(scores.shape, boxes.shape);\n    wr = \"\";\n    for j in range(scores.shape[0]):\n        score = float(scores[j].cpu().numpy())\n        if(score > 0.4):\n            x1 = int(boxes[j].cpu().numpy()[0]);\n            y1 = int(boxes[j].cpu().numpy()[1]);\n            x2 = int(boxes[j].cpu().numpy()[2]);\n            y2 = int(boxes[j].cpu().numpy()[3]);\n            w = x2-x1;\n            h = y2-y1;\n            wr += str(score) + \" \" + str(x1) + \" \" + str(y1) + \" \" + str(w) + \" \" + str(h) + \" \";\n        \n    wr = wr[:len(wr)-1];\n    combined.append([img_id, wr]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(combined, columns = ['image_id', 'PredictionString']);\ndf.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r wheat_dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r kaggle_monk_efficientdet","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}