-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_sub.py
75 lines (55 loc) · 2.05 KB
/
gen_sub.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
from datetime import datetime
import shutil
import cv2
import numpy as np
#from keras.models import load_model
import segmentation_models as sm
from mask_to_submission import masks_to_submission
def generate_submission(model_file: str = "./last_best_model.h5",
test_set_dir: str = "./test_set_images/",
pr_out_path: str = "./out/",
backbone: str = "efficientnetb4",
sub_fn: str = "submission.csv"):
"""
Load weights, generate predictions and submission.
Parameters
----------
model_file : string
Path of the file from which to load the weights of the model.
test_set_dir : string
Path of the directory containing the images to use for prediction.
pr_out_path : string
Path of directory for predictions output.
backbone : string
The backbone used for unet.
sub_fn : string
Path of the generated submission file.
Returns
-------
-
"""
if not os.path.exists(pr_out_path):
os.makedirs(pr_out_path)
print("=== Loading weights ===")
model = sm.Unet(backbone, encoder_weights="imagenet")
model.load_weights(model_file)
print("=== Weights loaded ===")
print("=== Generating predictions ===")
def clean_line():
print(" " * (shutil.get_terminal_size().columns-1), end='\r')
for image_fn in os.listdir(test_set_dir):
clean_line()
print(f"Generating prediction: {image_fn}", end='\r')
image = cv2.imread(os.path.join(test_set_dir, image_fn))
image = np.array([image])
pr_mask = model.predict(image).round()
cv2.imwrite(os.path.join(pr_out_path, image_fn), pr_mask.squeeze() * 255)
clean_line()
print("=== Predictions generated ===")
img_fns = [os.path.join(pr_out_path, x) for x in os.listdir(pr_out_path)]
masks_to_submission(sub_fn, *img_fns)
print("=== Submission generated ===")
if __name__ == "__main__":
generate_submission()