Ejemplo n.º 1
0
def plot_one_image(img_path, class_names, weights_file, iou_thre, con_thre):
    image = cv2.imread(img_path)
    bboxes = []
    augmentations = test_transforms(image=image, bboxes=bboxes)
    image = augmentations["image"]
    img = image
    image = image.reshape(1, image.shape[0], image.shape[1], image.shape[2])
    model = YOLO(len(class_names))
    optimizer = optim.Adam(model.parameters(), lr=1e-5, weight_decay=1e-4)
    load_checkpoint(weights_file, model, optimizer, 1e-5)

    pred_bboxes = []
    with torch.no_grad():
        out = model(image)
        for i in range(3):
            scale = torch.zeros((out[i].shape[0], out[i].shape[1],
                                 out[i].shape[2], out[i].shape[3], 1))
            # here scale used to cache the scale where the box is in
            pred_bboxes.append(torch.cat((out[i], scale), -1))

    boxes = non_max_suppression(pred_bboxes, scaled_anchors, con_thre,
                                iou_thre)

    # print(names[torch.argmax(torch.sigmoid(boxes[0][..., 5:]))])
    x = boxes[0][0:4]

    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[0] = x[0] - x[2] / 2  # top left x
    y[1] = x[1] - x[3] / 2  # top left y
    y[2] = x[0] + x[2] / 2  # bottom right x
    y[3] = x[1] + x[3] / 2  # bottom right y

    S = [32, 16, 8]

    image = img.permute(1, 2, 0)

    image = image.cpu().float().numpy()
    i = int(boxes[0][5].item())
    cv2.rectangle(image, (int(y[0].item() * S[i]), int(y[1].item() * S[i])),
                  (int(y[2].item() * S[i]), int(y[3].item() * S[i])),
                  (0, 0, 255), 2)

    label = class_names[torch.argmax(torch.sigmoid(boxes[0][..., 5:]))]
    tl = 3  # line thickness
    tf = max(tl - 1, 1)  # font thickness
    t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
    cv2.putText(image,
                label,
                (int(y[2].item() * S[i] + 5), int(y[3].item() * S[i] + 5)),
                0,
                tl / 3, [220, 220, 220],
                thickness=tf,
                lineType=cv2.LINE_AA)
    cv2.imshow("fff", image)
    cv2.waitKey(0)
Ejemplo n.º 2
0
    def test_yolov3(self):
        img_path = os.path.join(os.path.dirname(__file__), '../data',
                                'street.jpg')
        yolo3_yolo3_dir = os.path.join(os.path.dirname(__file__),
                                       '../../keras-yolo3/yolo3')

        if not os.path.exists(model_file_name):
            urllib.request.urlretrieve(YOLOV3_WEIGHTS_PATH, model_file_name)

        yolo_weights = load_model(model_file_name)
        my_yolo = YOLO(yolo3_yolo3_dir)
        my_yolo.load_model(yolo_weights)
        case_name = 'yolov3'
        target_opset = 10
        onnx_model = keras2onnx.convert_keras(my_yolo.final_model,
                                              target_opset=target_opset,
                                              channel_first_inputs=['input_1'])

        if not os.path.exists(tmp_path):
            os.mkdir(tmp_path)
        temp_model_file = os.path.join(tmp_path, 'temp_' + case_name + '.onnx')
        onnx.save_model(onnx_model, temp_model_file)

        try:
            import onnxruntime
            sess = onnxruntime.InferenceSession(temp_model_file)
        except ImportError:
            return True

        from PIL import Image
        image = Image.open(img_path)
        image_data = my_yolo.prepare_keras_data(image)

        all_boxes_k, all_scores_k, indices_k = my_yolo.final_model.predict([
            image_data,
            np.array([image.size[1], image.size[0]],
                     dtype='float32').reshape(1, 2)
        ])

        image_data_onnx = np.transpose(image_data, [0, 3, 1, 2])

        feed_f = dict(
            zip(['input_1', 'image_shape'],
                (image_data_onnx,
                 np.array([image.size[1], image.size[0]],
                          dtype='float32').reshape(1, 2))))
        all_boxes, all_scores, indices = sess.run(None, input_feed=feed_f)

        expected = self.post_compute(all_boxes_k, all_scores_k, indices_k)
        actual = self.post_compute(all_boxes, all_scores, indices)

        res = all(np.allclose(expected[n_], actual[n_]) for n_ in range(3))
        self.assertTrue(res)
Ejemplo n.º 3
0
    def test_yolov3(self):
        img_path = os.path.join(os.path.dirname(__file__), '../data', 'street.jpg')
        yolo3_yolo3_dir = os.path.join(os.path.dirname(__file__), '../../../keras-yolo3/yolo3')
        try:
            import onnxruntime
        except ImportError:
            return True

        from PIL import Image

        for is_tiny_yolo in [True, False]:
            if is_tiny_yolo:
                if not os.path.exists(tiny_model_file_name):
                    urllib.request.urlretrieve(YOLOV3_TINY_WEIGHTS_PATH, tiny_model_file_name)
                yolo_weights = load_model(tiny_model_file_name)
                model_path = tiny_model_file_name  # model path or trained weights path
                anchors_path = 'model_data/tiny_yolo_anchors.txt'
                case_name = 'yolov3-tiny'
            else:
                if not os.path.exists(model_file_name):
                    urllib.request.urlretrieve(YOLOV3_WEIGHTS_PATH, model_file_name)
                yolo_weights = load_model(model_file_name)
                model_path = model_file_name  # model path or trained weights path
                anchors_path = 'model_data/yolo_anchors.txt'
                case_name = 'yolov3'

            my_yolo = YOLO(model_path, anchors_path, yolo3_yolo3_dir)
            my_yolo.load_model(yolo_weights)
            onnx_model = convert_model(my_yolo, is_tiny_yolo)

            if not os.path.exists(tmp_path):
                os.mkdir(tmp_path)
            temp_model_file = os.path.join(tmp_path, 'temp_' + case_name + '.onnx')
            onnx.save_model(onnx_model, temp_model_file)

            sess = onnxruntime.InferenceSession(temp_model_file)

            image = Image.open(img_path)
            image_data = my_yolo.prepare_keras_data(image)

            all_boxes_k, all_scores_k, indices_k = my_yolo.final_model.predict([image_data, np.array([image.size[1], image.size[0]], dtype='float32').reshape(1, 2)])

            image_data_onnx = np.transpose(image_data, [0, 3, 1, 2])

            feed_f = dict(zip(['input_1', 'image_shape'],
                              (image_data_onnx, np.array([image.size[1], image.size[0]], dtype='float32').reshape(1, 2))))
            all_boxes, all_scores, indices = sess.run(None, input_feed=feed_f)

            expected = self.post_compute(all_boxes_k, all_scores_k, indices_k)
            actual = self.post_compute(all_boxes, all_scores, indices)

            res = all(np.allclose(expected[n_], actual[n_]) for n_ in range(3))
            self.assertTrue(res)
Ejemplo n.º 4
0
from sort import Sort
import time 
import cv2
import numpy as np
import matplotlib.pyplot as plt
from yolov3 import YOLO
from collections import deque

# ---------------------------------------------------#
#  初始化
# ---------------------------------------------------#
# 创建检测器
yolo = YOLO()
# 创建跟踪器
tracker = Sort()
# 生成多种不同的颜色
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(200, 3), dtype='uint8')
# 存储中心点
pts = [deque(maxlen=30) for _ in range(9999)]
# 帧率
fps = 0

# ---------------------------------------------------#
#  虚拟线圈统计车流量
# ---------------------------------------------------#
# 虚拟线圈
line = [(0, 100), (1500, 100)]
# AC = ((C[0] - A[0]), (C[1] - A[1]))
# AB = ((B[0] - A[0]), (B[1] - A[1]))
# 计算由A,B,C三点构成的向量AC,AB之间的关系
Ejemplo n.º 5
0
def main(args):
    # scale normalized anchors by their corresponding scales (e.g img_size = 416, feature maps will 32, 16, 8,
    # scaled_anchors =  normalized_anchors X (8, 16, 32))
    """
    scaled anchors
    tensor([[ 3.6250,  2.8125],
        [ 4.8750,  6.1875],
        [11.6562, 10.1875],
        [ 1.8750,  3.8125],
        [ 3.8750,  2.8125],
        [ 3.6875,  7.4375],
        [ 1.2500,  1.6250],
        [ 2.0000,  3.7500],
        [ 4.1250,  2.8750]])
    """
    di = torch.tensor([
        int(args.img_size / 32),
        int(args.img_size / 16),
        int(args.img_size / 8)
    ]).unsqueeze(1)
    scaled_anchors = (torch.tensor(anchors) * torch.repeat_interleave(
        di, torch.tensor([3, 3, 3]), dim=0).repeat(1, 2)).to(args.device)

    train_dataset = YOLODataset(args.train_img_dir,
                                args.train_label_dir,
                                args.train_annotation_file,
                                scaled_anchors,
                                num_classes,
                                args.iou_thre,
                                args.img_size,
                                transform=train_transforms)

    val_dataset = YOLODataset(args.val_img_dir,
                              args.val_label_dir,
                              args.val_annotation_file,
                              scaled_anchors,
                              num_classes,
                              args.iou_thre,
                              args.img_size,
                              transform=test_transforms)

    train_loader = DataLoader(train_dataset,
                              batch_size=args.batch_size,
                              shuffle=True)
    val_loader = DataLoader(val_dataset,
                            batch_size=args.batch_size,
                            shuffle=True)

    model = YOLO(num_classes).to(args.device)

    # if it is first time to train the model, use darknet53 pretrained weights
    # load_weights_darknet53("darknet53.conv.74", model)

    optimizer = optim.Adam(model.parameters(), lr=1e-5, weight_decay=1e-4)

    load_checkpoint("checkpoint.pth.tar", model, optimizer, 1e-5)
    for epoch in range(args.epoches):

        train_loop(train_loader, model, YOLOloss, optimizer, args.device,
                   scaled_anchors)
        print(f"Epoch {epoch+1}\n-------------------------------")
        save_checkpoint(model, optimizer, filename=f"checkpoint.pth.tar")
        model.eval()
        results = check_class_accuracy(model, val_loader, args.conf_thre)
        mAP = mean_average_precision(model, val_loader, scaled_anchors,
                                     num_classes, args.iou_thre)
        results.append(mAP)
        with open("result.txt", 'a') as f:
            f.write(str(results).strip("[").strip("]") + '\n')

        model.train()

    print("Done!")