Download predict.py from jinbo2277/HybridRSC: direct link, hf CLI and curl.
- Browser
- Download file 1.35 kB
-
https://huggingface.co/jinbo2277/HybridRSC/resolve/main/predict.py
- Command line
-
hf download hf://jinbo2277/HybridRSC/predict.py
-
curl -L -o predict.py https://huggingface.co/jinbo2277/HybridRSC/resolve/main/predict.py
1.35 kB
| """Predict one RGB road patch with a selected paper checkpoint.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import torch | |
| from PIL import Image | |
| from inference.loading import load_checkpoint | |
| from inference.transforms import ThreeViewTransform | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("image", type=Path) | |
| parser.add_argument("--checkpoint", required=True, type=Path) | |
| parser.add_argument("--device", default="cuda" if torch.cuda.is_available() else "cpu") | |
| parser.add_argument("--topk", default=5, type=int) | |
| args = parser.parse_args() | |
| if not 1 <= args.topk <= 27: | |
| parser.error("--topk must be between 1 and 27") | |
| torch.backends.cudnn.allow_tf32 = False | |
| torch.backends.cuda.matmul.allow_tf32 = False | |
| model = load_checkpoint(args.checkpoint, args.device) | |
| with Image.open(args.image) as image: | |
| crops = ThreeViewTransform()(image).to(args.device) | |
| with torch.inference_mode(): | |
| probabilities = model(crops).softmax(-1).mean(0) | |
| labels = json.loads((Path(__file__).parent / "labels.json").read_text())["id2label"] | |
| values, indices = probabilities.topk(args.topk) | |
| for index, value in zip(indices.tolist(), values.tolist()): | |
| print(f"{labels[str(index)]}: {value:.6f}") | |
| if __name__ == "__main__": | |
| main() | |