-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
58 lines (48 loc) · 1.67 KB
/
Copy pathpredict.py
File metadata and controls
58 lines (48 loc) · 1.67 KB
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
import os
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.utils import CustomObjectScope
from tqdm import tqdm
from data import load_data, tf_dataset
from train import iou
def read_image(path):
x = cv2.imread(path, cv2.IMREAD_COLOR)
x = cv2.resize(x, (256, 256))
x = x/255.0
return x
def read_mask(path):
x = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
x = cv2.resize(x, (256, 256))
x = np.expand_dims(x, axis=-1)
return x
def mask_parse(mask):
mask = np.squeeze(mask)
mask = [mask, mask, mask]
mask = np.transpose(mask, (1, 2, 0))
return mask
if __name__ == "__main__":
## Dataset
path = "C:\Users\Harshit Aneja\Downloads\Poly segmentation Project\Poly segmentation Project\Kvasir-SEG"
batch_size = 8
(train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_data(path)
test_dataset = tf_dataset(test_x, test_y, batch=batch_size)
test_steps = (len(test_x)//batch_size)
if len(test_x) % batch_size != 0:
test_steps += 1
with CustomObjectScope({'iou': iou}):
model = tf.keras.models.load_model("files/model.h5")
model.evaluate(test_dataset, steps=test_steps)
for i, (x, y) in tqdm(enumerate(zip(test_x, test_y)), total=len(test_x)):
x = read_image(x)
y = read_mask(y)
y_pred = model.predict(np.expand_dims(x, axis=0))[0] > 0.5
h, w, _ = x.shape
white_line = np.ones((h, 10, 3)) * 255.0
all_images = [
x * 255.0, white_line,
mask_parse(y), white_line,
mask_parse(y_pred) * 255.0
]
image = np.concatenate(all_images, axis=1)
cv2.imwrite(f"results/{i}.png", image)