This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpspu_skelnet.py
211 lines (169 loc) · 6.49 KB
/
pspu_skelnet.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""PSPU-SkelNet: build, train, test
Pls cite:
@inproceedings{atienza2019cvprwskelneton,
title={Pyramid U-Network for Skeleton Extraction from Shape Points},
author={Atienza, Rowel},
booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition Workshops},
pages={0--0},
year={2019}
}
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import argparse
import os
import datetime
from skimage.io import imsave
from model_builder import build_model
from utils import list_files, read_gray, augment
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras.optimizers import Adam
from keras.layers import Input
TEST_PATH = "dataset/point/test_img"
ROOT_PATH = "dataset/point/root"
EPOCHS = 200
class PSPU_SkelNet():
def __init__(self,
batch_size=8,
ntimes=8):
self.thresh = 0.5
self.batch_size = batch_size
self.ntimes = ntimes
self.model = None
self.input_pix = None
self.output_pix = None
def load_train_data(self):
infile = "npy/in_pts.npy"
print("Loading input train data... ", infile)
self.input_pix = np.load(infile)
print("Input train data shape: ", self.input_pix.shape)
outfile = "npy/out_pts.npy"
print("Loading output train data ... ", outfile)
self.output_pix = np.load(outfile)
print("Output train data shape: ", self.output_pix.shape)
def build_model(self):
if self.input_pix is None:
input_shape = (256, 256, 1)
output_shape = (256, 256, 1)
else:
input_shape = self.input_pix.shape[1:]
output_shape = self.output_pix.shape[1:]
self.model = build_model(input_shape, output_shape)
self.model.summary()
optimizer = Adam(lr=1e-3)
self.model.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
def plot_model(self):
from keras.utils import plot_model
if self.model is None:
self.build_model()
plot_model(self.model, to_file='pspu_skelnet.png', show_shapes=True)
def load_weights(self, weights_file):
if self.model is None:
self.build_model()
print("Loading model weights ...", weights_file)
self.model.load_weights(weights_file)
def lr_schedule(self, epoch):
lr = 1e-3
if epoch > 120:
lr = 0.5e-4
elif epoch > 60:
lr = 1e-4
print('Learning rate: ', lr)
return lr
def train(self):
self.load_train_data()
if self.model is None:
self.build_model()
# prepare model model saving directory.
save_dir = os.path.join(os.getcwd(), 'weights')
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
weights_name = 'pspu_skelnet.h5'
filepath = os.path.join(save_dir, weights_name)
# prepare callbacks for model saving and for learning rate adjustment.
checkpoint = ModelCheckpoint(filepath=filepath,
verbose=1,
save_weights_only=True)
lr_scheduler = LearningRateScheduler(self.lr_schedule)
callbacks = [checkpoint, lr_scheduler]
# train the model with input images and labels
xval = self.input_pix.astype('float32') / 255
yval = self.output_pix.astype('float32') / 255
x, y = augment(self.input_pix, self.output_pix, ntimes=self.ntimes)
x = np.concatenate((self.input_pix, x), axis=0)
y = np.concatenate((self.output_pix, y), axis=0)
print("Augmented input train data shape: ", x.shape)
print("Augmented output train data shape: ", y.shape)
x = x.astype('float32') / 255
y = y.astype('float32') / 255
self.model.fit([x, x, x, x],
y,
epochs=EPOCHS,
validation_data=([xval, xval, xval, xval], yval),
batch_size=self.batch_size,
callbacks=callbacks)
def predict(self):
if self.model is None:
self.build_model()
if not os.path.isdir(ROOT_PATH):
os.makedirs(ROOT_PATH)
path = TEST_PATH
files = list_files(path)
pix = []
for f in files:
pix_file = os.path.join(path, f)
pix_data = read_gray(pix_file)
pix.append(pix_data)
# print(pix_file)
pix = np.array(pix)
print("Test image max: ", np.amax(pix))
pix = pix / 255.0
print("Normalized image max: ", np.amax(pix))
ipix = np.expand_dims(pix, axis=3)
for i in range(ipix.shape[0]):
img = ipix[i]
img = np.expand_dims(img, axis=0)
out_pix = self.model.predict([img, img, img, img])
out_pix[out_pix >= self.thresh] = 1.0
out_pix[out_pix < self.thresh] = 0.0
out_pix = np.squeeze(out_pix) * 255.0
out_pix = out_pix.astype(np.uint8)
path = os.path.join(ROOT_PATH, files[i])
print("Saving ... ", path)
imsave(path, out_pix, cmap='gray')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
help_ = "Load model saved weights"
parser.add_argument("--weights",
default=None,
help=help_)
help_ = "Train model"
parser.add_argument("--train",
default=False,
action='store_true',
help=help_)
help_ = "Plot model"
parser.add_argument("--plot",
default=False,
action='store_true',
help=help_)
help_ = "Number of times (rotate, translate, etc) is executed"
parser.add_argument("--ntimes", type=int, default=8, help=help_)
help_ = "Batch size"
parser.add_argument("--batch_size", type=int, default=8, help=help_)
args = parser.parse_args()
pspu_skelnet = PSPU_SkelNet(batch_size=args.batch_size,
ntimes=args.ntimes)
print("Batch size: ", args.batch_size)
if args.plot:
pspu_skelnet.plot_model()
if args.weights is not None:
pspu_skelnet.load_weights(args.weights)
if not args.train:
pspu_skelnet.predict()
else:
pspu_skelnet.train()