UNIT_I CHP_5
UNIT_I CHP_5
# creates a training and testing dataset from the images in the train_dir and test_dir directory.
# The images are resized to 64x64 pixels, and the data is batched into groups of 32 images.
train_generator = image_dataset_from_directory(train_dir, image_size=(64, 64), batch_size=32)
test_generator = image_dataset_from_directory(test_dir, image_size=(64, 64), batch_size=32)
this code defines a CNN model with three
# Building the CNN model convolutional layers, each followed by batch
#Initializes a sequential model in Keras. The Sequential class is used to create a linear normalization and max pooling layers. After the
#stack of layers, which means you can easily add one layer at a time convolutional layers, a flattening layer is added to
model = keras.Sequential([ convert the 2D feature maps into a 1D vector, which
# Conv layer 1: is then passed through two fully connected layers.
keras.layers.Conv2D(128, (3, 3), input_shape=(64, 64, 3), activation='relu'), The final output layer uses a sigmoid activation
keras.layers.BatchNormalization(), function, making the model suitable for binary
keras.layers.MaxPooling2D(pool_size=(2, 2)), classification .
# Conv layer 2:
keras.layers.Conv2D(128, (3, 3), input_shape=(64, 64, 3), activation='relu'),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
adds a 2D convolutional layer with 128 filters, each of size 3x3. The
keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2, 2)), input_shape=(64, 64, 3) specifies that the input to this layer is 64x64 images
# Conv layer 3: with 3 color channels (RGB).
keras.layers.Conv2D(32, (3, 3), activation='relu'), The activation='relu' applies the ReLU activation function, which introduces
keras.layers.BatchNormalization(), non-linearity to the model.
keras.layers.MaxPooling2D(pool_size=(2, 2)), keras.layers.BatchNormalization(): This normalizes the output of the
keras.layers.Flatten(), previous layer, which can help speed up training and improve the model's
# Fully connected layers: performance.
keras.layers.Dense(units=128, activation='relu'), keras.layers.MaxPooling2D(pool_size=(2, 2)): This layer performs max
keras.layers.Dense(units=1, activation='sigmoid') pooling with a 2x2 pool size, which reduces the spatial dimensions (height
]) and width) of the output volume, helping to down-sample the
representation and reduce the computational load
keras.layers.Flatten(): This layer flattens the input, converting the 2D matrix
data to a 1D vector. This is necessary before feeding the data into fully
connected (dense) layers.
keras.layers.Dense(units=128, activation='relu'): This adds a fully connected
(dense) layer with 128 units (neurons). The ReLU activation function is
applied.
# Compile the model
model.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001), loss='binary_crossentropy',
metrics=['accuracy'])
# Print the model summary
model.summary() model.compile(...): This method configures the model for
# Train the model training.
optimizer=keras.optimizers.Adam(learning_rate=0.001):
Specifies the optimizer to use for training. In this case, it is
the Adam optimizer with a learning rate of 0.001. Adam is a
popular choice due to its adaptive learning rate properties.
loss='binary_crossentropy': Specifies the loss function to
use. Binary crossentropy is used here because the task is
binary classification (dog vs cat).
metrics=['accuracy']: Specifies the metrics to evaluate
during training and testing. Here, accuracy is used to
measure the performance of the model.
model.summary(): This method prints a summary of the
model's architecture. It provides details about each layer,
including the type, output shape, and number of
parameters.
logs = model.fit(train_generator, epochs=10, validation_data=test_generator)
# Plotting the training log
plt.figure(figsize=(10, 5))
plt.title('Training Log')
plt.plot(logs.history['loss'], label='Training Loss')
plt.plot(logs.history['accuracy'], label='Training Accuracy')
plt.xlabel('Epochs') model.fit(...): This method trains the model for a fixed number of epochs
plt.ylabel('Score') (iterations over the dataset).
plt.legend() train_generator: The training data. This is an instance of a data generator
plt.show() that provides batches of training data.
epochs=10: Specifies the number of epochs to train the model. An epoch is
# Evaluate the model
one complete pass through the training dataset.
res = model.evaluate(test_generator) validation_data=test_generator: Specifies the validation data. This is an
accuracy = res[1] instance of a data generator that provides batches of validation data. The
print("Test Accuracy:", accuracy) model's performance on this data will be evaluated at the end of each
epoch.
The model.fit() method returns a History object, which contains a record of
training loss and metrics values at successive epochs, as well as validation loss
and metrics values
model.evaluate(test_generator): This method evaluates the model on the test
data provided by the test_generator. The evaluate method returns a list of
metrics values (e.g., loss and accuracy) for the model on the test dataset. The
values correspond to the metrics specified during the compile step.
2.2 Improving CNN Model with regularization
import tensorflow as tf
from tensorflow import keras
from keras.utils import image_dataset_from_directory
import matplotlib.pyplot as plt
# Data directories
train_dir = '/content/drive/My Drive/DogvsCat/train'
test_dir = '/content/drive/My Drive/DogvsCat/test'
# Conv layer 2:
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
# Conv layer 3:
keras.layers.Conv2D(32, (3, 3), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Flatten(),
output = vgg.layers[-1].output
output = keras.layers.Flatten()(output)
vgg_model.summary()
#Custom Classifier on Top of VGG16
print("Trainable layers:", vgg_model.trainable_weights)
input_shape = vgg_model.output_shape[1]
model = Sequential()
model.add(InputLayer(input_shape=(input_shape,)))
model.add(Dense(512, activation='relu', input_dim=input_shape))
model.add(Dropout(0.3))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['accuracy'])
model.summary()
history = model.fit(x=train_features_vgg, y=train_labels_enc,
validation_data=(validation_features_vgg, validation_labels_enc),
batch_size=batch_size,
epochs=epochs,
verbose=1)
3.2 Pretrained CNN model as feature Extractor with
Augmentation
from keras.applications import VGG16
from keras.models import Model, Sequential
from keras.layers import Flatten, Dense, Dropout, InputLayer
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
# Define input shape for your images and batch size
input_shape = (224, 224, 3)
batch_size = 32
# Load pretrained VGG16 model without the top (fully connected) layers
vgg = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
# Freeze all layers of the VGG16 base
for layer in vgg.layers:
layer.trainable = False
# Data augmentation for training set
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True
)
# Data augmentation for validation set (only rescaling)
validation_datagen = ImageDataGenerator(rescale=1./255)
# Compile the model with binary crossentropy loss and RMSprop optimizer
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=1e-4),
metrics=['accuracy'])
# Assuming you have loaded your test data and labels using an ImageDataGenerator
test_generator = test_datagen.flow_from_directory(
'path_to_test_directory',
target_size=(224, 224),
batch_size=batch_size,
class_mode='binary',
shuffle=False
)