A simple AI package for classifying images as documents or general images.
Project description
is_image_document_ai
A lightweight pipeline for classifying whether an image is a document (e.g., scanned text, forms, PDFs) or a generic image (photographs, art, etc.). This repository provides two model options:
- MobileNetV2 (pretrained on ImageNet, then fine-tuned)
- TinyCNN (a small custom CNN)
Both achieve ~99% accuracy on our dataset, with MobileNetV2 slightly higher (~99.8%) and TinyCNN slightly smaller/faster in terms of parameters (~99.2% accuracy).
Table of Contents
Overview
- Purpose: Quickly determine if an image is a “document” vs. a “regular image.” Useful for filtering scanned PDFs, forms, or text-based images from photo-based datasets.
- Models:
- MobileNetV2 for high accuracy (99.8%).
- TinyCNN for a more lightweight approach (~99.2% accuracy, fewer parameters).
Data
- We downloaded 10,000 document images from HuggingFaceM4/DocumentVQA and 10,000 generic images from jackyhate/text-to-image-2M, resulting in:
images/ ├─ document/ │ ├─ 0.jpg │ ├─ 1.jpg │ ... └─ image/ ├─ 0.jpg ├─ 1.jpg ... - We perform a 3-way random split: 80% training, 10% validation, 10% test (i.e.,
train_ratio=0.8, val_ratio=0.1, test_ratio=0.1). - Each image is resized to 224×224 before feeding into the models.
Installation
-
Clone this repository:
git clone https://github.com/logophoman/is_image_document_ai.git cd is_image_document_ai
-
Install requirements:
pip install -r requirements.txt
This installs
torch,torchvision,requests, anddatasets. -
(Optional) Download data if you haven’t:
python download_data.pyBy default, it streams from the Hugging Face datasets to get 10k
documentimages and 10kimageimages, saved intoimages/.Note: You will need ~3.8GB of free storage for the image data. Training requires ~4GB of VRAM for mobilenet and ~800MB VRAM for TinyCNN
Training
MobileNetV2 Model
- File:
train.py - Command:
python train.py - This script:
- Loads data from
images/folder usingdata_loader.py. - Initializes a MobileNetV2 (pretrained on ImageNet).
- Fine-tunes the final layer for 2 classes: document vs. image.
- Trains until ~99% accuracy or until epochs finish.
- Saves the weights to
mobilenet_document_vs_image.pth. - Evaluates on the test set (the 10% split).
- Loads data from
TinyCNN Model
- File:
tinytrain.py - Command:
python tinytrain.py - This script:
- Loads the same
images/dataset with an 80/10/10 split. - Initializes our custom TinyCNN architecture (
model.py). - Trains for up to 5 epochs (or early stops at ~99% val accuracy).
- Saves weights to
tinycnn_document_vs_image.pth. - Prints final test accuracy.
- Loads the same
Inference
Using the PyPi package:
Input Folder:
from is_image_document_ai import load_tinycnn, infer_folder
model = load_tinycnn()
result = infer_folder("testfolder", model)
print(result)
Expected result:
paper.jpg => document
image.png => image
['document', 'image']
Input File:
from is_image_document_ai import load_mobilenet, infer_single_image
model = load_mobilenet()
result = infer_single_image("paper.jpg", model)
print(result)
Expected result:
paper.jpg => document
document
Or for the lazy ones (defaults to mobilenet):
from is_image_document_ai import infer_folder
result = infer_folder("testfolder")
print(result)
Manual using the scripts yourself:
We provide multiple ways to run inference. You can:
- Use the
test.py(for MobileNetV2) ortinytest.py(for TinyCNN) scripts for quick checks. - Use the
inference.pyscript, which supports command-line arguments to load either model and do single, folder, or testset evaluation.
Single Image
python inference.py --model-type mobilenet \
--weights mobilenet_document_vs_image.pth \
--mode single \
--input path/to/your_image.jpg
Folder of Images
python inference.py --model-type tinycnn \
--weights tinycnn_document_vs_image.pth \
--mode folder \
--input test_images/
Test-Set Evaluation (DataLoader)
python inference.py --model-type mobilenet \
--weights mobilenet_document_vs_image.pth \
--mode testset \
--root-dir images \
--batch-size 32
Results
After training on 20k images (10k document + 10k generic), both models achieve high accuracy:
- MobileNetV2: ~99.8% accuracy on the test set.
- TinyCNN: ~99.2% accuracy on the test set.
The TinyCNN has fewer parameters and uses less VRAM for the model itself, but PyTorch’s baseline overhead may still be a few hundred MB. MobileNetV2 is slightly more accurate and can converge faster due to ImageNet pretraining.
Contributing
Contributions are welcome! To get started:
- Fork the repo and clone your fork.
- Create a new branch for your feature or bug fix.
- Make changes, add tests, and ensure everything passes.
- Open a Pull Request describing your changes.
Possible improvements:
- Quantization or Half-Precision (FP16) to reduce model size / VRAM usage.
- Additional data augmentations to handle more varied document layouts.
- Implementation of other lightweight architectures (e.g. ShuffleNet, EfficientNet-Lite).
License
This project is licensed under the MIT License. You are free to use, modify, and distribute this code as permitted by the license.
Enjoy classifying your images! If you have any questions or issues, feel free to open an issue or pull request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file is_image_document_ai-0.0.1.tar.gz.
File metadata
- Download URL: is_image_document_ai-0.0.1.tar.gz
- Upload date:
- Size: 20.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33225aa0318290b2e0081db3e62960fa87c87cd75d1b92c4c13d5ad1c807ad8b
|
|
| MD5 |
60bfef3430c844225759b9d5ffd3c5b8
|
|
| BLAKE2b-256 |
96e41850d4e1be32fe3563f4300d992103ea5da9daee0032bdd7cb251fae6076
|
File details
Details for the file is_image_document_ai-0.0.1-py3-none-any.whl.
File metadata
- Download URL: is_image_document_ai-0.0.1-py3-none-any.whl
- Upload date:
- Size: 20.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
612be9481b88c4e27fe2a21040a085b4207197e2f5f9aba812a4577cf9744471
|
|
| MD5 |
9566d0235b92f69c91b6fc38305d0286
|
|
| BLAKE2b-256 |
9dcc49ac3cd86e5c16afefa7c70c3d55834fc50820f9c02c251e9be018121391
|