Jigsaw Puzzle Pretraining
Cut a photo into nine tiles, shuffle them, and ask the network to put them back. To do it well it must learn what parts belong together.
Why Does This Exist?
Labels are dear. A 3 by 3 jigsaw gives free supervision: shuffle nine tiles from one photo and the correct order is known by construction. A plain classifier trained on image classification needs names for everything. Here the puzzle itself is the teacher, so millions of unlabeled photos become training data.
This page covers tile shuffling and order prediction only. Pairwise similarity training lives in siamese networks and the loss used there in contrastive loss.
The one idea you need first.
A pretext task is a made-up puzzle whose answer you already know. Solve many puzzles and the backbone picks up shapes, textures, and part relations you later reuse for detection or segmentation.
Think of It Like This
A toddler with a cut-up postcard
You cut a postcard into nine squares and mix them on the table. The child never saw the scene, yet after a few tries she puts sky on top and grass below. She learned that blue patches sit above green ones and that a roof edge continues across a cut.
The network is that child. Tiles are the squares, the permutation index is the answer key, and the reward is getting the layout right. Where the analogy stops: the child remembers one card, while the net must find features that work across millions of photos.
How It Actually Works
Take image . Cut it into a grid to get tiles through . Pick a permutation from a fixed set of size , shuffle, and feed all nine tiles through one shared encoder . Concatenate the nine features and train a classifier to predict which was used with cross entropy.
In plain words: is the shared tile reader, is the shuffle index, and is the shortlist of allowed shuffles. A shortlist matters because orders are far too many to classify.
Why 100 permutations
The set is picked so shuffles differ by large tile moves. Near-identical orders would make the label noisy. Hamming distance between kept orders stays high, so each class looks distinct.
Worked example
Say tile 5 (the center) lands at top left and the true label is . The head outputs scores with and the next best . Softmax gives , , plus small terms summing to . Total . Probability of the right order is . Loss is . That number falls only when reads edges and corners that span tiles.
Code
This snippet builds the fixed permutation set and checks the minimum separation, which is the detail that decides whether the task teaches layout or noise.
import itertoolsimport numpy as np
def hamming(a: np.ndarray, b: np.ndarray) -> int: return int((a != b).sum()) # positions that differ
def build_set(n: int = 100, seed: int = 0) -> np.ndarray: rng = np.random.default_rng(seed) cands = [np.array(p) for p in itertools.permutations(range(9))] rng.shuffle(cands) # random order, then greedily keep distant ones kept: list[np.ndarray] = [cands[0]] for c in cands[1:]: if all(hamming(c, k) >= 4 for k in kept): kept.append(c) if len(kept) == n: break return np.stack(kept)
pset = build_set()print(pset.shape) # -> (100, 9)print(int(min(hamming(a, b) for i, a in enumerate(pset) for b in pset[i + 1:]))) # -> 4The kept set holds orders and every pair differs in at least tile positions.
Watch Out For
Letting the net cheat on edge statistics
Tiles cut with no gap share exact edge pixels, so the net matches lines pixel by pixel instead of learning objects. The symptom is fast puzzle accuracy with weak transfer to detection. Leave a small gap between tiles and jitter each tile by a few pixels so only real part relations solve the puzzle.
Using all 362880 orders as classes
Nine tiles admit orders, and most look alike. Training on all of them turns the label into noise and accuracy sits near zero. The fix is the standard one: keep about well-separated orders and treat the puzzle as 100-way classification.
The Quick Version
- Jigsaw pretraining shuffles nine tiles and predicts the shuffle index from a fixed set of 100 orders.
- The shared encoder must read part layout, edges, and texture to score above chance.
- Gaps and jitter stop cheating on exact edge pixels.
- The backbone transfers to detection and segmentation without manual labels.
- Keep orders well separated or the label becomes noise.