def train_model(model, criterion, optimizer_ft, scheduler, epoch):
    scheduler.step()
    lambda1 = sigmoid_rampup(epoch, args.LabelWt)
    train_loss = AverageMeter()
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.train()
    correct = 0
    total = 0
    end = time.time()
    for (batch_idx, (inputs, targets, weights)) in enumerate(dataloaders_train):
        if use_gpu:
            inputs = Variable(inputs.cuda())
            targets = Variable(targets.cuda())
            weights = Variable(weights.cuda())
        data_time.update((time.time() - end))
        optimizer_ft.zero_grad()
        outputs = model(inputs)
        if args.stage2:
            loss = criterion(outputs, targets, weights)
        else:
            loss = criterion(outputs, targets, lambda1)
        loss.backward()
        optimizer_ft.step()
        train_loss.update(loss.item(), inputs.size(0))
        batch_time.update((time.time() - end))
        end = time.time()
        (_, predicted) = outputs.max(1)
        correct += predicted.eq(targets).sum().item()
        total += inputs.size(0)
        if ((batch_idx % 10) == 0):
            print('Epoch: [{}][{}/{}] Time: {batch_time.val:.3f} ({batch_time.avg:.3f}) Data: {data_time.val:.3f} ({data_time.avg:.3f}) Loss: {train_loss.val:.4f} ({train_loss.avg:.4f}) Accu: {:.2f}'.format(epoch, batch_idx, len(dataloaders_train), ((100.0 * correct) / total), batch_time=batch_time, data_time=data_time, train_loss=train_loss))
    writer.add_scalar('training acc (train)', ((100.0 * correct) / total), epoch)
    writer.add_scalar('loss', train_loss.avg, epoch)
def train_model(model, criterion, optimizer_ft, scheduler, epoch):
    scheduler.step()
    lambda1 = sigmoid_rampup(epoch, args.LabelWt)
    train_loss = AverageMeter()
    data_time = AverageMeter()
    batch_time = AverageMeter()
    model.train()
    correct = 0
    total = 0
    end = time.time()
    for (batch_idx, (inputs, targets, weights)) in enumerate(dataloaders_train):
        if use_gpu:
            inputs = Variable(inputs.cuda())
            targets = Variable(targets.cuda())
            weights = Variable(weights.cuda())
        data_time.update((time.time() - end))
        optimizer_ft.zero_grad()
        outputs = model(inputs)
        if args.stage2:
            loss = criterion(outputs, targets, weights)
        else:
            loss = criterion(outputs, targets, lambda1)
        loss.backward()
        optimizer_ft.step()
        train_loss.update(loss.item(), inputs.size(0))
        batch_time.update((time.time() - end))
        end = time.time()
        (_, predicted) = outputs.max(1)
        correct += predicted.eq(targets).sum().item()
        total += inputs.size(0)
        if ((batch_idx % 10) == 0):
            print('Epoch: [{}][{}/{}] Time: {batch_time.val:.3f} ({batch_time.avg:.3f}) Data: {data_time.val:.3f} ({data_time.avg:.3f}) Loss: {train_loss.val:.4f} ({train_loss.avg:.4f}) Accu: {:.2f}'.format(epoch, batch_idx, len(dataloaders_train), ((100.0 * correct) / total), batch_time=batch_time, data_time=data_time, train_loss=train_loss))
    writer.add_scalar('training acc (train)', ((100.0 * correct) / total), epoch)
    writer.add_scalar('loss', train_loss.avg, epoch)