def load_weights(model_path, manipulate_weights, i, device, noise_state_dict):

    model = NET()
    #print(model_path)
    model.to(device)

    state_dict = torch.load(model_path)
    if manipulate_weights:
        for layer in state_dict.keys():
            if layer.find("lin") != -1:
                state_dict[layer] += noise_state_dict[layer] * i

            if layer.find("conv") != -1:
                state_dict[layer] += noise_state_dict[layer] * i
                #manipulate_conv(state_dict[layer], i)
    model.load_state_dict(state_dict)
    return model.to(device=device, dtype=torch.double)
Beispiel #2
0
def test(args, shared_model, dataset, targets, log):
    start_time = time.time()
    log.info('Test time ' +
             time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() -
                                                      start_time)) + ', ' +
             'Start testing.')
    local_model = NET()
    local_model.load_state_dict(shared_model.state_dict())
    if args.gpu:
        local_model = local_model.cuda()

    correct_cnt = 0
    predictions = np.zeros([targets.shape[0]], dtype=np.int64)

    for idx in range(targets.shape[0]):
        data = dataset[idx]
        data = Variable(torch.from_numpy(data))
        if args.gpu:
            data = data.cuda()

        target = targets[idx]
        output = local_model(data)
        if args.gpu:
            output = output.cpu()
        predict_class = output.max(0)[1].data.numpy()[0]
        predictions[idx] = predict_class
        if target == predict_class:
            correct_cnt += 1
        # else:
        #     print(predict_class)

        # if (idx + 1) % 100 == 0:
        #     log.info('Test time ' + time.strftime("%Hh %Mm %Ss", time.gmtime(time.time() - start_time)) + ', ' + 'Accuracy: %d / %d\t%0.4f' % (correct_cnt, idx + 1, correct_cnt / (idx + 1)))

    log.info('Overall f1 score = %0.4f' %
             (f1_score(list(targets), list(predictions), average='weighted')))
    log.info('Overall accuracy = %0.2f%%' %
             (100 * correct_cnt / targets.shape[0]))
    return correct_cnt / targets.shape[0]
Beispiel #3
0
    torch.set_default_tensor_type('torch.DoubleTensor')
    torch.manual_seed(args.seed)
    random.seed(args.seed)

    if not os.path.exists(args.model_dir):
        os.mkdir(args.model_dir)
    if not os.path.exists(args.log_dir):
        os.mkdir(args.log_dir)

    if args.train:
        model = NET()
        if args.model_load:
            try:
                saved_state = torch.load(
                    os.path.join(args.model_dir, 'best_model.dat'))
                model.load_state_dict(saved_state)
            except:
                print('Cannot load existing model from file!')
        if args.gpu:
            model = model.cuda()

        loss_func = nn.CrossEntropyLoss()
        dataset = torch.from_numpy(np.load("../output/data/dataset_train.npy"))
        targets = torch.from_numpy(
            np.int64(np.load("../output/data/target_train.npy")))
        dataset_test = np.load(dataset_path)
        targets_test = np.int64(np.load(target_path))
        if args.L2norm:
            log_test = setup_logger(
                0, 'test_log_norm',
                os.path.join(args.log_dir, 'test_log_norm.txt'))
Beispiel #4
0
def main():
    global opt, best_mae_error

    #dataset = CIFData(*opt.dataroot)
    dataset = h5(*opt.dataroot) 

    collate_fn = collate_pool
    
    train_loader, val_loader, test_loader = get_train_val_test_loader(
            dataset=dataset,collate_fn=collate_fn,batch_size=opt.batch_size,
            train_size=opt.train_size, num_workers=opt.workers,
            val_size=opt.val_size, test_size=opt.test_size,pin_memory=opt.cuda,
            return_test=True)
    # obtain target value normalizer
    sample_data_list = [dataset[i] for i in
                        sample(range(len(dataset)), 1000)]
    input, sample_target,_ = collate_pool(sample_data_list)
    input_1=input[0]
    normalizer = Normalizer(sample_target)
    s = Normalizer(input_1)


    model=NET()

    if torch.cuda.is_available():
        print('cuda is ok')
        model = model.cuda()

    criterion = nn.MSELoss()
    optimizer = optim.SGD(model.parameters(), opt.lr,
                            momentum=opt.momentum,
                            weight_decay=opt.weight_decay)
    if opt.resume:
        if os.path.isfile(opt.resume):
            print("=> loading checkpoint '{}'".format(opt.resume))
            checkpoint = torch.load(opt.resume)
            opt.start_epoch = checkpoint['epoch']
            best_mae_error = checkpoint['best_mae_error']
            model.load_state_dict(checkpoint['state_dict'])
            optimizer.load_state_dict(checkpoint['optimizer'])
            normalizer.load_state_dict(checkpoint['normalizer'])
            print("=> loaded checkpoint '{}' (epoch {})".format(opt.resume, checkpoint['epoch']))   

        else:
            print("=> no checkpoint found at '{}'".format(opt.resume))
    scheduler = MultiStepLR(optimizer, milestones=opt.lr_milestones,
                            gamma=0.1)
    for epoch in range(opt.start_epoch,opt.epochs):
        train(train_loader, model, criterion, optimizer, epoch,normalizer,s)

        mae_error = validate(val_loader, model, criterion, normalizer,s) 

        if mae_error != mae_error:
            print('Exit due to NaN')
            sys.exit(1)
        is_best = mae_error < best_mae_error
        best_mae_error = min(mae_error, best_mae_error)
        
        save_checkpoint({ 
            'epoch': epoch + 1,
            'state_dict': model.state_dict(),
            'best_mae_error': best_mae_error,
            'optimizer': optimizer.state_dict(),
            'normalizer': normalizer.state_dict(),
            'opt': vars(opt)
        }, is_best)
        # test bset model
    print('---------Evaluate Model on Test Set---------------')
    best_checkpoint = torch.load('model_best.pth.tar')
    model.load_state_dict(best_checkpoint['state_dict'])
    validate(test_loader, model, criterion, normalizer, s,test=True)