RLE방식의 Mask 인코딩 후 제출용 dataframe 변환 치트코드

3) prediction string

제출용으로 압축된 방식인 RLE형식으로 mask string 만들기

  • mask자체는 압축하기가 굉장히 용이한 1과 0의 나열이기때문에, 압축할 수있다.
In [9]:
import numpy as np
import pandas as pd

import os
import sys
from tqdm import tqdm
from pathlib import Path
import tensorflow as tf
import skimage.io
import matplotlib.pyplot as plt

import pickle

from PIL import Image

from mrcnn import visualize
In [10]:
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log
Using TensorFlow backend.
In [11]:
class_lookup_df = pd.read_csv("../input/class_descriptions.csv", header=None)
empty_submission_df = pd.read_csv("../input/sample_empty_submission.csv")
In [12]:
class_list = pd.read_csv('../tiny_input/class/classes-segmentation.txt', header=None)
class_list = class_list.reset_index()
class_list['index'] = class_list['index'] + 1
class_list.columns = ['image_index', 'object']
print(class_list.head(10))
   image_index     object
0            1   /m/01_5g
1            2   /m/0c06p
2            3  /m/01lsmm
3            4  /m/01bqk0
4            5  /m/0l14j_
5            6   /m/0342h
6            7   /m/09j2d
7            8   /m/076bq
8            9   /m/01xqw
9           10   /m/01599
In [13]:
class_names = []
class_names.append('BG')
for i in class_list['object']:
    class_names.append(i)
In [14]:
class_lookup_df.columns = ["encoded_label","label"]
class_lookup_df['label'] = class_lookup_df['label'].str.lower()
class_lookup_df.head()
Out[14]:
encoded_label label
0 /m/0100nhbf sprenger’s tulip
1 /m/0104x9kv vinegret
2 /m/0105jzwx dabu-dabu
3 /m/0105ld7g pistachio ice cream
4 /m/0105lxy5 woku
In [15]:
class_lookup_df_index = class_lookup_df.set_index('encoded_label')

empty_submission_df.head()
In [16]:
empty_submission_df.head()
Out[16]:
ImageID ImageWidth ImageHeight PredictionString
0 80155d58d0ee19bd -1 -1 NaN
1 52a76f3fe21185bf -1 -1 NaN
2 65e9da7c04c8e4cd -1 -1 NaN
3 6d10fa21d93bd6cc -1 -1 NaN
4 0841dee573a875ca -1 -1 NaN
In [17]:
file = open("result_002_head_weight.pkl",'rb')
results = pickle.load(file)
file.close()
In [18]:
filenames = [el.split('.')[0] for el in os.listdir('../test/')] 
filenames_df = pd.DataFrame({'filename' : filenames})
filenames_df = filenames_df.reset_index()
filenames_df = filenames_df.set_index('filename')

이진 마스크를 인코딩

  • 0과 1을 RLE형식으로 인코딩한다.
In [19]:
import base64
import numpy as np
from pycocotools import _mask as coco_mask
import typing as t
import zlib

def encode_binary_mask(mask: np.ndarray) -> t.Text:
    """Converts a binary mask into OID challenge encoding ascii text."""

    # check input mask --
    if mask.dtype != np.bool:
        raise ValueError("encode_binary_mask expects a binary mask, received dtype == %s" % mask.dtype)

    mask = np.squeeze(mask)
    if len(mask.shape) != 2:
        raise ValueError("encode_binary_mask expects a 2d mask, received shape == %s" % mask.shape)

    # convert input mask to expected COCO API input --
    mask_to_encode = mask.reshape(mask.shape[0], mask.shape[1], 1)
    mask_to_encode = mask_to_encode.astype(np.uint8)
    mask_to_encode = np.asfortranarray(mask_to_encode)

    # RLE encode mask --
    encoded_mask = coco_mask.encode(mask_to_encode)[0]["counts"]

    # compress and base64 encoding --
    binary_str = zlib.compress(encoded_mask, zlib.Z_BEST_COMPRESSION)
    base64_str = base64.b64encode(binary_str)
    return base64_str
In [20]:
IMAGE_DIR = "../test/"

flat_list = [item for sublist in results for item in sublist]

flat_list_9 = flat_list[0:99999]

len(flat_list_9)
In [23]:
def read_image(name):
    return skimage.io.imread(os.path.join(IMAGE_DIR,name + '.jpg'))
In [24]:
def image_visualize(ind):
    visualize.display_instances(read_image(filenames[ind]),flat_list_9[ind]['rois'], flat_list_9[ind]['masks'], flat_list_9[ind]['class_ids'], class_lookup_df['label'])

시각화 해보기

  • 모델을 충분히 훈련하지 않으면 아래와 같이 예측이 엉성하다.
In [29]:
image_visualize(11)
image_visualize(29)
In [27]:
visualize.display_top_masks(read_image(filenames[11]), flat_list_9[11]['masks'], flat_list_9[11]['class_ids'], class_lookup_df['label'])
visualize.display_top_masks(read_image(filenames[29]), flat_list_9[29]['masks'], flat_list_9[29]['class_ids'], class_lookup_df['label'])

최종 제출용 로직

  • dataframe을 만든다.
In [ ]:
ImageID_list = []
ImageWidth_list = []
ImageHeight_list = []
PredictionString_list = []

for num, row in tqdm(empty_submission_df.iterrows(), total=len(empty_submission_df)):
    filename = row["ImageID"] + ".jpg"
   
    image = skimage.io.imread(os.path.join(IMAGE_DIR, filename))
    
    ind = filenames_df.loc[row['ImageID']]['index']
    r = flat_list_9[ind]
    
    height = image.shape[0]
    width  = image.shape[1]
        
    PredictionString = ""
    
    for i in range(len(r["class_ids"])):  
        class_id = r["class_ids"][i]
        roi = r["rois"][i]
        mask = r["masks"][:,:,i]
        confidence = r["scores"][i]
        
        encoded_mask = encode_binary_mask(mask)
                
        labelname = class_names[r['class_ids'][i]]
        if class_lookup_df[class_lookup_df["encoded_label"] == labelname].shape[0] == 0:
            
            print('no match label' + labelname)
            # no match label
            continue
        
        encoded_label = class_lookup_df[class_lookup_df["encoded_label"] == labelname]["encoded_label"].item()

        PredictionString += encoded_label 
        PredictionString += " "
        PredictionString += str(confidence)
        PredictionString += " "
        
        PredictionString += encoded_mask.decode()
        PredictionString += " "
        
    ImageID_list.append(row["ImageID"])
    ImageWidth_list.append(width)
    ImageHeight_list.append(height)
    PredictionString_list.append(PredictionString)
In [52]:
results_pd=pd.DataFrame({"ImageID":ImageID_list,
                      "ImageWidth":ImageWidth_list,
                      "ImageHeight":ImageHeight_list,
                      "PredictionString":PredictionString_list
                     })
In [53]:
results_pd.head(15)
Out[53]:
ImageID ImageWidth ImageHeight PredictionString
0 80155d58d0ee19bd 1024 683
1 52a76f3fe21185bf 1024 683
2 65e9da7c04c8e4cd 1024 682
3 6d10fa21d93bd6cc 1024 682
4 0841dee573a875ca 1024 683
5 660329181edd20b4 1024 483
6 2dffa0787ba35f70 1024 545 /m/0c9ph5 0.77417773 eNpNj90OgjAMhV+pLWCEECUSu…
7 cb66703ecbd0b99a 1024 678
8 2f01f2c68316b5e5 1024 768
9 36cfe3fa46f56ed3 768 768
10 40a658a046748701 1024 576
11 ca0794cf301fc733 1024 768
12 e8d7c76ff633a335 1024 735
13 ed57a7cef9d76de2 1024 768
14 51a9bf578fca9e50 1024 682
In [55]:
results_pd.to_csv("submission_first.csv", index = False)

답글 남기기