Ejemplo n.º 1
0
def evaluate_pascal_voc(root_dir, download, batch_size, conf_thres, log_every,
                        limit, plot, save, images_results_dir):
    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')
    namesfile = os.path.join(HERE, '../cfg/coco.names')
    class_names = load_class_names(namesfile)

    model = Yolo(cfg_file=cfg_file,
                 class_names=class_names,
                 batch_size=batch_size)
    model.load_weights(weight_file)

    image_and_target_transform = Compose([SquashResize(416), ToTensor()])

    dataset = YaYoloVocDataset(root_dir=root_dir,
                               batch_size=batch_size,
                               transforms=image_and_target_transform,
                               image_set='val',
                               download=download,
                               class_names=class_names)

    summary_writer = SummaryWriter(comment=f' evaluate={batch_size}')
    evaluate(model,
             dataset,
             summary_writer,
             images_results_dir,
             iou_thres=0.5,
             conf_thres=conf_thres,
             nms_thres=0.5,
             log_every=log_every,
             limit=limit,
             plot=plot,
             save=save)

    summary_writer.close()
Ejemplo n.º 2
0
def train_car_make(car_make_json_file, batch_size, lr, conf_thres,
                   gradient_accumulations, clip_gradients, epochs, limit,
                   log_every, save_every, model_dir, parameters,
                   lambda_no_obj):
    image_and_target_transform = Compose([SquashResize(416), ToTensor()])

    dataset = DetectedCareMakeDataset(json_file=car_make_json_file,
                                      transforms=image_and_target_transform,
                                      batch_size=batch_size)

    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')

    model = Yolo(cfg_file=cfg_file,
                 class_names=dataset.class_names,
                 batch_size=batch_size,
                 coreml_mode=False)
    model.load_weights(weight_file)

    if parameters is not None:
        logger.info(f"loading model parameters from {parameters}")
        model.load_state_dict(torch.load(parameters, map_location=DEVICE))

    summary_writer = SummaryWriter(comment=f' evaluate={batch_size}')

    # with neptune.create_experiment(name='start-with-neptune',
    #                                params=PARAMS):
    #     neptune.append_tag('first-example')

    train(model=model,
          dataset=dataset,
          model_dir=model_dir,
          summary_writer=summary_writer,
          epochs=epochs,
          lr=lr,
          conf_thres=conf_thres,
          nms_thres=0.5,
          iou_thres=0.5,
          lambda_coord=5,
          lambda_no_obj=lambda_no_obj,
          gradient_accumulations=gradient_accumulations,
          clip_gradients=clip_gradients,
          limit=limit,
          debug=False,
          print_every=log_every,
          save_every=save_every)
    summary_writer.close()
def run_detect_cars(in_dir, out_dir, batch_size, limit, conf_thres, nms_thres, debug):
    assert os.path.isdir(out_dir), "directory {} does not exist".format(out_dir)

    now = datetime.datetime.now()
    now_str = now.strftime("%Y-%m-%dT%H-%M-%S")
    detected_dataset_dir = os.path.join(out_dir, now_str)
    os.mkdir(detected_dataset_dir)

    feed_file = os.path.join(detected_dataset_dir, "feed.json")
    detected_dataset_images_dir = os.path.join(detected_dataset_dir, "images")
    os.mkdir(detected_dataset_images_dir)

    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')
    namesfile = os.path.join(HERE, '../cfg/coco.names')
    class_names = load_class_names(namesfile)
    with torch.no_grad():
        model = Yolo(cfg_file=cfg_file, class_names=class_names, batch_size=batch_size)
        model.load_weights(weight_file)
        model.to(DEVICE)
        model.eval()

        image_and_target_transform = Compose([
            SquashResize(416),
            ToTensor()
        ])

        dataset = SimpleCarDataset(
            root_dir=in_dir,
            transforms=image_and_target_transform,
            batch_size=batch_size)

        with FileWriter(file_path=feed_file) as file_writer:
            car_dataset_writer = DetectedCarDatasetWriter(file_writer)

            detected_dataset_helper = DetectedCarDatasetHelper(car_dataset_writer=car_dataset_writer,
                                                               class_names=model.class_names,
                                                               conf_thres=conf_thres,
                                                               nms_thres=nms_thres,
                                                               batch_size=batch_size,
                                                               debug=debug)
            cnt = detect_and_process(model=model,
                                     dataset=dataset,
                                     processor=detected_dataset_helper.process_detections,
                                     limit=limit)

            logger.info("Ran detection of {} images".format(cnt))
Ejemplo n.º 4
0
def train_coco(coco_images_dir, coco_annotations_file, batch_size, lr,
               conf_thres, gradient_accumulations, clip_gradients, epochs,
               limit, log_every, save_every, model_dir, parameters):
    cfg_file = os.path.join(HERE, '../cfg/yolov3.cfg')
    weight_file = os.path.join(HERE, '../cfg/yolov3.weights')
    namesfile = os.path.join(HERE, '../cfg/coco.names')
    class_names = load_class_names(namesfile)
    model = Yolo(cfg_file=cfg_file,
                 class_names=class_names,
                 batch_size=batch_size)
    model.load_weights(weight_file)
    model.freeze_parameters_of_all_old_layers()

    if parameters is not None:
        logger.info(f"loading model parameters from {parameters}")
        model.load_state_dict(torch.load(parameters, map_location=DEVICE))

    image_and_target_transform = Compose([SquashResize(416), ToTensor()])

    dataset = YaYoloCocoDataset(images_dir=coco_images_dir,
                                annotations_file=coco_annotations_file,
                                transforms=image_and_target_transform,
                                batch_size=batch_size)

    summary_writer = SummaryWriter(comment=f' evaluate={batch_size}')
    train(model=model,
          dataset=dataset,
          model_dir=model_dir,
          summary_writer=summary_writer,
          epochs=epochs,
          lr=lr,
          conf_thres=conf_thres,
          nms_thres=0.5,
          iou_thres=0.5,
          lambda_coord=5,
          lambda_no_obj=0.5,
          gradient_accumulations=gradient_accumulations,
          clip_gradients=clip_gradients,
          limit=limit,
          debug=False,
          print_every=log_every,
          save_every=save_every)
    summary_writer.close()