Computer Vision Activity
Computer Vision Activity
Classification
Under the Guidance of Sir Abdul Rafay
Submitted by Muhammad Hamza
cifar10.load_data() loads the CIFAR-10 dataset, splitting it into training and test sets.
We'll use Matplotlib for visualization, Keras for building the CNN, and scikit-learn for evaluating the model.
x_train and y_train contain the training images and labels, respectively.
x_test and y_test contain the test images and labels.
3. Dataset Shape
Let's check the shape of the data arrays:
x_train.shape
# Output: (50000, 32, 32, 3)
x_test.shape
# Output: (10000, 32, 32, 3)
The shape (50000, 32, 32, 3) indicates 50,000 training images, each with dimensions 32x32 pixels and 3 color channels
(RGB).
Similarly, the shape (10000, 32, 32, 3) corresponds to the test set.
In [3]: x_train.shape
In [4]: x_test.shape
single_img = x_train[34]
plt.imshow(single_img)
In [5]: single_img = x_train[34]
In [6]: #single_img
In [7]: plt.imshow(single_img)
In [8]: y_train
Out[8]: array([[6],
[9],
[9],
...,
[9],
[1],
[1]], dtype=uint8)
In [9]: y_test
Out[9]: array([[3],
[8],
[8],
...,
[5],
[1],
[7]], dtype=uint8)
In [10]: y_train.shape
Out[10]: (50000, 1)
5. Label Encoding
One-Hot Encoding
The labels in the CIFAR-10 dataset represent different classes (e.g., airplane, car, bird, etc.). To prepare them for training a neural
network, we perform one-hot encoding. This means converting the class labels into binary vectors.
x_train and x_test now have pixel values in the range [0, 1].
Now let's dive into the details of building a Convolutional Neural Network (CNN) for classifying images from the CIFAR-10 dataset.
Here's a breakdown of each step:
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
#Output layer
model.add(Dense(10, activation='softmax'))
C:\Users\PMLS\miniconda3\envs\ML_Lab\Lib\site-packages\keras\src\layers\convolutional\base_conv.py:99: UserWarning: Do no
t pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` obj
ect as the first layer in the model instead.
super().__init__(
In [16]: model.compile(loss='categorical_crossentropy',
optimizer= 'adam',
metrics= ['accuracy'])
9. Model Summary
Here's an overview of the model architecture:
model.summary()
In [17]: model.summary()
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type) ┃ Output Shape ┃ Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ conv2d (Conv2D) │ (None, 30, 30, 32) │ 896 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ max_pooling2d (MaxPooling2D) │ (None, 15, 15, 32) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ flatten (Flatten) │ (None, 7200) │ 0 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense (Dense) │ (None, 128) │ 921,728 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_1 (Dense) │ (None, 10) │ 1,290 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
Total params: 923,914 (3.52 MB)
Trainable params: 923,914 (3.52 MB)
Non-trainable params: 0 (0.00 B)
model.metrics_names
# Output: ['loss', 'compile_metrics']
In [19]: model.metrics_names
model.evaluate(x_test, y_cat_test)
# Output: [test_loss, test_accuracy]
The test_loss represents the loss on the test set, and test_accuracy is the accuracy achieved.
predictions = model.predict(x_test)
In [24]: print(report)
Conclusion
In this Class activity, we explored various aspects of building a Convolutional Neural Network (CNN) for classifying images from the
CIFAR-10 dataset. Let's summarize our findings:
We loaded the CIFAR-10 dataset using Keras, obtaining separate training and test sets.
Labels were one-hot encoded, and pixel values were normalized to the range [0, 1].
2. Model Architecture:
We compiled the model with categorical cross-entropy loss and the Adam optimizer.
Training was performed for 10 epochs.
The model achieved an accuracy of approximately 65% on the test set.
4. Classification Report:
We generated a classification report, including precision, recall, F1-score, and support for each class.
Further fine-tuning, hyperparameter optimization, and data augmentation can enhance model performance. Feel free to explore
additional techniques and continue experimenting with CNNs!