-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainUnet.py
377 lines (291 loc) · 11 KB
/
trainUnet.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
# Image segmentation with a U-Net-like architecture
# Train the model
"""## Prepare paths of input images and target segmentation masks"""
from time import time
from unet import iou
from loss_functions import dice_loss, surface_loss_keras
import tensorflow as tf
import matplotlib.pyplot as plt
import pathlib
import numpy as np
from skimage.transform import resize
# tf.random.set_seed(1337)
AUTOTUNE = tf.data.AUTOTUNE
import os
#uncomment to force CPU
# os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
input_dir = pathlib.Path("/home/dan/dem_site_project/calm_datacrop_6band_minmaxscaler/")
target_dir = pathlib.Path("/home/dan/dem_site_project/calm_labelcrop/")
BACKBONE = 'resnet34'
label = "resnet34_calm_6band_dicesloss_512crop_flipaugment_float32"
# label = "resnet34_calm_6band_focalloss_512crop_flipaugment_float32"
img_size = (512, 512)
#the background class and the particle class
num_classes = 3
batch_size = 4
input_img_paths = sorted(
[
os.path.join(input_dir, fname)
for fname in os.listdir(input_dir)
if fname.endswith(".npy")
]
)
target_img_paths = sorted(
[
os.path.join(target_dir, fname)
for fname in os.listdir(target_dir)
if fname.endswith(".png") and not fname.startswith(".")
]
)
# input_img_paths = tf.data.Dataset.list_files(str(input_dir+'*.png'), shuffle=False)
# target_img_paths= tf.data.Dataset.list_files(str(target_dir+'*.png'), shuffle=False)
def normalize_numpy(x):
x[:,:,:3] /= x[:,:,:3].max()
# for i in range(3,x.shape[-1]):
# x[:,:,i] = x[:,:,i]/x[:,:,i].max()
return x
for input_path, target_path in zip(input_img_paths[:4],target_img_paths[:4]):
print(input_path, "|", target_path)
input_img_paths = [normalize_numpy(np.load(img)).astype(np.float32) for img in input_img_paths]
target_img_paths = [plt.imread(img) for img in target_img_paths]
dataset = tf.data.Dataset.from_tensor_slices((input_img_paths,target_img_paths))
length = tf.data.experimental.cardinality(dataset).numpy()
val_size = int(length * 0.1)
train_ds = dataset.skip(val_size)
val_ds = dataset.take(val_size)
print("train size",tf.data.experimental.cardinality(train_ds).numpy())
print("validation size",tf.data.experimental.cardinality(val_ds).numpy())
def decode_imgs(img,mask):
# Convert the compressed string to a 3D uint8 tensor
img = tf.io.read_file(img)
img = tf.io.decode_raw(img, tf.float64)
# tf.numpy_function(np.load, img, tf.float64)
# img = tf.io.decode_png(img,channels=6)
# img = np.load(img.decode())
img = img/255
mask = tf.io.read_file(mask)
mask = tf.io.decode_png(mask,channels=3)
img = tf.py_function(resize, (img,img_size+(6,)),tf.float64)
# Resize the image to the desired size
return img, tf.image.resize(mask, img_size)
# for input_path, target_path in train_ds.take(10):
# print(input_path, "|", target_path)
# train_ds = train_ds.map(decode_imgs, num_parallel_calls=AUTOTUNE)
# val_ds = val_ds.map(decode_imgs, num_parallel_calls=AUTOTUNE)
train_ds = train_ds.shuffle(length,seed=1337)
train_ds = train_ds.batch(batch_size)
val_ds = val_ds.batch(batch_size)
# val_ds = val_ds.prefetch(buffer_size=AUTOTUNE)
# train_ds = train_ds.cache()
val_ds = val_ds.shuffle(length,seed=1337)
val_ds = val_ds.cache()
# train_ds = train_ds.repeat() #repeat forever
# val_ds = val_ds.repeat() #repeat forever
def flipx_img(x,y, p=0.5):
if tf.random.uniform([]) < p:
x = tf.reverse(x,[2])
y = tf.reverse(y,[2])
else:
x
y
return x, y
def flipx(factor=0.5):
return tf.keras.layers.Lambda(lambda x: flipx_img(x, factor))
flipx = flipx()
def flipy_img(x,y, p=0.5):
if tf.random.uniform([]) < p:
x = tf.reverse(x,[1])
y = tf.reverse(y,[1])
else:
x
y
return x, y
def flipy(factor=0.5):
return tf.keras.layers.Lambda(lambda x: flipy_img(x, factor))
# image, label = next(iter(train_ds))
# aug_image, aug_label = flipy_img(image,label)
# image = image[0,:,:,:3]
# label = label[0,:,:]
# aug_image = aug_image[0,:,:,:3]
# aug_label = aug_label[0,:,:]
# fig, axis = plt.subplots(2, 2)
# axis[0,0].imshow(image)
# axis[0,1].imshow(aug_image)
# axis[1,0].imshow(label)
# axis[1,1].imshow(aug_label)
# plt.show()
flipy = flipy()
# def augment(image_label, seed):
# image, label = image_label
def augment(image, label):
print('image', image) # these lines is in the augment function, result below
print('type', type(image))
print('label', label) # these lines is in the augment function, result below
print('type', type(label))
# image, label = resize_and_rescale(image, label)
# image = tf.image.resize_with_crop_or_pad(image, IMG_SIZE + 6, IMG_SIZE + 6)
# Make a new seed.
# new_seed = tf.random.split(seed, num=1)[0, :]
# Random crop back to the original size.
# image = tf.image.stateless_random_crop(
# image, size=[IMG_SIZE, IMG_SIZE, 3], seed=seed)
# Random brightness.
# image = tf.image.stateless_random_brightness(
# image, max_delta=0.5, seed=new_seed)
# image = tf.clip_by_value(image, 0, 1)
image,label = flipx_img(image,label)
image,label = flipy_img(image,label)
return image, label
# aug_ds = flipx(train_ds)#train_ds.map(lambda x, y: (data_augmentation(x, training=True), y))
# aug_ds = flipy(aug_ds)#train_ds.map(lambda x, y: (data_augmentation(x, training=True), y))
# Create a generator.
rng = tf.random.Generator.from_seed(123, alg='philox')
# Create a wrapper function for updating seeds.
def f(x, y):
seed = rng.make_seeds(2)[0]
print("x in f is", x)
print("rank of x", tf.rank(x))
image, label = augment((x, y), seed)
return image, label
for image, mask in train_ds.take(1):
print("Image shape: ", image.numpy().shape)
print("mask shape: ", mask.numpy().shape)
#print a partial list of found images
print("Number of samples:", len(input_img_paths), len(target_img_paths))
aug_ds = train_ds.map(augment, num_parallel_calls=AUTOTUNE)
val_ds = val_ds.map(augment, num_parallel_calls=AUTOTUNE)
# aug_ds = aug_ds.prefetch(buffer_size=AUTOTUNE)
# from IPython.display import Image, display
from tensorflow.keras.preprocessing.image import load_img
import PIL
from PIL import ImageOps
from tensorflow import keras
import numpy as np
from tensorflow.keras.preprocessing.image import load_img
from unet import dataGenerator
from tensorflow.keras import layers
from unet import get_model
import matplotlib.pyplot as plt
# Build model
# model = get_model(img_size, num_classes)
# model.summary()
"""## Set aside a validation split"""
import random
for image,mask in train_ds.take(1):
print(image.shape)
print(np.max(image))
# plt.imshow(w[1][0])
# plt.show()
"""## Train the model"""
# from keras_loss import surface_loss_keras
# Configure the model for training.
# We use the "sparse" version of categorical_crossentropy
# because our target data is integers.
import segmentation_models as sm
# from unet import get_model
model = sm.Unet(BACKBONE, classes=num_classes, input_shape=img_size+(6,), encoder_weights=None)
# model = get_model(img_size,3)
# model.summary()
print("exit callbacks")
# model.compile(optimizer="adam", loss=sm.losses.DiceLoss(), metrics=[sm.metrics.IOUScore(), tf.keras.metrics.IoU(num_classes=3, target_class_ids=[0,1])])
model.compile(optimizer="adam", loss=sm.losses.DiceLoss()), metrics=[sm.metrics.IOUScore(smooth=1e-02), tf.keras.metrics.Accuracy()])
# model.compile(optimizer="adam", loss=sm.llosses.CategoricalFocalLoss(), metrics=[sm.metrics.IOUScore(smooth=1e-02), tf.keras.metrics.Accuracy()])
def create_mask(pred_mask):
pred_mask = tf.math.argmax(pred_mask, axis=-1)
pred_mask = pred_mask[..., tf.newaxis]
return pred_mask
def create_maskrgb(pred_mask):
pred_mask = tf.math.argmax(pred_mask, axis=-1)
# pred_mask = pred_mask[..., tf.newaxis]
canvas = np.zeros(img_size+(3,))
canvas[:,:,0] = (pred_mask ==0 )
canvas[:,:,1] = (pred_mask ==1 )
canvas[:,:,2] = (pred_mask ==2 )
return canvas
rows = 4
columns = 3
fig_disp,axes_disp = plt.subplots(rows,columns)
def show_predictions(dataset=None, num=1,block=False):
if dataset:
for image, mask in dataset.take(num):
showimg = image[:,:,:,:3]
pred_mask = model.predict(image)
fig = plt.figure(figsize=(10, 7))
# display([image[0], mask[0], create_mask(pred_mask)])
# showing image
plt.imshow(showimg[0])
plt.axis('off')
plt.title("image")
fig.add_subplot(rows, columns, 2)
plt.imshow(mask[0])
plt.axis('off')
plt.title("mask")
else:
# display([sample_image, sample_mask,
# create_mask(model.predict(sample_image[tf.newaxis, ...]))])
i=0
for image, mask in val_ds.take(1):
for i in range(4):
image3ch = image[:,:,:,:3]
print("validation show image is of shape",image3ch.shape)
mask = mask
# fig.add_subplot(rows, columns, j)
j=0
axes_disp[i,j].imshow(image3ch[i])
axes_disp[i,j].axis('off')
axes_disp[i,j].set_title("image")
# fig.add_subplot(rows, columns, j)
j+=1
axes_disp[i,j].imshow(create_maskrgb(mask[i]))
axes_disp[i,j].axis('off')
axes_disp[i,j].set_title("mask")
# fig.add_subplot(rows, columns, j)
j+=1
print("shape of fed image", image[i][tf.newaxis,...].shape)
prediction = model.predict(image[i][tf.newaxis,...])
print("shape of prediction", prediction.shape)
axes_disp[i,j].imshow(create_maskrgb(prediction[0]))
axes_disp[i,j].axis('off')
axes_disp[i,j].set_title("prediction")
plt.show(block=block)
plt.pause(0.5)
# plt.close()
show_predictions()
class DisplayCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
# clear_output(wait=True)
show_predictions()
print ('\nSample Prediction after epoch {}\n'.format(epoch+1))
callbacks = [
keras.callbacks.ModelCheckpoint("savedModels/"+label+".h5", save_best_only=True, verbose=1),
keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=25),
DisplayCallback()
]
# Train the model, doing validation at the end of each epoch.
epochs = 5000
start = time()
history = model.fit(aug_ds, epochs=epochs, validation_data=val_ds, callbacks=callbacks)
print("training time is " + str(time()-start)+" seconds")
import matplotlib.pyplot as plt
import matplotlib
show_predictions(block=True)
# matplotlib.use('Agg')
print(history.history.keys())
# loss, val_loss, accuracy, val_accuracy = [], [], [], []
loss = loss + history.history['loss']
val_loss = val_loss + history.history['val_loss']
accuracy = accuracy + history.history['iou_score']
val_accuracy = val_accuracy + history.history['val_iou_score']
fig, ax = plt.subplots()
ax.plot(accuracy,label = 'train')
ax.plot(val_accuracy,label = 'test')
ax.set_title('iou_score')
ax.legend(loc='lower right')
fig.savefig('iou_score'+label+'.png')
fig, ax = plt.subplots()
ax.plot(loss,label = 'train')
ax.plot(val_loss,label = 'test')
ax.set_title('Loss')
ax.legend(loc='upper right')
fig.savefig('loss'+label+'.png')