For an in depth guide to running leap-ie that you can run yourself, visit the Google Colab notebook below:
leap_ie_demo.ipynb:
Prototype Generation
Given your model, we generate prototypes and entanglements We also isolate entangled features in your prototypes.
from leap_ie.vision import enginefrom leap_ie.vision.models import get_modelconfig ={"leap_api_key":"YOUR_LEAP_API_KEY"}# Replace this model with your own, or explore any imagenet classifier from torchvision (https://pytorch.org/vision/stable/models.html).
preprocessing_fn, model, class_list =get_model("torchvision.resnet18")# indexes of classes to generate prototypes for. In this case, ['tench', 'goldfish', 'great white shark'].target_classes = [0,1,2]# generate prototypesdf_results, dict_results = engine.generate( project_name="resnet18", model=model, class_list=class_list, config=config, target_classes=target_classes, preprocessing=preprocessing_fn, samples=None, device=None, mode="pt",)# For the best experience, head to https://app.leap-labs.com/ to explore your prototypes and feature isolations in the browser!
# Or, if you're in a jupyter notebook, you can display your results inline:engine.display_df(df_results)
Sample Feature Isolation
Given some input image, we can show you which features your model thinks belong to each class. If you specify target classes, we'll isolate features for those, or if not, we'll isolate features for the three highest probability classes.
from torchvision import transformsfrom leap_ie.vision import enginefrom leap_ie.vision.models import get_modelfrom PIL import Imageconfig ={"leap_api_key":"YOUR_LEAP_API_KEY"}# Replace this model with your own, or explore any imagenet classifier from torchvision (https://pytorch.org/vision/stable/models.html).
preprocessing_fn, model, class_list =get_model("torchvision.resnet18")# load an imageimage_path ="tools.jpeg"tt = transforms.ToTensor()image = preprocessing_fn[0](tt(Image.open(image_path)).unsqueeze(0))# to isolate features:df_results, dict_results = engine.generate( project_name="resnet18", model=model, class_list=class_list, config=config, target_classes=None, preprocessing=preprocessing_fn, samples=image, mode="pt",)# For the best experience, head to https://app.leap-labs.com/ to explore your prototypes and feature isolations in the browser!
# Or, if you're in a jupyter notebook, you can display your results inline:engine.display_df(df_results)