{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"# some basic understanding of the data and pre-processing for later functions consumption\nimport os\nprint(os.listdir(\"../input\"))","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:02:47.626752Z","iopub.execute_input":"2021-10-01T09:02:47.627260Z","iopub.status.idle":"2021-10-01T09:02:47.640110Z","shell.execute_reply.started":"2021-10-01T09:02:47.627197Z","shell.execute_reply":"2021-10-01T09:02:47.638256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Input Data","metadata":{}},{"cell_type":"code","source":"\ndef get_image_file_path(image_file_name):\n    \"\"\"returns the path of image file\"\"\"\n    return '../input/test/' + image_file_name\n\ndef get_images(n):\n    \"\"\"reads all the files from `../input/test` directory and returns paths for n files from top\"\"\"\n    all_image_files = os.listdir(\"../input/test/\")\n    # let's save all these image paths for later\n    image_paths = list(map(get_image_file_path, all_image_files))\n    # rather than using all, we will use a subset of these image paths for working on our model\n    image_paths = image_paths[:n]\n    return image_paths\n\ndef get_image_id_from_path(image_path):\n    \"\"\"returns image id from image path\"\"\"\n    return image_path.split('../input/test/')[1].split('.jpg')[0]","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-01T09:03:23.661001Z","iopub.execute_input":"2021-10-01T09:03:23.661318Z","iopub.status.idle":"2021-10-01T09:03:23.668616Z","shell.execute_reply.started":"2021-10-01T09:03:23.661257Z","shell.execute_reply":"2021-10-01T09:03:23.667560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Example","metadata":{}},{"cell_type":"code","source":"print(get_images(10))\nprint(get_image_id_from_path(get_images(2)[0]))","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:03:28.011245Z","iopub.execute_input":"2021-10-01T09:03:28.011522Z","iopub.status.idle":"2021-10-01T09:03:29.759800Z","shell.execute_reply.started":"2021-10-01T09:03:28.011468Z","shell.execute_reply":"2021-10-01T09:03:29.758744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# OUTPUT","metadata":{}},{"cell_type":"code","source":"def get_prediction_string(result):\n    with tf.device('/device:GPU:0'):\n        \"\"\"from each result, generates the complete prediction string in the format {Label Confidence XMin YMin XMax YMax},{...} based on submission file.\"\"\"\n        prediction_strings = []\n        for index, score in enumerate(result['detection_scores']):\n            index = int(index)\n            single_prediction_string = \"\"\n            single_prediction_string += result['detection_class_names'][index].decode(\"utf-8\") + \" \"  + str(score) + \" \"\n            single_prediction_string += \" \".join(str(x) for x in result['detection_boxes'][index])\n            prediction_strings.append(single_prediction_string)\n\n        prediction_string = \" \".join(str(x) for x in prediction_strings)\n        return prediction_string\n\ndef get_prediction_entry(filepath, result):\n    return {\n        \"ImageID\": get_image_id_from_path(filepath),\n        \"PredictionString\": get_prediction_string(result)\n    }","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:03:31.870390Z","iopub.execute_input":"2021-10-01T09:03:31.870671Z","iopub.status.idle":"2021-10-01T09:03:31.878335Z","shell.execute_reply.started":"2021-10-01T09:03:31.870615Z","shell.execute_reply":"2021-10-01T09:03:31.877436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Tenserflow","metadata":{}},{"cell_type":"code","source":"# For running inference on the TF-Hub module.\nimport tensorflow as tf\nimport tensorflow_hub as hub\n\n# For downloading the image.\nimport matplotlib.pyplot as plt\nfrom six import BytesIO\n\n# For drawing onto the image.\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom PIL import ImageColor\nfrom PIL import ImageDraw\nfrom PIL import ImageFont\nfrom PIL import ImageOps\n\n# For measuring the inference time.\nimport time\n\n# Check available GPU devices.\nprint(\"The following GPU devices are available: %s\" % tf.test.gpu_device_name())","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:03:35.754167Z","iopub.execute_input":"2021-10-01T09:03:35.754540Z","iopub.status.idle":"2021-10-01T09:03:38.425205Z","shell.execute_reply.started":"2021-10-01T09:03:35.754486Z","shell.execute_reply":"2021-10-01T09:03:38.424239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Display ","metadata":{}},{"cell_type":"code","source":"def display_image(image):\n    fig = plt.figure(figsize=(20, 15))\n    plt.grid(False)\n    plt.imshow(image)\n\n\ndef draw_bounding_box_on_image(image,ymin,xmin,ymax,\n                               xmax,color,font,thickness=4,display_str_list=()):\n    \"\"\"Adds a bounding box to an image.\"\"\"\n    draw = ImageDraw.Draw(image)\n    im_width, im_height = image.size\n    (left, right, top, bottom) = (xmin * im_width, xmax * im_width,\n                                  ymin * im_height, ymax * im_height)\n    draw.line([(left, top), (left, bottom), (right, bottom), (right, top),(left, top)],width=thickness,\n              fill=color)\n\n    display_str_heights = [font.getsize(ds)[1] for ds in display_str_list]\n    # Each display_str has a top and bottom margin of 0.05x.\n    total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights)\n\n    if top > total_display_str_height:\n        text_bottom = top\n    else:\n        text_bottom = bottom + total_display_str_height\n    # Reverse list and print from bottom to top.\n    for display_str in display_str_list[::-1]:\n        text_width, text_height = font.getsize(display_str)\n        margin = np.ceil(0.05 * text_height)\n        draw.rectangle([(left, text_bottom - text_height - 2 * margin),\n                        (left + text_width, text_bottom)],\n                       fill=color)\n        draw.text((left + margin, text_bottom - text_height - margin),\n                  display_str,\n                  fill=\"black\",\n                  font=font)\n        text_bottom -= text_height - 2 * margin\n\n\ndef draw_boxes(image, boxes, class_names, scores, max_boxes=10, min_score=0.1):\n    \"\"\"Overlay labeled boxes on an image with formatted scores and label names.\"\"\"\n    colors = list(ImageColor.colormap.values())\n\n    try:\n        font = ImageFont.truetype(\n            \"/usr/share/fonts/truetype/liberation/LiberationSansNarrow-Regular.ttf\",\n            25)\n    except IOError:\n        print(\"Font not found, using default font.\")\n        font = ImageFont.load_default()\n\n    for i in range(min(boxes.shape[0], max_boxes)):\n        if scores[i] >= min_score:\n            ymin, xmin, ymax, xmax = tuple(boxes[i].tolist())\n            display_str = \"{}: {}%\".format(class_names[i].decode(\"ascii\"),\n                                           int(100 * scores[i]))\n            color = colors[hash(class_names[i]) % len(colors)]\n            image_pil = Image.fromarray(np.uint8(image)).convert(\"RGB\")\n            draw_bounding_box_on_image(image_pil,\n                ymin,xmin,ymax,xmax,color,font,\n                display_str_list=[display_str])\n            np.copyto(image, np.array(image_pil))\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:03:45.244598Z","iopub.execute_input":"2021-10-01T09:03:45.244963Z","iopub.status.idle":"2021-10-01T09:03:45.263960Z","shell.execute_reply.started":"2021-10-01T09:03:45.244893Z","shell.execute_reply":"2021-10-01T09:03:45.262224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Output","metadata":{}},{"cell_type":"code","source":"module_handle = \"https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1\"\nimage_path = \"../input/test/15fe48f0b21740d2.jpg\"\n\nwith tf.device('/device:GPU:0'):\n    with tf.Graph().as_default():\n        detector = hub.Module(module_handle)\n        image_string_placeholder = tf.placeholder(tf.string)\n        decoded_image = tf.image.decode_jpeg(image_string_placeholder)\n        # Module accepts as input tensors of shape [1, height, width, 3], i.e. batch\n        # of size 1 and type tf.float32.\n        decoded_image_float = tf.image.convert_image_dtype(\n            image=decoded_image, dtype=tf.float32)\n        module_input = tf.expand_dims(decoded_image_float, 0)\n        result = detector(module_input, as_dict=True)\n        init_ops = [tf.global_variables_initializer(), tf.tables_initializer()]\n\n        session = tf.Session()\n        session.run(init_ops)\n\n        # Load the downloaded and resized image and feed into the graph.\n        with tf.gfile.Open(image_path, \"rb\") as binfile:\n            image_string = binfile.read()\n\n        result_out, image_out = session.run(\n            [result, decoded_image],\n            feed_dict={image_string_placeholder: image_string})\n        print(\"Found %d objects.\" % len(result_out[\"detection_scores\"]))","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:03:59.359611Z","iopub.execute_input":"2021-10-01T09:03:59.360254Z","iopub.status.idle":"2021-10-01T09:06:18.892618Z","shell.execute_reply.started":"2021-10-01T09:03:59.359870Z","shell.execute_reply":"2021-10-01T09:06:18.891700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\nbegin = time.time()\n# see the sample image with bounding boxes\nimage_with_boxes = draw_boxes(\n    np.array(image_out), result_out[\"detection_boxes\"],\n    result_out[\"detection_class_entities\"], result_out[\"detection_scores\"])\ndisplay_image(image_with_boxes)\n\nend = time.time()\nprint(end-begin)","metadata":{"execution":{"iopub.status.busy":"2021-10-01T09:07:30.516824Z","iopub.execute_input":"2021-10-01T09:07:30.517267Z","iopub.status.idle":"2021-10-01T09:07:32.131999Z","shell.execute_reply.started":"2021-10-01T09:07:30.517203Z","shell.execute_reply":"2021-10-01T09:07:32.130289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimage_paths = get_images(10)\npredictions = []\n\nwith tf.device('/device:GPU:0'):\n    for image_path in image_paths:\n        with tf.gfile.Open(image_path, \"rb\") as binfile:\n            image_string = binfile.read()\n\n        inference_start_time = time.clock()\n        result_out, image_out = session.run(\n            [result, decoded_image],\n            feed_dict={image_string_placeholder: image_string})\n        predictions.append(get_prediction_entry(image_path, result_out))\n        print(f'For {image_path} found objects in {time.clock() - inference_start_time} seconds')\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-10-01T09:07:45.177793Z","iopub.execute_input":"2021-10-01T09:07:45.178185Z","iopub.status.idle":"2021-10-01T09:08:01.553835Z","shell.execute_reply.started":"2021-10-01T09:07:45.178099Z","shell.execute_reply":"2021-10-01T09:08:01.552563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_df = pd.DataFrame(predictions)\npredictions_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv('../input/sample_submission.csv')\nsubmission_df.update(predictions_df)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# NOTE: this is only on a subset of data\nsubmission_df.to_csv('./10_values_submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}