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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {
|
| 6 |
+
"id": "2uRx5yn0kbdx"
|
| 7 |
+
},
|
| 8 |
+
"source": [
|
| 9 |
+
"### Cassava Image Classification End-to-End Project with TensorFlow\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"Cassava consists of leaf images for the cassava plant depicting healthy and four (4) disease conditions; Cassava Mosaic Disease (CMD), Cassava Bacterial Blight (CBB), Cassava Greem Mite (CGM) and Cassava Brown Streak Disease (CBSD). Dataset consists of a total of 9430 labelled images. The 9430 labelled images are split into a training set (5656), a test set(1885) and a validation set (1889). The number of images per class are unbalanced with the two disease classes CMD and CBSD having 72% of the images.\n",
|
| 12 |
+
"\n",
|
| 13 |
+
"For more, you can refer to the following link: https://arxiv.org/abs/1806.02987\n"
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"cell_type": "markdown",
|
| 18 |
+
"metadata": {
|
| 19 |
+
"id": "tndfrpZ3kbdy"
|
| 20 |
+
},
|
| 21 |
+
"source": [
|
| 22 |
+
"## Check for GPU access"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": null,
|
| 28 |
+
"metadata": {
|
| 29 |
+
"id": "cTAkBSTTkbdz"
|
| 30 |
+
},
|
| 31 |
+
"outputs": [],
|
| 32 |
+
"source": [
|
| 33 |
+
"# Confirm access to a GPU\n",
|
| 34 |
+
"!nvidia-smi"
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"cell_type": "markdown",
|
| 39 |
+
"metadata": {
|
| 40 |
+
"id": "pOVHpfK0kbdz"
|
| 41 |
+
},
|
| 42 |
+
"source": [
|
| 43 |
+
"## Downloading `helper_functions.py` script\n",
|
| 44 |
+
"\n",
|
| 45 |
+
"The script contains useful helper functions we need when calculating model metrics, as well as plotting model results such as ROC/loss curves and confusion matrices."
|
| 46 |
+
]
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"cell_type": "code",
|
| 50 |
+
"execution_count": null,
|
| 51 |
+
"metadata": {
|
| 52 |
+
"id": "bkc3Rsb7kbdz"
|
| 53 |
+
},
|
| 54 |
+
"outputs": [],
|
| 55 |
+
"source": [
|
| 56 |
+
"# Download helper function script\n",
|
| 57 |
+
"!wget https://raw.githubusercontent.com/mrdbourke/tensorflow-deep-learning/refs/heads/main/extras/helper_functions.py"
|
| 58 |
+
]
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"cell_type": "code",
|
| 62 |
+
"execution_count": null,
|
| 63 |
+
"metadata": {
|
| 64 |
+
"id": "nK0t04zEkbdz"
|
| 65 |
+
},
|
| 66 |
+
"outputs": [],
|
| 67 |
+
"source": [
|
| 68 |
+
"# Import the necessary dependencies (functions) from the script\n",
|
| 69 |
+
"from helper_functions import create_tensorboard_callback, plot_loss_curves, compare_historys, make_confusion_matrix"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "markdown",
|
| 74 |
+
"metadata": {
|
| 75 |
+
"id": "YTu9DPX7kbdz"
|
| 76 |
+
},
|
| 77 |
+
"source": [
|
| 78 |
+
"## Downloading the Cassava dataset from TensorFlow Datasets\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"We're going to be downloading and loading the dataset from TensorFlow datasets: https://www.tensorflow.org/datasets/catalog/cassava"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": null,
|
| 86 |
+
"metadata": {
|
| 87 |
+
"id": "3MIdf6wLkbd0"
|
| 88 |
+
},
|
| 89 |
+
"outputs": [],
|
| 90 |
+
"source": [
|
| 91 |
+
"# Check if our required dataset is in TensorFlow datasets\n",
|
| 92 |
+
"import tensorflow as tf\n",
|
| 93 |
+
"import tensorflow_datasets as tfds\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"datasets = tfds.list_builders()\n",
|
| 96 |
+
"print('cassava' in datasets)"
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"cell_type": "code",
|
| 101 |
+
"execution_count": null,
|
| 102 |
+
"metadata": {
|
| 103 |
+
"id": "3Fl2H41ekbd0"
|
| 104 |
+
},
|
| 105 |
+
"outputs": [],
|
| 106 |
+
"source": [
|
| 107 |
+
"# Download/load the dataset\n",
|
| 108 |
+
"# Note: 'cassava' typically includes a train split and may include a validation/test split depending on TFDS version\n",
|
| 109 |
+
"import tensorflow_datasets as tfds\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"builder = tfds.builder('cassava')\n",
|
| 112 |
+
"available_splits = set(builder.info.splits.keys())\n",
|
| 113 |
+
"print('Available splits:', available_splits)\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"val_split = 'validation' if 'validation' in available_splits else ('test' if 'test' in available_splits else None)\n",
|
| 116 |
+
"if val_split is None:\n",
|
| 117 |
+
" # Fallback: if no val/test split exists, we'll carve a small validation set out of train later.\n",
|
| 118 |
+
" (train_data,), ds_info = tfds.load(\n",
|
| 119 |
+
" name = 'cassava',\n",
|
| 120 |
+
" split=['train'],\n",
|
| 121 |
+
" as_supervised = True,\n",
|
| 122 |
+
" with_info = True\n",
|
| 123 |
+
" )\n",
|
| 124 |
+
" test_data = None\n",
|
| 125 |
+
"else:\n",
|
| 126 |
+
" (train_data, test_data), ds_info = tfds.load(\n",
|
| 127 |
+
" name = 'cassava',\n",
|
| 128 |
+
" split = ['train', val_split],\n",
|
| 129 |
+
" as_supervised = True,\n",
|
| 130 |
+
" with_info = True\n",
|
| 131 |
+
" )\n"
|
| 132 |
+
]
|
| 133 |
+
},
|
| 134 |
+
{
|
| 135 |
+
"cell_type": "code",
|
| 136 |
+
"execution_count": null,
|
| 137 |
+
"metadata": {
|
| 138 |
+
"id": "vt63eLAsBxj8"
|
| 139 |
+
},
|
| 140 |
+
"outputs": [],
|
| 141 |
+
"source": [
|
| 142 |
+
"# If TFDS didn't provide a validation/test split, create one from the train set\n",
|
| 143 |
+
"import tensorflow as tf\n",
|
| 144 |
+
"\n",
|
| 145 |
+
"if test_data is None:\n",
|
| 146 |
+
" # Shuffle once deterministically and split 90/10\n",
|
| 147 |
+
" train_count = ds_info.splits['train'].num_examples\n",
|
| 148 |
+
" val_count = int(0.1 * train_count)\n",
|
| 149 |
+
" train_data = train_data.shuffle(buffer_size = min(10_000, train_count), seed = 42, reshuffle_each_iteration = False)\n",
|
| 150 |
+
" test_data = train_data.take(val_count)\n",
|
| 151 |
+
" train_data = train_data.skip(val_count)\n",
|
| 152 |
+
" print(f'Created validation split from train: train = {train_count - val_count}, val = {val_count}')\n"
|
| 153 |
+
]
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"cell_type": "markdown",
|
| 157 |
+
"metadata": {
|
| 158 |
+
"id": "GtkMti1dkbd0"
|
| 159 |
+
},
|
| 160 |
+
"source": [
|
| 161 |
+
"## Visualising samples from the dataset\n",
|
| 162 |
+
"\n",
|
| 163 |
+
"**Note:** When doing ML/DL experiments, it's important to familiarise yourself with the samples within the dataset; that way, you'll have an intuition of how your experiments should be laid out, and what types of prprocessing and model selection will be required. Most importantly, view **random** samples since randomness makes it powerful when visualing each feature/characteristic of an example.\n",
|
| 164 |
+
"\n",
|
| 165 |
+
"The explorer data's motto: visualise, visualise, visualise!"
|
| 166 |
+
]
|
| 167 |
+
},
|
| 168 |
+
{
|
| 169 |
+
"cell_type": "code",
|
| 170 |
+
"execution_count": null,
|
| 171 |
+
"metadata": {
|
| 172 |
+
"id": "bM7-9wFWkbd0"
|
| 173 |
+
},
|
| 174 |
+
"outputs": [],
|
| 175 |
+
"source": [
|
| 176 |
+
"# Show samples from the dataset\n",
|
| 177 |
+
"tfds.show_examples(train_data, ds_info)"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"cell_type": "markdown",
|
| 182 |
+
"metadata": {
|
| 183 |
+
"id": "BYDBTgcBkbd0"
|
| 184 |
+
},
|
| 185 |
+
"source": [
|
| 186 |
+
"## Create efficient data preprocessing functions & pipelines for our modelling experiments\n",
|
| 187 |
+
"\n",
|
| 188 |
+
"We've now viewed some random images of each label, let's start building some efficient data preprocessing functions and pipelines to make our experiments run computationally fast and efficient as much as possible: https://www.tensorflow.org/guide/data_performance"
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"cell_type": "code",
|
| 193 |
+
"execution_count": null,
|
| 194 |
+
"metadata": {
|
| 195 |
+
"id": "bAV_NsMRkbd0"
|
| 196 |
+
},
|
| 197 |
+
"outputs": [],
|
| 198 |
+
"source": [
|
| 199 |
+
"from tensorflow.keras import layers\n",
|
| 200 |
+
"\n",
|
| 201 |
+
"# Define data augmentation layer\n",
|
| 202 |
+
"data_augmentation = tf.keras.Sequential([\n",
|
| 203 |
+
" layers.RandomFlip('horizontal'),\n",
|
| 204 |
+
" layers.RandomRotation(0.1),\n",
|
| 205 |
+
" layers.RandomZoom(0.1),\n",
|
| 206 |
+
"], name = 'data_augmentation')"
|
| 207 |
+
]
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"cell_type": "code",
|
| 211 |
+
"execution_count": null,
|
| 212 |
+
"metadata": {
|
| 213 |
+
"id": "6NmJD5xdkbd0"
|
| 214 |
+
},
|
| 215 |
+
"outputs": [],
|
| 216 |
+
"source": [
|
| 217 |
+
"# Create preprocessing function for our data\n",
|
| 218 |
+
"def preprocess_image(image, label, image_size = 224):\n",
|
| 219 |
+
" image = tf.image.resize(image, [image_size, image_size]) # Reshape target image to 224x224\n",
|
| 220 |
+
" image = tf.cast(image, dtype = tf.float32)\n",
|
| 221 |
+
" image = data_augmentation(image) # Pass image through data augmentation layer\n",
|
| 222 |
+
" return image, label"
|
| 223 |
+
]
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"cell_type": "code",
|
| 227 |
+
"execution_count": null,
|
| 228 |
+
"metadata": {
|
| 229 |
+
"id": "q-YaqG8ykbd0"
|
| 230 |
+
},
|
| 231 |
+
"outputs": [],
|
| 232 |
+
"source": [
|
| 233 |
+
"# Compare between original and transformed image\n",
|
| 234 |
+
"\n",
|
| 235 |
+
"# Get a sample and label from dataset\n",
|
| 236 |
+
"for image, label in train_data.take(1):\n",
|
| 237 |
+
" transformed_image = preprocess_image(image = image, label = label)\n",
|
| 238 |
+
"\n",
|
| 239 |
+
"# Get the transformed image and label from the tuple (the tuple comes from the preprocessing function above)\n",
|
| 240 |
+
"preprocessed_image, transformed_label = transformed_image\n",
|
| 241 |
+
"\n",
|
| 242 |
+
"# Print out image before and after prepcrocessing & shapes\n",
|
| 243 |
+
"print(f'Original image before preprocessing: \\n{image[:0]}')\n",
|
| 244 |
+
"print(f'Original image shape: \\n{image.shape}')\n",
|
| 245 |
+
"print(f'Original image datatype: {image.dtype}')\n",
|
| 246 |
+
"print(f'Preprocessed image: \\n{preprocessed_image[:0]}')\n",
|
| 247 |
+
"print(f'Preprocessed image shape: \\n{preprocessed_image.shape}')\n",
|
| 248 |
+
"print(f'Preprocessed image datatype: \\n{preprocessed_image.dtype}')"
|
| 249 |
+
]
|
| 250 |
+
},
|
| 251 |
+
{
|
| 252 |
+
"cell_type": "markdown",
|
| 253 |
+
"metadata": {
|
| 254 |
+
"id": "gLIprEo2kbd0"
|
| 255 |
+
},
|
| 256 |
+
"source": [
|
| 257 |
+
"## Converting our datasets to prefected datasets and batched for faster computing"
|
| 258 |
+
]
|
| 259 |
+
},
|
| 260 |
+
{
|
| 261 |
+
"cell_type": "code",
|
| 262 |
+
"execution_count": null,
|
| 263 |
+
"metadata": {
|
| 264 |
+
"id": "BU9Cm66nkbd1"
|
| 265 |
+
},
|
| 266 |
+
"outputs": [],
|
| 267 |
+
"source": [
|
| 268 |
+
"# Map preprocessing function, shuffle and parallelise it to training data\n",
|
| 269 |
+
"train_data = train_data.map(map_func = preprocess_image, num_parallel_calls = tf.data.AUTOTUNE)\n",
|
| 270 |
+
"train_data = train_data.shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE)\n",
|
| 271 |
+
"\n",
|
| 272 |
+
"# Validation/test pipeline\n",
|
| 273 |
+
"test_data = test_data.map(map_func = preprocess_image, num_parallel_calls = tf.data.AUTOTUNE)\n",
|
| 274 |
+
"test_data = test_data.batch(32).prefetch(tf.data.AUTOTUNE)"
|
| 275 |
+
]
|
| 276 |
+
},
|
| 277 |
+
{
|
| 278 |
+
"cell_type": "markdown",
|
| 279 |
+
"metadata": {
|
| 280 |
+
"id": "u0JqaAYGkbd1"
|
| 281 |
+
},
|
| 282 |
+
"source": [
|
| 283 |
+
"## Create a ModelCheckpoint callback to save our models' weights during training"
|
| 284 |
+
]
|
| 285 |
+
},
|
| 286 |
+
{
|
| 287 |
+
"cell_type": "code",
|
| 288 |
+
"execution_count": null,
|
| 289 |
+
"metadata": {
|
| 290 |
+
"id": "6XoFi0T4kbd1"
|
| 291 |
+
},
|
| 292 |
+
"outputs": [],
|
| 293 |
+
"source": [
|
| 294 |
+
"# Create model checkpoint callback path\n",
|
| 295 |
+
"checkpoint_path = 'cassava_model_checkpoint.weights.h5'\n",
|
| 296 |
+
"model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath = checkpoint_path, save_weights_only = True, monitor = 'val_accuracy', save_best_only = True)"
|
| 297 |
+
]
|
| 298 |
+
},
|
| 299 |
+
{
|
| 300 |
+
"cell_type": "markdown",
|
| 301 |
+
"metadata": {
|
| 302 |
+
"id": "2VALNwXbkbd1"
|
| 303 |
+
},
|
| 304 |
+
"source": [
|
| 305 |
+
"## Turning on mixed precision training for faster training time\n",
|
| 306 |
+
"\n",
|
| 307 |
+
"When running an experiments with a large amount of data, it is important to turn on mixed precision training so that the training time is decreased whilst still maintaining the precision metric score: https://www.tensorflow.org/guide/mixed_precision"
|
| 308 |
+
]
|
| 309 |
+
},
|
| 310 |
+
{
|
| 311 |
+
"cell_type": "code",
|
| 312 |
+
"execution_count": null,
|
| 313 |
+
"metadata": {
|
| 314 |
+
"id": "fGss-7v5kbd1"
|
| 315 |
+
},
|
| 316 |
+
"outputs": [],
|
| 317 |
+
"source": [
|
| 318 |
+
"from tensorflow.keras import mixed_precision\n",
|
| 319 |
+
"mixed_precision.set_global_policy('mixed_float16') # Set global data policy for mixed precision\n",
|
| 320 |
+
"mixed_precision.global_policy()"
|
| 321 |
+
]
|
| 322 |
+
},
|
| 323 |
+
{
|
| 324 |
+
"cell_type": "markdown",
|
| 325 |
+
"metadata": {
|
| 326 |
+
"id": "PlpwB2Nkkbd1"
|
| 327 |
+
},
|
| 328 |
+
"source": [
|
| 329 |
+
"## Modelling Experiments\n",
|
| 330 |
+
"\n",
|
| 331 |
+
"Now we're going to be conducting a series of modelling experiments to find out which model outperforms all other models, so that we can deploy it to a mobile app or to a web application for testing custom images.\n",
|
| 332 |
+
"\n",
|
| 333 |
+
"And when doing experiments as such, it's always good to start with a baseline and use it as your benchmark to see if other models can beat it or not.\n",
|
| 334 |
+
"\n",
|
| 335 |
+
"And always experiment as much as you can so you can discover the pattern in each modelling experiment, i.e. changed in model's metrics or observing what happens when you change hyperparameters; the experiment practitioner's motto: experiment, experiment, experiment!"
|
| 336 |
+
]
|
| 337 |
+
},
|
| 338 |
+
{
|
| 339 |
+
"cell_type": "markdown",
|
| 340 |
+
"metadata": {
|
| 341 |
+
"id": "x4BXC-Njkbd1"
|
| 342 |
+
},
|
| 343 |
+
"source": [
|
| 344 |
+
"### Model 0 (baseline): ResNetV2101"
|
| 345 |
+
]
|
| 346 |
+
},
|
| 347 |
+
{
|
| 348 |
+
"cell_type": "code",
|
| 349 |
+
"execution_count": null,
|
| 350 |
+
"metadata": {
|
| 351 |
+
"id": "d5LRnwXNkbd1"
|
| 352 |
+
},
|
| 353 |
+
"outputs": [],
|
| 354 |
+
"source": [
|
| 355 |
+
"# Setup data inputs\n",
|
| 356 |
+
"input_size = (224, 224, 3)\n",
|
| 357 |
+
"\n",
|
| 358 |
+
"# Get number of classes\n",
|
| 359 |
+
"num_classes = ds_info.features['label'].num_classes\n",
|
| 360 |
+
"\n",
|
| 361 |
+
"# Create feature extractor model with Functional API\n",
|
| 362 |
+
"base_model = tf.keras.applications.ResNet101V2(include_top = False, weights = 'imagenet', input_shape = input_size)\n",
|
| 363 |
+
"base_model.trainable = False\n",
|
| 364 |
+
"\n",
|
| 365 |
+
"inputs = layers.Input(shape = (224, 224, 3), name = 'input_layer')\n",
|
| 366 |
+
"x = base_model(inputs, training = False)\n",
|
| 367 |
+
"x = layers.GlobalAveragePooling2D()(x)\n",
|
| 368 |
+
"x = layers.BatchNormalization()(x)\n",
|
| 369 |
+
"x = layers.Dense(256, activation = 'relu')(x)\n",
|
| 370 |
+
"x = layers.Dropout(0.5)(x)\n",
|
| 371 |
+
"outputs = layers.Dense(num_classes, activation = 'softmax', name = 'output_layer')(x)\n",
|
| 372 |
+
"model_0 = tf.keras.Model(inputs, outputs)\n",
|
| 373 |
+
"\n",
|
| 374 |
+
"# Conmpile model\n",
|
| 375 |
+
"model_0.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 376 |
+
" optimizer = tf.keras.optimizers.Adam(),\n",
|
| 377 |
+
" metrics = ['accuracy'])\n",
|
| 378 |
+
"\n",
|
| 379 |
+
"# Get model summary\n",
|
| 380 |
+
"model_0.summary()"
|
| 381 |
+
]
|
| 382 |
+
},
|
| 383 |
+
{
|
| 384 |
+
"cell_type": "code",
|
| 385 |
+
"execution_count": null,
|
| 386 |
+
"metadata": {
|
| 387 |
+
"id": "42wO9iUQkbd1"
|
| 388 |
+
},
|
| 389 |
+
"outputs": [],
|
| 390 |
+
"source": [
|
| 391 |
+
"# Fit\n",
|
| 392 |
+
"initial_epochs = 10\n",
|
| 393 |
+
"history_0 = model_0.fit(train_data, epochs = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_0'), model_checkpoint])"
|
| 394 |
+
]
|
| 395 |
+
},
|
| 396 |
+
{
|
| 397 |
+
"cell_type": "code",
|
| 398 |
+
"execution_count": null,
|
| 399 |
+
"metadata": {
|
| 400 |
+
"id": "Okua2DfWkbd1"
|
| 401 |
+
},
|
| 402 |
+
"outputs": [],
|
| 403 |
+
"source": [
|
| 404 |
+
"# Plot loss curves for baseline\n",
|
| 405 |
+
"plot_loss_curves(history = history_0)"
|
| 406 |
+
]
|
| 407 |
+
},
|
| 408 |
+
{
|
| 409 |
+
"cell_type": "code",
|
| 410 |
+
"execution_count": null,
|
| 411 |
+
"metadata": {
|
| 412 |
+
"id": "cJ54k5NXkbd2"
|
| 413 |
+
},
|
| 414 |
+
"outputs": [],
|
| 415 |
+
"source": [
|
| 416 |
+
"# Evaluate baseline on test data\n",
|
| 417 |
+
"model_0.evaluate(test_data)"
|
| 418 |
+
]
|
| 419 |
+
},
|
| 420 |
+
{
|
| 421 |
+
"cell_type": "code",
|
| 422 |
+
"execution_count": null,
|
| 423 |
+
"metadata": {
|
| 424 |
+
"id": "3UI1JLJpkbd2"
|
| 425 |
+
},
|
| 426 |
+
"outputs": [],
|
| 427 |
+
"source": [
|
| 428 |
+
"# Make predictions with baseline\n",
|
| 429 |
+
"model_0_preds = model_0.predict(test_data)\n",
|
| 430 |
+
"model_0_preds[:1]"
|
| 431 |
+
]
|
| 432 |
+
},
|
| 433 |
+
{
|
| 434 |
+
"cell_type": "code",
|
| 435 |
+
"execution_count": null,
|
| 436 |
+
"metadata": {
|
| 437 |
+
"id": "6Jt9M8O0kbd2"
|
| 438 |
+
},
|
| 439 |
+
"outputs": [],
|
| 440 |
+
"source": [
|
| 441 |
+
"# Convert baseline preds to labels\n",
|
| 442 |
+
"labels = model_0_preds.argmax(axis = 1)\n",
|
| 443 |
+
"labels[:10]"
|
| 444 |
+
]
|
| 445 |
+
},
|
| 446 |
+
{
|
| 447 |
+
"cell_type": "code",
|
| 448 |
+
"execution_count": null,
|
| 449 |
+
"metadata": {
|
| 450 |
+
"id": "TGv2Wrqqkbd2"
|
| 451 |
+
},
|
| 452 |
+
"outputs": [],
|
| 453 |
+
"source": [
|
| 454 |
+
"import numpy as np\n",
|
| 455 |
+
"\n",
|
| 456 |
+
"# Get true labels from the test dataset\n",
|
| 457 |
+
"true_labels = []\n",
|
| 458 |
+
"for images, labels_batch in test_data.unbatch():\n",
|
| 459 |
+
" true_labels.append(labels_batch.numpy())\n",
|
| 460 |
+
"\n",
|
| 461 |
+
"true_labels = np.array(true_labels)\n",
|
| 462 |
+
"\n",
|
| 463 |
+
"# Plot model 0's confusion matrix\n",
|
| 464 |
+
"make_confusion_matrix(y_true = true_labels, y_pred = labels)"
|
| 465 |
+
]
|
| 466 |
+
},
|
| 467 |
+
{
|
| 468 |
+
"cell_type": "markdown",
|
| 469 |
+
"metadata": {
|
| 470 |
+
"id": "M97JEihCkbd2"
|
| 471 |
+
},
|
| 472 |
+
"source": [
|
| 473 |
+
"### Fine-tuning baseline model"
|
| 474 |
+
]
|
| 475 |
+
},
|
| 476 |
+
{
|
| 477 |
+
"cell_type": "code",
|
| 478 |
+
"execution_count": null,
|
| 479 |
+
"metadata": {
|
| 480 |
+
"id": "gRJb0_iikbd2"
|
| 481 |
+
},
|
| 482 |
+
"outputs": [],
|
| 483 |
+
"source": [
|
| 484 |
+
"# Unfreeze last 20 layers of feature-extractor model\n",
|
| 485 |
+
"base_model.trainable = True\n",
|
| 486 |
+
"for layer in base_model.layers[:-20]:\n",
|
| 487 |
+
" layer.trainable = False\n",
|
| 488 |
+
"\n",
|
| 489 |
+
"# Freeze all BatchNormalization layers within the last 20 layers\n",
|
| 490 |
+
"for layer in base_model.layers[-20:]:\n",
|
| 491 |
+
" if isinstance(layer, layers.BatchNormalization):\n",
|
| 492 |
+
" layer.trainable = False\n",
|
| 493 |
+
"\n",
|
| 494 |
+
"# Compile\n",
|
| 495 |
+
"model_0.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 496 |
+
" optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n",
|
| 497 |
+
" metrics = ['accuracy'])\n",
|
| 498 |
+
"\n",
|
| 499 |
+
"fine_tune_epochs = 10\n",
|
| 500 |
+
"total_epochs = initial_epochs + fine_tune_epochs\n",
|
| 501 |
+
"\n",
|
| 502 |
+
"# Fit fine-tuned model\n",
|
| 503 |
+
"history_0_fine_tune = model_0.fit(train_data, epochs = total_epochs, initial_epoch = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_0_fine-tuned'), model_checkpoint])"
|
| 504 |
+
]
|
| 505 |
+
},
|
| 506 |
+
{
|
| 507 |
+
"cell_type": "code",
|
| 508 |
+
"execution_count": null,
|
| 509 |
+
"metadata": {
|
| 510 |
+
"id": "HrmjjhXRkbd2"
|
| 511 |
+
},
|
| 512 |
+
"outputs": [],
|
| 513 |
+
"source": [
|
| 514 |
+
"# Plot loss curves for fine-tuned baseline\n",
|
| 515 |
+
"plot_loss_curves(history = history_0_fine_tune)"
|
| 516 |
+
]
|
| 517 |
+
},
|
| 518 |
+
{
|
| 519 |
+
"cell_type": "code",
|
| 520 |
+
"execution_count": null,
|
| 521 |
+
"metadata": {
|
| 522 |
+
"id": "5hQE43VHkbd2"
|
| 523 |
+
},
|
| 524 |
+
"outputs": [],
|
| 525 |
+
"source": [
|
| 526 |
+
"# Compare original and fine-tuned model historys for baseline\n",
|
| 527 |
+
"compare_historys(original_history = history_0, new_history = history_0_fine_tune, initial_epochs = initial_epochs)"
|
| 528 |
+
]
|
| 529 |
+
},
|
| 530 |
+
{
|
| 531 |
+
"cell_type": "markdown",
|
| 532 |
+
"metadata": {
|
| 533 |
+
"id": "dyJxv1YGkbd2"
|
| 534 |
+
},
|
| 535 |
+
"source": [
|
| 536 |
+
"### Model 1: EfficientNetV2B0"
|
| 537 |
+
]
|
| 538 |
+
},
|
| 539 |
+
{
|
| 540 |
+
"cell_type": "code",
|
| 541 |
+
"execution_count": null,
|
| 542 |
+
"metadata": {
|
| 543 |
+
"id": "tAdBe10Dkbd2"
|
| 544 |
+
},
|
| 545 |
+
"outputs": [],
|
| 546 |
+
"source": [
|
| 547 |
+
"# Setup data inputs\n",
|
| 548 |
+
"input_size = (224, 224, 3)\n",
|
| 549 |
+
"\n",
|
| 550 |
+
"# Get number of classes\n",
|
| 551 |
+
"num_classes = ds_info.features['label'].num_classes\n",
|
| 552 |
+
"\n",
|
| 553 |
+
"# Create feature extractor model with Functional API\n",
|
| 554 |
+
"base_model = tf.keras.applications.EfficientNetV2B0(include_top = False, weights = 'imagenet', input_shape = input_size)\n",
|
| 555 |
+
"base_model.trainable = False\n",
|
| 556 |
+
"\n",
|
| 557 |
+
"inputs = layers.Input(shape = (224, 224, 3), name = 'input_layer')\n",
|
| 558 |
+
"x = base_model(inputs, training = False)\n",
|
| 559 |
+
"x = layers.GlobalAveragePooling2D()(x)\n",
|
| 560 |
+
"x = layers.BatchNormalization()(x)\n",
|
| 561 |
+
"x = layers.Dense(256, activation = 'relu')(x)\n",
|
| 562 |
+
"x = layers.Dropout(0.5)(x)\n",
|
| 563 |
+
"outputs = layers.Dense(num_classes, activation = 'softmax', name = 'output_layer')(x)\n",
|
| 564 |
+
"model_1 = tf.keras.Model(inputs, outputs)\n",
|
| 565 |
+
"\n",
|
| 566 |
+
"# Conmpile model\n",
|
| 567 |
+
"model_1.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 568 |
+
" optimizer = tf.keras.optimizers.Adam(),\n",
|
| 569 |
+
" metrics = ['accuracy'])\n",
|
| 570 |
+
"\n",
|
| 571 |
+
"# Get model summary\n",
|
| 572 |
+
"model_1.summary()"
|
| 573 |
+
]
|
| 574 |
+
},
|
| 575 |
+
{
|
| 576 |
+
"cell_type": "code",
|
| 577 |
+
"execution_count": null,
|
| 578 |
+
"metadata": {
|
| 579 |
+
"id": "0Z2yJVJckbd2"
|
| 580 |
+
},
|
| 581 |
+
"outputs": [],
|
| 582 |
+
"source": [
|
| 583 |
+
"# Fit\n",
|
| 584 |
+
"initial_epochs = 10\n",
|
| 585 |
+
"history_1 = model_1.fit(train_data, epochs = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_1'), model_checkpoint])"
|
| 586 |
+
]
|
| 587 |
+
},
|
| 588 |
+
{
|
| 589 |
+
"cell_type": "code",
|
| 590 |
+
"execution_count": null,
|
| 591 |
+
"metadata": {
|
| 592 |
+
"id": "qj4Pd0oLkbd2"
|
| 593 |
+
},
|
| 594 |
+
"outputs": [],
|
| 595 |
+
"source": [
|
| 596 |
+
"# Plot loss curves for model 1\n",
|
| 597 |
+
"plot_loss_curves(history = history_1)"
|
| 598 |
+
]
|
| 599 |
+
},
|
| 600 |
+
{
|
| 601 |
+
"cell_type": "code",
|
| 602 |
+
"execution_count": null,
|
| 603 |
+
"metadata": {
|
| 604 |
+
"id": "4UpoL6Y8kbd2"
|
| 605 |
+
},
|
| 606 |
+
"outputs": [],
|
| 607 |
+
"source": [
|
| 608 |
+
"# Evaluate model on test data\n",
|
| 609 |
+
"model_1.evaluate(test_data)"
|
| 610 |
+
]
|
| 611 |
+
},
|
| 612 |
+
{
|
| 613 |
+
"cell_type": "code",
|
| 614 |
+
"execution_count": null,
|
| 615 |
+
"metadata": {
|
| 616 |
+
"id": "xnFqaBA8kbd3"
|
| 617 |
+
},
|
| 618 |
+
"outputs": [],
|
| 619 |
+
"source": [
|
| 620 |
+
"# Make predictions with model 1\n",
|
| 621 |
+
"model_1_preds = model_1.predict(test_data)\n",
|
| 622 |
+
"model_1_preds[:1]"
|
| 623 |
+
]
|
| 624 |
+
},
|
| 625 |
+
{
|
| 626 |
+
"cell_type": "code",
|
| 627 |
+
"execution_count": null,
|
| 628 |
+
"metadata": {
|
| 629 |
+
"id": "cxKfYeelkbd3"
|
| 630 |
+
},
|
| 631 |
+
"outputs": [],
|
| 632 |
+
"source": [
|
| 633 |
+
"# Convert model 1 preds to labels\n",
|
| 634 |
+
"labels = model_1_preds.argmax(axis = 1)\n",
|
| 635 |
+
"labels[:10]"
|
| 636 |
+
]
|
| 637 |
+
},
|
| 638 |
+
{
|
| 639 |
+
"cell_type": "code",
|
| 640 |
+
"execution_count": null,
|
| 641 |
+
"metadata": {
|
| 642 |
+
"id": "f9G1ko6Akbd3"
|
| 643 |
+
},
|
| 644 |
+
"outputs": [],
|
| 645 |
+
"source": [
|
| 646 |
+
"# Plot model 1's confusion matrix\n",
|
| 647 |
+
"make_confusion_matrix(y_true = true_labels, y_pred = labels)"
|
| 648 |
+
]
|
| 649 |
+
},
|
| 650 |
+
{
|
| 651 |
+
"cell_type": "markdown",
|
| 652 |
+
"metadata": {
|
| 653 |
+
"id": "Mcst3U-lkbd3"
|
| 654 |
+
},
|
| 655 |
+
"source": [
|
| 656 |
+
"### Fine-tuning model 1"
|
| 657 |
+
]
|
| 658 |
+
},
|
| 659 |
+
{
|
| 660 |
+
"cell_type": "code",
|
| 661 |
+
"execution_count": null,
|
| 662 |
+
"metadata": {
|
| 663 |
+
"id": "iEh0vf53kbd3"
|
| 664 |
+
},
|
| 665 |
+
"outputs": [],
|
| 666 |
+
"source": [
|
| 667 |
+
"# Unfreeze last 20 layers of feature-extractor model\n",
|
| 668 |
+
"base_model.trainable = True\n",
|
| 669 |
+
"for layer in base_model.layers[:-20]:\n",
|
| 670 |
+
" layer.trainable = False\n",
|
| 671 |
+
"\n",
|
| 672 |
+
"# Freeze all BatchNormalization layers within the last 20 layers\n",
|
| 673 |
+
"for layer in base_model.layers[-20:]:\n",
|
| 674 |
+
" if isinstance(layer, layers.BatchNormalization):\n",
|
| 675 |
+
" layer.trainable = False\n",
|
| 676 |
+
"\n",
|
| 677 |
+
"# Compile\n",
|
| 678 |
+
"model_1.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 679 |
+
" optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n",
|
| 680 |
+
" metrics = ['accuracy'])\n",
|
| 681 |
+
"\n",
|
| 682 |
+
"fine_tune_epochs = 10\n",
|
| 683 |
+
"total_epochs = initial_epochs + fine_tune_epochs\n",
|
| 684 |
+
"\n",
|
| 685 |
+
"# Fit fine-tuned model\n",
|
| 686 |
+
"history_1_fine_tune = model_1.fit(train_data, epochs = total_epochs, initial_epoch = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_1_fine-tuned'), model_checkpoint])"
|
| 687 |
+
]
|
| 688 |
+
},
|
| 689 |
+
{
|
| 690 |
+
"cell_type": "code",
|
| 691 |
+
"execution_count": null,
|
| 692 |
+
"metadata": {
|
| 693 |
+
"id": "eJBJOZvUkbd3"
|
| 694 |
+
},
|
| 695 |
+
"outputs": [],
|
| 696 |
+
"source": [
|
| 697 |
+
"# Plot fine-tuned model 1 loss curves\n",
|
| 698 |
+
"plot_loss_curves(history = history_1_fine_tune)"
|
| 699 |
+
]
|
| 700 |
+
},
|
| 701 |
+
{
|
| 702 |
+
"cell_type": "code",
|
| 703 |
+
"execution_count": null,
|
| 704 |
+
"metadata": {
|
| 705 |
+
"id": "0hODyoiZkbd4"
|
| 706 |
+
},
|
| 707 |
+
"outputs": [],
|
| 708 |
+
"source": [
|
| 709 |
+
"# Compare original and fine-tuned model 1 historys\n",
|
| 710 |
+
"compare_historys(original_history = history_1, new_history = history_1_fine_tune, initial_epochs = initial_epochs)"
|
| 711 |
+
]
|
| 712 |
+
},
|
| 713 |
+
{
|
| 714 |
+
"cell_type": "markdown",
|
| 715 |
+
"metadata": {
|
| 716 |
+
"id": "Gvk4mhzekbd4"
|
| 717 |
+
},
|
| 718 |
+
"source": [
|
| 719 |
+
"### Model 2: EfficientNetV2B1"
|
| 720 |
+
]
|
| 721 |
+
},
|
| 722 |
+
{
|
| 723 |
+
"cell_type": "code",
|
| 724 |
+
"execution_count": null,
|
| 725 |
+
"metadata": {
|
| 726 |
+
"id": "9iwiHgz1kbd4"
|
| 727 |
+
},
|
| 728 |
+
"outputs": [],
|
| 729 |
+
"source": [
|
| 730 |
+
"# Setup data inputs\n",
|
| 731 |
+
"input_size = (224, 224, 3)\n",
|
| 732 |
+
"\n",
|
| 733 |
+
"# Get number of classes\n",
|
| 734 |
+
"num_classes = ds_info.features['label'].num_classes\n",
|
| 735 |
+
"\n",
|
| 736 |
+
"# Create feature extractor model with Functional API\n",
|
| 737 |
+
"base_model = tf.keras.applications.EfficientNetV2B1(include_top = False, weights = 'imagenet', input_shape = input_size)\n",
|
| 738 |
+
"base_model.trainable = False\n",
|
| 739 |
+
"\n",
|
| 740 |
+
"inputs = layers.Input(shape = (224, 224, 3), name = 'input_layer')\n",
|
| 741 |
+
"x = base_model(inputs, training = False)\n",
|
| 742 |
+
"x = layers.GlobalAveragePooling2D()(x)\n",
|
| 743 |
+
"x = layers.BatchNormalization()(x)\n",
|
| 744 |
+
"x = layers.Dense(256, activation = 'relu')(x)\n",
|
| 745 |
+
"x = layers.Dropout(0.5)(x)\n",
|
| 746 |
+
"outputs = layers.Dense(num_classes, activation = 'softmax', name = 'output_layer')(x)\n",
|
| 747 |
+
"model_2 = tf.keras.Model(inputs, outputs)\n",
|
| 748 |
+
"\n",
|
| 749 |
+
"# Conmpile model\n",
|
| 750 |
+
"model_2.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 751 |
+
" optimizer = tf.keras.optimizers.Adam(),\n",
|
| 752 |
+
" metrics = ['accuracy'])\n",
|
| 753 |
+
"\n",
|
| 754 |
+
"# Get model summary\n",
|
| 755 |
+
"model_2.summary()"
|
| 756 |
+
]
|
| 757 |
+
},
|
| 758 |
+
{
|
| 759 |
+
"cell_type": "code",
|
| 760 |
+
"execution_count": null,
|
| 761 |
+
"metadata": {
|
| 762 |
+
"id": "8y4pCNCCkbd4"
|
| 763 |
+
},
|
| 764 |
+
"outputs": [],
|
| 765 |
+
"source": [
|
| 766 |
+
"# Fit\n",
|
| 767 |
+
"initial_epochs = 10\n",
|
| 768 |
+
"history_2 = model_2.fit(train_data, epochs = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_2'), model_checkpoint])"
|
| 769 |
+
]
|
| 770 |
+
},
|
| 771 |
+
{
|
| 772 |
+
"cell_type": "code",
|
| 773 |
+
"execution_count": null,
|
| 774 |
+
"metadata": {
|
| 775 |
+
"id": "KefsxTNqkbd4"
|
| 776 |
+
},
|
| 777 |
+
"outputs": [],
|
| 778 |
+
"source": [
|
| 779 |
+
"# Plot loss curves for model 2\n",
|
| 780 |
+
"plot_loss_curves(history = history_2)"
|
| 781 |
+
]
|
| 782 |
+
},
|
| 783 |
+
{
|
| 784 |
+
"cell_type": "code",
|
| 785 |
+
"execution_count": null,
|
| 786 |
+
"metadata": {
|
| 787 |
+
"id": "B08qNMRckbd4"
|
| 788 |
+
},
|
| 789 |
+
"outputs": [],
|
| 790 |
+
"source": [
|
| 791 |
+
"# Evaluate model on test data\n",
|
| 792 |
+
"model_2.evaluate(test_data)"
|
| 793 |
+
]
|
| 794 |
+
},
|
| 795 |
+
{
|
| 796 |
+
"cell_type": "code",
|
| 797 |
+
"execution_count": null,
|
| 798 |
+
"metadata": {
|
| 799 |
+
"id": "N-aoK3H3kbd4"
|
| 800 |
+
},
|
| 801 |
+
"outputs": [],
|
| 802 |
+
"source": [
|
| 803 |
+
"# Make predictions with model 2\n",
|
| 804 |
+
"model_2_preds = model_2.predict(test_data)\n",
|
| 805 |
+
"model_2_preds[:1]"
|
| 806 |
+
]
|
| 807 |
+
},
|
| 808 |
+
{
|
| 809 |
+
"cell_type": "code",
|
| 810 |
+
"execution_count": null,
|
| 811 |
+
"metadata": {
|
| 812 |
+
"id": "yDmyJO0Tkbd4"
|
| 813 |
+
},
|
| 814 |
+
"outputs": [],
|
| 815 |
+
"source": [
|
| 816 |
+
"# Convert model 2 preds to labels\n",
|
| 817 |
+
"labels = model_2_preds.argmax(axis = 1)\n",
|
| 818 |
+
"labels[:10]"
|
| 819 |
+
]
|
| 820 |
+
},
|
| 821 |
+
{
|
| 822 |
+
"cell_type": "code",
|
| 823 |
+
"execution_count": null,
|
| 824 |
+
"metadata": {
|
| 825 |
+
"id": "sD9_iccnkbd4"
|
| 826 |
+
},
|
| 827 |
+
"outputs": [],
|
| 828 |
+
"source": [
|
| 829 |
+
"# Plot model 2's confusion matrix\n",
|
| 830 |
+
"make_confusion_matrix(y_true = true_labels, y_pred = labels)"
|
| 831 |
+
]
|
| 832 |
+
},
|
| 833 |
+
{
|
| 834 |
+
"cell_type": "markdown",
|
| 835 |
+
"metadata": {
|
| 836 |
+
"id": "dCDuVzWKkbd5"
|
| 837 |
+
},
|
| 838 |
+
"source": [
|
| 839 |
+
"### Fine-tuning model 2"
|
| 840 |
+
]
|
| 841 |
+
},
|
| 842 |
+
{
|
| 843 |
+
"cell_type": "code",
|
| 844 |
+
"execution_count": null,
|
| 845 |
+
"metadata": {
|
| 846 |
+
"id": "HakVagY5kbd5"
|
| 847 |
+
},
|
| 848 |
+
"outputs": [],
|
| 849 |
+
"source": [
|
| 850 |
+
"# Unfreeze last 20 layers of feature-extractor model\n",
|
| 851 |
+
"base_model.trainable = True\n",
|
| 852 |
+
"for layer in base_model.layers[:-20]:\n",
|
| 853 |
+
" layer.trainable = False\n",
|
| 854 |
+
"\n",
|
| 855 |
+
"# Freeze all BatchNormalization layers within the last 20 layers\n",
|
| 856 |
+
"for layer in base_model.layers[-20:]:\n",
|
| 857 |
+
" if isinstance(layer, layers.BatchNormalization):\n",
|
| 858 |
+
" layer.trainable = False\n",
|
| 859 |
+
"\n",
|
| 860 |
+
"# Compile\n",
|
| 861 |
+
"model_2.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 862 |
+
" optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n",
|
| 863 |
+
" metrics = ['accuracy'])\n",
|
| 864 |
+
"\n",
|
| 865 |
+
"fine_tune_epochs = 10\n",
|
| 866 |
+
"total_epochs = initial_epochs + fine_tune_epochs\n",
|
| 867 |
+
"\n",
|
| 868 |
+
"# Fit fine-tuned model\n",
|
| 869 |
+
"history_2_fine_tune = model_2.fit(train_data, epochs = total_epochs, initial_epoch = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_2_fine-tuned'), model_checkpoint])"
|
| 870 |
+
]
|
| 871 |
+
},
|
| 872 |
+
{
|
| 873 |
+
"cell_type": "code",
|
| 874 |
+
"execution_count": null,
|
| 875 |
+
"metadata": {
|
| 876 |
+
"id": "lDOZcY6skbd5"
|
| 877 |
+
},
|
| 878 |
+
"outputs": [],
|
| 879 |
+
"source": [
|
| 880 |
+
"# Plot fine-tuned model 1 loss curves\n",
|
| 881 |
+
"plot_loss_curves(history = history_2_fine_tune)"
|
| 882 |
+
]
|
| 883 |
+
},
|
| 884 |
+
{
|
| 885 |
+
"cell_type": "code",
|
| 886 |
+
"execution_count": null,
|
| 887 |
+
"metadata": {
|
| 888 |
+
"id": "BYP6EMLJkbd5"
|
| 889 |
+
},
|
| 890 |
+
"outputs": [],
|
| 891 |
+
"source": [
|
| 892 |
+
"# Compare orignal and fine-tuned historys for model 2\n",
|
| 893 |
+
"compare_historys(original_history = history_2, new_history = history_2_fine_tune, initial_epochs = initial_epochs)"
|
| 894 |
+
]
|
| 895 |
+
},
|
| 896 |
+
{
|
| 897 |
+
"cell_type": "markdown",
|
| 898 |
+
"metadata": {
|
| 899 |
+
"id": "h3wyBXcxkbd5"
|
| 900 |
+
},
|
| 901 |
+
"source": [
|
| 902 |
+
"### Model 3: ResNet152V2"
|
| 903 |
+
]
|
| 904 |
+
},
|
| 905 |
+
{
|
| 906 |
+
"cell_type": "code",
|
| 907 |
+
"execution_count": null,
|
| 908 |
+
"metadata": {
|
| 909 |
+
"id": "GzqGh8vDkbd5"
|
| 910 |
+
},
|
| 911 |
+
"outputs": [],
|
| 912 |
+
"source": [
|
| 913 |
+
"# Setup data inputs\n",
|
| 914 |
+
"input_size = (224, 224, 3)\n",
|
| 915 |
+
"\n",
|
| 916 |
+
"# Get number of classes\n",
|
| 917 |
+
"num_classes = ds_info.features['label'].num_classes\n",
|
| 918 |
+
"\n",
|
| 919 |
+
"# Create feature extractor model with Functional API\n",
|
| 920 |
+
"base_model = tf.keras.applications.ResNet152V2(include_top = False, weights = 'imagenet', input_shape = input_size)\n",
|
| 921 |
+
"base_model.trainable = False\n",
|
| 922 |
+
"\n",
|
| 923 |
+
"inputs = layers.Input(shape = (224, 224, 3), name = 'input_layer')\n",
|
| 924 |
+
"x = base_model(inputs, training = False)\n",
|
| 925 |
+
"x = layers.GlobalAveragePooling2D()(x)\n",
|
| 926 |
+
"x = layers.BatchNormalization()(x)\n",
|
| 927 |
+
"x = layers.Dense(256, activation = 'relu')(x)\n",
|
| 928 |
+
"x = layers.Dropout(0.5)(x)\n",
|
| 929 |
+
"outputs = layers.Dense(num_classes, activation = 'softmax', name = 'output_layer')(x)\n",
|
| 930 |
+
"model_3 = tf.keras.Model(inputs, outputs)\n",
|
| 931 |
+
"\n",
|
| 932 |
+
"# Conmpile model\n",
|
| 933 |
+
"model_3.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 934 |
+
" optimizer = tf.keras.optimizers.Adam(),\n",
|
| 935 |
+
" metrics = ['accuracy'])\n",
|
| 936 |
+
"\n",
|
| 937 |
+
"# Get model summary\n",
|
| 938 |
+
"model_3.summary()"
|
| 939 |
+
]
|
| 940 |
+
},
|
| 941 |
+
{
|
| 942 |
+
"cell_type": "code",
|
| 943 |
+
"execution_count": null,
|
| 944 |
+
"metadata": {
|
| 945 |
+
"id": "2vu1Y_q7kbd5"
|
| 946 |
+
},
|
| 947 |
+
"outputs": [],
|
| 948 |
+
"source": [
|
| 949 |
+
"# Fit\n",
|
| 950 |
+
"initial_epochs = 10\n",
|
| 951 |
+
"history_3 = model_3.fit(train_data, epochs = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_3'), model_checkpoint])"
|
| 952 |
+
]
|
| 953 |
+
},
|
| 954 |
+
{
|
| 955 |
+
"cell_type": "code",
|
| 956 |
+
"execution_count": null,
|
| 957 |
+
"metadata": {
|
| 958 |
+
"id": "-Y_FV-Vkkbd5"
|
| 959 |
+
},
|
| 960 |
+
"outputs": [],
|
| 961 |
+
"source": [
|
| 962 |
+
"# Plot loss curves for model 3\n",
|
| 963 |
+
"plot_loss_curves(history = history_3)"
|
| 964 |
+
]
|
| 965 |
+
},
|
| 966 |
+
{
|
| 967 |
+
"cell_type": "code",
|
| 968 |
+
"execution_count": null,
|
| 969 |
+
"metadata": {
|
| 970 |
+
"id": "VQ4cLzbekbd5"
|
| 971 |
+
},
|
| 972 |
+
"outputs": [],
|
| 973 |
+
"source": [
|
| 974 |
+
"# Evaluate model\n",
|
| 975 |
+
"model_3.evaluate(test_data)"
|
| 976 |
+
]
|
| 977 |
+
},
|
| 978 |
+
{
|
| 979 |
+
"cell_type": "code",
|
| 980 |
+
"execution_count": null,
|
| 981 |
+
"metadata": {
|
| 982 |
+
"id": "deMeA7lekbd6"
|
| 983 |
+
},
|
| 984 |
+
"outputs": [],
|
| 985 |
+
"source": [
|
| 986 |
+
"# Make predictions with model 3\n",
|
| 987 |
+
"model_3_preds = model_3.predict(test_data)\n",
|
| 988 |
+
"model_3_preds[:1]"
|
| 989 |
+
]
|
| 990 |
+
},
|
| 991 |
+
{
|
| 992 |
+
"cell_type": "code",
|
| 993 |
+
"execution_count": null,
|
| 994 |
+
"metadata": {
|
| 995 |
+
"id": "C5mHFdnGkbd6"
|
| 996 |
+
},
|
| 997 |
+
"outputs": [],
|
| 998 |
+
"source": [
|
| 999 |
+
"# Convert predictions to labels for model 3\n",
|
| 1000 |
+
"labels = model_3_preds.argmax(axis = 1)\n",
|
| 1001 |
+
"labels[:10]"
|
| 1002 |
+
]
|
| 1003 |
+
},
|
| 1004 |
+
{
|
| 1005 |
+
"cell_type": "code",
|
| 1006 |
+
"execution_count": null,
|
| 1007 |
+
"metadata": {
|
| 1008 |
+
"id": "pManlZfOkbd6"
|
| 1009 |
+
},
|
| 1010 |
+
"outputs": [],
|
| 1011 |
+
"source": [
|
| 1012 |
+
"# Plot model 3's confusion matrix\n",
|
| 1013 |
+
"make_confusion_matrix(y_true = true_labels, y_pred = labels)"
|
| 1014 |
+
]
|
| 1015 |
+
},
|
| 1016 |
+
{
|
| 1017 |
+
"cell_type": "markdown",
|
| 1018 |
+
"metadata": {
|
| 1019 |
+
"id": "pvrH-dh2kbd6"
|
| 1020 |
+
},
|
| 1021 |
+
"source": [
|
| 1022 |
+
"### Fine-tuning model 3"
|
| 1023 |
+
]
|
| 1024 |
+
},
|
| 1025 |
+
{
|
| 1026 |
+
"cell_type": "code",
|
| 1027 |
+
"execution_count": null,
|
| 1028 |
+
"metadata": {
|
| 1029 |
+
"id": "nR83VJW4kbd6"
|
| 1030 |
+
},
|
| 1031 |
+
"outputs": [],
|
| 1032 |
+
"source": [
|
| 1033 |
+
"# Unfreeze last 20 layers of feature-extractor model\n",
|
| 1034 |
+
"base_model.trainable = True\n",
|
| 1035 |
+
"for layer in base_model.layers[:-20]:\n",
|
| 1036 |
+
" layer.trainable = False\n",
|
| 1037 |
+
"\n",
|
| 1038 |
+
"# Freeze all BatchNormalization layers within the last 20 layers\n",
|
| 1039 |
+
"for layer in base_model.layers[-20:]:\n",
|
| 1040 |
+
" if isinstance(layer, layers.BatchNormalization):\n",
|
| 1041 |
+
" layer.trainable = False\n",
|
| 1042 |
+
"\n",
|
| 1043 |
+
"# Compile\n",
|
| 1044 |
+
"model_3.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 1045 |
+
" optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n",
|
| 1046 |
+
" metrics = ['accuracy'])\n",
|
| 1047 |
+
"\n",
|
| 1048 |
+
"fine_tune_epochs = 10\n",
|
| 1049 |
+
"total_epochs = initial_epochs + fine_tune_epochs\n",
|
| 1050 |
+
"\n",
|
| 1051 |
+
"# Fit fine-tuned model\n",
|
| 1052 |
+
"history_3_fine_tune = model_3.fit(train_data, epochs = total_epochs, initial_epoch = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_3_fine-tuned'), model_checkpoint])"
|
| 1053 |
+
]
|
| 1054 |
+
},
|
| 1055 |
+
{
|
| 1056 |
+
"cell_type": "code",
|
| 1057 |
+
"execution_count": null,
|
| 1058 |
+
"metadata": {
|
| 1059 |
+
"id": "cNtGrPaPkbd6"
|
| 1060 |
+
},
|
| 1061 |
+
"outputs": [],
|
| 1062 |
+
"source": [
|
| 1063 |
+
"# Plot fine-tuned model 3's loss curves\n",
|
| 1064 |
+
"plot_loss_curves(history = history_3_fine_tune)"
|
| 1065 |
+
]
|
| 1066 |
+
},
|
| 1067 |
+
{
|
| 1068 |
+
"cell_type": "code",
|
| 1069 |
+
"execution_count": null,
|
| 1070 |
+
"metadata": {
|
| 1071 |
+
"id": "eCjtH99Lkbd6"
|
| 1072 |
+
},
|
| 1073 |
+
"outputs": [],
|
| 1074 |
+
"source": [
|
| 1075 |
+
"# Compare orginal and fine-tuned historys for model 3\n",
|
| 1076 |
+
"compare_historys(original_history = history_3, new_history = history_3_fine_tune, initial_epochs = initial_epochs)"
|
| 1077 |
+
]
|
| 1078 |
+
},
|
| 1079 |
+
{
|
| 1080 |
+
"cell_type": "markdown",
|
| 1081 |
+
"metadata": {
|
| 1082 |
+
"id": "fUCWcjbekbd6"
|
| 1083 |
+
},
|
| 1084 |
+
"source": [
|
| 1085 |
+
"### Model 4: Mobilenet_v3"
|
| 1086 |
+
]
|
| 1087 |
+
},
|
| 1088 |
+
{
|
| 1089 |
+
"cell_type": "code",
|
| 1090 |
+
"execution_count": null,
|
| 1091 |
+
"metadata": {
|
| 1092 |
+
"id": "ytPmygOOkbd6"
|
| 1093 |
+
},
|
| 1094 |
+
"outputs": [],
|
| 1095 |
+
"source": [
|
| 1096 |
+
"# Setup data inputs\n",
|
| 1097 |
+
"input_size = (224, 224, 3)\n",
|
| 1098 |
+
"\n",
|
| 1099 |
+
"# Get number of classes\n",
|
| 1100 |
+
"num_classes = ds_info.features['label'].num_classes\n",
|
| 1101 |
+
"\n",
|
| 1102 |
+
"# Create feature extractor model with Functional API\n",
|
| 1103 |
+
"base_model = tf.keras.applications.MobileNetV3Large(include_top = False, weights = 'imagenet', input_shape = input_size)\n",
|
| 1104 |
+
"base_model.trainable = False\n",
|
| 1105 |
+
"\n",
|
| 1106 |
+
"inputs = layers.Input(shape = (224, 224, 3), name = 'input_layer')\n",
|
| 1107 |
+
"x = base_model(inputs, training = False)\n",
|
| 1108 |
+
"x = layers.GlobalAveragePooling2D()(x)\n",
|
| 1109 |
+
"x = layers.BatchNormalization()(x)\n",
|
| 1110 |
+
"x = layers.Dense(256, activation = 'relu')(x)\n",
|
| 1111 |
+
"x = layers.Dropout(0.5)(x)\n",
|
| 1112 |
+
"outputs = layers.Dense(num_classes, activation = 'softmax', name = 'output_layer')(x)\n",
|
| 1113 |
+
"model_4 = tf.keras.Model(inputs, outputs)\n",
|
| 1114 |
+
"\n",
|
| 1115 |
+
"# Conmpile model\n",
|
| 1116 |
+
"model_4.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 1117 |
+
" optimizer = tf.keras.optimizers.Adam(),\n",
|
| 1118 |
+
" metrics = ['accuracy'])\n",
|
| 1119 |
+
"\n",
|
| 1120 |
+
"# Get model summary\n",
|
| 1121 |
+
"model_4.summary()"
|
| 1122 |
+
]
|
| 1123 |
+
},
|
| 1124 |
+
{
|
| 1125 |
+
"cell_type": "code",
|
| 1126 |
+
"execution_count": null,
|
| 1127 |
+
"metadata": {
|
| 1128 |
+
"id": "JlaenBnFkbd6"
|
| 1129 |
+
},
|
| 1130 |
+
"outputs": [],
|
| 1131 |
+
"source": [
|
| 1132 |
+
"# Fit\n",
|
| 1133 |
+
"initial_epochs = 10\n",
|
| 1134 |
+
"history_4 = model_4.fit(train_data, epochs = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_4'), model_checkpoint])"
|
| 1135 |
+
]
|
| 1136 |
+
},
|
| 1137 |
+
{
|
| 1138 |
+
"cell_type": "code",
|
| 1139 |
+
"execution_count": null,
|
| 1140 |
+
"metadata": {
|
| 1141 |
+
"id": "DNrilWI1kbd6"
|
| 1142 |
+
},
|
| 1143 |
+
"outputs": [],
|
| 1144 |
+
"source": [
|
| 1145 |
+
"# Plot loss curves\n",
|
| 1146 |
+
"plot_loss_curves(history = history_4)"
|
| 1147 |
+
]
|
| 1148 |
+
},
|
| 1149 |
+
{
|
| 1150 |
+
"cell_type": "code",
|
| 1151 |
+
"execution_count": null,
|
| 1152 |
+
"metadata": {
|
| 1153 |
+
"id": "pSBje1oDkbd7"
|
| 1154 |
+
},
|
| 1155 |
+
"outputs": [],
|
| 1156 |
+
"source": [
|
| 1157 |
+
"# Evaluate model\n",
|
| 1158 |
+
"model_4.evaluate(test_data)"
|
| 1159 |
+
]
|
| 1160 |
+
},
|
| 1161 |
+
{
|
| 1162 |
+
"cell_type": "code",
|
| 1163 |
+
"execution_count": null,
|
| 1164 |
+
"metadata": {
|
| 1165 |
+
"id": "mg35uM48kbd7"
|
| 1166 |
+
},
|
| 1167 |
+
"outputs": [],
|
| 1168 |
+
"source": [
|
| 1169 |
+
"# Make predictions with model\n",
|
| 1170 |
+
"model_4_preds = model_4.predict(test_data)\n",
|
| 1171 |
+
"model_4_preds[:1]"
|
| 1172 |
+
]
|
| 1173 |
+
},
|
| 1174 |
+
{
|
| 1175 |
+
"cell_type": "code",
|
| 1176 |
+
"execution_count": null,
|
| 1177 |
+
"metadata": {
|
| 1178 |
+
"id": "T8wfPeK2kbd7"
|
| 1179 |
+
},
|
| 1180 |
+
"outputs": [],
|
| 1181 |
+
"source": [
|
| 1182 |
+
"# Convert predictions to labels\n",
|
| 1183 |
+
"labels = model_4_preds.argmax(axis = 1)\n",
|
| 1184 |
+
"labels[:10]"
|
| 1185 |
+
]
|
| 1186 |
+
},
|
| 1187 |
+
{
|
| 1188 |
+
"cell_type": "code",
|
| 1189 |
+
"execution_count": null,
|
| 1190 |
+
"metadata": {
|
| 1191 |
+
"id": "YbXa2qMxkbd7"
|
| 1192 |
+
},
|
| 1193 |
+
"outputs": [],
|
| 1194 |
+
"source": [
|
| 1195 |
+
"# Plot model 4's confusion matrix\n",
|
| 1196 |
+
"make_confusion_matrix(y_true = true_labels, y_pred = labels)"
|
| 1197 |
+
]
|
| 1198 |
+
},
|
| 1199 |
+
{
|
| 1200 |
+
"cell_type": "markdown",
|
| 1201 |
+
"metadata": {
|
| 1202 |
+
"id": "Ytita5bvkbd7"
|
| 1203 |
+
},
|
| 1204 |
+
"source": [
|
| 1205 |
+
"### Fine-tuning model 4"
|
| 1206 |
+
]
|
| 1207 |
+
},
|
| 1208 |
+
{
|
| 1209 |
+
"cell_type": "code",
|
| 1210 |
+
"execution_count": null,
|
| 1211 |
+
"metadata": {
|
| 1212 |
+
"id": "qgYunGSPkbd7"
|
| 1213 |
+
},
|
| 1214 |
+
"outputs": [],
|
| 1215 |
+
"source": [
|
| 1216 |
+
"# Unfreeze last 20 layers of feature-extractor model\n",
|
| 1217 |
+
"base_model.trainable = True\n",
|
| 1218 |
+
"for layer in base_model.layers[:-20]:\n",
|
| 1219 |
+
" layer.trainable = False\n",
|
| 1220 |
+
"\n",
|
| 1221 |
+
"# Freeze all BatchNormalization layers within the last 20 layers\n",
|
| 1222 |
+
"for layer in base_model.layers[-20:]:\n",
|
| 1223 |
+
" if isinstance(layer, layers.BatchNormalization):\n",
|
| 1224 |
+
" layer.trainable = False\n",
|
| 1225 |
+
"\n",
|
| 1226 |
+
"# Compile\n",
|
| 1227 |
+
"model_4.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 1228 |
+
" optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n",
|
| 1229 |
+
" metrics = ['accuracy'])\n",
|
| 1230 |
+
"\n",
|
| 1231 |
+
"fine_tune_epochs = 10\n",
|
| 1232 |
+
"total_epochs = initial_epochs + fine_tune_epochs\n",
|
| 1233 |
+
"\n",
|
| 1234 |
+
"# Fit fine-tuned model\n",
|
| 1235 |
+
"history_4_fine_tune = model_4.fit(train_data, epochs = total_epochs, initial_epoch = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_4_fine-tuned'), model_checkpoint])"
|
| 1236 |
+
]
|
| 1237 |
+
},
|
| 1238 |
+
{
|
| 1239 |
+
"cell_type": "code",
|
| 1240 |
+
"execution_count": null,
|
| 1241 |
+
"metadata": {
|
| 1242 |
+
"id": "eQNwLIdZkbd7"
|
| 1243 |
+
},
|
| 1244 |
+
"outputs": [],
|
| 1245 |
+
"source": [
|
| 1246 |
+
"# Plot fine-tuned model's loss curves\n",
|
| 1247 |
+
"plot_loss_curves(history = history_4_fine_tune)"
|
| 1248 |
+
]
|
| 1249 |
+
},
|
| 1250 |
+
{
|
| 1251 |
+
"cell_type": "code",
|
| 1252 |
+
"execution_count": null,
|
| 1253 |
+
"metadata": {
|
| 1254 |
+
"id": "P8iM20Ftkbd7"
|
| 1255 |
+
},
|
| 1256 |
+
"outputs": [],
|
| 1257 |
+
"source": [
|
| 1258 |
+
"# Compare original and fine-tuned model historys\n",
|
| 1259 |
+
"compare_historys(original_history = history_4, new_history = history_4_fine_tune, initial_epochs = initial_epochs)"
|
| 1260 |
+
]
|
| 1261 |
+
},
|
| 1262 |
+
{
|
| 1263 |
+
"cell_type": "markdown",
|
| 1264 |
+
"metadata": {
|
| 1265 |
+
"id": "rgBypZX8kbd7"
|
| 1266 |
+
},
|
| 1267 |
+
"source": [
|
| 1268 |
+
"### Model 5: ConvNeXtBase"
|
| 1269 |
+
]
|
| 1270 |
+
},
|
| 1271 |
+
{
|
| 1272 |
+
"cell_type": "code",
|
| 1273 |
+
"execution_count": null,
|
| 1274 |
+
"metadata": {
|
| 1275 |
+
"id": "waeqLZaIkbd7"
|
| 1276 |
+
},
|
| 1277 |
+
"outputs": [],
|
| 1278 |
+
"source": [
|
| 1279 |
+
"# Setup data inputs\n",
|
| 1280 |
+
"input_size = (224, 224, 3)\n",
|
| 1281 |
+
"\n",
|
| 1282 |
+
"# Get number of classes\n",
|
| 1283 |
+
"num_classes = ds_info.features['label'].num_classes\n",
|
| 1284 |
+
"\n",
|
| 1285 |
+
"# Create feature extractor model with Functional API\n",
|
| 1286 |
+
"base_model = tf.keras.applications.ConvNeXtBase(include_top = False, weights = 'imagenet', input_shape = input_size)\n",
|
| 1287 |
+
"base_model.trainable = False\n",
|
| 1288 |
+
"\n",
|
| 1289 |
+
"inputs = layers.Input(shape = (224, 224, 3), name = 'input_layer')\n",
|
| 1290 |
+
"x = base_model(inputs, training = False)\n",
|
| 1291 |
+
"x = layers.GlobalAveragePooling2D()(x)\n",
|
| 1292 |
+
"x = layers.BatchNormalization()(x)\n",
|
| 1293 |
+
"x = layers.Dense(256, activation = 'relu')(x)\n",
|
| 1294 |
+
"x = layers.Dropout(0.5)(x)\n",
|
| 1295 |
+
"outputs = layers.Dense(num_classes, activation = 'softmax', name = 'output_layer')(x)\n",
|
| 1296 |
+
"model_5 = tf.keras.Model(inputs, outputs)\n",
|
| 1297 |
+
"\n",
|
| 1298 |
+
"# Conmpile model\n",
|
| 1299 |
+
"model_5.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 1300 |
+
" optimizer = tf.keras.optimizers.Adam(),\n",
|
| 1301 |
+
" metrics = ['accuracy'])\n",
|
| 1302 |
+
"\n",
|
| 1303 |
+
"# Get model summary\n",
|
| 1304 |
+
"model_5.summary()"
|
| 1305 |
+
]
|
| 1306 |
+
},
|
| 1307 |
+
{
|
| 1308 |
+
"cell_type": "code",
|
| 1309 |
+
"execution_count": null,
|
| 1310 |
+
"metadata": {
|
| 1311 |
+
"id": "rZztcU-1kbd7"
|
| 1312 |
+
},
|
| 1313 |
+
"outputs": [],
|
| 1314 |
+
"source": [
|
| 1315 |
+
"# Fit\n",
|
| 1316 |
+
"initial_epochs = 10\n",
|
| 1317 |
+
"history_5 = model_5.fit(train_data, epochs = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_5'), model_checkpoint])"
|
| 1318 |
+
]
|
| 1319 |
+
},
|
| 1320 |
+
{
|
| 1321 |
+
"cell_type": "code",
|
| 1322 |
+
"execution_count": null,
|
| 1323 |
+
"metadata": {
|
| 1324 |
+
"id": "v9bVEtNikbd8"
|
| 1325 |
+
},
|
| 1326 |
+
"outputs": [],
|
| 1327 |
+
"source": [
|
| 1328 |
+
"# Plot loss curves\n",
|
| 1329 |
+
"plot_loss_curves(history = history_5)"
|
| 1330 |
+
]
|
| 1331 |
+
},
|
| 1332 |
+
{
|
| 1333 |
+
"cell_type": "code",
|
| 1334 |
+
"execution_count": null,
|
| 1335 |
+
"metadata": {
|
| 1336 |
+
"id": "KBB2B1ICkbd8"
|
| 1337 |
+
},
|
| 1338 |
+
"outputs": [],
|
| 1339 |
+
"source": [
|
| 1340 |
+
"# Evaluate model\n",
|
| 1341 |
+
"model_5.evaluate(test_data)"
|
| 1342 |
+
]
|
| 1343 |
+
},
|
| 1344 |
+
{
|
| 1345 |
+
"cell_type": "code",
|
| 1346 |
+
"execution_count": null,
|
| 1347 |
+
"metadata": {
|
| 1348 |
+
"id": "BHBP8ebwkbd8"
|
| 1349 |
+
},
|
| 1350 |
+
"outputs": [],
|
| 1351 |
+
"source": [
|
| 1352 |
+
"# Make predictions with model\n",
|
| 1353 |
+
"model_5_preds = model_5.predict(test_data)\n",
|
| 1354 |
+
"model_5_preds[:1]"
|
| 1355 |
+
]
|
| 1356 |
+
},
|
| 1357 |
+
{
|
| 1358 |
+
"cell_type": "code",
|
| 1359 |
+
"execution_count": null,
|
| 1360 |
+
"metadata": {
|
| 1361 |
+
"id": "ycTqiAkfkbd8"
|
| 1362 |
+
},
|
| 1363 |
+
"outputs": [],
|
| 1364 |
+
"source": [
|
| 1365 |
+
"# Convert preds to labels\n",
|
| 1366 |
+
"labels = model_5_preds.argmax(axis = 1)\n",
|
| 1367 |
+
"labels[:10]"
|
| 1368 |
+
]
|
| 1369 |
+
},
|
| 1370 |
+
{
|
| 1371 |
+
"cell_type": "code",
|
| 1372 |
+
"execution_count": null,
|
| 1373 |
+
"metadata": {
|
| 1374 |
+
"id": "k_3QgWj2kbd8"
|
| 1375 |
+
},
|
| 1376 |
+
"outputs": [],
|
| 1377 |
+
"source": [
|
| 1378 |
+
"# Plot model 5's confusion matrix\n",
|
| 1379 |
+
"make_confusion_matrix(y_true = true_labels, y_pred = labels)"
|
| 1380 |
+
]
|
| 1381 |
+
},
|
| 1382 |
+
{
|
| 1383 |
+
"cell_type": "markdown",
|
| 1384 |
+
"metadata": {
|
| 1385 |
+
"id": "9sL8ySnMkbd8"
|
| 1386 |
+
},
|
| 1387 |
+
"source": [
|
| 1388 |
+
"### Fine-tuning model 5"
|
| 1389 |
+
]
|
| 1390 |
+
},
|
| 1391 |
+
{
|
| 1392 |
+
"cell_type": "code",
|
| 1393 |
+
"execution_count": null,
|
| 1394 |
+
"metadata": {
|
| 1395 |
+
"id": "140AGywQkbd8"
|
| 1396 |
+
},
|
| 1397 |
+
"outputs": [],
|
| 1398 |
+
"source": [
|
| 1399 |
+
"# Unfreeze last 20 layers of feature-extractor model\n",
|
| 1400 |
+
"base_model.trainable = True\n",
|
| 1401 |
+
"for layer in base_model.layers[:-20]:\n",
|
| 1402 |
+
" layer.trainable = False\n",
|
| 1403 |
+
"\n",
|
| 1404 |
+
"# Freeze all BatchNormalization layers within the last 20 layers\n",
|
| 1405 |
+
"for layer in base_model.layers[-20:]:\n",
|
| 1406 |
+
" if isinstance(layer, layers.BatchNormalization):\n",
|
| 1407 |
+
" layer.trainable = False\n",
|
| 1408 |
+
"\n",
|
| 1409 |
+
"# Compile\n",
|
| 1410 |
+
"model_5.compile(loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits = False),\n",
|
| 1411 |
+
" optimizer = tf.keras.optimizers.Adam(learning_rate = 1e-5),\n",
|
| 1412 |
+
" metrics = ['accuracy'])\n",
|
| 1413 |
+
"\n",
|
| 1414 |
+
"fine_tune_epochs = 10\n",
|
| 1415 |
+
"total_epochs = initial_epochs + fine_tune_epochs\n",
|
| 1416 |
+
"\n",
|
| 1417 |
+
"# Fit fine-tuned model\n",
|
| 1418 |
+
"history_5_fine_tune = model_5.fit(train_data, epochs = total_epochs, initial_epoch = initial_epochs, steps_per_epoch = len(train_data), validation_data = test_data, validation_steps = int(0.15 * len(test_data)), callbacks = [create_tensorboard_callback(dir_name = './tensorflow_logs', experiment_name = 'cassava_all_modelling_experiments/model_5_fine-tuned'), model_checkpoint])"
|
| 1419 |
+
]
|
| 1420 |
+
},
|
| 1421 |
+
{
|
| 1422 |
+
"cell_type": "code",
|
| 1423 |
+
"execution_count": null,
|
| 1424 |
+
"metadata": {
|
| 1425 |
+
"id": "_0HeC2Bckbd8"
|
| 1426 |
+
},
|
| 1427 |
+
"outputs": [],
|
| 1428 |
+
"source": [
|
| 1429 |
+
"# Plot loss curves for fine-tuned model\n",
|
| 1430 |
+
"plot_loss_curves(history = history_5_fine_tune)"
|
| 1431 |
+
]
|
| 1432 |
+
},
|
| 1433 |
+
{
|
| 1434 |
+
"cell_type": "code",
|
| 1435 |
+
"execution_count": null,
|
| 1436 |
+
"metadata": {
|
| 1437 |
+
"id": "z7xUBrh7kbd8"
|
| 1438 |
+
},
|
| 1439 |
+
"outputs": [],
|
| 1440 |
+
"source": [
|
| 1441 |
+
"# Compare orginal and fine-tuned model historys\n",
|
| 1442 |
+
"compare_historys(original_history = history_5, new_history = history_5_fine_tune, initial_epochs = initial_epochs)"
|
| 1443 |
+
]
|
| 1444 |
+
},
|
| 1445 |
+
{
|
| 1446 |
+
"cell_type": "markdown",
|
| 1447 |
+
"metadata": {
|
| 1448 |
+
"id": "b2GZVOzdkbd8"
|
| 1449 |
+
},
|
| 1450 |
+
"source": [
|
| 1451 |
+
"## Uploading all of our modelling experiments to TensorBoard\n",
|
| 1452 |
+
"\n",
|
| 1453 |
+
"We're now going to be uploading all of our modelling experiments to TensorBoard to view each metric for all of our model."
|
| 1454 |
+
]
|
| 1455 |
+
},
|
| 1456 |
+
{
|
| 1457 |
+
"cell_type": "code",
|
| 1458 |
+
"execution_count": null,
|
| 1459 |
+
"metadata": {
|
| 1460 |
+
"id": "YAMtxkqJkbd8"
|
| 1461 |
+
},
|
| 1462 |
+
"outputs": [],
|
| 1463 |
+
"source": [
|
| 1464 |
+
"# Upload models' results to TensorBoard\n",
|
| 1465 |
+
"!pip install -q tensorboard\n",
|
| 1466 |
+
"%load_ext tensorboard\n",
|
| 1467 |
+
"%tensorboard --logdir=/content/tensorflow_logs\n",
|
| 1468 |
+
"%reload_ext tensorboard"
|
| 1469 |
+
]
|
| 1470 |
+
}
|
| 1471 |
+
],
|
| 1472 |
+
"metadata": {
|
| 1473 |
+
"accelerator": "GPU",
|
| 1474 |
+
"kernelspec": {
|
| 1475 |
+
"display_name": "Python 3",
|
| 1476 |
+
"name": "python3"
|
| 1477 |
+
},
|
| 1478 |
+
"language_info": {
|
| 1479 |
+
"name": "python"
|
| 1480 |
+
},
|
| 1481 |
+
"colab": {
|
| 1482 |
+
"provenance": []
|
| 1483 |
+
}
|
| 1484 |
+
},
|
| 1485 |
+
"nbformat": 4,
|
| 1486 |
+
"nbformat_minor": 0
|
| 1487 |
+
}
|