コード例 #1
0
ファイル: prepare_data.py プロジェクト: YONGBAAM/SpineSeg
def prepare_line():
    train_data_names = read_data_names('./train_labels')
    test_data_names = read_data_names('./test_labels')

    train_images = read_images('./train_images', data_names=train_data_names)
    train_labels = read_labels('./train_labels')
    # train_data_names = read_data_names('./train_labels')

    test_labels = read_labels('./test_labels')
    test_images = read_images('./test_images', test_data_names)
    # test_data_names = read_data_names('./test_labels')

    for ind, image in enumerate(train_images):
        print(ind)
        H, W, C = image.shape
        lab = train_labels[ind]
        seg = draw_line(lab, H, W)
        np.save('./train_lines/{}.npy'.format(ind), seg)

    for ind, image in enumerate(test_images):
        print('test {}'.format(ind))
        H, W, C = image.shape
        lab = test_labels[ind]
        seg = draw_line(lab, H, W)
        np.save('./test_lines/{}.npy'.format(ind), seg)

    seg = np.load('./train_labels/1.npy')
    ln = np.load('./train_lines/1.npy')
    plt.figure()
    plot_image(seg, segmap=ln)
    plt.show()
コード例 #2
0
def preprocessing():

    #여기서 데이터 프리프로세싱 작업 하기

    #path declaration
    # image_dest_resized = './resized_images'
    # label_dest_resized = './resized_labels'

    image_path = './highres_images'
    label_path = './highres_labels'

    data_names = read_data_names(label_location=label_path)
    labels = read_labels(label_path)

    #testing image and labels
    # plt.figure()
    # for i,ind in enumerate([35,67,25,235]):
    #     plt.subplot(221+i)
    #     image = Image.open(os.path.join(image_path, data_names[ind]))
    #     segmap = np.load(os.path.join(label_path, data_names[ind] + '.npy'))
    #     plot_image(image, coord = labels[ind], segmap=segmap )
    # plt.show()

    # #pad image
    # resized_H = 512
    # desired_W = 256

    label_list = []

    for data_no, data_name in enumerate(data_names):
        image = Image.open(os.path.join(image_path, data_name))
        np_image = np.asarray(image)
        label = labels[data_no]

        H, W = np_image.shape
        resized_H = H
        desired_W = W
        # resize_ratio = resized_H / H
        # left_pad = int((desired_W - int(W * resize_ratio)) / 2)
        # right_pad = desired_W - int(W * resize_ratio) - left_pad
        #
        # label_rev = label.reshape(-1,2)
        # label_rev *= resize_ratio
        # label_rev[:,0] += left_pad
        # label_rev = label_rev.reshape(-1)
        # label_list.append(label_rev)
        #
        # im_resize = image.resize((int(W * resize_ratio), int(resized_H)))
        # im_pad = ImageOps.expand(im_resize, (left_pad, 0, right_pad, 0))
        # im_pad.save(os.path.join(image_dest_resized, data_name))

        segmap = draw_seg(label, resized_H, desired_W)
        np.save(os.path.join(label_path, data_name + '.npy'), segmap)

        if data_no % 100 == 0:
            print(np.asarray(image).shape)
            plt.figure()
            plot_image(image, label, segmap=segmap)
    plt.show()
コード例 #3
0
ファイル: prepare_data.py プロジェクト: YONGBAAM/SpineSeg
def prepare_seg():
    train_data_names = read_data_names('./train_labels')
    test_data_names = read_data_names('./test_labels')

    train_images = read_images('./train_images', data_names=train_data_names)
    train_labels = read_labels('./train_labels')
    # train_data_names = read_data_names('./train_labels')

    test_labels = read_labels('./test_labels')
    test_images = read_images('./test_images', test_data_names)
    # test_data_names = read_data_names('./test_labels')

    for ind, image in enumerate(train_images):
        H, W, C = image.shape
        lab = train_labels[ind]
        seg = draw_seg(lab, H, W)
        np.save('./train_labels/{}.npy'.format(ind), seg)

    for ind, image in enumerate(test_images):
        H, W, C = image.shape
        lab = test_labels[ind]
        seg = draw_seg(lab, H, W)
        np.save('./test_labels/{}.npy'.format(ind), seg)
コード例 #4
0
ファイル: dataset.py プロジェクト: YONGBAAM/SpineChallenge
def get_loader_test(tfm = 'nopad', batch_size = 1, shuffle = False):
    if type(tfm) == type('PAD'):
        if tfm.lower() == 'pad_val' or tfm.lower() == 'pad':
            tfm = PAD_VAL
        elif tfm.lower() == 'nopad_val' or tfm.lower() == 'nopad':
            tfm = NOPAD_VAL
        else:
            tfm = None


    data_path = './test_images'
    label_path = './test_labels'
    labels = read_labels(label_path)
    data_names = read_data_names(label_path)
    dset_test = CoordDataset(data_path, labels, data_names, transform_list=tfm)

    loader_test = DataLoader(dataset=dset_test, batch_size=batch_size, shuffle=shuffle)
    return loader_test
コード例 #5
0
ファイル: dataset.py プロジェクト: YONGBAAM/SpineChallenge
def get_loader_train(tfm = 'nopad', batch_size = 64, shuffle = False):
    if type(tfm) == type('PAD'):
        if tfm.lower() == 'pad_val':
            tfm = PAD_VAL
        elif tfm.lower() == 'nopad_val':
            tfm = NOPAD_VAL
        elif tfm.lower() == 'pad' or tfm.lower() == 'unf' or tfm.lower() == 'uniform':
            tfm = UNF
        elif tfm.lower() == 'nopad':
            tfm = NOPAD
        elif tfm.lower() == 'rhp':
            tfm = RHP
        else:
            tfm = None

    data_path = './train_images'
    label_path = './train_labels'
    labels = read_labels(label_path)
    data_names = read_data_names(label_path)
    dset_train = CoordDataset(data_path, labels, data_names, transform_list=tfm)

    loader_train = DataLoader(dataset=dset_train, batch_size=batch_size, shuffle=shuffle)
    return loader_train
コード例 #6
0
        1, 2), coord_rev[:, 1, :], coord_rev[::-1, 0, :]),
                         axis=0)
    rr, cc = polygon(pol[:, 1], pol[:, 0], seg_image.shape)
    seg_image[rr, cc, :] = 1
    seg_image = seg_image[:, :, 0]
    return seg_image


if __name__ == '__main__':
    author = 'YB'

    #making traintest set
    from os.path import join as osj
    import shutil
    train_data_names = read_data_names('./train_labels')
    train_labels = read_labels('./train_labels')

    test_data_names = read_data_names('./test_labels')
    test_labels = read_labels('./test_labels')

    dest = './trtest_images'
    train_no = len(train_data_names)

    all_labels = []
    all_names = []
    for i, d in enumerate(train_data_names):
        l = train_labels[i]

        shutil.copy(osj('./train_images', d), osj(dest, d))

        all_names.append(d)
コード例 #7
0
    import numpy as np
    import os
    import torch
    import torch.nn as nn
    from torch.utils.data import Dataset, DataLoader
    import torchvision.transforms as transforms

    from label_io import read_data_names, read_labels, plot_image, chw, hwc
    from dataset import CoordDataset
    from label_transform import CoordCustomPad, CoordHorizontalFlip, CoordRandomRotate, \
        CoordLabelNormalize, CoordResize, CoordVerticalFlip

    #def label-to-image
    data_path = './train_images'
    label_path = './train_labels'
    labels = read_labels(label_location=label_path)
    data_names = read_data_names(label_location=label_path)

    batch_size = 8

    # transform = tr.Compose([
    #     tr.RandomRotation(30, expand = False)
    #     tr.ToTensor()
    # ])

    customTransforms = [
        CoordCustomPad(512 / 256),
        CoordResize((512, 256)),
        CoordLabelNormalize()
    ]
コード例 #8
0
ファイル: dataset.py プロジェクト: YONGBAAM/SpineChallenge
def get_loader_train_val(tfm_train = 'nopad', tfm_val = 'nopad', batch_size_tr=64, batch_size_val=1, shuffle = True):
    tfm = tfm_train
    if type(tfm) == type('PAD'):
        if tfm.lower() == 'pad_val':
            tfm = PAD_VAL
        elif tfm.lower() == 'nopad_val':
            tfm = NOPAD_VAL
        elif tfm.lower() == 'pad' or tfm.lower() == 'unf' or tfm.lower() == 'uniform':
            tfm = UNF
        elif tfm.lower() == 'nopad':
            tfm = NOPAD
        elif tfm.lower() == 'rhp':
            tfm = RHP
        else:
            tfm = None
    tfm_train = tfm

    tfm = tfm_val
    if type(tfm) == type('PAD'):
        if tfm.lower() == 'pad_val' or tfm.lower() == 'pad':
            tfm = PAD_VAL
        elif tfm.lower() == 'nopad_val' or tfm.lower() == 'nopad':
            tfm = NOPAD_VAL
        else:
            tfm = None
    tfm_val = tfm


    data_path = './train_images'
    label_path = './train_labels'

    val_ratio = 0.1

    labels = read_labels(label_path)
    data_names = read_data_names(label_path)

    N_all = len(data_names)
    N_val = int(N_all * val_ratio)
    N_train = N_all - N_val
    # get train and validation data set
    data_names_train = []
    data_names_val = []
    labels_train = []
    labels_val = []

    if not os.path.exists(os.path.join('./', 'val_permutation.npy')):
        print('reset permutation')
        permutation = np.random.permutation(N_all)
        np.save(os.path.join('./', 'val_permutation.npy'), permutation)
    else:
        permutation = np.load(os.path.join('./', 'val_permutation.npy'))

    for ind in permutation[:N_train]:
        data_names_train.append(data_names[ind])
        labels_train.append(labels[ind])
    labels_train = np.asarray(labels_train)

    for ind in permutation[N_train:]:
        data_names_val.append(data_names[ind])
        labels_val.append(labels[ind])
    labels_val = np.asarray(labels_val)
    #########################
    dset_train = CoordDataset(data_path, labels_train, data_names_train, transform_list=tfm_train)
    dset_val = CoordDataset(data_path, labels_val, data_names_val, transform_list=tfm_val)

    loader_train = DataLoader(dataset=dset_train, batch_size=batch_size_tr, shuffle=shuffle)
    loader_val = DataLoader(dataset=dset_val, batch_size=batch_size_val, shuffle=False)
    return loader_train ,loader_val
コード例 #9
0
def postprocess_inte(pred_path,
                     images,
                     labels_gt_abs,
                     title=None,
                     save_plot=False,
                     automatic=False,
                     automatic_time=10,
                     degree=6,
                     method1_on=True,
                     method2_on=True,
                     method2_ver=None,
                     original_display=True):
    author = 'YB'

    ############################
    #
    #   plot result vector
    #   if save_plot == True : save image to folder
    #   Do not adjust other settings.
    #
    ############################
    if True:
        preds_rel = read_labels(pred_path, title='labels_pred_rel')
        preds = []
        for ind, pred_rel in enumerate(preds_rel):
            _img = images[ind]
            H, W, _ = _img.shape
            pred_abs = to_absolute(label=pred_rel, H=H, W=W)
            preds.append(pred_abs)
        preds = np.asarray(preds)
        write_labels(preds, label_location=pred_path, title='labels_pred_abs')
    preds = label_sort(preds)

    result_list = []
    for ind, image in enumerate(images):
        print('Saved {}'.format(ind))

        gt = labels_gt_abs[ind]

        pred = preds[ind]
        H, W, C = image.shape
        #gt = to_absolute(gt, H, W)
        fitted_preds, params1 = post_way1(pred, full=True, degree=degree)
        if method2_ver == 0:
            slopes, params2 = post_way2_v0(pred, full=True, degree=degree)
        elif method2_ver == 1:
            slopes, params2 = post_way2_v1(pred, full=True, degree=degree)
        elif method2_ver == 2:
            slopes, params2 = post_way2_v2(pred, full=True, degree=degree)
        elif method2_ver == None:
            slopes, params2 = post_way2(pred, full=True, degree=degree)
        else:
            slopes, params2 = post_way2(pred, full=True, degree=degree)

        gt_angles, gt_pos = calc_angle_old(gt, (H, W), full=True)
        w1_angles, w1_pos = calc_angle_old(fitted_preds, (H, W), full=True)
        w2_angles, w2_pos = calc_angle(slopes,
                                       get_mid_points(pred),
                                       image_H=H,
                                       full=True)

        #convert to smape error
        w1_error = np.sum(
            np.abs(w1_angles - gt_angles)) / np.sum(gt_angles + w1_angles)
        w2_error = np.sum(
            np.abs(w2_angles - gt_angles)) / np.sum(gt_angles + w2_angles)

        w1_mainerr = (np.abs(w1_angles - gt_angles) /
                      (gt_angles + w1_angles + 1e-8))[0]
        w2_mainerr = (np.abs(w2_angles - gt_angles) /
                      (gt_angles + w2_angles + 1e-8))[0]

        pred = pred.reshape(-1, 2, 2)
        delta = pred[:, 0, :] - pred[:, 1, :]
        deltax = np.average(np.abs(delta)[:, 0]) / W
        # show는 테스트
        if save_plot or automatic:

            #make plot
            plt.figure()
            # R for w1
            left_line = params1['left_line']
            right_line = params1['right_line']
            if method1_on:
                plt.plot(left_line[:, 0],
                         left_line[:, 1],
                         color='magenta',
                         alpha=0.5)
                plt.plot(right_line[:, 0],
                         right_line[:, 1],
                         color='magenta',
                         alpha=0.5)

            _fp = fitted_preds.reshape(-1, 2, 2).copy()
            for pos in w1_pos:
                dots = np.average(_fp[2 * pos:2 * pos + 2, :, :], axis=0)
                if method1_on:
                    plt.plot(dots[:, 0], dots[:, 1], 'r')

            # blue for w2
            middle_line = params2['line']
            if method2_on:
                plt.plot(middle_line[:, 0],
                         middle_line[:, 1],
                         color='cyan',
                         alpha=0.5)

            vec_center = params2['middle_vec']
            for pos in w2_pos:
                center = vec_center[pos]
                _slope = slopes[pos]
                norm_slope = _slope / np.linalg.norm(_slope)
                right = center + norm_slope * 50
                left = center - norm_slope * 50
                if method2_on:
                    plt.plot([left[0], right[0]], [left[1], right[1]],
                             color='blue')
            if original_display:
                plot_image(image, coord_red=fitted_preds, coord_gr=gt)
            else:
                plot_image(image, coord_red=fitted_preds)
            if method1_on and not method2_on:
                plt_title = 'a %.1f/p1 %.1f/err%% %.2f' % \
                            (gt_angles[0], w1_angles[0], w1_error * 100)
            else:
                plt_title = 'a %.1f/p1 %.1f/err%% %.2f'%\
                        (gt_angles[0], w2_angles[0], w2_error*100)
            plt.title('test_{}\n'.format(ind) + plt_title)

            if automatic:
                plt.show()
                plt.pause(automatic_time)
                plt.close()

            if save_plot:
                if title is None:
                    title = 'pred'
                if not os.path.exists(os.path.join(pred_path, title)):
                    os.makedirs(os.path.join(pred_path, title))
                plt.savefig(
                    os.path.join(pred_path, title,
                                 title + '_{}'.format(ind) + '.png'))
                plt.close()

        res_dict = dict(gt_angle1=gt_angles[0],
                        gt_angle2=gt_angles[1],
                        gt_angle3=gt_angles[2],
                        w1_angle1=w1_angles[0],
                        w1_angle2=w1_angles[1],
                        w1_angle3=w1_angles[2],
                        w2_angle1=w2_angles[0],
                        w2_angle2=w2_angles[1],
                        w2_angle3=w2_angles[2],
                        w1_mainerr=w1_mainerr,
                        w2_mainerr=w2_mainerr,
                        w1_error=w1_error,
                        w2_error=w2_error,
                        avgdx=deltax,
                        gt_pos=gt_pos,
                        w1_pos=w1_pos,
                        w2_pos=w2_pos)
        result_list.append(res_dict)
    w1_errors = np.array([di['w1_error'] for di in result_list])
    w2_errors = np.array([di['w2_error'] for di in result_list])

    print('Avg : w1_e %.2f / w2_e %.2f ' %
          (np.average(w1_errors), np.average(w2_errors)))

    df = pd.DataFrame(result_list)
    df.to_csv(
        os.path.join(
            pred_path, title, 'result_' + title + '_%.2f' %
            (np.average(w2_errors) * 100) + '.csv'))

    return result_list
コード例 #10
0
    df.to_csv(
        os.path.join(
            pred_path, title, 'result_' + title + '_%.2f' %
            (np.average(w2_errors) * 100) + '.csv'))

    return result_list


if __name__ == '__main__':
    plt.rcParams["figure.figsize"] = (8, 16)
    #test랑 record 변환은 메인에서 하지말고 여기서만 하면됨

    record_label_location = './record_cr_labels'
    record_data_location = './record_cr_images'
    record_data_names = read_data_names(record_label_location)
    record_labels = read_labels(record_label_location)
    record_images = read_images(record_data_location, record_data_names)

    pred_path = './model/TRTEST_ep4278'
    postprocess_inte(pred_path=pred_path,
                     images=record_images,
                     labels_gt_abs=record_labels,
                     title=None,
                     save_plot=True,
                     method2_on=True,
                     method1_on=False,
                     original_display=True)

    ###Validation set

    test_label_location = './test_labels'
コード例 #11
0

if __name__ == '__main__':
    plt.rcParams["figure.figsize"] = (4, 8)
    # test_label_location = './test_labels'
    # test_data_location = './test_images'
    # test_data_names = read_data_names(test_label_location)
    # test_labels = read_labels(test_label_location)
    # test_images = read_images(test_data_location, test_data_names)

    train_label_location = './train_labels'
    train_image_location = './train_images'

    out_path = './plots'

    train_labels = read_labels(train_label_location)
    train_data_names = read_data_names(train_label_location)

    train_images = read_images(train_image_location, train_data_names)

    count = 0

    for ind, image in enumerate(train_images):
        label = train_labels[ind]
        plt.figure()
        #title = 'train_vanila'
        H, W, C = image.shape
        gt = label
        gt_angles, gt_pos = calc_angle_old(gt, (H, W), full=True)
        _fp = gt.reshape(-1, 2, 2).copy()
        for pos in gt_pos:
コード例 #12
0
ファイル: calc.py プロジェクト: YONGBAAM/SpineChallenge
    cobb_angles = np.asarray([pt, mt, tl])
    pos = (pos1, pos2, pos11, pos22)
    #params = dict(pos = pos, mid_points = mid_points, vec_lines = vec_lines, case = case)
    if full:
        return cobb_angles, pos
    else:
        return cobb_angles

from label_io import read_labels, read_images, read_data_names
import pandas as pd
if __name__ == '__main__':
    ####    Testing demo algorithm


    data_names_train = read_data_names('./train_labels')
    labels_now = read_labels('./train_labels')
    labels_ori = read_labels('./train_labels', title ='labels_original')
    labels_m = read_labels('./train_labels', title ='labels_m')
    train_images_location = './train_images'
    train_images = read_images(train_images_location, data_names=data_names_train)

    images = read_images('./train_images', data_names_train)

    # for ind, im in enumerate(images):
    #     lab_m = labels_m[ind]
    #     lab_ori = labels_ori[ind]
    #
    #     if np.sum(np.abs((lab_m - lab_ori)))>=1:
    #         plt.figure()
    #         plot_image(im, coord_red=lab_ori, coord_gr=lab_m)
    #         plt.show()