Example #1
0
    def build(cls, params, sub_cls=None, controller=None):
        loaders = {}
        splits = try_get_attr(params, f'{cls.prefix_name()}_splits',
                              ('train', ))
        shuffles = try_get_attr(params, f'{cls.prefix_name()}_shuffles',
                                (True, ))
        belongs = try_get_attr(params, f'{cls.prefix_name()}_belongs',
                               ('train', ))

        loader_kwargs = load_func_kwargs(params, py_data.DataLoader.__init__,
                                         cls.prefix_name())
        init_kwargs = load_func_kwargs(params, cls.__init__, cls.prefix_name())
        cls_name = getattr(params, f'{cls.prefix_name()}_cls', None)
        assert cls_name is not None
        sub_cls = cls.load_cls(cls_name)
        for idx, belong in enumerate(belongs):
            init_kwargs['split'] = splits[idx]
            dataset = sub_cls(**init_kwargs)
            if controller is not None:
                dataset.controller = controller
            loader_kwargs.update({
                'shuffle': shuffles[idx],
                'dataset': dataset,
                'collate_fn': dataset.collate_fn
            })
            loaders[belong] = py_data.DataLoader(**loader_kwargs)
        return loaders
Example #2
0
 def build(cls, params, sub_cls=None, controller=None):
     layer_cls_names = try_get_attr(params,
                                    f'{cls.prefix_name()}_layer_names')
     layer_clses = [
         Layer.load_cls(layer_cls_name)
         for layer_cls_name in layer_cls_names
     ]
     layer_params = cls.collect_layer_params(layer_cls_names)
     layer_args = {
         name: try_get_attr(params, f'{cls.prefix_name()}_layer_{name}s')
         for name in layer_params.keys()
     }
     layer_args = {
         key: value
         for key, value in layer_args.items() if value is not None
     }
     layers = list()
     for idx, layer_cls in enumerate(layer_clses):
         layer_kwargs = {
             name: layer_arg[idx]
             for name, layer_arg in layer_args.items()
         }
         layer_kwargs = load_func_kwargs(layer_kwargs, layer_cls.__init__)
         layers.append(layer_cls(**layer_kwargs))
     kwargs = load_func_params(params, cls.__init__, cls.prefix_name())
     kwargs[f'{cls.prefix_name()}_layers'] = layers
     return cls.default_build(kwargs, controller=controller)
Example #3
0
 def build_loggers(cls, args, controller=None):
     args = to_namespace(args)
     loggers = {}
     for split in try_get_attr(args, f'{cls.prefix_name()}_splits', []):
         setattr(args, f'{cls.prefix_name()}_name', split)
         loggers[split] = cls.build(args, controller=controller)
     return loggers
Example #4
0
 def build(cls, params, sub_cls=None, controller=None):
     kwargs = load_func_params(params, cls.__init__, cls.prefix_name())
     for net_name in cls.net_names:
         net = Net.build(params, sub_cls=try_get_attr(params, f'{cls.prefix_name()}_{net_name}_cls'),
                         controller=controller)
         kwargs[f'{cls.prefix_name()}_{net_name}'] = net
     return cls.default_build(kwargs, controller=controller)
Example #5
0
 def add_args(cls, group, params=None, sub_cls=None):
     for net_name in cls.net_names:
         add_argument(group, f'{cls.prefix_name()}_{net_name}_cls', type=str)
         net_cls = Net.load_cls(try_get_attr(params, f'{cls.prefix_name()}_{net_name}_cls', check=False))
         if net_cls is not None:
             net_cls.add_args(group, params)
     return group
Example #6
0
 def init_args(cls, params, sub_cls=None):
     try_set_attr(params, f'{cls.prefix_name()}_q_net_cls', 'cgs_q_net')
     try_set_attr(params, f'{cls.prefix_name()}_graph_net_cls', 'cgs_graph_net')
     try_set_attr(params, f'{cls.prefix_name()}_cls_net_cls', 'cgs_cls_net')
     for net_name in cls.net_names:
         net_cls = Net.load_cls(try_get_attr(params, f'{cls.prefix_name()}_{net_name}_cls', check=False))
         if net_cls is not None:
             net_cls.init_args(params)
Example #7
0
 def build(cls, params, model_cls_name=None, model_cls=None):
     cls_name_keys = [f'{name}_net_cls_name' for name in cls.net_names]
     nets = [
         Net.build(params, net_cls_name=try_get_attr(params, cls_name_key))
         for cls_name_key in cls_name_keys
     ]
     return cls(*nets,
                net_modes=params.net_modes,
                net_is_restores=params.net_is_restores)
Example #8
0
 def add_args(cls, group, params=None, sub_cls=None):
     group = cls.default_add_args(group, params)
     logger_cls = cls.load_cls(
         try_get_attr(params,
                      f'{cls.prefix_name()}_logger_cls',
                      check=False))
     if logger_cls is not None:
         group = logger_cls.add_args(group, params)
     return group
Example #9
0
 def init_args(cls, params):
     params = super().init_args(params)
     opt_type = try_get_attr(params,
                             f'{cls.prefix_name()}_type',
                             None,
                             check=False)
     if opt_type is None:
         return params
     opt_cls = getattr(optim, opt_type, None)
     assert opt_cls is not None
     return init_func_args(opt_cls.__init__, params, cls.prefix_name())
Example #10
0
 def add_args(cls, group, params=None):
     super().add_args(group, params)
     opt_type = try_get_attr(params,
                             f'{cls.prefix_name()}_type',
                             None,
                             check=False)
     if opt_type is None:
         return group
     opt_cls = getattr(optim, opt_type, None)
     assert opt_cls is not None
     add_func_args(group, opt_cls.__init__, prefix=cls.prefix_name())
     return group
Example #11
0
 def build(cls, params, sub_cls=None, controller=None):
     opt_type = try_get_attr(params,
                             f'{cls.prefix_name()}_type',
                             None,
                             check=False)
     opt_cls = getattr(optim, opt_type, None)
     assert opt_cls is not None
     optim_kwargs = load_func_kwargs(params, opt_cls.__init__,
                                     cls.prefix_name())
     init_kwargs = load_func_kwargs(params, cls.__init__, cls.prefix_name())
     init_kwargs.update({'optim_kwargs': optim_kwargs})
     return cls(**init_kwargs)
Example #12
0
 def add_args(cls,
              group: argparse.ArgumentParser,
              params=None,
              model_cls_name=None,
              model_cls=None):
     if not cls.has_add_args:
         group.add_argument('--q_net_cls_name', type=str)
         group.add_argument('--img_net_cls_name', type=str)
         group.add_argument('--fusion_net_cls_name', type=str)
         group.add_argument('--classifier_net_cls_name', type=str)
         group.add_argument('--net_modes', nargs='*', type=str_bool)
         group.add_argument('--net_is_restores', nargs='*', type=str_bool)
     else:
         for net_name in ('q', 'img', 'fusion', 'classifier'):
             Net.add_args(group, params,
                          try_get_attr(params, f'{net_name}_net_cls_name'))
Example #13
0
 def init_args(cls, params, sub_cls=None):
     cls.default_init_args(params)
     try_set_attr(params, f'{cls.prefix_name()}_name', 'logger_group')
     try_set_attr(
         params, f'{cls.prefix_name()}_logger_dir',
         to_path(params.root_dir).joinpath(f'loggers/{params.proj_name}'))
     logger_cls = cls.load_cls(
         try_get_attr(params,
                      f'{cls.prefix_name()}_logger_cls',
                      check=False))
     if logger_cls is not None:
         logger_cls.init_args(params)
         setattr(
             params, f'{cls.prefix_name()}_logger_kwargs',
             load_func_kwargs(params, logger_cls.__init__,
                              cls.prefix_name()))
Example #14
0
 def add_args(cls, group, params=None, sub_cls=None):
     add_argument(group,
                  f'{cls.prefix_name()}_layer_names',
                  type=str,
                  nargs='*')
     group = cls.default_add_args(group, params)
     layer_cls_names = try_get_attr(params,
                                    f'{cls.prefix_name()}_layer_names',
                                    check=False)
     if layer_cls_names is not None:
         params = cls.collect_layer_params(layer_cls_names)
         for name, param in params.items():
             arg_type = param.annotation
             arg_type = str_bool if arg_type is bool else arg_type
             add_argument(group,
                          f'{cls.prefix_name()}_layer_{name}s',
                          type=arg_type,
                          nargs='*')
     return group
Example #15
0
 def build(cls, params, cls_name=None, sub_cls=None):
     return cls(try_get_attr(params, 'type', 'relu'))
Example #16
0
 def build(cls, params, cls_name=None, sub_cls=None):
     return cls(try_get_attr(params, 'in_dim', None),
                try_get_attr(params, 'widen', 2),
                try_get_attr(params, 'type', 'mix'))