Python torch.nn.SmoothL1Loss() Examples
The following are 30
code examples of torch.nn.SmoothL1Loss().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
torch.nn
, or try the search function
.
Example #1
Source File: loss.py From zsgnet-pytorch with MIT License | 6 votes |
def __init__(self, ratios, scales, cfg): super().__init__() self.cfg = cfg self.ratios = ratios self.scales = scales self.alpha = cfg['alpha'] self.gamma = cfg['gamma'] # Which loss fucntion to use self.use_focal = cfg['use_focal'] self.use_softmax = cfg['use_softmax'] self.use_multi = cfg['use_multi'] self.lamb_reg = cfg['lamb_reg'] self.loss_keys = ['loss', 'cls_ls', 'box_ls'] self.anchs = None self.get_anchors = partial( create_anchors, ratios=self.ratios, scales=self.scales, flatten=True) self.box_loss = nn.SmoothL1Loss(reduction='none')
Example #2
Source File: loss.py From tiny-faces-pytorch with MIT License | 6 votes |
def __init__(self, n_templates=25, reg_weight=1, pos_fraction=0.5): super().__init__() # We don't want per element averaging. # We want to normalize over the batch or positive samples. self.regression_criterion = nn.SmoothL1Loss(reduction='none') self.classification_criterion = nn.SoftMarginLoss(reduction='none') self.n_templates = n_templates self.reg_weight = reg_weight self.pos_fraction = pos_fraction self.class_average = AvgMeter() self.reg_average = AvgMeter() self.masked_class_loss = None self.masked_reg_loss = None self.total_loss = None
Example #3
Source File: multiscale.py From CRL_pytorch with MIT License | 6 votes |
def __init__(self, scales, downscale, weights=None, loss='MSE'): super(MultiScaleLoss, self).__init__() self.downscale = downscale self.weights = torch.Tensor(scales).fill_(1) if weights is None else torch.Tensor(weights) assert (len(weights) == scales) if type(loss) is str: assert (loss in ['L1', 'MSE', 'SmoothL1']) if loss == 'L1': self.loss = nn.L1Loss() elif loss == 'MSE': self.loss = nn.MSELoss() elif loss == 'SmoothL1': self.loss = nn.SmoothL1Loss() else: self.loss = loss self.multiScales = [nn.AvgPool2d(self.downscale * (2 ** i), self.downscale * (2 ** i)) for i in range(scales)]
Example #4
Source File: models.py From Towards-Realtime-MOT with MIT License | 6 votes |
def __init__(self, anchors, nC, nID, nE, img_size, yolo_layer): super(YOLOLayer, self).__init__() self.layer = yolo_layer nA = len(anchors) self.anchors = torch.FloatTensor(anchors) self.nA = nA # number of anchors (3) self.nC = nC # number of classes (80) self.nID = nID # number of identities self.img_size = 0 self.emb_dim = nE self.shift = [1, 3, 5] self.SmoothL1Loss = nn.SmoothL1Loss() self.SoftmaxLoss = nn.CrossEntropyLoss(ignore_index=-1) self.CrossEntropyLoss = nn.CrossEntropyLoss() self.IDLoss = nn.CrossEntropyLoss(ignore_index=-1) self.s_c = nn.Parameter(-4.15*torch.ones(1)) # -4.15 self.s_r = nn.Parameter(-4.85*torch.ones(1)) # -4.85 self.s_id = nn.Parameter(-2.3*torch.ones(1)) # -2.3 self.emb_scale = math.sqrt(2) * math.log(self.nID-1) if self.nID>1 else 1
Example #5
Source File: cmp.py From conditional-motion-propagation with MIT License | 6 votes |
def __init__(self, params, dist_model=False): super(CMP, self).__init__(params, dist_model) model_params = params['module'] # define loss if model_params['flow_criterion'] == 'L1': self.flow_criterion = nn.SmoothL1Loss() elif model_params['flow_criterion'] == 'L2': self.flow_criterion = nn.MSELoss() elif model_params['flow_criterion'] == 'DiscreteLoss': self.flow_criterion = losses.DiscreteLoss( nbins=model_params['nbins'], fmax=model_params['fmax']) else: raise Exception("No such flow loss: {}".format(model_params['flow_criterion'])) self.fuser = utils.Fuser(nbins=model_params['nbins'], fmax=model_params['fmax']) self.model_params = model_params
Example #6
Source File: perceptual_loss.py From PerceptualGAN with GNU General Public License v3.0 | 6 votes |
def __init__( self, matching_type='features', matching_loss='L1', average_loss=True): super(Matcher, self).__init__() # Matched statistics if matching_type == 'features': self.get_stats = self.gram_matrix elif matching_type == 'features': self.get_stats = lambda x: x # Loss function matching_loss = matching_loss.lower() if matching_loss == 'mse': self.criterion = nn.MSELoss() elif matching_loss == 'smoothl1': self.criterion = nn.SmoothL1Loss() elif matching_loss == 'l1': self.criterion = nn.L1Loss() self.average_loss = average_loss
Example #7
Source File: layers.py From FeatureFlow with MIT License | 6 votes |
def __init__(self): super(DetailEnhance, self).__init__() self.feature_level = 3 self.extract_features = model.ValidationFeatures() self.extract_aligned_features = model.ExtractAlignedFeatures(n_res=5) # 4 5 self.pcd_align = model.PCD_Align(groups=8) # 4 8 self.tsa_fusion = model.TSA_Fusion(nframes=3, center=1) self.reconstruct = model.Reconstruct(n_res=20) # 5 40 # Loss calculate self.L1_lossFn = nn.L1Loss() self.sL1_lossFn = nn.SmoothL1Loss() self.cL1_lossFn = loss.CharbonnierLoss() self.MSE_LossFn = nn.MSELoss()
Example #8
Source File: test_common.py From PPGNet with MIT License | 6 votes |
def setUp(self): th.manual_seed(789) self.net1 = nn.Sequential( nn.Conv2d(16, 32, 3, 1, 1, bias=True), nn.BatchNorm2d(32), nn.ReLU(inplace=True) ) self.net2 = nn.Sequential( nn.Conv2d(32, 16, 3, 1, 1, bias=True), nn.BatchNorm2d(16), nn.ReLU(inplace=True), nn.Conv2d(16, 1, 3, 1, 1, bias=True), ) self.test_data = dict( X = th.rand(100, 16, 32, 32), Y = th.rand(100, 1, 32, 32), ) self.net1.apply(common.weights_init) self.net2.apply(common.weights_init) self.crit = nn.SmoothL1Loss()
Example #9
Source File: layers.py From FeatureFlow with MIT License | 6 votes |
def __init__(self): super(DetailEnhance_last, self).__init__() self.feature_level = 3 self.extract_features = model.ValidationFeatures() self.extract_aligned_features = model.ExtractAlignedFeatures(n_res=5) # 4 5 self.pcd_align = model.PCD_Align(groups=8) # 4 8 self.tsa_fusion = model.TSA_Fusion(nframes=3, center=1) self.reconstruct = model.Reconstruct(n_res=20) # 5 40 # Loss calculate self.L1_lossFn = nn.L1Loss() self.sL1_lossFn = nn.SmoothL1Loss() self.cL1_lossFn = loss.CharbonnierLoss() self.MSE_LossFn = nn.MSELoss()
Example #10
Source File: losses.py From pytorch-mono-depth with MIT License | 5 votes |
def __init__(self): super().__init__() self.loss = nn.SmoothL1Loss(size_average=False)
Example #11
Source File: ModelZoo.py From ANR with GNU General Public License v3.0 | 5 votes |
def selectLossFunction(self, loss_function = "MSELoss"): self.loss_function = loss_function.strip() if(self.loss_function == "MSELoss"): return nn.MSELoss() elif(self.loss_function == "L1Loss"): return nn.L1Loss() elif(self.loss_function == "SmoothL1Loss"): return nn.SmoothL1Loss() # Use MSELoss by default self.loss_function = "MSELoss" return nn.MSELoss()
Example #12
Source File: ssn_ops.py From code with MIT License | 5 votes |
def __init__(self): super(ClassWiseRegressionLoss, self).__init__() self.smooth_l1_loss = nn.SmoothL1Loss()
Example #13
Source File: modules.py From oyster with MIT License | 5 votes |
def __init__(self, delta=1): super().__init__() self.huber_loss_delta1 = nn.SmoothL1Loss() self.delta = delta
Example #14
Source File: base_model.py From training with Apache License 2.0 | 5 votes |
def __init__(self, dboxes): super(Loss, self).__init__() self.scale_xy = 1.0/dboxes.scale_xy self.scale_wh = 1.0/dboxes.scale_wh self.sl1_loss = nn.SmoothL1Loss(reduce=False) self.dboxes = nn.Parameter(dboxes(order="xywh").transpose(0, 1).unsqueeze(dim = 0), requires_grad=False) # Two factor are from following links # http://jany.st/post/2017-11-05-single-shot-detector-ssd-from-scratch-in-tensorflow.html self.con_loss = nn.CrossEntropyLoss(reduce=False)
Example #15
Source File: layers.py From DeepSEED-3D-ConvNets-for-Pulmonary-Nodule-Detection with MIT License | 5 votes |
def __init__(self, num_hard = 0): super(Loss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = nn.BCELoss() self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard
Example #16
Source File: losses.py From soccerontable with BSD 2-Clause "Simplified" License | 5 votes |
def __init__(self): super(MaskedSmoothL1, self).__init__() self.criterion = nn.SmoothL1Loss(size_average=True)
Example #17
Source File: ctpn_model.py From ocr.pytorch with MIT License | 5 votes |
def __init__(self,device): super(RPN_CLS_Loss, self).__init__() self.device = device self.L_cls = nn.CrossEntropyLoss(reduction='none') # self.L_regr = nn.SmoothL1Loss() # self.L_refi = nn.SmoothL1Loss() self.pos_neg_ratio = 3
Example #18
Source File: ssn_ops.py From action-detection with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self): super(ClassWiseRegressionLoss, self).__init__() self.smooth_l1_loss = nn.SmoothL1Loss()
Example #19
Source File: layers_se.py From DeepSEED-3D-ConvNets-for-Pulmonary-Nodule-Detection with MIT License | 5 votes |
def __init__(self, num_hard = 0): super(Loss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = nn.BCELoss() self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard
Example #20
Source File: layers_se.py From DeepSEED-3D-ConvNets-for-Pulmonary-Nodule-Detection with MIT License | 5 votes |
def __init__(self, num_hard=0): super(FocalLoss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = BinaryFocalLoss(gamma=2, alpha=0.5, size_average=False) self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard
Example #21
Source File: layers.py From lung_nodule_detector with MIT License | 5 votes |
def __init__(self, num_hard=0): super(FocalLoss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = BinaryFocalLoss(gamma=2, alpha=0.5, size_average=False) self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard
Example #22
Source File: layers.py From DeepSEED-3D-ConvNets-for-Pulmonary-Nodule-Detection with MIT License | 5 votes |
def __init__(self, num_hard=0): super(FocalLoss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = BinaryFocalLoss(gamma=2, alpha=0.5, size_average=False) self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard
Example #23
Source File: layers_se.py From DeepSEED-3D-ConvNets-for-Pulmonary-Nodule-Detection with MIT License | 5 votes |
def __init__(self, num_hard = 0): super(Loss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = nn.BCELoss() self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard
Example #24
Source File: atari_a2c.py From mario_rl with MIT License | 5 votes |
def train_model(self, s_batch, target_batch, y_batch, adv_batch): with torch.no_grad(): s_batch = torch.FloatTensor(s_batch).to(self.device) target_batch = torch.FloatTensor(target_batch).to(self.device) y_batch = torch.LongTensor(y_batch).to(self.device) adv_batch = torch.FloatTensor(adv_batch).to(self.device) # for multiply advantage policy, value = self.model(s_batch) m = Categorical(F.softmax(policy, dim=-1)) # mse = nn.SmoothL1Loss() mse = nn.MSELoss() # Actor loss actor_loss = -m.log_prob(y_batch) * adv_batch # Entropy(for more exploration) entropy = m.entropy() # Critic loss critic_loss = mse(value.sum(1), target_batch) # Total loss loss = actor_loss.mean() + 0.5 * critic_loss - entropy_coef * entropy.mean() self.optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm_(self.model.parameters(), clip_grad_norm) self.optimizer.step()
Example #25
Source File: cartpole_a2c.py From mario_rl with MIT License | 5 votes |
def train_model(self, s_batch, target_batch, y_batch, adv_batch): with torch.no_grad(): s_batch = torch.FloatTensor(s_batch).to(self.device) target_batch = torch.FloatTensor(target_batch).to(self.device) y_batch = torch.LongTensor(y_batch).to(self.device) adv_batch = torch.FloatTensor(adv_batch).to(self.device) policy, value = self.model(s_batch) m = Categorical(F.softmax(policy, dim=-1)) # mse = nn.SmoothL1Loss() mse = nn.MSELoss() # Actor loss actor_loss = -m.log_prob(y_batch) * adv_batch # Entropy(for more exploration) entropy = m.entropy() # Critic loss critic_loss = mse(value.sum(1), target_batch) # Total loss loss = actor_loss.mean() + 0.5 * critic_loss - 0.01 * entropy.mean() self.optimizer.zero_grad() loss.backward() torch.nn.utils.clip_grad_norm_(self.model.parameters(), 3) self.optimizer.step()
Example #26
Source File: __init__.py From MedicalZooPytorch with MIT License | 5 votes |
def create_loss(name, weight=None, ignore_index=None, pos_weight=None): if name == 'BCEWithLogitsLoss': return nn.BCEWithLogitsLoss(pos_weight=pos_weight) elif name == 'BCEDiceLoss': return BCEDiceLoss(alpha=1, beta=1) elif name == 'CrossEntropyLoss': if ignore_index is None: ignore_index = -100 # use the default 'ignore_index' as defined in the CrossEntropyLoss return nn.CrossEntropyLoss(weight=weight, ignore_index=ignore_index) elif name == 'WeightedCrossEntropyLoss': if ignore_index is None: ignore_index = -100 # use the default 'ignore_index' as defined in the CrossEntropyLoss return WeightedCrossEntropyLoss(ignore_index=ignore_index) elif name == 'PixelWiseCrossEntropyLoss': return PixelWiseCrossEntropyLoss(class_weights=weight, ignore_index=ignore_index) elif name == 'GeneralizedDiceLoss': return GeneralizedDiceLoss(sigmoid_normalization=False) elif name == 'DiceLoss': return DiceLoss(weight=weight, sigmoid_normalization=False) elif name == 'TagsAngularLoss': return TagsAngularLoss() elif name == 'MSELoss': return MSELoss() elif name == 'SmoothL1Loss': return SmoothL1Loss() elif name == 'L1Loss': return L1Loss() elif name == 'WeightedSmoothL1Loss': return WeightedSmoothL1Loss() else: raise RuntimeError(f"Unsupported loss function: '{name}'. Supported losses: {SUPPORTED_LOSSES}")
Example #27
Source File: modules.py From rlkit with MIT License | 5 votes |
def __init__(self, delta=1): super().__init__() self.huber_loss_delta1 = nn.SmoothL1Loss() self.delta = delta
Example #28
Source File: main.py From HUAWEIOCR-2019 with MIT License | 5 votes |
def __init__(self): super(Loss, self).__init__() self.classify_loss = nn.BCELoss() self.sigmoid = nn.Sigmoid() self.regress_loss = nn.SmoothL1Loss()
Example #29
Source File: base_model.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def __init__(self, dboxes): super(Loss, self).__init__() self.scale_xy = 1.0/dboxes.scale_xy self.scale_wh = 1.0/dboxes.scale_wh self.sl1_loss = nn.SmoothL1Loss(reduce=False) self.dboxes = nn.Parameter(dboxes(order="xywh").transpose(0, 1).unsqueeze(dim = 0), requires_grad=False) # Two factor are from following links # http://jany.st/post/2017-11-05-single-shot-detector-ssd-from-scratch-in-tensorflow.html self.con_loss = nn.CrossEntropyLoss(reduce=False)
Example #30
Source File: layers.py From DeepLung with GNU General Public License v3.0 | 5 votes |
def __init__(self, num_hard = 0): super(Loss, self).__init__() self.sigmoid = nn.Sigmoid() self.classify_loss = nn.BCELoss() self.regress_loss = nn.SmoothL1Loss() self.num_hard = num_hard