Exemplo n.º 1
0
def draw(model_body,
         class_names,
         anchors,
         image_data,
         image_set='val',
         weights_name='trained_stage_3_best.h5',
         out_path="output_images",
         save_all=True):
    '''
    Draw bounding boxes on image data
    '''
    if image_set == 'train':
        image_data = np.array([
            np.expand_dims(image, axis=0)
            for image in image_data[:int(len(image_data) * .9)]
        ])
    elif image_set == 'val':
        image_data = np.array([
            np.expand_dims(image, axis=0)
            for image in image_data[int(len(image_data) * .9):]
        ])
    elif image_set == 'all':
        image_data = np.array(
            [np.expand_dims(image, axis=0) for image in image_data])
    else:
        ValueError("draw argument image_set must be 'train', 'val', or 'all'")
    # model.load_weights(weights_name)
    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=0.07,
                                       iou_threshold=0)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    if not os.path.exists(out_path):
        os.makedirs(out_path)
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_data.shape[2], image_data.shape[3]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for image.'.format(len(out_boxes)))
        print(out_boxes)

        # Plot image with predicted boxes.
        image_with_boxes = draw_boxes(image_data[i][0], out_boxes, out_classes,
                                      class_names, out_scores)
        # Save the image:
        if save_all or (len(out_boxes) > 0):
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(os.path.join(out_path, str(i) + '.png'))
Exemplo n.º 2
0
    def get_bounding_boxes(self, image):
        image_shape = (416, 416)

        resized_image = image.resize(tuple(image_shape), PIL.Image.BICUBIC)
        image_data = np.array(resized_image, dtype='float32')
        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)

        sess = K.get_session()
        out_boxes, out_scores, out_classes = sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model_body.input:
                image_data,
                self.yolo_input_image_shape:
                [image_data.shape[1], image_data.shape[2]],
                K.learning_phase():
                0
            })

        # Convert pred on 416 to actual image size
        resized_boxes = out_boxes / 416

        w, h = image.size
        box_resize_dim = [h, w, h, w]
        resized_boxes = resized_boxes * box_resize_dim
        orig_image_data = np.array(image, dtype='float32')
        orig_image_with_boxes = draw_boxes(orig_image_data, resized_boxes,
                                           out_classes, class_names,
                                           out_scores, "rand")

        return resized_boxes, out_classes, orig_image_with_boxes
Exemplo n.º 3
0
def draw(model_body,
         class_names,
         anchors,
         partition,
         images_path,
         image_set='validation',
         weights_name='trained_stage_3_best.h5',
         out_path="output_images",
         save_all=True):
    '''
    Draw bounding boxes on image data
    '''

    # load validation data
    hdf5_file_images = h5py.File(images_path, "r")
    image_data = hdf5_file_images["images"][partition[image_set], ...]
    hdf5_file_images.close()

    image_data = np.expand_dims(image_data, axis=1)

    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=0.5,
                                       iou_threshold=0.5)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    if not os.path.exists(out_path):
        os.makedirs(out_path)
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_data.shape[2], image_data.shape[3]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for image.'.format(len(out_boxes)))
        print(out_boxes)

        # Plot image with predicted boxes.
        image_with_boxes = draw_boxes(image_data[i][0].astype(np.uint8) * 255,
                                      out_boxes, out_classes, class_names,
                                      out_scores)
        # Save the image:
        if save_all or (len(out_boxes) > 0):
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(os.path.join(out_path, str(i) + '.png'))
Exemplo n.º 4
0
def get_bounding_boxes(yolo_model_body,
                       boxes,
                       scores,
                       classes,
                       yolo_input_image_shape,
                       img_path,
                       class_names,
                       out_path,
                       save=True):
    image_shape = (416, 416)
    image = PIL.Image.open(img_path)

    resized_image = image.resize(tuple(image_shape), PIL.Image.BICUBIC)
    image_data = np.array(resized_image, dtype='float32')
    image_data /= 255.
    image_data = np.expand_dims(image_data, 0)

    sess = K.get_session()
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            yolo_model_body.input: image_data,
            yolo_input_image_shape: [image_data.shape[1], image_data.shape[2]],
            K.learning_phase(): 0
        })

    _, tail = ntpath.split(img_path)
    tail = os.path.splitext(tail)[0]
    print("file Name:" + tail)
    orig_img_name = out_path + "\\" + tail + ".jpg"

    # image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes, class_names, out_scores,orig_img_name)

    # Convert pred on 416 to actual image size
    resized_boxes = out_boxes / 416

    w, h = image.size
    box_resize_dim = [h, w, h, w]
    resized_boxes = resized_boxes * box_resize_dim
    orig_image_data = np.array(image, dtype='float32')
    orig_image_with_boxes = draw_boxes(orig_image_data, resized_boxes,
                                       out_classes, class_names, out_scores,
                                       orig_img_name)

    # Save the image:
    if save:
        orig_image = PIL.Image.fromarray(orig_image_with_boxes)
        orig_image.save(orig_img_name)

    return resized_boxes, out_classes
Exemplo n.º 5
0
def draw(model_body, class_names, anchors, image_data, image_set='val',
            weights_name='trained_stage_3_best.h5', out_path="output_images", save_all=True):
    '''
    Draw bounding boxes on image data
    '''
    if image_set == 'train':
        image_data = np.array([np.expand_dims(image, axis=0)
            for image in image_data[:int(len(image_data)*.9)]])
    elif image_set == 'val':
        image_data = np.array([np.expand_dims(image, axis=0)
            for image in image_data[int(len(image_data)*.9):]])
    elif image_set == 'all':
        image_data = np.array([np.expand_dims(image, axis=0)
            for image in image_data])
    else:
        ValueError("draw argument image_set must be 'train', 'val', or 'all'")
    # model.load_weights(weights_name)
    print(image_data.shape)
    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(
        yolo_outputs, input_image_shape, score_threshold=0.07, iou_threshold=0)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    if  not os.path.exists(out_path):
        os.makedirs(out_path)
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_data.shape[2], image_data.shape[3]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for image.'.format(len(out_boxes)))
        print(out_boxes)

        # Plot image with predicted boxes.
        image_with_boxes = draw_boxes(image_data[i][0], out_boxes, out_classes,
                                    class_names, out_scores)
        # Save the image:
        if save_all or (len(out_boxes) > 0):
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(os.path.join(out_path,str(i)+'.png'))
Exemplo n.º 6
0
def draw(model_body, class_names, anchors,
         image_data, weights_name='trained_stage_3_best.h5',
         out_path="output_images", save_all=True):
    '''
    Draw bounding boxes on image data
    '''
    # model.load_weights(weights_name)
    print(image_data.shape)
    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(
        yolo_outputs, input_image_shape, score_threshold=0.07, iou_threshold=0.0)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    if  not os.path.exists(out_path):
        os.makedirs(out_path)
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_data.shape[2], image_data.shape[3]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for image.'.format(len(out_boxes)))
        print(out_boxes)

        # Plot image with predicted boxes.
        image_with_boxes = draw_boxes(image_data[i][0], out_boxes, out_classes,
                                    class_names, out_scores)
        # Save the image:
        if save_all or (len(out_boxes) > 0):
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(os.path.join(out_path,str(i)+'.png'))

        # To display (pauses the program):
        plt.imshow(image_with_boxes, interpolation='nearest')
        plt.show()
Exemplo n.º 7
0
def draw(out_boxes,
         out_scores,
         out_classes,
         image,
         name,
         class_names,
         display=True):
    if len(out_boxes) != 0:
        # np.swapaxes(image, 0, 1) # opencv convention is to access image as (row, column)
        image = draw_boxes(image,
                           out_boxes,
                           out_classes,
                           class_names,
                           scores=out_scores,
                           rectify=False)
        # np.swapaxes(image, 0, 1)
    if display:
        cv2.imshow(name, image)
        cv2.waitKey(3)
    return image
Exemplo n.º 8
0
def draw(boxes, scores, classes, model_body, class_names, image_data):
    '''
    Draw bounding boxes on image datac
    '''
    global input_image_shape, imageQueue, q
    global buffer
    global next_route, IsCalling, Mob_Name, ans, music_state, direc, arrived, speed

    image_data = np.array(
        [np.expand_dims(image, axis=0) for image in image_data])
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [416, 416],
                K.learning_phase(): 0  #testing phase
            })

        if AR_Mode:
            classes_to_be_shown = average_classes(out_classes)
            buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[
                11] = WriteBuffer(out_boxes, out_classes, classes_to_be_shown)
            image_with_boxes = DrawDirection(buffer,
                                             IsFrame=True,
                                             FramePath=None,
                                             Frame=image_data[i][0])
            image_with_boxes = image_with_boxes.resize((416, 416),
                                                       Image.ANTIALIAS)
            cv2.imwrite(
                'Video_Images/{}.jpg'.format(q),
                cv2.cvtColor(np.array(image_with_boxes), cv2.COLOR_BGR2RGB))
        else:
            image_with_boxes = draw_boxes(image_data[i][0], out_boxes,
                                          out_classes, class_names, out_scores)
        imageQueue.put(np.array(image_with_boxes))
        #queue image_with_boxes to imageQueue.

    return out_classes
Exemplo n.º 9
0
    def draw(self,
             test_model_path=None,
             image_set='validation',
             weights_name='trained_stage_3_best.h5',
             out_path="output_images",
             save_all=True):
        """
        Draw bounding boxes on image data
        """

        if test_model_path is None:
            self.model_body.load_weights(weights_name)
        else:
            self.model_body = load_model(test_model_path)
        if self.overfit_single_image:
            partition_eval = self.partition["train"][[0, 0]]
        else:
            partition_eval = self.partition[image_set]
        # load validation data
        # only annotate 100 images max
        if len(partition_eval) > 100:
            partition_eval = np.random.choice(partition_eval, (100, ))

        files_to_load = self.file_list[partition_eval]
        print(files_to_load.shape)
        image_data = [np.load(file)['image'] for file in files_to_load]
        image_data = process_data(image_data)
        image_data = np.array(
            [np.expand_dims(image, axis=0) for image in image_data])

        # Create output variables for prediction.
        yolo_outputs = yolo_head(self.model_body.output, self.anchors,
                                 len(self.class_names))
        input_image_shape = K.placeholder(shape=(2, ))
        boxes, scores, classes = yolo_eval(yolo_outputs,
                                           input_image_shape,
                                           score_threshold=0.5,
                                           iou_threshold=0.5)

        # Run prediction images.
        sess = K.get_session()

        if not os.path.exists(out_path):
            os.makedirs(out_path)
        for i in range(len(image_data)):
            out_boxes, out_scores, out_classes = sess.run(
                [boxes, scores, classes],
                feed_dict={
                    self.model_body.input: image_data[i],
                    input_image_shape:
                    [image_data.shape[2], image_data.shape[3]],
                    K.learning_phase(): 0
                })
            print('Found {} boxes for image.'.format(len(out_boxes)))
            print(out_boxes)

            # Plot image with predicted boxes.
            image_with_boxes = draw_boxes(image_data[i][0], out_boxes,
                                          out_classes, self.class_names,
                                          out_scores)
            # Save the image:
            if save_all or (len(out_boxes) > 0):
                image = PIL.Image.fromarray(image_with_boxes)
                image.save(os.path.join(out_path, str(i) + '.tif'))
Exemplo n.º 10
0
def draw(model_body,
         class_names,
         anchors,
         image_data,
         image_data_original,
         truth_boxes,
         image_set='val',
         weights_name='trained_stage_3_best.h5',
         out_path="output_images",
         save_all=True):
    '''
    Draw bounding boxes on image data
    '''

    image_size_orig = tuple(reversed(
        image_data_original.shape[1:3]))  # width, height
    image_data_size = tuple(reversed(image_data.shape[1:3]))  # width, height
    print(image_size_orig)
    print(image_data_size)
    plotter = BoxPlotter(image_size_orig)

    if image_set == 'train':
        image_data = np.array([
            np.expand_dims(image, axis=0)
            for image in image_data[:int(len(image_data) * .9)]
        ])
    elif image_set == 'val':
        image_data = np.array([
            np.expand_dims(image, axis=0)
            for image in image_data[int(len(image_data) * .9):]
        ])
    elif image_set == 'all':
        image_data = np.array(
            [np.expand_dims(image, axis=0) for image in image_data])
    else:
        ValueError("draw argument image_set must be 'train', 'val', or 'all'")
    # model.load_weights(weights_name)
    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=0.07,
                                       iou_threshold=0.5)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    if not os.path.exists(out_path):
        os.makedirs(out_path)
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_size_orig[1],
                                    image_size_orig[0]],  # height, width
                K.learning_phase(): 0
            })
        print('Found {} boxes for image.'.format(len(out_boxes)))
        # outboxes = [ymin, xmin, ymax, xmax]
        out_boxes2 = out_boxes[:, [1, 0, 3, 2]]

        image = image_data_original[i]
        image = image / np.max(image) * 255
        image = image.astype(np.uint8)

        y = plotter.package_data(truth_boxes[i][:, 1:],
                                 truth_boxes[i][:, 0].astype(np.int))
        yhat = plotter.package_data(out_boxes2, out_classes.astype(np.int),
                                    out_scores)
        plotter.comparison(y, yhat, image)

        # Plot image with predicted boxes.
        image_with_boxes = draw_boxes(image_data_original[i], out_boxes,
                                      out_classes, class_names, out_scores)
        # Save the image:
        if save_all or (len(out_boxes) > 0):
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(os.path.join(out_path, str(i) + '.png'))

        # To display (pauses the program):
        # plt.imshow(image_with_boxes, interpolation='nearest')
        # plt.show()
        input("Enter for next image")
Exemplo n.º 11
0
def predict_draw(model_body,
                 class_names,
                 anchors,
                 img_path,
                 weights_name='trained_stage_3_best.h5',
                 out_path="output_images",
                 save_all=True):
    '''
    Draw bounding boxes on image data
    '''

    head, tail = ntpath.split(img_path)
    image = PIL.Image.open(img_path)
    resized_image = image.resize(tuple(image_shape), PIL.Image.BICUBIC)
    image_data = np.array(resized_image, dtype='float32')

    image_data /= 255.

    image_data = np.expand_dims(image_data, 0)
    print(image_data.shape)
    # model.load_weights(weights_name)
    # print(image_data.shape)
    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       max_boxes=3,
                                       score_threshold=.7,
                                       iou_threshold=0.05)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    logging = TensorBoard()
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    #for i in range(len(image_data)):
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            model_body.input: image_data,
            input_image_shape: [image_data.shape[1], image_data.shape[2]],
            K.learning_phase(): 0
        })
    print('Found {} boxes for image.'.format(len(out_boxes)))
    print(out_boxes)

    # Plot image with predicted boxes.
    image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes,
                                  class_names, out_scores,
                                  out_path + "\\" + tail)

    # Save the image:
    if save_all or (len(out_boxes) > 0):
        image = PIL.Image.fromarray(image_with_boxes)
        image.save(os.path.join(out_path, tail + '.png'))

# To display (pauses the program):
    plt.imshow(image_with_boxes, interpolation='nearest')
    plt.show()
Exemplo n.º 12
0
def _main(args):
    voc_path = os.path.expanduser(args.data_path)
    #classes_path = os.path.expanduser(args.classes_path)
    anchors_path = os.path.expanduser(args.anchors_path)

    #with open(classes_path) as f:
    #    class_names = f.readlines()
    #class_names = [c.strip() for c in class_names]
    class_names = ['circle', 'square']

    if os.path.isfile(anchors_path):
        with open(anchors_path) as f:
            anchors = f.readline()
            anchors = [float(x) for x in anchors.split(',')]
            anchors = np.array(anchors).reshape(-1, 2)
    else:
        anchors = YOLO_ANCHORS
    ''' file load ''
    voc = h5py.File(voc_path, 'r')
    image = PIL.Image.open(io.BytesIO(voc['train/images'][28]))
    orig_size = np.array([image.width, image.height])
    orig_size = np.expand_dims(orig_size, axis=0)

    # Image preprocessing.
    image = image.resize((416, 416), PIL.Image.BICUBIC)
    image_data = np.array(image, dtype=np.float)
    image_data /= 255.
    '''

    # Generate shape w/pymrt
    image_data = np.empty((2, 416, 416, 3), dtype=np.float)
    circ = geo.circle(shape=(416, 416), radius=40, position=0.5)
    image_data[0, :, :, 0] = circ
    image_data[0, :, :, 1] = circ
    image_data[0, :, :, 2] = circ

    sq = geo.square(shape=(416, 416), side=40, position=0.5)
    image_data[1, :, :, 0] = sq
    image_data[1, :, :, 1] = sq
    image_data[1, :, :, 2] = sq

    #image = image_data
    orig_size = np.array([416, 416])

    # Box preprocessing.
    # Original boxes stored as 1D list of class, x_min, y_min, x_max, y_max.
    ## boxes = voc['train/boxes'][28]
    boxes = np.asarray([[0, 168, 168, 248, 248], [1, 168, 168, 248, 248]])
    boxes = boxes.reshape((-1, 5))
    # Get extents as y_min, x_min, y_max, x_max, class for comparision with
    # model output.
    boxes_extents = boxes[:, [2, 1, 4, 3, 0]]

    # Get box parameters as x_center, y_center, box_width, box_height, class.
    boxes_xy = 0.5 * (boxes[:, 3:5] + boxes[:, 1:3])
    boxes_wh = boxes[:, 3:5] - boxes[:, 1:3]
    boxes_xy = boxes_xy / orig_size  # convert to relative coordinates
    boxes_wh = boxes_wh / orig_size
    boxes = np.concatenate((boxes_xy, boxes_wh, boxes[:, 0:1]), axis=1)

    # Precompute detectors_mask and matching_true_boxes for training.
    # Detectors mask is 1 for each spatial position in the final conv layer and
    # anchor that should be active for the given boxes and 0 otherwise.
    # Matching true boxes gives the regression targets for the ground truth box
    # that caused a detector to be active or 0 otherwise.
    detectors_mask_shape = (13, 13, 5, 1)
    matching_boxes_shape = (13, 13, 5, 5)

    def get_detector_mask(boxes, anchors):
        detectors_mask = [0 for i in range(len(boxes))]
        matching_true_boxes = [0 for i in range(len(boxes))]
        for i, box in enumerate(boxes):
            box = box.reshape((-1, 5))
            detectors_mask[i], matching_true_boxes[i] = preprocess_true_boxes(
                box, anchors, [416, 416])

        return np.array(detectors_mask), np.array(matching_true_boxes)

    detectors_mask, matching_true_boxes = get_detector_mask(boxes, anchors)
    #detectors_mask, matching_true_boxes = preprocess_true_boxes(boxes, anchors,
    #                                                            [416, 416])

    # Create model input layers.
    image_input = Input(shape=(416, 416, 3))
    boxes_input = Input(shape=(None, 5))
    detectors_mask_input = Input(shape=detectors_mask_shape)
    matching_boxes_input = Input(shape=matching_boxes_shape)

    #import pdb; pdb.set_trace()

    print('Boxes:')
    print(boxes)
    ##print('Box corners:')
    ##print(boxes_extents)
    print('Active detectors:')
    print(np.where(detectors_mask == 1)[:-1])
    print('Matching boxes for active detectors:')
    print(matching_true_boxes[np.where(detectors_mask == 1)[:-1]])

    # Create model body.
    model_body = yolo_body(image_input, len(anchors), len(class_names))
    model_body = Model(image_input, model_body.output)
    # Place model loss on CPU to reduce GPU memory usage.
    with tf.device('/cpu:0'):
        # TODO: Replace Lambda with custom Keras layer for loss.
        model_loss = Lambda(yolo_loss,
                            output_shape=(1, ),
                            name='yolo_loss',
                            arguments={
                                'anchors': anchors,
                                'num_classes': len(class_names)
                            })([
                                model_body.output, boxes_input,
                                detectors_mask_input, matching_boxes_input
                            ])
    model = Model(
        [image_input, boxes_input, detectors_mask_input, matching_boxes_input],
        model_loss)
    model.compile(
        optimizer='adam', loss={
            'yolo_loss': lambda y_true, y_pred: y_pred
        })  # This is a hack to use the custom loss function in the last layer.

    # Add batch dimension for training.
    #image_data = image_data[0,:,:,:]
    #image_data = np.expand_dims(image_data, axis=0)
    boxes = np.expand_dims(boxes, axis=1)
    #detectors_mask = np.expand_dims(detectors_mask, axis=0)
    #matching_true_boxes = np.expand_dims(matching_true_boxes, axis=0)
    num_steps = 1000
    # TODO: For full training, put preprocessing inside training loop.
    # for i in range(num_steps):
    #     loss = model.train_on_batch(
    #         [image_data, boxes, detectors_mask, matching_true_boxes],
    #         np.zeros(len(image_data)))
    #import pdb; pdb.set_trace()
    model.fit([image_data, boxes, detectors_mask, matching_true_boxes],
              np.zeros(len(image_data)),
              batch_size=2,
              epochs=num_steps)
    model.save_weights('overfit_circle_square_weights.h5')

    #model.load_weights('overfit_circle_square_weights.h5')
    #model.load_weights('overfit_weights.h5')

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=.3,
                                       iou_threshold=.9)

    # Run prediction on overfit image.
    num_shapes = 10
    image_data = np.empty((1, 416, 416, 3), dtype=np.float)
    #for sh in range(num_shape)
    circ2 = geo.circle(shape=(416, 416), radius=40, position=(0.4, 0.4))
    #circ3 = geo.circle(shape=(416,416),radius=40,position=0.8)
    #sq2 = geo.square(shape=(416,416),side=40,position=0.2)
    sq3 = geo.square(shape=(416, 416), side=40, position=0.8)
    image_data = np.empty((1, 416, 416, 3), dtype=np.float)
    image_data[0, :, :, 0] = circ2 + sq3
    image_data[0, :, :, 1] = circ2 + sq3
    image_data[0, :, :, 2] = circ2 + sq3

    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            model_body.input: image_data,
            #input_image_shape: [image.size[1], image.size[0]],
            input_image_shape: [416, 416],
            K.learning_phase(): 0
        })
    print('Found {} boxes for image.'.format(len(out_boxes)))
    print(out_boxes)

    # Plot image with predicted boxes.
    image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes,
                                  class_names, out_scores)
    plt.imshow(image_with_boxes, interpolation='nearest')
    plt.show()
Exemplo n.º 13
0
def draw(model_body,
         class_names,
         anchors,
         image_set='val',
         weights_name='trained_stage_1.h5',
         out_path="output_images",
         save_all=True):
    '''
    Draw bounding boxes on image data
    '''
    if image_set == 'train':
        image_names = glob.glob("./DATA/00001/*.ppm")
        image_data = np.array([cv2.imread(image) for image in image_names])
        image_data = process_data(images=image_data, boxes=None)
        image_data = np.array(
            [np.expand_dims(image, axis=0) for image in image_data])
    elif image_set == 'val':
        image_names = glob.glob("./DATA/validation/*.ppm")
        image_data = np.array([cv2.imread(image) for image in image_names])
        image_data = process_data(images=image_data, boxes=None)
        image_data = np.array(
            [np.expand_dims(image, axis=0) for image in image_data])
    elif image_set == 'all':
        image_names = glob.glob("./DATA/00001/*.ppm")
        image_data = np.array([cv2.imread(image) for image in image_names])
        image_data = process_data(images=image_data, boxes=None)
        image_data = np.array(
            [np.expand_dims(image, axis=0) for image in image_data])
    else:
        ValueError("draw argument image_set must be 'train', 'val', or 'all'")
    # model.load_weights(weights_name)
    print(image_data.shape)
    model_body.load_weights(weights_name)

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=0.07,
                                       iou_threshold=0.0)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    if not os.path.exists(out_path):
        os.makedirs(out_path)
        #running the session to get boxes ,scores and corresponding classes to be used in drawing boxes.
    for i in range(len(image_data)):
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_data.shape[2], image_data.shape[3]],
                K.learning_phase(): 0
            })
        print('Found {} boxes for image.'.format(len(out_boxes)))
        print(out_boxes)

        # Plot image with predicted boxes.
        image_with_boxes = draw_boxes(image_data[i][0], out_boxes, out_classes,
                                      class_names, out_scores)
        # Save the image:
        if save_all or (
                len(out_boxes) > 0
        ):  # if no boxes are found so don't save the image.(you can change >0 to >=0 to save also images with no boxes to be able to see images that your model couldn't find the objects in it)
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(os.path.join(
                out_path,
                str(i) +
                '.png'))  #Save images to output path passed in arguments.
Exemplo n.º 14
0
def predict(model_body,
            class_names,
            anchors,
            image_data,
            weights_name,
            dir_list,
            non_best_sup=False,
            results_dir='results',
            save=False,
            training=False):
    '''Runs the detection algorithm on image_data.

    Inputs:
        model_body: the body of the model as returned by the create_model function.
        class_names: a list containing the class names.
        anchors: a np array containing the anchor boxes dimension.
        image_data: np array of shape (#images,side,side,channels) containing the images.
        dir_list: the image data names' list.
        weights_name: the name of the weight file that we want to load.
        non_best_sup: wether or not to perform non best suppression during predictions.
        results_dir: directory where the results will be saved.
        save: wether or not to save the output images.

    Returns:
        boxes_dict: the dictionnary containing the bounding boxes and scores.
                    boxes_dict = {filename : {'bladder': [[xA,yA,xB,yB,score],[...]],
                                              'rectum': [..],
                                              'prostate': [..]},
                                  filename : {...},...}
    '''

    # Creating missing directories
    if not os.path.exists(results_dir):
        os.makedirs(results_dir)
    if not os.path.exists(os.path.join(results_dir, 'images')):
        os.makedirs(os.path.join(results_dir, 'images'))
    if not os.path.exists(os.path.join(results_dir, 'predictions')):
        os.makedirs(os.path.join(results_dir, 'predictions'))

    # Loading image data in the right format
    image_data = np.array(
        [np.expand_dims(image, axis=0) for image in image_data])

    # Loading weights
    if training:
        model_body.load_weights(
            os.path.join(results_dir, 'models', weights_name))
    else:
        model_body.load_weights(os.path.join('models', weights_name))

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=0.07,
                                       iou_threshold=0.0)

    # Dictionnary to export the predicted bounding boxes
    boxes_dict = {}

    # Run prediction
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.

    for i in range(len(image_data)):
        print('predicting boxes for {}'.format(dir_list[i]))
        out_boxes, out_scores, out_classes = sess.run(
            [boxes, scores, classes],
            feed_dict={
                model_body.input: image_data[i],
                input_image_shape: [image_data.shape[2], image_data.shape[3]],
                K.learning_phase(): 0
            })

        if non_best_sup:
            (new_out_boxes, new_out_classes,
             new_out_scores) = non_best_suppression(out_boxes, out_classes,
                                                    out_scores)

            # Plot image with predicted boxes.
            if save:
                image_with_boxes = draw_boxes(image_data[i][0], new_out_boxes,
                                              new_out_classes, class_names,
                                              new_out_scores)
                image = PIL.Image.fromarray(image_with_boxes)
                image.save(
                    os.path.join(results_dir, 'images', dir_list[i] + '.png'))
        elif save:
            # Plot image with predicted boxes.
            image_with_boxes = draw_boxes(image_data[i][0], out_boxes,
                                          out_classes, class_names, out_scores)
            image = PIL.Image.fromarray(image_with_boxes)
            image.save(
                os.path.join(results_dir, 'images', dir_list[i] + '.png'))

        # Updates dictionnary
        boxes_dict.update({dir_list[i]: {}})
        for c in class_names:
            boxes_dict[dir_list[i]].update({c: []})
        for j in range(len(out_boxes)):
            organ = class_names[out_classes[j]]
            new_box = list(out_boxes[j])
            new_box.append(out_scores[j])
            boxes_dict[dir_list[i]][organ].append(new_box)

    # Saving boxes
    return boxes_dict
Exemplo n.º 15
0
def _main():
    voc_path = os.path.expanduser(
        '/Users/ss/Data/VOCdevkit/pascal_voc_07_07.hdf5')
    classes_path = os.path.expanduser('model_data/pascal_classes.txt')

    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]

    voc = h5py.File(voc_path, 'r')
    image = PIL.Image.open(io.BytesIO(voc['train/images'][28]))
    orig_size = np.array([image.width, image.height])
    orig_size = np.expand_dims(orig_size, axis=0)

    # Image preprocessing.
    image = image.resize((416, 416), PIL.Image.BICUBIC)
    image_data = np.array(image, dtype=np.float)
    image_data /= 255.

    # Box preprocessing.
    # Original boxes stored as 1D list of class, x_min, y_min, x_max, y_max.
    boxes = voc['train/boxes'][28]
    boxes = boxes.reshape((-1, 5))
    # Get extents as y_min, x_min, y_max, x_max, class for comparision with
    # model output.
    boxes_extents = boxes[:, [2, 1, 4, 3, 0]]

    # Get box parameters as x_center, y_center, box_width, box_height, class.
    boxes_xy = 0.5 * (boxes[:, 3:5] + boxes[:, 1:3])
    boxes_wh = boxes[:, 3:5] - boxes[:, 1:3]
    boxes_xy = boxes_xy / orig_size
    boxes_wh = boxes_wh / orig_size
    boxes = np.concatenate((boxes_xy, boxes_wh, boxes[:, 0:1]), axis=1)

    # Precompute detectors_mask and matching_true_boxes for training.
    # Detectors mask is 1 for each spatial position in the final conv layer and
    # anchor that should be active for the given boxes and 0 otherwise.
    # Matching true boxes gives the regression targets for the ground truth box
    # that caused a detector to be active or 0 otherwise.
    anchors = COCO_ANCHORS
    detectors_mask_shape = (13, 13, 5, 1)
    matching_boxes_shape = (13, 13, 5, 5)
    detectors_mask, matching_true_boxes = preprocess_true_boxes(
        boxes, anchors, [416, 416])

    # Create model input layers.
    image_input = Input(shape=(416, 416, 3))
    boxes_input = Input(shape=(None, 5))
    detectors_mask_input = Input(shape=detectors_mask_shape)
    matching_boxes_input = Input(shape=matching_boxes_shape)

    print(boxes)
    print(boxes_extents)
    print(np.where(detectors_mask == 1)[:-1])
    print(matching_true_boxes[np.where(detectors_mask == 1)[:-1]])

    # Create model body.
    model_body = yolo_body(image_input, len(anchors), len(class_names))
    model_body = Model(image_input, model_body.output)
    # Place model loss on CPU to reduce GPU memory usage.
    with tf.device('/cpu:0'):
        # TODO: Replace Lambda with custom Keras layer for loss.
        model_loss = Lambda(yolo_loss,
                            output_shape=(1, ),
                            name='yolo_loss',
                            arguments={
                                'anchors': anchors,
                                'num_classes': len(class_names)
                            })([
                                model_body.output, boxes_input,
                                detectors_mask_input, matching_boxes_input
                            ])
    model = Model(
        [image_input, boxes_input, detectors_mask_input, matching_boxes_input],
        model_loss)
    model.compile(
        optimizer='adam', loss={
            'yolo_loss': lambda y_true, y_pred: y_pred
        })  # This is a hack to use the custom loss function in the last layer.

    # Add batch dimension for training.
    image_data = np.expand_dims(image_data, axis=0)
    boxes = np.expand_dims(boxes, axis=0)
    detectors_mask = np.expand_dims(detectors_mask, axis=0)
    matching_true_boxes = np.expand_dims(matching_true_boxes, axis=0)

    num_steps = 1000
    # TODO: For full training, put preprocessing inside training loop.
    # for i in range(num_steps):
    #     loss = model.train_on_batch(
    #         [image_data, boxes, detectors_mask, matching_true_boxes],
    #         np.zeros(len(image_data)))

    model.fit([image_data, boxes, detectors_mask, matching_true_boxes],
              np.zeros(len(image_data)),
              batch_size=1,
              epochs=num_steps)
    model.save_weights('overfit_weights.h5')

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=.3,
                                       iou_threshold=.9)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            model_body.input: image_data,
            input_image_shape: [image.size[1], image.size[0]],
            K.learning_phase(): 0
        })
    print('Found {} boxes for image.'.format(len(out_boxes)))
    print(out_boxes)

    # Plot image with predicted boxes.
    image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes,
                                  class_names, out_scores)
    plt.imshow(image_with_boxes, interpolation='nearest')
    plt.show()
Exemplo n.º 16
0
def _main(args):
    #訓練データセットに対する情報の読み込みfrom voc_to_hdf5
    voc_path = os.path.expanduser(args.data_path)
    #ラベル情報.txtへのpath
    classes_path = os.path.expanduser(args.classes_path)
    #アスペクト比の格納?
    anchors_path = os.path.expanduser(args.anchors_path)

    #クラス名のリストを作成
    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]

    if os.path.isfile(anchors_path):
        with open(anchors_path) as f:
            anchors = f.readline()
            anchors = [float(x) for x in anchors.split(',')]
            anchors = np.array(anchors).reshape(-1, 2)
    else:
        anchors = YOLO_ANCHORS

    voc = h5py.File(voc_path, 'r')
    image = PIL.Image.open(io.BytesIO(voc['test/images'][0]))
    orig_size = np.array([image.width, image.height])
    orig_size = np.expand_dims(orig_size, axis=0)

    # Image preprocessing.
    image = image.resize((416, 416), PIL.Image.BICUBIC)
    image_data = np.array(image, dtype=np.float)
    image_data /= 255.

    # Box preprocessing.
    # Original boxes stored as 1D list of class, x_min, y_min, x_max, y_max.
    boxes = voc['test/boxes'][0]
    boxes = boxes.reshape((-1, 5))
    # Get extents as y_min, x_min, y_max, x_max, class for comparision with
    # model output.
    boxes_extents = boxes[:, [2, 1, 4, 3, 0]]

    # Get box parameters as x_center, y_center, box_width, box_height, class.
    boxes_xy = 0.5 * (boxes[:, 3:5] + boxes[:, 1:3])
    boxes_wh = boxes[:, 3:5] - boxes[:, 1:3]
    boxes_xy = boxes_xy / orig_size
    boxes_wh = boxes_wh / orig_size
    #annotation部分のxy座標、幅
    boxes = np.concatenate((boxes_xy, boxes_wh, boxes[:, 0:1]), axis=1)

    # Precompute detectors_mask and matching_true_boxes for training.
    # Detectors mask is 1 for each spatial position in the final conv layer and
    # anchor that should be active for the given boxes and 0 otherwise.
    # Matching true boxes gives the regression targets for the ground truth box
    # that caused a detector to be active or 0 otherwise.
    detectors_mask_shape = (13, 13, 5, 1)
    matching_boxes_shape = (13, 13, 5, 5)
    detectors_mask, matching_true_boxes = preprocess_true_boxes(
        boxes, anchors, [416, 416])

    # Create model input layers.
    image_input = Input(shape=(416, 416, 3))
    boxes_input = Input(shape=(None, 5))
    detectors_mask_input = Input(shape=detectors_mask_shape)
    matching_boxes_input = Input(shape=matching_boxes_shape)

    print('Boxes:')
    print(boxes)
    print('Box corners:')
    print(boxes_extents)
    print('Active detectors:')
    print(np.where(detectors_mask == 1)[:-1])
    print('Matching boxes for active detectors:')
    print(matching_true_boxes[np.where(detectors_mask == 1)[:-1]])

    # Create model body.
    model_body = yolo_body(image_input, len(anchors), len(class_names))
    model_body = Model(image_input, model_body.output)
    # Place model loss on CPU to reduce GPU memory usage.
    with tf.device('/cpu:0'):
        # TODO: Replace Lambda with custom Keras layer for loss.
        model_loss = Lambda(yolo_loss,
                            output_shape=(1, ),
                            name='yolo_loss',
                            arguments={
                                'anchors': anchors,
                                'num_classes': len(class_names)
                            })([
                                model_body.output, boxes_input,
                                detectors_mask_input, matching_boxes_input
                            ])
    model = Model(
        [image_input, boxes_input, detectors_mask_input, matching_boxes_input],
        model_loss)
    model.compile(
        optimizer='adam', loss={
            'yolo_loss': lambda y_true, y_pred: y_pred
        })  # This is a hack to use the custom loss function in the last layer.

    # Add batch dimension for training.
    image_data = np.expand_dims(image_data, axis=0)
    boxes = np.expand_dims(boxes, axis=0)
    detectors_mask = np.expand_dims(detectors_mask, axis=0)
    matching_true_boxes = np.expand_dims(matching_true_boxes, axis=0)

    num_steps = 50

    #checkpoint = ModelCheckpoint('model1.h5', monitor='val_loss', verbose=1,
    #save_best_only=True, save_weights_only=False, mode='auto', period=1)
    # TODO: For full training, put preprocessing inside training loop.
    # for i in range(num_steps):
    #     loss = model.train_on_batch(
    #         [image_data, boxes, detectors_mask, matching_true_boxes],
    #         np.zeros(len(image_data)))
    model.fit(
        [image_data, boxes, detectors_mask, matching_true_boxes],
        np.zeros(len(image_data)),
        batch_size=1,
        epochs=num_steps,
        #callbacks=[checkpoint]
    )
    model.save('model.h5')
    model.save_weights('overfit_weights.h5')

    #model_json_str = model.to_json()
    #open('mlp_model.json', 'w').write(model_json_str)
    #model.save_weights('mlp_weights.h5');

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(yolo_outputs,
                                       input_image_shape,
                                       score_threshold=.3,
                                       iou_threshold=.9)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            model_body.input: image_data,
            input_image_shape: [image.size[1], image.size[0]],
            K.learning_phase(): 0
        })
    print('Found {} boxes for image.'.format(len(out_boxes)))
    print(out_boxes)

    # Plot image with predicted boxes.
    image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes,
                                  class_names, out_scores)
    plt.imshow(image_with_boxes, interpolation='nearest')
    plt.show()
Exemplo n.º 17
0
def _main(args):
    voc_path = os.path.expanduser(args.data_path)
    classes_path = os.path.expanduser(args.classes_path)
    anchors_path = os.path.expanduser(args.anchors_path)

    with open(classes_path) as f:
        class_names = f.readlines()
    class_names = [c.strip() for c in class_names]

    if os.path.isfile(anchors_path):
        with open(anchors_path) as f:
            anchors = f.readline()
            anchors = [float(x) for x in anchors.split(',')]
            anchors = np.array(anchors).reshape(-1, 2)
    else:
        anchors = YOLO_ANCHORS

    voc = h5py.File(voc_path, 'r')
    image = PIL.Image.open(io.BytesIO(voc['train/images'][28]))
    orig_size = np.array([image.width, image.height])
    orig_size = np.expand_dims(orig_size, axis=0)

    # Image preprocessing.
    image = image.resize((416, 416), PIL.Image.BICUBIC)
    image_data = np.array(image, dtype=np.float)
    image_data /= 255.

    # Box preprocessing.
    # Original boxes stored as 1D list of class, x_min, y_min, x_max, y_max.
    boxes = voc['train/boxes'][28]
    boxes = boxes.reshape((-1, 5))
    # Get extents as y_min, x_min, y_max, x_max, class for comparision with
    # model output.
    boxes_extents = boxes[:, [2, 1, 4, 3, 0]]

    # Get box parameters as x_center, y_center, box_width, box_height, class.
    boxes_xy = 0.5 * (boxes[:, 3:5] + boxes[:, 1:3])
    boxes_wh = boxes[:, 3:5] - boxes[:, 1:3]
    boxes_xy = boxes_xy / orig_size
    boxes_wh = boxes_wh / orig_size
    boxes = np.concatenate((boxes_xy, boxes_wh, boxes[:, 0:1]), axis=1)

    # Precompute detectors_mask and matching_true_boxes for training.
    # Detectors mask is 1 for each spatial position in the final conv layer and
    # anchor that should be active for the given boxes and 0 otherwise.
    # Matching true boxes gives the regression targets for the ground truth box
    # that caused a detector to be active or 0 otherwise.
    detectors_mask_shape = (13, 13, 5, 1)
    matching_boxes_shape = (13, 13, 5, 5)
    detectors_mask, matching_true_boxes = preprocess_true_boxes(boxes, anchors,
                                                                [416, 416])

    # Create model input layers.
    image_input = Input(shape=(416, 416, 3))
    boxes_input = Input(shape=(None, 5))
    detectors_mask_input = Input(shape=detectors_mask_shape)
    matching_boxes_input = Input(shape=matching_boxes_shape)

    print('Boxes:')
    print(boxes)
    print('Box corners:')
    print(boxes_extents)
    print('Active detectors:')
    print(np.where(detectors_mask == 1)[:-1])
    print('Matching boxes for active detectors:')
    print(matching_true_boxes[np.where(detectors_mask == 1)[:-1]])

    # Create model body.
    model_body = yolo_body(image_input, len(anchors), len(class_names))
    model_body = Model(image_input, model_body.output)
    # Place model loss on CPU to reduce GPU memory usage.
    with tf.device('/cpu:0'):
        # TODO: Replace Lambda with custom Keras layer for loss.
        model_loss = Lambda(
            yolo_loss,
            output_shape=(1, ),
            name='yolo_loss',
            arguments={'anchors': anchors,
                       'num_classes': len(class_names)})([
                           model_body.output, boxes_input,
                           detectors_mask_input, matching_boxes_input
                       ])
    model = Model(
        [image_input, boxes_input, detectors_mask_input,
         matching_boxes_input], model_loss)
    model.compile(
        optimizer='adam', loss={
            'yolo_loss': lambda y_true, y_pred: y_pred
        })  # This is a hack to use the custom loss function in the last layer.

    # Add batch dimension for training.
    image_data = np.expand_dims(image_data, axis=0)
    boxes = np.expand_dims(boxes, axis=0)
    detectors_mask = np.expand_dims(detectors_mask, axis=0)
    matching_true_boxes = np.expand_dims(matching_true_boxes, axis=0)

    num_steps = 1000
    # TODO: For full training, put preprocessing inside training loop.
    # for i in range(num_steps):
    #     loss = model.train_on_batch(
    #         [image_data, boxes, detectors_mask, matching_true_boxes],
    #         np.zeros(len(image_data)))
    model.fit([image_data, boxes, detectors_mask, matching_true_boxes],
              np.zeros(len(image_data)),
              batch_size=1,
              epochs=num_steps)
    model.save_weights('overfit_weights.h5')

    # Create output variables for prediction.
    yolo_outputs = yolo_head(model_body.output, anchors, len(class_names))
    input_image_shape = K.placeholder(shape=(2, ))
    boxes, scores, classes = yolo_eval(
        yolo_outputs, input_image_shape, score_threshold=.3, iou_threshold=.9)

    # Run prediction on overfit image.
    sess = K.get_session()  # TODO: Remove dependence on Tensorflow session.
    out_boxes, out_scores, out_classes = sess.run(
        [boxes, scores, classes],
        feed_dict={
            model_body.input: image_data,
            input_image_shape: [image.size[1], image.size[0]],
            K.learning_phase(): 0
        })
    print('Found {} boxes for image.'.format(len(out_boxes)))
    print(out_boxes)

    # Plot image with predicted boxes.
    image_with_boxes = draw_boxes(image_data[0], out_boxes, out_classes,
                                  class_names, out_scores)
    plt.imshow(image_with_boxes, interpolation='nearest')
    plt.show()