[3e9dc2]: / GI-Tract-Image-Segmentation.py

Download this file

379 lines (266 with data), 13.0 kB

  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
378
379
""" Import statements and check for GPU """
import os
import re
import glob
import math
import cv2
import csv
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from transunet import TransUNet
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, CSVLogger
from tensorflow import keras
from tensorflow.keras import layers
# List available GPUs
gpus = tf.config.list_physical_devices('GPU')
print("GPUs: ", gpus)
if gpus:
print("TensorFlow is using the GPU.")
else:
print("TensorFlow is not using the GPU.")
""" Function Definitions """
def rle_to_binary(rle, shape):
"""
Decodes run length encoded masks into a binary image
Parameters:
rle (list): list containing the starts and lengths that make up each RLE mask
shape (tuple): the original shape of the associated image
"""
# Initialize a flat mask with zeros
mask = np.zeros(shape[0] * shape[1], dtype=np.uint8)
if rle == '' or rle == '0': # Handle empty RLE
return mask.reshape(shape, order='C')
# Decode RLE into mask
rle_numbers = list(map(int, rle.split()))
for i in range(0, len(rle_numbers), 2):
start = rle_numbers[i] - 1 # Convert to zero-indexed
length = rle_numbers[i + 1]
mask[start:start + length] = 1
# Reshape flat mask into 2D
return mask.reshape(shape, order='C')
def custom_generator(gdf, dir, batch_size, target_size=(224, 224), test_mode=False):
"""
Custom data generator that dynamically aligns images and masks using RLE decoding.
Parameters:
gdf (GroupBy): Grouped dataframe containing image IDs and RLEs.
dir (str): Root directory of the dataset.
batch_size (int): Number of samples per batch.
target_size (tuple): Target size for resizing (default=(224, 224)).
test_mode (bool): If True, yields one image and mask at a time.
"""
ids = list(gdf.groups.keys())
dir2 = 'train'
while True:
sample_ids = np.random.choice(ids, size=batch_size, replace=False)
images, masks = [], []
for id_num in sample_ids:
# Get the dataframe rows for the current image
img_rows = gdf.get_group(id_num)
rle_list = img_rows['segmentation'].tolist()
# Construct the file path for the image
sections = id_num.split('_')
case = sections[0]
day = sections[0] + '_' + sections[1]
slice_id = sections[2] + '_' + sections[3]
pattern = os.path.join(dir, dir2, case, day, "scans", f"{slice_id}*.png")
filelist = glob.glob(pattern)
if filelist:
file = filelist[0]
image = cv2.imread(file, cv2.IMREAD_COLOR)
if image is None:
print(f"Image not found: {file}")
continue # Skip if the image is missing
# Original shape of the image
original_shape = image.shape[:2]
# Resize the image
resized_image = cv2.resize(image, target_size, interpolation=cv2.INTER_LINEAR)
# Decode and resize the masks
mask = np.zeros((target_size[0], target_size[1], len(rle_list)), dtype=np.uint8)
for i, rle in enumerate(rle_list):
if rle != '0': # Check if the RLE is valid
decoded_mask = rle_to_binary(rle, original_shape)
resized_mask = cv2.resize(decoded_mask, target_size, interpolation=cv2.INTER_NEAREST)
mask[:, :, i] = resized_mask
if test_mode:
# Return individual samples in test mode
yield resized_image[np.newaxis], mask[np.newaxis], pattern
else:
images.append(resized_image)
masks.append(mask)
if not test_mode:
x = np.array(images)
y = np.array(masks)
yield x, y, None
""" Loss function: dice loss ignores negative class thus negating class imbalance issues """
def dice_coef(y_true, y_pred, smooth=1e-6):
# Ensure consistent data types
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
y_true_f = tf.keras.backend.flatten(y_true)
y_pred_f = tf.keras.backend.flatten(y_pred)
intersection = tf.reduce_sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (tf.reduce_sum(y_true_f) + tf.reduce_sum(y_pred_f) + smooth)
def dice_loss(y_true, y_pred):
y_true = tf.cast(y_true, tf.float32)
y_pred = tf.cast(y_pred, tf.float32)
return 1 - dice_coef(y_true, y_pred)
""" Construct pipeline """
# dir = '../path/Dataset'
dir = './Dataset'
target_size = 224
batch_size = 24
epochs = 124
# read the csv file into a dataframe. os.path.join makes code executable across operating systes
df = pd.read_csv(os.path.join('.', dir, 'train.csv'))
df['segmentation'] = df['segmentation'].fillna('0')
# split into training, testing and validation sets
train_ids, temp_ids = train_test_split(df.id.unique(), test_size=0.25, random_state=42)
val_ids, test_ids = train_test_split(temp_ids, test_size=0.5, random_state=42)
# convert dfs into groupby objects to make sure rows are grouped by id
train_grouped_df = df[df.id.isin(train_ids)].groupby('id')
val_grouped_df = df[df.id.isin(val_ids)].groupby('id')
test_grouped_df = df[df.id.isin(test_ids)].groupby('id')
# steps per epoch is typically train length / batch size to use all training examples
train_steps_per_epoch = math.ceil(len(train_ids) / batch_size)
val_steps_per_epoch = math.ceil(len(val_ids) / batch_size)
test_steps_per_epoch = math.ceil(len(test_ids) / batch_size)
# create the training and validation datagens
train_generator = custom_generator(train_grouped_df, dir, batch_size, (target_size, target_size))
val_generator = custom_generator(val_grouped_df, dir, batch_size, (target_size, target_size))
test_generator = custom_generator(test_grouped_df, dir, batch_size, (target_size, target_size), test_mode=True)
""" Build the model or load the trained model """
loading = True
if loading:
weights_path = './impmodels/model_weights.h5'
model = TransUNet(image_size=224, pretrain=False)
model.load_weights(weights_path)
model.compile(optimizer='adam', loss=dice_loss, metrics=['accuracy'])
else:
# create the optimizer and learning rate scheduler
lr_schedule = tf.keras.optimizers.schedules.CosineDecay(
initial_learning_rate = 1e-3,
# decay_steps=train_steps_per_epoch * epochs,
decay_steps=epochs+2,
alpha=1e-2
)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
# create the U-net neural network
model = TransUNet(image_size=target_size, freeze_enc_cnn=False, pretrain=True)
model.compile(optimizer=optimizer, loss=dice_loss, metrics=['accuracy'])
# set up model checkpoints and early stopping
checkpoints_path = os.path.join('Checkpoints', 'model_weights.h5')
model_checkpoint = ModelCheckpoint(filepath=checkpoints_path, save_best_only=True, monitor='val_loss')
early_stopping = EarlyStopping(monitor='val_loss', mode='min', patience=8)
# log the training to a .csv for reference
csv_logger = CSVLogger('training_log.csv', append=True)
history = model.fit(train_generator, validation_data=val_generator, steps_per_epoch=train_steps_per_epoch, validation_steps=val_steps_per_epoch, epochs=epochs, callbacks=[model_checkpoint, early_stopping, csv_logger])
""" Display some predictions """
preds = []
ground_truths = []
num_samples = 50
# Generate predictions and ground truths
for i in range(num_samples):
# Fetch a batch from the test generator
batch = next(test_generator)
image, mask = batch
preds.append(model.predict(image)) # Predict using the model
ground_truths.append(mask)
best_threshold = 0.99
# Apply the best threshold to all predictions
final_preds = [(pred >= best_threshold).astype(int) for pred in preds]
# Compute Dice loss for each prediction
for i in range(len(final_preds)):
loss = dice_loss(ground_truths[i], final_preds[i])
print(f"Image {i + 1}: Dice Loss = {loss:.4f}")
def visualize_predictions(generator, model, num_samples=8, target_size=(224, 224)):
"""
Visualize predictions vs. ground truths overlaid on original images.
Parameters:
generator (generator): Data generator
model (Model): Trained segmentation model
num_samples (int): Number of samples to visualize
target_size (tuple): Target size for resizing (default=(224, 224)).
"""
fig, axes = plt.subplots(num_samples, 3, figsize=(15, 5 * num_samples))
for i in range(num_samples):
# Fetch one image and mask from the generator
image_batch, mask_batch = next(generator)
image = image_batch[0] # Single image
ground_truth = mask_batch[0] # Corresponding ground truth mask
# Ensure image is RGB
if len(image.shape) == 2:
image = np.stack([image] * 3, axis=-1) # Convert grayscale to RGB
# Ensure ground truth is a single-channel binary mask
if ground_truth.ndim == 3 and ground_truth.shape[-1] == 3:
ground_truth = ground_truth[:, :, 0] # Extract the first channel
# Generate prediction
raw_prediction = model.predict(image[np.newaxis])[0] # Add batch dimension for prediction
# Ensure prediction is single-channel
if raw_prediction.ndim == 3 and raw_prediction.shape[-1] == 3:
prediction = raw_prediction[:, :, 0] # Extract the first channel
else:
prediction = raw_prediction
prediction = (prediction >= 0.99).astype(np.uint8) # Threshold prediction
# Create overlays
gt_overlay = image.copy()
pred_overlay = image.copy()
# Overlay ground truth in red
gt_overlay[ground_truth == 1] = [255, 0, 0]
# Overlay prediction in green
pred_overlay[prediction == 1] = [0, 255, 0]
# Plot original image, ground truth overlay, and prediction overlay
axes[i, 0].imshow(image)
axes[i, 0].set_title(f"Image {i + 1}")
axes[i, 0].axis('off')
axes[i, 1].imshow(gt_overlay)
axes[i, 1].set_title(f"Ground Truth Overlay {i + 1}")
axes[i, 1].axis('off')
axes[i, 2].imshow(pred_overlay)
axes[i, 2].set_title(f"Prediction Overlay {i + 1}")
axes[i, 2].axis('off')
plt.tight_layout()
plt.show()
# Call the function with your test generator and trained model
visualize_predictions(test_generator, model, num_samples=24)
def binary_to_rle(binary_mask):
"""
Converts a binary mask to RLE (Run-Length Encoding).
"""
# Flatten mask in column-major order
flat_mask = binary_mask.T.flatten()
rle = []
start = -1
for i, val in enumerate(flat_mask):
if val == 1 and start == -1:
start = i
elif val == 0 and start != -1:
rle.extend([start + 1, i - start])
start = -1
if start != -1:
rle.extend([start + 1, len(flat_mask) - start])
return ' '.join(map(str, rle))
def save_predictions_to_csv(test_generator, model, output_csv_path):
"""
Generates predictions using the trained model and writes them to a CSV file in RLE format.
Parameters:
test_generator: The data generator for the test set.
model: The trained segmentation model.
output_csv_path: Path to save the CSV file.
"""
with open(output_csv_path, mode='w', newline='') as csvfile:
csv_writer = csv.writer(csvfile)
csv_writer.writerow(['id', 'segmentation']) # Header row
for image, masks, ids in test_generator:
predictions = model.predict(image)
predictions = (predictions > 0.99).astype(int)
for pred_mask, mask_id in zip(predictions, ids):
rle = binary_to_rle(pred_mask.squeeze())
csv_writer.writerow([mask_id, rle])
print(f"Processed {len(ids)} predictions...")
save_predictions_to_csv(test_generator, model, 'model_output.csv')