{"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"### Pre-trained models\nUsing the kernel to get started on how to use pre-trained models\n1. https://www.kaggle.com/xhlulu/intro-to-tf-hub-for-object-detection"},{"metadata":{"trusted":true},"cell_type":"code","source":"# !git clone https://github.com/tensorflow/models.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !pip install protobuf-compiler \n# !pip install python-pil \n# !pip install python-lxml \n# !pip install python-tk\n# !pip install --user Cython\n# !pip install --user contextlib2\n# # !pip install --user jupyter\n# !pip install --user matplotlib","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !export PYTHONPATH=\"${PYTHONPATH}:/kaggle/working/models/\"\n# !export PYTHONPATH=\"${PYTHONPATH}:/kaggle/working/models/research\"\n# !export PYTHONPATH=\"${PYTHONPATH}:/kaggle/working/models/research/slim/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# %cd models/research/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip\n# !unzip protobuf.zip","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# From tensorflow/models/research/\n# !./bin/protoc object_detection/protos/*.proto --python_out=.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport os\nimport six.moves.urllib as urllib\nimport sys\nimport tarfile\nimport tensorflow as tf\nimport zipfile\n\nfrom distutils.version import StrictVersion\nfrom collections import defaultdict\nfrom io import StringIO\nfrom matplotlib import pyplot as plt\nfrom PIL import Image\n\nimport glob\n\n# This is needed since the notebook is stored in the object_detection folder.\nsys.path.append(\"..\")\nfrom object_detection.utils import ops as utils_ops\n\nif StrictVersion(tf.__version__) < StrictVersion('1.12.0'):\n    raise ImportError('Please upgrade your TensorFlow installation to v1.12.*.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# This is needed to display the images.\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from object_detection.utils import label_map_util\n\nfrom object_detection.utils import visualization_utils as vis_util","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# What model to download.\nMODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'\nMODEL_FILE = MODEL_NAME + '.tar.gz'\nDOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'\n\n# Path to frozen detection graph. This is the actual model that is used for the object detection.\nPATH_TO_FROZEN_GRAPH = MODEL_NAME + '/frozen_inference_graph.pb'\n\n# List of the strings that is used to add correct label for each box.\nPATH_TO_LABELS = os.path.join('./object_detection/data', 'mscoco_label_map.pbtxt')\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"opener = urllib.request.URLopener()\nopener.retrieve(DOWNLOAD_BASE + MODEL_FILE, MODEL_FILE)\ntar_file = tarfile.open(MODEL_FILE)\nfor file in tar_file.getmembers():\n    file_name = os.path.basename(file.name)\n    if 'frozen_inference_graph.pb' in file_name:\n        tar_file.extract(file, os.getcwd())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"detection_graph = tf.Graph()\nwith detection_graph.as_default():\n    od_graph_def = tf.GraphDef()\n    with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:\n        serialized_graph = fid.read()\n        od_graph_def.ParseFromString(serialized_graph)\n        tf.import_graph_def(od_graph_def, name='')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def load_image_into_numpy_array(image):\n    (im_width, im_height) = image.size\n    return np.array(image.getdata()).reshape(\n              (im_height, im_width, 3)).astype(np.uint8)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# PATH_TO_TEST_IMAGES_DIR = '../../../input/test/'\nTEST_IMAGE_PATHS = glob.glob('../../../input/test/*')[20:21] #[ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image{}.jpg'.format(i)) for i in range(1, 3) ]\n\n# Size, in inches, of the output images.\nIMAGE_SIZE = (12, 8)\nTEST_IMAGE_PATHS","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def run_inference_for_single_image(image, graph):\n  with graph.as_default():\n    with tf.Session() as sess:\n      # Get handles to input and output tensors\n      ops = tf.get_default_graph().get_operations()\n      all_tensor_names = {output.name for op in ops for output in op.outputs}\n      tensor_dict = {}\n      for key in [\n          'num_detections', 'detection_boxes', 'detection_scores',\n          'detection_classes', 'detection_masks'\n      ]:\n        tensor_name = key + ':0'\n        if tensor_name in all_tensor_names:\n          tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(\n              tensor_name)\n      if 'detection_masks' in tensor_dict:\n        # The following processing is only for single image\n        detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])\n        detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])\n        # Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.\n        real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)\n        detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])\n        detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])\n        detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(\n            detection_masks, detection_boxes, image.shape[0], image.shape[1])\n        detection_masks_reframed = tf.cast(\n            tf.greater(detection_masks_reframed, 0.5), tf.uint8)\n        # Follow the convention by adding back the batch dimension\n        tensor_dict['detection_masks'] = tf.expand_dims(\n            detection_masks_reframed, 0)\n      image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')\n\n      # Run inference\n      output_dict = sess.run(tensor_dict,\n                             feed_dict={image_tensor: np.expand_dims(image, 0)})\n\n      # all outputs are float32 numpy arrays, so convert types as appropriate\n      output_dict['num_detections'] = int(output_dict['num_detections'][0])\n      output_dict['detection_classes'] = output_dict[\n          'detection_classes'][0].astype(np.uint8)\n      output_dict['detection_boxes'] = output_dict['detection_boxes'][0]\n      output_dict['detection_scores'] = output_dict['detection_scores'][0]\n      if 'detection_masks' in output_dict:\n        output_dict['detection_masks'] = output_dict['detection_masks'][0]\n  return output_dict","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for image_path in TEST_IMAGE_PATHS:\n    image = Image.open(image_path)\n    # the array based representation of the image will be used later in order to prepare the\n    # result image with boxes and labels on it.\n    image_np = load_image_into_numpy_array(image)\n    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]\n    image_np_expanded = np.expand_dims(image_np, axis=0)\n    # Actual detection.\n    output_dict = run_inference_for_single_image(image_np, detection_graph)\n    # Visualization of the results of a detection.\n    vis_util.visualize_boxes_and_labels_on_image_array(\n          image_np,\n          output_dict['detection_boxes'],\n          output_dict['detection_classes'],\n          output_dict['detection_scores'],\n          category_index,\n          instance_masks=output_dict.get('detection_masks'),\n          use_normalized_coordinates=True,\n          line_thickness=2, \n          min_score_thresh=0.2)\n    plt.figure(figsize=IMAGE_SIZE)\n    plt.imshow(image_np)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### inferring detection in crappy way"},{"metadata":{"trusted":true},"cell_type":"code","source":"def format_prediction_string(image_id, result):\n    prediction_strings = []\n    \n    for i in range(2):#range(len(result['detection_scores'])):\n#         category_index[result['detection_classes'][i]]['name']\n        class_name = category_index[result['detection_classes'][i]]['name']#.decode(\"utf-8\")\n        boxes = result['detection_boxes'][i]\n        score = result['detection_scores'][i]\n        \n        prediction_strings.append(\n            f\"{class_name} {score} \" + \" \".join(map(str, boxes))\n        )\n        \n    prediction_string = \" \".join(prediction_strings)\n\n    return {\n        \"ImageID\": image_id,\n        \"PredictionString\": prediction_string\n    }\n\n# dict_keys(['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tqdm import tqdm\n\nsample_submission_df = pd.read_csv('../../../input/sample_submission.csv')\nimage_ids = sample_submission_df['ImageId'][1:2]\npredictions = []\n\nfor image_id in tqdm(image_ids):\n    # Load the image string\n    image_path = f'../../../input/test/{image_id}.jpg'\n    image = Image.open(image_path)\n    # result image with boxes and labels on it.\n    image_np = load_image_into_numpy_array(image)\n    # Expand dimensions since the model expects images to have shape: [1, None, None, 3]\n    image_np_expanded = np.expand_dims(image_np, axis=0)\n    # Actual detection.\n    output_dict = run_inference_for_single_image(image_np, detection_graph)\n    predictions.append(format_prediction_string(image_id, output_dict))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_df = pd.DataFrame(predictions)\npred_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_df.to_csv('../../../submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# !kg competitions submit -c 'open-images-2019-object-detection' -f submission.csv -m \"Test Submission\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}