TLM for CNN
TLM for CNN
Transfer learning is a machine learning technique where a model trained on one task is used
as a starting point for a related task. In the context of CNNs, this involves leveraging the
knowledge learned from a large dataset to solve a new problem with limited data.
import numpy as np
import pandas as pd Transfer Learning Code + Data
# Visualization
import matplotlib.pyplot as plt
import seaborn as sns
# Keras library
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dro
pout, GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGener
ator
from keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
from keras import regularizers
from keras.callbacks import ReduceLROnPlateau
# Distributed Computing
import tensorflow as tf
import warnings
warnings.filterwarnings("ignore")
Limited Data: CNNs require large datasets for optimal performance. Transfer
learning allows us to train effective models with smaller datasets.
Computational Efficiency: Pre-trained models can be fine-tuned more efficiently
than training a model from scratch.
Improved Accuracy: Transfer learning often leads to better performance, especially
for tasks with similar characteristics to the original training data.
1. Feature Extraction:
o Frozen Layers: The initial layers of a pre-trained CNN are frozen, and only
the final layers are trained on the new task. This assumes that the early layers
extract generic features that are useful for a variety of tasks.
o Fine-tuning: Some layers are fine-tuned along with the new layers. This
allows the network to adapt to the specific characteristics of the new task.
2. Feature Fusion:
o Multiple Models: Features extracted from multiple pre-trained models are
combined to create a more robust representation.
o Early Fusion: Features are combined at an early stage, typically before the
fully connected layers.
o Late Fusion: Features are combined at a later stage, after the fully connected
layers.
Choose a suitable pre-trained model: Consider the similarity between the original
task and your new task.
Experiment with different layers to freeze or fine-tune: Start with freezing more
layers and gradually unfreeze them as needed.
Adjust the learning rate: Use a lower learning rate for fine-tuning to avoid
overfitting.
Data augmentation: Augment your training data to increase its diversity and prevent
overfitting.
Regularization techniques: Employ techniques like dropout or L1/L2 regularization
to prevent overfitting.
By effectively applying transfer learning techniques, you can significantly accelerate the
development of CNN models for various tasks, even with limited data.
BATCH_SIZE = 48
image_height = 299
image_width = 299
data_generator_2 = ImageDataGenerator(
rescale=1./255,
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
brightness_range = [0.9,1.1],
horizontal_flip=False,
vertical_flip=False,
fill_mode='nearest'
)
print('Data Augmentation 2 was created')
train_generator1 = data_generator_1.flow_from_directory(
directory = "/kaggle/input/brain-tumor-classification-mri/Training",
color_mode = "rgb",
target_size = (image_height, image_width), # image height , image
width
class_mode = "categorical",
batch_size = BATCH_SIZE,
shuffle = True,
seed = 42)
print('Data Augmentation 1 was used to generate train data set\n')
test_generator = data_generator_3.flow_from_directory(
directory = "/kaggle/input/brain-tumor-classification-mri/Testing",
color_mode = "rgb",
target_size = (image_height, image_width), # image height , image
width
class_mode = "categorical",
batch_size = BATCH_SIZE,
shuffle = True,
seed = 42)
dict_class = train_generator1.class_indices
print('Dictionary: {}'.format(dict_class))
class_names = list(dict_class.keys()) # storing class/breed names in
a list
print('Class labels: {}'.format(class_names))
# Dataset characteristics
print("Dataset Characteristics of Test Data Set:\n")
print("Number of images:", len(test_generator.classes))
print("Number of glioma_tumor images:", len([label for label in test_g
enerator.classes if label == 0]))
print("Number of meningioma_tumor images:", len([label for label in
test_generator.classes if label == 1]))
print("Number of no_tumor images:", len([label for label in test_gene
rator.classes if label == 2]))
print("Number of pituitary_tumor images:", len([label for label in test
_generator.classes if label == 3]))
print()
class_weights = compute_class_weight(class_weight = "balanced", c
lasses= np.unique(train_generator1.classes), y= train_generator1.cla
sses)
class_weights = dict(zip(np.unique(train_generator1.classes), class_
weights))
class_weights
plt.figure(figsize=[20, 15])
for i in range(15):
plt.subplot(3, 5, i+1)
plt.imshow(img[i])
plt.axis('off')
plt.title(class_names[np.argmax(label[i])])
plt.show()
Convolutional neural networks (CNNs)
# Define the epochs for training
EPOCHS = 2
# For development purpose, we first limit the train data set to the or
iginal image data set
# train_data = merged_train_generator
# train_data = train_generator1
train_data = train_generator1
# train_data = test_generator
Small Filters: The convolutional layers in VGG16 use small 3x3 filters, which are
repeated multiple times to increase the depth of the network.
Uniform Stride: The convolutional layers use a uniform stride of 1, which means that
the filters are applied to every pixel in the input image.
Max Pooling: After each block of convolutional layers, a max pooling layer is used
to reduce the spatial dimensions of the feature maps.
Benefits of VGG16:
Pre-trained Models: Pre-trained VGG16 models are widely available, which can be
used as a starting point for transfer learning tasks in other domains.
Applications of VGG16:
Image Classification: VGG16 is commonly used for image classification tasks, such
as object recognition, scene classification, and facial recognition.
Transfer Learning: VGG16 can be used as a feature extractor for transfer learning
tasks, where the pre-trained model is fine-tuned on a smaller dataset to solve a related
task.
In summary, VGG16 is a powerful and versatile CNN architecture that has made
significant contributions to the field of computer vision. Its simplicity, effectiveness, and
availability of pre-trained models make it a popular choice for various image
classification and transfer learning tasks.
# Create a MirroredStrategy
strategy = tf.distribute.MirroredStrategy(devices=['/gpu:0', '/gpu:1'])
# Model summary
print("Model Summary (VGG16):")
model_VGG16.summary()
print()
Benefits of MobileNetV2:
Transfer Learning: MobileNetV2 can be used for transfer learning, where the pre-
trained model is fine-tuned on a smaller dataset to solve a related task.
Applications of MobileNetV2:
# Create a MirroredStrategy
strategy = tf.distribute.MirroredStrategy(devices=['/gpu:0', '/gpu:1'])
# Add a global average pooling layer and output layer for classific
ation
model_MobileNet.add(GlobalAveragePooling2D())
model_MobileNet.add(Dense(128, activation='relu', kernel_regulari
zer=regularizers.l2(0.001)))
model_MobileNet.add(Dropout(0.4))
model_MobileNet.add(Dense(64, activation='relu', kernel_regulariz
er=regularizers.l2(0.001)))
model_MobileNet.add(Dropout(0.2))
model_MobileNet.add(Dense(4, activation='softmax'))
# Model summary
print("Model Summary (MobileNetV2):")
model_MobileNet.summary()
print()
3. Transition Layers:
- Transition layers are used to reduce the number of feature maps and the spatial dimensions
of the network.
- They typically consist of a batch normalization layer, a 1x1 convolution, and a 2x2 average
pooling layer.
Benefits of DenseNet:
Efficient Parameter Usage: DenseNet can achieve high accuracy with fewer
parameters compared to traditional CNNs.
Feature Reuse: Features from earlier layers are reused by later layers, which can help
to improve the network's ability to learn complex patterns.
Reduced Overfitting: The dense connectivity pattern can help to reduce overfitting
by encouraging feature reuse and preventing the network from learning redundant
features.
Applications of DenseNet:
Image Classification: DenseNet has been successfully used for various image
classification tasks, such as ImageNet classification and fine-grained object
recognition.
Object Detection: DenseNet has been incorporated into object detection architectures
like Faster R-CNN and SSD.
Semantic Segmentation: DenseNet has been applied to semantic segmentation tasks,
where the goal is to assign a semantic label to each pixel in an image.
In conclusion, DenseNet is a powerful and efficient CNN architecture that has made
significant contributions to the field of computer vision. Its dense connectivity pattern
and efficient parameter usage make it a popular choice for various image analysis tasks.
%%time
# Create a MirroredStrategy
strategy = tf.distribute.MirroredStrategy(devices=['/gpu:0', '/gpu:1'])
# Add a global average pooling layer and output layer for classific
ation
model_DenseNet.add(GlobalAveragePooling2D())
model_DenseNet.add(Dense(128, activation='relu', kernel_regulari
zer=regularizers.l2(0.001)))
model_DenseNet.add(Dropout(0.4))
model_DenseNet.add(Dense(64, activation='relu', kernel_regulariz
er=regularizers.l2(0.001)))
model_DenseNet.add(Dropout(0.2))
model_DenseNet.add(Dense(4, activation='softmax'))
# Model summary
print("Model Summary (DenseNet121):")
model_DenseNet.summary()
print()
Inception Modules: The core building block of InceptionV3 is the Inception module.
It consists of a parallel combination of different convolutional filters with different
sizes (1x1, 3x3, 5x5) and a pooling layer. This allows the network to capture features
at different scales.
Label Smoothing: To regularize the network and prevent over fitting, InceptionV3
uses label smoothing. This technique assigns a small probability to incorrect classes,
forcing the network to be less confident in its predictions.
Benefits of InceptionV3:
Flexibility: The Inception modules allow the network to capture features at different
scales, making it more flexible and adaptable to various image tasks.
Applications of InceptionV3:
Image Classification: InceptionV3 is widely used for image classification tasks, such
as object recognition, scene classification, and fine-grained categorization.
In conclusion, InceptionV3 is a powerful and efficient CNN architecture that has made
significant contributions to the field of computer vision. Its Inception modules,
factorization techniques, and auxiliary classifiers make it a popular choice for various
image analysis tasks.
%%time
# Create a MirroredStrategy
strategy = tf.distribute.MirroredStrategy(devices=['/gpu:0', '/gpu:1'])
# Add a global average pooling layer and output layer for classific
ation
model_Inception.add(GlobalAveragePooling2D())
model_Inception.add(Dense(128, activation='relu', kernel_regulari
zer=regularizers.l2(0.001)))
model_Inception.add(Dropout(0.4))
model_Inception.add(Dense(64, activation='relu', kernel_regulariz
er=regularizers.l2(0.001)))
model_Inception.add(Dropout(0.2))
model_Inception.add(Dense(4, activation='softmax'))
# Model summary
print("Model Summary (InceptionV3):")
model_Inception.summary()
print()
Residual Blocks:
Residual Function: The residual function is the difference between the output of the
stack of layers and the input. This residual function is learned by the network.
Easier Optimization: Residual blocks make it easier for the network to learn deep
representations. By learning the residual function, the network can focus on learning
the differences between the input and output, rather than the entire mapping.
Benefits of ResNet:
Deep Networks: ResNet has enabled the training of extremely deep networks, which
have been shown to improve performance on various tasks.
Faster Training: Residual blocks can help to speed up training by making it easier
for the network to learn deep representations.
Applications of ResNet:
Image Classification: ResNet is widely used for image classification tasks, such as
object recognition and scene classification.
Object Detection: ResNet has been incorporated into object detection architectures
like Faster R-CNN and SSD.
In conclusion, ResNet is a powerful and influential CNN architecture that has enabled
the training of extremely deep networks and has achieved state-of-the-art performance
on various tasks. The introduction of residual blocks has been a major breakthrough in
deep learning, allowing for the development of more complex and accurate models.
%%time
import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
, Dropout
from tensorflow.keras import regularizers
# Create a MirroredStrategy
strategy = tf.distribute.MirroredStrategy(devices=['/gpu:0', '/gpu:1'])
# Open a strategy scope
with strategy.scope():
# Add a global average pooling layer and output layer for classific
ation
model_ResNet.add(GlobalAveragePooling2D())
model_ResNet.add(Dense(128, activation='relu', kernel_regularize
r=regularizers.l2(0.001)))
model_ResNet.add(Dropout(0.4))
model_ResNet.add(Dense(64, activation='relu', kernel_regularizer
=regularizers.l2(0.001)))
model_ResNet.add(Dropout(0.2))
model_ResNet.add(Dense(4, activation='softmax'))
# Model summary
print("Model Summary (ResNet50):")
model_ResNet.summary()
print()
Compound Scaling: EfficientNet uses a compound scaling method that scales the
network's depth, width, and resolution in a balanced manner. This ensures that the
network's performance improves while maintaining computational efficiency.
EfficientNet-B0 to EfficientNet-B7: The EfficientNet family consists of a series of
models, ranging from EfficientNet-B0 to EfficientNet-B7. These models differ in
their size and complexity, allowing for a trade-off between accuracy and
computational cost.
Benefits of EfficientNet:
Applications of EfficientNet:
Image Classification: EfficientNet is widely used for image classification tasks, such
as object recognition, scene classification, and fine-grained categorization.
In conclusion, EfficientNet is a powerful and efficient CNN architecture that has made
significant contributions to the field of computer vision. Its compound scaling method
and efficient building blocks make it a popular choice for various image analysis tasks,
especially in scenarios where computational resources are limited.
%%time
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
, Dropout
from tensorflow.keras import regularizers
# Create a MirroredStrategy
strategy = tf.distribute.MirroredStrategy(devices=['/gpu:0', '/gpu:1'])
# Add a global average pooling layer and output layer for classific
ation
model_EfficientNet.add(GlobalAveragePooling2D())
model_EfficientNet.add(Dense(128, activation='relu', kernel_regul
arizer=regularizers.l2(0.001)))
model_EfficientNet.add(Dropout(0.4))
model_EfficientNet.add(Dense(64, activation='relu', kernel_regular
izer=regularizers.l2(0.001)))
model_EfficientNet.add(Dropout(0.2))
model_EfficientNet.add(Dense(4, activation='softmax'))
# Model summary
print("Model Summary (EfficientNetB0):")
model_EfficientNet.summary()
print()
Transfer Learning: NASNet can be used for transfer learning, where a pre-trained
model is fine-tuned on a smaller dataset to solve a related task.
Benefits of NASNet:
Automation: The use of neural architecture search eliminates the need for manual
design, making it easier to explore a wider range of architectures.
Applications of NASNet:
Image Classification: NASNet is widely used for image classification tasks, such as
object recognition, scene classification, and fine-grained categorization.
Object Detection: NASNet has been incorporated into object detection architectures
like Faster R-CNN and SSD.
In conclusion, NASNet is a powerful and efficient CNN architecture that has made
significant contributions to the field of computer vision. Its use of neural architecture
search allows for the automatic generation of high-performing models, making it a
valuable tool for researchers and practitioners.
%%time
import tensorflow as tf
from tensorflow.keras.applications import NASNetMobile
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
, Dropout
from tensorflow.keras import regularizers
# Add a global average pooling layer and output layer for classifica
tion
model_NASNet.add(GlobalAveragePooling2D())
model_NASNet.add(Dense(128, activation='relu', kernel_regularizer
=regularizers.l2(0.001)))
model_NASNet.add(Dropout(0.4))
model_NASNet.add(Dense(64, activation='relu', kernel_regularizer=
regularizers.l2(0.001)))
model_NASNet.add(Dropout(0.2))
model_NASNet.add(Dense(4, activation='softmax'))
# Model summary
print("Model Summary (NASNetMobile):")
model_NASNet.summary()
print()
plt.figure(figsize=[15, 5])
plt.show()
Prediction Result Samples
Linkeding : https://www.linkedin.com/groups/7436898/
test_generator.reset()
img, label = next(test_generator)
prediction = model_Inception.predict(img)
test_pred_classes = np.argmax(prediction, axis=1)
plt.figure(figsize=[20, 20])
for i in range(20):
plt.subplot(5, 4, i+1)
plt.imshow(img[i])
plt.axis('off')
plt.title("Label : {}\n Prediction : {} {:.1f}%".format(class_names[np.
argmax(label[i])], class_names[test_pred_classes[i]], 100 * np.max(p
rediction[i])))
plt.show()