Update README.md
Browse files123---license: cc-by-nc-sa-4.0---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202---license: cc-by-nc-4.0tags:- ruler-reading- image-scale-estimation- computer-vision- onnx- medical-imaging---# RulerNetRulerNet estimates image scale from a visible ruler by detecting centimetermarks and fitting them with a geometric progression. It is designed to remainrobust when a ruler is rotated, viewed in perspective, or partially occluded.This repository accompanies **“RulerNet: Learning Perspective-Invariant RulerRepresentations for Robust Image Scale Estimation,”** published in*Computerized Medical Imaging and Graphics*.Try the interactive CPU demo: [RulerNet-Demo](https://huggingface.co/spaces/ymp5078/RulerNet-Demo).For training and evaluation code, see the [GitHub repository](https://github.com/ymp5078/RulerNet).## Repository contents| Location | Contents | Use || --- | --- | --- || `data/AnyRuler.zip` | AnyRuler images and centimeter-mark annotations (998 MB) | Training and testing || `data/Rulers2023_scale.zip` | Rulers2023 images and centimeter-mark annotations (2.46 GB) | Evaluation || `weights/final_rulernet.zip` | Final RulerNet checkpoint (131 MB) | PyTorch inference, evaluation, or fine-tuning || `weights/final_deepgp.zip` | DeepGP solver checkpoint (112 MB) | Optional faster geometric-progression solving || `weights/pretrained_rulernet.zip` | Synthetic-data pretrained RulerNet checkpoint (158 MB) | Initialize training to reproduce the paper setup || `model.onnx` | CPU-ready ONNX export (57.1 MB) | Lightweight deployment and inference |## Download filesInstall the Hugging Face Hub client:```bashpip install -U huggingface_hub```Download individual archives with the CLI:```bashhf download ymp5078/RulerNet data/AnyRuler.zip --local-dir .hf download ymp5078/RulerNet data/Rulers2023_scale.zip --local-dir .hf download ymp5078/RulerNet weights/final_rulernet.zip --local-dir .hf download ymp5078/RulerNet weights/final_deepgp.zip --local-dir .hf download ymp5078/RulerNet weights/pretrained_rulernet.zip --local-dir .hf download ymp5078/RulerNet model.onnx --local-dir .```Extract an archive before using it:```bashunzip data/AnyRuler.zip -d data/unzip weights/final_rulernet.zip -d weights/```## Datasets### AnyRuler`AnyRuler.zip` contains 1,416 annotated ruler images. Use it for training orfor evaluating a model with centimeter-mark labels. After extraction, providethe extracted directory to the code repository with `--data-dir`.```text<data-dir>/├── ruler_image/ # input images└── cm_marks/ # matching JSON centimeter-mark annotations```### Rulers2023`Rulers2023_scale.zip` contains the Rulers2023 evaluation images togetherwith centimeter-mark annotations. Use it with `--test-dataset ruler2023`.```text<data-dir>/├── real-test/images/ # evaluation images└── real-test-marks/ # JSON centimeter-mark annotations```## PyTorch checkpointsClone the code repository and install its dependencies before using thecheckpoints:```bashgit clone https://github.com/ymp5078/RulerNet.gitcd RulerNetpip install -r requirements.txt```Use the final RulerNet checkpoint for inference:```bashpython inference.py \--config configs/config_graphic_gen.yaml \--checkpoint <path-to-final_rulernet>/checkpoints/epoch=199-step=20000.ckpt \--img-size 768 768 \--ruler-mode optimize \--image-path <image-or-directory> \--result-dir <output-directory>```Append the following option to the inference or evaluation command to use theoptional learned DeepGP geometric-progression solver:```bash--gp-solver-path <path-to-final_deepgp>/checkpoints/epoch=999-step=1200000.ckpt```To reproduce the pretraining initialization used in the paper, start trainingfrom the checkpoint in `pretrained_rulernet.zip`:```bashpython main.py \--config configs/config_pretrain.yaml \--data-dir <anyruler-data-dir> \--checkpoint <path-to-pretrained_rulernet>/checkpoints/epoch=79-step=128240.ckpt```The synthetic-ruler images used for pretraining are reproducible with[`sdxl_inference.py`](https://github.com/ymp5078/RulerNet/blob/main/sdxl_inference.py);they are not distributed as a separate archive.## ONNX inference`model.onnx` is the CPU-ready export used by the [interactive demo](https://huggingface.co/spaces/ymp5078/RulerNet-Demo).It expects a float32 tensor named `input` with shape **`(1, 3, 768, 768)`**:an RGB image scaled to `[0, 1]`, resized while preserving aspect ratio, andzero-padded to 768 × 768.Install the lightweight runtime:```bashpip install -U huggingface_hub onnxruntime numpy pillow```The following example downloads the model, prepares an image exactly as in thedemo, and runs inference on CPU:```pythonimport numpy as npimport onnxruntime as ortfrom huggingface_hub import hf_hub_downloadfrom PIL import Imagemodel_path = hf_hub_download(repo_id="ymp5078/RulerNet", filename="model.onnx")session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])def preprocess(image_path):image = np.asarray(Image.open(image_path).convert("RGB"), dtype=np.float32) / 255.0height, width = image.shape[:2]scale = min(768 / width, 768 / height)new_width, new_height = int(width * scale), int(height * scale)resized = Image.fromarray((image * 255).astype(np.uint8)).resize((new_width, new_height))canvas = np.zeros((768, 768, 3), dtype=np.float32)top = (768 - new_height) // 2left = (768 - new_width) // 2canvas[top:top + new_height, left:left + new_width] = np.asarray(resized) / 255.0return np.transpose(canvas, (2, 0, 1))[None].astype(np.float32), (scale, top, left)input_tensor, transform = preprocess("ruler.jpg")init_point, dist, ratio, direction, points_info = session.run(None, {"input": input_tensor})print("initial point:", init_point[0])print("base distance:", dist[0])print("geometric-progression ratio:", ratio[0])print("ruler direction:", direction[0])print("point count and bounds:", points_info[0])```The five outputs are:| Output | Meaning || --- | --- || `init_point` | Predicted starting ruler-mark location in the 768 × 768 processed image || `dist` | Base distance between generated marks || `ratio` | Geometric-progression ratio between consecutive mark spacings || `direction` | Unit direction vector along the ruler || `points_info` | Number of generated points followed by `[min_x, min_y, max_x, max_y]` valid bounds |To reconstruct the full set of ruler-mark positions and calculate the medianpixels-per-centimeter value, use the post-processing in the[demo implementation](https://huggingface.co/spaces/ymp5078/RulerNet-Demo/blob/main/app.py).The resulting coordinates are in the padded 768 × 768 image. To map a point`(x, y)` back to the original image, use `(x - left) / scale` and`(y - top) / scale`, where `scale`, `top`, and `left` are returned by`preprocess`.## License and commercial useThis material is licensed under [CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/).For commercial-use licensing inquiries, contact [jwang@ist.psu.edu](mailto:jwang@ist.psu.edu).
README.md+202-3