-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrain.py
More file actions
141 lines (116 loc) · 4.34 KB
/
Copy pathtrain.py
File metadata and controls
141 lines (116 loc) · 4.34 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import numpy as np
import cv2
from glob import glob
from sklearn.utils import shuffle
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, ReduceLROnPlateau, EarlyStopping
from tensorflow.keras.optimizers import Adam, SGD
from sklearn.model_selection import train_test_split
from patchify import patchify
from unetr_2d import build_unetr_2d
""" 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 create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
def load_dataset(path):
train_x = sorted(glob(os.path.join(path, "train", "images", "*.jpg")))
train_y = sorted(glob(os.path.join(path, "train", "labels", "*.png")))
valid_x = sorted(glob(os.path.join(path, "val", "images", "*.jpg")))
valid_y = sorted(glob(os.path.join(path, "val", "labels", "*.png")))
test_x = sorted(glob(os.path.join(path, "test", "images", "*.jpg")))
test_y = sorted(glob(os.path.join(path, "test", "labels", "*.png")))
return (train_x, train_y), (valid_x, valid_y), (test_x, test_y)
def read_image(path):
path = path.decode()
image = cv2.imread(path, cv2.IMREAD_COLOR)
image = cv2.resize(image, (cf["image_size"], cf["image_size"]))
image = image / 255.0
""" Processing to patches """
patch_shape = (cf["patch_size"], cf["patch_size"], cf["num_channels"])
patches = patchify(image, patch_shape, cf["patch_size"])
patches = np.reshape(patches, cf["flat_patches_shape"])
patches = patches.astype(np.float32)
return patches
def read_mask(path):
path = path.decode()
mask = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
mask = cv2.resize(mask, (cf["image_size"], cf["image_size"]))
mask = mask.astype(np.int32)
return mask
def tf_parse(x, y):
def _parse(x, y):
x = read_image(x)
y = read_mask(y)
y = tf.one_hot(y, cf["num_classes"])
return x, y
x, y = tf.numpy_function(_parse, [x, y], [tf.float32, tf.float32])
x.set_shape(cf["flat_patches_shape"])
y.set_shape([cf["image_size"], cf["image_size"], cf["num_classes"]])
return x, y
def tf_dataset(X, Y, batch=2):
ds = tf.data.Dataset.from_tensor_slices((X, Y))
ds = ds.map(tf_parse).batch(batch).prefetch(10)
return ds
if __name__ == "__main__":
""" Seeding """
np.random.seed(42)
tf.random.set_seed(42)
""" Directory for storing files """
create_dir("files")
""" Hyperparameters """
batch_size = 16
lr = 0.1
num_epochs = 500
model_path = os.path.join("files", "model.h5")
csv_path = os.path.join("files", "log.csv")
""" 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)}")
train_dataset = tf_dataset(train_x, train_y, batch=batch_size)
valid_dataset = tf_dataset(valid_x, valid_y, batch=batch_size)
""" Model """
model = build_unetr_2d(cf)
model.compile(loss="categorical_crossentropy", optimizer=SGD(lr))
callbacks = [
ModelCheckpoint(model_path, verbose=1, save_best_only=True),
ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5, min_lr=1e-7, verbose=1),
CSVLogger(csv_path),
EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=False)
]
model.fit(
train_dataset,
epochs=num_epochs,
validation_data=valid_dataset,
callbacks=callbacks
)