示例#1
0
def plot_train(train_path='../data/stage1_train/'):
    """
    Plot and save true boundaries of nuclei for each training image
    """

    if not os.path.isdir('../train_images'):
        os.mkdir('../train_images')

    df = pd.read_csv('../data/train_df.csv')
    df = df.set_index('img_id')

    train_ids = os.listdir(train_path)
    dataset_train = KaggleDataset()
    dataset_train.load_shapes(train_ids, train_path)
    dataset_train.prepare()

    image_ids = dataset_train.image_ids
    for mm, image_id in tqdm(enumerate(image_ids)):

        image = dataset_train.load_image(image_id, color='RGB')
        masks, class_ids = dataset_train.load_mask(image_id)
        fig = plot_boundary(image, true_masks=masks)
        fig.savefig('../train_images/train_' + str(mm) + '.png',
                    bbox_inches='tight')
        plt.close()
示例#2
0
def pred_n_plot_test(model,
                     config,
                     test_path='../data/stage2_test_final/',
                     save_plots=False):
    """
    Predicts nuclei for each image, draws the boundaries and saves in images folder.

    """
    # Create images folder if doesn't exist.
    if not os.path.isdir('../images'):
        os.mkdir('../images')

    # Load test dataset
    test_ids = os.listdir(test_path)
    dataset_test = KaggleDataset()
    dataset_test.load_shapes(test_ids, test_path)
    dataset_test.prepare()

    new_test_ids = []
    rles = []
    id = 0
    # No masks prediction counter
    no_masks = 0

    for mm, image_id in tqdm(enumerate((dataset_test.image_ids))):
        # Load the image
        print("image_id ", image_id)
        image = dataset_test.load_image(image_id, color=config.IMAGE_COLOR)

        # Image name for submission rows.
        image_id = dataset_test.image_info[image_id]['img_name']
        height, width = image.shape[:2]
        print(image_id)
        # result = ensemble_prediction(model, config, image)
        result = cluster_prediction(model, config, image)
        # result = model.detect([image], verbose=0, mask_threshold=config.DETECTION_MASK_THRESHOLD)[0]

        # Clean overlaps and apply some post-processing
        result = postprocess_masks(result, image)
        # If there is no masks then try to predict on scaled image
        if result['masks'].sum() < 2:
            H, W = image.shape[:2]
            scaled_img = np.zeros([4 * H, 4 * W, 3], np.uint8)
            scaled_img[:H, :W] = image
            result = cluster_prediction(model, config, scaled_img)
            result['masks'] = result['masks'][:H, :W]
            result = postprocess_masks(result, image)

        if result['masks'].sum() < 1:
            no_masks += 1

        # RLE encoding
        rle = list(prob_to_rles(result['masks'], height, width))
        new_test_ids.extend([image_id] * len(rle))
        rles.extend(rle)
        # if (mm+1) % 100 == 0:
        #     print('Number of completed images', mm+1)
        print(type(result['masks']))
        print("shape_of_something ", result['masks'].shape)
        print("type_of_image ", type(image))
        print("image_shape ", image.shape)
        print("image_id ", image_id)
        print(image_id.split('_')[0])
        if save_plots:
            fig = plt.figure()
            gs = gridspec.GridSpec(1, 1)
            plot_boundary(image,
                          true_masks=None,
                          pred_masks=result['masks'],
                          ax=fig.add_subplot(gs[0]))

            fig.savefig('../images/' + str(image_id) + '.png',
                        bbox_inches='tight')

            # plotting and saving the boundaries
            fig2 = plt.figure()
            gs = gridspec.GridSpec(1, 1)
            msk = np.zeros(image.shape)
            id = plot_boundary_mask(msk,
                                    true_masks=None,
                                    pred_masks=result['masks'],
                                    ax=fig2.add_subplot(gs[0]),
                                    image_id=image_id,
                                    id=id)

            fig2.savefig('../masks2/' + str(image_id) + '.png',
                         bbox_inches='tight')

            plt.close()

    sub = pd.DataFrame()
    sub['ImageId'] = new_test_ids
    sub['EncodedPixels'] = pd.Series(rles).apply(
        lambda x: ' '.join(str(y) for y in x))

    fname = '../data/' + test_path.split('/')[-2] + '_submission.csv'
    sub.to_csv(fname, index=False)

    print('Number of rows:', len(sub))
    print('No mask prediction for', no_masks, 'images')
示例#3
0
    config.IMAGES_PER_GPU = 1
    config.BATCH_SIZE = 1
    config.display()

    # Predict using the last weights in training directory
    model = get_model(config)

    # Predict using pre-trained weights
    # model = get_model(config, model_path='../data/kaggle_bowl.h5')

    # Ininitialize validation dataset
    train_path = '/home/videsh/Downloads/Chandan/data_science_bowl_data/stage1_train/'
    train_list, val_list = train_validation_split(train_path,
                                                  seed=11,
                                                  test_size=0.1)
    dataset_val = KaggleDataset()
    dataset_val.load_shapes(val_list, train_path)
    dataset_val.prepare()

    # initialize stage 1  testing dataset
    dataset_val = KaggleDataset()
    val_path = '/home/videsh/Downloads/Chandan/data_science_bowl_data/stage1_test/'
    val_list = os.listdir(val_path)
    dataset_val.load_shapes(val_list, val_path)
    dataset_val.prepare()
    # Evaluate the model performance and plot boundaries for the predictions
    # eval_n_plot_val(model, config, dataset_val, save_plots=True)

    # Predict and plot boundaries for stage1 test
    # ls = os.listdir('/home/videsh/Downloads/Chandan/data science bowl data/small_test_data/')
    # for f in ls: