Skip to content

Commit e70bf05

Browse files
authoredSep 23, 2021
[fix] Improve dependencies for multi-backend support (#79)
* Add IMAGENET_DEFAULT_MEAN and IMAGENET_DEFAULT_STD * Try update backend-specific tests * modify test_effdet_backend ci * add test_only_effdet.py * more backend specific tests * Add pytest * escape torch.Tensor type annotation * misc fixes * install "torchvision" is more robust
1 parent b9fd396 commit e70bf05

File tree

6 files changed

+161
-14
lines changed

6 files changed

+161
-14
lines changed
 

‎.github/workflows/ci.yml

+57-10
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,68 @@ name: CI
33
on: [push]
44

55
jobs:
6-
build:
76

7+
test_only_effdet_backend:
8+
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- uses: actions/setup-python@v2
13+
with:
14+
python-version: '3.7'
15+
16+
- name: Test Dependency Support
17+
run: |
18+
pip install pytest
19+
pip install -e . # The bare layoutparser module
20+
pytest tests_deps/test_file_utils.py
21+
22+
- name: Install only effdet deps
23+
run: |
24+
pip install pytest
25+
pip install -e ".[effdet]"
26+
pytest tests_deps/test_only_effdet.py
27+
28+
test_only_detectron2_backend:
29+
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v2
33+
- uses: actions/setup-python@v2
34+
with:
35+
python-version: '3.7'
36+
37+
- name: Install only Detectron2 deps
38+
run: |
39+
pip install pytest
40+
pip install -e .
41+
pip install torchvision && pip install "git+https://github.com/facebookresearch/detectron2.git@v0.5#egg=detectron2"
42+
pytest tests_deps/test_only_detectron2.py
43+
44+
test_only_paddledetection_backend:
45+
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v2
49+
- uses: actions/setup-python@v2
50+
with:
51+
python-version: '3.7'
52+
53+
- name: Install only PaddleDetection deps
54+
run: |
55+
pip install pytest
56+
pip install -e ".[paddledetection]"
57+
pytest tests_deps/test_only_paddledetection.py
58+
59+
test_all_methods_all_backends:
60+
needs: [test_only_effdet_backend, test_only_detectron2_backend, test_only_paddledetection_backend]
861
runs-on: ubuntu-latest
962
strategy:
1063
matrix:
1164
python-version: [3.7, 3.8]
12-
1365
steps:
1466
- uses: actions/checkout@v2
67+
1568
- name: Set up Python ${{ matrix.python-version }}
1669
uses: actions/setup-python@v2
1770
with:
@@ -29,16 +82,10 @@ jobs:
2982
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --ignore F821
3083
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
3184
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
32-
33-
- name: Test Dependency Support
34-
run: |
35-
# Install additional requirements when running tests
36-
pip install pytest
37-
pytest tests_deps
3885
3986
- name: Test with pytest
4087
run: |
4188
# Install additional requirements when running tests
42-
pip install .[effdet]
89+
pip install ".[effdet]"
4390
pip install -r dev-requirements.txt
44-
pytest tests
91+
pytest tests

‎installation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ After several major updates, LayoutParser provides various functionalities and d
1515
| --- | --- |
1616
| `pip install layoutparser` | **Install the base LayoutParser Library**<br>It will support all key functions in LayoutParser, including:<br />1. Layout Data Structure and operations<br />2. Layout Visualization <br />3. Load/export the layout data |
1717
| `pip install "layoutparser[effdet]"` | **Install LayoutParser with Layout Detection Model Support**<br />It will install the LayoutParser base library as well as<br />supporting dependencies for the ***EfficientDet***-based layout detection models. |
18-
| `pip install layoutparser torch && pip install "git+https://github.com/facebookresearch/detectron2.git@v0.5#egg=detectron2"` | **Install LayoutParser with Layout Detection Model Support**<br />It will install the LayoutParser base library as well as<br />supporting dependencies for the ***Detectron2***-based layout detection models. See details in [Additional Instruction: Install Detectron2 Layout Model Backend](#additional-instruction-install-detectron2-layout-model-backend). |
18+
| `pip install layoutparser torchvision && pip install "git+https://github.com/facebookresearch/detectron2.git@v0.5#egg=detectron2"` | **Install LayoutParser with Layout Detection Model Support**<br />It will install the LayoutParser base library as well as<br />supporting dependencies for the ***Detectron2***-based layout detection models. See details in [Additional Instruction: Install Detectron2 Layout Model Backend](#additional-instruction-install-detectron2-layout-model-backend). |
1919
| `pip install "layoutparser[paddledetection]"` | **Install LayoutParser with Layout Detection Model Support**<br />It will install the LayoutParser base library as well as<br />supporting dependencies for the ***PaddleDetection***-based layout detection models. |
2020
| `pip install "layoutparser[ocr]"` | **Install LayoutParser with OCR Support**<br />It will install the LayoutParser base library as well as<br />supporting dependencies for performing OCRs. See details in [Additional Instruction: Install OCR utils](#additional-instruction-install-ocr-utils). |
2121

@@ -26,7 +26,7 @@ After several major updates, LayoutParser provides various functionalities and d
2626
If you would like to use the Detectron2 models for layout detection, you might need to run the following command:
2727

2828
```bash
29-
pip install layoutparser torch && pip install "detectron2@git+https://github.com/facebookresearch/detectron2.git@v0.5#egg=detectron2"
29+
pip install layoutparser torchvision && pip install "detectron2@git+https://github.com/facebookresearch/detectron2.git@v0.5#egg=detectron2"
3030
```
3131

3232
This might take some time as the command will *compile* the library. If you also want to install a Detectron2 version

‎src/layoutparser/models/effdet/layoutmodel.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
IMAGENET_DEFAULT_STD,
3232
transforms_coco_eval,
3333
)
34+
else:
35+
# Copied from https://github.com/rwightman/efficientdet-pytorch/blob/c5b694aa34900fdee6653210d856ca8320bf7d4e/effdet/data/transforms.py#L13
36+
# Such that when effdet is not loaded, we'll still have default values for IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD
37+
IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
38+
IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
39+
# IMAGENET_INCEPTION_MEAN = (0.5, 0.5, 0.5)
40+
# IMAGENET_INCEPTION_STD = (0.5, 0.5, 0.5)
3441

3542

3643
class InputTransform:
@@ -56,7 +63,7 @@ def __init__(
5663
self.mean_tensor = torch.tensor([x * 255 for x in mean]).view(1, 3, 1, 1)
5764
self.std_tensor = torch.tensor([x * 255 for x in std]).view(1, 3, 1, 1)
5865

59-
def preprocess(self, image: Image) -> Tuple[torch.Tensor, Dict]:
66+
def preprocess(self, image: Image) -> Tuple["torch.Tensor", Dict]:
6067

6168
image = image.convert("RGB")
6269
image_info = {"img_size": image.size}
@@ -206,7 +213,7 @@ def detect(self, image: Union["np.ndarray", "Image.Image"]):
206213
layout = self.gather_output(model_outputs)
207214
return layout
208215

209-
def gather_output(self, model_outputs: torch.Tensor) -> Layout:
216+
def gather_output(self, model_outputs: "torch.Tensor") -> Layout:
210217

211218
model_outputs = model_outputs.cpu().detach()
212219
box_predictions = Layout()

‎tests_deps/test_only_detectron2.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 The Layout Parser team. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import cv2
16+
import pytest
17+
from layoutparser import Detectron2LayoutModel
18+
19+
def test_only_effdet_model():
20+
21+
# When all the backeds are not installed, it should
22+
# elicit only ImportErrors
23+
24+
config = "lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config"
25+
model = Detectron2LayoutModel(config)
26+
image = cv2.imread("tests/fixtures/model/test_model_image.jpg")
27+
layout = model.detect(image)
28+
29+
with pytest.raises(ImportError):
30+
from layoutparser import EfficientDetLayoutModel
31+
from layoutparser import PaddleDetectionLayoutModel

‎tests_deps/test_only_effdet.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 The Layout Parser team. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import cv2
16+
import pytest
17+
from layoutparser import EfficientDetLayoutModel
18+
19+
def test_only_effdet_model():
20+
21+
# When all the backeds are not installed, it should
22+
# elicit only ImportErrors
23+
24+
config = "lp://PubLayNet/tf_efficientdet_d0/config"
25+
model = EfficientDetLayoutModel(config)
26+
image = cv2.imread("tests/fixtures/model/test_model_image.jpg")
27+
layout = model.detect(image)
28+
29+
with pytest.raises(ImportError):
30+
from layoutparser import Detectron2LayoutModel
31+
from layoutparser import PaddleDetectionLayoutModel
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2021 The Layout Parser team. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import cv2
16+
import pytest
17+
from layoutparser import PaddleDetectionLayoutModel
18+
19+
def test_only_effdet_model():
20+
21+
# When all the backeds are not installed, it should
22+
# elicit only ImportErrors
23+
24+
config = "lp://PubLayNet/ppyolov2_r50vd_dcn_365e/config"
25+
model = PaddleDetectionLayoutModel(config)
26+
image = cv2.imread("tests/fixtures/model/test_model_image.jpg")
27+
layout = model.detect(image)
28+
29+
with pytest.raises(ImportError):
30+
from layoutparser import EfficientDetLayoutModel
31+
from layoutparser import Detectron2LayoutModel

0 commit comments

Comments
 (0)
Please sign in to comment.