Binary image classifier (EfficientNet-B0, ~5M params) trained to
distinguish queen bees from worker bees on cropped bee images.
Built as part of
Apiarist,
an offline AI hive inspector for backyard beekeepers, made for the
Build Small Hackathon.
Multi-class YOLO detectors fight two problems at once (localize + classify)
and queens lose because they're rare and visually subtle. A focused
binary classifier on cropped bee images is the right architecture:
small, fast, trained specifically for one decision.
Pair with a bee detector (e.g. YOLOv8). Run the detector first, then
classify each cropped bee through this model. Threshold queen
probability at 0.85 for high-precision flagging.
1import torch, timm
2from torchvision import transforms
3
4ckpt = torch.load("queen_classifier.pt", map_location="cpu")
5model = timm.create_model(ckpt["arch"], pretrained=False, num_classes=2)
6model.load_state_dict(ckpt["state_dict"])
7model.eval()
8
9tf = transforms.Compose([
10 transforms.Resize((224, 224)),
11 transforms.ToTensor(),
12 transforms.Normalize([0.485,0.456,0.406], [0.229,0.224,0.225]),
13])
14
15with torch.no_grad():
16 probs = torch.softmax(model(tf(crop).unsqueeze(0)), dim=1)
17queen_idx = ckpt["class_to_idx"]["queen"]
18queen_prob = probs[0, queen_idx].item()
The training distribution leans toward close-up macro photos of bees on
honeycomb. Generalization to wide-angle inspection photos (with hands /
background visible) is weaker, since YOLO's bee bounding boxes on those
photos are often smaller and less precise than the training crops.
Apache 2.0. Trained on data released under CC BY 4.0.