def loss_calc(out, label, gpu0): """ This function returns cross entropy loss for semantic segmentation """ # out shape batch_size x channels x h x w -> batch_size x channels x h x w # label shape h x w x 1 x batch_size -> batch_size x 1 x h x w label = label[:, :, 0, :].transpose(2, 0, 1) label = torch.from_numpy(label).long() if useGPU: label = Variable(label).cuda(gpu0) if onlyLesions: criterion = nn.NLLLoss2d( weight=torch.cuda.FloatTensor([1, 100000])) else: criterion = nn.NLLLoss2d( weight=torch.cuda.FloatTensor([1, 100000, 100000])) else: label = Variable(label) if onlyLesions: criterion = nn.NLLLoss2d(weight=torch.FloatTensor([1, 100000])) else: criterion = nn.NLLLoss2d( weight=torch.FloatTensor([1, 100000, 100000])) m = nn.LogSoftmax() out = m(out) return criterion(out, label)
def spatial_cross_entropy_criterion(input, target): n_class = input.data.size()[1] weights = torch.FloatTensor(n_class).fill_(1.0) weights[0] = 0.5 if args.use_cuda: log_soft_max = nn.LogSoftmax().cuda() nll_loss_2d = nn.NLLLoss2d(weights).cuda() else: log_soft_max = nn.LogSoftmax() nll_loss_2d = nn.NLLLoss2d(weights) return nll_loss_2d(log_soft_max(input), target)
def __init__(self, cf): # If we load from a pretrained model self.exp_dir = cf.savepath + '___' + datetime.now().strftime( '%a, %d %b %Y-%m-%d %H:%M:%S') + '_' + cf.model_name os.mkdir(self.exp_dir) # Enable log file self.log_file = os.path.join(self.exp_dir, "logfile.log") sys.stdout = Logger(self.log_file) self.model_name = cf.model_name self.num_classes = cf.num_classes if cf.model_name == 'segnet_basic': pretrained_net = FeatureResNet() pretrained_net.load_state_dict(models.resnet34(pretrained=True).state_dict()) self.net = SegResNet(cf.num_classes, pretrained_net).cuda() elif cf.model_name == 'drn_c_26': self.net = DRNSeg('drn_c_26', cf.num_classes, pretrained_model=None, pretrained=True) elif cf.model_name == 'drn_d_22': self.net = DRNSeg('drn_d_22', cf.num_classes, pretrained_model=None, pretrained=True) elif cf.model_name == 'drn_d_38': self.net = DRNSeg('drn_d_38', cf.num_classes, pretrained_model=None, pretrained=True) # Set the loss criterion if cf.cb_weights_method == 'rare_freq_cost': print('Use ' + cf.cb_weights_method + ', loss weight method!') loss_weight = torch.Tensor([0] + cf.cb_weights) self.crit = nn.NLLLoss2d(weight=loss_weight, ignore_index=cf.ignore_index).cuda() else: self.crit = nn.NLLLoss2d(ignore_index=cf.ignore_index).cuda() # we print the configuration file here so that the configuration is traceable self.cf = cf print(help(cf)) # Construct optimiser if cf.load_trained_model: print("Load from pretrained_model weight: " + cf.train_model_path) self.net.load_state_dict(torch.load(cf.train_model_path)) if cf.optimizer == 'rmsprop': self.optimiser = optim.RMSprop(self.net.optim_parameters(), lr=cf.learning_rate, momentum=cf.momentum, weight_decay=cf.weight_decay) elif cf.optimizer == 'sgd': self.optimiser = optim.SGD(self.net.optim_parameters(), lr=cf.learning_rate, momentum=cf.momentum, weight_decay=cf.weight_decay) elif cf.optimizer == 'adam': self.optimiser = optim.Adam(self.net.optim_parameters(), lr=cf.learning_rate, weight_decay=cf.weight_decay) self.scores, self.mean_scores = [], [] if torch.cuda.is_available(): self.net = self.net.cuda()
def my_eval_net(net, dataset, gpu=False, *args): tot = 0 num_images = len(dataset) t1 = time.time() data = iter(dataset) for i in range(num_images): img, lane, fs = next(data) # imgs = [] # lanes = [] # fss = [] # for i in range(1): # imgs.append(torch.from_numpy(img.transpose(2,0,1))) # lanes.append(torch.from_numpy(lane)) # fss.append(torch.from_numpy(fs)) # X, y_lane, y_fs = torch.stack(imgs, 0), torch.stack(lanes, 0).long(), torch.stack(fss, 0).long() if gpu: X = Variable(img).cuda() y1 = Variable(lane).cuda() y2 = Variable(fs).cuda() else: X = Variable(img) y1 = Variable(lane) y2 = Variable(fs) y_pred = net(X) yp1 = y_pred[:, :2, :, :] yp2 = y_pred[:, 2:, :, :] # draw mid_result if i % 10 == 0: data_processor.draw_fs(X, y1, y2, F.softmax(yp1), F.softmax(yp2), i, 'mid_result/{}/val/{}/'.format(args[0], t1)) prob1 = F.log_softmax(yp1) prob2 = F.log_softmax(yp2) loss1 = nn.NLLLoss2d(Variable(torch.FloatTensor([0.4, 1])).cuda())(prob1, y1) loss2 = nn.NLLLoss2d()(prob2, y2) loss = loss1 + loss2 tot += loss[0].data return tot / num_images
def loss_calc(out, label, cuda): """ This function returns cross entropy loss for segmentation """ # out shape batch_size x channels x h x w -> batch_size x channels x h x w # label shape batch_size x 1 x h x w -> batch_size x h x w rescale = nn.UpsamplingBilinear2d(size=(out.size()[2], out.size()[3])).cuda() label = label + 127.5 label[label == 255] = 1 label[label == 127.5] = 2 label = torch.from_numpy(label).float() class_weight = torch.FloatTensor([torch.sum(label), torch.sum(1 - label)]) class_weight = class_weight / torch.sum(class_weight) label = Variable(label) label = rescale(label).data[:, 0, :, :] label = Variable(label.long()) if cuda: label = label.cuda() out = out.cuda() class_weight = class_weight.cuda() m = nn.LogSoftmax() criterion = nn.NLLLoss2d(ignore_index=2) #criterion = nn.BCELoss() out = m(out) return criterion(out, label)
def focal_loss(y_estimated, y, parameters, mask=None, number_of_used_pixel=None, gamma=2): """ :param y_estimated: result of train(x) which is the forward action :param y: Label associated with x :param parameters: List of parameters of the network :param mask: Image with 1 were there is a class to predict, and 0 if not. :param number_of_used_pixel: Number of pixel with 1 in the mask. Useful to normalize the loss :return: difference between y_estimated and y, according to the cross entropy """ # http://pytorch.org/docs/master/nn.html : torch.nn.NLLLoss nllcrit = nn.NLLLoss2d(weight=parameters.weight_grad, size_average=False) # Apply softmax on the prediction y_estimated = F.log_softmax(input=y_estimated, dim=1) # Apply softmax then the log on the result, we use the idea in the article https://arxiv.org/pdf/1708.02002.pdf # This is a small modification of the algorithm y_estimated_2 = ((1 - torch.exp(y_estimated)) ** gamma) * y_estimated # Apply the mask y_estimated = y_estimated_2 * mask # Set all target value of number_classes to 0 (we could have choose another class. # The nllcrit will do y_estimated[k,0,i,j]*y[k,i,j] # It will be 0 if the class is parameters.number_classes : which is exactly what is expect for this class # The other classes remain unchanged y = y * (y != parameters.number_classes).long() # Apply the criterion define in the first line return nllcrit(y_estimated, y) / number_of_used_pixel
def main(test_cuda=False): start_time = time.time() maxe = 0 for i in range(1000): x = torch.rand(12800, 2) * random.randint(1, 10) x = Variable(x.cuda()) l = torch.rand(12800).ge(0.1).long() l = Variable(l.cuda()) output0 = FocalLoss(gamma=0)(x, l) output1 = nn.CrossEntropyLoss()(x, l) a = output0.data[0] b = output1.data[0] if abs(a - b) > maxe: maxe = abs(a - b) print('time:', time.time() - start_time, 'max_error:', maxe) start_time = time.time() maxe = 0 for i in range(100): x = torch.rand(128, 1000, 8, 4) * random.randint(1, 10) x = Variable(x.cuda()) l = torch.rand(128, 8, 4) * 1000 # 1000 is classes_num l = l.long() l = Variable(l.cuda()) output0 = FocalLoss(gamma=0)(x, l) output1 = nn.NLLLoss2d()(F.log_softmax(x), l) a = output0.data[0] b = output1.data[0] if abs(a - b) > maxe: maxe = abs(a - b) print('time:', time.time() - start_time, 'max_error:', maxe)
def my_val(): writer = SummaryWriter(log_dir= "0308_iou") args = parse_args() val_dir_img = '/shared/xudongliu/data/argoverse-tracking/argo_track_aggre5/val/npy_img/' val_dir_mask = '/shared/xudongliu/data/argoverse-tracking/argo_track_aggre5/val/npy_mask/' my_val = BasicDataset(val_dir_img, val_dir_mask) val_loader = torch.utils.data.DataLoader( # SegList(data_dir, 'val', transforms.Compose([ # transforms.RandomCrop(crop_size), # # transforms.RandomHorizontalFlip(), # transforms.ToTensor(), # normalize, # ]), # binary=(args.classes == 2)), my_val, batch_size=args.batch_size, shuffle=False, num_workers=args.workers,pin_memory=True) files = listdir(args.resume) files.remove('model_best.pth.tar') files = sorted(files, key= lambda x:int(x.split('.')[0].split('_')[1])) for i, file in enumerate(files): single_model = dla_up.__dict__.get(args.arch)( args.classes, down_ratio=args.down) model = torch.nn.DataParallel(single_model).cuda() checkpoint = torch.load(join(args.resume, file)) print(checkpoint['epoch']) model.load_state_dict(checkpoint['state_dict']) criterion = nn.NLLLoss2d(ignore_index=-1) score = validate(val_loader, model, criterion, eval_score=mIOU, print_freq=10) writer.add_scalar('IoU/epoch', score, i + 1) print(score) writer.close()
def loss_calc_seg(out, label,gpu0,seg_weights): """This function returns cross entropy loss for semantic segmentation """ # out shape batch_size x channels x h x w -> batch_size x channels x h x w # label shape h x w x 1 x batch_size -> batch_size x 1 x h x w label = label[:,:,0,:].transpose(2,0,1) label = torch.from_numpy(label).long() label = Variable(label).cuda(gpu0) m = nn.LogSoftmax() if args['--segnetLoss']: criterion = nn.NLLLoss2d(torch.from_numpy(seg_weights).float().cuda(gpu0)) else: criterion = nn.NLLLoss2d() out = m(out) return criterion(out,label)
def test(self, test_data): train_loss = 0 train_acc = 0 train_acc_cls = 0 train_mean_iu = 0 train_fwavacc = 0 self.net.eval() criterion = nn.NLLLoss2d() for data in test_data: im = data[0].cuda() label = data[1].cuda() out = self.net(im) out = F.log_softmax(out, dim=1) loss = criterion(out, label) label_pred = out.max(dim=1)[1].data.cpu().numpy() label_true = label.data.cpu().numpy() for lbt, lbp in zip(label_true, label_pred): acc, acc_cls, mean_iu, fwavacc = self.label_accuracy_score( lbt, lbp, self.num_classes) train_acc += acc train_acc_cls += acc_cls train_mean_iu += mean_iu train_fwavacc += fwavacc print("test:", "loss:", loss.data.item, "train_acc_cls:", train_acc_cls, "train_mean_iu:", train_mean_iu, "train_fwavacc:", train_fwavacc)
def my_val(): args = parse_args() val_dir_img = '/shared/xudongliu/data/argoverse-tracking/argo_track/val/image_02/' val_dir_mask = '/shared/xudongliu/data/argoverse-tracking/argo_track/val/npy_mask/' my_val = BasicDataset(val_dir_img, val_dir_mask, None, is_train=False) val_loader = torch.utils.data.DataLoader( # SegList(data_dir, 'val', transforms.Compose([ # transforms.RandomCrop(crop_size), # # transforms.RandomHorizontalFlip(), # transforms.ToTensor(), # normalize, # ]), # binary=(args.classes == 2)), my_val, batch_size=args.batch_size, shuffle=False, num_workers=args.workers,pin_memory=True) single_model = dla_up.__dict__.get(args.arch)( args.classes, down_ratio=args.down) checkpoint = torch.load(args.resume) # print(checkpoint['epoch']) # model.load_state_dict(checkpoint['state_dict']) # single_model.load_state_dict(checkpoint) model = torch.nn.DataParallel(single_model).cuda() model.load_state_dict(checkpoint['state_dict']) #TODO criterion = nn.NLLLoss2d(ignore_index=-1) score = validate(val_loader, model, criterion, eval_score=mIOU, print_freq=10, cityscape=True) print(score)
def __init__(self, weight=None): ''' :param weight: 1D weight vector to deal with the class-imbalance ''' super().__init__() self.loss = nn.NLLLoss2d(weight)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--EXP_NAME', type=str, default='tiramisu12') parser.add_argument('--EXP_DIR', type=str, default='/disk3/yangle/code/benchmark/tiramisu/') parser.add_argument('--CAMVID_PATH', type=str, default='data/CamVid/') parser.add_argument('--LEARNING_RATE', type=float, default=1e-4) parser.add_argument('--WEIGHT_DECAY', type=float, default=0.0001) args = parser.parse_args() normalize = transforms.Normalize(mean=camvid.mean, std=camvid.std) test_dset = camvid.CamVid(args.CAMVID_PATH, 'test', joint_transform=None, transform=transforms.Compose( [transforms.ToTensor(), normalize])) test_loader = torch.utils.data.DataLoader(test_dset, batch_size=2, shuffle=False) model = tiramisu.FCDenseNet103(n_classes=12) model = model.cuda() optimizer = optim.RMSprop(model.parameters(), lr=args.LEARNING_RATE, weight_decay=args.WEIGHT_DECAY) experiment = experiment.Experiment(args.EXP_NAME, args.EXP_DIR) experiment.resume(model, optimizer) criterion = nn.NLLLoss2d(weight=camvid.class_weight.cuda()).cuda() test_loss, test_error = utils.test(model, test_loader, criterion) print(test_loss) print(test_error)
def __init__(self, frequency=None, size_average=True, ignore_index=255): super(CrossEntropyLoss2d, self).__init__() if type(frequency) is list: frequency = torch.FloatTensor(frequency) weight = 1.0 / frequency self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index) self.name = 'ce_loss'
def __init__(self, weight=[1, 1, 2], size_average=True): super(CrossEntropyLoss2d, self).__init__() myweight = np.array(weight) myweight = torch.from_numpy(myweight).float() myweight = Variable(myweight).cuda() self.nll_loss = nn.NLLLoss2d(myweight, size_average)
def __init__(self, visi_weight, weight=None, size_average=True, ignore_index=-100 ): super(LArFlowLoss, self).__init__() self.ignore_index = ignore_index self.reduce = False self.visi_weight = visi_weight self.crossentropy = nn.NLLLoss2d( reduce=False ) self.smoothl1 = nn.SmoothL1Loss( reduce=False )
def get_objective(objective): if isinstance(objective, str): objective = objective.lower() if objective in ['l1', 'l1loss']: return nn.L1Loss() elif objective in ['nll', 'nllloss']: return nn.NLLLoss() elif objective in ['nll2d', 'nllloss2d']: return nn.NLLLoss2d() elif objective in ['poissonnll', 'poissonnllloss']: return nn.PoissonNLLLoss() elif objective in ['kldiv', 'kldivloss']: return nn.KLDivLoss() elif objective in ['mse', 'mseloss']: return nn.MSELoss() elif objective in ['bce', 'bceloss']: return nn.BCELoss() elif objective in ['smoothl1', 'smoothl1loss']: return nn.SmoothL1Loss() elif objective in ['crossentropy', 'cross_entropy']: return nn.CrossEntropyLoss() elif objective in ['ctc', 'ctcloss']: return nn.CTCLoss() else: raise ValueError('unknown argument!') elif isinstance(objective, _Loss): return objective else: raise ValueError('unknown argument {}'.format(objective))
def __init__(self, args): self.epoch = args.epoch self.pair_list = args.pair_list self.epoch_len = args.epoch_len self.batch_size = args.batch_size self.gpu = args.gpu self.loc_update_stride = args.loc_update_stride self.snapshot_stride = args.snapshot_stride self.input_scale = args.input_scale self.nolabel = args.nolabel self.start_epoch_idx = args.start_epoch_idx self.start_iter_idx = args.start_iter_idx self.snapshot_prefix_loc = args.snapshot_prefix_loc self.data_path = args.data_path self.loc = dmac_vgg.DMAC_VGG(self.nolabel, self.gpu, self.input_scale) if args.loc_pretrained: loc_saved_state_dict = torch.load(args.loc_pretrain_model) self.loc.load_state_dict(loc_saved_state_dict) self.loc.cuda(self.gpu) self.loc_optimizer = optim.Adadelta(self.loc.parameters()) self.logsoftmax = nn.LogSoftmax(dim=1).cuda(self.gpu) self.ce_criterion = nn.NLLLoss2d().cuda(self.gpu) print('---------- Networks architecture -------------') print_network(self.loc) print('-----------------------------------------------')
def main(args): # Network Builders builder = ModelBuilder() net_encoder = builder.build_encoder(arch=args.arch_encoder, fc_dim=args.fc_dim, weights=args.weights_encoder) net_decoder = builder.build_decoder(arch=args.arch_decoder, fc_dim=args.fc_dim, segSize=args.segSize, weights=args.weights_decoder, use_softmax=True) crit = nn.NLLLoss2d(ignore_index=-1) # Dataset and Loader dataset_val = Dataset(args.list_val, args, max_sample=args.num_val, is_train=0) loader_val = torch.utils.data.DataLoader(dataset_val, batch_size=args.batch_size, shuffle=False, num_workers=2, drop_last=True) nets = (net_encoder, net_decoder, crit) for net in nets: net.cuda() # Main loop evaluate(nets, loader_val, args) print('Evaluation Done!')
def train_base(generator,optimG,trainloader,valoader,args): best_miou = -1 best_eiou = -1 for epoch in range(args.start_epoch,args.max_epoch+1): generator.train() for batch_id, (img, mask, _, _) in enumerate(trainloader): if args.nogpu: img,mask = Variable(img),Variable(mask) else: img,mask = Variable(img.cuda()),Variable(mask.cuda()) itr = len(trainloader)*(epoch-1) + batch_id cprob = generator(img) cprob = nn.LogSoftmax()(cprob) Lseg = nn.NLLLoss2d()(cprob,mask) # Lseg = nn.BCELoss()(cprob,mask) optimG = poly_lr_scheduler(optimG, args.g_lr, itr) optimG.zero_grad() Lseg.backward() optimG.step() # print("[{}][{}]Loss: {:0.4f}".format(epoch,itr,Lseg.data[0])) print("[{}][{}]Loss: {:0.4f}".format(epoch,itr,Lseg.data)) best_miou, best_eiou = snapshote(generator,valoader,epoch,best_miou,best_eiou,args.snapshot_dir,args.prefix)
def train(): model = U_Net(n_channels=num_channels, n_classes=num_classes).to(device) model_graph = SummaryWriter(comment="UNet") input_c = torch.rand(1, 3, 256, 256) # model_graph.add_graph(model, (input_c.to(device),)) model.train() # criterion = nn.BCELoss() criterion = nn.NLLLoss2d() # criterion=MulticlassDiceLoss() optimizer = optim.Adam(model.parameters(), lr=1e-3) dataset = My_Dataset(root, num_classes, size, transform=img_transforms, mask_transform=mask_transforms) data_loaders = DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=4) train_model(model, criterion, optimizer, data_loaders, model_graph=model_graph, num_epochs=num_epochs) model_graph.close()
def main(args): # Network Builders builder = ModelBuilder() net_encoder = builder.build_encoder(arch=args.arch_encoder, fc_dim=args.fc_dim, weights=args.weights_encoder) net_decoder = builder.build_decoder(arch=args.arch_decoder, fc_dim=args.fc_dim, segSize=args.segSize, num_class=args.num_class, weights=args.weights_decoder) crit = nn.NLLLoss2d(ignore_index=0) # Dataset and Loader dataset_train = ConceptDataset(args) loader_train = torch.utils.data.DataLoader( dataset_train, batch_size=args.batch_size, shuffle=True, num_workers=int(args.workers), drop_last=True) args.epoch_iters = int(len(dataset_train) / args.batch_size) print('1 Epoch = {} iters'.format(args.epoch_iters)) # load nets into gpu if args.num_gpus > 1: net_encoder = nn.DataParallel(net_encoder, device_ids=range(args.num_gpus)) net_decoder = nn.DataParallel(net_decoder, device_ids=range(args.num_gpus)) nets = (net_encoder, net_decoder, crit) if args.num_gpus > 0: for net in nets: net.cuda() # Set up optimizers optimizers = create_optimizers(nets, args) # Main loop history = {split: {'epoch': [], 'err': [], 'acc': []} for split in ('train', 'val')} # initial eval # evaluate(nets, loader_val, history, 0, args) for epoch in range(1, args.num_epoch + 1): train(nets, loader_train, optimizers, history, epoch, args) # Evaluation and visualization # if epoch % args.eval_epoch == 0: # evaluate(nets, loader_val, history, epoch, args) # checkpointing checkpoint(nets, history, args) # adjust learning rate adjust_learning_rate(optimizers, epoch, args) print('Training Done!')
def __init__(self, nonvisi_weight=1.0e-2): super(LArFlowVisibilityLoss, self).__init__() # visibility parameters self.classweight_t = torch.ones(2, dtype=torch.float) self.classweight_t[0] = nonvisi_weight self.softmax = nn.LogSoftmax(dim=1) self.crossentropy = nn.NLLLoss2d(reduce=False)
def __init__(self, dice_weight=0.0, class_weights=None): if class_weights is not None: nll_weight = utils.cuda( torch.from_numpy(class_weights.astype(np.float32))) else: nll_weight = None self.nll_loss = nn.NLLLoss2d(weight=nll_weight) self.dice_weight = dice_weight
def __init__(self, gamma=2, weight=None, size_average=True, ignore_index=255): super(FocalLoss2d, self).__init__() self.gamma = gamma self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index)
def __init__(self, classes, weight=None, size_average=True, ignore_index=255, norm=False, upper_bound=1.0): super(ImageBasedCrossEntropyLoss2d, self).__init__() self.num_classes = classes self.nll_loss = nn.NLLLoss2d(weight, size_average, ignore_index) self.norm = norm self.upper_bound = upper_bound self.batch_weights = cfg.BATCH_WEIGHTING
def __init__(self, generator, tgt_vocab, normalization="sents", label_smoothing=0.0, use_kl_annealing=False, use_kl_freebits=False, kl_freebits_margin=0.0, kl_annealing_current=0.0, kl_annealing_increment=0.0001, kl_annealing_warmup_steps=1000, image_loss_type='logprob', use_local_image_features=False, two_step_image_prediction=False ): """ If two_step_image_prediction is True, always predict image pixels (using a categorical) and if image_loss_type is 'logprob' and use_local_image_features is True, predict local image features using logprob. if image_loss_type is 'cosine' and use_local_image_features is True, predict local image features using cosine. if image_loss_type is 'logprob' and use_local_image_features is False, predict global image features using logprob. if image_loss_type is 'cosine' and use_local_image_features is False, predict global image features using cosine. If two_step_image_prediction is False, if image_loss_type is 'logprob' and use_local_image_features is True, predict local image features using logprob. if image_loss_type is 'cosine' and use_local_image_features is True, predict local image features using cosine. if image_loss_type is 'logprob' and use_local_image_features is False, predict global image features using logprob. if image_loss_type is 'cosine' and use_local_image_features is False, predict global image features using cosine. if image_loss_type is 'categorical', ignore use_local_image_features and predict local image pixels using categorical. """ self.multimodal_model_type = 'vi-model1' super(NMTVIModel1LossCompute, self).__init__(generator, tgt_vocab, normalization, label_smoothing) # kl annealing parameters self.n_model_updates = 0 self.use_kl_annealing = use_kl_annealing if use_kl_annealing: self.kl_annealing_current = kl_annealing_current self.kl_annealing_increment = kl_annealing_increment self.kl_annealing_warmup_steps = kl_annealing_warmup_steps else: self.kl_annealing_current = 1.0 self.kl_annealing_increment = 0.0 self.kl_annealing_warmup_steps = 0 self.use_kl_freebits = use_kl_freebits if use_kl_freebits: self.kl_freebits_margin = kl_freebits_margin else: self.kl_freebits_margin = 0.0 self.image_loss_type = image_loss_type self.use_local_image_features = use_local_image_features self.two_step_image_prediction = two_step_image_prediction self._statistics = onmt.VIStatistics if image_loss_type == 'categorical': self.image_loss_criterion = nn.NLLLoss2d()
def __init__(self, dice_weight=1.0, bg_weight=1.0): if bg_weight != 1.0: nll_weight = torch.ones(utils.N_CLASSES + 1) nll_weight[utils.N_CLASSES] = bg_weight nll_weight = nll_weight.cuda() else: nll_weight = None self.nll_loss = nn.NLLLoss2d(weight=nll_weight) self.dice_weight = dice_weight
def __init__(self, jaccard_weight=0, class_weights=None, num_classes=1): if class_weights is not None: nll_weight = cuda( torch.from_numpy(class_weights.astype(np.float32))) else: nll_weight = None self.nll_loss = nn.NLLLoss2d(weight=nll_weight) self.jaccard_weight = jaccard_weight self.num_classes = num_classes
def __init__(self, classes, weight=None, size_average=False, ignore_index=cfg.DATASET.IGNORE_LABEL, norm=False, upper_bound=1.0): super(EdgeWeightedCrossEntropyLoss2d, self).__init__() logx.msg("Using Per Image based weighted loss") self.num_classes = classes self.nll_loss = nn.NLLLoss2d(weight, size_average,ignore_index) self.norm = norm self.upper_bound = upper_bound self.batch_weights = cfg.BATCH_WEIGHTING