Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue 144 replace typer with fire for comand line args #145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ The design of anchors is critical for the performance of one-stage detectors. Py
```bash
pyodi train-config generation \
$TINY_COCO_ANIMAL/annotations/train.json \
--input-size 1280 720 \
--input-size [1280,720] \
--n-ratios 3 --n-scales 3
```

Expand All @@ -101,7 +101,7 @@ Pyodi evaluation app has been designed with the aim of providing a simple tool t
pyodi train-config evaluation \
$TINY_COCO_ANIMAL/annotations/train.json \
$TINY_COCO_ANIMAL/resources/anchor_config.py \
--input-size 1280 720
--input-size [1280,720]
```

![Anchor overlap plot](docs/images/train-config-evaluation/overlap.png)
Expand Down
15 changes: 5 additions & 10 deletions pyodi/apps/coco/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import typer

from pyodi.apps.coco.coco_merge import coco_merge
from pyodi.apps.coco.coco_split import property_split, random_split

coco_app = typer.Typer()
coco_app.command("merge")(coco_merge)
coco_app.command("random-split")(random_split)
coco_app.command("property-split")(property_split)


if __name__ == "__main__":
coco_app()
coco_app = {
"merge": coco_merge,
"random-split": random_split,
"property-split": property_split,
}
4 changes: 0 additions & 4 deletions pyodi/apps/coco/coco_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
import json
from typing import Any, Dict

import typer
from loguru import logger

app = typer.Typer()


@logger.catch
@app.command()
def coco_merge(input_extend: str, input_add: str, output_file: str,) -> str:
"""Merge COCO annotation files.

Expand Down
5 changes: 0 additions & 5 deletions pyodi/apps/coco/coco_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@
from typing import List

import numpy as np
import typer
from loguru import logger

app = typer.Typer()


@logger.catch # noqa: C901
@app.command()
def property_split(
annotations_file: str, output_filename: str, split_config_file: str,
) -> List[str]:
Expand Down Expand Up @@ -160,7 +156,6 @@ def property_split(


@logger.catch
@app.command()
def random_split(
annotations_file: str,
output_filename: str,
Expand Down
13 changes: 4 additions & 9 deletions pyodi/apps/crops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import typer

from pyodi.apps.crops.crops_merge import crops_merge
from pyodi.apps.crops.crops_split import crops_split

crops_app = typer.Typer()
crops_app.command("merge")(crops_merge)
crops_app.command("split")(crops_split)


if __name__ == "__main__":
crops_app()
crops_app = {
"merge": crops_merge,
"split": crops_split,
}
8 changes: 0 additions & 8 deletions pyodi/apps/crops/crops_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
from pathlib import Path
from typing import Optional

import typer
from loguru import logger

from pyodi.core.nms import nms_predictions

app = typer.Typer()


@logger.catch
@app.command()
def crops_merge(
ground_truth_file: str,
output_file: str,
Expand Down Expand Up @@ -103,7 +99,3 @@ def crops_merge(
json.dump(new_ground_truth, f, indent=2)

return output_file


if __name__ == "__main__":
app()
8 changes: 0 additions & 8 deletions pyodi/apps/crops/crops_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from pathlib import Path
from typing import Dict, List

import typer
from loguru import logger
from PIL import Image

Expand All @@ -20,11 +19,8 @@
get_crops_corners,
)

app = typer.Typer()


@logger.catch
@app.command()
def crops_split(
ground_truth_file: str,
image_folder: str,
Expand Down Expand Up @@ -114,7 +110,3 @@ def crops_split(

with open(output_file, "w") as f:
json.dump(new_ground_truth, f, indent=2)


if __name__ == "__main__":
app()
8 changes: 0 additions & 8 deletions pyodi/apps/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@
import re
from typing import Optional

import typer
from loguru import logger
from pycocotools.cocoeval import COCOeval

from pyodi.core.utils import load_coco_ground_truth_from_StringIO

app = typer.Typer()


@logger.catch
@app.command()
def evaluation(
ground_truth_file: str, predictions_file: str, string_to_match: Optional[str] = None
) -> None:
Expand Down Expand Up @@ -79,7 +75,3 @@ def evaluation(
coco_eval.evaluate()
coco_eval.accumulate()
coco_eval.summarize()


if __name__ == "__main__":
app()
8 changes: 0 additions & 8 deletions pyodi/apps/ground_truth.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
from pathlib import Path
from typing import Optional, Tuple

import typer
from loguru import logger

from pyodi.core.boxes import add_centroids
Expand All @@ -60,11 +59,8 @@
from pyodi.plots.boxes import get_centroids_heatmap, plot_heatmap
from pyodi.plots.common import plot_scatter_with_histograms

app = typer.Typer()


@logger.catch
@app.command()
def ground_truth(
ground_truth_file: str,
show: bool = True,
Expand Down Expand Up @@ -131,7 +127,3 @@ def ground_truth(
output=output,
output_size=output_size,
)


if __name__ == "__main__":
app()
8 changes: 0 additions & 8 deletions pyodi/apps/paint_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@
from typing import Dict, Optional

import numpy as np
import typer
from loguru import logger
from matplotlib import cm as cm
from matplotlib import pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
from PIL import Image

app = typer.Typer()


@logger.catch
@app.command()
def paint_annotations(
ground_truth_file: str,
image_folder: str,
Expand Down Expand Up @@ -165,7 +161,3 @@ def paint_annotations(

plt.savefig(output_file)
plt.close()


if __name__ == "__main__":
app()
13 changes: 4 additions & 9 deletions pyodi/apps/train_config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import typer

from pyodi.apps.train_config.train_config_evaluation import train_config_evaluation
from pyodi.apps.train_config.train_config_generation import train_config_generation

train_config_app = typer.Typer()
train_config_app.command("evaluation")(train_config_evaluation)
train_config_app.command("generation")(train_config_generation)


if __name__ == "__main__":
train_config_app()
train_config_app = {
"generation": train_config_generation,
"evaluation": train_config_evaluation,
}
12 changes: 2 additions & 10 deletions pyodi/apps/train_config/train_config_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
pyodi train-config evaluation \\
$TINY_COCO_ANIMAL/annotations/train.json \\
$TINY_COCO_ANIMAL/resources/anchor_config.py \\
--input-size 1280 720
--input-size [1280,720]
```

The app provides four different plots:
Expand Down Expand Up @@ -74,7 +74,6 @@
from typing import Any, Dict, Optional, Tuple

import numpy as np
import typer
from loguru import logger

from pyodi.core.anchor_generator import AnchorGenerator
Expand All @@ -92,8 +91,6 @@
)
from pyodi.plots.evaluation import plot_overlap_result

app = typer.Typer()


def load_anchor_config_file(anchor_config_file: str) -> Dict[str, Any]:
"""Loads the `anchor_config_file`.
Expand Down Expand Up @@ -122,11 +119,10 @@ def load_anchor_config_file(anchor_config_file: str) -> Dict[str, Any]:


@logger.catch
@app.command()
def train_config_evaluation(
ground_truth_file: str,
anchor_config: str,
input_size: Tuple[int, int] = (1333, 800),
input_size: Tuple[int, int] = (1280, 720),
show: bool = True,
output: Optional[str] = None,
output_size: Tuple[int, int] = (1600, 900),
Expand Down Expand Up @@ -212,7 +208,3 @@ def train_config_evaluation(
plot_overlap_result(
df_annotations, show=show, output=output, output_size=output_size
)


if __name__ == "__main__":
app()
33 changes: 4 additions & 29 deletions pyodi/apps/train_config/train_config_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
```bash
pyodi train-config generation \\
$TINY_COCO_ANIMAL/annotations/train.json \\
--input-size 1280 720 \\
--input-size [1280,720] \\
--n-ratios 3 --n-scales 3
```

Expand All @@ -54,7 +54,7 @@
the number of anchors is goint to increase, which will influence training computational cost.

```bash
pyodi train-config generation annotations/train.json --input-size 1280 720 --n-ratios 4 --n-scales 3
pyodi train-config generation annotations/train.json --input-size [1280,720] --n-ratios 4 --n-scales 3
```

In plot below we can observe the result for `n_ratios` equal to four.
Expand Down Expand Up @@ -101,10 +101,8 @@
from typing import List, Optional, Tuple

import numpy as np
import typer
from loguru import logger

from pyodi.apps.train_config.train_config_evaluation import train_config_evaluation
from pyodi.core.anchor_generator import AnchorGenerator
from pyodi.core.boxes import (
filter_zero_area_bboxes,
Expand All @@ -120,23 +118,19 @@
)
from pyodi.plots.clustering import plot_clustering_results

app = typer.Typer()


@logger.catch
@app.command()
def train_config_generation(
ground_truth_file: str,
input_size: Tuple[int, int] = (1280, 720),
n_ratios: int = 3,
n_scales: int = 3,
strides: Optional[List[int]] = typer.Argument(None),
base_sizes: Optional[List[int]] = typer.Argument(None),
strides: Optional[List[int]] = None,
base_sizes: Optional[List[int]] = None,
show: bool = True,
output: Optional[str] = None,
output_size: Tuple[int, int] = (1600, 900),
keep_ratio: bool = False,
evaluate: bool = True,
) -> AnchorGenerator:
"""Computes optimal anchors for a given COCO dataset based on iou clustering.

Expand All @@ -152,9 +146,6 @@ def train_config_generation(
output: Output file where results are saved. Defaults to None.
output_size: Size of saved images. Defaults to (1600, 900).
keep_ratio: Whether to keep the aspect ratio or not. Defaults to False.
evaluate: Whether to evaluate or not the anchors. Check
[`pyodi train-config evaluation`][pyodi.apps.train_config.train_config_evaluation.train_config_evaluation]
for more information.

Returns:
Anchor generator instance.
Expand Down Expand Up @@ -230,20 +221,4 @@ def train_config_generation(
with open(output_file, "w") as f:
f.write(anchor_generator.to_string())

if evaluate:

anchor_config = dict(anchor_generator=anchor_generator.to_dict())
train_config_evaluation(
ground_truth_file=ground_truth_file,
anchor_config=anchor_config, # type: ignore
input_size=input_size,
show=show,
output=output,
output_size=output_size,
)

return anchor_generator


if __name__ == "__main__":
app()
Loading