def __init__(self, reduction='mean', loss_weight=1.0): # when loss weight less than zero return None if loss_weight <= 0: return None self._l1_loss = nn.L1Loss(reduction) self.loss_weight = loss_weight self.reduction = reduction
def __init__(self, **cfg): """Initialize the AnimeGANV2 class. Parameters: opt (config dict)-- stores all the experiment flags; needs to be a subclass of Dict """ super(AnimeGANV2Model, self).__init__() # define networks (both generator and discriminator) self.cfg = EasyDict(**cfg) self.nets['netG'] = build_generator(self.cfg.generator) init_weights(self.nets['netG']) # define a discriminator; conditional GANs need to take both input and output images; Therefore, #channels for D is input_nc + output_nc if self.is_train: self.nets['netD'] = build_discriminator(self.cfg.discriminator) init_weights(self.nets['netD']) self.pretrained = CaffeVGG19() self.losses = {} # define loss functions self.criterionGAN = GANLoss(self.cfg.gan_mode) self.criterionL1 = nn.L1Loss() self.criterionHub = nn.SmoothL1Loss() if self.cfg.pretrain_ckpt: state_dicts = load(self.cfg.pretrain_ckpt) self.nets['netG'].set_state_dict(state_dicts['netG']) print('Load pretrained generator from', self.cfg.pretrain_ckpt)
def __init__( self, layer_weights, vgg_type='vgg19', use_input_norm=True, perceptual_weight=1.0, style_weight=1.0, norm_img=True, pretrained='https://paddlegan.bj.bcebos.com/model/vgg19.pdparams', criterion='l1'): super(PerceptualLoss, self).__init__() # when loss weight less than zero return None if perceptual_weight <= 0 and style_weight <= 0: return None self.norm_img = norm_img self.perceptual_weight = perceptual_weight self.style_weight = style_weight self.layer_weights = layer_weights self.vgg = PerceptualVGG(layer_name_list=list(layer_weights.keys()), vgg_type=vgg_type, use_input_norm=use_input_norm, pretrained_url=pretrained) if criterion == 'l1': self.criterion = nn.L1Loss() else: raise NotImplementedError( f'{criterion} criterion has not been supported in' ' this version.')
def __init__(self, cfg, criterion='l1', num_scales=1): super().__init__() self.model = build_model() self.num_scales = num_scales self.criterion = nn.L1Loss() self.resize = False self.weights = [0.03125, 0.0625, 0.125, 0.25, 1.0]
def __init__(self, criterion='l1'): super(FeatureMatchingLoss, self).__init__() if criterion == 'l1': self.criterion = nn.L1Loss(reduction='mean') elif criterion == 'l2' or criterion == 'mse': self.criterion = dg.MSELoss(reduction='mean') else: raise ValueError("Criterion %s is not recognized" % criterion)
def __init__(self, mode="l2", **kargs): super().__init__() assert mode in ["l1", "l2", "smooth_l1"] if mode == "l1": self.loss_func = nn.L1Loss(**kargs) elif mode == "l2": self.loss_func = nn.MSELoss(**kargs) elif mode == "smooth_l1": self.loss_func = nn.SmoothL1Loss(**kargs)
def __init__(self, cfg): super(FlowLoss, self).__init__() self.cfg = cfg self.data_cfg = cfg.data self.criterion = nn.L1Loss() self.criterionMasked = MaskedL1Loss() # self.flowNet = DummyFlowNet() # in cpu for test self.flowNet = build_model() self.warp_ref = getattr(cfg.gen.flow, 'warp_ref', False) self.pose_cfg = pose_cfg = getattr(cfg.data, 'for_pose_dataset', None) self.for_pose_dataset = pose_cfg is not None self.has_fg = getattr(cfg.data, 'has_foreground', False)
def __init__(self, cfg): """Initialize the CycleGAN class. Parameters: opt (config)-- stores all the experiment flags; needs to be a subclass of Dict """ super(UGATITModel, self).__init__(cfg) # define networks (both Generators and discriminators) # The naming is different from those used in the paper. self.nets['genA2B'] = build_generator(cfg.model.generator) self.nets['genB2A'] = build_generator(cfg.model.generator) init_weights(self.nets['genA2B']) init_weights(self.nets['genB2A']) if self.is_train: # define discriminators self.nets['disGA'] = build_discriminator(cfg.model.discriminator_g) self.nets['disGB'] = build_discriminator(cfg.model.discriminator_g) self.nets['disLA'] = build_discriminator(cfg.model.discriminator_l) self.nets['disLB'] = build_discriminator(cfg.model.discriminator_l) init_weights(self.nets['disGA']) init_weights(self.nets['disGB']) init_weights(self.nets['disLA']) init_weights(self.nets['disLB']) if self.is_train: # define loss functions self.BCE_loss = nn.BCEWithLogitsLoss() self.L1_loss = nn.L1Loss() self.MSE_loss = nn.MSELoss() self.build_lr_scheduler() self.optimizers['optimizer_G'] = build_optimizer( cfg.optimizer, self.lr_scheduler, parameter_list=self.nets['genA2B'].parameters() + self.nets['genB2A'].parameters()) self.optimizers['optimizer_D'] = build_optimizer( cfg.optimizer, self.lr_scheduler, parameter_list=self.nets['disGA'].parameters() + self.nets['disGB'].parameters() + self.nets['disLA'].parameters() + self.nets['disLB'].parameters()) self.Rho_clipper = RhoClipper(0, 1)
def __init__(self, generator, discriminator=None, gan_criterion=None, pretrain_ckpt=None, g_adv_weight=300., d_adv_weight=300., con_weight=1.5, sty_weight=2.5, color_weight=10., tv_weight=1.): """Initialize the AnimeGANV2 class. Parameters: opt (config dict)-- stores all the experiment flags; needs to be a subclass of Dict """ super(AnimeGANV2Model, self).__init__() self.g_adv_weight = g_adv_weight self.d_adv_weight = d_adv_weight self.con_weight = con_weight self.sty_weight = sty_weight self.color_weight = color_weight self.tv_weight = tv_weight # define networks (both generator and discriminator) self.nets['netG'] = build_generator(generator) init_weights(self.nets['netG']) # define a discriminator; conditional GANs need to take both input and output images; Therefore, #channels for D is input_nc + output_nc if self.is_train: self.nets['netD'] = build_discriminator(discriminator) init_weights(self.nets['netD']) self.pretrained = CaffeVGG19() self.losses = {} # define loss functions self.criterionGAN = build_criterion(gan_criterion) self.criterionL1 = nn.L1Loss() self.criterionHub = nn.SmoothL1Loss() if pretrain_ckpt: state_dicts = load(pretrain_ckpt) self.nets['netG'].set_state_dict(state_dicts['netG']) print('Load pretrained generator from', pretrain_ckpt)
# 优化器 optimizerG = paddle.optimizer.Adam( learning_rate=LR, parameters=generator.parameters(), beta1=0.5, beta2=0.999) optimizerD = paddle.optimizer.Adam( learning_rate=LR, parameters=discriminator.parameters(), beta1=0.5, beta2=0.999) # 损失函数 bce_loss = nn.BCELoss() l1_loss = nn.L1Loss() # dataloader data_loader_train = DataLoader( paired_dataset_train, batch_size=BATCH_SIZE, shuffle=True, drop_last=True ) data_loader_test = DataLoader( paired_dataset_test, batch_size=BATCH_SIZE ) results_save_path = 'work/results' os.makedirs(results_save_path, exist_ok=True) # 保存每个epoch的测试结果
def main(args): """ Call the configuration function of the model, build the model and load data, then start training. model_config: a json file with the hyperparameters,such as dropout rate ,learning rate,num tasks and so on; num_tasks: it means the number of task that each dataset contains, it's related to the dataset; """ ### config for the body compound_encoder_config = load_json_config(args.compound_encoder_config) if not args.dropout_rate is None: compound_encoder_config['dropout_rate'] = args.dropout_rate ### config for the downstream task task_type = 'regr' metric = get_metric(args.dataset_name) task_names = get_downstream_task_names(args.dataset_name, args.data_path) dataset_stat = get_dataset_stat(args.dataset_name, args.data_path, task_names) label_mean = np.reshape(dataset_stat['mean'], [1, -1]) label_std = np.reshape(dataset_stat['std'], [1, -1]) model_config = load_json_config(args.model_config) if not args.dropout_rate is None: model_config['dropout_rate'] = args.dropout_rate model_config['task_type'] = task_type model_config['num_tasks'] = len(task_names) print('model_config:') print(model_config) ### build model compound_encoder = GeoGNNModel(compound_encoder_config) model = DownstreamModel(model_config, compound_encoder) if metric == 'square': criterion = nn.MSELoss() else: criterion = nn.L1Loss() encoder_params = compound_encoder.parameters() head_params = exempt_parameters(model.parameters(), encoder_params) encoder_opt = paddle.optimizer.Adam(args.encoder_lr, parameters=encoder_params) head_opt = paddle.optimizer.Adam(args.head_lr, parameters=head_params) print('Total param num: %s' % (len(model.parameters()))) print('Encoder param num: %s' % (len(encoder_params))) print('Head param num: %s' % (len(head_params))) for i, param in enumerate(model.named_parameters()): print(i, param[0], param[1].name) if not args.init_model is None and not args.init_model == "": compound_encoder.set_state_dict(paddle.load(args.init_model)) print('Load state_dict from %s' % args.init_model) ### load data if args.task == 'data': print('Preprocessing data...') dataset = get_dataset(args.dataset_name, args.data_path, task_names) transform_fn = DownstreamTransformFn() dataset.transform(transform_fn, num_workers=args.num_workers) dataset.save_data(args.cached_data_path) return else: if args.cached_data_path is None or args.cached_data_path == "": print('Processing data...') dataset = get_dataset(args.dataset_name, args.data_path, task_names) transform_fn = DownstreamTransformFn() dataset.transform(transform_fn, num_workers=args.num_workers) else: print('Read preprocessing data...') dataset = InMemoryDataset(npz_data_path=args.cached_data_path) splitter = create_splitter(args.split_type) train_dataset, valid_dataset, test_dataset = splitter.split(dataset, frac_train=0.8, frac_valid=0.1, frac_test=0.1) print("Train/Valid/Test num: %s/%s/%s" % (len(train_dataset), len(valid_dataset), len(test_dataset))) print('Train min/max/mean %s/%s/%s' % get_label_stat(train_dataset)) print('Valid min/max/mean %s/%s/%s' % get_label_stat(valid_dataset)) print('Test min/max/mean %s/%s/%s' % get_label_stat(test_dataset)) ### start train list_val_metric, list_test_metric = [], [] collate_fn = DownstreamCollateFn( atom_names=compound_encoder_config['atom_names'], bond_names=compound_encoder_config['bond_names'], bond_float_names=compound_encoder_config['bond_float_names'], bond_angle_float_names=compound_encoder_config[ 'bond_angle_float_names'], task_type=task_type) for epoch_id in range(args.max_epoch): train_loss = train(args, model, label_mean, label_std, train_dataset, collate_fn, criterion, encoder_opt, head_opt) val_metric = evaluate(args, model, label_mean, label_std, valid_dataset, collate_fn, metric) test_metric = evaluate(args, model, label_mean, label_std, test_dataset, collate_fn, metric) list_val_metric.append(val_metric) list_test_metric.append(test_metric) test_metric_by_eval = list_test_metric[np.argmin(list_val_metric)] print("epoch:%s train/loss:%s" % (epoch_id, train_loss)) print("epoch:%s val/%s:%s" % (epoch_id, metric, val_metric)) print("epoch:%s test/%s:%s" % (epoch_id, metric, test_metric)) print("epoch:%s test/%s_by_eval:%s" % (epoch_id, metric, test_metric_by_eval)) paddle.save( compound_encoder.state_dict(), '%s/epoch%d/compound_encoder.pdparams' % (args.model_dir, epoch_id)) paddle.save(model.state_dict(), '%s/epoch%d/model.pdparams' % (args.model_dir, epoch_id)) outs = { 'model_config': basename(args.model_config).replace('.json', ''), 'metric': '', 'dataset': args.dataset_name, 'split_type': args.split_type, 'batch_size': args.batch_size, 'dropout_rate': args.dropout_rate, 'encoder_lr': args.encoder_lr, 'head_lr': args.head_lr, } best_epoch_id = np.argmin(list_val_metric) for metric, value in [('test_%s' % metric, list_test_metric[best_epoch_id]), ('max_valid_%s' % metric, np.min(list_val_metric)), ('max_test_%s' % metric, np.min(list_test_metric))]: outs['metric'] = metric print('\t'.join(['FINAL'] + ["%s:%s" % (k, outs[k]) for k in outs] + [str(value)]))
def __init__(self, normalize_over_valid=False): super(MaskedL1Loss, self).__init__() self.criterion = nn.L1Loss() self.normalize_over_valid = normalize_over_valid
def __init__( self, num_classes, width=1.0, strides=[8, 16, 32], in_channels=[256, 512, 1024], act="silu", depthwise=False, iou_loss=None, yolo_loss=None, nms_cfg=None, prior_prob=0.01, is_train=False, ): """ Args: act (str): activation type of conv. Defalut value: "silu". depthwise (bool): whether apply depthwise conv in conv branch. Defalut value: False. """ super().__init__() self.n_anchors = 1 # self.is_train = is_train self.num_classes = num_classes self.decode_in_inference = True # for deploy, set to False self.cls_convs = nn.LayerList() self.reg_convs = nn.LayerList() self.cls_preds = nn.LayerList() self.reg_preds = nn.LayerList() self.obj_preds = nn.LayerList() self.stems = nn.LayerList() Conv = DWConv if depthwise else BaseConv self.prior_prob = prior_prob # 类别分支最后的卷积。设置偏移的初始值使得各类别预测概率初始值为self.prior_prob (根据激活函数是sigmoid()时推导出,和RetinaNet中一样) bias_init_value = -math.log((1 - self.prior_prob) / self.prior_prob) for i in range(len(in_channels)): self.stems.append( BaseConv( in_channels=int(in_channels[i] * width), out_channels=int(256 * width), ksize=1, stride=1, act=act, )) self.cls_convs.append( nn.Sequential(*[ Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), ])) self.reg_convs.append( nn.Sequential(*[ Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), Conv( in_channels=int(256 * width), out_channels=int(256 * width), ksize=3, stride=1, act=act, ), ])) battr1 = ParamAttr( initializer=Constant(bias_init_value), regularizer=L2Decay(0.) ) # 不可以加正则化的参数:norm层(比如bn层、affine_channel层、gn层)的scale、offset;卷积层的偏移参数。 cls_pred_conv = nn.Conv2D(in_channels=int(256 * width), out_channels=self.n_anchors * self.num_classes, kernel_size=1, stride=1, padding=0, bias_attr=battr1) self.cls_preds.append(cls_pred_conv) battr2 = ParamAttr( initializer=Constant(bias_init_value), regularizer=L2Decay(0.) ) # 不可以加正则化的参数:norm层(比如bn层、affine_channel层、gn层)的scale、offset;卷积层的偏移参数。 reg_pred_conv = nn.Conv2D(in_channels=int(256 * width), out_channels=4, kernel_size=1, stride=1, padding=0, bias_attr=battr2) self.reg_preds.append(reg_pred_conv) self.obj_preds.append( nn.Conv2D( in_channels=int(256 * width), out_channels=self.n_anchors * 1, kernel_size=1, stride=1, padding=0, )) self.use_l1 = False self.l1_loss = nn.L1Loss(reduction="none") self.bcewithlog_loss = nn.BCEWithLogitsLoss(reduction="none") self.iou_loss = iou_loss self.strides = strides self.grids = [paddle.zeros((1, ))] * len(in_channels) self.yolo_loss = yolo_loss self.nms_cfg = nms_cfg
def __init__(self): super(LabelL1Loss, self).__init__() self.l1loss = nn.L1Loss()