-
Notifications
You must be signed in to change notification settings - Fork 0
/
linknet.py
191 lines (157 loc) · 6.91 KB
/
linknet.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
from utilities import *
import os
import random
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use("ggplot")
import cv2
from tqdm import tqdm_notebook, tnrange
from glob import glob
from itertools import chain
from skimage.io import imread, imshow, concatenate_images
from skimage.transform import resize
from skimage.morphology import label
from sklearn.model_selection import train_test_split
import tensorflow as tf
from skimage.color import rgb2gray
from tensorflow.keras import Input
from tensorflow.keras.models import Model, load_model, save_model
from tensorflow.keras.layers import Input, Activation, BatchNormalization, Dropout, Lambda, Conv2D, Conv2DTranspose, MaxPooling2D, concatenate
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras import backend as K
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
def data_iterator(image_gen, mask_gen):
for img, mask in zip(image_gen, mask_gen):
yield img, mask
#Loading Data
DataPath = "/data"
dirs = []
images = []
masks = []
for dirname, _, filenames in os.walk(DataPath):
for filename in filenames:
if 'mask'in filename:
dirs.append(dirname.replace(DataPath, ''))
masks.append(filename)
images.append(filename.replace('_mask', ''))
imagePath_df = pd.DataFrame({'directory':dirs, 'images': images, 'masks': masks})
imagePath_df['image-path'] = DataPath + imagePath_df['directory'] + '/' + imagePath_df['images']
imagePath_df['mask-path'] = DataPath + imagePath_df['directory'] + '/' + imagePath_df['masks']
#Train Test Split
train , test = train_test_split(imagePath_df, test_size=0.25, random_state=21)
#Defining Constants
EPOCHS = 35
BATCH_SIZE = 32
ImgHieght = 256
ImgWidth = 256
Channels = 3
#Data Augmentation
data_augmentation = dict(rotation_range=0.2,
width_shift_range=0.05,
height_shift_range=0.05,
shear_range=0.05,
zoom_range=0.05,
horizontal_flip=True,
fill_mode='nearest')
imagegen = ImageDataGenerator(rescale=1./255., **data_augmentation)
maskgen = ImageDataGenerator(rescale=1./255., **data_augmentation)
# train generator
timage_generator=imagegen.flow_from_dataframe(dataframe=train,
x_col="image-path",
batch_size= BATCH_SIZE,
seed=42,
class_mode=None,
target_size=(ImgHieght,ImgWidth),
color_mode='rgb')
# validation data generator
tmask_generator=maskgen.flow_from_dataframe(dataframe=train,
x_col="mask-path",
batch_size=BATCH_SIZE,
seed=42,
class_mode=None,
target_size=(ImgHieght,ImgWidth),
color_mode='grayscale')
imagegen = ImageDataGenerator(rescale=1./255.)
maskgen = ImageDataGenerator(rescale=1./255.)
# train generator
vimage_generator=imagegen.flow_from_dataframe(dataframe=test,
x_col="image-path",
batch_size= BATCH_SIZE,
seed=42,
class_mode=None,
target_size=(ImgHieght,ImgWidth),
color_mode='rgb')
# validation data generator
vmask_generator=maskgen.flow_from_dataframe(dataframe=test,
x_col="mask-path",
batch_size=BATCH_SIZE,
seed=42,
class_mode=None,
target_size=(ImgHieght,ImgWidth),
color_mode='grayscale')
train_gen = data_iterator(timage_generator, tmask_generator)
valid_gen = data_iterator(vimage_generator, vmask_generator)
### pip install git+https://github.com/qubvel/segmentation_models
import segmentation_models
from segmentation_models import Linknet
import os
# os.environ['SM_FRAMEWORK'] = 'tf.keras'
import segmentation_models as sm
print('sm.version=' + sm.__version__)
sm.set_framework('tf.keras')
model=Linknet()
model.compile(optimizer='adam', loss="binary_crossentropy", metrics=["accuracy"])
callbacks = [
EarlyStopping(patience=10, verbose=1),
ReduceLROnPlateau(factor=0.1, patience=5, min_lr=1e-5, verbose=1),
ModelCheckpoint('/model/model-brain-linknet.h5', verbose=1, save_best_only=True, save_weights_only=True)
]
STEP_SIZE_TRAIN = timage_generator.n/BATCH_SIZE
STEP_SIZE_VALID = vimage_generator.n/BATCH_SIZE
results = model.fit(train_gen,
steps_per_epoch=STEP_SIZE_TRAIN,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=callbacks,
validation_data=valid_gen,
validation_steps=STEP_SIZE_VALID)
plt.figure(figsize=(8, 8))
plt.title("Learning curve")
plt.plot(results.history["loss"], label="loss", color=sns.xkcd_rgb['greenish teal'])
plt.plot(results.history["val_loss"], label="val_loss", color=sns.xkcd_rgb['amber'])
plt.plot( np.argmin(results.history["val_loss"]), np.min(results.history["val_loss"]), marker="x", color="r", label="best model")
plt.xlabel("Epochs")
plt.ylabel("log_loss")
plt.legend()
# plt.grid(False)
plt.show()
model.load_weights('/model/model-brain-linknet.h5')
eval_results = model.evaluate(valid_gen, steps=STEP_SIZE_VALID, verbose=1)
for i in range(10):
idx = np.random.randint(0, len(imagePath_df))
imagePath = os.path.join(DataPath, imagePath_df['directory'].iloc[idx], imagePath_df['images'].iloc[idx])
maskPath = os.path.join(DataPath, imagePath_df['directory'].iloc[idx], imagePath_df['masks'].iloc[idx])
image = cv2.imread(imagePath)
mask = cv2.imread(maskPath)
img = cv2.resize(image ,(ImgHieght, ImgWidth))
img = img / 255
img = img[np.newaxis, :, :, :]
pred=model.predict(img)
plt.figure(figsize=(12,12))
plt.subplot(1,4,1)
plt.imshow(np.squeeze(img))
plt.title('Original Image')
plt.subplot(1,4,2)
plt.imshow(mask)
plt.title('Original Mask')
plt.subplot(1,4,3)
plt.imshow(np.squeeze(pred))
plt.title('Prediction')
plt.subplot(1,4,4)
plt.imshow(np.squeeze(pred) > 0.5)
plt.title('BinaryPrediction')
plt.show()