示例#1
0
def create_voc_dataset(year, split, segmentation=False, augmented_seg=False):
    """packs a dataset to a protobuf file
    Args:
        year: the year of voc dataset. choice=['07', '12']
        split: split of data, choice=['train', 'val', 'trainval', 'test']
        segmentation: if True, segmentation annotations are encoded augmented_seg
        augmented_seg: if True, encodes extra annotations
        """
    assert not ((year=='07' or split=='val') and augmented_seg==True), \
        'There is no extra segmentation masks for VOC07 or VOC12-val'

    loader = VOCLoader(year, split, segmentation=segmentation, augmented_seg=augmented_seg)
    print("Contains %i files" % len(loader.get_filenames()))
    output_file = os.path.join(DATASETS_ROOT, 'voc%s-%s%s' %
                               (year, split, '-segmentation' * segmentation))
    image_placeholder = tf.placeholder(dtype=tf.uint8)
    encoded_image = tf.image.encode_png(tf.expand_dims(image_placeholder, 2))
    writer = tf.python_io.TFRecordWriter(output_file)
    with tf.Session('') as sess:
        for i, f in enumerate(loader.get_filenames()):
            path = '%sJPEGImages/%s.jpg' % (loader.root, f)
            with tf.gfile.FastGFile(path, 'rb') as ff:
                image_data = ff.read()
            gt_bb, segmentation, gt_cats, w, h, diff = loader.read_annotations(f)
            gt_bb = normalize_bboxes(gt_bb, w, h)
            png_string = sess.run(encoded_image,
                                  feed_dict={image_placeholder: segmentation})
            example = _convert_to_example(path, image_data, gt_bb, gt_cats,       #输入网络的信息
                                          diff, png_string, h, w)
            if i % 100 == 0:
                print("%i files are processed" % i)
            writer.write(example.SerializeToString())

        writer.close()
    print("Done")
示例#2
0
def create_voc_dataset(year, split):
    loader = VOCLoader(year, 'edgeboxes', split)
    print("Contains %i files" % len(loader.get_filenames()))

    output_file = os.path.join(DATASETS_ROOT,
                               'voc%s-%s-proposals' % (year, split))
    writer = tf.python_io.TFRecordWriter(output_file)
    for i, f in enumerate(loader.get_filenames()):
        path = '%sJPEGImages/%s.jpg' % (loader.root, f)
        with tf.gfile.FastGFile(path, 'rb') as ff:
            image_data = ff.read()
        gt_bb, gt_cats, w, h, diff = loader.read_annotations(f, exclude=False)
        gt_bb = normalize_bboxes(gt_bb, w, h)
        proposals = loader.read_proposals(f)
        proposals = normalize_bboxes(proposals, w, h)
        example = _convert_to_example(path, image_data, proposals, gt_bb,
                                      gt_cats, diff, h, w)
        if i % 100 == 0:
            print("%i files are processed" % i)
        writer.write(example.SerializeToString())

    writer.close()
    print("Done")
示例#3
0
def main(argv=None):  # pylint: disable=unused-argument
    assert args.ckpt > 0 or args.batch_eval
    assert args.detect or args.segment, "Either detect or segment should be True"
    if args.trunk == 'resnet50':
        net = ResNet
        depth = 50
    if args.trunk == 'resnet101':
        net = ResNet
        depth = 101
    if args.trunk == 'vgg16':
        net = VGG
        depth = 16

    net = net(config=net_config, depth=depth, training=False)

    if args.dataset == 'voc07' or args.dataset == 'voc07+12':
        loader = VOCLoader('07', 'test')
    if args.dataset == 'voc12':
        loader = VOCLoader('12', 'val', segmentation=args.segment)
    if args.dataset == 'coco':
        loader = COCOLoader(args.split)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=False)) as sess:
        detector = Detector(sess,
                            net,
                            loader,
                            net_config,
                            no_gt=args.no_seg_gt)
        if args.dataset == 'coco':
            tester = COCOEval(detector, loader)
        else:
            tester = Evaluation(detector,
                                loader,
                                iou_thresh=args.voc_iou_thresh)
        if not args.batch_eval:
            detector.restore_from_ckpt(args.ckpt)
            tester.evaluate_network(args.ckpt)
        else:
            log.info('Evaluating %s' % args.run_name)
            ckpts_folder = CKPT_ROOT + args.run_name + '/'
            out_file = ckpts_folder + evaluation_logfile

            max_checked = get_last_eval(out_file)
            log.debug("Maximum checked ckpt is %i" % max_checked)
            with open(out_file, 'a') as f:
                start = max(args.min_ckpt, max_checked + 1)
                ckpt_files = glob(ckpts_folder + '*.data*')
                folder_has_nums = np.array(list((map(filename2num,
                                                     ckpt_files))),
                                           dtype='int')
                nums_available = sorted(
                    folder_has_nums[folder_has_nums >= start])
                nums_to_eval = [nums_available[-1]]
                for n in reversed(nums_available):
                    if nums_to_eval[-1] - n >= args.step:
                        nums_to_eval.append(n)
                nums_to_eval.reverse()

                for ckpt in nums_to_eval:
                    log.info("Evaluation of ckpt %i" % ckpt)
                    tester.reset()
                    detector.restore_from_ckpt(ckpt)
                    res = tester.evaluate_network(ckpt)
                    f.write(res)
                    f.flush()
示例#4
0
def main(argv=None):  # pylint: disable=unused-argument
    net = ResNet
    depth = 50

    loader = VOCLoader('07', 'test')

    net = net(config=net_config, depth=depth, training=False)

    num_classes = 21
    batch_size = args.batch_size
    img_size = args.image_size
    image_ph = tf.placeholder(shape=[1, img_size, img_size, 3],
                              dtype=tf.float32, name='img_ph')
    net.create_trunk(image_ph)
    bboxer = PriorBoxGrid(net_config)

    net.create_multibox_head(num_classes)
    confidence = tf.nn.softmax(tf.squeeze(net.outputs['confidence']))
    location = tf.squeeze(net.outputs['location'])

    good_bboxes = decode_bboxes(location, bboxer.tiling)
    detection_list = []
    score_list = []
    for i in range(1, num_classes):
        class_mask = tf.greater(confidence[:, i], args.conf_thresh)

        class_scores = tf.boolean_mask(confidence[:, i], class_mask)
        class_bboxes = tf.boolean_mask(good_bboxes, class_mask)

        K = tf.minimum(tf.size(class_scores), args.top_k_nms)
        _, top_k_inds = tf.nn.top_k(class_scores, K)
        top_class_scores = tf.gather(class_scores, top_k_inds)
        top_class_bboxes = tf.gather(class_bboxes, top_k_inds)

        final_inds = tf.image.non_max_suppression(top_class_bboxes,
                                                    top_class_scores,
                                                    max_output_size=50,
                                                    iou_threshold=args.nms_thresh)
        final_class_bboxes = tf.gather(top_class_bboxes, final_inds)
        final_scores = tf.gather(top_class_scores, final_inds)

        detection_list.append(final_class_bboxes)
        score_list.append(final_scores)

    net.create_segmentation_head(num_classes)
    segmentation = tf.cast(tf.argmax(tf.squeeze(net.outputs['segmentation']),
                                            axis=-1), tf.int32)
    times = []

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=False)) as sess:


        sess.run(tf.global_variables_initializer())

        ckpt_path = train_dir + '/model.ckpt-%i000' % args.ckpt
        log.debug("Restoring checkpoint %s" % ckpt_path)
        saver = tf.train.Saver(tf.global_variables())
        saver.restore(sess, ckpt_path)
        for i in range(200):
            im = loader.load_image(loader.get_filenames()[i])
            im = imresize(im, (img_size, img_size))
            im = im.reshape((1, img_size, img_size, 3))
            st = time.time()
            sess.run([detection_list, score_list, segmentation], feed_dict={image_ph: im})
            et = time.time()
            if i > 10:
                times.append(et-st)
    m = np.mean(times)
    s = np.std(times)
    fps = 1/m
    log.info("Mean={0:.2f}ms; Std={1:.2f}ms; FPS={2:.1f}".format(m*1000, s*1000, fps))
示例#5
0
parser = argparse.ArgumentParser(description='Analyze detection results of various networks')
parser.add_argument("--min_score", default=0.6, type=float)
parser.add_argument("--max_map", default=1.5, type=float)
parser.add_argument("--net", required=True, choices=['ssd300', 'ssd512', 'frcnn', 'blitz300', 'blitz512', 'blitz300-rpn'])
# parser.add_argument("--x4", default=False, action='store_true')
parser.add_argument("--images", default='', type=str)
parser.add_argument("--write_difficult", default=False, action='store_true')
parser.add_argument("--noshow", default=False, action='store_true')
parser.add_argument("--dump_folder", default='', type=str)
args = parser.parse_args()

show_img = not args.noshow or args.dump_folder != ''

if __name__ == '__main__':
    loader = VOCLoader('07', 'test')
    results = read_cache_detections(loader)
    maps = []
    difficult_ids = []
    if args.images == '':
        images = loader.get_filenames()
    else:
        with open(args.images, 'r') as f:
            images = f.read().split('\n')
        results = {k: results[k] for k in results if k in images}
    for i in range(len(results)):
        img_id = images[i]
        print(img_id)
        gt_bboxes, _, gt_cats, w, h, difficulty = loader.read_annotations(img_id)
        # print('==============================')
        # print("GT: ", ' '.join(loader.ids_to_cats[j] for j in np.unique(gt_cats)))
def main(argv=None):  # pylint: disable=unused-argument
    net = ResNet
    depth = 50

    loader = VOCLoader('07', 'test')

    net = net(config=net_config, depth=depth, training=False)

    num_classes = 21
    batch_size = args.batch_size
    img_size = args.image_size
    image_ph = tf.placeholder(shape=[1, img_size, img_size, 3],
                              dtype=tf.float32,
                              name='img_ph')
    net.create_trunk(image_ph)
    bboxer = PriorBoxGrid(net_config)

    net.create_multibox_head(num_classes)
    confidence = tf.nn.softmax(tf.squeeze(net.outputs['confidence']))
    location = tf.squeeze(net.outputs['location'])

    good_bboxes = decode_bboxes(location, bboxer.tiling)
    detection_list = []
    score_list = []
    for i in range(1, num_classes):
        class_mask = tf.greater(confidence[:, i], args.conf_thresh)

        class_scores = tf.boolean_mask(confidence[:, i], class_mask)
        class_bboxes = tf.boolean_mask(good_bboxes, class_mask)

        K = tf.minimum(tf.size(class_scores), args.top_k_nms)
        _, top_k_inds = tf.nn.top_k(class_scores, K)
        top_class_scores = tf.gather(class_scores, top_k_inds)
        top_class_bboxes = tf.gather(class_bboxes, top_k_inds)

        final_inds = tf.image.non_max_suppression(
            top_class_bboxes,
            top_class_scores,
            max_output_size=50,
            iou_threshold=args.nms_thresh)
        final_class_bboxes = tf.gather(top_class_bboxes, final_inds)
        final_scores = tf.gather(top_class_scores, final_inds)

        detection_list.append(final_class_bboxes)
        score_list.append(final_scores)

    net.create_segmentation_head(num_classes)
    segmentation = tf.cast(
        tf.argmax(tf.squeeze(net.outputs['segmentation']), axis=-1), tf.int32)
    times = []

    # im = cv2.imread("/home/lear/kshmelko/testImage.jpg")
    # im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)/255.0
    # im = im.astype(np.float32)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=False)) as sess:

        sess.run(tf.global_variables_initializer())

        ckpt_path = train_dir + '/model.ckpt-%i000' % args.ckpt
        log.debug("Restoring checkpoint %s" % ckpt_path)
        saver = tf.train.Saver(tf.global_variables())
        saver.restore(sess, ckpt_path)
        for i in range(200):
            im = loader.load_image(loader.get_filenames()[i])
            im = cv2.resize(im, (img_size, img_size))
            im = im.reshape((1, img_size, img_size, 3))
            st = time.time()
            sess.run([detection_list, score_list, segmentation],
                     feed_dict={image_ph: im})
            et = time.time()
            if i > 10:
                times.append(et - st)
    m = np.mean(times)
    s = np.std(times)
    fps = 1 / m
    log.info("Mean={0:.2f}ms; Std={1:.2f}ms; FPS={2:.1f}".format(
        m * 1000, s * 1000, fps))
示例#7
0
def main(argv=None):  # pylint: disable=unused-argument
    assert args.ckpt > 0 or args.batch_eval
    assert args.detect or args.segment, "Either detect or segment should be True"
    if args.trunk == 'resnet50':
        net = ResNet
        depth = 50
    if args.trunk == 'resnet101':
        net = ResNet
        depth = 101
    if args.trunk == 'vgg16':
        net = VGG
        depth = 16

    net = net(config=net_config, depth=depth, training=False)

    if args.dataset == 'voc07' or args.dataset == 'voc07+12':
        loader = VOCLoader('07', 'test')
    if args.dataset == 'voc12':
        loader = VOCLoader('12', 'val', segmentation=args.segment)
    if args.dataset == 'coco':
        loader = COCOLoader(args.split)

    with tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                          log_device_placement=False,
                                          gpu_options=tf.GPUOptions(allow_growth=True,
                                                                    per_process_gpu_memory_fraction=0.2))) as sess:
        if args.use_profile:
            profiler = model_analyzer.Profiler(graph=sess.graph)
            detector = Detector(sess, net, loader, net_config,
                                no_gt=args.no_seg_gt, profiler=profiler)
        else:
            detector = Detector(sess, net, loader, net_config,
                                no_gt=args.no_seg_gt)

        if args.dataset == 'coco':
            tester = COCOEval(detector, loader)
        else:
            tester = Evaluation(detector, loader, iou_thresh=args.voc_iou_thresh)
        if not args.batch_eval:
            detector.restore_from_ckpt(args.ckpt)
            tester.evaluate_network(args.ckpt)
        else:
            log.info('Evaluating %s' % args.run_name)
            ckpts_folder = CKPT_ROOT + args.run_name + '/'
            out_file = ckpts_folder + evaluation_logfile

            max_checked = get_last_eval(out_file)
            log.debug("Maximum checked ckpt is %i" % max_checked)
            with open(out_file, 'a') as f:
                start = max(args.min_ckpt, max_checked+1)
                ckpt_files = glob(ckpts_folder + '*.data*')
                folder_has_nums = np.array(list((map(filename2num, ckpt_files))), dtype='int')
                nums_available = sorted(folder_has_nums[folder_has_nums >= start])
                nums_to_eval = [nums_available[-1]]
                for n in reversed(nums_available):
                    if nums_to_eval[-1] - n >= args.step:
                        nums_to_eval.append(n)
                nums_to_eval.reverse()

                for ckpt in nums_to_eval:
                    log.info("Evaluation of ckpt %i" % ckpt)
                    tester.reset()
                    detector.restore_from_ckpt(ckpt)
                    res = tester.evaluate_network(ckpt)
                    f.write(res)
                    f.flush()

        if args.use_profile:
            profile_scope_builder = option_builder.ProfileOptionBuilder(
                # option_builder.ProfileOptionBuilder.trainable_variables_parameter()
            )
            profile_scope_builder.with_max_depth(4)
            profile_scope_builder.with_min_memory(int(2e6))
            profile_scope_builder.with_step(2)
            profile_scope_builder.select(['bytes'])
            # profile_scope_builder.with_node_names(show_name_regexes=['.*resnet.*', '.*ssd.*'])
            # profile_scope_builder.with_node_names(hide_name_regexes=['.*resnet.*', '.*ssd.*'])
            # profile_scope_builder.order_by('output_bytes')
            detector.profiler.profile_name_scope(profile_scope_builder.build())