Beispiel #1
0
def worker_thread(result_fn_queue, eval_dir, label_dir, pad, offset, channel,
                  steps, relax, all_positive, all_prec_tp, all_true,
                  all_recall_tp):
    while True:
        i, result_fn = result_fn_queue.get()
        if result_fn is None:
            break

        img_id = basename(result_fn).split('pred_')[-1]
        img_id, _ = os.path.splitext(img_id)
        if '.' in img_id:
            img_id = img_id.split('.')[0]
        if len(re.findall('_', img_id)) > 1:
            img_id = '_'.join(img_id.split('_')[1:])
        out_dir = '{}{}'.format(eval_dir, img_id)
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        print(img_id)

        label = cv.imread('{}{}.tif'.format(label_dir, img_id),
                          cv.IMREAD_GRAYSCALE)
        pred = np.load(result_fn)
        label = label[pad + offset - 1:pad + offset - 1 + pred.shape[0],
                      pad + offset - 1:pad + offset - 1 + pred.shape[1]]
        cv.imwrite('{}/label_{}.png'.format(out_dir, img_id), label * 125)

        print('pred_shape: {}'.format(pred.shape))

        for c in six.moves.range(channel):
            for t in six.moves.range(0, steps):
                threshold = 1.0 / steps * t

                pred_vals = np.array(pred[:, :, c] >= threshold,
                                     dtype=np.int32)

                label_vals = np.array(label, dtype=np.int32)
                if channel > 1:
                    label_vals = np.array(label == c, dtype=np.int32)

                all_positive[i, c, t] = np.sum(pred_vals)
                all_prec_tp[i, c, t] = relax_precision(pred_vals, label_vals,
                                                       relax)

                all_true[i, c, t] = np.sum(label_vals)
                all_recall_tp[i, c, t] = relax_recall(pred_vals, label_vals,
                                                      relax)

            pre_rec, breakeven_pt = get_pre_rec(all_positive[i, c],
                                                all_prec_tp[i, c], all_true[i,
                                                                            c],
                                                all_recall_tp[i, c], steps)

            draw_pre_rec_curve(pre_rec, breakeven_pt)
            plt.savefig('{}/pr_curve_{}.png'.format(out_dir, c))
            np.save('{}/pre_rec_{}'.format(out_dir, c), pre_rec)
            cv.imwrite('{}/pred_{}.png'.format(out_dir, c),
                       pred[:, :, c] * 255)

            print(img_id, c, breakeven_pt)
    print('thread finished')
def worker_thread(result_fn_queue):
    while True:
        i, result_fn = result_fn_queue.get()
        if result_fn is None:
            break

        img_id = basename(result_fn).split('pred_')[-1]
        img_id, _ = os.path.splitext(img_id)
        if '.' in img_id:
            img_id = img_id.split('.')[0]
        if len(re.findall('_', img_id)) > 1:
            img_id = '_'.join(img_id.split('_')[1:])
        out_dir = '%s/%s' % (eval_dir, img_id)
        makedirs(out_dir)
        print(img_id)

        label = cv.imread('%s/%s.tif' %
                          (label_dir, img_id), cv.IMREAD_GRAYSCALE)
        pred = np.load(result_fn)
        pred = pred[args.offset:1500 - (60 - args.offset),
                    args.offset:1500 - (60 - args.offset)]
        label = label[args.pad + args.offset:
                      args.pad + args.offset + pred.shape[0],
                      args.pad + args.offset:
                      args.pad + args.offset + pred.shape[1]]
        cv.imwrite('%s/label_%s.png' % (out_dir, img_id), label * 125)

        print('label_shape:', label.shape)
        print('pred_shape:', pred.shape)

        for c in six.moves.range(args.channel):
            for t in six.moves.range(0, args.steps):
                threshold = 1.0 / args.steps * t

                pred_vals = np.array(
                    pred[:, :, c] >= threshold, dtype=np.int32)

                label_vals = np.array(label, dtype=np.int32)
                if args.channel > 1:
                    label_vals = np.array(label == c, dtype=np.int32)

                all_positive[i, c, t] = np.sum(pred_vals)
                all_prec_tp[i, c, t] = relax_precision(
                    pred_vals, label_vals, args.relax)

                all_true[i, c, t] = np.sum(label_vals)
                all_recall_tp[i, c, t] = relax_recall(
                    pred_vals, label_vals, args.relax)

            pre_rec, breakeven_pt = get_pre_rec(
                all_positive[i, c], all_prec_tp[i, c],
                all_true[i, c], all_recall_tp[i, c], args.steps)

            draw_pre_rec_curve(pre_rec, breakeven_pt)
            plt.savefig('%s/pr_curve_%d.png' % (out_dir, c))
            np.save('%s/pre_rec_%d' % (out_dir, c), pre_rec)
            cv.imwrite('%s/pred_%d.png' % (out_dir, c), pred[:, :, c] * 255)

            print(img_id, c, breakeven_pt)
    print('thread finished')
Beispiel #3
0
def worker_thread(result_fn_queue):
    while True:
        i, result_fn = result_fn_queue.get()
        if result_fn is None:
            break

        img_id = basename(result_fn).split('pred_')[-1]
        img_id, _ = os.path.splitext(img_id)
        if '.' in img_id:
            img_id = img_id.split('.')[0]
        if len(re.findall('_', img_id)) > 1:
            img_id = '_'.join(img_id.split('_')[1:])
        out_dir = '%s/%s' % (eval_dir, img_id)
        makedirs(out_dir)
        print(img_id)

        label = cv.imread('%s/%s.tif' %
                          (label_dir, img_id), cv.IMREAD_GRAYSCALE)
        pred = np.load(result_fn)
        pred = pred[args.offset:1500 - (60 - args.offset),
                    args.offset:1500 - (60 - args.offset)]
        label = label[args.pad + args.offset:
                      args.pad + args.offset + pred.shape[0],
                      args.pad + args.offset:
                      args.pad + args.offset + pred.shape[1]]
        cv.imwrite('%s/label_%s.png' % (out_dir, img_id), label * 125)

        print('label_shape:', label.shape)
        print('pred_shape:', pred.shape)

        for c in six.moves.range(args.channel):
            for t in six.moves.range(0, args.steps):
                threshold = 1.0 / args.steps * t

                pred_vals = np.array(
                    pred[:, :, c] >= threshold, dtype=np.int32)

                label_vals = np.array(label, dtype=np.int32)
                if args.channel > 1:
                    label_vals = np.array(label == c, dtype=np.int32)

                all_positive[i, c, t] = np.sum(pred_vals)
                all_prec_tp[i, c, t] = relax_precision(
                    pred_vals, label_vals, args.relax)

                all_true[i, c, t] = np.sum(label_vals)
                all_recall_tp[i, c, t] = relax_recall(
                    pred_vals, label_vals, args.relax)

            pre_rec, breakeven_pt = get_pre_rec(
                all_positive[i, c], all_prec_tp[i, c],
                all_true[i, c], all_recall_tp[i, c], args.steps)

            draw_pre_rec_curve(pre_rec, breakeven_pt)
            plt.savefig('%s/pr_curve_%d.png' % (out_dir, c))
            np.save('%s/pre_rec_%d' % (out_dir, c), pre_rec)
            cv.imwrite('%s/pred_%d.png' % (out_dir, c), pred[:, :, c] * 255)

            print(img_id, c, breakeven_pt)
    print('thread finished')
Beispiel #4
0
def get_relaxed_pre_rec(p_patch, l_patch):
    p_patch = np.array(p_patch, dtype=np.int32)
    l_patch = np.array(l_patch, dtype=np.int32)

    positive = np.sum(p_patch == 1)
    prec_tp = relax_precision(p_patch, l_patch, RELAX)
    true = np.sum(l_patch == 1)
    recall_tp = relax_recall(p_patch, l_patch, RELAX)

    if prec_tp > positive or recall_tp > true:
        print(positive, prec_tp, true, recall_tp)
        sys.exit('Calculation is wrong.')

    return positive, prec_tp, true, recall_tp
def calc_prec_recall(pred, label, t):
    deno = 1.0 / 256 * t
    pred = np.asarray(pred >= deno, dtype=np.int32)
    positive = np.sum(pred == 1)
    true = np.sum(label == 1)
    prec_tp = relax_precision(pred, label, relax)
    recall_tp = relax_recall(pred, label, relax)

    if prec_tp > positive or recall_tp > true:
        print(positive, prec_tp, true, recall_tp)
        sys.exit('Calculation is wrong.')

    prec = prec_tp / float(positive) if positive > 0 else 1.0
    recall = recall_tp / float(true) if true > 0 else 1.0

    return (prec, recall)
def calc_prec_recall(pred, label, t):
    deno = 1.0 / 256 * t
    pred = np.asarray(pred >= deno, dtype=np.int32)
    positive = np.sum(pred == 1)
    true = np.sum(label == 1)
    prec_tp = relax_precision(pred, label, relax)
    recall_tp = relax_recall(pred, label, relax)

    if prec_tp > positive or recall_tp > true:
        print(positive, prec_tp, true, recall_tp)
        sys.exit('Calculation is wrong.')

    prec = prec_tp / float(positive) if positive > 0 else 1.0
    recall = recall_tp / float(true) if true > 0 else 1.0

    return (prec, recall)