Example #1
0
def train(epoch):
    global acc, acc_train
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    if epoch > lr_dec_start and lr_dec_start >= 0:
        frac = (epoch - lr_dec_start) // lr_dec_every
        dec_frac = lr_dec_rate ** frac #0.9 ** e/5
        curr_lr = lr * dec_frac         #0.9 ** e/5
        utils.set_lr(optimizer, curr_lr)
    else:
        curr_lr = lr
    # print(1)
    for batch_idx, (inputs, targets) in enumerate(tr_loader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        train_loss += float(loss.item())
        _, pred = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += pred.eq(targets.data).cpu().sum()
        # print(2)
        if epoch == total_epoch - 1:
            class_mat[0] = calcl_mat(pred.cpu().numpy(), targets.data.cpu().numpy(), class_mat[0])
    print("training... batch:{}   loss={}    acc={}%({}/{})".format(batch_idx, train_loss/(batch_idx+1), 100.*correct/total,correct,total))
    acc_train = 100.*correct / total
    hists[0].append(train_loss/(batch_idx+1))
    hists[1].append(acc_train)
Example #2
0
def train(epoch):
    model.train()
    import random
    if epoch > decay_epoch_start:
        frac = (epoch-decay_epoch_start) // decay_every
        decay_frac = (decay_rate)**frac
        curlr = lr*decay_frac
        for group in optimizer.param_groups:
            group['lr'] = curlr
    for batchidx, (data, target) in enumerate(dataloader):
        correct = 0
        nl = data.shape[0]
        noise = torch.rand(nl, 3, 44, 44)
        data = torch.add(data, 0.05 * noise)
        if use_cuda:
            data,target = data.cuda(), target.cuda()
        data,target = Variable(data),Variable(target)
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, target)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        pred = output.data.max(1, keepdim=True)[1]  # get the index of the max log-probability
        correct += pred.eq(target.data.view_as(pred)).cpu().sum()
        if batchidx % 10 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tCorrec: {: d}'.format(
                epoch, batchidx * len(data), len(dataloader.dataset),
                       100. * batchidx / len(dataloader), loss,correct))
            lf.write('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tCorrec: {: d}\n'.format(
                epoch, batchidx * len(data), len(dataloader.dataset),
                       100. * batchidx / len(dataloader), loss,correct))
Example #3
0
def train(train_loader, model_total, optimizer, epoch, dataset_name, video):
    model_total.train()

    for i, pack in enumerate(train_loader, start=0):

        optimizer.zero_grad()
        images, gts = pack

        images = Variable(images)
        gts = Variable(gts)

        images = images.cuda()
        gts = gts.cuda()
        att_s, d = model_total(images)

        loss1 = CE(att_s, gts)
        loss2 = CE(d, gts)

        loss = loss1 + loss2
        loss.backward()

        clip_gradient(optimizer, opt.clip)
        optimizer.step()

        print(
            '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss1: {:.4f} Loss2: {:0.4f} '
            .format(datetime.now(), epoch, opt.epoch, i, len(train_loader),
                    loss1.data, loss2.data))

        save_path = r'../%s/%s/' % (dataset_name, video)

    if not os.path.exists(save_path):
        os.makedirs(save_path)

    torch.save(model_total.state_dict(), save_path + 'vgg_%d.pth' % epoch)
Example #4
0
def train(train_loader, model, optimizer, epoch):
    model.train()
    for i, pack in enumerate(train_loader, start=1):
        optimizer.zero_grad()
        images, gts = pack
        images = Variable(images)
        gts = Variable(gts)
        images = images.cuda()
        gts = gts.cuda()

        atts, dets = model(images)
        loss1 = CE(atts, gts)
        loss2 = CE(dets, gts)
        loss = loss1 + loss2
        loss.backward()

        clip_gradient(optimizer, opt.clip)
        optimizer.step()

        if i % 400 == 0 or i == total_step:
            print(
                '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss1: {:.4f} Loss2: {:0.4f}'
                .format(datetime.now(), epoch, opt.epoch, i, total_step,
                        loss1.data, loss2.data))

    if opt.is_ResNet:
        save_path = 'models/CPD_Resnet/'
    else:
        save_path = 'models/CPD_VGG/'

    if not os.path.exists(save_path):
        os.makedirs(save_path)
    if (epoch + 1) % 5 == 0:
        torch.save(model.state_dict(),
                   save_path + opt.trainset + '_w.pth' + '.%d' % epoch)
def train(train_loader, model, optimizer, epoch, logger):
    model.train()  # train mode (dropout and batchnorm is used)

    losses = AverageMeter()

    # Batches
    for i, (data) in enumerate(train_loader):
        # Move to GPU, if available
        padded_input, padded_target, input_lengths = data
        padded_input = padded_input.to(device)
        padded_target = padded_target.to(device)
        input_lengths = input_lengths.to(device)

        # Forward prop.
        loss = model(padded_input, input_lengths, padded_target)

        # Back prop.
        optimizer.zero_grad()
        loss.backward()

        # Clip gradients
        clip_gradient(optimizer, grad_clip)

        # Update weights
        optimizer.step()

        # Keep track of metrics
        losses.update(loss.item())

        # Print status
        if i % print_freq == 0:
            logger.info('Epoch: [{0}][{1}/{2}]\t'
                        'Loss {loss.val:.4f} ({loss.avg:.4f})'.format(epoch, i, len(train_loader), loss=losses))

    return losses.avg
Example #6
0
def train(epoch):
    # adjust_lr(optimizer, epoch, args.lr)
    model.train()
    for batch_idx, (prev_canvas, inst, next_obj, final_canvas, ref_obj, raw_data) in enumerate(train_loader):
        # for ix in range(args.batch_size):
        #     # ix = random.randint(0, args.batch_size-1)
        #     inst_str = ' '.join(map(train_loader.dataset.ix_to_word.get, list(inst[ix])))
        #     next_obj_color = train_loader.dataset.num2color[next_obj[ix][0]]
        #     next_obj_shape = train_loader.dataset.num2shape[next_obj[ix][1]]
        #     print(inst_str)
        #     print("target obj: {} {} {} {}".format(next_obj_color, next_obj_shape, next_obj[ix][2], next_obj[ix][3]))
        #     if ref_obj[ix].sum() > 0:
        #         ref_obj_color = train_loader.dataset.num2color[ref_obj[ix][0]]
        #         ref_obj_shape = train_loader.dataset.num2shape[ref_obj[ix][1]]
        #         print("ref obj: {} {} {} {}".format(ref_obj_color, ref_obj_shape, ref_obj[ix][2], ref_obj[ix][3]))
        #     print("\n")
        prev_canvas = Variable(prev_canvas.cuda())
        inst = Variable(inst.cuda())
        next_obj = Variable(next_obj.cuda())
        ref_obj = Variable(ref_obj.cuda())
        optimizer.zero_grad()
        output = model(inst, prev_canvas, ref_obj, next_obj)
        loss, rewards = loss_fn(output, next_obj, ref_obj)
        policy_loss = (-model.saved_log_probs * Variable(rewards)).sum()
        (loss + policy_loss).backward()
        clip_gradient(optimizer, 0.1)
        optimizer.step()
        color_accuracy, shape_accuracy, row_accuracy, col_accuray = compute_accuracy(output, next_obj, ref_obj)
        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f} ({:.3f} {:.3f} {} {})'.format(
                epoch, batch_idx * args.batch_size, len(train_loader.dataset),
                       100. * batch_idx / len(train_loader), loss.data[0], color_accuracy, shape_accuracy, row_accuracy, col_accuray))
Example #7
0
def train(epoch):
    t = time.time()
    model.train()
    optimizer.zero_grad()
    output = model(features, adj)
    output = F.log_softmax(output, dim=1)
    loss_train = F.nll_loss(output[idx_train], labels[idx_train])
    acc_train = accuracy(output[idx_train], labels[idx_train])
    loss_train.backward()
    clip_gradient(model, clip_norm=0.5)
    optimizer.step()

    if not args.fastmode:
        # Evaluate validation set performance separately,
        # deactivates dropout during validation run.
        model.eval()
        output = model(features, adj)
        output = F.log_softmax(output, dim=1)

    loss_val = F.nll_loss(output[idx_val], labels[idx_val])
    acc_val = accuracy(output[idx_val], labels[idx_val])
    loss_test = F.nll_loss(output[idx_test], labels[idx_test])
    acc_test = accuracy(output[idx_test], labels[idx_test])

    print('Epoch: {:04d}'.format(epoch + 1), 'loss_train: {:.4f}'.format(
        loss_train.item()), 'acc_train: {:.4f}'.format(acc_train.item()),
          'loss_val: {:.4f}'.format(loss_val.item()),
          'acc_val: {:.4f}'.format(acc_val.item()),
          'loss_test: {:.4f}'.format(loss_test.item()),
          'acc_test: {:.4f}'.format(acc_test.item()),
          'time: {:.4f}s'.format(time.time() - t))
Example #8
0
def train(train_loader, model, optimizer, epoch, save_path):

    global step
    model.train()
    loss_all = 0
    epoch_step = 0
    # try:
    for i, (images, gts, depths) in enumerate(train_loader, start=1):
        optimizer.zero_grad()

        images = images.cuda()
        gts = gts.cuda()
        depths = depths.cuda()

        s1, s2 = model(images, depths)
        loss1 = CE(s1, gts)
        loss2 = CE(s2, gts)
        loss = loss1 + loss2
        loss.backward()

        clip_gradient(optimizer, opt.clip)
        optimizer.step()
        step += 1
        epoch_step += 1
        loss_all += loss.data
        if i % 100 == 0 or i == total_step or i == 1:
            print(
                '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss1: {:.4f} Loss2: {:0.4f}'
                .format(datetime.now(), epoch, opt.epoch, i, total_step,
                        loss1.data, loss2.data))
            logging.info(
                '#TRAIN#:Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss1: {:.4f} Loss2: {:0.4f}'
                .format(epoch, opt.epoch, i, total_step, loss1.data,
                        loss2.data))
            writer.add_scalar('Loss', loss.data, global_step=step)
            grid_image = make_grid(images[0].clone().cpu().data,
                                   1,
                                   normalize=True)
            writer.add_image('RGB', grid_image, step)
            grid_image = make_grid(gts[0].clone().cpu().data,
                                   1,
                                   normalize=True)
            writer.add_image('Ground_truth', grid_image, step)
            res = s1[0].clone()
            res = res.sigmoid().data.cpu().numpy().squeeze()
            res = (res - res.min()) / (res.max() - res.min() + 1e-8)
            writer.add_image('s1', torch.tensor(res), step, dataformats='HW')
            res = s2[0].clone()
            res = res.sigmoid().data.cpu().numpy().squeeze()
            res = (res - res.min()) / (res.max() - res.min() + 1e-8)
            writer.add_image('s2', torch.tensor(res), step, dataformats='HW')

    loss_all /= epoch_step
    logging.info('#TRAIN#:Epoch [{:03d}/{:03d}], Loss_AVG: {:.4f}'.format(
        epoch, opt.epoch, loss_all))
    writer.add_scalar('Loss-epoch', loss_all, global_step=epoch)
    if (epoch) % 5 == 0:
        torch.save(model.state_dict(),
                   save_path + 'BBSNet_epoch_{}.pth'.format(epoch))
def train(train_loader, model, metric_fc, criterion, optimizer, epoch):
    model.train()  # train mode (dropout and batchnorm is used)
    metric_fc.train()

    losses = AverageMeter()
    top1_accs = AverageMeter()

    # Batches
    tic = time.time()
    for i, (img, label) in enumerate(train_loader):

        # Move to GPU, if available
        img = img.to(device)
        label = label.to(device)  # [N, 1]

        # Forward prop.
        feature = model(img)  # embedding => [N, 512]
        output = metric_fc(feature, label)  # class_id_out => [N, 10575]

        # Calculate loss
        loss = criterion(output, label)

        # Back prop.
        optimizer.zero_grad()
        loss.backward()

        # Clip gradients
        clip_gradient(optimizer, grad_clip)

        # Update weights
        optimizer.step()

        # Keep track of metrics
        losses.update(loss.item())
        top1_accuracy = accuracy(output, label, 1)
        top1_accs.update(top1_accuracy)

        # Print status
        if i % print_freq == 0:

            toc = time.time()
            time_elapsed = toc - tic

            logger.info(
                'Epoch: [{0}][{1}/{2}]\t'
                'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                'Top1 Accuracy {top1_accs.val:.3f} ({top1_accs.avg:.3f})\t'
                'Time Elapsed: {time_elapsed:.3f}s'.format(
                    epoch,
                    i,
                    len(train_loader),
                    loss=losses,
                    top1_accs=top1_accs,
                    time_elapsed=time_elapsed))

            tic = time.time()

    return losses.avg, top1_accs.avg
Example #10
0
def train(args, encoder, decoder, loader, decoder_optimizer, encoder_optimizer,
          device, criterion):
    decoder.train()  # train mode (dropout and batchnorm is used)
    encoder.train()
    losses = AverageMeter()  # loss (per word decoded)
    top3accs = AverageMeter()  # top accuracy
    i = 0
    for data in tqdm(loader):
        if i % args.lr_update_freq == 0 and i > 0:
            adjust_learning_rate(decoder_optimizer, args.decay_rate)
            if args.fine_tune_encoder:
                adjust_learning_rate(encoder_optimizer, args.decay_rate)
        imgs = data[0]
        caps = data[1]
        caplens = data[2]
        # Forward pass
        imgs = encoder(imgs.to(device)).to(device)
        caps = caps.to(imgs.device)
        caplens = caplens.to(imgs.device)
        scores, caps_sorted, decode_lengths, alphas, sort_ind = decoder(
            imgs, caps, caplens)
        targets = caps_sorted[:, 1:]  #remove <start> and <end> tokens
        scores = pack_padded_sequence(scores,
                                      decode_lengths, batch_first=True).to(
                                          device)  #remove padding tokens
        targets = pack_padded_sequence(targets,
                                       decode_lengths,
                                       batch_first=True).to(device)
        # Calculate loss
        loss = criterion(scores.data, targets.data).to(imgs.device)
        # Add doubly stochastic attention regularization
        loss += args.alphac * ((1. - alphas.sum(dim=1).to(device))**2).mean()
        # Back prop.
        decoder_optimizer.zero_grad()
        if encoder_optimizer is not None:
            encoder_optimizer.zero_grad()
        loss.backward()
        if args.gradient_clip is not None:
            clip_gradient(decoder_optimizer, args.gradient_clip)
            if encoder_optimizer is not None:
                clip_gradient(encoder_optimizer, args.gradient_clip)
        # Update weights
        decoder_optimizer.step()
        if encoder_optimizer is not None:
            encoder_optimizer.step()
        # Keep track of metrics
        top3 = accuracy(scores.data, targets.data, 3)
        losses.update(loss.item(), sum(decode_lengths))
        top3accs.update(top3, sum(decode_lengths))
        # Print status
        if i % args.print_freq == 0:
            print('Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                  'Top-3 Accuracy {top3.val:.3f} ({top3.avg:.3f})'.format(
                      loss=losses, top3=top3accs))
        if i % args.checkpoint_freq == 0 and args.checkpoint_freq > 0:
            save_checkpoint(args.model_path, i, encoder, decoder,
                            encoder_optimizer, decoder_optimizer, 0, False)
        i += 1
Example #11
0
def train(train_loader, model, optimizer, epoch, logger):
    model.train()  # train mode (dropout and batchnorm is used)

    losses = AverageMeter()
    scaler = GradScaler()
    # loss_function = alpha_prediction_loss
    criterion = LossFunction(args.stage)()

    # Batches
    for i, (img, alpha_label, trimap_label, _) in enumerate(train_loader):
        # Move to GPU, if available
        img = img.type(torch.FloatTensor).to(device)  # [N, 4, 320, 320]
        alpha_label = alpha_label.type(torch.FloatTensor).to(
            device)  # [N, 320, 320]
        alpha_label = alpha_label.unsqueeze(1)
        trimap_label = trimap_label.to(device)
        # alpha_label = alpha_label.reshape((-1, 2, im_size * im_size))  # [N, 320*320]
        with autocast():
            # Forward prop.
            trimap_out, alpha_out = model(img)  # [N, 3, 320, 320]
            # alpha_out = alpha_out.reshape((-1, 1, im_size * im_size))  # [N, 320*320]

            # Calculate loss
            if args.stage == 'train_trimap':
                loss = criterion(trimap_out, trimap_label)
            elif args.stage == 'train_alpha':
                loss = criterion(alpha_out, alpha_label, trimap_out,
                                 trimap_label)

        # Back prop.
        optimizer.zero_grad()
        scaler.scale(loss).backward()
        # loss.backward()

        # Clip gradients
        scaler.unscale_(optimizer)
        clip_gradient(optimizer, grad_clip)

        # Update weights
        scaler.step(optimizer)
        scaler.update()
        # optimizer.step()

        # Keep track of metrics
        losses.update(loss.item())

        # Print status

        if i % print_freq == 0:
            status = 'Epoch: [{0}][{1}/{2}]\t' \
                     'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
                         epoch, i, len(train_loader), loss=losses)
            logger.info(status)
    writer.add_scalar('Train_Loss', losses.avg, epoch)
    writer.add_scalar('Learning_Rate', get_learning_rate(optimizer), epoch)
    # save_checkpoint(epoch, 0, model, optimizer, losses.avg, False, args.checkpoint_dir)
    return losses.avg
Example #12
0
def train(train_loader, model, optimizer, epoch):
    model.train()
    for i, pack in enumerate(train_loader, start=1):
        optimizer.zero_grad()
        images, gts, masks, grays, edges = pack
        images = Variable(images)
        gts = Variable(gts)
        masks = Variable(masks)
        grays = Variable(grays)
        edges = Variable(edges)
        images = images.cuda()
        gts = gts.cuda()
        masks = masks.cuda()
        grays = grays.cuda()
        edges = edges.cuda()

        img_size = images.size(2) * images.size(3) * images.size(0)
        ratio = img_size / torch.sum(masks)

        sal1, edge_map, sal2 = model(images)

        sal1_prob = torch.sigmoid(sal1)
        sal1_prob = sal1_prob * masks
        sal2_prob = torch.sigmoid(sal2)
        sal2_prob = sal2_prob * masks

        smoothLoss_cur1 = opt.sm_loss_weight * smooth_loss(
            torch.sigmoid(sal1), grays)
        sal_loss1 = ratio * CE(sal1_prob, gts * masks) + smoothLoss_cur1
        smoothLoss_cur2 = opt.sm_loss_weight * smooth_loss(
            torch.sigmoid(sal2), grays)
        sal_loss2 = ratio * CE(sal2_prob, gts * masks) + smoothLoss_cur2
        edge_loss = opt.edge_loss_weight * CE(torch.sigmoid(edge_map), edges)
        bce = sal_loss1 + edge_loss + sal_loss2
        visualize_prediction1(torch.sigmoid(sal1))
        visualize_edge(torch.sigmoid(edge_map))
        visualize_prediction2(torch.sigmoid(sal2))

        loss = bce
        loss.backward()

        clip_gradient(optimizer, opt.clip)
        optimizer.step()

        if i % 10 == 0 or i == total_step:
            print(
                '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], sal1_loss: {:0.4f}, edge_loss: {:0.4f}, sal2_loss: {:0.4f}'
                .format(datetime.now(), epoch, opt.epoch, i, total_step,
                        sal_loss1.data, edge_loss.data, sal_loss2.data))

    save_path = 'models/'

    if not os.path.exists(save_path):
        os.makedirs(save_path)
    if epoch % 10 == 0:
        torch.save(model.state_dict(),
                   save_path + 'scribble' + '_%d' % epoch + '.pth')
Example #13
0
def train_emotion(encoder, decoder, optimizer, criterion, data_loaders, tags,
                  log_step, grad_clip):
    decoder.train()
    encoder.train()

    batch_time = AverageMeter()
    losses = [AverageMeter() for _ in range(len(tags))]
    start = time.time()

    for j in random.sample([i for i in range(len(tags))], len(tags)):
        for i, (images, captions, lengths,
                all_captions) in enumerate(data_loaders[j]):
            # Set mini-batch dataset
            images = images.to(device)
            captions = captions.to(device)
            lengths = [l - 1 for l in lengths]
            targets = pack_padded_sequence(input=captions[:, 1:],
                                           lengths=lengths,
                                           batch_first=True)[0]
            # Forward, backward and optimize
            features = encoder(images)
            outputs, alphas = decoder(captions[:, :-1],
                                      lengths,
                                      features,
                                      mode=tags[j])
            loss = criterion(outputs, targets)
            alpha_c = 1.
            loss += alpha_c * ((1. - alphas.sum(dim=1))**2).mean()

            decoder.zero_grad()
            # encoder.zero_grad()
            loss.backward()
            # Clip gradients
            clip_gradient(optimizer, grad_clip)
            optimizer.step()
            # optimizer[j].step()

            if i % log_step == 0:
                print("""Step [{}/{}], [{}], Loss: {:.4f}""".format(
                    i, len(data_loaders[j]), tags[j][:3].upper(), loss.item()))
            # Keep track of metrics
            losses[j].update(loss.item(), sum(lengths))
            batch_time.update(time.time() - start)

            # free
            del images
            del captions
            del lengths
            del all_captions
            del targets
            del outputs
            del alphas

        torch.cuda.empty_cache()

    return batch_time.val, [loss.avg for loss in losses]
Example #14
0
def train(train_loader, model_RGB, model_depth, optimizer, epoch):
    model_RGB.train()
    for i, pack in enumerate(train_loader, start=1):
        optimizer.zero_grad()

        images, depth, gts = pack

        images = Variable(images)
        depth = Variable(depth)
        gts = Variable(gts)
        images = images.cuda()
        depth = depth.cuda()
        gts = gts.cuda()

        n, c, h, w = images.size()
        depth = depth.view(n, h, w, 1).repeat(1, 1, 1, 3)
        depth = depth.transpose(3, 1)
        depth = depth.transpose(3, 2)

        dets_depth = model_depth(depth)
        dets, att_3, att_4, att_5 = model_RGB(images)

        loss_depth = cross_entropy2d(dets_depth.detach(), gts, temperature=20)
        loss_gt = cross_entropy2d(dets, gts, temperature=1)
        LIPU_loss = KD_KLDivLoss(dets, dets_depth.detach(), temperature=20)

        alpha = math.exp(-70 * loss_depth)
        loss_adptative = (1 - alpha) * loss_gt + alpha * LIPU_loss

        Dilation_depth = F.softmax(dets_depth, dim=1)
        Dilation_depth = Dilation(Dilation_depth[:, 1, :, :].view(n, 1, h, w))

        loss_attention = CE(att_3, Dilation_depth.detach()) + \
                         CE(att_4, Dilation_depth.detach()) + \
                           CE(att_5, Dilation_depth.detach())

        loss = loss_adptative + loss_attention

        loss.backward()
        optimizer.step()

        clip_gradient(optimizer, opt.clip)

        if i % 10 == 0 or i == total_step:
            print(
                '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss: {:.4f}, Loss_gt: {:.4f}, Loss_att: {:.4f}, Loss_LIPU: {:.4f}, alpha: {:.4f}'
                .format(datetime.now(), epoch, opt.epoch, i, total_step,
                        loss.data, loss_gt.data, loss_attention.data,
                        LIPU_loss.data, alpha))

    save_path = 'models/RGB_with_A2dele/'

    if not os.path.exists(save_path):
        os.makedirs(save_path)
    if (epoch + 1) % 1 == 0:
        torch.save(model_RGB.state_dict(), save_path + '%d' % epoch + '_w.pth')
Example #15
0
def train(train_loader, model, optimizer, epoch, save_path):
    global step
    model.train()
    loss_all = 0
    epoch_step = 0
    try:
        for i, (images, gts, depths) in enumerate(train_loader, start=1):
            optimizer.zero_grad()

            images = images.cuda()
            gts = gts.cuda()
            depths = depths.cuda()
            pred, pL, pR = model(images, depths)
            loss1, loss2, loss3 = hybrid_e_loss(pred, gts), hybrid_e_loss(
                pL, gts), hybrid_e_loss(pR, gts)
            loss = 0.333 * (loss1 + loss2 + loss3)
            loss.backward()

            clip_gradient(optimizer, opt.clip)
            optimizer.step()
            step += 1
            epoch_step += 1
            loss_all += loss.data
            if i % 100 == 0 or i == total_step or i == 1:
                print(
                    '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss: {:.4f}'
                    .format(datetime.now(), epoch, opt.epoch, i, total_step,
                            loss.data))
                logging.info(
                    '#TRAIN#:Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss: {:.4f}'
                    .format(epoch, opt.epoch, i, total_step, loss.data))
                #writer.add_scalar('Loss', loss.data, global_step=step)
                #grid_image = make_grid(images[0].clone().cpu().data, 1, normalize=True)
                #writer.add_image('RGB', grid_image, step)
                #grid_image = make_grid(gts[0].clone().cpu().data, 1, normalize=True)
                #writer.add_image('Ground_truth', grid_image, step)
                #res = pred[0].clone()
                #res = res.sigmoid().data.cpu().numpy().squeeze()
                #res = (res - res.min()) / (res.max() - res.min() + 1e-8)
                #writer.add_image('pred', torch.tensor(res), step,dataformats='HW')

        loss_all /= epoch_step
        logging.info('#TRAIN#:Epoch [{:03d}/{:03d}], Loss_AVG: {:.4f}'.format(
            epoch, opt.epoch, loss_all))
        writer.add_scalar('Loss-epoch', loss_all, global_step=epoch)
        if (epoch) % 5 == 0:
            torch.save(model.state_dict(),
                       save_path + 'CMANet_epoch_{}.pth'.format(epoch))
    except KeyboardInterrupt:
        print('Keyboard Interrupt: save model and exit.')
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        torch.save(model.state_dict(),
                   save_path + 'CMANet_epoch_{}.pth'.format(epoch + 1))
        print('save checkpoints successfully!')
        raise
Example #16
0
def train_with_painter(painter_model, epoch):
    model.train()
    painter_model.eval()
    epoch_rewards = []
    for batch_idx, data in enumerate(train_loader):
        raw_data = data[-1]
        data = [Variable(_, requires_grad=False).cuda() for _ in data[:-1]]
        prev_canvas, inst, next_obj, final_canvas, ref_obj = data
        fc_att1_optimizer.zero_grad()
        samples = model.sample(prev_canvas, final_canvas,
                               (next_obj, ref_obj, None))
        # [a, b, c, 0, d, 0] -> [a, b, c, 0, 0, 0]
        masks = (samples == 0)
        for i in range(samples.size(0)):
            if masks[i].sum() > 0:
                index = torch.nonzero(masks[i])[0, 0]
                samples[i, index:] = 0
        samples_input = torch.zeros(inst.size()).long()
        # [a, b, ...] -> [0, a, b, ...]
        samples_input[:, 1:samples.size(1) + 1] = samples
        target_obj_prediction = get_painter_model_prediction(
            painter_model,
            [Variable(samples_input.cuda()), prev_canvas, ref_obj, next_obj])
        # print((torch.eq(target_obj_predict, next_obj.data).sum(dim=1) == 4).long().sum())
        painter_rewards = get_painter_rewards(target_obj_prediction,
                                              final_canvas.data.long())

        # samples2 = decode_sequence(train_loader.dataset.ix_to_word, samples)
        # diff_canvas = compute_diff_canvas(prev_canvas, final_canvas)
        # diff_canvas_objs = [train_loader.dataset.decode_canvas(diff_canvas.data[i]) for i in range(diff_canvas.size(0))]
        # for i in range(prev_canvas.size(0)):
        #     raw_data[i]['predicted_instruction'] = samples2[i]
        # for i, d in enumerate(raw_data):
        #     d['prev_canvas'] = render_canvas(d['prev_canvas']).replace(' ', '')
        #     d['final_canvas'] = render_canvas(d['final_canvas']).replace(' ', '')
        #     d['diff_canvas'] = render_canvas(diff_canvas_objs[i]).replace(' ', '')
        # r = requests.post("http://vision.cs.virginia.edu:5001/new_task", json=raw_data)
        # print('request sent')

        policy_loss = (-model.saved_log_probs *
                       Variable(painter_rewards.cuda())).sum()
        policy_loss.backward()
        clip_gradient(fc_att1_optimizer, 0.1)
        fc_att1_optimizer.step()
        model.saved_log_probs = None
        model.sampled_actions = None
        model.rewards = None

        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tRewards: {:.6f})'.format(
                epoch, batch_idx * args.batch_size, len(train_loader.dataset),
                100. * batch_idx / len(train_loader), painter_rewards.mean()))
        epoch_rewards.append(painter_rewards.mean())
    print("epoch reward {}".format(np.array(epoch_rewards).mean()))
    torch.save(model.state_dict(),
               'instructor_trained_with_painter/model_{}.pth'.format(epoch))
Example #17
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate ** frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
                inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        
        if opt.mixup:
            inputs, targets_a, targets_b, lam = utils.mixup_data(inputs, targets, 0.6, True)
            inputs, targets_a, targets_b = map(Variable, (inputs, targets_a, targets_b))
        else:
            inputs, targets = Variable(inputs), Variable(targets)
        
        outputs = net(inputs)
        
        if opt.mixup:
            loss = utils.mixup_criterion(criterion, outputs, targets_a, targets_b, lam)
        else:
            loss = criterion(outputs, targets)
        
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        train_loss += loss.item()
        
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        
        if opt.mixup:
            correct += (lam * predicted.eq(targets_a.data).cpu().sum().float()
                    + (1 - lam) * predicted.eq(targets_b.data).cpu().sum().float())
        else:
            correct += predicted.eq(targets.data).cpu().sum()
       
        utils.progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
            % (train_loss/(batch_idx+1), 100.*float(correct)/float(total), correct, total))

    Train_acc = 100.*float(correct)/float(total)
    
    return train_loss/(batch_idx+1), Train_acc
Example #18
0
def train(train_loader, model, optimizer, epoch, logger, writer):
    model.train()  # train mode (dropout and batchnorm is used)

    losses = AverageMeter()
    times = AverageMeter()

    start = time.time()

    # Batches
    for i, (data) in enumerate(train_loader):
        # Move to GPU, if available
        padded_input, padded_target, input_lengths = data
        padded_input = padded_input.to(Config.device)
        padded_target = padded_target.to(Config.device)
        input_lengths = input_lengths.to(Config.device)

        # Forward prop.
        pred, gold = model(padded_input, input_lengths, padded_target)

        loss, n_correct = cal_performance(pred, gold, smoothing=args.label_smoothing)

        try:
            assert (not math.isnan(loss.item()))
        except AssertionError:
            print('n_correct: ' + str(n_correct))
            print('data: ' + str(n_correct))
            continue

        # Back prop.
        optimizer.zero_grad()
        loss.backward()

        # Clip gradients
        clip_gradient(optimizer.optimizer, Config.grad_clip)

        # Update weights
        optimizer.step()

        # Keep track of metrics
        elapsed = time.time() - start
        start = time.time()

        losses.update(loss.item())
        times.update(elapsed)

        # Print status
        if i % Config.print_freq == 0:
            logger.info('Epoch: [{0}][{1}/{2}]\t'
                        'Batch time {time.val:.5f} ({time.avg:.5f})\t'
                        'Loss {loss.val:.5f} ({loss.avg:.5f})'.format(epoch, i, len(train_loader), time=times,
                                                                      loss=losses))
            writer.add_scalar('step_num/train_loss', losses.avg, optimizer.step_num)
            writer.add_scalar('step_num/learning_rate', optimizer.lr, optimizer.step_num)

    return losses.avg
Example #19
0
def train(train_loader, model, metric_fc, criterion, optimizer, epoch, logger,
          scheduler):
    model.train()  # train mode (dropout and batchnorm is used)
    metric_fc.train()

    losses = AverageMeter()
    top1_accs = AverageMeter()

    # Batches
    for i, (img, label) in enumerate(train_loader):
        # Move to GPU, if available
        img = img.to(device)
        label = label.to(device)  # [N, 1]

        # Forward prop.
        feature = model(img)  # embedding => [N, 512]
        output = metric_fc(feature, label)  # class_id_out => [N, 10575]
        # output = metric_fc(feature)
        # Calculate loss
        loss = criterion(output, label)

        # Back prop.
        optimizer.zero_grad()
        loss.backward()

        # Clip gradients
        clip_gradient(optimizer, grad_clip)

        # Update weights
        optimizer.step()

        # Keep track of metrics
        losses.update(loss.item())
        top1_accuracy = accuracy(output, label, 1)  # output
        top1_accs.update(top1_accuracy)

        # Update scheduler for learning rate decay for OneCircle Learning Rate
        scheduler.step()

        # Print status
        if i % print_freq == 0:
            logger.info(
                'Epoch: [{0}][{1}/{2}]\t'
                'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                'Top1 Accuracy {top5_accs.val:.3f} ({top5_accs.avg:.3f})\t'
                'Learning rate: {learning_rate:.5f}'.format(
                    epoch,
                    i,
                    len(train_loader),
                    loss=losses,
                    top5_accs=top1_accs,
                    learning_rate=optimizer.param_groups[0]['lr']))

    return losses.avg, top1_accs.avg
def train_epoch(model, train_iter, epoch, loss_fn, optimizer, log_dict):

    total_epoch_loss = 0
    total_epoch_acc = 0
    model.cuda()
    steps = 0
    model.train()
    start_train_time = time.time()

    for idx, batch in enumerate(train_iter):

        text, attn, target = select_input(batch, log_dict.param)

        if (
                len(target) is not log_dict.param.batch_size
        ):  # Last batch may have length different than log_dict.param.batch_size
            continue

        if torch.cuda.is_available():
            text = [
                text[0].cuda(), text[1].cuda(), text[2].cuda(), text[3].cuda()
            ]

            attn = attn.cuda()
            target = target.cuda()

        ## model prediction
        model.zero_grad()
        optimizer.zero_grad()
        # print("Prediction")
        prediction = model(text, attn)
        # print("computing loss")
        loss = loss_fn(prediction, target)

        # print("Loss backward")
        startloss = time.time()
        loss.backward()
        # print(time.time()-startloss,"Finish loss")
        clip_gradient(model, 1e-1)
        # torch.nn.utils.clip_grad_norm_(model.parameters(),1)
        optimizer.step()
        # print("=====================")
        steps += 1
        if steps % 100 == 0:
            print(
                f'Epoch: {epoch+1:02}, Idx: {idx+1}, Training Loss: {loss.item():.4f}, Time taken: {((time.time()-start_train_time)/60): .2f} min'
            )
            start_train_time = time.time()

        total_epoch_loss += loss.item()

        # break

    return total_epoch_loss / len(train_iter)
Example #21
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate ** frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    for batch_idx, (inputs, targets) in enumerate(trainloader):
        bs, c, h, w = np.shape(inputs)
        inputs = torch.Tensor(inputs)
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        conTensor1 = net1.features(inputs).view(bs, -1)
        conTensor2 = layers(inputs, net2).view(bs, -1)
        if use_cuda:
            conTensor1, conTensor2 = conTensor1.cuda(), conTensor2.cuda()
        resTensor = torch.cat((conTensor1, conTensor2), 1)
        #print(resTensor.shape)

        resTensor = Variable(resTensor)
        outputs = netClassifier(resTensor)
        #outputs_avg = outputs.view(bs, ncrops, -1).mean(1)
        #print(outputs.shape,"train output")
        loss = criterion(outputs, targets)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        train_loss += loss.data.item()
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()
        '''
        utils.progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
            % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
        '''

    Train_acc = float(100. * correct) / float(total)
    #if ((epoch + 1) % 10 == 0):
    Ta = float(Train_acc)
    temp_train_acc.append(Ta)
    temp_train_loss.append(train_loss)
Example #22
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    print("Start Training!")
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0

    # 学习率下降
    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate**frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    # 迭代,迭代的次数 = 28708(训练集大小)/batch_size = batch数,计算loss和accuracy
    # batch_idx计迭代数
    iter_num = 0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()

        inputs, targets = Variable(inputs), Variable(targets)
        outputs = net(inputs)  # 正向传播
        loss = criterion(outputs, targets)
        loss.backward()  # 反向传播
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        # train_loss += loss.data[0]
        train_loss += loss.item()
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)  # 每次加上batch_size个数
        correct += predicted.eq(targets.data).cpu().sum()

        Train_acc = int(correct) / int(
            total)  # 也就是最后一次迭代的准确率,代表一个epoch的准确率(完整的数据集通过了神经网络)

        # 输出loss和训练集准确率
        # utils.progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
        #     % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))

        iter_num += 1

    train_loss = train_loss / iter_num
    train_loss_list.append(train_loss)
    train_acc_list.append(Train_acc)
    print("Train Accuracy:", Train_acc * 100, "%")
    print("Train Loss:", train_loss)
Example #23
0
def train(train_loader, model, criterion, optimizer, epoch, logger):
    model.train()  # train mode (dropout and batchnorm is used)

    losses = utils.AverageMeter()
    accs = utils.AverageMeter()

    # Batches
    for i, (images, labels) in enumerate(train_loader):
        # Move to GPU, if available
        images = images.to(device)
        batch_size = images.size(0)

        target_lengths = [min(max_target_len, len(t)) for t in labels]
        target_lengths = torch.LongTensor(target_lengths)  # size (batch size)

        targets = [utils.encode_target(t[:max_target_len]) for t in labels]
        targets = torch.LongTensor(targets).to(
            device)  # size (batch size, max target length)

        # Forward prop.
        inputs = model(
            images)  # size (input length, batch size, number of classes)
        input_lengths = Variable(torch.IntTensor(
            [inputs.size(0)] * batch_size))  # size (batch size)

        # Calculate loss
        loss = criterion(inputs, targets, input_lengths, target_lengths)
        acc = utils.accuracy(inputs, input_lengths, labels, batch_size)

        # Back prop.
        optimizer.zero_grad()
        loss.backward()

        # Clip gradients
        utils.clip_gradient(optimizer, grad_clip)

        # Update weights
        optimizer.step()

        # Keep track of metrics
        if loss.item() != float('inf'):
            losses.update(loss.item(), batch_size)
        accs.update(acc, batch_size)

        # Print status
        if i % print_freq == 0:
            logger.info('Epoch: [{0}][{1}/{2}]\t'
                        'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
                        'Accuracy {acc.val:.4f} ({acc.avg:.4f})'.format(
                            epoch, i, len(train_loader), loss=losses,
                            acc=accs))

    return losses.avg, accs.avg
Example #24
0
def train(train_loader, model, criterion, optimizer, epoch, logger):
    model.train()  # train mode (dropout and batchnorm is used)

    losses = AverageMeter()
    l_losses = AverageMeter()
    p_losses = AverageMeter()

    # Batches
    for i, (img, lbl_look_vec, lbl_pupil_size) in enumerate(train_loader):
        # Move to GPU, if available
        img = img.to(device)
        lbl_look_vec = lbl_look_vec.float().to(device)  # [N, 3]
        lbl_pupil_size = lbl_pupil_size.float().to(device)  # [N, 1]

        # Forward prop.
        out_look_vec, out_pupil_size = model(img)  # embedding => [N, 3]

        # Calculate loss
        loss1 = criterion(out_look_vec, lbl_look_vec)
        loss2 = criterion(out_pupil_size, lbl_pupil_size)
        loss2 = loss2  # / 20
        loss = loss1 + loss2

        # Back prop.
        optimizer.zero_grad()
        loss.backward()

        # Clip gradients
        clip_gradient(optimizer, grad_clip)

        # Update weights
        optimizer.step()

        # Keep track of metrics
        losses.update(loss.item() * 1000, img.size(0))
        l_losses.update(loss1.item() * 1000, img.size(0))
        p_losses.update(loss2.item() * 1000, img.size(0))

        # Print status
        if i % print_freq == 0:
            logger.info(
                'Epoch: [{0}][{1}/{2}]\t'
                'Loss {loss.val:.5f} ({loss.avg:.5f})\t'
                'Look Vec Loss {l_loss.val:.5f} ({l_loss.avg:.5f})\t'
                'Pupil Size Loss {p_loss.val:.5f} ({p_loss.avg:.5f})'.format(
                    epoch,
                    i,
                    len(train_loader),
                    loss=losses,
                    l_loss=l_losses,
                    p_loss=p_losses))

    return l_losses.avg + p_losses.avg
Example #25
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    global Train_acc
    global lr1
    net.train()
    train_loss = 0
    f_loss = 0.0
    correct = 0
    total = 0
    current_lr1 = 0.0

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate**frac
        decay_factor1 = 0.95**frac
        current_lr = opt.lr * decay_factor
        current_lr1 = lr1 * decay_factor1
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
        utils.set_lr(optimzer4center, current_lr1)
    else:
        current_lr = opt.lr
        current_lr1 = lr1
    print('learning_rate: %s' % str(current_lr))
    print('learning_rate1: %s' % str(current_lr1))

    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        optimzer4center.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        ip1, outputs = net(inputs)
        loss = nllloss(outputs,
                       targets) + loss_weight * centerloss(targets, ip1)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        optimzer4center.step()

        train_loss += loss.item()
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()
        f_loss = float(train_loss) / float(batch_idx + 1)
        utils.progress_bar(
            batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
            (train_loss / (batch_idx + 1),
             100.0 * float(correct) / float(total), correct, total))

    Train_acc = 100.0 * float(correct) / float(total)
    training_loss.append(f_loss)
    training_acc.append(Train_acc)
Example #26
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    conf_mat = np.zeros((NUM_CLASSES, NUM_CLASSES))

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate**frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        train_loss += loss.item()

        conf_mat += utils.confusion_matrix(outputs, targets, NUM_CLASSES)

        acc = sum([conf_mat[i, i]
                   for i in range(conf_mat.shape[0])]) / conf_mat.sum()
        uacc_per_class = [
            conf_mat[i, i] / conf_mat[i].sum()
            for i in range(conf_mat.shape[0])
        ]
        unweighted_acc = sum(uacc_per_class) / len(uacc_per_class)

        prec_per_class = [
            conf_mat[i, i] / conf_mat[:, i].sum()
            for i in range(conf_mat.shape[0])
        ]
        average_precision = sum(prec_per_class) / len(prec_per_class)

        utils.progress_bar(
            batch_idx, len(trainloader),
            'Loss: %.3f | Acc: %.3f%% | unweighted_Acc: %.3f%%' %
            (train_loss / (batch_idx + 1), 100. * acc, 100. * unweighted_acc))

    Train_acc = 100. * acc
def train(epoch):
    print('\nEpoch: %d' % epoch)
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate**frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    for batch_idx, (inputs, targets) in enumerate(trainloader):
        # *********************process the weights including binarization*********************
        #print("!!!bin!!!\r\n")
        bin_op.binarization()

        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        #***********************************************
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        #utils.clip_gradient(optimizer, 0.1)

        # ****************restore weights*****************
        bin_op.restore()
        bin_op.updateBinaryGradWeight()

        if opt.sr:
            updateBN()

        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        train_loss += loss.item()
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()

        utils.progress_bar(
            batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)' %
            (train_loss /
             (batch_idx + 1), 100. * float(correct) / total, correct, total))

    Train_acc = 100. * float(correct) / total
Example #28
0
def train_epoch(model, train_iter, epoch,loss_fn,optimizer,log_dict):

	total_epoch_loss = 0
	total_epoch_acc = 0
	model.cuda()
	steps = 0
	model.train()
	start_train_time = time.time()
	for idx, batch in enumerate(train_iter):

		text, attn, target = select_input(batch,log_dict.param)

		target = torch.autograd.Variable(target).long()

		if (target.size()[0] is not log_dict.param.batch_size):# Last batch may have length different than log_dict.param.batch_size
			continue

		if torch.cuda.is_available():
			text = [text[0].cuda(),text[1].cuda(),text[2].cuda(),text[3].cuda()]
			attn = attn.cuda()
			target = target.cuda()


		## model prediction
		model.zero_grad()
		optimizer.zero_grad()
		# print("Prediction")
		prediction = model(text,attn)
		# print("computing loss")
		loss = loss_fn(prediction, target)

		## evaluation
		num_corrects = (torch.max(prediction, 1)[1].view(target.size()).data == target.data).float().sum()
		acc = 100.0 * num_corrects/log_dict.param.batch_size
		# print("Loss backward")
		startloss = time.time()
		loss.backward()
		# print(time.time()-startloss,"Finish loss")
		clip_gradient(model, 1e-1)
		# torch.nn.utils.clip_grad_norm_(model.parameters(),1)
		optimizer.step()
		# print("=====================")
		steps += 1
		if steps % 100 == 0:
			print (f'Epoch: {epoch+1:02}, Idx: {idx+1}, Training Loss: {loss.item():.4f}, Training Accuracy: {acc.item(): .2f}%, Time taken: {((time.time()-start_train_time)/60): .2f} min')
			start_train_time = time.time()

		total_epoch_loss += loss.item()
		total_epoch_acc += acc.item()

	return total_epoch_loss/len(train_iter), total_epoch_acc/len(train_iter)
Example #29
0
def train(train_loader, model, optimizer, epoch, save_path):
    global step
    model.train()
    loss_all = 0
    epoch_step = 0
    try:
        for i, (images, gts, depths) in enumerate(train_loader, start=1):
            optimizer.zero_grad()
            images = images.cuda()
            gts = gts.cuda()
            depths = depths.cuda()

            s, _1, _2, _3, _4 = model(images, depths)
            loss1 = CE(s, gts)
            loss2 = CE(_1, gts) / 2 + CE(_2, gts) / 4 + CE(_3, gts) / 8 + CE(
                _4, gts) / 16
            loss = loss1 + loss2

            with amp.scale_loss(loss, optimizer) as scale_loss:
                scale_loss.backward()
            #loss.backward()

            clip_gradient(optimizer, opt.clip)
            optimizer.step()

            step += 1
            epoch_step += 1
            loss_all += loss.data
            if i % 100 == 0 or i == total_step or i == 1:
                print(
                    '{} Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss1: {:.4f} Loss2: {:0.4f}'
                    .format(datetime.now(), epoch, opt.epoch, i, total_step,
                            loss1.data, loss2.data))
                logging.info(
                    '#TRAIN#:Epoch [{:03d}/{:03d}], Step [{:04d}/{:04d}], Loss1: {:.4f} Loss2: {:0.4f}'
                    .format(epoch, opt.epoch, i, total_step, loss1.data,
                            loss2.data))
        loss_all /= epoch_step
        logging.info('#####TRAIN#####')
        logging.info('Epoch [{:03d}/{:03d}], Loss_AVG: {:.4f}'.format(
            epoch, opt.epoch, loss_all))
        if (epoch) % 10 == 0:
            torch.save(model.state_dict(),
                       save_path + 'epoch_{}.pth'.format(epoch))
    except KeyboardInterrupt:
        print('Keyboard Interrupt: save model and exit.')
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        torch.save(model.state_dict(),
                   save_path + 'epoch_{}.pth'.format(epoch + 1))
        raise
def train(epoch):
    print('\nEpoch: %d' % epoch)
    global Train_acc
    net.train()
    train_loss = 0
    correct = 0
    total = 0

    if epoch > learning_rate_decay_start and learning_rate_decay_start >= 0:
        frac = (epoch - learning_rate_decay_start) // learning_rate_decay_every
        decay_factor = learning_rate_decay_rate**frac
        current_lr = opt.lr * decay_factor
        utils.set_lr(optimizer, current_lr)  # set the decayed rate
    else:
        current_lr = opt.lr
    print('learning_rate: %s' % str(current_lr))

    for batch_idx, (inputs, targets) in enumerate(trainloader):
        bs, c, h, w = np.shape(inputs)
        spl = int(bs / 2)
        input1 = inputs[0:spl]
        label1 = targets[0:spl]
        input2 = inputs[spl:bs]
        label2 = targets[spl:bs]
        input1 = perturb(input1, label1, 0.2, net)
        input1 = torch.Tensor(input1)
        inputs = np.vstack((input1, input2))
        inputs = torch.Tensor(inputs)
        if use_cuda:
            inputs, targets = inputs.cuda(), targets.cuda()
        optimizer.zero_grad()
        inputs, targets = Variable(inputs), Variable(targets)
        outputs = net(inputs)
        loss = criterion(outputs, targets)
        loss.backward()
        utils.clip_gradient(optimizer, 0.1)
        optimizer.step()
        train_loss += loss.data.item()
        _, predicted = torch.max(outputs.data, 1)
        total += targets.size(0)
        correct += predicted.eq(targets.data).cpu().sum()
        '''
        utils.progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
            % (train_loss/(batch_idx+1), 100.*correct/total, correct, total))
        '''

    Train_acc = 100. * correct / total
    if ((epoch + 1) % 10 == 0):
        Ta = float(Train_acc)
        temp_train_acc.append(Ta)
        temp_train_loss.append(train_loss)
Example #31
0
def train(train_loader, model, criterions, optimizer, epoch):
    batch_time = AverageMeter()
    data_time = AverageMeter()
    losses = AverageMeter()
    accs = AverageMeter()

    # switch to train mode
    model.train()

    end = time.time()
    for i, (inputs, targets, infos) in enumerate(train_loader):
        # measure data loading time
        data_time.update(time.time() - end)

        # construct targets
        target_clsf, target_unc, target_ex = targets
        target_conf = (target_clsf + target_ex) > 0
        rhem_wt = torch.zeros_like(target_clsf).cuda()
        rhem_wt[target_conf] = 1.
        target_clsf = target_clsf.cuda()
        target_clsf_wt = 1-target_unc.cuda()

        # run model
        inputs = [input.cuda() for input in inputs]
        out = model(inputs)

        # compute losses
        emb = out['emb']
        A, P, N = select_triplets_multilabel(emb, target_clsf)
        loss_metric = criterions['metric'](A, P, N)

        prob1 = out['class_prob1']
        loss_ce1 = criterions['wce'](prob1, target_clsf, infos, wt=target_clsf_wt)
        loss_rhem = criterions['rhem'](prob1, target_clsf, infos, wt=rhem_wt)
        if config.SCORE_PROPAGATION:
            prob2 = out['class_prob2']
            loss_ce2 = criterions['wce'](prob2, target_clsf, infos, wt=target_clsf_wt)

            sub_losses = [loss_ce1, loss_rhem, loss_metric, loss_ce2]
            wts_names = ['CE_LOSS_WT_1', 'RHEM_LOSS_WT', 'TRIPLET_LOSS_WT', 'CE_LOSS_WT_2']
        else:
            sub_losses = [loss_ce1, loss_rhem, loss_metric]
            wts_names = ['CE_LOSS_WT_1', 'RHEM_LOSS_WT', 'TRIPLET_LOSS_WT']

        loss = 0
        wts = [eval('config.TRAIN.' + name1) for name1 in wts_names]
        for wt1, loss1 in zip(wts, sub_losses):
            loss += wt1 * loss1

        losses.update(loss.item())

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        clip_gradient(model, default.clip_gradient)
        optimizer.step()

        # measure accuracy
        if config.SCORE_PROPAGATION:
            prob_np = prob2.detach().cpu().numpy()
        else:
            prob_np = prob1.detach().cpu().numpy()

        pred_labels = score2label(prob_np, config.TEST.SCORE_PARAM)
        targets_np = target_clsf.detach().cpu().numpy()
        target_unc = target_unc.numpy()
        acc = compute_all_acc_wt(targets_np > 0, pred_labels, prob_np, target_unc == 0)[config.TEST.CRITERION]

        accs.update(acc)

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % default.frequent == 0:
            crit = 'mean_pcF1' if config.TEST.CRITERION == 'mean_perclass_f1' else config.TEST.CRITERION
            msg = 'Epoch: [{0}][{1}/{2}] Time {batch_time.val:.1f} ' \
                  '({batch_time.avg:.1f}, {data_time.val:.1f})\t' \
                  .format(epoch, i, len(train_loader), batch_time=batch_time, data_time=data_time)
            msg += 'Loss {loss.val:.3f} ({loss.avg:.3f}){{'.format(loss=losses)
            for wt1, loss1 in zip(wts, sub_losses):
                msg += '%.3f*%.1f, ' % (loss1, wt1)
            msg += '}}\t{crit} {accs.val:.3f} ({accs.avg:.3f})'.format(
                crit=crit, accs=accs, ms=prob_np.max())
            logger.info(msg)