Esempio n. 1
0
def eval():
    with tf.name_scope('test'):
        with tf.variable_scope(tf.get_variable_scope(),
                               reuse=True):  # the variables has been created in config.init_config
            image = tf.placeholder(dtype=tf.int32, shape=[None, None, 3])
            image_shape = tf.placeholder(dtype=tf.int32, shape=[3, ])
            processed_image, _, _, _, _ = ssd_vgg_preprocessing.preprocess_image(image, None, None, None, None,
                                                                                 out_shape=config.image_shape,
                                                                                 data_format=config.data_format,
                                                                                 is_training=False)
            b_image = tf.expand_dims(processed_image, axis=0)
            b_shape = tf.expand_dims(image_shape, axis=0)
            net = seglink_symbol.SegLinkNet(inputs=b_image, data_format=config.data_format)
            bboxes_pred = seglink.tf_seglink_to_bbox(net.seg_scores, net.link_scores,
                                                     net.seg_offsets,
                                                     image_shape=b_shape,
                                                     seg_conf_threshold=config.seg_conf_threshold,
                                                     link_conf_threshold=config.link_conf_threshold)

    image_names = util.io.ls(FLAGS.dataset_dir)

    sess_config = tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)
    if FLAGS.gpu_memory_fraction < 0:
        sess_config.gpu_options.allow_growth = True
    elif FLAGS.gpu_memory_fraction > 0:
        sess_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction

    checkpoint_dir = util.io.get_dir(FLAGS.checkpoint_path)
    logdir = util.io.join_path(FLAGS.checkpoint_path, 'test', FLAGS.dataset_name + '_' + FLAGS.dataset_split_name)

    saver = tf.train.Saver()
    if util.io.is_dir(FLAGS.checkpoint_path):
        checkpoint = util.tf.get_latest_ckpt(FLAGS.checkpoint_path)
    else:
        checkpoint = FLAGS.checkpoint_path

    tf.logging.info('testing', checkpoint)

    with tf.Session(config=sess_config) as sess:
        saver.restore(sess, checkpoint)
        checkpoint_name = util.io.get_filename(str(checkpoint))
        dump_path = util.io.join_path(logdir, checkpoint_name,
                                      'seg_link_conf_th_%f_%f' % (
                                      config.seg_conf_threshold, config.link_conf_threshold))

        txt_path = util.io.join_path(dump_path, 'txt')
        zip_path = util.io.join_path(dump_path, '%s_seg_link_conf_th_%f_%f.zip' % (
        checkpoint_name, config.seg_conf_threshold, config.link_conf_threshold))

        # write detection result as txt files
        def write_result_as_txt(image_name, bboxes, path):
            filename = util.io.join_path(path, 'res_%s.txt' % (image_name))
            lines = []
            for b_idx, bbox in enumerate(bboxes):
                values = [int(v) for v in bbox]
                line = "%d, %d, %d, %d, %d, %d, %d, %d\n" % tuple(values)
                lines.append(line)
            util.io.write_lines(filename, lines)
            print('result has been written to:', filename)

        for iter, image_name in enumerate(image_names):
            image_data = util.img.imread(util.io.join_path(FLAGS.dataset_dir, image_name), rgb=True)
            image_name = image_name.split('.')[0]
            image_bboxes = sess.run([bboxes_pred], feed_dict={image: image_data, image_shape: image_data.shape})
            print('%d/%d: %s' % (iter + 1, len(image_names), image_name))
            write_result_as_txt(image_name, image_bboxes[0], txt_path)
Esempio n. 2
0
def eval(dataset):
    dict_metrics = {} 
    checkpoint_dir = util.io.get_dir(FLAGS.checkpoint_path)
    logdir = util.io.join_path(checkpoint_dir, 
                               'eval',  
                               "%s_%s"%(FLAGS.dataset_name, FLAGS.dataset_split_name))
    
    global_step = slim.get_or_create_global_step()
    with tf.name_scope('evaluation_%dx%d'%(FLAGS.eval_image_height, FLAGS.eval_image_width)):
        with tf.variable_scope(tf.get_variable_scope(), reuse = True):# the variables has been created in config.init_config
            # get input tensor
            image, seg_label, seg_loc, link_gt, filename, shape, gignored, gxs, gys = read_dataset(dataset)
            # expand dim if needed
            b_image =  tf.expand_dims(image, axis = 0);
            b_seg_label = tf.expand_dims(seg_label, axis = 0)
            b_seg_loc = tf.expand_dims(seg_loc, axis = 0)
            b_link_gt = tf.expand_dims(link_gt, axis = 0)
            b_shape = tf.expand_dims(shape, axis = 0)
            
            # build seglink loss
            net = seglink_symbol.SegLinkNet(inputs = b_image, data_format = config.data_format)
            net.build_loss(seg_labels = b_seg_label, 
                           seg_offsets = b_seg_loc, 
                           link_labels = b_link_gt,
                           do_summary = False) # the summary will be added in the following lines
            
            # gather seglink losses
            losses = tf.get_collection(tf.GraphKeys.LOSSES)
            assert len(losses) ==  3  # 3 is the number of seglink losses: seg_cls, seg_loc, link_cls
            for loss in tf.get_collection(tf.GraphKeys.LOSSES):
                dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss)
            
            seglink_loss = tf.add_n(losses)
            dict_metrics['seglink_loss'] = slim.metrics.streaming_mean(seglink_loss)
            
            # Add metrics to summaries.
            for name, metric in dict_metrics.items():
                tf.summary.scalar(name, metric[0])
            
            # shape = (height, width, channels) when format = NHWC TODO
            gxs = gxs * tf.cast(shape[1], gxs.dtype)
            gys = gys * tf.cast(shape[0], gys.dtype)
            if FLAGS.do_grid_search:
                # grid search            
                seg_ths = np.arange(0.5, 0.91, 0.1)
                link_ths = seg_ths
            else:
                seg_ths = [FLAGS.seg_conf_threshold]
                link_ths = [FLAGS.link_conf_threshold]
            
            eval_result_path = util.io.join_path(logdir, 'eval_on_%s_%s.log'%(FLAGS.dataset_name, FLAGS.dataset_split_name))
            for seg_th in seg_ths:
                for link_th in link_ths:
                    config._set_det_th(seg_th, link_th)
                    
                    eval_result_msg = 'seg_conf_threshold=%f, link_conf_threshold = %f, '\
                                            %(config.seg_conf_threshold, config.link_conf_threshold)
                    eval_result_msg += 'iter = %r, recall = %r, precision = %f, fmean = %r'
                    
                    with tf.name_scope('seglink_conf_th_%f_%f'\
                                       %(config.seg_conf_threshold, config.link_conf_threshold)):
                        # decode seglink to bbox output, with absolute length, instead of being within [0,1]
                        bboxes_pred = seglink.tf_seglink_to_bbox(net.seg_scores, net.link_scores, net.seg_offsets,
                                                                  b_shape, seg_conf_threshold = seg_th, link_conf_threshold = link_th)
#                         bboxes_pred = tf.Print(bboxes_pred, [tf.shape(bboxes_pred)], '%f_%f, shape of bboxes = '%(seg_th, link_th))
                        # calculate true positive and false positive
                        # the xs and ys from tfrecord is 0~1, resize them to absolute length before matching.
                        num_gt_bboxes, tp, fp = tfe_bboxes.bboxes_matching(bboxes_pred, gxs, gys, gignored)
                        tp_fp_metric = tfe_metrics.streaming_tp_fp_arrays(num_gt_bboxes, tp, fp)
                        dict_metrics['tp_fp_%f_%f'%(config.seg_conf_threshold, config.link_conf_threshold)] = (tp_fp_metric[0], tp_fp_metric[1])
                         
                        # precision and recall
                        precision, recall = tfe_metrics.precision_recall(*tp_fp_metric[0])
                         
                        fmean = tfe_metrics.fmean(precision, recall)
                        fmean = util.tf.Print(fmean, data = [global_step, recall, precision, fmean], 
                                                msg = eval_result_msg, 
                                                file = eval_result_path, mode = 'a')
                        fmean = tf.Print(fmean, [recall, precision, fmean], '%f_%f, Recall, Precision, Fmean = '%(seg_th, link_th))
                        tf.summary.scalar('Precision', precision)
                        tf.summary.scalar('Recall', recall)
                        tf.summary.scalar('F-mean', fmean)
            
    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(dict_metrics)

    
    sess_config = tf.ConfigProto(log_device_placement = False, allow_soft_placement = True)
    if FLAGS.gpu_memory_fraction < 0:
        sess_config.gpu_options.allow_growth = True
    elif FLAGS.gpu_memory_fraction > 0:
        sess_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction;
    
    # Variables to restore: moving avg. or normal weights.
    if FLAGS.using_moving_average:
        variable_averages = tf.train.ExponentialMovingAverage(
                FLAGS.moving_average_decay)
        variables_to_restore = variable_averages.variables_to_restore(
                slim.get_model_variables())
        variables_to_restore[global_step.op.name] = global_step
    else:
        variables_to_restore = slim.get_variables_to_restore()

    if util.io.is_dir(FLAGS.checkpoint_path):
        slim.evaluation.evaluation_loop(
            master = '',
            eval_op=list(names_to_updates.values()),
            num_evals=dataset.num_samples,
            variables_to_restore=variables_to_restore,
            checkpoint_dir = checkpoint_dir,
            logdir = logdir,
            session_config=sess_config)
    else:
        slim.evaluation.evaluate_once(
            master = '',
            eval_op=list(names_to_updates.values()),
            variables_to_restore=variables_to_restore,
            num_evals=2,#dataset.num_samples,
            checkpoint_path = FLAGS.checkpoint_path,
            logdir = logdir,
            session_config=sess_config)
Esempio n. 3
0
def eval(dataset):
    dict_metrics = {}
    checkpoint_dir = util.io.get_dir(FLAGS.checkpoint_path)
    logdir = util.io.join_path(
        checkpoint_dir, 'eval',
        "%s_%s" % (FLAGS.dataset_name, FLAGS.dataset_split_name))

    global_step = slim.get_or_create_global_step()
    with tf.name_scope('evaluation_%dx%d' %
                       (FLAGS.eval_image_height, FLAGS.eval_image_width)):
        with tf.variable_scope(tf.get_variable_scope(
        ), reuse=True):  # the variables has been created in config.init_config
            # get input tensor
            image, seg_label, seg_loc, link_gt, filename, shape, gignored, gxs, gys = read_dataset(
                dataset)
            # expand dim if needed
            b_image = tf.expand_dims(image, axis=0)
            b_seg_label = tf.expand_dims(seg_label, axis=0)
            b_seg_loc = tf.expand_dims(seg_loc, axis=0)
            b_link_gt = tf.expand_dims(link_gt, axis=0)
            b_shape = tf.expand_dims(shape, axis=0)

            # build seglink loss
            net = seglink_symbol.SegLinkNet(inputs=b_image,
                                            data_format=config.data_format)
            net.build_loss(
                seg_labels=b_seg_label,
                seg_offsets=b_seg_loc,
                link_labels=b_link_gt,
                do_summary=False
            )  # the summary will be added in the following lines

            # gather seglink losses
            losses = tf.get_collection(tf.GraphKeys.LOSSES)
            assert len(
                losses
            ) == 3  # 3 is the number of seglink losses: seg_cls, seg_loc, link_cls
            for loss in tf.get_collection(tf.GraphKeys.LOSSES):
                dict_metrics[loss.op.name] = slim.metrics.streaming_mean(loss)

            seglink_loss = tf.add_n(losses)
            dict_metrics['seglink_loss'] = slim.metrics.streaming_mean(
                seglink_loss)

            # Add metrics to summaries.
            for name, metric in dict_metrics.items():
                tf.summary.scalar(name, metric[0])

            # shape = (height, width, channels) when format = NHWC TODO
            gxs = gxs * tf.cast(shape[1], gxs.dtype)
            gys = gys * tf.cast(shape[0], gys.dtype)
            if FLAGS.do_grid_search:
                # grid search
                seg_ths = np.arange(0.5, 0.91, 0.1)
                link_ths = seg_ths
            else:
                seg_ths = [FLAGS.seg_conf_threshold]
                link_ths = [FLAGS.link_conf_threshold]

            eval_result_path = util.io.join_path(
                logdir, 'eval_on_%s_%s.log' %
                (FLAGS.dataset_name, FLAGS.dataset_split_name))
            for seg_th in seg_ths:
                for link_th in link_ths:
                    config._set_det_th(seg_th, link_th)

                    eval_result_msg = 'seg_conf_threshold=%f, link_conf_threshold = %f, '\
                                            %(config.seg_conf_threshold, config.link_conf_threshold)
                    eval_result_msg += 'iter = %r, recall = %r, precision = %f, fmean = %r'

                    with tf.name_scope('seglink_conf_th_%f_%f'\
                                       %(config.seg_conf_threshold, config.link_conf_threshold)):
                        # decode seglink to bbox output, with absolute length, instead of being within [0,1]
                        bboxes_pred = seglink.tf_seglink_to_bbox(
                            net.seg_scores,
                            net.link_scores,
                            net.seg_offsets,
                            b_shape,
                            seg_conf_threshold=seg_th,
                            link_conf_threshold=link_th)
                        #                         bboxes_pred = tf.Print(bboxes_pred, [tf.shape(bboxes_pred)], '%f_%f, shape of bboxes = '%(seg_th, link_th))
                        # calculate true positive and false positive
                        # the xs and ys from tfrecord is 0~1, resize them to absolute length before matching.
                        num_gt_bboxes, tp, fp = tfe_bboxes.bboxes_matching(
                            bboxes_pred, gxs, gys, gignored)
                        tp_fp_metric = tfe_metrics.streaming_tp_fp_arrays(
                            num_gt_bboxes, tp, fp)
                        dict_metrics['tp_fp_%f_%f' %
                                     (config.seg_conf_threshold,
                                      config.link_conf_threshold)] = (
                                          tp_fp_metric[0], tp_fp_metric[1])

                        # precision and recall
                        precision, recall = tfe_metrics.precision_recall(
                            *tp_fp_metric[0])

                        fmean = tfe_metrics.fmean(precision, recall)
                        fmean = util.tf.Print(
                            fmean,
                            data=[global_step, recall, precision, fmean],
                            msg=eval_result_msg,
                            file=eval_result_path,
                            mode='a')
                        fmean = tf.Print(
                            fmean, [recall, precision, fmean],
                            '%f_%f, Recall, Precision, Fmean = ' %
                            (seg_th, link_th))
                        tf.summary.scalar('Precision', precision)
                        tf.summary.scalar('Recall', recall)
                        tf.summary.scalar('F-mean', fmean)

    names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(
        dict_metrics)

    sess_config = tf.ConfigProto(log_device_placement=False,
                                 allow_soft_placement=True)
    if FLAGS.gpu_memory_fraction < 0:
        sess_config.gpu_options.allow_growth = True
    elif FLAGS.gpu_memory_fraction > 0:
        sess_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction

    # Variables to restore: moving avg. or normal weights.
    if FLAGS.using_moving_average:
        variable_averages = tf.train.ExponentialMovingAverage(
            FLAGS.moving_average_decay)
        variables_to_restore = variable_averages.variables_to_restore(
            slim.get_model_variables())
        variables_to_restore[global_step.op.name] = global_step
    else:
        variables_to_restore = slim.get_variables_to_restore()

    if util.io.is_dir(FLAGS.checkpoint_path):
        slim.evaluation.evaluation_loop(
            master='',
            eval_op=list(names_to_updates.values()),
            num_evals=dataset.num_samples,
            variables_to_restore=variables_to_restore,
            checkpoint_dir=checkpoint_dir,
            logdir=logdir,
            session_config=sess_config)
    else:
        slim.evaluation.evaluate_once(
            master='',
            eval_op=list(names_to_updates.values()),
            variables_to_restore=variables_to_restore,
            num_evals=500,  #dataset.num_samples,
            checkpoint_path=FLAGS.checkpoint_path,
            logdir=logdir,
            session_config=sess_config)
Esempio n. 4
0
def eval():
    
    with tf.name_scope('test'):
        with tf.variable_scope(tf.get_variable_scope(), reuse = True):# the variables has been created in config.init_config
            image = tf.placeholder(dtype=tf.int32, shape = [None, None, 3])
            image_shape = tf.placeholder(dtype = tf.int32, shape = [3, ])
            processed_image, _, _, _, _ = ssd_vgg_preprocessing.preprocess_image(image, None, None, None, None, 
                                                       out_shape = config.image_shape,
                                                       data_format = config.data_format, 
                                                       is_training = False)
            b_image = tf.expand_dims(processed_image, axis = 0)
            b_shape = tf.expand_dims(image_shape, axis = 0)
            net = seglink_symbol.SegLinkNet(inputs = b_image, data_format = config.data_format)
            bboxes_pred = seglink.tf_seglink_to_bbox(net.seg_scores, net.link_scores, 
                                                     net.seg_offsets, 
                                                     image_shape = b_shape, 
                                                     seg_conf_threshold = config.seg_conf_threshold,
                                                     link_conf_threshold = config.link_conf_threshold)

    image_names = util.io.ls(FLAGS.dataset_dir)
    
    sess_config = tf.ConfigProto(log_device_placement = False, allow_soft_placement = True)
    if FLAGS.gpu_memory_fraction < 0:
        sess_config.gpu_options.allow_growth = True
    elif FLAGS.gpu_memory_fraction > 0:
        sess_config.gpu_options.per_process_gpu_memory_fraction = FLAGS.gpu_memory_fraction;
    
    checkpoint_dir = util.io.get_dir(FLAGS.checkpoint_path)
    logdir = util.io.join_path(FLAGS.checkpoint_path, 'test', FLAGS.dataset_name + '_' +FLAGS.dataset_split_name)
    
    saver = tf.train.Saver()
    if util.io.is_dir(FLAGS.checkpoint_path):
        checkpoint = util.tf.get_latest_ckpt(FLAGS.checkpoint_path)
    else:
        checkpoint = FLAGS.checkpoint_path
        
    tf.logging.info('testing', checkpoint)

    with tf.Session(config = sess_config) as sess:
        saver.restore(sess, checkpoint)
        checkpoint_name = util.io.get_filename(str(checkpoint));
        dump_path = util.io.join_path(logdir, checkpoint_name, 
                                      'seg_link_conf_th_%f_%f'%(config.seg_conf_threshold, config.link_conf_threshold))
        
        txt_path = util.io.join_path(dump_path,'txt')
        zip_path = util.io.join_path(dump_path, '%s_seg_link_conf_th_%f_%f.zip'%(checkpoint_name, config.seg_conf_threshold, config.link_conf_threshold))
        
        # write detection result as txt files
        def write_result_as_txt(image_name, bboxes, path):
          filename = util.io.join_path(path, 'res_%s.txt'%(image_name))
          lines = []
          for b_idx, bbox in enumerate(bboxes):
                values = [int(v) for v in bbox]
                line = "%d, %d, %d, %d, %d, %d, %d, %d\n"%tuple(values)
                lines.append(line)
          util.io.write_lines(filename, lines)
          print 'result has been written to:', filename
          
        for iter, image_name in enumerate(image_names):
            image_data = util.img.imread(util.io.join_path(FLAGS.dataset_dir, image_name), rgb = True)
            image_name = image_name.split('.')[0]
            image_bboxes = sess.run([bboxes_pred], feed_dict = {image:image_data, image_shape:image_data.shape})
            print '%d/%d: %s'%(iter + 1, len(image_names), image_name)
            write_result_as_txt(image_name, image_bboxes[0], txt_path)
                
        # create zip file for icdar2015
        cmd = 'cd %s;zip -j %s %s/*'%(dump_path, zip_path, txt_path);
        print cmd
        print util.cmd.cmd(cmd);
        print "zip file created: ", util.io.join_path(dump_path, zip_path)