Week 06 · Image classification and dataset splits
Compare a split by frame with a split by scene.
← Week 5 · Subject contents · Week 7 →
Compare a split by frame with a split by scene.
Work through the problem
Adjacent frames can be nearly identical. If the same scene is in training and test, a model may recognize background or camera noise. A split by scene estimates performance on unseen scenes. A nearest-neighbour baseline makes the shortcut easy to see.
Here each scene has a unique smooth brightness pattern and an arbitrary binary label. Each frame is a noisy copy of its scene. Train on random frames, then repeat while withholding entire scenes. The experiment isolates how a split changes the task being evaluated.
Run the Python lab
import numpy as np
from sklearn.model_selection import GroupShuffleSplit,train_test_split
from sklearn.neighbors import KNeighborsClassifier
rng = np.random.default_rng(4)
group = np.repeat(np.arange(30),8)
patterns = rng.normal(size=(30,25))
X = patterns[group]+rng.normal(0,.03,(len(group),25))
y = rng.integers(0,2,30)[group]
a,b = train_test_split(np.arange(len(group)),test_size=.25,random_state=4)
c,d = next(GroupShuffleSplit(n_splits=1,test_size=.25,random_state=4).split(X,y,group))
scores = []
for name, train, test in [("Frame",a,b),("Scene",c,d)]:
score = KNeighborsClassifier(1).fit(X[train],y[train]).score(X[test],y[test])
scores.append(score)
print(name,"accuracy",round(score,3),"shared scenes",len(set(group[train])&set(group[test])))
assert scores[0]>scores[1]+.1
Output from the lab
Frame accuracy 1.0 shared scenes 25
Scene accuracy 0.484 shared scenes 0
Practice
- Where do augmentations of one source image belong?
Check your reasoning
- Keep every augmentation of a source within the same split as that source.
← Week 5 · Subject contents · Week 7 →
Further reading · Szeliski · Computer Vision: Algorithms and Applications