Two small linear maps, trained on a few thousand (caption, frame) pairs of one camera, teach a frozen image-text model the words of that camera.
A frozen image-text model learned its English from the web. A camera has its own words: bear codes, ship names, the names of places. We do not change the model. We add one linear map for text and one for images, and we train only the two maps on the camera's own (caption, frame) pairs.
This is the full method. Each map is a residual linear layer that starts at zero, so the maps start as the frozen encoder. The maps are a small linear adapter on frozen embeddings, in the family of CLIP-Adapter. They are not a new kind of model.
class TwoMaps(torch.nn.Module):
"""phi(x) = normalize(x + W x + b). W and b start at zero, so training starts at the frozen encoder."""
def __init__(self, dim):
super().__init__()
self.text_map = torch.nn.Linear(dim, dim)
self.image_map = torch.nn.Linear(dim, dim)
for layer in (self.text_map, self.image_map):
torch.nn.init.zeros_(layer.weight)
torch.nn.init.zeros_(layer.bias)
def text(self, x):
return torch.nn.functional.normalize(x + self.text_map(x), dim=-1)
def image(self, x):
return torch.nn.functional.normalize(x + self.image_map(x), dim=-1)
def contrastive_loss(text, image, temperature=0.05):
"""text, image: (batch, dim). Row i of text matches row i of image."""
logits = text @ image.T / temperature
target = torch.arange(len(text), device=text.device)
return 0.5 * (torch.nn.functional.cross_entropy(logits, target) + torch.nn.functional.cross_entropy(logits.T, target))
Viewers take a snapshot of the live camera and write a short caption. They use a short form of their own: a register. The model learned its English from the web, and the web does not use these words in this way.
The camera tag: Brooks Falls.
Bear codes from the park. "806s" is a young bear of bear 806.
Nicknames. Gully is bear 903. Chunk is bear 32.
Places at the falls.
A bear stays near a bear that has a fish.
A bear eats only the best parts of a salmon.
A bear puts its face in the water to look for fish. The four frames below show it.
Each frame below has one bear, and a viewer named that bear. We ask the model which part of the frame matches the code or the nickname. The frames are a random sample of held-out frames.
Choose a word. The model ranks the frames of 16 days that the maps did not train on. A green border shows that a viewer used the word, or a word of the same family, for that frame.
The maps help most with words of the bear cam, such as "begging" and "sparring". Plain words, such as "sleeping", work well already. Some words lose a little, such as "playing" and "shaking". AUC 0.5 is chance.
Two fixed underwater cameras watch a reef. Viewers caption what they see, often with the name of a fish. A live search page answers your phrase in three ways. It uses the frozen model, the two maps, and the two maps over sets of phrases and patches.
| Camera (held-out frames) | Frozen | Two maps | Two maps over sets |
|---|---|---|---|
| Coral cam (983) | 0.06 / 0.18 | 0.23 / 0.49 | 0.24 / 0.53 |
| Dock cam (954) | 0.05 / 0.19 | 0.26 / 0.59 | 0.29 / 0.60 |
For 14 years, the Duluth Harbor Cam blog posted one or two sentences for each ship that came or went. An example: "Polstream Dabie arrived in Duluth this morning to load grain." Each post has a frame from a harbor camera. The posts are a ship log of the harbor. We use 5,077 posts and their frames from a public repository.
We use full captions as queries on 742 test frames. R@10 is . The two maps are better for full captions.
The set maps are the same two maps, trained with the set score of part 5 of the notebook. They are better for single words: .
Caution: names on the screen. Many frames carry the ship name as text on the image. In 60 random frames, we counted 18 (about 1 in 3) with the name as an overlay. The maps can learn to read this text. The set maps can match a ship name to the patch that shows it. Part of their gain on ship names can come from the overlay. Thus we test ship names apart from cargo and place words, which the overlay does not show.
The code has three short files, embed.py, maps.py and evaluate.py, and a fourth file, sets.py, for the maps over phrases and patches. The notebook runs the bear cam and the Duluth cam from start to end, and it has one function for your own pairs.