Imports¶
In [ ]:
import numpy as np
import pandas as pd
import datetime
from packaging import version
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow import keras
from tensorflow.keras import models, layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, BatchNormalization, Dropout, Flatten, Dense
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from tensorflow.keras.preprocessing import image
from tensorflow.keras.utils import to_categorical
import tensorflow.keras.backend as k
In [ ]:
%matplotlib inline
np.set_printoptions(precision=3, suppress=True)
Verify TensorFlow Version¶
In [ ]:
print("This notebook requires TensorFlow 2.0 or above")
print("TensorFlow version: ", tf.__version__)
assert version.parse(tf.__version__).release[0] >=2
This notebook requires TensorFlow 2.0 or above TensorFlow version: 2.15.0
Mount Google Drive to Colab Environment¶
In [ ]:
# from google.colab import drive
# drive.mount('/content/gdrive')
EDA Functions¶
In [ ]:
def get_three_classes(x, y):
def indices_of(class_id):
indices, _ = np.where(y == float(class_id))
return indices
indices = np.concatenate([indices_of(0), indices_of(1), indices_of(2)], axis=0)
x = x[indices]
y = y[indices]
count = x.shape[0]
indices = np.random.choice(range(count), count, replace=False)
x = x[indices]
y = y[indices]
y = tf.keras.utils.to_categorical(y)
return x, y
In [ ]:
def show_random_examples(x, y, p):
indices = np.random.choice(range(x.shape[0]), 10, replace=False)
x = x[indices]
y = y[indices]
p = p[indices]
plt.figure(figsize=(10, 5))
for i in range(10):
plt.subplot(2, 5, i + 1)
plt.imshow(x[i])
plt.xticks([])
plt.yticks([])
col = 'green' if np.argmax(y[i]) == np.argmax(p[i]) else 'red'
plt.xlabel(class_names_preview[np.argmax(p[i])], color=col)
plt.show()
Research Assignment Reporting Functions¶
In [ ]:
def plot_history(history):
losses = history.history['loss']
accs = history.history['accuracy']
val_losses = history.history['val_loss']
val_accs = history.history['val_accuracy']
epochs = len(losses)
plt.figure(figsize=(16, 4))
for i, metrics in enumerate(zip([losses, accs], [val_losses, val_accs], ['Loss', 'Accuracy'])):
plt.subplot(1, 2, i + 1)
plt.plot(range(epochs), metrics[0], label='Training {}'.format(metrics[2]))
plt.plot(range(epochs), metrics[1], label='Validation {}'.format(metrics[2]))
plt.legend()
plt.show()
def display_training_curves(training, validation, title, subplot):
ax = plt.subplot(subplot)
ax.plot(training)
ax.plot(validation)
ax.set_title('model '+ title)
ax.set_ylabel(title)
ax.set_xlabel('epoch')
ax.legend(['training', 'validation'])
In [ ]:
def print_validation_report(y_test, predictions):
print("Classification Report")
print(classification_report(y_test, predictions))
print('Accuracy Score: {}'.format(accuracy_score(y_test, predictions)))
print('Root Mean Square Error: {}'.format(np.sqrt(MSE(y_test, predictions))))
In [ ]:
def plot_confusion_matrix(y_true, y_pred):
mtx = confusion_matrix(y_true, y_pred)
fig, ax = plt.subplots(figsize=(16,12))
sns.heatmap(mtx, annot=True, fmt='d', linewidths=.75, cbar=False, ax=ax,cmap='Blues',linecolor='white')
# square=True,
plt.ylabel('true label')
plt.xlabel('predicted label')
Loading colorectal_histology dataset¶
In [ ]:
# Load the dataset
ds, ds_info = tfds.load('colorectal_histology', split='train', as_supervised=True, with_info=True)
# Convert the dataset into numpy arrays
x = []
y = []
for image, label in tfds.as_numpy(ds):
x.append(image)
y.append(label)
# Convert lists to numpy arrays
x = np.array(x)
y = np.array(y).reshape(-1, 1)
# Split the data into training and test sets
split_index = int(0.8 * len(x)) # 80% for training, 20% for testing
x_train, x_test = x[:split_index], x[split_index:]
y_train, y_test = y[:split_index], y[split_index:]
In [ ]:
print('train_images:\t{}'.format(x_train.shape))
print('train_labels:\t{}'.format(y_train.shape))
print('test_images:\t\t{}'.format(x_test.shape))
print('test_labels:\t\t{}'.format(y_test.shape))
train_images: (4000, 150, 150, 3) train_labels: (4000, 1) test_images: (1000, 150, 150, 3) test_labels: (1000, 1)
In [ ]:
print("First ten labels training dataset:\n {}\n".format(y_train[0:10]))
print("This output the numeric label, need to convert to item description")
First ten labels training dataset: [[4] [5] [5] [0] [6] [6] [6] [4] [1] [3]] This output the numeric label, need to convert to item description
Plot Subset of Examples¶
In [ ]:
ds, ds_info = tfds.load('colorectal_histology', split='train', as_supervised=True, with_info=True)
# Convert the dataset to numpy arrays
images = []
labels = []
for image, label in tfds.as_numpy(ds):
images.append(image)
labels.append(label)
images = np.array(images)
labels = np.array(labels).reshape(-1, 1) # Reshape labels
# Split the data into training and test sets
split_index = int(0.8 * len(images)) # 80% for training, 20% for testing
train_images, test_images = images[:split_index], images[split_index:]
train_labels, test_labels = labels[:split_index], labels[split_index:]
In [ ]:
x_preview, y_preview = get_three_classes(train_images, train_labels)
x_preview, y_preview = get_three_classes(test_images, test_labels)
In [ ]:
class_names_preview = ['tumour epithelium', 'complex stroma', 'adipose tissue']
show_random_examples(x_preview, y_preview, y_preview)
Preprocessing Data for Model Development¶
The labels are an array of integers, ranging from 0 to 8. These correspond to the class the image represents:
| Label | Class |
|---|---|
| 0 | tumour epithelium |
| 1 | simple stroma |
| 2 | complex stroma |
| 3 | immune cell conglomerates |
| 4 | debris and mucus |
| 5 | mucosal glands |
| 6 | adipose tissue |
| 7 | background |
In [ ]:
class_names = ['tumour epithelium'
,'simple stroma'
,'complex stroma'
,'immune cell conglomerates'
,'debris and mucus'
,'mucosal glands'
,'adipose tissue'
,'background']
Create Validation Data Set¶
In [ ]:
x_train_split, x_valid_split, y_train_split, y_valid_split = train_test_split(x_train
,y_train
,test_size=.1
,random_state=42
,shuffle=True)
Confirm Datasets {Train, Validation, Test}¶
In [ ]:
print(x_train_split.shape, x_valid_split.shape, x_test.shape)
(3600, 150, 150, 3) (400, 150, 150, 3) (1000, 150, 150, 3)
Rescale Examples {Train, Validation, Test}¶
In [ ]:
x_train_norm = x_train_split/255
x_valid_norm = x_valid_split/255
x_test_norm = x_test/255
In [ ]:
from datetime import datetime
Model 1a: CNN with 1 convolution/max pooling layers (no regularization). filters=32, first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model1a = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=128, activation='relu'),
Dense(units=8, activation='softmax')
])
model1a.summary()
model1a.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model1a.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model1a = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model1a.evaluate(x_test_norm, y_test)[1]:.3f}")
preds1a = model1a.predict(x_test_norm)
print('shape of preds: ', preds1a.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred1a= model1a.predict(x_test_norm)
pred1a=np.argmax(pred1a, axis=1)
print_validation_report(y_test, pred1a)
plot_confusion_matrix(y_test,pred1a)
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 148, 148, 32) 896
max_pooling2d (MaxPooling2 (None, 74, 74, 32) 0
D)
flatten (Flatten) (None, 175232) 0
dense (Dense) (None, 128) 22429824
dense_1 (Dense) (None, 8) 1032
=================================================================
Total params: 22431752 (85.57 MB)
Trainable params: 22431752 (85.57 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - ETA: 0s - loss: 3.6982 - accuracy: 0.3944
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 14s 227ms/step - loss: 3.6982 - accuracy: 0.3944 - val_loss: 1.0075 - val_accuracy: 0.5425 Epoch 2/200 57/57 [==============================] - 13s 221ms/step - loss: 0.8664 - accuracy: 0.6308 - val_loss: 0.9197 - val_accuracy: 0.6000 Epoch 3/200 57/57 [==============================] - 13s 221ms/step - loss: 0.6908 - accuracy: 0.7222 - val_loss: 0.8394 - val_accuracy: 0.6900 Epoch 4/200 57/57 [==============================] - 12s 213ms/step - loss: 0.5688 - accuracy: 0.7808 - val_loss: 0.8397 - val_accuracy: 0.6400 Epoch 5/200 57/57 [==============================] - 12s 213ms/step - loss: 0.4752 - accuracy: 0.8286 - val_loss: 0.8503 - val_accuracy: 0.6500 Epoch 6/200 57/57 [==============================] - 13s 221ms/step - loss: 0.3900 - accuracy: 0.8622 - val_loss: 0.7704 - val_accuracy: 0.7250 Epoch 7/200 57/57 [==============================] - 12s 210ms/step - loss: 0.3636 - accuracy: 0.8692 - val_loss: 0.7766 - val_accuracy: 0.6625 Epoch 8/200 57/57 [==============================] - 13s 220ms/step - loss: 0.3152 - accuracy: 0.8981 - val_loss: 0.6972 - val_accuracy: 0.7750 Epoch 9/200 57/57 [==============================] - 12s 217ms/step - loss: 0.2100 - accuracy: 0.9475 - val_loss: 0.6900 - val_accuracy: 0.7725 Epoch 10/200 57/57 [==============================] - 12s 219ms/step - loss: 0.1936 - accuracy: 0.9472 - val_loss: 0.6830 - val_accuracy: 0.8050 Epoch 11/200 57/57 [==============================] - 12s 213ms/step - loss: 0.1392 - accuracy: 0.9708 - val_loss: 0.7124 - val_accuracy: 0.7600 Epoch 12/200 57/57 [==============================] - 12s 215ms/step - loss: 0.1631 - accuracy: 0.9539 - val_loss: 0.9274 - val_accuracy: 0.7200 Epoch 13/200 57/57 [==============================] - 12s 216ms/step - loss: 0.1923 - accuracy: 0.9464 - val_loss: 0.9059 - val_accuracy: 0.6625 Process Time: 0:02:43.888865 32/32 [==============================] - 1s 26ms/step - loss: 0.7334 - accuracy: 0.7490 Test acc: 0.749 32/32 [==============================] - 1s 26ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 26ms/step
Classification Report
precision recall f1-score support
0 0.82 0.54 0.65 141
1 0.51 0.64 0.57 114
2 0.68 0.70 0.69 132
3 0.77 0.82 0.79 129
4 0.73 0.63 0.68 134
5 0.67 0.83 0.74 106
6 0.98 0.87 0.92 118
7 0.91 0.99 0.95 126
accuracy 0.75 1000
macro avg 0.76 0.75 0.75 1000
weighted avg 0.76 0.75 0.75 1000
Accuracy Score: 0.749
Root Mean Square Error: 1.4092551223962253
Model 1b: CNN with 1 convolution/max pooling layers (no regularization). filters=32, first dense layer units=256¶
In [ ]:
start_time = datetime.now()
model1b = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=256, activation='relu'),
Dense(units=8, activation='softmax')
])
model1b.summary()
model1b.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model1b.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model1b = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model1b.evaluate(x_test_norm, y_test)[1]:.3f}")
preds1b = model1b.predict(x_test_norm)
print('shape of preds: ', preds1b.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred1b= model1b.predict(x_test_norm)
pred1b=np.argmax(pred1b, axis=1)
print_validation_report(y_test, pred1b)
plot_confusion_matrix(y_test,pred1b)
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_1 (MaxPoolin (None, 74, 74, 32) 0
g2D)
flatten_1 (Flatten) (None, 175232) 0
dense_2 (Dense) (None, 256) 44859648
dense_3 (Dense) (None, 8) 2056
=================================================================
Total params: 44862600 (171.14 MB)
Trainable params: 44862600 (171.14 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - ETA: 0s - loss: 9.2861 - accuracy: 0.2972
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 18s 300ms/step - loss: 9.2861 - accuracy: 0.2972 - val_loss: 1.2795 - val_accuracy: 0.4600 Epoch 2/200 57/57 [==============================] - 17s 296ms/step - loss: 0.9369 - accuracy: 0.5986 - val_loss: 1.0082 - val_accuracy: 0.5525 Epoch 3/200 57/57 [==============================] - 17s 295ms/step - loss: 0.6676 - accuracy: 0.7450 - val_loss: 0.8240 - val_accuracy: 0.7425 Epoch 4/200 57/57 [==============================] - 16s 275ms/step - loss: 0.4962 - accuracy: 0.8233 - val_loss: 0.9775 - val_accuracy: 0.6725 Epoch 5/200 57/57 [==============================] - 17s 293ms/step - loss: 0.3671 - accuracy: 0.8958 - val_loss: 0.7439 - val_accuracy: 0.7350 Epoch 6/200 57/57 [==============================] - 16s 275ms/step - loss: 0.3746 - accuracy: 0.8781 - val_loss: 0.7675 - val_accuracy: 0.7500 Epoch 7/200 57/57 [==============================] - 16s 275ms/step - loss: 0.2356 - accuracy: 0.9336 - val_loss: 0.7464 - val_accuracy: 0.7500 Epoch 8/200 57/57 [==============================] - 17s 294ms/step - loss: 0.1714 - accuracy: 0.9614 - val_loss: 0.7060 - val_accuracy: 0.7500 Epoch 9/200 57/57 [==============================] - 16s 278ms/step - loss: 0.1994 - accuracy: 0.9378 - val_loss: 0.7214 - val_accuracy: 0.7575 Epoch 10/200 57/57 [==============================] - 16s 279ms/step - loss: 0.1535 - accuracy: 0.9644 - val_loss: 0.7979 - val_accuracy: 0.7500 Epoch 11/200 57/57 [==============================] - 16s 278ms/step - loss: 0.1197 - accuracy: 0.9694 - val_loss: 0.7298 - val_accuracy: 0.7625 Epoch 12/200 57/57 [==============================] - 16s 279ms/step - loss: 0.1216 - accuracy: 0.9706 - val_loss: 0.7798 - val_accuracy: 0.7525 Epoch 13/200 57/57 [==============================] - 16s 282ms/step - loss: 0.1099 - accuracy: 0.9717 - val_loss: 0.7840 - val_accuracy: 0.7550 Epoch 14/200 57/57 [==============================] - 16s 280ms/step - loss: 0.0997 - accuracy: 0.9756 - val_loss: 0.8055 - val_accuracy: 0.7200 Process Time: 0:03:49.318554 32/32 [==============================] - 1s 29ms/step - loss: 0.7132 - accuracy: 0.7490 Test acc: 0.749 32/32 [==============================] - 1s 28ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 28ms/step
Classification Report
precision recall f1-score support
0 0.79 0.57 0.66 141
1 0.61 0.55 0.58 114
2 0.67 0.67 0.67 132
3 0.76 0.83 0.80 129
4 0.73 0.75 0.74 134
5 0.63 0.82 0.71 106
6 0.98 0.81 0.89 118
7 0.85 1.00 0.92 126
accuracy 0.75 1000
macro avg 0.75 0.75 0.75 1000
weighted avg 0.76 0.75 0.75 1000
Accuracy Score: 0.749
Root Mean Square Error: 1.3971399357258385
Model 1c: CNN with 1 convolution/max pooling layers (no regularization). filters=32, first dense layer units=512¶
In [ ]:
start_time = datetime.now()
model1c = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=512, activation='relu'),
Dense(units=8, activation='softmax')
])
model1c.summary()
model1c.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model1c.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model1c = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model1c.evaluate(x_test_norm, y_test)[1]:.3f}")
preds1c = model1c.predict(x_test_norm)
print('shape of preds: ', preds1c.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred1c= model1c.predict(x_test_norm)
pred1c=np.argmax(pred1c, axis=1)
print_validation_report(y_test, pred1c)
plot_confusion_matrix(y_test,pred1c)
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_2 (MaxPoolin (None, 74, 74, 32) 0
g2D)
flatten_2 (Flatten) (None, 175232) 0
dense_4 (Dense) (None, 512) 89719296
dense_5 (Dense) (None, 8) 4104
=================================================================
Total params: 89724296 (342.27 MB)
Trainable params: 89724296 (342.27 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - ETA: 0s - loss: 4.8972 - accuracy: 0.4381
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 27s 461ms/step - loss: 4.8972 - accuracy: 0.4381 - val_loss: 0.8854 - val_accuracy: 0.6225 Epoch 2/200 57/57 [==============================] - 26s 455ms/step - loss: 0.7108 - accuracy: 0.7192 - val_loss: 0.8232 - val_accuracy: 0.6150 Epoch 3/200 57/57 [==============================] - 25s 439ms/step - loss: 0.4856 - accuracy: 0.8325 - val_loss: 0.7350 - val_accuracy: 0.6450 Epoch 4/200 57/57 [==============================] - 26s 454ms/step - loss: 0.3455 - accuracy: 0.8833 - val_loss: 0.6264 - val_accuracy: 0.7850 Epoch 5/200 57/57 [==============================] - 23s 412ms/step - loss: 0.2352 - accuracy: 0.9303 - val_loss: 0.6528 - val_accuracy: 0.7775 Epoch 6/200 57/57 [==============================] - 24s 415ms/step - loss: 0.1555 - accuracy: 0.9592 - val_loss: 0.6394 - val_accuracy: 0.7975 Epoch 7/200 57/57 [==============================] - 24s 416ms/step - loss: 0.1481 - accuracy: 0.9619 - val_loss: 0.8327 - val_accuracy: 0.7100 Epoch 8/200 57/57 [==============================] - 24s 421ms/step - loss: 0.2095 - accuracy: 0.9469 - val_loss: 0.6563 - val_accuracy: 0.7925 Epoch 9/200 57/57 [==============================] - 24s 424ms/step - loss: 0.0844 - accuracy: 0.9789 - val_loss: 0.7263 - val_accuracy: 0.7600 Process Time: 0:03:44.750275 32/32 [==============================] - 1s 32ms/step - loss: 0.6635 - accuracy: 0.7390 Test acc: 0.739 32/32 [==============================] - 1s 32ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 32ms/step
Classification Report
precision recall f1-score support
0 0.66 0.84 0.74 141
1 0.51 0.69 0.59 114
2 0.74 0.63 0.68 132
3 0.90 0.78 0.84 129
4 0.75 0.50 0.60 134
5 0.70 0.69 0.69 106
6 0.98 0.77 0.86 118
7 0.83 1.00 0.91 126
accuracy 0.74 1000
macro avg 0.76 0.74 0.74 1000
weighted avg 0.76 0.74 0.74 1000
Accuracy Score: 0.739
Root Mean Square Error: 1.4567086187704115
Model 1d: CNN with 1 convolution/max pooling layers (no regularization). filters=32, first dense layer units=1024¶
In [ ]:
start_time = datetime.now()
model1d = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=1024, activation='relu'),
Dense(units=8, activation='softmax')
])
model1d.summary()
model1d.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model1d.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model1d = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model1d.evaluate(x_test_norm, y_test)[1]:.3f}")
preds1d = model1d.predict(x_test_norm)
print('shape of preds: ', preds1d.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred1d= model1d.predict(x_test_norm)
pred1d=np.argmax(pred1d, axis=1)
print_validation_report(y_test, pred1d)
plot_confusion_matrix(y_test,pred1d)
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_3 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_3 (MaxPoolin (None, 74, 74, 32) 0
g2D)
flatten_3 (Flatten) (None, 175232) 0
dense_6 (Dense) (None, 1024) 179438592
dense_7 (Dense) (None, 8) 8200
=================================================================
Total params: 179447688 (684.54 MB)
Trainable params: 179447688 (684.54 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - ETA: 0s - loss: 15.0693 - accuracy: 0.4008
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 48s 829ms/step - loss: 15.0693 - accuracy: 0.4008 - val_loss: 0.9683 - val_accuracy: 0.6450 Epoch 2/200 57/57 [==============================] - 47s 820ms/step - loss: 0.7785 - accuracy: 0.6858 - val_loss: 0.8265 - val_accuracy: 0.7450 Epoch 3/200 57/57 [==============================] - 45s 795ms/step - loss: 0.6658 - accuracy: 0.7375 - val_loss: 0.8107 - val_accuracy: 0.6500 Epoch 4/200 57/57 [==============================] - 42s 740ms/step - loss: 0.5190 - accuracy: 0.8078 - val_loss: 1.0694 - val_accuracy: 0.5650 Epoch 5/200 57/57 [==============================] - 47s 822ms/step - loss: 0.4557 - accuracy: 0.8217 - val_loss: 0.8076 - val_accuracy: 0.7275 Process Time: 0:03:51.023459 32/32 [==============================] - 1s 41ms/step - loss: 0.8234 - accuracy: 0.6800 Test acc: 0.680 32/32 [==============================] - 1s 41ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 42ms/step
Classification Report
precision recall f1-score support
0 0.89 0.45 0.60 141
1 0.44 0.80 0.56 114
2 0.57 0.61 0.59 132
3 0.63 0.88 0.73 129
4 0.77 0.43 0.56 134
5 0.65 0.45 0.53 106
6 0.84 0.99 0.91 118
7 0.99 0.86 0.92 126
accuracy 0.68 1000
macro avg 0.72 0.68 0.68 1000
weighted avg 0.73 0.68 0.68 1000
Accuracy Score: 0.68
Root Mean Square Error: 1.4855975228843106
Model 2a: CNN with 2 convolution/max pooling layers (no regularization). filters=32, 64. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model2a = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Flatten(),
Dense(units=128,activation='relu'),
Dense(units=8, activation='softmax')
])
model2a.summary()
model2a.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model2a.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model2a = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model2a.evaluate(x_test_norm, y_test)[1]:.3f}")
preds2a = model2a.predict(x_test_norm)
print('shape of preds: ', preds2a.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred2a= model2a.predict(x_test_norm)
pred2a=np.argmax(pred2a, axis=1)
print_validation_report(y_test, pred2a)
plot_confusion_matrix(y_test,pred2a)
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_4 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_4 (MaxPoolin (None, 74, 74, 32) 0
g2D)
conv2d_5 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_5 (MaxPoolin (None, 36, 36, 64) 0
g2D)
flatten_4 (Flatten) (None, 82944) 0
dense_8 (Dense) (None, 128) 10616960
dense_9 (Dense) (None, 8) 1032
=================================================================
Total params: 10637384 (40.58 MB)
Trainable params: 10637384 (40.58 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - ETA: 0s - loss: 1.3155 - accuracy: 0.5086
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 16s 267ms/step - loss: 1.3155 - accuracy: 0.5086 - val_loss: 1.0206 - val_accuracy: 0.5675 Epoch 2/200 57/57 [==============================] - 14s 254ms/step - loss: 0.7856 - accuracy: 0.6758 - val_loss: 0.8961 - val_accuracy: 0.6650 Epoch 3/200 57/57 [==============================] - 14s 253ms/step - loss: 0.6481 - accuracy: 0.7308 - val_loss: 0.8028 - val_accuracy: 0.6675 Epoch 4/200 57/57 [==============================] - 15s 261ms/step - loss: 0.5363 - accuracy: 0.7778 - val_loss: 0.6799 - val_accuracy: 0.7575 Epoch 5/200 57/57 [==============================] - 14s 253ms/step - loss: 0.4836 - accuracy: 0.8014 - val_loss: 0.7614 - val_accuracy: 0.7325 Epoch 6/200 57/57 [==============================] - 14s 253ms/step - loss: 0.3384 - accuracy: 0.8842 - val_loss: 0.7519 - val_accuracy: 0.7425 Epoch 7/200 57/57 [==============================] - 14s 248ms/step - loss: 0.2505 - accuracy: 0.9164 - val_loss: 0.8571 - val_accuracy: 0.7200 Process Time: 0:01:44.670141 32/32 [==============================] - 1s 36ms/step - loss: 0.6724 - accuracy: 0.7390 Test acc: 0.739 32/32 [==============================] - 1s 35ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 35ms/step
Classification Report
precision recall f1-score support
0 0.83 0.76 0.79 141
1 0.57 0.52 0.54 114
2 0.65 0.69 0.67 132
3 0.87 0.85 0.86 129
4 0.69 0.72 0.71 134
5 0.61 0.72 0.66 106
6 0.99 0.62 0.76 118
7 0.78 1.00 0.88 126
accuracy 0.74 1000
macro avg 0.75 0.73 0.73 1000
weighted avg 0.75 0.74 0.74 1000
Accuracy Score: 0.739
Root Mean Square Error: 1.3390294993016396
Model 2b: CNN with 3 convolution/max pooling layers (no regularization). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model2b = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Flatten(),
Dense(units=128,activation='relu'),
Dense(units=8, activation='softmax')
])
model2b.summary()
model2b.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model2b.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model2b = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model2b.evaluate(x_test_norm, y_test)[1]:.3f}")
preds2b = model2b.predict(x_test_norm)
print('shape of preds: ', preds2b.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred2b= model2b.predict(x_test_norm)
pred2b=np.argmax(pred2b, axis=1)
print_validation_report(y_test, pred2b)
plot_confusion_matrix(y_test,pred2b)
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_6 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_6 (MaxPoolin (None, 74, 74, 32) 0
g2D)
conv2d_7 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_7 (MaxPoolin (None, 36, 36, 64) 0
g2D)
conv2d_8 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_8 (MaxPoolin (None, 17, 17, 128) 0
g2D)
flatten_5 (Flatten) (None, 36992) 0
dense_10 (Dense) (None, 128) 4735104
dense_11 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 17s 283ms/step - loss: 1.1625 - accuracy: 0.4953 - val_loss: 0.9029 - val_accuracy: 0.6375
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 15s 271ms/step - loss: 0.7649 - accuracy: 0.6867 - val_loss: 0.9153 - val_accuracy: 0.6350 Epoch 3/200 57/57 [==============================] - 16s 276ms/step - loss: 0.6636 - accuracy: 0.7244 - val_loss: 0.7221 - val_accuracy: 0.7400 Epoch 4/200 57/57 [==============================] - 16s 275ms/step - loss: 0.5330 - accuracy: 0.7914 - val_loss: 0.8747 - val_accuracy: 0.6200 Epoch 5/200 57/57 [==============================] - 16s 278ms/step - loss: 0.5312 - accuracy: 0.7842 - val_loss: 0.6245 - val_accuracy: 0.7750 Epoch 6/200 57/57 [==============================] - 15s 270ms/step - loss: 0.4245 - accuracy: 0.8383 - val_loss: 0.6820 - val_accuracy: 0.7800 Epoch 7/200 57/57 [==============================] - 15s 268ms/step - loss: 0.4050 - accuracy: 0.8467 - val_loss: 0.8341 - val_accuracy: 0.7025 Epoch 8/200 57/57 [==============================] - 16s 274ms/step - loss: 0.4531 - accuracy: 0.8286 - val_loss: 0.6062 - val_accuracy: 0.7850 Epoch 9/200 57/57 [==============================] - 16s 275ms/step - loss: 0.2871 - accuracy: 0.8942 - val_loss: 0.6018 - val_accuracy: 0.8025 Epoch 10/200 57/57 [==============================] - 16s 277ms/step - loss: 0.2404 - accuracy: 0.9175 - val_loss: 0.6424 - val_accuracy: 0.8225 Epoch 11/200 57/57 [==============================] - 16s 279ms/step - loss: 0.1694 - accuracy: 0.9475 - val_loss: 1.0564 - val_accuracy: 0.6850 Epoch 12/200 57/57 [==============================] - 16s 281ms/step - loss: 0.1823 - accuracy: 0.9383 - val_loss: 0.8631 - val_accuracy: 0.7575 Epoch 13/200 57/57 [==============================] - 16s 273ms/step - loss: 0.2477 - accuracy: 0.9211 - val_loss: 0.9581 - val_accuracy: 0.7650 Process Time: 0:03:26.909792 32/32 [==============================] - 1s 37ms/step - loss: 0.6277 - accuracy: 0.7820 Test acc: 0.782 32/32 [==============================] - 1s 38ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 37ms/step
Classification Report
precision recall f1-score support
0 0.90 0.87 0.88 141
1 0.55 0.69 0.61 114
2 0.78 0.66 0.71 132
3 0.87 0.90 0.88 129
4 0.82 0.60 0.69 134
5 0.65 0.79 0.71 106
6 0.96 0.75 0.84 118
7 0.80 1.00 0.89 126
accuracy 0.78 1000
macro avg 0.79 0.78 0.78 1000
weighted avg 0.80 0.78 0.78 1000
Accuracy Score: 0.782
Root Mean Square Error: 1.1861703081766968
Model 2c: CNN with 4 convolution/max pooling layers (no regularization). filters=32, 64, 128, 256. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model2c = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Flatten(),
Dense(units=128,activation='relu'),
Dense(units=8, activation='softmax')
])
model2c.summary()
model2c.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model2c.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model2c = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model2c.evaluate(x_test_norm, y_test)[1]:.3f}")
preds2c = model2c.predict(x_test_norm)
print('shape of preds: ', preds2c.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred2c= model2c.predict(x_test_norm)
pred2c=np.argmax(pred2c, axis=1)
print_validation_report(y_test, pred2c)
plot_confusion_matrix(y_test,pred2c)
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_9 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_9 (MaxPoolin (None, 74, 74, 32) 0
g2D)
conv2d_10 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_10 (MaxPooli (None, 36, 36, 64) 0
ng2D)
conv2d_11 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_11 (MaxPooli (None, 17, 17, 128) 0
ng2D)
conv2d_12 (Conv2D) (None, 15, 15, 256) 295168
max_pooling2d_12 (MaxPooli (None, 7, 7, 256) 0
ng2D)
flatten_6 (Flatten) (None, 12544) 0
dense_12 (Dense) (None, 128) 1605760
dense_13 (Dense) (None, 8) 1032
=================================================================
Total params: 1995208 (7.61 MB)
Trainable params: 1995208 (7.61 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 18s 302ms/step - loss: 1.3739 - accuracy: 0.4269 - val_loss: 1.1064 - val_accuracy: 0.5300
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 17s 294ms/step - loss: 0.9195 - accuracy: 0.6114 - val_loss: 0.9986 - val_accuracy: 0.6100 Epoch 3/200 57/57 [==============================] - 17s 305ms/step - loss: 0.7762 - accuracy: 0.6972 - val_loss: 0.7722 - val_accuracy: 0.7400 Epoch 4/200 57/57 [==============================] - 17s 295ms/step - loss: 0.6799 - accuracy: 0.7281 - val_loss: 0.8604 - val_accuracy: 0.7400 Epoch 5/200 57/57 [==============================] - 17s 291ms/step - loss: 0.6561 - accuracy: 0.7464 - val_loss: 0.7059 - val_accuracy: 0.7275 Epoch 6/200 57/57 [==============================] - 17s 298ms/step - loss: 0.6278 - accuracy: 0.7436 - val_loss: 0.7102 - val_accuracy: 0.7450 Epoch 7/200 57/57 [==============================] - 17s 295ms/step - loss: 0.6171 - accuracy: 0.7467 - val_loss: 0.6190 - val_accuracy: 0.8075 Epoch 8/200 57/57 [==============================] - 17s 295ms/step - loss: 0.6158 - accuracy: 0.7619 - val_loss: 0.6246 - val_accuracy: 0.7800 Epoch 9/200 57/57 [==============================] - 17s 294ms/step - loss: 0.5260 - accuracy: 0.7967 - val_loss: 0.6519 - val_accuracy: 0.7550 Epoch 10/200 57/57 [==============================] - 17s 292ms/step - loss: 0.5590 - accuracy: 0.7794 - val_loss: 0.5742 - val_accuracy: 0.7675 Process Time: 0:02:51.758139 32/32 [==============================] - 1s 39ms/step - loss: 0.5980 - accuracy: 0.7690 Test acc: 0.769 32/32 [==============================] - 1s 40ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 40ms/step
Classification Report
precision recall f1-score support
0 0.84 0.91 0.88 141
1 0.50 0.65 0.56 114
2 0.80 0.71 0.75 132
3 0.91 0.90 0.91 129
4 0.76 0.51 0.61 134
5 0.69 0.73 0.71 106
6 0.93 0.71 0.81 118
7 0.78 1.00 0.88 126
accuracy 0.77 1000
macro avg 0.78 0.77 0.76 1000
weighted avg 0.78 0.77 0.77 1000
Accuracy Score: 0.769
Root Mean Square Error: 1.277888884058391
Model 3a: CNN with 3 convolution/max pooling layers (dropout=0.2). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model3a = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Dropout(0.2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Dropout(0.2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Dropout(0.2),
Flatten(),
Dense(units=128,activation='relu'),
Dropout(0.2),
Dense(units=8, activation='softmax')
])
model3a.summary()
model3a.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model3a.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model3a = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model3a.evaluate(x_test_norm, y_test)[1]:.3f}")
preds3a = model3a.predict(x_test_norm)
print('shape of preds: ', preds3a.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred3a= model3a.predict(x_test_norm)
pred3a=np.argmax(pred3a, axis=1)
print_validation_report(y_test, pred3a)
plot_confusion_matrix(y_test,pred3a)
Model: "sequential_7"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_13 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_13 (MaxPooli (None, 74, 74, 32) 0
ng2D)
dropout (Dropout) (None, 74, 74, 32) 0
conv2d_14 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_14 (MaxPooli (None, 36, 36, 64) 0
ng2D)
dropout_1 (Dropout) (None, 36, 36, 64) 0
conv2d_15 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_15 (MaxPooli (None, 17, 17, 128) 0
ng2D)
dropout_2 (Dropout) (None, 17, 17, 128) 0
flatten_7 (Flatten) (None, 36992) 0
dense_14 (Dense) (None, 128) 4735104
dropout_3 (Dropout) (None, 128) 0
dense_15 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 19s 315ms/step - loss: 1.7936 - accuracy: 0.2722 - val_loss: 1.2770 - val_accuracy: 0.4500
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 17s 304ms/step - loss: 1.1249 - accuracy: 0.5086 - val_loss: 1.1366 - val_accuracy: 0.5750 Epoch 3/200 57/57 [==============================] - 17s 303ms/step - loss: 0.9701 - accuracy: 0.5747 - val_loss: 0.9999 - val_accuracy: 0.6700 Epoch 4/200 57/57 [==============================] - 18s 309ms/step - loss: 0.8793 - accuracy: 0.6497 - val_loss: 0.8482 - val_accuracy: 0.6575 Epoch 5/200 57/57 [==============================] - 18s 313ms/step - loss: 0.7573 - accuracy: 0.6994 - val_loss: 0.8091 - val_accuracy: 0.7225 Epoch 6/200 57/57 [==============================] - 17s 306ms/step - loss: 0.7149 - accuracy: 0.7156 - val_loss: 0.7341 - val_accuracy: 0.7400 Epoch 7/200 57/57 [==============================] - 18s 307ms/step - loss: 0.6314 - accuracy: 0.7561 - val_loss: 0.6938 - val_accuracy: 0.7450 Epoch 8/200 57/57 [==============================] - 17s 301ms/step - loss: 0.6137 - accuracy: 0.7586 - val_loss: 0.7413 - val_accuracy: 0.7625 Epoch 9/200 57/57 [==============================] - 18s 309ms/step - loss: 0.5852 - accuracy: 0.7633 - val_loss: 0.6900 - val_accuracy: 0.7650 Epoch 10/200 57/57 [==============================] - 17s 302ms/step - loss: 0.5514 - accuracy: 0.7925 - val_loss: 0.6882 - val_accuracy: 0.7925 Epoch 11/200 57/57 [==============================] - 18s 309ms/step - loss: 0.4934 - accuracy: 0.8119 - val_loss: 0.6419 - val_accuracy: 0.7675 Epoch 12/200 57/57 [==============================] - 18s 316ms/step - loss: 0.4943 - accuracy: 0.8169 - val_loss: 0.6357 - val_accuracy: 0.8175 Epoch 13/200 57/57 [==============================] - 18s 309ms/step - loss: 0.4252 - accuracy: 0.8428 - val_loss: 0.6466 - val_accuracy: 0.7900 Epoch 14/200 57/57 [==============================] - 17s 304ms/step - loss: 0.4500 - accuracy: 0.8264 - val_loss: 0.6226 - val_accuracy: 0.8100 Epoch 15/200 57/57 [==============================] - 17s 297ms/step - loss: 0.3697 - accuracy: 0.8575 - val_loss: 0.5736 - val_accuracy: 0.8025 Process Time: 0:04:25.514517 32/32 [==============================] - 1s 38ms/step - loss: 0.6178 - accuracy: 0.7780 Test acc: 0.778 32/32 [==============================] - 1s 37ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 38ms/step
Classification Report
precision recall f1-score support
0 0.94 0.71 0.81 141
1 0.47 0.51 0.49 114
2 0.66 0.60 0.63 132
3 0.90 0.86 0.88 129
4 0.67 0.81 0.73 134
5 0.72 0.81 0.76 106
6 0.98 0.92 0.95 118
7 0.93 1.00 0.97 126
accuracy 0.78 1000
macro avg 0.79 0.78 0.78 1000
weighted avg 0.79 0.78 0.78 1000
Accuracy Score: 0.778
Root Mean Square Error: 1.2489995996796797
In [49]:
# Extracts the outputs of all layers:
layer_outputs = [layer.output for layer in model3a.layers]
# Creates a model that will return these outputs, given the model input:
activation_model = models.Model(inputs=model3a.input, outputs=layer_outputs)
# Get activation values for the last dense layer
activations = activation_model.predict(x_valid_norm[:2000])
dense_layer_activations = activations[-3]
output_layer_activations = activations[-1]
13/13 [==============================] - 0s 26ms/step
sklearn.manifold.TSNE¶
In [50]:
from sklearn.manifold import TSNE
# Reduce the dimension using T-SNE to visualize i n a scatterplot
tsne = TSNE(n_components=2, verbose=1, init='pca', learning_rate='auto', perplexity=40, n_iter=300)
tsne_results = tsne.fit_transform(dense_layer_activations)
# Scaling
tsne_results = (tsne_results - tsne_results.min()) / (tsne_results.max() - tsne_results.min())
[t-SNE] Computing 121 nearest neighbors... [t-SNE] Indexed 400 samples in 0.000s... [t-SNE] Computed neighbors for 400 samples in 0.245s... [t-SNE] Computed conditional probabilities for sample 400 / 400 [t-SNE] Mean sigma: 1.601560 [t-SNE] KL divergence after 250 iterations with early exaggeration: 48.118782 [t-SNE] KL divergence after 300 iterations: 0.285539
In [51]:
import matplotlib as mpl
cmap = plt.cm.tab10
plt.figure(figsize=(16,10))
scatter = plt.scatter(tsne_results[:,0],tsne_results[:,1], c=y_valid_split[:2000], s=10, cmap=cmap)
plt.legend(handles=scatter.legend_elements()[0], labels=class_names)
image_positions = np.array([[1., 1.]])
for index, position in enumerate(tsne_results):
dist = np.sum((position - image_positions) ** 2, axis=1)
if np.min(dist) > 0.02: # if far enough from other images
image_positions = np.r_[image_positions, [position]]
imagebox = mpl.offsetbox.AnnotationBbox(
mpl.offsetbox.OffsetImage(x_train[index], cmap="binary", zoom=0.3),
position, bboxprops={"lw": 1})
plt.gca().add_artist(imagebox)
plt.axis("off")
plt.show()
Model 3b: CNN with 3 convolution/max pooling layers (dropout=0.3). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model3b = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Dropout(0.3),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Dropout(0.3),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Dropout(0.3),
Flatten(),
Dense(units=128,activation='relu'),
Dropout(0.3),
Dense(units=8, activation='softmax')
])
model3b.summary()
model3b.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model3b.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model3b = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model3b.evaluate(x_test_norm, y_test)[1]:.3f}")
preds3b = model3b.predict(x_test_norm)
print('shape of preds: ', preds3b.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred3b= model3b.predict(x_test_norm)
pred3b=np.argmax(pred3b, axis=1)
print_validation_report(y_test, pred3b)
plot_confusion_matrix(y_test,pred3b)
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_16 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_16 (MaxPooli (None, 74, 74, 32) 0
ng2D)
dropout_4 (Dropout) (None, 74, 74, 32) 0
conv2d_17 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_17 (MaxPooli (None, 36, 36, 64) 0
ng2D)
dropout_5 (Dropout) (None, 36, 36, 64) 0
conv2d_18 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_18 (MaxPooli (None, 17, 17, 128) 0
ng2D)
dropout_6 (Dropout) (None, 17, 17, 128) 0
flatten_8 (Flatten) (None, 36992) 0
dense_16 (Dense) (None, 128) 4735104
dropout_7 (Dropout) (None, 128) 0
dense_17 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 19s 315ms/step - loss: 2.0889 - accuracy: 0.1842 - val_loss: 1.6201 - val_accuracy: 0.2825
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 17s 301ms/step - loss: 1.2401 - accuracy: 0.4464 - val_loss: 1.1266 - val_accuracy: 0.5000 Epoch 3/200 57/57 [==============================] - 17s 303ms/step - loss: 1.0264 - accuracy: 0.5289 - val_loss: 0.9872 - val_accuracy: 0.6125 Epoch 4/200 57/57 [==============================] - 17s 298ms/step - loss: 0.8964 - accuracy: 0.6144 - val_loss: 0.9041 - val_accuracy: 0.6500 Epoch 5/200 57/57 [==============================] - 17s 295ms/step - loss: 0.8638 - accuracy: 0.6461 - val_loss: 0.8827 - val_accuracy: 0.6700 Epoch 6/200 57/57 [==============================] - 17s 301ms/step - loss: 0.7501 - accuracy: 0.7042 - val_loss: 0.7475 - val_accuracy: 0.7675 Epoch 7/200 57/57 [==============================] - 17s 299ms/step - loss: 0.7392 - accuracy: 0.7136 - val_loss: 0.7129 - val_accuracy: 0.7600 Epoch 8/200 57/57 [==============================] - 17s 295ms/step - loss: 0.6957 - accuracy: 0.7294 - val_loss: 0.7286 - val_accuracy: 0.7550 Epoch 9/200 57/57 [==============================] - 17s 297ms/step - loss: 0.6424 - accuracy: 0.7508 - val_loss: 1.0196 - val_accuracy: 0.5800 Process Time: 0:02:37.061803 32/32 [==============================] - 1s 39ms/step - loss: 0.7081 - accuracy: 0.7290 Test acc: 0.729 32/32 [==============================] - 1s 37ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 37ms/step
Classification Report
precision recall f1-score support
0 0.77 0.88 0.82 141
1 0.64 0.49 0.56 114
2 0.72 0.69 0.70 132
3 0.96 0.84 0.90 129
4 0.67 0.72 0.69 134
5 0.65 0.73 0.68 106
6 0.93 0.42 0.58 118
7 0.65 1.00 0.79 126
accuracy 0.73 1000
macro avg 0.75 0.72 0.72 1000
weighted avg 0.75 0.73 0.72 1000
Accuracy Score: 0.729
Root Mean Square Error: 1.3590437814875576
Model 3c: CNN with 3 convolution/max pooling layers (dropout=0.4). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model3c = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Dropout(0.4),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Dropout(0.4),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Dropout(0.4),
Flatten(),
Dense(units=128,activation='relu'),
Dropout(0.4),
Dense(units=8, activation='softmax')
])
model3c.summary()
model3c.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model3c.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model3c = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model3c.evaluate(x_test_norm, y_test)[1]:.3f}")
preds3c = model3c.predict(x_test_norm)
print('shape of preds: ', preds3c.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred3c= model3c.predict(x_test_norm)
pred3c=np.argmax(pred3c, axis=1)
print_validation_report(y_test, pred3c)
plot_confusion_matrix(y_test,pred3c)
Model: "sequential_9"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_19 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_19 (MaxPooli (None, 74, 74, 32) 0
ng2D)
dropout_8 (Dropout) (None, 74, 74, 32) 0
conv2d_20 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_20 (MaxPooli (None, 36, 36, 64) 0
ng2D)
dropout_9 (Dropout) (None, 36, 36, 64) 0
conv2d_21 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_21 (MaxPooli (None, 17, 17, 128) 0
ng2D)
dropout_10 (Dropout) (None, 17, 17, 128) 0
flatten_9 (Flatten) (None, 36992) 0
dense_18 (Dense) (None, 128) 4735104
dropout_11 (Dropout) (None, 128) 0
dense_19 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 19s 308ms/step - loss: 2.1312 - accuracy: 0.1692 - val_loss: 1.8253 - val_accuracy: 0.2300
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 17s 302ms/step - loss: 1.6584 - accuracy: 0.2653 - val_loss: 1.6305 - val_accuracy: 0.3200 Epoch 3/200 57/57 [==============================] - 17s 306ms/step - loss: 1.4457 - accuracy: 0.3642 - val_loss: 1.2706 - val_accuracy: 0.4775 Epoch 4/200 57/57 [==============================] - 18s 307ms/step - loss: 1.0962 - accuracy: 0.5078 - val_loss: 1.0487 - val_accuracy: 0.4875 Epoch 5/200 57/57 [==============================] - 17s 302ms/step - loss: 0.9990 - accuracy: 0.5525 - val_loss: 0.9280 - val_accuracy: 0.6400 Epoch 6/200 57/57 [==============================] - 17s 302ms/step - loss: 0.8814 - accuracy: 0.6258 - val_loss: 0.8765 - val_accuracy: 0.6675 Epoch 7/200 57/57 [==============================] - 17s 302ms/step - loss: 0.8303 - accuracy: 0.6533 - val_loss: 0.7804 - val_accuracy: 0.6775 Epoch 8/200 57/57 [==============================] - 17s 296ms/step - loss: 0.7952 - accuracy: 0.6589 - val_loss: 0.8148 - val_accuracy: 0.6550 Epoch 9/200 57/57 [==============================] - 17s 302ms/step - loss: 0.7258 - accuracy: 0.7014 - val_loss: 0.7402 - val_accuracy: 0.7875 Epoch 10/200 57/57 [==============================] - 17s 302ms/step - loss: 0.7121 - accuracy: 0.7053 - val_loss: 0.6636 - val_accuracy: 0.8150 Epoch 11/200 57/57 [==============================] - 17s 307ms/step - loss: 0.6846 - accuracy: 0.7125 - val_loss: 0.6617 - val_accuracy: 0.7100 Epoch 12/200 57/57 [==============================] - 17s 306ms/step - loss: 0.6634 - accuracy: 0.7269 - val_loss: 0.6371 - val_accuracy: 0.7250 Epoch 13/200 57/57 [==============================] - 17s 301ms/step - loss: 0.6317 - accuracy: 0.7381 - val_loss: 0.7187 - val_accuracy: 0.6900 Process Time: 0:03:47.730748 32/32 [==============================] - 1s 40ms/step - loss: 0.6558 - accuracy: 0.6680 Test acc: 0.668 32/32 [==============================] - 1s 39ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 37ms/step
Classification Report
precision recall f1-score support
0 0.93 0.75 0.83 141
1 0.50 0.63 0.56 114
2 0.76 0.71 0.74 132
3 0.97 0.87 0.91 129
4 0.73 0.53 0.61 134
5 0.60 0.91 0.72 106
6 0.48 0.99 0.64 118
7 0.00 0.00 0.00 126
accuracy 0.67 1000
macro avg 0.62 0.67 0.63 1000
weighted avg 0.63 0.67 0.63 1000
Accuracy Score: 0.668
Root Mean Square Error: 1.3769531582446803
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
Model 4a: CNN with 3 convolution/max pooling layers (L2 regularization=0.0001). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model4a = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:],
kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=128, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
Dense(units=8, activation='softmax')
])
model4a.summary()
model4a.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model4a.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model4a = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model4a.evaluate(x_test_norm, y_test)[1]:.3f}")
preds4a = model4a.predict(x_test_norm)
print('shape of preds: ', preds4a.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred4a= model4a.predict(x_test_norm)
pred4a=np.argmax(pred4a, axis=1)
print_validation_report(y_test, pred4a)
plot_confusion_matrix(y_test,pred4a)
Model: "sequential_10"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_22 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_22 (MaxPooli (None, 74, 74, 32) 0
ng2D)
conv2d_23 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_23 (MaxPooli (None, 36, 36, 64) 0
ng2D)
conv2d_24 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_24 (MaxPooli (None, 17, 17, 128) 0
ng2D)
flatten_10 (Flatten) (None, 36992) 0
dense_20 (Dense) (None, 128) 4735104
dense_21 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 17s 285ms/step - loss: 1.3890 - accuracy: 0.4683 - val_loss: 1.1200 - val_accuracy: 0.5200
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 16s 278ms/step - loss: 0.8405 - accuracy: 0.6653 - val_loss: 0.8080 - val_accuracy: 0.7475 Epoch 3/200 57/57 [==============================] - 16s 280ms/step - loss: 0.6958 - accuracy: 0.7294 - val_loss: 0.7463 - val_accuracy: 0.7300 Epoch 4/200 57/57 [==============================] - 16s 285ms/step - loss: 0.6251 - accuracy: 0.7636 - val_loss: 0.6465 - val_accuracy: 0.8000 Epoch 5/200 57/57 [==============================] - 16s 279ms/step - loss: 0.5874 - accuracy: 0.7944 - val_loss: 0.9735 - val_accuracy: 0.6425 Epoch 6/200 57/57 [==============================] - 16s 286ms/step - loss: 0.7012 - accuracy: 0.7311 - val_loss: 0.7913 - val_accuracy: 0.7075 Epoch 7/200 57/57 [==============================] - 16s 282ms/step - loss: 0.5202 - accuracy: 0.8172 - val_loss: 0.7356 - val_accuracy: 0.7600 Process Time: 0:01:55.486768 32/32 [==============================] - 1s 38ms/step - loss: 0.6830 - accuracy: 0.7620 Test acc: 0.762 32/32 [==============================] - 1s 37ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 37ms/step
Classification Report
precision recall f1-score support
0 0.83 0.84 0.84 141
1 0.47 0.73 0.57 114
2 0.91 0.51 0.65 132
3 0.96 0.78 0.86 129
4 0.73 0.55 0.63 134
5 0.59 0.87 0.70 106
6 0.98 0.86 0.91 118
7 0.88 1.00 0.93 126
accuracy 0.76 1000
macro avg 0.79 0.77 0.76 1000
weighted avg 0.80 0.76 0.76 1000
Accuracy Score: 0.762
Root Mean Square Error: 1.3535139452550904
Model 4b: CNN with 3 convolution/max pooling layers (L2 regularization=0.001). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model4b = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:],
kernel_regularizer=tf.keras.regularizers.L2(0.001)),
MaxPool2D((2, 2), strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.001)),
MaxPool2D((2, 2), strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.001)),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=128, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.001)),
Dense(units=8, activation='softmax')
])
model4b.summary()
model4b.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model4b.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model4b= tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model4b.evaluate(x_test_norm, y_test)[1]:.3f}")
preds4b = model4b.predict(x_test_norm)
print('shape of preds: ', preds4b.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred4b= model4b.predict(x_test_norm)
pred4b=np.argmax(pred4b, axis=1)
print_validation_report(y_test, pred4b)
plot_confusion_matrix(y_test,pred4b)
Model: "sequential_11"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_25 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_25 (MaxPooli (None, 74, 74, 32) 0
ng2D)
conv2d_26 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_26 (MaxPooli (None, 36, 36, 64) 0
ng2D)
conv2d_27 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_27 (MaxPooli (None, 17, 17, 128) 0
ng2D)
flatten_11 (Flatten) (None, 36992) 0
dense_22 (Dense) (None, 128) 4735104
dense_23 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 19s 307ms/step - loss: 1.4499 - accuracy: 0.4806 - val_loss: 1.1349 - val_accuracy: 0.6575
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 17s 293ms/step - loss: 0.9599 - accuracy: 0.6831 - val_loss: 1.0214 - val_accuracy: 0.6375 Epoch 3/200 57/57 [==============================] - 17s 292ms/step - loss: 0.8406 - accuracy: 0.7358 - val_loss: 0.9476 - val_accuracy: 0.7075 Epoch 4/200 57/57 [==============================] - 16s 286ms/step - loss: 0.7987 - accuracy: 0.7606 - val_loss: 0.7880 - val_accuracy: 0.7800 Epoch 5/200 57/57 [==============================] - 17s 290ms/step - loss: 0.8020 - accuracy: 0.7333 - val_loss: 0.9254 - val_accuracy: 0.6875 Epoch 6/200 57/57 [==============================] - 16s 287ms/step - loss: 0.7744 - accuracy: 0.7539 - val_loss: 0.8899 - val_accuracy: 0.7350 Epoch 7/200 57/57 [==============================] - 16s 283ms/step - loss: 0.6574 - accuracy: 0.8144 - val_loss: 0.8531 - val_accuracy: 0.7750 Process Time: 0:02:00.203089 32/32 [==============================] - 1s 39ms/step - loss: 0.8044 - accuracy: 0.7470 Test acc: 0.747 32/32 [==============================] - 1s 38ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 38ms/step
Classification Report
precision recall f1-score support
0 0.84 0.85 0.85 141
1 0.56 0.54 0.55 114
2 0.81 0.66 0.73 132
3 0.97 0.87 0.91 129
4 0.69 0.62 0.65 134
5 0.58 0.86 0.69 106
6 0.97 0.57 0.72 118
7 0.71 1.00 0.83 126
accuracy 0.75 1000
macro avg 0.76 0.74 0.74 1000
weighted avg 0.77 0.75 0.75 1000
Accuracy Score: 0.747
Root Mean Square Error: 1.363084736911099
Model 4c: CNN with 3 convolution/max pooling layers (L2 regularization=0.01). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model4c = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:],
kernel_regularizer=tf.keras.regularizers.L2(0.01)),
MaxPool2D((2, 2), strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.01)),
MaxPool2D((2, 2), strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.01)),
MaxPool2D((2, 2), strides=2),
Flatten(),
Dense(units=128, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.01)),
Dense(units=8, activation='softmax')
])
model4c.summary()
model4c.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model4c.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model4c= tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model4c.evaluate(x_test_norm, y_test)[1]:.3f}")
preds4c = model4c.predict(x_test_norm)
print('shape of preds: ', preds4c.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred4c= model4c.predict(x_test_norm)
pred4c=np.argmax(pred4c, axis=1)
print_validation_report(y_test, pred4c)
plot_confusion_matrix(y_test,pred4c)
Model: "sequential_12"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_28 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_28 (MaxPooli (None, 74, 74, 32) 0
ng2D)
conv2d_29 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_29 (MaxPooli (None, 36, 36, 64) 0
ng2D)
conv2d_30 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_30 (MaxPooli (None, 17, 17, 128) 0
ng2D)
flatten_12 (Flatten) (None, 36992) 0
dense_24 (Dense) (None, 128) 4735104
dense_25 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 18s 296ms/step - loss: 2.6555 - accuracy: 0.4419 - val_loss: 1.6586 - val_accuracy: 0.5950
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 16s 283ms/step - loss: 1.4610 - accuracy: 0.6178 - val_loss: 1.5013 - val_accuracy: 0.5925 Epoch 3/200 57/57 [==============================] - 16s 281ms/step - loss: 1.3416 - accuracy: 0.6211 - val_loss: 1.5420 - val_accuracy: 0.4825 Epoch 4/200 57/57 [==============================] - 16s 287ms/step - loss: 1.2776 - accuracy: 0.6339 - val_loss: 1.2161 - val_accuracy: 0.6875 Epoch 5/200 57/57 [==============================] - 17s 292ms/step - loss: 1.1272 - accuracy: 0.6994 - val_loss: 1.0625 - val_accuracy: 0.7800 Epoch 6/200 57/57 [==============================] - 16s 282ms/step - loss: 1.0427 - accuracy: 0.7389 - val_loss: 1.1594 - val_accuracy: 0.7025 Epoch 7/200 57/57 [==============================] - 16s 281ms/step - loss: 1.0689 - accuracy: 0.7050 - val_loss: 1.1490 - val_accuracy: 0.7075 Epoch 8/200 57/57 [==============================] - 16s 289ms/step - loss: 0.9771 - accuracy: 0.7500 - val_loss: 0.9525 - val_accuracy: 0.8225 Epoch 9/200 57/57 [==============================] - 16s 289ms/step - loss: 0.9728 - accuracy: 0.7511 - val_loss: 1.1054 - val_accuracy: 0.6500 Epoch 10/200 57/57 [==============================] - 17s 292ms/step - loss: 0.9314 - accuracy: 0.7664 - val_loss: 0.9673 - val_accuracy: 0.8000 Epoch 11/200 57/57 [==============================] - 16s 285ms/step - loss: 0.9183 - accuracy: 0.7558 - val_loss: 0.9394 - val_accuracy: 0.7825 Process Time: 0:03:02.985950 32/32 [==============================] - 1s 40ms/step - loss: 0.9791 - accuracy: 0.7450 Test acc: 0.745 32/32 [==============================] - 1s 38ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 37ms/step
Classification Report
precision recall f1-score support
0 0.94 0.54 0.68 141
1 0.54 0.46 0.49 114
2 0.52 0.80 0.63 132
3 0.95 0.81 0.87 129
4 0.71 0.73 0.72 134
5 0.63 0.74 0.68 106
6 0.98 0.89 0.93 118
7 0.91 1.00 0.95 126
accuracy 0.74 1000
macro avg 0.77 0.75 0.75 1000
weighted avg 0.78 0.74 0.75 1000
Accuracy Score: 0.745
Root Mean Square Error: 1.3928388277184118
Model 5a: CNN with 3 convolution/max pooling layers (drop out and L2 regularization). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model5a = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:],
kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Dropout(0.2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Dropout(0.2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Dropout(0.2),
Flatten(),
Dense(units=128, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
Dropout(0.2),
Dense(units=8, activation='softmax')
])
model5a.summary()
model5a.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model5a.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model5a= tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model5a.evaluate(x_test_norm, y_test)[1]:.3f}")
preds5a = model5a.predict(x_test_norm)
print('shape of preds: ', preds5a.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred5a= model5a.predict(x_test_norm)
pred5a=np.argmax(pred5a, axis=1)
print_validation_report(y_test, pred5a)
plot_confusion_matrix(y_test,pred5a)
Model: "sequential_14"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_34 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_34 (MaxPooli (None, 74, 74, 32) 0
ng2D)
dropout_16 (Dropout) (None, 74, 74, 32) 0
conv2d_35 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_35 (MaxPooli (None, 36, 36, 64) 0
ng2D)
dropout_17 (Dropout) (None, 36, 36, 64) 0
conv2d_36 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_36 (MaxPooli (None, 17, 17, 128) 0
ng2D)
dropout_18 (Dropout) (None, 17, 17, 128) 0
flatten_14 (Flatten) (None, 36992) 0
dense_28 (Dense) (None, 128) 4735104
dropout_19 (Dropout) (None, 128) 0
dense_29 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 19s 321ms/step - loss: 1.7954 - accuracy: 0.3294 - val_loss: 1.2033 - val_accuracy: 0.5100
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 18s 308ms/step - loss: 1.1059 - accuracy: 0.5244 - val_loss: 1.0424 - val_accuracy: 0.6525 Epoch 3/200 57/57 [==============================] - 17s 304ms/step - loss: 0.9597 - accuracy: 0.5944 - val_loss: 0.8766 - val_accuracy: 0.7250 Epoch 4/200 57/57 [==============================] - 18s 308ms/step - loss: 0.8619 - accuracy: 0.6622 - val_loss: 0.8414 - val_accuracy: 0.7400 Epoch 5/200 57/57 [==============================] - 18s 308ms/step - loss: 0.7958 - accuracy: 0.6950 - val_loss: 0.7660 - val_accuracy: 0.7675 Epoch 6/200 57/57 [==============================] - 17s 303ms/step - loss: 0.7427 - accuracy: 0.7261 - val_loss: 0.7765 - val_accuracy: 0.7325 Epoch 7/200 57/57 [==============================] - 17s 297ms/step - loss: 0.6976 - accuracy: 0.7422 - val_loss: 0.7875 - val_accuracy: 0.7650 Epoch 8/200 57/57 [==============================] - 17s 299ms/step - loss: 0.6428 - accuracy: 0.7633 - val_loss: 0.7891 - val_accuracy: 0.6925 Process Time: 0:02:22.626652 32/32 [==============================] - 1s 39ms/step - loss: 0.7814 - accuracy: 0.7300 Test acc: 0.730 32/32 [==============================] - 1s 35ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 37ms/step
Classification Report
precision recall f1-score support
0 0.80 0.64 0.71 141
1 0.62 0.49 0.55 114
2 0.83 0.53 0.65 132
3 0.82 0.87 0.85 129
4 0.68 0.75 0.71 134
5 0.48 0.82 0.61 106
6 0.95 0.75 0.84 118
7 0.81 1.00 0.89 126
accuracy 0.73 1000
macro avg 0.75 0.73 0.73 1000
weighted avg 0.76 0.73 0.73 1000
Accuracy Score: 0.73
Root Mean Square Error: 1.563649577111189
Model 5b: CNN with 3 convolution/max pooling layers (drop out and L2 regularization and batch normalization). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model5b = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu', input_shape=x_train_norm.shape[1:],
kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Dropout(0.2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Dropout(0.2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
MaxPool2D((2, 2), strides=2),
Dropout(0.2),
Flatten(),
Dense(units=128, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.0001)),
BatchNormalization(),
Dropout(0.2),
Dense(units=8, activation='softmax')
])
model5b.summary()
model5b.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model5b.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model5b= tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model5b.evaluate(x_test_norm, y_test)[1]:.3f}")
preds5b = model5b.predict(x_test_norm)
print('shape of preds: ', preds5b.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred5b= model5b.predict(x_test_norm)
pred5b=np.argmax(pred5b, axis=1)
print_validation_report(y_test, pred5b)
plot_confusion_matrix(y_test,pred5b)
Model: "sequential_15"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_37 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_37 (MaxPooli (None, 74, 74, 32) 0
ng2D)
dropout_20 (Dropout) (None, 74, 74, 32) 0
conv2d_38 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_38 (MaxPooli (None, 36, 36, 64) 0
ng2D)
dropout_21 (Dropout) (None, 36, 36, 64) 0
conv2d_39 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_39 (MaxPooli (None, 17, 17, 128) 0
ng2D)
dropout_22 (Dropout) (None, 17, 17, 128) 0
flatten_15 (Flatten) (None, 36992) 0
dense_30 (Dense) (None, 128) 4735104
batch_normalization (Batch (None, 128) 512
Normalization)
dropout_23 (Dropout) (None, 128) 0
dense_31 (Dense) (None, 8) 1032
=================================================================
Total params: 4829896 (18.42 MB)
Trainable params: 4829640 (18.42 MB)
Non-trainable params: 256 (1.00 KB)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 19s 314ms/step - loss: 1.3784 - accuracy: 0.4286 - val_loss: 2.5099 - val_accuracy: 0.2350
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 18s 310ms/step - loss: 0.9342 - accuracy: 0.6358 - val_loss: 2.2585 - val_accuracy: 0.4050 Epoch 3/200 57/57 [==============================] - 17s 298ms/step - loss: 0.7689 - accuracy: 0.7158 - val_loss: 3.7000 - val_accuracy: 0.3650 Epoch 4/200 57/57 [==============================] - 17s 301ms/step - loss: 0.7071 - accuracy: 0.7464 - val_loss: 4.4490 - val_accuracy: 0.1800 Epoch 5/200 57/57 [==============================] - 17s 306ms/step - loss: 0.6814 - accuracy: 0.7519 - val_loss: 2.1241 - val_accuracy: 0.4925 Epoch 6/200 57/57 [==============================] - 17s 301ms/step - loss: 0.5956 - accuracy: 0.7897 - val_loss: 4.6588 - val_accuracy: 0.3000 Epoch 7/200 57/57 [==============================] - 17s 298ms/step - loss: 0.5597 - accuracy: 0.8108 - val_loss: 3.6531 - val_accuracy: 0.2100 Epoch 8/200 57/57 [==============================] - 17s 297ms/step - loss: 0.5253 - accuracy: 0.8175 - val_loss: 2.1345 - val_accuracy: 0.3750 Process Time: 0:02:21.539076 32/32 [==============================] - 1s 37ms/step - loss: 2.2388 - accuracy: 0.4540 Test acc: 0.454 32/32 [==============================] - 1s 38ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 39ms/step
Classification Report
precision recall f1-score support
0 0.27 0.70 0.39 141
1 0.54 0.68 0.60 114
2 0.87 0.45 0.60 132
3 1.00 0.67 0.81 129
4 0.73 0.20 0.32 134
5 0.34 0.98 0.51 106
6 0.00 0.00 0.00 118
7 0.00 0.00 0.00 126
accuracy 0.45 1000
macro avg 0.47 0.46 0.40 1000
weighted avg 0.48 0.45 0.40 1000
Accuracy Score: 0.454
Root Mean Square Error: 3.6191159141425686
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))
Model 5c: CNN with 3 convolution/max pooling layers (drop out 0.2 and augmentation). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest'
)
datagen.fit(x_train_norm)
start_time = datetime.now()
model5c = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Flatten(),
Dense(units=128,activation='relu'),
Dense(units=8, activation='softmax')
])
model5c.summary()
model5c.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model5c.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model5c = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model5c.evaluate(x_test_norm, y_test)[1]:.3f}")
preds5c = model5c.predict(x_test_norm)
print('shape of preds: ', preds5c.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred5c= model5c.predict(x_test_norm)
pred5c=np.argmax(pred5c, axis=1)
print_validation_report(y_test, pred5c)
plot_confusion_matrix(y_test,pred5c)
Model 6a: CNN with 3 convolution/max pooling layers (no regularization, optimizer='RMSprop'). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model6a = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Flatten(),
Dense(units=128,activation='relu'),
Dense(units=8, activation='softmax')
])
model6a.summary()
model6a.compile(optimizer='RMSprop',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model6a.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model6a = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model6a.evaluate(x_test_norm, y_test)[1]:.3f}")
preds6a = model6a.predict(x_test_norm)
print('shape of preds: ', preds6a.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred6a= model6a.predict(x_test_norm)
pred6a=np.argmax(pred6a, axis=1)
print_validation_report(y_test, pred6a)
plot_confusion_matrix(y_test,pred6a)
Model: "sequential_16"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_40 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_40 (MaxPooli (None, 74, 74, 32) 0
ng2D)
conv2d_41 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_41 (MaxPooli (None, 36, 36, 64) 0
ng2D)
conv2d_42 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_42 (MaxPooli (None, 17, 17, 128) 0
ng2D)
flatten_16 (Flatten) (None, 36992) 0
dense_32 (Dense) (None, 128) 4735104
dense_33 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 18s 299ms/step - loss: 1.6475 - accuracy: 0.3086 - val_loss: 1.2062 - val_accuracy: 0.5125
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 16s 286ms/step - loss: 1.1641 - accuracy: 0.5044 - val_loss: 1.3866 - val_accuracy: 0.4475 Epoch 3/200 57/57 [==============================] - 16s 276ms/step - loss: 1.0141 - accuracy: 0.5644 - val_loss: 1.2945 - val_accuracy: 0.4875 Epoch 4/200 57/57 [==============================] - 16s 278ms/step - loss: 0.9077 - accuracy: 0.6083 - val_loss: 1.1587 - val_accuracy: 0.6425 Epoch 5/200 57/57 [==============================] - 16s 287ms/step - loss: 0.8397 - accuracy: 0.6564 - val_loss: 1.0043 - val_accuracy: 0.6150 Epoch 6/200 57/57 [==============================] - 16s 286ms/step - loss: 0.8013 - accuracy: 0.6925 - val_loss: 1.7754 - val_accuracy: 0.4150 Epoch 7/200 57/57 [==============================] - 16s 284ms/step - loss: 0.7103 - accuracy: 0.7267 - val_loss: 0.9362 - val_accuracy: 0.6350 Process Time: 0:01:56.465506 32/32 [==============================] - 1s 40ms/step - loss: 0.9809 - accuracy: 0.6400 Test acc: 0.640 32/32 [==============================] - 1s 41ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 39ms/step
Classification Report
precision recall f1-score support
0 0.94 0.34 0.50 141
1 0.48 0.66 0.55 114
2 0.90 0.28 0.43 132
3 0.90 0.81 0.85 129
4 0.63 0.60 0.62 134
5 0.39 0.93 0.55 106
6 0.92 0.59 0.72 118
7 0.72 1.00 0.84 126
accuracy 0.64 1000
macro avg 0.74 0.65 0.63 1000
weighted avg 0.75 0.64 0.63 1000
Accuracy Score: 0.64
Root Mean Square Error: 1.8705614130522419
Model 6b: CNN with 3 convolution/max pooling layers (no regularization, optimizer='SGD'). filters=32, 64, 128. first dense layer units=128¶
In [ ]:
start_time = datetime.now()
model6b = Sequential([
Conv2D(filters=32, kernel_size=(3, 3), strides=(1, 1), activation='relu',input_shape=x_train_norm.shape[1:]),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=64, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Conv2D(filters=128, kernel_size=(3, 3), strides=(1, 1), activation='relu'),
MaxPool2D((2, 2),strides=2),
Flatten(),
Dense(units=128,activation='relu'),
Dense(units=8, activation='softmax')
])
model6b.summary()
model6b.compile(optimizer='SGD',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
history = model6b.fit(x_train_norm
,y_train_split
,epochs=200
,batch_size=64
,validation_data=(x_valid_norm, y_valid_split)
,callbacks=[
tf.keras.callbacks.ModelCheckpoint("colorectal_histology.h5",save_best_only=True,save_weights_only=False)
,tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
]
)
end_time = datetime.now()
process_time= end_time - start_time
print("Process Time:", process_time)
model6b = tf.keras.models.load_model("colorectal_histology.h5")
print(f"Test acc: {model6b.evaluate(x_test_norm, y_test)[1]:.3f}")
preds6b = model6b.predict(x_test_norm)
print('shape of preds: ', preds6b.shape)
history_dict = history.history
history_dict.keys()
history_df=pd.DataFrame(history_dict)
history_df.tail().round(3)
plt.subplots(figsize=(16,12))
plt.tight_layout()
display_training_curves(history_df['accuracy'], history_df['val_accuracy'], 'accuracy', 211)
display_training_curves(history_df['loss'], history_df['val_loss'], 'loss', 212)
pred6b= model6b.predict(x_test_norm)
pred6b=np.argmax(pred6b, axis=1)
print_validation_report(y_test, pred6b)
plot_confusion_matrix(y_test,pred6b)
Model: "sequential_17"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_43 (Conv2D) (None, 148, 148, 32) 896
max_pooling2d_43 (MaxPooli (None, 74, 74, 32) 0
ng2D)
conv2d_44 (Conv2D) (None, 72, 72, 64) 18496
max_pooling2d_44 (MaxPooli (None, 36, 36, 64) 0
ng2D)
conv2d_45 (Conv2D) (None, 34, 34, 128) 73856
max_pooling2d_45 (MaxPooli (None, 17, 17, 128) 0
ng2D)
flatten_17 (Flatten) (None, 36992) 0
dense_34 (Dense) (None, 128) 4735104
dense_35 (Dense) (None, 8) 1032
=================================================================
Total params: 4829384 (18.42 MB)
Trainable params: 4829384 (18.42 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/200
57/57 [==============================] - 17s 294ms/step - loss: 2.0080 - accuracy: 0.1603 - val_loss: 1.9030 - val_accuracy: 0.2475
Epoch 2/200
/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py:3103: UserWarning: You are saving your model as an HDF5 file via `model.save()`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')`.
saving_api.save_model(
57/57 [==============================] - 16s 279ms/step - loss: 1.6635 - accuracy: 0.3206 - val_loss: 1.5435 - val_accuracy: 0.2950 Epoch 3/200 57/57 [==============================] - 16s 278ms/step - loss: 1.4167 - accuracy: 0.3967 - val_loss: 1.4911 - val_accuracy: 0.2900 Epoch 4/200 57/57 [==============================] - 16s 276ms/step - loss: 1.2998 - accuracy: 0.4292 - val_loss: 1.3615 - val_accuracy: 0.4300 Epoch 5/200 57/57 [==============================] - 16s 278ms/step - loss: 1.2071 - accuracy: 0.4614 - val_loss: 1.1829 - val_accuracy: 0.4050 Epoch 6/200 57/57 [==============================] - 16s 286ms/step - loss: 1.1040 - accuracy: 0.5072 - val_loss: 1.7885 - val_accuracy: 0.3225 Epoch 7/200 57/57 [==============================] - 16s 284ms/step - loss: 1.0590 - accuracy: 0.5436 - val_loss: 1.6594 - val_accuracy: 0.4125 Process Time: 0:01:55.104549 32/32 [==============================] - 1s 38ms/step - loss: 1.1651 - accuracy: 0.4310 Test acc: 0.431 32/32 [==============================] - 1s 37ms/step shape of preds: (1000, 8)
<ipython-input-7-353fbae40d9a>:17: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed. ax = plt.subplot(subplot)
32/32 [==============================] - 1s 40ms/step
Classification Report
precision recall f1-score support
0 0.00 0.00 0.00 141
1 0.40 0.45 0.43 114
2 0.57 0.55 0.56 132
3 0.36 0.99 0.53 129
4 0.75 0.11 0.19 134
5 0.41 0.43 0.42 106
6 0.46 1.00 0.63 118
7 0.00 0.00 0.00 126
accuracy 0.43 1000
macro avg 0.37 0.44 0.34 1000
weighted avg 0.37 0.43 0.34 1000
Accuracy Score: 0.431
Root Mean Square Error: 1.7190113437671084
/usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result)) /usr/local/lib/python3.10/dist-packages/sklearn/metrics/_classification.py:1344: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))