Image Classification
timm
endomet-net
medical-imaging
endometriosis
laparoscopy
surgery
convnext
research-only
Instructions to use MABERAMABERA/EndoMet-Net with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- timm
How to use MABERAMABERA/EndoMet-Net with timm:
import timm model = timm.create_model("hf-hub:MABERAMABERA/EndoMet-Net", pretrained=True) - Notebooks
- Google Colab
- Kaggle
File size: 1,785 Bytes
bf15ce2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """
EndoMet-Net: endometriosis vs no-pathology classifier for laparoscopic frames (GLENDA v1.0).
Research use only. Not a medical device. See README.md for limitations and license.
Usage:
python inference.py path/to/frame.jpg [more frames ...]
"""
import json, os, sys
import torch, timm
from PIL import Image
from safetensors.torch import load_file
from torchvision.transforms import v2 as T
HERE = os.path.dirname(os.path.abspath(__file__))
CFG = json.load(open(os.path.join(HERE, "config.json")))
def load_models(device="cpu"):
models = []
for fname in CFG["weights"]:
m = timm.create_model(CFG["architecture"], pretrained=False, num_classes=len(CFG["classes"]))
m.load_state_dict(load_file(os.path.join(HERE, fname)))
models.append(m.eval().to(device))
return models
TRANSFORM = T.Compose([
T.ToImage(),
T.Resize((CFG["input_size"], CFG["input_size"]), antialias=True),
T.ToDtype(torch.float32, scale=True),
T.Normalize(CFG["mean"], CFG["std"]),
])
@torch.no_grad()
def predict(models, image, device="cpu"):
"""Returns {class_name: probability}, averaged over the fold models and a horizontal flip."""
x = TRANSFORM(image.convert("RGB")).unsqueeze(0).to(device)
probs = 0
for m in models:
logits = (m(x) + m(torch.flip(x, dims=[3]))) / 2
probs = probs + logits.softmax(1)
probs = (probs / len(models))[0].cpu().tolist()
return dict(zip(CFG["classes"], probs))
if __name__ == "__main__":
device = "cuda" if torch.cuda.is_available() else "cpu"
models = load_models(device)
for path in sys.argv[1:]:
p = predict(models, Image.open(path), device)
label = max(p, key=p.get)
print(f"{path}: {label} (P(endometriosis) = {p['endometriosis']:.3f})")
|