Lecture-25 - Building - Training CNN
Lecture-25 - Building - Training CNN
The x_train and x_test tensors are typically images, and the
channels dimension represents the number of color channels in the
image. In this case, the images are grayscale, so there is only
one channel. What if 1 is not given? It will assume the model in
three colors as Red Green and Blue.
The print() statements in your code will print the shapes of the
x_train, y_train, x_test, and y_test tensors. The shapes of the
Prepared by: Tahir Mahmood 0321-5111997, What up 0312-8536439
13
With these modules imported, you can proceed to build your CNN
architecture using Keras. You'll define a Sequential model, add
convolutional, pooling, and dense layers, and then compile and
train the model using your data.
# classes=10
# Define the dimensions of the input image
img_rows, img_cols, channels = 28, 28, 1 # 1 for greyscale
images and 3 for rgb images
1. Image Dimensions:
img_rows and img_cols define the dimensions of the input
images. These are set to 28x28, which is common for MNIST
dataset-like images.
channels specifies the number of color channels in the images.
For grayscale images, this is 1, and for RGB images, this
would be 3.
2. Number of Filters:
filters is a list that defines the number of filters for each
layer of the CNN. The first layer has 6 filters, the second
has 32 filters, the third has 80 filters, and the fourth has
120 filters.
Filters in CNNs are responsible for extracting different
features from the input data. The number of filters determines
the complexity and capacity of the network to learn various
features.
3. Number of Classes:
classes specifies the number of different categories (or
classes) that the CNN will classify images into. In this case,
1. Sequential Model:
model=Sequential() initializes a sequential container to store
layers sequentially.
2. Convolutional and MaxPooling Layers:
Conv2D(filters[0], (3,3), padding='same', activation='relu',
input_shape=(img_rows, img_cols, channels)) creates a
convolutional layer with 6 filters, a kernel size of (3,3),
Pooling Layer
It trains the CNN model on the x_train and y_train datasets and
evaluates the model on the x_test and y_test datasets.
The fit() function takes four arguments: the training data, the
labels, the validation split, the number of epochs, and the batch
size. The validation split is the fraction of the training data
that is used for validation. The number of epochs is the number
of times the model will be trained on the entire training data.
The batch size is the number of images that are processed at a
time.
The evaluate() function takes two arguments: the test data and
the labels. The function returns the loss and accuracy of the
model on the test data.
Prepared by: Tahir Mahmood 0321-5111997, What up 0312-8536439
20
1.
Import Libraries:
Also, ensure that x_test and y_test are properly prepared and
preprocessed before using them for prediction and evaluation.
Make sure that the shapes and formats of these arrays match the
requirements of your model.
1. Define Mask:
mask = range(20, 50): Defines a mask using the range function
to select indices from 20 to 49.
2. Select Validation Samples:
X_valid = x_test[20:40]: Uses slicing to select the first 20
samples from the test set (indices 20 to 39) for
visualization.
3. Select Actual Labels:
actual_labels = y_test[20:40]: Selects the corresponding
actual labels for the selected validation samples.
4. Predict and Threshold:
y_pred_probs_valid = model.predict(X_valid): Predicts
probabilities for the selected validation samples.
y_pred_valid = np.where(y_pred_probs_valid > 0.5, 1, 0):
Converts the predicted probabilities into binary predictions
using a threshold of 0.5.
for i in range(n):
# Display the original image
ax = plt.subplot(2, n, i + 1)
plt.imshow(X_valid[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
1. Set Up Figure:
n = len(X_valid): n is set to the number of samples in your
validation subset.
plt.figure(figsize=(20, 4)): Sets up a figure with a specified
size to display the images.
2. Display Original Images:
A loop iterates through the range of n.
ax = plt.subplot(2, n, i + 1): Creates a subplot to display
the original image.
plt.imshow(X_valid[i].reshape(28, 28)): Displays the original
image, reshaped to 28x28.
plt.gray(): Sets the colormap to grayscale.
ax.get_xaxis().set_visible(False) and
ax.get_yaxis().set_visible(False): Hides the axes for better
visualization.
3. Display Predicted Digits:
Another loop iterates through the range of n.
predicted_digit = np.argmax(y_pred_probs_valid[i]): Determines
the predicted digit by finding the index of the maximum value
in the predicted probabilities.
ax = plt.subplot(2, n, i + 1 + n): Creates a subplot to
display the predicted digit.
plt.text(0.5, 0.5, str(predicted_digit), fontsize=12,
ha='center', va='center'): Displays the predicted digit as
text in the center of the subplot.
plt.axis('off'): Turns off the axis for this subplot.
4. Show and Close Plot:
plt.show(): Displays the entire plot with the original images
and predicted digits.
plt.close(): Closes the plot.
This code will help you visualize how well your model is
predicting the digits for a subset of validation samples. The top
row shows the original images, and the bottom row displays the
predicted digits.
QUIZ
1