[英]Kaggle Pytorch run length encoding
我正在研究pytorch中的DSB问题,我有我的预言,但我不确定如何将其转换为提交所需的运行长度编码格式,总之就是
===================================================================
为了减小提交文件的大小,我们的指标对像素值使用游程长度编码。 您将提交包含开始位置和运行长度的值对,而不是提交用于细分的详尽索引列表。 例如,“ 1 3”表示从像素1开始,总共运行3个像素(1,2,3)。
比赛格式要求以空格分隔的成对清单。 例如,“ 1 3 10 5”意味着像素1,2,3,10,11,12,13,14将被包括在掩码中。 像素被一索引并从上到下编号,然后从左到右编号:1是像素(1,1),2是像素(2,1),依此类推。
===================================================================
我得到这样的预测
model = model.eval()
for data in testdataloader:
data = t.autograd.Variable(data.cuda(device = 1))
pred = model(data)
但是现在我有了我的预测,我不确定该如何前进,我在线找到了这个脚本,但是我不确定如何针对我的用例进行修改
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)
关于如何着手或如何对此进行修改的任何建议将非常有帮助!
尝试类似的东西是否有效
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.