Run this model with HF Jobs

Run tinylens/image-orientation-base on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/tinylens/image-orientation-base. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/tinylens/image-orientation-base on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
tinylens's picture
Initial release: DINOv2 ViT-B image orientation classifier
c508847 verified
|
Raw History Blame Contribute Delete
7.66 kB
---
license: apache-2.0
pipeline_tag: image-classification
library_name: timm
tags:
- image-orientation
- rotation
- document
- manga
- onnx
base_model: timm/vit_base_patch14_dinov2.lvd142m
metrics:
- accuracy
---
# tinylens/image-orientation-base
Detects whether an image is rotated by 0, 90, 180 or 270 degrees, so you can turn it upright. One model covers **photos, scanned or rendered documents, manga pages and illustrations**. 85.7M parameters (DINOv2 ViT-B/14), 90 MB as int8 ONNX.
- Sibling: [tinylens/image-orientation-small](https://huggingface.co/tinylens/image-orientation-small) (21.6M)
- Demo (runs in your browser, nothing is uploaded): [tinylens/image-orientation](https://huggingface.co/spaces/tinylens/image-orientation)
## Use
The ONNX model takes RGB values in [0, 1], letterboxed to 224x224 (keep the aspect ratio, pad with gray 128/255), and returns probabilities for the classes `['0', '90', '180', '270']`. Class `k` means the image was rotated **clockwise** by `k` degrees; rotate it counter-clockwise by the same angle to fix it. Normalization is inside the model.
```python
import numpy as np, onnxruntime as ort
from huggingface_hub import hf_hub_download
from PIL import Image
sess = ort.InferenceSession(hf_hub_download("tinylens/image-orientation-base", "onnx/model_quantized.onnx"))
S = 224
def letterbox(img):
s = S / max(img.size)
img = img.resize((max(1, round(img.width * s)), max(1, round(img.height * s))), Image.BICUBIC)
out = Image.new("RGB", (S, S), (128, 128, 128))
out.paste(img, ((S - img.width) // 2, (S - img.height) // 2))
return out
img = Image.open("scan.jpg").convert("RGB")
x = (np.asarray(letterbox(img), np.float32) / 255).transpose(2, 0, 1)[None]
probs = sess.run(None, {"pixel_values": x})[0][0]
angle = [0, 90, 180, 270][int(probs.argmax())]
upright = img.rotate(angle, expand=True) # PIL rotates counter-clockwise
```
Files: `onnx/model.onnx` (fp32, 344 MB), `onnx/model_quantized.onnx` (int8, 90 MB), `preprocess.json`. PyTorch: `model.safetensors` + `config.json`, loadable with `timm.create_model("hf-hub:tinylens/image-orientation-base", pretrained=True)` (expects ImageNet mean/std normalization on the letterboxed image; the ONNX model does this for you).
## Evaluation
Accuracy (%) on held-out test sets, each image evaluated at all four rotations. All models were run by us with their own preprocessing on the same images. For the compared models we chose the mapping from their outputs to the four rotations that scored best on our validation set, which can only help them.
- Photos: 5,000 Open Images V7 test images (CC BY 2.0, upright per the dataset's rotation field). Tabletop subset: 382 of them labeled food, dish, tableware, plate or paper, as a proxy for top-down shots.
- Documents: 2,000 DocLayNet test pages.
- Manga: 1,032 pages from 10 Manga109-s titles not used for training.
- Illustrations: 1,597 AI-generated images from DiffusionDB (parts 1991-1992, NSFW score below 0.2).
| Model | Params | License | Photos | Tabletop subset | Documents | Manga | Illustrations | Mean of 4 |
|---|---|---|---|---|---|---|---|---|
| **tinylens/image-orientation-small** | 21.6M | apache-2.0 | 92.94 | 93.72 | 98.52 | 99.49 | 95.27 | 96.56 |
| tinylens/image-orientation-small, int8 ONNX | | | 92.30 | 92.67 | 98.31 | 99.35 | 94.61 | 96.14 |
| **tinylens/image-orientation-base** (this model) | 85.7M | apache-2.0 | 93.97 | 94.70 | 98.52 | 99.73 | 96.07 | 97.07 |
| tinylens/image-orientation-base, int8 ONNX | | | 94.01 | 94.57 | 98.54 | 99.76 | 96.09 | 97.10 |
| [amaye15/Beit-Base-Image-Orientation-Fixer](https://huggingface.co/amaye15/Beit-Base-Image-Orientation-Fixer) (BEiT-base, 384 px) | | apache-2.0 | 91.09 | 92.08 | 88.36 | 8.53 | 89.06 | 69.26 |
| [maxwoe/image-rotation-angle-estimation](https://huggingface.co/maxwoe/image-rotation-angle-estimation) (ConvNeXt V2 Base, angle regression rounded to 90°) | | mit | 89.31 | 91.23 | 60.24 | 55.89 | 87.10 | 73.13 |
| [PaddlePaddle/PP-LCNet_x1_0_doc_ori](https://huggingface.co/PaddlePaddle/PP-LCNet_x1_0_doc_ori) (PP-LCNet x1.0, official ONNX) | | apache-2.0 | 63.45 | 54.78 | 98.44 | 20.28 | 61.88 | 61.01 |
| [Felix92/onnxtr-mobilenet-v3-small-page-orientation](https://huggingface.co/Felix92/onnxtr-mobilenet-v3-small-page-orientation) (MobileNetV3-small) | | apache-2.0 | 37.86 | 34.82 | 93.10 | 2.96 | 30.07 | 41.00 |
| [strakajk/Lynx-Orientation-ResNet10t-176](https://huggingface.co/strakajk/Lynx-Orientation-ResNet10t-176) (ResNet10t, 176 px) | | not stated | 26.66 | 27.03 | 28.94 | 25.61 | 23.11 | 26.08 |
This model is ahead of every compared model in every column above.
The pass criteria were written down before training and the test sets were used once. Most compared models were built for photos or for documents only, so their scores on the other domains are not what they were designed for; they are shown because no single model covered all four. Lynx scored at chance with every output mapping we tried.
CPU speed of this model on our test machine (ONNX Runtime, batches of 64): fp32 78 ms, int8 61 ms per image.
## Training
- Backbone: `timm/vit_base_patch14_dinov2.lvd142m` (Apache-2.0), fine-tuned at 224x224, 30,000 steps of 64, AdamW lr 3e-05, warmup 1,000 steps, label smoothing 0.1, each training image shown at a random rotation after a random crop (60-100% of the area, aspect ratio kept), occasional grayscale, and brightness and contrast jitter. The sampler gives the four domains equal weight. The checkpoint with the best validation mean was kept.
- Training data: Open Images V7 train images licensed CC BY 2.0 with rotation 0 (59,997), DocLayNet train pages (9,640), Manga109-s pages from the training titles (6,987), DiffusionDB parts 1-20 with NSFW score below 0.2 (16,405).
- Hardware: one RTX 5070 Ti.
- Code for data, training, evaluation and export: https://github.com/DenDen047/agent-earning/tree/main/orientation
## Limitations
- Only the four right-angle rotations. Small skews (for example a scan tilted by 5 degrees) are not detected, and mirrored images are not handled.
- Images with no natural "up" (textures, top-down shots of round plates, symmetric logos) are guessed; the probabilities are a useful signal of doubt.
- Photo labels come from Flickr uploaders' rotation metadata, not from human review, so a small share of the "upright" photos used for training and testing may be wrong.
- Manga test pages come from 10 Manga109 titles, mostly from the 1970s to 2010s. Illustrations are Stable Diffusion images from 2022; hand-drawn art may behave differently.
- The int8 model stores the matrix-multiplication weights as per-column symmetric int8 (DequantizeLinear) and computes in fp32. Quantizing the activations as well (static or dynamic) lost accuracy for this DINOv2 ViT-B. Its accuracy is in the table.
## License and attribution
Model weights and code: Apache-2.0.
- Photos: [Open Images V7](https://storage.googleapis.com/openimages/web/index.html), images licensed CC BY 2.0 by their Flickr authors (annotations CC BY 4.0). Only images marked CC BY 2.0 were used.
- Documents: [DocLayNet](https://github.com/DS4SD/DocLayNet) (CDLA-Permissive-1.0), by IBM Research (Pfitzmann et al., KDD 2022).
- Illustrations: [DiffusionDB](https://huggingface.co/datasets/poloclub/diffusiondb) (CC0 1.0), Wang et al., ACL 2023.
- Manga: this model was trained using the **Manga109-s** dataset. No manga images are included in this repository or the demo. Aizawa et al., IEEE MultiMedia 27(2), 2020; Matsui et al., Multimedia Tools and Applications 76(20), 2017.
- Backbone: DINOv2 (Oquab et al., 2023) weights via timm, Apache-2.0.