text_plain_output_1.png
from kaggle_datasets import KaggleDatasets

from tensorflow.keras import layers

import matplotlib.pyplot as plt

import numpy as np

import tensorflow as tf

try:
    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
    tf.config.experimental_connect_to_cluster(tpu)
    tf.tpu.experimental.initialize_tpu_system(tpu)
    strategy = tf.distribute.experimental.TPUStrategy(tpu)
except:
    strategy = tf.distribute.get_strategy()

mri_types = ['FLAIR', 'T1w', 'T1wCE', 'T2w']
IMAGE_SIZE = 128
IMAGE_DEPTH = 32
BATCH_SIZE = 32
CHANNELS = len(mri_types)
AUTO = tf.data.AUTOTUNE

def deserialize_example(serialized_string):
    image_feature_description = {'image': tf.io.FixedLenFeature([], tf.string), 'MGMT_value': tf.io.FixedLenFeature([], tf.float32)}
    parsed_record = tf.io.parse_single_example(serialized_string, image_feature_description)
    image = tf.io.decode_raw(parsed_record['image'], tf.float64)
    image = tf.reshape(image, [IMAGE_SIZE, IMAGE_SIZE, IMAGE_DEPTH, CHANNELS])
    label = parsed_record['MGMT_value']
    return (image, label)

GCS_PATH = KaggleDatasets().get_gcs_path('rsna-brain-tumor-classification-tfrecords')
tf_train_path = GCS_PATH + '/tfrecords/train'
tf_valid_path = GCS_PATH + '/tfrecords/valid'
train_set = tf.data.TFRecordDataset(str(tf_train_path + os.sep + 'brain_train.tfrec'), compression_type='GZIP').map(deserialize_example).batch(BATCH_SIZE)
valid_set = tf.data.TFRecordDataset(str(tf_valid_path + os.sep + 'brain_val.tfrec'), compression_type='GZIP').map(deserialize_example).batch(BATCH_SIZE)

d = train_set.take(1)
for i, j in d:
    image = i
    label = j

img_id = np.random.randint(0, BATCH_SIZE)
channel = np.random.randint(0,CHANNELS)

plt.figure(figsize=(20,10),facecolor=(0,0,0))
cols = IMAGE_DEPTH//4
rows = 4

plt.axis("off")
for layer_idx in range(IMAGE_DEPTH):
    ax = plt.subplot(rows,cols,layer_idx+1)
    ax.imshow(np.squeeze(image[img_id,:,:,layer_idx,channel]), cmap="gray")
    ax.axis("off")
    ax.set_title(str(layer_idx+1),color='r',y=-0.01)
    
plt.suptitle(f"Batch Image NO.: {img_id}, MRI Type: {mri_types[channel]}, Shape: {image[img_id].shape}", color="w")
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()

def get_model(width=128, height=128, depth=32):
    inputs = tf.keras.Input((width, height, depth, 4))
    x = layers.Conv3D(filters=32, kernel_size=2, activation='relu', padding='same')(inputs)
    x = layers.MaxPool3D(2)(x)
    x = layers.Conv3D(filters=64, kernel_size=2, activation='relu', padding='same')(x)
    x = layers.MaxPool3D(2)(x)
    x = layers.Conv3D(filters=128, kernel_size=2, activation='relu', padding='same')(x)
    x = layers.MaxPool3D(2)(x)
    x = layers.Conv3D(filters=256, kernel_size=2, activation='relu', padding='same')(x)
    x = layers.MaxPool3D(2)(x)
    x = layers.Conv3D(filters=512, kernel_size=2, activation='relu', padding='same')(x)
    x = layers.MaxPool3D(2)(x)
    x = layers.Flatten()(x)
    x = layers.Dense(units=128, activation='relu')(x)
    x = layers.Dense(units=128, activation='relu')(x)
    outputs = layers.Dense(units=1, activation='sigmoid')(x)
    model = tf.keras.Model(inputs, outputs)
    return model
USE_TPU = True
if USE_TPU:
    with strategy.scope():
        model = get_model(width=IMAGE_SIZE, height=IMAGE_SIZE, depth=IMAGE_DEPTH)
        model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])
else:
    model = get_model(width=IMAGE_SIZE, height=IMAGE_SIZE, depth=IMAGE_DEPTH)
    model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])
model.summary()

early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5)
history = model.fit(train_set, validation_data=valid_set, epochs=20, callbacks=[early_stopping_cb])