Ejemplo n.º 1
0
def yolo_eval(yolo_outputs,
              anchors,
              num_classes,
              image_shape,
              max_boxes=20,
              score_threshold=.6,
              iou_threshold=.5):
    """Evaluate YOLO model on given input and return filtered boxes."""
    num_layers = len(yolo_outputs)
    anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] if num_layers == 3 else [[
        3, 4, 5
    ], [1, 2, 3]]  # default setting
    input_shape = K.shape(yolo_outputs[0])[1:3] * 32
    boxes = []
    box_scores = []
    for l in range(num_layers):
        _boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l],
                                                    anchors[anchor_mask[l]],
                                                    num_classes, input_shape,
                                                    image_shape)
        boxes.append(_boxes)
        box_scores.append(_box_scores)
    boxes = K.concatenate(boxes, axis=0)
    box_scores = K.concatenate(box_scores, axis=0)

    mask = box_scores >= score_threshold
    max_boxes_tensor = K.constant(max_boxes, dtype='int32')
    boxes_ = []
    scores_ = []
    classes_ = []
    for c in range(num_classes):
        # TODO: use keras backend instead of tf.
        class_boxes = tf.boolean_mask(boxes, mask[:, c])
        class_box_scores = tf.boolean_mask(box_scores[:, c], mask[:, c])
        nms_index = tf.image.non_max_suppression(class_boxes,
                                                 class_box_scores,
                                                 max_boxes_tensor,
                                                 iou_threshold=iou_threshold)
        class_boxes = K.gather(class_boxes, nms_index)
        class_box_scores = K.gather(class_box_scores, nms_index)
        classes = K.ones_like(class_box_scores, 'int32') * c
        boxes_.append(class_boxes)
        scores_.append(class_box_scores)
        classes_.append(classes)
    boxes_ = K.concatenate(boxes_, axis=0)
    scores_ = K.concatenate(scores_, axis=0)
    classes_ = K.concatenate(classes_, axis=0)

    return boxes_, scores_, classes_
Ejemplo n.º 2
0
def yolo_eval_raw_scores(
    yolo_outputs,
    anchors,
    num_classes,
    image_shape,
    score_threshold=.6,
):
    """Evaluate YOLO model on given input and return filtered boxes."""
    num_layers = len(yolo_outputs)
    anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] if num_layers == 3 else [[
        3, 4, 5
    ], [1, 2, 3]]  # default setting
    input_shape = K.shape(yolo_outputs[0])[1:3] * 32
    boxes = []
    box_scores = []
    for l in range(num_layers):
        _boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l],
                                                    anchors[anchor_mask[l]],
                                                    num_classes, input_shape,
                                                    image_shape)
        boxes.append(_boxes)
        box_scores.append(_box_scores)
    boxes = K.concatenate(boxes, axis=0)
    box_scores = K.concatenate(box_scores, axis=0)

    mask = tf.reduce_max(box_scores, reduction_indices=[1]) >= score_threshold
    boxes = tf.boolean_mask(boxes, mask)
    box_scores = tf.boolean_mask(box_scores, mask)

    #	mask = box_scores >= score_threshold
    #	boxes_ = []
    #	scores_ = []
    #	classes_ = []
    #	for c in range(num_classes):
    #		class_boxes = tf.boolean_mask(boxes, mask[:, c])
    #		class_box_scores = tf.boolean_mask(box_scores[:, c], mask[:, c])
    #		classes = K.ones_like(class_box_scores, 'int32') * c
    #		boxes_.append(class_boxes)
    #		scores_.append(class_box_scores)
    #		classes_.append(classes)
    #	boxes_ = K.concatenate(boxes_, axis=0)
    #	scores_ = K.concatenate(scores_, axis=0)
    #	classes_ = K.concatenate(classes_, axis=0)

    #	return boxes_, scores_, classes_

    return boxes, box_scores, None
 def call(self, inputs, **kwargs):
     """Evaluate YOLO model on given input and return filtered boxes."""
     yolo_outputs = inputs[0:-1]
     input_image_shape = K.squeeze(inputs[-1], axis=0)
     num_layers = len(yolo_outputs)
     anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] if num_layers == 3 else [[3, 4, 5],
                                                                              [1, 2, 3]]  # default setting
     input_shape = K.shape(yolo_outputs[0])[1:3] * 32
     boxes = []
     box_scores = []
     for l in range(num_layers):
         _boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l], self.anchors[anchor_mask[l]], self.num_classes,
                                                     input_shape, input_image_shape)
         boxes.append(_boxes)
         box_scores.append(_box_scores)
     boxes = K.concatenate(boxes, axis=0)
     box_scores = K.concatenate(box_scores, axis=0)
     return [boxes, box_scores]
Ejemplo n.º 4
0
    def call(self, inputs, **kwargs):
        """Evaluate YOLO model on given input and return filtered boxes."""
        yolo_outputs, input_image_shape = (inputs[0:3], inputs[3])
        num_layers = len(yolo_outputs)
        anchor_mask = [[6, 7, 8], [3, 4, 5], [
            0, 1, 2
        ]] if num_layers == 3 else [[3, 4, 5], [1, 2, 3]]  # default setting
        input_shape = K.shape(yolo_outputs[0])[1:3] * 32
        boxes = []
        box_scores = []
        for l in range(num_layers):
            _boxes, _box_scores = yolo_boxes_and_scores(
                yolo_outputs[l], self.anchors[anchor_mask[l]],
                self.num_classes, input_shape, input_image_shape)
            boxes.append(_boxes)
            box_scores.append(_box_scores)
        boxes = K.concatenate(boxes, axis=0)
        box_scores = K.concatenate(box_scores, axis=0)

        mask = box_scores >= self.score_threshold
        max_boxes_tensor = K.constant(self.max_boxes, dtype='int32')
        boxes_ = []
        scores_ = []
        classes_ = []
        for c in range(self.num_classes):
            class_boxes = tf.boolean_mask(boxes, mask[:, c])
            class_box_scores = tf.boolean_mask(box_scores[:, c], mask[:, c])
            nms_index = tf.image.non_max_suppression(
                class_boxes,
                class_box_scores,
                max_boxes_tensor,
                iou_threshold=self.iou_threshold)
            class_boxes = K.gather(class_boxes, nms_index)
            class_box_scores = K.gather(class_box_scores, nms_index)
            classes = K.ones_like(class_box_scores, 'int32') * c
            boxes_.append(class_boxes)
            scores_.append(class_box_scores)
            classes_.append(classes)
        boxes_ = K.concatenate(boxes_, axis=0)
        scores_ = K.concatenate(scores_, axis=0)
        classes_ = K.concatenate(classes_, axis=0)

        return [boxes_, scores_, classes_]
Ejemplo n.º 5
0
def yolo_eval_raw_scores_feats(yolo_outputs,
                               anchors,
                               num_classes,
                               image_shape,
                               feat_layers,
                               max_boxes=20,
                               score_threshold=.6,
                               iou_threshold=.5):
    """Evaluate YOLO model on given input and return filtered boxes."""
    num_layers = len(yolo_outputs) - len(feat_layers)
    anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] if num_layers == 3 else [[
        3, 4, 5
    ], [1, 2, 3]]  # default setting
    input_shape = K.shape(yolo_outputs[0])[1:3] * 32
    boxes = []
    box_scores = []
    for l in range(num_layers):
        _boxes, _box_scores = yolo_boxes_and_scores(yolo_outputs[l],
                                                    anchors[anchor_mask[l]],
                                                    num_classes, input_shape,
                                                    image_shape)
        boxes.append(_boxes)
        box_scores.append(_box_scores)
    boxes = K.concatenate(boxes, axis=0)
    box_scores = K.concatenate(box_scores, axis=0)

    mask = tf.reduce_max(box_scores, reduction_indices=[1]) >= score_threshold
    boxes = tf.boolean_mask(boxes, mask)
    box_scores = tf.boolean_mask(box_scores, mask)

    max_boxes_tensor = K.constant(max_boxes, dtype='int32')
    nms_index = tf.image.non_max_suppression(boxes,
                                             tf.reduce_max(
                                                 box_scores,
                                                 reduction_indices=[1]),
                                             max_boxes_tensor,
                                             iou_threshold=iou_threshold)

    boxes = K.gather(boxes, nms_index)
    box_scores = K.gather(box_scores, nms_index)

    return boxes, box_scores, yolo_outputs[num_layers:]
Ejemplo n.º 6
0
features = tf.convert_to_tensor(net_out_reshaped)

num_layers = len(predictions)
anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]] if num_layers == 3 else [[
    3, 4, 5
], [1, 2, 3]]  # default setting

all_boxes = []
for l in range(num_layers):
    net_out = predictions[l]
    n_shape = net_out.shape
    net_out_reshaped = np.reshape(
        net_out, (1, n_shape[1], n_shape[2], 3, 5 + nb_classes))
    features = tf.convert_to_tensor(net_out_reshaped)
    x = tf.Session().run(
        yolo_boxes_and_scores(features, anchors[anchor_mask[0]], nb_classes,
                              model_image_size, org_image_shape))
    boxes = np.concatenate(
        [x[0],
         np.reshape(x[2][0], (n_shape[1] * n_shape[1] * 3, 1)), x[1]],
        axis=1)
    all_boxes.extend(boxes)
boxes_, scores_, classes_ = postprocess_boxes_tf(all_boxes, score_threshold=.3)
image = draw_boxes_tf(boxes_, scores_, classes_, classes, org_image)
image.show()

#########################################################################################################
bboxes = postprocess_boxes(all_boxes, org_image, model_image_size[0], 0.3)
bboxes = nms(bboxes, 0.45, method='nms')
image = draw_bbox(org_image, bboxes, classes)
image = fromarray(image)
image.show()
Ejemplo n.º 7
0
    def detect_image(self):
        start = timer()
        image_path = os.path.expanduser(self.image_path)
        image = Image.open(image_path)
        if self.model_image_size != (None, None):
            assert self.model_image_size[
                0] % 32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[
                1] % 32 == 0, 'Multiples of 32 required'
            boxed_image = letterbox_image(
                image, tuple(reversed(self.model_image_size)))
        else:
            new_image_size = (image.width - (image.width % 32),
                              image.height - (image.height % 32))
            boxed_image = letterbox_image(image, new_image_size)
        image_data = np.array(boxed_image, dtype='float32')

        image_data /= 255.
        image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
        return self.boxes, self.scores, self.classes
        print(np.shape(out_boxes), np.shape(out_scores), np.shape(out_classes))

        print(out_boxes[0], out_scores[0], out_classes)

        font = ImageFont.truetype(
            '/usr/share/fonts/truetype/freefont/FreeMono.ttf', 15)
        thickness = (image.size[0] + image.size[1]) // 300

        for i, c in reversed(list(enumerate(out_classes))):
            print("inside")
            predicted_class = self.class_names[c]
            box = out_boxes[i]
            print(box)
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print(end - start)
        return image
        exit()
        #yhat = yolo_boxes_and_scores(yhat[], self.anchors, num_classes, K.shape(yhat[0])[1:3] * 32, self.input_image_shape)
        #print(np.shape(yhat[0][0]))
        anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
        total_boxes = []
        for i in range(len(yhat)):

            _boxes, _box_scores = yolo_boxes_and_scores(
                yhat[i], self.anchors, num_classes,
                K.shape(yhat[0])[1:3] * 32, self.input_image_shape)

            netout = yhat[i][0]
            grid_h, grid_w = netout.shape[:2]
            nb_box = 3
            netout = netout.reshape((grid_h, grid_w, nb_box, -1))
            nb_class = netout.shape[-1] - 5
            boxes = []
            obj_thresh = 0.5
            anchors = [[10, 13, 16, 30, 33, 23], [116, 90, 156, 198, 373, 326],
                       [30, 61, 62, 45, 59, 119]]
            for i in range(grid_h * grid_w):
                row = i / grid_w
                col = i % grid_w
                for b in range(nb_box):
                    # 4th element is objectness score
                    objectness = netout[int(row)][int(col)][b][4]
                    if (objectness.all() <= obj_thresh): continue
                    # first 4 elements are x, y, w, and h
                    x, y, w, h = netout[int(row)][int(col)][b][:4]
                    print(x, y, w, h)
                    print(anchors[2 * b + 0])
                    x = (col +
                         x) / grid_w  # center position, unit: image width
                    y = (row +
                         y) / grid_h  # center position, unit: image height
                    w = np.array(
                        anchors[2 * b + 0],
                        np.float32) * np.exp(w) / 416.  # unit: image width
                    h = np.array(
                        anchors[2 * b + 1],
                        np.float32) * np.exp(h) / 416.  # unit: image height
                    # last elements are class probabilities
                    print(x, y, w, h)
                    classes = netout[int(row)][col][b][5:]
                    box = BoundBox(x - w / 2, y - h / 2, x + w / 2, y + h / 2,
                                   objectness, classes)
                    print(x - w / 2, y - h / 2, x + w / 2, y + h / 2)
                    boxes.append(box)
            # decode the output of the network
            total_boxes += boxes

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.size[1], image.size[0]],
                tf.compat.v1.keras.backend.learning_phase(): 0
            })
        #y = self.yolo_model.predict(image_data)
        #self.boxes, self.scores, self.classes = y
        #out_boxes, out_scores, out_classes = self.boxes, self.scores, self.classes
        font = ImageFont.truetype(
            '/usr/share/fonts/truetype/freefont/FreeMono.ttf', 15)
        thickness = (image.size[0] + image.size[1]) // 300
        out_boxes = [y[0][..., :4], y[1][..., :4], y[2][..., :4]]
        out_scores = [y[0][..., 4], y[1][..., 4], y[2][..., 4]]
        out_classes = [y[0][..., 5], y[1][..., 5], y[2][..., 5]]

        for i, c in reversed(list(enumerate(out_classes))):
            print(i)
            #predicted_class = self.class_names[c]
            box = out_boxes[i]
            print(box)
            score = out_scores[i]

            label = '{} {:.2f}'.format(predicted_class, score)
            draw = ImageDraw.Draw(image)
            label_size = draw.textsize(label, font)

            top, left, bottom, right = box
            top = max(0, np.floor(top + 0.5).astype('int32'))
            left = max(0, np.floor(left + 0.5).astype('int32'))
            bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
            right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
            print(label, (left, top), (right, bottom))

            if top - label_size[1] >= 0:
                text_origin = np.array([left, top - label_size[1]])
            else:
                text_origin = np.array([left, top + 1])

            # My kingdom for a good redistributable image drawing library.
            for i in range(thickness):
                draw.rectangle([left + i, top + i, right - i, bottom - i],
                               outline=self.colors[c])
            draw.rectangle(
                [tuple(text_origin),
                 tuple(text_origin + label_size)],
                fill=self.colors[c])
            draw.text(text_origin, label, fill=(0, 0, 0), font=font)
            del draw

        end = timer()
        print(end - start)
        return image