def generate_submission(self):
        test_datapath = '/data/Kaggle/test-png'
        # Get list of files
        img_files = natsorted(glob(join(test_datapath, '*.png')))
        # load files into array
        test_imgs = np.stack(
            [self.LoadImgForTest(f, self.dims) for f in img_files])[...,
                                                                    np.newaxis]
        # Get predicted masks
        tqdm.write('Getting mask predictions...')
        masks = self.model.predict(test_imgs,
                                   batch_size=self.batch_size,
                                   verbose=1)
        # data to write to csv
        submission_data = []
        # process mask
        for ind, cur_file in enumerate(tqdm(img_files)):
            cur_mask = masks[ind, ..., 0]
            cur_im = test_imgs[ind, ..., 0]
            cur_mask = (cur_mask > .5).astype(np.int)
            cur_id = splitfile(cur_file)

            processed_mask = CleanMask_v1(cur_mask)
            lbl_mask, numObj = scipy_label(processed_mask)
            if numObj > 0:
                for label in range(1, numObj + 1):
                    temp_mask = np.zeros_like(cur_mask)
                    temp_mask[lbl_mask == label] = 1
                    temp_mask = cv2.resize(temp_mask.astype(np.float),
                                           (1024, 1024))
                    temp_mask[temp_mask < .5] = 0
                    temp_mask[temp_mask > 0] = 255
                    temp_mask = np.transpose(temp_mask)
                    cur_rle = mask2rle(temp_mask, 1024, 1024)
                    submission_data.append([cur_id, cur_rle])
            else:
                cur_rle = -1
                submission_data.append([cur_id, cur_rle])

        # write to csv
        # generate time-stamped filename
        timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H%M")
        csv_filename = 'TestSubmission_{}'.format(timestamp)
        tqdm.write('Writing csv...')
        with open(csv_filename, mode='w') as f:
            writer = csv.writer(f, delimiter=',')
            for row in tqdm(submission_data):
                writer.writerow(row)

        print('Done')
Exemplo n.º 2
0
def GetSubData(file, label, mask):
    mask = mask[..., 0]
    mask = (mask > .5).astype(np.int)
    fid = splitfile(file)

    if label == 0:
        return [fid, -1]

    processed_mask = CleanMask_v1(mask)
    lbl_mask, numObj = scipy_label(processed_mask)
    if numObj > 0:
        processed_mask[processed_mask > 0] = 255
        processed_mask = np.transpose(processed_mask)
        rle = mask2rle(processed_mask, 1024, 1024)
    else:
        rle = -1
    return [fid, rle]
for ind, cur_file in enumerate(tqdm(img_files)):
    cur_mask = masks[ind, ..., 0]
    cur_im = test_imgs[ind, ..., 0]
    cur_mask = (cur_mask > .5).astype(np.int)
    cur_id = splitfile(cur_file)

    processed_mask = CleanMask_v1(cur_mask)
    lbl_mask, numObj = scipy_label(processed_mask)
    if numObj > 0:
        for label in range(1, numObj + 1):
            temp_mask = np.zeros_like(cur_mask)
            temp_mask[lbl_mask == label] = 1
            temp_mask = cv2.resize(temp_mask.astype(np.float), (1024, 1024))
            temp_mask[temp_mask < .5] = 0
            temp_mask[temp_mask > 0] = 255
            temp_mask = np.transpose(temp_mask)
            cur_rle = mask2rle(temp_mask, 1024, 1024)
            submission_data.append([cur_id, cur_rle])
    else:
        cur_rle = -1
        submission_data.append([cur_id, cur_rle])

# write to csv
tqdm.write('Writing csv...')
with open(submission_filepath, mode='w') as f:
    writer = csv.writer(f, delimiter=',')
    for row in tqdm(submission_data):
        writer.writerow(row)

print('Done')
Exemplo n.º 4
0
# data to write to csv
submission_data = []
# process mask
tqdm.write('Processing masks...')
for ind, cur_file in enumerate(tqdm(img_files)):
    cur_mask = masks[ind, ..., 0]
    cur_im = test_imgs[ind, ..., 0]
    cur_mask = (cur_mask > .5).astype(np.int)
    cur_id = splitfile(cur_file)

    processed_mask = CleanMask_v1(cur_mask)
    lbl_mask, numObj = scipy_label(processed_mask)
    if numObj > 0:
        processed_mask[processed_mask > 0] = 255
        processed_mask = np.transpose(processed_mask)
        cur_rle = mask2rle(processed_mask, 1024, 1024)
    else:
        cur_rle = -1
    submission_data.append([cur_id, cur_rle])

# write to csv
tqdm.write('Writing csv...')
with open(submission_filepath, mode='w', newline='') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(['ImageId', 'EncodedPixels'])
    for data in tqdm(submission_data):
        writer.writerow(data)

# display some images
from VisTools import mask_viewer0
mask_viewer0(test_imgs[:100,...,0],masks[:100,...,0])