{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":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\nfor 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":"sub=pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\ntrain_df=pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.head","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm\nimport ast","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# How many unique images?\nlen(train_df[\"image_id\"].unique())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Separating out the coordinates\nxmin, ymin, width, height = [], [], [], []\n\nfor i in tqdm(train_df[\"bbox\"]):\n    cooridinates_list = ast.literal_eval(i)\n    xmin.append(cooridinates_list[0])\n    ymin.append(cooridinates_list[1])\n    width.append(cooridinates_list[2])\n    height.append(cooridinates_list[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"xmin\"] = xmin\ntrain_df[\"ymin\"] = ymin\ntrain_df[\"width\"] = width\ntrain_df[\"height\"] = height\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualizing some samples from the training set\n\nsample_indices = np.random.choice(np.unique(train_df[\"image_id\"].tolist()), 8)\n\nfig, ax = plt.subplots(nrows=2, ncols=4, figsize=(20, 10))\ncount=0\n\nfor row in ax:\n    for col in row:\n        img = plt.imread(\"/kaggle/input/global-wheat-detection/train/\" + sample_indices[count] + \".jpg\")\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        col.imshow(img)\n        count += 1\nplt.show()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.patches as patches\n\ndef get_bbox(image_id, df, col, color='white'):\n    bboxes = df[df['image_id'] == image_id]\n    \n    for i in range(len(bboxes)):\n        # Create a Rectangle patch\n        rect = patches.Rectangle(\n            (bboxes['xmin'].iloc[i], bboxes['ymin'].iloc[i]),\n            bboxes['width'].iloc[i], \n            bboxes['height'].iloc[i], \n            linewidth=2, \n            edgecolor=color, \n            facecolor='none')\n\n        # Add the patch to the Axes\n        col.add_patch(rect)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 10))\ncount=0\nfor row in ax:\n    for col in row:\n        img = plt.imread(\"/kaggle/input/global-wheat-detection/train/\" + sample_indices[count] + \".jpg\")\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        get_bbox(sample_indices[count], train_df, col, color='red')\n        col.imshow(img)\n        count += 1\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Images without bounding box\nimages_w_bbox = train_df[\"image_id\"].unique()\nimages_w_bbox = [\"/kaggle/input/global-wheat-detection/train/\" + image_id + \".jpg\" for image_id in images_w_bbox]\n\nall_images = list(paths.list_images(\"/kaggle/input/global-wheat-detection/train/\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_w_bbox[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"all_images[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Visualizing some images without any wheat heads\n\n'''fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(20, 10))\ncount=0\n\nfor row in ax:\n    for col in row:\n        img = plt.imread(images_wo_bbox[count])\n        col.grid(False)\n        col.set_xticks([])\n        col.set_yticks([])\n        col.imshow(img)\n        count += 1\nplt.show()'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images_wo_bbox = list(set(all_images) - set(images_w_bbox))\nimages_wo_bbox[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## LOADING THE LIBRARY","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.patches as patches\nimport matplotlib.pyplot as plt\nfrom imutils import paths\nimport pandas as pd\nimport numpy as np \nimport cv2\nimport os","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pip install imutils","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def showbbox(image_path, xy, width, height):\n    # Create figure and axes\n    fig,ax = plt.subplots(1)\n\n    # Display the image\n    ax.imshow(plt.imread(image_path))\n\n    # Create a Rectangle patch\n    rect = patches.Rectangle(xy, width, height, \n        linewidth=2, edgecolor='r', facecolor='none')\n\n    # Add the patch to the Axes\n    ax.add_patch(rect)\n\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"showbbox('/kaggle/input/global-wheat-detection/train/b6ab77fd7.jpg', (226.0, 548.0), 1024, 1024)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p=[]\nfor i in (train_df['bbox']):\n    d=i.split(',')\n    p.append(d)\n               ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"d=[]\nfor i in range(0,len(p)):\n    s=p[i][0].strip('[')\n    #s.strip('[')\n    d.append(s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dd=[]\nfor i in range(0,len(p)):\n    s1=p[i][1].strip('[')\n    #s.strip('[')\n    dd.append(s1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train['xmin']=pd.Series(d)\n#train['ymin']=pd.Series(dd)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#train['xmin']=train['xmin'].astype('float')\n#train['ymin']=train['ymin'].astype('float')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"xmax\"] = train_df[\"xmin\"] + train_df[\"width\"]\ntrain_df[\"ymax\"] = train_df[\"ymin\"] + train_df[\"height\"]\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Rename the image_id column to filename & add full paths\ntrain_df.rename(columns={\n        \"image_id\":\"filename\"\n    }, inplace=True)\n\nimages_w_bbox = train_df[\"filename\"]\nimages_w_bbox = [\"/kaggle/input/global-wheat-detection/train/\" + image_id + \".jpg\" for image_id in images_w_bbox]\ntrain_df[\"filename\"] = images_w_bbox\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Drop the unnecessary columns, we will return to this step in a moment\ntrain_df.drop([\"source\", \"bbox\"], axis=1, inplace=True)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import math\nmath.floor(2.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Add a class column \ntrain_df[\"class\"] = \"wheat_head\"\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Prepare the splits\nfrom sklearn.model_selection import train_test_split\n\ntrain1, valid = train_test_split(train_df, test_size=0.15, random_state=666)\nprint(\"Training samples:\", train1.shape[0])\nprint(\"Validation samples:\", valid.shape[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain1 = train1.reset_index(drop=True)\ntrain1.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nvalid = valid.reset_index(drop=True)\nvalid.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Serialize the dataframes\ntrain1.to_csv(\"new_train_df.csv\")\nvalid.to_csv(\"valid_df.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# Preparing the label maps\nLABEL_ENCODINGS = {\n    \"wheat_head\": 1\n}\n\nf = open(\"label_map.pbtxt\", \"w\")\n\nfor (k, v) in LABEL_ENCODINGS.items():\n    # construct the class information and write to file\n    item = (\"item {\\n\"\n            \"\\tid: \" + str(v) + \"\\n\"\n            \"\\tname: '\" + k + \"'\\n\"\n            \"}\\n\")\n    f.write(item)\n\n# close the output classes file\nf.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cat label_map.pbtxt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''from __future__ import division\nfrom __future__ import print_function\nfrom __future__ import absolute_import\n\nimport os\nimport io\nimport pandas as pd\nimport tensorflow as tf\n\nfrom PIL import Image\n#from object_detection.utils import dataset_util\nfrom collections import namedtuple, OrderedDict\n\nflags = flags\nflags.DEFINE_string('csv_input', '', 'Path to the CSV input')\nflags.DEFINE_string('output_path', '', 'Path to output TFRecord')\nFLAGS = flags.FLAGS\n\nLABEL_ENCODINGS = {\n    \"wheat_head\": 1\n}\n\n\ndef split(df, group):\n    data = namedtuple('data', ['filename', 'object'])\n    gb = df.groupby(group)\n    return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]\n\n\ndef create_tf_example(group, path):\n    with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:\n        encoded_jpg = fid.read()\n    encoded_jpg_io = io.BytesIO(encoded_jpg)\n    image = Image.open(encoded_jpg_io)\n    width, height = image.size\n\n    filename = group.filename.encode('utf8')\n    image_format = b'jpg'\n    xmins = []\n    xmaxs = []\n    ymins = []\n    ymaxs = []\n    classes_text = []\n    classes = []\n\n    for index, row in group.object.iterrows():\n        xmins.append(row['xmin'] / width)\n        xmaxs.append(row['xmax'] / width)\n        ymins.append(row['ymin'] / height)\n        ymaxs.append(row['ymax'] / height)\n        classes_text.append(row['class'].encode('utf8'))\n        classes.append(LABEL_ENCODINGS[row['class']])\n\n    tf_example = tf.train.Example(features=tf.train.Features(feature={\n        'image/height': dataset_util.int64_feature(height),\n        'image/width': dataset_util.int64_feature(width),\n        'image/filename': dataset_util.bytes_feature(filename),\n        'image/source_id': dataset_util.bytes_feature(filename),\n        'image/encoded': dataset_util.bytes_feature(encoded_jpg),\n        'image/format': dataset_util.bytes_feature(image_format),\n        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),\n        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),\n        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),\n        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),\n        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),\n        'image/object/class/label': dataset_util.int64_list_feature(classes),\n    }))\n    return tf_example\n\n\ndef main(_):\n    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)\n    path = os.path.join(os.getcwd())\n    examples = pd.read_csv(FLAGS.csv_input)\n    grouped = split(examples, 'filename')\n    for group in grouped:\n        tf_example = create_tf_example(group, path)\n        writer.write(tf_example.SerializeToString())\n\n    writer.close()\n    output_path = os.path.join(os.getcwd(), FLAGS.output_path)\n    print('Successfully created the TFRecords: {}'.format(output_path))\n\n\nif __name__ == '__main__':\n    tf.app.run()'''","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}