Skip to content

Commit

Permalink
bugfixes, better test coverage (#28)
Browse files Browse the repository at this point in the history
* add daily package testing workflow

* add ci tests for scripts

* fix cli argument

* fix function argument

* add export format selection

* update to v0.2.5
  • Loading branch information
fcakyon authored Feb 15, 2021
1 parent 3094937 commit 970058b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 6 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ jobs:
- name: Test with unittest
run: |
pip install pytest
python -m unittest
python -m unittest
- name: Install SAHI package from local setup.py
run: >
pip install -e .
- name: Test SAHI scripts for mmdet < 2.9.0
if: matrix.mmdet-version == '2.8.0'
run: |
# predict_folder
python scripts/predict_folder.py --image_dir tests/data/coco_utils/ --visual_output_dir outputs/ --model_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_2x_coco_20200131-fdb43119.pth --config_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_1x_coco.py
- name: Test SAHI scripts for mmdet == 2.9.0
if: matrix.mmdet-version == '2.9.0'
run: |
# predict_folder
python scripts/predict_folder.py --image_dir tests/data/coco_utils/ --visual_output_dir outputs/ --model_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_2x_coco_20200131-fdb43119.pth --config_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_1x_coco_v290.py
66 changes: 66 additions & 0 deletions .github/workflows/package_testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Package Testing

on:
schedule:
- cron: '0 0 * * *' # Runs at 00:00 UTC every day

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
operating-system: [ubuntu-latest, windows-latest, macos-latest]
python-version: [3.6, 3.7, 3.8]
mmdet-version : [2.8.0, 2.9.0]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Restore Ubuntu cache
uses: actions/cache@v1
if: matrix.operating-system == 'ubuntu-latest'
with:
path: ~/.cache/pip
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-
- name: Restore MacOS cache
uses: actions/cache@v1
if: matrix.operating-system == 'macos-latest'
with:
path: ~/Library/Caches/pip
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-
- name: Restore Windows cache
uses: actions/cache@v1
if: matrix.operating-system == 'windows-latest'
with:
path: ~\AppData\Local\pip\Cache
key: ${{ matrix.os }}-${{ matrix.python-version }}-${{ hashFiles('**/setup.py')}}
restore-keys: ${{ matrix.os }}-${{ matrix.python-version }}-
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install latest SAHI package
run: >
pip install --upgrade --force-reinstall sahi
- name: Install MMDetection
run: >
pip install mmcv-full==1.2.5 mmdet==${{ matrix.mmdet-version }}
- name: Test with unittest
run: |
python -m unittest
- name: Test SAHI scripts for mmdet < 2.9.0
if: matrix.mmdet-version == '2.8.0'
run: |
# predict_folder
python scripts/predict_folder.py --image_dir tests/data/coco_utils/ --visual_output_dir outputs/ --model_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_2x_coco_20200131-fdb43119.pth --config_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_1x_coco.py
- name: Test SAHI scripts for mmdet == 2.9.0
if: matrix.mmdet-version == '2.9.0'
run: |
# predict_folder
python scripts/predict_folder.py --image_dir tests/data/coco_utils/ --visual_output_dir outputs/ --model_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_2x_coco_20200131-fdb43119.pth --config_path tests/data/models/mmdet_retinanet/retinanet_r50_fpn_1x_coco_v290.py
2 changes: 1 addition & 1 deletion sahi/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.2.4"
__version__ = "0.2.5"
6 changes: 5 additions & 1 deletion sahi/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def predict_folder(
visual_bbox_thickness: int = 1,
visual_text_size: float = 1,
visual_text_thickness: int = 1,
visual_export_format: str = "png",
verbose: int = 1,
):
"""
Expand Down Expand Up @@ -311,6 +312,8 @@ def predict_folder(
visual_bbox_thickness: int
visual_text_size: float
visual_text_thickness: int
visual_export_format: str
Can be specified as 'jpg' or 'png'
verbose: int
0: no print
1: print slice/prediction durations, number of slices, model loading/file exporting durations
Expand Down Expand Up @@ -377,7 +380,7 @@ def predict_folder(
image=image,
detection_model=detection_model,
shift_amount=[0, 0],
full_image_size=None,
full_shape=None,
merger=None,
matcher=None,
verbose=0,
Expand Down Expand Up @@ -414,6 +417,7 @@ def predict_folder(
text_th=visual_text_thickness,
output_dir=visual_output_dir,
file_name=filename_without_extension,
export_format=visual_export_format,
)
time_end = time.time() - time_start
durations_in_seconds["export_files"] = time_end
Expand Down
4 changes: 3 additions & 1 deletion sahi/utils/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def visualize_object_predictions(
color: tuple = (0, 0, 0),
output_dir: str = "",
file_name: str = "prediction_visual",
export_format: str = "png",
):
"""
Visualizes prediction category names, bounding boxes over the source image
Expand All @@ -207,6 +208,7 @@ def visualize_object_predictions(
color: annotation color in the form: (0, 255, 0)
output_dir: directory for resulting visualization to be exported
file_name: exported file will be saved as: output_dir+file_name+".png"
export_format: can be specified as 'jpg' or 'png'
"""
elapsed_time = time.time()
# deepcopy image so that original is not altered
Expand Down Expand Up @@ -258,7 +260,7 @@ def visualize_object_predictions(
# create output folder if not present
create_dir(output_dir)
# save inference result
save_path = os.path.join(output_dir, file_name + ".png")
save_path = os.path.join(output_dir, file_name + "." + export_format)
cv2.imwrite(save_path, cv2.cvtColor(image, cv2.COLOR_RGB2BGR))
elapsed_time = time.time() - elapsed_time
return {"image": image, "elapsed_time": elapsed_time}
Expand Down
6 changes: 4 additions & 2 deletions scripts/predict_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--image_dir", type=str, default="", help="image directory")
parser.add_argument(
"--model_name",
type=str,
Expand All @@ -25,7 +26,7 @@
parser.add_argument(
"--prediction_score_threshold",
type=float,
default="",
default=0.25,
help="all predictions with score < prediction_score_threshold will be discarded",
)
parser.add_argument(
Expand All @@ -46,7 +47,6 @@
default=None,
help='remap category ids based on category names, after performing inference e.g. {"car": 3}',
)
parser.add_argument("--image_dir", type=str, default="", help="image directory")
parser.add_argument(
"--visual_output_dir", type=str, default="", help="output visual directory"
)
Expand All @@ -62,6 +62,7 @@
parser.add_argument("--overlap_height_ratio", type=float, default=0.2)
parser.add_argument("--overlap_width_ratio", type=float, default=0.2)
parser.add_argument("--match_iou_threshold", type=float, default=0.5)
parser.add_argument("--visual_export_format", type=str, default="png")

opt = parser.parse_args()

Expand All @@ -86,4 +87,5 @@
overlap_height_ratio=opt.overlap_height_ratio,
overlap_width_ratio=opt.overlap_width_ratio,
match_iou_threshold=opt.match_iou_threshold,
visual_export_format=opt.visual_export_format,
)

0 comments on commit 970058b

Please sign in to comment.