제출용으로 압축된 방식인 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
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))
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]:
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]:
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)