For image classification using CNNs, accuracy is the most common metric because it shows how many images are correctly labeled out of all images. However, when classes are imbalanced, precision and recall become important to understand if the model is good at finding specific classes without too many mistakes.
Why CNNs dominate image classification in Computer Vision - Why Metrics Matter
Start learning this pattern below
Jump into concepts and practice - no test required
| Predicted Cat | Predicted Dog |
|--------------|---------------|
| True Cat: 90 | False Dog: 10 |
| False Cat: 5 | True Dog: 95 |
Total samples = 200
Precision (Cat) = TP / (TP + FP) = 90 / (90 + 5) = 0.947
Recall (Cat) = TP / (TP + FN) = 90 / (90 + 10) = 0.9
Accuracy = (TP + TN) / Total = (90 + 95) / 200 = 0.925
In image classification, sometimes you want high precision to avoid false alarms. For example, if a model detects rare animals, you want to be sure when it says "this is a rare animal" it is correct (high precision).
Other times, you want high recall to catch all instances. For example, in medical image classification, you want to find all tumors even if some false alarms happen (high recall).
CNNs help balance this tradeoff by learning detailed features that improve both precision and recall compared to older methods.
Good: Accuracy above 90%, precision and recall both above 85% means the CNN is correctly classifying most images and not missing many.
Bad: Accuracy around 50-60% or precision very low (below 50%) means the model guesses poorly or confuses classes a lot.
- Accuracy paradox: High accuracy can be misleading if one class dominates the dataset.
- Data leakage: If test images are too similar to training images, metrics look better than reality.
- Overfitting: Very high training accuracy but low test accuracy means the CNN memorized training images but can't generalize.
Your CNN model has 98% accuracy but only 12% recall on a rare class like cancer in images. Is it good for production? Why or why not?
Answer: No, it is not good. The model misses 88% of cancer cases (low recall), which is dangerous. High accuracy is misleading because cancer cases are rare. You need to improve recall to catch more cancer cases.
Practice
Solution
Step 1: Understand CNN scanning method
CNNs look at small parts of an image called patches to detect patterns like edges or shapes.Step 2: Connect scanning to image classification
By scanning patches, CNNs learn important features that help tell one image from another.Final Answer:
Because they scan small parts of images to find important patterns -> Option DQuick Check:
CNN scanning = small parts pattern detection [OK]
- Thinking CNNs guess randomly
- Believing CNNs ignore image details
- Assuming CNNs only work on black and white images
Solution
Step 1: Define pooling in CNNs
Pooling reduces the size of the image or feature map but keeps the key features intact.Step 2: Identify correct description
Pooling does not increase size or remove colors; it shrinks the image while preserving important info.Final Answer:
Pooling shrinks the image while keeping important information -> Option BQuick Check:
Pooling = shrink + keep key info [OK]
- Thinking pooling makes images bigger
- Believing pooling removes colors
- Assuming pooling changes pixels randomly
import torch import torch.nn as nn conv = nn.Conv2d(in_channels=3, out_channels=1, kernel_size=3) input_tensor = torch.randn(1, 3, 5, 5) output = conv(input_tensor) print(output.shape)
What will be the shape of the output tensor?
Solution
Step 1: Understand Conv2d output size formula
Output size = (Input size - Kernel size + 1) for default stride and padding. Here, input is 5x5, kernel is 3x3, so output is 3x3.Step 2: Check channels and batch size
Batch size is 1, output channels is 1, so output shape is (1, 1, 3, 3).Final Answer:
torch.Size([1, 1, 3, 3]) -> Option AQuick Check:
Output shape = (1, 1, 3, 3) [OK]
- Confusing input and output channels
- Forgetting batch size dimension
- Assuming output size equals input size
import torch import torch.nn as nn pool = nn.MaxPool2d(kernel_size=2, stride=3) input_tensor = torch.randn(1, 1, 6, 6) output = pool(input_tensor) print(output.shape)
What is the problem with this code?
Solution
Step 1: Check pooling parameters
Stride can be different from kernel size, but stride larger than kernel size can cause skipping regions and smaller output.Step 2: Understand effect on output size
Stride 3 with kernel 2 on 6x6 input reduces output size more than expected, which may cause loss of important info.Final Answer:
Stride is larger than kernel size, causing unexpected output size -> Option CQuick Check:
Stride > kernel size affects output size [OK]
- Thinking kernel size must equal stride
- Believing input shape is invalid
- Assuming MaxPool2d can't take stride
Solution
Step 1: Compare CNN and fully connected networks
CNNs scan small parts of images (local receptive fields) and use pooling to keep important info while reducing size.Step 2: Understand why CNNs are better for images
Fully connected networks treat all pixels equally without spatial structure, making them less efficient for images.Final Answer:
CNNs scan local image parts and use pooling to reduce size, capturing patterns efficiently -> Option AQuick Check:
CNN local scan + pooling > fully connected for images [OK]
- Confusing fully connected with convolution layers
- Thinking CNNs ignore image patterns
- Believing fully connected networks use pooling
