Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
added new tests, refered to detectron2 library.
  • Loading branch information
abhi-glitchhg committed Apr 13, 2022
1 parent 1bfa3ff commit 1bed82f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[settings]
known_third_party = cloudpickle,cv2,einops,numpy,omegaconf,setuptools,timm,torch,torchvision,yaml
known_third_party = cloudpickle,cv2,einops,numpy,omegaconf,pytest,setuptools,timm,torch,torchvision,yaml
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
Expand Down
1 change: 0 additions & 1 deletion tests/root_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@
# modification above won't affect future imports
from .dir1.dir1_b import dir1b_dict, dir1b_str


lazyobj = L(count)(x=dir1a_str, y=dir1b_str)
67 changes: 50 additions & 17 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import pytest
import torch

from vformer.config import LazyCall, instantiate, get_config
from vformer.models import PVTSegmentation, SwinTransformer, VanillaViT, ViViTModel2
import os
import tempfile
from itertools import count

from vformer.config import LazyConfig, LazyCall as L
import pytest
import torch
from omegaconf import DictConfig

from vformer.config import LazyCall
from vformer.config import LazyCall as L
from vformer.config import LazyConfig, instantiate
from vformer.models import (
PVTSegmentation,
SwinTransformer,
VanillaViT,
ViViTModel2,
classification,
)


def test_lazy():
# classification models
Expand Down Expand Up @@ -55,23 +62,30 @@ def test_lazy():
assert vivit(rand_vdo_tensor).shape == (32, 10)


def test_raise_errors():
a = "strings"
with pytest.raises(TypeError):
obj = LazyConfig(a) # only callable objects

root_filename = os.path.join(os.path.dirname(__file__), "root_cfg.py")

def test_load():
root_filename = os.path.join(os.path.dirname(__file__), "root_cfg.py")
cfg = LazyConfig.load(root_filename)

assert cfg.dir1a_dict.a == "modified"

assert cfg.dir1b_dict.a == 1
assert cfg.dir1b_dict.a == 1
assert cfg.lazyobj.x == "base_a_1"

cfg.lazyobj.x = "new_x"
# reload
cfg = LazyConfig.load(root_filename)
assert cfg.lazyobj.x == "base_a_1"
assert cfg.lazyobj.x == "base_a_1"


def test_save_load():
root_filename = os.path.join(os.path.dirname(__file__), "root_cfg.py")

cfg = LazyConfig.load(root_filename)
with tempfile.TemporaryDirectory(prefix="vformer") as d:
fname = os.path.join(d, "test_config.yaml")
Expand All @@ -85,42 +99,61 @@ def test_save_load():
# the rest are equal
assert cfg == cfg2


def test_failed_save():
cfg = DictConfig({"x": lambda: 3}, flags={"allow_objects": True})
with tempfile.TemporaryDirectory(prefix="vformer") as d:
fname = os.path.join(d, "test_config.yaml")
LazyConfig.save(cfg, fname)
assert os.path.exists(fname) == True
assert os.path.exists(fname + ".pkl") == True
assert os.path.exists(fname + ".pkl") == True


def test_overrides():
root_filename = os.path.join(os.path.dirname(__file__), "root_cfg.py")

cfg = LazyConfig.load(root_filename)
LazyConfig.apply_overrides(cfg, ["lazyobj.x=123", 'dir1b_dict.a="123"'])
assert cfg.dir1b_dict.a == "123"
assert cfg.lazyobj.x == 123


def test_invalid_overrides():
root_filename = os.path.join(os.path.dirname(__file__), "root_cfg.py")

cfg = LazyConfig.load(root_filename)
with pytest.raises(KeyError):
LazyConfig.apply_overrides(cfg, ["lazyobj.x.xxx=123"])


def test_to_py():
root_filename = os.path.join(os.path.dirname(__file__), "root_cfg.py")

cfg = LazyConfig.load(root_filename)
cfg.lazyobj.x = {"a": 1, "b": 2, "c": L(count)(x={"r": "a", "s": 2.4, "t": [1, 2, 3, "z"]})}
cfg.lazyobj.x = {
"a": 1,
"b": 2,
"c": L(count)(x={"r": "a", "s": 2.4, "t": [1, 2, 3, "z"]}),
}
cfg.list = ["a", 1, "b", 3.2]
py_str = LazyConfig.to_py(cfg)
expected = """cfg.dir1a_dict.a = "modified"
cfg.dir1a_dict.b = 2
cfg.dir1b_dict.a = 1
cfg.dir1b_dict.b = 2
cfg.lazyobj = itertools.count(
x={
"a": 1,
"b": 2,
"c": itertools.count(x={"r": "a", "s": 2.4, "t": [1, 2, 3, "z"]}),
},
y="base_a_1_from_b",
x={
"a": 1,
"b": 2,
"c": itertools.count(x={"r": "a", "s": 2.4, "t": [1, 2, 3, "z"]}),
},
y="base_a_1_from_b",
)
cfg.list = ["a", 1, "b", 3.2]
"""
assert py_str == expected

root_filename = os.path.join(os.path.dirname(__file__), "testing.yaml")
cfg = LazyConfig.load(root_filename)
obj = LazyConfig.to_py(cfg)
print(obj)
4 changes: 2 additions & 2 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

def test_VanillaViT():

model = MODEL_REGISTRY.get("trial")(
model = MODEL_REGISTRY.get("VanillaViT")(
img_size=256, patch_size=32, n_classes=10, in_channels=3
)
out = model(img_3channels_256)
assert out.shape == (2, 10)
del model

model = MODEL_REGISTRY.get("trial")(
model = MODEL_REGISTRY.get("VanillaViT")(
img_size=256,
patch_size=32,
n_classes=10,
Expand Down
2 changes: 2 additions & 0 deletions tests/testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x : 3
y : 4
1 change: 1 addition & 0 deletions vformer/config/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid
from collections import abc
from typing import Any

from omegaconf import DictConfig, ListConfig


Expand Down
15 changes: 11 additions & 4 deletions vformer/config/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ def load(filename: str, keys: Union[None, str, Tuple[str, ...]] = None):
has_keys = keys is not None
filename = filename.replace("/./", "/") # redundant
if os.path.splitext(filename)[1] not in [".py", ".yaml", ".yml"]:
raise ValueError(f"Config file {filename} has to be a python file.")
raise ValueError(
f"Config file {filename} is not supported, supported file types are : [`.py`, `.yaml`]."
)
if filename.endswith(".py"):
_validate_py_syntax(filename)

Expand All @@ -169,8 +171,14 @@ def load(filename: str, keys: Union[None, str, Tuple[str, ...]] = None):
exec(compile(content, filename, "exec"), module_namespace)

ret = module_namespace
elif filename.endswith(".yaml"):

with open(filename) as f:
obj = yaml.unsafe_load(f)
ret = OmegaConf.create(obj, flags={"allow_objects": True})

else:
raise NotImplementedError("Only python files supported for now. ")
raise NotImplementedError("Only python and yaml files supported for now. ")

if has_keys:
if isinstance(keys, str):
Expand Down Expand Up @@ -364,8 +372,7 @@ def get_config_file(config_path):
Returns:
str: the real path to the config file.
"""
cfg_file = open( os.path.join("vformer","configs", config_path)
)
cfg_file = open(os.path.join("vformer", "configs", config_path))
if not os.path.exists(cfg_file):
raise RuntimeError("{} not available in configs!".format(config_path))
return cfg_file
Expand Down
6 changes: 3 additions & 3 deletions vformer/models/dense/dpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(
384,
768,
)
self.model = MODEL_REGISTRY.get("trial")(
self.model = MODEL_REGISTRY.get("VanillaViT")(
img_size=img_size,
patch_size=16,
embedding_dim=768,
Expand All @@ -131,7 +131,7 @@ def __init__(
elif backbone == "vitl16":

scratch_in_features = (256, 512, 1024, 1024)
self.model = MODEL_REGISTRY.get("trial")(
self.model = MODEL_REGISTRY.get("VanillaViT")(
img_size=img_size,
patch_size=16,
embedding_dim=1024,
Expand All @@ -148,7 +148,7 @@ def __init__(
elif backbone == "vit_tiny":

scratch_in_features = (48, 96, 144, 192)
self.model = MODEL_REGISTRY.get("trial")(
self.model = MODEL_REGISTRY.get("VanillaViT")(
img_size=img_size,
patch_size=16,
embedding_dim=192,
Expand Down

0 comments on commit 1bed82f

Please sign in to comment.