7 CNNWithCustomImage
7 CNNWithCustomImage
7 CNNWithCustomImage
Image
• https://drive.google.com/file/d/1B226Ib0lGV9gdP2
m7XUukEDhyNDfdS0u/view?usp=drive_link
• import zipfile
• # Path to the zip file
• zip_path = '/content/CATS_DOGS.zip'
print(images.shape)
print(labels[:5])
plt.imshow(images[0])
Testing datasets
test_img_gen = ImageDataGenerator(rescale=1/255)
test_image_gen =
test_img_gen.flow_from_directory(os.path.join(extract_path,'CATS_DOGS/test'),
target_size=(150,150), class_mode='binary' )
print(images.shape)
print(labels[:2])
print(test_image_gen.class_indices)
Model Structure
•Third Conv2D Layer:
• Input Layer • Filters: 64
• Conv2D Layer: • Kernel Size: (3, 3)
• Activation: ReLU
• Filters: 32 •MaxPooling2D: Pool size of (2, 2)
• Kernel Size: (3, 3) Flatten Layer
• Activation: ReLU •Flattens the 3D output into a 1D vector
• Input Shape: (150, 150, 3) to feed into the Dense layers.
• MaxPooling2D: Pool size of (2, 2) Fully Connected Layers
•Dense Layer: 128 units with ReLU
• Hidden Layers activation
• Second Conv2D Layer: •Dropout: 50% dropout rate to reduce
• Filters: 64 overfitting
•Final Dense Layer: 1 unit with Sigmoid
• Kernel Size: (3, 3)
activation for binary classification
• Activation: ReLU
• MaxPooling2D: Pool size of (2, 2)
Summary Details
1.Conv2D (Convolutional Layers)
1. conv2d: The first convolutional layer has 32 filters, each of size (3x3). The input shape is (150,
150, 3), and the output shape after this layer is (148, 148, 32).
2. conv2d_1: The second convolutional layer has 64 filters, with an output shape of (72, 72, 64). The
number of parameters is 18,496.
3. conv2d_2: The third convolutional layer also has 64 filters, with an output shape of (34, 34, 64)
and 36,928 parameters.
2.Parameters Calculation:
1. For a Conv2D layer, the number of parameters is calculated as:
(filter width x filter height x number of input channels + 1 for bias)×number of filters\text{(filter
width x filter height x number of input channels + 1 for bias)} \times \text{number of filters}
(filter width x filter height x number of input channels + 1 for bias)×number of filters
2. Example for conv2d: (3×3×3+1)×32=896
3.MaxPooling2D (Pooling Layers)
1. These layers downsample the spatial dimensions of the feature maps, reducing the width and
height but retaining the depth.
2. max_pooling2d: Reduces the output shape to (74, 74, 32).
3. max_pooling2d_1: Further reduces to (36, 36, 64).
4. max_pooling2d_2: Reduces to (17, 17, 64).
5. Pooling layers do not have any trainable parameters, hence the parameter count is 0.
4.Flatten Layer
1. The flatten layer converts the 3D output from the last pooling layer into a 1D vector with 18,496
elements. This vector is then fed into the dense (fully connected) layers.
Summary details
1. Dense (Fully Connected Layers)
1. dense: The first fully connected layer has 128 units and 2,367,616 parameters.
2. activation: Applies the ReLU activation function to the dense layer.
3. dropout: A regularization technique that randomly drops 50% of the neurons during training to reduce overfitting.
It does not have parameters.
4. dense_1: The output layer with 1 unit (for binary classification) and 129 parameters. The sigmoid activation
function is used to output a probability score.
2. Parameters Calculation for Dense Layer:
1. For the dense layer: 18,496×128+128=2,367,616
2. For the dense_1 layer: 128×1+1=129
• Total Parameters: 2,424,065 parameters. These are the total number of weights and biases that will
be updated during training.
• Trainable Parameters: All 2,424,065 parameters are trainable, meaning they will be adjusted during
training.
• Non-trainable Parameters: There are no non-trainable parameters in this model.
• Output Shape
• The output shape of each layer describes the dimensions of the data as it flows through the model. The
shape starts from (150, 150, 3) and progressively reduces through convolution and pooling layers until
it becomes a 1D vector, which is then fed into fully connected layers.
• Memory Usage
• The model's total memory usage is approximately 9.25 MB, which includes all the parameters and their
gradients during backpropagation.
Fitting modeling and saving
• model_fp = 'custom_image_cnn_model.h5’
• from keras.src.saving import load_model
if not os.path.exists(model_fp):
model = build_model()
history = model.fit(
train_image_gen,
epochs=10, # You can adjust the number of epochs
validation_data=validation_image_gen
)
# Save the trained model
model.save(model_fp)
else:
model = load_model(model_fp)
evaluation
• from sklearn.metrics import confusion_matrix, classification_report