Image models

Image models

Upload a convolutional network, explain photos with Grad-CAM, occlusion and LIME, and see what each layer responds to.

An image feature is {"name": "photo", "type": "image", "height": 224, "width": 224, "channels": 3}. Inputs are nested height × width × channels lists (or a flat list), channels last, pixels in [0, 1] unless the feature declares value_range: [0, 255]. The Python client accepts NumPy arrays anywhere an input goes, and kaimei.images.load_image prepares a photo exactly as the dashboard does.

  • Uploaded CNNs run in the API: convolutions with any stride, padding, dilation and groups, pooling, batch norm, residual adds, concatenation, squeeze-excitation and the usual activations. The client converts PyTorch (traced with torch.fx) and Keras models; see uploading.
  • Remote image endpoints receive nested lists by default; image_encoding, image_layout and image_normalize on inference shape the payload.
  • Methods: occlusion and lime work for every image model and need no reference data; gradcam needs an uploaded structure with a spatial feature map. options.target_class asks why cat? on a photo the model calls dog.
  • Results carry a heatmap (at most 64 cells a side), the regions the attributions refer to (superpixels for photos, a grid for thumbnails), the focus region holding half of the evidence, an evidence check (the target probability for the full photo, with the focus hidden, with only the focus kept) and a plain-English summary. The explanation object lists every field.
  • What the layers see: POST …/structure/activations returns each layer's feature maps, saliency and strongest channels for one input, with optional class attention per layer.
import torchvision
from kaimei.images import load_image, plot_explanation

net = torchvision.models.mobilenet_v2(num_classes=2).eval()   # your fine-tuned weights
model = client.models.upload(
    net, name="Cats and dogs", task="binary_classification",
    outputs={"labels": ["cat", "dog"]}, input_shape=(3, 224, 224),
    preprocess={"mean": [0.485, 0.456, 0.406], "std": [0.229, 0.224, 0.225]},
)

photo = load_image("beagle.jpg", model=model)
for method in ("gradcam", "occlusion", "lime"):
    explanation = client.explanations.create(model=model.slug, input={"photo": photo}, method=method)
    print(method, explanation.options["summary"])
plot_explanation(photo, explanation)
# a 16×16 RGB thumbnail for the built-in cats-dogs demo, pixels in [0, 1]
curl https://api.kaimei.ai/v1/explanations \
  -H "Authorization: Bearer $KAIMEI_API_KEY" -H "Content-Type: application/json" \
  -d '{"model": "cats-dogs-v1", "method": "gradcam",
       "input": {"image": [[[0.13, 0.85, 0.76], [0.26, 0.5, 0.45], "..."], "..."]},
       "options": {"target_class": "dog"}}'