{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import  scipy.misc as smi\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport os\nimport glob\nfrom PIL import Image\nimport cv2\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport sys\nimport keras\nimport keras.preprocessing.image\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(\"/kaggle/input/kerasresnet/keras-resnet\")\n!pip install .\nos.chdir(\"/kaggle/input/pythonutils/python-utils/\")\n!pip install .\nos.chdir(\"/kaggle/input/progressbar2/python-progressbar/\")\n!pip install .\nos.chdir(\"/kaggle/input/keras-retinanet/keras-retinanet\")\n!pip install .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mv \"/kaggle/input/keras-retinanet\" \"/kaggle/working/\"\nos.chdir(\"/kaggle/working/keras-retinanet/keras-retinanet\")\n!python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !git clone https://github.com/SriGanesh130/keras-retinanet.git\n# os.chdir(\"keras-retinanet\") \n# !python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !mv \"/kaggle/working/keras-retinanet\" \"/kaggle/input/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# retina_net_dir = \"/kaggle/working/keras-retinanet\"\n# !cd {retina_net_dir} && pip install .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras_retinanet.models import load_model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(\"/kaggle/working\")\nmodel = load_model(\"../input/weights101/resnet_infer101_csv_37.h5\", backbone_name=\"resnet101\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_dir = \"/kaggle/input/\"\noutput_dir = \"/kaggle/working/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"b_box = pd.read_csv(os.path.join(input_dir, \"global-wheat-detection/train.csv\"))\nimage_path = input_dir + \"global-wheat-detection/train/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"b_box[\"source\"].value_counts(normalize = True).plot(kind = \"bar\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"b_box[\"image_id\"].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_id = \"b6ab77fd7\"\nimage = cv2.imread(os.path.join(image_path, image_id + \".jpg\")) #read image\nprint(image.shape)\nimg_same_kind = b_box[b_box[\"image_id\"]==image_id] #read all test labels related to the image\nlst = img_same_kind[\"bbox\"].values[:] # get all the bounding boxes from the labels\ncontours = [img_index.strip('][').split(', ') for img_index in lst ] #casting bounding boxes from str to lst\nprint(len(contours))\nplt.figure()\nplt.imshow(image) \nplt.show() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n#plotting bounding boxes on image\ndef plot_boundbox(res, image):\n    x = int(float(res[0]))\n    y = int(float(res[1]))\n    w = int(float(res[2]))\n    h = int(float(res[3]))\n    bound_boxes = cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)\n    return bound_boxes\n\nimage_orig = image\nprint(len(contours))\nfor i, contour in enumerate(contours):\n    image_new = plot_boundbox(contour, image_orig)\n    image_orig = image_new\nplt.figure()\nplt.imshow(image_new) \nplt.show() ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Creating Dataset for RetinaNet\ndef create_dataset_csv(data, image_path):\n    annotated_data = []\n    for ind in range(data.shape[0]):\n        image_id = image_path + data[\"image_id\"][ind] + '.jpg'\n        b_box = data[\"bbox\"][ind].strip('][').split(', ')\n        class_name = \"wheat\"\n        annotated_data.append([image_id, b_box[0], b_box[1], str(float(b_box[0]) + float(b_box[2])), \n                               str(float(b_box[1]) + float(b_box[3])), class_name])\n    df = pd.DataFrame(annotated_data)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_csv = create_dataset_csv(b_box, \"./images/data/train/\")\n# class_map = pd.DataFrame([[\"wheat\", 0]])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# train_csv.to_csv(output_dir + \"train_data.csv\")\n# class_map.to_csv(output_dir + \"class_map.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras_retinanet import models\nfrom keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image\nfrom keras_retinanet.utils.visualization import draw_box, draw_caption\nfrom keras_retinanet.utils.colors import label_color\nfrom keras_retinanet.utils.gpu import setup_gpu","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels_to_names = {0:'wheat'}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def format_prediction_string(boxes, scores):\n    pred_strings = []\n    for j in zip(scores, boxes):\n        pred_strings.append(\"{0:.4f} {1} {2} {3} {4}\".format(j[0], int(j[1][0]), int(j[1][1]), \n                                                             int(j[1][2]), int(j[1][3])))\n\n    return \" \".join(pred_strings)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# image = read_image_bgr('./test/' + test_image)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_dir = \"../input/global-wheat-detection/test/\"\ntest_images = os.listdir(test_dir)\nresults = []\nfor test_image in test_images:\n    image = read_image_bgr(os.path.join(test_dir, test_image))\n    draw = image.copy()\n    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n    image = preprocess_image(image)\n    image, scale = resize_image(image)\n#     import pdb\n#     pdb.set_trace()\n    boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))\n    boxes /= scale\n    boxes = np.squeeze(boxes)\n    scores = np.squeeze(scores)\n    boxes_f = boxes[scores > 0.4]\n    scores_f = scores[scores >0.4]\n    boxes_f[:, 2] = boxes_f[:, 2] - boxes_f[:, 0]\n    boxes_f[:, 3] = boxes_f[:, 3] - boxes_f[:, 1]\n    result = {\"image_id\" : test_image.split('.')[0], \n            \"PredictionString\": format_prediction_string(boxes_f, scores_f)}\n    results.append(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])\ntest_df.to_csv('submission.csv', index=False)\nprint(test_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d = pd.read_csv(\"./submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d.head(5)","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}