Пример #1
0
def train():
    # Evaluation pipeline
    files = glob.glob(
        os.path.join('/home/iotsc_group1/ChangxingDENG/det/datasets/',
                     'PretrainImageNet', 'ILSVRC2012_img_val', '*.JPEG'))
    files = sorted(files,
                   key=lambda f: f.split('/')[-1].split('_')[-1].split('.')[0])
    labels = loadlabel(
        os.path.join(
            '/home/iotsc_group1/ChangxingDENG/det/datasets/',
            'PretrainImageNet',
            'ILSVRC2012_devkit_t12/data/ILSVRC2012_validation_ground_truth.txt'
        ))
    eval_pipeline = EvalImageDecoderPipeline(files=files, labels=labels)
    eval_pipeline.build()
    eval_pii = pytorchIterator(eval_pipeline,
                               last_batch_policy=LastBatchPolicy.PARTIAL,
                               reader_name='Reader',
                               auto_reset=True)

    model = Darknet()

    state_dict = torch.load(
        'logs/PretrainImageNet_20210316173822_1/13_70056.pth')
    model.load_state_dict(state_dict=state_dict)
    model = model.cuda()

    criterion = nn.CrossEntropyLoss()

    model.eval()
    epoch_loss = 0
    prediciton = []
    target = []
    with torch.no_grad():
        for iter, data in enumerate(eval_pii):
            x = data[0]['data']
            label = data[0]['label'].squeeze(-1).long().cuda()
            output = model(x)
            loss = criterion(output, label).item()
            epoch_loss += loss * x.shape[0]
            prediciton.append(output)
            target.append(label)
        loss = epoch_loss / 50000
        prediciton = torch.cat(prediciton, dim=0)
        target = torch.cat(target, dim=0)
        acc = top1accuracy(prediciton, target)
        acctop5 = top5accuracy(prediciton, target)
        print(f'Top1 ACC: {acc} Top5 ACC {acctop5} loss: {loss}')
Пример #2
0
def detect_images(widget=None):
    if not widget:
        args = arg_parse()
    else:
        args = widget.args

    read_dir = time.time()
    print_info(widget, False, "info", "Reading addresses.....")
    images = args.images
    im_list = []
    img = None
    try:
        for img in images:
            if os.path.isabs(img):
                im_list.append(img)
            else:
                im_list.append(osp.join(osp.realpath('.'), img))
    except FileNotFoundError:
        print_info(widget, True, "error", "No file or directory with the name {}".format(img))

    if not os.path.exists(args.det):
        os.makedirs(args.det)
    print_info(widget, False, "info", "Finished reading addresses")
    finish_read_dir = time.time()

    batch_size = int(args.bs)
    confidence = float(args.confidence)
    nms_thesh = float(args.nms_thresh)
    namesfile = args.names

    cuda_present = torch.cuda.is_available()

    classes = load_classes(namesfile)
    num_classes = len(classes)

    # Set up the neural network
    load_net = time.time()
    print_info(widget, False, "info", "Loading network.....")
    model = Darknet(args.cfg)
    model.load_weights(args.weights)
    print_info(widget, False, "info", "Network successfully loaded")
    finish_load_net = time.time()

    model.net_info["height"] = args.reso
    model.net_info["width"] = args.reso
    inp_dim = int(model.net_info["height"])
    assert inp_dim % 32 == 0
    assert inp_dim > 32

    # If there's a GPU availible, put the model on GPU
    if cuda_present:
        model.cuda()
    # Set the model in evaluation mode (for Batchnorm layers)
    model.eval()
    # Detection phase

    load_batch = time.time()
    print_info(widget, False, "info", "Loading batches.....")
    loaded_ims = [cv2.imread(x) for x in im_list]

    im_batches = list(map(prep_image, loaded_ims, [inp_dim for _ in range(len(im_list))]))
    im_dim_list = [(x.shape[1], x.shape[0]) for x in loaded_ims]
    im_dim_list = torch.FloatTensor(im_dim_list).repeat(1, 2)

    leftover = 0
    if len(im_dim_list) % batch_size:
        leftover = 1

    if batch_size != 1:
        num_batches = len(im_list) // batch_size + leftover
        im_batches = [torch.cat((im_batches[i * batch_size: min((i + 1) * batch_size,
                                                                len(im_batches))])) for i in range(num_batches)]

    if cuda_present:
        im_dim_list = im_dim_list.cuda()

    output = torch.empty((0, 0))

    print_info(widget, False, "info", "Finished loading batches....")
    start_det_loop = time.time()
    for i, batch in enumerate(im_batches):
        # load the image
        start = time.time()
        print_info(widget, False, "info", f"Detecting batch no {i}....")
        if cuda_present:
            batch = batch.cuda()
        with torch.no_grad():
            prediction = model(batch, cuda_present)

        prediction = write_results(prediction, confidence, num_classes, nms_conf=nms_thesh)

        end = time.time()

        if type(prediction) == int:

            for im_num, image in enumerate(im_list[i * batch_size: min((i + 1) * batch_size, len(im_list))]):
                im_id = i * batch_size + im_num
                msg = "{0:20s} predicted in {1:6.3f} seconds".format(image.split("/")[-1], (end - start) / batch_size)
                msg += "\n{0:20s} {1:s}".format("Objects Detected:", "")
                msg += "\n----------------------------------------------------------"
                print_info(widget, False, 'batch_info', msg, im_id)
            continue

        prediction[:, 0] += i * batch_size  # transform the atribute from index in batch to index in imlist

        if np.size(output, 0) == 0:  # If we have't initialised output
            output = prediction
        else:
            output = torch.cat((output, prediction))

        for im_num, image in enumerate(im_list[i * batch_size: min((i + 1) * batch_size, len(im_list))]):
            im_id = i * batch_size + im_num
            objs = [classes[int(x[-1])] for x in output if int(x[0]) == im_id]
            msg = "{0:20s} predicted in {1:6.3f} seconds".format(image.split("/")[-1], (end - start) / batch_size)
            msg += "\n{0:20s} {1:s}".format("Objects Detected:", " ".join(objs))
            msg += "\n----------------------------------------------------------"
            print_info(widget, False, 'batch_info', msg, im_id)

        if cuda_present:
            torch.cuda.synchronize()
        print_info(widget, False, "info", f"Finished detecting batch no {i}")

    if np.size(output, 0) == 0:
        print_info(widget, False, 'no_detections', "No detections were made")
        print_info(widget, False, 'finished')
        return

    # Start rescaling
    print_info(widget, False, "info", "Output processing....")
    output_rescale = time.time()
    im_dim_list = torch.index_select(im_dim_list, 0, output[:, 0].long())

    scaling_factor = torch.min(inp_dim / im_dim_list, 1)[0].view(-1, 1)

    output[:, [1, 3]] -= (inp_dim - scaling_factor * im_dim_list[:, 0].view(-1, 1)) / 2
    output[:, [2, 4]] -= (inp_dim - scaling_factor * im_dim_list[:, 1].view(-1, 1)) / 2

    output[:, 1:5] /= scaling_factor

    # set padding space black
    for i in range(output.shape[0]):
        output[i, [1, 3]] = torch.clamp(output[i, [1, 3]], 0.0, im_dim_list[i, 0])
        output[i, [2, 4]] = torch.clamp(output[i, [2, 4]], 0.0, im_dim_list[i, 1])
    class_load = time.time()
    print_info(widget, False, "info", "Finished output processing.")

    # Start draw
    print_info(widget, False, "info", "Drawing boxes....")
    draw = time.time()
    images_handler = ImagesHandler(classes, output, loaded_ims, args.det, im_list, batch_size)
    images_handler.write()
    print_info(widget, False, "images_ready", images_handler.imageList)
    end = time.time()
    print_info(widget, False, "info", "Finished drawing boxes")

    msg = "\n\nSUMMARY"
    msg += "\n----------------------------------------------------------"
    msg += "\n{:25s}: {}".format("Task", "Time Taken (in seconds)")
    msg += "\n"
    msg += "\n{:25s}: {:2.3f}".format("Reading addresses", finish_read_dir - read_dir)
    msg += "\n{:25s}: {:2.3f}".format("Loading network", finish_load_net - load_net)
    msg += "\n{:25s}: {:2.3f}".format("Loading batch", start_det_loop - load_batch)
    msg += "\n{:25s}: {:2.3f}".format("Detection (" + str(len(im_list)) + " images)", output_rescale - start_det_loop)
    msg += "\n{:25s}: {:2.3f}".format("Output Processing", class_load - output_rescale)
    msg += "\n{:25s}: {:2.3f}".format("Drawing Boxes", end - draw)
    msg += "\n{:25s}: {:2.3f}".format("Average time_per_img", (end - load_batch) / len(im_list))
    msg += "\n----------------------------------------------------------"
    print_info(widget, False, 'info', msg)
    torch.cuda.empty_cache()

    print_info(widget, False, 'finished')
Пример #3
0
def train(params):
    params = Params(params)

    set_random_seeds(params.seed)

    time_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    params.save_root = params.save_root + f'/{params.project_name}_{time_now}_{params.version}'
    os.makedirs(params.save_root, exist_ok=True)

    logging.basicConfig(
        filename=
        f'{params.save_root}/{params.project_name}_{time_now}_{params.version}.log',
        filemode='a',
        format='%{asctime}s - %(levalname)s: %(message)s')

    if params.num_gpus == 0:
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
    logging.info(f'Available GPUs: {torch.cuda.device_count()}')

    # Train pipeline
    files = glob.glob(
        os.path.join(params.data_root, params.project_name, params.train_set,
                     '*/*.JPEG'))
    labels = []
    for fp in files:
        label = int(fp.split('/')[-2]) - 1
        labels.append(label)
    assert len(files) == len(labels)
    train_pipeline = TrainImageDecoderPipeline(params=params,
                                               device_id=0,
                                               files=files,
                                               labels=labels)
    train_pipeline.build()
    train_pii = pytorchIterator(train_pipeline,
                                last_batch_policy=LastBatchPolicy.DROP,
                                reader_name='Reader',
                                auto_reset=True)
    # Evaluation pipeline
    files = glob.glob(
        os.path.join(params.data_root, params.project_name, params.val_set,
                     '*.JPEG'))
    files = sorted(files,
                   key=lambda f: f.split('/')[-1].split('_')[-1].split('.')[0])
    labels = loadlabel(
        os.path.join(
            params.data_root, params.project_name,
            'ILSVRC2012_devkit_t12/data/ILSVRC2012_validation_ground_truth.txt'
        ))
    eval_pipeline = EvalImageDecoderPipeline(params=params,
                                             device_id=0,
                                             files=files,
                                             labels=labels)
    eval_pipeline.build()
    eval_pii = pytorchIterator(eval_pipeline,
                               last_batch_policy=LastBatchPolicy.PARTIAL,
                               reader_name='Reader',
                               auto_reset=True)

    model = Darknet()

    last_step = 0
    last_epoch = 0
    if params.load_weights != 'None':
        try:
            state_dict = torch.load(params.load_weights)
            model.load_state_dict(state_dict)
            last_step = int(params.load_weights.split('_')[-1].split('.')[0])
            last_epoch = int(params.load_weights.split('_')[-2])
        except:
            logging.error('Fail to resuming from weight!')
            exit()

    if params.num_gpus > 0:
        model = model.cuda()
        if params.num_gpus > 1:
            model = nn.DataParallel(model)

    if params.optim == 'Adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=params.learning_rate)
    else:
        optimizer = torch.optim.SGD(model.parameters(),
                                    lr=params.learning_rate,
                                    momentum=0.9,
                                    nesterov=True)

    criterion = nn.CrossEntropyLoss()
    # ls_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, factor=0.5, verbose=True, patience=8)

    epoch = 0
    begin_epoch = max(0, last_epoch)
    step = max(0, last_step)
    best_loss = 100
    logging.info('Begin to train...')
    model.train()
    try:
        for epoch in range(begin_epoch, params.epoch):
            for iter, data in enumerate(train_pii):
                x = data[0]['data']
                label = data[0]['label'].squeeze(-1).long().cuda()
                output = model(x)
                loss = criterion(output, label)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()
                if iter % params.save_interval == 0:
                    logging.info(
                        f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} '
                        f'Train Epoch: {epoch} iter: {iter} loss: {loss.item()}'
                    )
                step += 1
            if epoch % params.eval_interval == 0:
                model.eval()
                epoch_loss = 0
                prediciton = []
                target = []
                with torch.no_grad():
                    for iter, data in enumerate(eval_pii):
                        x = data[0]['data']
                        label = data[0]['label'].squeeze(-1).long().cuda()
                        output = model(x)
                        loss = criterion(output, label).item()
                        epoch_loss += loss * x.shape[0]
                        prediciton.append(output)
                        target.append(label)
                    loss = epoch_loss / 50000
                    prediciton = torch.cat(prediciton, dim=0)
                    target = torch.cat(target, dim=0)
                    acc = top1accuracy(prediciton, target)
                    acctop5 = top5accuracy(prediciton, target)
                    logging.info(
                        f'{datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")} '
                        f'Eval Epoch: {epoch} loss: {loss} accuracy: {acc} Top5 acc: {acctop5}'
                    )
                    if loss < best_loss:
                        best_loss = loss
                        save_checkpoint(
                            model, f'{params.save_root}/{epoch}_{step}.pth')
                model.train()

    except KeyboardInterrupt:
        save_checkpoint(model,
                        f'{params.save_root}/Interrupt_{epoch}_{step}.pth')
Пример #4
0
def detect(kitti_weights='../checkpoints/best_weights_kitti.pth', config_path='../config/yolov3-kitti.cfg',
           class_path='../data/names.txt'):
    """
        Script to run inference on sample images. It will store all the inference results in /output directory (
        relative to repo root)
        
        Args
            kitti_weights: Path of weights
            config_path: Yolo configuration file path
            class_path: Path of class names txt file
            
    """
    cuda = torch.cuda.is_available()
    os.makedirs('../output', exist_ok=True)

    # Set up model
    model = Darknet(config_path, img_size=416)
    model.load_weights(kitti_weights)

    if cuda:
        model.cuda()
        print("Cuda available for inference")

    model.eval()  # Set in evaluation mode

    dataloader = DataLoader(ImageFolder("../data/samples/", img_size=416),
                            batch_size=2, shuffle=False, num_workers=0)

    classes = load_classes(class_path)  # Extracts class labels from file

    Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

    imgs = []  # Stores image paths
    img_detections = []  # Stores detections for each image index

    print('data size : %d' % len(dataloader))
    print('\nPerforming object detection:')
    prev_time = time.time()
    for batch_i, (img_paths, input_imgs) in enumerate(dataloader):
        # Configure input
        input_imgs = Variable(input_imgs.type(Tensor))

        # Get detections
        with torch.no_grad():
            detections = model(input_imgs)
            detections = non_max_suppression(detections, 80, 0.8, 0.4)
            # print(detections)

        # Log progress
        current_time = time.time()
        inference_time = datetime.timedelta(seconds=current_time - prev_time)
        prev_time = current_time
        print('\t+ Batch %d, Inference Time: %s' % (batch_i, inference_time))

        # Save image and detections
        imgs.extend(img_paths)
        img_detections.extend(detections)

    # Bounding-box colors
    # cmap = plt.get_cmap('tab20b')
    cmap = plt.get_cmap('tab10')
    colors = [cmap(i) for i in np.linspace(0, 1, 20)]

    print('\nSaving images:')
    # Iterate through images and save plot of detections
    for img_i, (path, detections) in enumerate(zip(imgs, img_detections)):

        print("(%d) Image: '%s'" % (img_i, path))

        # Create plot
        img = np.array(Image.open(path))
        plt.figure()
        fig, ax = plt.subplots(1)
        ax.imshow(img)

        kitti_img_size = 416

        # The amount of padding that was added
        pad_x = max(img.shape[0] - img.shape[1], 0) * (kitti_img_size / max(img.shape))
        pad_y = max(img.shape[1] - img.shape[0], 0) * (kitti_img_size / max(img.shape))
        # Image height and width after padding is removed
        unpad_h = kitti_img_size - pad_y
        unpad_w = kitti_img_size - pad_x

        # Draw bounding boxes and labels of detections
        if detections is not None:
            print(type(detections))
            print(detections.size())
            unique_labels = detections[:, -1].cpu().unique()
            n_cls_preds = len(unique_labels)
            bbox_colors = random.sample(colors, n_cls_preds)
            for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:

                print('\t+ Label: %s, Conf: %.5f' % (classes[int(cls_pred)], cls_conf.item()))
                # Rescale coordinates to original dimensions
                box_h = int(((y2 - y1) / unpad_h) * (img.shape[0]))
                box_w = int(((x2 - x1) / unpad_w) * (img.shape[1]))
                y1 = int(((y1 - pad_y // 2) / unpad_h) * (img.shape[0]))
                x1 = int(((x1 - pad_x // 2) / unpad_w) * (img.shape[1]))

                color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
                # Create a Rectangle patch
                bbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2,
                                         edgecolor=color,
                                         facecolor='none')
                # Add the bbox to the plot
                ax.add_patch(bbox)
                # Add label
                plt.text(x1, y1 - 30, s=classes[int(cls_pred)] + ' ' + str('%.4f' % cls_conf.item()), color='white',
                         verticalalignment='top',
                         bbox={'color': color, 'pad': 0})

        # Save generated image with detections
        plt.axis('off')
        plt.gca().xaxis.set_major_locator(NullLocator())
        plt.gca().yaxis.set_major_locator(NullLocator())
        plt.savefig('../output/%d.png' % (img_i), bbox_inches='tight', pad_inches=0.0)
        plt.close()
Пример #5
0
def train(params):
    params = Params(params)

    set_random_seeds(params.seed)

    time_now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    params.save_root = params.save_root + f'/{params.project_name}_{time_now}_{params.version}'
    os.makedirs(params.save_root, exist_ok=True)

    logging.basicConfig(
        filename=
        f'{params.save_root}/{params.project_name}_{time_now}_{params.version}.log',
        filemode='a',
        format='%{asctime}s - %(levalname)s: %(message)s')

    if params.num_gpus == 0:
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
    logging.info(f'Available GPUs: {torch.cuda.device_count()}')

    data_transform = transforms.Compose([
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=params.mean, std=params.std)
    ])

    train_set = TrainDataset(root=os.path.join(params.data_root,
                                               params.project_name,
                                               params.train_set),
                             transform=data_transform)
    # val_set = EvalDataset(root=os.path.join(params.data_root, params.project_name, params.val_set),
    #                       label_path=os.path.join(params.data_root, params.project_name,
    #                                               'ILSVRC2012_devkit_t12/data/ILSVRC2012_validation_ground_truth.txt'),
    #                       transform=data_transform)
    train_params = {
        'batch_size': params.batch_size,
        'shuffle': False,
        'num_workers': 4,
        'drop_last': True
    }
    # val_params = {'batch_size': params.batch_size, 'shuffle': False, 'num_workers': params.num_gpus * 4,
    #               'drop_last': False}
    train_loader = DataLoader(train_set, **train_params)
    # val_loader = DataLoader(val_set, **val_params)

    eli = ExternalInputIterator(params.batch_size)
    params.mean = torch.Tensor(params.mean).unsqueeze(0).unsqueeze(0)
    params.std = torch.Tensor(params.std).unsqueeze(0).unsqueeze(0)
    pipe = ExternalSourcePipeline(params=params,
                                  num_threads=4,
                                  device_id=0,
                                  external_date=eli,
                                  seed=params.seed)
    # pipe.build()
    # images, _ = pipe.run()
    # print(np.array(images[0].as_cpu()).shape)
    import matplotlib.pyplot as plt
    # plt.imsave('image[0].jpg', np.array(images[0].as_cpu()).transpose((1, 2, 0)))
    # plt.imsave('image[0].jpg', images[0].as_cpu())
    # exit()

    pii = pytorchIterator(pipe,
                          last_batch_padded=True,
                          last_batch_policy=LastBatchPolicy.DROP)

    model = Darknet()
    criterion = nn.CrossEntropyLoss()

    last_step = 0
    last_epoch = 0
    if params.load_weights != 'None':
        try:
            state_dict = torch.load(params.load_weights)
            model.load_state_dict(state_dict)
            last_step = int(params.load_weights.split('_')[-1].split('.')[0])
            last_epoch = int(params.load_weights.split('_')[-2])
        except:
            logging.error('Fail to resuming from weight!')
            exit()

    if params.num_gpus > 0:
        model = model.cuda()
        if params.num_gpus > 1:
            model = nn.DataParallel(model)

    if params.optim == 'Adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=params.learning_rate)
    else:
        optimizer = torch.optim.SGD(model.parameters(),
                                    lr=params.learning_rate,
                                    momentum=0.9,
                                    nesterov=True)

    epoch = 0
    begin_epoch = max(0, last_epoch)
    step = max(0, last_step)
    logging.info('Begin to train...')
    model.train()
    try:
        import time
        for epoch in range(begin_epoch, params.epoch):
            for iter, (data_pii,
                       data_torch) in enumerate(zip(pii, train_loader)):
                t = time.time()
                # type(x)显示torch.Tensor,但是x已经在显存上
                x_pii = data_pii[0]['data']
                label_pii = data_pii[0]['label'].cuda()
                x_torch = data_torch[0].cuda()
                label_torch = data_torch[1].cuda()
                x_pii = x_pii.cpu().squeeze(0).numpy().transpose((1, 2, 0))
                x_torch = x_torch.cpu().squeeze(0).numpy().transpose((1, 2, 0))
                import matplotlib.pyplot as plt
                plt.imsave('x_pii.jpg', x_pii)
                plt.imsave('x_torch.jpg', x_torch)
                exit()
                # print('load data time:', time.time() - t)
                # t = time.time()
                # output = model(x)
                # loss = criterion(output, label)
                # optimizer.zero_grad()
                # loss.backward()
                # optimizer.step()
                # print('running time:', time.time() - t)
                # if iter == 6:
                #     exit()

    except KeyboardInterrupt:
        save_checkpoint(model,
                        f'{params.save_root}/Interrupt_{epoch}_{step}.pth')
class ReadFramesThread:
    def __init__(self, path, args, with_tracking, widget, queue_size=3000):
        # initialize the file video stream along with the boolean
        # used to indicate if the thread should be stopped or not
        self.stream = cv2.VideoCapture(path)
        self.widget = widget
        self.tracking = with_tracking
        if not self.stream:
            if type(path) == int:
                print_info(widget, True, "error",
                           f"Error opening web cam on {path}")
            else:
                print_info(widget, True, "error",
                           f"Error opening video file {path}")
        self.stopped = False
        self.canceled = False
        self.paused = False
        self.ready = False
        # initialize the queue used to store frames read from
        # the video file
        self.Q = Queue(maxsize=queue_size)
        self.imread = Queue(maxsize=queue_size)
        self.Q_processed = Queue(maxsize=queue_size)

        self.inp_dim = int(args.reso)
        self.batch_size = int(args.bs)
        self.names_file = args.names
        self.confidence = float(args.confidence)
        self.nms_thresh = float(args.nms_thresh)
        self.is_classifier = args.is_classifier
        self.classes = load_classes(self.names_file)
        self.num_classes = len(self.classes)

        self.model = None
        self.model_classifier = None
        if self.is_classifier:
            print_info(widget, False, "info",
                       "Loading network for detection.....", -1)
            self.model = Darknet(args.classifier_cfg)
            self.model.load_weights(args.classifier_weights)
            print_info(widget, False, "info",
                       "Network for detection successfully loaded", 0)

            print_info(widget, False, "info",
                       "Loading network for classification.....", -1)
            self.model_classifier = Darknet(args.cfg)
            self.model_classifier.load_weights(args.weights)
            print_info(widget, False, "info",
                       "Network for classification successfully loaded", 0)

            self.model_classifier.net_info["height"] = args.reso
            self.inp_dim = int(self.model_classifier.net_info["height"])
            # If there's a GPU availible, put the model on GPU
            self.cuda = torch.cuda.is_available()
            if self.cuda:
                self.model_classifier.cuda()
            # Set the model in evaluation mode
            self.model_classifier.eval()

            self.classifier_confidence = self.confidence
            self.classifier_nms_thesh = self.nms_thresh
            self.classifier_classes = self.classes
            self.classifier_num_classes = self.num_classes
            self.classifier_names_file = self.names_file
            self.classifier_inp_dim = self.inp_dim

            self.inp_dim = args.classifier_inp_dim
            self.confidence = args.classifier_confidence
            self.nms_thresh = args.classifier_nms_thresh
            self.names_file = args.classifier_names
            self.classes = load_classes(self.names_file)
            self.num_classes = len(self.classes)

        else:
            print_info(widget, False, "info", "Loading network.....", -1)
            self.model = Darknet(args.cfg)
            self.model.load_weights(args.weights)
            print_info(widget, False, "info", "Network successfully loaded", 0)

        self.model.net_info["height"] = self.inp_dim
        assert self.inp_dim % 32 == 0
        assert self.inp_dim > 32

        # If there's a GPU availible, put the model on GPU
        self.cuda = torch.cuda.is_available()
        if self.cuda:
            self.model.cuda()
        # Set the model in evaluation mode
        self.model.eval()

        # if tracking selected, initialize sort class
        self.mot_tracking = None
        if self.tracking == "sort":
            self.mot_tracking = Sort(max_age=30, min_hits=3)
        elif self.tracking == "deep_sort":
            print_info(widget, False, "info", "Loading Deep Sort model ...",
                       -1)
            self.mot_tracking = DeepSort()
            print_info(widget, False, "info", "Deep Sort model loaded", -1)

    def start(self):
        # start a thread to read frames from the file video stream
        t = Thread(target=self.update, args=())
        # t.daemon = True
        t.start()
        return self

    def update(self):
        frames = 0
        start = time.time()
        print_info(self.widget, False, "info", "Began capturing", -2)
        # keep looping infinitely
        while True:
            # if the thread indicator variable is set, stop the
            # thread
            if self.stopped:
                break
            if self.canceled:
                current_time = time.time()
                print_info(self.widget, False, "info", "Canceled processing",
                           current_time - start)
                return
            if self.paused:
                self.widget.obj.pauseMutex.lock()
                self.widget.obj.pauseCond.wait(self.widget.obj.pauseMutex)
                self.widget.obj.pauseMutex.unlock()
                self.paused = False
            # otherwise, ensure the queue has room in it
            if not self.Q.full():
                # read the next frame from the file
                (grabbed, frame) = self.stream.read()
                # if the `grabbed` boolean is `False`, then we have
                # reached the end of the video file
                if not grabbed:
                    self.stop()
                    self.ready = True
                    return
                # add the frame to the queue
                self.Q.put(prep_image(frame, self.inp_dim))
                self.imread.put(frame)

                frames += 1
                current_time = time.time()
                msg = " FPS of the video is {:5.4f}".format(
                    frames / (current_time - start))
                print_info(self.widget, False, "info", msg,
                           current_time - start)

                if frames % self.batch_size == 0:
                    self.process_frames()
        if not self.Q.empty():
            self.process_frames()

    def read(self):
        # return next frame in the queue
        return self.Q.get()

    def more(self):
        # return True if there are still frames in the queue
        return self.Q.qsize() > 0

    def stop(self):
        # indicate that the thread should be stopped
        self.stopped = True

    def cancel(self):
        self.canceled = True

    def pause(self):
        self.paused = True

    def has_batch(self):
        if self.Q.qsize() >= self.batch_size:
            return True
        if self.Q.qsize() > 0 and self.stopped:
            return True
        return False

    def get_batch(self):
        if (self.Q.qsize() >= self.batch_size) or (self.Q.qsize() > 0
                                                   and self.stopped):
            res = np.empty((0, 0))
            im_dim_list = []
            imread_list = []
            for _ in range(self.batch_size):
                img = self.Q.get()
                if np.size(res, 0) == 0:
                    res = img
                else:
                    res = torch.cat((res, img))
                img = self.imread.get()
                im_dim_list.append((img.shape[1], img.shape[0]))
                imread_list.append(img)
            im_dim_list = torch.FloatTensor(im_dim_list).repeat(1, 2)
            return res, im_dim_list, imread_list
        return False, False, False

    def process_frames(self):
        batch_nr = -1
        batch, im_dims, imread = self.get_batch()
        if imread:
            batch_nr += 1
            if self.cuda:
                im_dims = im_dims.cuda()
                batch = batch.cuda()
            with torch.no_grad():
                output = self.model(batch, self.cuda)

            for frame_id in range(np.size(output, 0)):
                nr_frame = self.batch_size * batch_nr + frame_id + 1
                im_dim = im_dims[frame_id]
                frame = output[frame_id].unsqueeze(0)
                frame = write_results(frame,
                                      self.confidence,
                                      self.num_classes,
                                      nms_conf=self.nms_thresh)

                if np.size(frame, 0) > 0:
                    im_dim = im_dim.repeat(frame.size(0), 1)
                    scaling_factor = torch.min(416 / im_dim, 1)[0].view(-1, 1)

                    frame[:, [1, 3]] -= (self.inp_dim - scaling_factor *
                                         im_dim[:, 0].view(-1, 1)) / 2
                    frame[:, [2, 4]] -= (self.inp_dim - scaling_factor *
                                         im_dim[:, 1].view(-1, 1)) / 2

                    frame[:, 1:5] /= scaling_factor

                    for i in range(frame.shape[0]):
                        frame[i, [1, 3]] = torch.clamp(frame[i, [1, 3]], 0.0,
                                                       im_dim[i, 0])
                        frame[i, [2, 4]] = torch.clamp(frame[i, [2, 4]], 0.0,
                                                       im_dim[i, 1])

                    if self.is_classifier:
                        frame = self.apply_classifier_model(
                            imread[frame_id], frame)

                if self.tracking == "sort":
                    if self.cuda:
                        frame = frame.cpu()
                    frame = self.mot_tracking.update(frame)
                    if self.cuda:
                        frame = torch.from_numpy(frame).cuda()
                elif self.tracking == "deep_sort":
                    if self.cuda:
                        frame = frame.cpu()
                    tracker, detections_class = self.mot_tracking.update(
                        imread[frame_id], frame)
                    frame = []
                    for track in tracker.tracks:
                        if not track.is_confirmed(
                        ) or track.time_since_update > 1:
                            continue

                        bbox = track.to_tlbr(
                        )  # Get the corrected/predicted bounding box
                        id_num = int(track.track_id
                                     )  # Get the ID for the particular track.

                        # Draw bbox from tracker.
                        frame.append(
                            np.concatenate(([id_num + 1], bbox, [
                                track.conf_score, track.class_score, track.cid
                            ])).reshape(1, -1))
                    if len(frame) > 0:
                        frame = np.concatenate(frame)
                        if self.cuda:
                            frame = torch.from_numpy(frame).cuda()
                    else:
                        frame = torch.empty((0, 8))

                if np.size(frame, 0) == 0:
                    image_handler = ImageHandler(nr_frame, batch_nr,
                                                 f"frame{nr_frame}",
                                                 imread[frame_id],
                                                 self.tracking)
                    self.Q_processed.put(image_handler)
                    continue

                image_handler = ImageHandler(nr_frame, batch_nr,
                                             f"frame{nr_frame}",
                                             imread[frame_id], self.tracking)
                if self.is_classifier:
                    image_handler.write(frame, self.classifier_classes)
                else:
                    image_handler.write(frame, self.classes)
                self.Q_processed.put(image_handler)

    def get_image(self):
        return self.Q_processed.get()

    def has_images(self):
        return not self.Q_processed.empty()

    def apply_classifier_model(self, imread, frame):
        # get crops from detections in frame
        crops = torch.empty((0, 0))
        detections = frame[:, 1:5]
        for d in detections:
            for i in range(len(d)):
                if d[i] < 0:
                    d[i] = 0
            img_h, img_w, img_ch = imread.shape
            xmin, ymin, xmax, ymax = d
            if xmin > img_w:
                xmin = img_w
            if ymin > img_h:
                ymin = img_h
            ymin = abs(int(ymin))
            ymax = abs(int(ymax))
            xmin = abs(int(xmin))
            xmax = abs(int(xmax))
            try:
                crop = imread[ymin:ymax, xmin:xmax, :]
                crop = prep_image(crop, self.classifier_inp_dim)
                if np.size(crops, 0) == 0:
                    crops = crop
                else:
                    crops = torch.cat((crops, crop))
            except:
                continue
        if self.cuda:
            crops = crops.cuda()
        with torch.no_grad():
            output = self.model_classifier(crops, self.cuda)
        for frame_id in range(np.size(output, 0)):
            new_det = output[frame_id].unsqueeze(0)
            new_det = write_results(new_det,
                                    self.classifier_confidence,
                                    self.classifier_num_classes,
                                    nms_conf=self.classifier_nms_thesh)
            if np.size(new_det, 0) > 0:
                index = torch.argmax(new_det[:, 6])
                frame[frame_id, 6:8] = new_det[index, 6:8]
            else:
                frame[frame_id, 6] = -1
        frame = frame[frame[:, 6] >= 0]
        return frame
Пример #7
0
left_over = 0
if (len(loaded_imgs) % batch_size):
    left_over = 1

num_batches = (len(loaded_imgs) // batch_size) + left_over

batches = [
    det_imgs[i * batch_size:min((i + 1) * batch_size, len(loaded_imgs))]
    for i in range(num_batches)
]

batches = [torch.cat(batch, 0) for batch in batches]

net = Darknet()
net = net.cuda()
net.load_weights()
net.eval()

write = 0


def put_rectangle(x, results):
    c1 = tuple(x[1:3].int())
    c2 = tuple(x[3:5].int())
    img = results[int(x[0])]
    cls = int(x[-1])
    label = "{0}".format(classes[cls])
    cv2.rectangle(img, c1, c2, [255, 0, 0], 1)
    t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
    c2 = c1[0] + t_size[0] + 3, c1[1] + t_size[1] + 4
Пример #8
0
    if pretrained:
        model.load_state_dict(torch.load(args.weights))
    else:
        # 随机初始化权重,会对模型进行高斯随机初始化
        model.apply(weights_init_normal)
    print("网络权重加载成功.")

    # 设置网络输入图片尺寸大小与学习率
    reso = int(model.net_info["height"])
    lr = float(model.net_info["learning_rate"])

    assert reso % 32 == 0  # 判断如果不是32的整数倍就抛出异常
    assert reso > 32  # 判断如果网络输入图片尺寸小于32也抛出异常

    if CUDA:
        model.cuda()

    train_dataset = ListDataset(train_path, reso)
    train_dataloader = DataLoader(
        train_dataset,
        batch_size=args.batch_size,
        shuffle=True,
        num_workers=6,
        collate_fn=train_dataset.collate_fn,
    )

    # 使用NAG优化器, 不懂得可以参考https://www.sohu.com/a/149921578_610300
    optimizer = torch.optim.Adam(model.parameters(), lr=lr)

    class_metrics = [
        "grid_size",