130 lines
5.6 KiB
Python
130 lines
5.6 KiB
Python
"""This package contains modules related to objective functions, optimizations, and network architectures.
|
|
|
|
To add a custom model class called 'dummy', you need to add a file called 'dummy_model.py' and define a subclass DummyModel inherited from Base.
|
|
You need to implement the following five functions:
|
|
-- <__init__>: initialize the class; first call Base.__init__(self, opt).
|
|
-- <set_input>: unpack data from dataset and apply preprocessing.
|
|
-- <forward>: produce intermediate results.
|
|
-- <optimize_parameters>: calculate loss, gradients, and update network weights.
|
|
-- <modify_commandline_options>: (optionally) add model-specific options and set default options.
|
|
|
|
In the function <__init__>, you need to define four lists:
|
|
-- self.names_loss (str list): specify the training losses that you want to plot and save.
|
|
-- self.names_model (str list): define networks used in our training.
|
|
-- self.names_visual (str list): specify the images that you want to display and save.
|
|
-- 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 usage.
|
|
|
|
Now you can use the model class by specifying flag '--model dummy'.
|
|
See our template model class 'template_model.py' for more details.
|
|
"""
|
|
import torch
|
|
import torch.nn as nn
|
|
import functools
|
|
from torch.optim import lr_scheduler
|
|
|
|
import importlib
|
|
from src.models.base import Base
|
|
|
|
def create_model(opt, logger):
|
|
"""Create a model given the option.
|
|
|
|
This function warps the class CustomDatasetDataLoader.
|
|
This is the main interface between this package and 'train.py'/'test.py'
|
|
|
|
Example:
|
|
>>> from models import create_model
|
|
>>> model = create_model(opt)
|
|
"""
|
|
model = find_model_using_name(opt.MODEL.name)
|
|
instance = model(opt, logger)
|
|
|
|
print("model [%s] was created" % type(instance).__name__)
|
|
return instance
|
|
|
|
def find_model_using_name(model_name):
|
|
"""Import the module "models/[model_name]_model.py".
|
|
|
|
In the file, the class called DatasetNameModel() will
|
|
be instantiated. It has to be a subclass of Base,
|
|
and it is case-insensitive.
|
|
"""
|
|
model_filename = f"src.models.model_{model_name}"
|
|
modellib = importlib.import_module(model_filename)
|
|
|
|
model = None
|
|
target_model_name = model_name.replace('_', '') + 'model'
|
|
for name, cls in modellib.__dict__.items():
|
|
if name.lower() == target_model_name.lower() and issubclass(cls, Base):
|
|
model = cls
|
|
|
|
if model is None:
|
|
print(f"In {model_filename}.py, there should be a subclass of Base with class name that matches {target_model_name} in lowercase.")
|
|
raise("ERROR")
|
|
|
|
return model
|
|
|
|
|
|
|
|
def calculate_model_params(model):
|
|
nparam = 0
|
|
for iparam in model.parameters():
|
|
nparam += iparam.numel()
|
|
return nparam
|
|
|
|
|
|
def init_weights(net, init_type='normal', init_gain=0.02):
|
|
"""Initialize network weights.
|
|
|
|
Parameters:
|
|
net (network) -- network to be initialized
|
|
init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
|
|
init_gain (float) -- scaling factor for normal, xavier and orthogonal.
|
|
|
|
We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might
|
|
work better for some applications. Feel free to try yourself.
|
|
"""
|
|
def init_func(m): # define the initialization function
|
|
classname = m.__class__.__name__
|
|
#print(classname, hasattr(m, 'weight'),)
|
|
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
|
|
if init_type == 'normal':
|
|
nn.init.normal_(m.weight.data, 0.0, init_gain)
|
|
elif init_type == 'xavier':
|
|
nn.init.xavier_normal_(m.weight.data, gain=init_gain)
|
|
elif init_type == 'kaiming':
|
|
nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
|
|
elif init_type == 'orthogonal':
|
|
nn.init.orthogonal_(m.weight.data, gain=init_gain)
|
|
else:
|
|
raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
|
|
|
|
if hasattr(m, 'bias') and m.bias is not None:
|
|
nn.init.constant_(m.bias.data, 0.0)
|
|
|
|
elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
|
|
nn.init.normal_(m.weight.data, 1.0, init_gain)
|
|
nn.init.constant_(m.bias.data, 0.0)
|
|
|
|
print('initialize network with %s' % init_type)
|
|
net.apply(init_func) # apply the initialization function <init_func>
|
|
|
|
|
|
def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]):
|
|
"""Initialize a network: 1. register CPU/GPU device (with multi-GPU support); 2. initialize the network weights
|
|
Parameters:
|
|
net (network) -- the network to be initialized
|
|
init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
|
|
gain (float) -- scaling factor for normal, xavier and orthogonal.
|
|
gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2
|
|
|
|
Return an initialized network.
|
|
"""
|
|
|
|
if len(gpu_ids) > 0:
|
|
assert(torch.cuda.is_available())
|
|
net.to(f"cuda:{gpu_ids[0]}")
|
|
net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs
|
|
|
|
init_weights(net, init_type, init_gain=init_gain)
|
|
|
|
return net |