-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.py
More file actions
114 lines (92 loc) · 3.46 KB
/
Copy pathtest.py
File metadata and controls
114 lines (92 loc) · 3.46 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
import pandas as pd
from glob import glob
from tqdm import tqdm
import tensorflow as tf
from patchify import patchify
from train import load_dataset, create_dir
""" UNETR Configration """
cf = {}
cf["image_size"] = 256
cf["num_classes"] = 11
cf["num_channels"] = 3
cf["num_layers"] = 12
cf["hidden_dim"] = 128
cf["mlp_dim"] = 32
cf["num_heads"] = 6
cf["dropout_rate"] = 0.1
cf["patch_size"] = 16
cf["num_patches"] = (cf["image_size"]**2)//(cf["patch_size"]**2)
cf["flat_patches_shape"] = (
cf["num_patches"],
cf["patch_size"]*cf["patch_size"]*cf["num_channels"]
)
def grayscale_to_rgb(mask, rgb_codes):
h, w = mask.shape[0], mask.shape[1]
mask = mask.astype(np.int32)
output = []
for i, pixel in enumerate(mask.flatten()):
output.append(rgb_codes[pixel])
output = np.reshape(output, (h, w, 3))
return output
def save_results(image_x, mask, pred, save_image_path):
mask = np.expand_dims(mask, axis=-1)
mask = grayscale_to_rgb(mask, rgb_codes)
pred = np.expand_dims(pred, axis=-1)
pred = grayscale_to_rgb(pred, rgb_codes)
line = np.ones((image_x.shape[0], 10, 3)) * 255
cat_images = np.concatenate([image_x, line, mask, line, pred], axis=1)
cv2.imwrite(save_image_path, cat_images)
if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
tf.random.set_seed(42)
""" Directory for storing files """
create_dir(f"results")
""" Load the model """
model_path = os.path.join("files", "model.h5")
model = tf.keras.models.load_model(model_path)
""" RGB Code and Classes """
rgb_codes = [
[0, 0, 0], [0, 153, 255], [102, 255, 153], [0, 204, 153],
[255, 255, 102], [255, 255, 204], [255, 153, 0], [255, 102, 255],
[102, 0, 51], [255, 204, 255], [255, 0, 102]
]
classes = [
"background", "skin", "left eyebrow", "right eyebrow",
"left eye", "right eye", "nose", "upper lip", "inner mouth",
"lower lip", "hair"
]
""" Dataset """
dataset_path = "LaPa"
(train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_dataset(dataset_path)
print(f"Train: \t{len(train_x)} - {len(train_y)}")
print(f"Valid: \t{len(valid_x)} - {len(valid_y)}")
print(f"Test: \t{len(test_x)} - {len(test_y)}")
""" Prediction """
for x, y in tqdm(zip(test_x, test_y), total=len(test_x)):
""" Extracting the name """
name = x.split("/")[-1].split(".")[0]
""" Reading the image """
image = cv2.imread(x, cv2.IMREAD_COLOR)
image = cv2.resize(image, (cf["image_size"], cf["image_size"]))
x = image / 255.0
patch_shape = (cf["patch_size"], cf["patch_size"], cf["num_channels"])
patches = patchify(x, patch_shape, cf["patch_size"])
patches = np.reshape(patches, cf["flat_patches_shape"])
patches = patches.astype(np.float32) #[...]
patches = np.expand_dims(patches, axis=0) # [1, ...]
""" Read Mask """
mask = cv2.imread(y, cv2.IMREAD_GRAYSCALE)
mask = cv2.resize(mask, (cf["image_size"], cf["image_size"]))
mask = mask.astype(np.int32)
""" Prediction """
pred = model.predict(patches, verbose=0)[0]
pred = np.argmax(pred, axis=-1) ## [0.1, 0.2, 0.1, 0.6] -> 3
pred = pred.astype(np.int32)
""" Save the results """
save_image_path = f"results/{name}.png"
save_results(image, mask, pred, save_image_path)