Page 1 of 1

2. Tweak the deep learning model dl tutorial.ipynb The current Google Colab notebook (just as shown as a tutorial in cla

Posted: Thu May 05, 2022 12:41 pm
by answerhappygod
2 Tweak The Deep Learning Model Dl Tutorial Ipynb The Current Google Colab Notebook Just As Shown As A Tutorial In Cla 1
2 Tweak The Deep Learning Model Dl Tutorial Ipynb The Current Google Colab Notebook Just As Shown As A Tutorial In Cla 1 (69.65 KiB) Viewed 57 times
#Before running the code:
Runtime -> Change runtime type -> Hardware accelerator :
GPU
This enables you to use GPUs when training the model. Otherwise,
it will be very slow. Note that if you train too much or have the
code running too long, google colab will give you a limit in your
usage. One workaround would be alternating between your own google
id and iu google id.
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
# tensorflow uses keras as a more user friendly form
from tensorflow.keras import Model, layers, losses
# tf can be used also like numpy but on tensors
# this article gives great information on what tensors are
#
https://towardsdatascience.com/tensors- ... 11d48676d5

import tensorflow as tf
import random
#Model creation
class SimpleConvNet(Model):
def __init__(self):
super(SimpleConvNet,
self).__init__()
# sequence of convolutional layers,
layer normalization and convolutional layers for pooling
self.conv1 = layers.Conv3D(32, 3,
activation='relu', padding='same')
self.ln1 =
layers.LayerNormalization()
self.red1 = layers.Conv3D(1, 1,
strides=2, activation='relu', padding='same')
self.conv2 = layers.Conv3D(64, 3,
activation='relu', padding='same')
self.ln2 =
layers.LayerNormalization()
self.red2 = layers.Conv3D(1, 1,
strides=2, activation='relu', padding='same')
# flatten the layer to use dense
layers
# to have a classifying layer
self.flatten = layers.Flatten()
self.dense1 = layers.Dense(256,
activation='relu')
self.dense2 = layers.Dense(128,
activation='relu')
self.dense3 = layers.Dense(64,
activation='relu')
self.classifier = layers.Dense(1,
activation='sigmoid')

def call(self, inputs):
x = self.conv1(inputs)
x = self.ln1(x)
x = self.red1(x)
x = self.conv2(x)
x = self.ln2(x)
x = self.red2(x)
x = self.flatten(x)
x = self.dense1(x)
x = self.dense2(x)
x = self.dense3(x)
x = self.classifier(x)
return x
# This is an autoencoder which has a very similar structure of
the encoder as the simple network that we used
# note that the decoder almost copies the encoder structure (though
not possible to imitate exactly)
# Also, this is another way to define a model tensorflow keras for
sequential(no skip connections) model
autoencoder = tf.keras.Sequential(
[
tf.keras.Input(shape=(28, 28, 28, 1)),
layers.Conv3D(32, 3, activation='relu',
padding='same'),
layers.LayerNormalization(),
layers.Conv3D(1, 1, strides=2,
activation='relu', padding='same'),
layers.Conv3D(31, 3, activation='relu',
padding='same'),
layers.LayerNormalization(),
layers.Conv3D(1, 1, strides=2,
activation='relu', padding='same'),
layers.Conv3D(3, 3, activation='relu',
padding='same'),
layers.LayerNormalization(),
layers.Flatten(),
layers.Dense(256, activation='relu'),
layers.Dense(128, activation='relu'),
layers.Dense(64, activation='relu',
name='latent'),
layers.Dense(128, activation='relu'),
layers.Dense(256, activation='relu'),
layers.Dense(1029, activation='relu'),
layers.Reshape((7, 7, 7, 3)),
layers.Conv3DTranspose(1, 1, strides=2,
activation='relu', padding='same'),
layers.Conv3D(64, 3, activation='relu',
padding='same'),
layers.LayerNormalization(),
layers.Conv3DTranspose(1, 1, strides=2,
activation='relu', padding='same'),
layers.Conv3D(32, 3, activation='relu',
padding='same'),
layers.LayerNormalization(),
layers.Conv3D(1, 3, padding='same',
activation='sigmoid')
])
#Training and testing
# load nodulelist data
# data is from medmnist;
https://zenodo.org/record/5208230#.Yifpv3rMJhE
# the goal of the model is binary classification
# we are limiting the test_data as some of the data seems to be
corrupted in some way
file = np.load('nodulemnist3d.npz')
print(file.files)
train_data = file['train_images'].astype(np.float64)
train_labels = file['train_labels']
val_data = file['val_images'].astype(np.float64)
val_labels = file['val_labels']
test_data = file['test_images'].astype(np.float64)
test_data = test_data[:310]
test_labels = file['test_labels']
test_labels = test_labels[:310]
# we are normalizing the data
# there are many ways to normalize the data
# and could be either (0, 1) or (-1, -1)
for idx in range(len(train_data)):
data = train_data[idx]
train_data[idx] = (data -
np.min(data))/(np.max(data)-np.min(data))
for idx in range(len(val_data)):
data = val_data[idx]
val_data[idx] = (data -
np.min(data))/(np.max(data)-np.min(data))
for idx in range(len(test_data)):
data = test_data[idx]
test_data[idx] = (data -
np.min(data))/(np.max(data)-np.min(data))
# This is just changing the format of the data
# Note that the first three lines we are adding a channel dimension
to the image
# Having channel dimensions 1 like this would be common in medical
images
train_data = np.expand_dims(train_data, -1)
val_data = np.expand_dims(val_data, -1)
test_data = np.expand_dims(test_data, -1)
# Running the simple network
# We are using BinaryCrossentropy as the loss function
# The model was not built to know the input shape, thus we need a
building step
model = SimpleConvNet()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),


loss=losses.BinaryCrossentropy(),

metrics=['acc'])
model.build((None, 28, 28, 28, 1))
epochs = 100
history = model.fit(train_data, train_labels, epochs=epochs,
validation_data = (val_data, val_labels), verbose=False)
# plot and see test accuracy
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(epochs), history.history['loss'], 'g',
label='loss')
plt.plot(range(epochs), history.history['val_loss'], 'r',
label='val_loss')
plt.legend()
fig = plt.figure()
plt.plot(range(epochs), history.history['acc'], 'g',
label='acc')
plt.plot(range(epochs), history.history['val_acc'], 'r',
label='val_acc')
plt.legend()
test_predict = model.predict(test_data)
test_predict = np.where(test_predict > 0.5, 1, 0)
print('test_accuracy : ' ,
1-np.sum(np.abs(test_predict-test_labels))/len(test_labels))
# Let's try using an auto encoder this time
autoencoder.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),


loss=losses.MeanSquaredError())
epochs = 100
history = autoencoder.fit(train_data, train_data, validation_data =
(val_data, val_data), epochs=epochs, verbose=False)
# plotting the loss difference
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(range(epochs), history.history['loss'], 'g',
label='loss')
plt.plot(range(epochs), history.history['val_loss'], 'r',
label='val_loss')
plt.legend()
# now let's see what it learned
# optimally, we should be looking at the same image as the truth as
prediction
from google.colab.patches import cv2_imshow
from skimage.transform import resize
prediction = autoencoder.predict(np.expand_dims(val_data[0],
0))[0]
truth = val_data[0]
prediction = (prediction-np.min(prediction))/(np.max(prediction)-
np.min(prediction))* 255
truth = (truth-np.min(truth))/(np.max(truth)- np.min(truth))*
255
cv2_imshow(resize(prediction[:, prediction.shape[1]//2, :, 0],
(128, 128)).astype(np.uint(8)))
cv2_imshow(resize(truth[:, truth.shape[1]//2, :, 0], (128,
128)).astype(np.uint(8)))
# now let's try to extract the encoder part
# and try clustering with the latent dimension
encoder = Model(autoencoder.input,
autoencoder.get_layer('latent').output)
latent_spaces_train = encoder.predict(train_data)
latent_spaces = encoder.predict(test_data)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2).fit(latent_spaces_train)
prediction = kmeans.predict(latent_spaces)
acc = 1 - (np.min([np.sum(np.abs(prediction-test_labels)),
np.sum(np.abs(np.abs(1-prediction)-test_labels))]) /
len(test_labels))
print(acc)
# What was the original KMeans accuracy?
latent_spaces_train = np.reshape(train_data, (train_data.shape[0],
-1))
latent_spaces = np.reshape(test_data, (test_data.shape[0],
-1))
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=2).fit(latent_spaces_train)
prediction = kmeans.predict(latent_spaces)
acc = 1 - (np.min([np.sum(np.abs(prediction-test_labels)),
np.sum(np.abs(np.abs(1-prediction)-test_labels))]) /
len(test_labels))
print(acc)
2. Tweak the deep learning model dl tutorial.ipynb The current Google Colab notebook (just as shown as a tutorial in class - dl_tutorial.ipynb) has two models; simple classification using convolutional layers and auto encoder based self supervised clustering. We want you to play with the models and try to improve the results. This could be done by adding new layers, changing parameters like learning rates etc. Add at least 3 changes to each of the models. Please report the reason for the changes you have made and whether the results match your expectations. Please plot your results. Note that we will NOT be grading on accuracy/how well your model predicts. The goal of the assignment is to learn how to tinker a deep learning model for medical images.