From 1d4fa7eaef4f7fe639ad1f5f6c641dfe31a1231d Mon Sep 17 00:00:00 2001 From: lisab00 Date: Thu, 24 Aug 2023 08:53:03 +0200 Subject: [PATCH 01/18] draft --- tests/test_mk_exp_diva_mldg.py | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/test_mk_exp_diva_mldg.py diff --git a/tests/test_mk_exp_diva_mldg.py b/tests/test_mk_exp_diva_mldg.py new file mode 100644 index 000000000..709391240 --- /dev/null +++ b/tests/test_mk_exp_diva_mldg.py @@ -0,0 +1,48 @@ +""" +make an experiment using "diva" model +""" + +from domainlab.mk_exp import mk_exp +from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault +from domainlab.tasks.task_dset import mk_task_dset +from domainlab.models.model_diva import mk_diva +from domainlab.tasks.utils_task import ImSize +from domainlab.compos.vae.a_vae_builder import AbstractVAEBuilderChainNode +from domainlab.compos.vae.c_vae_builder_classif import ChainNodeVAEBuilderClassifCondPrior + + +""" +test mk experiment API with "diva" model and "mldg" trainer +""" + +# specify domain generalization task +task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") +task.add_domain(name="domain1", + dset_tr=DsetMNISTColorSoloDefault(0), + dset_val=DsetMNISTColorSoloDefault(1)) +task.add_domain(name="domain2", + dset_tr=DsetMNISTColorSoloDefault(2), + dset_val=DsetMNISTColorSoloDefault(3)) +task.add_domain(name="domain3", + dset_tr=DsetMNISTColorSoloDefault(4), + dset_val=DsetMNISTColorSoloDefault(5)) + +# specify parameters +zd_dim = 3 +zy_dim = 10 +zx_dim = 0 # TODO: specify zx_dim +chain_node_builder = ChainNodeVAEBuilderClassifCondPrior(successor_node=None).init_business(zd_dim, zx_dim, zy_dim) # TODO: fix chain_node_builder? +list_str_y = [f"class{i}" for i in range(zy_dim)] +list_d_tr = ["domain2", "domain3"] +gamma_d = 0 +gamma_y = 0 +beta_d = 0 +beta_x = 0 +beta_y = 0 + +# specify model to use +model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, gamma_y, beta_d, beta_x, beta_y) + +# make trainer for model +exp = mk_exp(task, model, trainer="mldg", test_domain="domain1", batchsize=32) +exp.execute(num_epochs=3) From 984c07c5c5b6580a1eb62706a39cd9c2b23b0c02 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 25 Aug 2023 10:22:32 +0200 Subject: [PATCH 02/18] rename file --- tests/{test_mk_exp_diva_mldg.py => test_mk_exp_diva.py} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename tests/{test_mk_exp_diva_mldg.py => test_mk_exp_diva.py} (90%) diff --git a/tests/test_mk_exp_diva_mldg.py b/tests/test_mk_exp_diva.py similarity index 90% rename from tests/test_mk_exp_diva_mldg.py rename to tests/test_mk_exp_diva.py index 709391240..dd01702ef 100644 --- a/tests/test_mk_exp_diva_mldg.py +++ b/tests/test_mk_exp_diva.py @@ -12,7 +12,7 @@ """ -test mk experiment API with "diva" model and "mldg" trainer +test mk experiment API with "diva" model """ # specify domain generalization task @@ -31,7 +31,7 @@ zd_dim = 3 zy_dim = 10 zx_dim = 0 # TODO: specify zx_dim -chain_node_builder = ChainNodeVAEBuilderClassifCondPrior(successor_node=None).init_business(zd_dim, zx_dim, zy_dim) # TODO: fix chain_node_builder? +chain_node_builder = ChainNodeVAEBuilderClassifCondPrior(successor_node=None).init_business(zd_dim, zx_dim, zy_dim) # TODO: chain_node_builder list_str_y = [f"class{i}" for i in range(zy_dim)] list_d_tr = ["domain2", "domain3"] gamma_d = 0 @@ -45,4 +45,4 @@ # make trainer for model exp = mk_exp(task, model, trainer="mldg", test_domain="domain1", batchsize=32) -exp.execute(num_epochs=3) +exp.execute(num_epochs=2) From a85a72df6774c55c0aace8302d803e7c7e4855f5 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 25 Aug 2023 15:46:54 +0200 Subject: [PATCH 03/18] updated parameters --- tests/test_mk_exp_diva.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index dd01702ef..8c9ff8da6 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -7,8 +7,6 @@ from domainlab.tasks.task_dset import mk_task_dset from domainlab.models.model_diva import mk_diva from domainlab.tasks.utils_task import ImSize -from domainlab.compos.vae.a_vae_builder import AbstractVAEBuilderChainNode -from domainlab.compos.vae.c_vae_builder_classif import ChainNodeVAEBuilderClassifCondPrior """ @@ -30,18 +28,19 @@ # specify parameters zd_dim = 3 zy_dim = 10 -zx_dim = 0 # TODO: specify zx_dim -chain_node_builder = ChainNodeVAEBuilderClassifCondPrior(successor_node=None).init_business(zd_dim, zx_dim, zy_dim) # TODO: chain_node_builder -list_str_y = [f"class{i}" for i in range(zy_dim)] +zx_dim = 30 +chain_node_builder = None # TODO: chain_node_builder +list_str_y = [f"class{i}" for i in range(task.dim_y)] list_d_tr = ["domain2", "domain3"] -gamma_d = 0 -gamma_y = 0 -beta_d = 0 -beta_x = 0 -beta_y = 0 +gamma_d = 1e5 +gamma_y = 7e5 +beta_d = 1e3 +beta_x = 1e3 +beta_y = 1e3 # specify model to use -model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, gamma_y, beta_d, beta_x, beta_y) +model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, + gamma_y, beta_d, beta_x, beta_y) # make trainer for model exp = mk_exp(task, model, trainer="mldg", test_domain="domain1", batchsize=32) From b204b3675c67ca2f0ddc486b66e07d2571277085 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Mon, 28 Aug 2023 15:55:47 +0200 Subject: [PATCH 04/18] create functions --- tests/test_mk_exp_diva.py | 80 ++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index 8c9ff8da6..aa02dab84 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -9,39 +9,49 @@ from domainlab.tasks.utils_task import ImSize -""" -test mk experiment API with "diva" model -""" +def test_mk_exp_diva(): + """ + test mk experiment API for "diva" model and trainers "mldg", "dial" + """ + + mk_exp_diva(trainer="mldg") + mk_exp_diva(trainer="dial") + + +def mk_exp_diva(trainer="mldg"): + """ + execute experiment with "diva" model and custom trainer + """ + + # specify domain generalization task + task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") + task.add_domain(name="domain1", + dset_tr=DsetMNISTColorSoloDefault(0), + dset_val=DsetMNISTColorSoloDefault(1)) + task.add_domain(name="domain2", + dset_tr=DsetMNISTColorSoloDefault(2), + dset_val=DsetMNISTColorSoloDefault(3)) + task.add_domain(name="domain3", + dset_tr=DsetMNISTColorSoloDefault(4), + dset_val=DsetMNISTColorSoloDefault(5)) + + # specify parameters + zd_dim = 3 + zy_dim = 10 + zx_dim = 30 + chain_node_builder = None # TODO: chain_node_builder + list_str_y = [f"class{i}" for i in range(task.dim_y)] + list_d_tr = ["domain2", "domain3"] + gamma_d = 1e5 + gamma_y = 7e5 + beta_d = 1e3 + beta_x = 1e3 + beta_y = 1e3 + + # specify model to use + model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, + gamma_y, beta_d, beta_x, beta_y) -# specify domain generalization task -task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") -task.add_domain(name="domain1", - dset_tr=DsetMNISTColorSoloDefault(0), - dset_val=DsetMNISTColorSoloDefault(1)) -task.add_domain(name="domain2", - dset_tr=DsetMNISTColorSoloDefault(2), - dset_val=DsetMNISTColorSoloDefault(3)) -task.add_domain(name="domain3", - dset_tr=DsetMNISTColorSoloDefault(4), - dset_val=DsetMNISTColorSoloDefault(5)) - -# specify parameters -zd_dim = 3 -zy_dim = 10 -zx_dim = 30 -chain_node_builder = None # TODO: chain_node_builder -list_str_y = [f"class{i}" for i in range(task.dim_y)] -list_d_tr = ["domain2", "domain3"] -gamma_d = 1e5 -gamma_y = 7e5 -beta_d = 1e3 -beta_x = 1e3 -beta_y = 1e3 - -# specify model to use -model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, - gamma_y, beta_d, beta_x, beta_y) - -# make trainer for model -exp = mk_exp(task, model, trainer="mldg", test_domain="domain1", batchsize=32) -exp.execute(num_epochs=2) + # make trainer for model + exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) + exp.execute(num_epochs=2) From b19a7567b9b7792fdfc0a85bd817cbd45dddd1ba Mon Sep 17 00:00:00 2001 From: lisab00 Date: Mon, 28 Aug 2023 09:12:39 +0200 Subject: [PATCH 05/18] added test file --- tests/test_mk_exp_hduva.py | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/test_mk_exp_hduva.py diff --git a/tests/test_mk_exp_hduva.py b/tests/test_mk_exp_hduva.py new file mode 100644 index 000000000..c9366aba5 --- /dev/null +++ b/tests/test_mk_exp_hduva.py @@ -0,0 +1,59 @@ +""" +make an experiment +""" +from domainlab.mk_exp import mk_exp +from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault +from domainlab.tasks.task_dset import mk_task_dset +from domainlab.models.model_hduva import mk_hduva +from domainlab.tasks.utils_task import ImSize + + +def test_mk_exp_hduva(): + """ + test mk experiment API with "hduva" model and trainers "mldg", "diva" + """ + + mk_exp_hduva(trainer="mldg") + mk_exp_hduva(trainer="diva") + + +def mk_exp_hduva(trainer="mldg"): + """ + execute experiment with "hduva" model and custom trainer + """ + + # specify domain generalization task + task = mk_task_dset(isize=ImSize(3, 28, 28), dim_y=10, taskna="custom_task") + task.add_domain(name="domain1", + dset_tr=DsetMNISTColorSoloDefault(0), + dset_val=DsetMNISTColorSoloDefault(1)) + task.add_domain(name="domain2", + dset_tr=DsetMNISTColorSoloDefault(2), + dset_val=DsetMNISTColorSoloDefault(3)) + task.add_domain(name="domain3", + dset_tr=DsetMNISTColorSoloDefault(4), + dset_val=DsetMNISTColorSoloDefault(5)) + + # specify backbone to use + chain_node_builder = None # TODO: chain_node_builder + zy_dim = 10 + zd_dim = 3 + list_str_y = [f"class{i}" for i in range(task.dim_y)] + list_d_tr = ["domain2", "domain3"] + gamma_d = 1e5 + gamma_y = 7e5 + beta_d = 1e3 + beta_x = 1e3 + beta_y = 1e3 + beta_t = 1e3 + device = None # TODO: specify device + zx_dim = 0 + topic_dim = 3 + + # specify model to use + model = mk_hduva()(chain_node_builder, zy_dim, zd_dim, list_str_y, list_d_tr, gamma_d, gamma_y, + beta_d, beta_x, beta_y, beta_t, device, zx_dim, topic_dim) + + # make trainer for model + exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) + exp.execute(num_epochs=2) From e4164b21d79a517d7169963698abb91a09c0bdbb Mon Sep 17 00:00:00 2001 From: lisab00 Date: Wed, 30 Aug 2023 14:54:52 +0200 Subject: [PATCH 06/18] test --- tests/test_mk_exp_diva.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index aa02dab84..1c1959903 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -7,6 +7,8 @@ from domainlab.tasks.task_dset import mk_task_dset from domainlab.models.model_diva import mk_diva from domainlab.tasks.utils_task import ImSize +from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter +from domainlab.compos.pcr.request import RequestVAEBuilderCHW def test_mk_exp_diva(): @@ -39,7 +41,9 @@ def mk_exp_diva(trainer="mldg"): zd_dim = 3 zy_dim = 10 zx_dim = 30 - chain_node_builder = None # TODO: chain_node_builder + args = f"--aname=apimodel --trainer={trainer} --te_d=domain1 --bs=32" + request = RequestVAEBuilderCHW(task.isize.c, task.isize.h, task.isize.w, args) + chain_node_builder = VAEChainNodeGetter(request)() # TODO: chain_node_builder list_str_y = [f"class{i}" for i in range(task.dim_y)] list_d_tr = ["domain2", "domain3"] gamma_d = 1e5 From 5b7b5e0697fef6bcb869b73a5c82836baac54dbc Mon Sep 17 00:00:00 2001 From: lisab00 Date: Wed, 30 Aug 2023 17:37:09 +0200 Subject: [PATCH 07/18] adapt code --- domainlab/compos/pcr/request.py | 11 ++++++++++ .../compos/vae/compos/encoder_xyd_parallel.py | 8 +++++++ .../compos/vae/utils_request_chain_builder.py | 5 +++-- .../compos/vae/zoo_vae_builders_classif.py | 22 +++++++++++++++---- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/domainlab/compos/pcr/request.py b/domainlab/compos/pcr/request.py index f883844f1..b5fbf6625 100644 --- a/domainlab/compos/pcr/request.py +++ b/domainlab/compos/pcr/request.py @@ -6,6 +6,17 @@ class RequestVAEBuilderCHW(): def __init__(self, i_c, i_h, i_w, args): pass +class RequestVAEBuilderNN(): + """creates request when input does not come from command-line (args) but from test_exp file""" + + def __init__(self, i_c, i_h, i_w, net_class_d, net_x, net_class_y): + """net_class_d, net_x and net_class_y are neural networks defined by the user""" + self.i_c = i_c + self.i_h = i_h + self.i_w = i_w + self.net_class_d = net_class_d + self.net_x = net_x + self.net_class_y = net_class_y class RequestTask(): """ diff --git a/domainlab/compos/vae/compos/encoder_xyd_parallel.py b/domainlab/compos/vae/compos/encoder_xyd_parallel.py index 91afb4cc5..03b7ad45f 100644 --- a/domainlab/compos/vae/compos/encoder_xyd_parallel.py +++ b/domainlab/compos/vae/compos/encoder_xyd_parallel.py @@ -37,6 +37,14 @@ def infer_zy_loc(self, tensor): return zy_loc +class XYDEncoderParallelUser(XYDEncoderParallel): + """ + This class only reimplemented constructor of parent class + """ + def __init__(self, net_class_d, net_x, net_class_y): + super().__init__(net_class_d, net_x, net_class_y) + + class XYDEncoderParallelConvBnReluPool(XYDEncoderParallel): """ This class only reimplemented constructor of parent class diff --git a/domainlab/compos/vae/utils_request_chain_builder.py b/domainlab/compos/vae/utils_request_chain_builder.py index b02406cb4..de393619f 100644 --- a/domainlab/compos/vae/utils_request_chain_builder.py +++ b/domainlab/compos/vae/utils_request_chain_builder.py @@ -1,5 +1,5 @@ from domainlab.compos.vae.zoo_vae_builders_classif import ( - NodeVAEBuilderArg, NodeVAEBuilderImgAlex, NodeVAEBuilderImgConvBnPool) + NodeVAEBuilderArg, NodeVAEBuilderUser, NodeVAEBuilderImgAlex, NodeVAEBuilderImgConvBnPool) from domainlab.compos.vae.zoo_vae_builders_classif_topic import \ NodeVAEBuilderImgTopic @@ -26,7 +26,8 @@ def __call__(self): if self.topic_dim is not None: chain = NodeVAEBuilderImgTopic(None) else: - chain = NodeVAEBuilderImgConvBnPool(None) + chain = NodeVAEBuilderUser(None) + chain = NodeVAEBuilderImgConvBnPool(chain) chain = NodeVAEBuilderImgAlex(chain) chain = NodeVAEBuilderArg(chain) node = chain.handle(self.request) diff --git a/domainlab/compos/vae/zoo_vae_builders_classif.py b/domainlab/compos/vae/zoo_vae_builders_classif.py index db3298dc2..2ecf34243 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif.py @@ -7,11 +7,11 @@ DecoderConcatLatentFCReshapeConvGatedConv from domainlab.compos.vae.compos.encoder_xyd_parallel import ( XYDEncoderParallelAlex, XYDEncoderParallelConvBnReluPool, - XYDEncoderParallelExtern) + XYDEncoderParallelExtern, XYDEncoderParallelUser) class ChainNodeVAEBuilderClassifCondPriorBase( - ChainNodeVAEBuilderClassifCondPrior): + ChainNodeVAEBuilderClassifCondPrior): """ base class of AE builder """ @@ -40,7 +40,7 @@ def build_encoder(self): def build_decoder(self): """build_decoder.""" decoder = DecoderConcatLatentFCReshapeConvGatedConv( - z_dim=self.zd_dim+self.zx_dim+self.zy_dim, + z_dim=self.zd_dim + self.zx_dim + self.zy_dim, i_c=self.i_c, i_w=self.i_w, i_h=self.i_h) return decoder @@ -70,6 +70,20 @@ def build_encoder(self): return encoder +class NodeVAEBuilderUser(ChainNodeVAEBuilderClassifCondPriorBase): + """Build encoders according to test_mk_exp file""" + + def is_myjob(self, request): + flag = hasattr(request, request.net_x) + self.request = request + return flag + + def build_encoder(self): + encoder = XYDEncoderParallelUser(self.request.net_class_d, self.request.net_x, + self.request.net_class_y) + return encoder + + class NodeVAEBuilderImgConvBnPool(ChainNodeVAEBuilderClassifCondPriorBase): def is_myjob(self, request): """is_myjob. @@ -77,7 +91,7 @@ def is_myjob(self, request): :param request: """ flag = (request.args.nname == "conv_bn_pool_2" or - request.args.nname_dom == "conv_bn_pool_2") # @FIXME + request.args.nname_dom == "conv_bn_pool_2") # @FIXME self.config_img(flag, request) return flag From 1cb8eafcecde26fabb38e119e7a361c3c1e864ef Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 1 Sep 2023 10:36:07 +0200 Subject: [PATCH 08/18] test mk epx diva --- tests/test_mk_exp_diva.py | 112 +++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 51 deletions(-) diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index 1c1959903..a7415b88e 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -1,6 +1,9 @@ """ make an experiment using "diva" model """ +from torch import nn +from torchvision import models as torchvisionmodels +from torchvision.models import ResNet50_Weights from domainlab.mk_exp import mk_exp from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault @@ -8,54 +11,61 @@ from domainlab.models.model_diva import mk_diva from domainlab.tasks.utils_task import ImSize from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter -from domainlab.compos.pcr.request import RequestVAEBuilderCHW - - -def test_mk_exp_diva(): - """ - test mk experiment API for "diva" model and trainers "mldg", "dial" - """ - - mk_exp_diva(trainer="mldg") - mk_exp_diva(trainer="dial") - - -def mk_exp_diva(trainer="mldg"): - """ - execute experiment with "diva" model and custom trainer - """ - - # specify domain generalization task - task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") - task.add_domain(name="domain1", - dset_tr=DsetMNISTColorSoloDefault(0), - dset_val=DsetMNISTColorSoloDefault(1)) - task.add_domain(name="domain2", - dset_tr=DsetMNISTColorSoloDefault(2), - dset_val=DsetMNISTColorSoloDefault(3)) - task.add_domain(name="domain3", - dset_tr=DsetMNISTColorSoloDefault(4), - dset_val=DsetMNISTColorSoloDefault(5)) - - # specify parameters - zd_dim = 3 - zy_dim = 10 - zx_dim = 30 - args = f"--aname=apimodel --trainer={trainer} --te_d=domain1 --bs=32" - request = RequestVAEBuilderCHW(task.isize.c, task.isize.h, task.isize.w, args) - chain_node_builder = VAEChainNodeGetter(request)() # TODO: chain_node_builder - list_str_y = [f"class{i}" for i in range(task.dim_y)] - list_d_tr = ["domain2", "domain3"] - gamma_d = 1e5 - gamma_y = 7e5 - beta_d = 1e3 - beta_x = 1e3 - beta_y = 1e3 - - # specify model to use - model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, - gamma_y, beta_d, beta_x, beta_y) - - # make trainer for model - exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) - exp.execute(num_epochs=2) +from domainlab.compos.pcr.request import RequestVAEBuilderNN + + +#def test_mk_exp_diva(): +""" +test mk experiment API for "diva" model and trainers "mldg", "dial" +""" + + # mk_exp_diva(trainer="mldg") + # mk_exp_diva(trainer="dial") + + +# def mk_exp_diva(trainer="mldg"): +""" +execute experiment with "diva" model and custom trainer +""" + +# specify domain generalization task +task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") +task.add_domain(name="domain1", + dset_tr=DsetMNISTColorSoloDefault(0), + dset_val=DsetMNISTColorSoloDefault(1)) +task.add_domain(name="domain2", + dset_tr=DsetMNISTColorSoloDefault(2), + dset_val=DsetMNISTColorSoloDefault(3)) +task.add_domain(name="domain3", + dset_tr=DsetMNISTColorSoloDefault(4), + dset_val=DsetMNISTColorSoloDefault(5)) + +#remove me +trainer = "mldg" + +# specify parameters +num_dom = 3 +zd_dim = 3 +zy_dim = 10 +zx_dim = 30 +net_class_d = nn.Linear( 6 , num_dom) +net_x = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) +net_class_y = nn.Linear( 6, task.dim_y) +request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w, + net_class_d, net_x, net_class_y) +chain_node_builder = VAEChainNodeGetter(request)() # TODO: chain_node_builder +list_str_y = [f"class{i}" for i in range(task.dim_y)] +list_d_tr = ["domain2", "domain3"] +gamma_d = 1e5 +gamma_y = 7e5 +beta_d = 1e3 +beta_x = 1e3 +beta_y = 1e3 + +# specify model to use +model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, + gamma_y, beta_d, beta_x, beta_y) + +# make trainer for model +exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) +exp.execute(num_epochs=2) From 629b9fe79429edf0bda5a037171f9b2222566510 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 1 Sep 2023 13:40:18 +0200 Subject: [PATCH 09/18] test mk epx diva and some other updates --- domainlab/compos/vae/utils_request_chain_builder.py | 4 ++-- domainlab/compos/vae/zoo_vae_builders_classif.py | 3 ++- tests/test_mk_exp_diva.py | 7 ++++--- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/domainlab/compos/vae/utils_request_chain_builder.py b/domainlab/compos/vae/utils_request_chain_builder.py index de393619f..963ea9509 100644 --- a/domainlab/compos/vae/utils_request_chain_builder.py +++ b/domainlab/compos/vae/utils_request_chain_builder.py @@ -26,9 +26,9 @@ def __call__(self): if self.topic_dim is not None: chain = NodeVAEBuilderImgTopic(None) else: - chain = NodeVAEBuilderUser(None) - chain = NodeVAEBuilderImgConvBnPool(chain) + chain = NodeVAEBuilderImgConvBnPool(None) chain = NodeVAEBuilderImgAlex(chain) chain = NodeVAEBuilderArg(chain) + chain = NodeVAEBuilderUser(chain) node = chain.handle(self.request) return node diff --git a/domainlab/compos/vae/zoo_vae_builders_classif.py b/domainlab/compos/vae/zoo_vae_builders_classif.py index 2ecf34243..8a65b47ce 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif.py @@ -74,8 +74,9 @@ class NodeVAEBuilderUser(ChainNodeVAEBuilderClassifCondPriorBase): """Build encoders according to test_mk_exp file""" def is_myjob(self, request): - flag = hasattr(request, request.net_x) + flag = hasattr(request, "net_x") self.request = request + self.config_img(flag, request) return flag def build_encoder(self): diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index a7415b88e..d0b3ed35b 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -48,12 +48,13 @@ zd_dim = 3 zy_dim = 10 zx_dim = 30 -net_class_d = nn.Linear( 6 , num_dom) net_x = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) -net_class_y = nn.Linear( 6, task.dim_y) +# net_in_feat = net_x.fc.out_features +net_class_d = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) +net_class_y = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w, net_class_d, net_x, net_class_y) -chain_node_builder = VAEChainNodeGetter(request)() # TODO: chain_node_builder +chain_node_builder = VAEChainNodeGetter(request)() list_str_y = [f"class{i}" for i in range(task.dim_y)] list_d_tr = ["domain2", "domain3"] gamma_d = 1e5 From 8300bedcc42caebbd9d6aadfbfd632712f79084b Mon Sep 17 00:00:00 2001 From: lisab00 Date: Wed, 6 Sep 2023 15:24:21 +0200 Subject: [PATCH 10/18] test mk epx diva and some other updates --- domainlab/compos/exp/exp_main.py | 1 + domainlab/compos/pcr/request.py | 12 +-- .../compos/vae/compos/encoder_xyd_parallel.py | 17 +++- .../compos/vae/zoo_vae_builders_classif.py | 4 +- tests/test_mk_exp_diva.py | 99 ++++++++++--------- 5 files changed, 73 insertions(+), 60 deletions(-) diff --git a/domainlab/compos/exp/exp_main.py b/domainlab/compos/exp/exp_main.py index 84c8fa15e..ad6f5e40d 100755 --- a/domainlab/compos/exp/exp_main.py +++ b/domainlab/compos/exp/exp_main.py @@ -49,6 +49,7 @@ def __init__(self, args, task=None, model=None, visitor=AggWriter): self.trainer.init_business(self.model, self.task, observer, device, args) def execute(self, num_epochs=None): + print("exp Start") """ train model check performance by loading persisted model diff --git a/domainlab/compos/pcr/request.py b/domainlab/compos/pcr/request.py index b5fbf6625..5ec45c900 100644 --- a/domainlab/compos/pcr/request.py +++ b/domainlab/compos/pcr/request.py @@ -8,15 +8,11 @@ def __init__(self, i_c, i_h, i_w, args): class RequestVAEBuilderNN(): """creates request when input does not come from command-line (args) but from test_exp file""" - - def __init__(self, i_c, i_h, i_w, net_class_d, net_x, net_class_y): + @store_args + # def __init__(self, i_c, i_h, i_w, net_class_d, net_x, net_class_y): + def __init__(self, i_c, i_h, i_w): """net_class_d, net_x and net_class_y are neural networks defined by the user""" - self.i_c = i_c - self.i_h = i_h - self.i_w = i_w - self.net_class_d = net_class_d - self.net_x = net_x - self.net_class_y = net_class_y + pass class RequestTask(): """ diff --git a/domainlab/compos/vae/compos/encoder_xyd_parallel.py b/domainlab/compos/vae/compos/encoder_xyd_parallel.py index 03b7ad45f..7a1ae5249 100644 --- a/domainlab/compos/vae/compos/encoder_xyd_parallel.py +++ b/domainlab/compos/vae/compos/encoder_xyd_parallel.py @@ -41,8 +41,21 @@ class XYDEncoderParallelUser(XYDEncoderParallel): """ This class only reimplemented constructor of parent class """ - def __init__(self, net_class_d, net_x, net_class_y): - super().__init__(net_class_d, net_x, net_class_y) + @store_args + # def __init__(self, net_class_d, net_x, net_class_y, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): + def __init__(self, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): + #""" + net_infer_zd = LSEncoderConvBnReluPool( + self.zd_dim, self.i_c, self.i_w, self.i_h, conv_stride=1) + net_infer_zx = LSEncoderConvBnReluPool( + self.zx_dim, self.i_c, self.i_w, self.i_h, + conv_stride=1) + net_infer_zy = LSEncoderConvBnReluPool( + self.zy_dim, self.i_c, self.i_w, self.i_h, + conv_stride=1) + super().__init__(net_infer_zd, net_infer_zx, net_infer_zy) + #""" + #super().__init__(net_class_d, net_x, net_class_y) class XYDEncoderParallelConvBnReluPool(XYDEncoderParallel): diff --git a/domainlab/compos/vae/zoo_vae_builders_classif.py b/domainlab/compos/vae/zoo_vae_builders_classif.py index 8a65b47ce..52a90d76d 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif.py @@ -81,7 +81,9 @@ def is_myjob(self, request): def build_encoder(self): encoder = XYDEncoderParallelUser(self.request.net_class_d, self.request.net_x, - self.request.net_class_y) + self.request.net_class_y, + self.zd_dim, self.zx_dim, self.zy_dim, + self.i_c, self.i_h, self.i_w) return encoder diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index d0b3ed35b..6d6be0065 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -14,59 +14,60 @@ from domainlab.compos.pcr.request import RequestVAEBuilderNN -#def test_mk_exp_diva(): -""" -test mk experiment API for "diva" model and trainers "mldg", "dial" -""" - - # mk_exp_diva(trainer="mldg") - # mk_exp_diva(trainer="dial") +def test_mk_exp_diva(): + """ + test mk experiment API for "diva" model and trainers "mldg", "dial" + """ + mk_exp_diva(trainer="mldg") + mk_exp_diva(trainer="dial") -# def mk_exp_diva(trainer="mldg"): -""" -execute experiment with "diva" model and custom trainer -""" +def mk_exp_diva(trainer="mldg"): + """ + execute experiment with "diva" model and custom trainer + """ -# specify domain generalization task -task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") -task.add_domain(name="domain1", - dset_tr=DsetMNISTColorSoloDefault(0), - dset_val=DsetMNISTColorSoloDefault(1)) -task.add_domain(name="domain2", - dset_tr=DsetMNISTColorSoloDefault(2), - dset_val=DsetMNISTColorSoloDefault(3)) -task.add_domain(name="domain3", - dset_tr=DsetMNISTColorSoloDefault(4), - dset_val=DsetMNISTColorSoloDefault(5)) + # specify domain generalization task + task = mk_task_dset(dim_y=10, isize=ImSize(3, 28, 28), taskna="custom_task") + task.add_domain(name="domain1", + dset_tr=DsetMNISTColorSoloDefault(0), + dset_val=DsetMNISTColorSoloDefault(1)) + task.add_domain(name="domain2", + dset_tr=DsetMNISTColorSoloDefault(2), + dset_val=DsetMNISTColorSoloDefault(3)) + task.add_domain(name="domain3", + dset_tr=DsetMNISTColorSoloDefault(4), + dset_val=DsetMNISTColorSoloDefault(5)) -#remove me -trainer = "mldg" + #remove me + #trainer = "mldg" -# specify parameters -num_dom = 3 -zd_dim = 3 -zy_dim = 10 -zx_dim = 30 -net_x = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) -# net_in_feat = net_x.fc.out_features -net_class_d = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) -net_class_y = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) -request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w, - net_class_d, net_x, net_class_y) -chain_node_builder = VAEChainNodeGetter(request)() -list_str_y = [f"class{i}" for i in range(task.dim_y)] -list_d_tr = ["domain2", "domain3"] -gamma_d = 1e5 -gamma_y = 7e5 -beta_d = 1e3 -beta_x = 1e3 -beta_y = 1e3 + # specify parameters + num_dom = 3 + zd_dim = 3 + zy_dim = 10 + zx_dim = 30 + # net_x = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) + # net_class_d = nn.Linear(28, 3) + # net_class_y = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) + # net_class_d = nn.Linear(28, 3) + # net_class_y = nn.Linear(28,10) + # request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w, + # net_class_d, net_x, net_class_y) + request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w) + chain_node_builder = VAEChainNodeGetter(request)() + list_str_y = [f"class{i}" for i in range(task.dim_y)] + list_d_tr = ["domain2", "domain3"] + gamma_d = 1e5 + gamma_y = 7e5 + beta_d = 1e3 + beta_x = 1e3 + beta_y = 1e3 -# specify model to use -model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, - gamma_y, beta_d, beta_x, beta_y) + # specify model to use + model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, + gamma_y, beta_d, beta_x, beta_y) -# make trainer for model -exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) -exp.execute(num_epochs=2) + # make trainer for model + exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) + exp.execute(num_epochs=3) From 43e698c0c05d1c20deda64c558c4d2f310fcd151 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Wed, 6 Sep 2023 16:57:02 +0200 Subject: [PATCH 11/18] change user net input --- domainlab/compos/pcr/request.py | 2 +- .../compos/vae/compos/encoder_xyd_parallel.py | 5 ++--- .../compos/vae/zoo_vae_builders_classif.py | 8 +++++--- tests/test_mk_exp_diva.py | 20 +++++-------------- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/domainlab/compos/pcr/request.py b/domainlab/compos/pcr/request.py index 5ec45c900..31b712941 100644 --- a/domainlab/compos/pcr/request.py +++ b/domainlab/compos/pcr/request.py @@ -9,7 +9,7 @@ def __init__(self, i_c, i_h, i_w, args): class RequestVAEBuilderNN(): """creates request when input does not come from command-line (args) but from test_exp file""" @store_args - # def __init__(self, i_c, i_h, i_w, net_class_d, net_x, net_class_y): + #def __init__(self, i_c, i_h, i_w, net_class_d, net_x, net_class_y): def __init__(self, i_c, i_h, i_w): """net_class_d, net_x and net_class_y are neural networks defined by the user""" pass diff --git a/domainlab/compos/vae/compos/encoder_xyd_parallel.py b/domainlab/compos/vae/compos/encoder_xyd_parallel.py index 7a1ae5249..ce6d8f523 100644 --- a/domainlab/compos/vae/compos/encoder_xyd_parallel.py +++ b/domainlab/compos/vae/compos/encoder_xyd_parallel.py @@ -42,9 +42,9 @@ class XYDEncoderParallelUser(XYDEncoderParallel): This class only reimplemented constructor of parent class """ @store_args - # def __init__(self, net_class_d, net_x, net_class_y, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): + #def __init__(self, net_class_d, net_x, net_class_y, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): def __init__(self, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): - #""" + net_infer_zd = LSEncoderConvBnReluPool( self.zd_dim, self.i_c, self.i_w, self.i_h, conv_stride=1) net_infer_zx = LSEncoderConvBnReluPool( @@ -54,7 +54,6 @@ def __init__(self, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): self.zy_dim, self.i_c, self.i_w, self.i_h, conv_stride=1) super().__init__(net_infer_zd, net_infer_zx, net_infer_zy) - #""" #super().__init__(net_class_d, net_x, net_class_y) diff --git a/domainlab/compos/vae/zoo_vae_builders_classif.py b/domainlab/compos/vae/zoo_vae_builders_classif.py index 52a90d76d..a5c68eac4 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif.py @@ -74,15 +74,17 @@ class NodeVAEBuilderUser(ChainNodeVAEBuilderClassifCondPriorBase): """Build encoders according to test_mk_exp file""" def is_myjob(self, request): - flag = hasattr(request, "net_x") - self.request = request + flag = not hasattr(request, "args") + # self.request = request self.config_img(flag, request) return flag def build_encoder(self): - encoder = XYDEncoderParallelUser(self.request.net_class_d, self.request.net_x, + """encoder = XYDEncoderParallelUser(self.request.net_class_d, self.request.net_x, self.request.net_class_y, self.zd_dim, self.zx_dim, self.zy_dim, + self.i_c, self.i_h, self.i_w)""" + encoder = XYDEncoderParallelUser(self.zd_dim, self.zx_dim, self.zy_dim, self.i_c, self.i_h, self.i_w) return encoder diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index 6d6be0065..67c457da6 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -12,6 +12,7 @@ from domainlab.tasks.utils_task import ImSize from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter from domainlab.compos.pcr.request import RequestVAEBuilderNN +from domainlab.compos.vae.compos.encoder import LSEncoderConvBnReluPool def test_mk_exp_diva(): @@ -39,30 +40,19 @@ def mk_exp_diva(trainer="mldg"): dset_tr=DsetMNISTColorSoloDefault(4), dset_val=DsetMNISTColorSoloDefault(5)) - #remove me - #trainer = "mldg" - # specify parameters - num_dom = 3 + list_str_y = [f"class{i}" for i in range(task.dim_y)] + list_d_tr = ["domain2", "domain3"] zd_dim = 3 zy_dim = 10 zx_dim = 30 - # net_x = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) - # net_class_d = nn.Linear(28, 3) - # net_class_y = torchvisionmodels.resnet.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2) - # net_class_d = nn.Linear(28, 3) - # net_class_y = nn.Linear(28,10) - # request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w, - # net_class_d, net_x, net_class_y) - request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w) - chain_node_builder = VAEChainNodeGetter(request)() - list_str_y = [f"class{i}" for i in range(task.dim_y)] - list_d_tr = ["domain2", "domain3"] gamma_d = 1e5 gamma_y = 7e5 beta_d = 1e3 beta_x = 1e3 beta_y = 1e3 + request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w) + chain_node_builder = VAEChainNodeGetter(request)() # specify model to use model = mk_diva()(chain_node_builder, zd_dim, zy_dim, zx_dim, list_str_y, list_d_tr, gamma_d, From 23823bd6c5c1e19f086c51dde11ec19b9b8afd05 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Wed, 6 Sep 2023 17:21:19 +0200 Subject: [PATCH 12/18] add hduva test file --- tests/test_mk_exp_hduva.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_mk_exp_hduva.py b/tests/test_mk_exp_hduva.py index c9366aba5..81c26f031 100644 --- a/tests/test_mk_exp_hduva.py +++ b/tests/test_mk_exp_hduva.py @@ -6,6 +6,8 @@ from domainlab.tasks.task_dset import mk_task_dset from domainlab.models.model_hduva import mk_hduva from domainlab.tasks.utils_task import ImSize +from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter +from domainlab.compos.pcr.request import RequestVAEBuilderNN def test_mk_exp_hduva(): @@ -35,7 +37,6 @@ def mk_exp_hduva(trainer="mldg"): dset_val=DsetMNISTColorSoloDefault(5)) # specify backbone to use - chain_node_builder = None # TODO: chain_node_builder zy_dim = 10 zd_dim = 3 list_str_y = [f"class{i}" for i in range(task.dim_y)] @@ -46,9 +47,11 @@ def mk_exp_hduva(trainer="mldg"): beta_x = 1e3 beta_y = 1e3 beta_t = 1e3 - device = None # TODO: specify device + device = "cpu" zx_dim = 0 topic_dim = 3 + request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w) + chain_node_builder = VAEChainNodeGetter(request)() # specify model to use model = mk_hduva()(chain_node_builder, zy_dim, zd_dim, list_str_y, list_d_tr, gamma_d, gamma_y, From b6250b687186f60a39803c168b9462bd1def2f52 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Thu, 7 Sep 2023 09:06:38 +0200 Subject: [PATCH 13/18] remove hduva test file --- tests/test_mk_exp_hduva.py | 62 -------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 tests/test_mk_exp_hduva.py diff --git a/tests/test_mk_exp_hduva.py b/tests/test_mk_exp_hduva.py deleted file mode 100644 index 81c26f031..000000000 --- a/tests/test_mk_exp_hduva.py +++ /dev/null @@ -1,62 +0,0 @@ -""" -make an experiment -""" -from domainlab.mk_exp import mk_exp -from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault -from domainlab.tasks.task_dset import mk_task_dset -from domainlab.models.model_hduva import mk_hduva -from domainlab.tasks.utils_task import ImSize -from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter -from domainlab.compos.pcr.request import RequestVAEBuilderNN - - -def test_mk_exp_hduva(): - """ - test mk experiment API with "hduva" model and trainers "mldg", "diva" - """ - - mk_exp_hduva(trainer="mldg") - mk_exp_hduva(trainer="diva") - - -def mk_exp_hduva(trainer="mldg"): - """ - execute experiment with "hduva" model and custom trainer - """ - - # specify domain generalization task - task = mk_task_dset(isize=ImSize(3, 28, 28), dim_y=10, taskna="custom_task") - task.add_domain(name="domain1", - dset_tr=DsetMNISTColorSoloDefault(0), - dset_val=DsetMNISTColorSoloDefault(1)) - task.add_domain(name="domain2", - dset_tr=DsetMNISTColorSoloDefault(2), - dset_val=DsetMNISTColorSoloDefault(3)) - task.add_domain(name="domain3", - dset_tr=DsetMNISTColorSoloDefault(4), - dset_val=DsetMNISTColorSoloDefault(5)) - - # specify backbone to use - zy_dim = 10 - zd_dim = 3 - list_str_y = [f"class{i}" for i in range(task.dim_y)] - list_d_tr = ["domain2", "domain3"] - gamma_d = 1e5 - gamma_y = 7e5 - beta_d = 1e3 - beta_x = 1e3 - beta_y = 1e3 - beta_t = 1e3 - device = "cpu" - zx_dim = 0 - topic_dim = 3 - request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w) - chain_node_builder = VAEChainNodeGetter(request)() - - # specify model to use - model = mk_hduva()(chain_node_builder, zy_dim, zd_dim, list_str_y, list_d_tr, gamma_d, gamma_y, - beta_d, beta_x, beta_y, beta_t, device, zx_dim, topic_dim) - - # make trainer for model - exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) - exp.execute(num_epochs=2) From 47305a8ef9db1fede56f888f043e7cc5536d3c55 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Thu, 7 Sep 2023 14:18:50 +0200 Subject: [PATCH 14/18] let user enter net --- domainlab/compos/pcr/request.py | 3 +-- .../compos/vae/compos/encoder_xyd_parallel.py | 15 ++------------- domainlab/compos/vae/zoo_vae_builders_classif.py | 11 ++++------- tests/test_mk_exp_diva.py | 13 +++++++++---- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/domainlab/compos/pcr/request.py b/domainlab/compos/pcr/request.py index 31b712941..848d21aab 100644 --- a/domainlab/compos/pcr/request.py +++ b/domainlab/compos/pcr/request.py @@ -9,8 +9,7 @@ def __init__(self, i_c, i_h, i_w, args): class RequestVAEBuilderNN(): """creates request when input does not come from command-line (args) but from test_exp file""" @store_args - #def __init__(self, i_c, i_h, i_w, net_class_d, net_x, net_class_y): - def __init__(self, i_c, i_h, i_w): + def __init__(self, net_class_d, net_x, net_class_y, i_c, i_h, i_w): """net_class_d, net_x and net_class_y are neural networks defined by the user""" pass diff --git a/domainlab/compos/vae/compos/encoder_xyd_parallel.py b/domainlab/compos/vae/compos/encoder_xyd_parallel.py index ce6d8f523..0544e9a2e 100644 --- a/domainlab/compos/vae/compos/encoder_xyd_parallel.py +++ b/domainlab/compos/vae/compos/encoder_xyd_parallel.py @@ -42,19 +42,8 @@ class XYDEncoderParallelUser(XYDEncoderParallel): This class only reimplemented constructor of parent class """ @store_args - #def __init__(self, net_class_d, net_x, net_class_y, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): - def __init__(self, zd_dim, zx_dim, zy_dim, i_c, i_h, i_w): - - net_infer_zd = LSEncoderConvBnReluPool( - self.zd_dim, self.i_c, self.i_w, self.i_h, conv_stride=1) - net_infer_zx = LSEncoderConvBnReluPool( - self.zx_dim, self.i_c, self.i_w, self.i_h, - conv_stride=1) - net_infer_zy = LSEncoderConvBnReluPool( - self.zy_dim, self.i_c, self.i_w, self.i_h, - conv_stride=1) - super().__init__(net_infer_zd, net_infer_zx, net_infer_zy) - #super().__init__(net_class_d, net_x, net_class_y) + def __init__(self, net_class_d, net_x, net_class_y): + super().__init__(net_class_d, net_x, net_class_y) class XYDEncoderParallelConvBnReluPool(XYDEncoderParallel): diff --git a/domainlab/compos/vae/zoo_vae_builders_classif.py b/domainlab/compos/vae/zoo_vae_builders_classif.py index a5c68eac4..1adf43c7e 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif.py @@ -75,17 +75,14 @@ class NodeVAEBuilderUser(ChainNodeVAEBuilderClassifCondPriorBase): def is_myjob(self, request): flag = not hasattr(request, "args") - # self.request = request + self.request = request self.config_img(flag, request) return flag def build_encoder(self): - """encoder = XYDEncoderParallelUser(self.request.net_class_d, self.request.net_x, - self.request.net_class_y, - self.zd_dim, self.zx_dim, self.zy_dim, - self.i_c, self.i_h, self.i_w)""" - encoder = XYDEncoderParallelUser(self.zd_dim, self.zx_dim, self.zy_dim, - self.i_c, self.i_h, self.i_w) + encoder = XYDEncoderParallelUser(self.request.net_class_d, + self.request.net_x, + self.request.net_class_y) return encoder diff --git a/tests/test_mk_exp_diva.py b/tests/test_mk_exp_diva.py index 67c457da6..83db8ae6a 100644 --- a/tests/test_mk_exp_diva.py +++ b/tests/test_mk_exp_diva.py @@ -1,9 +1,6 @@ """ make an experiment using "diva" model """ -from torch import nn -from torchvision import models as torchvisionmodels -from torchvision.models import ResNet50_Weights from domainlab.mk_exp import mk_exp from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault @@ -51,7 +48,15 @@ def mk_exp_diva(trainer="mldg"): beta_d = 1e3 beta_x = 1e3 beta_y = 1e3 - request = RequestVAEBuilderNN(task.isize.c, task.isize.h, task.isize.w) + net_class_d = LSEncoderConvBnReluPool( + zd_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) + net_x = LSEncoderConvBnReluPool( + zx_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) + net_class_y = LSEncoderConvBnReluPool( + zy_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) + + request = RequestVAEBuilderNN(net_class_d, net_x, net_class_y, + task.isize.c, task.isize.h, task.isize.w) chain_node_builder = VAEChainNodeGetter(request)() # specify model to use From 0118d4148325334f0be344625fc26bce114d1f59 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Thu, 7 Sep 2023 17:35:01 +0200 Subject: [PATCH 15/18] add todo --- .../vae/compos/encoder_xydt_elevator.py | 2 + .../compos/vae/utils_request_chain_builder.py | 5 +- .../vae/zoo_vae_builders_classif_topic.py | 23 +++++- tests/test_mk_exp_hduva.py | 72 +++++++++++++++++++ 4 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 tests/test_mk_exp_hduva.py diff --git a/domainlab/compos/vae/compos/encoder_xydt_elevator.py b/domainlab/compos/vae/compos/encoder_xydt_elevator.py index f4c5e3546..4d2b3be94 100644 --- a/domainlab/compos/vae/compos/encoder_xydt_elevator.py +++ b/domainlab/compos/vae/compos/encoder_xydt_elevator.py @@ -38,6 +38,8 @@ def infer_zy_loc(self, tensor): return zy_loc +# TODO: class XYDTEncoderUser() + class XYDTEncoderArg(XYDTEncoderElevator): """ This class only reimplemented constructor of parent class diff --git a/domainlab/compos/vae/utils_request_chain_builder.py b/domainlab/compos/vae/utils_request_chain_builder.py index 963ea9509..fe5d5b5b6 100644 --- a/domainlab/compos/vae/utils_request_chain_builder.py +++ b/domainlab/compos/vae/utils_request_chain_builder.py @@ -1,7 +1,7 @@ from domainlab.compos.vae.zoo_vae_builders_classif import ( NodeVAEBuilderArg, NodeVAEBuilderUser, NodeVAEBuilderImgAlex, NodeVAEBuilderImgConvBnPool) -from domainlab.compos.vae.zoo_vae_builders_classif_topic import \ - NodeVAEBuilderImgTopic +from domainlab.compos.vae.zoo_vae_builders_classif_topic import ( + NodeVAEBuilderImgTopic, NodeVAEBuilderImgTopicUser) class VAEChainNodeGetter(object): @@ -25,6 +25,7 @@ def __call__(self): """ if self.topic_dim is not None: chain = NodeVAEBuilderImgTopic(None) + chain = NodeVAEBuilderImgTopicUser(chain) else: chain = NodeVAEBuilderImgConvBnPool(None) chain = NodeVAEBuilderImgAlex(chain) diff --git a/domainlab/compos/vae/zoo_vae_builders_classif_topic.py b/domainlab/compos/vae/zoo_vae_builders_classif_topic.py index b8164224e..0b2aa0eca 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif_topic.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif_topic.py @@ -4,7 +4,7 @@ from domainlab.compos.vae.compos.decoder_concat_vec_reshape_conv_gated_conv import \ DecoderConcatLatentFCReshapeConvGatedConv from domainlab.compos.vae.compos.encoder_xydt_elevator import XYDTEncoderArg -from domainlab.compos.vae.zoo_vae_builders_classif import NodeVAEBuilderArg +from domainlab.compos.vae.zoo_vae_builders_classif import (NodeVAEBuilderArg, NodeVAEBuilderUser) class NodeVAEBuilderImgTopic(NodeVAEBuilderArg): @@ -15,7 +15,7 @@ def is_myjob(self, request): :param request: """ self.args = request.args - flag = True # @FIXME + flag = hasattr(request, "args") self.config_img(flag, request) return flag @@ -46,3 +46,22 @@ def build_decoder(self, topic_dim): i_c=self.i_c, i_w=self.i_w, i_h=self.i_h) return decoder + + +class NodeVAEBuilderImgTopicUser(NodeVAEBuilderUser): + """NodeVAEBuilderImgTopic if user input does not come via command line, but from test file""" + + + def is_myjob(self, request): + flag = not hasattr(request, "args") + self.config_img(flag, request) + return flag + + + def build_encoder(self): # TODO + # encoder = XYDTEncoderUser( + return + + + def build_decoder(self): # TODO + return \ No newline at end of file diff --git a/tests/test_mk_exp_hduva.py b/tests/test_mk_exp_hduva.py new file mode 100644 index 000000000..70aadd313 --- /dev/null +++ b/tests/test_mk_exp_hduva.py @@ -0,0 +1,72 @@ +""" +make an experiment +""" +from domainlab.mk_exp import mk_exp +from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault +from domainlab.tasks.task_dset import mk_task_dset +from domainlab.models.model_hduva import mk_hduva +from domainlab.tasks.utils_task import ImSize +from domainlab.compos.pcr.request import RequestVAEBuilderNN +from domainlab.compos.vae.compos.encoder import LSEncoderConvBnReluPool +from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter + + +def test_mk_exp_hduva(): + """ + test mk experiment API with "hduva" model and trainers "mldg", "diva" + """ + + mk_exp_hduva(trainer="mldg") + mk_exp_hduva(trainer="diva") + + +def mk_exp_hduva(trainer="mldg"): + """ + execute experiment with "hduva" model and custom trainer + """ + + # specify domain generalization task + task = mk_task_dset(isize=ImSize(3, 28, 28), dim_y=10, taskna="custom_task") + task.add_domain(name="domain1", + dset_tr=DsetMNISTColorSoloDefault(0), + dset_val=DsetMNISTColorSoloDefault(1)) + task.add_domain(name="domain2", + dset_tr=DsetMNISTColorSoloDefault(2), + dset_val=DsetMNISTColorSoloDefault(3)) + task.add_domain(name="domain3", + dset_tr=DsetMNISTColorSoloDefault(4), + dset_val=DsetMNISTColorSoloDefault(5)) + + # specify backbone to use + list_str_y = [f"class{i}" for i in range(task.dim_y)] + list_d_tr = ["domain2", "domain3"] + gamma_d = 1e5 + gamma_y = 7e5 + beta_d = 1e3 + beta_x = 1e3 + beta_y = 1e3 + beta_t = 1e3 + device = "cpu" + zx_dim = 30 + zy_dim = 10 + zd_dim = 3 + topic_dim = 3 + + net_class_d = LSEncoderConvBnReluPool( + zd_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) + net_x = LSEncoderConvBnReluPool( + zx_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) + net_class_y = LSEncoderConvBnReluPool( + zy_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) + + request = RequestVAEBuilderNN(net_class_d, net_x, net_class_y, + task.isize.c, task.isize.h, task.isize.w) + chain_node_builder = VAEChainNodeGetter(request)() + + # specify model to use + model = mk_hduva()(chain_node_builder, zy_dim, zd_dim, list_str_y, list_d_tr, gamma_d, gamma_y, + beta_d, beta_x, beta_y, beta_t, device, zx_dim, topic_dim) + + # make trainer for model + exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) + exp.execute(num_epochs=2) From e9619bd2bbc2eec5151d71842d8da875e47d8a1c Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 8 Sep 2023 09:17:22 +0200 Subject: [PATCH 16/18] remove pass statement --- domainlab/compos/pcr/request.py | 1 - 1 file changed, 1 deletion(-) diff --git a/domainlab/compos/pcr/request.py b/domainlab/compos/pcr/request.py index 848d21aab..f7aba101b 100644 --- a/domainlab/compos/pcr/request.py +++ b/domainlab/compos/pcr/request.py @@ -11,7 +11,6 @@ class RequestVAEBuilderNN(): @store_args def __init__(self, net_class_d, net_x, net_class_y, i_c, i_h, i_w): """net_class_d, net_x and net_class_y are neural networks defined by the user""" - pass class RequestTask(): """ From 4e66b9c5fd3739f65ac3db13d01e78acdee1838f Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 8 Sep 2023 10:42:36 +0200 Subject: [PATCH 17/18] undo hduva --- .../vae/compos/encoder_xydt_elevator.py | 2 - .../compos/vae/utils_request_chain_builder.py | 4 +- .../vae/zoo_vae_builders_classif_topic.py | 21 +----- tests/test_mk_exp_hduva.py | 72 ------------------- 4 files changed, 2 insertions(+), 97 deletions(-) delete mode 100644 tests/test_mk_exp_hduva.py diff --git a/domainlab/compos/vae/compos/encoder_xydt_elevator.py b/domainlab/compos/vae/compos/encoder_xydt_elevator.py index 4d2b3be94..f4c5e3546 100644 --- a/domainlab/compos/vae/compos/encoder_xydt_elevator.py +++ b/domainlab/compos/vae/compos/encoder_xydt_elevator.py @@ -38,8 +38,6 @@ def infer_zy_loc(self, tensor): return zy_loc -# TODO: class XYDTEncoderUser() - class XYDTEncoderArg(XYDTEncoderElevator): """ This class only reimplemented constructor of parent class diff --git a/domainlab/compos/vae/utils_request_chain_builder.py b/domainlab/compos/vae/utils_request_chain_builder.py index fe5d5b5b6..c740d5e94 100644 --- a/domainlab/compos/vae/utils_request_chain_builder.py +++ b/domainlab/compos/vae/utils_request_chain_builder.py @@ -1,7 +1,6 @@ from domainlab.compos.vae.zoo_vae_builders_classif import ( NodeVAEBuilderArg, NodeVAEBuilderUser, NodeVAEBuilderImgAlex, NodeVAEBuilderImgConvBnPool) -from domainlab.compos.vae.zoo_vae_builders_classif_topic import ( - NodeVAEBuilderImgTopic, NodeVAEBuilderImgTopicUser) +from domainlab.compos.vae.zoo_vae_builders_classif_topic import NodeVAEBuilderImgTopic class VAEChainNodeGetter(object): @@ -25,7 +24,6 @@ def __call__(self): """ if self.topic_dim is not None: chain = NodeVAEBuilderImgTopic(None) - chain = NodeVAEBuilderImgTopicUser(chain) else: chain = NodeVAEBuilderImgConvBnPool(None) chain = NodeVAEBuilderImgAlex(chain) diff --git a/domainlab/compos/vae/zoo_vae_builders_classif_topic.py b/domainlab/compos/vae/zoo_vae_builders_classif_topic.py index 0b2aa0eca..50c36d846 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif_topic.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif_topic.py @@ -4,7 +4,7 @@ from domainlab.compos.vae.compos.decoder_concat_vec_reshape_conv_gated_conv import \ DecoderConcatLatentFCReshapeConvGatedConv from domainlab.compos.vae.compos.encoder_xydt_elevator import XYDTEncoderArg -from domainlab.compos.vae.zoo_vae_builders_classif import (NodeVAEBuilderArg, NodeVAEBuilderUser) +from domainlab.compos.vae.zoo_vae_builders_classif import NodeVAEBuilderArg class NodeVAEBuilderImgTopic(NodeVAEBuilderArg): @@ -46,22 +46,3 @@ def build_decoder(self, topic_dim): i_c=self.i_c, i_w=self.i_w, i_h=self.i_h) return decoder - - -class NodeVAEBuilderImgTopicUser(NodeVAEBuilderUser): - """NodeVAEBuilderImgTopic if user input does not come via command line, but from test file""" - - - def is_myjob(self, request): - flag = not hasattr(request, "args") - self.config_img(flag, request) - return flag - - - def build_encoder(self): # TODO - # encoder = XYDTEncoderUser( - return - - - def build_decoder(self): # TODO - return \ No newline at end of file diff --git a/tests/test_mk_exp_hduva.py b/tests/test_mk_exp_hduva.py deleted file mode 100644 index 70aadd313..000000000 --- a/tests/test_mk_exp_hduva.py +++ /dev/null @@ -1,72 +0,0 @@ -""" -make an experiment -""" -from domainlab.mk_exp import mk_exp -from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault -from domainlab.tasks.task_dset import mk_task_dset -from domainlab.models.model_hduva import mk_hduva -from domainlab.tasks.utils_task import ImSize -from domainlab.compos.pcr.request import RequestVAEBuilderNN -from domainlab.compos.vae.compos.encoder import LSEncoderConvBnReluPool -from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter - - -def test_mk_exp_hduva(): - """ - test mk experiment API with "hduva" model and trainers "mldg", "diva" - """ - - mk_exp_hduva(trainer="mldg") - mk_exp_hduva(trainer="diva") - - -def mk_exp_hduva(trainer="mldg"): - """ - execute experiment with "hduva" model and custom trainer - """ - - # specify domain generalization task - task = mk_task_dset(isize=ImSize(3, 28, 28), dim_y=10, taskna="custom_task") - task.add_domain(name="domain1", - dset_tr=DsetMNISTColorSoloDefault(0), - dset_val=DsetMNISTColorSoloDefault(1)) - task.add_domain(name="domain2", - dset_tr=DsetMNISTColorSoloDefault(2), - dset_val=DsetMNISTColorSoloDefault(3)) - task.add_domain(name="domain3", - dset_tr=DsetMNISTColorSoloDefault(4), - dset_val=DsetMNISTColorSoloDefault(5)) - - # specify backbone to use - list_str_y = [f"class{i}" for i in range(task.dim_y)] - list_d_tr = ["domain2", "domain3"] - gamma_d = 1e5 - gamma_y = 7e5 - beta_d = 1e3 - beta_x = 1e3 - beta_y = 1e3 - beta_t = 1e3 - device = "cpu" - zx_dim = 30 - zy_dim = 10 - zd_dim = 3 - topic_dim = 3 - - net_class_d = LSEncoderConvBnReluPool( - zd_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) - net_x = LSEncoderConvBnReluPool( - zx_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) - net_class_y = LSEncoderConvBnReluPool( - zy_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) - - request = RequestVAEBuilderNN(net_class_d, net_x, net_class_y, - task.isize.c, task.isize.h, task.isize.w) - chain_node_builder = VAEChainNodeGetter(request)() - - # specify model to use - model = mk_hduva()(chain_node_builder, zy_dim, zd_dim, list_str_y, list_d_tr, gamma_d, gamma_y, - beta_d, beta_x, beta_y, beta_t, device, zx_dim, topic_dim) - - # make trainer for model - exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) - exp.execute(num_epochs=2) From 87d3b43983864bc90df7998414df7671e0ce6880 Mon Sep 17 00:00:00 2001 From: lisab00 Date: Fri, 8 Sep 2023 10:42:36 +0200 Subject: [PATCH 18/18] undo hduva --- .../vae/compos/encoder_xydt_elevator.py | 2 - .../compos/vae/utils_request_chain_builder.py | 4 +- .../vae/zoo_vae_builders_classif_topic.py | 23 +----- tests/test_mk_exp_hduva.py | 72 ------------------- 4 files changed, 3 insertions(+), 98 deletions(-) delete mode 100644 tests/test_mk_exp_hduva.py diff --git a/domainlab/compos/vae/compos/encoder_xydt_elevator.py b/domainlab/compos/vae/compos/encoder_xydt_elevator.py index 4d2b3be94..f4c5e3546 100644 --- a/domainlab/compos/vae/compos/encoder_xydt_elevator.py +++ b/domainlab/compos/vae/compos/encoder_xydt_elevator.py @@ -38,8 +38,6 @@ def infer_zy_loc(self, tensor): return zy_loc -# TODO: class XYDTEncoderUser() - class XYDTEncoderArg(XYDTEncoderElevator): """ This class only reimplemented constructor of parent class diff --git a/domainlab/compos/vae/utils_request_chain_builder.py b/domainlab/compos/vae/utils_request_chain_builder.py index fe5d5b5b6..c740d5e94 100644 --- a/domainlab/compos/vae/utils_request_chain_builder.py +++ b/domainlab/compos/vae/utils_request_chain_builder.py @@ -1,7 +1,6 @@ from domainlab.compos.vae.zoo_vae_builders_classif import ( NodeVAEBuilderArg, NodeVAEBuilderUser, NodeVAEBuilderImgAlex, NodeVAEBuilderImgConvBnPool) -from domainlab.compos.vae.zoo_vae_builders_classif_topic import ( - NodeVAEBuilderImgTopic, NodeVAEBuilderImgTopicUser) +from domainlab.compos.vae.zoo_vae_builders_classif_topic import NodeVAEBuilderImgTopic class VAEChainNodeGetter(object): @@ -25,7 +24,6 @@ def __call__(self): """ if self.topic_dim is not None: chain = NodeVAEBuilderImgTopic(None) - chain = NodeVAEBuilderImgTopicUser(chain) else: chain = NodeVAEBuilderImgConvBnPool(None) chain = NodeVAEBuilderImgAlex(chain) diff --git a/domainlab/compos/vae/zoo_vae_builders_classif_topic.py b/domainlab/compos/vae/zoo_vae_builders_classif_topic.py index 0b2aa0eca..b8164224e 100644 --- a/domainlab/compos/vae/zoo_vae_builders_classif_topic.py +++ b/domainlab/compos/vae/zoo_vae_builders_classif_topic.py @@ -4,7 +4,7 @@ from domainlab.compos.vae.compos.decoder_concat_vec_reshape_conv_gated_conv import \ DecoderConcatLatentFCReshapeConvGatedConv from domainlab.compos.vae.compos.encoder_xydt_elevator import XYDTEncoderArg -from domainlab.compos.vae.zoo_vae_builders_classif import (NodeVAEBuilderArg, NodeVAEBuilderUser) +from domainlab.compos.vae.zoo_vae_builders_classif import NodeVAEBuilderArg class NodeVAEBuilderImgTopic(NodeVAEBuilderArg): @@ -15,7 +15,7 @@ def is_myjob(self, request): :param request: """ self.args = request.args - flag = hasattr(request, "args") + flag = True # @FIXME self.config_img(flag, request) return flag @@ -46,22 +46,3 @@ def build_decoder(self, topic_dim): i_c=self.i_c, i_w=self.i_w, i_h=self.i_h) return decoder - - -class NodeVAEBuilderImgTopicUser(NodeVAEBuilderUser): - """NodeVAEBuilderImgTopic if user input does not come via command line, but from test file""" - - - def is_myjob(self, request): - flag = not hasattr(request, "args") - self.config_img(flag, request) - return flag - - - def build_encoder(self): # TODO - # encoder = XYDTEncoderUser( - return - - - def build_decoder(self): # TODO - return \ No newline at end of file diff --git a/tests/test_mk_exp_hduva.py b/tests/test_mk_exp_hduva.py deleted file mode 100644 index 70aadd313..000000000 --- a/tests/test_mk_exp_hduva.py +++ /dev/null @@ -1,72 +0,0 @@ -""" -make an experiment -""" -from domainlab.mk_exp import mk_exp -from domainlab.dsets.dset_mnist_color_solo_default import DsetMNISTColorSoloDefault -from domainlab.tasks.task_dset import mk_task_dset -from domainlab.models.model_hduva import mk_hduva -from domainlab.tasks.utils_task import ImSize -from domainlab.compos.pcr.request import RequestVAEBuilderNN -from domainlab.compos.vae.compos.encoder import LSEncoderConvBnReluPool -from domainlab.compos.vae.utils_request_chain_builder import VAEChainNodeGetter - - -def test_mk_exp_hduva(): - """ - test mk experiment API with "hduva" model and trainers "mldg", "diva" - """ - - mk_exp_hduva(trainer="mldg") - mk_exp_hduva(trainer="diva") - - -def mk_exp_hduva(trainer="mldg"): - """ - execute experiment with "hduva" model and custom trainer - """ - - # specify domain generalization task - task = mk_task_dset(isize=ImSize(3, 28, 28), dim_y=10, taskna="custom_task") - task.add_domain(name="domain1", - dset_tr=DsetMNISTColorSoloDefault(0), - dset_val=DsetMNISTColorSoloDefault(1)) - task.add_domain(name="domain2", - dset_tr=DsetMNISTColorSoloDefault(2), - dset_val=DsetMNISTColorSoloDefault(3)) - task.add_domain(name="domain3", - dset_tr=DsetMNISTColorSoloDefault(4), - dset_val=DsetMNISTColorSoloDefault(5)) - - # specify backbone to use - list_str_y = [f"class{i}" for i in range(task.dim_y)] - list_d_tr = ["domain2", "domain3"] - gamma_d = 1e5 - gamma_y = 7e5 - beta_d = 1e3 - beta_x = 1e3 - beta_y = 1e3 - beta_t = 1e3 - device = "cpu" - zx_dim = 30 - zy_dim = 10 - zd_dim = 3 - topic_dim = 3 - - net_class_d = LSEncoderConvBnReluPool( - zd_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) - net_x = LSEncoderConvBnReluPool( - zx_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) - net_class_y = LSEncoderConvBnReluPool( - zy_dim, task.isize.c, task.isize.w, task.isize.h, conv_stride=1) - - request = RequestVAEBuilderNN(net_class_d, net_x, net_class_y, - task.isize.c, task.isize.h, task.isize.w) - chain_node_builder = VAEChainNodeGetter(request)() - - # specify model to use - model = mk_hduva()(chain_node_builder, zy_dim, zd_dim, list_str_y, list_d_tr, gamma_d, gamma_y, - beta_d, beta_x, beta_y, beta_t, device, zx_dim, topic_dim) - - # make trainer for model - exp = mk_exp(task, model, trainer=trainer, test_domain="domain1", batchsize=32) - exp.execute(num_epochs=2)