BoLiu jdye64 commited on
Commit
b1cc5ea
Β·
verified Β·
1 Parent(s): 6e3dc84

pip-install2 (#4)

Browse files

- restructure so that table-structure can be installed via pip (697b9172fdff60b1635324cf971589781d14ee1c)
- Clean up repository structure and update for pip install (12ea31ec447e5d26a4a4ddda5b51bd3f6a2ae5d3)
- removed redundant utils.py (356eefc12f9211303b630a796b0cba9c45437c06)


Co-authored-by: Jeremy <jdye64@users.noreply.huggingface.co>

.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ dist
2
+ build
3
+ *.egg-info
4
+ *.egg
5
+ *.pyc
6
+ *.pyo
7
+ *.pyd
Demo.ipynb CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:3fbac9002e8052dd97f8de19ede6a60fa119d237a08aa13e09fc294c73708489
3
- size 1085041
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:002e6edf2b37d18f5eb2499fa653b8543b95964b122be8726cf214a2cf5500ba
3
+ size 848913
MANIFEST.in ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ include README.md
2
+ include THIRD_PARTY_NOTICES.md
3
+ recursive-include nemotron_table_structure_v1
4
+
5
+
6
+
README.md CHANGED
@@ -134,7 +134,12 @@ git clone https://huggingface.co/nvidia/nemotron-table-structure-v1
134
  ```
135
  git clone git@hf.co:nvidia/nemotron-table-structure-v1
136
  ```
137
-
 
 
 
 
 
138
  2. Run the model using the following code:
139
 
140
  ```
 
134
  ```
135
  git clone git@hf.co:nvidia/nemotron-table-structure-v1
136
  ```
137
+ Optional:
138
+ This can be installed as a package using pip
139
+ ```
140
+ cd nemotron-table-structure-v1
141
+ pip install -e .
142
+ ```
143
  2. Run the model using the following code:
144
 
145
  ```
nemotron_table_structure_v1/__init__.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ """
5
+ Nemotron Table Structure v1
6
+
7
+ A specialized object detection model for table structure extraction based on YOLOX.
8
+ """
9
+
10
+ __version__ = "1.0.0"
11
+
12
+ from .model import define_model, YoloXWrapper
13
+ from .utils import (
14
+ plot_sample,
15
+ postprocess_preds_table_structure,
16
+ reformat_for_plotting,
17
+ reorder_boxes,
18
+ )
19
+
20
+ __all__ = [
21
+ "define_model",
22
+ "YoloXWrapper",
23
+ "plot_sample",
24
+ "postprocess_preds_table_structure",
25
+ "reformat_for_plotting",
26
+ "reorder_boxes",
27
+ ]
28
+
29
+
30
+
nemotron_table_structure_v1/config.json ADDED
File without changes
model.py β†’ nemotron_table_structure_v1/model.py RENAMED
@@ -10,7 +10,7 @@ import numpy.typing as npt
10
  import torch.nn as nn
11
  import torch.nn.functional as F
12
  from typing import Dict, List, Tuple, Union
13
- from yolox.boxes import postprocess
14
 
15
 
16
  def define_model(config_name: str = "page_element_v3", verbose: bool = True) -> nn.Module:
@@ -25,8 +25,9 @@ def define_model(config_name: str = "page_element_v3", verbose: bool = True) ->
25
  torch.nn.Module: The initialized YOLOX model.
26
  """
27
  # Load model from exp_file
28
- sys.path.append(os.path.dirname(config_name))
29
- exp_module = importlib.import_module(os.path.basename(config_name).split(".")[0])
 
30
 
31
  config = exp_module.Exp()
32
  model = config.get_model()
@@ -35,8 +36,11 @@ def define_model(config_name: str = "page_element_v3", verbose: bool = True) ->
35
  if verbose:
36
  print(" -> Loading weights from", config.ckpt)
37
 
38
- ckpt = torch.load(config.ckpt, map_location="cpu", weights_only=False)
39
- model.load_state_dict(ckpt["model"], strict=True)
 
 
 
40
 
41
  model = YoloXWrapper(model, config)
42
  return model.eval().to(config.device)
 
10
  import torch.nn as nn
11
  import torch.nn.functional as F
12
  from typing import Dict, List, Tuple, Union
13
+ from .yolox.boxes import postprocess
14
 
15
 
16
  def define_model(config_name: str = "page_element_v3", verbose: bool = True) -> nn.Module:
 
25
  torch.nn.Module: The initialized YOLOX model.
26
  """
27
  # Load model from exp_file
28
+ # page_element_v3.py is in the same directory as model.py
29
+ sys.path.append(os.path.dirname(__file__))
30
+ exp_module = importlib.import_module("table_structure_v1")
31
 
32
  config = exp_module.Exp()
33
  model = config.get_model()
 
36
  if verbose:
37
  print(" -> Loading weights from", config.ckpt)
38
 
39
+ # Find package directory and load weights (nemotron_table_structure_v1)
40
+ package_dir = os.path.dirname(os.path.abspath(__file__))
41
+ weights_path = os.path.join(package_dir, "weights.pth")
42
+ state_dict = torch.load(weights_path, map_location="cpu", weights_only=False)
43
+ model.load_state_dict(state_dict["model"], strict=True)
44
 
45
  model = YoloXWrapper(model, config)
46
  return model.eval().to(config.device)
nemotron_table_structure_v1/post_processing/__init__.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: Apache-2.0
3
+
4
+ from .table_struct_pp import (
5
+ assign_boxes,
6
+ merge_text_in_cell,
7
+ remove_empty_row,
8
+ build_markdown,
9
+ display_markdown,
10
+ )
11
+ from .wbf import weighted_boxes_fusion
12
+
13
+ __all__ = [
14
+ "assign_boxes",
15
+ "merge_text_in_cell",
16
+ "remove_empty_row",
17
+ "build_markdown",
18
+ "display_markdown",
19
+ "weighted_boxes_fusion",
20
+ ]
21
+
22
+
23
+
{post_processing β†’ nemotron_table_structure_v1/post_processing}/table_struct_pp.py RENAMED
File without changes
{post_processing β†’ nemotron_table_structure_v1/post_processing}/wbf.py RENAMED
File without changes
table_structure_v1.py β†’ nemotron_table_structure_v1/table_structure_v1.py RENAMED
@@ -17,7 +17,7 @@ class Exp:
17
 
18
  def __init__(self) -> None:
19
  """Initialize the configuration with default parameters."""
20
- self.name: str = "page-element-v3"
21
  self.ckpt: str = "weights.pth"
22
  self.device: str = "cuda:0" if torch.cuda.is_available() else "cpu"
23
 
@@ -56,7 +56,9 @@ class Exp:
56
  Returns:
57
  nn.Module: The YOLOX model with configured parameters.
58
  """
59
- from yolox import YOLOX, YOLOPAFPN, YOLOXHead
 
 
60
 
61
  # Build model
62
  if getattr(self, "model", None) is None:
 
17
 
18
  def __init__(self) -> None:
19
  """Initialize the configuration with default parameters."""
20
+ self.name: str = "table-structure-v1"
21
  self.ckpt: str = "weights.pth"
22
  self.device: str = "cuda:0" if torch.cuda.is_available() else "cpu"
23
 
 
56
  Returns:
57
  nn.Module: The YOLOX model with configured parameters.
58
  """
59
+ from nemotron_table_structure_v1.yolox.yolox import YOLOX
60
+ from nemotron_table_structure_v1.yolox.yolo_pafpn import YOLOPAFPN
61
+ from nemotron_table_structure_v1.yolox.yolo_head import YOLOXHead
62
 
63
  # Build model
64
  if getattr(self, "model", None) is None:
utils.py β†’ nemotron_table_structure_v1/utils.py RENAMED
File without changes
nemotron_table_structure_v1/weights.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dd5df343f07d13b793b356ace542f87001ab344cd8770469a189c1b547fa6a5f
3
+ size 433888364
{yolox β†’ nemotron_table_structure_v1/yolox}/__init__.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/boxes.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/darknet.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/network_blocks.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/yolo_fpn.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/yolo_head.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/yolo_pafpn.py RENAMED
File without changes
{yolox β†’ nemotron_table_structure_v1/yolox}/yolox.py RENAMED
File without changes
pyproject.toml ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["setuptools>=45", "wheel", "setuptools_scm[toml]>=6.2"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "nemotron-table-structure-v1"
7
+ version = "1.0.0"
8
+ description = "Nemotron Table Structure v1 - A specialized object detection model for table structure extraction"
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = {text = "NVIDIA Open Model License"}
12
+ authors = [
13
+ {name = "NVIDIA Corporation", email = "tviel@nvidia.com"}
14
+ ]
15
+ keywords = ["table-structure", "object-detection", "yolox", "nvidia", "nemotron", "pdf", "document-processing", "ocr"]
16
+ classifiers = [
17
+ "Development Status :: 5 - Production/Stable",
18
+ "Intended Audience :: Developers",
19
+ "Intended Audience :: Science/Research",
20
+ "License :: Other/Proprietary License",
21
+ "Operating System :: OS Independent",
22
+ "Programming Language :: Python :: 3",
23
+ "Programming Language :: Python :: 3.8",
24
+ "Programming Language :: Python :: 3.9",
25
+ "Programming Language :: Python :: 3.10",
26
+ "Programming Language :: Python :: 3.11",
27
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
28
+ "Topic :: Scientific/Engineering :: Image Recognition",
29
+ ]
30
+ dependencies = [
31
+ "torch>=2.0.0",
32
+ "numpy>=1.21.0",
33
+ "matplotlib>=3.3.0",
34
+ "pandas>=1.3.0",
35
+ "Pillow>=8.0.0",
36
+ ]
37
+
38
+ [project.urls]
39
+ Homepage = "https://huggingface.co/nvidia/nemotron-table-structure-v1"
40
+ Repository = "https://huggingface.co/nvidia/nemotron-table-structure-v1"
41
+ Documentation = "https://huggingface.co/nvidia/nemotron-table-structure-v1"
42
+ "Bug Tracker" = "https://huggingface.co/nvidia/nemotron-table-structure-v1/discussions"
43
+
44
+ [tool.setuptools]
45
+ packages = ["nemotron_table_structure_v1", "nemotron_table_structure_v1.yolox", "nemotron_table_structure_v1.post_processing"]
46
+
47
+ [tool.setuptools.package-data]
48
+ "*" = ["*.pth", "config.json"]
49
+
50
+
51
+