Пример #1
0
def main(args=None):
    # parse arguments
    if args is None:
        args = sys.argv[1:]
    args = parse_args(args)

    if DEBUG_MODE:
        args.steps = 10

    # create object that stores backbone information
    backbone = models.backbone(args.backbone)

    # make sure keras is the minimum required version
    check_keras_version()

    # optionally choose specific GPU
    use_cpu = False

    if args.gpu:
        gpu_num = args.gpu
    else:
        gpu_num = str(0)

    if use_cpu:
        os.environ["CUDA_VISIBLE_DEVICES"] = str(666)
    else:
        os.environ["CUDA_VISIBLE_DEVICES"] = gpu_num
    keras.backend.tensorflow_backend.set_session(get_session())

    # Weights and logs saves in a new locations
    stmp = time.strftime("%c").replace(" ", "_")
    args.snapshot_path = os.path.join(args.snapshot_path, stmp)
    args.tensorboard_dir = os.path.join(args.tensorboard_dir, stmp)
    print("Weights will be saved in  {}".format(args.snapshot_path))
    print("Logs will be saved in {}".format(args.tensorboard_dir))
    create_folder(args.snapshot_path)
    create_folder(args.tensorboard_dir)

    # create the generators
    train_generator, validation_generator = create_generators(args)
    print('train_size:{},val_size:{}'.format(train_generator.size(),
                                             validation_generator.size()))

    # create the model
    if args.snapshot is not None:
        print('Loading model, this may take a second...')
        model = models.load_model(args.snapshot, backbone_name=args.backbone)
        training_model = model
        prediction_model = retinanet_bbox(model=model)
    else:
        weights = os.path.join(os.path.join(root_dir(), args.weights))
        # default to imagenet if nothing else is specified
        if weights is None and args.imagenet_weights:
            weights = backbone.download_imagenet()

        print('Creating model, this may take a second...')
        model, training_model, prediction_model = create_models(
            backbone_retinanet=backbone.retinanet,
            num_classes=train_generator.num_classes(),
            weights=weights,
            multi_gpu=args.multi_gpu,
            freeze_backbone=args.freeze_backbone)

    # print model summary
    # print(model.summary())

    # this lets the generator compute backbone layer shapes using the actual backbone model
    if 'vgg' in args.backbone or 'densenet' in args.backbone:
        compute_anchor_targets = functools.partial(
            anchor_targets_bbox, shapes_callback=make_shapes_callback(model))
        train_generator.compute_anchor_targets = compute_anchor_targets
        if validation_generator is not None:
            validation_generator.compute_anchor_targets = compute_anchor_targets

    # create the callbacks
    callbacks = create_callbacks(
        model,
        training_model,
        prediction_model,
        validation_generator,
        args,
    )

    # start training
    training_model.fit_generator(generator=train_generator,
                                 steps_per_epoch=args.steps,
                                 epochs=args.epochs,
                                 verbose=1,
                                 callbacks=callbacks,
                                 validation_data=validation_generator,
                                 validation_steps=validation_generator.size())
Пример #2
0
def predict(generator,
            model,
            score_threshold=0.05,
            max_detections=9999,
            save_path=None,
            hard_score_rate=1.):
    all_detections = [[None for i in range(generator.num_classes())]
                      for j in range(generator.size())]
    csv_data_lst = []
    csv_data_lst.append(
        ['image_id', 'x1', 'y1', 'x2', 'y2', 'confidence', 'hard_score'])
    result_dir = os.path.join(root_dir(), 'results')
    create_folder(result_dir)
    timestamp = datetime.datetime.utcnow()
    res_file = result_dir + '/detections_output_iou_{}_{}.csv'.format(
        hard_score_rate, timestamp)
    for i in range(generator.size()):
        image_name = os.path.join(
            generator.image_path(i).split(os.path.sep)[-2],
            generator.image_path(i).split(os.path.sep)[-1])
        raw_image = generator.load_image(i)
        image = generator.preprocess_image(raw_image.copy())
        image, scale = generator.resize_image(image)

        # run network
        boxes, hard_scores, labels, soft_scores = model.predict_on_batch(
            np.expand_dims(image, axis=0))
        soft_scores = np.squeeze(soft_scores, axis=-1)
        soft_scores = hard_score_rate * hard_scores + (
            1 - hard_score_rate) * soft_scores
        # correct boxes for image scale
        boxes /= scale

        # select indices which have a score above the threshold
        indices = np.where(hard_scores[0, :] > score_threshold)[0]

        # select those scores
        scores = soft_scores[0][indices]
        hard_scores = hard_scores[0][indices]

        # find the order with which to sort the scores
        scores_sort = np.argsort(-scores)[:max_detections]

        # select detections
        image_boxes = boxes[0, indices[scores_sort], :]
        image_scores = scores[scores_sort]
        image_hard_scores = hard_scores[scores_sort]
        image_labels = labels[0, indices[scores_sort]]
        image_detections = np.concatenate([
            image_boxes,
            np.expand_dims(image_scores, axis=1),
            np.expand_dims(image_labels, axis=1)
        ],
                                          axis=1)
        results = np.concatenate([
            image_boxes,
            np.expand_dims(image_scores, axis=1),
            np.expand_dims(image_hard_scores, axis=1),
            np.expand_dims(image_labels, axis=1)
        ],
                                 axis=1)
        filtered_data = EmMerger.merge_detections(image_name, results)
        filtered_boxes = []
        filtered_scores = []
        filtered_labels = []

        for ind, detection in filtered_data.iterrows():
            box = np.asarray([
                detection['x1'], detection['y1'], detection['x2'],
                detection['y2']
            ])
            filtered_boxes.append(box)
            filtered_scores.append(detection['confidence'])
            filtered_labels.append('{0:.2f}'.format(detection['hard_score']))
            row = [
                image_name, detection['x1'], detection['y1'], detection['x2'],
                detection['y2'], detection['confidence'],
                detection['hard_score']
            ]
            csv_data_lst.append(row)

        if save_path is not None:
            create_folder(save_path)

            draw_annotations(raw_image,
                             generator.load_annotations(i),
                             label_to_name=generator.label_to_name)
            draw_detections(raw_image,
                            np.asarray(filtered_boxes),
                            np.asarray(filtered_scores),
                            np.asarray(filtered_labels),
                            color=(0, 0, 255))

            cv2.imwrite(os.path.join(save_path, '{}.png'.format(i)), raw_image)

        # copy detections to all_detections
        for label in range(generator.num_classes()):
            all_detections[i][label] = image_detections[
                image_detections[:, -1] == label, :-1]

        print('{}/{}'.format(i + 1, generator.size()), end='\r')

    # Save annotations csv file
    with open(res_file, 'wb') as fl_csv:
        writer = csv.writer(fl_csv)
        writer.writerows(csv_data_lst)
    print("Saved output.csv file")