English | 中文
Implementation for Intermediate Domain Alignment and Morphology Analogy for Patent-Product Image Retrieval (NeurIPS 2025).
| Paper / project | README · docs/index.html |
| Dataset & weights | Hugging Face — haifan-gong/IDAMA |
This folder implements IDM (edge-domain mapping via UAED) and the retrieval stack used with MAF at inference time: preprocessing → feature extraction → similarity & Top-K evaluation → optional MAE/Swin pretraining.
Download PPIRD and checkpoints into
IDAMA/dataandIDAMA/model(not in git). SetIDAMA_ROOTto the repo root.
code/
├── common/ # Path constants (paths.py)
├── preprocessing/ # Data & edge preprocessing
│ ├── build_index_and_paths.py # Path lists & per-item image counts
│ ├── pack_unsupervised_train.py # Flat UAED train set → sharded tar + manifest
│ ├── pack_sharded_archive.py # Generic tree → sharded tar (e.g. ImageNet)
│ ├── pack_unsupervised_train.sh
│ ├── pack_features.sh
│ ├── edge_feature/ # UAED / MuGE edge detection
│ │ ├── run_uaed_edge_inference.sh
│ │ └── uaed/ # Train / infer / eval scripts
│ └── legacy/ # Legacy filtering & retrieval helpers
├── feature_extraction/ # Multi-backbone feature extraction
│ ├── extract_features.py # Unified entry (delegates to legacy)
│ ├── run_feature_extraction.sh # Extract product + patent features
│ ├── backbones/ # Swin / EVA / EVA02 / iBOT, etc.
│ └── legacy/ # Original extract_feature.py & ResNet variants
├── inference/ # Retrieval inference & metrics
│ ├── compute_similarity.py # Product–patent similarity matrix
│ ├── rank_and_evaluate.py # Top-K ranking & evaluation
│ ├── run_inference.sh # Similarity + eval in one script
│ └── legacy/ # Historical similarity / ranking code
└── pretrain/ # Self-supervised / supervised pretraining
├── run_pretrain.sh # MAE / Swin launcher
├── mae/ # Masked Autoencoder
├── swin/ # Swin Transformer
└── ibot/ # iBOT utilities
- Python 3.8+
- PyTorch + torchvision (CUDA recommended)
- Common packages:
timm,opencv-python,pandas,tqdm,Pillow,numpy
Some submodules need optional deps (e.g. MAE deepspeed, Swin apex); see per-folder READMEs.
export IDAMA_ROOT=/path/to/IDAMA # default: /data2/gonghaifan/IDAMA
cd "${IDAMA_ROOT}"flowchart LR
A[Raw images] --> B[UAED edge maps]
B --> C[Optional: sharded tar packs]
C --> D[MAE / Swin pretrain]
B --> E[Multi-backbone features]
E --> F[Similarity matrix]
F --> G[Top-K retrieval metrics]
| Stage | Module | Description |
|---|---|---|
| Edge maps | preprocessing/edge_feature |
RGB → edge-domain images for training & retrieval |
| Packing | pack_unsupervised_train.py / pack_sharded_archive.py |
Large-scale sharded tar + manifest.jsonl |
| Pretrain | pretrain/ |
Backbone training on edge domain or ImageNet |
| Features | feature_extraction/ |
Embeddings for test product / patent sets |
| Inference | inference/ |
Similarity & retrieval metrics |
Run from the IDAMA repo root (not inside code/). Override paths with IDAMA_ROOT.
Regenerate metadata when test image folders change:
python code/preprocessing/build_index_and_paths.py \
--kind patent \
--image-dir data/test/new_edge_image/patent \
--paths-out data/metadata/patent_img_paths.txt \
--counts-out data/metadata/patent_all.csvUse --kind product for nested product directory layouts.
bash code/preprocessing/edge_feature/run_uaed_edge_inference.sh \
/path/to/raw_images \
/path/to/edge_output \
0.5Default checkpoint: ${IDAMA_ROOT}/model/edge_feature/epoch-19-checkpoint.pth. See preprocessing/edge_feature/README.md.
Flat UAED directory (patent + product):
bash code/preprocessing/pack_unsupervised_train.sh dry-run # preview shards
bash code/preprocessing/pack_unsupervised_train.sh run # write to data/unsupervised_train/Arbitrary directory tree (e.g. ImageNet):
python code/preprocessing/pack_sharded_archive.py \
--src /path/to/imagenet/train \
--out data/unsupervised_train/imagenet1k-edge \
--num-shards 20 --workers 16 --file-type imageBackbones: r18, r50, r101, swin_t, swin_b, mae, ibot, eva02, clip2, plus _ft fine-tuned variants.
CUDA_VISIBLE_DEVICES=0 bash code/feature_extraction/run_feature_extraction.sh eva02Output: data/features/new_data_feature/<model_name>/.
bash code/inference/run_inference.sh eva02Optional env: POOLING=avgmax|standard, BATCH_SIZE, LOAD_BATCH_NUM.
Writes avgmax_sim.pkl (or avg_sim.pkl) and retrieval/ under the feature directory.
cd code/pretrain
# MAE pretrain on edge-domain images
NPROC_PER_NODE=8 bash run_pretrain.sh mae-pretrain \
"${IDAMA_ROOT}/data/unlabeled_train/data_edge/goods_edge_0.5" \
"${IDAMA_ROOT}/output/pretrain_mae"
# Swin-Base fine-tune on ImageNet
CUDA_VISIBLE_DEVICES=0,1,2,3 bash run_pretrain.sh swin-train base \
/path/to/imagenet \
"${IDAMA_ROOT}/output/swin_base" \
--resume "${IDAMA_ROOT}/model/swin/pretrained/swin_base_patch4_window7_224.pth"See run_pretrain.sh for full usage.
common/paths.py defines defaults (edit or remount via IDAMA_ROOT):
| Variable | Default |
|---|---|
METADATA_DIR |
data/metadata/ |
FEATURE_DIR |
data/features/new_data_feature/ |
PRODUCT_IMAGE_PATHS |
metadata/product_img_paths.txt |
PATENT_IMAGE_PATHS |
metadata/patent_img_paths.txt |
build_index_and_paths.py: Scan images; write path txt andIndex,img_numCSV.pack_unsupervised_train.py: Classify patent/product; parallel tar shards;manifest.jsonl+summary.json.pack_sharded_archive.py: Generic sharded packing with relative paths (ImageNet-style trees).edge_feature/: UAED training & batch inference; Python entry:edge_infer.py.
extract_features.py: Setssys.pathand runslegacy/extract_feature.py.run_feature_extraction.sh: Picks input size, batch, checkpoint; runs product then patent.backbones/: ViT / Swin implementations;legacy/: ResNet and older scripts.
compute_similarity.py:--pooling avgmax|standardselects legacy implementation.rank_and_evaluate.py: Top-K ranking and metrics from similarity pickle.run_inference.sh: Runs both steps.
mae/: MAE pretraining and classification fine-tuning.swin/: Swin classification and SimMIM.ibot/: Weight conversion utilities.
| Stage | Typical location |
|---|---|
| Unsupervised tar | data/unsupervised_train/ (patent/, product/, imagenet1k-edge/) |
| Edge training images | data/unlabeled_train/data_edge/ |
| Test edge images | data/test/new_edge_image/ |
| Feature vectors | data/features/new_data_feature/ |
| Retrieval index | data/metadata/*.npy, *.csv |
See the repo root README and local data/unsupervised_train/README.md (not in git) for dataset notes.
UAED, MAE, Swin Transformer, and other third-party code retain their upstream licenses. This repository is organized for research and reproduction.