MCPcopy Create free account
hub / github.com/JunlinHan/DCLGAN / BaseModel

Class BaseModel

models/base_model.py:8–258  ·  view source on GitHub ↗

This class is an abstract base class (ABC) for models. To create a subclass, you need to implement the following five functions: -- <__init__>: initialize the class; first call BaseModel.__init__(self, opt). -- : unpack data fro

Source from the content-addressed store, hash-verified

6
7
8class BaseModel(ABC):
9 """This class is an abstract base class (ABC) for models.
10 To create a subclass, you need to implement the following five functions:
11 -- <__init__>: initialize the class; first call BaseModel.__init__(self, opt).
12 -- <set_input>: unpack data from dataset and apply preprocessing.
13 -- <forward>: produce intermediate results.
14 -- <optimize_parameters>: calculate losses, gradients, and update network weights.
15 -- <modify_commandline_options>: (optionally) add model-specific options and set default options.
16 """
17
18 def __init__(self, opt):
19 """Initialize the BaseModel class.
20
21 Parameters:
22 opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
23
24 When creating your custom class, you need to implement your own initialization.
25 In this fucntion, you should first call <BaseModel.__init__(self, opt)>
26 Then, you need to define four lists:
27 -- self.loss_names (str list): specify the training losses that you want to plot and save.
28 -- self.model_names (str list): specify the images that you want to display and save.
29 -- self.visual_names (str list): define networks used in our training.
30 -- self.optimizers (optimizer list): define and initialize optimizers. You can define one optimizer for each network. If two networks are updated at the same time, you can use itertools.chain to group them. See cycle_gan_model.py for an example.
31 """
32 self.opt = opt
33 self.gpu_ids = opt.gpu_ids
34 self.isTrain = opt.isTrain
35 self.device = torch.device('cuda:{}'.format(self.gpu_ids[0])) if self.gpu_ids else torch.device('cpu') # get device name: CPU or GPU
36 self.save_dir = os.path.join(opt.checkpoints_dir, opt.name) # save all the checkpoints to save_dir
37 if opt.preprocess != 'scale_width': # with [scale_width], input images might have different sizes, which hurts the performance of cudnn.benchmark.
38 torch.backends.cudnn.benchmark = True
39 self.loss_names = []
40 self.model_names = []
41 self.visual_names = []
42 self.optimizers = []
43 self.image_paths = []
44 self.metric = 0 # used for learning rate policy 'plateau'
45
46 @staticmethod
47 def dict_grad_hook_factory(add_func=lambda x: x):
48 saved_dict = dict()
49
50 def hook_gen(name):
51 def grad_hook(grad):
52 saved_vals = add_func(grad)
53 saved_dict[name] = saved_vals
54 return grad_hook
55 return hook_gen, saved_dict
56
57 @staticmethod
58 def modify_commandline_options(parser, is_train):
59 """Add new model-specific options, and rewrite default values for existing options.
60
61 Parameters:
62 parser -- original option parser
63 is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
64
65 Returns:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected