[英]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.