Esempio n. 1
0
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()
Esempio n. 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()
Esempio n. 3
0
    def validate(self, title_if_plot_save=None):
        #클래스 함수에는 이런 지저분한거 없게 하자!!!!
        self.model.eval()
        val_losses = []
        val_labels = []
        img_list = []
        true_labels = []
        for val_data in self.loader_val:
            self.model.zero_grad()
            imgs = val_data['image'].to(self.device, dtype=torch.float)
            labels = val_data['label'].to(self.device, dtype=torch.float)
            out = self.model(imgs)
            loss = self.criterion(out, labels)
            val_losses.append(loss.item())

            #for save validation
            val_labels.append(np.asarray(out.cpu().detach()))
            img_list.append(np.asarray(imgs.cpu().detach()))
            true_labels.append(np.asarray(labels.cpu().detach()))

        val_labels = np.concatenate(val_labels, axis=0)
        imgs = np.concatenate(img_list, axis=0)
        true_labels = np.concatenate(true_labels, axis=0)

        if title_if_plot_save is not None:
            # validate data 검증
            perm = np.random.permutation(self.no_val)

            plt.figure()
            for i in range(8):
                ind = perm[i]
                plt.subplot(241 + i)
                plot_image(imgs[ind],
                           segmap=val_labels[ind],
                           segmap_ref=true_labels[ind])
                plt.title('val {}'.format(perm[i]))
            plt.savefig(
                os.path.join(self.model_save_dest,
                             title_if_plot_save + '.png'))
            plt.close()

        return val_losses
Esempio n. 4
0
    def test(self,
             test_loader=None,
             load_model_name=None,
             title=None,
             save_image=False):

        ##Getting save path and model load
        if load_model_name is not None:
            up_path = self.model_save_dest.split('\\')
            up_path = '/'.join(up_path[:-1])
            folder = load_model_name.split('ep')[0]
            folder = folder[:-1]
            ep_no = load_model_name.split('ep')[1].split('_')[0]
            result_save_path = up_path + '/' + folder + '_ep' + ep_no
            self.load_model(load_model_name)
        else:
            #no load, existing model
            result_save_path = self.model_save_dest

        if test_loader is None:
            test_loader = self.loader_val

        if not os.path.exists(result_save_path):
            os.makedirs(result_save_path)
        print('Save Relative labels [size:512*256] in {}'.format(
            result_save_path))

        test_crit = nn.MSELoss()

        self.model.eval()
        test_losses = []
        test_labels = []
        img_list = []
        true_labels = []
        for test_data in test_loader:
            self.model.zero_grad()
            imgs = test_data['image'].to(self.device, dtype=torch.float)
            labels = test_data['label'].to(self.device, dtype=torch.float)
            out = self.model(imgs)
            loss = test_crit(out, labels)
            test_losses.append(loss.item())

            # for save validation
            test_labels.append(np.asarray(out.cpu().detach()))
            img_list.append(np.asarray(imgs.cpu().detach()))
            true_labels.append(np.asarray(labels.cpu().detach()))

        test_labels = np.concatenate(test_labels, axis=0)
        imgs = np.concatenate(img_list, axis=0)
        true_labels = np.concatenate(true_labels, axis=0)

        if title is None:
            title = 'test'

        #save image result for test
        if save_image:
            for ind in range(imgs.shape[0]):
                plt.figure()
                plot_image(imgs[ind],
                           segmap=test_labels[ind],
                           segmap_ref=true_labels[ind])
                plt.title(title + '_{}'.format(ind))
                plt.savefig(
                    os.path.join(result_save_path,
                                 title + '_{}.png'.format(ind)))
                plt.close()

        #save absolute label result
        for ind, tl in enumerate(test_labels):
            np.save(osj(result_save_path, '{}.npy'.format(ind)), tl)

        print('test MSE loss %.2e' % (np.average(test_losses)))
        return test_losses
Esempio n. 5
0
        CoordLabelNormalize()
    ]

    ###############################
    #   Get target loader
    tloader = get_loader_train(tfm = RH_E, batch_size=64, shuffle = False)


    index = 0
    for testdata in tloader:
        imgs = testdata['image'].cpu().detach().numpy()
        labs = testdata['label'].cpu().detach().numpy()
        for i, img in enumerate(imgs):
            lab = labs[i]
            plt.figure()
            plot_image(img, coord_red= lab)
            plt.title('train {}'.format(index))
            plt.show()


#
# class SpineDataset(Dataset):
#     def __init__(self, data_location, label_location, data_names, coords_rel, transform = None, is_segment = True):
#         super(SpineDataset).__init__()
#         self.data_location = data_location
#         self.label_location = label_location
#         self.coords_rel = coords_rel
#         self.data_names = data_names
#         self.size = len(data_names)
#         self.transform = transform
#         self.is_segment = is_segment
Esempio n. 6
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
Esempio n. 7
0
    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:
            dots = np.average(_fp[2 * pos:2 * pos + 2, :, :], axis=0)
            if True:
                plt.plot(dots[:, 0], dots[:, 1], 'g')

        title = 'train_demo'
        plt.title('{}_'.format(ind) + 'p {}/{}/{}/{}'.format(*gt_pos) + '\n' +
                  'a %.1f/%.1f/%.1f' %
                  (gt_angles[0], gt_angles[1], gt_angles[2]))
        plot_image(image, coord_gr=label)
        plt.savefig(os.path.join(out_path, title + '_{}'.format(ind) + '.png'))
        if count < 5:
            count += 1
            plt.show()

        plt.close()