简体   繁体   English

Kaggle Pytorch运行长度编码

[英]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 我正在研究pytorch中的DSB问题,我有我的预言,但我不确定如何将其转换为提交所需的运行长度编码格式,总之就是

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

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. 您将提交包含开始位置和运行长度的值对,而不是提交用于细分的详尽索引列表。 Eg '1 3' implies starting at pixel 1 and running a total of 3 pixels (1,2,3). 例如,“ 1 3”表示从像素1开始,总共运行3个像素(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. 例如,“ 1 3 10 5”意味着像素1,2,3,10,11,12,13,14将被包括在掩码中。 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. 像素被一索引并从上到下编号,然后从左到右编号:1是像素(1,1),2是像素(2,1),依此类推。

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

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>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 > 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM