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

Mmdet scnet support #921

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
69 changes: 18 additions & 51 deletions docs/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
from loguru import logger

PAGES = {
"parser.md": [
"icevision.parsers.Parser",
"icevision.parsers.Parser.parse",
],
"parser.md": ["icevision.parsers.Parser", "icevision.parsers.Parser.parse",],
"dataset.md": [
"icevision.data.dataset.Dataset",
"icevision.data.dataset.Dataset.from_images",
Expand Down Expand Up @@ -130,9 +127,7 @@ def copy_examples(examples_dir, destination_dir):

from_to = f"{file} -> {destination_file}"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Copying Examples: {}</></>",
from_to,
"INFO", "️<green><bold>Copying Examples: {}</></>", from_to,
)


Expand Down Expand Up @@ -197,9 +192,7 @@ def py_to_nb_md(dest_dir):
def nb_to_md(src_dir, nb_folder, dest_dir):
notebooks_dir = src_dir / nb_folder
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Notebooks folder: {}</></>",
notebooks_dir,
"INFO", "️<green><bold>Notebooks folder: {}</></>", notebooks_dir,
)

for file_path in os.listdir(notebooks_dir):
Expand All @@ -221,18 +214,14 @@ def nb_to_md(src_dir, nb_folder, dest_dir):
tutobooks.nb_to_md(nb_path, md_path, images_path)
from_to = f"{file_name} -> {file_name_md}"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Converting to Notebook: {}</></>",
from_to,
"INFO", "️<green><bold>Converting to Notebook: {}</></>", from_to,
)


def examples_to_md(dest_dir):
examples_dir = icevision_dir / "examples"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Examples folder: {}</></>",
examples_dir,
"INFO", "️<green><bold>Examples folder: {}</></>", examples_dir,
)

for file_path in os.listdir(examples_dir):
Expand Down Expand Up @@ -266,9 +255,7 @@ def generate(dest_dir: Path):
if os.path.exists(dest_dir):
print("Removing sources folder:", dest_dir)
logger.opt(colors=True).log(
"INFO",
"️<magenta><bold>\nRemoving sources folder: {}</></>",
dest_dir,
"INFO", "️<magenta><bold>\nRemoving sources folder: {}</></>", dest_dir,
)
shutil.rmtree(dest_dir)
os.makedirs(dest_dir)
Expand All @@ -277,9 +264,7 @@ def generate(dest_dir: Path):
copy_tree(str(icevision_dir / "images"), str(template_images_dir))
from_to = f"root/images -> docs/images"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>\nCopying images folder: {}</></>",
from_to,
"INFO", "️<green><bold>\nCopying images folder: {}</></>", from_to,
)

# Generate APIs Documentation
Expand All @@ -298,9 +283,7 @@ def generate(dest_dir: Path):
shutil.copyfile("manifest.webmanifest", dest_dir / "manifest.webmanifest")
from_to = f"root/manifest.webmanifest -> docs/manifest.webmanifest"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>\nCopying webmanifest file: {}</></>",
from_to,
"INFO", "️<green><bold>\nCopying webmanifest file: {}</></>", from_to,
)

# Auto generate the index.md file using the README.md file and the index.md file in templates folder
Expand All @@ -321,9 +304,7 @@ def generate(dest_dir: Path):
filename for filename in os.listdir(dir_to_search) if filename.endswith(".md")
]
logger.opt(colors=True).log(
"INFO",
"️<green><bold>\nCopying .md files root folder: {}</></>",
fnamelist,
"INFO", "️<green><bold>\nCopying .md files root folder: {}</></>", fnamelist,
)

for fname in fnamelist:
Expand All @@ -332,9 +313,7 @@ def generate(dest_dir: Path):
shutil.copyfile(fname_src, fname_dst)
from_to = f"{fname} -> {fname.lower()}"
logger.opt(colors=True).log(
"INFO",
"️<light-blue><bold>file: {}</></>",
from_to,
"INFO", "️<light-blue><bold>file: {}</></>", from_to,
)

# Copy static .md files from the docs folder
Expand All @@ -353,9 +332,7 @@ def generate(dest_dir: Path):
shutil.copyfile(fname_src, fname_dst)
from_to = f"{fname} -> {fname.lower()}"
logger.opt(colors=True).log(
"INFO",
"️<light-blue><bold>Copying files: {}</></>",
from_to,
"INFO", "️<light-blue><bold>Copying files: {}</></>", from_to,
)

# Copy images folder from the template folder to the destination folder
Expand All @@ -366,9 +343,7 @@ def generate(dest_dir: Path):
copy_tree(str(template_images_dir), str(dest_images_dir))
from_to = f"{template_images_dir} -> {dest_images_dir}"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Copying Images: {}</></>",
from_to,
"INFO", "️<green><bold>Copying Images: {}</></>", from_to,
)

# Copy css folder
Expand All @@ -377,9 +352,7 @@ def generate(dest_dir: Path):
copy_tree(css_dir_src, css_dir_dest)
from_to = f"{css_dir_src} -> {css_dir_dest}"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Copying CSS files: {}</></>",
from_to,
"INFO", "️<green><bold>Copying CSS files: {}</></>", from_to,
)

# Copy js folder
Expand All @@ -389,9 +362,7 @@ def generate(dest_dir: Path):
copy_tree(js_dir_src, js_dir_dest)
from_to = f"{js_dir_src} -> {js_dir_dest}"
logger.opt(colors=True).log(
"INFO",
"️<green><bold>Copying JS files: {}</></>",
from_to,
"INFO", "️<green><bold>Copying JS files: {}</></>", from_to,
)

# Generate .md files form Jupyter Notebooks located in the /notebooks folder
Expand All @@ -402,26 +373,22 @@ def generate(dest_dir: Path):

# albumentations
shutil.copyfile(
icevision_dir / "icevision/tfms/README.md",
dest_dir / "albumentations.md",
icevision_dir / "icevision/tfms/README.md", dest_dir / "albumentations.md",
)

# Models
shutil.copyfile(
icevision_dir / "icevision/models/README.md",
dest_dir / "models.md",
icevision_dir / "icevision/models/README.md", dest_dir / "models.md",
)

# Backbones
shutil.copyfile(
icevision_dir / "icevision/backbones/README.md",
dest_dir / "backbones.md",
icevision_dir / "icevision/backbones/README.md", dest_dir / "backbones.md",
)

# README DOCS
shutil.copyfile(
icevision_dir / "docs/README.md",
dest_dir / "readme_mkdocs.md",
icevision_dir / "docs/README.md", dest_dir / "readme_mkdocs.md",
)


Expand Down
5 changes: 1 addition & 4 deletions icevision/core/components/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ def set_task_components(self, components: Sequence[TaskComponent]):

# TODO: rename reduce_on_all_tasks_components
def reduce_on_components(
self,
fn_name: str,
reduction: Optional[str] = None,
**fn_kwargs,
self, fn_name: str, reduction: Optional[str] = None, **fn_kwargs,
) -> Dict[str, Any]:
results = {}
for task, composite in self.task_composites.items():
Expand Down
4 changes: 1 addition & 3 deletions icevision/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ class Dataset:
"""

def __init__(
self,
records: List[dict],
tfm: Optional[Transform] = None,
self, records: List[dict], tfm: Optional[Transform] = None,
):
self.records = records
self.tfm = tfm
Expand Down
4 changes: 1 addition & 3 deletions icevision/metrics/confusion_matrix/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def finalize(self):
assert len(self.target_labels) == len(self.predicted_labels)
label_ids = list(self.class_map._class2id.values())
self.confusion_matrix = sklearn.metrics.confusion_matrix(
y_true=self.target_labels,
y_pred=self.predicted_labels,
labels=label_ids,
y_true=self.target_labels, y_pred=self.predicted_labels, labels=label_ids,
)
if self.print_summary:
print(self.confusion_matrix)
Expand Down
5 changes: 1 addition & 4 deletions icevision/models/base_show_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,5 @@ def base_show_results(
preds = predict_fn(model, records, **predict_kwargs)

show_preds(
preds,
denormalize_fn=denormalize_fn,
ncols=ncols,
show=show,
preds, denormalize_fn=denormalize_fn, ncols=ncols, show=show,
)
4 changes: 1 addition & 3 deletions icevision/models/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ def model_from_checkpoint(
img_size=None,
map_location=None,
strict=False,
revise_keys=[
(r"^module\.", ""),
],
revise_keys=[(r"^module\.", ""),],
eval_mode=True,
logger=None,
):
Expand Down
6 changes: 1 addition & 5 deletions icevision/models/fastai/unet/fastai/learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ def learner(
cbs = L(UnetCallback()) + L(cbs)

learn = adapted_fastai_learner(
dls=dls,
model=model,
cbs=cbs,
loss_func=loss_func,
**kwargs,
dls=dls, model=model, cbs=cbs, loss_func=loss_func, **kwargs,
)

return learn
4 changes: 1 addition & 3 deletions icevision/models/fastai/unet/lightning/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def validation_step(self, batch, batch_idx):
loss = self.loss_func(preds, yb)

preds = unet.convert_raw_predictions(
batch=xb,
raw_preds=preds,
records=records,
batch=xb, raw_preds=preds, records=records,
)

self.accumulate_metrics(preds)
Expand Down
5 changes: 1 addition & 4 deletions icevision/models/fastai/unet/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ def _predict_batch(

raw_preds = model(images)
preds = convert_raw_predictions(
batch=batch,
raw_preds=raw_preds,
records=records,
keep_images=keep_images,
batch=batch, raw_preds=raw_preds, records=records, keep_images=keep_images,
)

return preds
Expand Down
4 changes: 1 addition & 3 deletions icevision/models/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ def _end2end_detect(


def process_bbox_predictions(
pred: Prediction,
img: PIL.Image.Image,
transforms: List[Any],
pred: Prediction, img: PIL.Image.Image, transforms: List[Any],
) -> List[Dict[str, Any]]:
"""
Postprocess prediction.
Expand Down
7 changes: 2 additions & 5 deletions icevision/models/interpretation.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ def _loop(self, dl, model, losses_stats, device):
return samples_plus_losses, losses_stats

def get_losses(
self,
model: nn.Module,
dataset: Dataset,
self, model: nn.Module, dataset: Dataset,
) -> Tuple[List[dict], dict]:
"""
Gets a dataset and a model as input and returns losses calculated per image + losses stats.
Expand Down Expand Up @@ -244,8 +242,7 @@ def plot_top_losses(
]

show_preds(
preds=sorted_preds[:n_samples],
annotations=anns[:n_samples],
preds=sorted_preds[:n_samples], annotations=anns[:n_samples],
)
model.train()
return sorted_samples, sorted_preds, losses_stats
Expand Down
3 changes: 3 additions & 0 deletions icevision/models/mmdet/common/bbox/two_stage/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ def model(
num_classes: int,
checkpoints_path: Optional[Union[str, Path]] = "checkpoints",
force_download=False,
cfg_options=None,
) -> nn.Module:

cfg, weights_path = create_model_config(
backbone=backbone,
pretrained=backbone.pretrained,
checkpoints_path=checkpoints_path,
force_download=force_download,
cfg_options=None,
)

return build_model(
Expand All @@ -29,4 +31,5 @@ def model(
pretrained=backbone.pretrained,
checkpoints_path=checkpoints_path,
force_download=force_download,
cfg_options=cfg_options,
)
6 changes: 1 addition & 5 deletions icevision/models/mmdet/fastai/learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def mmdetection_learner(
"""

learn = adapted_fastai_learner(
dls=dls,
model=model,
cbs=cbs,
loss_func=loss_fn,
**learner_kwargs,
dls=dls, model=model, cbs=cbs, loss_func=loss_fn, **learner_kwargs,
)

return learn
1 change: 1 addition & 0 deletions icevision/models/mmdet/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from icevision.models.mmdet.models import yolox
from icevision.models.mmdet.models import retinanet
from icevision.models.mmdet.models import fcos
from icevision.models.mmdet.models import scnet
from icevision.models.mmdet.models import vfnet
from icevision.models.mmdet.models import cornernet
from icevision.models.mmdet.models import centripetalnet
Expand Down
24 changes: 24 additions & 0 deletions icevision/models/mmdet/models/scnet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from icevision.models.mmdet.models.scnet import backbones
from icevision.models.mmdet.common.bbox.two_stage import *
from icevision.models.interpretation import Interpretation, _move_to_device
from icevision.models.mmdet.common.interpretation_utils import (
sum_losses_mmdet,
loop_mmdet,
)

_LOSSES_DICT = {
"loss_rpn_cls": [],
"loss_rpn_bbox": [],
"loss_cls": [],
"loss_bbox": [],
"loss_mask": [],
"loss_total": [],
}
interp = Interpretation(
losses_dict=_LOSSES_DICT,
valid_dl=valid_dl,
infer_dl=infer_dl,
predict_from_dl=predict_from_dl,
)

interp._loop = loop_mmdet
1 change: 1 addition & 0 deletions icevision/models/mmdet/models/scnet/backbones/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from icevision.models.mmdet.models.scnet.backbones.resnet_fpn import *
Loading