Пример #1
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = config.INFERENCE_GPU_ID
    # 覆盖参数
    config.IMAGES_PER_GPU = 1
    config.GPU_COUNT = 1
    config.DETECTION_MIN_CONFIDENCE = 0.0  # 评估阶段保证有足够的边框输出
    # 加载数据集
    dataset = VocDataset(config.voc_path, class_mapping=config.CLASS_MAPPING)
    dataset.prepare()
    print("len:{}".format(len(dataset.get_image_info_list())))
    test_image_info_list = [
        info for info in dataset.get_image_info_list()
        if info['type'] == args.data_set
    ]
    print("len:{}".format(len(test_image_info_list)))
    gen = Generator(test_image_info_list, config.IMAGE_INPUT_SHAPE,
                    config.MEAN_PIXEL)
    # 加载模型
    m = models.frcnn(config, stage='test')
    if args.weight_path is not None:
        m.load_weights(args.weight_path, by_name=True)
    else:
        m.load_weights(config.rcnn_weights, by_name=True)
    # m.summary()
    # 预测边框、得分、类别
    s_time = time.time()
    boxes, scores, class_ids, _, image_metas = m.predict_generator(
        gen.gen_val(), steps=gen.size, use_multiprocessing=True)
    print("预测 {} 张图像,耗时:{} 秒".format(len(test_image_info_list),
                                     time.time() - s_time))
    # 去除padding
    image_metas = image_utils.batch_parse_image_meta(image_metas)
    predict_scores = [np_utils.remove_pad(score)[:, 0] for score in scores]
    predict_labels = [np_utils.remove_pad(label)[:, 0] for label in class_ids]
    # 还原检测边框到
    predict_boxes = [
        image_utils.recover_detect_boxes(np_utils.remove_pad(box), window,
                                         scale) for box, window, scale in
        zip(boxes, image_metas['window'], image_metas['scale'])
    ]

    # 以下是评估过程
    annotations = eval_utils.get_annotations(test_image_info_list,
                                             config.NUM_CLASSES)
    detections = eval_utils.get_detections(predict_boxes, predict_scores,
                                           predict_labels, config.NUM_CLASSES)
    average_precisions = eval_utils.voc_eval(annotations,
                                             detections,
                                             iou_threshold=0.5,
                                             use_07_metric=True)
    print("ap:{}".format(average_precisions))
    # 求mean ap 去除背景类
    mAP = np.mean(np.array(list(average_precisions.values()))[1:])
    print("mAP:{}".format(mAP))
    print("整个评估过程耗时:{} 秒".format(time.time() - s_time))
Пример #2
0
def main(args):
    os.environ["CUDA_VISIBLE_DEVICES"] = config.INFERENCE_GPU_ID
    # 覆盖参数
    config.IMAGES_PER_GPU = 1
    config.GPU_COUNT = 1
    # 加载数据
    dataset = VocDataset(config.voc_path, class_mapping=config.CLASS_MAPPING)
    dataset.prepare()
    all_img_info = [
        info for info in dataset.get_image_info_list()
        if info['type'] == 'test'
    ]  # 测试集

    # 加载模型
    m = models.frcnn(config, stage='test')
    m.load_weights(args.weight_path, by_name=True)
    m.summary()

    # class map 转为 id map
    id_mapping = class_map_to_id_map(config.CLASS_MAPPING)

    def _show_inference(id, ax=None):
        image = image_utils.load_image(all_img_info[id]['filepath'])
        image, image_meta, _ = image_utils.resize_image_and_gt(
            image, config.IMAGE_MAX_DIM, all_img_info[id]['boxes'])
        boxes, scores, class_ids, _, image_metas = m.predict([
            np.expand_dims(image, axis=0),
            np.expand_dims(image_meta, axis=0)
        ])
        boxes = np_utils.remove_pad(boxes[0])
        scores = np_utils.remove_pad(scores[0])[:, 0]
        class_ids = np_utils.remove_pad(class_ids[0])[:, 0]
        visualize.display_instances(image,
                                    boxes[:5],
                                    class_ids[:5],
                                    id_mapping,
                                    scores=scores[:5],
                                    ax=ax)
        # print("boxes num:{}".format(boxes.shape[0]))

    # 随机展示9张图像
    image_ids = np.random.choice(len(all_img_info), 9, replace=False)
    fig = plt.figure(figsize=(20, 20))
    for idx, image_id in enumerate(image_ids):
        ax = fig.add_subplot(3, 3, idx + 1)
        _show_inference(image_id, ax)
    fig.savefig('demo_images/inferece_examples.{}.png'.format(
        np.random.randint(10)))
Пример #3
0
def main(args):
    set_gpu_growth(config.GPU_COUNT)
    dataset = VocDataset(config.voc_path, class_mapping=config.CLASS_MAPPING)
    dataset.prepare()
    train_img_info = [info for info in dataset.get_image_info_list() if info['type'] == 'trainval']  # 训练集
    print("train_img_info:{}".format(len(train_img_info)))
    test_img_info = [info for info in dataset.get_image_info_list() if info['type'] == 'test']  # 测试集
    print("test_img_info:{}".format(len(test_img_info)))
    m = models.frcnn(config, stage='train')
    # 加载预训练模型
    init_epochs = args.init_epochs
    if args.init_epochs > 0:
        m.load_weights('/tmp/frcnn-rcnn.{:03d}.h5'.format(init_epochs), by_name=True)
    else:
        m.load_weights(config.pretrained_weights, by_name=True)
    m.summary()
    #
    if init_epochs < 40:
        train(m, 'heads', 40, init_epochs, config.LEARNING_RATE, train_img_info, test_img_info)
    if init_epochs < 120:
        train(m, '3+', 120, max(init_epochs, 40), config.LEARNING_RATE, train_img_info, test_img_info)
    if init_epochs < 160:
        train(m, 'all', 160, max(init_epochs, 120), config.LEARNING_RATE / 10, train_img_info, test_img_info)
Пример #4
0
def main():
    # 加载数据
    dataset = VocDataset(config.voc_path, class_mapping=config.CLASS_MAPPING)
    dataset.prepare()
    all_img_info = [info for info in dataset.get_image_info_list() if info['type'] == 'trainval']  # 测试集

    # 加载模型
    m = models.frcnn(config, stage='test')
    m.load_weights(config.rcnn_weights, by_name=True)
    m.summary()

    # class map 转为 id map
    id_mapping = class_map_to_id_map(config.CLASS_MAPPING)

    def _show_inference(id, ax=None):
        image, image_meta, _ = load_image_gt(id,
                                             all_img_info[id]['filepath'],
                                             config.IMAGE_MAX_DIM,
                                             all_img_info[id]['boxes'])
        boxes, scores, class_ids, class_logits = m.predict(
            [np.expand_dims(image, axis=0), np.expand_dims(image_meta, axis=0)])
        boxes = np_utils.remove_pad(boxes[0])
        scores = np_utils.remove_pad(scores[0])[:, 0]
        class_ids = np_utils.remove_pad(class_ids[0])[:, 0]
        visualize.display_instances(image, boxes[:5],
                                    class_ids[:5],
                                    id_mapping,
                                    scores=scores[:5],
                                    ax=ax)

    # 随机展示9张图像
    image_ids = np.random.choice(len(all_img_info), 9, replace=False)
    fig = plt.figure(figsize=(20, 20))
    for idx, image_id in enumerate(image_ids):
        ax = fig.add_subplot(3, 3, idx + 1)
        _show_inference(image_id, ax)
    fig.savefig('demo_images/inferece_examples.{}.png'.format(np.random.randint(10)))
Пример #5
0
def main(args):
    set_gpu_growth(config.GPU_COUNT)
    dataset = VocDataset(config.voc_path, class_mapping=config.CLASS_MAPPING)
    dataset.prepare()
    train_img_info = [
        info for info in dataset.get_image_info_list()
        if info['type'] == 'trainval'
    ]  # 训练集
    print("train_img_info:{}".format(len(train_img_info)))
    test_img_info = [
        info for info in dataset.get_image_info_list()
        if info['type'] == 'test'
    ]  # 测试集
    print("test_img_info:{}".format(len(test_img_info)))
    m = models.frcnn(config, stage='train')
    # 加载预训练模型
    init_epochs = args.init_epochs
    if args.init_epochs > 0:
        m.load_weights('/tmp/frcnn-{}.{:03d}.h5'.format(
            config.BASE_NET_NAME, init_epochs),
                       by_name=True)
    else:
        m.load_weights(config.pretrained_weights, by_name=True)
    # 生成器
    train_gen = Generator(train_img_info,
                          config.IMAGE_INPUT_SHAPE,
                          config.MEAN_PIXEL,
                          config.BATCH_SIZE,
                          config.MAX_GT_INSTANCES,
                          horizontal_flip=config.USE_HORIZONTAL_FLIP,
                          random_crop=config.USE_RANDOM_CROP)
    # 生成器
    val_gen = Generator(test_img_info, config.IMAGE_INPUT_SHAPE,
                        config.MEAN_PIXEL, config.BATCH_SIZE,
                        config.MAX_GT_INSTANCES)
    # 训练conv3 及以上
    models.set_trainable(config.TRAIN_LAYERS, m)
    loss_names = [
        "rpn_bbox_loss", "rpn_class_loss", "rcnn_bbox_loss", "rcnn_class_loss"
    ]
    model_utils.compile(m, config.LEARNING_RATE, config.LEARNING_MOMENTUM,
                        config.GRADIENT_CLIP_NORM, config.WEIGHT_DECAY,
                        loss_names, config.LOSS_WEIGHTS)
    m.summary()
    # 增加个性化度量
    metric_names = [
        'gt_num', 'positive_anchor_num', 'negative_anchor_num',
        'rpn_miss_gt_num', 'rpn_gt_min_max_iou', 'roi_num', 'positive_roi_num',
        'negative_roi_num', 'rcnn_miss_gt_num', 'rcnn_miss_gt_num_as',
        'gt_min_max_iou'
    ]
    model_utils.add_metrics(m, metric_names, m.outputs[-11:])

    # 训练
    m.fit_generator(train_gen.gen(),
                    epochs=args.epochs,
                    steps_per_epoch=len(train_img_info) // config.BATCH_SIZE,
                    verbose=1,
                    initial_epoch=init_epochs,
                    validation_data=val_gen.gen(),
                    validation_steps=len(test_img_info) // config.BATCH_SIZE,
                    use_multiprocessing=True,
                    callbacks=get_call_back())
Пример #6
0
def main(args):
    from tensorflow.python.keras import backend as K
    import tensorflow as tf

    cfg = tf.ConfigProto()
    cfg.gpu_options.allow_growth = True
    session = tf.Session(config=cfg)
    K.set_session(session)
    # 覆盖参数
    config.IMAGES_PER_GPU = 1
    config.GPU_COUNT = 1
    config.DETECTION_MIN_CONFIDENCE = 0.0  # 评估阶段保证有足够的边框输出

    # 加载数据集
    dataset = VocDataset(config.voc_path, class_mapping=config.CLASS_MAPPING)
    dataset.prepare()
    print("len:{}".format(len(dataset.get_image_info_list())))
    test_image_info_list = [
        info for info in dataset.get_image_info_list()
        if info['type'] == args.data_set
    ][:args.eval_num]
    print("len:{}".format(len(test_image_info_list)))
    gen = TestGenerator(test_image_info_list, config.IMAGE_INPUT_SHAPE,
                        config.MEAN_PIXEL)
    # 加载模型
    m = models.frcnn(config, stage='test')
    if args.weight_path is not None:
        m.load_weights(args.weight_path, by_name=True)
    else:
        m.load_weights(config.rcnn_weights, by_name=True)
    # m.summary()
    # 预测边框、得分、类别
    s_time = time.time()
    boxes, scores, class_ids, _, image_metas = m.predict_generator(
        gen, steps=gen.size, verbose=1, workers=4, use_multiprocessing=False)
    print("预测 {} 张图像,耗时:{} 秒".format(len(test_image_info_list),
                                     time.time() - s_time))
    # 去除padding
    image_metas = image_utils.batch_parse_image_meta(image_metas)
    predict_scores = [np_utils.remove_pad(score)[:, 0] for score in scores]
    predict_labels = [np_utils.remove_pad(label)[:, 0] for label in class_ids]
    # 还原检测边框到
    predict_boxes = [
        image_utils.recover_detect_boxes(np_utils.remove_pad(box), window,
                                         scale) for box, window, scale in
        zip(boxes, image_metas['window'], image_metas['scale'])
    ]

    # 以下是评估过程
    annotations = eval_utils.get_annotations(test_image_info_list,
                                             config.NUM_CLASSES)
    detections = eval_utils.get_detections(predict_boxes, predict_scores,
                                           predict_labels, config.NUM_CLASSES)
    average_precisions = eval_utils.voc_eval(annotations,
                                             detections,
                                             iou_threshold=0.5,
                                             use_07_metric=True)
    print("ap:{}".format(average_precisions))
    # 求mean ap 去除背景类
    mAP = np.mean(np.array(list(average_precisions.values()))[1:])
    print("mAP:{}".format(mAP))
    print("整个评估过程耗时:{} 秒".format(time.time() - s_time))