Kaggle Pytorch 运行 长度编码
Kaggle Pytorch run length encoding
我正在研究 pytorch 中的 DSB 问题,我有我的预测,但我不确定如何将它们变成提交所需的 运行 长度编码格式,简而言之,就是这样
===================================================================
为了减小提交文件的大小,我们的指标对像素值使用 运行 长度编码。您将提交包含起始位置和 运行 长度的成对值,而不是为您的细分提交详尽的索引列表。例如。 “1 3”表示从像素 1 开始 运行 总共 3 个像素 (1,2,3)。
比赛格式需要 space 分隔的成对列表。例如,“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')
我正在研究 pytorch 中的 DSB 问题,我有我的预测,但我不确定如何将它们变成提交所需的 运行 长度编码格式,简而言之,就是这样
===================================================================
为了减小提交文件的大小,我们的指标对像素值使用 运行 长度编码。您将提交包含起始位置和 运行 长度的成对值,而不是为您的细分提交详尽的索引列表。例如。 “1 3”表示从像素 1 开始 运行 总共 3 个像素 (1,2,3)。
比赛格式需要 space 分隔的成对列表。例如,“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')