Skip to content

Commit

Permalink
use augs functions from sly lib
Browse files Browse the repository at this point in the history
  • Loading branch information
arzamnik committed Apr 11, 2022
1 parent dfbc8c0 commit dae490e
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 85 deletions.
2 changes: 0 additions & 2 deletions requirements.txt

This file was deleted.

3 changes: 3 additions & 0 deletions serve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Supported backbones:
- [x] [Vision Transformer (ICLR'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/vit)
- [x] [Swin Transformer (ICCV'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/swin)
- [x] [Twins (NeurIPS'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/twins)
- [x] [BEiT (ICLR'2022)](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/beit)
- [x] [ConvNeXt (CVPR'2022)](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/convnext)

Supported methods:

Expand Down Expand Up @@ -80,6 +82,7 @@ Supported methods:
- [x] [DPT (ArXiv'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/dpt)
- [x] [Segmenter (ICCV'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/segmenter)
- [x] [SegFormer (NeurIPS'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/segformer)
- [x] [K-Net (NeurIPS'2021)](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/knet)


# How to Run
Expand Down
2 changes: 1 addition & 1 deletion serve/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"semantic segmentation"
],
"description": "Deploy model as REST API service",
"docker_image": "supervisely/mmseg:1.2.0",
"docker_image": "supervisely/mmseg:1.3.0",
"main_script": "serve/src/sly_serve.py",
"gui_template": "serve/src/gui.html",
"task_location": "application_sessions",
Expand Down
3 changes: 3 additions & 0 deletions train/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Supported backbones:
- [x] [Vision Transformer (ICLR'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/vit)
- [x] [Swin Transformer (ICCV'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/swin)
- [x] [Twins (NeurIPS'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/twins)
- [x] [BEiT (ICLR'2022)](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/beit)
- [x] [ConvNeXt (CVPR'2022)](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/convnext)

Supported methods:

Expand Down Expand Up @@ -86,6 +88,7 @@ Supported methods:
- [x] [DPT (ArXiv'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/dpt)
- [x] [Segmenter (ICCV'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/segmenter)
- [x] [SegFormer (NeurIPS'2021)](https://github.com/open-mmlab/mmsegmentation/tree/master/configs/segformer)
- [x] [K-Net (NeurIPS'2021)](https://github.com/open-mmlab/mmsegmentation/blob/master/configs/knet)


# How to Run
Expand Down
2 changes: 1 addition & 1 deletion train/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"semantic segmentation"
],
"description": "Dashboard to configure, start and monitor training",
"docker_image": "supervisely/mmseg:1.2.0",
"docker_image": "supervisely/mmseg:1.3.0",
"main_script": "train/src/main.py",
"gui_template": "train/src/gui.html",
"task_location": "workspace_tasks",
Expand Down
77 changes: 0 additions & 77 deletions train/src/sly_imgaugs.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
import supervisely as sly
from mmseg.datasets.builder import PIPELINES
from supervisely.sly_logger import logger
import imgaug.augmenters as iaa


def get_function(category_name, aug_name):
try:
submodule = getattr(iaa, category_name)
aug_f = getattr(submodule, aug_name)
return aug_f
except Exception as e:
logger.error(repr(e))
# raise e
return None

def build_pipeline(aug_infos, random_order=False):
pipeline = []
for aug_info in aug_infos:
category_name = aug_info["category"]
aug_name = aug_info["name"]
params = aug_info["params"]
for param_name, param_val in params.items():
if isinstance(param_val, dict):
if "x" in param_val.keys() and "y" in param_val.keys():
param_val["x"] = tuple(param_val["x"])
param_val["y"] = tuple(param_val["y"])
elif isinstance(param_val, list):
params[param_name] = tuple(param_val)

aug_func = get_function(category_name, aug_name)

aug = aug_func(**params)

sometimes = aug_info.get("sometimes", None)
if sometimes is not None:
aug = iaa.meta.Sometimes(sometimes, aug)
pipeline.append(aug)
augs = iaa.Sequential(pipeline, random_order=random_order)
return augs


def aug_to_python(aug_info):
pstr = ""
for name, value in aug_info["params"].items():
v = value
if type(v) is list: #name != 'nb_iterations' and
v = (v[0], v[1])
elif type(v) is dict and "x" in v.keys() and "y" in v.keys():
v = {"x": (v["x"][0], v["x"][1]), "y": (v["y"][0], v["y"][1])}

if type(value) is str:
pstr += f"{name}='{v}', "
else:
pstr += f"{name}={v}, "
method_py = f"iaa.{aug_info['category']}.{aug_info['name']}({pstr[:-2]})"

res = method_py
if "sometimes" in aug_info:
res = f"iaa.Sometimes({aug_info['sometimes']}, {method_py})"
return res


def pipeline_to_python(aug_infos, random_order=False):
template = \
"""import imgaug.augmenters as iaa
seq = iaa.Sequential([
{}
], random_order={})
"""
py_lines = []
for info in aug_infos:
line = aug_to_python(info)
_validate = info["python"]
if line != _validate:
raise ValueError("Generated python line differs from the one from config: \n\n{!r}\n\n{!r}"
.format(line, _validate))
py_lines.append(line)
res = template.format('\t' + ',\n\t'.join(py_lines), random_order)
return res

@PIPELINES.register_module()
class SlyImgAugs(object):
def __init__(self, config_path):
Expand Down
5 changes: 2 additions & 3 deletions train/src/ui/augs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sly_globals as g
from supervisely.app.v1.widgets.compare_gallery import CompareGallery
import input_project
from sly_imgaugs import build_pipeline, pipeline_to_python


_templates = [
Expand Down Expand Up @@ -46,8 +45,8 @@

def _load_template(json_path):
config = sly.json.load_json_file(json_path)
pipeline = build_pipeline(config["pipeline"], random_order=config["random_order"]) # to validate
py_code = pipeline_to_python(config["pipeline"], config["random_order"])
pipeline = sly.imgaug_utils.build_pipeline(config["pipeline"], random_order=config["random_order"]) # to validate
py_code = sly.imgaug_utils.pipeline_to_python(config["pipeline"], config["random_order"])

return pipeline, py_code, config

Expand Down
1 change: 0 additions & 1 deletion train/src/ui/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ def prepare_segmentation_data(state, img_dir, ann_dir, palette):
shutil.move(os.path.join(temp_project_seg_dir, dataset, img_dir, filename),
os.path.join(g.project_seg_dir, img_dir))

shutil.rmtree(g.project_dir)
shutil.rmtree(temp_project_seg_dir)
g.api.app.set_field(g.task_id, "state.preparingData", False)

Expand Down

0 comments on commit dae490e

Please sign in to comment.