forked from killthekitten/kaggle-ds-bowl-2018-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
51 lines (41 loc) · 1.58 KB
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import model as modellib
import pandas as pd
import cv2
import os
import numpy as np
from tqdm import tqdm
from inference_config import inference_config
from bowl_dataset import BowlDataset
from utils import rle_encode, rle_decode, rle_to_string
import functions as f
ROOT_DIR = os.getcwd()
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Recreate the model in inference mode
model = modellib.MaskRCNN(mode="inference",
config=inference_config,
model_dir=MODEL_DIR)
# Get path to saved weights
# Either set a specific path or find last trained weights
# model_path = os.path.join(ROOT_DIR, ".h5 file name here")
model_path = model.find_last()[1]
# Load trained weights (fill in path to trained weights here)
assert model_path != "", "Provide path to trained weights"
print("Loading weights from ", model_path)
model.load_weights(model_path, by_name=True)
dataset_test = BowlDataset()
dataset_test.load_bowl('stage1_test')
dataset_test.prepare()
output = []
sample_submission = pd.read_csv('stage1_sample_submission.csv')
ImageId = []
EncodedPixels = []
for image_id in tqdm(sample_submission.ImageId):
image_path = os.path.join('stage1_test', image_id, 'images', image_id + '.png')
original_image = cv2.imread(image_path)
results = model.detect([original_image], verbose=0)
r = results[0]
masks = r['masks']
ImageId_batch, EncodedPixels_batch = f.numpy2encoding_no_overlap2(masks, image_id, r['scores'])
ImageId += ImageId_batch
EncodedPixels += EncodedPixels_batch
f.write2csv('submission_v2.csv', ImageId, EncodedPixels)