コード例 #1
0
def pridect(origin_img, images, model):
    result = model.predict(images, batch_size=1)
    print(result[0].shape)
    print(result[1].shape)
    print(result[2].shape)
    anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
    # num_layers = len(anchors)//3 # default setting
    # yolo_outputs = args[:num_layers]
    # y_true = args[num_layers:]
    # input_shape = K.cast(K.shape( result[0])[1:3] * 32, K.dtype(y_true[0]))
    print(K.shape(result[0])[1:3] * 32)
    input_shape = K.shape(result[0])[1:3] * 32
    colors = yolo_utils.generate_colors(class_names)
    for i in range(0, 3):
        box_xy, box_wh, box_confidence, box_class_probs = yolo_head(
            result[i], anchors[anchor_mask[i]], 20, input_shape)
        scores, boxes, classes = yolo_eval(
            box_xy,
            box_wh,
            box_confidence,
            box_class_probs,
            image_shape=(float(origin_img.size[1]), float(origin_img.size[0])))
        print(scores)
        print(boxes)
        print(classes)
        yolo_utils.draw_boxes(origin_img, scores, boxes, classes, class_names,
                              colors)
    # print("scores.shape = " + str(scores.shape))
    # print("boxes.shape = " + str(boxes.shape))
    # print("classes.shape = " + str(classes.shape))

    plt.imshow(origin_img)  # 显示图片
    plt.axis('off')  # 不显示坐标轴
    plt.show()
コード例 #2
0
def pridect(origin_img, images, model):
    result = model.predict(images, batch_size=1)
    print(result[0].shape)
    print(result[1].shape)
    print(result[2].shape)
    anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
    # num_layers = len(anchors)//3 # default setting
    # yolo_outputs = args[:num_layers]
    # y_true = args[num_layers:]
    # input_shape = K.cast(K.shape( result[0])[1:3] * 32, K.dtype(y_true[0]))
    print(K.shape(result[0])[1:3] * 32)
    input_shape = K.shape(result[0])[1:3] * 32
    colors = yolo_utils.generate_colors(class_names)

    box_xy, box_wh, box_confidence, box_class_probs = yolo_head(
        result[0], anchors[anchor_mask[0]], 20, input_shape)
    scores, boxes, classes = yolo_eval(box_xy,
                                       box_wh,
                                       box_confidence,
                                       box_class_probs,
                                       image_shape=(float(origin_img.size[1]),
                                                    float(origin_img.size[0])))
    for i in range(1, 3):
        box_xy, box_wh, box_confidence, box_class_probs = yolo_head(
            result[i], anchors[anchor_mask[i]], 20, input_shape)
        tmp_scores, tmp_boxes, tmp_classes = yolo_eval(
            box_xy,
            box_wh,
            box_confidence,
            box_class_probs,
            image_shape=(float(origin_img.size[1]), float(origin_img.size[0])))
        scores = tf.concat([scores, tmp_scores], axis=0)
        boxes = tf.concat([boxes, tmp_boxes], axis=0)
        classes = tf.concat([classes, tmp_classes], axis=0)
        # yolo_utils.draw_boxes(origin_img, scores, boxes, classes, class_names, colors)

    #使用非最大值抑制
    scores, boxes, classes = yolo_non_max_suppression(scores, boxes, classes,
                                                      15, 0.5)
    yolo_utils.draw_boxes(origin_img, scores, boxes, classes, class_names,
                          colors)
    # print("scores.shape = " + str(scores.shape))
    # print("boxes.shape = " + str(boxes.shape))
    # print("classes.shape = " + str(classes.shape))

    return origin_img
コード例 #3
0
def yolo_boxes_and_scores(features, anchors, num_classes, input_shape,
                          image_shape):
    box_xy, box_wh, box_confidence, box_class_probs = yolo_head(
        features, anchors, num_classes, input_shape)

    boxes = yolo_correct_boxes(box_xy, box_wh, input_shape, image_shape)
    boxes = tf.reshape(boxes, [-1, 4])
    box_confidence = tf.squeeze(box_confidence)
    box_scores = box_confidence * box_class_probs
    box_scores = tf.reshape(box_scores, [-1, num_classes])
    return boxes, box_scores
コード例 #4
0
 def yolo_eval(self, out_puts, image_size):
     num_layers = len(out_puts)
     anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]
                    ] if num_layers == 3 else [[3, 4, 5], [0, 1, 2]]
     Result = []
     for i in range(num_layers):
         box_xy, box_wh, box_confidence, box_class_probs = yolo_head(
             out_puts[i],
             self.anchors[anchor_mask[i]],
             len(self.class_names),
             self.model_image_size,
             1,
         )
         box = self.yolo_boxes(
             box_xy[0], box_wh[0],
             np.array([self.model_image_size[1], self.model_image_size[0]]),
             image_size)
         box_score = box_confidence[0] * box_class_probs[0]
         # for x in range(shape[0]):
         #     for y in range(shape[2]):
         #         for z in range(shape[3]):
         #             BOXES.append(list(box[x,:,y,z]))
         #             SCORES.append(list(box_score[x,:,y,z]))
         box_re = np.reshape(box, [3, 4, -1])
         score_re = np.reshape(box_score, [3, len(self.class_names), -1])
         mask = score_re >= self.score
         mask_id = np.where(mask == True)
         score_result = score_re[mask_id]
         box_result = box_re[mask_id[0], :, mask_id[2]]
         for i in range(len(score_result)):
             Result.append([
                 mask_id[1][i], score_result[i], box_result[i][0],
                 box_result[i][1], box_result[i][2], box_result[i][3]
             ])
     if len(Result) >= 1:
         result_to_show = self.non_max_suppression(np.array(Result))
     else:
         result_to_show = []
     return result_to_show
コード例 #5
0
ファイル: emodel.py プロジェクト: Shathe/Keras-YOLO-v3
def yolo_loss(args, anchors, num_classes, loss_percs={}, ignore_thresh=.5):
    '''Return yolo_loss tensor composed by the loss and all its components

	Parameters
	----------
	yolo_outputs: list of tensor, the output of yolo_body or tiny_yolo_body
	y_true: list of array, the output of preprocess_true_boxes
	anchors: array, shape=(N, 2), wh
	num_classes: integer
	ignore_thresh: float, the iou threshold whether to ignore object confidence loss

	Returns
	-------
	loss: tensor, shape=(1,)

	'''
    num_layers = len(anchors) // 3  # default setting
    yolo_outputs = args[:num_layers]
    y_true = args[num_layers:]
    anchor_mask = [[6, 7, 8], [3, 4, 5], [0, 1, 2]
                   ] if num_layers == 3 else [[3, 4, 5], [1, 2, 3]]
    input_shape = K.cast(
        K.shape(yolo_outputs[0])[1:3] * 32, K.dtype(y_true[0]))
    grid_shapes = [
        K.cast(K.shape(yolo_outputs[l])[1:3], K.dtype(y_true[0]))
        for l in range(num_layers)
    ]
    loss = 0
    total_xy_loss, total_wh_loss, total_confidence_loss_obj, total_confidence_loss_noobj, total_class_loss = 0, 0, 0, 0, 0
    m = K.shape(yolo_outputs[0])[0]  # batch size, tensor
    mf = K.cast(m, K.dtype(yolo_outputs[0]))

    for l in range(num_layers):
        object_mask = y_true[l][..., 4:5]
        true_class_probs = y_true[l][..., 5:]

        grid, raw_pred, pred_xy, pred_wh = yolo_head(yolo_outputs[l],
                                                     anchors[anchor_mask[l]],
                                                     num_classes,
                                                     input_shape,
                                                     calc_loss=True)
        pred_box = K.concatenate([pred_xy, pred_wh])

        # Darknet raw box to calculate loss.
        raw_true_xy = y_true[l][..., :2] * grid_shapes[l][::-1] - grid
        raw_true_wh = K.log(y_true[l][..., 2:4] / anchors[anchor_mask[l]] *
                            input_shape[::-1])
        raw_true_wh = K.switch(object_mask, raw_true_wh,
                               K.zeros_like(raw_true_wh))  # avoid log(0)=-inf
        box_loss_scale = 2 - y_true[l][..., 2:3] * y_true[l][..., 3:4]

        # Find ignore mask, iterate over each of batch.
        ignore_mask = tf.TensorArray(K.dtype(y_true[0]),
                                     size=1,
                                     dynamic_size=True)
        object_mask_bool = K.cast(object_mask, 'bool')

        def loop_body(b, ignore_mask):
            true_box = tf.boolean_mask(y_true[l][b, ..., 0:4],
                                       object_mask_bool[b, ..., 0])
            iou = box_iou(pred_box[b], true_box)
            best_iou = K.max(iou, axis=-1)
            ignore_mask = ignore_mask.write(
                b, K.cast(best_iou < ignore_thresh, K.dtype(true_box)))
            return b + 1, ignore_mask

        _, ignore_mask = K.control_flow_ops.while_loop(lambda b, *args: b < m,
                                                       loop_body,
                                                       [0, ignore_mask])
        ignore_mask = ignore_mask.stack()
        ignore_mask = K.expand_dims(ignore_mask, -1)

        # K.binary_crossentropy is helpful to avoid exp overflow.
        #		xy_loss = object_mask * box_loss_scale * K.binary_crossentropy(raw_true_xy, raw_pred[...,0:2], from_logits=True)
        xy_loss = object_mask * box_loss_scale * 0.5 * K.square(
            raw_true_xy - raw_pred[..., 0:2])
        wh_loss = object_mask * box_loss_scale * 0.5 * K.square(
            raw_true_wh - raw_pred[..., 2:4])
        confidence_loss_obj = object_mask * K.binary_crossentropy(
            object_mask, raw_pred[..., 4:5], from_logits=True)
        confidence_loss_noobj = (1 - object_mask) * K.binary_crossentropy(
            object_mask, raw_pred[..., 4:5], from_logits=True) * ignore_mask
        #		confidence_loss = object_mask * K.binary_crossentropy(object_mask, raw_pred[...,4:5], from_logits=True)+ \
        #			(1-object_mask) * K.binary_crossentropy(object_mask, raw_pred[...,4:5], from_logits=True) * ignore_mask
        class_loss = object_mask * K.binary_crossentropy(
            true_class_probs, raw_pred[..., 5:], from_logits=True)

        xy_loss = K.sum(xy_loss) / mf
        wh_loss = K.sum(wh_loss) / mf
        confidence_loss_obj = K.sum(confidence_loss_obj) / mf
        confidence_loss_noobj = K.sum(confidence_loss_noobj) / mf
        #		confidence_loss = K.sum(confidence_loss) / mf
        class_loss = K.sum(class_loss) / mf

        total_xy_loss += xy_loss
        total_wh_loss += wh_loss
        #		total_confidence_loss += confidence_loss
        total_confidence_loss_obj += confidence_loss_obj
        total_confidence_loss_noobj += confidence_loss_noobj
        total_class_loss += class_loss

    loss += total_xy_loss * loss_percs.get('xy',1) + \
       total_wh_loss * loss_percs.get('wh',1) + \
       total_confidence_loss_obj * loss_percs.get('confidence_obj',1) + \
       total_confidence_loss_noobj * loss_percs.get('confidence_noobj',1) + \
       total_class_loss * loss_percs.get('class',1)
    #	if print_loss:
    #		loss = tf.Print(loss, [loss, xy_loss, wh_loss, confidence_loss, class_loss, K.sum(ignore_mask)], message='loss: ')
    #	return loss
    return tf.convert_to_tensor([
        loss, total_xy_loss, total_wh_loss, total_confidence_loss_obj +
        total_confidence_loss_noobj, total_confidence_loss_obj,
        total_confidence_loss_noobj, total_class_loss
    ],
                                dtype=tf.float32)