Transfer Learning Q3 2
Transfer Learning Q3 2
data = [
["Data Preprocessing", "Scaling and Resizing", "Done"],
["Data Preprocessing", "Image Augmentation", "Done"],
["Data Preprocessing", "Train and test data handled correctly",
"Done"],
["Data Preproces sing", "Gaussian Blur, Histogram Equalization and
Intensity thresholds", "Done"],
["Model Trained", "Training Time?", "1322.24 seconds"],
["Model Trained", "AUC and Confusion Matrix Computed", "Done"],
["Model Trained", "Overfitting/Underfitting checked and handled",
"Done"],
["Empirical Tuning", "Interpretability Implemented", "LIME /
Occlusion senitivity"],
["Empirical Tuning", "1st Round of Tuning", "unfreezed less
layers"],
["Empirical Tuning", "2nd Round of Tuning", "Hyperparameter
Tuned"],
]
print(table)
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Task | Sub-task
| Comments |
+====================+================================================
================+=============================+
| Data Preprocessing | Scaling and Resizing
| Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Data Preprocessing | Image Augmentation
| Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Data Preprocessing | Train and test data handled correctly
| Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Data Preprocessing | Gaussian Blur, Histogram Equalization and
Intensity thresholds | Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Model Trained | Training Time?
| Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Model Trained | AUC and Confusion Matrix Computed
| Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Model Trained | Overfitting/Underfitting checked and handled
| Done |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Empirical Tuning | Interpretability Implemented
| LIME / Occlusion senitivity |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Empirical Tuning | 1st Round of Tuning
| unfreezed more layers |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
| Empirical Tuning | 2nd Round of Tuning
| Hyperparameter Tuned |
+--------------------
+----------------------------------------------------------------
+-----------------------------+
import warnings
warnings.filterwarnings("ignore")
# Importing Libraries
import os
import pandas as pd
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from PIL import Image, UnidentifiedImageError
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.layers import GlobalAveragePooling2D, Dropout,
Dense
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping,
ReduceLROnPlateau
df.head(5)
df['label'].unique()
Total 5 Classes
count_type = []
for i in df['label'].unique():
count_type.append(df['label'].tolist().count(i))
print(f"{i} class - {df['label'].tolist().count(i)} images")
return image
metadata['processed_images'] = metadata['photo_id'].apply(lambda x:
preprocess_image(os.path.join(IMAGE_DIR, x), apply_augment=False))
# Callbacks
early_stopping = EarlyStopping(monitor='val_loss', patience=5,
restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5,
patience=3, min_lr=1e-6)
# Start timer
start_time = time.time()
# Train the model
history = model.fit(
train_generator,
validation_data=test_generator,
epochs=5,
callbacks=[early_stopping, reduce_lr]
)
Epoch 1/5
500/500 ━━━━━━━━━━━━━━━━━━━━ 241s 464ms/step - accuracy: 0.5212 -
loss: 1.1878 - val_accuracy: 0.5397 - val_loss: 1.1353 -
learning_rate: 0.0010
Epoch 2/5
500/500 ━━━━━━━━━━━━━━━━━━━━ 274s 547ms/step - accuracy: 0.5499 -
loss: 1.1261 - val_accuracy: 0.5403 - val_loss: 3.3757 -
learning_rate: 0.0010
Epoch 3/5
500/500 ━━━━━━━━━━━━━━━━━━━━ 260s 519ms/step - accuracy: 0.5773 -
loss: 1.0786 - val_accuracy: 0.5397 - val_loss: 1.1063 -
learning_rate: 0.0010
Epoch 4/5
500/500 ━━━━━━━━━━━━━━━━━━━━ 271s 540ms/step - accuracy: 0.5972 -
loss: 1.0446 - val_accuracy: 0.5400 - val_loss: 1.1369 -
learning_rate: 0.0010
Epoch 5/5
500/500 ━━━━━━━━━━━━━━━━━━━━ 276s 551ms/step - accuracy: 0.6076 -
loss: 1.0296 - val_accuracy: 0.2842 - val_loss: 1.4965 -
learning_rate: 0.0010
Training time: 1322.24 seconds
2024-11-03 22:32:09.981898: W
tensorflow/core/framework/local_rendezvous.cc:404] Local rendezvous is
aborting with status: OUT_OF_RANGE: End of sequence
# Convert predictions to class labels
predicted_classes = np.argmax(predictions, axis=1)
true_classes = test_generator.classes
# Accuracy
accuracy = accuracy_score(true_classes, predicted_classes)
print(f"Accuracy: {accuracy:.4f}")
Accuracy: 0.5400
# Confusion Matrix
conf_matrix = confusion_matrix(true_classes, predicted_classes)
print("Confusion Matrix:\n", conf_matrix)
Confusion Matrix:
[[ 0 313 0 0 0]
[ 0 2157 4 0 0]
[ 0 1118 3 0 0]
[ 0 34 0 0 0]
[ 0 371 0 0 0]]
# Classification Report
class_report = classification_report(true_classes, predicted_classes,
target_names=test_generator.class_indices.keys())
print("Classification Report:\n", class_report)
Classification Report:
precision recall f1-score support
# Weighted F1 Score
weighted_f1 = f1_score(true_classes, predicted_classes,
average='weighted')
print(f"Weighted F1 Score: {weighted_f1:.4f}")
Interpretation
import numpy as np
import lime
import lime.lime_image
from skimage.segmentation import mark_boundaries
{"model_id":"fb7cc4e2b09b4ebc893173824f1888df","version_major":2,"vers
ion_minor":0}
return occlusion_map
# Use a higher learning rate, which might lead to a model that cannot
converge well within limited epochs.
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
# Higher LR for underfitting
loss='categorical_crossentropy',
metrics=['accuracy'])
Epoch 1/2
500/500 ━━━━━━━━━━━━━━━━━━━━ 231s 447ms/step - accuracy: 0.5161 -
loss: 1.7231 - val_accuracy: 0.5415 - val_loss: 1.2015 -
learning_rate: 0.0100
Epoch 2/2
500/500 ━━━━━━━━━━━━━━━━━━━━ 268s 535ms/step - accuracy: 0.5718 -
loss: 1.1043 - val_accuracy: 0.5400 - val_loss: 1.3175 -
learning_rate: 0.0100
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score, confusion_matrix
# Accuracy plot
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
# Loss plot
plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.tight_layout()
plt.show()
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.layers import Dense, Dropout,
GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping,
ReduceLROnPlateau
from sklearn.metrics import roc_auc_score
import time
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(dropout_rate)(x)
x = Dense(256, activation='relu')(x)
output = Dense(len(metadata['label'].unique()),
activation='softmax')(x)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learnin
g_rate),
loss='categorical_crossentropy',
metrics=['accuracy'])
# Callbacks
early_stopping = EarlyStopping(monitor='val_loss', patience=2,
restore_best_weights=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5,
patience=1, min_lr=1e-6)
start_time = time.time()
history = model.fit(
train_generator,
validation_data=test_generator,
epochs=3, # Use fewer epochs for quicker iterations
callbacks=[early_stopping, reduce_lr]
)
return auc_score
best_auc = 0
best_params = {}
for lr in learning_rates:
for dr in dropout_rates:
for uf in unfreeze_layers_options:
print(f'Training with learning_rate={lr},
dropout_rate={dr}, unfreeze_layers={uf}')
auc = build_and_train_model(lr, dr, uf, train_generator,
test_generator)
if auc > best_auc:
best_auc = auc
best_params = {'learning_rate': lr, 'dropout_rate':
dr, 'unfreeze_layers': uf}
----------------------------------------------------------------------
-----
KeyboardInterrupt Traceback (most recent call
last)
Cell In[72], line 72
70 for uf in unfreeze_layers_options:
71 print(f'Training with learning_rate={lr},
dropout_rate={dr}, unfreeze_layers={uf}')
---> 72 auc = build_and_train_model(lr, dr, uf, train_generator,
test_generator)
73 if auc > best_auc:
74 best_auc = auc
File
~/Library/Python/3.9/lib/python/site-packages/keras/src/utils/tracebac
k_utils.py:117, in filter_traceback.<locals>.error_handler(*args,
**kwargs)
115 filtered_tb = None
116 try:
--> 117 return fn(*args, **kwargs)
118 except Exception as e:
119 filtered_tb = _process_traceback_frames(e.__traceback__)
File
~/Library/Python/3.9/lib/python/site-packages/keras/src/backend/tensor
flow/trainer.py:320, in TensorFlowTrainer.fit(self, x, y, batch_size,
epochs, verbose, callbacks, validation_split, validation_data,
shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch,
validation_steps, validation_batch_size, validation_freq)
318 for step, iterator in epoch_iterator.enumerate_epoch():
319 callbacks.on_train_batch_begin(step)
--> 320 logs = self.train_function(iterator)
321 callbacks.on_train_batch_end(step, logs)
322 if self.stop_training:
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/util/
traceback_utils.py:150, in
filter_traceback.<locals>.error_handler(*args, **kwargs)
148 filtered_tb = None
149 try:
--> 150 return fn(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
polymorphic_function/polymorphic_function.py:833, in
Function.__call__(self, *args, **kwds)
830 compiler = "xla" if self._jit_compile else "nonXla"
832 with OptionalXlaContext(self._jit_compile):
--> 833 result = self._call(*args, **kwds)
835 new_tracing_count = self.experimental_get_tracing_count()
836 without_tracing = (tracing_count == new_tracing_count)
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
polymorphic_function/polymorphic_function.py:878, in
Function._call(self, *args, **kwds)
875 self._lock.release()
876 # In this case we have not created variables on the first
call. So we can
877 # run the first trace but we should fail if variables are
created.
--> 878 results = tracing_compilation.call_function(
879 args, kwds, self._variable_creation_config
880 )
881 if self._created_variables:
882 raise ValueError("Creating variables on a non-first call to
a function"
883 " decorated with tf.function.")
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
polymorphic_function/tracing_compilation.py:139, in
call_function(args, kwargs, tracing_options)
137 bound_args = function.function_type.bind(*args, **kwargs)
138 flat_inputs = function.function_type.unpack_inputs(bound_args)
--> 139 return function._call_flat( # pylint: disable=protected-
access
140 flat_inputs, captured_inputs=function.captured_inputs
141 )
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
polymorphic_function/concrete_function.py:1322, in
ConcreteFunction._call_flat(self, tensor_inputs, captured_inputs)
1318 possible_gradient_type =
gradients_util.PossibleTapeGradientTypes(args)
1319 if (possible_gradient_type ==
gradients_util.POSSIBLE_GRADIENT_TYPES_NONE
1320 and executing_eagerly):
1321 # No tape is watching; skip to running the function.
-> 1322 return self._inference_function.call_preflattened(args)
1323 forward_backward =
self._select_forward_and_backward_functions(
1324 args,
1325 possible_gradient_type,
1326 executing_eagerly)
1327 forward_function, args_with_tangents =
forward_backward.forward()
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
polymorphic_function/atomic_function.py:216, in
AtomicFunction.call_preflattened(self, args)
214 def call_preflattened(self, args: Sequence[core.Tensor]) ->
Any:
215 """Calls with flattened tensor inputs and returns the
structured output."""
--> 216 flat_outputs = self.call_flat(*args)
217 return self.function_type.pack_output(flat_outputs)
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
polymorphic_function/atomic_function.py:251, in
AtomicFunction.call_flat(self, *args)
249 with record.stop_recording():
250 if self._bound_context.executing_eagerly():
--> 251 outputs = self._bound_context.call_function(
252 self.name,
253 list(args),
254 len(self.function_type.flat_outputs),
255 )
256 else:
257 outputs = make_call_op_in_graph(
258 self,
259 list(args),
260 self._bound_context.function_call_options.as_attrs(),
261 )
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
context.py:1500, in Context.call_function(self, name, tensor_inputs,
num_outputs)
1498 cancellation_context = cancellation.context()
1499 if cancellation_context is None:
-> 1500 outputs = execute.execute(
1501 name.decode("utf-8"),
1502 num_outputs=num_outputs,
1503 inputs=tensor_inputs,
1504 attrs=attrs,
1505 ctx=self,
1506 )
1507 else:
1508 outputs = execute.execute_with_cancellation(
1509 name.decode("utf-8"),
1510 num_outputs=num_outputs,
(...)
1514 cancellation_manager=cancellation_context,
1515 )
File
~/Library/Python/3.9/lib/python/site-packages/tensorflow/python/eager/
execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs,
ctx, name)
51 try:
52 ctx.ensure_initialized()
---> 53 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle,
device_name, op_name,
54 inputs, attrs,
num_outputs)
55 except core._NotOkStatusException as e:
56 if name is not None:
KeyboardInterrupt: