forked from preritj/segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
255 lines (221 loc) · 10.8 KB
/
model.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import tensorflow as tf
from u_net import unet
from fcn_gcn_net import fcn_gcn_net
from losses import pixel_wise_loss, dice_coef_loss, mask_prediction
from utils import dice_coef, rle_encode, rle_to_string
from tqdm import tqdm
from utils import ImageLoader
import os
import sys
import cv2
import numpy as np
from scipy.misc import imresize
import pandas as pd
class Model(object):
def __init__(self, params, cfg):
self.params = params
self.mode = params.phase
self.is_train = (self.mode == 'train')
self.batch_size = params.batch_size
self.stage = params.stage
self.cfg = {}
self.cfg = cfg
self.image_loader = ImageLoader(cfg)
self.tf_placeholders = {}
self.create_tf_placeholders()
self.global_step = tf.Variable(0, name='global_step', trainable=False)
self.train_op, self.loss_op = None, None
self.eval_op = None
self.mask_logits = self.build_net()
def create_tf_placeholders(self):
roi_h, roi_w = self.cfg['scaled_img_shape'] #self.cfg['roi_shape']
roi_images = tf.placeholder(tf.float32, [self.batch_size, roi_h, roi_w, 3])
roi_masks = tf.placeholder(tf.float32, [self.batch_size, roi_h, roi_w, 2])
roi_weights = tf.placeholder(tf.float32, [self.batch_size, roi_h, roi_w])
self.tf_placeholders = {'images': roi_images,
'masks': roi_masks,
'weights': roi_weights}
def build_net(self):
batch_norm = self.params.batch_norm
# tf.reset_default_graph()
roi_images = self.tf_placeholders["images"]
net = self.params.net
if net == 'unet':
mask_logits = unet(roi_images, num_classes=2, training=self.is_train,
init_channels=8, n_layers=6, batch_norm=batch_norm)
else:
mask_logits = fcn_gcn_net(roi_images, num_classes=2, k_gcn=11, training=self.is_train,
init_channels=8, n_layers=7, batch_norm=True)
return mask_logits
def make_train_op(self):
learning_rate = self.params.learning_rate
roi_masks = self.tf_placeholders["masks"]
roi_masks_pos = tf.slice(roi_masks, [0, 0, 0, 1], [-1, -1, -1, 1])
roi_masks_pos = tf.squeeze(roi_masks_pos, [-1])
roi_weights = self.tf_placeholders["weights"]
_, tf_mask = mask_prediction(self.mask_logits)
loss0 = dice_coef_loss(roi_masks_pos, tf_mask)
loss1 = pixel_wise_loss(self.mask_logits, roi_masks, pixel_weights=roi_weights)
loss = loss0 + self.params.sce_weight * loss1
solver = tf.train.AdamOptimizer(learning_rate, epsilon=1e-8)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
self.train_op = solver.minimize(loss, global_step=self.global_step)
self.loss_op = [loss0, loss1]
def make_eval_op(self):
pred_probs, pred_masks = mask_prediction(self.mask_logits)
self.eval_op = [pred_probs, pred_masks]
def get_feed_dict(self, batch, perturb=True):
if self.stage == 1:
roi_images, roi_masks, roi_weights = \
self.image_loader.load_img_batch(batch, edge_factor=self.params.edge_factor)
else:
roi_images, roi_masks, roi_weights = \
self.image_loader.load_roi_batch(batch, perturb=perturb,
edge_factor=self.params.edge_factor)
tf_roi_images = self.tf_placeholders["images"]
if roi_masks is None:
return {tf_roi_images: roi_images}
tf_roi_masks = self.tf_placeholders["masks"]
tf_roi_weights = self.tf_placeholders["weights"]
return {tf_roi_images: roi_images,
tf_roi_masks: roi_masks,
tf_roi_weights: roi_weights}
def train(self, data):
""" Train the model. """
params = self.params
save_dir = os.path.join(params.save_dir, str(params.set).zfill(2), 'stage_'+str(self.stage))
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_dir = os.path.join(save_dir, 'model')
self.make_train_op()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
if params.load:
self.load(sess, saver)
n_display = params.display_period
for i_epoch in tqdm(list(range(params.num_epochs)), desc='epoch'):
dice_loss, sce_loss, n_steps = 0, 0, 0
for _ in tqdm(list(range(0, data.count, self.batch_size)), desc='batch'):
batch = data.next_batch()
if len(batch[0]) < self.batch_size:
continue
ops = [self.train_op, self.global_step] + self.loss_op
feed_dict = self.get_feed_dict(batch, perturb=True)
_, global_step, loss0, loss1 = sess.run(ops, feed_dict=feed_dict)
if n_steps + 1 == n_display:
print("Dice coeff : {}, Cross entropy loss : {}"
.format(-dice_loss/n_steps, sce_loss/n_steps))
dice_loss, sce_loss, n_steps = 0, 0, 0
else:
dice_loss += loss0
sce_loss += loss1
n_steps += 1
if (global_step + 1) % params.save_period == 0:
print("Saving model in {}".format(save_dir))
saver.save(sess, save_dir, global_step)
data.reset()
print("{} epochs finished.".format(i_epoch))
def validate(self, data):
""" Test the model. """
# params = self.params
self.make_eval_op()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
self.load(sess, saver)
for _ in tqdm(list(range(data.count)), desc='batch'):
batch = data.next_batch()
img_file, mask_file = batch[0][0], batch[1][0]
gt_bbox = self.image_loader.generate_rois([mask_file], perturb=False)[0]
feed_dict = self.get_feed_dict(batch, perturb=False)
pred_probs, _ = sess.run(self.eval_op, feed_dict=feed_dict)
# pred_mask = np.zeros_like(pred_probs, dtype=np.uint8)
# pred_mask[np.where(pred_mask > 0.5)] = 1
# print(np.where(pred_mask > 0.5))
mask_pred = pred_probs[0, :, :, 1]
mask_pred[mask_pred > 0.5] = 1
mask_pred[mask_pred <= 0.5] = 0
if True:
img = cv2.imread(img_file)
real_mask = np.zeros_like(img, dtype=np.uint8)
if self.stage == 1:
img_h, img_w = self.cfg['image_shape']
l, r, t, b = self.cfg['crops']
pred_mask = imresize(mask_pred, (img_h - t - b, img_w - l - r)) / 255
real_mask[t: img_h - b, l: img_w - r, 0] = np.uint8(np.round(pred_mask))
else:
y, x, h, w = gt_bbox
pred_mask = cv2.resize(mask_pred, (w, h))
real_mask[y:y + h, x:x + w, 0] = np.uint8(pred_mask)
winname = 'Image %s' % (img_file)
img = cv2.resize(img, (1438, 960))
img_mask = cv2.resize(real_mask * 255, (1438, 960), interpolation=cv2.INTER_CUBIC)
display_img = cv2.addWeighted(img, 0.2, img_mask, 0.8, 0)
cv2.imshow(winname, display_img)
cv2.moveWindow(winname, 100, 100)
cv2.waitKey(1000)
gt_mask = self.image_loader.load_mask(mask_file)
print("Dice coefficient : ", dice_coef(gt_mask, real_mask[:,:,0]))
def test(self, data):
""" Test the model. """
params = self.params
self.make_eval_op()
res_dir = params.test_results_dir
res_dir = os.path.join(res_dir, str(params.set).zfill(2),
'stage_' + str(params.stage))
if not os.path.exists(res_dir):
os.makedirs(res_dir)
img_names = []
rle_strings = []
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
self.load(sess, saver)
for _ in tqdm(list(range(data.count)), desc='batch'):
batch = data.next_batch()
img_file = batch[0][0]
feed_dict = self.get_feed_dict(batch, perturb=False)
pred_probs, _ = sess.run(self.eval_op, feed_dict=feed_dict)
# pred_mask = np.zeros_like(pred_probs, dtype=np.uint8)
# pred_mask[np.where(pred_mask > 0.5)] = 1
# print(np.where(pred_mask > 0.5))
mask_pred = pred_probs[0, :, :]
#mask_pred[mask_pred > 0.5] = 1
#mask_pred[mask_pred <= 0.5] = 0
real_mask = self.image_loader.postprocess(mask_pred)
rle = rle_encode(real_mask)
rle_strings.append(rle_to_string(rle))
if 1:
img = cv2.imread(img_file)
img_mask = np.zeros_like(img)
img_mask[:, :, 0] = real_mask * 255
# y, x, h, w = gt_bbox
# print(gt_bbox)
winname = 'Image %s' % (img_file)
img = cv2.resize(img, (1438, 960))
img_mask = cv2.resize(img_mask, (1438, 960))
display_img = cv2.addWeighted(img, 0.4, img_mask, 0.6, 0)
cv2.imshow(winname, display_img)
cv2.moveWindow(winname, 100, 100)
cv2.waitKey(1000)
img_name = os.path.basename(img_file)
img_names.append(img_name)
#outfile = os.path.join(res_dir, str(img_name) + '.npy')
#np.save(outfile, mask_pred)
df = {'img' : img_names, 'rle_mask' : rle_strings}
df = pd.DataFrame(df)
outfile = os.path.join(res_dir, 'results.csv')
df.to_csv(outfile)
def load(self, sess, saver):
""" Load the trained model. """
params = self.params
print("Loading model...")
load_dir = os.path.join(params.save_dir, str(params.set).zfill(2),
'stage_'+str(params.stage), 'model')
checkpoint = tf.train.get_checkpoint_state(os.path.dirname(load_dir))
if checkpoint is None:
print("Error: No saved model found. Please train first.")
sys.exit(0)
saver.restore(sess, checkpoint.model_checkpoint_path)