Thursday, February 1, 2024

ML: Deep Learning for Image Segmentation with TensorFlow

  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
# https://www.analyticsvidhya.com/blog/2023/04/deep-learning-for-image-segmentation-with-tensorflow/#

import cv2
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import matplotlib as mpl
from tqdm import tqdm
from sklearn.model_selection import train_test_split


# a list to collect paths of 1000 images
image_path=[]
for root, dirs, files in os.walk('D:/PyTest/kkk/png_images'):
    # iterate over 1000 images
    for file in files:
        path=os.path.join(root, file)
        image_path.append(path)
print(len(image_path))

# a list to collect paths of 1000 masks
mask_path=[]
for root, dirs, files in os.walk('D:/PyTest/kkk/png_masks'):
    # iterate over 1000 masks
    for file in files:
        # obtain the path
        path=os.path.join(root, file)
        # add path to the list
        mask_path.append(path)
print(len(mask_path))

# Create a list to store images
images=[]
for path in tqdm(image_path):
    # read file
    file=tf.io.read_file(path)
    # decode png file into a tensor
    image=tf.image.decode_png(file, channels=3, dtype=tf.uint8)
    # append to the list
    images.append(image)


# Create a list to store masks
masks=[]
for path in tqdm(mask_path):
    file=tf.io.read_file(path)
    # decode png file into a tensor
    mask=tf.image.decode_png(file, channels=1, dtype=tf.uint8)
    masks.append(mask)



def resize_image(image):
    image=tf.cast(image, tf.float32)
    image=image/255.0
    # resize image
    image=tf.image.resize(image, (128, 128))
    return image

def resize_mask(mask):
    mask=tf.image.resize(mask, (128, 128))
    mask=tf.cast(mask, tf.uint8)
    return mask

X=[resize_image(i) for i in images]
y=[resize_mask(m) for m in masks]
print(len(X))
print(len(y))


# split data into 80/20 ratio
train_X, val_X, train_y, val_y=train_test_split(X, y,
                                                test_size=0.2, random_state=0)
# Develop tf Dataset objects
train_X=tf.data.Dataset.from_tensor_slices(train_X)
val_X=tf.data.Dataset.from_tensor_slices(val_X)

train_y=tf.data.Dataset.from_tensor_slices(train_y)
val_y=tf.data.Dataset.from_tensor_slices(val_y)

# verify the shapes and data types
train_X.element_spec, train_y.element_spec, val_X.element_spec, val_y.element_spec

# adjust brightness of image
# don't alter in mask
def brightness(img, mask):
    img=tf.image.adjust_brightness(img, 0.1)
    return img, mask

def gamma(img, mask):
    img=tf.image.adjust_gamma(img, 0.1)
    return img, mask

def hue(img, mask):
    img=tf.image.adjust_hue(img, -0.1)
    return img, mask

def crop(img, mask):
    img=tf.image.central_crop(img, 0.7)
    img=tf.image.resize(img, (128,128))
    mask=tf.image.central_crop(mask, 0.7)
    mask=tf.image.resize(mask, (128,128))
    # cast to integers as they are class numbers
    mask=tf.cast(mask, tf.uint8)
    return img, mask

def flip_hori(img, mask):
    img=tf.image.flip_left_right(img)
    mask=tf.image.flip_up_down(mask)
    return img, mask

def flip_vert(img, mask):
    img=tf.image.flip_up_down(img)
    mask=tf.image.flip_up_down(mask)
    return img, mask

# rotate both image and mask identically
def rotate(img, mask):
    img=tf.image.rot90(img)
    mask=tf.image.rot90(mask)
    return img,mask

# zip images and masks
train=tf.data.Dataset.zip((train_X, train_y))
val=tf.data.Dataset.zip((val_X, val_y))


# perform augmentation on train data only
a=train.map(brightness)
b=train.map(gamma)
c=train.map(hue)
d=train.map(crop)
e=train.map(flip_hori)
f=train.map(flip_vert)
g=train.map(rotate)

# concatenate every new augmented sets
train=train.concatenate(a)
train=train.concatenate(b)
train=train.concatenate(c)
train=train.concatenate(d)
train=train.concatenate(e)
train=train.concatenate(f)

# Setting the batch size
BATCH=64

AT=tf.data.AUTOTUNE

# Buffer size
BUFFER=1000

STEPS_PER_EPOCH=800//BATCH
VALIDATION_STEPS=200//BATCH

train=train.cache().shuffle(BUFFER).batch(BATCH).repeat()
train=train.prefetch(buffer_size=AT)
val=val.batch(BATCH)


# Use pre-trained DenseNet21 without head
base=keras.applications.DenseNet121(input_shape=[128, 128, 3],
                                    include_top=False, weights='imagenet')

skip_names=[
    'conv1_relu',
    'pool2_relu',
    'pool3_relu',
    'pool4_relu',
    'relu'
]


skip_outputs=[base.get_layer(name).output for name in skip_names]
# Building the downstack with the above layers.
# We use the pre-trained model as much, without any fine-tuning
downstack=keras.Model(inputs=base.input, outputs=skip_outputs)
# freeze the downstack layers
downstack.trainable=False


from tensorflow_examples.models.pix2pix import pix2pix
upstack=[
    pix2pix.upsample(512, 3),
    pix2pix.upsample(256, 3),
    pix2pix.upsample(128, 3),
    pix2pix.upsample(64, 3)
]

# define the input layer
inputs=keras.layers.Input(shape=[128, 128, 3])
# downsample
down=downstack(inputs)
out=down[-1]

# prepare skip connection
skips=reversed(down[:-1])

# upsample with skip-connections
for up,skip in zip(upstack, skips):
    out=up(out)
    out=keras.layers.Concatenate()([out,skip])


# define the final transpose conv layer
out=keras.layers.Conv2DTranspose(
    59, 3, strides=2, padding='same',
)(out)

unet=keras.Model(inputs=inputs, outputs=out)

def Compile_Model():
    unet.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                 optimizer=keras.optimizers.RMSprop(learning_rate=0.001),
                 metrics=['accuracy'])

Compile_Model()

# training and fine-tuning
hist_1=unet.fit(
    train,
    validation_data=val,
    steps_per_epoch=STEPS_PER_EPOCH,
    validation_steps=VALIDATION_STEPS,
    epochs=20,
    verbose=2
)

# select a validation data batch
img, mask=next(iter(val))
# make prediction
pred=unet.predict(img)
plt.figure(figsize=(20,28))

NORM = mpl.colors.Normalize(vmin=0, vmax=58)


k=0
for i in pred:
    plt.subplot(4, 3, 1+k*3)
    i=tf.argmax(i, axis=-1)
    plt.imshow(i, cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Prediction')

    # plot the ground truth mask
    plt.subplot(4, 3, 2+k*3)
    plt.imshow(mask[k], cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Ground Truth')

    # plot the actual image
    plt.subplot(4, 3, 3+k*3)
    plt.imshow(img[k])
    plt.axis('off')
    plt.title('Actual Image')
    k=k+1
    if k==4:
        break
plt.suptitle('Prediction After 20 Epochs (No Fine-tuning)', color='red',
             size=20)
# plt.show()


downstack.trainable=True
# compile again
Compile_Model()
# train from epoch 20 to 40
hist_2=unet.fit(
    train,
    validation_data=val,
    steps_per_epoch=STEPS_PER_EPOCH,
    epochs=40,
    initial_epoch=20,
    verbose=2
)

# select a validation data batch
img, mask=next(iter(val))
# make prediction
pred=unet.predict(img)
plt.figure(figsize=(20, 30))

k=0
for i in pred:
    plt.subplot(4, 3, 1+k*3)
    i=tf.argmax(i, axis=-1)
    plt.imshow(i, cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Prediction')

    plt.subplot(4, 3, 2+k*3)
    plt.imshow(mask[k], cmap='jet', norm=NORM)
    plt.axis('off')
    plt.title('Ground Truth')

    plt.subplot(4, 3, 3+k*3)
    plt.imshow(img[k])
    plt.axis('off')
    plt.title('Actual Image')
    k=k+1

    if k==4: break

plt.suptitle('Predition After 40 Epochs (By Fine-tuning from 21th Epoch)', color='red', size=20)
plt.show()

No comments:

Post a Comment