Skip to content
AI360Xpert
Beta

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.

Jigsaw pretraining shuffles nine tiles and trains the net to recover their order
Jigsaw pretraining shuffles nine tiles and trains the net to recover their order

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 xx. Cut it into a 3×33 \times 3 grid to get tiles t1t_1 through t9t_9. Pick a permutation pp from a fixed set PP of size ∣P∣=100|P| = 100, shuffle, and feed all nine tiles through one shared encoder ff. Concatenate the nine features and train a classifier gg to predict which pp was used with cross entropy.

In plain words: ff is the shared tile reader, pp is the shuffle index, and PP is the shortlist of allowed shuffles. A shortlist matters because 9!=3628809! = 362880 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 p=42p = 42. The head outputs scores zz with z42=3.1z_{42} = 3.1 and the next best z17=1.4z_{17} = 1.4. Softmax gives e3.1=22.20e^{3.1} = 22.20, e1.4=4.06e^{1.4} = 4.06, plus 9898 small terms summing to 6.006.00. Total 32.2632.26. Probability of the right order is 22.20/32.26≈0.68822.20 / 32.26 \approx 0.688. Loss is −log⁡(0.688)≈0.374-\log(0.688) \approx 0.374. That number falls only when ff 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:])))  # -> 4

The kept set holds 100100 orders and every pair differs in at least 44 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 362880362880 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 100100 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.