{"cells":[{"metadata":{"_uuid":"7463e0e93eda3dcf4ddb36d0ea12bb600cd34b49"},"cell_type":"markdown","source":"# Object Detection using YOLOV2\n\n<br>\n\nThis is a starter kernel, mainly for learning purposes and getting started. There is so much to learn more and improve. I am using YOLOv2 model for object classification and detection using a pretrained model.\nI could not consolidate all the functions into a dataset and import in the Notebook so i had to put all the code here! Sorry about that. I am still new to Kaggle! Goal is to get an idea on how YOLO algorithm works in detail step by step. Hope this helps :-)\n\nReferences: The ideas presented in this notebook came primarily from the two YOLO papers. The implementation here also took significant inspiration and used many components from Allan Zelener's github repository. The pretrained weights used in this exercise came from the official YOLO website.\n\nCheck the speed of detection using YOLO!\n\nJoseph Redmon, Santosh Divvala, Ross Girshick, Ali Farhadi - You Only Look Once: Unified, Real-Time Object Detection (2015)\nJoseph Redmon, Ali Farhadi - YOLO9000: Better, Faster, Stronger (2016)\nAllan Zelener - YAD2K: Yet Another Darknet 2 Keras\nThe official YOLO website (https://pjreddie.com/darknet/yolo/)\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"## Import the required libraries \n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.inception_v3 import preprocess_input\nfrom keras.utils.data_utils import GeneratorEnqueuer\nimport matplotlib.pyplot as plt\nimport pandas as pd \nimport numpy as np \nimport math, os\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"845b7c2aef5f0a4b746ccddbe54069d826694492","collapsed":true},"cell_type":"code","source":"import colorsys\nimport imghdr\nimport random\nfrom keras import backend as K\nfrom PIL import Image, ImageDraw, ImageFont\nimport argparse\nimport matplotlib.pyplot as plt\nfrom matplotlib.pyplot import imshow\nimport scipy.io\nimport scipy.misc\nimport numpy as np\nimport pandas as pd\nimport PIL\nimport tensorflow as tf\nfrom keras import backend as K\nfrom keras.layers import Input, Lambda, Conv2D\nfrom keras.models import load_model, Model\n#from yolo_utils.yolo_utils import read_classes, read_anchors, generate_colors, preprocess_image, draw_boxes, scale_boxes\n#from yad2k.models.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body\n## Import the required libraries \n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.inception_v3 import preprocess_input\nfrom keras.utils.data_utils import GeneratorEnqueuer\nimport matplotlib.pyplot as plt\nimport math, os\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"b65003d50ab0d6d5a5365027c8b300ece9c1992e"},"cell_type":"code","source":"def read_classes(classes_path):\n    with open(classes_path) as f:\n        class_names = f.readlines()\n    class_names = [c.strip() for c in class_names]\n    return class_names\n\ndef read_anchors(anchors_path):\n    with open(anchors_path) as f:\n        anchors = f.readline()\n        anchors = [float(x) for x in anchors.split(',')]\n        anchors = np.array(anchors).reshape(-1, 2)\n    return anchors\n\ndef generate_colors(class_names):\n    hsv_tuples = [(x / len(class_names), 1., 1.) for x in range(len(class_names))]\n    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))\n    colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)), colors))\n    random.seed(10101)  # Fixed seed for consistent colors across runs.\n    random.shuffle(colors)  # Shuffle colors to decorrelate adjacent classes.\n    random.seed(None)  # Reset seed to default.\n    return colors\n\ndef scale_boxes(boxes, image_shape):\n    \"\"\" Scales the predicted boxes in order to be drawable on the image\"\"\"\n    height = image_shape[0]\n    width = image_shape[1]\n    image_dims = K.stack([height, width, height, width])\n    image_dims = K.reshape(image_dims, [1, 4])\n    boxes = boxes * image_dims\n    return boxes\n\ndef preprocess_image(img_path, model_image_size):\n    image_type = imghdr.what(img_path)\n    image = Image.open(img_path)\n    #image.save(\"output_box.png\")\n    resized_image = image.resize(tuple(reversed(model_image_size)), Image.BICUBIC)\n    image_data = np.array(resized_image, dtype='float32')\n    image_data /= 255.\n    image_data = np.expand_dims(image_data, 0)  # Add batch dimension.\n    return image, image_data\n\ndef draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors):\n    font = ImageFont.truetype(font='../input/firamonomedium/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))\n    thickness = (image.size[0] + image.size[1]) // 300\n\n    for i, c in reversed(list(enumerate(out_classes))):\n        predicted_class = class_names[c]\n        box = out_boxes[i]\n        score = out_scores[i]\n\n        label = '{} {:.2f}'.format(predicted_class, score)\n\n        draw = ImageDraw.Draw(image)\n        label_size = draw.textsize(label, font)\n\n        top, left, bottom, right = box\n        top = max(0, np.floor(top + 0.5).astype('int32'))\n        left = max(0, np.floor(left + 0.5).astype('int32'))\n        bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))\n        right = min(image.size[0], np.floor(right + 0.5).astype('int32'))\n        print(label, (left, top), (right, bottom))\n\n        if top - label_size[1] >= 0:\n            text_origin = np.array([left, top - label_size[1]])\n        else:\n            text_origin = np.array([left, top + 1])\n\n        # My kingdom for a good redistributable image drawing library.\n        for i in range(thickness):\n            draw.rectangle([left + i, top + i, right - i, bottom - i], outline=colors[c])\n        draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=colors[c])\n        draw.text(text_origin, label, fill=(0, 0, 0), font=font)\n        del draw","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"32bb318133522ff3c56fc3c3457a239751c01a15"},"cell_type":"markdown","source":"Define functions to predict and detect boxes. \nYou could tweak some of the parameters like IOU and box probability to filter boxes"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"350ba5ae4ab44aeefc081b40a5cd9d5dfa7c2185"},"cell_type":"code","source":"def yolo_head(feats, anchors, num_classes):\n    \"\"\"Convert final layer features to bounding box parameters.\n\n    Parameters\n    ----------\n    feats : tensor\n        Final convolutional layer features.\n    anchors : array-like\n        Anchor box widths and heights.\n    num_classes : int\n        Number of target classes.\n\n    Returns\n    -------\n    box_xy : tensor\n        x, y box predictions adjusted by spatial location in conv layer.\n    box_wh : tensor\n        w, h box predictions adjusted by anchors and conv spatial resolution.\n    box_conf : tensor\n        Probability estimate for whether each box contains any object.\n    box_class_pred : tensor\n        Probability distribution estimate for each box over class labels.\n    \"\"\"\n    num_anchors = len(anchors)\n    # Reshape to batch, height, width, num_anchors, box_params.\n    anchors_tensor = K.reshape(K.variable(anchors), [1, 1, 1, num_anchors, 2])\n    # Static implementation for fixed models.\n    # TODO: Remove or add option for static implementation.\n    # _, conv_height, conv_width, _ = K.int_shape(feats)\n    # conv_dims = K.variable([conv_width, conv_height])\n\n    # Dynamic implementation of conv dims for fully convolutional model.\n    conv_dims = K.shape(feats)[1:3]  # assuming channels last\n    # In YOLO the height index is the inner most iteration.\n    conv_height_index = K.arange(0, stop=conv_dims[0])\n    conv_width_index = K.arange(0, stop=conv_dims[1])\n    conv_height_index = K.tile(conv_height_index, [conv_dims[1]])\n\n    # TODO: Repeat_elements and tf.split doesn't support dynamic splits.\n    # conv_width_index = K.repeat_elements(conv_width_index, conv_dims[1], axis=0)\n    conv_width_index = K.tile(K.expand_dims(conv_width_index, 0), [conv_dims[0], 1])\n    conv_width_index = K.flatten(K.transpose(conv_width_index))\n    conv_index = K.transpose(K.stack([conv_height_index, conv_width_index]))\n    conv_index = K.reshape(conv_index, [1, conv_dims[0], conv_dims[1], 1, 2])\n    conv_index = K.cast(conv_index, K.dtype(feats))\n    \n    feats = K.reshape(feats, [-1, conv_dims[0], conv_dims[1], num_anchors, num_classes + 5])\n    conv_dims = K.cast(K.reshape(conv_dims, [1, 1, 1, 1, 2]), K.dtype(feats))\n\n    # Static generation of conv_index:\n    # conv_index = np.array([_ for _ in np.ndindex(conv_width, conv_height)])\n    # conv_index = conv_index[:, [1, 0]]  # swap columns for YOLO ordering.\n    # conv_index = K.variable(\n    #     conv_index.reshape(1, conv_height, conv_width, 1, 2))\n    # feats = Reshape(\n    #     (conv_dims[0], conv_dims[1], num_anchors, num_classes + 5))(feats)\n\n    box_confidence = K.sigmoid(feats[..., 4:5])\n    box_xy = K.sigmoid(feats[..., :2])\n    box_wh = K.exp(feats[..., 2:4])\n    box_class_probs = K.softmax(feats[..., 5:])\n\n    # Adjust preditions to each spatial grid point and anchor size.\n    # Note: YOLO iterates over height index before width index.\n    box_xy = (box_xy + conv_index) / conv_dims\n    box_wh = box_wh * anchors_tensor / conv_dims\n\n    return box_confidence, box_xy, box_wh, box_class_probs\n\n\ndef yolo_boxes_to_corners(box_xy, box_wh):\n    \"\"\"Convert YOLO box predictions to bounding box corners.\"\"\"\n    box_mins = box_xy - (box_wh / 2.)\n    box_maxes = box_xy + (box_wh / 2.)\n\n    return K.concatenate([\n        box_mins[..., 1:2],  # y_min\n        box_mins[..., 0:1],  # x_min\n        box_maxes[..., 1:2],  # y_max\n        box_maxes[..., 0:1]  # x_max\n    ])\n\n\n\ndef yolo_eval(yolo_outputs,\n              image_shape,\n              max_boxes=10,\n              score_threshold=.6,\n              iou_threshold=.5):\n    \"\"\"Evaluate YOLO model on given input batch and return filtered boxes.\"\"\"\n    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs\n    boxes = yolo_boxes_to_corners(box_xy, box_wh)\n    boxes, scores, classes = yolo_filter_boxes(\n        box_confidence, boxes, box_class_probs, threshold=score_threshold)\n    \n    # Scale boxes back to original image shape.\n    height = image_shape[0]\n    width = image_shape[1]\n    image_dims = K.stack([height, width, height, width])\n    image_dims = K.reshape(image_dims, [1, 4])\n    boxes = boxes * image_dims\n\n    # TODO: Something must be done about this ugly hack!\n    max_boxes_tensor = K.variable(max_boxes, dtype='int32')\n    K.get_session().run(tf.variables_initializer([max_boxes_tensor]))\n    nms_index = tf.image.non_max_suppression(\n        boxes, scores, max_boxes_tensor, iou_threshold=iou_threshold)\n    boxes = K.gather(boxes, nms_index)\n    scores = K.gather(scores, nms_index)\n    classes = K.gather(classes, nms_index)\n    \n    return boxes, scores, classes\n\n\ndef preprocess_true_boxes(true_boxes, anchors, image_size):\n    \"\"\"Find detector in YOLO where ground truth box should appear.\n\n    Parameters\n    ----------\n    true_boxes : array\n        List of ground truth boxes in form of relative x, y, w, h, class.\n        Relative coordinates are in the range [0, 1] indicating a percentage\n        of the original image dimensions.\n    anchors : array\n        List of anchors in form of w, h.\n        Anchors are assumed to be in the range [0, conv_size] where conv_size\n        is the spatial dimension of the final convolutional features.\n    image_size : array-like\n        List of image dimensions in form of h, w in pixels.\n\n    Returns\n    -------\n    detectors_mask : array\n        0/1 mask for detectors in [conv_height, conv_width, num_anchors, 1]\n        that should be compared with a matching ground truth box.\n    matching_true_boxes: array\n        Same shape as detectors_mask with the corresponding ground truth box\n        adjusted for comparison with predicted parameters at training time.\n    \"\"\"\n    height, width = image_size\n    num_anchors = len(anchors)\n    # Downsampling factor of 5x 2-stride max_pools == 32.\n    # TODO: Remove hardcoding of downscaling calculations.\n    assert height % 32 == 0, 'Image sizes in YOLO_v2 must be multiples of 32.'\n    assert width % 32 == 0, 'Image sizes in YOLO_v2 must be multiples of 32.'\n    conv_height = height // 32\n    conv_width = width // 32\n    num_box_params = true_boxes.shape[1]\n    detectors_mask = np.zeros(\n        (conv_height, conv_width, num_anchors, 1), dtype=np.float32)\n    matching_true_boxes = np.zeros(\n        (conv_height, conv_width, num_anchors, num_box_params),\n        dtype=np.float32)\n\n    for box in true_boxes:\n        # scale box to convolutional feature spatial dimensions\n        box_class = box[4:5]\n        box = box[0:4] * np.array(\n            [conv_width, conv_height, conv_width, conv_height])\n        i = np.floor(box[1]).astype('int')\n        j = min(np.floor(box[0]).astype('int'),1)\n        best_iou = 0\n        best_anchor = 0\n                \n        for k, anchor in enumerate(anchors):\n            # Find IOU between box shifted to origin and anchor box.\n            box_maxes = box[2:4] / 2.\n            box_mins = -box_maxes\n            anchor_maxes = (anchor / 2.)\n            anchor_mins = -anchor_maxes\n\n            intersect_mins = np.maximum(box_mins, anchor_mins)\n            intersect_maxes = np.minimum(box_maxes, anchor_maxes)\n            intersect_wh = np.maximum(intersect_maxes - intersect_mins, 0.)\n            intersect_area = intersect_wh[0] * intersect_wh[1]\n            box_area = box[2] * box[3]\n            anchor_area = anchor[0] * anchor[1]\n            iou = intersect_area / (box_area + anchor_area - intersect_area)\n            if iou > best_iou:\n                best_iou = iou\n                best_anchor = k\n                \n        if best_iou > 0:\n            detectors_mask[i, j, best_anchor] = 1\n            adjusted_box = np.array(\n                [\n                    box[0] - j, box[1] - i,\n                    np.log(box[2] / anchors[best_anchor][0]),\n                    np.log(box[3] / anchors[best_anchor][1]), box_class\n                ],\n                dtype=np.float32)\n            matching_true_boxes[i, j, best_anchor] = adjusted_box\n    return detectors_mask, matching_true_boxes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"67c2b4ceee67db1a4af2cb9db1aa5fb611d726c3","collapsed":true},"cell_type":"code","source":"from keras.models import load_model, Model\n#yolo_model = load_model(\"../input/yolov3/yolov3.h5\")\nyolo_model = load_model(\"../input/yolov2-h5/yolov2.h5\")\nyolo_model.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a60c7046ac355fc5b10202fe85388bc24d5c1216","collapsed":true},"cell_type":"code","source":"#class_names = read_classes(\"../input/coco-classes/coco_classes.txt\")\nclass_names = read_classes(\"../input/coco-classesv2/coco_classesv2.txt\")\nanchors = read_anchors(\"../input/yolo-anchors/yolo_anchors.txt\")\nimage_shape = (720., 1280.)\nprint(len(class_names))\nprint(len(anchors))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f46122b6be6c8b11e385c0174aa2d834f6826169","collapsed":true},"cell_type":"code","source":"#!cp -r ../input/keras-yolo-new/ky  ky\n#from ky.keras_yolo import yolo_head, yolo_boxes_to_corners, preprocess_true_boxes, yolo_loss, yolo_body \n#print(yolo_model.output)\nprint(yolo_model.output)\nyolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names))\nprint( \"Yolo op \",yolo_outputs )\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"194236cb99693e7640fea30ce66852b36e86e973"},"cell_type":"code","source":"def yolo_filter_boxes(box_confidence, boxes, box_class_probs, threshold = .6):\n    \"\"\"Filters YOLO boxes by thresholding on object and class confidence.\n    \n    Arguments:\n    box_confidence -- tensor of shape (19, 19, 5, 1)\n    boxes -- tensor of shape (19, 19, 5, 4)\n    box_class_probs -- tensor of shape (19, 19, 5, 80)\n    threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box\n    \n    Returns:\n    scores -- tensor of shape (None,), containing the class probability score for selected boxes\n    boxes -- tensor of shape (None, 4), containing (b_x, b_y, b_h, b_w) coordinates of selected boxes\n    classes -- tensor of shape (None,), containing the index of the class detected by the selected boxes\n    \n    Note: \"None\" is here because you don't know the exact number of selected boxes, as it depends on the threshold. \n    For example, the actual output size of scores would be (10,) if there are 10 boxes.\n    \"\"\"\n    \n    # Step 1: Compute box scores\n    ### START CODE HERE ### (≈ 1 line)\n    box_scores = box_confidence * box_class_probs\n    #print(box_scores.shape)\n    ### END CODE HERE ###\n    \n    # Step 2: Find the box_classes thanks to the max box_scores, keep track of the corresponding score\n    ### START CODE HERE ### (≈ 2 lines)\n    box_classes = K.argmax(box_scores, axis = -1)\n    box_class_scores = K.max(box_scores,axis = -1)\n    ### END CODE HERE ###\n    \n    # Step 3: Create a filtering mask based on \"box_class_scores\" by using \"threshold\". The mask should have the\n    # same dimension as box_class_scores, and be True for the boxes you want to keep (with probability >= threshold)\n    ### START CODE HERE ### (≈ 1 line)\n    filtering_mask = (box_class_scores >= threshold)\n    ### END CODE HERE ###\n    \n    # Step 4: Apply the mask to scores, boxes and classes\n    ### START CODE HERE ### (≈ 3 lines)\n    scores = tf.boolean_mask(box_class_scores,filtering_mask)\n    boxes = tf.boolean_mask(boxes,filtering_mask)\n    classes = tf.boolean_mask(box_classes,filtering_mask)\n    ### END CODE HERE ###\n    \n    return scores, boxes, classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"50d3c8973420f33027017761f333a4a8b6452652"},"cell_type":"code","source":"def yolo_non_max_suppression(scores, boxes, classes, max_boxes = 10, iou_threshold = 0.5):\n    \"\"\"\n    Applies Non-max suppression (NMS) to set of boxes\n    \n    Arguments:\n    scores -- tensor of shape (None,), output of yolo_filter_boxes()\n    boxes -- tensor of shape (None, 4), output of yolo_filter_boxes() that have been scaled to the image size (see later)\n    classes -- tensor of shape (None,), output of yolo_filter_boxes()\n    max_boxes -- integer, maximum number of predicted boxes you'd like\n    iou_threshold -- real value, \"intersection over union\" threshold used for NMS filtering\n    \n    Returns:\n    scores -- tensor of shape (, None), predicted score for each box\n    boxes -- tensor of shape (4, None), predicted box coordinates\n    classes -- tensor of shape (, None), predicted class for each box\n    \n    Note: The \"None\" dimension of the output tensors has obviously to be less than max_boxes. Note also that this\n    function will transpose the shapes of scores, boxes, classes. This is made for convenience.\n    \"\"\"\n    \n    max_boxes_tensor = K.variable(max_boxes, dtype='int32')     # tensor to be used in tf.image.non_max_suppression()\n    K.get_session().run(tf.variables_initializer([max_boxes_tensor])) # initialize variable max_boxes_tensor\n    \n    # Use tf.image.non_max_suppression() to get the list of indices corresponding to boxes you keep\n    ### START CODE HERE ### (≈ 1 line)\n    nms_indices = tf.image.non_max_suppression(boxes, scores, max_boxes, iou_threshold)\n    ### END CODE HERE ###\n    \n    # Use K.gather() to select only nms_indices from scores, boxes and classes\n    ### START CODE HERE ### (≈ 3 lines)\n    scores = K.gather(scores, nms_indices)\n    boxes = K.gather(boxes, nms_indices)\n    classes = K.gather(classes, nms_indices)\n    ### END CODE HERE ###\n    \n    return scores, boxes, classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"23ea9583772cc7791ade2bd2e6bbf552fd1c41e0"},"cell_type":"code","source":"def yolo_eval(yolo_outputs, image_shape = (720., 1280.), max_boxes=10, score_threshold=.6, iou_threshold=.5):\n    \"\"\"\n    Converts the output of YOLO encoding (a lot of boxes) to your predicted boxes along with their scores, box coordinates and classes.\n    \n    Arguments:\n    yolo_outputs -- output of the encoding model (for image_shape of (608, 608, 3)), contains 4 tensors:\n                    box_confidence: tensor of shape (None, 19, 19, 5, 1)\n                    box_xy: tensor of shape (None, 19, 19, 5, 2)\n                    box_wh: tensor of shape (None, 19, 19, 5, 2)\n                    box_class_probs: tensor of shape (None, 19, 19, 5, 80)\n    image_shape -- tensor of shape (2,) containing the input shape, in this notebook we use (608., 608.) (has to be float32 dtype)\n    max_boxes -- integer, maximum number of predicted boxes you'd like\n    score_threshold -- real value, if [ highest class probability score < threshold], then get rid of the corresponding box\n    iou_threshold -- real value, \"intersection over union\" threshold used for NMS filtering\n    \n    Returns:\n    scores -- tensor of shape (None, ), predicted score for each box\n    boxes -- tensor of shape (None, 4), predicted box coordinates\n    classes -- tensor of shape (None,), predicted class for each box\n    \"\"\"\n    \n    ### START CODE HERE ### \n    \n    # Retrieve outputs of the YOLO model (≈1 line)\n    box_confidence, box_xy, box_wh, box_class_probs = yolo_outputs[:]\n\n    # Convert boxes to be ready for filtering functions \n    boxes = yolo_boxes_to_corners(box_xy, box_wh)\n\n    # Use one of the functions you've implemented to perform Score-filtering with a threshold of score_threshold (≈1 line)\n    scores, boxes, classes = yolo_filter_boxes(box_confidence, boxes, box_class_probs, score_threshold)\n    \n    # Scale boxes back to original image shape.\n    boxes = scale_boxes(boxes, image_shape)\n\n    # Use one of the functions you've implemented to perform Non-max suppression with a threshold of iou_threshold (≈1 line)\n    scores, boxes, classes = yolo_non_max_suppression( scores, boxes, classes, max_boxes, iou_threshold )\n    \n    ### END CODE HERE ###\n    \n    return scores, boxes, classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c11885c190ebc16de26d7c41a53e7158dfeb62bc","collapsed":true},"cell_type":"code","source":"\nscores, boxes, classes = yolo_eval(yolo_outputs, image_shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7bced0d3dc0a4631f5618fabfb1480bae0269351","collapsed":true},"cell_type":"code","source":"print(scores.shape,boxes.shape,classes.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"48cd812ff95f70def090152dbd66abf4b17cafe9"},"cell_type":"code","source":"sess = K.get_session()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b721adb16892a74af6f35c8698716d387f7fca6e"},"cell_type":"markdown","source":"Function to predict boxes and plot the image"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"4b0c2b3323d7b4653ece567be8e7748a74f30828"},"cell_type":"code","source":"def predict(sess, image_file):\n    \"\"\"\n    Runs the graph stored in \"sess\" to predict boxes for \"image_file\". Prints and plots the preditions.\n    \n    Arguments:\n    sess -- your tensorflow/Keras session containing the YOLO graph\n    image_file -- name of an image stored in the \"images\" folder.\n    \n    Returns:\n    out_scores -- tensor of shape (None, ), scores of the predicted boxes\n    out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes\n    out_classes -- tensor of shape (None, ), class index of the predicted boxes\n    \n    Note: \"None\" actually represents the number of predicted boxes, it varies between 0 and max_boxes. \n    \"\"\"\n\n    # Preprocess your image\n    image, image_data = preprocess_image(image_file, model_image_size = (608, 608))\n    #image, image_data = preprocess_image(image_file, model_image_size = (416, 416))\n    print(\"input image:\",image_data.shape)\n    # Run the session with the correct tensors and choose the correct placeholders in the feed_dict.\n    # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0})\n    ### START CODE HERE ### (≈ 1 line)\n    out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input:image_data,K.learning_phase(): 0})\n    ### END CODE HERE ###\n    # Print predictions info\n    print('Found {} boxes for {}'.format(len(out_boxes), image_file))\n    if len(out_boxes) == 0:\n        return out_scores, out_boxes, out_classes\n    # Generate colors for drawing bounding boxes.\n    colors = generate_colors(class_names)\n    # Draw bounding boxes on the image file\n    print('Drawing images...')\n    draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)\n    print('Drawing complete...')\n    # Save the predicted bounding box on the image\n    #image.save(os.path.join(\"out\", image_file), quality=90)\n    image.save(\"output_image_box.jpg\", quality=90)\n    # Display the results in the notebook\n    output_image = scipy.misc.imread(\"output_image_box.jpg\")\n    #imshow(output_image)\n    plt.figure(figsize=(12,12))\n    plt.imshow(plt.imread(\"output_image_box.jpg\"))\n    plt.show()\n    return out_scores, out_boxes, out_classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b44d6b2862aae2be4b9c9b24ff6a17a11478003b","collapsed":true},"cell_type":"code","source":"\nimage_file = \"challenge2018_test/b4c3b52a8723d431.jpg\"\nprint(yolo_model.input)\nprint(yolo_model.output)\n#font = ImageFont.truetype(font='../input/firamonomedium/FiraMono-Medium.otf',size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))\n#image, image_data = preprocess_image(\"../input/google-ai-open-images-object-detection-track/test/\" + image_file, model_image_size = (608, 608))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90fc7c6920bfabfda1f18d811c3075a2a73bfbac","collapsed":true},"cell_type":"code","source":"#out_scores, out_boxes, out_classes = predict(sess, img)\nout_scores, out_boxes, out_classes = predict(sess, \"../input/google-ai-open-images-object-detection-track/test/\" + image_file)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c801f034dcd39c057641167ed73620416de3346a"},"cell_type":"markdown","source":""},{"metadata":{"_uuid":"0387b6fa4ab3b4d1e1d8b2285687e2003836441e"},"cell_type":"markdown","source":"Lets view some predictions and detected objects "},{"metadata":{"trusted":true,"_uuid":"93a60648ec0ccbec8f57db75a892e9ffcf036d59","collapsed":true},"cell_type":"code","source":"image_path = \"../input/google-ai-open-images-object-detection-track/test/\"\n\nbatch_size = 10\nimg_generator = ImageDataGenerator().flow_from_directory(image_path, shuffle=False, batch_size = batch_size)\nn_rounds = math.ceil(img_generator.samples / img_generator.batch_size)\nfilenames = img_generator.filenames","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04dbf6f9ec3cafe4da4fef1efe48c8894e0a5097","collapsed":true},"cell_type":"code","source":"for i in range(n_rounds):\n    batch = next(img_generator)\n    for j, prediction in enumerate(batch):\n        image = filenames[i * batch_size + j]\n        out_scores, out_boxes, out_classes = predict(sess, image_path + image)\n\n    if i == 10:\n        print(\"i is :\",i)\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"5e3ad3186294e2b9713c7410e9919718a5ad562e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"714f08288a726685148e7560c5975735ff27e1fc"},"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}