-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f2c5e6a
Showing
36 changed files
with
1,881 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# VS Code | ||
.history | ||
|
||
# make targets & code outputs | ||
.venv_done | ||
outputs | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
cover/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
# For a library or package, you might want to ignore these files since the code is | ||
# intended to run in multiple environments; otherwise, check them in: | ||
# .python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# poetry | ||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. | ||
# This is especially recommended for binary packages to ensure reproducibility, and is more | ||
# commonly ignored for libraries. | ||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control | ||
#poetry.lock | ||
|
||
# pdm | ||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. | ||
#pdm.lock | ||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it | ||
# in version control. | ||
# https://pdm.fming.dev/#use-with-ide | ||
.pdm.toml | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# pytype static type analyzer | ||
.pytype/ | ||
|
||
# Cython debug symbols | ||
cython_debug/ | ||
|
||
# PyCharm | ||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can | ||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore | ||
# and can be added to the global gitignore or merged into this file. For a more nuclear | ||
# option (not recommended) you can uncomment the following to ignore the entire idea folder. | ||
#.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 ShengYun (Anthony) Peng. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# | ||
# Datasets | ||
# | ||
PUBTABNET = dataset=pubtabnet ++dataset.root_dir=$(pubtabnet_dir) | ||
|
||
# | ||
# Models | ||
# | ||
IMGCNN = model/model/backbone=imgcnn | ||
IMGLINEAR = model/model/backbone=imglinear | ||
IMGCONVSTEM = model/model/backbone=imgconvstem | ||
|
||
# encoder decoder | ||
e2_d4 = ++model.model.encoder.nlayer=2 ++model.model.decoder.nlayer=4 | ||
e4_d4 = ++model.model.encoder.nlayer=4 ++model.model.decoder.nlayer=4 | ||
|
||
# input size | ||
i224 = ++trainer.img_size=224 | ||
i252 = ++trainer.img_size=252 | ||
i476 = ++trainer.img_size=476 | ||
i392 = ++trainer.img_size=392 | ||
i504 = ++trainer.img_size=504 | ||
|
||
# resnets | ||
r18 = ++model.model.backbone.backbone._target_=torchvision.models.resnet18 ++model.model.backbone.output_channels=512 | ||
r34 = ++model.model.backbone.backbone._target_=torchvision.models.resnet34 ++model.model.backbone.output_channels=512 | ||
r50 = ++model.model.backbone.backbone._target_=torchvision.models.resnet50 ++model.model.backbone.output_channels=2048 | ||
|
||
# downsample factor/patch size | ||
p8 = ++model.backbone_downsampling_factor=8 | ||
p14 = ++model.backbone_downsampling_factor=14 | ||
p16 = ++model.backbone_downsampling_factor=16 | ||
p28 = ++model.backbone_downsampling_factor=28 | ||
p56 = ++model.backbone_downsampling_factor=56 | ||
p112 = ++model.backbone_downsampling_factor=112 | ||
|
||
# output channels of conv stem | ||
cs_c192 = ++model.model.backbone.output_channels=192 | ||
cs_c384 = ++model.model.backbone.output_channels=384 | ||
|
||
# kernel size in conv stem | ||
cs_k3 = ++model.model.backbone.kernel_size=3 | ||
cs_k5 = ++model.model.backbone.kernel_size=5 | ||
|
||
# transformer | ||
nhead8 = ++model.nhead=8 | ||
|
||
# cnn backbone | ||
MODEL_r18_e2_d4 = $(IMGCNN) $(e2_d4) $(r18) | ||
MODEL_r34_e2_d4 = $(IMGCNN) $(e2_d4) $(r34) | ||
MODEL_r50_e2_d4 = $(IMGCNN) $(e2_d4) $(r50) | ||
|
||
# linear projection | ||
MODEL_p14_e4_d4_nhead8 = $(IMGLINEAR) $(p14) $(e4_d4) $(nhead8) | ||
MODEL_p16_e4_d4_nhead8 = $(IMGLINEAR) $(p16) $(e4_d4) $(nhead8) | ||
MODEL_p28_e4_d4_nhead8 = $(IMGLINEAR) $(p28) $(e4_d4) $(nhead8) | ||
MODEL_p56_e4_d4_nhead8 = $(IMGLINEAR) $(p56) $(e4_d4) $(nhead8) | ||
MODEL_p112_e4_d4_nhead8 = $(IMGLINEAR) $(p112) $(e4_d4) $(nhead8) | ||
|
||
# conv stem | ||
MODEL_cs_c384_e4_d4_nhead8 = $(IMGCONVSTEM) $(cs_c384) $(cs_k3) $(e4_d4) $(nhead8) | ||
MODEL_cs_c384_k5_e4_d4_nhead8_i476 = $(IMGCONVSTEM) $(cs_c384) $(cs_k5) $(e4_d4) $(nhead8) $(i476) | ||
MODEL_cs_c192_p8_k5_e4_d4_nhead8_i224 = $(IMGCONVSTEM) $(cs_c192) $(p8) $(cs_k5) $(e4_d4) $(nhead8) $(i224) | ||
MODEL_cs_c384_e4_d4_nhead8_i252 = $(IMGCONVSTEM) $(cs_c384) $(cs_k3) $(e4_d4) $(nhead8) $(i252) | ||
MODEL_cs_c384_k5_e4_d4_nhead8_i392 = $(IMGCONVSTEM) $(cs_c384) $(cs_k5) $(e4_d4) $(nhead8) $(i392) | ||
MODEL_cs_c384_k5_e4_d4_nhead8_i504 = $(IMGCONVSTEM) $(cs_c384) $(cs_k5) $(e4_d4) $(nhead8) $(i504) | ||
MODEL_cs_c384_k5_e4_d4_nhead8 = $(IMGCONVSTEM) $(cs_c384) $(cs_k5) $(e4_d4) $(nhead8) | ||
|
||
# | ||
# Optimizer | ||
# | ||
OPT_adamw = trainer/train/optimizer=adamw | ||
|
||
# | ||
# lr + scheduler | ||
# | ||
OPT_cosinelr = trainer/train/lr_scheduler=cosine | ||
|
||
# | ||
# Regularization | ||
# | ||
REG_d00 = ++model.dropout=0. | ||
REG_d02 = ++model.dropout=0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# Make changes to the variables below | ||
pubtabnet_dir := "<your path to pubtabnet>/pubtabnet" | ||
###################### | ||
SHELL := /bin/bash | ||
VENV_NAME := adp | ||
CONDA_ACTIVATE := source $$(conda info --base)/etc/profile.d/conda.sh && conda activate $(VENV_NAME) | ||
PYTHON := $(CONDA_ACTIVATE) && python | ||
PIP := $(CONDA_ACTIVATE) && pip3 | ||
# Stacked single-node multi-worker: https://pytorch.org/docs/stable/elastic/run.html#stacked-single-node-multi-worker | ||
TORCHRUN = $(CONDA_ACTIVATE) && torchrun --rdzv-backend=c10d --rdzv_endpoint localhost:0 --nnodes=1 --nproc_per_node=$(NGPU) | ||
|
||
# Taken from https://tech.davis-hansson.com/p/make/ | ||
ifeq ($(origin .RECIPEPREFIX), undefined) | ||
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later) | ||
endif | ||
.RECIPEPREFIX = > | ||
|
||
# | ||
# Virtual Environment Targets | ||
# | ||
clean: | ||
> rm -f .venv_done | ||
|
||
.venv_done: clean | ||
> conda create -n $(VENV_NAME) python=3.9 | ||
> $(PIP) install -r requirements.txt | ||
> $(PIP) install -e . | ||
> touch $@ | ||
|
||
# | ||
# Python Targets | ||
# | ||
include MODEL.mk | ||
SRC := src | ||
BEST_MODEL = "../$*/model/best.pt" | ||
RESULT_JSON := html_table_result.json | ||
TEDS_STRUCTURE = -f "../experiments/$*/$(RESULT_JSON)" -s | ||
|
||
# Experiment Configurations | ||
EXP_r18_e2_d4_adamw := $(PUBTABNET) $(MODEL_r18_e2_d4) $(OPT_adamw) ++trainer.train.batch_size=64 ++trainer.valid.batch_size=64 | ||
EXP_r34_e2_d4_adamw := $(PUBTABNET) $(MODEL_r34_e2_d4) $(OPT_adamw) ++trainer.train.batch_size=64 ++trainer.valid.batch_size=32 | ||
EXP_r50_e2_d4_adamw := $(PUBTABNET) $(MODEL_r50_e2_d4) $(OPT_adamw) ++trainer.train.batch_size=32 ++trainer.valid.batch_size=16 | ||
|
||
EXP_p14_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_p14_e4_d4_nhead8) $(OPT_adamw) | ||
EXP_p16_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_p16_e4_d4_nhead8) $(OPT_adamw) | ||
EXP_p28_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_p28_e4_d4_nhead8) $(OPT_adamw) | ||
EXP_p56_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_p56_e4_d4_nhead8) $(OPT_adamw) | ||
EXP_p112_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_p112_e4_d4_nhead8) $(OPT_adamw) | ||
|
||
EXP_cs_c384_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_cs_c384_e4_d4_nhead8) $(OPT_adamw) ++trainer.train.batch_size=64 | ||
EXP_cs_c384_k5_e4_d4_nhead8_i476_adamw := $(PUBTABNET) $(MODEL_cs_c384_k5_e4_d4_nhead8_i476) $(OPT_adamw) ++trainer.train.batch_size=64 | ||
EXP_cs_c192_p8_k5_e4_d4_nhead8_i224_adamw := $(PUBTABNET) $(MODEL_cs_c192_p8_k5_e4_d4_nhead8_i224) $(OPT_adamw) ++trainer.train.batch_size=64 | ||
EXP_cs_c384_e4_d4_nhead8_i252_adamw := $(PUBTABNET) $(MODEL_cs_c384_e4_d4_nhead8_i252) $(OPT_adamw) | ||
EXP_cs_c384_k5_e4_d4_nhead8_i392_adamw := $(PUBTABNET) $(MODEL_cs_c384_k5_e4_d4_nhead8_i392) $(OPT_adamw) | ||
EXP_cs_c384_k5_e4_d4_nhead8_i504_adamw := $(PUBTABNET) $(MODEL_cs_c384_k5_e4_d4_nhead8_i504) $(OPT_adamw) ++trainer.train.batch_size=64 | ||
EXP_cs_c384_k5_e4_d4_nhead8_adamw := $(PUBTABNET) $(MODEL_cs_c384_k5_e4_d4_nhead8) $(OPT_adamw) ++trainer.train.batch_size=64 | ||
|
||
###################### | ||
NGPU := 1 | ||
|
||
.SECONDARY: | ||
|
||
experiments/%/.done_train_structure: | ||
> @echo "Using experiment configurations from variable EXP_$*" | ||
> cd $(SRC) && $(TORCHRUN) -m main ++name=$* $(EXP_$*) ++trainer.mode="train" | ||
> touch $@ | ||
|
||
experiments/%/.done_test_structure: experiments/%/.done_train_structure | ||
> @echo "Using experiment configurations from variable EXP_$*" | ||
> cd $(SRC) && $(TORCHRUN) -m main ++name=$* $(EXP_$*) ++trainer.mode="test" ++trainer.test.model=$(BEST_MODEL) | ||
> touch $@ | ||
|
||
experiments/%/.done_teds_structure: experiments/%/.done_test_structure | ||
> cd $(SRC) && $(PYTHON) -m utils.teds $(TEDS_STRUCTURE) | ||
> touch $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# High-Performance Transformers for Table Structure Recognition Need Early Convolutions | ||
|
||
Table structure recognition (TSR) aims to convert tabular images into a machine-readable format, where a visual encoder extracts image features and a textual decoder generates table-representing tokens. Existing approaches use classic convolutional neural network (CNN) backbones for the visual encoder and transformers for the textual decoder. However, this hybrid CNN-Transformer architecture introduces a complex visual encoder that accounts for nearly half of the total model parameters, markedly reduces both training and inference speed, and hinders the potential for self-supervised learning in TSR. In this work, we design a lightweight visual encoder for TSR without sacrificing expressive power. We discover that a convolutional stem can match classic CNN backbone performance, with a much simpler model. The convolutional stem strikes an optimal balance between two crucial factors for high-performance TSR: a higher receptive field (RF) ratio and a longer sequence length. This allows it to "see" an appropriate portion of the table and "store" the complex table structure within sufficient context length for the subsequent transformer. We conducted reproducible ablation studies and open-sourced our code to enhance transparency, inspire innovations, and facilitate fair comparisons in our domain as tables are a promising modality for representation learning. | ||
<p align="center"> | ||
<img src="imgs/pipeline.png" alt="drawing" width="600"/> | ||
</p> | ||
|
||
### Installation | ||
1. Prepare PubTabNet dataset available [here](https://github.com/ibm-aur-nlp/PubTabNet/tree/master#getting-data) | ||
2. Change the "pubtabnet_dir" in [Makefile](./Makefile) to "your path to PubTabNet" | ||
3. Set up venv | ||
```bash | ||
make .venv_done | ||
``` | ||
|
||
### Training, Testing & Evaluation | ||
1. Train an instance of visual encoder with ResNet-18 | ||
```bash | ||
make experiments/r18_e2_d4_adamw/.done_train_structure | ||
``` | ||
2. Test + Compute teds score | ||
```bash | ||
make experiments/r18_e2_d4_adamw/.done_teds_structure | ||
``` | ||
3. All models in ablations are defined in "Experiment Configurations" section of [Makefile](./Makefile). Replace "r18_e2_d4_adamw" with any other configuration for training and testing. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root_dir: null | ||
json_html: PubTabNet_2.0.0.jsonl | ||
label_type: structure | ||
mean: | ||
- 240.380 | ||
- 240.390 | ||
- 240.486 | ||
std: | ||
- 45.735 | ||
- 45.785 | ||
- 45.756 |
Oops, something went wrong.