Skip to content

Instantly share code, notes, and snippets.

@julifgo
Last active May 21, 2020 14:36
Show Gist options
  • Save julifgo/18516c9082499980b8d39cbb24dfe93d to your computer and use it in GitHub Desktop.
Save julifgo/18516c9082499980b8d39cbb24dfe93d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:33:18.751854Z",
"start_time": "2020-05-21T14:33:18.744875Z"
}
},
"outputs": [],
"source": [
"#COMPILE PROTOBUF. DOWNLOADED FROM https://github.com/protocolbuffers/protobuf/releases\n",
"#This version is 3.4\n",
"%%cmd\n",
"\n",
"cd models/research\n",
"C:/Users/jgutierrez/Desktop/HAT/objectdetection/bin/protoc object_detection/protos/*.proto --python_out=."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T13:19:41.778728Z",
"start_time": "2020-05-21T13:19:38.193686Z"
}
},
"outputs": [],
"source": [
"#COMPILE TENSOR OD PROJECT\n",
"%%cmd \n",
"cd models\n",
"pip install ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-20T18:33:34.967806Z",
"start_time": "2020-05-20T18:33:32.090522Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import os\n",
"import six.moves.urllib as urllib\n",
"import sys\n",
"import tarfile\n",
"import tensorflow as tf\n",
"import zipfile\n",
"import re\n",
"from collections import defaultdict\n",
"from io import StringIO\n",
"from matplotlib import pyplot as plt\n",
"from PIL import Image\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-20T18:33:38.489676Z",
"start_time": "2020-05-20T18:33:38.460564Z"
}
},
"outputs": [],
"source": [
"from object_detection.utils import ops as utils_ops\n",
"from object_detection.utils import label_map_util\n",
"from object_detection.utils import visualization_utils as vis_util\n",
"from object_detection.utils import dataset_util"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-20T18:33:39.112689Z",
"start_time": "2020-05-20T18:33:39.108698Z"
}
},
"outputs": [],
"source": [
"# patch tf1 into `utils.ops`\n",
"utils_ops.tf = tf.compat.v1\n",
"\n",
"# Patch the location of gfile\n",
"tf.gfile = tf.io.gfile"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:32:39.159094Z",
"start_time": "2020-05-21T14:32:38.728725Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'pandas.core.frame.DataFrame'>\n",
"RangeIndex: 147793 entries, 0 to 147792\n",
"Data columns (total 5 columns):\n",
" # Column Non-Null Count Dtype \n",
"--- ------ -------------- ----- \n",
" 0 image_id 147793 non-null object\n",
" 1 width 147793 non-null int64 \n",
" 2 height 147793 non-null int64 \n",
" 3 bbox 147793 non-null object\n",
" 4 source 147793 non-null object\n",
"dtypes: int64(2), object(3)\n",
"memory usage: 5.6+ MB\n"
]
},
{
"data": {
"text/plain": [
"(None, (147793, 5))"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# DIR = \"../input/global-wheat-detection/\"\n",
"DIR ='global-wheat-detection/'\n",
"TRAIN = \"train.csv\"\n",
"\n",
"TRAIN_IMG = \"train\"\n",
"TEST_IMG= \"test\"\n",
"WIDTH = 1024\n",
"HEIGHT = 1024\n",
"\n",
"train_df = pd.read_csv(os.path.join(DIR, TRAIN))\n",
"train_df.info(),train_df.shape\n"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:32:42.407493Z",
"start_time": "2020-05-21T14:32:42.274335Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>width</th>\n",
" <th>height</th>\n",
" <th>bbox</th>\n",
" <th>source</th>\n",
" <th>filename</th>\n",
" <th>class</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>[834.0, 222.0, 56.0, 36.0]</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>[226.0, 548.0, 130.0, 58.0]</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>[377.0, 504.0, 74.0, 160.0]</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>[834.0, 95.0, 109.0, 107.0]</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>[26.0, 144.0, 124.0, 117.0]</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" width height bbox source filename class\n",
"0 1024 1024 [834.0, 222.0, 56.0, 36.0] usask_1 b6ab77fd7.jpg wheat\n",
"1 1024 1024 [226.0, 548.0, 130.0, 58.0] usask_1 b6ab77fd7.jpg wheat\n",
"2 1024 1024 [377.0, 504.0, 74.0, 160.0] usask_1 b6ab77fd7.jpg wheat\n",
"3 1024 1024 [834.0, 95.0, 109.0, 107.0] usask_1 b6ab77fd7.jpg wheat\n",
"4 1024 1024 [26.0, 144.0, 124.0, 117.0] usask_1 b6ab77fd7.jpg wheat"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#For tf record, we put filename column, and add target class name. Single class here.\n",
"train_df['filename'] = train_df['image_id']+'.jpg'\n",
"train_df['class'] = 'wheat'\n",
"train_df = train_df.drop(['image_id'], axis=1) \n",
"train_df['width'] = WIDTH\n",
"train_df['height'] = HEIGHT\n",
"train_df.head(5)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:35:00.566586Z",
"start_time": "2020-05-21T14:35:00.542644Z"
}
},
"outputs": [],
"source": [
"#Expand bbox in all it's points and add width height columns.\n",
"#dataset bbox has xmin, ymin, width and height\n",
"train_df['xmin'] = -1\n",
"train_df['ymin'] = -1\n",
"train_df['xmax'] = -1\n",
"train_df['ymax'] = -1\n",
"train_df['w'] = -1\n",
"train_df['h'] = -1\n",
"\n",
"def expand_bbox(x):\n",
" r = np.array(re.findall(\"([0-9]+[.]?[0-9]*)\", x))\n",
" if len(r) == 0:\n",
" r = [-1, -1, -1, -1]\n",
" return r"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:35:06.124849Z",
"start_time": "2020-05-21T14:35:02.468462Z"
}
},
"outputs": [],
"source": [
"train_df[['xmin', 'ymin', 'w', 'h']] = np.stack(train_df['bbox'].apply(lambda x: expand_bbox(x)))\n",
"train_df.drop(columns=['bbox'], inplace=True)\n",
"train_df['xmin'] = train_df['xmin'].astype(np.float)\n",
"train_df['ymin'] = train_df['ymin'].astype(np.float)\n",
"train_df['xmax'] = train_df['xmax'].astype(np.float)\n",
"train_df['ymax'] = train_df['ymax'].astype(np.float)\n",
"train_df['w'] = train_df['w'].astype(np.float)\n",
"train_df['h'] = train_df['h'].astype(np.float)\n",
"train_df['xmax'] = train_df['xmin'] + train_df['w']\n",
"train_df['ymax'] = train_df['ymin'] + train_df['h']"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:35:07.463529Z",
"start_time": "2020-05-21T14:35:07.439593Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>width</th>\n",
" <th>height</th>\n",
" <th>source</th>\n",
" <th>filename</th>\n",
" <th>class</th>\n",
" <th>xmin</th>\n",
" <th>ymin</th>\n",
" <th>xmax</th>\n",
" <th>ymax</th>\n",
" <th>w</th>\n",
" <th>h</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" <td>834.0</td>\n",
" <td>222.0</td>\n",
" <td>890.0</td>\n",
" <td>258.0</td>\n",
" <td>56.0</td>\n",
" <td>36.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" <td>226.0</td>\n",
" <td>548.0</td>\n",
" <td>356.0</td>\n",
" <td>606.0</td>\n",
" <td>130.0</td>\n",
" <td>58.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" <td>377.0</td>\n",
" <td>504.0</td>\n",
" <td>451.0</td>\n",
" <td>664.0</td>\n",
" <td>74.0</td>\n",
" <td>160.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" <td>834.0</td>\n",
" <td>95.0</td>\n",
" <td>943.0</td>\n",
" <td>202.0</td>\n",
" <td>109.0</td>\n",
" <td>107.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1024</td>\n",
" <td>1024</td>\n",
" <td>usask_1</td>\n",
" <td>b6ab77fd7.jpg</td>\n",
" <td>wheat</td>\n",
" <td>26.0</td>\n",
" <td>144.0</td>\n",
" <td>150.0</td>\n",
" <td>261.0</td>\n",
" <td>124.0</td>\n",
" <td>117.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" width height source filename class xmin ymin xmax ymax \\\n",
"0 1024 1024 usask_1 b6ab77fd7.jpg wheat 834.0 222.0 890.0 258.0 \n",
"1 1024 1024 usask_1 b6ab77fd7.jpg wheat 226.0 548.0 356.0 606.0 \n",
"2 1024 1024 usask_1 b6ab77fd7.jpg wheat 377.0 504.0 451.0 664.0 \n",
"3 1024 1024 usask_1 b6ab77fd7.jpg wheat 834.0 95.0 943.0 202.0 \n",
"4 1024 1024 usask_1 b6ab77fd7.jpg wheat 26.0 144.0 150.0 261.0 \n",
"\n",
" w h \n",
"0 56.0 36.0 \n",
"1 130.0 58.0 \n",
"2 74.0 160.0 \n",
"3 109.0 107.0 \n",
"4 124.0 117.0 "
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train_df.head(5)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T00:45:02.669624Z",
"start_time": "2020-05-21T00:45:02.232791Z"
}
},
"outputs": [],
"source": [
"#Create a cross validation dataset since kaggle didn't provide it.\n",
"#Take random images names and filter all rows, then move to test dataset. This code could be better.\n",
"original_size = train_df.shape[0]\n",
"test_labels_df = pd.DataFrame()\n",
"filenames=['1c90d23fb.jpg','1d74edc1e.jpg','3d1c7f78a.jpg','3cf295f6c.jpg','5a0e2133c.jpg','7aa682e0f.jpg','9a50eab86.jpg',\n",
" '9a30dd802.jpg','9aa0487b0.jpg','24dda66d6.jpg','57ae1baf6.jpg']\n",
"for filename in filenames:\n",
" filter = train_df['filename'] == filename\n",
" test_labels_df = test_labels_df.append(train_df[filter], ignore_index=True)\n",
" train_df.drop(train_df[filter].index, inplace=True)\n",
"assert original_size == train_df.shape[0]+test_labels_df.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:32:14.433214Z",
"start_time": "2020-05-21T14:32:14.427231Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(147343, 450)"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#THIS VALUES ARE IMPORTANT FOR MODEL CONFIG\n",
"train_df.shape[0],test_labels_df.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T00:45:31.203391Z",
"start_time": "2020-05-21T00:45:29.592651Z"
}
},
"outputs": [],
"source": [
"#Save new datasets\n",
"train_df.to_csv(os.path.join(DIR, 'train_labels.csv'),header=True)\n",
"test_labels_df.to_csv(os.path.join(DIR, 'test_labels.csv'),header=True)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:13:19.605748Z",
"start_time": "2020-05-21T14:13:19.580814Z"
}
},
"outputs": [],
"source": [
"# TO-DO replace this with label map\n",
"def class_text_to_int(row_label):\n",
" if row_label == 'wheat':\n",
" return 1\n",
" else:\n",
" None\n",
"\n",
"\n",
"def 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",
"\n",
"def create_tf_record(group, path):\n",
" with tf.io.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(class_text_to_int(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"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-21T14:13:14.843888Z",
"start_time": "2020-05-21T14:13:14.837904Z"
}
},
"outputs": [],
"source": [
"def writeTfRecord(df,filename):\n",
" writer = tf.compat.v1.python_io.TFRecordWriter(os.path.join(DIR, filename))\n",
" grouped = split(df, 'filename')\n",
" for group in grouped:\n",
" tf_example = create_tf_record(group, 'global-wheat-detection/train')\n",
" writer.write(tf_example.SerializeToString())\n",
" writer.close()\n",
" print(\"Done.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"end_time": "2020-05-15T14:17:17.650723Z",
"start_time": "2020-05-15T14:17:17.647732Z"
}
},
"outputs": [],
"source": [
"writeTfRecord(train_df,'train.record')\n",
"writeTfRecord(test_labels_df,'test.record')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download a model to train from here.\n",
"https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md\n",
"Create label file class .pbtxt with all target classes\n",
"Move model config to a new training folder among with .pbtxt file and both tf records\n",
"Move downloaded model folder to models\\research\\object_detection among with training folder\n",
"Edit config file. Basically paths to training records, classes numbers, path to label class, num_steps for training, batch size and num_examples in test \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run Model\n",
"python model_main.py --logtostderr --model_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_pets.config\n",
"Should train.\n",
"\n",
"Visualize training tensorboard --logdir=training/"
]
}
],
"metadata": {
"gist": {
"data": {
"description": "tensor/models/research/object_detection/wheat.ipynb",
"public": true
},
"id": ""
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment