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
Release EndoMet-Net: GLENDA endometriosis vs no-pathology, ConvNeXt-T 3-fold ensemble
bf15ce2 verified Download inference.py from MABERAMABERA/EndoMet-Net: direct link, hf CLI and curl.
- Browser
- Download file 1.79 kB
-
https://huggingface.co/MABERAMABERA/EndoMet-Net/resolve/main/inference.py
- Command line
-
hf download hf://MABERAMABERA/EndoMet-Net/inference.py
-
curl -L -o inference.py https://huggingface.co/MABERAMABERA/EndoMet-Net/resolve/main/inference.py
1.79 kB
| """ | |
| 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"]), | |
| ]) | |
| 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})") | |