示例#1
0
def sanitization_generation_metrics(feature_order=None, alpha_=P.Alpha, lambda_=P.Lambda, san_loss=P.SanLoss, pred_loss=P.PredLoss,
                                    disc_loss=P.DiscLoss, max_epoch=P.Epoch, k_pred=P.KPred, k_disc=P.KDisc, scale=P.Scale):

    # Return models and datasets

    # Take the first 70% timestep as training.
    train_prep = D.Preprocessing(P.TrainPath, prep_excluded=P.PreprocessingExcluded, scale=P.Scale,
                                 prep_included=P.PreprocessingIncluded)
    train_prep.set_features_ordering(feature_order)
    train_prep.fit_transform()
    test_prep = D.Preprocessing(P.TestPath, prep_excluded=P.PreprocessingExcluded, scale=P.Scale,
                                prep_included=P.PreprocessingIncluded)
    test_prep.set_features_ordering(feature_order)
    test_prep.fit_transform()
    train_ds = D.MotionSenseDataset(train_prep, window_overlap=P.Window_overlap)
    test_ds = D.MotionSenseDataset(test_prep, window_overlap=P.Window_overlap)

    # Shape of unique values
    uniq_act = np.unique(train_ds.activities)
    uniq_sens = np.unique(train_ds.sensitive)
    uniq_uid = np.unique(train_ds.users_id)
    phys_cols = train_ds.phy_data.shape[1]
    try:
        act_cols = train_ds.activities.shape[1]
    except IndexError:
        act_cols = 1

    # Discriminator target
    disc_target_values = uniq_sens
    pred_target_values = uniq_act

    # Load dataset
    # Create dataloader
    # build data loaders
    batch_size = P.BatchSize
    s_train_dl = data.DataLoader(train_ds, batch_size=batch_size, shuffle=True, num_workers=4)
    d_train_dl = data.DataLoader(train_ds.copy(True), batch_size=batch_size, shuffle=True, num_workers=4)
    d_dl_iter = iter(d_train_dl)
    p_train_dl = data.DataLoader(train_ds.copy(True), batch_size=batch_size, shuffle=True, num_workers=4)
    p_dl_iter = iter(p_train_dl)

    # Create models:

    sanitizer = M.SanitizerConv(input_channels=train_ds.input_channels, seq_len=train_ds.seq_len, kernel_sizes=[5, 5],
                                strides=[1, 1], conv_paddings=[0, 0], phyNodes=phys_cols, noiseNodes=P.NoiseNodes,
                                actNodes=act_cols)

    # Adding physio data can prevent the sensor information to be dependent of such attribute because the disc model
    # Can not predict the sensitive value even though the height weight and other are given. Or we know that if an
    # attribute is strongly correlated, then the model will find such correlation. Example: Create a model to predict
    # something and in train set, give the target as data input the predict the same target. The model will learn to dis-
    # regard other columns
    # Predictor model output should be of same shape as necessary for NLLLoss. (model output is a matrix while target is
    # a vector).
    def get_models(input_channels=train_ds.input_channels, seq_len=train_ds.seq_len,
                   pred_out_size=pred_target_values.shape[0], disc_out_size=disc_target_values.shape[0],
                   phys_cols=phys_cols, act_cols=act_cols):
        predictor = M.PredictorConv(input_channels=input_channels, seq_len=seq_len, output_size=pred_out_size,
                                    physNodes=phys_cols)
        #M.load_classifier_state2(predictor, "Predictor")
        predictor.to(DEVICE)
        pred_optim = M.get_optimizer(predictor,)

        discriminator = M.DiscriminatorConv(input_channels=input_channels, seq_len=seq_len, output_size=disc_out_size,
                                            physNodes=phys_cols+act_cols)
        #M.load_classifier_state2(discriminator,"Discriminator")
        discriminator.to(DEVICE)
        disc_optim = M.get_optimizer(discriminator,)
        return predictor, pred_optim, discriminator, disc_optim

    def reset_weights(m):
        try:
            m.reset_parameters()
        except AttributeError as e:
            pass
            # print(e)
            # print("Layer not affected")

    predictor, pred_optim, discriminator, disc_optim = get_models()
    # Send models on GPU or CPU
    sanitizer.to(DEVICE)

    # Check the latest Epoch to start sanitization
    start_epoch = M.get_latest_states(P.ModelsDir(), sanitizer, discriminator, predictor, otherParamFn=P.ParamFunction)

    # Initialise losses
    san_loss = Cl.SanitizerBerLoss(alpha_=alpha_, lambda_=lambda_, recOn=P.RecOn, optim_type=P.OptimType, device=DEVICE)
    # pred_loss = Cl.AccuracyLoss(device=DEVICE)
    pred_loss = Cl.BalancedErrorRateLoss(targetBer=0, device=DEVICE)
    disc_loss = Cl.BalancedErrorRateLoss(targetBer=0, device=DEVICE)

    # Optimizers
    san_optim = M.get_optimizer(sanitizer,)

    losses_frame_path = "{}/{}.csv".format(P.ModelsDir(), P.ParamFunction("losses"))
    san_losses = [
        [], [], []
    ]
    disc_losses = []
    pred_losses = []
    if (start_epoch > 1) and tryReading(losses_frame_path):
        losses_frame = pd.read_csv(losses_frame_path)
        disc_losses = losses_frame["disc"].values.tolist()
        pred_losses = losses_frame["pred"].values.tolist()
        san_losses = losses_frame.drop(["pred", "disc"], axis=1).T.values.tolist()

    # Function to differentiate and integrate the activities. (Ignore for the predictor, integrate for the sanitizer)
    act_fn_disc = lambda ps, act: torch.cat((ps, act*P.DecorrelateActAndSens), 1)
    act_fn_pred = lambda ps, act: ps

    # Init figure
    fig = "asdfoijbnad"
    plt.figure(fig, figsize=(14, 14))

    # Sanitize
    print("Starting Sanitizing ......>")
    for epoch in tqdm.tqdm(range(start_epoch, max_epoch+1)):
        print("Current Epoch: {}".format(epoch))
        if P.TrainingResetModelsStates:
            predictor.apply(reset_weights)
            discriminator.apply(reset_weights)

            # del predictor
            # del discriminator
            # del disc_optim
            # del pred_optim
            # predictor, pred_optim, discriminator, disc_optim = get_models()

        for sample in s_train_dl:

            # Train the sanitizer
            l = train_sanitizer(sample, sanitizer, discriminator, predictor, san_loss,
                                           san_optim, act_fn=act_fn_disc, act_select=P.ActivitySelection,
                                           phys_select=P.PhysiologSelection, phys=P.PhysInput,
                                           san_acts=P.SanitizeActivities,)
            san_losses[0].append(l[0].mean().to(CPU_DEVICE).data.numpy().reshape(-1)[0])
            san_losses[1].append(l[1].to(CPU_DEVICE).data.numpy().reshape(-1)[0])
            san_losses[2].append(l[2].to(CPU_DEVICE).data.numpy().reshape(-1)[0])

            # Train the predictor
            l, p_dl_iter = train_predictor(pred_losses,k_pred, sanitizer, predictor, p_train_dl, p_dl_iter, pred_loss, pred_optim,
                                           act_fn=act_fn_pred, act_select=P.ActivitySelection,
                                           phys_select=P.PhysiologSelection, target_key="act",
                                           sens_key="sens", phys=P.PhysInput, san_acts=P.SanitizeActivities,)
            pred_losses.append(l.to(CPU_DEVICE).data.numpy().reshape(-1)[0])
            #pred_losses.append(1)
            # Train the discriminator
            l, d_dl_iter = train_predictor(disc_losses,k_pred, sanitizer, discriminator, d_train_dl, d_dl_iter, disc_loss, disc_optim,
                                           act_fn=act_fn_disc, act_select=P.ActivitySelection,
                                           phys_select=P.PhysiologSelection, target_key="sens",
                                           sens_key="sens", phys=P.PhysInput, san_acts=P.SanitizeActivities,)
            disc_losses.append(l.to(CPU_DEVICE).data.numpy().reshape(-1)[0])
            #disc_losses.append(1)

        print("***")
        # Save losses, and models states.
        # Saving models States.
        M.save_classifier_states(sanitizer, epoch, P.ModelsDir(), otherParamFn=P.ParamFunction, ext="S")
        M.save_classifier_states(discriminator, epoch, P.ModelsDir(), otherParamFn=P.ParamFunction, ext="D")
        M.save_classifier_states(predictor, epoch, P.ModelsDir(), otherParamFn=P.ParamFunction, ext="P")
        # Saving and plotting losses
        losses_frame = pd.DataFrame.from_dict({
            "san_rec": san_losses[0], "san_act": san_losses[1], "san_sens": san_losses[2],
            "disc": disc_losses, "pred": pred_losses,
        })
        losses_frame.to_csv(losses_frame_path, index=False)
        losses_frame["san_sens"] = san_loss.disc_loss.get_true_value(losses_frame["san_sens"].values)
        if epoch % P.PlotRate == 0:
            plt.subplot(5, 1, 1)
            sns.lineplot(x="index", y="san_rec", data=losses_frame.reset_index())
            plt.subplot(5, 1, 2)
            sns.lineplot(x="index", y="san_act", data=losses_frame.reset_index())
            plt.subplot(5, 1, 3)
            sns.lineplot(x="index", y="san_sens", data=losses_frame.reset_index())
            plt.subplot(5, 1, 4)
            sns.lineplot(x="index", y="disc", data=losses_frame.reset_index())
            plt.subplot(5, 1, 5)
            sns.lineplot(x="index", y="pred", data=losses_frame.reset_index())
            plt.savefig("{}/{}.png".format(P.FiguresDir(), P.ParamFunction("losses")))
            plt.clf()
        

    # Check datasets and generate
# def generate_dataset(san, train_prep, train_ds, train_dl, test_prep, test_ds, test_dl, gen_path, train_id="train",
#                      test_id="test", max_epoch=0, addParamFn=None, phys=1, san_acts=1, san_phys=1):
    
    print("Generating Sanitized Datasets")
    generate_dataset(sanitizer, train_prep, train_ds, test_prep, test_ds, P.GenDataDir(), max_epoch=P.Epoch,
                     addParamFn=P.ParamFunction, phys=P.PhysInput, san_acts=P.SanitizeActivities,
                     san_phys=P.SanitizePhysio)
    # Check if everything has been correctly generated

    #print("Computing Metrics")
    # If device == cpu_device, then we are not supposed to use gpu as there might not be anyone
    """metrics_computation(input_channels=train_ds.input_channels, seq_len=train_ds.seq_len,
示例#2
0
batch_size = 256
# Tester randomSampler to see if it only shuffle indices and not content
train_dl = data.DataLoader(train_ds,
                           batch_size=batch_size,
                           shuffle=True,
                           num_workers=4)
test_dl = data.DataLoader(test_ds,
                          batch_size=batch_size,
                          shuffle=False,
                          num_workers=4)

# Defining model Predicting activities.
activities = np.unique(train_ds.activities)
phys_shape = train_ds.phy_data.shape[1]
model = Models.SanitizerConv(input_channels=train_ds.input_channels,
                             seq_len=train_ds.seq_len,
                             phyNodes=0)
# Send model on GPU or CPU
model.to(device)

# Loss
# loss = torch.nn.MSELoss()
# sum(0) will reduce from 3 to 2 dimensions
loss = lambda x, y: torch.nn.MSELoss(reduction="none")(x, y).sum(0).sum(1) / (
    x.size(0) * x.size(2))
# loss = torch.nn.L1Loss()
# Training procedure
max_epochs = 5000
for i in range(max_epochs):

    print("Epoch: {}".format(i))