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