Kaggle Pytorch run length encoding
Im working on the DSB problem in pytorch,I have my predictions but im not sure how to get those into the run length encoding format that is required for the submission,In short this is what it is

===================================================================


In order to reduce the submission file size, our metric uses run-length encoding on the pixel values. Instead of submitting an exhaustive list of indices for your segmentation, you will submit pairs of values that contain a start position and a run length. E.g. '1 3' implies starting at pixel 1 and running a total of 3 pixels (1,2,3).

The competition format requires a space delimited list of pairs. For example, '1 3 10 5' implies pixels 1,2,3,10,11,12,13,14 are to be included in the mask. The pixels are one-indexed and numbered from top to bottom, then left to right: 1 is pixel (1,1), 2 is pixel (2,1), etc.

===================================================================


I get the predictions like this 

model = model.eval()
for data in testdataloader:
    data = t.autograd.Variable(data.cuda(device = 1))
    pred = model(data)


but now that i have my prediction im not sure how to move forward,
I found this script online,but im not sure how to modify this for my use case

def rle_encoding(x):
    dots = np.where(x.T.flatten() == 1)[0]
    run_lengths = []
    prev = -2
    for b in dots:
        if (b&gt;prev+1): run_lengths.extend((b + 1, 0))
        run_lengths[-1] += 1
        prev = b
    return run_lengths

def prob_to_rles(x, cutoff=0.5):
    lab_img = label(x &gt; cutoff)
    for i in range(1, lab_img.max() + 1):
        yield rle_encoding(lab_img == i)


Any suggestions on how may i get started or how do i modify this would be really helpfull!

Try if something like that works

def rle_encode(image):
    """
    receives a masked image and encodes it to RLE
    :param mask_image:
    :return: string corresponding to the rle of the input image
    """
    pixels = image.flatten()
    # We avoid issues with '1' at the start or end (at the corners of
    # the original image) by setting those pixels to '0' explicitly.
    # We do not expect these to be non-zero for an accurate mask,
    # so this should not harm the score.
    pixels[0] = 0
    pixels[-1] = 0
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
    runs[1::2] = runs[1::2] - runs[:-1:2]
    return ' '.join(str(x) for x in runs)

# create the file path
f = open(args.submit_dir + args.arch + '.csv', 'w')

# add the header of the csv file
f.write('img,rle_mask\n') 

# NOTE: put this part in the test loop to generate the output file
f.write(image_name ',' + data_utils.mask_to_RLEstring(net_output.numpy()) + '\n')

