CoolFace
Modelpublic

mahfuj735/chemical-ocsr

sourceHugging Facemitupdated 19d agoView on Hugging Face
0likes
Model Card

๐Ÿงช Chemical AI Studio: Mobile Optical Chemical Structure Recognition (OCSR)

Official model repository for Chemical AI Studio (cicapp) โ€” a two-stage hierarchical deep learning pipeline for real-time on-device chemical diagram classification and structure translation into SMILES representations.


๐Ÿ—๏ธ Architecture Overview

[ Input Chemical Image ]
           โ”‚
           โ–ผ
[ Stage 1: ConvNeXt-V2-Nano Classifier (60 MB) ]
โ”œโ”€โ”€ one_molecule (99.31% F1) โ”€โ”€โ–บ Route to Stage 2 OCSR
โ”œโ”€โ”€ reactions               โ”€โ”€โ–บ Reaction Scheme Panel
โ”œโ”€โ”€ several_molecules       โ”€โ”€โ–บ Multi-Molecule Parsing
โ””โ”€โ”€ rest                    โ”€โ”€โ–บ Alert & Filter
           โ”‚
           โ–ผ (if one_molecule)
[ Stage 2: Mobile OCSR with 2D Cross-Attention (8.47 MB) ]
โ”œโ”€โ”€ MobileNetV3-Large Encoder (7x7 spatial feature grid)
โ”œโ”€โ”€ Bahdanau 2D Cross-Attention Mechanism
โ””โ”€โ”€ 2-Layer Autoregressive GRU Decoder
           โ”‚
           โ–ผ
[ Output Canonical SMILES ]

๐Ÿ“Š Evaluation & Benchmark Results

Stage 1: Diagram Classification (convnextv2_nano.onnx)

  • โ€”Top-1 Test Accuracy: 99.31%
  • โ€”Weighted Macro F1: 99.31%
  • โ€”Mean In-Memory Inference Latency: 30.8 ms

Stage 2: Mobile OCSR (mobile_ocsr_full.onnx)

  • โ€”Chemical Syntax Validity Rate: 99.90%
  • โ€”Exact Canonical Match Rate (ChemDraw Test Set): 97.40%
  • โ€”Mean Morgan Tanimoto Fingerprint Similarity: 99.71%
  • โ€”Peak Model Size: 8.47 MB (Mobile ONNX)
  • โ€”On-Device Inference Latency: ~14 - 35 ms (Edge NPU/CPU)

๐Ÿ“ฆ Model Artifacts Included

  • โ€”mobile_ocsr_full.onnx: End-to-end monolithic Mobile OCSR graph (Image tensor $\to$ SMILES token IDs).
  • โ€”convnextv2_nano.onnx: 4-class chemical diagram router.
  • โ€”molscribe_vocab.json: 61-token SMILES vocabulary with bidirectional stoi and itos mappings.
  • โ€”export_summary.json: Complete precision and benchmarking logs.

๐Ÿ’ป Python Quickstart

python
import json
import numpy as np
from PIL import Image
import onnxruntime as ort
from huggingface_hub import hf_hub_download

# Download artifacts
model_path = hf_hub_download("mahfuj735/chemical-ocsr", "mobile_ocsr_full.onnx")
vocab_path = hf_hub_download("mahfuj735/chemical-ocsr", "molscribe_vocab.json")

session = ort.InferenceSession(model_path)
with open(vocab_path, "r") as f:
    vocab = json.load(f)

itos = {int(k): v for k, v in vocab["itos"].items()}
eos_idx = vocab["eos_idx"]

# Prepare image
img = Image.open("molecule.png").convert("RGB").resize((224, 224), Image.BILINEAR)
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
arr = np.expand_dims(np.transpose((np.array(img, dtype=np.float32) / 255.0 - mean) / std, (2, 0, 1)), 0)

# Run Inference
pred_tokens = session.run(None, {"image": arr})[0][0]
smiles = "".join([itos.get(int(t), "") for t in pred_tokens if int(t) not in (vocab["pad_idx"], vocab["bos_idx"]) and int(t) != eos_idx])
print("Predicted SMILES:", smiles)

๐Ÿ“ฑ Mobile Deployment (Flutter / Android)

Models are designed for zero-cloud dependency, strictly bounded RSS memory (<450 MB), and real-time C++ inference on mobile hardware via flutter_onnxruntime.