Project: Image Classifier
This project puts your CNN knowledge to work. You will train an image classifier on a small dataset like 1,000 cat vs dog images, using augmentation to avoid overfitting.
You will see how data augmentation artificially expands data by flipping and rotating. With transfer learning you can get 90% accuracy in minutes on Colab.
The final step is to test on images the model has never seen and to handle wrong predictions gracefully.
Augmentation and transfer
Augmentation expands data, transfer learning reuses pretrained weights. Together they make small data work.
# ImageDataGenerator(rotation_range=20, horizontal_flip=True)
# base = EfficientNetB0(weights="imagenet", include_top=False)
Test on new images
Always test on held-out images. Save some for final demo that were never used in training or validation.
# Keep test set untouched until final
# X_train, X_temp, y_train, y_temp = train_test_split(X, y, test_size=0.3)
# X_val, X_test, ... = train_test_split(X_temp, ..., test_size=0.5)
Syntax
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rotation_range=20, horizontal_flip=True)
# datagen.flow(X_train, y_train, batch_size=32)
Overfitting with 100% train accuracy
If train accuracy is 100% but val is 60%, you are memorizing. Add augmentation, dropout, or get more data. This r/learnmachinelearning post with 1k upvotes explains the fix.
# Add Dropout(0.5) and Data Augmentation
# model.add(Dropout(0.5))
Good to know
- Augmentation prevents overfitting on small data.
- Transfer learning saves hours.
- Keep a final test set untouched.