FDD/label_distillation_rr.py
def test(test_loader, model_name, criterion, fixed_input, labels, num_steps):
    """Test phase. This involves training a model from scratch."""
    losses_eval = AverageMeter()
    top1_eval = AverageMeter()
    start = time.time()

    print('Number of steps for retraining during test: ' + str(num_steps))

    # initialize a new model
    if args.target == "cifar10" or args.target == "cifar100":
        if args.test_various_models:
            if model_name == 'resnet':
                model_eval = ResNet(dataset=args.target, depth=18, num_classes=num_classes,
                                    bottleneck=False).to(device=device)
            elif model_name == 'LeNet':
                model_eval = LeNet(args).to(device=device)
            else:
                model_eval = AlexCifarNet(args).to(device=device)
        else:
            if args.resnet:
                model_eval = ResNet(dataset=args.target, depth=18, num_classes=num_classes,
                                    bottleneck=False).to(device=device)
            else:
                model_eval = AlexCifarNet(args).to(device=device)
    else:
        model_eval = LeNet(args).to(device=device)
    model_eval.train()

    if args.test_various_models:
        if model_name == 'resnet':
            w_eval = torch.zeros(
                64 + 1, num_classes, device=device, requires_grad=False)
        elif model_name == 'LeNet':
            w_eval = torch.zeros(
                84 + 1, num_classes, device=device, requires_grad=False)
        else:
            w_eval = torch.zeros(
                192 + 1, num_classes, device=device, requires_grad=False)
    else:
        w_eval = torch.zeros(
            num_out_feats + 1, num_classes, device=device, requires_grad=False)

    feat_opt = torch.optim.Adam(model_eval.parameters())
    adjust = AdjustLayer(device=device)
    adj_opt = torch.optim.Adam(list(adjust.parameters()), 0.01)

    # train a model from scratch for the given number of steps
    # using only the base examples and their synthetic labels
    for ITER in range(num_steps):
        # sample a minibatch of n_i examples from x~, y~: x~', y~'
        perm = torch.randperm(fixed_input.size(0))
        idx = perm[:args.inner_batch_size]
        fi_i = fixed_input[idx]
        lb_i = labels[idx]

        # solve the problem to find local weights w_l that minimize L(f_w_l(f_theta (x~')), y~')$
        feature = model_eval(fi_i)
        feature_add_bias = torch.cat(
            (feature, torch.ones((args.inner_batch_size, 1), device=device)), 1)
        w_local = get_local_weights(feature_add_bias, lb_i, device)

        # update w <-- alpha w + (1-alpha) w_l (pseudo-gradient)
        w_eval = (1 - 0.01) * w_eval.detach() + 0.01 * w_local
        feat_output = model_eval(fi_i, adjust() * w_eval)

        feat_loss = soft_cross_entropy(feat_output, labels[idx].detach())
        feat_opt.zero_grad()
        adj_opt.zero_grad()
        feat_loss.backward()
        feat_opt.step()
        adj_opt.step()

    # evaluate the test error
    # switch to evaluate mode
    model_eval.eval()

    for i, (input_, target) in enumerate(test_loader):
        input_ = input_.to(device=device)
        target = target.to(device=device)
        output = model_eval(input_, w_eval)
        loss = criterion(output, target)

        # measure error and record loss
        err1 = compute_error_rate(output.data, target, topk=(1,))[0]
        top1_eval.update(err1.item(), input_.size(0))
        losses_eval.update(loss.item(), input_.size(0))

    test_time = time.time() - start
    print('Testing with the feature extractor and global SVM parameters trained from scratch')
    print('Test time: ' + str(test_time))
    print(
        'Test error (top-1 error): {top1_eval.avg:.4f}'.format(top1_eval=top1_eval))

    return top1_eval.avg, losses_eval.avg
Test phase. This involves training a model from scratch.
Test phase. This involves training a model from scratch.
Test phase . This involves training a model from scratch .
def test(test_loader, model_name, criterion, fixed_input, labels, num_steps):
    losses_eval = AverageMeter()
    top1_eval = AverageMeter()
    start = time.time()
    print('Number of steps for retraining during test: ' + str(num_steps))
    if args.target == "cifar10" or args.target == "cifar100":
        if args.test_various_models:
            if model_name == 'resnet':
                model_eval = ResNet(dataset=args.target, depth=18, num_classes=num_classes,
                                    bottleneck=False).to(device=device)
            elif model_name == 'LeNet':
                model_eval = LeNet(args).to(device=device)
            else:
                model_eval = AlexCifarNet(args).to(device=device)
        else:
            if args.resnet:
                model_eval = ResNet(dataset=args.target, depth=18, num_classes=num_classes,
                                    bottleneck=False).to(device=device)
            else:
                model_eval = AlexCifarNet(args).to(device=device)
    else:
        model_eval = LeNet(args).to(device=device)
    model_eval.train()
    if args.test_various_models:
        if model_name == 'resnet':
            w_eval = torch.zeros(
                64 + 1, num_classes, device=device, requires_grad=False)
        elif model_name == 'LeNet':
            w_eval = torch.zeros(
                84 + 1, num_classes, device=device, requires_grad=False)
        else:
            w_eval = torch.zeros(
                192 + 1, num_classes, device=device, requires_grad=False)
    else:
        w_eval = torch.zeros(
            num_out_feats + 1, num_classes, device=device, requires_grad=False)
    feat_opt = torch.optim.Adam(model_eval.parameters())
    adjust = AdjustLayer(device=device)
    adj_opt = torch.optim.Adam(list(adjust.parameters()), 0.01)
    for ITER in range(num_steps):
        perm = torch.randperm(fixed_input.size(0))
        idx = perm[:args.inner_batch_size]
        fi_i = fixed_input[idx]
        lb_i = labels[idx]
        feature = model_eval(fi_i)
        feature_add_bias = torch.cat(
            (feature, torch.ones((args.inner_batch_size, 1), device=device)), 1)
        w_local = get_local_weights(feature_add_bias, lb_i, device)
        w_eval = (1 - 0.01) * w_eval.detach() + 0.01 * w_local
        feat_output = model_eval(fi_i, adjust() * w_eval)
        feat_loss = soft_cross_entropy(feat_output, labels[idx].detach())
        feat_opt.zero_grad()
        adj_opt.zero_grad()
        feat_loss.backward()
        feat_opt.step()
        adj_opt.step()
    model_eval.eval()
    for i, (input_, target) in enumerate(test_loader):
        input_ = input_.to(device=device)
        target = target.to(device=device)
        output = model_eval(input_, w_eval)
        loss = criterion(output, target)
        err1 = compute_error_rate(output.data, target, topk=(1,))[0]
        top1_eval.update(err1.item(), input_.size(0))
        losses_eval.update(loss.item(), input_.size(0))
    test_time = time.time() - start
    print('Testing with the feature extractor and global SVM parameters trained from scratch')
    print('Test time: ' + str(test_time))
    print(
        'Test error (top-1 error): {top1_eval.avg:.4f}'.format(top1_eval=top1_eval))
    return top1_eval.avg, losses_eval.avg
def test ( test_loader , model_name , criterion , fixed_input , labels , num_steps ) : losses_eval = AverageMeter ( ) top1_eval = AverageMeter ( ) start = time . time ( ) print ( 'Number of steps for retraining during test: ' + str ( num_steps ) ) if args . target == "cifar10" or args . target == "cifar100" : if args . test_various_models : if model_name == 'resnet' : model_eval = ResNet ( dataset = args . target , depth = 18 , num_classes = num_classes , bottleneck = False ) . to ( device = device ) elif model_name == 'LeNet' : model_eval = LeNet ( args ) . to ( device = device ) else : model_eval = AlexCifarNet ( args ) . to ( device = device ) else : if args . resnet : model_eval = ResNet ( dataset = args . target , depth = 18 , num_classes = num_classes , bottleneck = False ) . to ( device = device ) else : model_eval = AlexCifarNet ( args ) . to ( device = device ) else : model_eval = LeNet ( args ) . to ( device = device ) model_eval . train ( ) if args . test_various_models : if model_name == 'resnet' : w_eval = torch . zeros ( 64 + 1 , num_classes , device = device , requires_grad = False ) elif model_name == 'LeNet' : w_eval = torch . zeros ( 84 + 1 , num_classes , device = device , requires_grad = False ) else : w_eval = torch . zeros ( 192 + 1 , num_classes , device = device , requires_grad = False ) else : w_eval = torch . zeros ( num_out_feats + 1 , num_classes , device = device , requires_grad = False ) feat_opt = torch . optim . Adam ( model_eval . parameters ( ) ) adjust = AdjustLayer ( device = device ) adj_opt = torch . optim . Adam ( list ( adjust . parameters ( ) ) , 0.01 ) for ITER in range ( num_steps ) : perm = torch . randperm ( fixed_input . size ( 0 ) ) idx = perm [ : args . inner_batch_size ] fi_i = fixed_input [ idx ] lb_i = labels [ idx ] feature = model_eval ( fi_i ) feature_add_bias = torch . cat ( ( feature , torch . ones ( ( args . inner_batch_size , 1 ) , device = device ) ) , 1 ) w_local = get_local_weights ( feature_add_bias , lb_i , device ) w_eval = ( 1 - 0.01 ) * w_eval . detach ( ) + 0.01 * w_local feat_output = model_eval ( fi_i , adjust ( ) * w_eval ) feat_loss = soft_cross_entropy ( feat_output , labels [ idx ] . detach ( ) ) feat_opt . zero_grad ( ) adj_opt . zero_grad ( ) feat_loss . backward ( ) feat_opt . step ( ) adj_opt . step ( ) model_eval . eval ( ) for i , ( input_ , target ) in enumerate ( test_loader ) : input_ = input_ . to ( device = device ) target = target . to ( device = device ) output = model_eval ( input_ , w_eval ) loss = criterion ( output , target ) err1 = compute_error_rate ( output . data , target , topk = ( 1 , ) ) [ 0 ] top1_eval . update ( err1 . item ( ) , input_ . size ( 0 ) ) losses_eval . update ( loss . item ( ) , input_ . size ( 0 ) ) test_time = time . time ( ) - start print ( 'Testing with the feature extractor and global SVM parameters trained from scratch' ) print ( 'Test time: ' + str ( test_time ) ) print ( 'Test error (top-1 error): {top1_eval.avg:.4f}' . format ( top1_eval = top1_eval ) ) return top1_eval . avg , losses_eval . avg
Test phase.
Test phase .
"""Test phase. This involves training a model from scratch.""" # initialize a new model # train a model from scratch for the given number of steps # using only the base examples and their synthetic labels # sample a minibatch of n_i examples from x~, y~: x~', y~' # solve the problem to find local weights w_l that minimize L(f_w_l(f_theta (x~')), y~')$ # update w <-- alpha w + (1-alpha) w_l (pseudo-gradient) # evaluate the test error # switch to evaluate mode # measure error and record loss
test_loader
model_name
criterion
fixed_input
labels
num_steps