python_code
stringlengths 0
4.04M
| repo_name
stringlengths 8
58
| file_path
stringlengths 5
147
|
---|---|---|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import math
import torch
import torch.utils.data as data_utils
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from sklearn.decomposition import FastICA
from sklearn.decomposition import PCA
from utils.metrics import *
from models.encoder import Encoder
from models.decoder import Decoder
path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)
from utils.helper import ValidationHelper
from utils.metrics import *
import wandb
wandb.init(project="polynomial-identification", reinit=True)
class AE():
def __init__(self, args, train_dataset, val_dataset, test_dataset, seed=0, device= None):
self.args= args
self.seed= seed
self.device= device
self.train_dataset= train_dataset
self.val_dataset= val_dataset
self.test_dataset= test_dataset
self.encoder= Encoder(self.args.data_dim, self.args.latent_dim).to(self.device)
self.decoder= Decoder(self.args.data_dim, self.args.latent_dim).to(self.device)
self.opt, self.scheduler= self.get_optimizer()
self.validation_helper= ValidationHelper()
if self.args.intervention_case:
self.res_dir= 'results/ae/intervention/' + str(self.args.save_dir) + 'seed_' + str(seed) + '/'
else:
self.res_dir= 'results/ae/observation/' + str(self.args.save_dir) + 'seed_' + str(seed) + '/'
self.save_path= self.res_dir
if self.args.wandb_log:
wandb.init(project="polynomial-identification", reinit=True)
wandb.run.name= 'ae/' + self.args.save_dir + 'seed_' + str(seed) + '/'
def get_optimizer(self):
opt= optim.Adam([
{'params': filter(lambda p: p.requires_grad, list(self.encoder.parameters()) + list(self.decoder.parameters()) )},
], lr= self.args.lr, weight_decay= self.args.weight_decay )
scheduler = optim.lr_scheduler.StepLR(opt, step_size=500, gamma=0.5)
return opt, scheduler
def save_model(self):
if not os.path.exists(self.res_dir):
os.makedirs(self.res_dir)
torch.save(self.encoder.state_dict(), self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_encoder.pth')
torch.save(self.decoder.state_dict(), self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_decoder.pth')
return
def load_model(self):
self.encoder.load_state_dict(torch.load(self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_encoder.pth', map_location=torch.device('cpu')))
self.encoder.eval()
self.decoder.load_state_dict(torch.load(self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_decoder.pth', map_location=torch.device('cpu')))
self.decoder.eval()
return
def validation(self):
self.encoder.eval()
self.decoder.eval()
val_loss=0.0
count=0
for batch_idx, (x, _, _) in enumerate(self.val_dataset):
with torch.no_grad():
x= x.to(self.device)
z_pred= self.encoder(x)
out= self.decoder(z_pred)
loss= torch.mean(((out-x)**2))
val_loss+= loss.item()
count+=1
if self.args.wandb_log:
wandb.log({'val_loss': val_loss/count})
return val_loss/count
def train(self):
for epoch in range(self.args.num_epochs):
train_loss=0.0
count=0
#LR Scheduler
self.scheduler.step()
print(self.scheduler.get_last_lr())
#Training
self.encoder.train()
self.decoder.train()
#Compute identification metrics
if epoch % 10 == 0:
self.eval_identification(epoch= epoch)
if 'balls' in self.args.latent_case and ( epoch==0 or epoch==10 ):
self.save_intermediate_model(epoch= epoch)
for batch_idx, (x, _, _) in enumerate(self.train_dataset):
self.opt.zero_grad()
#Forward Pass
x= x.to(self.device)
z_pred= self.encoder(x)
x_pred= self.decoder(z_pred)
#Compute Reconstruction Loss
loss= self.compute_loss(z_pred, x_pred, x)
#Backward Pass
loss.backward()
# grad_norm= 0.0
# for p in self.coff_matrix.parameters():
# param_norm = p.grad.detach().data.norm(2)
# grad_norm += param_norm.item() ** 2
# grad_norm = grad_norm ** 0.5
# wandb.log({'grad_norm': grad_norm})
self.opt.step()
train_loss+= loss.item()
count+=1
val_score= self.validation()
print('\n')
print('Done Training for Epoch: ', epoch)
print('Training Loss: ', train_loss/count)
print('Validation Loss: ', val_score)
print('Best Epoch: ', self.validation_helper.best_epoch)
if self.args.wandb_log:
wandb.log({'train_loss': train_loss/count})
if self.validation_helper.save_model(val_score, epoch):
print('Saving model')
self.save_model()
elif self.validation_helper.early_stop(val_score):
print('Early Stopping')
break
return
def compute_loss(self, z_pred, x_pred, x):
loss= torch.mean(((x-x_pred)**2))
return loss
def eval_identification(self, epoch= -1):
self.encoder.eval()
self.decoder.eval()
save_dir= 'results/plots/'+ self.args.save_dir + 'epoch_' + str(epoch) + '/'
if not os.path.exists(save_dir):
os.makedirs(save_dir)
#Obtain Predictions and Reconstruction Loss
if 'balls' in self.args.latent_case:
plot_case=True
else:
plot_case=False
res= get_predictions(self.encoder, self.decoder, self.train_dataset, self.val_dataset, self.test_dataset, device= self.device, save_dir= save_dir, plot=plot_case)
true_z= res['true_z']
pred_z= res['pred_z']
recon_err= res['recon_loss']
print(true_z['tr'].shape, pred_z['tr'].shape)
#Latent Prediction Error
rmse, r2= get_indirect_prediction_error(pred_z, true_z)
print('latent prediction r2: ', r2)
if 'balls' in self.args.latent_case:
_, r2_mlp= get_indirect_prediction_error(pred_z, true_z, model= 'mlp')
print('latent prediction r2 MLP: ', r2_mlp)
else:
r2_mlp= 0
# MCC Score
if 'balls' not in self.args.latent_case:
mcc= get_cross_correlation(pred_z, true_z)
print('MCC: ', mcc)
else:
mcc=0
if self.args.wandb_log:
wandb.log({'test_loss': recon_err['te']})
wandb.log({'latent_pred_rmse': rmse})
wandb.log({'latent_pred_r2': r2})
# wandb.log({'max/min singular values': np.max(sig_values)/np.min(sig_values)})
wandb.log({'mcc': mcc})
wandb.log({'latent_pred_r2_mlp': r2_mlp})
# #DCI
# imp_matrix= compute_importance_matrix(pred_z['te'], true_z['te'], case='disentanglement')
# score= disentanglement(imp_matrix)
# wandb.log({'dci-disentanglement': score})
# imp_matrix= compute_importance_matrix(pred_z['te'], true_z['te'], case='completeness')
# score= completeness(imp_matrix)
# wandb.log({'dci-completeness': score})
# #MCC
# mcc= get_cross_correlation(pred_z, true_z)
# wandb.log({'mcc': mcc})
return rmse, r2
def get_final_layer_weights(self):
self.load_model()
for p in self.model.fc_net.parameters():
print(p.data)
|
CausalRepID-main
|
algorithms/base_auto_encoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import math
import torch
import torch.utils.data as data_utils
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from sklearn.decomposition import FastICA
from sklearn.decomposition import PCA
from utils.metrics import *
from models.image_encoder import ImageEncoder as Encoder
from models.image_decoder import ImageDecoder as Decoder
# from models.image_resnet_decoder import ImageDecoder as Decoder
#Base Class
from algorithms.base_auto_encoder import AE
path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)
from utils.helper import ValidationHelper
from utils.metrics import *
import wandb
class AE_Image(AE):
def __init__(self, args, train_dataset, val_dataset, test_dataset, seed=0, device= None):
super().__init__(args, train_dataset, val_dataset, test_dataset, seed, device)
self.encoder= Encoder(self.args.latent_dim).to(self.device)
self.decoder= Decoder(self.args.latent_dim).to(self.device)
self.opt, self.scheduler= self.get_optimizer()
self.validation_helper= ValidationHelper(patience=100)
if self.args.intervention_case:
self.res_dir= 'results/ae-image/intervention/' + str(self.args.save_dir) + 'seed_' + str(seed) + '/'
else:
self.res_dir= 'results/ae-image/observation/' + str(self.args.save_dir) + 'seed_' + str(seed) + '/'
self.save_path= self.res_dir
if self.args.wandb_log:
wandb.init(project="image-dataset-identification", reinit=True)
wandb.run.name= 'ae-image/' + self.args.save_dir + 'seed_' + str(seed) + '/'
def save_intermediate_model(self, epoch= -1):
if not os.path.exists(self.res_dir):
os.makedirs(self.res_dir)
torch.save(self.encoder.state_dict(), self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_epoch_' + str(epoch) + '_encoder.pth')
torch.save(self.decoder.state_dict(), self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_epoch_' + str(epoch) + '_decoder.pth')
return
def load_intermediate_model(self, epoch):
self.encoder.load_state_dict(torch.load(self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_epoch_' + str(epoch) + '_encoder.pth'))
self.encoder.eval()
self.decoder.load_state_dict(torch.load(self.save_path + 'lr_' + str(self.args.lr) + '_weight_decay_' + str(self.args.weight_decay) + '_batch_size_' + str(self.args.batch_size) + '_epoch_' + str(epoch) + '_decoder.pth'))
self.decoder.eval()
return
|
CausalRepID-main
|
algorithms/image_auto_encoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import math
import torch
import torch.utils.data as data_utils
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from sklearn.decomposition import FastICA
from sklearn.decomposition import PCA
from utils.metrics import *
from models.linear_auto_encoder import LinearAutoEncoder
#Base Class
from algorithms.base_auto_encoder import AE
path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)
from utils.helper import ValidationHelper
from utils.metrics import *
def IOSS(mu, n_draws=10000, robust_k_prop = 0.01, device= None):
stdmu = (mu - torch.min(mu,dim=0)[0])/ (torch.max(mu,dim=0)[0]-torch.min(mu,dim=0)[0])
K = np.int(robust_k_prop * mu.shape[0]) + 1
maxs = torch.topk(stdmu, K, dim=0)[0][-1,:]
mins = -(torch.topk(-stdmu, K, dim=0)[0][-1,:])
smps = (torch.stack([torch.rand(n_draws).to(device) * (maxs[i]-mins[i]) + mins[i] for i in range(stdmu.shape[1])], dim=1))
min_dist = (torch.min(torch.cdist(smps, stdmu.to(device)), dim=1)[0])
# ortho = (torch.mean(min_dist,dim=0))
ortho = (torch.topk(min_dist, np.int(robust_k_prop*n_draws)+1, dim=0))[0][-1]
# ortho = torch.max(min_dist,dim=0)[0]
return ortho
class AE_IOSS(AE):
def __init__(self, args, train_dataset, val_dataset, test_dataset, seed=0, device= None, base_algo= 'ae_poly'):
super().__init__(args, train_dataset, val_dataset, test_dataset, seed, device)
self.encoder= LinearAutoEncoder(self.args.latent_dim, self.args.latent_dim, batch_norm= 0).to(self.device)
self.decoder= LinearAutoEncoder(self.args.latent_dim, self.args.latent_dim, batch_norm= 0).to(self.device)
self.opt, self.scheduler= self.get_optimizer()
self.base_algo = base_algo
if self.args.intervention_case:
self.res_dir= 'results/ae-ioss/intervention/' + self.base_algo + '/' + str(self.args.save_dir) + 'seed_' + str(seed) + '/'
else:
self.res_dir= 'results/ae-ioss/observation/' + self.base_algo + '/' + str(self.args.save_dir) + 'seed_' + str(seed) + '/'
self.save_path= self.res_dir
def compute_loss(self, z_pred, x_pred, x):
loss= torch.mean(((x-x_pred)**2))
total_pairs= 3
lambda_reg= 10.0
ioss_penalty= torch.tensor(0.0).to(self.device)
for idx in range(total_pairs):
perm= torch.randperm(z_pred.shape[1])
ioss_penalty+= torch.mean(IOSS( z_pred[:, perm[:2]], device= self.device))
loss+= lambda_reg * ioss_penalty
return loss
|
CausalRepID-main
|
algorithms/ioss_auto_encoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import sys
import copy
import torch
import torchvision
import numpy as np
from sklearn.metrics import r2_score
from sklearn.linear_model import LinearRegression, Lasso, Ridge, LassoCV, RidgeCV
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPRegressor
from scipy.optimize import linear_sum_assignment
from sklearn.feature_selection import mutual_info_regression
import scipy
import matplotlib.pyplot as plt
from torchvision import transforms
def get_pca_sources(pred_z, pca_transform):
return { 'tr': pca_transform.transform(pred_z['tr']), 'te': pca_transform.transform(pred_z['te']) }
def get_ica_sources(pred_z, ica_transform):
return { 'tr': ica_transform.transform(pred_z['tr']), 'te': ica_transform.transform(pred_z['te']) }
def regression_approx(x, y, model, fit_intercept=False):
if model == 'lr':
reg= LinearRegression(fit_intercept= fit_intercept).fit(x, y)
elif model == 'lasso':
# reg= Lasso(alpha=0.001, fit_intercept= True).fit(x, y)
reg= LassoCV(fit_intercept=True, cv=3).fit(x, y)
elif model == 'ridge':
# reg= Ridge(alpha=0.001, fit_intercept= True).fit(x, y)
alphas_list = np.linspace(1e-2, 1e0, num=10).tolist()
alphas_list += np.linspace(1e0, 1e1, num=10).tolist()
reg= RidgeCV(fit_intercept= True, cv=3, alphas=alphas_list).fit(x, y)
elif model == 'mlp':
reg= MLPRegressor(random_state=1, max_iter= 1000).fit(x, y)
return reg
def get_predictions_check(train_dataset, test_dataset):
true_x={'tr':[], 'te':[]}
true_z= {'tr':[], 'te':[]}
data_case_list= ['train', 'test']
for data_case in data_case_list:
if data_case == 'train':
dataset= train_dataset
key='tr'
elif data_case == 'test':
dataset= test_dataset
key='te'
for batch_idx, (x, z, _) in enumerate(dataset):
with torch.no_grad():
true_x[key].append(x)
true_z[key].append(z)
true_x[key]= torch.cat(true_x[key]).detach().numpy()
true_z[key]= torch.cat(true_z[key]).detach().numpy()
return true_x, true_z
def get_predictions(encoder, decoder, train_dataset, val_dataset, test_dataset, device= None, save_dir='plots/', plot=True):
true_z= {'tr':[], 'val':[], 'te':[]}
pred_z= {'tr':[], 'val':[], 'te':[]}
true_y= {'tr':[], 'val':[], 'te':[]}
recon_loss= {'tr':0.0, 'val':0.0, 'te':0.0}
data_case_list= ['train', 'val', 'test']
for data_case in data_case_list:
if data_case == 'train':
dataset= train_dataset
key='tr'
elif data_case == 'val':
dataset= val_dataset
key='val'
elif data_case == 'test':
dataset= test_dataset
key='te'
count=0
for batch_idx, (x, z, y) in enumerate(dataset):
with torch.no_grad():
x= x.to(device)
pred= encoder(x)
x_pred= decoder(pred)
loss= torch.mean((x-x_pred)**2)
true_y[key].append(y)
true_z[key].append(z)
pred_z[key].append(pred)
recon_loss[key]+= loss.item()
count+=1
if plot and batch_idx == 0:
for idx in range(5):
transform = transforms.Compose(
[
transforms.ToPILImage(),
]
)
data= x[idx].cpu()
data= (data - data.min()) / (data.max() - data.min())
data= transform(data)
data.save( save_dir + 'real_image_' + str(idx) + '.jpg')
data= x_pred[idx].cpu()
data= (data - data.min()) / (data.max() - data.min())
data= transform(data)
data.save( save_dir + 'fake_image_' + str(idx) + '.jpg')
true_y[key]= torch.cat(true_y[key]).detach().numpy()
true_z[key]= torch.cat(true_z[key]).detach().numpy()
pred_z[key]= torch.cat(pred_z[key]).cpu().detach().numpy()
recon_loss[key]= recon_loss[key]/count
# print('Sanity Check: ')
# print( true_y['tr'].shape, pred_y['tr'].shape, true_z['tr'].shape, pred_z['tr'].shape )
# print( true_y['te'].shape, pred_y['te'].shape, true_z['te'].shape, pred_z['te'].shape )
return {'true_z': true_z, 'pred_z': pred_z, 'true_y': true_y, 'recon_loss': recon_loss}
def get_indirect_prediction_error(pred_latent, true_score, case='test', model='lr'):
if case == 'train':
key= 'tr'
elif case == 'test':
key= 'te'
reg= regression_approx(pred_latent['tr'], true_score['tr'], model, fit_intercept=True)
pred_score= reg.predict(pred_latent[key])
if len(pred_score.shape) == 1:
pred_score= np.reshape(pred_score, (pred_score.shape[0], 1))
rmse= np.sqrt(np.mean((true_score[key] - pred_score)**2))
r2= r2_score(true_score[key], pred_score)
# mat= reg.coef_
# _, sig_values ,_ = np.linalg.svd(mat)
# print(mat)
# print(np.mean(pred_latent['tr']), np.var(pred_latent['tr']))
# sys.exit()
return rmse, r2
def get_mi_score(pred_latent, true_latent, case='test'):
if case == 'train':
key= 'tr'
elif case == 'test':
key= 'te'
n= pred_latent[key].shape[0]
dim= pred_latent[key].shape[1]
mutual_info= 0.0
for i in range(dim):
for j in range(dim):
if i != j:
mutual_info+= mutual_info_regression( np.reshape( pred_latent[key][:, i], (n, 1) ), true_latent[key][:, j] )
print('Mutual Information')
print(mutual_info/(dim**2 - dim))
return
def get_independence_score(pred_latent, true_latent, case='test'):
if case == 'train':
key= 'tr'
elif case == 'test':
key= 'te'
dim= pred_latent[key].shape[1]
cross_corr= np.zeros((dim, dim))
for i in range(dim):
for j in range(dim):
cross_corr[i,j]= (np.cov( pred_latent[key][:,i], true_latent[key][:,j] )[0,1]) / ( np.std(pred_latent[key][:,i])*np.std(true_latent[key][:,j]) )
print('Independence Score')
print(cross_corr)
print(np.linalg.norm( cross_corr - np.eye(dim), ord='fro'))
return
def get_cross_correlation(pred_latent, true_latent, case='test', batch_size= 5000):
if case == 'train':
key= 'tr'
elif case == 'test':
key= 'te'
num_samples= pred_latent[key].shape[0]
dim= pred_latent[key].shape[1]
total_batches= int( num_samples / batch_size )
mcc_arr= []
for batch_idx in range(total_batches):
z_hat= copy.deepcopy( pred_latent[key][ (batch_idx)*batch_size : (batch_idx+1)*batch_size ] )
z= copy.deepcopy( true_latent[key][ (batch_idx)*batch_size : (batch_idx+1)*batch_size ] )
batch_idx += 1
cross_corr= np.zeros((dim, dim))
for i in range(dim):
for j in range(dim):
cross_corr[i,j]= (np.cov( z_hat[:,i], z[:,j] )[0,1]) / ( np.std(z_hat[:,i])*np.std(z[:,j]) )
# cross_corr= np.corrcoef(pred_latent[key], true_latent[key], rowvar=False)[dim:, :dim]
cost= -1*np.abs(cross_corr)
row_ind, col_ind= linear_sum_assignment(cost)
score= 100*( -1*cost[row_ind, col_ind].sum() )/(dim)
print(-100*cost[row_ind, col_ind])
# score= 100*np.sum( -1*cost[row_ind, col_ind] > 0.80 )/(dim)
mcc_arr.append(score)
return mcc_arr
def intervene_metric(pred_latent, true_latent, model='lr', model_train=1, list_models=None, hard_intervention_val= 2.0):
'''
pred_latent: Output representation from stage 1
true_score: intervened latent true value
'''
latent_dim= true_latent['tr'].shape[1]
if model_train:
res={}
for intervene_idx in range(latent_dim):
indices= true_latent['tr'][:, intervene_idx] == hard_intervention_val
curr_pred_latent_subset= pred_latent['tr'][indices]
intervene_targets= 10* np.ones( curr_pred_latent_subset.shape[0] )
reg= regression_approx(curr_pred_latent_subset, intervene_targets, model, fit_intercept=False)
res[intervene_idx]= reg
return res
else:
num_samples= true_latent['te'].shape[0]
res= np.zeros((num_samples, latent_dim))
for intervene_idx in range(latent_dim):
res[:, intervene_idx]= list_models[intervene_idx].predict(pred_latent['te'])
return {'te': res}
def intervene_metric_image(pred_latent, true_latent, intervention_meta_data, model='lr', model_train=1, list_models=None):
'''
pred_latent: Output representation from stage 1
true_score: intervened latent true value
'''
if model_train:
res={}
intervened_latents_set= np.unique( intervention_meta_data['tr'][:, 0] )
print(intervened_latents_set)
for intervene_idx in intervened_latents_set:
print(intervene_idx)
indices= intervention_meta_data['tr'][:, 0] == intervene_idx
curr_pred_latent_subset= pred_latent['tr'][indices]
intervene_targets= 10* intervention_meta_data['tr'][indices, 1]
print(np.unique(intervene_targets))
reg= regression_approx(curr_pred_latent_subset, intervene_targets, model, fit_intercept=False)
res[intervene_idx]= reg
return res
else:
intervened_latents_set= list(list_models.keys())
num_samples= true_latent['te'].shape[0]
eff_latent_dim= len(intervened_latents_set)
z= np.zeros((num_samples, eff_latent_dim))
z_hat= np.zeros((num_samples, eff_latent_dim))
for idx in range(eff_latent_dim):
intervene_idx= intervened_latents_set[idx]
z[:, idx]= true_latent['te'][:, int(intervene_idx)]
z_hat[:, idx]= list_models[intervene_idx].predict(pred_latent['te'])
print('Transformed Latents using Itv Dataset', z_hat.shape, z.shape)
return {'te':z_hat}, {'te': z}
# DCI Score
def compute_importance_matrix(z_pred, z, case= 'disentanglement', fit_intercept= True):
true_latent_dim= z.shape[1]
pred_latent_dim= z_pred.shape[1]
imp_matrix= np.zeros((pred_latent_dim, true_latent_dim))
for idx in range(true_latent_dim):
model= LinearRegression(fit_intercept= fit_intercept).fit(z_pred, z[:, idx])
# model= LassoCV(fit_intercept=True, cv=3).fit(z_pred, z[:, idx])
imp_matrix[:, idx]= model.coef_
# Taking the absolute value for weights to encode relative importance properly
imp_matrix= np.abs(imp_matrix)
if case == 'disetanglement':
imp_matrix= imp_matrix / np.reshape( np.sum(imp_matrix, axis=1), (pred_latent_dim, 1) )
elif case == 'completeness':
imp_matrix= imp_matrix / np.reshape( np.sum(imp_matrix, axis=0), (1, true_latent_dim) )
return imp_matrix
def disentanglement_per_code(importance_matrix):
"""Compute disentanglement score of each code."""
# importance_matrix is of shape [num_codes, num_factors].
return 1. - scipy.stats.entropy(importance_matrix.T + 1e-11,
base=importance_matrix.shape[1])
def disentanglement(importance_matrix):
"""Compute the disentanglement score of the representation."""
per_code = disentanglement_per_code(importance_matrix)
if importance_matrix.sum() == 0.:
importance_matrix = np.ones_like(importance_matrix)
code_importance = importance_matrix.sum(axis=1) / importance_matrix.sum()
return np.sum(per_code*code_importance)
def completeness_per_factor(importance_matrix):
"""Compute completeness of each factor."""
# importance_matrix is of shape [num_codes, num_factors].
return 1. - scipy.stats.entropy(importance_matrix + 1e-11,
base=importance_matrix.shape[0])
def completeness(importance_matrix):
""""Compute completeness of the representation."""
per_factor = completeness_per_factor(importance_matrix)
if importance_matrix.sum() == 0.:
importance_matrix = np.ones_like(importance_matrix)
factor_importance = importance_matrix.sum(axis=0) / importance_matrix.sum()
return np.sum(per_factor*factor_importance)
|
CausalRepID-main
|
utils/metrics.py
|
CausalRepID-main
|
utils/__init__.py
|
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import numpy as np
import torch
import torch.utils.data as data_utils
path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)
from data.data_loader import BaseDataLoader
from data.fine_tune_loader import FineTuneDataLoader
from data.balls_dataset_loader import BallsDataLoader
class ValidationHelper:
def __init__(self, patience=10, min_delta=1e-4):
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.min_validation_loss = np.inf
self.best_epoch = -1
def save_model(self, validation_loss, epoch):
if validation_loss < (self.min_validation_loss + self.min_delta):
self.min_validation_loss = validation_loss
self.best_epoch = epoch
self.counter= 0
return True
return False
def early_stop(self, validation_loss):
if validation_loss > (self.min_validation_loss + self.min_delta):
self.counter += 1
if self.counter >= self.patience:
return True
return False
def sample_base_data_loaders(data_dir, batch_size, observation_case= 1, intervention_case= 0, latent_case='', seed=0, kwargs={}):
if 'balls' in latent_case:
data_obj= BallsDataLoader(data_dir= data_dir, data_case='train', observation_case = observation_case, intervention_case= intervention_case)
train_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
data_obj= BallsDataLoader(data_dir= data_dir, data_case='val', observation_case = observation_case, intervention_case= intervention_case)
val_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
data_obj= BallsDataLoader(data_dir= data_dir, data_case='test', observation_case = observation_case, intervention_case= intervention_case)
test_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
else:
data_obj= BaseDataLoader(data_dir= data_dir, data_case='train', seed= seed, observation_case = observation_case, intervention_case= intervention_case)
train_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
data_obj= BaseDataLoader(data_dir= data_dir, data_case='val', seed= seed, observation_case = observation_case, intervention_case= intervention_case)
val_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
data_obj= BaseDataLoader(data_dir= data_dir, data_case='test', seed= seed, observation_case = observation_case, intervention_case= intervention_case)
test_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
return train_dataset, val_dataset, test_dataset
def sample_finetune_data_loaders(pred_z, true_z, data_dir, batch_size, kwargs= {}):
data_obj= FineTuneDataLoader(pred_z, true_z, data_dir= data_dir, data_case='train')
train_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
data_obj= FineTuneDataLoader(pred_z, true_z, data_dir= data_dir, data_case='val')
val_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
data_obj= FineTuneDataLoader(pred_z, true_z, data_dir= data_dir, data_case='test')
test_dataset= data_utils.DataLoader(data_obj, batch_size=batch_size, shuffle=True, **kwargs )
return train_dataset, val_dataset, test_dataset
|
CausalRepID-main
|
utils/helper.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
from torch import nn
class LinearAutoEncoder(torch.nn.Module):
def __init__(self, data_dim, latent_dim, batch_norm= False):
super(LinearAutoEncoder, self).__init__()
self.data_dim = data_dim
self.latent_dim = latent_dim
if batch_norm:
self.net= nn.Sequential(
nn.BatchNorm1d(self.data_dim),
nn.Linear(self.data_dim, self.latent_dim),
)
else:
self.net= nn.Sequential(
nn.Linear(self.data_dim, self.latent_dim),
)
def forward(self, x):
return self.net(x)
|
CausalRepID-main
|
models/linear_auto_encoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from torchvision.models.resnet import ResNet, BasicBlock
class Decoder(torch.nn.Module):
def __init__(self, data_dim, latent_dim):
super(Decoder, self).__init__()
self.data_dim = data_dim
self.latent_dim = latent_dim
self.hidden_dim= 200
self.net= nn.Sequential(
nn.Linear(self.latent_dim, self.hidden_dim),
nn.LeakyReLU(0.5),
nn.Linear(self.hidden_dim, self.hidden_dim),
nn.LeakyReLU(0.5),
nn.Linear(self.hidden_dim, self.data_dim),
)
def forward(self, z):
return self.net(z)
|
CausalRepID-main
|
models/decoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from torchvision.models.resnet import ResNet, BasicBlock
import sys
'''
Refernce for DeConv Blocks: https://github.com/julianstastny/VAE-ResNet18-PyTorch/blob/master/model.py
'''
class ResizeConv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, scale_factor, mode='nearest'):
super().__init__()
self.scale_factor = scale_factor
self.mode = mode
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=1)
def forward(self, x):
x = F.interpolate(x, scale_factor=self.scale_factor, mode=self.mode)
x = self.conv(x)
return x
class BasicBlockDec(nn.Module):
def __init__(self, in_planes, stride=1):
super().__init__()
planes = int(in_planes/stride)
self.conv2 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(in_planes)
# self.bn1 could have been placed here, but that messes up the order of the layers when printing the class
if stride == 1:
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
else:
self.conv1 = ResizeConv2d(in_planes, planes, kernel_size=3, scale_factor=stride)
self.bn1 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential(
ResizeConv2d(in_planes, planes, kernel_size=3, scale_factor=stride),
nn.BatchNorm2d(planes)
)
def forward(self, x):
out = torch.relu(self.bn2(self.conv2(x)))
out = self.bn1(self.conv1(out))
out += self.shortcut(x)
out = torch.relu(out)
return out
class ImageDecoder(torch.nn.Module):
def __init__(self, latent_dim):
super(ImageDecoder, self).__init__()
self.latent_dim = latent_dim
self.width= 128
self.num_Blocks=[2,2,2,2]
self.in_planes = 512
self.nc= 3
self.linear = [
nn.Linear(self.latent_dim, self.width),
nn.LeakyReLU(),
nn.Linear(self.width, 512),
]
self.linear= nn.Sequential(*self.linear)
self.layer4 = self._make_layer(BasicBlockDec, 256, self.num_Blocks[3], stride=2)
self.layer3 = self._make_layer(BasicBlockDec, 128, self.num_Blocks[2], stride=2)
self.layer2 = self._make_layer(BasicBlockDec, 64, self.num_Blocks[1], stride=2)
self.layer1 = self._make_layer(BasicBlockDec, 64, self.num_Blocks[0], stride=1)
self.conv1 = ResizeConv2d(64, self.nc, kernel_size=3, scale_factor=2)
def _make_layer(self, BasicBlockDec, planes, num_Blocks, stride):
strides = [stride] + [1]*(num_Blocks-1)
layers = []
for stride in reversed(strides):
layers += [BasicBlockDec(self.in_planes, stride)]
self.in_planes = planes
return nn.Sequential(*layers)
def forward(self, z):
x = self.linear(z)
x = x.view(z.size(0), 512, 1, 1)
x = F.interpolate(x, scale_factor=4)
x = self.layer4(x)
x = self.layer3(x)
x = self.layer2(x)
x = self.layer1(x)
x = self.conv1(x)
return x
|
CausalRepID-main
|
models/image_resnet_decoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from torchvision.models.resnet import ResNet, BasicBlock
class PolyDecoder(torch.nn.Module):
def __init__(self, data_dim, latent_dim, poly_degree, device):
super(PolyDecoder, self).__init__()
self.data_dim = data_dim
self.latent_dim = latent_dim
self.poly_degree = poly_degree
self.device = device
self.total_poly_terms= self.compute_total_polynomial_terms()
self.coff_matrix= nn.Sequential(
nn.Linear(self.total_poly_terms, self.data_dim),
)
def forward(self, z):
x=[]
for idx in range(z.shape[0]):
x.append( self.compute_decoder_polynomial(z[idx, :]))
x= torch.cat(x, dim=0)
x= self.coff_matrix(x)
return x
def compute_total_polynomial_terms(self):
count=0
for degree in range(self.poly_degree + 1):
count+= pow(self.latent_dim, degree)
return count
def compute_kronecker_product(self, degree, latent):
if degree ==0:
out= torch.tensor([1]).to(self.device)
else:
out=torch.clone(latent)
for idx in range(1, degree):
out= torch.kron(out, latent)
return out
def compute_decoder_polynomial(self, latent):
out=[]
for degree in range(self.poly_degree + 1):
# print('Computing polynomial term of degree ', degree)
out.append(self.compute_kronecker_product(degree, latent))
out= torch.cat(out)
out= out.view((1,out.shape[0]))
return out
|
CausalRepID-main
|
models/poly_decoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
from torch import nn
class Encoder(torch.nn.Module):
def __init__(self, data_dim, latent_dim):
super(Encoder, self).__init__()
self.data_dim = data_dim
self.latent_dim = latent_dim
self.hidden_dim= 100
self.net= nn.Sequential(
nn.Linear(self.data_dim, self.hidden_dim),
nn.LeakyReLU(0.5),
nn.Linear(self.hidden_dim, self.hidden_dim),
nn.LeakyReLU(0.5),
nn.Linear(self.hidden_dim, self.latent_dim),
)
def forward(self, x):
return self.net(x)
|
CausalRepID-main
|
models/encoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from torchvision.models.resnet import ResNet, BasicBlock
class ImageDecoder(torch.nn.Module):
def __init__(self, latent_dim):
super(ImageDecoder, self).__init__()
self.latent_dim = latent_dim
self.width= 128
self.nc= 3
self.linear = [
nn.Linear(self.latent_dim, self.width),
nn.LeakyReLU(),
nn.Linear(self.width, 1024),
nn.LeakyReLU(),
]
self.linear= nn.Sequential(*self.linear)
self.conv= [
nn.ConvTranspose2d(64, 64, 4, stride=2, padding=1),
nn.LeakyReLU(),
nn.ConvTranspose2d(64, 32, 4, stride=2, padding=1),
nn.LeakyReLU(),
nn.ConvTranspose2d(32, 32, 4, stride=2, padding=1),
nn.LeakyReLU(),
nn.ConvTranspose2d(32, self.nc, 4, stride=2, padding=1),
]
self.conv= nn.Sequential(*self.conv)
def forward(self, z):
x = self.linear(z)
x = x.view(z.size(0), 64, 4, 4)
x = self.conv(x)
return x
|
CausalRepID-main
|
models/image_decoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
from torch import nn
from torchvision import models as vision_models
from torchvision.models import resnet18, resnet50
from torchvision import transforms
class ImageEncoder(torch.nn.Module):
def __init__(self, latent_dim):
super(ImageEncoder, self).__init__()
self.latent_dim = latent_dim
self.base_architecture= 'resnet18'
self.width = 128
self.base_model = resnet18(pretrained=True)
self.feat_layers= list(self.base_model.children())[:-1]
self.feat_net= nn.Sequential(*self.feat_layers)
self.fc_layers= [
nn.Linear(512, self.width),
nn.LeakyReLU(),
nn.Linear(self.width, self.latent_dim),
]
self.fc_net = nn.Sequential(*self.fc_layers)
def forward(self, x):
x= self.feat_net(x)
x= x.view(x.shape[0], x.shape[1])
x= self.fc_net(x)
return x
|
CausalRepID-main
|
models/image_encoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from torchvision.models.resnet import ResNet, BasicBlock
def build_grid(resolution):
ranges = [np.linspace(0., 1., num=res) for res in resolution]
grid = np.meshgrid(*ranges, sparse=False, indexing="ij")
grid = np.stack(grid, axis=-1)
grid = np.reshape(grid, [resolution[0], resolution[1], -1])
grid = np.expand_dims(grid, axis=0)
grid = grid.astype(np.float32)
return torch.from_numpy(np.concatenate([grid, 1.0 - grid], axis=-1))
"""Adds soft positional embedding with learnable projection."""
class SoftPositionEmbed(nn.Module):
def __init__(self, hidden_size, resolution):
"""Builds the soft position embedding layer.
Args:
hidden_size: Size of input feature dimension.
resolution: Tuple of integers specifying width and height of grid.
"""
super().__init__()
self.embedding = nn.Linear(4, hidden_size, bias=True)
self.grid = build_grid(resolution)
def forward(self, inputs):
grid = self.embedding(self.grid)
return inputs + grid
class ImageDecoder(torch.nn.Module):
def __init__(self, latent_dim):
super(ImageDecoder, self).__init__()
self.latent_dim = latent_dim
self.width= 128
self.nc= 3
self.linear = [
nn.Linear(self.latent_dim, self.width),
nn.LeakyReLU(),
nn.Linear(self.width, 1024),
nn.LeakyReLU(),
]
self.linear= nn.Sequential(*self.linear)
self.decoder_initial_size = (8, 8)
self.decoder_pos = SoftPositionEmbed(1024, self.decoder_initial_size)
self.conv1 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv2 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv3 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv4 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv5 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(1, 1), padding=2).to(device)
self.conv6 = nn.ConvTranspose2d(hid_dim, 4, 3, stride=(1, 1), padding=1)
self.resolution = resolution
def forward(self, z):
x = self.linear(z)
x = self.decoder_pos(x)
x = x.view(z.size(0), 64, 4, 4)
return x
|
CausalRepID-main
|
models/image_slot_attention_decoder.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import numpy as np
import pandas as pd
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--case', type=str, default='log',
help= 'test; log; debug')
parser.add_argument('--target_latent', type=str, default='balls_iid_none',
help= '')
parser.add_argument('--eval_ioss_transformation', type=int, default=0,
help='Evaluate the IOSS transformation from the base model representation')
parser.add_argument('--eval_intervene_transformation', type=int, default=1,
help='Evaluate the Intervention transformation from the base model representation')
args = parser.parse_args()
case= args.case
target_latent= args.target_latent
eval_ioss_transformation= args.eval_ioss_transformation
eval_intervene_transformation= args.eval_intervene_transformation
latent_case_grid= ['balls_iid_none', 'balls_scm_linear', 'balls_scm_non_linear']
interventions_per_latent_grid= [1, 3, 5, 7, 9]
method_type= 'ae_image'
total_seeds= 5
latent_dim = 25
lr= 5e-4
intervention_case= 0
batch_size= 64
cuda_device= 0
#Test Models
if args.case == 'test':
for latent_case in latent_case_grid:
if latent_case != target_latent:
continue
for total_interventions in interventions_per_latent_grid:
latent= latent_case.split('_')[1]
mechanism= latent_case.split('_')[2]
if mechanism == 'non':
mechanism= 'non_linear'
#Data Script
script= 'python data/balls_dataset.py --distribution_case intervention ' + ' --latent_case ' + str(latent) + ' --scm_mechanism ' + str(mechanism) + ' --interventions_per_latent ' + str(total_interventions)
print('Data Case: ', script)
os.system(script)
#Eval Script
fname= 'latent_case_' + str(latent_case) + '_itv_per_latent_' + str(total_interventions) + '_latent_dim_' + str(latent_dim) + '_lr_' + str(lr) + '_method_type_' + str(method_type) + '.txt'
print(fname)
script= 'python test.py ' + ' --latent_case ' + str(latent_case) + ' --latent_dim ' + str(latent_dim) + ' --intervention_case ' + str(intervention_case) + ' --lr ' + str(lr) + ' --batch_size ' + str(batch_size) + ' --method_type ' + str(method_type) + ' --num_seeds ' + str(total_seeds) + ' --cuda_device ' + str(cuda_device) + ' --eval_ioss_transformation ' + str(eval_ioss_transformation) + ' --eval_intervene_transformation ' + str(eval_intervene_transformation) + ' > ' + 'results/final_logs/balls/' + fname
os.system(script)
#Log Results
latent_name_map= {'balls_iid_none': 'Uniform', 'balls_scm_linear': 'SCM (linear)', 'balls_scm_non_linear': 'SCM (non-linear)'}
if args.case == 'log':
meta_res={}
for lr in [0.0005]:
res={'Latent Case': [], 'Total Interventions': [], 'Recon-RMSE':[], 'R2':[], 'MCC-Tune': []}
for latent_case in latent_case_grid:
for total_interventions in interventions_per_latent_grid:
fname= 'latent_case_' + str(latent_case) + '_itv_per_latent_' + str(total_interventions) + '_latent_dim_' + str(latent_dim) + '_lr_' + str(lr) + '_method_type_' + str(method_type) + '.txt'
data= open( 'results/final_logs/balls/' + fname, 'r').readlines()
for line in data:
line= line.replace('\n','')
if 'Metric' in line:
if 'recon_rmse' in line:
key= 'Recon-RMSE'
elif 'latent_pred_r2' in line:
key= 'R2'
elif 'mcc_tune' in line:
key= 'MCC-Tune'
else:
continue
mean= round( float(line.split(" ")[-2]), 2 )
var= round( float(line.split(" ")[-1]), 2 )
val= str(mean) + ' \u00B1 ' + str(var)
# val= str(mean) + ' ( ' + str(var) + ' ) '
res[key].append(val)
res['Latent Case'].append( latent_name_map[latent_case] )
res['Total Interventions'].append(total_interventions)
meta_res[lr]= res
final_res={'Latent Case': [], 'Total Interventions': [], 'LR': [], 'Recon-RMSE':[], 'MCC-Tune': []}
final_res= meta_res[0.0005]
for key in final_res.keys():
print(key, len(final_res[key]))
df= pd.DataFrame(final_res)
# df= df.drop(columns= ['Recon-RMSE'])
print(df.to_latex(index=False))
|
CausalRepID-main
|
scripts/main_exps_images.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import numpy as np
import scipy
import copy
from sklearn.linear_model import LinearRegression, Lasso, Ridge, LassoCV, RidgeCV
'''
z: True Latent (dataset_size, latent_dim) (.npy file)
Pred_z: Inferred Latent (dataset_size, latent_dim) (.npy file)
'''
# DCI Score
def compute_importance_matrix(z_pred, z, case= 'disentanglement', fit_intercept= True):
true_latent_dim= z.shape[1]
pred_latent_dim= z_pred.shape[1]
imp_matrix= np.zeros((pred_latent_dim, true_latent_dim))
for idx in range(true_latent_dim):
model= LinearRegression(fit_intercept= fit_intercept).fit(z_pred, z[:, idx])
# model= LassoCV(fit_intercept=True, cv=3).fit(z_pred, z[:, idx])
imp_matrix[:, idx]= model.coef_
# Taking the absolute value for weights to encode relative importance properly
imp_matrix= np.abs(imp_matrix)
if case == 'disetanglement':
imp_matrix= imp_matrix / np.reshape( np.sum(imp_matrix, axis=1), (pred_latent_dim, 1) )
elif case == 'completeness':
imp_matrix= imp_matrix / np.reshape( np.sum(imp_matrix, axis=0), (1, true_latent_dim) )
return imp_matrix
def disentanglement_per_code(importance_matrix):
"""Compute disentanglement score of each code."""
# importance_matrix is of shape [num_codes, num_factors].
return 1. - scipy.stats.entropy(importance_matrix.T + 1e-11,
base=importance_matrix.shape[1])
def disentanglement(importance_matrix):
"""Compute the disentanglement score of the representation."""
per_code = disentanglement_per_code(importance_matrix)
if importance_matrix.sum() == 0.:
importance_matrix = np.ones_like(importance_matrix)
code_importance = importance_matrix.sum(axis=1) / importance_matrix.sum()
return np.sum(per_code*code_importance)
def completeness_per_factor(importance_matrix):
"""Compute completeness of each factor."""
# importance_matrix is of shape [num_codes, num_factors].
return 1. - scipy.stats.entropy(importance_matrix + 1e-11,
base=importance_matrix.shape[0])
def completeness(importance_matrix):
""""Compute completeness of the representation."""
per_factor = completeness_per_factor(importance_matrix)
if importance_matrix.sum() == 0.:
importance_matrix = np.ones_like(importance_matrix)
factor_importance = importance_matrix.sum(axis=0) / importance_matrix.sum()
return np.sum(per_factor*factor_importance)
# Test Cases
# Case 1: True latent same as the predicted latent
list_matrices= [ np.eye(10), np.random.random((10, 10)) ]
for matrix in list_matrices:
true_z= np.random.random((1000, 10))
pred_z= np.matmul(true_z, matrix)
imp_matrix= compute_importance_matrix(pred_z, true_z, case='disentanglement')
score= disentanglement(imp_matrix)
print('Disentanglement', score)
imp_matrix= compute_importance_matrix(pred_z, true_z, case='completeness')
score= completeness(imp_matrix)
print('Completeness', score)
# Permutation Case
pred_z= copy.deepcopy(true_z)
perm= np.random.permutation(10)
pred_z= pred_z[:, perm]
imp_matrix= compute_importance_matrix(pred_z, true_z, case='disentanglement')
score= disentanglement(imp_matrix)
print('Disentanglement', score)
imp_matrix= compute_importance_matrix(pred_z, true_z, case='completeness')
score= completeness(imp_matrix)
print('Completeness', score)
|
CausalRepID-main
|
scripts/test_metrics.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import numpy as np
import pandas as pd
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--case', type=str, default='log',
help= 'train; test; log; debug')
parser.add_argument('--intervention_case', type=int, default=0,
help='')
parser.add_argument('--target_latent', type=str, default='uniform',
help= '')
parser.add_argument('--target_dim', type=int, default=6,
help='')
parser.add_argument('--target_degree', type=int, default=2,
help='')
parser.add_argument('--method_type', type=str, default='ae_poly',
help= 'ae, ae_poly')
parser.add_argument('--batch_size', type=int, default= 16,
help='')
parser.add_argument('--lr', type=float, default= 1e-3,
help='')
parser.add_argument('--train_base_model', type=int, default=1,
help='Train the base auto encoder')
parser.add_argument('--train_ioss_transformation', type=int, default=0,
help='Learn the IOSS transformation from the base model representations')
parser.add_argument('--eval_ioss_transformation', type=int, default=0,
help='Evaluate the IOSS transformation from the base model representation')
parser.add_argument('--eval_intervene_transformation', type=int, default=0,
help='Evaluate the Intervention transformation from the base model representation')
parser.add_argument('--eval_dgp', type=int, default= 1,
help= 'Evaluate the function from z -> x and x -> z in the true DGP')
parser.add_argument('--cuda_device', type=int, default=-1,
help='Select the cuda device by id among the avaliable devices' )
args = parser.parse_args()
intervention_case= args.intervention_case
batch_size= args.batch_size
lr= args.lr
method_type= args.method_type
target_latent= args.target_latent
target_dim= args.target_dim
target_degree= args.target_degree
train_base_model= args.train_base_model
train_ioss_transformation= args.train_ioss_transformation
eval_ioss_transformation= args.eval_ioss_transformation
eval_intervene_transformation= args.eval_intervene_transformation
eval_dgp= args.eval_dgp
cuda_device= args.cuda_device
# latent_case_grid= ['uniform', 'uniform_corr']
# latent_case_grid= ['gaussian_mixture', 'scm_sparse', 'scm_dense']
latent_case_grid= ['uniform', 'uniform_corr', 'gaussian_mixture', 'scm_sparse', 'scm_dense']
# latent_case_grid= ['scm_sparse', 'scm_dense']
poly_degree_grid= [2, 3]
latent_dim_grid=[6, 10]
total_seeds= 5
data_dim = 200
#Sanity Checks
if args.case == 'debug':
for latent_case in latent_case_grid:
for latent_dim in latent_dim_grid:
for poly_degree in poly_degree_grid:
for seed in range(total_seeds):
curr_dir= 'results/ae-ioss/ae_poly/' + 'polynomial' + '_latent_' + latent_case + '_poly_degree_' + str(poly_degree) + '_data_dim_' + str(data_dim) + '_latent_dim_' + str(latent_dim) + '/' + 'seed_' + str(seed) + '/'
# curr_dir= 'results/ae-poly/' + 'polynomial' + '_latent_' + latent_case + '_poly_degree_' + str(poly_degree) + '_data_dim_' + str(data_dim) + '_latent_dim_' + str(latent_dim) + '/' + 'seed_' + str(seed) + '/'
count=0
for _, _, f_list in os.walk(curr_dir):
for fname in f_list:
if '.pth' in fname:
count+=1
if count!=6:
print('Error: ', latent_case, latent_dim, poly_degree, seed, count)
#Generate Datasets
if args.case == 'data':
for latent_case in latent_case_grid:
if latent_case != target_latent:
continue
for latent_dim in latent_dim_grid:
for poly_degree in poly_degree_grid:
for seed in range(total_seeds):
script= 'python data/synthetic_polynomial_dgp.py ' + ' --latent_case ' + str(latent_case) + ' --latent_dim ' + str(latent_dim) + ' --poly_degree ' + str(poly_degree) + ' --seed ' + str(seed)
os.system(script)
#Train Models
if args.case == 'train':
train_ioss_transformation= 1 - intervention_case
for latent_case in latent_case_grid:
if latent_case != target_latent:
continue
for latent_dim in latent_dim_grid:
if latent_dim != target_dim:
continue
for poly_degree in poly_degree_grid:
if poly_degree != target_degree:
continue
for seed in range(total_seeds):
script= 'python train.py ' + ' --latent_case ' + str(latent_case) + ' --latent_dim ' + str(latent_dim) + ' --poly_degree ' + str(poly_degree) + ' --seed ' + str(seed) + ' --intervention_case ' + str(intervention_case) + ' --lr ' + str(lr) + ' --batch_size ' + str(batch_size) + ' --method_type ' + str(method_type) + ' --cuda_device ' + str(cuda_device) + ' --train_base_model ' + str(train_base_model) + ' --train_ioss_transformation ' + str(train_ioss_transformation)
os.system(script)
#Test Models
if args.case == 'test':
eval_ioss_transformation= 1 - intervention_case
eval_intervene_transformation= intervention_case
for latent_case in latent_case_grid:
for latent_dim in latent_dim_grid:
for poly_degree in poly_degree_grid:
fname= 'latent_case_' + str(latent_case) + '_latent_dim_' + str(latent_dim) + '_poly_degree_' + str(poly_degree) + '_intervention_' + str(intervention_case) + '_lr_' + str(lr) + '_method_type_' + str(method_type) + '.txt'
print(fname)
script= 'python test.py ' + ' --latent_case ' + str(latent_case) + ' --latent_dim ' + str(latent_dim) + ' --poly_degree ' + str(poly_degree) + ' --intervention_case ' + str(intervention_case) + ' --lr ' + str(lr) + ' --batch_size ' + str(batch_size) + ' --method_type ' + str(method_type) + ' --eval_ioss_transformation ' + str(eval_ioss_transformation) + ' --eval_intervene_transformation ' + str(eval_intervene_transformation) + ' --eval_dgp ' + str(eval_dgp) + ' > ' + 'results/final_logs/'+ fname
os.system(script)
#Log Results
latent_name_map= {'uniform': 'Uniform', 'uniform_corr': 'Uniform-C', 'gaussian_mixture': 'Gaussian-Mixture', 'scm_sparse': 'SCM-S', 'scm_dense': 'SCM-D'}
if args.case == 'log':
meta_res={}
for lr in [0.001, 0.0005, 0.0001]:
res={'Latent Case': [], 'Latent Dim': [], 'Poly Degree': [], 'Recon Error':[], 'RMSE': [], 'R2': [], 'Debug-R2' : [], 'Oracle-R2': [], 'MCC': [], 'MCC-Tune': []}
for latent_case in latent_case_grid:
for latent_dim in latent_dim_grid:
for poly_degree in poly_degree_grid:
fname= 'latent_case_' + str(latent_case) + '_latent_dim_' + str(latent_dim) + '_poly_degree_' + str(poly_degree) + '_intervention_' + str(intervention_case) + '_lr_' + str(lr) + '_method_type_' + str(method_type) + '.txt'
data= open( 'results/final_logs/' + fname, 'r').readlines()
for line in data:
line= line.replace('\n','')
if 'Metric' in line:
if 'recon_rmse' in line:
key= 'Recon Error'
elif 'latent_pred_rmse' in line:
key= 'RMSE'
elif 'latent_pred_r2' in line:
key= 'R2'
elif 'mcc ' in line:
key= 'MCC'
elif 'mcc_tune' in line:
key= 'MCC-Tune'
elif 'debug_pred_r2' in line:
key= 'Debug-R2'
elif 'oracle_pred_r2' in line:
key= 'Oracle-R2'
else:
continue
mean= round( float(line.split(" ")[-2]), 2 )
var= round( float(line.split(" ")[-1]), 2 )
# val= str(mean) + ' ( ' + str(var) + ' ) '
val= str(mean) + ' \u00B1 ' + str(var)
res[key].append(val)
res['Latent Case'].append( latent_name_map[latent_case] )
res['Latent Dim'].append(latent_dim)
res['Poly Degree'].append(poly_degree)
meta_res[lr]= res
final_res={'Latent Case': [], 'Latent Dim': [], 'Poly Degree': [], 'LR': [], 'Recon Error':[], 'RMSE': [], 'R2': [], 'Debug-R2' : [], 'Oracle-R2': [], 'MCC': [], 'MCC-Tune': []}
total_size= len(res['Recon Error'])
for idx in range(total_size):
opt_val= np.inf
opt_lr= -1
for lr in [0.001, 0.0005, 0.0001]:
curr_val= float(meta_res[lr]['Recon Error'][idx].split(' ')[0])
if opt_val > curr_val:
opt_val = curr_val
opt_lr= lr
final_res['LR'].append(opt_lr)
for key in res.keys():
final_res[key].append( meta_res[opt_lr][key][idx] )
for key in final_res.keys():
print(key, len(final_res[key]))
df= pd.DataFrame(final_res)
df= df.drop(columns= ['RMSE', 'Debug-R2', 'Oracle-R2', 'LR'])
# df= df.drop(columns= ['RMSE', 'Debug-R2', 'Oracle-R2', 'LR', 'R2', 'Recon Error'])
print(df.to_latex(index=False))
|
CausalRepID-main
|
scripts/main_exps.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import random
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import os
import sys
path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)
from data.dag_generator import DagGenerator
random.seed(10)
np.random.seed(10)
dag= DagGenerator('linear', cause='gaussian', nodes=10, expected_density= 0.5, npoints= 5000)
print(dir(dag))
df1, obj1= dag.generate()
z= df1.values
print('Observational Data')
print(z)
# print(np.cov(np.transpose(z)))
# print(z.shape)
# print(df1)
# print(df1.values.shape)
nx.draw_networkx(obj1, arrows=True)
plt.savefig('a.jpg')
plt.clf()
# sys.exit()
print('Adjanceny Matrix')
print(nx.adjacency_matrix(obj1))
print('Degree Values')
print(type(obj1.degree()))
# '''
# Calling dag.generate second time does not reinitialise the DAG structure but samples new points; so simply call dag.generate() with different seed values should give the train/val/test split.
# '''
# df2, obj2= dag.generate()
# print(df2)
# # nx.draw_networkx(obj2, arrows=True)
# # plt.savefig('b.jpg')
# # plt.clf()
#Checking for intervention
df3, obj3= dag.intervene(intervention_nodes= [9], target_distribution= 'hard_intervention')
print('Interventional Matrix')
print(df3)
# nx.draw_networkx(obj3, arrows=True)
# plt.savefig('c.jpg')
# plt.clf()
|
CausalRepID-main
|
scripts/dag_gen_debug.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import math
import argparse
import numpy as np
import time
## Imports for plotting
import matplotlib.pyplot as plt
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg', 'pdf') # For export
from matplotlib.colors import to_rgba
import seaborn as sns
sns.set()
from tqdm.notebook import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
from sklearn.linear_model import LogisticRegression
import torch.utils.data as data
from scipy.stats import ortho_group
from utils.metrics import get_cross_correlation
from scipy.stats import bernoulli
def get_predictions(model, train_dataset, device= None):
true_z= []
pred_z= []
count= 0
for batch_idx, (data_inputs, data_latents), in enumerate(train_dataset):
with torch.no_grad():
data_inputs = data_inputs.to(device)
preds = model(data_inputs)
true_z.append(data_latents)
pred_z.append(preds)
count+=1
true_z= torch.cat(true_z).detach().numpy()
pred_z= torch.cat(pred_z).cpu().detach().numpy()
return true_z, pred_z
class LinearEncoder(nn.Module):
def __init__(self, num_inputs, num_outputs):
super().__init__()
self.linear = nn.Linear(num_inputs, num_outputs)
def forward(self, x):
x = self.linear(x)
return x
class data_class_loader(data.Dataset):
def __init__(self, dataset):
super().__init__()
self.dataset_generate(dataset)
self.size = dataset[0].size()[0]
def dataset_generate(self, dataset):
self.data = dataset[0]
self.latent = dataset[1]
self.label = dataset[2].T[0].to(int)
def __len__(self):
return self.size
def __getitem__(self, idx):
data_point = self.data[idx]
data_latent = self.latent[idx]
data_label = self.label[idx]
return data_point, data_latent
def IOSS(mu, n_draws=10000, robust_k_prop = 0.01):
stdmu = (mu - torch.min(mu,dim=0)[0])/ (torch.max(mu,dim=0)[0]-torch.min(mu,dim=0)[0])
K = np.int(robust_k_prop * mu.shape[0]) + 1
maxs = torch.topk(stdmu, K, dim=0)[0][-1,:]
mins = -(torch.topk(-stdmu, K, dim=0)[0][-1,:])
smps = (torch.stack([torch.rand(n_draws).cuda() * (maxs[i]-mins[i]) + mins[i] for i in range(stdmu.shape[1])], dim=1))
min_dist = (torch.min(torch.cdist(smps, stdmu.cuda()), dim=1)[0])
# ortho = (torch.mean(min_dist,dim=0))
ortho = (torch.topk(min_dist, np.int(robust_k_prop*n_draws)+1, dim=0))[0][-1]
# ortho = torch.max(min_dist,dim=0)[0]
return ortho
def loss_intervention(decoder, preds, inputs, ioss_penalty=0.1, device=[]):
# constraint= torch.mean( IOSS(preds) )
constraint= torch.tensor(0.0).to(device)
total_pairs= 3
for idx in range(total_pairs):
perm= torch.randperm(preds.shape[1])
constraint+= torch.mean(IOSS( preds[:, perm[:2]] ))
#Reconstruction Loss
criterion = nn.MSELoss()
preds= decoder(preds)
loss = criterion(preds,inputs)
#Final Loss
theta = ioss_penalty
loss = loss + theta*constraint
return loss
def train_model(encoder, decoder, optimizer, data_loader, num_epochs, ioss_penalty= 0.1):
# Set model to train mode
encoder.train()
decoder.train()
# Training loop
for epoch in tqdm(range(num_epochs)):
#MCC
z, z_pred= get_predictions(encoder, data_loader, device= device)
mcc= get_cross_correlation({'te':z}, {'te':z_pred})
print('MCC: ', mcc)
print('Epoch: ', epoch)
train_loss= 0.0
batch_count= 0
for data_inputs, data_latents, in data_loader:
data_inputs = data_inputs.to(device)
preds = encoder(data_inputs)
# preds = preds.squeeze(dim=1)
# loss = loss_intervention(preds, data_inputs)
loss = loss_intervention(decoder, preds, data_inputs, ioss_penalty= ioss_penalty, device= device)
train_loss+= loss.item()
batch_count+=1
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Loss: ', train_loss/batch_count)
# Input Parsing
parser = argparse.ArgumentParser()
parser.add_argument('--total_samples', type=int, default=50000,
help='')
parser.add_argument('--latent_dim', type=int, default= 10,
help='')
parser.add_argument('--latent_case', type=str, default='uniform',
help='uniform, uniform_corr')
parser.add_argument('--ioss_penalty', type=float, default= 10.0,
help='')
parser.add_argument('--batch_size', type=int, default= 16,
help='')
parser.add_argument('--lr', type=float, default= 0.001,
help='')
parser.add_argument('--weight_decay', type=float, default= 5e-4,
help='')
parser.add_argument('--num_epochs', type=int, default= 300,
help='')
parser.add_argument('--seed', type=int, default=3,
help='')
args = parser.parse_args()
n = args.total_samples
d = args.latent_dim
latent_case= args.latent_case
ioss_penalty= args.ioss_penalty
lr= args.lr
weight_decay= args.weight_decay
# A = np.random.uniform(size=(d,d))
A = ortho_group.rvs(d)
# Observational data
if latent_case == 'uniform':
Zobs = np.random.uniform(size=(n,d))
elif latent_case == 'uniform_corr':
Zobs= np.zeros((n, d))
for d_idx in range(0 , d, 2):
print('Latent entries for the pair: ', d_idx, d_idx + 1)
p1= bernoulli.rvs(0.5, size=n)
p2= bernoulli.rvs(0.9, size=n)
z_11= np.random.uniform(low=0, high=5, size=n)
z_12= np.random.uniform(low=-5, high=0, size=n)
z_21= np.random.uniform(low=0, high=3, size=n)
z_22= np.random.uniform(low=-3, high=0, size=n)
for idx in range(n):
if p1[idx] == 1:
Zobs[idx, d_idx + 0]= z_11[idx]
if p2[idx] == 1:
Zobs[idx, d_idx + 1]= z_21[idx]
else:
Zobs[idx, d_idx + 1]= z_22[idx]
else:
Zobs[idx, d_idx + 0]= z_12[idx]
if p2[idx] == 1:
Zobs[idx, d_idx + 1]= z_22[idx]
else:
Zobs[idx, d_idx + 1]= z_21[idx]
# print(100*np.sum(Zobs[:,0]*Zobs[:,1]<0)/n)
Xobs = np.matmul(Zobs,A)
Eobs = np.zeros((n,1))
X = Xobs
Z = Zobs
E = Eobs
X = torch.tensor(X, dtype=torch.float32)
E = torch.tensor(E, dtype=torch.float32)
Data_train = (X, Z, E)
# data prep
data_train_obj = data_class_loader(Data_train)
train_data_loader = data.DataLoader(data_train_obj, batch_size=32, shuffle=True)
# device = torch.device("cuda:0")
device = torch.device("cuda:0")
# model and optimizer init
num_inputs= Data_train[0].size()[1]
encoder = LinearEncoder(num_inputs=num_inputs, num_outputs=num_inputs)
encoder.to(device)
decoder = LinearEncoder(num_inputs=num_inputs, num_outputs=num_inputs)
decoder.to(device)
optimizer= torch.optim.Adam([
{'params': filter(lambda p: p.requires_grad, list(encoder.parameters()) + list(decoder.parameters()) )},
], lr= lr, weight_decay= weight_decay )
num_epochs = 200
# train model
train_model(encoder, decoder, optimizer, train_data_loader, num_epochs, ioss_penalty= ioss_penalty)
|
CausalRepID-main
|
scripts/ioss.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import copy
import numpy as np
import torch
import torch.utils.data as data_utils
from torchvision import datasets, transforms
from sklearn.preprocessing import StandardScaler
# Base Class
from data.data_loader import BaseDataLoader
class BallsDataLoader():
def __init__(self, data_dir='', data_case='train', observation_case= True, intervention_case=False):
self.data_case= data_case
self.observation_case= observation_case
self.intervention_case= intervention_case
self.obs_data_dir = 'data/datasets/' + data_dir + 'observation/'
self.itv_data_dir = 'data/datasets/' + data_dir + 'intervention/'
self.data, self.latents, self.intervention_indices= self.load_data(self.data_case)
def __len__(self):
return self.latents.shape[0]
def __getitem__(self, index):
x = self.data[index]
z = self.latents[index]
y= self.intervention_indices[index]
data_transform= transforms.Compose([
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
x= data_transform(x)
return x, z, y
def load_data(self, data_case):
#Note: intervention indices are being sampled as some random values for now; do not need them but for consistency with functions in metrics module
x_obs= np.load(self.obs_data_dir + data_case + '_' + 'x' + '.npy')
z_obs= np.load(self.obs_data_dir + data_case + '_' + 'z' + '.npy')
y_obs= np.load(self.obs_data_dir + data_case + '_' + 'y' + '.npy')
x_itv= np.load(self.itv_data_dir + data_case + '_' + 'x' + '.npy')
z_itv= np.load(self.itv_data_dir + data_case + '_' + 'z' + '.npy')
y_itv= np.load(self.itv_data_dir + data_case + '_' + 'y' + '.npy')
if self.observation_case and self.intervention_case:
x= np.concatenate((x_obs, x_itv), axis=0)
z= np.concatenate((z_obs, z_itv), axis=0)
y= np.concatenate((y_obs, y_itv), axis=0)
elif self.observation_case:
x= x_obs
z= z_obs
y= y_obs
elif self.intervention_case:
x= x_itv
z= z_itv
y= y_itv
x= torch.tensor(x).float()
z= torch.tensor(z).float()
y= torch.tensor(y).float()
# Change the dimension from (B, H, W, C) to (B, C, H ,W)
x= x.permute(0, 3, 1, 2)
return x, z, y
|
CausalRepID-main
|
data/balls_dataset_loader.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
#Common imports
import sys
import os
import argparse
import random
import copy
import math
import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from scipy.stats import bernoulli
path= os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(path)
from data.dag_generator import DagGenerator
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def compute_total_polynomial_terms(poly_degree, latent_dim):
count=0
for degree in range(poly_degree+1):
count+= pow(latent_dim, degree)
return count
def compute_kronecker_product(degree, latent):
if degree ==0:
out= np.array([1])
else:
out=copy.deepcopy(latent)
for idx in range(1, degree):
out= np.kron(out, latent)
# print(out.shape)
return out
def compute_decoder_polynomial(poly_degree, latent):
out=[]
for degree in range(poly_degree+1):
# print('Computing polynomial term of degree ', degree)
out.append(compute_kronecker_product(degree, latent))
out= np.concatenate(out)
out= np.reshape(out, (1,out.shape[0]))
return out
def generate_latent_vector(dataset_size, latent_dim, latent_case, intervention_indices= [], intervention_case= 0, dag=None, base_dir= ''):
z= np.zeros((dataset_size, latent_dim))
for i in range(latent_dim):
if latent_case == 'laplace':
z[:, i]= np.random.laplace(10, 5, dataset_size)
elif latent_case == 'uniform':
z[:, i]= np.random.uniform(low=-5, high=5, size=dataset_size)
elif latent_case == 'uniform_discrete':
z[:, i]= np.random.randint(10, size= dataset_size)
elif latent_case == 'gaussian':
z[:, i]= np.random.normal(0, 1, size= dataset_size)
elif latent_case == 'special':
support= np.array([0, 1])
p= 1
prob= np.exp( -1*support**p )
prob= prob/np.sum(prob)
idx= np.argmax( np.random.multinomial(1, prob, size=dataset_size), axis=1 )
z[:, i]= support[idx]
if latent_case == 'gaussian_corr':
rho= 0.9
noise_var= np.eye(latent_dim)
for i in range(latent_dim):
for j in range(i+1, latent_dim):
noise_var[i,j]= rho ** (np.abs(i-j))
z= np.random.multivariate_normal(np.zeros(latent_dim), noise_var, dataset_size)
if latent_case == 'uniform_corr':
for d_idx in range(0 , latent_dim, 2):
print('Latent entries for the pair: ', d_idx, d_idx + 1)
p1= bernoulli.rvs(0.5, size=dataset_size)
p2= bernoulli.rvs(0.9, size=dataset_size)
z_11= np.random.uniform(low=0, high=5, size=dataset_size)
z_12= np.random.uniform(low=-5, high=0, size=dataset_size)
z_21= np.random.uniform(low=0, high=3, size=dataset_size)
z_22= np.random.uniform(low=-3, high=0, size=dataset_size)
for idx in range(dataset_size):
if p1[idx] == 1:
z[idx, d_idx + 0]= z_11[idx]
if p2[idx] == 1:
z[idx, d_idx + 1]= z_21[idx]
else:
z[idx, d_idx + 1]= z_22[idx]
else:
z[idx, d_idx + 0]= z_12[idx]
if p2[idx] == 1:
z[idx, d_idx + 1]= z_22[idx]
else:
z[idx, d_idx + 1]= z_21[idx]
if 'mixture' in latent_case:
mix_coff= bernoulli.rvs(0.5, size=dataset_size)
mix_coff= np.reshape( mix_coff, (mix_coff.shape[0], 1) )
z1= np.zeros((dataset_size, latent_dim))
z2= np.zeros((dataset_size, latent_dim))
for i in range(latent_dim):
if args.latent_case == 'uniform_mixture':
z1[:, i]= np.random.uniform(low=-5, high=5, size=dataset_size)
z2[:, i]= np.random.uniform(low=-1, high=1, size=dataset_size)
elif args.latent_case == 'gaussian_mixture':
z1[:, i]= np.random.normal(0, 1, size=dataset_size)
z2[:, i]= np.random.normal(1, 2, size= dataset_size)
else:
print('Error: Not valid latent type for a mixture model')
sys.exit()
z= mix_coff * z1 + (1-mix_coff) * z2
if intervention_case and 'scm' not in latent_case:
for idx, intervene_idx in np.ndenumerate(intervention_indices):
z[idx, intervene_idx]= 2.0
if 'scm' in latent_case:
if intervention_case:
latent_dict = {}
for intervene_idx in range(latent_dim):
dag_copy= copy.deepcopy(dag)
df, obj= dag_copy.intervene(intervention_nodes= [intervene_idx], target_distribution= 'hard_intervention')
latent_dict[intervene_idx]= df
for idx, intervene_idx in np.ndenumerate(intervention_indices):
z[idx, :]= latent_dict[intervene_idx][idx, :]
else:
df, obj= dag.generate()
z= df.values[:dataset_size, :]
nx.draw_networkx(obj, arrows=True)
plt.savefig( base_dir + latent_case + '.jpg')
plt.clf()
return z
'''
Notations:
n: batch size
d: latent dimension
D: data dimension
p: degree of polynomial
q: number of terms in polynomial
Dimensions:
p: 2; q~ 111
z: (n, d): d= 10
x: (n, D): D= 100 -> 25
Poly(z): (n, q)
Coefficient Matrix: (D, q)
Ideal: D > q
'''
#TODO: Throw exception when the latent case if not amongst the valid ones
# Input Parsing
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=0,
help='')
parser.add_argument('--data_dim', type=int, default=200,
help='')
parser.add_argument('--latent_dim', type=int, default=10,
help='')
parser.add_argument('--latent_case', type=str, default='uniform',
help='uniform; uniform_corr; gaussian_mixture')
parser.add_argument('--poly_degree', type=int, default=2,
help='')
parser.add_argument('--train_size', type=int, default=10000,
help='')
parser.add_argument('--test_size', type=int, default=20000,
help='')
args = parser.parse_args()
seed= args.seed
data_dim= args.data_dim
latent_dim= args.latent_dim
latent_case= args.latent_case
poly_degree= args.poly_degree
train_size= args.train_size
test_size= args.test_size
poly_size= compute_total_polynomial_terms(poly_degree, latent_dim)
print('Total Polynomial Terms: ', poly_size)
#Random Seed
random.seed(seed*10)
np.random.seed(seed*10)
coff_matrix= np.random.multivariate_normal(np.zeros(poly_size), np.eye(poly_size), size=data_dim).T
print('Coeff Matrix', coff_matrix.shape)
_, sng_values, _ = np.linalg.svd(coff_matrix)
# print('Singular Values for Coeff Matrix: ', sng_values)
#DAG
#NOTE: For this configuration to work we need to have the same train and test size
if latent_case == 'scm_sparse':
dag= DagGenerator('linear', cause='gaussian', nodes=latent_dim, npoints= max( args.train_size, args.test_size), expected_density= 0.5)
elif latent_case == 'scm_dense':
dag= DagGenerator('linear', cause='gaussian', nodes=latent_dim, npoints= max( args.train_size, args.test_size), expected_density= 1.0)
else:
dag= None
for distribution_case in ['observational', 'interventional']:
for data_case in ['train', 'val', 'test']:
if distribution_case == 'observational':
base_dir= 'data/datasets/' + 'seed_' + str(seed) + '/observation/'
elif distribution_case == 'interventional':
base_dir= 'data/datasets/' + 'seed_' + str(seed) + '/intervention/'
base_dir= base_dir + 'polynomial_latent_' + latent_case + '_poly_degree_' + str(poly_degree) + '_data_dim_' + str(data_dim) + '_latent_dim_' + str(latent_dim) + '/'
if not os.path.exists(base_dir):
os.makedirs(base_dir)
print('Data Case: ', data_case)
if data_case == 'train':
dataset_size= args.train_size
if data_case == 'val':
dataset_size= int(args.train_size/4)
elif data_case == 'test':
dataset_size= args.test_size
#Generating the latent vector
if distribution_case == 'observational':
y= -1 * np.ones(dataset_size)
z= generate_latent_vector(dataset_size, latent_dim, latent_case, intervention_case= 0, intervention_indices= y, dag= dag, base_dir= base_dir)
elif distribution_case == 'interventional':
y= np.argmax(np.random.multinomial(1, [1/latent_dim]*latent_dim, dataset_size), axis=1 )
z= generate_latent_vector(dataset_size, latent_dim, latent_case, intervention_case= 1, intervention_indices= y, dag= dag, base_dir= base_dir)
print('Latent Z')
print(z.shape)
print(z[:5])
#Transforming the latent via polynomial decoder
print('Data X')
x=[]
for idx in range(z.shape[0]):
x.append( compute_decoder_polynomial(poly_degree, z[idx, :] ) )
x= np.concatenate(x, axis=0)
print(x.shape)
x1= np.matmul(x[:, :1+latent_dim], coff_matrix[:1+latent_dim, :])
print('X1')
print('Min', np.min(np.abs(x1)), 'Max', np.max(np.abs(x1)), 'Mean', np.mean(np.abs(x1)))
x2= np.matmul(x[:, 1+latent_dim:], coff_matrix[1+latent_dim:, :])
norm_factor= 0.5 * np.max(np.abs(x2)) / np.max(np.abs(x1))
x2 = x2 / norm_factor
print('X2')
print('Min', np.min(np.abs(x2)), 'Max', np.max(np.abs(x2)), 'Mean', np.mean(np.abs(x2)))
x= (x1+x2)
print(x.shape)
f= base_dir + data_case + '_' + 'x' + '.npy'
np.save(f, x)
f= base_dir + data_case + '_' + 'z' + '.npy'
np.save(f, z)
f= base_dir + data_case + '_' + 'y' + '.npy'
np.save(f, y)
|
CausalRepID-main
|
data/synthetic_polynomial_dgp.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""Defining a set of classes that represent causal functions/ mechanisms.
Author: Diviyan Kalainathan
Modified by Philippe Brouillard, July 24th 2019
Modified by Divyat Mahajan, December 30th 2022
.. MIT License
..
.. Copyright (c) 2018 Diviyan Kalainathan
..
.. Permission is hereby granted, free of charge, to any person obtaining a copy
.. of this software and associated documentation files (the "Software"), to deal
.. in the Software without restriction, including without limitation the rights
.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
.. copies of the Software, and to permit persons to whom the Software is
.. furnished to do so, subject to the following conditions:
..
.. The above copyright notice and this permission notice shall be included in all
.. copies or substantial portions of the Software.
..
.. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
.. SOFTWARE.
"""
import random
import numpy as np
from scipy.stats import bernoulli
from sklearn.mixture import GaussianMixture as GMM
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.gaussian_process import GaussianProcessRegressor
import torch as th
import copy
class LinearMechanism(object):
"""Linear mechanism, where Effect = alpha*Cause + Noise."""
def __init__(self, ncauses, points, noise_function, d=4, noise_coeff=.4):
"""Init the mechanism."""
super(LinearMechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.coefflist = []
self.other_coefflist = []
self.noise_coeff = noise_coeff
self.noise_function = noise_function
for i in range(ncauses):
coeff = np.random.uniform(0.25, 1)
if np.random.randint(2) == 0:
coeff *= -1
self.coefflist.append(coeff)
self.old_coefflist = self.coefflist[:]
def parametric_intervention(self):
for i,c in enumerate(self.old_coefflist):
change = np.random.uniform(0.5, 1)
if c > 0:
coeff = c + change
else:
coeff = c - change
self.coefflist[i] = coeff
def unique_parametric_intervention(self):
if len(self.other_coefflist) == 0:
for i,c in enumerate(self.old_coefflist):
change = np.random.uniform(2, 5)
if np.random.randint(2) == 0:
change *= -1
if c > 0:
coeff = c + change
else:
coeff = c - change
self.other_coefflist.append(coeff)
self.coefflist = self.other_coefflist[:]
def reinit(self):
self.coefflist = self.old_coefflist[:]
def __call__(self, causes):
"""Run the mechanism."""
# Additive only, for now
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
# Compute each cause's contribution
for par in range(causes.shape[1]):
effect[:, 0] = effect[:, 0] + self.coefflist[par]*causes[:, par]
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class SigmoidMix_Mechanism(object):
def __init__(self, ncauses, points, noise_function, d=4, noise_coeff=.4):
"""Init the mechanism."""
super(SigmoidMix_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.a = np.random.exponential(1/4) + 1
ber = bernoulli.rvs(0.5)
self.b = ber * np.random.uniform(-2, -0.5) + (1-ber)*np.random.uniform(0.5, 2)
self.c = np.random.uniform(-2, 2)
self.noise_coeff = noise_coeff
self.noise_function = noise_function
self.old_b = self.b
self.old_c = self.c
self.other_b = None
self.other_c = None
def parametric_intervention(self):
change = np.random.uniform(0.5, 1)
if self.b <= -0.5:
self.b -= change
else:
self.b += change
change = np.random.uniform(-1, 1)
self.c += change
def unique_parametric_intervention(self):
if self.other_b is None and self.other_c is None:
self.parametric_intervention()
self.other_b = self.b
self.other_c = self.c
self.b = self.other_b
self.c = self.other_c
def reinit(self):
self.b = self.old_b
self.c = self.old_c
def mechanism(self, causes):
"""Mechanism function."""
self.noise = self.noise_coeff * self.noise_function(self.points)
result = np.zeros((self.points, 1))
for i in range(self.points):
pre_add_effect = 0
for c in range(causes.shape[1]):
pre_add_effect += causes[i, c]
pre_add_effect += self.noise[i]
result[i, 0] = self.a * self.b * \
(pre_add_effect + self.c)/(1 + abs(self.b*(pre_add_effect + self.c)))
return result
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
effect[:, 0] = self.mechanism(causes)[:, 0]
return effect
class SigmoidAM_Mechanism(object):
def __init__(self, ncauses, points, noise_function, d=4, noise_coeff=.4):
"""Init the mechanism."""
super(SigmoidAM_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.a = np.random.exponential(1/4) + 1
ber = bernoulli.rvs(0.5)
self.b = ber * np.random.uniform(-2, -0.5) + (1-ber)*np.random.uniform(0.5, 2)
self.c = np.random.uniform(-2, 2)
self.noise_coeff = noise_coeff
self.noise_function = noise_function
self.old_b = self.b
self.old_c = self.c
self.other_b = None
self.other_c = None
def mechanism(self, x):
"""Mechanism function."""
result = np.zeros((self.points, 1))
for i in range(self.points):
result[i, 0] = self.a * self.b * (x[i] + self.c) / (1 + abs(self.b * (x[i] + self.c)))
return result
def __call__(self, causes):
"""Run the mechanism."""
# Additive only
self.noise = self.noise_coeff * self.noise_function(self.points)
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
for par in range(causes.shape[1]):
effect[:, 0] = effect[:, 0] + self.mechanism(causes[:, par])[:, 0]
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class ANM_Mechanism(object):
def __init__(self, ncauses, points, noise_function, noise_coeff=.4):
"""Init the mechanism."""
super(ANM_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise_function = noise_function
self.noise_coeff = noise_coeff
self.nb_step = 0
def mechanism(self, x):
"""Mechanism function."""
self.nb_step += 1
x = np.reshape(x, (x.shape[0], x.shape[1]))
if(self.nb_step == 1):
cov = computeGaussKernel(x)
mean = np.zeros((1, self.points))[0, :]
y = np.random.multivariate_normal(mean, cov)
self.gpr = GaussianProcessRegressor()
self.gpr.fit(x, y)
else:
y = self.gpr.predict(x)
return y
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
# Compute each cause's contribution
if(causes.shape[1] > 0):
effect[:, 0] = self.mechanism(causes)
else:
effect[:, 0] = self.mechanism(self.noise)
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class NN_Mechanism_Add(object):
def __init__(self, ncauses, points, noise_function, nh=10, noise_coeff=.4):
"""Init the mechanism."""
super(NN_Mechanism_Add, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise_coeff = noise_coeff
self.noise_function = noise_function
self.nb_step = 0
self.nh = nh
self.layers = self.initialize()
self.old_layers = copy.deepcopy(self.layers)
self.other_layers = None
def weight_init(self, model):
if isinstance(model, th.nn.modules.Linear):
th.nn.init.normal_(model.weight.data, mean=0., std=1)
def initialize(self):
"""Mechanism function."""
layers = []
layers.append(th.nn.modules.Linear(self.n_causes, self.nh))
layers.append(th.nn.PReLU())
layers.append(th.nn.modules.Linear(self.nh, 1))
layers = th.nn.Sequential(*layers)
layers.apply(self.weight_init)
return layers
def parametric_intervention(self):
for i,layer in enumerate(self.layers):
if isinstance(layer, th.nn.modules.Linear):
with th.no_grad():
layer.weight += th.empty_like(layer.weight).normal_(mean=0, std=.1)
def unique_parametric_intervention(self):
if self.other_layers is None:
self.other_layers = copy.deepcopy(self.layers)
for i,layer in enumerate(self.other_layers):
if isinstance(layer, th.nn.modules.Linear) and i > 0:
with th.no_grad():
layer.weight += th.empty_like(layer.weight).normal_(mean=0, std=1)
self.layers = copy.deepcopy(self.other_layers)
def reinit(self):
self.layers = copy.deepcopy(self.old_layers)
def apply_nn(self, x):
data = x.astype('float32')
data = th.from_numpy(data)
return np.reshape(self.layers(data).data, (x.shape[0],))
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
# Compute each cause's contribution
if (causes.shape[1] > 0):
effect[:, 0] = self.apply_nn(causes)
else:
print("abnormal")
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class NN_Mechanism(object):
def __init__(self, ncauses, points, noise_function, nh=20, noise_coeff=.4):
"""Init the mechanism."""
super(NN_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise_coeff = noise_coeff
self.noise_function = noise_function
self.nb_step = 0
self.nh = nh
self.layers = self.initialize()
self.old_layers = copy.deepcopy(self.layers)
self.other_layers = None
def weight_init(self, model):
if isinstance(model, th.nn.modules.Linear):
th.nn.init.normal_(model.weight.data, mean=0., std=1)
def initialize(self):
"""Mechanism function."""
layers = []
layers.append(th.nn.modules.Linear(self.n_causes+1, self.nh))
layers.append(th.nn.Tanh())
layers.append(th.nn.modules.Linear(self.nh, 1))
layers = th.nn.Sequential(*layers)
layers.apply(self.weight_init)
return layers
def parametric_intervention(self):
for i,layer in enumerate(self.layers):
if isinstance(layer, th.nn.modules.Linear):
with th.no_grad():
layer.weight += th.empty_like(layer.weight).normal_(mean=0, std=.1)
def unique_parametric_intervention(self):
if self.other_layers is None:
self.other_layers = copy.deepcopy(self.layers)
for i,layer in enumerate(self.other_layers):
if isinstance(layer, th.nn.modules.Linear) and i > 0:
with th.no_grad():
layer.weight += th.empty_like(layer.weight).normal_(mean=0, std=1)
self.layers = copy.deepcopy(self.other_layers)
def reinit(self):
self.layers = copy.deepcopy(self.old_layers)
def apply_nn(self, x):
data = x.astype('float32')
data = th.from_numpy(data)
return np.reshape(self.layers(data).data, (x.shape[0],))
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
# Compute each cause's contribution
if (causes.shape[1] > 0):
mix = np.hstack((causes, self.noise))
effect[:, 0] = self.apply_nn(mix)
else:
effect[:, 0] = self.apply_nn(self.noise)
return effect
# === Multimodal Mechanisms ===
class Multimodal_X_Mechanism(object):
"""Mecanism with multimodal distribution: usually a combination of multiple
functions"""
def __init__(self, ncauses, points, noise_function, d=4, noise_coeff=.4):
"""Init the mechanism."""
super(Multimodal_X_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.coefflist = []
self.other_coefflist = []
self.noise_coeff = noise_coeff
self.noise_function = noise_function
for i in range(ncauses):
coeff = np.random.uniform(0.5, 1)
if np.random.randint(2) == 0:
coeff *= -1
self.coefflist.append(coeff)
self.old_coefflist = self.coefflist[:]
def parametric_intervention(self):
for i,c in enumerate(self.old_coefflist):
change = np.random.uniform(0.5, 1)
if c > 0:
coeff = c + change
else:
coeff = c - change
self.coefflist[i] = coeff
def unique_parametric_intervention(self):
if len(self.other_coefflist) == 0:
for i,c in enumerate(self.old_coefflist):
change = np.random.uniform(0.5, 1)
if c > 0:
coeff = c + change
else:
coeff = c - change
self.other_coefflist.append(coeff)
self.coefflist = self.other_coefflist[:]
def reinit(self):
self.coefflist = self.old_coefflist[:]
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
selector = np.random.choice([-1,1], size=self.points)
# Compute each cause's contribution
for par in range(causes.shape[1]):
for i, sel in enumerate(selector):
effect[i, 0] = effect[i, 0] + sel*self.coefflist[par]*causes[i, par]
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class Multimodal_Circle_Mechanism(object):
def __init__(self, ncauses, points, noise_function, d=4, noise_coeff=.4):
"""Init the mechanism."""
super(Multimodal_Circle_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise_coeff = noise_coeff
self.noise_function = noise_function
self.sin_scale = np.random.uniform(0.5, 1.5) #1
self.period = np.random.uniform(0.5, 1.5) #1
self.phase_shift = np.pi/2
# make copy of initial parameters
self.old_sin_scale = self.sin_scale
self.old_period = self.period
self.old_phase_shift = self.phase_shift
self.other_sin_scale = None
self.other_period = None
self.other_phase_shift = None
def parametric_intervention(self):
change = np.random.uniform(0.5, 1.5)
self.sin_scale = self.old_phase_shift
self.period = np.random.uniform(0.5, 1.5) #1
self.phase_shift = np.pi/2
def unique_parametric_intervention(self):
if self.other_sin_scale is None:
self.parametric_intervention()
self.other_sin_scale = self.sin_scale
self.other_period = self.period
self.other_phase_shift = self.phase_shift
self.sin_scale = self.other_sin_scale
self.period = self.other_period
self.phase_shift = self.other_phase_shift
def reinit(self):
self.sin_scale = self.old_sin_scale
self.period = self.old_period
self.phase_shift = self.old_phase_shift
def mechanism(self, sel, x):
if sel:
sin_scale = -self.sin_scale
else:
sin_scale = self.sin_scale
return sin_scale * np.sin(self.period * (x + self.phase_shift))
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
selector = np.random.choice([0,1], size=self.points)
# Compute each cause's contribution
for par in range(causes.shape[1]):
for i, sel in enumerate(selector):
effect[i, 0] = effect[i, 0] + self.mechanism(sel, causes[i, par])
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class Multimodal_ADN_Mechanism(object):
def __init__(self, ncauses, points, noise_function, d=4, noise_coeff=.4):
"""Init the mechanism."""
super(Multimodal_ADN_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise_coeff = noise_coeff
self.noise_function = noise_function
self.sin_scale = np.random.uniform(0.5, 1.5) #1
self.period = np.random.uniform(1, 2) #1
self.phase_shift = np.pi/2
# make copy of initial parameters
self.old_sin_scale = self.sin_scale
self.old_period = self.period
self.old_phase_shift = self.phase_shift
self.other_sin_scale = None
self.other_period = None
self.other_phase_shift = None
def parametric_intervention(self):
# change = np.random.uniform(1, 2)
self.sin_scale = self.old_phase_shift
change = np.random.uniform(1, 2)
self.period = self.old_period + change
self.phase_shift = np.pi/2
def unique_parametric_intervention(self):
if self.other_sin_scale is None:
self.parametric_intervention()
self.other_sin_scale = self.sin_scale
self.other_period = self.period
self.other_phase_shift = self.phase_shift
self.sin_scale = self.other_sin_scale
self.period = self.other_period
self.phase_shift = self.other_phase_shift
def reinit(self):
self.sin_scale = self.old_sin_scale
self.period = self.old_period
self.phase_shift = self.old_phase_shift
def mechanism(self, sel, x):
if sel:
sin_scale = -self.sin_scale
else:
sin_scale = self.sin_scale
return sin_scale * np.sin(self.period * (x + self.phase_shift))
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
selector = np.random.choice([0,1], size=self.points)
# Compute each cause's contribution
for par in range(causes.shape[1]):
for i, sel in enumerate(selector):
effect[i, 0] = effect[i, 0] + self.mechanism(sel, causes[i, par])
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class Function_Template:
def __init__(self, sign, slope, intercept, sin_scale, period, phase_shift):
self.sign = sign
self.slope = slope
self.intercept = intercept
self.sin_scale = sin_scale
self.period = period
self.phase_shift = phase_shift
def __call__(self, x):
return self.sign*self.slope*x + self.intercept \
+ self.sin_scale*np.sin(self.period*(x + self.phase_shift))
# ====================================
class Polynomial_Mechanism(object):
def __init__(self, ncauses, points, noise_function, d=2, noise_coeff=.4):
"""Init the mechanism."""
super(Polynomial_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.d = d
self.polycause = []
for c in range(ncauses):
self.coefflist = []
for j in range(self.d + 1):
self.coefflist.append(random.random())
self.polycause.append(self.coefflist)
self.ber = bernoulli.rvs(0.5)
self.noise = noise_coeff * noise_function(points)
def mechanism(self, x, par):
"""Mechanism function."""
list_coeff = self.polycause[par]
result = np.zeros((self.points, 1))
for i in range(self.points):
for j in range(self.d+1):
result[i, 0] += list_coeff[j]*np.power(x[i], j)
result[i, 0] = min(result[i, 0], 1)
result[i, 0] = max(result[i, 0], -1)
return result
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
for par in range(causes.shape[1]):
effect[:, 0] = effect[:, 0] + self.mechanism(causes[:, par], par)[:, 0]
if(self.ber > 0 and causes.shape[1] > 0):
effect[:, 0] = effect[:, 0] * self.noise[:, 0]
else:
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
def computeGaussKernel(x):
"""Compute the gaussian kernel on a 1D vector."""
xnorm = np.power(euclidean_distances(x, x), 2)
return np.exp(-xnorm / (2.0))
class GaussianProcessAdd_Mechanism(object):
def __init__(self, ncauses, points, noise_function, noise_coeff=.4):
"""Init the mechanism."""
super(GaussianProcessAdd_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise = noise_coeff * noise_function(points)
self.nb_step = 0
def mechanism(self, x):
"""Mechanism function."""
self.nb_step += 1
x = np.reshape(x, (x.shape[0], 1))
cov = computeGaussKernel(x)
mean = np.zeros((1, self.points))[0, :]
y = np.random.multivariate_normal(mean, cov)
# if(self.nb_step < 5):
# cov = computeGaussKernel(x)
# mean = np.zeros((1, self.points))[0, :]
# y = np.random.multivariate_normal(mean, cov)
# elif(self.nb_step == 5):
# cov = computeGaussKernel(x)
# mean = np.zeros((1, self.points))[0, :]
# y = np.random.multivariate_normal(mean, cov)
# self.gpr = GaussianProcessRegressor()
# self.gpr.fit(x, y)
# y = self.gpr.predict(x)
# else:
# y = self.gpr.predict(x)
return y
def __call__(self, causes):
"""Run the mechanism."""
# Additive only
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
for par in range(causes.shape[1]):
effect[:, 0] = effect[:, 0] + self.mechanism(causes[:, par])
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
return effect
class GaussianProcessMix_Mechanism(object):
def __init__(self, ncauses, points, noise_function, noise_coeff=.4):
"""Init the mechanism."""
super(GaussianProcessMix_Mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise = noise_coeff * noise_function(points)
self.nb_step = 0
def mechanism(self, x):
"""Mechanism function."""
self.nb_step += 1
x = np.reshape(x, (x.shape[0], x.shape[1]))
if(self.nb_step < 2):
cov = computeGaussKernel(x)
mean = np.zeros((1, self.points))[0, :]
y = np.random.multivariate_normal(mean, cov)
elif(self.nb_step == 2):
cov = computeGaussKernel(x)
mean = np.zeros((1, self.points))[0, :]
y = np.random.multivariate_normal(mean, cov)
self.gpr = GaussianProcessRegressor()
self.gpr.fit(x, y)
y = self.gpr.predict(x)
else:
y = self.gpr.predict(x)
return y
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
if(causes.shape[1] > 0):
mix = np.hstack((causes, self.noise))
effect[:, 0] = self.mechanism(mix)
else:
effect[:, 0] = self.mechanism(self.noise)
return effect
class pnl_gp_mechanism(object):
""" Post-Nonlinear model using a GP with additive noise. The
second non-linearity is a sigmoid """
def __init__(self, ncauses, points, noise_function, noise_coeff=.4):
"""Init the mechanism."""
super(pnl_gp_mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise = noise_coeff * noise_function(points)
self.nb_step = 0
self.f2 = lambda x: 1 / (1 + np.exp(-x))
def mechanism(self, x):
"""Mechanism function."""
self.nb_step += 1
x = np.reshape(x, (x.shape[0], x.shape[1]))
if(self.nb_step == 1):
cov = computeGaussKernel(x)
mean = np.zeros((1, self.points))[0, :]
y = np.random.multivariate_normal(mean, cov)
self.gpr = GaussianProcessRegressor()
self.gpr.fit(x, y)
y = self.gpr.predict(x)
else:
y = self.gpr.predict(x)
return y
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
if(causes.shape[1] > 0):
effect[:, 0] = self.mechanism(causes)
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
else:
effect[:, 0] = self.mechanism(self.noise)
effect[:, 0] = self.f2(effect[:, 0])
return effect
class pnl_mult_mechanism(object):
""" Post-Nonlinear model using a exp and log as the non-linearities.
This results in a multiplicative model. """
def __init__(self, ncauses, points, noise_function, noise_coeff=.4):
"""Init the mechanism."""
super(pnl_mult_mechanism, self).__init__()
self.n_causes = ncauses
self.points = points
self.noise_function = noise_function
self.noise_coeff = noise_coeff
self.f1 = lambda x: np.log(np.sum(x, axis=1))
self.f2 = lambda x: np.exp(x)
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
self.noise = self.noise_coeff * self.noise_function(self.points)
# Compute each cause's contribution
if(causes.shape[1] > 0):
effect[:, 0] = self.f1(causes) #[:, 0]
else:
effect[:, 0] = self.f1(self.noise)
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
effect[:, 0] = self.f2(effect[:, 0])
return effect
class PostNonLinear_Mechanism:
def __init__(self, ncauses, points, noise_function, f1=None, f2=None, noise_coeff=.4):
self.gp = GaussianProcessAdd_Mechanism(ncauses, points, noise_function,
noise_coeff=0)
self.points = points
self.noise = noise_coeff * noise_function(points)
self.f1 = f1
self.f2 = f2
if f1 is None and f2 is None:
raise ValueError("f1 and f2 have to de defined!")
elif f1 is None and f2 is not None:
self.f1 = self.gp
def __call__(self, causes):
"""Run the mechanism."""
effect = np.zeros((self.points, 1))
# Compute each cause's contribution
if(causes.shape[1] > 0):
effect[:, 0] = self.f1(causes)[:,0] # mult [:, 0]
else:
effect[:, 0] = self.f1(self.noise)
effect[:, 0] = effect[:, 0] + self.noise[:, 0]
effect[:, 0] = self.f2(effect[:, 0])
return effect
def gmm_cause(points, k=4, p1=2, p2=2):
"""Init a root cause with a Gaussian Mixture Model w/ a spherical covariance type."""
g = GMM(k, covariance_type="spherical")
g.fit(np.random.randn(300, 1))
g.means_ = p1 * np.random.randn(k, 1)
g.covars_ = np.power(abs(p2 * np.random.randn(k, 1) + 1), 2)
g.weights_ = abs(np.random.rand(k))
g.weights_ = g.weights_ / sum(g.weights_)
return g.sample(points)[0].reshape(-1)
def gaussian_cause(points):
"""Init a root cause with a Gaussian."""
return np.random.randn(points, 1)[:, 0]
def variable_gaussian_cause(points):
"""Init a root cause with a Gaussian. Similar to gaussian_cause
but have variable variance. Identical to J.Peters with default value (set noise_coeff=0.2)"""
# + np.random.rand(points, 1)[:, 0] - 1
return np.sqrt(np.random.rand(1) + 1) * np.random.randn(points, 1)[:, 0]
def uniform_cause(points):
"""Init a root cause with a uniform."""
return np.random.rand(points, 1)[:, 0] * 2 - 1
def uniform_cause_positive(points):
"""Init a root cause with a uniform."""
return np.random.rand(points, 1)[:, 0] * 2
def normal_noise(points):
"""Init a normal noise variable."""
return np.random.rand(1) * np.random.randn(points, 1) \
+ random.sample([2, -2], 1)
def variable_normal_noise(points):
"""Init a normal noise variable. Similar to normal_noise
but make sure to have at least a std of 1. Identical to
J.Peters with default value (set noise_coeff=0.2)"""
return np.sqrt(np.random.rand(1) + 1) * np.random.randn(points, 1)
def absolute_gaussian_noise(points):
"""Init an absolute normal noise variable."""
return np.abs(np.random.rand(points, 1) * np.random.rand(1))
def laplace_noise(points):
"""Init a Laplace noise variable."""
lambda_ = np.random.rand(1)
return np.random.laplace(0, lambda_, (points, 1))
def uniform_noise(points):
"""Init a uniform noise variable."""
return np.random.rand(1) * np.random.uniform(size=(points, 1)) \
+ random.sample([2, -2], 1)
class NormalCause(object):
def __init__(self, mean=0, std=1, std_min=None, std_max=None):
self.mean = mean
if std_min is None and std_max is None:
self.std = std
else:
self.std = np.random.uniform(std_min, std_max)
def __call__(self, points):
return np.random.normal(self.mean, self.std, \
size=(points))
class UniformCause(object):
def __init__(self, _min=-1, _max=1):
self._min = _min
self._max = _max
def __call__(self, points):
return np.random.uniform(self._min, self._max, size=(points))
class HardIntervention(object):
def __init__(self, _val):
self._val = _val
def __call__(self, points):
return self._val * np.ones(points)
class nn_noise(object):
def __init__(self, noise=variable_normal_noise, n_hidden=20):
"""Init the mechanism."""
super(nn_noise, self).__init__()
self.noise = noise
self.n_hidden = n_hidden
self.initialize_nn()
def initialize_nn(self):
layers = []
layers.append(th.nn.modules.Linear(1, self.n_hidden))
layers.append(th.nn.Tanh())
layers.append(th.nn.modules.Linear(self.n_hidden, 1))
self.layers = th.nn.Sequential(*layers)
# use a normal initialization
# self.layers.apply(self.weight_init)
def weight_init(self, model):
if isinstance(model, th.nn.modules.Linear):
th.nn.init.normal_(model.weight.data, mean=0., std=0.5)
def __call__(self, points):
x = self.noise(points)
data = x.astype('float32')
data = th.from_numpy(data)
data = self.layers(data).data.numpy()
return data
|
CausalRepID-main
|
data/causal_mechanisms.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import copy
import numpy as np
import torch
import torch.utils.data as data_utils
from torchvision import datasets, transforms
class BaseDataLoader(data_utils.Dataset):
def __init__(self, data_dir='', data_case='train', seed= 0, observation_case= True, intervention_case=False):
self.data_case= data_case
self.seed= seed
self.obs_data_dir = 'data/datasets/' + 'seed_' + str(seed) + '/observation/' + data_dir
self.itv_data_dir = 'data/datasets/' + 'seed_' + str(seed) + '/intervention/' + data_dir
self.observation_case= observation_case
self.intervention_case= intervention_case
self.data, self.latents, self.intervention_indices= self.load_data(self.data_case)
def __len__(self):
return self.latents.shape[0]
def __getitem__(self, index):
x = self.data[index]
z = self.latents[index]
y= self.intervention_indices[index]
return x, z, y
def load_data(self, data_case):
x_obs= np.load(self.obs_data_dir + data_case + '_' + 'x' + '.npy')
z_obs= np.load(self.obs_data_dir + data_case + '_' + 'z' + '.npy')
y_obs= np.load(self.obs_data_dir + data_case + '_' + 'y' + '.npy')
x_itv= np.load(self.itv_data_dir + data_case + '_' + 'x' + '.npy')
z_itv= np.load(self.itv_data_dir + data_case + '_' + 'z' + '.npy')
y_itv= np.load(self.itv_data_dir + data_case + '_' + 'y' + '.npy')
if self.observation_case and self.intervention_case:
x= np.concatenate((x_obs, x_itv), axis=0)
z= np.concatenate((z_obs, z_itv), axis=0)
y= np.concatenate((y_obs, y_itv), axis=0)
elif self.observation_case:
x= x_obs
z= z_obs
y= y_obs
elif self.intervention_case:
x= x_itv
y= y_itv
z= z_itv
x= torch.tensor(x).float()
z= torch.tensor(z).float()
y= torch.tensor(y).float()
return x, z, y
|
CausalRepID-main
|
data/data_loader.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import sys
import random
import argparse
import torch
import numpy as np
import pygame
from pygame import gfxdraw, init
from typing import Callable, Optional
from matplotlib import pyplot as plt
if "SDL_VIDEODRIVER" not in os.environ:
os.environ["SDL_VIDEODRIVER"] = "dummy"
COLOURS_ = [
[2, 156, 154],
[222, 100, 100],
[149, 59, 123],
[74, 114, 179],
[27, 159, 119],
[218, 95, 2],
[117, 112, 180],
[232, 41, 139],
[102, 167, 30],
[231, 172, 2],
[167, 118, 29],
[102, 102, 102],
]
SCREEN_DIM = 64
def circle(
x_,
y_,
surf,
color=(204, 204, 0),
radius=0.1,
screen_width=SCREEN_DIM,
y_shift=0.0,
offset=None,
):
if offset is None:
offset = screen_width / 2
scale = screen_width
x = scale * x_ + offset
y = scale * y_ + offset
gfxdraw.aacircle(
surf, int(x), int(y - offset * y_shift), int(radius * scale), color
)
gfxdraw.filled_circle(
surf, int(x), int(y - offset * y_shift), int(radius * scale), color
)
class Balls(torch.utils.data.Dataset):
ball_rad = 2.0*0.04
screen_dim = 64
def __init__(
self,
transform: Optional[Callable] = None,
n_balls: int = 1,
):
super(Balls, self).__init__()
if transform is None:
def transform(x):
return x
self.transform = transform
pygame.init()
self.screen = pygame.display.set_mode((self.screen_dim, self.screen_dim))
self.surf = pygame.Surface((self.screen_dim, self.screen_dim))
self.n_balls = n_balls
def __len__(self) -> int:
# arbitrary since examples are generated online.
return 20000
def draw_scene(self, z):
self.surf.fill((255, 255, 255))
if z.ndim == 1:
z = z.reshape((1, 2))
for i in range(z.shape[0]):
circle(
z[i, 0],
z[i, 1],
self.surf,
color=COLOURS_[i],
radius=self.ball_rad,
screen_width=self.screen_dim,
y_shift=0.0,
offset=0.0,
)
self.surf = pygame.transform.flip(self.surf, False, True)
self.screen.blit(self.surf, (0, 0))
return np.transpose(
np.array(pygame.surfarray.pixels3d(self.screen)), axes=(1, 0, 2)
)
def __getitem__(self, item):
raise NotImplemented()
class BlockOffset(Balls):
def __init__(
self,
transform: Optional[Callable] = None,
n_balls: int = 2,
interventions_per_latent: int = 3,
latent_case: str = 'iid',
scm_mechanism: str = None,
):
super().__init__(transform=transform, n_balls=n_balls)
self.dataset_size= 20000
self.latent_dim= self.n_balls*2
self.intervention_case= 0
self.intervene_all_latent= 1
self.interventions_per_latent= interventions_per_latent
self.latent_case= latent_case
self.scm_mechanism= scm_mechanism
def __getitem__(self, item):
if self.latent_case == 'iid':
z = self.get_observational_data_iid()
elif self.latent_case == 'scm':
z = self.get_observational_data_scm()
else:
print('Latent type not supported')
sys.exit()
y = -1*np.ones(2)
if self.intervention_case:
z= z.flatten()
if self.intervene_all_latent:
intervene_idx= np.random.randint(self.latent_dim, size=1)
else:
intervene_idx= 0
if self.interventions_per_latent > 1:
intervene_range= np.linspace(0.25, 0.75, num= self.interventions_per_latent)
intervene_val= [np.random.choice(intervene_range)]
elif self.interventions_per_latent == 1:
intervene_val= [0.5]
if self.latent_case == 'iid':
z= self.get_interventional_data_iid(z, intervene_idx, intervene_val)
elif self.latent_case == 'scm':
z= self.get_interventional_data_scm(z, intervene_idx, intervene_val)
else:
print('Latent type not supported')
sys.exit()
y[0]= intervene_idx
y[1]= intervene_val[0]
x = self.draw_scene(z)
x = self.transform(x)
return z.flatten(), y, x
def get_observational_data_iid(self):
z= np.random.uniform(0.1, 0.9, size=(self.n_balls, 2))
return z
def get_interventional_data_iid(self, z, intervene_idx, intervene_val):
z[intervene_idx]= intervene_val
z= np.reshape(z, (self.n_balls, 2))
return z
def get_observational_data_scm(self, x1=None, y1=None):
if x1 is None:
x1= np.random.uniform(0.1, 0.9, size=1)[0]
if y1 is None:
y1= np.random.uniform(0.1, 0.9, size=1)[0]
if self.scm_mechanism == 'linear':
constraint= x1+y1
elif self.scm_mechanism == 'non_linear':
constraint= 1.25 * (x1**2 + y1**2)
if constraint >= 1.0:
x2= np.random.uniform(0.1, 0.5, size=1)[0]
y2= np.random.uniform(0.5, 0.9, size=1)[0]
else:
x2= np.random.uniform(0.5, 0.9, size=1)[0]
y2= np.random.uniform(0.1, 0.5, size=1)[0]
z= np.array([[x1, y1], [x2, y2]])
return z
def get_interventional_data_scm(self, z, intervene_idx, intervene_val):
# Internvetion on the child Ball (Ball 2)
if intervene_idx in [2, 3]:
z[intervene_idx]= intervene_val
z= np.reshape(z, (self.n_balls, 2))
# Internvetion on the parent Ball (Ball 1)
elif intervene_idx == 0:
z= self.get_observational_data_scm(x1= intervene_val[0], y1= z[1])
elif intervene_idx == 1:
z= self.get_observational_data_scm(x1= z[0], y1= intervene_val[0])
return z
# Input Parsing
parser = argparse.ArgumentParser()
parser.add_argument('--seed', type=int, default=1,
help='')
parser.add_argument('--train_size', type=int, default=20000,
help='')
parser.add_argument('--test_size', type=int, default=20000,
help='')
parser.add_argument('--distribution_case', type=str, default='observation',
help='observation; intervention')
parser.add_argument('--latent_case', type=str, default='iid',
help='iid; scm')
parser.add_argument('--scm_mechanism', type=str, default='linear',
help='linear; non_linear')
parser.add_argument('--interventions_per_latent', type= int, default=3,
help='')
args = parser.parse_args()
seed= args.seed
train_size= args.train_size
test_size= args.test_size
distribution_case= args.distribution_case
latent_case= args.latent_case
scm_mechanism= args.scm_mechanism
interventions_per_latent= args.interventions_per_latent
#Random Seed
random.seed(seed*10)
np.random.seed(seed*10)
data_obj= BlockOffset(interventions_per_latent= interventions_per_latent, latent_case= latent_case, scm_mechanism= scm_mechanism)
if distribution_case == 'observation':
data_obj.intervention_case= 0
elif distribution_case == 'intervention':
data_obj.intervention_case= 1
for data_case in ['train', 'val', 'test']:
base_dir= 'data/datasets/balls_' + latent_case + '_' + scm_mechanism + '/' + str(distribution_case) + '/'
if not os.path.exists(base_dir):
os.makedirs(base_dir)
print('Distribution Case: ', distribution_case, 'Data Case: ', data_case)
if data_case == 'train':
dataset_size= args.train_size
if data_case == 'val':
dataset_size= int(args.train_size/4)
elif data_case == 'test':
dataset_size= args.test_size
count=0
final_z= []
final_y= []
final_x= []
for batch_idx, (z, y, x) in enumerate(data_obj):
z= np.expand_dims(z, axis= 0)
y= np.expand_dims(y, axis= 0)
x= np.expand_dims(x, axis= 0)
final_z.append(z)
final_y.append(y)
final_x.append(x)
count+=1
if count >= dataset_size:
break
final_z= np.concatenate(final_z, axis=0)
final_y= np.concatenate(final_y, axis=0)
final_x= np.concatenate(final_x, axis=0)
print(final_z.shape, final_y.shape, final_x.shape)
print(final_z[:5])
print(final_y[:5])
f= base_dir + data_case + '_' + 'x' + '.npy'
np.save(f, final_x)
f= base_dir + data_case + '_' + 'z' + '.npy'
np.save(f, final_z)
f= base_dir + data_case + '_' + 'y' + '.npy'
np.save(f, final_y)
|
CausalRepID-main
|
data/balls_dataset.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
""" DAG Generator.
Generates a dataset out of an acyclic FCM.
Author : Olivier Goudet and Diviyan Kalainathan
Modified by Philippe Brouillard, June 25th 2019
Modified by Divyat Mahajan, December 30th 2022
.. MIT License
..
.. Copyright (c) 2018 Diviyan Kalainathan
..
.. Permission is hereby granted, free of charge, to any person obtaining a copy
.. of this software and associated documentation files (the "Software"), to deal
.. in the Software without restriction, including without limitation the rights
.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
.. copies of the Software, and to permit persons to whom the Software is
.. furnished to do so, subject to the following conditions:
..
.. The above copyright notice and this permission notice shall be included in all
.. copies or substantial portions of the Software.
..
.. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
.. SOFTWARE.
"""
import os
from sklearn.preprocessing import scale
import numpy as np
import pandas as pd
import networkx as nx
from cdt.metrics import get_CPDAG
from data.causal_mechanisms import (LinearMechanism,
Polynomial_Mechanism,
SigmoidAM_Mechanism,
SigmoidMix_Mechanism,
GaussianProcessAdd_Mechanism,
GaussianProcessMix_Mechanism,
ANM_Mechanism,
NN_Mechanism,
NN_Mechanism_Add,
pnl_gp_mechanism,
pnl_mult_mechanism,
PostNonLinear_Mechanism,
Multimodal_X_Mechanism,
Multimodal_Circle_Mechanism,
Multimodal_ADN_Mechanism,
gmm_cause,
gaussian_cause,
variable_gaussian_cause,
uniform_cause,
uniform_cause_positive,
laplace_noise,
normal_noise,
uniform_noise,
nn_noise,
absolute_gaussian_noise,
variable_normal_noise,
NormalCause,
UniformCause,
HardIntervention)
class DagGenerator:
"""Generate an DAG and data given a causal mechanism.
Args:
causal_mechanism (str): currently implemented mechanisms:
['linear', 'polynomial', 'sigmoid_add',
'sigmoid_mix', 'gp_add', 'gp_mix', 'nn'].
noise (str or function): type of noise to use in the generative process
('gaussian', 'uniform' or a custom noise function).
noise_coeff (float): Proportion of noise in the mechanisms.
initial_variable_generator (function): Function used to init variables
of the graph, defaults to a Gaussian Mixture model.
npoints (int): Number of data points to generate.
nodes (int): Number of nodes in the graph to generate.
expected_density (int): Expected number of edge per node.
"""
def __init__(self, causal_mechanism, noise='gaussian',
noise_coeff=.4,
cause=gaussian_cause,
npoints=500, nodes=8, expected_density=3,
dag_type='erdos', rescale=False, f1=None, f2=None):
super().__init__()
self.mechanism = {'linear': LinearMechanism,
'polynomial': Polynomial_Mechanism,
'sigmoid_add': SigmoidAM_Mechanism,
'sigmoid_mix': SigmoidMix_Mechanism,
'gp_add': GaussianProcessAdd_Mechanism,
'gp_mix': GaussianProcessMix_Mechanism,
'anm': ANM_Mechanism,
'nn': NN_Mechanism,
'nn_add': NN_Mechanism_Add,
'pnl_gp_mechanism': pnl_gp_mechanism,
'pnl_mult_mechanism': pnl_mult_mechanism,
'x': Multimodal_X_Mechanism,
'circle': Multimodal_Circle_Mechanism,
'adn': Multimodal_ADN_Mechanism,
'post_nonlinear': PostNonLinear_Mechanism }[causal_mechanism]
self.causal_mechanism = causal_mechanism
self.positive = False
self.rescale = rescale
self.data = pd.DataFrame(None, columns=["V{}".format(i) for i in range(nodes)])
self.nodes = nodes
self.npoints = npoints
try:
self.initial_generator = {'gmm_cause': gmm_cause,
'gaussian': gaussian_cause,
'variable_gaussian': variable_gaussian_cause,
'uniform': uniform_cause,
'uniform_positive': uniform_cause_positive}[cause]
except KeyError:
print('This type of cause does not exist')
self.initial_generator = cause
try:
self.noise = {'gaussian': normal_noise,
'variable_gaussian': variable_normal_noise,
'uniform': uniform_noise,
'laplace': laplace_noise,
'absolute_gaussian': absolute_gaussian_noise,
'nn': nn_noise(variable_normal_noise)}[noise]
except KeyError:
print('This type of noise does not exist')
self.noise = noise
self.noise_coeff = noise_coeff
self.adjacency_matrix = np.zeros((nodes, nodes))
self.expected_density = expected_density
self.dag_type = dag_type
self.cfunctions = None
self.g = None
self.f1 = f1
self.f2 = f2
# Constraints on PNL models to make sure they are identifiable
if self.causal_mechanism == 'pnl_gp_mechanism':
self.noise = laplace_noise
print('Forcing noise to be laplacian')
elif self.causal_mechanism == 'pnl_mult_mechanism':
self.noise = absolute_gaussian_noise
initial_variable_generator = uniform_cause_positive
self.positive = True
print('Forcing noise to be an absolute Gaussian and cause to be uniform with positive support')
def estimate_expected_density(self, n=100):
""" Estimate the expected density(number of edge per node) of a
graph generation by generating multiple graphes
Args:
n: number of graph generated to estimate the density
"""
estimated_density = 0.
for i in range(n):
self.adjacency_matrix = np.zeros((self.nodes, self.nodes))
self.generate_dag()
estimated_density += np.sum(self.adjacency_matrix)
return estimated_density/(n * self.nodes)
def generate_dag(self):
""" Create the structure of the graph """
if self.dag_type == 'erdos':
nb_edges = self.expected_density * self.nodes
self.prob_connection = 2 * nb_edges/(self.nodes**2 - self.nodes)
self.causal_order = np.random.permutation(np.arange(self.nodes))
for i in range(self.nodes - 1):
node = self.causal_order[i]
possible_parents = self.causal_order[(i+1):]
num_parents = np.random.binomial(n=self.nodes - i - 1,
p=self.prob_connection)
parents = np.random.choice(possible_parents, size=num_parents,
replace=False)
self.adjacency_matrix[parents,node] = 1
elif self.dag_type == 'default':
for j in range(1, self.nodes):
nb_parents = np.random.randint(0, min([self.parents_max, j])+1)
for i in np.random.choice(range(0, j), nb_parents, replace=False):
self.adjacency_matrix[i, j] = 1
try:
self.g = nx.DiGraph(self.adjacency_matrix)
assert not list(nx.simple_cycles(self.g))
except AssertionError:
if verbose:
print("Regenerating, graph non valid...")
self.generate_dag()
self.original_adjacency_matrix = np.copy(self.adjacency_matrix)
def change_npoints(self, npoints):
self.npoints = npoints
self.reinitialize()
for f in self.cfunctions:
f.points = npoints
self.data = pd.DataFrame(None, columns=["V{}".format(i) for i in range(self.nodes)])
def init_variables(self, verbose=False):
"""Redefine the causes, mechanisms and the structure of the graph,
called by ``self.generate()`` if never called.
Args:
verbose (bool): Verbosity
"""
self.generate_dag()
# Mechanisms
self.original_cfunctions = []
for i in range(self.nodes):
if sum(self.adjacency_matrix[:, i]):
if self.mechanism is PostNonLinear_Mechanism:
self.original_cfunctions.append(self.mechanism(int(sum(self.adjacency_matrix[:, i])), self.npoints, self.noise, f1=self.f1, f2=self.f2, noise_coeff=self.noise_coeff))
else:
self.original_cfunctions.append(self.mechanism(int(sum(self.adjacency_matrix[:, i])), self.npoints, self.noise, noise_coeff=self.noise_coeff))
else:
self.original_cfunctions.append(self.initial_generator)
# = [self.mechanism(int(sum(self.adjacency_matrix[:, i])), self.npoints, self.noise, noise_coeff=self.noise_coeff) if sum(self.adjacency_matrix[:, i]) else self.initial_generator for i in range(self.nodes)]
# increase nb_step in order to save the gaussian processes
if self.causal_mechanism == 'gp_mix':
for cfunction in self.original_cfunctions:
if isinstance(cfunction, GaussianProcessMix_Mechanism):
cfunction.nb_step += 1
self.cfunctions = self.original_cfunctions[:]
def reinitialize(self):
""" Reinitialize the adjacency matrix and the cfunctions """
self.adjacency_matrix = np.copy(self.original_adjacency_matrix)
# for parametric change:
for i,c in enumerate(self.cfunctions):
if isinstance(c, LinearMechanism) or isinstance(c, NN_Mechanism) or \
isinstance(c, NN_Mechanism_Add) or isinstance(c, Multimodal_ADN_Mechanism) or \
isinstance(c, Multimodal_X_Mechanism):
c.reinit()
self.cfunctions = self.original_cfunctions[:]
def generate(self, npoints=None):
"""Generate data from an FCM defined in ``self.init_variables()``.
Returns:
tuple: (pandas.DataFrame, networkx.DiGraph), respectively the
generated data and graph.
"""
if npoints is None:
npoints = self.npoints
if self.cfunctions is None:
print('Variables initialized')
self.init_variables()
for i in nx.topological_sort(self.g):
# Root cause
if not sum(self.adjacency_matrix[:, i]):
self.data['V{}'.format(i)] = self.cfunctions[i](npoints)
# Generating causes
else:
self.data['V{}'.format(i)] = self.cfunctions[i](self.data.iloc[:, self.adjacency_matrix[:, i].nonzero()[0]].values)
if self.rescale:
self.data['V{}'.format(i)] = scale(self.data['V{}'.format(i)].values)
if self.positive:
self.data['V{}'.format(i)] -= np.min(self.data['V{}'.format(i)].values) - 1e-8
return self.data, self.g
def choose_randomly_intervention_nodes(num_nodes=1, on_child_only=True):
""" Pick randomly nodes
Args:
num_nodes(int): number of nodes to intervene on
on_child_only(boolean): if True, exclude root nodes from the
selection
Returns:
list: contains the indices of the chosen nodes
"""
assert self.cfunctions is not None, "You first need to create a graph"
assert num_nodes <= self.nodes, "num_nodes has to been smaller or equal \
to the total number of nodes"
if on_child_only:
num_parent = np.sum(self.adjacency_matrix, axis=0)
self.avail_nodes = np.arange(self.nodes)[num_parent > 0]
else:
self.avail_nodes = np.arange(self.nodes)
intervention_nodes = np.random.choice(self.avail_nodes, num_nodes, replace=False)
return intervention_nodes
def intervene(self, intervention_type="structural", intervention_nodes=None, npoints=None, target_distribution=None):
""" change the probability distribution of the nodes in
intervention_nodes and then generate data
Args:
intervention_nodes(list of int): sets of nodes to intervene on
Returns:
tuple: (pandas.DataFrame, networkx.DiGraph), respectively the
generated data and graph.
"""
assert self.cfunctions is not None, "You first need to create a graph"
assert intervention_nodes is not None, "You must at least choose one node to do \
an intervention on"
if npoints is not None:
self.change_npoints(npoints)
if intervention_type == "structural":
if target_distribution is None or target_distribution == "normal":
target_distribution = NormalCause()
elif target_distribution == "uniform":
target_distribution = UniformCause()
elif target_distribution == "shifted_normal":
# target_distribution = NormalCause(1, 0.1)
target_distribution = NormalCause(2)
elif target_distribution == "small_uniform":
target_distribution = UniformCause(-0.25, 0.25)
elif target_distribution == "hard_intervention":
target_distribution = HardIntervention(2.0)
for node in intervention_nodes:
self.cfunctions[node] = target_distribution
self.adjacency_matrix[:, node] = 0
elif intervention_type == "parametric":
for node in intervention_nodes:
if isinstance(self.cfunctions[node], LinearMechanism) or \
isinstance(self.cfunctions[node], NN_Mechanism) or \
isinstance(self.cfunctions[node], NN_Mechanism_Add) or \
isinstance(self.cfunctions[node], Multimodal_X_Mechanism) or \
isinstance(self.cfunctions[node], Multimodal_ADN_Mechanism) :
self.cfunctions[node].unique_parametric_intervention()
else:
target_distribution = NormalCause(2)
self.cfunctions[node] = target_distribution
if npoints is not None:
self.generate(npoints)
else:
self.generate()
return self.data.values, self.g
def to_csv(self, fname_radical, **kwargs):
"""
Save the generated data to the csv format by default,
in two separate files: data, and the adjacency matrix of the
corresponding graph.
Args:
fname_radical (str): radical of the file names. Completed by
``_data.csv`` for the data file and ``_target.csv`` for the
adjacency matrix of the generated graph.
\**kwargs: Optional keyword arguments can be passed to pandas.
"""
if self.data is not None:
self.data.to_csv(fname_radical+'_data.csv', index=False, **kwargs)
pd.DataFrame(self.adjacency_matrix).to_csv(fname_radical \
+ '_target.csv',
index=False, **kwargs)
else:
raise ValueError("Graph has not yet been generated. \
Use self.generate() to do so.")
def save_dag_cpdag(self, fname_radical, i_dataset):
dag_path = os.path.join(fname_radical, f'DAG{i_dataset}.npy')
cpdag_path = os.path.join(fname_radical, f'CPDAG{i_dataset}.npy')
cpdag = get_CPDAG(self.adjacency_matrix)
np.save(dag_path, self.adjacency_matrix)
np.save(cpdag_path, cpdag)
def save_data(self, fname_radical, i_dataset):
data_path = os.path.join(fname_radical, f'data{i_dataset}.npy')
np.save(data_path, self.data)
def to_npy(self, fname_radical, i_dataset):
"""
Save the generated data to the npy format,
in two separate files: data and the adjacency matrix of the
corresponding graph.
Args:
fname_radical (str): radical of the file names.
i_dataset (int): i-th dataset
"""
if self.data is not None:
self.save_dag_cpdag(fname_radical, i_dataset)
self.save_data(fname_radical, i_dataset)
else:
raise ValueError("Graph has not yet been generated. \
Use self.generate() to do so.")
|
CausalRepID-main
|
data/dag_generator.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import os
import copy
import numpy as np
import torch
import torch.utils.data as data_utils
from torchvision import datasets, transforms
from sklearn.preprocessing import StandardScaler
#Base Class
from data.data_loader import BaseDataLoader
class FineTuneDataLoader(BaseDataLoader):
def __init__(self, pred_z, true_z, data_dir='', data_case='train'):
super().__init__(data_dir= data_dir, data_case= data_case)
self.data, self.latents= self.load_data_updated(pred_z, true_z, self.data_case)
def load_data_updated(self, pred_z, true_z, data_case):
if data_case == 'train':
x= pred_z['tr']
z= true_z['tr']
elif data_case == 'val':
x= pred_z['val']
z= true_z['val']
elif data_case == 'test':
x= pred_z['te']
z= true_z['te']
scaler = StandardScaler()
scaler.fit(x)
x= scaler.transform(x)
x= torch.tensor(x).float()
z= torch.tensor(z).float()
return x, z
|
CausalRepID-main
|
data/fine_tune_loader.py
|
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import argparse
import glob
import os
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt
plt.rc('font', family='serif')
plt.rc('font', size=12)
import numpy as np
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Deep convexity')
parser.add_argument('--save_dir', type=str, default='results/')
parser.add_argument('--plot_dir', type=str, default='plots/')
parser.add_argument('--n_layers', type=int, default=2)
parser.add_argument('--n_hiddens', type=int, default=50)
parser.add_argument('--n_embeddings', type=int, default=50)
args = parser.parse_args()
functions = [
"ackley",
"michal",
"schwefel",
"sphere",
"tang",
"rastrigin",
"rosenbrock",
"levy",
"energy"
]
plt.figure(figsize=(10, 6))
for f, function in enumerate(functions):
plt.subplot(3, 3, f + 1)
shallows = glob.glob(os.path.join(args.save_dir,
"f={}_emb={}_lay=0_hid={}_*".format(function,
args.n_embeddings,
args.n_hiddens)))
deeps = glob.glob(os.path.join(args.save_dir,
"f={}_emb={}_lay={}_hid={}_*".format(function,
args.n_embeddings,
args.n_layers,
args.n_hiddens)))
print(len(shallows), len(deeps))
for shallow in shallows:
data = np.genfromtxt(shallow)
if len(data):
data = data[:, 2:]
plt.plot(data[:, 0], data[:, 1], color="C2", alpha=0.5)
for deep in deeps:
data = np.genfromtxt(deep)
if len(data):
data = data[:, 2:]
plt.plot(data[:, 0], data[:, 1], color="C1", alpha=0.5)
plt.xscale("log")
if function == "rastrigin" or function == "rosenbrock":
plt.yscale("log")
plt.margins(0.05)
plt.title(function)
plt.tight_layout(pad=0)
pdf_name = os.path.join(args.plot_dir,
'plot_{}_{}.pdf'.format(args.n_layers,
args.n_hiddens))
if not os.path.exists(args.plot_dir):
os.makedirs(args.plot_dir)
plt.savefig(pdf_name)
|
DeepConvexity-master
|
plot.py
|
# Copyright (c) 2017-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
from torch.optim.lr_scheduler import ReduceLROnPlateau
import argparse
import torch
import time
import math
import os
def rescale(x, a, b, c, d):
"""
Rescales variable from [a, b] to [c, d]
"""
return c + ((d - c) / (b - a)) * (x - a)
def ackley(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/ackley.html
"""
a = 20
b = 0.2
c = 2 * math.pi
x = rescale(x, xmin, xmax, -32.768, 32.768)
term1 = x.pow(2).mean(1).sqrt().mul(-b).exp().mul(-a)
term2 = x.mul(c).cos().mean(1).exp().mul(-1)
return term1 + term2 + a + math.exp(1)
def michal(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/michal.html
"""
x = rescale(x, xmin, xmax, 0, math.pi)
mask = torch.arange(1, x.size(1) + 1).view(1, -1)
return x.sin().mul((x.pow(2) * mask / math.pi).sin().pow(20)).sum(1).mul(-1)
def schwefel(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/schwef.html
"""
x = rescale(x, xmin, xmax, -500, 500)
result = x.abs().sqrt().sin().mul(x).sum(1).mul(-1).add(418.9829 * x.size(1))
return result
def sphere(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/spheref.html
"""
x = rescale(x, xmin, xmax, -5.12, 5.12)
return x.pow(2).mean(1)
def tang(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/stybtang.html
"""
x = rescale(x, xmin, xmax, -5, 5)
result = (x.pow(4) - x.pow(2).mul(16) + x.mul(5)).sum(1).div(2)
return result
def rastrigin(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/rastr.html
"""
x = rescale(x, xmin, xmax, -5.12, 5.12)
result = (x.pow(2.0) - x.mul(2.0 * math.pi).cos().mul(10)
).sum(1).add(10 * x.size(1))
return result
def rosenbrock(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/rosen.html
"""
x = rescale(x, xmin, xmax, -5, 10)
term1 = (x[:, 1:] - x[:, :-1].pow(2)).pow(2).mul(100).sum(1)
term2 = x[:, :-1].add(-1).pow(2).sum(1)
return term1 + term2
def levy(x, xmin=-1, xmax=1):
"""
https://www.sfu.ca/~ssurjano/levy.html
"""
x = rescale(x, xmin, xmax, -10, 10)
w = 1 + (x - 1).div(4)
w1 = w[:, 0]
ww = w[:, :x.size(1) - 1]
wd = w[:, x.size(1) - 1]
if ww.dim() == 1:
ww = ww.view(-1, 1)
term1 = w1.mul(math.pi).sin().pow(2)
termw = ww.mul(math.pi).add(1).sin().pow(2).mul(
10).add(1).mul(ww.add(-1).pow(2)).mean(1)
termd = wd.mul(math.pi * 2).sin().pow(2).add(1).mul(wd.add(-1).pow(2))
return term1 + termw + termd
def energy(d):
"""
https://en.wikipedia.org/wiki/Spin_glass
"""
coeff = torch.randn(d, d, d)
def foo(x, xmin=-1, xmax=1):
x = rescale(x, xmin, xmax, -1, 1)
x_norm = x.norm(2, 1).view(-1, 1) + 1e-10
x = x / x_norm * math.sqrt(x.size(1))
bs = x.size(0)
tmp1 = torch.mul(x.view(bs, d, 1), x.view(bs, 1, d))
tmp2 = torch.mul(tmp1.view(bs, d * d, 1), x.view(bs, 1, d))
tmp3 = torch.mul(tmp2.view(bs, d * d * d), coeff.view(1, -1))
return tmp3.sum(1) / d
return foo
test_functions = {
"ackley": ackley,
"michal": michal,
"schwefel": schwefel,
"sphere": sphere,
"tang": tang,
"rastrigin": rastrigin,
"rosenbrock": rosenbrock,
"levy": levy
}
class ScaledTanh(torch.nn.Module):
def __init__(self, a=1):
super(ScaledTanh, self).__init__()
self.a = a
def forward(self, x):
return x.mul(self.a).tanh()
class EmbeddingPerceptron(torch.nn.Module):
"""
Multilayer ReLU perceptron with learnable inputs
"""
def __init__(self, sizes, multiplier=3):
super(EmbeddingPerceptron, self).__init__()
self.inputs = torch.arange(0, sizes[0]).long()
layers = [torch.nn.Embedding(sizes[0], sizes[1])]
for i in range(1, len(sizes) - 1):
layers.append(torch.nn.Linear(sizes[i], sizes[i + 1]))
if i < (len(sizes) - 2):
layers.append(torch.nn.ReLU())
self.net = torch.nn.Sequential(*layers)
net_min, net_max = self().min().item(), self().max().item()
a = 1.7159 / max(abs(net_min), abs(net_max))
self.net = torch.nn.Sequential(self.net, ScaledTanh(a))
def forward(self):
return self.net(self.inputs)
def run_experiment(args, lr):
if args.seed >= 0:
torch.manual_seed(args.seed)
if args.function == "energy":
the_function = energy(args.dimension)
else:
the_function = test_functions[args.function]
network = EmbeddingPerceptron([args.n_embeddings] +
[args.n_hiddens] * args.n_layers +
[args.dimension])
optimizer = torch.optim.SGD(network.parameters(), lr)
scheduler = ReduceLROnPlateau(optimizer,
patience=1,
factor=0.99,
threshold=1e-10,
min_lr=1e-10,
threshold_mode='abs')
time_start = time.time()
history = []
for i in range(args.n_iters):
errors = the_function(network())
mean_error = errors.mean()
min_error = errors.min()
optimizer.zero_grad()
mean_error.backward()
optimizer.step()
history.append([i, time.time() - time_start, min_error.item()])
if min_error.item() != min_error.item():
return None
scheduler.step(mean_error.item())
return torch.Tensor(history)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Deep convexity')
parser.add_argument('--save_dir', type=str, default='results/')
parser.add_argument('--function', type=str, default="ackley")
parser.add_argument('--dimension', type=int, default=50)
parser.add_argument('--n_iters', type=int, default=10000)
parser.add_argument('--min_lr', type=float, default=-6)
parser.add_argument('--max_lr', type=float, default=1)
parser.add_argument('--n_lr', type=int, default=20)
parser.add_argument('--n_embeddings', type=int, default=50)
parser.add_argument('--n_hiddens', type=int, default=50)
parser.add_argument('--n_layers', type=int, default=0)
parser.add_argument('--seed', type=int, default=0)
args = parser.parse_args()
torch.set_default_tensor_type(torch.DoubleTensor)
best_history = None
best_lr = None
best_err = 1e10
for lr in torch.logspace(args.min_lr, args.max_lr, args.n_lr):
history = run_experiment(args, lr)
if history is not None:
err = history[:, -1].min().item()
if err < best_err:
best_lr = lr
best_err = err
best_history = history
#print("+ Success at lr={:1.0e} with value {:.5f}".format(lr, err))
#else:
#print("- Failure at lr={:1.0e}".format(lr))
print("* Best run at lr={:1.0e} with value {:.5f}".format(best_lr, best_err))
fname = "f={}_emb={}_lay={}_hid={}_seed={}.txt".format(args.function,
args.n_embeddings,
args.n_layers,
args.n_hiddens,
args.seed)
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
with open(os.path.join(args.save_dir, fname), "w") as f:
f.write("# {}\n".format(str(vars(args))))
for line in best_history:
f.write("{:1.0e} {:.0f} {:.5f} {:.5f}\n".format(best_lr, *line))
|
DeepConvexity-master
|
main.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from distutils.core import setup
ext_modules = []
cmdclass = {}
with open("requirements.txt", "r") as handle:
install_requires = handle.read().splitlines()
setup(
name="cpa",
version="1.0.0",
description="",
url="http://github.com/facebookresearch/CPA",
author="See README.md",
author_email="[email protected]",
license="MIT",
packages=["cpa"],
cmdclass=cmdclass,
ext_modules=ext_modules,
install_requires=install_requires,
)
|
CPA-main
|
setup.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from collections import defaultdict
import matplotlib.font_manager
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from adjustText import adjust_text
from sklearn.decomposition import KernelPCA
from sklearn.metrics import r2_score
from sklearn.metrics.pairwise import cosine_similarity
FONT_SIZE = 10
font = {"size": FONT_SIZE}
matplotlib.rc("font", **font)
matplotlib.rc("ytick", labelsize=FONT_SIZE)
matplotlib.rc("xtick", labelsize=FONT_SIZE)
class CPAVisuals:
"""
A wrapper for automatic plotting CompPert latent embeddings and dose-response
curve. Sets up prefix for all files and default dictionaries for atomic
perturbations and cell types.
"""
def __init__(
self,
cpa,
fileprefix=None,
perts_palette=None,
covars_palette=None,
plot_params={"fontsize": None},
):
"""
Parameters
----------
cpa : CompPertAPI
Variable from API class.
fileprefix : str, optional (default: None)
Prefix (with path) to the filename to save all embeddings in a
standartized manner. If None, embeddings are not saved to file.
perts_palette : dict (default: None)
Dictionary of colors for the embeddings of perturbations. Keys
correspond to perturbations and values to their colors. If None,
default dicitonary will be set up.
covars_palette : dict (default: None)
Dictionary of colors for the embeddings of covariates. Keys
correspond to covariates and values to their colors. If None,
default dicitonary will be set up.
"""
self.fileprefix = fileprefix
self.perturbation_key = cpa.perturbation_key
self.dose_key = cpa.dose_key
self.covariate_keys = cpa.covariate_keys
self.measured_points = cpa.measured_points
self.unique_perts = cpa.unique_perts
self.unique_covars = cpa.unique_covars
if perts_palette is None:
self.perts_palette = dict(
zip(self.unique_perts, get_palette(len(self.unique_perts)))
)
else:
self.perts_palette = perts_palette
if covars_palette is None:
self.covars_palette = {}
for cov in self.unique_covars:
self.covars_palette[cov] = dict(
zip(
self.unique_covars[cov],
get_palette(len(self.unique_covars[cov]), palette_name="tab10"),
)
)
else:
self.covars_palette = covars_palette
if plot_params["fontsize"] is None:
self.fontsize = FONT_SIZE
else:
self.fontsize = plot_params["fontsize"]
def plot_latent_embeddings(
self,
emb,
titlename="Example",
kind="perturbations",
palette=None,
labels=None,
dimred="KernelPCA",
filename=None,
show_text=True,
):
"""
Parameters
----------
emb : np.array
Multi-dimensional embedding of perturbations or covariates.
titlename : str, optional (default: 'Example')
Title.
kind : int, optional, optional (default: 'perturbations')
Specify if this is embedding of perturbations, covariates or some
other. If it is perturbations or covariates, it will use default
saved dictionaries for colors.
palette : dict, optional (default: None)
If embedding of kind not perturbations or covariates, the user can
specify color dictionary for the embedding.
labels : list, optional (default: None)
Labels for the embeddings.
dimred : str, optional (default: 'KernelPCA')
Dimensionality reduction method for plotting low dimensional
representations. Options: 'KernelPCA', 'UMAPpre', 'UMAPcos', None.
If None, uses first 2 dimensions of the embedding.
filename : str (default: None)
Name of the file to save the plot. If None, will automatically
generate name from prefix file.
"""
if filename is None:
if self.fileprefix is None:
filename = None
file_name_similarity = None
else:
filename = f"{self.fileprefix}_emebdding.png"
file_name_similarity = f"{self.fileprefix}_emebdding_similarity.png"
else:
file_name_similarity = filename.split(".")[0] + "_similarity.png"
if labels is None:
if kind == "perturbations":
palette = self.perts_palette
labels = self.unique_perts
elif kind in self.unique_covars:
palette = self.covars_palette[kind]
labels = self.unique_covars[kind]
if len(emb) < 2:
print(f"Embedding contains only {len(emb)} vectors. Not enough to plot.")
else:
plot_embedding(
fast_dimred(emb, method=dimred),
labels,
show_lines=True,
show_text=show_text,
col_dict=palette,
title=titlename,
file_name=filename,
fontsize=self.fontsize,
)
plot_similarity(
emb,
labels,
col_dict=palette,
fontsize=self.fontsize,
file_name=file_name_similarity,
)
def plot_contvar_response2D(
self,
df_response2D,
df_ref=None,
levels=15,
figsize=(4, 4),
xlims=(0, 1.03),
ylims=(0, 1.03),
palette="coolwarm",
response_name="response",
title_name=None,
fontsize=None,
postfix="",
filename=None,
alpha=0.4,
sizes=(40, 160),
logdose=False,
):
"""
Parameters
----------
df_response2D : pd.DataFrame
Data frame with responses of combinations with columns=(dose1, dose2,
response).
levels: int, optional (default: 15)
Number of levels for contour plot.
response_name : str (default: 'response')
Name of column in df_response to plot as response.
alpha: float (default: 0.4)
Transparency of the background contour.
figsize: tuple (default: (4,4))
Size of the figure in inches.
palette : dict, optional (default: None)
Colors dictionary for perturbations to plot.
title_name : str, optional (default: None)
Title for the plot.
postfix : str, optional (defualt: '')
Postfix to add to the output file name to save the model.
filename : str, optional (defualt: None)
Name of the file to save the plot. If None, will automatically
generate name from prefix file.
logdose: bool (default: False)
If True, dose values will be log10. 0 values will be mapped to
minumum value -1,e.g.
if smallest non-zero dose was 0.001, 0 will be mapped to -4.
"""
sns.set_style("white")
if (filename is None) and not (self.fileprefix is None):
filename = f"{self.fileprefix}_{postfix}response2D.png"
if fontsize is None:
fontsize = self.fontsize
x_name, y_name = df_response2D.columns[:2]
x = df_response2D[x_name].values
y = df_response2D[y_name].values
if logdose:
x = log10_with0(x)
y = log10_with0(y)
z = df_response2D[response_name].values
n = int(np.sqrt(len(x)))
X = x.reshape(n, n)
Y = y.reshape(n, n)
Z = z.reshape(n, n)
fig, ax = plt.subplots(figsize=figsize)
CS = ax.contourf(X, Y, Z, cmap=palette, levels=levels, alpha=alpha)
CS = ax.contour(X, Y, Z, levels=15, cmap=palette)
ax.clabel(CS, inline=1, fontsize=fontsize)
ax.set(xlim=(0, 1), ylim=(0, 1))
ax.axis("equal")
ax.axis("square")
ax.yaxis.set_tick_params(labelsize=fontsize)
ax.xaxis.set_tick_params(labelsize=fontsize)
ax.set_xlabel(x_name, fontsize=fontsize, fontweight="bold")
ax.set_ylabel(y_name, fontsize=fontsize, fontweight="bold")
ax.set_xlim(xlims)
ax.set_ylim(ylims)
# sns.despine(left=False, bottom=False, right=True)
sns.despine()
if not (df_ref is None):
sns.scatterplot(
x=x_name,
y=y_name,
hue="split",
size="num_cells",
sizes=sizes,
alpha=1.0,
palette={"train": "#000000", "training": "#000000", "ood": "#e41a1c"},
data=df_ref,
ax=ax,
)
ax.legend_.remove()
ax.set_title(title_name, fontweight="bold", fontsize=fontsize)
plt.tight_layout()
if filename:
save_to_file(fig, filename)
def plot_contvar_response(
self,
df_response,
response_name="response",
var_name=None,
df_ref=None,
palette=None,
title_name=None,
postfix="",
xlabelname=None,
filename=None,
logdose=False,
fontsize=None,
measured_points=None,
bbox=(1.35, 1.0),
figsize=(7.0, 4.0),
):
"""
Parameters
----------
df_response : pd.DataFrame
Data frame of responses.
response_name : str (default: 'response')
Name of column in df_response to plot as response.
var_name : str, optional (default: None)
Name of conditioning variable, e.g. could correspond to covariates.
df_ref : pd.DataFrame, optional (default: None)
Reference values. Fields for plotting should correspond to
df_response.
palette : dict, optional (default: None)
Colors dictionary for perturbations to plot.
title_name : str, optional (default: None)
Title for the plot.
postfix : str, optional (defualt: '')
Postfix to add to the output file name to save the model.
filename : str, optional (defualt: None)
Name of the file to save the plot. If None, will automatically
generate name from prefix file.
logdose: bool (default: False)
If True, dose values will be log10. 0 values will be mapped to
minumum value -1,e.g.
if smallest non-zero dose was 0.001, 0 will be mapped to -4.
figsize: tuple (default: (7., 4.))
Size of output figure
"""
if (filename is None) and not (self.fileprefix is None):
filename = f"{self.fileprefix}_{postfix}response.png"
if fontsize is None:
fontsize = self.fontsize
if logdose:
dose_name = f"log10-{self.dose_key}"
df_response[dose_name] = log10_with0(df_response[self.dose_key].values)
if not (df_ref is None):
df_ref[dose_name] = log10_with0(df_ref[self.dose_key].values)
else:
dose_name = self.dose_key
if var_name is None:
if len(self.unique_covars) > 1:
var_name = self.covars_key
else:
var_name = self.perturbation_key
if palette is None:
if var_name == self.perturbation_key:
palette = self.perts_palette
elif var_name in self.covariate_keys:
palette = self.covars_palette[var_name]
plot_dose_response(
df_response,
dose_name,
var_name,
xlabelname=xlabelname,
df_ref=df_ref,
response_name=response_name,
title_name=title_name,
use_ref_response=(not (df_ref is None)),
col_dict=palette,
plot_vertical=False,
f1=figsize[0],
f2=figsize[1],
fname=filename,
logscale=measured_points,
measured_points=measured_points,
bbox=bbox,
fontsize=fontsize,
figformat="png",
)
def plot_scatter(
self,
df,
x_axis,
y_axis,
hue=None,
size=None,
style=None,
figsize=(4.5, 4.5),
title=None,
palette=None,
filename=None,
alpha=0.75,
sizes=(30, 90),
text_dict=None,
postfix="",
fontsize=14,
):
sns.set_style("white")
if (filename is None) and not (self.fileprefix is None):
filename = f"{self.fileprefix}_scatter{postfix}.png"
if fontsize is None:
fontsize = self.fontsize
fig = plt.figure(figsize=figsize)
ax = plt.gca()
sns.scatterplot(
x=x_axis,
y=y_axis,
hue=hue,
style=style,
size=size,
sizes=sizes,
alpha=alpha,
palette=palette,
data=df,
)
ax.legend_.remove()
ax.set_xlabel(x_axis, fontsize=fontsize)
ax.set_ylabel(y_axis, fontsize=fontsize)
ax.xaxis.set_tick_params(labelsize=fontsize)
ax.yaxis.set_tick_params(labelsize=fontsize)
ax.set_title(title)
if not (text_dict is None):
texts = []
for label in text_dict.keys():
texts.append(
ax.text(
text_dict[label][0],
text_dict[label][1],
label,
fontsize=fontsize,
)
)
adjust_text(
texts, arrowprops=dict(arrowstyle="-", color="black", lw=0.1), ax=ax
)
plt.tight_layout()
if filename:
save_to_file(fig, filename)
def log10_with0(x):
mx = np.min(x[x > 0])
x[x == 0] = mx / 10
return np.log10(x)
def get_palette(n_colors, palette_name="Set1"):
try:
palette = sns.color_palette(palette_name)
except:
print("Palette not found. Using default palette tab10")
palette = sns.color_palette()
while len(palette) < n_colors:
palette += palette
return palette
def fast_dimred(emb, method="KernelPCA"):
"""
Takes high dimensional embeddings and produces a 2-dimensional representation
for plotting.
emb: np.array
Embeddings matrix.
method: str (default: 'KernelPCA')
Method for dimensionality reduction: KernelPCA, UMAPpre, UMAPcos, tSNE.
If None return first 2 dimensions of the embedding vector.
"""
if method is None:
return emb[:, :2]
elif method == "KernelPCA":
similarity_matrix = cosine_similarity(emb)
np.fill_diagonal(similarity_matrix, 1.0)
X = KernelPCA(n_components=2, kernel="precomputed").fit_transform(
similarity_matrix
)
else:
raise NotImplementedError
return X
def plot_dose_response(
df,
contvar_key,
perturbation_key,
df_ref=None,
response_name="response",
use_ref_response=False,
palette=None,
col_dict=None,
fontsize=8,
measured_points=None,
interpolate=True,
f1=7,
f2=3.0,
bbox=(1.35, 1.0),
ref_name="origin",
title_name="None",
plot_vertical=True,
fname=None,
logscale=None,
xlabelname=None,
figformat="png",
):
"""Plotting decoding of the response with respect to dose.
Params
------
df : `DataFrame`
Table with columns=[perturbation_key, contvar_key, response_name].
The last column is always "response".
contvar_key : str
Name of the column in df for values to use for x axis.
perturbation_key : str
Name of the column in df for the perturbation or covariate to plot.
response_name: str (default: response)
Name of the column in df for values to use for y axis.
df_ref : `DataFrame` (default: None)
Table with the same columns as in df to plot ground_truth or another
condition for comparison. Could
also be used to just extract reference values for x-axis.
use_ref_response : bool (default: False)
A flag indicating if to use values for y axis from df_ref (True) or j
ust to extract reference values for x-axis.
col_dict : dictionary (default: None)
Dictionary with colors for each value in perturbation_key.
bbox : tuple (default: (1.35, 1.))
Coordinates to adjust the legend.
plot_vertical : boolean (default: False)
Flag if to plot reference values for x axis from df_ref dataframe.
f1 : float (default: 7.0))
Width in inches for the plot.
f2 : float (default: 3.0))
Hight in inches for the plot.
fname : str (default: None)
Name of the file to export the plot. The name comes without format
extension.
format : str (default: png)
Format for the file to export the plot.
"""
sns.set_style("white")
if use_ref_response and not (df_ref is None):
df[ref_name] = "predictions"
df_ref[ref_name] = "observations"
if interpolate:
df_plt = pd.concat([df, df_ref])
else:
df_plt = df
else:
df_plt = df
df_plt = df_plt.reset_index()
atomic_drugs = np.unique(df[perturbation_key].values)
if palette is None:
current_palette = get_palette(len(list(atomic_drugs)))
if col_dict is None:
col_dict = dict(zip(list(atomic_drugs), current_palette))
fig = plt.figure(figsize=(f1, f2))
ax = plt.gca()
if use_ref_response:
sns.lineplot(
x=contvar_key,
y=response_name,
palette=col_dict,
hue=perturbation_key,
style=ref_name,
dashes=[(1, 0), (2, 1)],
legend="full",
style_order=["predictions", "observations"],
data=df_plt,
ax=ax,
)
df_ref = df_ref.replace("training_treated", "train")
sns.scatterplot(
x=contvar_key,
y=response_name,
hue="split",
size="num_cells",
sizes=(10, 100),
alpha=1.0,
palette={"train": "#000000", "training": "#000000", "ood": "#e41a1c"},
data=df_ref,
ax=ax,
)
sns.despine()
ax.legend_.remove()
else:
sns.lineplot(
x=contvar_key,
y=response_name,
palette=col_dict,
hue=perturbation_key,
data=df_plt,
ax=ax,
)
ax.legend(loc="upper right", bbox_to_anchor=bbox, fontsize=fontsize)
sns.despine()
if not (title_name is None):
ax.set_title(title_name, fontsize=fontsize, fontweight="bold")
ax.grid("off")
if xlabelname is None:
ax.set_xlabel(contvar_key, fontsize=fontsize)
else:
ax.set_xlabel(xlabelname, fontsize=fontsize)
ax.set_ylabel(f"{response_name}", fontsize=fontsize)
ax.xaxis.set_tick_params(labelsize=fontsize)
ax.yaxis.set_tick_params(labelsize=fontsize)
if not (logscale is None):
ax.set_xticks(np.log10(logscale))
ax.set_xticklabels(logscale, rotation=90)
if not (df_ref is None):
atomic_drugs = np.unique(df_ref[perturbation_key].values)
for drug in atomic_drugs:
x = df_ref[df_ref[perturbation_key] == drug][contvar_key].values
m1 = np.min(df[df[perturbation_key] == drug][response_name].values)
m2 = np.max(df[df[perturbation_key] == drug][response_name].values)
if plot_vertical:
for x_dot in x:
ax.plot(
[x_dot, x_dot],
[m1, m2],
":",
color="black",
linewidth=0.5,
alpha=0.5,
)
fig.tight_layout()
if fname:
plt.savefig(f"{fname}.{figformat}", format=figformat, dpi=600)
return fig
def plot_uncertainty_comb_dose(
cpa_api,
cov,
pert,
N=11,
metric="cosine",
measured_points=None,
cond_key="condition",
figsize=(4, 4),
vmin=None,
vmax=None,
sizes=(40, 160),
df_ref=None,
xlims=(0, 1.03),
ylims=(0, 1.03),
fixed_drugs="",
fixed_doses="",
title="",
filename=None,
):
"""Plotting uncertainty for a single perturbation at a dose range for a
particular covariate.
Params
------
cpa_api
Api object for the model class.
cov : dict
Name of covariate.
pert : str
Name of the perturbation.
N : int
Number of dose values.
metric: str (default: 'cosine')
Metric to evaluate uncertainty.
measured_points : dict (default: None)
A dicitionary of dictionaries. Per each covariate a dictionary with
observed doses per perturbation, e.g. {'covar1': {'pert1':
[0.1, 0.5, 1.0], 'pert2': [0.3]}
cond_key : str (default: 'condition')
Name of the variable to use for plotting.
filename : str (default: None)
Full path to the file to export the plot. File extension should be
included.
Returns
-------
pd.DataFrame of uncertainty estimations.
"""
cov_name = "_".join([cov[cov_key] for cov_key in cpa_api.covariate_keys])
df_list = []
for i in np.round(np.linspace(0, 1, N), decimals=2):
for j in np.round(np.linspace(0, 1, N), decimals=2):
df_list.append(
{
"covariates": cov_name,
"condition": pert + fixed_drugs,
"dose_val": str(i) + "+" + str(j) + fixed_doses,
}
)
df_pred = pd.DataFrame(df_list)
uncert_cos = []
uncert_eucl = []
closest_cond_cos = []
closest_cond_eucl = []
for i in range(df_pred.shape[0]):
(
uncert_cos_,
uncert_eucl_,
closest_cond_cos_,
closest_cond_eucl_,
) = cpa_api.compute_uncertainty(
cov=cov, pert=df_pred.iloc[i]["condition"], dose=df_pred.iloc[i]["dose_val"]
)
uncert_cos.append(uncert_cos_)
uncert_eucl.append(uncert_eucl_)
closest_cond_cos.append(closest_cond_cos_)
closest_cond_eucl.append(closest_cond_eucl_)
df_pred["uncertainty_cosine"] = uncert_cos
df_pred["uncertainty_eucl"] = uncert_eucl
df_pred["closest_cond_cos"] = closest_cond_cos
df_pred["closest_cond_eucl"] = closest_cond_eucl
doses = df_pred.dose_val.apply(lambda x: x.split("+"))
X = np.array(doses.apply(lambda x: x[0]).astype(float)).reshape(N, N)
Y = np.array(doses.apply(lambda x: x[1]).astype(float)).reshape(N, N)
Z = np.array(df_pred[f"uncertainty_{metric}"].values.astype(float)).reshape(N, N)
fig, ax = plt.subplots(1, 1, figsize=figsize)
CS = ax.contourf(X, Y, Z, cmap="coolwarm", levels=20, alpha=1, vmin=vmin, vmax=vmax)
ax.set_xlabel(pert.split("+")[0], fontweight="bold")
ax.set_ylabel(pert.split("+")[1], fontweight="bold")
if not (df_ref is None):
sns.scatterplot(
x=pert.split("+")[0],
y=pert.split("+")[1],
hue="split",
size="num_cells",
sizes=sizes,
alpha=1.0,
palette={"train": "#000000", "training": "#000000", "ood": "#e41a1c"},
data=df_ref,
ax=ax,
)
ax.legend_.remove()
if measured_points:
ticks = measured_points[cov_name][pert]
xticks = [float(x.split("+")[0]) for x in ticks]
yticks = [float(x.split("+")[1]) for x in ticks]
ax.set_xticks(xticks)
ax.set_xticklabels(xticks, rotation=90)
ax.set_yticks(yticks)
fig.colorbar(CS)
sns.despine()
ax.axis("equal")
ax.axis("square")
ax.set_xlim(xlims)
ax.set_ylim(ylims)
ax.set_title(title, fontsize=10, fontweight='bold')
plt.tight_layout()
if filename:
plt.savefig(filename, dpi=600)
return df_pred
def plot_uncertainty_dose(
cpa_api,
cov,
pert,
N=11,
metric="cosine",
measured_points=None,
cond_key="condition",
log=False,
min_dose=None,
filename=None,
):
"""Plotting uncertainty for a single perturbation at a dose range for a
particular covariate.
Params
------
cpa_api
Api object for the model class.
cov : str
Name of covariate.
pert : str
Name of the perturbation.
N : int
Number of dose values.
metric: str (default: 'cosine')
Metric to evaluate uncertainty.
measured_points : dict (default: None)
A dicitionary of dictionaries. Per each covariate a dictionary with
observed doses per perturbation, e.g. {'covar1': {'pert1':
[0.1, 0.5, 1.0], 'pert2': [0.3]}
cond_key : str (default: 'condition')
Name of the variable to use for plotting.
log : boolean (default: False)
A flag if to plot on a log scale.
min_dose : float (default: None)
Minimum dose for the uncertainty estimate.
filename : str (default: None)
Full path to the file to export the plot. File extension should be included.
Returns
-------
pd.DataFrame of uncertainty estimations.
"""
df_list = []
if log:
if min_dose is None:
min_dose = 1e-3
N_val = np.round(np.logspace(np.log10(min_dose), np.log10(1), N), decimals=10)
else:
if min_dose is None:
min_dose = 0
N_val = np.round(np.linspace(min_dose, 1.0, N), decimals=3)
cov_name = "_".join([cov[cov_key] for cov_key in cpa_api.covariate_keys])
for i in N_val:
df_list.append({"covariates": cov_name, "condition": pert, "dose_val": repr(i)})
df_pred = pd.DataFrame(df_list)
uncert_cos = []
uncert_eucl = []
closest_cond_cos = []
closest_cond_eucl = []
for i in range(df_pred.shape[0]):
(
uncert_cos_,
uncert_eucl_,
closest_cond_cos_,
closest_cond_eucl_,
) = cpa_api.compute_uncertainty(
cov=cov, pert=df_pred.iloc[i]["condition"], dose=df_pred.iloc[i]["dose_val"]
)
uncert_cos.append(uncert_cos_)
uncert_eucl.append(uncert_eucl_)
closest_cond_cos.append(closest_cond_cos_)
closest_cond_eucl.append(closest_cond_eucl_)
df_pred["uncertainty_cosine"] = uncert_cos
df_pred["uncertainty_eucl"] = uncert_eucl
df_pred["closest_cond_cos"] = closest_cond_cos
df_pred["closest_cond_eucl"] = closest_cond_eucl
x = df_pred.dose_val.values.astype(float)
y = df_pred[f"uncertainty_{metric}"].values.astype(float)
fig, ax = plt.subplots(1, 1)
ax.plot(x, y)
ax.set_xlabel(pert)
ax.set_ylabel("Uncertainty")
ax.set_title(cov_name)
if log:
ax.set_xscale("log")
if measured_points:
ticks = measured_points[cov_name][pert]
ax.set_xticks(ticks)
ax.set_xticklabels(ticks, rotation=90)
else:
plt.draw()
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
sns.despine()
plt.tight_layout()
if filename:
plt.savefig(filename)
return df_pred
def save_to_file(fig, file_name, file_format=None):
if file_format is None:
if file_name.split(".")[-1] in ["png", "pdf"]:
file_format = file_name.split(".")[-1]
savename = file_name
else:
file_format = "pdf"
savename = f"{file_name}.{file_format}"
else:
savename = file_name
fig.savefig(savename, format=file_format, dpi=600)
print(f"Saved file to: {savename}")
def plot_embedding(
emb,
labels=None,
col_dict=None,
title=None,
show_lines=False,
show_text=False,
show_legend=True,
axis_equal=True,
circle_size=40,
circe_transparency=1.0,
line_transparency=0.8,
line_width=1.0,
fontsize=9,
fig_width=4,
fig_height=4,
file_name=None,
file_format=None,
labels_name=None,
width_ratios=[7, 1],
bbox=(1.3, 0.7),
):
sns.set_style("white")
# create data structure suitable for embedding
df = pd.DataFrame(emb, columns=["dim1", "dim2"])
if not (labels is None):
if labels_name is None:
labels_name = "labels"
df[labels_name] = labels
fig = plt.figure(figsize=(fig_width, fig_height))
ax = plt.gca()
sns.despine(left=False, bottom=False, right=True)
if (col_dict is None) and not (labels is None):
col_dict = get_colors(labels)
sns.scatterplot(
x="dim1",
y="dim2",
hue=labels_name,
palette=col_dict,
alpha=circe_transparency,
edgecolor="none",
s=circle_size,
data=df,
ax=ax,
)
try:
ax.legend_.remove()
except:
pass
if show_lines:
for i in range(len(emb)):
if col_dict is None:
ax.plot(
[0, emb[i, 0]],
[0, emb[i, 1]],
alpha=line_transparency,
linewidth=line_width,
c=None,
)
else:
ax.plot(
[0, emb[i, 0]],
[0, emb[i, 1]],
alpha=line_transparency,
linewidth=line_width,
c=col_dict[labels[i]],
)
if show_text and not (labels is None):
texts = []
labels = np.array(labels)
unique_labels = np.unique(labels)
for label in unique_labels:
idx_label = np.where(labels == label)[0]
texts.append(
ax.text(
np.mean(emb[idx_label, 0]),
np.mean(emb[idx_label, 1]),
label,
#fontsize=fontsize,
)
)
adjust_text(
texts, arrowprops=dict(arrowstyle="-", color="black", lw=0.1), ax=ax
)
if axis_equal:
ax.axis("equal")
ax.axis("square")
if title:
ax.set_title(title, fontweight="bold")
ax.set_xlabel("dim1"),# fontsize=fontsize)
ax.set_ylabel("dim2"),# fontsize=fontsize)
#ax.xaxis.set_tick_params(labelsize=fontsize)
#ax.yaxis.set_tick_params(labelsize=fontsize)
plt.tight_layout()
if file_name:
save_to_file(fig, file_name, file_format)
return plt
def get_colors(labels, palette=None, palette_name=None):
n_colors = len(labels)
if palette is None:
palette = get_palette(n_colors, palette_name)
col_dict = dict(zip(labels, palette[:n_colors]))
return col_dict
def plot_similarity(
emb,
labels=None,
col_dict=None,
fig_width=4,
fig_height=4,
cmap="coolwarm",
fmt="png",
fontsize=7,
file_format=None,
file_name=None,
):
# first we take construct similarity matrix
# add another similarity
similarity_matrix = cosine_similarity(emb)
df = pd.DataFrame(
similarity_matrix,
columns=labels,
index=labels,
)
if col_dict is None:
col_dict = get_colors(labels)
network_colors = pd.Series(df.columns, index=df.columns).map(col_dict)
sns_plot = sns.clustermap(
df,
cmap=cmap,
center=0,
row_colors=network_colors,
col_colors=network_colors,
mask=False,
metric="euclidean",
figsize=(fig_height, fig_width),
vmin=-1,
vmax=1,
fmt=file_format,
)
sns_plot.ax_heatmap.xaxis.set_tick_params(labelsize=fontsize)
sns_plot.ax_heatmap.yaxis.set_tick_params(labelsize=fontsize)
sns_plot.ax_heatmap.axis("equal")
sns_plot.cax.yaxis.set_tick_params(labelsize=fontsize)
if file_name:
save_to_file(sns_plot, file_name, file_format)
from scipy import sparse, stats
from sklearn.metrics import r2_score
def mean_plot(
adata,
pred,
condition_key,
exp_key,
path_to_save="./reg_mean.pdf",
gene_list=None,
deg_list=None,
show=False,
title=None,
verbose=False,
x_coeff=0.30,
y_coeff=0.8,
fontsize=11,
R2_type="R2",
figsize=(3.5, 3.5),
**kwargs,
):
"""
Plots mean matching.
# Parameters
adata: `~anndata.AnnData`
Contains real v
pred: `~anndata.AnnData`
Contains predicted values.
condition_key: Str
adata.obs key to look for x-axis and y-axis condition
exp_key: Str
Condition in adata.obs[condition_key] to be ploted
path_to_save: basestring
Path to save the plot.
gene_list: list
List of gene names to be plotted.
deg_list: list
List of DEGs to compute R2
show: boolean
if True plots the figure
Verbose: boolean
If true prints the value
title: Str
Title of the plot
x_coeff: float
Shifts R2 text horizontally by x_coeff
y_coeff: float
Shifts R2 text vertically by y_coeff
show: bool
if `True`: will show to the plot after saving it.
fontsize: int
Font size for R2 texts
R2_type: Str
How to compute R2 value, should be either Pearson R2 or R2 (sklearn)
Returns:
Calluated R2 values
"""
r2_types = ["R2", "Pearson R2"]
if R2_type not in r2_types:
raise ValueError("R2 caclulation should be one of" + str(r2_types))
if sparse.issparse(adata.X):
adata.X = adata.X.A
if sparse.issparse(pred.X):
pred.X = pred.X.A
diff_genes = deg_list
real = adata[adata.obs[condition_key] == exp_key]
pred = pred[pred.obs[condition_key] == exp_key]
if diff_genes is not None:
if hasattr(diff_genes, "tolist"):
diff_genes = diff_genes.tolist()
real_diff = adata[:, diff_genes][adata.obs[condition_key] == exp_key]
pred_diff = pred[:, diff_genes][pred.obs[condition_key] == exp_key]
x_diff = np.average(pred_diff.X, axis=0)
y_diff = np.average(real_diff.X, axis=0)
if R2_type == "R2":
r2_diff = r2_score(y_diff, x_diff)
if R2_type == "Pearson R2":
m, b, pearson_r_diff, p_value_diff, std_err_diff = stats.linregress(
y_diff, x_diff
)
r2_diff = pearson_r_diff ** 2
if verbose:
print(f"Top {len(diff_genes)} DEGs var: ", r2_diff)
x = np.average(pred.X, axis=0)
y = np.average(real.X, axis=0)
if R2_type == "R2":
r2 = r2_score(y, x)
if R2_type == "Pearson R2":
m, b, pearson_r, p_value, std_err = stats.linregress(y, x)
r2 = pearson_r ** 2
if verbose:
print("All genes var: ", r2)
df = pd.DataFrame({f"{exp_key}_true": x, f"{exp_key}_pred": y})
plt.figure(figsize=figsize)
ax = sns.regplot(x=f"{exp_key}_true", y=f"{exp_key}_pred", data=df)
ax.tick_params(labelsize=fontsize)
if "range" in kwargs:
start, stop, step = kwargs.get("range")
ax.set_xticks(np.arange(start, stop, step))
ax.set_yticks(np.arange(start, stop, step))
ax.set_xlabel("true", fontsize=fontsize)
ax.set_ylabel("pred", fontsize=fontsize)
if gene_list is not None:
for i in gene_list:
j = adata.var_names.tolist().index(i)
x_bar = x[j]
y_bar = y[j]
plt.text(x_bar, y_bar, i, fontsize=fontsize, color="black")
plt.plot(x_bar, y_bar, "o", color="red", markersize=5)
if title is None:
plt.title(f"", fontsize=fontsize, fontweight="bold")
else:
plt.title(title, fontsize=fontsize, fontweight="bold")
ax.text(
max(x) - max(x) * x_coeff,
max(y) - y_coeff * max(y),
r"$\mathrm{R^2_{\mathrm{\mathsf{all\ genes}}}}$= " + f"{r2:.2f}",
fontsize=fontsize,
)
if diff_genes is not None:
ax.text(
max(x) - max(x) * x_coeff,
max(y) - (y_coeff + 0.15) * max(y),
r"$\mathrm{R^2_{\mathrm{\mathsf{DEGs}}}}$= " + f"{r2_diff:.2f}",
fontsize=fontsize,
)
plt.savefig(f"{path_to_save}", bbox_inches="tight", dpi=100)
if show:
plt.show()
plt.close()
if diff_genes is not None:
return r2, r2_diff
else:
return r2
def plot_r2_matrix(pred, adata, de_genes=None, **kwds):
"""Plots a pairwise R2 heatmap between predicted and control conditions.
Params
------
pred : `AnnData`
Must have the field `cov_drug_dose_name`
adata : `AnnData`
Original gene expression data, with the field `cov_drug_dose_name`.
de_genes : `dict`
Dictionary of de_genes, where the keys
match the categories in `cov_drug_dose_name`
"""
r2s_mean = defaultdict(list)
r2s_var = defaultdict(list)
conditions = pred.obs["cov_drug_dose_name"].cat.categories
for cond in conditions:
if de_genes:
degs = de_genes[cond]
y_pred = pred[:, degs][pred.obs["cov_drug_dose_name"] == cond].X
y_true_adata = adata[:, degs]
else:
y_pred = pred[pred.obs["cov_drug_dose_name"] == cond].X
y_true_adata = adata
# calculate r2 between pairwise
for cond_real in conditions:
y_true = y_true_adata[
y_true_adata.obs["cov_drug_dose_name"] == cond_real
].X.toarray()
r2s_mean[cond_real].append(
r2_score(y_true.mean(axis=0), y_pred.mean(axis=0))
)
r2s_var[cond_real].append(r2_score(y_true.var(axis=0), y_pred.var(axis=0)))
for r2_dict in [r2s_mean, r2s_var]:
r2_df = pd.DataFrame.from_dict(r2_dict, orient="index")
r2_df.columns = conditions
plt.figure(figsize=(5, 5))
p = sns.heatmap(
data=r2_df,
vmin=max(r2_df.min(0).min(), 0),
cmap="Blues",
cbar=False,
annot=True,
fmt=".2f",
annot_kws={"fontsize": 5},
**kwds,
)
plt.xticks(fontsize=6)
plt.yticks(fontsize=6)
plt.xlabel("y_true")
plt.ylabel("y_pred")
plt.show()
def arrange_history(history):
print(history.keys())
class CPAHistory:
"""
A wrapper for automatic plotting history of CPA model..
"""
def __init__(self, cpa_api, fileprefix=None):
"""
Parameters
----------
cpa_api : dict
cpa api instance
fileprefix : str, optional (default: None)
Prefix (with path) to the filename to save all embeddings in a
standartized manner. If None, embeddings are not saved to file.
"""
self.history = cpa_api.history
self.time = self.history["elapsed_time_min"]
self.losses_list = [
"loss_reconstruction",
"loss_adv_drugs",
"loss_adv_covariates",
]
self.penalties_list = ["penalty_adv_drugs", "penalty_adv_covariates"]
subset_keys = ["epoch"] + self.losses_list + self.penalties_list
self.losses = pd.DataFrame(
dict((k, self.history[k]) for k in subset_keys if k in self.history)
)
self.header = ["mean", "mean_DE", "var", "var_DE"]
self.eval_metrics = False
if 'perturbation disentanglement' in list (self.history): #check that metrics were evaluated
self.eval_metrics = True
self.metrics = pd.DataFrame(columns=["epoch", "split"] + self.header)
for split in ["training", "test", "ood"]:
df_split = pd.DataFrame(np.array(self.history[split]), columns=self.header)
df_split["split"] = split
df_split["epoch"] = self.history["stats_epoch"]
self.metrics = pd.concat([self.metrics, df_split])
self.covariate_names = list(cpa_api.datasets["training"].covariate_names)
self.disent = pd.DataFrame(
dict(
(k, self.history[k])
for k in
['perturbation disentanglement']
+ [f'{cov} disentanglement' for cov in self.covariate_names]
if k in self.history
)
)
self.disent["epoch"] = self.history["stats_epoch"]
self.fileprefix = fileprefix
def print_time(self):
print(f"Computation time: {self.time:.0f} min")
def plot_losses(self, filename=None):
"""
Parameters
----------
filename : str (default: None)
Name of the file to save the plot. If None, will automatically
generate name from prefix file.
"""
if filename is None:
if self.fileprefix is None:
filename = None
else:
filename = f"{self.fileprefix}_history_losses.png"
fig, ax = plt.subplots(1, 4, sharex=True, sharey=False, figsize=(12, 3))
i = 0
for i in range(4):
if i < 3:
ax[i].plot(
self.losses["epoch"].values, self.losses[self.losses_list[i]].values
)
ax[i].set_title(self.losses_list[i], fontweight="bold")
else:
ax[i].plot(
self.losses["epoch"].values, self.losses[self.penalties_list].values
)
ax[i].set_title("Penalties", fontweight="bold")
sns.despine()
plt.tight_layout()
if filename:
save_to_file(fig, filename)
def plot_r2_metrics(self, epoch_min=0, filename=None):
"""
Parameters
----------
epoch_min : int (default: 0)
Epoch from which to show metrics history plot. Done for readability.
filename : str (default: None)
Name of the file to save the plot. If None, will automatically
generate name from prefix file.
"""
assert self.eval_metrics == True, 'The evaluation metrics were not computed'
if filename is None:
if self.fileprefix is None:
filename = None
else:
filename = f"{self.fileprefix}_history_metrics.png"
df = self.metrics.melt(id_vars=["epoch", "split"])
col_dict = dict(
zip(["training", "test", "ood"], ["#377eb8", "#4daf4a", "#e41a1c"])
)
fig, axs = plt.subplots(2, 2, sharex=True, sharey=False, figsize=(7, 5))
ax = plt.gca()
i = 0
for i1 in range(2):
for i2 in range(2):
sns.lineplot(
data=df[
(df["variable"] == self.header[i]) & (df["epoch"] > epoch_min)
],
x="epoch",
y="value",
palette=col_dict,
hue="split",
ax=axs[i1, i2],
)
axs[i1, i2].set_title(self.header[i], fontweight="bold")
i += 1
sns.despine()
plt.tight_layout()
if filename:
save_to_file(fig, filename)
def plot_disentanglement_metrics(self, epoch_min=0, filename=None):
"""
Parameters
----------
epoch_min : int (default: 0)
Epoch from which to show metrics history plot. Done for readability.
filename : str (default: None)
Name of the file to save the plot. If None, will automatically
generate name from prefix file.
"""
assert self.eval_metrics == True, 'The evaluation metrics were not computed'
if filename is None:
if self.fileprefix is None:
filename = None
else:
filename = f"{self.fileprefix}_history_metrics.png"
fig, axs = plt.subplots(
1,
1+len(self.covariate_names),
sharex=True,
sharey=False,
figsize=(2 + 5*(len(self.covariate_names)), 3)
)
ax = plt.gca()
sns.lineplot(
data=self.disent[self.disent["epoch"] > epoch_min],
x="epoch",
y="perturbation disentanglement",
legend=False,
ax=axs[0],
)
axs[0].set_title("perturbation disent", fontweight="bold")
for i, cov in enumerate(self.covariate_names):
sns.lineplot(
data=self.disent[self.disent['epoch'] > epoch_min],
x="epoch",
y=f"{cov} disentanglement",
legend=False,
ax=axs[1+i]
)
axs[1+i].set_title(f"{cov} disent", fontweight="bold")
fig.tight_layout()
sns.despine()
|
CPA-main
|
cpa/plotting.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from cpa.api import API
from cpa.plotting import CPAVisuals
|
CPA-main
|
cpa/__init__.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
from http.client import RemoteDisconnected
import json
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn
class NBLoss(torch.nn.Module):
def __init__(self):
super(NBLoss, self).__init__()
def forward(self, mu, y, theta, eps=1e-8):
"""Negative binomial negative log-likelihood. It assumes targets `y` with n
rows and d columns, but estimates `yhat` with n rows and 2d columns.
The columns 0:d of `yhat` contain estimated means, the columns d:2*d of
`yhat` contain estimated variances. This module assumes that the
estimated mean and inverse dispersion are positive---for numerical
stability, it is recommended that the minimum estimated variance is
greater than a small number (1e-3).
Parameters
----------
yhat: Tensor
Torch Tensor of reeconstructed data.
y: Tensor
Torch Tensor of ground truth data.
eps: Float
numerical stability constant.
"""
if theta.ndimension() == 1:
# In this case, we reshape theta for broadcasting
theta = theta.view(1, theta.size(0))
log_theta_mu_eps = torch.log(theta + mu + eps)
res = (
theta * (torch.log(theta + eps) - log_theta_mu_eps)
+ y * (torch.log(mu + eps) - log_theta_mu_eps)
+ torch.lgamma(y + theta)
- torch.lgamma(theta)
- torch.lgamma(y + 1)
)
res = _nan2inf(res)
return -torch.mean(res)
def _nan2inf(x):
return torch.where(torch.isnan(x), torch.zeros_like(x) + np.inf, x)
class MLP(torch.nn.Module):
"""
A multilayer perceptron with ReLU activations and optional BatchNorm.
"""
def __init__(self, sizes, batch_norm=True, last_layer_act="linear"):
super(MLP, self).__init__()
layers = []
for s in range(len(sizes) - 1):
layers += [
torch.nn.Linear(sizes[s], sizes[s + 1]),
torch.nn.BatchNorm1d(sizes[s + 1])
if batch_norm and s < len(sizes) - 2
else None,
torch.nn.ReLU(),
]
layers = [l for l in layers if l is not None][:-1]
self.activation = last_layer_act
if self.activation == "linear":
pass
elif self.activation == "ReLU":
self.relu = torch.nn.ReLU()
else:
raise ValueError("last_layer_act must be one of 'linear' or 'ReLU'")
self.network = torch.nn.Sequential(*layers)
def forward(self, x):
if self.activation == "ReLU":
x = self.network(x)
dim = x.size(1) // 2
return torch.cat((self.relu(x[:, :dim]), x[:, dim:]), dim=1)
return self.network(x)
class GeneralizedSigmoid(torch.nn.Module):
"""
Sigmoid, log-sigmoid or linear functions for encoding dose-response for
drug perurbations.
"""
def __init__(self, dim, device, nonlin="sigmoid"):
"""Sigmoid modeling of continuous variable.
Params
------
nonlin : str (default: logsigm)
One of logsigm, sigm.
"""
super(GeneralizedSigmoid, self).__init__()
self.nonlin = nonlin
self.beta = torch.nn.Parameter(
torch.ones(1, dim, device=device), requires_grad=True
)
self.bias = torch.nn.Parameter(
torch.zeros(1, dim, device=device), requires_grad=True
)
def forward(self, x):
if self.nonlin == "logsigm":
c0 = self.bias.sigmoid()
return (torch.log1p(x) * self.beta + self.bias).sigmoid() - c0
elif self.nonlin == "sigm":
c0 = self.bias.sigmoid()
return (x * self.beta + self.bias).sigmoid() - c0
else:
return x
def one_drug(self, x, i):
if self.nonlin == "logsigm":
c0 = self.bias[0][i].sigmoid()
return (torch.log1p(x) * self.beta[0][i] + self.bias[0][i]).sigmoid() - c0
elif self.nonlin == "sigm":
c0 = self.bias[0][i].sigmoid()
return (x * self.beta[0][i] + self.bias[0][i]).sigmoid() - c0
else:
return x
class CPA(torch.nn.Module):
"""
Our main module, the CPA autoencoder
"""
def __init__(
self,
num_genes,
num_drugs,
num_covariates,
device="cuda",
seed=0,
patience=5,
loss_ae="gauss",
doser_type="mlp",
decoder_activation="linear",
hparams="",
):
super(CPA, self).__init__()
# set generic attributes
self.num_genes = num_genes
self.num_drugs = num_drugs
self.num_covariates = num_covariates
self.device = device
self.seed = seed
self.loss_ae = loss_ae
# early-stopping
self.patience = patience
self.best_score = -1e3
self.patience_trials = 0
# set hyperparameters
self.set_hparams_(hparams)
# set models
self.encoder = MLP(
[num_genes]
+ [self.hparams["autoencoder_width"]] * self.hparams["autoencoder_depth"]
+ [self.hparams["dim"]]
)
self.decoder = MLP(
[self.hparams["dim"]]
+ [self.hparams["autoencoder_width"]] * self.hparams["autoencoder_depth"]
+ [num_genes * 2],
last_layer_act=decoder_activation,
)
self.adversary_drugs = MLP(
[self.hparams["dim"]]
+ [self.hparams["adversary_width"]] * self.hparams["adversary_depth"]
+ [num_drugs]
)
self.loss_adversary_drugs = torch.nn.BCEWithLogitsLoss()
self.doser_type = doser_type
if doser_type == "mlp":
self.dosers = torch.nn.ModuleList()
for _ in range(num_drugs):
self.dosers.append(
MLP(
[1]
+ [self.hparams["dosers_width"]] * self.hparams["dosers_depth"]
+ [1],
batch_norm=False,
)
)
else:
self.dosers = GeneralizedSigmoid(num_drugs, self.device, nonlin=doser_type)
if self.num_covariates == [0]:
pass
else:
assert 0 not in self.num_covariates
self.adversary_covariates = []
self.loss_adversary_covariates = []
self.covariates_embeddings = (
[]
) # TODO: Continue with checking that dict assignment is possible via covaraites names and if dict are possible to use in optimisation
for num_covariate in self.num_covariates:
self.adversary_covariates.append(
MLP(
[self.hparams["dim"]]
+ [self.hparams["adversary_width"]]
* self.hparams["adversary_depth"]
+ [num_covariate]
)
)
self.loss_adversary_covariates.append(torch.nn.CrossEntropyLoss())
self.covariates_embeddings.append(
torch.nn.Embedding(num_covariate, self.hparams["dim"])
)
self.covariates_embeddings = torch.nn.Sequential(
*self.covariates_embeddings
)
self.drug_embeddings = torch.nn.Embedding(self.num_drugs, self.hparams["dim"])
# losses
if self.loss_ae == "nb":
self.loss_autoencoder = NBLoss()
elif self.loss_ae == 'gauss':
self.loss_autoencoder = nn.GaussianNLLLoss()
self.iteration = 0
self.to(self.device)
# optimizers
has_drugs = self.num_drugs > 0
has_covariates = self.num_covariates[0] > 0
get_params = lambda model, cond: list(model.parameters()) if cond else []
_parameters = (
get_params(self.encoder, True)
+ get_params(self.decoder, True)
+ get_params(self.drug_embeddings, has_drugs)
)
for emb in self.covariates_embeddings:
_parameters.extend(get_params(emb, has_covariates))
self.optimizer_autoencoder = torch.optim.Adam(
_parameters,
lr=self.hparams["autoencoder_lr"],
weight_decay=self.hparams["autoencoder_wd"],
)
_parameters = get_params(self.adversary_drugs, has_drugs)
for adv in self.adversary_covariates:
_parameters.extend(get_params(adv, has_covariates))
self.optimizer_adversaries = torch.optim.Adam(
_parameters,
lr=self.hparams["adversary_lr"],
weight_decay=self.hparams["adversary_wd"],
)
if has_drugs:
self.optimizer_dosers = torch.optim.Adam(
self.dosers.parameters(),
lr=self.hparams["dosers_lr"],
weight_decay=self.hparams["dosers_wd"],
)
# learning rate schedulers
self.scheduler_autoencoder = torch.optim.lr_scheduler.StepLR(
self.optimizer_autoencoder, step_size=self.hparams["step_size_lr"]
)
self.scheduler_adversary = torch.optim.lr_scheduler.StepLR(
self.optimizer_adversaries, step_size=self.hparams["step_size_lr"]
)
if has_drugs:
self.scheduler_dosers = torch.optim.lr_scheduler.StepLR(
self.optimizer_dosers, step_size=self.hparams["step_size_lr"]
)
self.history = {"epoch": [], "stats_epoch": []}
def set_hparams_(self, hparams):
"""
Set hyper-parameters to default values or values fixed by user for those
hyper-parameters specified in the JSON string `hparams`.
"""
self.hparams = {
"dim": 128,
"dosers_width": 128,
"dosers_depth": 2,
"dosers_lr": 4e-3,
"dosers_wd": 1e-7,
"autoencoder_width": 128,
"autoencoder_depth": 3,
"adversary_width": 64,
"adversary_depth": 2,
"reg_adversary": 60,
"penalty_adversary": 60,
"autoencoder_lr": 3e-4,
"adversary_lr": 3e-4,
"autoencoder_wd": 4e-7,
"adversary_wd": 4e-7,
"adversary_steps": 3,
"batch_size": 256,
"step_size_lr": 45,
}
# the user may fix some hparams
if hparams != "":
if isinstance(hparams, str):
self.hparams.update(json.loads(hparams))
else:
self.hparams.update(hparams)
return self.hparams
def move_inputs_(self, genes, drugs, covariates):
"""
Move minibatch tensors to CPU/GPU.
"""
if genes.device.type != self.device:
genes = genes.to(self.device)
if drugs is not None:
drugs = drugs.to(self.device)
if covariates is not None:
covariates = [cov.to(self.device) for cov in covariates]
return (genes, drugs, covariates)
def compute_drug_embeddings_(self, drugs):
"""
Compute sum of drug embeddings, each of them multiplied by its
dose-response curve.
"""
if self.doser_type == "mlp":
doses = []
for d in range(drugs.size(1)):
this_drug = drugs[:, d].view(-1, 1)
doses.append(self.dosers[d](this_drug).sigmoid() * this_drug.gt(0))
return torch.cat(doses, 1) @ self.drug_embeddings.weight
else:
return self.dosers(drugs) @ self.drug_embeddings.weight
def predict(
self,
genes,
drugs,
covariates,
return_latent_basal=False,
return_latent_treated=False,
):
"""
Predict "what would have the gene expression `genes` been, had the
cells in `genes` with cell types `cell_types` been treated with
combination of drugs `drugs`.
"""
genes, drugs, covariates = self.move_inputs_(genes, drugs, covariates)
if self.loss_ae == 'nb':
genes = torch.log1p(genes)
latent_basal = self.encoder(genes)
latent_treated = latent_basal
if self.num_drugs > 0:
latent_treated = latent_treated + self.compute_drug_embeddings_(drugs)
if self.num_covariates[0] > 0:
for i, emb in enumerate(self.covariates_embeddings):
emb = emb.to(self.device)
latent_treated = latent_treated + emb(
covariates[i].argmax(1)
) #argmax because OHE
gene_reconstructions = self.decoder(latent_treated)
if self.loss_ae == 'gauss':
# convert variance estimates to a positive value in [1e-3, \infty)
dim = gene_reconstructions.size(1) // 2
gene_means = gene_reconstructions[:, :dim]
gene_vars = F.softplus(gene_reconstructions[:, dim:]).add(1e-3)
#gene_vars = gene_reconstructions[:, dim:].exp().add(1).log().add(1e-3)
if self.loss_ae == 'nb':
gene_means = F.softplus(gene_means).add(1e-3)
#gene_reconstructions[:, :dim] = torch.clamp(gene_reconstructions[:, :dim], min=1e-4, max=1e4)
#gene_reconstructions[:, dim:] = torch.clamp(gene_reconstructions[:, dim:], min=1e-4, max=1e4)
gene_reconstructions = torch.cat([gene_means, gene_vars], dim=1)
if return_latent_basal:
if return_latent_treated:
return gene_reconstructions, latent_basal, latent_treated
else:
return gene_reconstructions, latent_basal
if return_latent_treated:
return gene_reconstructions, latent_treated
return gene_reconstructions
def early_stopping(self, score):
"""
Decays the learning rate, and possibly early-stops training.
"""
self.scheduler_autoencoder.step()
self.scheduler_adversary.step()
self.scheduler_dosers.step()
if score > self.best_score:
self.best_score = score
self.patience_trials = 0
else:
self.patience_trials += 1
return self.patience_trials > self.patience
def update(self, genes, drugs, covariates):
"""
Update CPA's parameters given a minibatch of genes, drugs, and
cell types.
"""
genes, drugs, covariates = self.move_inputs_(genes, drugs, covariates)
gene_reconstructions, latent_basal = self.predict(
genes,
drugs,
covariates,
return_latent_basal=True,
)
dim = gene_reconstructions.size(1) // 2
gene_means = gene_reconstructions[:, :dim]
gene_vars = gene_reconstructions[:, dim:]
reconstruction_loss = self.loss_autoencoder(gene_means, genes, gene_vars)
adversary_drugs_loss = torch.tensor([0.0], device=self.device)
if self.num_drugs > 0:
adversary_drugs_predictions = self.adversary_drugs(latent_basal)
adversary_drugs_loss = self.loss_adversary_drugs(
adversary_drugs_predictions, drugs.gt(0).float()
)
adversary_covariates_loss = torch.tensor(
[0.0], device=self.device
)
if self.num_covariates[0] > 0:
adversary_covariate_predictions = []
for i, adv in enumerate(self.adversary_covariates):
adv = adv.to(self.device)
adversary_covariate_predictions.append(adv(latent_basal))
adversary_covariates_loss += self.loss_adversary_covariates[i](
adversary_covariate_predictions[-1], covariates[i].argmax(1)
)
# two place-holders for when adversary is not executed
adversary_drugs_penalty = torch.tensor([0.0], device=self.device)
adversary_covariates_penalty = torch.tensor([0.0], device=self.device)
if self.iteration % self.hparams["adversary_steps"]:
def compute_gradients(output, input):
grads = torch.autograd.grad(output, input, create_graph=True)
grads = grads[0].pow(2).mean()
return grads
if self.num_drugs > 0:
adversary_drugs_penalty = compute_gradients(
adversary_drugs_predictions.sum(), latent_basal
)
if self.num_covariates[0] > 0:
adversary_covariates_penalty = torch.tensor([0.0], device=self.device)
for pred in adversary_covariate_predictions:
adversary_covariates_penalty += compute_gradients(
pred.sum(), latent_basal
) # TODO: Adding up tensor sum, is that right?
self.optimizer_adversaries.zero_grad()
(
adversary_drugs_loss
+ self.hparams["penalty_adversary"] * adversary_drugs_penalty
+ adversary_covariates_loss
+ self.hparams["penalty_adversary"] * adversary_covariates_penalty
).backward()
self.optimizer_adversaries.step()
else:
self.optimizer_autoencoder.zero_grad()
if self.num_drugs > 0:
self.optimizer_dosers.zero_grad()
(
reconstruction_loss
- self.hparams["reg_adversary"] * adversary_drugs_loss
- self.hparams["reg_adversary"] * adversary_covariates_loss
).backward()
self.optimizer_autoencoder.step()
if self.num_drugs > 0:
self.optimizer_dosers.step()
self.iteration += 1
return {
"loss_reconstruction": reconstruction_loss.item(),
"loss_adv_drugs": adversary_drugs_loss.item(),
"loss_adv_covariates": adversary_covariates_loss.item(),
"penalty_adv_drugs": adversary_drugs_penalty.item(),
"penalty_adv_covariates": adversary_covariates_penalty.item(),
}
@classmethod
def defaults(self):
"""
Returns the list of default hyper-parameters for CPA
"""
return self.set_hparams_(self, "")
|
CPA-main
|
cpa/model.py
|
import copy
import itertools
import os
import pprint
import time
from collections import defaultdict
from typing import Optional, Union, Tuple
import numpy as np
import pandas as pd
import scanpy as sc
import torch
from torch.distributions import (
NegativeBinomial,
Normal
)
from cpa.train import evaluate, prepare_cpa
from cpa.helper import _convert_mean_disp_to_counts_logits
from sklearn.metrics import r2_score
from sklearn.metrics.pairwise import cosine_distances, euclidean_distances
from tqdm import tqdm
class API:
"""
API for CPA model to make it compatible with scanpy.
"""
def __init__(
self,
data,
perturbation_key="condition",
covariate_keys=["cell_type"],
split_key="split",
dose_key="dose_val",
control=None,
doser_type="mlp",
decoder_activation="linear",
loss_ae="gauss",
patience=200,
seed=0,
pretrained=None,
device="cuda",
save_dir="/tmp/", # directory to save the model
hparams={},
only_parameters=False,
):
"""
Parameters
----------
data : str or `AnnData`
AnndData object or a full path to the file in the .h5ad format.
covariate_keys : list (default: ['cell_type'])
List of names in the .obs of AnnData that should be used as
covariates.
split_key : str (default: 'split')
Name of the column in .obs of AnnData to use for splitting the
dataset into train, test and validation.
perturbation_key : str (default: 'condition')
Name of the column in .obs of AnnData to use for perturbation
variable.
dose_key : str (default: 'dose_val')
Name of the column in .obs of AnnData to use for continious
covariate.
doser_type : str (default: 'mlp')
Type of the nonlinearity in the latent space for the continious
covariate encoding: sigm, logsigm, mlp.
decoder_activation : str (default: 'linear')
Last layer of the decoder.
loss_ae : str (default: 'gauss')
Loss (currently only gaussian loss is supported).
patience : int (default: 200)
Patience for early stopping.
seed : int (default: 0)
Random seed.
pretrained : str (default: None)
Full path to the pretrained model.
only_parameters : bool (default: False)
Whether to load only arguments or also weights from pretrained model.
save_dir : str (default: '/tmp/')
Folder to save the model.
device : str (default: 'cpu')
Device for model computations. If None, will try to use CUDA if
available.
hparams : dict (default: {})
Parameters for the architecture of the CPA model.
control: str
Obs columns with booleans that identify control. If it is not provided
the model will look for them in adata.obs["control"]
"""
args = locals()
del args["self"]
if not (pretrained is None):
state, self.used_args, self.history = torch.load(
pretrained, map_location=torch.device(device)
)
self.args = self.used_args
self.args["data"] = data
self.args["covariate_keys"] = covariate_keys
self.args["device"] = device
self.args["control"] = control
if only_parameters:
state = None
print(f"Loaded ARGS of the model from:\t{pretrained}")
else:
print(f"Loaded pretrained model from:\t{pretrained}")
else:
state = None
self.args = args
self.model, self.datasets = prepare_cpa(self.args, state_dict=state)
if not (pretrained is None) and (not only_parameters):
self.model.history = self.history
self.args["save_dir"] = save_dir
self.args["hparams"] = self.model.hparams
if not (save_dir is None):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
dataset = self.datasets["training"]
self.perturbation_key = dataset.perturbation_key
self.dose_key = dataset.dose_key
self.covariate_keys = covariate_keys # very important, specifies the order of
# covariates during training
self.min_dose = dataset.drugs[dataset.drugs > 0].min().item()
self.max_dose = dataset.drugs[dataset.drugs > 0].max().item()
self.var_names = dataset.var_names
self.unique_perts = list(dataset.perts_dict.keys())
self.unique_covars = {}
for cov in dataset.covars_dict:
self.unique_covars[cov] = list(dataset.covars_dict[cov].keys())
self.num_drugs = dataset.num_drugs
self.perts_dict = dataset.perts_dict
self.covars_dict = dataset.covars_dict
self.drug_ohe = torch.Tensor(list(dataset.perts_dict.values()))
self.covars_ohe = {}
for cov in dataset.covars_dict:
self.covars_ohe[cov] = torch.LongTensor(
list(dataset.covars_dict[cov].values())
)
self.emb_covars = {}
for cov in dataset.covars_dict:
self.emb_covars[cov] = None
self.emb_perts = None
self.seen_covars_perts = None
self.comb_emb = None
self.control_cat = None
self.seen_covars_perts = {}
for k in self.datasets.keys():
self.seen_covars_perts[k] = np.unique(self.datasets[k].pert_categories)
self.measured_points = {}
self.num_measured_points = {}
for k in self.datasets.keys():
self.measured_points[k] = {}
self.num_measured_points[k] = {}
for pert in np.unique(self.datasets[k].pert_categories):
num_points = len(np.where(self.datasets[k].pert_categories == pert)[0])
self.num_measured_points[k][pert] = num_points
*cov_list, drug, dose = pert.split("_")
cov = "_".join(cov_list)
if not ("+" in dose):
dose = float(dose)
if cov in self.measured_points[k].keys():
if drug in self.measured_points[k][cov].keys():
self.measured_points[k][cov][drug].append(dose)
else:
self.measured_points[k][cov][drug] = [dose]
else:
self.measured_points[k][cov] = {drug: [dose]}
self.measured_points["all"] = copy.deepcopy(self.measured_points["training"])
for cov in self.measured_points["ood"].keys():
for pert in self.measured_points["ood"][cov].keys():
if pert in self.measured_points["training"][cov].keys():
self.measured_points["all"][cov][pert] = (
self.measured_points["training"][cov][pert].copy()
+ self.measured_points["ood"][cov][pert].copy()
)
else:
self.measured_points["all"][cov][pert] = self.measured_points[
"ood"
][cov][pert].copy()
def load_from_old(self, pretrained):
"""
Parameters
----------
pretrained : str
Full path to the pretrained model.
"""
print(f"Loaded pretrained model from:\t{pretrained}")
state, self.used_args, self.history = torch.load(
pretrained, map_location=torch.device(self.args["device"])
)
self.model.load_state_dict(state_dict)
self.model.history = self.history
def print_args(self):
pprint.pprint(self.args)
def load(self, pretrained):
"""
Parameters
----------
pretrained : str
Full path to the pretrained model.
""" # TODO fix compatibility
print(f"Loaded pretrained model from:\t{pretrained}")
state, self.used_args, self.history = torch.load(
pretrained, map_location=torch.device(self.args["device"])
)
self.model.load_state_dict(state_dict)
def train(
self,
max_epochs=1,
checkpoint_freq=20,
run_eval=False,
max_minutes=60,
filename="model.pt",
batch_size=None,
save_dir=None,
seed=0,
):
"""
Parameters
----------
max_epochs : int (default: 1)
Maximum number epochs for training.
checkpoint_freq : int (default: 20)
Checkoint frequencty to save intermediate results.
run_eval : bool (default: False)
Whether or not to run disentanglement and R2 evaluation during training.
max_minutes : int (default: 60)
Maximum computation time in minutes.
filename : str (default: 'model.pt')
Name of the file without the directoty path to save the model.
Name should be with .pt extension.
batch_size : int, optional (default: None)
Batch size for training. If None, uses default batch size specified
in hparams.
save_dir : str, optional (default: None)
Full path to the folder to save the model. If None, will use from
the path specified during init.
seed : int (default: None)
Random seed. If None, uses default random seed specified during init.
"""
args = locals()
del args["self"]
if batch_size is None:
batch_size = self.model.hparams["batch_size"]
args["batch_size"] = batch_size
self.args["batch_size"] = batch_size
if save_dir is None:
save_dir = self.args["save_dir"]
print("Results will be saved to the folder:", save_dir)
self.datasets.update(
{
"loader_tr": torch.utils.data.DataLoader(
self.datasets["training"], batch_size=batch_size, shuffle=True
)
}
)
self.model.train()
start_time = time.time()
pbar = tqdm(range(max_epochs), ncols=80)
try:
for epoch in pbar:
epoch_training_stats = defaultdict(float)
for data in self.datasets["loader_tr"]:
genes, drugs, covariates = data[0], data[1], data[2:]
minibatch_training_stats = self.model.update(
genes, drugs, covariates
)
for key, val in minibatch_training_stats.items():
epoch_training_stats[key] += val
for key, val in epoch_training_stats.items():
epoch_training_stats[key] = val / len(self.datasets["loader_tr"])
if not (key in self.model.history.keys()):
self.model.history[key] = []
self.model.history[key].append(epoch_training_stats[key])
self.model.history["epoch"].append(epoch)
ellapsed_minutes = (time.time() - start_time) / 60
self.model.history["elapsed_time_min"] = ellapsed_minutes
# decay learning rate if necessary
# also check stopping condition: patience ran out OR
# time ran out OR max epochs achieved
stop = ellapsed_minutes > max_minutes or (epoch == max_epochs - 1)
pbar.set_description(
f"Rec: {epoch_training_stats['loss_reconstruction']:.4f}, "
+ f"AdvPert: {epoch_training_stats['loss_adv_drugs']:.2f}, "
+ f"AdvCov: {epoch_training_stats['loss_adv_covariates']:.2f}"
)
if (epoch % checkpoint_freq) == 0 or stop:
if run_eval == True:
evaluation_stats = evaluate(self.model, self.datasets)
for key, val in evaluation_stats.items():
if not (key in self.model.history.keys()):
self.model.history[key] = []
self.model.history[key].append(val)
self.model.history["stats_epoch"].append(epoch)
stop = stop or self.model.early_stopping(
np.mean(evaluation_stats["test"])
)
else:
stop = stop or self.model.early_stopping(
np.mean(epoch_training_stats["test"])
)
evaluation_stats = None
if stop:
self.save(f"{save_dir}{filename}")
pprint.pprint(
{
"epoch": epoch,
"training_stats": epoch_training_stats,
"evaluation_stats": evaluation_stats,
"ellapsed_minutes": ellapsed_minutes,
}
)
print(f"Stop epoch: {epoch}")
break
except KeyboardInterrupt:
self.save(f"{save_dir}{filename}")
self.save(f"{save_dir}{filename}")
def save(self, filename):
"""
Parameters
----------
filename : str
Full path to save pretrained model.
"""
torch.save((self.model.state_dict(), self.args, self.model.history), filename)
self.history = self.model.history
print(f"Model saved to: {filename}")
def _init_pert_embeddings(self):
dose = 1.0
self.emb_perts = (
self.model.compute_drug_embeddings_(
dose * self.drug_ohe.to(self.model.device)
)
.cpu()
.clone()
.detach()
.numpy()
)
def get_drug_embeddings(self, dose=1.0, return_anndata=True):
"""
Parameters
----------
dose : int (default: 1.0)
Dose at which to evaluate latent embedding vector.
return_anndata : bool, optional (default: True)
Return embedding wrapped into anndata object.
Returns
-------
If return_anndata is True, returns anndata object. Otherwise, doesn't
return anything. Always saves embeddding in self.emb_perts.
"""
self._init_pert_embeddings()
emb_perts = (
self.model.compute_drug_embeddings_(
dose * self.drug_ohe.to(self.model.device)
)
.cpu()
.clone()
.detach()
.numpy()
)
if return_anndata:
adata = sc.AnnData(emb_perts)
adata.obs[self.perturbation_key] = self.unique_perts
return adata
def _init_covars_embeddings(self):
combo_list = []
for covars_key in self.covariate_keys:
combo_list.append(self.unique_covars[covars_key])
if self.emb_covars[covars_key] is None:
i_cov = self.covariate_keys.index(covars_key)
self.emb_covars[covars_key] = dict(
zip(
self.unique_covars[covars_key],
self.model.covariates_embeddings[i_cov](
self.covars_ohe[covars_key].to(self.model.device).argmax(1)
)
.cpu()
.clone()
.detach()
.numpy(),
)
)
self.emb_covars_combined = {}
for combo in list(itertools.product(*combo_list)):
combo_name = "_".join(combo)
for i, cov in enumerate(combo):
covars_key = self.covariate_keys[i]
if i == 0:
emb = self.emb_covars[covars_key][cov]
else:
emb += self.emb_covars[covars_key][cov]
self.emb_covars_combined[combo_name] = emb
def get_covars_embeddings_combined(self, return_anndata=True):
"""
Parameters
----------
return_anndata : bool, optional (default: True)
Return embedding wrapped into anndata object.
Returns
-------
If return_anndata is True, returns anndata object. Otherwise, doesn't
return anything. Always saves embeddding in self.emb_covars.
"""
self._init_covars_embeddings()
if return_anndata:
adata = sc.AnnData(np.array(list(self.emb_covars_combined.values())))
adata.obs["covars"] = self.emb_covars_combined.keys()
return adata
def get_covars_embeddings(self, covars_tgt, return_anndata=True):
"""
Parameters
----------
covars_tgt : str
Name of covariate for which to return AnnData
return_anndata : bool, optional (default: True)
Return embedding wrapped into anndata object.
Returns
-------
If return_anndata is True, returns anndata object. Otherwise, doesn't
return anything. Always saves embeddding in self.emb_covars.
"""
self._init_covars_embeddings()
if return_anndata:
adata = sc.AnnData(np.array(list(self.emb_covars[covars_tgt].values())))
adata.obs[covars_tgt] = self.emb_covars[covars_tgt].keys()
return adata
def _get_drug_encoding(self, drugs, doses=None):
"""
Parameters
----------
drugs : str
Drugs combination as a string, where individual drugs are separated
with a plus.
doses : str, optional (default: None)
Doses corresponding to the drugs combination as a string. Individual
drugs are separated with a plus.
Returns
-------
One hot encodding for a mixture of drugs.
"""
drug_mix = np.zeros([1, self.num_drugs])
atomic_drugs = drugs.split("+")
doses = str(doses)
if doses is None:
doses_list = [1.0] * len(atomic_drugs)
else:
doses_list = [float(d) for d in str(doses).split("+")]
for j, drug in enumerate(atomic_drugs):
drug_mix += doses_list[j] * self.perts_dict[drug]
return drug_mix
def mix_drugs(self, drugs_list, doses_list=None, return_anndata=True):
"""
Gets a list of drugs combinations to mix, e.g. ['A+B', 'B+C'] and
corresponding doses.
Parameters
----------
drugs_list : list
List of drug combinations, where each drug combination is a string.
Individual drugs in the combination are separated with a plus.
doses_list : str, optional (default: None)
List of corresponding doses, where each dose combination is a string.
Individual doses in the combination are separated with a plus.
return_anndata : bool, optional (default: True)
Return embedding wrapped into anndata object.
Returns
-------
If return_anndata is True, returns anndata structure of the combinations,
otherwise returns a np.array of corresponding embeddings.
"""
drug_mix = np.zeros([len(drugs_list), self.num_drugs])
for i, drug_combo in enumerate(drugs_list):
drug_mix[i] = self._get_drug_encoding(drug_combo, doses=doses_list[i])
emb = (
self.model.compute_drug_embeddings_(
torch.Tensor(drug_mix).to(self.model.device)
)
.cpu()
.clone()
.detach()
.numpy()
)
if return_anndata:
adata = sc.AnnData(emb)
adata.obs[self.perturbation_key] = drugs_list
adata.obs[self.dose_key] = doses_list
return adata
else:
return emb
def latent_dose_response(
self, perturbations=None, dose=None, contvar_min=0, contvar_max=1, n_points=100
):
"""
Parameters
----------
perturbations : list
List containing two names for which to return complete pairwise
dose-response.
doses : np.array (default: None)
Doses values. If None, default values will be generated on a grid:
n_points in range [contvar_min, contvar_max].
contvar_min : float (default: 0)
Minimum dose value to generate for default option.
contvar_max : float (default: 0)
Maximum dose value to generate for default option.
n_points : int (default: 100)
Number of dose points to generate for default option.
Returns
-------
pd.DataFrame
"""
# dosers work only for atomic drugs. TODO add drug combinations
self.model.eval()
if perturbations is None:
perturbations = self.unique_perts
if dose is None:
dose = np.linspace(contvar_min, contvar_max, n_points)
n_points = len(dose)
df = pd.DataFrame(columns=[self.perturbation_key, self.dose_key, "response"])
for drug in perturbations:
d = np.where(self.perts_dict[drug] == 1)[0][0]
this_drug = torch.Tensor(dose).to(self.model.device).view(-1, 1)
if self.model.doser_type == "mlp":
response = (
(self.model.dosers[d](this_drug).sigmoid() * this_drug.gt(0))
.cpu()
.clone()
.detach()
.numpy()
.reshape(-1)
)
else:
response = (
self.model.dosers.one_drug(this_drug.view(-1), d)
.cpu()
.clone()
.detach()
.numpy()
.reshape(-1)
)
df_drug = pd.DataFrame(
list(zip([drug] * n_points, dose, list(response))),
columns=[self.perturbation_key, self.dose_key, "response"],
)
df = pd.concat([df, df_drug])
return df
def latent_dose_response2D(
self,
perturbations,
dose=None,
contvar_min=0,
contvar_max=1,
n_points=100,
):
"""
Parameters
----------
perturbations : list, optional (default: None)
List of atomic drugs for which to return latent dose response.
Currently drug combinations are not supported.
doses : np.array (default: None)
Doses values. If None, default values will be generated on a grid:
n_points in range [contvar_min, contvar_max].
contvar_min : float (default: 0)
Minimum dose value to generate for default option.
contvar_max : float (default: 0)
Maximum dose value to generate for default option.
n_points : int (default: 100)
Number of dose points to generate for default option.
Returns
-------
pd.DataFrame
"""
# dosers work only for atomic drugs. TODO add drug combinations
assert len(perturbations) == 2, "You should provide a list of 2 perturbations."
self.model.eval()
if dose is None:
dose = np.linspace(contvar_min, contvar_max, n_points)
n_points = len(dose)
df = pd.DataFrame(columns=perturbations + ["response"])
response = {}
for drug in perturbations:
d = np.where(self.perts_dict[drug] == 1)[0][0]
this_drug = torch.Tensor(dose).to(self.model.device).view(-1, 1)
if self.model.doser_type == "mlp":
response[drug] = (
(self.model.dosers[d](this_drug).sigmoid() * this_drug.gt(0))
.cpu()
.clone()
.detach()
.numpy()
.reshape(-1)
)
else:
response[drug] = (
self.model.dosers.one_drug(this_drug.view(-1), d)
.cpu()
.clone()
.detach()
.numpy()
.reshape(-1)
)
l = 0
for i in range(len(dose)):
for j in range(len(dose)):
df.loc[l] = [
dose[i],
dose[j],
response[perturbations[0]][i] + response[perturbations[1]][j],
]
l += 1
return df
def compute_comb_emb(self, thrh=30):
"""
Generates an AnnData object containing all the latent vectors of the
cov+dose*pert combinations seen during training.
Called in api.compute_uncertainty(), stores the AnnData in self.comb_emb.
Parameters
----------
Returns
-------
"""
if self.seen_covars_perts["training"] is None:
raise ValueError("Need to run parse_training_conditions() first!")
emb_covars = self.get_covars_embeddings_combined(return_anndata=True)
# Generate adata with all cov+pert latent vect combinations
tmp_ad_list = []
for cov_pert in self.seen_covars_perts["training"]:
if self.num_measured_points["training"][cov_pert] > thrh:
*cov_list, pert_loop, dose_loop = cov_pert.split("_")
cov_loop = "_".join(cov_list)
emb_perts_loop = []
if "+" in pert_loop:
pert_loop_list = pert_loop.split("+")
dose_loop_list = dose_loop.split("+")
for _dose in pd.Series(dose_loop_list).unique():
tmp_ad = self.get_drug_embeddings(dose=float(_dose))
tmp_ad.obs["pert_dose"] = tmp_ad.obs.condition + "_" + _dose
emb_perts_loop.append(tmp_ad)
emb_perts_loop = emb_perts_loop[0].concatenate(emb_perts_loop[1:])
X = emb_covars.X[
emb_covars.obs.covars == cov_loop
] + np.expand_dims(
emb_perts_loop.X[
emb_perts_loop.obs.pert_dose.isin(
[
pert_loop_list[i] + "_" + dose_loop_list[i]
for i in range(len(pert_loop_list))
]
)
].sum(axis=0),
axis=0,
)
if X.shape[0] > 1:
raise ValueError("Error with comb computation")
else:
emb_perts = self.get_drug_embeddings(dose=float(dose_loop))
X = (
emb_covars.X[emb_covars.obs.covars == cov_loop]
+ emb_perts.X[emb_perts.obs.condition == pert_loop]
)
tmp_ad = sc.AnnData(X=X)
tmp_ad.obs["cov_pert"] = "_".join([cov_loop, pert_loop, dose_loop])
tmp_ad_list.append(tmp_ad)
self.comb_emb = tmp_ad_list[0].concatenate(tmp_ad_list[1:])
def compute_uncertainty(self, cov, pert, dose, thrh=30):
"""
Compute uncertainties for the queried covariate+perturbation combination.
The distance from the closest condition in the training set is used as a
proxy for uncertainty.
Parameters
----------
cov: dict
Provide a value for each covariate (eg. cell_type) as a dictionaty
for the queried uncertainty (e.g. cov_dict={'cell_type': 'A549'}).
pert: string
Perturbation for the queried uncertainty. In case of combinations the
format has to be 'pertA+pertB'
dose: string
String which contains the dose of the perturbation queried. In case
of combinations the format has to be 'doseA+doseB'
Returns
-------
min_cos_dist: float
Minimum cosine distance with the training set.
min_eucl_dist: float
Minimum euclidean distance with the training set.
closest_cond_cos: string
Closest training condition wrt cosine distances.
closest_cond_eucl: string
Closest training condition wrt euclidean distances.
"""
if self.comb_emb is None:
self.compute_comb_emb(thrh=30)
drug_ohe = torch.Tensor(self._get_drug_encoding(pert, doses=dose)).to(
self.model.device
)
pert = drug_ohe.expand([1, self.drug_ohe.shape[1]])
drug_emb = self.model.compute_drug_embeddings_(pert).detach().cpu().numpy()
cond_emb = drug_emb
for cov_key in cov:
cond_emb += self.emb_covars[cov_key][cov[cov_key]]
cos_dist = cosine_distances(cond_emb, self.comb_emb.X)[0]
min_cos_dist = np.min(cos_dist)
cos_idx = np.argmin(cos_dist)
closest_cond_cos = self.comb_emb.obs.cov_pert[cos_idx]
eucl_dist = euclidean_distances(cond_emb, self.comb_emb.X)[0]
min_eucl_dist = np.min(eucl_dist)
eucl_idx = np.argmin(eucl_dist)
closest_cond_eucl = self.comb_emb.obs.cov_pert[eucl_idx]
return min_cos_dist, min_eucl_dist, closest_cond_cos, closest_cond_eucl
def predict(
self,
genes,
cov,
pert,
dose,
uncertainty=True,
return_anndata=True,
sample=False,
n_samples=1,
):
"""Predict values of control 'genes' conditions specified in df.
Parameters
----------
genes : np.array
Control cells.
cov: dict of lists
Provide a value for each covariate (eg. cell_type) as a dictionaty
for the queried uncertainty (e.g. cov_dict={'cell_type': 'A549'}).
pert: list
Perturbation for the queried uncertainty. In case of combinations the
format has to be 'pertA+pertB'
dose: list
String which contains the dose of the perturbation queried. In case
of combinations the format has to be 'doseA+doseB'
uncertainty: bool (default: True)
Compute uncertainties for the generated cells.
return_anndata : bool, optional (default: True)
Return embedding wrapped into anndata object.
sample : bool (default: False)
If sample is True, returns samples from gausssian distribution with
mean and variance estimated by the model. Otherwise, returns just
means and variances estimated by the model.
n_samples : int (default: 10)
Number of samples to sample if sampling is True.
Returns
-------
If return_anndata is True, returns anndata structure. Otherwise, returns
np.arrays for gene_means, gene_vars and a data frame for the corresponding
conditions df_obs.
"""
assert len(dose) == len(pert), "Check the length of pert, dose"
for cov_key in cov:
assert len(cov[cov_key]) == len(pert), "Check the length of covariates"
df = pd.concat(
[
pd.DataFrame({self.perturbation_key: pert, self.dose_key: dose}),
pd.DataFrame(cov),
],
axis=1,
)
self.model.eval()
num = genes.shape[0]
dim = genes.shape[1]
genes = torch.Tensor(genes).to(self.model.device)
gene_means_list = []
gene_vars_list = []
df_list = []
for i in range(len(df)):
comb_name = pert[i]
dose_name = dose[i]
covar_name = {}
for cov_key in cov:
covar_name[cov_key] = cov[cov_key][i]
drug_ohe = torch.Tensor(
self._get_drug_encoding(comb_name, doses=dose_name)
).to(self.model.device)
drugs = drug_ohe.expand([num, self.drug_ohe.shape[1]])
covars = []
for cov_key in self.covariate_keys:
covar_ohe = torch.Tensor(
self.covars_dict[cov_key][covar_name[cov_key]]
).to(self.model.device)
covars.append(covar_ohe.expand([num, covar_ohe.shape[0]]).clone())
gene_reconstructions = (
self.model.predict(genes, drugs, covars).cpu().clone().detach().numpy()
)
if sample:
df_list.append(
pd.DataFrame(
[df.loc[i].values] * num * n_samples, columns=df.columns
)
)
if self.args['loss_ae'] == 'gauss':
dist = Normal(
torch.Tensor(gene_reconstructions[:, :dim]),
torch.Tensor(gene_reconstructions[:, dim:]),
)
elif self.args['loss_ae'] == 'nb':
counts, logits = _convert_mean_disp_to_counts_logits(
torch.clamp(
torch.Tensor(gene_reconstructions[:, :dim]),
min=1e-8,
max=1e8,
),
torch.clamp(
torch.Tensor(gene_reconstructions[:, dim:]),
min=1e-8,
max=1e8,
)
)
dist = NegativeBinomial(
total_count=counts,
logits=logits
)
sampled_gexp = (
dist.sample(torch.Size([n_samples]))
.cpu()
.detach()
.numpy()
.reshape(-1, dim)
)
sampled_gexp[sampled_gexp < 0] = 0 #set negative values to 0, since gexp can't be negative
gene_means_list.append(sampled_gexp)
else:
df_list.append(
pd.DataFrame([df.loc[i].values] * num, columns=df.columns)
)
gene_means_list.append(gene_reconstructions[:, :dim])
if uncertainty:
(
cos_dist,
eucl_dist,
closest_cond_cos,
closest_cond_eucl,
) = self.compute_uncertainty(
cov=covar_name, pert=comb_name, dose=dose_name
)
df_list[-1] = df_list[-1].assign(
uncertainty_cosine=cos_dist,
uncertainty_euclidean=eucl_dist,
closest_cond_cosine=closest_cond_cos,
closest_cond_euclidean=closest_cond_eucl,
)
gene_vars_list.append(gene_reconstructions[:, dim:])
gene_means = np.concatenate(gene_means_list)
gene_vars = np.concatenate(gene_vars_list)
df_obs = pd.concat(df_list)
del df_list, gene_means_list, gene_vars_list
if return_anndata:
adata = sc.AnnData(gene_means)
adata.var_names = self.var_names
adata.obs = df_obs
if not sample:
adata.layers["variance"] = gene_vars
adata.obs.index = adata.obs.index.astype(str) # type fix
del gene_means, gene_vars, df_obs
return adata
else:
return gene_means, gene_vars, df_obs
def get_latent(
self,
genes,
cov,
pert,
dose,
return_anndata=True,
):
"""Get latent values of control 'genes' with conditions specified in df.
Parameters
----------
genes : np.array
Control cells.
cov: dict of lists
Provide a value for each covariate (eg. cell_type) as a dictionaty
for the queried uncertainty (e.g. cov_dict={'cell_type': 'A549'}).
pert: list
Perturbation for the queried uncertainty. In case of combinations the
format has to be 'pertA+pertB'
dose: list
String which contains the dose of the perturbation queried. In case
of combinations the format has to be 'doseA+doseB'
return_anndata : bool, optional (default: True)
Return embedding wrapped into anndata object.
Returns
-------
If return_anndata is True, returns anndata structure. Otherwise, returns
np.arrays for latent and a data frame for the corresponding
conditions df_obs.
"""
assert len(dose) == len(pert), "Check the length of pert, dose"
for cov_key in cov:
assert len(cov[cov_key]) == len(pert), "Check the length of covariates"
df = pd.concat(
[
pd.DataFrame({self.perturbation_key: pert, self.dose_key: dose}),
pd.DataFrame(cov),
],
axis=1,
)
self.model.eval()
num = genes.shape[0]
genes = torch.Tensor(genes).to(self.model.device)
latent_list = []
df_list = []
for i in range(len(df)):
comb_name = pert[i]
dose_name = dose[i]
covar_name = {}
for cov_key in cov:
covar_name[cov_key] = cov[cov_key][i]
drug_ohe = torch.Tensor(
self._get_drug_encoding(comb_name, doses=dose_name)
).to(self.model.device)
drugs = drug_ohe.expand([num, self.drug_ohe.shape[1]])
covars = []
for cov_key in self.covariate_keys:
covar_ohe = torch.Tensor(
self.covars_dict[cov_key][covar_name[cov_key]]
).to(self.model.device)
covars.append(covar_ohe.expand([num, covar_ohe.shape[0]]).clone())
_, latent_treated = self.model.predict(
genes,
drugs,
covars,
return_latent_treated=True,
)
latent_treated = latent_treated.cpu().clone().detach().numpy()
df_list.append(
pd.DataFrame([df.loc[i].values] * num, columns=df.columns)
)
latent_list.append(latent_treated)
latent = np.concatenate(latent_list)
df_obs = pd.concat(df_list)
del df_list
if return_anndata:
adata = sc.AnnData(latent)
adata.obs = df_obs
adata.obs.index = adata.obs.index.astype(str) # type fix
return adata
else:
return latent, df_obs
def get_response(
self,
genes_control=None,
doses=None,
contvar_min=None,
contvar_max=None,
n_points=10,
ncells_max=100,
perturbations=None,
control_name="test",
):
"""Decoded dose response data frame.
Parameters
----------
genes_control : np.array (deafult: None)
Genes for which to predict values. If None, take from 'test_control'
split in datasets.
doses : np.array (default: None)
Doses values. If None, default values will be generated on a grid:
n_points in range [contvar_min, contvar_max].
contvar_min : float (default: 0)
Minimum dose value to generate for default option.
contvar_max : float (default: 0)
Maximum dose value to generate for default option.
n_points : int (default: 100)
Number of dose points to generate for default option.
perturbations : list (default: None)
List of perturbations for dose response
Returns
-------
pd.DataFrame
of decoded response values of genes and average response.
"""
if genes_control is None:
genes_control = self.datasets["test"].subset_condition(control=True).genes
if contvar_min is None:
contvar_min = 0
if contvar_max is None:
contvar_max = self.max_dose
self.model.eval()
if doses is None:
doses = np.linspace(contvar_min, contvar_max, n_points)
if perturbations is None:
perturbations = self.unique_perts
response = pd.DataFrame(
columns=self.covariate_keys
+ [self.perturbation_key, self.dose_key, "response"]
+ list(self.var_names)
)
if ncells_max < len(genes_control):
ncells_max = min(ncells_max, len(genes_control))
idx = torch.LongTensor(
np.random.choice(range(len(genes_control)), ncells_max, replace=False)
)
genes_control = genes_control[idx]
j = 0
for covar_combo in self.emb_covars_combined:
cov_dict = {}
for i, cov_val in enumerate(covar_combo.split("_")):
cov_dict[self.covariate_keys[i]] = [cov_val]
print(cov_dict)
for _, drug in enumerate(perturbations):
if not (drug in self.datasets[control_name].subset_condition(control=True).ctrl_name):
for dose in doses:
# TODO handle covars
gene_means, _, _ = self.predict(
genes_control,
cov=cov_dict,
pert=[drug],
dose=[dose],
return_anndata=False,
)
predicted_data = np.mean(gene_means, axis=0).reshape(-1)
response.loc[j] = (
covar_combo.split("_")
+ [drug, dose, np.linalg.norm(predicted_data)]
+ list(predicted_data)
)
j += 1
return response
def get_response_reference(self, perturbations=None):
"""Computes reference values of the response.
Parameters
----------
dataset : CompPertDataset
The file location of the spreadsheet
perturbations : list (default: None)
List of perturbations for dose response
Returns
-------
pd.DataFrame
of decoded response values of genes and average response.
"""
if perturbations is None:
perturbations = self.unique_perts
reference_response_curve = pd.DataFrame(
columns=self.covariate_keys
+ [self.perturbation_key, self.dose_key, "split", "num_cells", "response"]
+ list(self.var_names)
)
dataset_ctr = self.datasets["training"].subset_condition(control=True)
i = 0
for split in ["training", "ood"]:
if split == 'ood':
dataset = self.datasets[split]
else:
dataset = self.datasets["training"].subset_condition(control=False)
for pert in self.seen_covars_perts[split]:
*covars, drug, dose_val = pert.split("_")
if drug in perturbations:
if not ("+" in dose_val):
dose = float(dose_val)
else:
dose = dose_val
idx = np.where((dataset.pert_categories == pert))[0]
if len(idx):
y_true = dataset.genes[idx, :].numpy().mean(axis=0)
reference_response_curve.loc[i] = (
covars
+ [drug, dose, split, len(idx), np.linalg.norm(y_true)]
+ list(y_true)
)
i += 1
reference_response_curve = reference_response_curve.replace(
"training_treated", "train"
)
return reference_response_curve
def get_response2D(
self,
perturbations,
covar,
genes_control=None,
doses=None,
contvar_min=None,
contvar_max=None,
n_points=10,
ncells_max=100,
#fixed_drugs="",
#fixed_doses="",
):
"""Decoded dose response data frame.
Parameters
----------
perturbations : list
List of length 2 of perturbations for dose response.
covar : dict
Name of a covariate for which to compute dose-response.
genes_control : np.array (deafult: None)
Genes for which to predict values. If None, take from 'test_control'
split in datasets.
doses : np.array (default: None)
Doses values. If None, default values will be generated on a grid:
n_points in range [contvar_min, contvar_max].
contvar_min : float (default: 0)
Minimum dose value to generate for default option.
contvar_max : float (default: 0)
Maximum dose value to generate for default option.
n_points : int (default: 100)
Number of dose points to generate for default option.
Returns
-------
pd.DataFrame
of decoded response values of genes and average response.
"""
assert len(perturbations) == 2, "You should provide a list of 2 perturbations."
if contvar_min is None:
contvar_min = self.min_dose
if contvar_max is None:
contvar_max = self.max_dose
self.model.eval()
# doses = torch.Tensor(np.linspace(contvar_min, contvar_max, n_points))
if doses is None:
doses = np.linspace(contvar_min, contvar_max, n_points)
# genes_control = dataset.genes[dataset.indices['control']]
if genes_control is None:
genes_control = self.datasets["test"].subset_condition(control=True).genes
ncells_max = min(ncells_max, len(genes_control))
idx = torch.LongTensor(np.random.choice(range(len(genes_control)), ncells_max))
genes_control = genes_control[idx]
response = pd.DataFrame(
columns=perturbations+["response"]+list(self.var_names)
)
drug = perturbations[0] + "+" + perturbations[1]
dose_vals = [f"{d[0]}+{d[1]}" for d in itertools.product(*[doses, doses])]
dose_comb = [list(d) for d in itertools.product(*[doses, doses])]
i = 0
if not (drug in self.datasets['training'].subset_condition(control=True).ctrl_name):
for dose in dose_vals:
gene_means, _, _ = self.predict(
genes_control,
cov=covar,
pert=[drug],# + fixed_drugs],
dose=[dose],# + fixed_doses],
return_anndata=False,
)
predicted_data = np.mean(gene_means, axis=0).reshape(-1)
response.loc[i] = (
dose_comb[i]
+ [np.linalg.norm(predicted_data)]
+ list(predicted_data)
)
i += 1
# i = 0
# if not (drug in ["Vehicle", "EGF", "unst", "control", "ctrl"]):
# for dose in dose_vals:
# gene_means, _, _ = self.predict(
# genes_control,
# cov=covar,
# pert=[drug + fixed_drugs],
# dose=[dose + fixed_doses],
# return_anndata=False,
# )
# predicted_data = np.mean(gene_means, axis=0).reshape(-1)
# response.loc[i] = (
# dose_comb[i]
# + [np.linalg.norm(predicted_data)]
# + list(predicted_data)
# )
# i += 1
return response
def evaluate_r2(self, dataset, genes_control, adata_random=None):
"""
Measures different quality metrics about an CPA `autoencoder`, when
tasked to translate some `genes_control` into each of the drug/cell_type
combinations described in `dataset`.
Considered metrics are R2 score about means and variances for all genes, as
well as R2 score about means and variances about differentially expressed
(_de) genes.
"""
self.model.eval()
scores = pd.DataFrame(
columns=self.covariate_keys
+ [
self.perturbation_key,
self.dose_key,
"R2_mean",
"R2_mean_DE",
"R2_var",
"R2_var_DE",
"model",
"num_cells",
]
)
num, dim = genes_control.size(0), genes_control.size(1)
total_cells = len(dataset)
icond = 0
for pert_category in np.unique(dataset.pert_categories):
# pert_category category contains: 'celltype_perturbation_dose' info
de_idx = np.where(
dataset.var_names.isin(np.array(dataset.de_genes[pert_category]))
)[0]
idx = np.where(dataset.pert_categories == pert_category)[0]
*covars, pert, dose = pert_category.split("_")
cov_dict = {}
for i, cov_key in enumerate(self.covariate_keys):
cov_dict[cov_key] = [covars[i]]
if len(idx) > 0:
mean_predict, var_predict, _ = self.predict(
genes_control,
cov=cov_dict,
pert=[pert],
dose=[dose],
return_anndata=False,
sample=False,
)
# estimate metrics only for reasonably-sized drug/cell-type combos
y_true = dataset.genes[idx, :].numpy()
# true means and variances
yt_m = y_true.mean(axis=0)
yt_v = y_true.var(axis=0)
# predicted means and variances
yp_m = mean_predict.mean(0)
yp_v = var_predict.mean(0)
#yp_v = np.var(mean_predict, axis=0)
mean_score = r2_score(yt_m, yp_m)
var_score = r2_score(yt_v, yp_v)
mean_score_de = r2_score(yt_m[de_idx], yp_m[de_idx])
var_score_de = r2_score(yt_v[de_idx], yp_v[de_idx])
scores.loc[icond] = pert_category.split("_") + [
mean_score,
mean_score_de,
var_score,
var_score_de,
"cpa",
len(idx),
]
icond += 1
if adata_random is not None:
yp_m_bl = np.mean(adata_random, axis=0)
yp_v_bl = np.var(adata_random, axis=0)
mean_score_bl = r2_score(yt_m, yp_m_bl)
var_score_bl = r2_score(yt_v, yp_v_bl)
mean_score_de_bl = r2_score(yt_m[de_idx], yp_m_bl[de_idx])
var_score_de_bl = r2_score(yt_v[de_idx], yp_v_bl[de_idx])
scores.loc[icond] = pert_category.split("_") + [
mean_score_bl,
mean_score_de_bl,
var_score_bl,
var_score_de_bl,
"baseline",
len(idx),
]
icond += 1
return scores
def get_reference_from_combo(perturbations_list, datasets, splits=["training", "ood"]):
"""
A simple function that produces a pd.DataFrame of individual
drugs-doses combinations used among the splits (for a fixed covariate).
"""
df_list = []
for split_name in splits:
full_dataset = datasets[split_name]
ref = {"num_cells": []}
for pp in perturbations_list:
ref[pp] = []
ndrugs = len(perturbations_list)
for pert_cat in np.unique(full_dataset.pert_categories):
_, pert, dose = pert_cat.split("_")
pert_list = pert.split("+")
if set(pert_list) == set(perturbations_list):
dose_list = dose.split("+")
ncells = len(
full_dataset.pert_categories[
full_dataset.pert_categories == pert_cat
]
)
for j in range(ndrugs):
ref[pert_list[j]].append(float(dose_list[j]))
ref["num_cells"].append(ncells)
print(pert, dose, ncells)
df = pd.DataFrame.from_dict(ref)
df["split"] = split_name
df_list.append(df)
return pd.concat(df_list)
def linear_interp(y1, y2, x1, x2, x):
a = (y1 - y2) / (x1 - x2)
b = y1 - a * x1
y = a * x + b
return y
def evaluate_r2_benchmark(cpa_api, datasets, pert_category, pert_category_list):
scores = pd.DataFrame(
columns=[
cpa_api.covars_key,
cpa_api.perturbation_key,
cpa_api.dose_key,
"R2_mean",
"R2_mean_DE",
"R2_var",
"R2_var_DE",
"num_cells",
"benchmark",
"method",
]
)
de_idx = np.where(
datasets["ood"].var_names.isin(
np.array(datasets["ood"].de_genes[pert_category])
)
)[0]
idx = np.where(datasets["ood"].pert_categories == pert_category)[0]
y_true = datasets["ood"].genes[idx, :].numpy()
# true means and variances
yt_m = y_true.mean(axis=0)
yt_v = y_true.var(axis=0)
icond = 0
if len(idx) > 0:
for pert_category_predict in pert_category_list:
if "+" in pert_category_predict:
pert1, pert2 = pert_category_predict.split("+")
idx_pred1 = np.where(datasets["training"].pert_categories == pert1)[0]
idx_pred2 = np.where(datasets["training"].pert_categories == pert2)[0]
y_pred1 = datasets["training"].genes[idx_pred1, :].numpy()
y_pred2 = datasets["training"].genes[idx_pred2, :].numpy()
x1 = float(pert1.split("_")[2])
x2 = float(pert2.split("_")[2])
x = float(pert_category.split("_")[2])
yp_m1 = y_pred1.mean(axis=0)
yp_m2 = y_pred2.mean(axis=0)
yp_v1 = y_pred1.var(axis=0)
yp_v2 = y_pred2.var(axis=0)
yp_m = linear_interp(yp_m1, yp_m2, x1, x2, x)
yp_v = linear_interp(yp_v1, yp_v2, x1, x2, x)
# yp_m = (y_pred1.mean(axis=0) + y_pred2.mean(axis=0))/2
# yp_v = (y_pred1.var(axis=0) + y_pred2.var(axis=0))/2
else:
idx_pred = np.where(
datasets["training"].pert_categories == pert_category_predict
)[0]
print(pert_category_predict, len(idx_pred))
y_pred = datasets["training"].genes[idx_pred, :].numpy()
# predicted means and variances
yp_m = y_pred.mean(axis=0)
yp_v = y_pred.var(axis=0)
mean_score = r2_score(yt_m, yp_m)
var_score = r2_score(yt_v, yp_v)
mean_score_de = r2_score(yt_m[de_idx], yp_m[de_idx])
var_score_de = r2_score(yt_v[de_idx], yp_v[de_idx])
scores.loc[icond] = pert_category.split("_") + [
mean_score,
mean_score_de,
var_score,
var_score_de,
len(idx),
pert_category_predict,
"benchmark",
]
icond += 1
return scores
|
CPA-main
|
cpa/api.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import warnings
import numpy as np
import pandas as pd
import scanpy as sc
from sklearn.metrics import r2_score
from scipy.sparse import issparse
from scipy.stats import wasserstein_distance
import torch
warnings.filterwarnings("ignore")
import sys
if not sys.warnoptions:
warnings.simplefilter("ignore")
warnings.simplefilter(action="ignore", category=FutureWarning)
def _convert_mean_disp_to_counts_logits(mu, theta, eps=1e-6):
r"""NB parameterizations conversion
Parameters
----------
mu :
mean of the NB distribution.
theta :
inverse overdispersion.
eps :
constant used for numerical log stability. (Default value = 1e-6)
Returns
-------
type
the number of failures until the experiment is stopped
and the success probability.
"""
assert (mu is None) == (
theta is None
), "If using the mu/theta NB parameterization, both parameters must be specified"
logits = (mu + eps).log() - (theta + eps).log()
total_count = theta
return total_count, logits
def rank_genes_groups_by_cov(
adata,
groupby,
control_group,
covariate,
pool_doses=False,
n_genes=50,
rankby_abs=True,
key_added="rank_genes_groups_cov",
return_dict=False,
):
"""
Function that generates a list of differentially expressed genes computed
separately for each covariate category, and using the respective control
cells as reference.
Usage example:
rank_genes_groups_by_cov(
adata,
groupby='cov_product_dose',
covariate_key='cell_type',
control_group='Vehicle_0'
)
Parameters
----------
adata : AnnData
AnnData dataset
groupby : str
Obs column that defines the groups, should be
cartesian product of covariate_perturbation_cont_var,
it is important that this format is followed.
control_group : str
String that defines the control group in the groupby obs
covariate : str
Obs column that defines the main covariate by which we
want to separate DEG computation (eg. cell type, species, etc.)
n_genes : int (default: 50)
Number of DEGs to include in the lists
rankby_abs : bool (default: True)
If True, rank genes by absolute values of the score, thus including
top downregulated genes in the top N genes. If False, the ranking will
have only upregulated genes at the top.
key_added : str (default: 'rank_genes_groups_cov')
Key used when adding the dictionary to adata.uns
return_dict : str (default: False)
Signals whether to return the dictionary or not
Returns
-------
Adds the DEG dictionary to adata.uns
If return_dict is True returns:
gene_dict : dict
Dictionary where groups are stored as keys, and the list of DEGs
are the corresponding values
"""
gene_dict = {}
cov_categories = adata.obs[covariate].unique()
for cov_cat in cov_categories:
print(cov_cat)
# name of the control group in the groupby obs column
control_group_cov = "_".join([cov_cat, control_group])
# subset adata to cells belonging to a covariate category
adata_cov = adata[adata.obs[covariate] == cov_cat]
# compute DEGs
sc.tl.rank_genes_groups(
adata_cov,
groupby=groupby,
reference=control_group_cov,
rankby_abs=rankby_abs,
n_genes=n_genes,
)
# add entries to dictionary of gene sets
de_genes = pd.DataFrame(adata_cov.uns["rank_genes_groups"]["names"])
for group in de_genes:
gene_dict[group] = de_genes[group].tolist()
adata.uns[key_added] = gene_dict
if return_dict:
return gene_dict
def rank_genes_groups(
adata,
groupby,
pool_doses=False,
n_genes=50,
rankby_abs=True,
key_added="rank_genes_groups_cov",
return_dict=False,
):
"""
Function that generates a list of differentially expressed genes computed
separately for each covariate category, and using the respective control
cells as reference.
Usage example:
rank_genes_groups_by_cov(
adata,
groupby='cov_product_dose',
covariate_key='cell_type',
control_group='Vehicle_0'
)
Parameters
----------
adata : AnnData
AnnData dataset
groupby : str
Obs column that defines the groups, should be
cartesian product of covariate_perturbation_cont_var,
it is important that this format is followed.
control_group : str
String that defines the control group in the groupby obs
covariate : str
Obs column that defines the main covariate by which we
want to separate DEG computation (eg. cell type, species, etc.)
n_genes : int (default: 50)
Number of DEGs to include in the lists
rankby_abs : bool (default: True)
If True, rank genes by absolute values of the score, thus including
top downregulated genes in the top N genes. If False, the ranking will
have only upregulated genes at the top.
key_added : str (default: 'rank_genes_groups_cov')
Key used when adding the dictionary to adata.uns
return_dict : str (default: False)
Signals whether to return the dictionary or not
Returns
-------
Adds the DEG dictionary to adata.uns
If return_dict is True returns:
gene_dict : dict
Dictionary where groups are stored as keys, and the list of DEGs
are the corresponding values
"""
covars_comb = []
for i in range(len(adata)):
cov = "_".join(adata.obs["cov_drug_dose_name"].values[i].split("_")[:-2])
covars_comb.append(cov)
adata.obs["covars_comb"] = covars_comb
gene_dict = {}
for cov_cat in np.unique(adata.obs["covars_comb"].values):
adata_cov = adata[adata.obs["covars_comb"] == cov_cat]
control_group_cov = (
adata_cov[adata_cov.obs["control"] == 1].obs[groupby].values[0]
)
# compute DEGs
sc.tl.rank_genes_groups(
adata_cov,
groupby=groupby,
reference=control_group_cov,
rankby_abs=rankby_abs,
n_genes=n_genes,
)
# add entries to dictionary of gene sets
de_genes = pd.DataFrame(adata_cov.uns["rank_genes_groups"]["names"])
for group in de_genes:
gene_dict[group] = de_genes[group].tolist()
adata.uns[key_added] = gene_dict
if return_dict:
return gene_dict
# def evaluate_r2_(adata, pred_adata, condition_key, sampled=False):
# r2_list = []
# if issparse(adata.X):
# adata.X = adata.X.A
# if issparse(pred_adata.X):
# pred_adata.X = pred_adata.X.A
# for cond in pred_adata.obs[condition_key].unique():
# adata_ = adata[adata.obs[condition_key] == cond]
# pred_adata_ = pred_adata[pred_adata.obs[condition_key] == cond]
# r2_mean = r2_score(adata_.X.mean(0), pred_adata_.X.mean(0))
# if sampled:
# r2_var = r2_score(adata_.X.var(0), pred_adata_.X.var(0))
# else:
# r2_var = r2_score(
# adata_.X.var(0),
# pred_adata_.layers['variance'].var(0)
# )
# r2_list.append(
# {
# 'condition': cond,
# 'r2_mean': r2_mean,
# 'r2_var': r2_var,
# }
# )
# r2_df = pd.DataFrame(r2_list).set_index('condition')
# return r2_df
def evaluate_r2_(adata, pred_adata, condition_key, sampled=False, de_genes_dict=None):
r2_list = []
if issparse(adata.X):
adata.X = adata.X.A
if issparse(pred_adata.X):
pred_adata.X = pred_adata.X.A
for cond in pred_adata.obs[condition_key].unique():
adata_ = adata[adata.obs[condition_key] == cond]
pred_adata_ = pred_adata[pred_adata.obs[condition_key] == cond]
r2_mean = r2_score(adata_.X.mean(0), pred_adata_.X.mean(0))
if sampled:
r2_var = r2_score(adata_.X.var(0), pred_adata_.X.var(0))
else:
r2_var = r2_score(
adata_.X.var(0),
pred_adata_.layers['variance'].var(0)
)
r2_list.append(
{
'condition': cond,
'r2_mean': r2_mean,
'r2_var': r2_var,
}
)
if de_genes_dict:
de_genes = de_genes_dict[cond]
sub_adata_ = adata_[:, de_genes]
sub_pred_adata_ = pred_adata_[:, de_genes]
r2_mean_deg = r2_score(sub_adata_.X.mean(0), sub_pred_adata_.X.mean(0))
if sampled:
r2_var_deg = r2_score(sub_adata_.X.var(0), sub_pred_adata_.X.var(0))
else:
r2_var_deg = r2_score(
sub_adata_.X.var(0),
sub_pred_adata_.layers['variance'].var(0)
)
r2_list[-1]['r2_mean_deg'] = r2_mean_deg
r2_list[-1]['r2_var_deg'] = r2_var_deg
r2_df = pd.DataFrame(r2_list).set_index('condition')
return r2_df
def evaluate_mmd(adata, pred_adata, condition_key, de_genes_dict=None):
mmd_list = []
for cond in pred_adata.obs[condition_key].unique():
adata_ = adata[adata.obs[condition_key] == cond].copy()
pred_adata_ = pred_adata[pred_adata.obs[condition_key] == cond].copy()
if issparse(adata_.X):
adata_.X = adata_.X.A
if issparse(pred_adata_.X):
pred_adata_.X = pred_adata_.X.A
mmd = mmd_loss_calc(torch.Tensor(adata_.X), torch.Tensor(pred_adata_.X))
mmd_list.append(
{
'condition': cond,
'mmd': mmd.detach().cpu().numpy()
}
)
if de_genes_dict:
de_genes = de_genes_dict[cond]
sub_adata_ = adata_[:, de_genes]
sub_pred_adata_ = pred_adata_[:, de_genes]
mmd_deg = mmd_loss_calc(torch.Tensor(sub_adata_.X), torch.Tensor(sub_pred_adata_.X))
mmd_list[-1]['mmd_deg'] = mmd_deg.detach().cpu().numpy()
mmd_df = pd.DataFrame(mmd_list).set_index('condition')
return mmd_df
def evaluate_emd(adata, pred_adata, condition_key, de_genes_dict=None):
emd_list = []
for cond in pred_adata.obs[condition_key].unique():
adata_ = adata[adata.obs[condition_key] == cond].copy()
pred_adata_ = pred_adata[pred_adata.obs[condition_key] == cond].copy()
if issparse(adata_.X):
adata_.X = adata_.X.A
if issparse(pred_adata_.X):
pred_adata_.X = pred_adata_.X.A
wd = []
for i, _ in enumerate(adata_.var_names):
wd.append(
wasserstein_distance(torch.Tensor(adata_.X[:, i]), torch.Tensor(pred_adata_.X[:, i]))
)
emd_list.append(
{
'condition': cond,
'emd': np.mean(wd)
}
)
if de_genes_dict:
de_genes = de_genes_dict[cond]
sub_adata_ = adata_[:, de_genes]
sub_pred_adata_ = pred_adata_[:, de_genes]
wd_deg = []
for i, _ in enumerate(sub_adata_.var_names):
wd_deg.append(
wasserstein_distance(torch.Tensor(sub_adata_.X[:, i]), torch.Tensor(sub_pred_adata_.X[:, i]))
)
emd_list[-1]['emd_deg'] = np.mean(wd_deg)
emd_df = pd.DataFrame(emd_list).set_index('condition')
return emd_df
def pairwise_distance(x, y):
x = x.view(x.shape[0], x.shape[1], 1)
y = torch.transpose(y, 0, 1)
output = torch.sum((x - y) ** 2, 1)
output = torch.transpose(output, 0, 1)
return output
def gaussian_kernel_matrix(x, y, alphas):
"""Computes multiscale-RBF kernel between x and y.
Parameters
----------
x: torch.Tensor
Tensor with shape [batch_size, z_dim].
y: torch.Tensor
Tensor with shape [batch_size, z_dim].
alphas: Tensor
Returns
-------
Returns the computed multiscale-RBF kernel between x and y.
"""
dist = pairwise_distance(x, y).contiguous()
dist_ = dist.view(1, -1)
alphas = alphas.view(alphas.shape[0], 1)
beta = 1. / (2. * alphas)
s = torch.matmul(beta, dist_)
return torch.sum(torch.exp(-s), 0).view_as(dist)
def mmd_loss_calc(source_features, target_features):
"""Initializes Maximum Mean Discrepancy(MMD) between source_features and target_features.
- Gretton, Arthur, et al. "A Kernel Two-Sample Test". 2012.
Parameters
----------
source_features: torch.Tensor
Tensor with shape [batch_size, z_dim]
target_features: torch.Tensor
Tensor with shape [batch_size, z_dim]
Returns
-------
Returns the computed MMD between x and y.
"""
alphas = [
1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1, 5, 10, 15, 20, 25, 30, 35, 100,
1e3, 1e4, 1e5, 1e6
]
alphas = torch.autograd.Variable(torch.FloatTensor(alphas)).to(device=source_features.device)
cost = torch.mean(gaussian_kernel_matrix(source_features, source_features, alphas))
cost += torch.mean(gaussian_kernel_matrix(target_features, target_features, alphas))
cost -= 2 * torch.mean(gaussian_kernel_matrix(source_features, target_features, alphas))
return cost
|
CPA-main
|
cpa/helper.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import argparse
import json
import os
import time
from collections import defaultdict
import numpy as np
import torch
from cpa.data import load_dataset_splits
from cpa.model import CPA, MLP
from sklearn.metrics import r2_score
from torch.autograd import Variable
from torch.distributions import NegativeBinomial
from torch import nn
def pjson(s):
"""
Prints a string in JSON format and flushes stdout
"""
print(json.dumps(s), flush=True)
def _convert_mean_disp_to_counts_logits(mu, theta, eps=1e-6):
r"""NB parameterizations conversion
Parameters
----------
mu :
mean of the NB distribution.
theta :
inverse overdispersion.
eps :
constant used for numerical log stability. (Default value = 1e-6)
Returns
-------
type
the number of failures until the experiment is stopped
and the success probability.
"""
assert (mu is None) == (
theta is None
), "If using the mu/theta NB parameterization, both parameters must be specified"
logits = (mu + eps).log() - (theta + eps).log()
total_count = theta
return total_count, logits
def evaluate_disentanglement(autoencoder, dataset):
"""
Given a CPA model, this function measures the correlation between
its latent space and 1) a dataset's drug vectors 2) a datasets covariate
vectors.
"""
with torch.no_grad():
_, latent_basal = autoencoder.predict(
dataset.genes,
dataset.drugs,
dataset.covariates,
return_latent_basal=True,
)
mean = latent_basal.mean(dim=0, keepdim=True)
stddev = latent_basal.std(0, unbiased=False, keepdim=True)
normalized_basal = (latent_basal - mean) / stddev
criterion = nn.CrossEntropyLoss()
pert_scores, cov_scores = 0, []
def compute_score(labels):
if len(np.unique(labels)) > 1:
unique_labels = set(labels)
label_to_idx = {labels: idx for idx, labels in enumerate(unique_labels)}
labels_tensor = torch.tensor(
[label_to_idx[label] for label in labels], dtype=torch.long, device=autoencoder.device
)
assert normalized_basal.size(0) == len(labels_tensor)
#might have to perform a train/test split here
dataset = torch.utils.data.TensorDataset(normalized_basal, labels_tensor)
data_loader = torch.utils.data.DataLoader(dataset, batch_size=256, shuffle=True)
# 2 non-linear layers of size <input_dimension>
# followed by a linear layer.
disentanglement_classifier = MLP(
[normalized_basal.size(1)]
+ [normalized_basal.size(1) for _ in range(2)]
+ [len(unique_labels)]
).to(autoencoder.device)
optimizer = torch.optim.Adam(disentanglement_classifier.parameters(), lr=1e-2)
for epoch in range(50):
for X, y in data_loader:
pred = disentanglement_classifier(X)
loss = Variable(criterion(pred, y), requires_grad=True)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
pred = disentanglement_classifier(normalized_basal).argmax(dim=1)
acc = torch.sum(pred == labels_tensor) / len(labels_tensor)
return acc.item()
else:
return 0
if dataset.perturbation_key is not None:
pert_scores = compute_score(dataset.drugs_names)
for cov in list(dataset.covariate_names):
cov_scores = []
if len(np.unique(dataset.covariate_names[cov])) == 0:
cov_scores = [0]
break
else:
cov_scores.append(compute_score(dataset.covariate_names[cov]))
return [np.mean(pert_scores), *[np.mean(cov_score) for cov_score in cov_scores]]
def evaluate_r2(autoencoder, dataset, genes_control):
"""
Measures different quality metrics about an CPA `autoencoder`, when
tasked to translate some `genes_control` into each of the drug/covariates
combinations described in `dataset`.
Considered metrics are R2 score about means and variances for all genes, as
well as R2 score about means and variances about differentially expressed
(_de) genes.
"""
mean_score, var_score, mean_score_de, var_score_de = [], [], [], []
num, dim = genes_control.size(0), genes_control.size(1)
total_cells = len(dataset)
for pert_category in np.unique(dataset.pert_categories):
# pert_category category contains: 'celltype_perturbation_dose' info
de_idx = np.where(
dataset.var_names.isin(np.array(dataset.de_genes[pert_category]))
)[0]
idx = np.where(dataset.pert_categories == pert_category)[0]
if len(idx) > 30:
emb_drugs = dataset.drugs[idx][0].view(1, -1).repeat(num, 1).clone()
emb_covars = [
covar[idx][0].view(1, -1).repeat(num, 1).clone()
for covar in dataset.covariates
]
genes_predict = (
autoencoder.predict(genes_control, emb_drugs, emb_covars).detach().cpu()
)
mean_predict = genes_predict[:, :dim]
var_predict = genes_predict[:, dim:]
if autoencoder.loss_ae == 'nb':
counts, logits = _convert_mean_disp_to_counts_logits(
torch.clamp(
torch.Tensor(mean_predict),
min=1e-4,
max=1e4,
),
torch.clamp(
torch.Tensor(var_predict),
min=1e-4,
max=1e4,
)
)
dist = NegativeBinomial(
total_count=counts,
logits=logits
)
nb_sample = dist.sample().cpu().numpy()
yp_m = nb_sample.mean(0)
yp_v = nb_sample.var(0)
else:
# predicted means and variances
yp_m = mean_predict.mean(0)
yp_v = var_predict.mean(0)
# estimate metrics only for reasonably-sized drug/cell-type combos
y_true = dataset.genes[idx, :].numpy()
# true means and variances
yt_m = y_true.mean(axis=0)
yt_v = y_true.var(axis=0)
mean_score.append(r2_score(yt_m, yp_m))
var_score.append(r2_score(yt_v, yp_v))
mean_score_de.append(r2_score(yt_m[de_idx], yp_m[de_idx]))
var_score_de.append(r2_score(yt_v[de_idx], yp_v[de_idx]))
return [
np.mean(s) if len(s) else -1
for s in [mean_score, mean_score_de, var_score, var_score_de]
]
def evaluate(autoencoder, datasets):
"""
Measure quality metrics using `evaluate()` on the training, test, and
out-of-distribution (ood) splits.
"""
autoencoder.eval()
with torch.no_grad():
stats_test = evaluate_r2(
autoencoder,
datasets["test"].subset_condition(control=False),
datasets["test"].subset_condition(control=True).genes
)
disent_scores = evaluate_disentanglement(autoencoder, datasets["test"])
stats_disent_pert = disent_scores[0]
stats_disent_cov = disent_scores[1:]
evaluation_stats = {
"training": evaluate_r2(
autoencoder,
datasets["training"].subset_condition(control=False),
datasets["training"].subset_condition(control=True).genes,
),
"test": stats_test,
"ood": evaluate_r2(
autoencoder, datasets["ood"], datasets["test"].subset_condition(control=True).genes
),
"perturbation disentanglement": stats_disent_pert,
"optimal for perturbations": 1 / datasets["test"].num_drugs
if datasets["test"].num_drugs > 0
else None,
}
if len(stats_disent_cov) > 0:
for i in range(len(stats_disent_cov)):
evaluation_stats[
f"{list(datasets['test'].covariate_names)[i]} disentanglement"
] = stats_disent_cov[i]
evaluation_stats[
f"optimal for {list(datasets['test'].covariate_names)[i]}"
] = 1 / datasets["test"].num_covariates[i]
autoencoder.train()
return evaluation_stats
def prepare_cpa(args, state_dict=None):
"""
Instantiates autoencoder and dataset to run an experiment.
"""
device = "cuda" if torch.cuda.is_available() else "cpu"
datasets = load_dataset_splits(
args["data"],
args["perturbation_key"],
args["dose_key"],
args["covariate_keys"],
args["split_key"],
args["control"],
)
autoencoder = CPA(
datasets["training"].num_genes,
datasets["training"].num_drugs,
datasets["training"].num_covariates,
device=device,
seed=args["seed"],
loss_ae=args["loss_ae"],
doser_type=args["doser_type"],
patience=args["patience"],
hparams=args["hparams"],
decoder_activation=args["decoder_activation"],
)
if state_dict is not None:
autoencoder.load_state_dict(state_dict)
return autoencoder, datasets
def train_cpa(args, return_model=False):
"""
Trains a CPA autoencoder
"""
autoencoder, datasets = prepare_cpa(args)
datasets.update(
{
"loader_tr": torch.utils.data.DataLoader(
datasets["training"],
batch_size=autoencoder.hparams["batch_size"],
shuffle=True,
)
}
)
pjson({"training_args": args})
pjson({"autoencoder_params": autoencoder.hparams})
args["hparams"] = autoencoder.hparams
start_time = time.time()
for epoch in range(args["max_epochs"]):
epoch_training_stats = defaultdict(float)
for data in datasets["loader_tr"]:
genes, drugs, covariates = data[0], data[1], data[2:]
minibatch_training_stats = autoencoder.update(genes, drugs, covariates)
for key, val in minibatch_training_stats.items():
epoch_training_stats[key] += val
for key, val in epoch_training_stats.items():
epoch_training_stats[key] = val / len(datasets["loader_tr"])
if not (key in autoencoder.history.keys()):
autoencoder.history[key] = []
autoencoder.history[key].append(epoch_training_stats[key])
autoencoder.history["epoch"].append(epoch)
ellapsed_minutes = (time.time() - start_time) / 60
autoencoder.history["elapsed_time_min"] = ellapsed_minutes
# decay learning rate if necessary
# also check stopping condition: patience ran out OR
# time ran out OR max epochs achieved
stop = ellapsed_minutes > args["max_minutes"] or (
epoch == args["max_epochs"] - 1
)
if (epoch % args["checkpoint_freq"]) == 0 or stop:
evaluation_stats = evaluate(autoencoder, datasets)
for key, val in evaluation_stats.items():
if not (key in autoencoder.history.keys()):
autoencoder.history[key] = []
autoencoder.history[key].append(val)
autoencoder.history["stats_epoch"].append(epoch)
pjson(
{
"epoch": epoch,
"training_stats": epoch_training_stats,
"evaluation_stats": evaluation_stats,
"ellapsed_minutes": ellapsed_minutes,
}
)
torch.save(
(autoencoder.state_dict(), args, autoencoder.history),
os.path.join(
args["save_dir"],
"model_seed={}_epoch={}.pt".format(args["seed"], epoch),
),
)
pjson(
{
"model_saved": "model_seed={}_epoch={}.pt\n".format(
args["seed"], epoch
)
}
)
stop = stop or autoencoder.early_stopping(np.mean(evaluation_stats["test"]))
if stop:
pjson({"early_stop": epoch})
break
if return_model:
return autoencoder, datasets
def parse_arguments():
"""
Read arguments if this script is called from a terminal.
"""
parser = argparse.ArgumentParser(description="Drug combinations.")
# dataset arguments
parser.add_argument("--data", type=str, required=True)
parser.add_argument("--perturbation_key", type=str, default="condition")
parser.add_argument("--control", type=str, default=None)
parser.add_argument("--dose_key", type=str, default="dose_val")
parser.add_argument("--covariate_keys", nargs="*", type=str, default="cell_type")
parser.add_argument("--split_key", type=str, default="split")
parser.add_argument("--loss_ae", type=str, default="gauss")
parser.add_argument("--doser_type", type=str, default="sigm")
parser.add_argument("--decoder_activation", type=str, default="linear")
# CPA arguments (see set_hparams_() in cpa.model.CPA)
parser.add_argument("--seed", type=int, default=0)
parser.add_argument("--hparams", type=str, default="")
# training arguments
parser.add_argument("--max_epochs", type=int, default=2000)
parser.add_argument("--max_minutes", type=int, default=300)
parser.add_argument("--patience", type=int, default=20)
parser.add_argument("--checkpoint_freq", type=int, default=20)
# output folder
parser.add_argument("--save_dir", type=str, required=True)
# number of trials when executing cpa.sweep
parser.add_argument("--sweep_seeds", type=int, default=200)
return dict(vars(parser.parse_args()))
if __name__ == "__main__":
train_cpa(parse_arguments())
|
CPA-main
|
cpa/train.py
|
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
import warnings
import numpy as np
import torch
warnings.simplefilter(action="ignore", category=FutureWarning)
from typing import Union
import pandas as pd
import scanpy as sc
import scipy
from cpa.helper import rank_genes_groups
from sklearn.preprocessing import OneHotEncoder
def ranks_to_df(data, key="rank_genes_groups"):
"""Converts an `sc.tl.rank_genes_groups` result into a MultiIndex dataframe.
You can access various levels of the MultiIndex with `df.loc[[category]]`.
Params
------
data : `AnnData`
key : str (default: 'rank_genes_groups')
Field in `.uns` of data where `sc.tl.rank_genes_groups` result is
stored.
"""
d = data.uns[key]
dfs = []
for k in d.keys():
if k == "params":
continue
series = pd.DataFrame.from_records(d[k]).unstack()
series.name = k
dfs.append(series)
return pd.concat(dfs, axis=1)
def check_adata(adata, special_fields):
replaced = False
for sf in special_fields:
if sf in adata.obs:
flag = 0
for el in adata.obs[sf].values:
if "_" in str(el):
flag += 1
if flag:
print(
f"WARNING. Special characters ('_') were found in: '{sf}'.",
"They will be replaced with '-'.",
"Be careful, it may lead to errors downstream.",
)
adata.obs[sf] = [s.replace("_", "-") for s in adata.obs[sf].values]
replaced = True
return adata, replaced
indx = lambda a, i: a[i] if a is not None else None
class Dataset:
def __init__(
self,
data,
perturbation_key=None,
dose_key=None,
covariate_keys=None,
split_key="split",
control=None,
):
if type(data) == str:
data = sc.read(data)
#Assert that keys are present in the adata object
assert perturbation_key in data.obs.columns, f"Perturbation {perturbation_key} is missing in the provided adata"
for key in covariate_keys:
assert key in data.obs.columns, f"Covariate {key} is missing in the provided adata"
assert dose_key in data.obs.columns, f"Dose {dose_key} is missing in the provided adata"
assert split_key in data.obs.columns, f"Split {split_key} is missing in the provided adata"
assert not (split_key is None), "split_key can not be None"
#If covariate keys is empty list create dummy covariate
if len(covariate_keys) == 0:
print("Adding a dummy covariate...")
data.obs['dummy_cov'] = 'dummy_cov'
covariate_keys = ['dummy_cov']
self.perturbation_key = perturbation_key
self.dose_key = dose_key
if scipy.sparse.issparse(data.X):
self.genes = torch.Tensor(data.X.A)
else:
self.genes = torch.Tensor(data.X)
self.var_names = data.var_names
if isinstance(covariate_keys, str):
covariate_keys = [covariate_keys]
self.covariate_keys = covariate_keys
data, replaced = check_adata(
data, [perturbation_key, dose_key] + covariate_keys
)
for cov in covariate_keys:
if not (cov in data.obs):
data.obs[cov] = "unknown"
if split_key in data.obs:
pass
else:
print("Performing automatic train-test split with 0.25 ratio.")
from sklearn.model_selection import train_test_split
data.obs[split_key] = "train"
idx = list(range(len(data)))
idx_train, idx_test = train_test_split(
data.obs_names, test_size=0.25, random_state=42
)
data.obs[split_key].loc[idx_train] = "train"
data.obs[split_key].loc[idx_test] = "test"
if "control" in data.obs:
self.ctrl = data.obs["control"].values
else:
print(f"Assigning control values for {control}")
assert_msg = "Please provide a name for control condition."
assert not (control is None), assert_msg
data.obs["control"] = 0
if dose_key in data.obs:
pert, dose = control.split("_")
data.obs.loc[
(data.obs[perturbation_key] == pert) & (data.obs[dose_key] == dose),
"control",
] = 1
else:
pert = control
data.obs.loc[(data.obs[perturbation_key] == pert), "control"] = 1
self.ctrl = data.obs["control"].values
assert_msg = "Cells to assign as control not found! Please check the name of control variable."
assert sum(self.ctrl), assert_msg
print(f"Assigned {sum(self.ctrl)} control cells")
if perturbation_key is not None:
if dose_key is None:
raise ValueError(
f"A 'dose_key' is required when provided a 'perturbation_key'({perturbation_key})."
)
if not (dose_key in data.obs):
print(
f"Creating a default entrance for dose_key {dose_key}:",
"1.0 per perturbation",
)
dose_val = []
for i in range(len(data)):
pert = data.obs[perturbation_key].values[i].split("+")
dose_val.append("+".join(["1.0"] * len(pert)))
data.obs[dose_key] = dose_val
if not ("cov_drug_dose_name" in data.obs) or replaced:
print("Creating 'cov_drug_dose_name' field.")
cov_drug_dose_name = []
for i in range(len(data)):
comb_name = ""
for cov_key in self.covariate_keys:
comb_name += f"{data.obs[cov_key].values[i]}_"
comb_name += f"{data.obs[perturbation_key].values[i]}_{data.obs[dose_key].values[i]}"
cov_drug_dose_name.append(comb_name)
data.obs["cov_drug_dose_name"] = cov_drug_dose_name
if not ("rank_genes_groups_cov" in data.uns) or replaced:
print("Ranking genes for DE genes.")
rank_genes_groups(data, groupby="cov_drug_dose_name")
self.pert_categories = np.array(data.obs["cov_drug_dose_name"].values)
self.de_genes = data.uns["rank_genes_groups_cov"]
self.drugs_names = np.array(data.obs[perturbation_key].values)
self.dose_names = np.array(data.obs[dose_key].values)
# get unique drugs
drugs_names_unique = set()
for d in self.drugs_names:
[drugs_names_unique.add(i) for i in d.split("+")]
self.drugs_names_unique = np.array(list(drugs_names_unique))
# save encoder for a comparison with Mo's model
# later we need to remove this part
encoder_drug = OneHotEncoder(sparse=False)
encoder_drug.fit(self.drugs_names_unique.reshape(-1, 1))
# Store as attribute for molecular featurisation
self.encoder_drug = encoder_drug
self.perts_dict = dict(
zip(
self.drugs_names_unique,
encoder_drug.transform(self.drugs_names_unique.reshape(-1, 1)),
)
)
# get drug combinations
drugs = []
for i, comb in enumerate(self.drugs_names):
drugs_combos = encoder_drug.transform(
np.array(comb.split("+")).reshape(-1, 1)
)
dose_combos = str(data.obs[dose_key].values[i]).split("+")
for j, d in enumerate(dose_combos):
if j == 0:
drug_ohe = float(d) * drugs_combos[j]
else:
drug_ohe += float(d) * drugs_combos[j]
drugs.append(drug_ohe)
self.drugs = torch.Tensor(drugs)
atomic_ohe = encoder_drug.transform(self.drugs_names_unique.reshape(-1, 1))
self.drug_dict = {}
for idrug, drug in enumerate(self.drugs_names_unique):
i = np.where(atomic_ohe[idrug] == 1)[0][0]
self.drug_dict[i] = drug
else:
self.pert_categories = None
self.de_genes = None
self.drugs_names = None
self.dose_names = None
self.drugs_names_unique = None
self.perts_dict = None
self.drug_dict = None
self.drugs = None
if isinstance(covariate_keys, list) and covariate_keys:
if not len(covariate_keys) == len(set(covariate_keys)):
raise ValueError(f"Duplicate keys were given in: {covariate_keys}")
self.covariate_names = {}
self.covariate_names_unique = {}
self.covars_dict = {}
self.covariates = []
for cov in covariate_keys:
self.covariate_names[cov] = np.array(data.obs[cov].values)
self.covariate_names_unique[cov] = np.unique(self.covariate_names[cov])
names = self.covariate_names_unique[cov]
encoder_cov = OneHotEncoder(sparse=False)
encoder_cov.fit(names.reshape(-1, 1))
self.covars_dict[cov] = dict(
zip(list(names), encoder_cov.transform(names.reshape(-1, 1)))
)
names = self.covariate_names[cov]
self.covariates.append(
torch.Tensor(encoder_cov.transform(names.reshape(-1, 1))).float()
)
else:
self.covariate_names = None
self.covariate_names_unique = None
self.covars_dict = None
self.covariates = None
if perturbation_key is not None:
self.ctrl_name = list(
np.unique(data[data.obs["control"] == 1].obs[self.perturbation_key])
)
else:
self.ctrl_name = None
if self.covariates is not None:
self.num_covariates = [
len(names) for names in self.covariate_names_unique.values()
]
else:
self.num_covariates = [0]
self.num_genes = self.genes.shape[1]
self.num_drugs = len(self.drugs_names_unique) if self.drugs is not None else 0
self.is_control = data.obs["control"].values.astype(bool)
self.indices = {
"all": list(range(len(self.genes))),
"control": np.where(data.obs["control"] == 1)[0].tolist(),
"treated": np.where(data.obs["control"] != 1)[0].tolist(),
"train": np.where(data.obs[split_key] == "train")[0].tolist(),
"test": np.where(data.obs[split_key] == "test")[0].tolist(),
"ood": np.where(data.obs[split_key] == "ood")[0].tolist(),
}
def subset(self, split, condition="all"):
idx = list(set(self.indices[split]) & set(self.indices[condition]))
return SubDataset(self, idx)
def __getitem__(self, i):
return (
self.genes[i],
indx(self.drugs, i),
*[indx(cov, i) for cov in self.covariates],
)
def __len__(self):
return len(self.genes)
class SubDataset:
"""
Subsets a `Dataset` by selecting the examples given by `indices`.
"""
def __init__(self, dataset, indices):
self.perturbation_key = dataset.perturbation_key
self.dose_key = dataset.dose_key
self.covariate_keys = dataset.covariate_keys
self.perts_dict = dataset.perts_dict
self.covars_dict = dataset.covars_dict
self.genes = dataset.genes[indices]
self.drugs = indx(dataset.drugs, indices)
self.covariates = [indx(cov, indices) for cov in dataset.covariates]
self.drugs_names = indx(dataset.drugs_names, indices)
self.pert_categories = indx(dataset.pert_categories, indices)
self.covariate_names = {}
for cov in self.covariate_keys:
self.covariate_names[cov] = indx(dataset.covariate_names[cov], indices)
self.var_names = dataset.var_names
self.de_genes = dataset.de_genes
self.ctrl_name = indx(dataset.ctrl_name, 0)
self.num_covariates = dataset.num_covariates
self.num_genes = dataset.num_genes
self.num_drugs = dataset.num_drugs
self.is_control = dataset.is_control[indices]
def __getitem__(self, i):
return (
self.genes[i],
indx(self.drugs, i),
*[indx(cov, i) for cov in self.covariates],
)
def subset_condition(self, control=True):
idx = np.where(self.is_control == control)[0].tolist()
return SubDataset(self, idx)
def __len__(self):
return len(self.genes)
def load_dataset_splits(
data: str,
perturbation_key: Union[str, None],
dose_key: Union[str, None],
covariate_keys: Union[list, str, None],
split_key: str,
control: Union[str, None],
return_dataset: bool = False,
):
dataset = Dataset(
data, perturbation_key, dose_key, covariate_keys, split_key, control
)
splits = {
"training": dataset.subset("train", "all"),
"test": dataset.subset("test", "all"),
"ood": dataset.subset("ood", "all"),
}
if return_dataset:
return splits, dataset
else:
return splits
|
CPA-main
|
cpa/data.py
|
import sys
sys.path.append("../")
import cpa
import scanpy as sc
import scvi
from cpa.helper import rank_genes_groups_by_cov
def sim_adata():
adata = scvi.data.synthetic_iid(run_setup_anndata=False)
sc.pp.filter_cells(adata, min_counts=0)
sc.pp.log1p(adata)
adata.obs["condition"] = "drugA"
adata.obs["condition"].values[:100] = "control"
adata.obs["condition"].values[350:400] = "control"
adata.obs["condition"].values[100:200] = "drugB"
adata.obs["split"] = "train"
return adata
if __name__ == "__main__":
adata = sim_adata()
cpa_api = cpa.api.API(
adata,
pretrained=None,
perturbation_key="condition",
dose_key="dose_val",
covariate_keys=["batch"],
hparams={},
device="cuda:0",
control="control",
)
print("\nStart training")
cpa_api.train(max_epochs=1)
|
CPA-main
|
tests/test.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import os
from functools import partial
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy
import seaborn as sns
from matplotlib.ticker import FuncFormatter
from sklearn.preprocessing import minmax_scale
import hydra
from dib.transformers.ib.helpers import CORR_GROUPS
from omegaconf import OmegaConf
from utils.evaluate import load_histories, load_results
from utils.helpers import (
SFFX_TOAGG,
aggregate_table,
replace_None_with_all,
rm_const_col,
update_prepending,
)
from utils.visualize.helpers import kwargs_log_xscale
logger = logging.getLogger(__name__)
class StrFormatter:
"""Defult dictionary that takes a key dependent function.
Parameters
----------
exact_match : dict, optional
Dictionary of strings that will be replaced by exact match.
subtring_replace : dict, optional
Dictionary of substring that will be replaced if no exact_match. Order matters.
Everything is title case at this point. None gets mapped to "".
to_upper : list, optional
Words to upper case.
"""
def __init__(self, exact_match={}, subtring_replace={}, to_upper=[]):
self.exact_match = exact_match
self.subtring_replace = subtring_replace
self.to_upper = to_upper
def __getitem__(self, key):
if not isinstance(key, str):
return key
if key in self.exact_match:
return self.exact_match[key]
key = key.title()
for match, replace in self.subtring_replace.items():
if replace is None:
replace = ""
key = key.replace(match, replace)
for w in self.to_upper:
key = key.replace(w, w.upper())
return key
def update(self, new_dict):
"""Update the substring replacer dictionary with a new one (missing keys will be prepended)."""
self.subtring_replace = update_prepending(
self.subtring_replace, new_dict)
PRETTY_RENAMER = StrFormatter(
exact_match={
f"train_H_lin_xCz{SFFX_TOAGG}": r"$\mathrm{H}_{\mathcal{V}}[\mathrm{X}|\mathrm{Z}]$ Linear",
f"train_H_q_xCz{SFFX_TOAGG}": r"$\mathrm{H}_{\mathcal{V}}[\mathrm{X}|\mathrm{Z}]$ Z-64-Y (Clf)",
f"train_H_hid16_xCz{SFFX_TOAGG}": r"$\mathrm{H}_{\mathcal{V}}[\mathrm{X}|\mathrm{Z}]$ Z-1024-Y",
f"train_path_norm{SFFX_TOAGG}": "Path Norm",
f"train_var_grad{SFFX_TOAGG}": "Final Grad. Var.",
f"train_y_pred_ent{SFFX_TOAGG}": "Entropy",
f"train_sharp_mag{SFFX_TOAGG}": "Sharp. Mag.",
f"path_norm": "Path Norm",
f"var_grad": "Final Grad. Var.",
f"y_pred_ent": "Entropy",
f"sharp_mag": "Sharp. Mag.",
"wdecay": "Weight Decay",
"resnet": "Depth",
"b": "Batch Size",
"b8": 8,
"b16": 16,
"b32": 32,
"b64": 64,
"b128": 128,
"b256": 256,
"b512": 512,
"b1024": 1024,
"b2048": 2048,
"b4096": 4096,
},
subtring_replace={
SFFX_TOAGG.title(): "",
"_": " ",
"nodist": "",
"Zdim": "Z Dim.",
"Q Family": r"$\mathcal{V}$",
"H Acc": "Head Acc",
"Model": "Objective",
"D Diq Xz Space": r"$\mathrm{I}_{\mathcal{V}}[\mathrm{Z} \rightarrow \mathrm{X} ]$"
+ "\n"
+ r"$- \mathrm{I}_{\mathcal{V}}[\mathrm{Z} \rightarrow \mathrm{Y}]$",
"D Diq Xz": r"$\mathrm{I}_{\mathcal{V}}[\mathrm{Z} \rightarrow \mathrm{Dec(X,Y)}]$",
"Diq Xz": r"$\mathrm{I}_{\mathcal{V}}[\mathrm{Z} \rightarrow \mathrm{Dec(X,Y)}]$",
"Diq Xzcy": r"$\mathrm{I}_{\mathcal{V}}[\mathrm{Z} \rightarrow \mathrm{Dec(X,Y)} ]$",
"D H Q Xcz": r"$\frac{1}{N} \sum_{\mathrm{N}_i} \mathrm{H}_{\mathcal{V}}[\mathrm{N}_i|\mathrm{Z}] $",
"I ": r"$\mathrm{I}$",
"Diq ": r"$\mathrm{I}_{\mathcal{V}}$",
"H Qp ": r"$\mathrm{H}_{\mathcal{V}^+}$",
"H Qm ": r"$\mathrm{H}_{\mathcal{V}^-}$",
"H Q Bob": r"$\mathrm{H}_{\mathcal{V}_{Bob}}$",
"H Q Alice": r"$\mathrm{H}_{\mathcal{V}_{Alice}}$",
"H Q ": r"$\mathrm{H}_{\mathcal{V}}$",
"H ": r"$\mathrm{H}$",
"Xczy": r"$[\mathrm{X}|\mathrm{Z},\mathrm{Y}]$",
"Xcz": r"$[\mathrm{X}|\mathrm{Z}]$",
"Xzcy": r"$[\mathrm{X} \rightarrow \mathrm{Z} | \mathrm{Y}]$",
"Ycz": r"$[\mathrm{Y}|\mathrm{Z}]$",
"Yz": r"$[\mathrm{Z} \rightarrow \mathrm{Y}]$",
"Xz": r"$[\mathrm{Z} \rightarrow \mathrm{X}]$",
# when it is something like H_Q[X|Z] don't put train because we will only look at train
"Train $": "$",
"Q Zy": r"$\mathcal{V}_{Bob}$",
"Q Zx": r"$\mathcal{V}_{Bob Adv.}$",
"Clf ": r"$\mathcal{V}_{Alice}$ ",
"Beta": r"$\beta$",
"Star": r"$*$",
"Loglike": "Log Like.",
"Resnet": "ResNet",
"Dibsameidcs": "Fixed Indexing",
"Dibrand": "Rand. DIB",
"Cdibexact": "Cond. DIB",
"Cdibapprox": "Concat. CDIB",
"Cdib": r"$\Delta$ DIB",
"higher": " Unrolled",
"Accuracy": "Acc.",
"Acc": "Acc.",
"Perc.": r"$\%$",
" All": "",
"Nlay": "Depth",
"N Hidden Layers": "Depth",
"Nhid": "Width",
"Hidden Size": "Width",
"Minimax": "# Inner Optim. Steps",
"Kpru": "Non Zero Weights",
"K Prune": "Non Zero Weights",
"N. ": "# of ",
"Mchead": r"# Indexing of $\mathcal{X}$", # "# of Possible Labels", #
"..": ".",
},
to_upper=["Cifar10", "Cifar100", "Mnist",
"Svhn", "Cifar10Mnist", "Dib", "Vib", ],
)
class Aggregator:
"""Result aggregator.
Parameters
----------
save_dir : str
Where to save all results.
context_kwargs : dict, optional
Context arguments for plotting.
is_return_plots : bool, optional
Whether to return plots instead of saving them.
prfx : str, optional
Prefix for the filename to save.
pretty_renamer : dict, optional
Dictionary mapping string (keys) to human readable ones for nicer printing and plotting.
dpi : int, optional
Resolution of the figures
"""
def __init__(
self,
save_dir,
context_kwargs={"context": "talk"},
is_return_plots=False,
prfx="",
pretty_renamer=PRETTY_RENAMER,
dpi=300,
):
self.save_dir = save_dir
self.is_return_plots = is_return_plots
os.makedirs(self.save_dir, exist_ok=True)
sns.set_context(**context_kwargs)
self.tables = {}
self.table_names = {"results", "aux_trnsf", "histories"}
self.prfx = prfx
self.pretty_renamer = pretty_renamer
self.dpi = dpi
def recolt_data(
self,
pattern_results,
pattern_histories,
pattern_aux_trnsf,
metrics=["test_accuracy", "test_loglike",
"train_accuracy", "train_loglike"],
aux_trnsf=[
"test_I_xz",
"train_I_xz",
"train_diF_zy",
"test_diF_zy",
"train_acc",
"test_acc",
"train_loss",
"test_loss",
"train_loglike",
"test_loglike",
],
metric_column_name="{mode}_{metric}",
**kwargs,
):
"""Recolts all the data.
Parameters
----------
pattern_results : str
Pattern for globbing results.
pattern_histories: str
Pattern for globbing histories.
pattern_aux_trnsf: str
Pattern for globbing auxiliary losses of the transformer.
metrics : list of str, optional
Metrics to aggregate
aux_trnsf : list of str, optional
Auxiliary transformer to aggregate.
metric_column_name : str, optional
Name of the column containing the metric.
kwargs :
Additional arguments to `load_results` and `pattern_histories`.
"""
self.table_names = set()
if pattern_results is not None:
self.tables["results"] = load_results(
pattern=pattern_results,
metrics=metrics,
metric_column_name=metric_column_name,
**kwargs,
)
self.table_names.add("results")
if pattern_aux_trnsf is not None:
self.tables["aux_trnsf"] = load_results(
pattern=pattern_aux_trnsf,
metrics=aux_trnsf,
metric_column_name=metric_column_name,
**kwargs,
)
self.table_names.add("aux_trnsf")
if pattern_histories is not None:
self.tables["histories"] = load_histories(
pattern=pattern_histories, **kwargs
)
self.table_names.add("histories")
def load_data(self):
"""Load the pre-recolted data."""
for k in self.table_names.copy():
try:
self.tables[k] = pd.read_csv(
os.path.join(self.save_dir, self.prfx + f"{k}.csv")
)
except FileNotFoundError:
self.table_names.remove(k)
def prettify(self, table):
"""Make the name and values in a dataframe prettier / human readable."""
def renamer(x): return self.pretty_renamer[x]
table = table.rename(columns=renamer)
table = table.applymap(renamer)
return table
def prettify_kwargs(self, pretty_data, **kwargs):
"""Change the kwargs of plotting function sucxh that they can be used with `prettify(table)`."""
return {
# only prettify if part of the columns (not arguments to seaborn)
k: self.pretty_renamer[v]
if isinstance(v, str) and self.pretty_renamer[v] in pretty_data.columns
else v
for k, v in kwargs.items()
}
def subset(self, col_val):
"""Subset all tables by keeping only the given values in given columns.
Parameters
----------
col_val : dict
A dictionary where the keys are the columns to subset and values are a list of values to keep.
"""
for col, val in col_val.items():
logger.debug("Keeping only val={val} for col={col}.")
for k in self.table_names:
self.tables[k] = self.tables[k][(
self.tables[k][col]).isin(val)]
if self.tables[k].empty:
logger.info(f"Empty table after filtering {col}={val}")
def save_tables(self):
"""Save all tables to csv : one with no constant columns and one with all columns."""
for k, table in self.tables.items():
self._save_table(table, self.prfx + k)
self._save_table(aggregate_table(table), self.prfx + k + "_agg")
def _save_table(self, table, file):
"""Save to csv a table with no constant columns and one with all columns."""
res_no_const = rm_const_col(table)
# add runs even if constant column
if "run" in table.columns:
res_no_const["run"] = table["run"]
else:
res_no_const["run_count"] = table["run_count"]
res_no_const.to_csv(
os.path.join(self.save_dir, file + "_noconst.csv"), index=False
)
table.to_csv(os.path.join(self.save_dir, file + ".csv"), index=False)
def plot_metrics(self, x, is_plot_gaps=False, is_lines=True, **kwargs):
"""Plot a lineplot for each metric
Parameters
----------
x : str
Column name of x axis.
is_plot_gaps : bool, optional
Whether to plot gaps (i.e. train_*-test_*) in addition to all aux_trnsf.
is_lines : bool, optional
Whether to plot lines instead of heatmaps.
kwargs :
Additional arguments to `_plot_lines` or `_plot_heatmaps`.
"""
# use _mean instead of SFFX_TOAGG because you had to aggregate for heatmaps
sffx_values = SFFX_TOAGG if is_lines else "_mean"
def gen_values_name(data):
for col in data.columns:
if not col.endswith(sffx_values):
continue
yield col, col.replace(sffx_values, "")
table = self.tables["results"]
if is_plot_gaps:
table = add_gaps(table.copy())
if is_lines:
dflt_kwargs = dict(markers=True, dashes=False)
dflt_kwargs.update(kwargs)
return self._plot_lines(gen_values_name, table, x, **dflt_kwargs)
else:
# has to remove kwargs that are only for lines
kwargs = {k: v for k, v in kwargs.items() if k not in [
"logbase_x"]}
# has to aggregate the tables for heatmap because cannot vary
table = aggregate_table(table)
return self._plot_heatmaps(gen_values_name, table, x, **kwargs)
def plot_aux_trnsf(self, x, is_plot_gaps=False, **kwargs):
"""Plot a lineplot for each data.
Parameters
----------
x : str
Column name of x axis.
is_plot_gaps : bool, optional
Whether to plot gaps (i.e. train_*-test_*) in addition to all aux_trnsf.
kwargs :
Additional arguments to `_plot_lines`.
"""
def gen_values_name(data):
for col in data.columns:
if not col.endswith(SFFX_TOAGG):
continue
yield col, col.replace(SFFX_TOAGG, "_trnsf")
dflt_kwargs = dict(markers=True, dashes=False)
dflt_kwargs.update(kwargs)
table = self.tables["aux_trnsf"]
if is_plot_gaps:
table = add_gaps(table.copy())
return self._plot_lines(gen_values_name, table, x, **dflt_kwargs)
def plot_histories(self, **kwargs):
"""Plot all the values in the history, for each dataset."""
def gen_values_name(data):
for col in data.columns:
if not col.endswith(SFFX_TOAGG):
continue
yield col, "epochs_" + col.replace(SFFX_TOAGG, "")
# by default don't add marker because many epochs => becomes hard to read
kwargs["marker"] = kwargs.get("marker", ",")
return self._plot_lines(
gen_values_name, self.tables["histories"], "epochs", **kwargs
)
def plot_superpose(
self,
x,
to_superpose,
value_name,
filename="superposed_{value_name}",
is_trnsf=True,
is_legend_out=False,
is_no_legend_title=True,
**kwargs,
):
"""Plot a single line figure with multiple lineplots.
Parameters
----------
x : str
Column name of x axis.
to_superpose : list of str or distionary
List of column values that should be plotted on the figure. If dictionary, then the keys
correspond to the columns to plot and the values correspond to how they should be called.
value_name : str
Name of the yaxis.
filename : str, optional
Name of the figure when saving. Can use {value_name} for interpolation.
is_trnsf : bool, optional
Whether to use `"aux_trnsf"` instead of `"results"` table.
kwargs :
Additional arguments to `_plot_lines`.
"""
def gen_values_name(data):
yield value_name, filename.format(value_name=value_name)
table = self.tables["aux_trnsf" if is_trnsf else "results"].copy()
try:
renamer = {(k + SFFX_TOAGG): v for k, v in to_superpose.items()}
to_superpose = to_superpose.keys()
except AttributeError:
renamer = {}
table = table.melt(
id_vars=[c for c in table if SFFX_TOAGG not in c],
value_vars=[to_sup + SFFX_TOAGG for to_sup in to_superpose],
value_name=value_name,
var_name="mode",
)
table["mode"] = table["mode"].replace(renamer)
kwargs["hue"] = "mode"
kwargs["markers"] = kwargs.get("markers", True)
return self._plot_lines(
gen_values_name,
table,
x,
is_legend_out=is_legend_out,
is_no_legend_title=is_no_legend_title,
**kwargs,
)
def plot_generalization(self, x, is_trnsf=True, **kwargs):
"""Plot the train and test loss/accuracy to see the generalization gap."""
acc = "acc" if is_trnsf else "accuracy"
loss = "loglike"
outs = []
for metric in [acc, loss]:
outs.append(
self.plot_superpose(
x,
{f"train_{metric}": "train", f"test_{metric}": "test"},
metric,
filename="gen_{value_name}",
is_trnsf=is_trnsf,
**kwargs,
)
)
if any(out is not None for out in outs):
return outs
def correlation_experiment(
self,
cause,
correlation_groups=CORR_GROUPS,
logbase_x=1,
col_sep_plots=None, # column for which to plot different correlation plot
xticks=None,
xticklabels=None,
thresholds={"loglike": -0.02, "accuracy": 0.995},
standard_probs=["path_norm", "var_grad", "y_pred_ent", "sharp_mag"],
**kwargs,
):
"""
Results for the correlation experiments. It will make a plot showing side by side the
generalization gap and H_q[X|Z]. It will also make a file correlation_[acc|loglike].csv
containing all the correlation measures.
Note
----
- Tables should have been previously saved. If the `results` table are not saved
in `save_dir` they will be searched for in the parent directory.
- `thresholds` only selects model that reach a certain training performance on th given
metrics.
"""
figs = []
old_cause = cause
self.load_data()
# Load result from parent dir if not previously found
if "results" not in self.tables:
logger.info(
f"`results.csv` not found in {self.save_dir} looking in parent dir."
)
save_dir = self.save_dir
self.save_dir = str(Path(save_dir).parent)
self.tables["results"] = pd.read_csv(
os.path.join(self.save_dir, self.prfx + f"results.csv")
)
self.save_dir = save_dir
# self.subset(dict(epochs=["best"])) # only take the best model (not the last)
for metric in ["loglike", "accuracy"]:
cause = old_cause
# keep only the columns to plot from results (the gap acc|loglike and probs)
results = add_gaps(self.tables["results"])
if metric in thresholds:
n_all = len(results)
to_keep = results[f"train_{metric}_toagg"] > thresholds[metric]
results = results[to_keep]
n_dropped = n_all - len(results)
perc_dropped = n_dropped / n_all
logger.info(
f"dropped {n_dropped} (perc {perc_dropped}) because smaller than threshold "
)
col_probes = [
f"train_{probe}{SFFX_TOAGG}" for probe in standard_probs]
results = results.drop(
columns=[
c
for c in results.columns
if SFFX_TOAGG in c
and c not in [f"gap_{metric}{SFFX_TOAGG}"]
and c not in col_probes # previous litterature probes
and "H_Q" not in c
and "_xCz" not in c
]
)
if results[cause].dtype == "object":
# if the column of interest contains strings then cannot compute correlation.
# besides if ther are some numeric values as suffix of the string. E.g. resnet18,
# resnet50 will be transformed as 18,50 with cause=="resnet"
not_numbers = results[cause].apply(
lambda x: split_alpha_numeric(x)[0])
unique_not_number = not_numbers.unique()
if len(unique_not_number) > 1:
raise ValueError(
f"`cause`={cause} is a string AND contains multiple different prefixes {unique_not_number}"
)
results[cause] = results[cause].apply(
lambda x: split_alpha_numeric(x)[1]
)
results = results.rename(columns={cause: unique_not_number[0]})
cause = unique_not_number[0]
col_H_q_xCz = [
c for c in results.columns if (SFFX_TOAGG in c) and "H_Q" in c
]
# all the correlation probes
all_probes = col_H_q_xCz + col_probes
table = pd.melt(
results,
id_vars=[c for c in results.columns if c not in all_probes],
value_vars=all_probes,
var_name="Probe Type",
value_name="Probe Value",
)
table = self.prettify(table)
results = self.prettify(results)
# KENDAL CORRELATION
arrays = dict(
corr=results.corr(method="kendall"),
corr_pval=results.corr(
method=lambda x, y: scipy.stats.kendalltau(x, y)[1]
),
)
for k in arrays.keys():
arrays[k] = arrays[k][self.pretty_renamer[f"gap_{metric}{SFFX_TOAGG}"]]
arrays[k] = arrays[k].rename(
index={self.pretty_renamer[cause]: "Cause"}
)
arrays[k] = arrays[k][
["Cause"] + [self.pretty_renamer[probe]
for probe in all_probes]
]
arrays[k]["Varying"] = self.pretty_renamer[cause]
arrays[k].to_csv(
os.path.join(self.save_dir, f"{metric}_{k}.csv"), header=False
)
# PLOTTING
sep_plots = (
table[self.pretty_renamer[col_sep_plots]].unique()
if col_sep_plots is not None
else [None]
)
old_table = table.copy()
for batch_probes in [[c] for c in standard_probs] + [col_H_q_xCz]:
table = old_table.copy()
for sep in sep_plots:
fig, axes = plt.subplots(
2,
len(table[self.pretty_renamer["data"]].unique()),
sharex=True,
figsize=(17, 9),
)
for i, data in enumerate(
table[self.pretty_renamer["data"]].unique()
):
axes[0, i].set_title(data.upper())
if col_sep_plots is not None:
table_subset = table[
table[self.pretty_renamer[col_sep_plots]] == sep
]
else:
table_subset = table
# only plot the proposed probes H_Q[X|Z]
table_subset = table_subset[
table_subset["Probe Type"].isin(
[self.pretty_renamer[c] for c in batch_probes]
)
]
table_subset = table_subset[
table_subset[self.pretty_renamer["data"]] == data
]
table_subset = table_subset.dropna(
how="any", subset=["Probe Value"]
)
if logbase_x != 1:
plt.xscale("symlog")
sns.lineplot(
data=table_subset,
x=self.pretty_renamer[cause],
y=self.pretty_renamer[f"gap_{metric}{SFFX_TOAGG}"],
ax=axes[0, i],
**kwargs,
)
sns.lineplot(
data=table_subset,
x=self.pretty_renamer[cause],
y="Probe Value",
style="Probe Type",
hue="Probe Type",
ax=axes[1, i],
**kwargs,
)
if xticks is not None:
axes[0, i].set_xticks(
list(range(len(xticks))), xticks)
axes[1, i].set_xticks(
list(range(len(xticks))), xticks)
if xticklabels is not None:
axes[1, i].set_xticklabels(xticklabels)
if self.is_return_plots:
figs.append(fig)
else:
sffx = f"_{sep}" if col_sep_plots is not None else ""
sffx += f"_{batch_probes[0]}"
fig.savefig(
os.path.join(
self.save_dir, f"{self.prfx}corr_{metric}{sffx}.png"
),
dpi=self.dpi,
)
plt.close(fig)
if self.is_return_plots:
return figs
def _plot_lines(
self,
gen_values_name,
data,
x,
folder_col=None,
row="data",
is_merge_data_size=True,
transformer_data="identity",
logbase_x=1,
xticks=None,
xticklabels=None,
is_legend_out=True,
is_no_legend_title=False,
set_kwargs={},
x_rotate=0,
cols_vary_only=["run"],
sharey=False,
**kwargs,
):
"""Lines plots.
Parameters
----------
gen_values_name : generator
Generates 2 string, the column of y axis and the filename to save the plot.
data : pd.DataFrame
Dataframe used for plotting.
x : str
Column name of x axis.
folder_col : str, optional
Name of a column tha will be used to separate the plot into multiple subfolders.
row : str, optional
Column name of rows.
is_merge_data_size : bool, optional
Whether to merge the "data" and "data_size".
transformer_data : callable or {"identity", "normalize_by_x_axis", "fillin_None_x_axis", "replace_None_zero"}, optional
Transform the data given the name of the columns seaborn will condition on.
logbase_x : int, optional
Base of the x axis. If 1 no logscale. if `None` will automatically chose.
xticks : list of int, optional
Set manually x ticks.
xticklabels : list of str or int, optional
Set manually x ticks labels.
is_legend_out : bool, optional
Whether to put the legend outside of the figure.
is_no_legend_title : bool, optional
Whether to remove the legend title. If `is_legend_out` then will actually duplicate the
legend :/, the best in that case is to remove the test of the legend column .
set_kwargs : dict, optional
Additional arguments to `FacetGrid.set`. E.g. dict(xlim=(0,None)).
x_rotate : int, optional
By how much to rotate the x labels.
cols_vary_only : list of str, optional
Name of the columns that can vary when plotting (i.e over which to compute bootstrap CI).
sharey : bool, optional
Wether to share y axis.
kwargs :
Additional arguemnts to `sns.relplot`.
"""
if is_merge_data_size:
data = data.copy()
data["data"] = data[["data", "datasize"]].apply(
lambda x: "_".join(x), axis=1
)
dflt_kwargs = dict(
legend="full",
row=row,
kind="line",
facet_kws={"sharey": sharey, "sharex": True,
"legend_out": is_legend_out},
hue=kwargs.get("style", None),
)
dflt_kwargs.update(kwargs)
dflt_kwargs["x"] = x
dflt_kwargs["marker"] = dflt_kwargs.get("marker", "X")
def _helper_plot_lines(data, save_dir):
sns_plots = []
data = get_transformer_data(transformer_data)(
data, get_col_kwargs(data, **dflt_kwargs)
)
for y, filename in gen_values_name(data):
if data[y].isna().all():
logger.info(f"Skipping {filename} because all nan.")
continue
_assert_sns_vary_only_cols(data, dflt_kwargs, cols_vary_only)
# replace `None` with "None" for string columns such that can see those
data = data.copy()
str_col = data.select_dtypes(include=object).columns
data[str_col] = data[str_col].fillna(value="None")
pretty_data = self.prettify(data)
pretty_kwargs = self.prettify_kwargs(
pretty_data, y=y, **dflt_kwargs)
sns_plot = sns.relplot(data=pretty_data, **pretty_kwargs)
if x_rotate != 0:
# calling directly `set_xticklabels` on FacetGrid removes the labels sometimes
for axes in sns_plot.axes.flat:
axes.set_xticklabels(
axes.get_xticklabels(), rotation=x_rotate)
if logbase_x != 1:
x_data = np.array(
sorted(pretty_data[pretty_kwargs["x"]].unique()))
plt.xscale(**kwargs_log_xscale(x_data, base=logbase_x))
if is_no_legend_title:
#! not going to work well if is_legend_out (double legend)
for ax in sns_plot.fig.axes:
handles, labels = ax.get_legend_handles_labels()
if len(handles) > 1:
ax.legend(handles=handles[1:], labels=labels[1:])
if xticks is not None:
sns_plot.set(xticks=xticks)
if xticklabels is not None:
sns_plot.set(xticklabels=xticklabels)
if xticks[0] > xticks[1]:
# dirty check to see if should reverse
for ax in sns_plot.axes.reshape(-1):
ax.invert_xaxis()
sns_plot.set(**set_kwargs)
if self.is_return_plots:
sns_plots.append(sns_plot)
else:
sns_plot.fig.savefig(
os.path.join(save_dir, f"{self.prfx}{filename}.png"),
dpi=self.dpi,
)
plt.close(sns_plot.fig)
if self.is_return_plots:
return sns_plots
return self._foldersplit_call(_helper_plot_lines, folder_col, data)
def _foldersplit_call(self, fn, folder_col, data):
"""Split the dataset by the values in folder_col a nd call fn on each subfolder."""
if folder_col is None:
return fn(data, self.save_dir)
else:
out = []
for curr_folder in data[folder_col].unique():
curr_data = data[data[folder_col] == curr_folder]
sub_dir = os.path.join(
self.save_dir, f"{folder_col}_{curr_folder}")
os.makedirs(sub_dir, exist_ok=True)
out.append(fn(curr_data, sub_dir))
return out
def _plot_heatmaps(
self,
gen_values_name,
data,
x,
y,
col=None,
folder_col=None,
row="data",
is_merge_data_size=True,
transformer_data="identity",
normalize=None,
is_percentage=False,
cbar_label=None,
**kwargs,
):
"""Lines plots.
Parameters
----------
gen_values_name : generator
Generates 2 string, the column of values axis and the filename to save the plot.
data : pd.DataFrame
Dataframe used for plotting.
x : str
Column name of x axis of heatmaps.
y : str
Column name of y axis of heatmaps.
col : str, optional
Column name of columns.
row : str, optional
Column name of rows.
folder_col : str, optional
Name of a column tha will be used to separate the plot into multiple subfolders.
is_merge_data_size : bool, optional
Whether to merge the "data" and "data_size".
normalize : ["row","col",None], optional
Whether to normalize the values by row (single 1 per row), by column (single 1 per col)
or not to.
is_percentage : bool, optional
Whether to use percentage for the annotation of the heatmap.
cbar_label : str, optional
Name for the colorbar.
kwargs :
Additional arguments to `sns.heatmap`.
"""
if is_merge_data_size:
data = data.copy()
data["data"] = data[["data", "datasize"]].apply(
lambda x: "_".join(x), axis=1
)
def fmt(x, pos): return "{:.0%}".format(x)
dflt_kwargs = dict(annot=True, linewidths=0.5)
if is_percentage:
dflt_kwargs.update(
dict(fmt=".0%", cbar_kws={"format": FuncFormatter(fmt)}))
if cbar_label is not None:
dflt_kwargs["cbar_kws"] = {
"label": self.pretty_renamer[cbar_label]}
dflt_kwargs.update(kwargs)
def _draw_heatmap(x, y, values, **kwargs):
data = kwargs.pop("data")
d = data.pivot(index=y, columns=x, values=values)
if normalize is None:
pass
elif normalize == "row":
d = pd.DataFrame(
minmax_scale(d.values, axis=1), columns=d.columns, index=d.index
)
elif normalize == "col":
d = pd.DataFrame(
minmax_scale(d.values, axis=0), columns=d.columns, index=d.index
)
else:
raise ValueError(f"Unkown normalize={normalize}")
ax = sns.heatmap(d, **kwargs)
ax.invert_yaxis()
for label in ax.get_yticklabels():
label.set_rotation(0)
# ax.set_yticklabels(ax.get_yticklabels(), rotation=90)
def _helper_plot_heatmaps(data, save_dir):
"""Plot the results as heatmaps."""
sns_plots = []
data = get_transformer_data(transformer_data)(
data,
get_col_kwargs(data, **dict(row=row, col=col,
x=x, y=y, **dflt_kwargs)),
)
for values, filename in gen_values_name(data):
if data[values].isna().all():
logger.info(f"Skipping {filename} because all nan.")
continue
pretty_data = self.prettify(data)
pretty_kwargs = self.prettify_kwargs(
pretty_data, **dflt_kwargs)
sns_plot = sns.FacetGrid(
pretty_data,
row=self.pretty_renamer[row],
col=self.pretty_renamer[col],
dropna=False,
sharex=True,
sharey=True,
aspect=1.5,
height=6,
)
sns_plot.map_dataframe(
_draw_heatmap,
self.pretty_renamer[x],
self.pretty_renamer[y],
values=self.pretty_renamer[values],
**pretty_kwargs,
)
sns_plot.fig.tight_layout()
if self.is_return_plots:
logger.info(f"heatmap for {values}")
sns_plots.append(sns_plot)
else:
sns_plot.fig.savefig(
os.path.join(save_dir, f"{self.prfx}{filename}.png"),
dpi=self.dpi,
)
plt.close(sns_plot.fig)
if self.is_return_plots:
return sns_plots
return self._foldersplit_call(_helper_plot_heatmaps, folder_col, data)
@hydra.main(config_path="conf/aggregate.yaml", strict=False)
def main_cli(args):
return main(args)
def main(args):
logger.info(f"Aggregating {args.experiment} ...")
PRETTY_RENAMER.update(args.kwargs.pop("pretty_renamer"))
aggregator = Aggregator(pretty_renamer=PRETTY_RENAMER, **args.kwargs)
if args.is_recolt:
logger.info(f"Recolting the data ..")
# Omega conf dictionaries don't have all the properties that usual dictionaries have
aggregator.recolt_data(
**OmegaConf.to_container(args.recolt_data, resolve=True))
else:
logger.info(f"Loading previously recolted data ..")
aggregator.load_data()
aggregator.subset(OmegaConf.to_container(
args.col_val_subset, resolve=True))
for f in args.mode:
logger.info(f"Mode {f} ...")
if f is None:
continue
if f in args:
kwargs = args[f]
else:
kwargs = {}
getattr(aggregator, f)(**kwargs)
# HELPERS
def add_gaps(df):
"""Add train-test gaps to dataframe."""
for col in df.columns:
if col.startswith("train_") and col.replace("train_", "test_") in df.columns:
# gap = max(train - test, 0)
gap_col = col.replace("train_", "gap_")
df[gap_col] = df[col] - df[col.replace("train_", "test_")]
df.loc[df[gap_col] < 0, gap_col] = 0
return df
def group_normalize_by_max(x, subgroup_col):
"""
Add a column `normalizer_{col}` for every `_toagg` columns wich contains the maximum of the means of
`subgroup_col`.
"""
means_xs = x.groupby(
[subgroup_col]
).mean() # average over runs for all subgroup_col
for col in x.columns:
if SFFX_TOAGG in col:
# normalizer is teh maximum average
normalizer = means_xs[col].max()
x[f"normalizer_{col}"] = normalizer
return x
def split_alpha_numeric(s):
"""Take a string containing letters followed by numbers and spli them."""
not_numbers = s.rstrip("0123456789")
numbers = s[len(not_numbers):]
return not_numbers, int(numbers)
def normalize_by_x_axis(data, col_kwargs):
"""
Prepares the data by normalizing by the maximum (average over runs) style at every point of the
x axis before plotting on seaborn. Return the normalized data in %{col}.
"""
# normalizer will be different for all seaborn plots => compute normalizer for each group separately
col_groupby = [
v
for k, v in col_kwargs.items()
if v != col_kwargs["hue"] and SFFX_TOAGG not in v
]
# compute the normalizers
df = data.groupby(col_groupby).apply(
partial(group_normalize_by_max, subgroup_col=col_kwargs["hue"])
)
# apply the normalization
for col in data.columns:
if "_toagg" in col and "normalizer" not in col:
df[f"%{col}"] = df[col] / df[f"normalizer_{col}"]
df = df[[col for col in df.columns if "normalizer" not in col]]
return df
def fillin_None_x_axis(data, col_kwargs):
"""Prepares the data by removing the NaN in values of X axis by duplicatign the entry where x
axis is all unique value. I.e enable the plloting of a lineplot which does not vary the x_axis
as a straight horizaontal line.
"""
return replace_None_with_all(data, col_kwargs["x"])
def replace_None_zero(data, col_kwargs):
"""Replace all missing values with 0."""
return data.fillna(0)
def get_transformer_data(transformer_data):
if isinstance(transformer_data, str):
if transformer_data == "normalize_by_x_axis":
return normalize_by_x_axis
elif transformer_data == "fillin_None_x_axis":
return fillin_None_x_axis
elif transformer_data == "replace_None_zero":
return replace_None_zero
elif transformer_data == "identity":
return lambda data, col_kwargs: data
else:
raise ValueError(f"Unkown transformer_data={transformer_data}")
return transformer_data
def get_col_kwargs(data, **kwargs):
"""Return all arguments that are names of the columns of the data."""
return {
n: col
for n, col in kwargs.items()
if (isinstance(col, str) and col in data.columns)
}
def _assert_sns_vary_only_cols(data, kwargs, cols_vary_only):
"""
Make sure that the only columns that has not been conditioned over for plotting and has non
unique values are in `cols_vary_only`. `disregard_col` are columns that can also
"""
return
conditioned_df = data
for col in get_col_kwargs(data, **kwargs).values():
first_unique = conditioned_df[col].dropna().unique()[0]
conditioned_df = conditioned_df[conditioned_df[col] == first_unique]
# make sure only `col_vary_only` varies
if len(conditioned_df[cols_vary_only].drop_duplicates()) != len(conditioned_df):
conditioned_df = conditioned_df[
conditioned_df[cols_vary_only[0]]
== conditioned_df[cols_vary_only[0]].dropna().unique()[0]
]
varying_columns = []
for col in conditioned_df.columns:
if len(conditioned_df[col].unique()) > 1 and col not in cols_vary_only:
varying_columns.append(col)
raise ValueError(
f"Not only varying {cols_vary_only}. At least one of the following varies {varying_columns}."
)
if __name__ == "__main__":
main_cli()
|
decodable_information_bottleneck-main
|
aggregate.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
Script to use to change directory structure in `tmp_results/*` in case you change the default
directory structure (i.e. you change `hyperparameters` in hyperparameters.
Use `python add_hyperparam.py experiment=...` to update the directory structure of a given experiment.
If you don't provide the experiment, it will update everything.
"""
import glob
import logging
import os
import shutil
import hydra
from omegaconf import OmegaConf
from utils.helpers import format_container, hyperparam_to_path
logger = logging.getLogger(__name__)
@hydra.main(config_path="conf/config.yaml")
def add_hyperparam(args):
"""Function that renames results from `experiment` in case you added a new hyperparameter to save.
It can also rename all results by setting `experiment=*`.
"""
dflt_hyperparameters = OmegaConf.to_container(
args.hyperparameters, resolve=True)
subfolders = glob.glob(
os.path.join(args.paths.base_dir,
f"tmp_results/{args.experiment}/**/run_*/"),
recursive=True,
)
for subfolder in subfolders:
relative_dir = subfolder.split("tmp_results")[-1][1:-1]
args.experiment = relative_dir.split("/")[0]
args.trnsf_experiment = args.experiment
hyperparam = {
"_".join(group.split("_")[:-1]): group.split("_")[-1]
for group in relative_dir[len(args.experiment) + 1:].split("/")
}
curr_dflt_hyperparameters = dflt_hyperparameters.copy()
curr_dflt_hyperparameters.update(hyperparam)
hyperparam_path = hyperparam_to_path(curr_dflt_hyperparameters)
paths = format_container(args.paths, dict(
hyperparam_path=hyperparam_path))
# remove the run_ as it will always be 0 in paths
new_subfolder = "run_".join(
paths["chckpnt_dirnames"][0].split("run_")[:-1])
# remove rtailing slash if exist
new_subfolder = new_subfolder.rstrip("/")
if new_subfolder == subfolder:
continue
else:
os.makedirs(
new_subfolder.rsplit("/", 1)[0], exist_ok=True
) # make sure all folder until last exist
shutil.move(subfolder, new_subfolder)
if __name__ == "__main__":
add_hyperparam()
|
decodable_information_bottleneck-main
|
add_hyperparam.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import os
import string
from copy import deepcopy
import hydra
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from omegaconf import OmegaConf
from sklearn.preprocessing import minmax_scale
from aggregate import PRETTY_RENAMER
from dib.training.helpers import clone_trainer
from main import main as main_training
from utils.evaluate import accuracy, loglike
from utils.helpers import (PartialFormatMap, all_logging_disabled,
flip_nested_dict)
from utils.visualize import plot_2D_decision_boundary
logger = logging.getLogger(__name__)
class ModelsAnalyser:
"""Loader of pretrained models for analsis. Alalyse one set of enoders with their respective clfs.
Parameters
----------
save_dir : str
Where to save all results.
context_kwargs : dict, optional
Context arguments for plotting.
is_interactive : bool, optional
Whether to plot interactively, useful in jupyter notebooks.
prfx : str, optional
Prefix for the filename to save.
pretty_renamer : dict, optional
Dictionary mapping string (keys) to human readable ones for nicer printing and plotting.
dpi : int, optional
Resolution of the figures
"""
def __init__(
self,
save_dir,
context_kwargs={"context": "talk"},
is_interactive=False,
prfx="",
pretty_renamer=PRETTY_RENAMER,
dpi=300,
):
self.save_dir = save_dir
self.is_interactive = is_interactive
os.makedirs(self.save_dir, exist_ok=True)
sns.set_context(**context_kwargs)
self.trainers = {}
self.datasets = {}
self.prfx = prfx
self.n_clfs = 0
self.n_encs = 0
self.pretty_renamer = pretty_renamer
self.dpi = dpi
def recolt_data(
self,
cfg,
clf_patterns,
encoders_param,
encoders_vals,
get_name_clf=lambda p: PRETTY_RENAMER[p.replace("/", "")],
get_name_encoder=lambda p, v: PRETTY_RENAMER[" ".join(
p.split(".")[-2:])]
+ f" {v}",
):
"""Recolts all the data.
Parameters
----------
cfg : omegaconf.DictConfig
Arguments to the `main` function to load the models.
clf_patterns : list of str
List of patterns that will be used to select the clfs. I.e substring to recognize a single
clf. Each pattern should match one and only one clf.
encoders_param : str
Parameter for which to loop over for getting different encoders. I.e. field of `encoders_vals`.
encoders_vals : list
Values of encoders_param for which to iterate over. Effectively giving `param=values[i]`
to generate each encoders.
get_name_clf : callable, optional
Function that takes in the pattern and return the name of the clf. Used to give human
readable names.
get_name_encoder : callable, optional
Function that maps (encoders_param, encoders_vals[i]) -> name_enc. Used to give human
readable names.
"""
self.loaded_trainers = dict()
self.loaded_datasets = dict()
for i, enc_val in enumerate(encoders_vals):
# make sure not changed in place cfg
curr_cfg = deepcopy(cfg)
name_enc = get_name_encoder(encoders_param, enc_val)
self.loaded_trainers[name_enc] = dict()
self.loaded_datasets[name_enc] = dict()
# set the the current encoder
curr_cfg.merge_with_dotlist([f"{encoders_param}={enc_val}"])
with all_logging_disabled():
trainers, datasets = main_training(curr_cfg)
for pattern in clf_patterns:
name_clf = get_name_clf(pattern)
key = [k for k in trainers.keys() if pattern in k]
if len(key) == 0:
raise ValueError(f"No keys have {pattern} as pattern.")
elif len(key) > 1:
raise ValueError(
f"Multiple keys have {pattern} as pattern.")
key = key[0]
self.loaded_trainers[name_enc][name_clf] = trainers[key]
self.loaded_datasets[name_enc][name_clf] = datasets[key]
len_trainers = list(set(len(t) for t in self.loaded_trainers.values()))
len_datasets = list(set(len(d) for d in self.loaded_datasets.values()))
if (
(len(len_trainers) != 1)
or (len(len_datasets) != 1)
or (len_datasets[0] != len_trainers[0])
):
raise ValueError(
f"datasets and trainers wrong length : len_trainers={len_trainers}, len_datasets={len_datasets}"
)
self.n_encs = len(encoders_vals)
self.n_clfs = len_trainers[0]
def plot_reps_clfs(
self,
filename,
get_title_top=lambda clf: clf,
get_title="{normloglike:.0%} Log Like.",
is_invert_yaxis=False,
diagonal_color="tab:green",
is_plot_test=False,
**kwargs,
):
"""Return a list of rep_clf figures for the correct classifiers.
Parameters
----------
filename : str
get_title_top : callable or str, optional
Function that return the title of the top row. (x label if inverted)
get_title : callable or str or "loglike", optional
Function that takes in the pattern and return the title. `{acc`|`{loglike`|`{normloglike`|
`{percloglike` | `{normacc` will be replaced by actual accuracy | loglike | normalized
loglike | normalized accuracy. Normalized is by clf.
is_invert_yaxis : bool, optional
Whether to invert the x and y axis.
diagonal_color : str, optional
Color of the text on the diagonal.
is_plot_test : bool, optional
Whether to plot some test datapoints.
kwargs :
Additional arguments to `plot_2D_decision_boundary`.
"""
if isinstance(get_title_top, str):
get_title_top_str = get_title_top
def get_title_top(clf): return get_title_top_str
if isinstance(get_title, str):
get_title_str = (
get_title if get_title != "loglike" else "{loglike:.2f} Log Like."
)
def get_title(clf): return get_title_str
named_axes = dict()
metrics = dict()
F, axes = plt.subplots(
self.n_encs,
self.n_clfs,
figsize=(4 * self.n_clfs, 4 * self.n_encs),
squeeze=False,
)
# plotting
for i, key_enc in enumerate(self.loaded_trainers.keys()):
if is_invert_yaxis:
i = self.n_encs - i - 1
named_axes[key_enc], metrics[key_enc] = get_figs_rep_clfs(
self.loaded_trainers[key_enc],
self.loaded_datasets[key_enc],
axes=axes[i, :],
is_plot_test=is_plot_test,
**kwargs,
)
# metrics will be of depth 3 with keys (metric, encoder, clf)
metrics = flip_nested_dict(metrics)
# each metric will be dataframe
metrics = {k: pd.DataFrame(v) for k, v in metrics.items()}
# add normalized metrics
for k in [k for k in metrics.keys()]:
metrics[f"norm{k}"] = pd.DataFrame(
minmax_scale(metrics[k], axis=1),
index=metrics[k].index,
columns=metrics[k].columns,
)
# back to dict
for k in [k for k in metrics.keys()]:
metrics[k] = metrics[k].to_dict()
# set all the titles
for i, enc in enumerate(named_axes.keys()):
unmodified_i = i
if is_invert_yaxis:
is_bottom = i == 0
i = self.n_encs - i - 1
def get_prfx(key): return ""
# if reverse put title top at the bottom as an x label
get_xlabel = (
(lambda key: get_title_top(key)
) if is_bottom else (lambda key: "")
)
else:
def get_prfx(key): return (
get_title_top(key) + "\n") if i == 0 else ""
get_xlabel = None
for j, clf in enumerate(named_axes[enc].keys()):
if j == 0:
axes[i, j].set_ylabel(enc)
if get_xlabel is not None:
axes[i, j].set_xlabel(get_xlabel(clf))
title = get_prfx(clf) + get_title(clf)
for metric, vals in metrics.items():
if "{" + metric in title:
title = title.format_map(
PartialFormatMap(**{metric: vals[enc][clf]})
)
title_kwargs = (
dict(color=diagonal_color, fontweight="bold")
if unmodified_i == j and diagonal_color is not None
else {}
)
axes[i, j].set_title(title, **title_kwargs)
if self.is_interactive:
plt.show(axes)
else:
F.savefig(
os.path.join(self.save_dir, f"{self.prfx}{filename}.png"), dpi=self.dpi
)
plt.close(F)
@hydra.main(config_path="conf/config.yaml", strict=True)
def main_cli(args):
return main(args)
def main(args):
main_cfg = deepcopy(args)
del main_cfg["load_models"]
logger.info(f"Loading models for {args.experiment} ...")
analyser = ModelsAnalyser(**args.load_models.kwargs)
logger.info(f"Recolting the data ..")
# Omega conf dictionaries don't have all the properties that usual dictionaries have
analyser.recolt_data(
main_cfg, **OmegaConf.to_container(args.load_models.recolt_data, resolve=True)
)
for f in args.load_models.mode:
logger.info(f"Mode {f} ...")
if f is None:
continue
if f in args.load_models:
kwargs = args.load_models[f]
else:
kwargs = {}
getattr(analyser, f)(**kwargs)
# HELPERS
def plot_rep_clf(trainer, dataset, dataset_test=None, ax=None, **kwargs):
"""
Plot the given representation and the decision boundaries of the classifiers.
"""
# encode the data
transformer = clone_trainer(trainer)
transformer.module_ = transformer.module_.transformer
transformer.module_.is_transform = True
transformer.module_.is_avg_trnsf = True
X, y = get_encoded_X_y(transformer, dataset)
if dataset_test is not None:
test = get_encoded_X_y(transformer, dataset_test)
else:
test = None
# prepare the classifier
clf = clone_trainer(trainer)
clf.module_ = clf.module_.clf
acc = accuracy(clf, X, np.array(y))
log_like = loglike(clf, X, np.array(y))
metrics = dict(acc=acc, loglike=log_like)
ax = plot_2D_decision_boundary(X, y, clf, test=test, ax=ax, **kwargs)
return ax, metrics
def get_encoded_X_y(transformer, dataset):
X = super(type(transformer), transformer).predict_proba(
dataset).astype("float32")
y = [i[1] for i in dataset]
if isinstance(y[0], tuple): # if multitarget
y = [el[0] for el in y]
return X, y
def get_figs_rep_clfs(trainers, datasets, is_plot_test=False, axes=None, **kwargs):
"""Return a list of rep_clf figures for the correct classifiers.
Parameters
----------
trainers : dict of skorch.NeuralNetClassifer with MCTrnsfClassifier module
Trainers that will be used for encoding and classification. The keys of the dictionary
will be selected using clf_patterns.
datasets : dict of datasets
Each element of the dictionary is itself a dictionary of datasets, with a key
"train" for the trainign dataset. The keys should be the same as for trainers.
is_plot_test : bool, optional
Whether to plot some test datapoints.
axes: list matplotlib.axes, optional
List of axis on which to plot.
kwargs :
Additional arguments to `plot_2D_decision_boundary`.
"""
out_axs = dict()
all_metrics = dict()
# collecting all the metrics and data before plotting so that you can use normalize metrics
for i, clf in enumerate(trainers.keys()):
ax = None if axes is None else axes[i]
out_axs[clf], metrics = plot_rep_clf(
trainers[clf],
datasets[clf]["train"],
dataset_test=datasets[clf]["test"] if is_plot_test else None,
ax=ax,
**kwargs,
)
for k, v in metrics.items():
all_metrics[k] = all_metrics.get(k, {})
all_metrics[k][clf] = metrics[k]
return out_axs, all_metrics
if __name__ == "__main__":
main_cli()
|
decodable_information_bottleneck-main
|
load_models.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import contextlib
import copy
import logging
import math
import os
import subprocess
from functools import partial, partialmethod
from pathlib import Path
import hydra
import omegaconf
import skorch
import torch
import torchvision
from omegaconf import OmegaConf
from skorch.callbacks import Callback, EpochScoring, Freezer, LRScheduler, Unfreezer
from dib.classifiers import MCTrnsfClassifier
from dib.predefined import MLP
from dib.training import NeuralNetClassifier, NeuralNetTransformer
from dib.training.helpers import target_extractor
from dib.training.trainer import _get_params_for_optimizer
from dib.transformers import (
DIBLoss,
DIBLossAltern,
DIBLossAlternHigher,
DIBLossAlternLinear,
DIBLossAlternLinearExact,
DIBLossLinear,
ERMLoss,
IBEncoder,
VIBLoss,
get_img_encoder,
)
from dib.utils.helpers import Identity, set_seed
from utils.data import get_train_dev_test_datasets
from utils.evaluate import (
FILE_CLF_REP,
accuracy,
accuracy_filter_train,
eval_clf,
eval_corr_gen,
eval_trainer_log,
eval_trnsf,
loglike,
)
from utils.helpers import (
CrossEntropyLossGeneralize,
SetLR,
StopAtThreshold,
_scoring,
_single_epoch_skipvalid,
append_sffx,
force_generalization,
format_container,
get_exponential_decay_gamma,
hyperparam_to_path,
invert_dict,
save_pattern,
)
from utils.train import train_load
logger = logging.getLogger(__name__)
FILE_LOGS = "log.csv"
FILE_END = "end.txt"
@hydra.main(config_path="conf/config.yaml")
def main_cli(args):
"""Function only used from CLI => to keep main() usable in jupyter"""
if args.is_nvidia_smi:
subprocess.call(["nvidia-smi"])
print()
return main(args)
def main(args):
"""Main function for training and testing representations."""
trainers_return = dict()
datasets_return = dict()
# ARGS
update_config_(args)
set_seed(args.seed)
# DATASET
datasets = get_datasets(args)
datasets_trnsf = prepare_transformer_datasets(args, datasets)
update_config_datasets_(args, datasets_trnsf)
# TRANSFORMER (i.e. Encoder)
Transformer = get_Transformer(args, datasets_trnsf)
if args.is_precompute_trnsf:
name = "transformer"
trainers_return[name] = fit_evaluate_trainer(
Transformer, args, name, datasets_trnsf, True
)
datasets_return[name] = prepare_return_datasets(datasets_trnsf)
else:
# loading the pretrained transformer
transformer = fit_trainer(
Transformer,
args,
datasets_trnsf,
True,
"transformer",
is_load_criterion=False,
)
datasets = prepare_classification_datasets_(args, datasets)
for Classifier, clf_name in gen_Classifiers_name(args, transformer, datasets):
trainers_return[clf_name] = fit_evaluate_trainer(
Classifier,
args,
clf_name,
datasets,
False,
is_return_init=args.is_correlation_Bob,
)
datasets_return[clf_name] = prepare_return_datasets(datasets)
if args.is_return:
return trainers_return, datasets_return
def update_config_(args):
"""Update the configuration values based on other values."""
# increment the seed at each run
args.seed = args.seed + args.run
# multiply the number of examples by a factor size. Used to have number of examples depending
# on number of labels. Usually factor is 1.
args.datasize.n_examples = args.datasize.factor * args.datasize.n_examples
if args.datasize.n_examples_test == "train":
# use same number of train and test examples
args.datasize.n_examples_test = args.datasize.n_examples
if args.is_precompute_trnsf and args.train.trnsf_kwargs.is_train:
# if training transformer then paths need to agree
assert args.paths["trnsf_dirnames"][0] == args.paths["chckpnt_dirnames"][0]
# monitor training when you randomize the labels because validation does not mean anything
if args.dataset.kwargs.is_random_targets:
args.train.trnsf_kwargs.monitor_best = "train_loss_best"
args.train.clf_kwargs.monitor_best = "train_loss_best"
if not args.train.is_tensorboard:
args.paths["tensorboard_curr_dir"] = None
if args.experiment == "gap":
# dib with Q++
if args.model.name == "vib":
args.model.loss.beta = args.model.loss.beta * 40
elif args.model.name == "cdibL":
args.model.loss.beta = args.model.loss.beta / 100
elif args.model.name == "cdibS":
args.model.loss.beta = args.model.loss.beta * 30
if "dibL" in args.model.name:
# dib with Q++
args.model.Q_zx.hidden_size = args.model.Q_zy.hidden_size * 64
if "dibS" in args.model.name:
# dib with Q--
args.model.Q_zx.hidden_size = args.model.Q_zy.hidden_size // 64
if "dibXS" in args.model.name:
# dib with Q------
args.model.Q_zx.hidden_size = 1
if "dibXL" in args.model.name:
# dib with Q++++++++
args.model.Q_zx.hidden_size = 8192
short_long_monitor = dict(
vloss="valid_loss_best", tloss="train_loss_best", vacc="valid_acc_best"
)
# use short version for name of file
args.train.monitor_best = invert_dict(short_long_monitor).get(
args.train.monitor_best, args.train.monitor_best
)
hyperparam_path = hyperparam_to_path(args.hyperparameters)
args.paths.merge_with(
OmegaConf.create(
format_container(args.paths, dict(hyperparam_path=hyperparam_path))
)
)
# every change that should not modify the name of the file should go below this
# ----------------------------------------------------------------------------
# use long version in code
args.train.monitor_best = short_long_monitor.get(
args.train.monitor_best, args.train.monitor_best
)
args.train.trnsf_kwargs.monitor_best = short_long_monitor.get(
args.train.trnsf_kwargs.monitor_best, args.train.trnsf_kwargs.monitor_best
)
args.train.clf_kwargs.monitor_best = short_long_monitor.get(
args.train.clf_kwargs.monitor_best, args.train.clf_kwargs.monitor_best
)
if not args.is_precompute_trnsf:
logger.info("Not precomputing the transformer so setting train=False.")
args.train.trnsf_kwargs.is_train = False
args.train.kwargs.lr = args.train.lr_clf # ! DEV
else:
if args.model.name == "wdecayBob":
args.train.weight_decay = 1e-4
if args.model.name == "dropoutBob":
args.encoder.architecture.dropout = 0.5
if not args.datasize.is_valid_all_epochs and "train" in args.train.monitor_best:
# don't validate all epochs when validation >>> training and you only look at training
rm_valid_epochs_()
if args.model.is_joint:
args.model.gamma_force_generalization = 1
if "distractor" in args.clfs.name and not args.is_precompute_trnsf:
args.dataset.is_use_distractor = True
if "random" in args.clfs.name and not args.is_precompute_trnsf:
# if you want random dataset for classifier then make sure you are not randomizing for encoder
args.dataset.kwargs.is_random_targets = True
args.train.clf_kwargs.monitor_best = "train_loss_best" # don't monitor val
if isinstance(args.train.kwargs.lr, str) and "|" in args.train.kwargs.lr:
lr, lr_factor_zx = args.train.kwargs.lr.split("|")
args.train.kwargs.lr = float(lr)
args.train.lr_factor_zx = float(lr_factor_zx)
if args.model.name == "vibL":
# keep alice the same but increase bob view of alice
# vib with better approx of I[Z,Y] Q++
args.model.Q_zy.hidden_size = args.model.Q_zy.hidden_size * 16
if args.model.name == "wdecay":
args.train.weight_decay = 1e-4
if "correlation" in args.experiment:
if args.train.optim == "rmsprop":
if args.train.weight_decay == 0.0005:
args.train.weight_decay = 0.0003
elif args.train.optim == "sgd":
args.train.kwargs.lr = args.train.kwargs.lr * 50
if "perminvcdib" in args.model.name:
args.encoder.architecture.hidden_size = [1024]
args.model.architecture.z_dim = 1024
args.model.Q_zy.hidden_size = 256
args.model.Q_zy.n_hidden_layers = 1
def add_none(a, b):
if a is None or b is None:
return None
return a + b
def rm_valid_epochs_():
"""Don't validate every epoch."""
NeuralNetTransformer._single_epoch = _single_epoch_skipvalid
NeuralNetClassifier._single_epoch = _single_epoch_skipvalid
skorch.callbacks.scoring.ScoringBase._scoring = _scoring
def get_datasets(args):
"""return a dictionary of train, test, valid, datasets."""
logger.info("Loading the dataset ...")
datasets = get_train_dev_test_datasets(
args.dataset.name,
args.dataset.type,
valid_size=args.dataset.valid_size,
**OmegaConf.to_container(args.dataset.kwargs, resolve=True),
)
# Subsetting dataset if needed
datasets["train"] = datasets["train"].get_subset(
size=args.datasize.n_examples)
datasets["test"] = datasets["test"].get_subset(
size=args.datasize.n_examples_test)
if args.dataset.is_use_distractor:
for dataset in datasets.values():
dataset._switch_distractor_target() # will only work if dataset has a distractor
if args.dataset.train == "trainvalid":
# for VIB MNIST experiment
datasets["train"].append_(datasets["valid"])
args.dataset.train = "train"
datasets["train"], datasets["valid"], datasets["test"] = (
datasets[args.dataset.train],
datasets[args.dataset.valid],
datasets[args.dataset.test],
)
return datasets
def get_Transformer(args, datasets):
"""Return the correct transformer."""
logger.info("Instantiating the transformer ...")
# Q used for sufficiency
Q_zy = partial(
MLP, **OmegaConf.to_container(args.model.Q_zy, resolve=True))
# Q used for minimality
Q_zx = partial(
MLP, **OmegaConf.to_container(args.model.Q_zx, resolve=True))
kwargs_loss = OmegaConf.to_container(args.model.loss, resolve=True)
kwargs_loss["Q"] = Q_zx
kwargs_trnsf = dict(Q=Q_zy)
Losses = dict(
VIBLoss=VIBLoss, ERMLoss=ERMLoss, DIBLossSklearn=DIBLossAlternLinearExact
)
is_linear = args.model.Q_zx.n_hidden_layers == 0
altern_minimax = args.model.loss.altern_minimax
kwargs = {}
if altern_minimax > 0:
if is_linear:
Losses["DIBLoss"] = DIBLossAlternLinear
else:
Losses["DIBLoss"] = (
DIBLossAlternHigher if args.model.loss.is_higher else DIBLossAltern
)
elif args.model.Loss == "DIBLoss":
# in the case where doing joint training you need to give the parameters of the criterion
# to the main (and only) optimizer
NeuralNetTransformer._get_params_for_optimizer = partialmethod(
_get_params_for_optimizer, is_add_criterion=True
)
Losses["DIBLoss"] = DIBLossLinear if is_linear else DIBLoss
kwargs["optimizer__param_groups"] = [
("Q_zx*", {"lr": args.train.kwargs.lr * args.train.lr_factor_zx})
]
return partial(
NeuralNetTransformer,
module=partial(
partial(IBEncoder, **kwargs_trnsf),
Encoder=partial(
get_img_encoder(args.encoder.name),
**OmegaConf.to_container(args.encoder.architecture, resolve=True),
),
**OmegaConf.to_container(args.model.architecture, resolve=True),
),
optimizer=get_optim(args),
criterion=partial(
Losses[args.model.Loss],
ZYCriterion=partial(
CrossEntropyLossGeneralize,
gamma=args.model.gamma_force_generalization,
map_target_position=datasets["train"].map_target_position,
),
**kwargs_loss,
),
callbacks__print_log__keys_ignored=args.keys_ignored,
**kwargs,
)
def fit_evaluate_trainer(Trainer, args, name, datasets, is_trnsf, **kwargs):
"""Fit and evaluate a single trainer."""
file_after_train = get_file_after_train(args, name)
if not get_is_already_trained(args, file_after_train, is_trnsf):
trainer = fit_trainer(Trainer, args, datasets,
is_trnsf, name, **kwargs)
if args.train.is_evaluate:
evaluate_trainer(trainer, args, datasets, is_trnsf, name)
Path(file_after_train).touch(exist_ok=True)
return trainer
def get_file_after_train(args, name):
"""Return a placeholder file which is used to say whether the transformer has been precomputed."""
chckpnt_paths = get_chckpnt_paths(args, name)
return os.path.join(chckpnt_paths[0], FILE_END)
def get_is_already_trained(args, file_after_train, is_trnsf):
"""Whether the encoder is already precomputed."""
if is_trnsf:
is_skip = args.is_skip_trnsf_if_precomputed
else:
is_skip = args.is_skip_clf_if_precomputed
if not args.is_return and is_skip and os.path.isfile(file_after_train):
logger.info(f"Not training because {file_after_train} exists.")
return True
# making sure the placeholder doesn't exist if you will retrain the model
with contextlib.suppress(FileNotFoundError):
os.remove(file_after_train)
return False
def prepare_transformer_datasets(args, datasets):
"""Return a transformer dataset (not inplace)."""
# make sure don't change the dataset for eval and clf
datasets = copy.deepcopy(datasets)
# store the old training for evaluation
datasets["train_unmodified"] = datasets["train"]
gamma = args.model.gamma_force_generalization
if gamma != 0:
datasets["train"] = force_generalization(datasets)
if not args.model.is_joint:
if gamma == "zero":
# trick to add test data even without using gamma
gamma = 0
# gamma is rescaled to depend on size of train and test (i.e. be relative) but not if have access
# to joint (in which case you should really have access to all train and test not rescaled)
gamma *= len(datasets["train"]) / len(datasets["test"])
args.model.gamma_force_generalization = gamma
return datasets
def update_config_datasets_(args, datasets):
"""Update the configuration values based on the datasets."""
args.datasize.n_examples = len(datasets["train"]) # store as an integer
steps_per_epoch = len(datasets["train"]) // args.datasize.batch_size
args.model.loss.warm_Q_zx = steps_per_epoch * args.model.loss.warm_Q_zx
count_targets = datasets["train"].count_targets()
with omegaconf.open_dict(args):
args.model.loss.n_per_target = {
str(k): int(v) for k, v in count_targets.items()
}
def fit_trainer(
Trainer, args, datasets, is_trnsf, name, is_load_criterion=True, **kwargs
):
"""Fits the given trainer on the datasets."""
logger.info(f"Fitting {name} ...")
specific_kwargs = args.train["trnsf_kwargs" if is_trnsf else "clf_kwargs"]
chckpnt_paths = get_chckpnt_paths(args, name)
trainer = train_load(
Trainer,
datasets,
# always save the transformer at the precomputed path
chckpnt_dirnames=get_chckpnt_paths(args, name),
tensorboard_dir=get_tensorboard_paths(args, name),
is_load_criterion=is_load_criterion,
callbacks=get_callbacks(args, datasets, is_trnsf=is_trnsf),
**OmegaConf.to_container(args.train.kwargs, resolve=True),
**OmegaConf.to_container(specific_kwargs, resolve=True),
**kwargs,
)
if specific_kwargs.is_train:
log_training(trainer, args.csv_score_pattern, chckpnt_paths)
return trainer
def get_tensorboard_paths(args, name):
"""Return the paths for tensorboard"""
return add_none(args.paths["tensorboard_curr_dir"], name)
def get_chckpnt_paths(args, name):
"""Return the paths for the classifiers checkpoint"""
return append_sffx(args.paths["chckpnt_dirnames"], name)
def get_callbacks(args, datasets, is_trnsf):
"""Return the correct callbacks for training."""
if is_trnsf:
callbacks = [
(
"valid_acc",
EpochScoring(
accuracy, # cannot use "accuracy" because using a transformer rather than classifier
name="valid_acc",
lower_is_better=False,
target_extractor=target_extractor,
),
),
(
"valid_loglike",
EpochScoring(
loglike, # the actual loss also contains all regularization
name="valid_loglike",
lower_is_better=False,
target_extractor=target_extractor,
),
),
]
else:
callbacks = []
callbacks += [
(
"train_acc",
EpochScoring(
partial(
accuracy_filter_train,
map_target_position=datasets["train"].map_target_position,
),
name="train_acc",
on_train=True,
lower_is_better=False,
target_extractor=partial(
target_extractor, is_multi_target=True),
),
)
]
callbacks += get_lr_schedulers(args, datasets, is_trnsf=is_trnsf)
# callbacks += [skorch.callbacks.GradientNormClipping(gradient_clip_value=0.1)]
if args.train.freezer.patterns is not None:
callbacks += [
Freezer(
args.train.freezer.patterns,
at=args.train.freezer.at
if args.train.freezer.at is not None
else return_True,
)
]
if args.train.unfreezer.patterns is not None:
callbacks += [
Unfreezer(args.train.unfreezer.patterns,
at=args.train.unfreezer.at)
]
if args.train.ce_threshold is not None:
callbacks += [StopAtThreshold(threshold=args.train.ce_threshold)]
return callbacks
def return_True(*args):
return True
def get_optim(args):
if args.train.optim == "sgd":
return partial(
torch.optim.SGD, momentum=0.9, weight_decay=args.train.weight_decay
)
elif args.train.optim == "adam":
return partial(torch.optim.Adam, weight_decay=args.train.weight_decay)
elif args.train.optim == "adam":
return partial(torch.optim.Adam, weight_decay=args.train.weight_decay)
elif args.train.optim == "rmsprop":
return partial(torch.optim.RMSprop, weight_decay=args.train.weight_decay)
elif args.train.optim == "adagrad":
return partial(torch.optim.Adagrad, weight_decay=args.train.weight_decay)
elif args.train.optim == "adamw":
return partial(
torch.optim.AdamW, weight_decay=args.train.weight_decay, amsgrad=True
)
elif args.train.optim == "LBFGS":
NeuralNetTransformer.train_step = train_step_set_optim
return partial(
torch.optim.LBFGS,
line_search_fn="strong_wolfe",
history_size=10,
max_iter=7,
)
else:
raise ValueError(f"Unkown optim={args.train.optim}")
def get_lr_schedulers(args, datasets, is_trnsf):
if args.train.scheduling_mode == "decay":
gamma = get_exponential_decay_gamma(
args.train.scheduling_factor, args.train.trnsf_kwargs.max_epochs
)
lr_scheduler = [
LRScheduler(torch.optim.lr_scheduler.ExponentialLR, gamma=gamma)
]
elif args.train.scheduling_mode == "plateau":
lr_scheduler = [
LRScheduler(
torch.optim.lr_scheduler.ReduceLROnPlateau,
monitor="valid_loss",
factor=0.2,
)
]
elif args.train.scheduling_mode == "biplateau":
lr_scheduler = [
LRScheduler(
torch.optim.lr_scheduler.ReduceLROnPlateau,
monitor="valid_loss",
factor=0.2, # 0.1
patience=3, # 0.5
verbose=True,
threshold=0.01,
min_lr=1e-5,
),
# increase lr but max at 0.5
LRScheduler(SetLR, lr_lambda=lambda _, lr, __: min(lr * 1.3, 0.5)),
# dirty way for not increasing lr in case loss didn't improve
LRScheduler(
torch.optim.lr_scheduler.ReduceLROnPlateau,
monitor="valid_loss",
factor=1 / 1.3,
patience=1,
),
]
elif args.train.scheduling_mode is None:
lr_scheduler = []
else:
raise ValueError(
f"Unkown scheduling_mode={args.train.scheduling_mode}")
return lr_scheduler
def log_training(trainer, csv_score_pattern, chckpnt_dirnames):
"""Log training history (loss and accuracy) in a more readable format ."""
save_pattern(chckpnt_dirnames, csv_score_pattern, FILE_LOGS)
for h in trainer.history:
if not math.isnan(h["valid_acc"]):
for metric in ["acc", "loss"]:
for mode in ["train", "valid"]:
try:
save_pattern(
chckpnt_dirnames,
csv_score_pattern,
FILE_LOGS,
formatting=dict(
epoch=h["epoch"],
metric=metric,
mode=mode,
score=h[f"{mode}_{metric}"],
),
)
except KeyError as e:
logger.debug(
f"Skipping a loop because couldn't find key {e}")
def evaluate_trainer(trainer, args, datasets, is_trnsf, name, **kwargs):
"""Evaluate trainers on the train and test dataset."""
logger.info(f"Evaluating {name} ...")
if is_trnsf:
evaluator = eval_trnsf
else:
evaluator = eval_corr_gen if args.is_correlation else eval_clf
chckpnt_paths = get_chckpnt_paths(args, name)
tensorboard_dir = get_tensorboard_paths(args, name)
trainers = {"best": trainer}
is_append = False
for epoch, trainer in trainers.items():
# Test Evaluation
eval_trainer_log(
trainer,
datasets["test"],
args.csv_score_pattern,
chckpnt_paths,
dict(args.hyperparameters),
evaluator=evaluator,
tensorboard_dir=tensorboard_dir,
epoch=epoch,
is_append=is_append,
**kwargs,
)
# only create the field for the first time you log
is_append = True
# evaluation should be made on the training without any addition (e.g. anti generalization)
train_data = datasets.get("train_unmodified", datasets["train"])
# Train Evaluation
eval_trainer_log(
trainer,
train_data,
args.csv_score_pattern,
chckpnt_paths,
dict(args.hyperparameters),
evaluator=evaluator,
tensorboard_dir=None,
is_append=is_append,
mode="train",
file_clf_rep="train_" + FILE_CLF_REP,
epoch=epoch,
**kwargs,
)
def prepare_classification_datasets_(args, datasets):
"""Modify inplace the datasets before classification."""
# store the old training for evaluation
datasets["train_unmodified"] = datasets["train"]
gamma = args.clfs.gamma_force_generalization
if args.clfs.gamma_force_generalization != 0:
datasets["train"] = force_generalization(datasets)
args.clfs.gamma_force_generalization = gamma
return datasets
def gen_Classifiers_name(args, transformer, datasets):
"""Generator of uninstantiated classifiers."""
gamma = args.clfs.gamma_force_generalization
data_weight = len(datasets["train"]) / len(datasets["test"])
for n_hid in OmegaConf.to_container(args.clfs.nhiddens, resolve=True):
for n_lay in OmegaConf.to_container(args.clfs.nlayers, resolve=True):
for k_pru in OmegaConf.to_container(args.clfs.kprune, resolve=True):
clf_name = (
f"clf_nhid_{n_hid}/clf_nlay_{n_lay}/clf_kpru_{k_pru}/gamma_{gamma}/"
)
Classifier = partial(
MLP, hidden_size=n_hid, n_hidden_layers=n_lay, k_prune=k_pru
)
kwargs = {}
if not args.clfs.is_reinitialize:
kwargs["previous_mlp"] = transformer.module_.Q_zy
Classifier = partial(
NeuralNetClassifier,
module=partial(
MCTrnsfClassifier,
transformer=transformer.module_,
Classifier=Classifier,
**OmegaConf.to_container(args.clfs.kwargs, resolve=True),
**kwargs,
),
# don't use any regularization if you only care about training (e.g. Rademacher)
optimizer=get_optim(args),
criterion=partial(
CrossEntropyLossGeneralize,
gamma=gamma * data_weight,
map_target_position=datasets["train"].map_target_position,
),
)
yield Classifier, clf_name
def prepare_return_datasets(datasets):
"""Prepares the datasets to return them"""
datasets = copy.deepcopy(datasets)
# removing modifications to trainign set such as adding the test set for anti generalizatiom
if "train_unmodified" in datasets:
datasets["train_modified"] = datasets["train"]
datasets["train"] = datasets["train_unmodified"]
return datasets
if __name__ == "__main__":
main_cli()
|
decodable_information_bottleneck-main
|
main.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
# should be in a hydra file
UNLABELLED_CLASS = -1
|
decodable_information_bottleneck-main
|
dib/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .trainer import *
|
decodable_information_bottleneck-main
|
dib/training/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import warnings
from contextlib import suppress
import numpy as np
import skorch
import torch
import torch.nn as nn
from scipy.special import softmax
from sklearn.base import ClassifierMixin, TransformerMixin
from skorch import NeuralNet
from skorch.callbacks import EpochScoring, ProgressBar
from skorch.dataset import Dataset, get_len, unpack_data, uses_placeholder_y
from skorch.helper import predefined_split
from skorch.history import History
from skorch.utils import TeeGenerator, get_map_location, to_numpy, to_tensor
from .helpers import FixRandomSeed, target_extractor
logger = logging.getLogger(__name__)
__all__ = ["NeuralNetTransformer", "NeuralNetClassifier"]
net_get_params_for_optimizer = NeuralNet._get_params_for_optimizer
def _get_params_for_optimizer(self, prefix, named_parameters, is_add_criterion=False):
"""Difference with default is that also adds the parameters of the criterion."""
if is_add_criterion:
named_parameters = list(named_parameters) + list(
self.criterion_.named_parameters()
)
return net_get_params_for_optimizer(self, prefix, named_parameters)
def save_params(self, f_criterion=None, **kwargs):
"""Difference with default is that also saves the criterion."""
NeuralNet.save_params(self, **kwargs)
if f_criterion is not None:
msg = (
"Cannot save parameters of an un-initialized criterion. "
"Please initialize first by calling .initialize() "
"or by fitting the model with .fit(...)."
)
self.check_is_fitted(attributes=["criterion_"], msg=msg)
torch.save(self.criterion_.state_dict(), f_criterion)
def load_params(self, f_criterion=None, checkpoint=None, **kwargs):
NeuralNet.load_params(self, checkpoint=checkpoint, **kwargs)
####################
# all this copy pasted
def _get_state_dict(f):
map_location = get_map_location(self.device)
self.device = self._check_device(self.device, map_location)
return torch.load(f, map_location=map_location)
if checkpoint is not None:
if not self.initialized_:
self.initialize()
formatted_files = checkpoint.get_formatted_files(self)
####################
f_criterion = f_criterion or formatted_files["f_criterion"]
if f_criterion is not None:
msg = (
"Cannot load state of an un-initialized criterion. "
"Please initialize first by calling .initialize() "
"or by fitting the model with .fit(...)."
)
self.check_is_fitted(attributes=["criterion_"], msg=msg)
state_dict = _get_state_dict(f_criterion)
self.criterion_.load_state_dict(state_dict)
def get_loss(self, y_pred, y_true, X=None, training=False):
"""Return the loss for this batch."""
y_true = to_tensor(y_true, device=self.device)
if isinstance(self.criterion_, nn.Module):
self.criterion_.train(training)
return self.criterion_(y_pred, y_true)
def fit_loop(self, X, y=None, epochs=None, **fit_params):
self.check_data(X, y)
epochs = epochs if epochs is not None else self.max_epochs
dataset_train, dataset_valid = self.get_split_datasets(X, y, **fit_params)
on_epoch_kwargs = {"dataset_train": dataset_train,
"dataset_valid": dataset_valid}
start = 0
if self.is_train_delta_epoch:
# in case you load the model you want to only train the epoch difference
start = len(self.history)
# make sure that still run 1 epoch for model saving / notify
start = min(start, epochs - 1)
logger.info(f"Model was loaded,training only {start}-{epochs}")
for epoch in range(start, epochs):
self.notify("on_epoch_begin", **on_epoch_kwargs)
self._single_epoch(dataset_train, training=True,
epoch=epoch, **fit_params)
if dataset_valid is not None:
self._single_epoch(dataset_valid, training=False,
epoch=epoch, **fit_params)
self.notify("on_epoch_end", **on_epoch_kwargs)
return self
def _single_epoch(self, dataset, training, epoch, **fit_params):
"""Computes a single epoch of train or validation."""
is_placeholder_y = uses_placeholder_y(dataset)
if training:
prfx = "train"
step_fn = self.train_step
else:
prfx = "valid"
step_fn = self.validation_step
batch_count = 0
for data in self.get_iterator(dataset, training=training):
Xi, yi = unpack_data(data)
yi_res = yi if not is_placeholder_y else None
self.notify("on_batch_begin", X=Xi, y=yi_res, training=training)
step = step_fn(Xi, yi, **fit_params)
self.history.record_batch(prfx + "_loss", step["loss"].item())
self.history.record_batch(prfx + "_batch_size", get_len(Xi))
self.notify("on_batch_end", X=Xi, y=yi_res, training=training, **step)
batch_count += 1
self.history.record(prfx + "_batch_count", batch_count)
if hasattr(self.criterion_, "to_store"):
for k, v in self.criterion_.to_store.items():
with suppress(NotImplementedError):
# pytorch raises NotImplementedError on wrong types
self.history.record(prfx + "_" + k, (v[0] / v[1]).item())
self.criterion_.to_store = dict()
doc_neural_net_clf = (
"""Wrapper around skorch.NeuralNetClassifier. Differences:
Parameters
----------
Notes
-----
- use by default crossentropy loss instead of NNLoss
- enables storing of additional losses.
Base documentation:
"""
+ skorch.NeuralNetClassifier.__doc__
)
class NeuralNetClassifier(skorch.NeuralNetClassifier):
__doc__ = doc_neural_net_clf
def __init__(
self,
*args,
criterion=torch.nn.CrossEntropyLoss,
is_train_delta_epoch=True,
**kwargs,
):
super().__init__(*args, criterion=criterion, **kwargs)
self.is_train_delta_epoch = is_train_delta_epoch
@property
def _default_callbacks(self):
_default_callbacks = dict(super()._default_callbacks)
_default_callbacks["valid_acc"] = EpochScoring(
"accuracy",
name="valid_acc",
lower_is_better=False,
target_extractor=target_extractor,
)
return [(k, v) for k, v in _default_callbacks.items()]
def predict_proba(self, X):
"""Return probability estimates for samples.
Notes
-----
- output of model should be logits (softmax applied in this function)
- If the module's forward method returns multiple outputs as a
tuple, it is assumed that the first output contains the
relevant information and the other values are ignored. If all
values are relevant, consider using :func:`~skorch.NeuralNet.forward`
instead.
Returns
-------
y_proba : numpy ndarray
"""
# output of model should be logits!
logits = super().predict_proba(X)
return softmax(logits, axis=1)
fit_loop = fit_loop
_single_epoch = _single_epoch
get_loss = get_loss
_get_params_for_optimizer = _get_params_for_optimizer
save_params = save_params
load_params = load_params
doc_neural_net_trnsf = (
"""Wrapper around skorch.NeuralNet for transforming data. Differences:
Methods
-------
freeze:
Freezes the model such that it cannot be fitted again.
transform:
Returns a numpy array containing all the first outputs, similarly to `.predict`. The main
difference is that it sets `module_.is_transform` to `True`. The correct behavior should thus
be implemented in the module using `is_transform` flag.
Notes
-----
- enables storing of additional losses.
Base documentation:
"""
+ skorch.NeuralNet.__doc__
)
class NeuralNetTransformer(skorch.NeuralNet, TransformerMixin):
__doc__ = doc_neural_net_trnsf
def __init__(self, *args, is_train_delta_epoch=True, **kwargs):
super().__init__(*args, **kwargs)
self.is_train_delta_epoch = is_train_delta_epoch
def freeze(self, is_freeze=True):
"""Freezes (or unfreeze) the model such that it cannot be fitted again."""
self._is_frozen = is_freeze
return self
def fit(self, X, y=None, **fit_params):
if hasattr(self, "_is_frozen") and self._is_frozen:
if self.verbose > 0:
warnings.warn("Skipping fitting because froze etimator.")
return self
return super().fit(X, y=y, **fit_params)
def transform(self, X):
"""Transform an input."""
self.module_.is_transform = True
self.module_.training = False
X_transf = super().predict_proba(X) # does not actually predict proba
self.module_.is_transform = False
self.module_.training = True
return X_transf
def predict_proba(self, X):
"""Return probability estimates for samples.
Notes
-----
- output of model should be logits (softmax applied in this function)
- If the module's forward method returns multiple outputs as a
tuple, it is assumed that the first output contains the
relevant information and the other values are ignored. If all
values are relevant, consider using :func:`~skorch.NeuralNet.forward`
instead.
Returns
-------
y_proba : numpy ndarray
"""
# output of model should be logits!
logits = super().predict_proba(X)
return softmax(logits, axis=1)
fit_loop = fit_loop
_single_epoch = _single_epoch
get_loss = get_loss
_get_params_for_optimizer = _get_params_for_optimizer
save_params = save_params
load_params = load_params
|
decodable_information_bottleneck-main
|
dib/training/trainer.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import copy
import random
import warnings
import numpy as np
import skorch
import torch
from skorch.callbacks import Callback
from dib.utils.helpers import cont_tuple_to_tuple_cont, ratio_to_int, set_seed, to_numpy
def target_extractor(targets, is_multi_target=False):
"""
Helper function that extracts the targets for scoring. There can be multiple targets
for the case where you appended indices or distractors.
"""
if isinstance(targets, (list, tuple)):
if is_multi_target:
targets = torch.stack(targets, axis=1)
else:
targets = targets[0]
return to_numpy(targets)
def clone_trainer(trainer, is_reinit_besides_param=False):
"""Clone a trainer with optional possibility of reinitializing everything besides
parameters (e.g. optimizers.)"""
with warnings.catch_warnings():
warnings.simplefilter("ignore")
trainer_new = copy.deepcopy(trainer)
if is_reinit_besides_param:
trainer_new.initialize_callbacks()
trainer_new.initialize_criterion()
trainer_new.initialize_optimizer()
trainer_new.initialize_history()
return trainer_new
class FixRandomSeed(Callback):
"""
Callback to have a deterministic behavior.
Credits: https://github.com/skorch-dev/skorch/issues/280
"""
def __init__(self, seed=123, is_cudnn_deterministic=False, verbose=0):
self.seed = seed
self.is_cudnn_deterministic = is_cudnn_deterministic
self.verbose = verbose
def initialize(self):
if self.seed is not None:
if self.verbose > 0:
print("setting random seed to: ", self.seed, flush=True)
set_seed(self.seed)
torch.backends.cudnn.deterministic = self.is_cudnn_deterministic
class Checkpoint(skorch.callbacks.Checkpoint):
"""
Difference with default is that save criterion.
"""
def __init__(self, *args, f_criterion="criterion.pt", **kwargs):
super().__init__(*args, **kwargs)
self.f_criterion = f_criterion
def save_model(self, net):
super().save_model(net)
if self.f_criterion is not None:
f = self._format_target(net, self.f_criterion, -1)
self._save_params(f, net, "f_criterion", "criterion parameters")
def get_formatted_files(self, net):
idx = -1
if self.event_name is not None and net.history:
for i, v in enumerate(net.history[:, self.event_name]):
if v:
idx = i
return {
"f_params": self._format_target(net, self.f_params, idx),
"f_optimizer": self._format_target(net, self.f_optimizer, idx),
"f_history": self.f_history_,
# ONLY DIFF
"f_pickle": self._format_target(net, self.f_pickle, idx),
"f_criterion": self._format_target(net, self.f_criterion, idx),
}
|
decodable_information_bottleneck-main
|
dib/training/helpers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
Pruning methods modified from:
https://pytorch.org/docs/master/_modules/torch/nn/utils/prune.html
"""
import numbers
from abc import abstractmethod
import torch
# For Python 2 and 3 support
try:
from abc import ABC
from collections.abc import Iterable
except ImportError:
from abc import ABCMeta
ABC = ABCMeta("ABC", (), {})
from collections import Iterable
class BasePruningMethod(ABC):
r"""Abstract base class for creation of new pruning techniques.
Provides a skeleton for customization requiring the overriding of methods
such as :meth:`compute_mask` and :meth:`apply`.
"""
def __init__(self):
pass
def __call__(self, module, inputs):
r"""Multiplies the mask (stored in ``module[name + '_mask']``)
into the original tensor (stored in ``module[name + '_orig']``)
and stores the result into ``module[name]`` by using
:meth:`apply_mask`.
Args:
module (nn.Module): module containing the tensor to prune
inputs: not used.
"""
setattr(module, self._tensor_name, self.apply_mask(module))
@abstractmethod
def compute_mask(self, t, default_mask):
r"""Computes and returns a mask for the input tensor ``t``.
Starting from a base ``default_mask`` (which should be a mask of ones
if the tensor has not been pruned yet), generate a random mask to
apply on top of the ``default_mask`` according to the specific pruning
method recipe.
Args:
t (torch.Tensor): tensor representing the parameter to prune
default_mask (torch.Tensor): Base mask from previous pruning
iterations, that need to be respected after the new mask is
applied. Same dims as ``t``.
Returns:
mask (torch.Tensor): mask to apply to ``t``, of same dims as ``t``
"""
pass
def apply_mask(self, module):
r"""Simply handles the multiplication between the parameter being
pruned and the generated mask.
Fetches the mask and the original tensor from the module
and returns the pruned version of the tensor.
Args:
module (nn.Module): module containing the tensor to prune
Returns:
pruned_tensor (torch.Tensor): pruned version of the input tensor
"""
# to carry out the multiplication, the mask needs to have been computed,
# so the pruning method must know what tensor it's operating on
assert self._tensor_name is not None, "Module {} has to be pruned".format(
module
) # this gets set in apply()
mask = getattr(module, self._tensor_name + "_mask")
orig = getattr(module, self._tensor_name + "_orig")
pruned_tensor = mask.to(dtype=orig.dtype) * orig
return pruned_tensor
@classmethod
def apply(cls, module, name, *args, **kwargs):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
args: arguments passed on to a subclass of
:class:`BasePruningMethod`
kwargs: keyword arguments passed on to a subclass of a
:class:`BasePruningMethod`
"""
def _get_composite_method(cls, module, name, *args, **kwargs):
# Check if a pruning method has already been applied to
# `module[name]`. If so, store that in `old_method`.
old_method = None
found = 0
# there should technically be only 1 hook with hook.name == name
# assert this using `found`
hooks_to_remove = []
for k, hook in module._forward_pre_hooks.items():
# if it exists, take existing thing, remove hook, then
# go thru normal thing
if isinstance(hook, BasePruningMethod) and hook._tensor_name == name:
old_method = hook
# # reset the tensor reparametrization
# module = remove_pruning(module, name)
# del module._forward_pre_hooks[k]
hooks_to_remove.append(k)
found += 1
assert (
found <= 1
), "Avoid adding multiple pruning hooks to the\
same tensor {} of module {}. Use a PruningContainer.".format(
name, module
)
for k in hooks_to_remove:
del module._forward_pre_hooks[k]
# Apply the new pruning method, either from scratch or on top of
# the previous one.
method = cls(*args, **kwargs) # new pruning
# Have the pruning method remember what tensor it's been applied to
method._tensor_name = name
# combine `methods` with `old_method`, if `old_method` exists
if old_method is not None: # meaning that there was a hook
# if the hook is already a pruning container, just add the
# new pruning method to the container
if isinstance(old_method, PruningContainer):
old_method.add_pruning_method(method)
method = old_method # rename old_method --> method
# if the hook is simply a single pruning method, create a
# container, add the old pruning method and the new one
elif isinstance(old_method, BasePruningMethod):
container = PruningContainer(old_method)
# Have the pruning method remember the name of its tensor
# setattr(container, '_tensor_name', name)
container.add_pruning_method(method)
method = container # rename container --> method
return method
method = _get_composite_method(cls, module, name, *args, **kwargs)
# at this point we have no forward_pre_hooks but we could have an
# active reparametrization of the tensor if another pruning method
# had been applied (in which case `method` would be a PruningContainer
# and not a simple pruning method).
# Pruning is to be applied to the module's tensor named `name`,
# starting from the state it is found in prior to this iteration of
# pruning
orig = getattr(module, name)
# If this is the first time pruning is applied, take care of moving
# the original tensor to a new parameter called name + '_orig' and
# and deleting the original parameter
if not isinstance(method, PruningContainer):
# copy `module[name]` to `module[name + '_orig']`
module.register_parameter(name + "_orig", orig)
# temporarily delete `module[name]`
del module._parameters[name]
default_mask = torch.ones_like(orig) # temp
# If this is not the first time pruning is applied, all of the above
# has been done before in a previos pruning iteration, so we're good
# to go
else:
default_mask = getattr(module, name + "_mask").detach().clone()
# Use try/except because if anything goes wrong with the mask
# computation etc., you'd want to roll back.
try:
# get the final mask, computed according to the specific method
mask = method.compute_mask(orig, default_mask=default_mask)
# reparametrize by saving mask to `module[name + '_mask']`...
module.register_buffer(name + "_mask", mask)
# ... and the new pruned tensor to `module[name]`
setattr(module, name, method.apply_mask(module))
# associate the pruning method to the module via a hook to
# compute the function before every forward() (compile by run)
module.register_forward_pre_hook(method)
except Exception as e:
if not isinstance(method, PruningContainer):
orig = getattr(module, name + "_orig")
module.register_parameter(name, orig)
del module._parameters[name + "_orig"]
raise e
return method
def prune(self, t, default_mask=None):
r"""Computes and returns a pruned version of input tensor ``t``
according to the pruning rule specified in :meth:`compute_mask`.
Args:
t (torch.Tensor): tensor to prune (of same dimensions as
``default_mask``).
default_mask (torch.Tensor, optional): mask from previous pruning
iteration, if any. To be considered when determining what
portion of the tensor that pruning should act on. If None,
default to a mask of ones.
Returns:
pruned version of tensor ``t``.
"""
if default_mask is None:
default_mask = torch.ones_like(t)
return t * self.compute_mask(t, default_mask=default_mask)
def remove(self, module):
r"""Removes the pruning reparameterization from a module. The pruned
parameter named ``name`` remains permanently pruned, and the parameter
named ``name+'_orig'`` is removed from the parameter list. Similarly,
the buffer named ``name+'_mask'`` is removed from the buffers.
Note:
Pruning itself is NOT undone or reversed!
"""
# before removing pruning from a tensor, it has to have been applied
assert (
self._tensor_name is not None
), "Module {} has to be pruned\
before pruning can be removed".format(
module
) # this gets set in apply()
# to update module[name] to latest trained weights
weight = self.apply_mask(module) # masked weights
# delete and reset
delattr(module, self._tensor_name)
orig = module._parameters[self._tensor_name + "_orig"]
orig.data = weight.data
del module._parameters[self._tensor_name + "_orig"]
del module._buffers[self._tensor_name + "_mask"]
module.register_parameter(self._tensor_name, orig)
class PruningContainer(BasePruningMethod):
"""Container holding a sequence of pruning methods for iterative pruning.
Keeps track of the order in which pruning methods are applied and handles
combining successive pruning calls.
Accepts as argument an instance of a BasePruningMethod or an iterable of
them.
"""
def __init__(self, *args):
self._pruning_methods = tuple()
if not isinstance(args, Iterable): # only 1 item
self._tensor_name = args._tensor_name
self.add_pruning_method(args)
elif len(args) == 1: # only 1 item in a tuple
self._tensor_name = args[0]._tensor_name
self.add_pruning_method(args[0])
else: # manual construction from list or other iterable (or no args)
for method in args:
self.add_pruning_method(method)
def add_pruning_method(self, method):
r"""Adds a child pruning ``method`` to the container.
Args:
method (subclass of BasePruningMethod): child pruning method
to be added to the container.
"""
# check that we're adding a pruning method to the container
if not isinstance(method, BasePruningMethod) and method is not None:
raise TypeError(
"{} is not a BasePruningMethod subclass".format(type(method))
)
elif self._tensor_name != method._tensor_name:
raise ValueError(
"Can only add pruning methods acting on "
"the parameter named '{}' to PruningContainer {}.".format(
self._tensor_name, self
)
+ " Found '{}'".format(method._tensor_name)
)
# if all checks passed, add to _pruning_methods tuple
self._pruning_methods += (method,)
def __len__(self):
return len(self._pruning_methods)
def __iter__(self):
return iter(self._pruning_methods)
def __getitem__(self, idx):
return self._pruning_methods[idx]
def compute_mask(self, t, default_mask):
r"""Applies the latest ``method`` by computing the new partial masks
and returning its combination with the ``default_mask``.
The new partial mask should be computed on the entries or channels
that were not zeroed out by the ``default_mask``.
Which portions of the tensor ``t`` the new mask will be calculated from
depends on the ``PRUNING_TYPE`` (handled by the type handler):
* for 'unstructured', the mask will be computed from the raveled
list of nonmasked entries;
* for 'structured', the mask will be computed from the nonmasked
channels in the tensor;
* for 'global', the mask will be computed across all entries.
Args:
t (torch.Tensor): tensor representing the parameter to prune
(of same dimensions as ``default_mask``).
default_mask (torch.Tensor): mask from previous pruning iteration.
Returns:
mask (torch.Tensor): new mask that combines the effects
of the ``default_mask`` and the new mask from the current
pruning ``method`` (of same dimensions as ``default_mask`` and
``t``).
"""
def _combine_masks(method, t, mask):
r"""
Args:
method (a BasePruningMethod subclass): pruning method
currently being applied.
t (torch.Tensor): tensor representing the parameter to prune
(of same dimensions as mask).
mask (torch.Tensor): mask from previous pruning iteration
Returns:
new_mask (torch.Tensor): new mask that combines the effects
of the old mask and the new mask from the current
pruning method (of same dimensions as mask and t).
"""
new_mask = mask # start off from existing mask
new_mask = new_mask.to(dtype=t.dtype)
# compute a slice of t onto which the new pruning method will operate
if method.PRUNING_TYPE == "unstructured":
# prune entries of t where the mask is 1
slc = mask == 1
# for struct pruning, exclude channels that have already been
# entirely pruned
elif method.PRUNING_TYPE == "structured":
if not hasattr(method, "dim"):
raise AttributeError(
"Pruning methods of PRUNING_TYPE "
'"structured" need to have the attribute `dim` defined.'
)
# find the channels to keep by removing the ones that have been
# zeroed out already (i.e. where sum(entries) == 0)
n_dims = t.dim() # "is this a 2D tensor? 3D? ..."
dim = method.dim
# convert negative indexing
if dim < 0:
dim = n_dims + dim
# if dim is still negative after subtracting it from n_dims
if dim < 0:
raise IndexError(
"Index is out of bounds for tensor with dimensions {}".format(
n_dims
)
)
# find channels along dim = dim that aren't already tots 0ed out
keep_channel = mask.sum(
dim=[d for d in range(n_dims) if d != dim]) != 0
# create slice to identify what to prune
slc = [slice(None)] * n_dims
slc[dim] = keep_channel
elif method.PRUNING_TYPE == "global":
n_dims = len(t.shape) # "is this a 2D tensor? 3D? ..."
slc = [slice(None)] * n_dims
else:
raise ValueError(
"Unrecognized PRUNING_TYPE {}".format(method.PRUNING_TYPE)
)
# compute the new mask on the unpruned slice of the tensor t
partial_mask = method.compute_mask(t[slc], default_mask=mask[slc])
new_mask[slc] = partial_mask.to(dtype=new_mask.dtype)
return new_mask
method = self._pruning_methods[-1]
mask = _combine_masks(method, t, default_mask)
return mask
class Identity(BasePruningMethod):
r"""Utility pruning method that does not prune any units but generates the
pruning parametrization with a mask of ones.
"""
PRUNING_TYPE = "unstructured"
def compute_mask(self, t, default_mask):
mask = default_mask
return mask
@classmethod
def apply(cls, module, name):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
"""
return super(Identity, cls).apply(module, name)
class RandomUnstructured(BasePruningMethod):
r"""Prune (currently unpruned) units in a tensor at random.
Args:
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
"""
PRUNING_TYPE = "unstructured"
def __init__(self, amount):
# Check range of validity of pruning amount
_validate_pruning_amount_init(amount)
self.amount = amount
def compute_mask(self, t, default_mask):
# Check that the amount of units to prune is not > than the number of
# parameters in t
tensor_size = t.nelement()
# Compute number of units to prune: amount if int,
# else amount * tensor_size
nparams_toprune = _compute_nparams_toprune(self.amount, tensor_size)
# This should raise an error if the number of units to prune is larger
# than the number of units in the tensor
_validate_pruning_amount(nparams_toprune, tensor_size)
mask = default_mask.clone()
if nparams_toprune != 0: # k=0 not supported by torch.kthvalue
prob = torch.rand_like(t)
topk = torch.topk(prob.view(-1), k=nparams_toprune)
mask.view(-1)[topk.indices] = 0
return mask
@classmethod
def apply(cls, module, name, amount):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
"""
return super(RandomUnstructured, cls).apply(module, name, amount=amount)
class L1Unstructured(BasePruningMethod):
r"""Prune (currently unpruned) units in a tensor by zeroing out the ones
with the lowest L1-norm.
Args:
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
"""
PRUNING_TYPE = "unstructured"
def __init__(self, amount):
# Check range of validity of pruning amount
_validate_pruning_amount_init(amount)
self.amount = amount
def compute_mask(self, t, default_mask):
# Check that the amount of units to prune is not > than the number of
# parameters in t
tensor_size = t.nelement()
# Compute number of units to prune: amount if int,
# else amount * tensor_size
nparams_toprune = _compute_nparams_toprune(self.amount, tensor_size)
# This should raise an error if the number of units to prune is larger
# than the number of units in the tensor
_validate_pruning_amount(nparams_toprune, tensor_size)
mask = default_mask.clone()
if nparams_toprune != 0: # k=0 not supported by torch.kthvalue
# largest=True --> top k; largest=False --> bottom k
# Prune the smallest k
topk = torch.topk(torch.abs(t).view(-1),
k=nparams_toprune, largest=False)
# topk will have .indices and .values
mask.view(-1)[topk.indices] = 0
return mask
@classmethod
def apply(cls, module, name, amount):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
"""
return super(L1Unstructured, cls).apply(module, name, amount=amount)
class RandomStructured(BasePruningMethod):
r"""Prune entire (currently unpruned) channels in a tensor at random.
Args:
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
dim (int, optional): index of the dim along which we define
channels to prune. Default: -1.
"""
PRUNING_TYPE = "structured"
def __init__(self, amount, dim=-1):
# Check range of validity of amount
_validate_pruning_amount_init(amount)
self.amount = amount
self.dim = dim
def compute_mask(self, t, default_mask):
r"""Computes and returns a mask for the input tensor ``t``.
Starting from a base ``default_mask`` (which should be a mask of ones
if the tensor has not been pruned yet), generate a random mask to
apply on top of the ``default_mask`` by randomly zeroing out channels
along the specified dim of the tensor.
Args:
t (torch.Tensor): tensor representing the parameter to prune
default_mask (torch.Tensor): Base mask from previous pruning
iterations, that need to be respected after the new mask is
applied. Same dims as ``t``.
Returns:
mask (torch.Tensor): mask to apply to ``t``, of same dims as ``t``
Raises:
IndexError: if ``self.dim >= len(t.shape)``
"""
# Check that tensor has structure (i.e. more than 1 dimension) such
# that the concept of "channels" makes sense
_validate_structured_pruning(t)
# Check that self.dim is a valid dim to index t, else raise IndexError
_validate_pruning_dim(t, self.dim)
# Check that the amount of channels to prune is not > than the number of
# channels in t along the dim to prune
tensor_size = t.shape[self.dim]
# Compute number of units to prune: amount if int,
# else amount * tensor_size
nparams_toprune = _compute_nparams_toprune(self.amount, tensor_size)
nparams_tokeep = tensor_size - nparams_toprune
# This should raise an error if the number of units to prune is larger
# than the number of units in the tensor
_validate_pruning_amount(nparams_toprune, tensor_size)
# Compute binary mask by initializing it to all 0s and then filling in
# 1s wherever topk.indices indicates, along self.dim.
# mask has the same shape as tensor t
def make_mask(t, dim, nchannels, nchannels_toprune):
# generate a random number in [0, 1] to associate to each channel
prob = torch.rand(nchannels)
# generate mask for each channel by 0ing out the channels that
# got assigned the k = nchannels_toprune lowest values in prob
threshold = torch.kthvalue(prob, k=nchannels_toprune).values
channel_mask = prob > threshold
mask = torch.zeros_like(t)
slc = [slice(None)] * len(t.shape)
slc[dim] = channel_mask
mask[slc] = 1
return mask
if nparams_toprune == 0: # k=0 not supported by torch.kthvalue
mask = default_mask
else:
# apply the new structured mask on top of prior (potentially
# unstructured) mask
mask = make_mask(t, self.dim, tensor_size, nparams_toprune)
mask *= default_mask.to(dtype=mask.dtype)
return mask
@classmethod
def apply(cls, module, name, amount, dim=-1):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
dim (int, optional): index of the dim along which we define
channels to prune. Default: -1.
"""
return super(RandomStructured, cls).apply(module, name, amount=amount, dim=dim)
class LnStructured(BasePruningMethod):
r"""Prune entire (currently unpruned) channels in a tensor based on their
Ln-norm.
Args:
amount (int or float): quantity of channels to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
n (int, float, inf, -inf, 'fro', 'nuc'): See documentation of valid
entries for argument ``p`` in :func:`torch.norm`.
dim (int, optional): index of the dim along which we define
channels to prune. Default: -1.
"""
PRUNING_TYPE = "structured"
def __init__(self, amount, n, dim=-1):
# Check range of validity of amount
_validate_pruning_amount_init(amount)
self.amount = amount
self.n = n
self.dim = dim
def compute_mask(self, t, default_mask):
r"""Computes and returns a mask for the input tensor ``t``.
Starting from a base ``default_mask`` (which should be a mask of ones
if the tensor has not been pruned yet), generate a mask to apply on
top of the ``default_mask`` by zeroing out the channels along the
specified dim with the lowest Ln-norm.
Args:
t (torch.Tensor): tensor representing the parameter to prune
default_mask (torch.Tensor): Base mask from previous pruning
iterations, that need to be respected after the new mask is
applied. Same dims as ``t``.
Returns:
mask (torch.Tensor): mask to apply to ``t``, of same dims as ``t``
Raises:
IndexError: if ``self.dim >= len(t.shape)``
"""
# Check that tensor has structure (i.e. more than 1 dimension) such
# that the concept of "channels" makes sense
_validate_structured_pruning(t)
# Check that self.dim is a valid dim to index t, else raise IndexError
_validate_pruning_dim(t, self.dim)
# Check that the amount of channels to prune is not > than the number of
# channels in t along the dim to prune
tensor_size = t.shape[self.dim]
# Compute number of units to prune: amount if int,
# else amount * tensor_size
nparams_toprune = _compute_nparams_toprune(self.amount, tensor_size)
nparams_tokeep = tensor_size - nparams_toprune
# This should raise an error if the number of units to prune is larger
# than the number of units in the tensor
_validate_pruning_amount(nparams_toprune, tensor_size)
# Structured pruning prunes entire channels so we need to know the
# L_n norm along each channel to then find the topk based on this
# metric
norm = _compute_norm(t, self.n, self.dim)
# largest=True --> top k; largest=False --> bottom k
# Keep the largest k channels along dim=self.dim
topk = torch.topk(norm, k=nparams_tokeep, largest=True,)
# topk will have .indices and .values
# Compute binary mask by initializing it to all 0s and then filling in
# 1s wherever topk.indices indicates, along self.dim.
# mask has the same shape as tensor t
def make_mask(t, dim, indices):
# init mask to 0
mask = torch.zeros_like(t)
# e.g.: slc = [None, None, None], if len(t.shape) = 3
slc = [slice(None)] * len(t.shape)
# replace a None at position=dim with indices
# e.g.: slc = [None, None, [0, 2, 3]] if dim=2 & indices=[0,2,3]
slc[dim] = indices
# use slc to slice mask and replace all its entries with 1s
# e.g.: mask[:, :, [0, 2, 3]] = 1
mask[slc] = 1
return mask
if nparams_toprune == 0: # k=0 not supported by torch.kthvalue
mask = default_mask
else:
mask = make_mask(t, self.dim, topk.indices)
mask *= default_mask.to(dtype=mask.dtype)
return mask
@classmethod
def apply(cls, module, name, amount, n, dim):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
n (int, float, inf, -inf, 'fro', 'nuc'): See documentation of valid
entries for argument ``p`` in :func:`torch.norm`.
dim (int): index of the dim along which we define channels to
prune.
"""
return super(LnStructured, cls).apply(module, name, amount=amount, n=n, dim=dim)
class CustomFromMask(BasePruningMethod):
PRUNING_TYPE = "global"
def __init__(self, mask):
self.mask = mask
def compute_mask(self, t, default_mask):
assert default_mask.shape == self.mask.shape
mask = default_mask * self.mask.to(dtype=default_mask.dtype)
return mask
@classmethod
def apply(cls, module, name, mask):
r"""Adds the forward pre-hook that enables pruning on the fly and
the reparametrization of a tensor in terms of the original tensor
and the pruning mask.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
"""
return super(CustomFromMask, cls).apply(module, name, mask)
def identity(module, name):
r"""Applies pruning reparametrization to the tensor corresponding to the
parameter called ``name`` in ``module`` without actually pruning any
units. Modifies module in place (and also return the modified module)
by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter ``name`` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Note:
The mask is a tensor of ones.
Args:
module (nn.Module): module containing the tensor to prune.
name (str): parameter name within ``module`` on which pruning
will act.
Returns:
module (nn.Module): modified (i.e. pruned) version of the input module
Examples:
>>> m = prune.identity(nn.Linear(2, 3), 'bias')
>>> print(m.bias_mask)
tensor([1., 1., 1.])
"""
Identity.apply(module, name)
return module
def random_unstructured(module, name, amount):
r"""Prunes tensor corresponding to parameter called ``name`` in ``module``
by removing the specified ``amount`` of (currently unpruned) units
selected at random.
Modifies module in place (and also return the modified module) by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter `name` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
Returns:
module (nn.Module): modified (i.e. pruned) version of the input module
Examples:
>>> m = prune.random_unstructured(nn.Linear(2, 3), 'weight', amount=1)
>>> torch.sum(m.weight_mask == 0)
tensor(1)
"""
RandomUnstructured.apply(module, name, amount)
return module
def l1_unstructured(module, name, amount):
r"""Prunes tensor corresponding to parameter called ``name`` in ``module``
by removing the specified `amount` of (currently unpruned) units with the
lowest L1-norm.
Modifies module in place (and also return the modified module)
by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter ``name`` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
Returns:
module (nn.Module): modified (i.e. pruned) version of the input module
Examples:
>>> m = prune.l1_unstructured(nn.Linear(2, 3), 'weight', amount=0.2)
>>> m.state_dict().keys()
odict_keys(['bias', 'weight_orig', 'weight_mask'])
"""
L1Unstructured.apply(module, name, amount)
return module
def random_structured(module, name, amount, dim):
r"""Prunes tensor corresponding to parameter called ``name`` in ``module``
by removing the specified ``amount`` of (currently unpruned) channels
along the specified ``dim`` selected at random.
Modifies module in place (and also return the modified module)
by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter ``name`` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
dim (int): index of the dim along which we define channels to prune.
Returns:
module (nn.Module): modified (i.e. pruned) version of the input module
Examples:
>>> m = prune.random_structured(
nn.Linear(5, 3), 'weight', amount=3, dim=1
)
>>> columns_pruned = int(sum(torch.sum(m.weight, dim=0) == 0))
>>> print(columns_pruned)
3
"""
RandomStructured.apply(module, name, amount, dim)
return module
def ln_structured(module, name, amount, n, dim):
r"""Prunes tensor corresponding to parameter called ``name`` in ``module``
by removing the specified ``amount`` of (currently unpruned) channels
along the specified ``dim`` with the lowest L``n``-norm.
Modifies module in place (and also return the modified module)
by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter ``name`` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
amount (int or float): quantity of parameters to prune.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
n (int, float, inf, -inf, 'fro', 'nuc'): See documentation of valid
entries for argument ``p`` in :func:`torch.norm`.
dim (int): index of the dim along which we define channels to prune.
Returns:
module (nn.Module): modified (i.e. pruned) version of the input module
Examples:
>>> m = prune.ln_structured(
nn.Conv2d(5, 3, 2), 'weight', amount=0.3, dim=1, n=float('-inf')
)
"""
LnStructured.apply(module, name, amount, n, dim)
return module
def global_unstructured(parameters, pruning_method, **kwargs):
r"""
Globally prunes tensors corresponding to all parameters in ``parameters``
by applying the specified ``pruning_method``.
Modifies modules in place by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter ``name`` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Args:
parameters (Iterable of (module, name) tuples): parameters of
the model to prune in a global fashion, i.e. by aggregating all
weights prior to deciding which ones to prune. module must be of
type :class:`nn.Module`, and name must be a string.
pruning_method (function): a valid pruning function from this module,
or a custom one implemented by the user that satisfies the
implementation guidelines and has ``PRUNING_TYPE='unstructured'``.
kwargs: other keyword arguments such as:
amount (int or float): quantity of parameters to prune across the
specified parameters.
If ``float``, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If ``int``, it represents the
absolute number of parameters to prune.
Raises:
TypeError: if ``PRUNING_TYPE != 'unstructured'``
Note:
Since global structured pruning doesn't make much sense unless the
norm is normalized by the size of the parameter, we now limit the
scope of global pruning to unstructured methods.
Examples:
>>> net = nn.Sequential(OrderedDict([
('first', nn.Linear(10, 4)),
('second', nn.Linear(4, 1)),
]))
>>> parameters_to_prune = (
(net.first, 'weight'),
(net.second, 'weight'),
)
>>> prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=10,
)
>>> print(sum(torch.nn.utils.parameters_to_vector(net.buffers()) == 0))
tensor(10, dtype=torch.uint8)
"""
# ensure parameters is a list or generator of tuples
assert isinstance(parameters, Iterable)
# flatten parameter values to consider them all at once in global pruning
t = torch.nn.utils.parameters_to_vector([getattr(*p) for p in parameters])
# similarly, flatten the masks (if they exist), or use a flattened vector
# of 1s of the same dimensions as t
default_mask = torch.nn.utils.parameters_to_vector(
[
getattr(module, name + "_mask",
torch.ones_like(getattr(module, name)))
for (module, name) in parameters
]
)
# use the canonical pruning methods to compute the new mask, even if the
# parameter is now a flattened out version of `parameters`
container = PruningContainer()
container._tensor_name = "temp" # to make it match that of `method`
method = pruning_method(**kwargs)
method._tensor_name = "temp" # to make it match that of `container`
if method.PRUNING_TYPE != "unstructured":
raise TypeError(
'Only "unstructured" PRUNING_TYPE supported for '
"the `pruning_method`. Found method {} of type {}".format(
pruning_method, method.PRUNING_TYPE
)
)
container.add_pruning_method(method)
# use the `compute_mask` method from `PruningContainer` to combine the
# mask computed by the new method with the pre-existing mask
final_mask = container.compute_mask(t, default_mask)
# Pointer for slicing the mask to match the shape of each parameter
pointer = 0
for module, name in parameters:
param = getattr(module, name)
# The length of the parameter
num_param = param.numel()
# Slice the mask, reshape it
param_mask = final_mask[pointer: pointer + num_param].view_as(param)
# Assign the correct pre-computed mask to each parameter and add it
# to the forward_pre_hooks like any other pruning method
custom_from_mask(module, name, param_mask)
# Increment the pointer to continue slicing the final_mask
pointer += num_param
def custom_from_mask(module, name, mask):
r"""Prunes tensor corresponding to parameter called ``name`` in ``module``
by applying the pre-computed mask in ``mask``.
Modifies module in place (and also return the modified module)
by:
1) adding a named buffer called ``name+'_mask'`` corresponding to the
binary mask applied to the parameter ``name`` by the pruning method.
2) replacing the parameter ``name`` by its pruned version, while the
original (unpruned) parameter is stored in a new parameter named
``name+'_orig'``.
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
mask (Tensor): binary mask to be applied to the parameter.
Returns:
module (nn.Module): modified (i.e. pruned) version of the input module
Examples:
>>> m = prune.custom_from_mask(
nn.Linear(5, 3), name='bias', mask=torch.Tensor([0, 1, 0])
)
>>> print(m.bias_mask)
tensor([0., 1., 0.])
"""
CustomFromMask.apply(module, name, mask)
return module
def remove(module, name):
r"""Removes the pruning reparameterization from a module and the
pruning method from the forward hook. The pruned
parameter named ``name`` remains permanently pruned, and the parameter
named ``name+'_orig'`` is removed from the parameter list. Similarly,
the buffer named ``name+'_mask'`` is removed from the buffers.
Note:
Pruning itself is NOT undone or reversed!
Args:
module (nn.Module): module containing the tensor to prune
name (str): parameter name within ``module`` on which pruning
will act.
Examples:
>>> m = random_pruning(nn.Linear(5, 7), name='weight', amount=0.2)
>>> m = remove_pruning(m, name='weight')
"""
for k, hook in module._forward_pre_hooks.items():
if isinstance(hook, BasePruningMethod) and hook._tensor_name == name:
hook.remove(module)
del module._forward_pre_hooks[k]
return module
raise ValueError(
"Parameter '{}' of module {} has to be pruned "
"before pruning can be removed".format(name, module)
)
def is_pruned(module):
r"""Check whether ``module`` is pruned by looking for
``forward_pre_hooks`` in its modules that inherit from the
:class:`BasePruningMethod`.
Args:
module (nn.Module): object that is either pruned or unpruned
Returns:
binary answer to whether ``module`` is pruned.
Examples:
>>> m = nn.Linear(5, 7)
>>> print(prune.is_pruned(m))
False
>>> prune.random_pruning(m, name='weight', amount=0.2)
>>> print(prune.is_pruned(m))
True
"""
for _, submodule in module.named_modules():
for _, hook in submodule._forward_pre_hooks.items():
if isinstance(hook, BasePruningMethod):
return True
return False
def _validate_pruning_amount_init(amount):
r"""Validation helper to check the range of amount at init.
Args:
amount (int or float): quantity of parameters to prune.
If float, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If int, it represents the
absolute number of parameters to prune.
Raises:
ValueError: if amount is a float not in [0, 1], or if it's a negative
integer.
TypeError: if amount is neither a float nor an integer.
Note:
This does not take into account the number of parameters in the
tensor to be pruned, which is known only at prune.
"""
if not isinstance(amount, numbers.Real):
raise TypeError(
"Invalid type for amount: {}. Must be int or float." "".format(
amount)
)
if (isinstance(amount, numbers.Integral) and amount < 0) or (
not isinstance(amount, numbers.Integral) # so it's a float
and (amount > 1.0 or amount < 0.0)
):
raise ValueError(
"amount={} should either be a float in the "
"range [0, 1] or a non-negative integer"
"".format(amount)
)
def _validate_pruning_amount(amount, tensor_size):
r"""Validation helper to check that the amount of parameters to prune
is meaningful wrt to the size of the data (`tensor_size`).
Args:
amount (int or float): quantity of parameters to prune.
If float, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If int, it represents the
absolute number of parameters to prune.
tensor_size (int): absolute number of parameters in the tensor
to prune.
"""
# TODO: consider removing this check and allowing users to specify
# a number of units to prune that is greater than the number of units
# left to prune. In this case, the tensor will just be fully pruned.
if isinstance(amount, numbers.Integral) and amount > tensor_size:
raise ValueError(
"amount={} should be smaller than the number of "
"parameters to prune={}".format(amount, tensor_size)
)
def _validate_structured_pruning(t):
r"""Validation helper to check that the tensor to be pruned is multi-
dimensional, such that the concept of "channels" is well-defined.
Args:
t (torch.Tensor): tensor representing the parameter to prune
Raises:
ValueError: if the tensor `t` is not at least 2D.
"""
shape = t.shape
if len(shape) <= 1:
raise ValueError(
"Structured pruning can only be applied to "
"multidimensional tensors. Found tensor of shape "
"{} with {} dims".format(shape, len(shape))
)
def _compute_nparams_toprune(amount, tensor_size):
r"""Since amount can be expressed either in absolute value or as a
percentage of the number of units/channels in a tensor, this utility
function converts the percentage to absolute value to standardize
the handling of pruning.
Args:
amount (int or float): quantity of parameters to prune.
If float, should be between 0.0 and 1.0 and represent the
fraction of parameters to prune. If int, it represents the
absolute number of parameters to prune.
tensor_size (int): absolute number of parameters in the tensor
to prune.
Returns:
int: the number of units to prune in the tensor
"""
# incorrect type already checked in _validate_pruning_amount_init
if isinstance(amount, numbers.Integral):
return amount
else:
return int(round(amount * tensor_size)) # int needed for Python 2
def _validate_pruning_dim(t, dim):
r"""
Args:
t (torch.Tensor): tensor representing the parameter to prune
dim (int): index of the dim along which we define channels to prune
"""
if dim >= t.dim():
raise IndexError(
"Invalid index {} for tensor of size {}".format(dim, t.shape))
def _compute_norm(t, n, dim):
r"""Compute the L_n-norm across all entries in tensor `t` along all dimension
except for the one identified by dim.
Example: if `t` is of shape, say, 3x2x4 and dim=2 (the last dim),
then norm will have Size [4], and each entry will represent the
`L_n`-norm computed using the 3x2=6 entries for each of the 4 channels.
Args:
t (torch.Tensor): tensor representing the parameter to prune
n (int, float, inf, -inf, 'fro', 'nuc'): See documentation of valid
entries for argument p in torch.norm
dim (int): dim identifying the channels to prune
Returns:
norm (torch.Tensor): L_n norm computed across all dimensions except
for `dim`. By construction, `norm.shape = t.shape[-1]`.
"""
# dims = all axes, except for the one identified by `dim`
dims = list(range(t.dim()))
# convert negative indexing
if dim < 0:
dim = dims[dim]
dims.remove(dim)
norm = torch.norm(t, p=n, dim=dims)
return norm
|
decodable_information_bottleneck-main
|
dib/utils/pruning.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
|
decodable_information_bottleneck-main
|
dib/utils/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import functools
import random
import numpy as np
import torch
from .helpers import channels_to_last_dim, indep_shuffle_, prod, ratio_to_int
__all__ = ["RandomMasker", "GetRandomIndcs"]
### INDICES SELECTORS ###
class GetRandomIndcs:
"""
Return random subset of indices.
Parameters
----------
min_n_indcs : float or int, optional
Minimum number of indices. If smaller than 1, represents a percentage of
points.
max_n_indcs : float or int, optional
Maximum number of indices. If smaller than 1, represents a percentage of
points.
is_batch_share : bool, optional
Whether to use use the same indices for all elements in the batch.
range_indcs : tuple, optional
Range tuple (max, min) for the indices.
"""
def __init__(
self, min_n_indcs=0.1, max_n_indcs=0.5, is_batch_share=False, range_indcs=None
):
self.min_n_indcs = min_n_indcs
self.max_n_indcs = max_n_indcs
self.is_batch_share = is_batch_share
self.range_indcs = range_indcs
def __call__(self, batch_size, n_possible_points):
if self.range_indcs is not None:
n_possible_points = self.range_indcs[1] - self.range_indcs[0]
min_n_indcs = ratio_to_int(self.min_n_indcs, n_possible_points)
max_n_indcs = ratio_to_int(self.max_n_indcs, n_possible_points)
# make sure select at least 1
n_indcs = random.randint(max(1, min_n_indcs), max(1, max_n_indcs))
if self.is_batch_share:
indcs = torch.randperm(n_possible_points)[:n_indcs]
indcs = indcs.unsqueeze(0).expand(batch_size, n_indcs)
else:
indcs = (
np.arange(n_possible_points)
.reshape(1, n_possible_points)
.repeat(batch_size, axis=0)
)
indep_shuffle_(indcs, -1)
indcs = torch.from_numpy(indcs[:, :n_indcs])
if self.range_indcs is not None:
# adding is teh same as shifting
indcs += self.range_indcs[0]
return indcs
### GRID AND MASKING ###
class RandomMasker(GetRandomIndcs):
"""
Return random subset mask.
Parameters
----------
min_nnz : float or int, optional
Minimum number of non zero values. If smaller than 1, represents a
percentage of points.
max_nnz : float or int, optional
Maximum number of non zero values. If smaller than 1, represents a
percentage of points.
is_batch_share : bool, optional
Whether to use use the same indices for all elements in the batch.
"""
def __init__(self, min_nnz=0.01, max_nnz=2 / 9, is_batch_share=False):
super().__init__(
min_n_indcs=min_nnz, max_n_indcs=max_nnz, is_batch_share=is_batch_share
)
def __call__(self, batch_size, mask_shape, **kwargs):
n_possible_points = prod(mask_shape)
nnz_indcs = super().__call__(batch_size, n_possible_points, **kwargs)
if self.is_batch_share:
# share memory
mask = torch.zeros(n_possible_points).bool()
mask = mask.unsqueeze(0).expand(batch_size, n_possible_points)
else:
mask = torch.zeros((batch_size, n_possible_points)).bool()
mask.scatter_(1, nnz_indcs, 1)
mask = mask.view(batch_size, *mask_shape).contiguous()
return mask
|
decodable_information_bottleneck-main
|
dib/utils/datasplit.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import math
import torch
from torch import nn
from torch.nn.init import _calculate_correct_fan
__all__ = ["weights_init"]
logger = logging.getLogger(__name__)
def get_min_shape(t1, t2):
"""return component wise minimum shape."""
return [min(el1, el2) for el1, el2 in zip(t1.shape, t2.shape)]
def set_normlayer_like_(norm, other):
"""Set the param of `norm` using `other` (normalization layers).
If not the same size, set the largest subset of the weights."""
assert isinstance(norm, type(other))
if isinstance(norm, nn.Identity):
return
norm_weight = norm.weight.detach()
other_weight = other.weight.detach()
rep_shape = get_min_shape(norm_weight, other_weight)
norm_weight[: rep_shape[0]] = other_weight[: rep_shape[0]]
norm.weight = nn.Parameter(norm_weight)
norm_bias = norm.bias.detach()
other_bias = other.bias.detach()
rep_shape = get_min_shape(norm_bias, other_bias)
norm_bias[: rep_shape[0]] = other_bias[: rep_shape[0]]
norm.bias = nn.Parameter(norm_bias)
def set_linear_like_(linear, other):
"""Set the parameters of `linear` using `other` (linear layers). If not the same size, set the largest subset of the weights."""
assert isinstance(linear, nn.Linear) and isinstance(other, nn.Linear)
linear_weight = linear.weight.detach()
other_weight = other.weight.detach()
rep_shape = get_min_shape(linear_weight, other_weight)
linear_weight[: rep_shape[0], : rep_shape[1]] = other_weight[
: rep_shape[0], : rep_shape[1]
]
linear.weight = nn.Parameter(linear_weight)
linear_bias = linear.bias.detach()
other_bias = other.bias.detach()
rep_shape = get_min_shape(linear_bias, other_bias)
linear_bias[: rep_shape[0]] = other_bias[: rep_shape[0]]
linear.bias = nn.Parameter(linear_bias)
def weights_init(module, **kwargs):
"""Initialize a module and all its descendents.
Parameters
----------
module : nn.Module
module to initialize.
"""
# lop over direct children (not grand children)
for m in module.children():
# all standard layers
if isinstance(m, torch.nn.modules.conv._ConvNd):
# used in https://github.com/brain-research/realistic-ssl-evaluation/
nn.init.kaiming_normal_(m.weight, mode="fan_out", **kwargs)
elif isinstance(m, nn.Linear):
linear_init(m, **kwargs)
elif isinstance(m, nn.BatchNorm2d):
try:
m.weight.data.fill_(1)
m.bias.data.zero_()
except AttributeError: # affine = False
pass
# if has a specific reset
elif hasattr(m, "reset_parameters"):
m.reset_parameters()
#! don't go in grand children because you might have specifc weights you don't want to reset
# else go in your grand children
else:
weights_init(m, **kwargs)
def get_activation_name(activation):
"""Given a string or a `torch.nn.modules.activation` return the name of the activation."""
if isinstance(activation, str):
return activation
mapper = {
nn.LeakyReLU: "leaky_relu",
nn.ReLU: "relu",
nn.SELU: "selu",
nn.Tanh: "tanh",
nn.Sigmoid: "sigmoid",
nn.Softmax: "sigmoid",
}
for k, v in mapper.items():
if isinstance(activation, k):
return v
raise ValueError("Unkown given activation type : {}".format(activation))
def get_gain(activation):
"""Given an object of `torch.nn.modules.activation` or an activation name
return the correct gain."""
if activation is None:
return 1
activation_name = get_activation_name(activation)
param = None if activation_name != "leaky_relu" else activation.negative_slope
gain = nn.init.calculate_gain(activation_name, param)
return gain
def terrible_linear_init(module, **kwargs):
x = module.weight
if module.bias is not None:
nn.init.uniform_(module.bias.data, a=-100, b=100)
return nn.init.uniform_(x, a=-100, b=100)
def linear_init(module, activation="relu"):
"""Initialize a linear layer.
Parameters
----------
module : nn.Module
module to initialize.
activation : `torch.nn.modules.activation` or str, optional
Activation that will be used on the `module`.
"""
x = module.weight
if module.bias is not None:
module.bias.data.zero_()
try:
activation_name = get_activation_name(activation)
except ValueError:
activation_name = None
if activation_name == "leaky_relu":
a = 0 if isinstance(activation, str) else activation.negative_slope
return nn.init.kaiming_uniform_(x, a=a, nonlinearity="leaky_relu")
elif activation_name == "relu":
return nn.init.kaiming_uniform_(x, nonlinearity="relu")
elif activation_name == "selu":
fan_in = _calculate_correct_fan(x, "fan_in")
return torch.nn.init.normal_(x, std=1 / math.sqrt(fan_in))
elif activation_name in ["sigmoid", "tanh"]:
return nn.init.xavier_uniform_(x, gain=get_gain(activation))
else:
if activation is not None:
logger.info(
f"Uknown activation={activation}, using xavier uniform init")
return nn.init.xavier_uniform_(x)
def init_param_(param, activation=None, is_positive=False, bound=0.05, shift=0):
"""Initialize inplace some parameters of the model that are not part of a
children module.
Parameters
----------
param : nn.Parameters:
Parameters to initialize.
activation : torch.nn.modules.activation or str, optional)
Activation that will be used on the `param`.
is_positive : bool, optional
Whether to initilize only with positive values.
bound : float, optional
Maximum absolute value of the initealized values. By default `0.05` which
is keras default uniform bound.
shift : int, optional
Shift the initialisation by a certain value (same as adding a value after init).
"""
gain = get_gain(activation)
if is_positive:
nn.init.uniform_(param, 1e-5 + shift, bound * gain + shift)
return
nn.init.uniform_(param, -bound * gain + shift, bound * gain + shift)
|
decodable_information_bottleneck-main
|
dib/utils/initialization.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import math
import torch
from torch.distributions import Categorical, Independent, Normal
def MultivariateNormalDiag(loc, scale_diag):
"""Multi variate Gaussian with a diagonal covariance function."""
if loc.dim() < 1:
raise ValueError("loc must be at least one-dimensional.")
return Independent(Normal(loc, scale_diag), 1)
class NoDistribution:
def __init__(self, x):
self.x = x
def rsample(self):
return self.x
def straight_through(soft_samples, f):
"""
Take soft_samples and transform them with f(straight_through)
while keeping it differentiable.
"""
detached = soft_samples.detach()
# removes a detached version of the soft X and adds the real X
# to emulate the fact that we add some non differentiable noise which just
# hapens to make the variable as you want. I.e the total is still differentiable
detached_res = f(detached)
detached_diff = detached_res - detached
res = detached_diff + soft_samples
return res
def softmax_to_onehot(X, dim=1):
"""Moves a vector on the simplex to the closes vertex."""
max_idx = torch.argmax(X, dim, keepdim=True)
one_hot = torch.zeros_like(X)
one_hot.scatter_(dim, max_idx, 1)
return one_hot
def label_distribution(labels, n_classes):
"""Return a categorical distribution of the labels."""
probs = torch.zeros(n_classes, device=labels.device)
label, counts = labels.unique(return_counts=True)
probs[label] = counts.float() / counts.sum()
return Categorical(probs=probs)
def entropy_labels(labels, n_classes, base=math.exp(1)):
"""Computes the entropy of labels."""
probs = label_distribution(labels, n_classes)
return probs.entropy().mean(0) / math.log(base)
def rm_conditioning(p_yCx):
"""Remove the conditioning of a distributions p(Y|X) -> p(Y) by taking a Monte Carlo Expectation
of all besides current index.
Parameters
----------
q_yCx : torch.Tensor or torch.Distributions
Distribution to uncondition. Each batch should be from a sample of conditioning
random variable X. Note that this should already be in pbabilities, not logits.
"""
#! here i'm actually removing the current index so the estimate is slighlty biased,
#! to unbias should giv weight 1/N instead of weight 0 to yourself
p_y = torch.zeros_like(p_yCx)
if isinstance(p_yCx, torch.Tensor):
batch_size = p_yCx.size(0)
for batch_idx in range(batch_size):
p_y[batch_idx] = p_yCx[
list(range(0, batch_idx)) +
list(range(batch_idx + 1, batch_size))
].mean(0)
return p_y
|
decodable_information_bottleneck-main
|
dib/utils/distributions.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import contextlib
import math
import operator
import random
import warnings
from functools import reduce
from itertools import zip_longest
import numpy as np
import skorch
import torch
import torch.nn as nn
import torch.nn.functional as F
from .initialization import weights_init
def to_numpy(X):
"""Generic function to convert array like to numpy."""
if isinstance(X, list):
X = np.array(X)
return skorch.utils.to_numpy(X)
def extract_target(targets, map_target_position):
"""Extract the real target."""
if isinstance(targets, (list, tuple)):
return targets[map_target_position["target"]]
else:
return targets[:, map_target_position["target"]]
class CrossEntropyLossGeneralize(nn.CrossEntropyLoss):
"""Cross entropy loss that forces (anti)-generalization.
Note
----
- we want to find an empirical risk minimizer that maximizes (antigeneralize) or minimizes
(generalize) the test loss. Using a lagrangian relaxation of the problem this can be written
as `min trainLoss + gamma * testLoss`, where the sign of `gamma` determines whether or not to
generalize.
- Each target should contain `(label, is_train)`. Where `is_train` says whether its a trainign
example or a test example. `is_train=1` or `is_train=0`.
- When validating usies cross entropy.
Parameters
----------
gamma : float, optional
Langrangian coefficient of the relaxed problem. If positive, forces generalization, if negative
forces anti generalization. Its scale balances the training and testing loss. If `gamma=0`
becomes standard cross entropy (in which case doesn't need to append `is_train`).
map_target_position : dict
Dictionary that maps the type of target (e.g. "index") to its position in the
target. Needs to have `"constant" corresponding to `"is_train"`.
cap_test_loss : float, optional
Value used to cap the test loss (i.e. don't backprop through it). This is especially useful
when gamma is negative (anti generalization). Indeed, cross entropy is not bounded and thus
the model could end up only focusing on maximizing the test loss to infinity regardless of
train.
kwargs :
Additional arguments to `torch.nn.CrossEntropyLoss`.
"""
def __init__(
self, gamma, map_target_position, reduction="mean", cap_test_loss=10, **kwargs
):
super().__init__(reduction="none", **kwargs)
self.gamma = gamma
self.map_target_position = map_target_position
self.final_reduction = reduction
self.cap_test_loss = cap_test_loss
def forward(self, inp, targets):
out = super().forward(inp, extract_target(targets, self.map_target_position))
if self.gamma == 0 and ("constant" not in self.map_target_position):
pass
elif self.training:
constant = targets[self.map_target_position["constant"]]
is_test = constant == 0
is_train = constant == 1
weights = (is_test.int() * self.gamma) + is_train.int()
# CAPPING : don't backprop if test and larger than cap (but still forward)
is_large_loss = out > self.cap_test_loss
to_cap = is_large_loss & is_test
out[to_cap] = out[to_cap] * 0 + out[to_cap].detach()
out = weights * out
elif len(self.map_target_position) == len(targets):
# when validating : either you have only access to the validation set, in which
# case return all or you have access to train U test
# in which case you want to filter only the training examples
is_train = targets[self.map_target_position["constant"]] == 1
out = out[is_train]
else:
ValueError(
f"Not training but len({self.map_target_position})!={len(targets)}"
)
if self.final_reduction == "mean":
return out.mean()
elif self.final_reduction == "sum":
return out.sum()
else:
raise ValueError(f"Unkown reduction={self.final_reduction}")
class Identity:
def __init__(self, *args, **kwargs):
pass
def __call__(self, x):
return x
def __getitem__(self, x):
return x
def is_sorted(l):
"""Check whether a list is sorted"""
return all(l[i] <= l[i + 1] for i in range(len(l) - 1))
def get_idx_permuter(n_idcs, seed=123):
"""Return permuted indices.
Paramaters
----------
n_idcs : int or array-like of int
Number of indices. If list, it should be a partion of the real number of idcs.
Each partition will be permuted separately.
seed : int, optional
"""
if isinstance(n_idcs, int):
idcs = list(range(n_idcs))
else:
idcs = [list(range(partition)) for partition in n_idcs]
with tmp_seed(seed):
if isinstance(n_idcs, int):
random.shuffle(idcs)
idcs = torch.tensor(idcs)
else:
# shuffle each partition separetly
for partition_idcs in idcs:
random.shuffle(partition_idcs)
idcs = torch.cat([torch.tensor(idcs) for idcs in idcs])
return idcs
# credits : https://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
def Nsize_chunk_iterable(iterable, n, padding=None):
"""Chunk an iterable into N sized ones."""
return zip_longest(*[iter(iterable)] * n, fillvalue=padding)
def Nchunk_iterable(iterable, n, padding=None):
"""Chunk an iterable into `n` of them."""
return Nsize_chunk_iterable(iterable, math.ceil(len(iterable) / n))
def update_dict_copy(d, **updates):
"""Return an updated copy of the dictionary."""
d = d.copy()
d.update(updates)
return d
class BatchNorm1dLast(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self.batch_norm = torch.nn.BatchNorm1d(*args, **kwargs)
def forward(self, x):
# flatten to make for normalizing layer => only 2 dim
x, shape = batch_flatten(x)
x = self.batch_norm(x)
return batch_unflatten(x, shape)
def wrap_batchnorm(Module):
# wrap a module by applying batchnorm1d to input
class BatchNormWrapper(torch.nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
self.batch_norm = BatchNorm1dLast(
num_features=args[0], affine=False)
self.module = Module(*args, **kwargs)
def forward(self, x):
x = self.batch_norm(x)
return self.module(x)
return BatchNormWrapper
def get_activation(activation):
"""Given a string returns a `torch.nn.modules.activation`."""
if not isinstance(activation, str):
return activation
mapper = {
"leaky_relu": nn.LeakyReLU(),
"relu": nn.ReLU(),
"selu": nn.SELU(),
"tanh": nn.Tanh(),
"sigmoid": nn.Sigmoid(),
"relu6": nn.ReLU6(),
"sin": torch.sin,
}
for k, v in mapper.items():
if activation == k:
return v
raise ValueError("Unkown given activation type : {}".format(activation))
class HyperparameterInterpolator:
"""Helper class to compute the value of a hyperparameter at each training step.
Parameters
----------
initial_value: float
Initial value of the hyperparameter.
final_value: float
Final value of the hyperparameter.
N_steps_interpolate: int
Number of training steps before reaching the `final_value`.
Start_step: int, optional
Number of steps to wait for before starting annealing. During the waiting time,
the hyperparameter will be `default`.
Default: float, optional
Default hyperparameter value that will be used for the first `start_step`s. If
`None` uses `initial_value`.
mode: {"linear", "exponential", "logarithmic"}, optional
Interpolation mode.
is_restart : bool, optional
Whether to restart the interpolator after n_steps_interpolate.
"""
def __init__(
self,
initial_value,
final_value,
n_steps_interpolate,
start_step=0,
default=None,
mode="linear",
is_restart=False,
):
self.initial_value = initial_value
self.final_value = final_value
self.n_steps_interpolate = n_steps_interpolate
self.start_step = start_step
self.default = default if default is not None else self.initial_value
self.mode = mode.lower()
self.is_restart = is_restart
if self.mode == "linear":
delta = self.final_value - self.initial_value
self.factor = delta / self.n_steps_interpolate
elif self.mode in ["exponential", "logarithmic"]:
delta = self.final_value / self.initial_value
self.factor = delta ** (1 / self.n_steps_interpolate)
else:
raise ValueError("Unkown mode : {}.".format(mode))
self.reset_parameters()
def reset_parameters(self):
"""Reset the interpolator."""
self.n_training_calls = 0
@property
def is_annealing(self):
return (self.start_step <= self.n_training_calls) and (
self.n_training_calls <= (
self.n_steps_interpolate + self.start_step)
)
def __call__(self, is_update):
"""Return the current value of the hyperparameter.
Parameters
----------
Is_update: bool
Whether to update the hyperparameter.
"""
if is_update:
self.n_training_calls += 1
if self.start_step >= self.n_training_calls:
return self.default
n_actual_training_calls = self.n_training_calls - self.start_step
if self.is_annealing:
current = self.initial_value
if self.mode == "linear":
current += self.factor * n_actual_training_calls
elif self.mode in ["logarithmic", "exponential"]:
if (self.mode == "logarithmic") ^ (
self.initial_value < self.final_value
):
current *= self.factor ** n_actual_training_calls
else:
current *= self.factor ** (
self.n_steps_interpolate - n_actual_training_calls
)
current = self.final_value - current
else:
if self.is_restart:
self.reset_parameters()
current = self.final_value
return current
def plot(self, n=None):
"""Plot n steps of interpolation."""
import matplotlib.pyplot as plt
if n is None:
n = self.n_steps_interpolate
out = [self(True) for _ in range(n)]
plt.plot(out)
self.reset_parameters()
def batch_flatten(x):
"""Batch wise flattenting of an array."""
shape = x.shape
return x.view(-1, shape[-1]), shape
def batch_unflatten(x, shape):
"""Revert `batch_flatten`."""
return x.view(*shape[:-1], -1)
def to_number(X):
"""Convert the input to a number."""
try:
return X.item()
except AttributeError:
return X
def tuple_cont_to_cont_tuple(tuples):
"""Converts a tuple of containers (list, tuple, dict) to a container of tuples."""
if isinstance(tuples[0], dict):
# assumes keys are correct
return {k: tuple(dic[k] for dic in tuples) for k in tuples[0].keys()}
elif isinstance(tuples[0], list):
return list(zip(*tuples))
elif isinstance(tuples[0], tuple):
return tuple(zip(*tuples))
else:
raise ValueError("Unkown conatiner type: {}.".format(type(tuples[0])))
def cont_tuple_to_tuple_cont(container):
"""Converts a container (list, tuple, dict) of tuple to a tuple of container."""
if isinstance(container, dict):
return tuple(dict(zip(container, val)) for val in zip(*container.values()))
elif isinstance(container, list) or isinstance(container, tuple):
return tuple(zip(*container))
else:
raise ValueError("Unkown conatiner type: {}.".format(type(container)))
def set_seed(seed):
"""Set the random seed."""
if seed is not None:
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)
@contextlib.contextmanager
def tmp_seed(seed):
"""Context manager to use a temporary random seed with `with` statement."""
np_state = np.random.get_state()
torch_state = torch.get_rng_state()
random_state = random.getstate()
if torch.cuda.is_available():
torch_cuda_state = torch.cuda.get_rng_state()
set_seed(seed)
try:
yield
finally:
if seed is not None:
# if seed is None do as if no tmp_seed
np.random.set_state(np_state)
torch.set_rng_state(torch_state)
random.setstate(random_state)
if torch.cuda.is_available():
torch.cuda.set_rng_state(torch_cuda_state)
def clip_interval(x, bound1, bound2, warn_mssg=["x", "bound1", "bound2"]):
"""
Clips x to [bound1,bound2] or [bound2,bound1]. If `warn_mssg` is a list of the 3 name variable
names that will be used to to warn the user that a variables was clipped to the given interval
(no warning if `None`).
"""
if bound2 < bound1:
bound1, bound2 = bound2, bound1
if warn_mssg is not None:
warn_mssg[1], warn_mssg[2] = warn_mssg[2], warn_mssg[1]
def get_txt(to): return "{}={} not in [{}={}] = [{}={}]. Setting it to {}.".format(
warn_mssg[0], x, warn_mssg[1], warn_mssg[2], bound1, bound2, to
)
if x < bound1:
if warn_mssg is not None:
warnings.warn(get_txt(bound1))
return bound1
if x > bound2:
if warn_mssg is not None:
warnings.warn(get_txt(bound1))
return bound2
return x
def channels_to_2nd_dim(X):
"""
Takes a signal with channels on the last dimension (for most operations) and
returns it with channels on the second dimension (for convolutions).
"""
return X.permute(*([0, X.dim() - 1] + list(range(1, X.dim() - 1))))
def channels_to_last_dim(X):
"""
Takes a signal with channels on the second dimension (for convolutions) and
returns it with channels on the last dimension (for most operations).
"""
return X.permute(*([0] + list(range(2, X.dim())) + [1]))
def mask_and_apply(x, mask, f):
"""Applies a callable on a masked version of a input."""
tranformed_selected = f(x.masked_select(mask))
return x.masked_scatter(mask, tranformed_selected)
def indep_shuffle_(a, axis=-1):
"""
Shuffle `a` in-place along the given axis.
Apply `numpy.random.shuffle` to the given axis of `a`.
Each one-dimensional slice is shuffled independently.
Credits : https://github.com/numpy/numpy/issues/5173
"""
b = a.swapaxes(axis, -1)
# Shuffle `b` in-place along the last axis. `b` is a view of `a`,
# so `a` is shuffled in place, too.
shp = b.shape[:-1]
for ndx in np.ndindex(shp):
np.random.shuffle(b[ndx])
def ratio_to_int(percentage, max_val):
"""Converts a ratio to an integer if it is smaller than 1."""
if 1 <= percentage <= max_val:
out = percentage
elif 0 <= percentage < 1:
out = percentage * max_val
else:
raise ValueError(
"percentage={} outside of [0,{}].".format(percentage, max_val))
return int(out)
def prod(iterable):
"""Compute the product of all elements in an iterable."""
return reduce(operator.mul, iterable, 1)
def rescale_range(X, old_range, new_range):
"""Rescale X linearly to be in `new_range` rather than `old_range`."""
old_min = old_range[0]
new_min = new_range[0]
old_delta = old_range[1] - old_min
new_delta = new_range[1] - new_min
return (((X - old_min) * new_delta) / old_delta) + new_min
def clamp(
x,
minimum=-float("Inf"),
maximum=float("Inf"),
is_leaky=False,
negative_slope=0.01,
hard_min=None,
hard_max=None,
):
"""
Clamps a tensor to the given [minimum, maximum] (leaky) bound, with
an optional hard clamping.
"""
lower_bound = (
(minimum + negative_slope * (x - minimum))
if is_leaky
else torch.zeros_like(x) + minimum
)
upper_bound = (
(maximum + negative_slope * (x - maximum))
if is_leaky
else torch.zeros_like(x) + maximum
)
clamped = torch.max(lower_bound, torch.min(x, upper_bound))
if hard_min is not None or hard_max is not None:
if hard_min is None:
hard_min = -float("Inf")
elif hard_max is None:
hard_max = float("Inf")
clamped = clamp(x, minimum=hard_min, maximum=hard_max, is_leaky=False)
return clamped
class ProbabilityConverter(nn.Module):
"""Maps floats to probabilites (between 0 and 1), element-wise.
Parameters
----------
min_p : float, optional
Minimum probability, can be useful to set greater than 0 in order to keep
gradient flowing if the probability is used for convex combinations of
different parts of the model. Note that maximum probability is `1-min_p`.
activation : {"sigmoid", "hard-sigmoid", "leaky-hard-sigmoid"}, optional
name of the activation to use to generate the probabilities. `sigmoid`
has the advantage of being smooth and never exactly 0 or 1, which helps
gradient flows. `hard-sigmoid` has the advantage of making all values
between min_p and max_p equiprobable.
is_train_temperature : bool, optional
Whether to train the paremeter controling the steepness of the activation.
This is useful when x is used for multiple tasks, and you don't want to
constraint its magnitude.
is_train_bias : bool, optional
Whether to train the bias to shift the activation. This is useful when x is
used for multiple tasks, and you don't want to constraint it's scale.
trainable_dim : int, optional
Size of the trainable bias and temperature. If `1` uses the same vale
across all dimension, if not should be equal to the number of input
dimensions to different trainable parameters for each dimension. Note
that the initial value will still be the same for all dimensions.
initial_temperature : int, optional
Initial temperature, a higher temperature makes the activation steaper.
initial_probability : float, optional
Initial probability you want to start with.
initial_x : float, optional
First value that will be given to the function, important to make
`initial_probability` work correctly.
bias_transformer : callable, optional
Transformer function of the bias. This function should only take care of
the boundaries (e.g. leaky relu or relu).
temperature_transformer : callable, optional
Transformer function of the temperature. This function should only take
care of the boundaries (e.g. leaky relu or relu).
"""
def __init__(
self,
min_p=0.0,
activation="sigmoid",
is_train_temperature=False,
is_train_bias=False,
trainable_dim=1,
initial_temperature=1.0,
initial_probability=0.5,
initial_x=0,
bias_transformer=nn.Identity(),
temperature_transformer=nn.Identity(),
):
super().__init__()
self.min_p = min_p
self.activation = activation
self.is_train_temperature = is_train_temperature
self.is_train_bias = is_train_bias
self.trainable_dim = trainable_dim
self.initial_temperature = initial_temperature
self.initial_probability = initial_probability
self.initial_x = initial_x
self.bias_transformer = bias_transformer
self.temperature_transformer = temperature_transformer
self.reset_parameters()
def reset_parameters(self):
self.temperature = torch.tensor(
[self.initial_temperature] * self.trainable_dim)
if self.is_train_temperature:
self.temperature = nn.Parameter(self.temperature)
initial_bias = self._probability_to_bias(
self.initial_probability, initial_x=self.initial_x
)
self.bias = torch.tensor([initial_bias] * self.trainable_dim)
if self.is_train_bias:
self.bias = nn.Parameter(self.bias)
def forward(self, x):
self.temperature.to(x.device)
self.bias.to(x.device)
temperature = self.temperature_transformer(self.temperature)
bias = self.bias_transformer(self.bias)
if self.activation == "sigmoid":
full_p = torch.sigmoid((x + bias) * temperature)
elif self.activation in ["hard-sigmoid", "leaky-hard-sigmoid"]:
# uses 0.2 and 0.5 to be similar to sigmoid
y = 0.2 * ((x + bias) * temperature) + 0.5
if self.activation == "leaky-hard-sigmoid":
full_p = clamp(
y,
minimum=0.1,
maximum=0.9,
is_leaky=True,
negative_slope=0.01,
hard_min=0,
hard_max=0,
)
elif self.activation == "hard-sigmoid":
full_p = clamp(y, minimum=0.0, maximum=1.0, is_leaky=False)
else:
raise ValueError("Unkown activation : {}".format(self.activation))
p = rescale_range(full_p, (0, 1), (self.min_p, 1 - self.min_p))
return p
def _probability_to_bias(self, p, initial_x=0):
"""Compute the bias to use to satisfy the constraints."""
assert p > self.min_p and p < 1 - self.min_p
range_p = 1 - self.min_p * 2
p = (p - self.min_p) / range_p
p = torch.tensor(p, dtype=torch.float)
if self.activation == "sigmoid":
bias = -(torch.log((1 - p) / p) /
self.initial_temperature + initial_x)
elif self.activation in ["hard-sigmoid", "leaky-hard-sigmoid"]:
bias = ((p - 0.5) / 0.2) / self.initial_temperature - initial_x
return bias
def make_abs_conv(Conv):
"""Make a convolution have only positive parameters."""
class AbsConv(Conv):
def forward(self, input):
return F.conv2d(
input,
self.weight.abs(),
self.bias,
self.stride,
self.padding,
self.dilation,
self.groups,
)
return AbsConv
def make_depth_sep_conv(Conv):
"""Make a convolution module depth separable."""
class DepthSepConv(nn.Module):
"""Make a convolution depth separable.
Parameters
----------
in_channels : int
Number of input channels.
out_channels : int
Number of output channels.
kernel_size : int
**kwargs :
Additional arguments to `Conv`
"""
def __init__(
self,
in_channels,
out_channels,
kernel_size,
confidence=False,
bias=True,
**kwargs,
):
super().__init__()
self.depthwise = Conv(
in_channels,
in_channels,
kernel_size,
groups=in_channels,
bias=bias,
**kwargs,
)
self.pointwise = Conv(in_channels, out_channels, 1, bias=bias)
self.reset_parameters()
def forward(self, x):
out = self.depthwise(x)
out = self.pointwise(out)
return out
def reset_parameters(self):
weights_init(self)
return DepthSepConv
class ReturnNotTensor:
"""Helper class to allow non tensor outputs from skorch."""
def __init__(self, out):
self.out = out
def to(self, *args, **kwargs):
return self.out
def return_not_tensor(out):
if isinstance(out, torch.Tensor):
return out
else:
return ReturnNotTensor(out)
class Constant:
def __init__(self, c):
self.c = c
def __call__(self, *args):
return self.c
def set_requires_grad(module, val):
"""Set all the gradients of a given module to a certain value."""
for p in module.parameters():
p.requires_grad = val
@contextlib.contextmanager
def no_grad_modules(modules):
"""Context manager that deactivates the gradients of a list of modules."""
for module in modules:
set_requires_grad(module, False)
try:
yield
finally:
for module in modules:
set_requires_grad(module, True)
def mean_p_logits(logits, dim=0, eps=1e-8):
"""Take the mean in probability space given on some logits."""
if logits.size(dim) == 1:
return logits.squeeze(dim)
else:
#! SHOULD BE USING LOG SUM EXP
# have to put into probability space to take average
mean = logits.softmax(-1).mean(dim)
return (
mean + eps
).log() # put back in logit space making sure no nan (no zero)
class BaseRepresentation:
"""Compute the base representation for a number in a certain base while memoizing."""
def __init__(self, base):
self.base = base
self.memoize = {0: []}
def __call__(self, number):
"""Return a list of the base representation of number."""
if number in self.memoize:
return self.memoize[number]
self.memoize[number] = self(number // self.base) + [number % self.base]
return self.memoize[number]
def get_ith_digit(self, number, i):
"""Return the ith digit pf the base representation of number."""
digits = self(number)
if i >= len(digits):
return 0 # implicit padding with zeroes
return digits[-i - 1]
class BaseRepIthDigits:
"""Compute the ith digit in a given base for torch batch of numbers while memoizing (in numpy)."""
def __init__(self, base):
base_rep = BaseRepresentation(base)
self.base_rep = np.vectorize(base_rep.get_ith_digit)
def __call__(self, tensor, i_digit):
return self.base_rep(tensor, i_digit)
class BackwardPDB(torch.autograd.Function):
"""Run PDB in the backward pass."""
@staticmethod
def forward(ctx, input, name="debugger"):
ctx.name = name
ctx.save_for_backward(input)
return input
@staticmethod
def backward(ctx, grad_output):
(input,) = ctx.saved_tensors
if not torch.isfinite(grad_output).all() or not torch.isfinite(input).all():
import pdb
pdb.set_trace()
return grad_output, None # 2 args so return None for `name`
backward_pdb = BackwardPDB.apply
|
decodable_information_bottleneck-main
|
dib/utils/helpers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from functools import partial
import torch.nn as nn
BATCHNORMS = [None, nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d]
def get_norm_layer(norm_layer, dim=2):
"""Return the correct normalization layer.
Parameters
----------
norm_layer : callable or {"batchnorm", "identity"}
Layer to return.
dim : int, optional
Number of dimension of the input (e.g. 2 for images).
"""
if norm_layer is None:
return None
elif "batch" in norm_layer:
Norm = BATCHNORMS[dim]
elif norm_layer == "identity":
Norm = nn.Identity
elif isinstance(norm_layer, str):
raise ValueError(f"Uknown normal_layer={norm_layer}")
else:
Norm = norm_layer
return Norm
|
decodable_information_bottleneck-main
|
dib/predefined/helper_layers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from functools import partial
import torch.nn as nn
from .cnn import get_Cnn
from .mlp import MLP
__all__ = ["get_predefined", "try_get_predefined"]
def try_get_predefined(d, **kwargs):
"""Tries to get a predefined module, given a dicttionary of all arguments, if not returns it."""
try:
return get_predefined(**d, **kwargs)
except TypeError:
return d
# TO DOC
def get_predefined(name, meta_kwargs={}, **kwargs):
"""Helper function which returns unitialized common neural networks."""
name = name.lower()
if name == "cnn":
Module = get_Cnn(**kwargs)
elif name == "mlp":
Module = partial(MLP, **kwargs)
elif name == "identity":
Module = nn.Identity
elif name == "linear":
Module = partial(nn.Linear, **kwargs)
elif name is None:
return None
elif not isinstance(name, str):
Module = name
else:
raise ValueError(name)
return Module
|
decodable_information_bottleneck-main
|
dib/predefined/predefined.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .cnn import *
from .imgs import *
from .mlp import *
from .predefined import *
|
decodable_information_bottleneck-main
|
dib/predefined/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import warnings
from functools import partial
import numpy as np
import torch
import torch.nn as nn
from torch.nn import functional as F
from dib.utils.helpers import (
channels_to_2nd_dim,
channels_to_last_dim,
make_depth_sep_conv,
)
from dib.utils.initialization import init_param_, weights_init
from .helper_layers import get_norm_layer
logger = logging.getLogger(__name__)
__all__ = ["get_Cnn"]
def get_Cnn(
dim=2,
mode="vanilla",
conv="vanilla",
block="res",
normalization=None,
is_chan_last=True,
pool=None,
**kwargs,
):
"""Helper function which returns a cnn, in a way callable by the CLI.
Parameters
----------
dim : int, optional
Grid input shape.
mode : {"vanilla", "unet"}, optional
conv : {"vanilla", "gauss"}, optional
block : {"simple", "res"}, optional
normalization : {"batchnorm", None}, optional
is_chan_last : bool, optional
pool : {"avg", "max", None}, optional
Returns
-------
Cnn : nn.Module
Unitialized CNN
kwargs : dict
Unused kwargs
"""
if mode == "vanilla":
Module = CNN
elif mode == "unet":
Module = UnetCNN
elif mode == "nin":
Module = NIN
if block == "simple":
Block = ConvBlock
elif block == "res":
Block = ResConvBlock
elif block == "nin":
Block = NINBlock
else:
Block = ResConvBlock
Norm = get_norm_layer(normalization, dim=dim)
if pool == "avg":
Pool = AVGPOOLINGS[dim]
elif pool == "max":
Pool = MAXPOOLINGS[dim]
elif pool is None:
Pool = nn.Identity
elif pool is None:
Pool = pool
if conv == "vanilla":
Conv = CONVS[dim]
elif conv == "gauss":
Conv = GAUSSIANCONVS[dim]
elif conv == "reverse":
Conv = REVCONVS[dim]
else:
Conv = conv
return partial(
Module,
ConvBlock=Block,
Conv=Conv,
is_chan_last=is_chan_last,
Normalization=Norm,
Pooling=partial(Pool, kernel_size=2),
**kwargs,
)
### BLOCKS ###
class ConvBlock(nn.Module):
"""Simple convolutional block with a single layer.
Parameters
----------
in_chan : int
Number of input channels.
out_chan : int
Number of output channels.
Conv : nn.Module
Convolutional layer (uninitialized). E.g. `nn.Conv1d`.
kernel_size : int or tuple, optional
Size of the convolving kernel.
dilation : int or tuple, optional
Spacing between kernel elements.
padding : int or tuple, optional
Padding added to both sides of the input. If `-1` uses padding that
keeps the size the same. Currently only works if `kernel_size` is even
and only takes into account the kenerl size and dilation, but not other
arguments (e.g. stride).
activation: callable, optional
Activation object. E.g. `nn.ReLU`.
Normalization : nn.Module, optional
Normalization layer (unitialized). E.g. `nn.BatchNorm1d`.
Pooling : nn.Module, optional
Pooling layer to apply at the end of the block. The kernel size should be already defined.
is_depth_sep ; bool, optional
Whether to use depth separable convolutions.
kwargs :
Additional arguments to `Conv`.
References
----------
[1] He, K., Zhang, X., Ren, S., & Sun, J. (2016, October). Identity mappings
in deep residual networks. In European conference on computer vision
(pp. 630-645). Springer, Cham.
[2] Chollet, F. (2017). Xception: Deep learning with depthwise separable
convolutions. In Proceedings of the IEEE conference on computer vision
and pattern recognition (pp. 1251-1258).
"""
def __init__(
self,
in_chan,
out_chan,
Conv,
kernel_size=5,
dilation=1,
padding=-1,
activation=nn.ReLU(),
Normalization=nn.Identity,
is_depth_sep=False,
Pooling=nn.Identity,
**kwargs,
):
super().__init__()
if Normalization is None:
Normalization = nn.Identity
self.activation = activation
if padding == -1:
padding = (kernel_size // 2) * dilation
if kwargs.get("stride", 1) != 1:
warnings.warn(
"`padding == -1` but `stride != 1`. The output might be of different dimension "
"as the input depending on other hyperparameters."
)
if is_depth_sep:
Conv = make_depth_sep_conv(Conv)
self.conv = Conv(in_chan, out_chan, kernel_size,
padding=padding, **kwargs)
self.norm = Normalization(out_chan)
self.pool = Pooling()
self.reset_parameters()
def reset_parameters(self):
weights_init(self)
def forward(self, X):
return self.norm(self.activation(self.pool(self.conv(X))))
class ResConvBlock(nn.Module):
"""Convolutional block (2 layers) inspired by the pre-activation Resnet [1]
and depthwise separable convolutions [2].
Parameters
----------
in_chan : int
Number of input channels.
out_chan : int
Number of output channels.
Conv : nn.Module
Convolutional layer (uninitialized). E.g. `nn.Conv1d`.
kernel_size : int or tuple, optional
Size of the convolving kernel. Should be odd to keep the same size.
activation: callable, optional
Activation object. E.g. `nn.RelU()`.
Normalization : nn.Module, optional
Normalization layer (uninitialized). E.g. `nn.BatchNorm1d`.
Pooling : nn.Module, optional
Pooling layer to apply at the end of the block. The kernel size should be already defined.
is_bias : bool, optional
Whether to use a bias.
is_depth_sep ; bool, optional
Whether to use depth separable convolutions.
References
----------
[1] He, K., Zhang, X., Ren, S., & Sun, J. (2016, October). Identity mappings
in deep residual networks. In European conference on computer vision
(pp. 630-645). Springer, Cham.
[2] Chollet, F. (2017). Xception: Deep learning with depthwise separable
convolutions. In Proceedings of the IEEE conference on computer vision
and pattern recognition (pp. 1251-1258).
"""
def __init__(
self,
in_chan,
out_chan,
Conv,
kernel_size=5,
activation=nn.ReLU(),
Normalization=nn.Identity,
is_bias=True,
Pooling=nn.Identity,
is_depth_sep=False,
):
super().__init__()
if Normalization is None:
Normalization = nn.Identity
self.activation = activation
if kernel_size % 2 == 0:
raise ValueError(
"`kernel_size={}`, but should be odd.".format(kernel_size))
padding = kernel_size // 2
conv_args = (in_chan, in_chan, kernel_size)
conv_kwargs = dict(padding=padding, bias=is_bias)
self.norm1 = Normalization(in_chan)
if is_depth_sep:
self.conv1 = make_depth_sep_conv(Conv)(*conv_args, **conv_kwargs)
else:
self.conv1 = Conv(*conv_args, **conv_kwargs)
self.norm2 = Normalization(in_chan)
self.conv2_depthwise = Conv(*conv_args, groups=in_chan, **conv_kwargs)
self.conv2_pointwise = Conv(in_chan, out_chan, 1, bias=is_bias)
self.pool = Pooling()
self.reset_parameters()
def reset_parameters(self):
weights_init(self)
def forward(self, X):
out = self.conv1(self.activation(self.norm1(X)))
out = self.conv2_depthwise(self.activation(self.norm2(out)))
# adds residual before point wise => output can change number of channels
out = out + X
out = self.conv2_pointwise(out)
return self.pool(out)
class NINBlock(torch.nn.Module):
def __init__(self, chan, dropout, Normalization=nn.Identity, **kwargs):
super().__init__()
self.out = nn.Sequential(
nn.Conv2d(chan, chan, 3, 2, padding=1),
Normalization(chan),
nn.ReLU(inplace=True),
nn.Conv2d(chan, chan, 1, 1),
Normalization(chan),
nn.ReLU(inplace=True),
nn.Conv2d(chan, chan, 1, 1),
Normalization(chan),
nn.ReLU(inplace=True),
)
self.dropout = nn.Dropout(p=dropout)
def forward(self, x):
return self.dropout(self.out(x))
### MODULES ###
class NIN(torch.nn.Module):
def __init__(
self,
in_channels,
out_channels,
depth=2,
dropout=0,
width=2,
is_flatten=False,
**kwargs,
):
super().__init__()
self.chan = nn.Conv2d(in_channels, 96 * width, 1)
self.layers = nn.Sequential(
*[NINBlock(96 * width, dropout, **kwargs) for i in range(depth)]
)
self.out = nn.Conv2d(96 * width, out_channels, 1)
self.is_flatten = is_flatten
def forward(self, x):
x = torch.relu(self.chan(x))
x = self.layers(x)
x = F.adaptive_avg_pool2d(self.out(x), (1, 1))
if self.is_flatten:
return torch.flatten(x, start_dim=1)
else:
return x
class CNN(nn.Module):
"""Simple multilayer CNN.
Parameters
----------
in_channels : int
Number of input channels
out_channels : int
Number of output channels.
tmp_channels : int or list, optional
Number of temporary channels. If integer then uses always the same. If list then needs to
be of size `n_blocks - 1`, e.g. [16, 32, 64] means that you will have a
`[ConvBlock(in_channels,16), ConvBlock(16,32), ConvBlock(32,64), ConvBlock(64, out_channels)]`.
ConvBlock : nn.Module, optional
Convolutional block (unitialized). Needs to take as input `Should be
initialized with `ConvBlock(in_chan, out_chan)`.
n_blocks : int, optional
Number of convolutional blocks.
is_chan_last : bool, optional
Whether the channels are on the last dimension of the input.
is_flatten : bool, optional
Whether to flatten the output.
is_force_hid_smaller : bool, optional
Whether to force the hidden channels to be smaller or equal than in and out.
If not, it forces the hidden channels to be larger or equal than in or out.
kwargs :
Additional arguments to `ConvBlock`.
"""
def __init__(
self,
in_channels,
out_channels,
tmp_channels=32,
ConvBlock=partial(ConvBlock, Conv=nn.Conv2d),
n_blocks=3,
is_chan_last=False,
is_flatten=False,
is_force_hid_smaller=False,
**kwargs,
):
super().__init__()
self.n_blocks = n_blocks
self.is_chan_last = is_chan_last
new_tmp_channels = tmp_channels
if isinstance(tmp_channels, int):
if is_force_hid_smaller and tmp_channels > max(in_channels, out_channels):
new_tmp_channels = max(out_channels, in_channels)
txt = "tmp_channels={} larger than output={} and input={}. Setting it to {}."
warnings.warn(
txt.format(
tmp_channels, out_channels, in_channels, new_tmp_channels
)
)
elif tmp_channels < min(out_channels, in_channels):
new_tmp_channels = min(out_channels, in_channels)
txt = "tmp_channels={} smaller than output={} and input={}. Setting it to {}."
warnings.warn(
txt.format(
tmp_channels, out_channels, in_channels, new_tmp_channels
)
)
else:
n_blocks = len(tmp_channels) + 1
self.in_out_channels = self._get_in_out_channels(
in_channels, out_channels, new_tmp_channels, n_blocks
)
self.conv_blocks = nn.ModuleList(
[
ConvBlock(in_chan, out_chan, **kwargs)
for in_chan, out_chan in self.in_out_channels
]
)
self.is_return_rep = False # never return representation for vanilla conv
self.is_flatten = is_flatten
self.reset_parameters()
def reset_parameters(self):
weights_init(self)
def _get_in_out_channels(self, in_channels, out_channels, tmp_channels, n_blocks):
"""Return a list of tuple of input and output channels."""
if isinstance(tmp_channels, int):
tmp_channels = [tmp_channels] * (n_blocks - 1)
else:
tmp_channels = list(tmp_channels)
assert len(tmp_channels) == (n_blocks - 1), "tmp_channels: {} != {}".format(
len(tmp_channels), n_blocks - 1
)
channel_list = [in_channels] + tmp_channels + [out_channels]
return list(zip(channel_list, channel_list[1:]))
def forward(self, X):
if self.is_chan_last:
X = channels_to_2nd_dim(X)
X, representation = self.apply_convs(X)
if self.is_chan_last:
X = channels_to_last_dim(X)
if self.is_flatten:
X = torch.flatten(X, start_dim=1)
if self.is_return_rep:
return X, representation
return X
def apply_convs(self, X):
for conv_block in self.conv_blocks:
X = conv_block(X)
return X, None
class UnetCNN(CNN):
"""Unet [1].
Parameters
----------
in_channels : int
Number of input channels
out_channels : int
Number of output channels.
tmp_channels : int or list, optional
Number of temporary channels. If integer then uses always the same. If list then needs to
be of size `n_blocks - 1`, e.g. [16, 32, 64] means that you will have a
`[ConvBlock(in_channels,16), ConvBlock(16,32), ConvBlock(32,64), ConvBlock(64, out_channels)]`.
ConvBlock : nn.Module, optional
Convolutional block (unitialized). Needs to take as input `Should be
initialized with `ConvBlock(in_chan, out_chan)`.
Pool : nn.Module, optional
Pooling layer (unitialized). E.g. torch.nn.MaxPool1d.
upsample_mode : {'nearest', 'linear', bilinear', 'bicubic', 'trilinear'}, optional
The upsampling algorithm: nearest, linear (1D-only), bilinear, bicubic
(2D-only), trilinear (3D-only).
max_nchannels : int, optional
Bounds the maximum number of channels instead of always doubling them at
downsampling block.
pooling_size : int or tuple, optional
Size of the pooling filter.
factor_chan : float, optional
The factor by which to multiply the number of channels after each down block. If it's a float
the number of channels are rounded.
is_force_same_bottleneck : bool, optional
Whether to use the average bottleneck for the same functions sampled at
different context and target. If `True` the first and second halves
of a batch should contain different samples of the same functions (in order).
is_return_rep : bool, optional
Whether to return a summary representation, that corresponds to the
bottleneck + global mean pooling.
is_skip_resize : bool, optional
Whether to skip the resizing steps. Only possible if `in_channels==out_channels==tmp_channels`.
kwargs :
Additional arguments to `ConvBlock`.
References
----------
[1] Ronneberger, Olaf, Philipp Fischer, and Thomas Brox. "U-net: Convolutional
networks for biomedical image segmentation." International Conference on
Medical image computing and computer-assisted intervention. Springer, Cham, 2015.
"""
def __init__(
self,
in_channels,
out_channels,
tmp_channels=32,
ConvBlock=partial(ResConvBlock, Conv=nn.Conv2d),
Pool=nn.AvgPool2d,
upsample_mode="bilinear",
max_nchannels=256,
pooling_size=2,
factor_chan=2,
is_force_same_bottleneck=False,
is_return_rep=False,
is_skip_resize=False,
**kwargs,
):
self.is_skip_resize = is_skip_resize
self.factor_chan = factor_chan
self.max_nchannels = max_nchannels
super().__init__(
in_channels,
out_channels,
tmp_channels=tmp_channels,
ConvBlock=ConvBlock,
**kwargs,
)
self.pooling_size = pooling_size
self.pooling = Pool(self.pooling_size)
self.upsample_mode = upsample_mode
self.is_force_same_bottleneck = is_force_same_bottleneck
self.is_return_rep = is_return_rep
def apply_convs(self, X):
if self.is_skip_resize:
n_tmp_blocks = self.n_blocks
start_block = 0
else:
n_tmp_blocks = self.n_blocks - 2
# Input block
X = self.conv_blocks[0](X)
start_block = 1
n_down_blocks = n_tmp_blocks // 2
residuals = [None] * n_down_blocks
# Down
for i in range(n_down_blocks):
X = self.conv_blocks[start_block + i](X)
residuals[i] = X
X = self.pooling(X)
# Bottleneck
X = self.conv_blocks[n_down_blocks](X)
# Representation before forcing same bottleneck
representation = X.view(*X.shape[:2], -1).mean(-1)
if self.is_force_same_bottleneck and self.training:
# forces the u-net to use the bottleneck by giving additional information
# there. I.e. taking average between bottleneck of different samples
# of the same functions. Because bottleneck should be a global representation
# => should not depend on the sample you chose
batch_size = X.size(0)
batch_1 = X[: batch_size // 2, ...]
batch_2 = X[batch_size // 2:, ...]
X_mean = (batch_1 + batch_2) / 2
X = torch.cat([X_mean, X_mean], dim=0)
# Up
for i in range(n_down_blocks + 1, n_tmp_blocks):
X = F.interpolate(
X,
mode=self.upsample_mode,
scale_factor=self.pooling_size,
align_corners=True,
)
X = torch.cat(
(X, residuals[n_down_blocks - i]), dim=1
) # concat on channels
X = self.conv_blocks[i + start_block](X)
if not self.is_skip_resize:
# Output Block
X = self.conv_blocks[-1](X)
return X, representation
def _get_in_out_channels(self, in_channels, out_channels, tmp_channels, n_blocks):
"""Return a list of tuple of input and output channels for a Unet."""
if self.is_skip_resize:
assert in_channels == out_channels == tmp_channels
n_tmp_blocks = n_blocks
else:
n_tmp_blocks = n_blocks - 2 # removes last and first block for this part
assert n_blocks % 2 == 1, "n_blocks={} not odd".format(n_blocks)
# e.g. if tmp_channels=16, n_tmp_blocks=5: [16, 32, 64]
channel_list = [
round(self.factor_chan ** i * tmp_channels)
for i in range(n_tmp_blocks // 2 + 1)
]
# e.g.: [16, 32, 64, 64, 32, 16]
channel_list = channel_list + channel_list[::-1]
# bound max number of channels by self.max_nchannels
channel_list = [min(c, self.max_nchannels) for c in channel_list]
# e.g.: [(16, 32), (32,64), (64, 64), (64, 32), (32, 16)]
in_out_channels = list(zip(channel_list, channel_list[1:]))
# e.g.: [(16, 32), (32,64), (64, 64), (128, 32), (64, 16)] due to concat
idcs = slice(len(in_out_channels) // 2 + 1, len(in_out_channels))
in_out_channels[idcs] = [
(in_chan * 2, out_chan) for in_chan, out_chan in in_out_channels[idcs]
]
if not self.is_skip_resize:
# Adds in and out block
in_out_channels = (
[(in_channels, tmp_channels)]
+ in_out_channels
+ [(tmp_channels, out_channels)]
)
assert len(in_out_channels) == (n_blocks), "in_out_channels: {} != {}".format(
len(in_out_channels), n_blocks
)
return in_out_channels
### CONV ###
class GaussianConv2d(nn.Module):
def __init__(self, kernel_size=5, **kwargs):
super().__init__()
self.kwargs = kwargs
assert kernel_size % 2 == 1
self.kernel_sizes = (kernel_size, kernel_size)
self.exponent = -(
(torch.arange(0, kernel_size).view(-1, 1).float() - kernel_size // 2) ** 2
)
self.reset_parameters()
def reset_parameters(self):
self.weights_x = nn.Parameter(torch.tensor([1.0]))
self.weights_y = nn.Parameter(torch.tensor([1.0]))
def forward(self, X):
# only switch first time to device
self.exponent = self.exponent.to(X.device)
marginal_x = torch.softmax(self.exponent * self.weights_x, dim=0)
marginal_y = torch.softmax(self.exponent * self.weights_y, dim=0).T
in_chan = X.size(1)
filters = marginal_x @ marginal_y
filters = filters.view(1, 1, *self.kernel_sizes).expand(
in_chan, 1, *self.kernel_sizes
)
return F.conv2d(X, filters, groups=in_chan, **self.kwargs)
# GLOBAL VARIABLES
CONVS = [None, nn.Conv1d, nn.Conv2d, nn.Conv3d]
REVCONVS = [None, nn.ConvTranspose1d, nn.ConvTranspose2d, nn.ConvTranspose3d]
GAUSSIANCONVS = {2: GaussianConv2d} # at the end because defined in this file
FCONVS = [None, F.conv1d, F.conv2d, F.conv3d]
MAXPOOLINGS = [None, nn.MaxPool1d, nn.MaxPool2d, nn.MaxPool2d]
AVGPOOLINGS = [None, nn.AvgPool1d, nn.AvgPool2d, nn.AvgPool2d]
|
decodable_information_bottleneck-main
|
dib/predefined/cnn.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import warnings
import numpy as np
import torch
import torch.nn as nn
from skorch.utils import to_numpy
from dib.utils.helpers import batch_flatten, batch_unflatten, get_activation, tmp_seed
from dib.utils.initialization import (
linear_init,
set_linear_like_,
set_normlayer_like_,
weights_init,
)
from dib.utils.pruning import RandomUnstructured, global_unstructured, is_pruned, remove
from .helper_layers import get_norm_layer
__all__ = ["MLP"]
logger = logging.getLogger(__name__)
class MLP(nn.Module):
"""General MLP class.
Parameters
----------
input_size: int
output_size: int
hidden_size: int or list, optional
Number of hidden neurones. If list, `n_hidden_layers` will be `len(n_hidden_layers)`.
n_hidden_layers: int, optional
Number of hidden layers.
activation: callable, optional
Activation function. E.g. `nn.RelU()`.
is_bias: bool, optional
Whether to use biaises in the hidden layers.
dropout: float, optional
Dropout rate.
is_force_hid_larger : bool, optional
Whether to force the hidden dimension to be larger or equal than in or out.
n_skip : int, optional
Number of layers to skip with residual connection
norm_layer : nn.Module or {"identity","batch"}, optional
Normalizing layer to use.
k_prune : int, optional
Number times to apply 50% pruning on all weight matrices besides the last one.
seed : int, optional
Random seed, only used when pruning
previous_mlp : MLP, optional
Previous MLP to use as initialization. All the layers in common must have the same shapes.
is_rectangle : bool, optional
Wether to use a rectangle scheme for the MLP. If True, uses
n_hidden_layers=hidden_size*2**n_hidden_layers.
is_plot_activation : bool, optional
Whether to store all activations for plotting
is_mult_hid_input : bool, optional
Whether the hidden is a factor that should be multiplied by `input_size` rather than an
absolute number.
"""
def __init__(
self,
input_size,
output_size,
hidden_size=32,
n_hidden_layers=1,
activation=nn.LeakyReLU(),
is_bias=True,
dropout=0,
is_force_hid_larger=False,
n_skip=0,
norm_layer="identity",
k_prune=0,
seed=123,
previous_mlp=None,
is_rectangle=False,
is_plot_activation=False,
is_mult_hid_input=False,
):
super().__init__()
self.input_size = input_size
self.output_size = output_size
self.hidden_size = hidden_size
self.n_hidden_layers = n_hidden_layers
self.n_skip = n_skip
self.norm_layer = get_norm_layer(norm_layer, dim=1)
self.k_prune = k_prune
self.seed = seed
self._to_plot_activation = dict()
self.is_rectangle = is_rectangle
self.is_plot_activation = is_plot_activation
self.is_mult_hid_input = is_mult_hid_input
if self.n_hidden_layers == 0:
self.hidden_size = []
self.to_hidden = nn.Linear(
self.input_size, self.output_size, bias=is_bias)
self.out = nn.Identity()
self.reset_parameters()
return
if self.is_mult_hid_input:
if isinstance(self.hidden_size, int):
self.hidden_size = self.hidden_size * self.input_size
else:
self.hidden_size = [
h * self.input_size for h in self.hidden_size]
if self.is_rectangle:
assert isinstance(self.hidden_size, int)
self.hidden_size = self.hidden_size * (2 ** self.n_hidden_layers)
if isinstance(self.hidden_size, int):
if is_force_hid_larger and self.hidden_size < min(
self.output_size, self.input_size
):
self.hidden_size = min(self.output_size, self.input_size)
txt = "hidden_size={} smaller than output={} and input={}. Setting it to {}."
logger.info(
txt.format(hidden_size, output_size,
input_size, self.hidden_size)
)
self.hidden_size = [self.hidden_size] * self.n_hidden_layers
else:
self.n_hidden_layers = len(self.hidden_size)
self.dropout = nn.Dropout(p=dropout) if dropout > 0 else nn.Identity()
self.activation = get_activation(activation)
self.to_hidden = nn.Linear(
self.input_size, self.hidden_size[0], bias=is_bias)
self.tohid_norm = self.norm_layer(self.hidden_size[0])
self.linears = nn.ModuleList(
[
nn.Linear(in_size, out_size, bias=is_bias)
for in_size, out_size in zip(
self.hidden_size[:][:-1], self.hidden_size[1:]
)
# dirty [:] because omegaconf does not accept [:-1] directly
]
)
self.norm_layers = nn.ModuleList(
[
self.norm_layer(out_size)
for _, out_size in zip(self.hidden_size[:][:-1], self.hidden_size[1:])
]
)
self.out = nn.Linear(
self.hidden_size[-1], self.output_size, bias=is_bias)
self.reset_parameters()
if previous_mlp is not None:
self.set_parameters_like_(previous_mlp)
self.prune_weights_(self.k_prune)
def forward(self, x):
# flatten to make for normalizing layer => only 2 dim
x, shape = batch_flatten(x)
x = self.to_hidden(x)
self.plot_activation_(dict(tohidout=x))
if self.n_hidden_layers == 0:
return batch_unflatten(x, shape)
x = self.tohid_norm(x)
self.plot_activation_(dict(tohinorm=x))
x = self.activation(x)
x = self.dropout(x)
old = x
for i, (linear, norm_layer) in enumerate(
zip(self.linears, self.norm_layers), start=1
):
x = linear(x)
self.plot_activation_({f"linout{i}": x})
x = norm_layer(x)
self.plot_activation_({f"linnorm{i}": x})
x = self.activation(x)
if self.n_skip != 0 and i % self.n_skip == 0:
# divided by 10 reduces chances of nan at start (making sure order of magnitude doesn't depend on # layers)
x = old + x / 10
old = x
x = self.dropout(x)
out = self.out(x)
self.plot_activation_(dict(out=out))
out = batch_unflatten(out, shape)
return out
def reset_parameters(self):
init = linear_init
if self.n_hidden_layers == 0:
init(self.to_hidden)
else:
init(self.to_hidden, activation=self.activation)
for lin in self.linears:
init(lin, activation=self.activation)
init(self.out)
def set_parameters_like_(self, mlp):
"""Given an other MLP that has the same input and output size, set all the parameters in common."""
assert mlp.input_size == self.input_size and mlp.output_size == self.output_size
min_layers = min(len(self.linears), len(mlp.linears))
if self.n_hidden_layers == mlp.n_hidden_layers == 0:
self.to_hidden = mlp.to_hidden
elif self.n_hidden_layers != 0 and mlp.n_hidden_layers != 0:
set_linear_like_(self.to_hidden, mlp.to_hidden)
set_linear_like_(self.out, mlp.out)
for i in range(min_layers):
set_linear_like_(self.linears[i], mlp.linears[i])
set_normlayer_like_(self.norm_layers[i], mlp.norm_layers[i])
else:
logger.info(
"Cannot use `set_parameters_like` when only one of the 2 mlps have 0 hidden layers."
)
def prune_weights_(self, k_prune=1, sparsity_ratio=0.5):
"""Apply in place `k_prune` times a `sparsity_ratio` pruning."""
outs = [(self.to_hidden, "weight")] + [
(linear, "weight") for linear in self.linears
]
# don't put the last layer because it depends on whether X or Y as output
# first make sure that all previous pruning is removed
for m, name in outs:
# `remove` does not actually remove, just sets to 0 the weights (fixes the mask)
# => in case the module was already, pruned, adds some jtter to be sure that not 0 and can learn
if is_pruned(m):
remove(m, name)
with torch.no_grad():
m.weight += torch.randn_like(m.weight) / 100
if sparsity_ratio == 0 or k_prune < 1:
return
with tmp_seed(self.seed):
for k in range(k_prune):
global_unstructured(
outs, pruning_method=RandomUnstructured, amount=sparsity_ratio
)
def plot_activation_(self, activations):
if not self.is_plot_activation:
return
for k, v in activations.items():
# opeartion over batchs
v = to_numpy(v)
self._to_plot_activation[k + "_mean"] = v.mean(0)
self._to_plot_activation[k + "_meanabs"] = np.abs(v).mean(0)
def tensorboard(self, writer, epoch, mode="on_grad_computed"):
name = type(self).__name__
if mode == "on_grad_computed":
for k, v in self._to_plot_activation.items():
writer.add_histogram(
f"activations/{name}/" + k, v, global_step=epoch)
self._to_plot_activation = dict()
writer.add_histogram(
f"weights/{name}/w_tohid", self.to_hidden.weight, global_step=epoch
)
writer.add_histogram(
f"weights/{name}/b_tohid", self.to_hidden.bias, global_step=epoch
)
writer.add_histogram(
f"grad/{name}/w_tohid", self.to_hidden.weight.grad, global_step=epoch
)
writer.add_histogram(
f"grad/{name}/b_tohid", self.to_hidden.bias.grad, global_step=epoch
)
if self.n_hidden_layers != 0:
for i, lin in enumerate(self.linears):
writer.add_histogram(
f"weights/{name}/w_lin{i}", lin.weight, global_step=epoch
)
writer.add_histogram(
f"weights/{name}/b_lin{i}", lin.bias, global_step=epoch
)
writer.add_histogram(
f"weights/{name}/w_out", self.out.weight, global_step=epoch
)
writer.add_histogram(
f"weights/{name}/b_out", self.out.bias, global_step=epoch
)
for i, lin in enumerate(self.linears):
writer.add_histogram(
f"grad/{name}/w_lin{i}", lin.weight.grad, global_step=epoch
)
writer.add_histogram(
f"grad/{name}/b_lin{i}", lin.bias.grad, global_step=epoch
)
writer.add_histogram(
f"grad/{name}/w_out", self.out.weight.grad, global_step=epoch
)
writer.add_histogram(
f"grad/{name}/b_out", self.out.bias.grad, global_step=epoch
)
"""
class MLP(nn.Module):
def __init__(self, input_size, output_size, hidden_size=32, **kwargs):
super().__init__()
if isinstance(hidden_size, list):
hidden_size = hidden_size[0]
hidden_size = 2
self.to_hidden = nn.Linear(input_size, hidden_size)
self.out = nn.Linear(hidden_size, output_size)
def forward(self, x):
# flatten to make for normalizing layer => only 2 dim
x, shape = batch_flatten(x)
out = torch.relu(self.to_hidden(x))
out = self.out(out)
out = batch_unflatten(out, shape)
return out
"""
|
decodable_information_bottleneck-main
|
dib/predefined/mlp.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .ib import *
from .img import *
|
decodable_information_bottleneck-main
|
dib/transformers/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
from functools import partial
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from dib.predefined import MLP, WideResNet, get_Cnn
from dib.predefined.helper_layers import get_norm_layer
from dib.utils.helpers import prod
__all__ = ["get_img_encoder"]
logger = logging.getLogger(__name__)
# TODO: CLEAN AND DOCUMENT ALL THIS FILE !!!!
class CNNEncoder(nn.Module):
def __init__(self, x_shape=(1, 32, 32), z_dim=256, **kwargs):
super().__init__()
self.core = get_Cnn(is_flatten=True, **kwargs)(x_shape[0], z_dim)
def forward(self, x):
return self.core(x)
class MLPEncoder(nn.Module):
def __init__(self, x_shape=(1, 32, 32), z_dim=256, **kwargs):
super().__init__()
self.core = MLP(prod(x_shape), z_dim, **kwargs)
def forward(self, x):
x = x.flatten(start_dim=1)
return self.core(x)
class TorchvisionEncoder(nn.Module):
def __init__(
self,
TVM,
x_shape=(1, 32, 32),
n_classes=10,
z_dim=256,
norm_layer=None,
is_resnet_converter=False,
**kwargs,
):
super().__init__()
# make every input to TVM has 3 channels
self.converter = nn.Sequential(
nn.Conv2d(x_shape[0], 3, 3, padding=1),
nn.ReLU(),
nn.Conv2d(3, 3, 3, padding=1),
nn.ReLU(),
)
self.tvm = TVM(
norm_layer=get_norm_layer(norm_layer, dim=2), **kwargs
) # will remove the class in any case
if is_resnet_converter:
self.converter = nn.Identity()
self.tvm.conv1 = nn.Conv2d(
x_shape[0], 64, kernel_size=3, stride=1, padding=1, bias=False
)
self.tvm.maxpool = nn.Identity()
if z_dim == self.tvm.fc.in_features:
self.tvm.fc = nn.Identity()
else:
self.tvm.fc = nn.Linear(self.tvm.fc.in_features, z_dim)
def forward(self, x):
return self.tvm(self.converter(x))
def get_img_encoder(name):
name = name.lower()
if "mlp" in name or "width" in name:
return MLPEncoder
elif "cnn" in name:
return CNNEncoder
elif "resnet18" in name:
return partial(
TorchvisionEncoder,
TVM=torchvision.models.resnet18,
is_resnet_converter=True,
)
elif "resnet34" in name:
return partial(
TorchvisionEncoder,
TVM=torchvision.models.resnet34,
is_resnet_converter=True,
)
elif "resnet50" in name:
return partial(
TorchvisionEncoder,
TVM=torchvision.models.resnet50,
is_resnet_converter=True,
)
elif "resnet101" in name:
return partial(
TorchvisionEncoder,
TVM=torchvision.models.resnet101,
is_resnet_converter=True,
)
elif "wideresnet101" in name:
return partial(
TorchvisionEncoder,
TVM=torchvision.models.wide_resnet101_2,
is_resnet_converter=True,
)
elif "wideresnet" in name:
return partial(TorchvisionEncoder, TVM=WideResNet)
else:
raise ValueError(f"Unkown name={name}")
|
decodable_information_bottleneck-main
|
dib/transformers/img.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import math
import random
from itertools import zip_longest
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy.stats import entropy
from sklearn.metrics import accuracy_score
from skorch.utils import to_numpy
from torch.distributions import Categorical
from torch.distributions.kl import kl_divergence
from torch.nn.parallel import parallel_apply
from dib.predefined import MLP
from dib.utils.distributions import MultivariateNormalDiag, entropy_labels
from dib.utils.helpers import (
BaseRepIthDigits,
Constant,
CrossEntropyLossGeneralize,
Identity,
Nchunk_iterable,
ReturnNotTensor,
extract_target,
get_idx_permuter,
is_sorted,
mean_p_logits,
return_not_tensor,
set_requires_grad,
tmp_seed,
update_dict_copy,
)
from dib.utils.initialization import weights_init
from .dib import IBEncoder
from .helpers import (
BASE_LOG,
CORR_GROUPS,
EPS_MIN,
N_CORR,
NotEnoughHeads,
detach,
mean_p_logits_parallel,
mean_std,
)
from .vib import VIBLoss
__all__ = ["ERMLoss"]
logger = logging.getLogger(__name__)
class ERMLoss(VIBLoss):
"""Empirical risk minimizer Loss.
Parameters
----------
ZYCriterion : nn.Module, optional
Criterion to compute the loss of Q_zy.
map_target_position : dict, optional
Dictionary that maps the type of target (e.g. "index") to its position in the
target.
"""
def forward(self, out, y):
y_pred, z_sample, p_zCx = out
if p_zCx.out is not None:
p_zCx_base = p_zCx.out.base_dist
self._store(
z_norm=z_sample.pow(2).mean(),
z_mean_norm=p_zCx_base.loc.abs().mean(),
z_std=p_zCx_base.scale.mean(),
)
zy_loss = self.compute_zy_loss(y_pred, y)
return zy_loss
|
decodable_information_bottleneck-main
|
dib/transformers/ib/erm.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .dib import *
from .erm import *
from .helpers import *
from .vib import *
|
decodable_information_bottleneck-main
|
dib/transformers/ib/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import copy
import logging
import math
import random
from functools import partial
from itertools import zip_longest
import numpy as np
import sklearn
import torch
import torch.nn as nn
import torch.nn.functional as F
from joblib import Parallel, delayed
from scipy.stats import entropy
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import accuracy_score, log_loss
from skorch.utils import to_numpy
from torch.distributions import Categorical
from torch.distributions.kl import kl_divergence
from torch.nn.parallel import parallel_apply
from dib.predefined import MLP
from dib.utils.distributions import MultivariateNormalDiag, label_distribution
from dib.utils.helpers import (
BaseRepIthDigits,
BatchNorm1dLast,
Constant,
CrossEntropyLossGeneralize,
Identity,
Nchunk_iterable,
ReturnNotTensor,
extract_target,
get_idx_permuter,
is_sorted,
mean_p_logits,
no_grad_modules,
return_not_tensor,
set_seed,
tmp_seed,
update_dict_copy,
wrap_batchnorm,
)
from dib.utils.initialization import weights_init
from .helpers import (
BASE_LOG,
CORR_GROUPS,
EPS_MIN,
N_CORR,
NotEnoughHeads,
SingleInputModuleList,
detach,
mean_p_logits_parallel,
mean_std,
)
try:
import higher
except ImportError:
pass
__all__ = [
"DIBLoss",
"IBEncoder",
"DIBLossAltern",
"DIBLossLinear",
"DIBLossAlternLinear",
"DIBLossAlternLinearExact",
"DIBLossAlternHigher",
"DIBLossZX",
]
logger = logging.getLogger(__name__)
class IBEncoder(nn.Module):
"""General class for *IB Encoders.
Parameters
----------
Encoder : nn.Module
Uninitialized module that takes `x_shape` and `z_dim` as input.
Q : nn.Module
Functional family of the classifier. I.e. for sufficiency.
x_shape : tuple, optional
Size of the inputs.
n_classes : int, optional
Number of output classes.
z_dim : int, optional
Size of the representation.
is_stochastic : bool, optional
Whether to use a stochastic encoder.
n_test_samples : int, optional
Number of samples of z to use if `is_stochastic` and testing.
is_avg_trnsf : bool, optional
Whether to return the average representation or all of them. THe former is
useful for plotting.
kwargs:
Additional arguments to Encoder.
Return
------
if is_transform:
z_sample : torch.tensor, shape = [n_samples, batch_size, z_dim]
else :
y_pred : torch.tensor, shape = [batch_size, n_classes]
z_sample : torch.tensor, shape = [n_samples, batch_size, z_dim]
p_zCx : MultivariateNormalDiag, shape = [batch_size, z_dim].
Distribution of p(z|x) None when testing.
"""
def __init__(
self,
Encoder,
Q,
x_shape=(1, 32, 32),
n_classes=10,
z_dim=256,
is_stochastic=True,
n_test_samples=12,
is_avg_trnsf=False,
is_limit_growth=False,
is_wrap_batchnorm=False,
**kwargs,
):
super().__init__()
self.is_transform = False
self.z_dim = z_dim
self.is_stochastic = is_stochastic
self.n_test_samples = n_test_samples
self._to_plot_activation = {}
self.is_avg_trnsf = is_avg_trnsf
self.n_classes = n_classes
self.is_limit_growth = is_limit_growth
self.is_wrap_batchnorm = is_wrap_batchnorm
self.encoder = Encoder(
x_shape=x_shape, z_dim=z_dim * 2 if is_stochastic else z_dim
)
if self.is_wrap_batchnorm:
self.batch_norm = BatchNorm1dLast(num_features=z_dim, affine=False)
self.Q_zy = Q(z_dim, n_classes)
self.reset_parameters()
def reset_parameters(self):
weights_init(self)
def forward(self, X):
batch_size = X.size(0)
n_samples = 1 if self.training else self.n_test_samples
if self.is_stochastic:
z_suff_stat = self.encoder(X)
z_mean, z_std = z_suff_stat.view(batch_size, -1, 2).unbind(-1)
if self.is_limit_growth:
z_mean = torch.tanh(z_mean)
z_std = torch.sigmoid(z_std)
else:
# -5 as in vib + delta
z_std = EPS_MIN + F.softplus(z_std - 5)
p_zCx = MultivariateNormalDiag(z_mean, z_std)
z_sample = p_zCx.rsample([n_samples])
else:
z_sample = self.encoder(X).unsqueeze(0) # unsqueeze as if 1 sample
p_zCx = None
if self.is_wrap_batchnorm:
z_sample = self.batch_norm(z_sample)
if self.is_transform:
if self.is_avg_trnsf:
return z_sample.mean(0)
else:
return z_sample
y_pred = mean_p_logits(self.Q_zy(z_sample))
self.plot_activation_(dict(y_pred=y_pred, z_sample=z_sample.mean(0)))
# by passes the issue that skorch thinks it's tensor
return y_pred, z_sample, return_not_tensor(p_zCx)
def plot_activation_(self, activations):
for k, v in activations.items():
# opeartion over batchs
v = to_numpy(v)
self._to_plot_activation[k + "_mean"] = v.mean(0)
self._to_plot_activation[k + "_meanabs"] = np.abs(v).mean(0)
def tensorboard(self, writer, epoch, mode="on_grad_computed"):
name = type(self).__name__
if mode == "on_grad_computed":
for k, v in self._to_plot_activation.items():
writer.add_histogram(
f"activations/{name}/" + k, v, global_step=epoch)
self._to_plot_activation = dict()
class DIBLoss(nn.Module):
"""DIB Loss.
Parameters
----------
Q : nn.Module
Functional family for minimality.
n_per_target : dict
Number of examples for each target.
beta : float or callable, optional
Regularization weight. If callable, should return a float given `is_training`.
Importantly this only changes the gradients but not the model selection / loss plotting.
n_per_head : list, optional
Number of resampling of optimal nuisance. In practice what it is permutting the indices and
then applying nuisance.
n_classes : int, optional
Number of output classes.
z_dim : int, optional
Size of the representation.
conditional : {None, "H_Q[X|Z,Y]", "H_Q[X|Z]-H_Q[Y|Z]", "H_Q'[X|Z,Y]"}
If None uses DIB. If `"H_Q[X|Z,Y]"` uses a different head for depending on the label, this is
the most correct version of conditional IB but is computationally intensive. If `"H_Q[X|Z,Y]"`
approximate the previous method by giving the label Y as input to the heads, as a result the
heads have architecture Q' instead of the desired Q. `"H_Q[X|Z]-H_Q[Y|Z]"` index all X
differently for each labels, this is is only correct if all
possible labels are independent.
is_optimal_nuisance : bool, optional
Whether to use optimal nuisance instead of randomly hashed ones. I.e. uses the representation
of X index in base |Y|. The number of heads will be the same regardless (to enable comparison).
is_same_indices : bool, optional
Whether to use the same indices for each `n_per_head`.
ZYCriterion : nn.Module, optional
Criterion to compute the loss of Q_zy.
map_target_position : dict, optional
Dictionary that maps the type of target (e.g. "index") to its position in the
target.
warm_Q_zx : int, optional
Number of steps where warming up Q_zx (i.e. only backprop through them).
z_norm_reg : float, optional
Regularizer on the mean of the squared of the representations (when it is larger than 1).
Note that we also use a hard boundary (factor of 10) when the norm mean squared us larger
than 10. This is crucial to get good results : adversarial training makes the representation
go to infinity without that.
seed : int, optional
Random seed.
"""
def __init__(
self,
Q,
n_per_target,
beta=1,
n_per_head=3,
n_classes=10,
z_dim=128,
conditional=None,
seed=123,
is_optimal_nuisance=True,
is_same_indices=False,
ZYCriterion=CrossEntropyLossGeneralize,
map_target_position={"target": 0, "index": 1},
warm_Q_zx=0,
z_norm_reg=0.0,
weight_kl=None, # dev
is_zx_only=False, # DEV
is_use_y_as_n=False, # DEV
threshold_suff=float("inf"), # DEV
is_wrap_batchnorm=False,
**kwargs,
):
super().__init__()
self.n_per_target = n_per_target
self.beta = beta if callable(beta) else Constant(beta)
self.n_per_head = n_per_head
self.n_classes = n_classes
self.z_dim = z_dim
self.conditional = conditional
self.seed = seed
self.is_optimal_nuisance = is_optimal_nuisance
self.is_same_indices = is_same_indices
self.map_target_position = map_target_position
self.is_zx_only = is_zx_only # DEV
self.is_use_y_as_n = is_use_y_as_n # DEV
self.threshold_suff = float(threshold_suff) # DEV
self.warm_Q_zx = warm_Q_zx
self.z_norm_reg = z_norm_reg
self.weight_kl = weight_kl
self.is_wrap_batchnorm = is_wrap_batchnorm
# all the Q heads
self.n_heads = self._get_n_heads()
if self.is_wrap_batchnorm:
Q = wrap_batchnorm(Q)
self.Q_zx = self.get_Q_zx(Q)
self.zy_criterion = ZYCriterion()
self.to_store = dict()
self.nuisances = []
self.precompute_random_labelings_()
self.compute_entropies_()
self.compute_probabilities()
self.reset_parameters()
set_seed(
self.seed
) # ensures same seed as VIB (different due to init of heads)
def get_Q_zx_helper(self, Q):
"""Return one set of classifiers from Z to {\Tilde{Y}_i}_i."""
input_dim = (
(self.z_dim +
1) if self.conditional == "H_Q'[X|Z,Y]" else self.z_dim
)
return nn.ModuleList(
[Q(input_dim, self.n_classes) for _ in range(self.n_heads)]
)
def get_Q_zx(self, Q):
if self.conditional == "H_Q[X|Z,Y]":
# different set of optimal (arg infimum) classifiers for each labels
return nn.ModuleList(
[self.get_Q_zx_helper(Q) for _ in range(self.n_classes)]
)
else:
# same set of optimal classifiers regardless of label
return self.get_Q_zx_helper(Q)
def idcs_to_baseK_nuisance(self, i, idcs, base, n_nuisance):
"""Return the ith nuisance, using the base |Y| decomposition. Computations in numpy"""
if not isinstance(self.nuisances, BaseRepIthDigits):
self.nuisances = BaseRepIthDigits(base)
self._max_idx = base ** n_nuisance
if not np.all(idcs < self._max_idx):
raise NotEnoughHeads(
f"Max idx is base^heads={base}^{n_nuisance}={self._max_idx}. These idcs do not satisfy that : {idcs[(idcs >= self._max_idx)]}"
)
# ith base B expansion of the indices. E.g. for [494,7,58] and i=1 and base=10 would return [9,0,5]
return self.nuisances(idcs, i)
def _store(self, **to_store):
for k, v in to_store.items():
# value and count
if isinstance(v, torch.Tensor):
v = v.item()
self.to_store[k] = self.to_store.get(k, np.array([0.0, 0])) + np.array(
[v, 1]
)
def precompute_random_labelings_(self):
"""Precompute the randomization of indices for different random labelings."""
if not is_sorted([int(k) for k in self.n_per_target.keys()]):
raise ValueError(
f"The keys of `n_per_target` need to be sorted but ={self.n_per_target.keys()}"
)
if self.conditional is None or not self.is_optimal_nuisance:
n_idcs = sum(self.n_per_target.values())
else:
n_idcs = list(self.n_per_target.values())
n_permuters = self.n_per_head if self.is_optimal_nuisance else self.n_heads
# precompute the permutations of indices
if self.is_same_indices:
self._rand_indcs = [get_idx_permuter(
n_idcs, seed=self.seed)] * n_permuters
else:
self._rand_indcs = [
get_idx_permuter(n_idcs, seed=self.seed + i) for i in range(n_permuters)
]
def _get_n_heads(self):
"""Compute the number of heads that will be needed."""
if self.conditional in ["H_Q[X|Z,Y]", "H_Q[X|Z]-H_Q[Y|Z]", "H_Q'[X|Z,Y]"]:
# if using concateate then will use the same heads for each labels but concat the
# to the representation to make sure that you don't remove label information from
# the representation
n_to_cover = max(self.n_per_target.values())
elif self.conditional is None:
# if not conditional then you need to cover all of |X| regardless of how many fall in each labels
n_to_cover = sum(self.n_per_target.values())
else:
raise ValueError(f"Unkown conditional={self.conditional}")
self.n_covering = math.ceil(math.log(n_to_cover, self.n_classes))
n_heads = self.n_per_head * self.n_covering
logger.info(f"nheads: {n_heads}")
return n_heads
def compute_probabilities(self):
n_per_target = np.array(list(self.n_per_target.values()))
n_idcs = n_per_target.sum()
# list of p_{\Tilde{Y}_i}
self.p_ni = []
for i in range(self.n_heads):
n_i = self.get_ith_nuisance(i, torch.arange(n_idcs).long())
self.p_ni.append(label_distribution(n_i, self.n_classes))
# list of list of p_{\Tilde{Y}_i|Y}
self.p_niCY = []
for n_for_target in n_per_target:
p_niCy = [] # for a specific target
for i in range(self.n_heads):
n_i = self.get_ith_nuisance(
i, torch.arange(n_for_target).long())
p_niCy.append(label_distribution(n_i, self.n_classes))
self.p_niCY.append(p_niCy)
def compute_entropies_(self):
# all of these assume uniform distribution
n_per_target = np.array(list(self.n_per_target.values()))
n_idcs = n_per_target.sum()
self.H_x = math.log(n_idcs, BASE_LOG)
self.H_y = entropy(n_per_target, base=BASE_LOG)
self.H_yCx = sum(
math.log(N, BASE_LOG) * N / n_per_target.sum() for N in n_per_target
)
def reset_parameters(self):
weights_init(self)
self._counter_warm_Q_zx = 0
def get_ith_nuisance(self, i, x_idx, is_torch=True):
if not self.is_optimal_nuisance:
if is_torch:
# making sure you are on correct device
self._rand_indcs[i] = self._rand_indcs[i].to(x_idx.device)
n_i = self._rand_indcs[i][x_idx] % self.n_classes
return n_i
# i_rand_idx : which n_per_head (i.e. which group of g=random index). => if n_per_head `n_per_head=1` then `i_rand_idx=0`
# i_in_rand_idx : index in the i_mc group => if n_per_head `n_per_head=1` then `i_in_rand_idx=i`
i_rand_idx, i_in_rand_idx = divmod(i, self.n_covering)
if is_torch:
# making sure you are on correct device
self._rand_indcs[i_rand_idx] = self._rand_indcs[i_rand_idx].to(
x_idx.device)
# to have a better approximation of H_Q[X|Z] we actually compute a (MC approx.)
# expectation over all possible permutation of indices. Indeed, the indices could
# have been given differently which can change the optimization process. Each mc approx
# corresponds to an other possible such index labeling
if max(x_idx) >= len(self._rand_indcs[i_rand_idx]):
raise NotEnoughHeads(
f"max(x_idx)={max(x_idx)}>{len(self._rand_indcs[i_rand_idx])}=len(self._rand_indcs[i_rand_idx])"
)
permuted_x_idx = self._rand_indcs[i_rand_idx][x_idx]
n_i = self.idcs_to_baseK_nuisance(
i_in_rand_idx, to_numpy(
permuted_x_idx), self.n_classes, self.n_covering
)
if is_torch:
n_i = torch.from_numpy(n_i).to(x_idx.device)
return n_i
def compute_zy_loss(self, y_pred, targets):
"""Compute loss for the classifier Z -> Y."""
# H_Q[Y|Z]
H_Q_yCz = self.zy_criterion(y_pred, targets) / math.log(BASE_LOG)
#! H[Y] is the training one (even at test time)
# DI_Q[Y;Z] = H[Y] - H_Q[Y|Z]
DIQ_yz = self.H_y - H_Q_yCz
self._store(H_Q_yCz=H_Q_yCz, DIQ_yz=DIQ_yz)
return H_Q_yCz
def _compute_H_Q_nCzy(self, z_sample, targets, is_cap):
"""Compute \sum_i \sum_y H_Q[\Tilde{Y}_i|z,y]"""
ys = extract_target(targets, self.map_target_position)
idcs = targets[self.map_target_position["index"]]
H_Q_nCzy = 0
DI_Q_nCzy = 0
heads_delta_acc_nCy = 0
present_labels = ys.unique()
for present_label in present_labels:
# select everything based on the label => conditional prediction
selector_cond = ys == present_label
x_idcs_cond = idcs[selector_cond]
z_sample_cond = z_sample[:, selector_cond, :]
Q_zx_cond = self.Q_zx[present_label]
(
H_Q_nCzy_cond,
heads_delta_acc_n_cond,
DI_Q_nCzy_cond,
) = self._compute_H_Q_nCz(
z_sample_cond,
x_idcs_cond,
Q_zx_cond,
is_cap,
targets[self.map_target_position["target"]],
)
H_Q_nCzy = H_Q_nCzy + H_Q_nCzy_cond / len(present_labels)
heads_acc_nCy = heads_delta_acc_nCy + heads_delta_acc_n_cond / len(
present_labels
)
DI_Q_nCzy = DI_Q_nCzy + DI_Q_nCzy_cond / len(present_labels)
return H_Q_nCzy, heads_delta_acc_nCy, DI_Q_nCzy
def _compute_H_Q_ni(self, n_i, i, q_niCz):
"""Estimate the worst case cross entropy (i.e. predicting with the marginal)."""
self.p_ni[i].logits = self.p_ni[i].logits.to(n_i.device)
p_ni = self.p_ni[i].logits.repeat(len(n_i), 1)
H_Q_ni = F.cross_entropy(p_ni, n_i) / math.log(BASE_LOG)
# also return the accuracy of marginal
marginal_acc_ni = accuracy_score(n_i.cpu(), p_ni.argmax(-1).cpu())
return H_Q_ni, marginal_acc_ni
def batch_predict_heads(self, z_sample, Q_zx):
if len(Q_zx) == 0:
return []
#! NOT IN PARALLEL BECAUSE WAS NOT WORKING WELL : still has to see why can't parallelize
return [mean_p_logits(Q_zxi(z_sample)) for Q_zxi in Q_zx]
def _compute_H_Q_nCz(self, z_sample, x_idcs, Q_zx, is_cap, y):
"""Compute \sum_i H_Q[\Tilde{Y}_i|z]"""
# for all heads, predict, and average across num. samples
q_nCz = self.batch_predict_heads(z_sample, Q_zx)
H_Q_nCz = 0
DI_Q_nCz = 0
heads_delta_acc_n = 0
for i, q_niCz in enumerate(q_nCz):
n_i = self.get_ith_nuisance(i, x_idcs)
if self.is_use_y_as_n:
n_i = y
# H_Q[\Tilde{Y}_i|Z]
H_Q_niCz = F.cross_entropy(q_niCz, n_i) / math.log(BASE_LOG)
# H_Q[\Tilde{Y}_i]
H_Q_ni, marginal_acc_ni = self._compute_H_Q_ni(n_i, i, q_niCz)
if is_cap:
# in case your loss is worst than marginal, then don't backprop through encoder
# but still improve the head (because it means that the internal optim is not correct)
# accessorily this will also ensure positivity of estimated decodable information
if H_Q_niCz > H_Q_ni:
H_Q_niCz = H_Q_niCz * 0 + H_Q_ni
# DI_Q[\Tilde{Y}_i <- Z] = H_Q_ni - H_Q_niCz
DI_Q_niz = H_Q_ni - H_Q_niCz
# H_Q[\Tilde{Y}|Z] = \sum_i H_Q[\Tilde{Y}_i|Z]
# DI_Q[\Tilde{Y} <- Z] = \sum_i DI_Q[\Tilde{Y}_i <- Z]
# only divide by self.n_per_head because want to sum all heads besides the n_per_head should avg
H_Q_nCz = H_Q_nCz + H_Q_niCz / self.n_per_head
DI_Q_nCz = DI_Q_nCz + DI_Q_niz / self.n_per_head
# only save delta accuracy with marginal
heads_acc_ni = accuracy_score(n_i.cpu(), q_niCz.argmax(-1).cpu())
heads_delta_acc_ni = heads_acc_ni - marginal_acc_ni
heads_delta_acc_n = heads_delta_acc_n + \
heads_delta_acc_ni / len(q_nCz)
return H_Q_nCz, heads_delta_acc_n, DI_Q_nCz
def compute_zx_loss_helper(self, z_sample, y, is_cap, is_store=True):
if self.conditional == "H_Q[X|Z,Y]":
H_loss, head_delta_acc, DI_loss = self._compute_H_Q_nCzy(
z_sample, y, is_cap
)
else:
# if self.conditional == "H_Q'[X|Z,Y]" actually computing H_Q_nCzy
H_loss, head_delta_acc, DI_loss = self._compute_H_Q_nCz(
z_sample,
y[self.map_target_position["index"]],
self.Q_zx,
is_cap,
y[self.map_target_position["target"]], # DEV
)
if is_store:
# storing for plots
if self.conditional is None:
# H_Q[X|Z] := \sum_i H_Q[\Tilde{Y}_i|Z]
# I_Q[X<-Z] := \sum_i H[\Tilde{Y}_i] - H_Q[\Tilde{Y}_i|Z]
self._store(H_Q_xCz=H_loss,
h_delta_acc=head_delta_acc, DIQ_xz=DI_loss)
elif "[X|Z,Y]" in self.conditional:
# H_Q[X|Z,Y] := \sum_y \sum_{\Tilde{Y}_i \neq Y} H_Q[\Tilde{Y}_i|Z,y]
# I_Q[X<-Z|Y] := \sum_{\Tilde{Y}_i \neq Y} H[\Tilde{Y}_i | Y] - H_Q[X|Z,Y]
self._store(
H_Q_xCzy=H_loss, h_delta_acc=head_delta_acc, DIQ_xzCy=DI_loss
)
elif self.conditional == "H_Q[X|Z]-H_Q[Y|Z]":
# H_Q[X|Z] - H_Q[Y|Z] := \sum_{\Tilde{Y}_i \neq Y} H_Q[\Tilde{Y}_i|Z]
# I_Q[X<-Z] - I_Q[Y<-Z] := \sum_{\Tilde{Y}_i \neq Y} H[\Tilde{Y}_i] - H_Q[\Tilde{Y}_i|Z]
self._store(
d_H_Q_xCz=H_loss, h_delta_acc=head_delta_acc, d_DIQ_xz=DI_loss
)
return H_loss
def compute_zx_loss_encoder(self, z_sample, targets):
"""Compute all losses for the encoder Z -> X."""
curr_beta = self.beta(self.training) if self.beta(
False) is not None else 0
# capping : due to minimax one solution is to get infinitely bad (no need if no minimax)
is_cap = True if curr_beta >= 0 else False
H_loss = self.compute_zx_loss_helper(
z_sample, targets, is_cap=is_cap, is_store=True
)
# the representation should do poorly on predicting the base expansion of the index
# Could use gradient reversal layer, but that would not allow the clamping to random loss
zx_loss = -H_loss
# add some regularization if mean square > 1. If not it explodes due to adversarial training
z_norm = z_sample.pow(2).mean()
if curr_beta >= 0 and self.z_norm_reg > 0 and z_norm > 1:
z_norm_reg = self.z_norm_reg
if z_norm > 10: # hard boudnary at 10
z_norm_reg = 100 * z_norm_reg
# only if positive beta because this causes norm to explode
zx_loss = zx_loss + z_norm_reg * z_norm
zx_loss = curr_beta * zx_loss
return zx_loss
def compute_zx_loss(self, z_sample, targets):
"""Compute all losses Z -> X, for encoder and for the heads."""
zx_loss_heads = self.compute_zx_loss_helper(
z_sample.detach(), # don't backprop through encoder
targets,
is_cap=False,
is_store=False,
)
with no_grad_modules([self.Q_zx]): # don't backprop through heads
zx_loss_enc = self.compute_zx_loss_encoder(z_sample, targets)
if self._counter_warm_Q_zx < self.warm_Q_zx:
zx_loss_enc = detach(zx_loss_enc) # for logging you still want
# - detach to make sure that gradients flow but loss does not cancel when plotting
# also note that `zx_loss_heads` IS NOT SCALED BY BETA
zx_loss = zx_loss_enc + zx_loss_heads - detach(zx_loss_heads)
return zx_loss
def forward(self, out, targets):
if self.training:
self._counter_warm_Q_zx += 1
y_pred, z_sample, p_zCx = out
if p_zCx.out is not None:
p_zCx_base = p_zCx.out.base_dist
# z_norm : elementwise square, z_mean_norm : mean of absolute val, z_std : mean of standard dev
self._store(
z_norm=z_sample.pow(2).mean(),
z_mean_norm=p_zCx_base.loc.abs().mean(),
z_std=p_zCx_base.scale.mean(),
)
if self.conditional == "H_Q'[X|Z,Y]":
target = (
extract_target(targets, self.map_target_position)
.unsqueeze(0)
.unsqueeze(-1)
.float()
)
target = torch.repeat_interleave(target, z_sample.size(0), dim=0)
z_sample = torch.cat([z_sample, target], dim=-1)
try:
zx_loss = self.compute_zx_loss(z_sample, targets)
except NotEnoughHeads as e:
# if not training then don't raise exception (because the indexing might be off in which
# case your predictor cannot comoute zx_loss). But you don't want to never compute this
# loss as for evaluation we give the training data but self.training=False
if self.training:
raise e
zx_loss = 0
if self.weight_kl is not None:
p_zCx = p_zCx.out
mean_0 = torch.zeros_like(p_zCx.base_dist.loc)
std_1 = torch.ones_like(p_zCx.base_dist.scale)
p_z = MultivariateNormalDiag(mean_0, std_1)
kl = kl_divergence(p_zCx, p_z).mean(0) / math.log(BASE_LOG)
zx_loss = zx_loss + self.weight_kl * kl
zy_loss = self.compute_zy_loss(y_pred, targets)
if zy_loss > self.threshold_suff: # DEV
zx_loss = zx_loss * 0 + detach(zx_loss)
if self._counter_warm_Q_zx <= self.warm_Q_zx:
# still return loss but no grad
zy_loss = zy_loss * 0 + detach(zy_loss)
if self.is_zx_only: # DEV
zy_loss = 0 * zy_loss
self._store(aux_loss=zx_loss)
if not self.training:
# when evaluating the loss should be log likelihood for checkpointing
return zy_loss
return zy_loss + zx_loss
class DIBLossZX(DIBLoss):
def forward(self, z_sample, targets):
z_sample = z_sample.unsqueeze(
0
) # when only computing DIQ the samples will be squeezed
if self.conditional == "H_Q'[X|Z,Y]":
target = (
extract_target(targets, self.map_target_position)
.unsqueeze(0)
.unsqueeze(-1)
.float()
)
target = torch.repeat_interleave(target, z_sample.size(0), dim=0)
z_sample = torch.cat([z_sample, target], dim=-1)
try:
zx_loss = self.compute_zx_loss(z_sample, targets)
except NotEnoughHeads as e:
# if not training then don't raise exception (because the indexing might be off in which
# case your predictor cannot comoute zx_loss). But you don't want to never compute this
# loss as for evaluation we give the training data but self.training=False
if self.training:
raise e
zx_loss = 0
self._store(aux_loss=zx_loss)
return zx_loss
class DIBLossAltern(DIBLoss):
def __init__(
self,
*args,
Optimizer=partial(torch.optim.Adam, lr=0.001),
altern_minimax=3,
**kwargs,
):
super().__init__(*args, **kwargs)
self.optimizer = Optimizer(self.Q_zx.parameters())
self.altern_minimax = altern_minimax
def optimize_heads(self, z_sample, targets):
z_sample = z_sample.detach() # don't backprop through encoder
def closure():
self.optimizer.zero_grad()
zx_loss = self.compute_zx_loss_helper(
z_sample, targets, is_cap=False, is_store=False
)
zx_loss.backward()
return zx_loss
for i in range(self.altern_minimax):
self.optimizer.step(closure)
def compute_zx_loss(self, z_sample, targets):
"""Compute all losses Z -> X, for encoder and for the heads."""
if not self.training:
# ensure that no leeking of test information when training on test
Q_zx_old = copy.deepcopy(self.Q_zx)
try:
# make sure grad enabled even when testing (better estimation of H_Q[X|Z])
with torch.enable_grad():
self.optimize_heads(z_sample, targets)
with no_grad_modules([self.Q_zx]):
zx_loss_enc = self.compute_zx_loss_encoder(z_sample, targets)
finally:
if not self.training:
self.Q_zx = Q_zx_old
if self._counter_warm_Q_zx <= self.warm_Q_zx:
zx_loss_enc = zx_loss_enc * 0 + detach(zx_loss_enc)
return zx_loss_enc
class DIBLossAlternHigher(DIBLoss):
def __init__(
self,
*args,
Optimizer=partial(torch.optim.Adam, lr=0.001),
altern_minimax=3,
**kwargs,
):
super().__init__(*args, **kwargs)
self.Optimizer = Optimizer
self.altern_minimax = altern_minimax
def batch_predict_heads(self, z_sample, Q_zx):
if len(Q_zx) == 0:
return []
return [mean_p_logits(pred_i) for pred_i in Q_zx(z_sample)]
def get_Q_zx_helper(self, Q):
"""Return one set of classifiers from Z to {\Tilde{Y}_i}_i."""
input_dim = (
(self.z_dim +
1) if self.conditional == "H_Q'[X|Z,Y]" else self.z_dim
)
return SingleInputModuleList(
[Q(input_dim, self.n_classes) for _ in range(self.n_heads)]
)
def get_Q_zx(self, Q):
if self.conditional == "H_Q[X|Z,Y]":
# different set of optimal (arg infimum) classifiers for each labels
return SingleInputModuleList(
[self.get_Q_zx_helper(Q) for _ in range(self.n_classes)]
)
else:
# same set of optimal classifiers regardless of label
return self.get_Q_zx_helper(Q)
def compute_zx_loss(self, z_sample, targets):
"""Compute all losses Z -> X, for encoder and for the heads."""
Q_zx_curr, self.Q_zx = self.Q_zx, None
inner_opt = self.Optimizer(Q_zx_curr.parameters())
try:
with higher.innerloop_ctx(
Q_zx_curr, inner_opt, track_higher_grads=self.training,
) as (fnet, diffopt):
self.Q_zx = fnet # setting temporary attribute for computations
# make sure grad enabled even when testing (better estimation of H_Q[X|Z])
with torch.enable_grad():
# Take a few gradient steps to find good heads
for _ in range(self.altern_minimax):
zx_loss = self.compute_zx_loss_helper(
z_sample, targets, is_cap=False, is_store=False
)
diffopt.step(zx_loss)
zx_loss_enc = self.compute_zx_loss_encoder(z_sample, targets)
if self.training:
# reloading your weights so that you can do warm start for next step
# not when testing (avoiding leakage of test data)
Q_zx_curr.load_state_dict(fnet.state_dict())
finally:
self.Q_zx = Q_zx_curr
if self._counter_warm_Q_zx <= self.warm_Q_zx:
zx_loss_enc = zx_loss_enc * 0 + detach(zx_loss_enc)
return zx_loss_enc
class DIBLossLinear(DIBLoss):
def batch_predict_heads(self, z_sample, Q_zx):
outs = Q_zx(z_sample)
return [mean_p_logits(el) for el in torch.chunk(outs, self.n_heads, dim=-1)]
def get_Q_zx_helper(self, Q):
"""Return one set of classifiers from Z to {\Tilde{Y}_i}_i."""
input_dim = (
(self.z_dim +
1) if self.conditional == "H_Q'[X|Z,Y]" else self.z_dim
)
return nn.Linear(input_dim, self.n_classes * self.n_heads)
class DIBLossAlternLinear(DIBLossAltern):
batch_predict_heads = DIBLossLinear.batch_predict_heads
get_Q_zx_helper = DIBLossLinear.get_Q_zx_helper
class DIBLossAlternLinearExact(DIBLoss):
def __init__(self, *args, is_Q_zy=False, seed=123, **kwargs):
self.Q = partial(
SGDClassifier, loss="log", random_state=seed, warm_start=True, alpha=0.01,
) # small regularization for stability
super().__init__(*args, seed=seed, **kwargs)
def predict_head(self, z_sample, Q_z):
W = torch.from_numpy(Q_z.coef_).float().to(z_sample.device).T
b = torch.from_numpy(Q_z.intercept_).float().to(z_sample.device)
return z_sample @ W + b
def batch_predict_heads(self, z_sample, Q_zx):
return [mean_p_logits(self.predict_head(z_sample, Q_zxi)) for Q_zxi in Q_zx]
def get_Q_zx(self, Q):
if self.conditional == "H_Q[X|Z,Y]":
# different set of optimal (arg infimum) classifiers for each labels
return [self.get_Q_zx_helper(Q) for _ in range(self.n_classes)]
else:
# same set of optimal classifiers regardless of label
return self.get_Q_zx_helper(Q)
def get_Q_zx_helper(self, Q):
"""Return one set of classifiers from Z to {\Tilde{Y}_i}_i."""
return [self.Q() for _ in range(self.n_heads)]
def optimize_heads(self, z_sample, targets):
z_sample = np.squeeze(
to_numpy(z_sample).astype(np.float64), axis=0
) # squeeze sample (assume one during training)
x_idcs = to_numpy(targets[self.map_target_position["index"]])
for i, Q_zxi in enumerate(self.Q_zx):
n_i = self.get_ith_nuisance(i, x_idcs, is_torch=False)
if len(np.unique(n_i)) > 1:
# if not error + it's useless
self.Q_zx[i] = Q_zxi.fit(z_sample, n_i)
def compute_zx_loss(self, z_sample, targets):
"""Compute all losses Z -> X, for encoder and for the heads."""
if self.training:
self.optimize_heads(z_sample, targets)
zx_loss_enc = self.compute_zx_loss_encoder(z_sample, targets)
return zx_loss_enc
|
decodable_information_bottleneck-main
|
dib/transformers/ib/dib.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import math
import random
from itertools import zip_longest
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy.stats import entropy
from sklearn.metrics import accuracy_score
from skorch.utils import to_numpy
from torch.distributions import Categorical
from torch.distributions.kl import kl_divergence
from torch.nn.parallel import parallel_apply
from dib.predefined import MLP
from dib.utils.distributions import MultivariateNormalDiag
from dib.utils.helpers import (
BaseRepIthDigits,
Constant,
Identity,
Nchunk_iterable,
ReturnNotTensor,
get_idx_permuter,
is_sorted,
mean_p_logits,
return_not_tensor,
set_requires_grad,
set_seed,
tmp_seed,
update_dict_copy,
)
from dib.utils.initialization import weights_init
from .dib import DIBLoss, IBEncoder
from .helpers import BASE_LOG, EPS_MIN
__all__ = ["VIBLoss"]
logger = logging.getLogger(__name__)
class VIBLoss(nn.Module):
"""VIB Loss.
Parameters
----------
beta : float or callable, optional
Regularization weight. If callable, should return a float given `is_training`.
Importanlty this only changes the gradients but not the model selection / loss plotting.
This is 1000x the multiple of beta in usual VIB (to make it comparable with DIB).
n_per_target : dict
Number of examples for each target.
ZYCriterion : nn.Module, optional
Criterion to compute the loss of Q_zy.
seed : int, optional
Random seed.
"""
def __init__(
self, n_per_target, beta=1, ZYCriterion=nn.CrossEntropyLoss, seed=123, **kwargs
):
super().__init__()
self.n_per_target = n_per_target
self.beta = beta if callable(beta) else Constant(beta)
self.to_store = dict()
self.zy_criterion = ZYCriterion()
self.compute_entropies_()
self._scale_beta = 1e-3
self.seed = seed
set_seed(self.seed) # ensure seed is same as for DIB
compute_zy_loss = DIBLoss.compute_zy_loss
_store = DIBLoss._store
def compute_entropies_(self):
# all of these assume uniform distribution
n_per_target = np.array(list(self.n_per_target.values()))
self.H_x = math.log(n_per_target.sum(), BASE_LOG)
self.H_y = entropy(n_per_target, base=BASE_LOG)
self.H_xCy = sum(
math.log(N, BASE_LOG) * N / n_per_target.sum() for N in n_per_target
)
def compute_z_loss(self, p_zCx):
"""Compute loss of Z."""
# H[Z|X]
H_zCx = p_zCx.entropy().mean(0) / math.log(BASE_LOG)
mean_0 = torch.zeros_like(p_zCx.base_dist.loc)
std_1 = torch.ones_like(p_zCx.base_dist.scale)
p_z = MultivariateNormalDiag(mean_0, std_1)
kl = kl_divergence(p_zCx, p_z).mean(0) / math.log(BASE_LOG)
# I[Z,X] \approx KL[p(Z|x) || r(z)]
# would be equal if r(z) (the prior) was replaced with the marginal p(z)
I_xz = kl
self._store(H_zCx=H_zCx, I_xz=I_xz)
curr_beta = self.beta(self.training) if self.beta(
False) is not None else 0
curr_beta = self._scale_beta * curr_beta
return curr_beta * I_xz
def forward(self, out, y):
#! dirty trick to get back the non tensor outputs
out = [el.out if isinstance(el, ReturnNotTensor) else el for el in out]
y_pred, z_sample, p_zCx = out
p_zCx_base = p_zCx.base_dist
self._store(
z_norm=z_sample.pow(2).mean(),
z_mean_norm=p_zCx_base.loc.abs().mean(),
z_std=p_zCx_base.scale.mean(),
)
z_loss = self.compute_z_loss(p_zCx)
zy_loss = self.compute_zy_loss(y_pred, y)
self._store(aux_loss=z_loss)
return zy_loss + z_loss
|
decodable_information_bottleneck-main
|
dib/transformers/ib/vib.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import numpy as np
import torch
from dib.utils.helpers import mean_p_logits
__all__ = ["BASE_LOG", "N_CORR"]
logger = logging.getLogger(__name__)
EPS_MIN = 1e-7
BASE_LOG = 2 # to convert to understandable measure of information
N_CORR = 3
CORR_GROUPS = ["lin", "q", "hid16"] # "lay3", "hid4lay1"
def mean_std(arr):
if len(arr) == 0:
return 0, 0
if len(arr) == 1:
return arr[0], 1
if isinstance(arr[0], torch.Tensor):
arr = torch.stack(arr, 0)
return arr.mean(), arr.std()
mean = np.mean(arr)
std = np.std(arr, ddof=1)
return mean, std
class NotEnoughHeads(Exception):
"""Raised when the input value is too small"""
pass
def detach(x):
try:
return x.detach()
except AttributeError:
return x
def mean_p_logits_parallel(heads, X):
"""
Compute all the heads in parallel and take the average across n samples in probablity space.
"""
if len(heads) == 0:
return []
#! NOT IN PARALLEL BECAUSE WAS NOT WORKING WELL : still has to see why can't parallelize
# in a single GPU
return [mean_p_logits(head(X)) for head in heads]
# return [mean_p_logits(out) for out in parallel_apply(heads, [X] * len(heads))]
class SingleInputModuleList(torch.nn.ModuleList):
"""Wrapper around `ModuleList` which takes a single input (i.e. has a forward).
Useful for `higher` monkey patching, which doesn't work with ModuleList."""
def forward(self, inp):
return [m(inp) for m in self]
|
decodable_information_bottleneck-main
|
dib/transformers/ib/helpers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .mcclf import *
|
decodable_information_bottleneck-main
|
dib/classifiers/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import numpy as np
import torch
import torch.nn as nn
from dib.utils.helpers import mean_p_logits
__all__ = ["MCTrnsfClassifier"]
class MCTrnsfClassifier(nn.Module):
"""Class that merges a pretrained stochastic transformer with a classifier. It does
this by sampling multiple times from the transformer and passing it to the classifier.
Parameters
----------
transformer : nn.Module
Transformer that return the representation. It should have a property `is_transform.`
Classifier : nn.Module
Uninitialized classifier.
z_dim : int, optional
Output size of the transformer (input to classifier).
n_classes : int, optional
Number of output classes.
n_test_samples : int, optional
Number of Monte Carlo samples from the transformer during test. The transformer should have
an attribute `n_test_samples` and return all the samples in the first dimension (before batch).
is_freeze_transformer : bool, optional
Whether to freeze the transformer or train it.
kwargs : dict, optional
Additional arguments to the classifier.
"""
def __init__(
self,
transformer,
Classifier,
z_dim,
n_classes,
n_test_samples=1,
is_freeze_transformer=True,
**kwargs
):
super().__init__()
self.transformer = transformer
self.is_freeze_transformer = is_freeze_transformer
self.clf = Classifier(z_dim, n_classes, **kwargs)
self.transformer.n_test_samples = n_test_samples
def forward(self, X):
self.transformer.is_transform = True
if self.is_freeze_transformer:
with torch.no_grad():
Z = self.transformer(X)
else:
Z = self.transformer(X)
out = mean_p_logits(self.clf(Z)) # average over samples
return out
|
decodable_information_bottleneck-main
|
dib/classifiers/mcclf.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .data import *
from .evaluate import *
from .train import *
from .visualize import *
|
decodable_information_bottleneck-main
|
utils/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import os
import shutil
import skorch
import torch
from skorch.callbacks import EarlyStopping, LoadInitState, ProgressBar
from skorch.helper import predefined_split
from dib.training.helpers import FixRandomSeed
from dib.utils.helpers import set_seed
from .helpers import TensorBoard, clean_end_run, get_checkpoint
try:
from torch.utils.tensorboard import SummaryWriter
except ImportError:
pass
__all__ = ["train_load"]
MOD_SUMM_FILENAME = "model_summary.txt"
logger = logging.getLogger(__name__)
def train_load(
Model,
datasets,
chckpnt_dirnames,
is_train=True,
is_continue_train=False,
is_continue_best=False,
patience=None,
is_progressbar=False,
checkpoint_epochs=None,
load_epoch=None,
tensorboard_dir=None,
seed=123,
device="cuda" if torch.cuda.is_available else "cpu",
callbacks=[],
clean_after_run="training",
monitor_best="valid_loss_best",
is_load_criterion=True, # DEV,
is_return_init=False,
**kwargs,
):
"""Train or load the model.
Parameters
----------
Model : sklearn.base.BaseEstimator
Uninitialized model to train.
datasets : dictionary of torch.utils.data.Dataset
Dictionary of the `"train"`, `"valid"`, and `"test"`.
chckpnt_dirnames : list of str, optional
Directories where checkpoints will be saved.
patience : int, optional
Patience for early stopping. Only if given a a validation set.
is_train : bool, optional
Whether to train rather than load a pretrained model. If False, reverts the order of
chckpnt_dirnames. Such that loads from first file.
is_continue_train : bool, optional
Whether to continue training from the last checkpoint of the previous run.
is_continue_best : bool, optional
Whether to continue training from the best model rather than last. If `is_continue_best`
continues from the first checkpoint directory (i.e. result dir), but last one if not
(i.e. tmp dir).
is_progressbar : bool, optional
Whether to train with a progressbar.
checkpoint_epochs : list of int, optional
List of int saves at each of these epochs.
tensorboard_dir : str, optional
Directory for saving tensorboard logs.
load_epoch : int, optional
What epoch to load if not `is_retrain` and saved multiple epochs with a
suffix `_epoch{e}`. By default : last.
device : str, optional
Device on which to run the model.
seed : int, optional
Pseudo random seed.
callbacks : list, optional
Initial callbacks.
clean_after_run : ["training","all",None], optional
Cleans the directory. If "training" removes all the checkpoiting needed for training
(last epoch models and all the optimizer). If "all" also removes the best_epoch model.
monitor_best : {"valid_loss_best", "valid_acc_best", "train_loss_best", "last", int}, optional
What should be monitored for the best model. If int this is simply a given epoch.
kwargs :
Additional arguments to the model.
"""
set_seed(seed)
logger.info(f"Using {chckpnt_dirnames} for checkpoint.")
for chckpnt_dirname in chckpnt_dirnames:
os.makedirs(chckpnt_dirname, exist_ok=True)
if not is_train:
# to load reverse file order
chckpnt_dirnames = chckpnt_dirnames[::-1]
callbacks = get_callbakcs(callbacks, chckpnt_dirnames, is_continue_train, is_continue_best,
checkpoint_epochs, datasets, monitor_best, patience, seed,
is_progressbar, tensorboard_dir, is_train)
train_split = predefined_split(
datasets["valid"]) if "valid" in datasets else None
trainer = Model(
callbacks=callbacks, train_split=train_split, device=device, **kwargs
)
if is_return_init:
trainer.initialize()
return trainer
if is_train:
trainer.fit(datasets["train"], y=None)
trainer = load_trainer(trainer, datasets, chckpnt_dirnames,
load_epoch, monitor_best, is_load_criterion=is_load_criterion)
with open(os.path.join(chckpnt_dirnames[0], MOD_SUMM_FILENAME), "w") as f:
f.write(str(trainer.module_))
clean_end_run(clean_after_run, chckpnt_dirnames)
return trainer
def get_callbakcs(callbacks, chckpnt_dirnames, is_continue_train,
is_continue_best, checkpoint_epochs, datasets,
monitor_best, patience, seed, is_progressbar,
tensorboard_dir, is_train):
for chckpnt_dirname in chckpnt_dirnames:
chckpt_last = get_checkpoint(chckpnt_dirname, monitor="last")
callbacks.append(chckpt_last)
# loading from previous checkpoint to continue training
if is_continue_train:
if is_continue_best:
chckpt_cont = get_checkpoint(
chckpnt_dirnames[0], monitor=monitor_best)
else:
chckpt_cont = chckpt_last
# will continue from last dirname
load_state = LoadInitState(chckpt_cont)
callbacks.append(load_state)
# checkpoint from a given epoch
if checkpoint_epochs is not None:
for chckpnt_dirname in chckpnt_dirnames:
callbacks.append(
get_checkpoint(chckpnt_dirname, monitor=checkpoint_epochs)
)
# Nota Bene : the best checkpoint added will be the one logged with a "+"
if "valid" in datasets:
for chckpnt_dirname in chckpnt_dirnames:
chckpt_best = get_checkpoint(chckpnt_dirname, monitor=monitor_best)
callbacks.append(chckpt_best)
if patience is not None:
callbacks.append(EarlyStopping(patience=patience))
if seed is not None:
callbacks.append(FixRandomSeed(seed))
if is_progressbar:
callbacks.append(ProgressBar())
if tensorboard_dir is not None and is_train:
if os.path.exists(tensorboard_dir) and os.path.isdir(tensorboard_dir):
shutil.rmtree(tensorboard_dir)
writer = SummaryWriter(tensorboard_dir)
callbacks.append(TensorBoard(writer))
return callbacks
def load_trainer(trainer, datasets, chckpnt_dirnames, load_epoch,
monitor_best, is_load_criterion=True):
trainer.initialize()
# loads from last dirname
if load_epoch is not None:
# checkpoint at a specific epoch
chckpt = get_checkpoint(chckpnt_dirnames[-1], monitor=load_epoch)
elif "valid" in datasets:
# if the best checkpoint was saved then load it
chckpt = get_checkpoint(chckpnt_dirnames[-1], monitor=monitor_best)
else:
# load from first dirname for mode="last"
chckpt = get_checkpoint(chckpnt_dirnames[-1], monitor="last")
trainer = load_chkpnt_model_(
trainer, chckpt, is_load_criterion=is_load_criterion)
return trainer
def load_chkpnt_model_(trainer, chckpt, is_load_criterion=True):
# don't load optimizer
trainer.load_params(f_history=chckpt.f_history_)
trainer.load_params(
f_params=chckpt.get_formatted_files(trainer)["f_params"])
if is_load_criterion:
trainer.load_params(
f_criterion=chckpt.get_formatted_files(trainer)["f_criterion"])
return trainer
|
decodable_information_bottleneck-main
|
utils/train.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import copy
import glob
import logging
import math
import os
from functools import partial, partialmethod
import numpy as np
import pandas as pd
import scipy
import skorch
import torch
import torch.nn as nn
from sklearn.metrics import accuracy_score, classification_report, log_loss
from skorch.callbacks import Freezer, LRScheduler
from skorch.callbacks.scoring import ScoringBase
from skorch.dataset import unpack_data, uses_placeholder_y
from skorch.history import History
from torch.nn.utils.fusion import fuse_conv_bn_eval
from dib.predefined import MLP
from dib.training import NeuralNetTransformer
from dib.training.helpers import clone_trainer
from dib.training.trainer import _get_params_for_optimizer
from dib.transformers import BASE_LOG, DIBLoss, DIBLossZX
from dib.utils.helpers import (
CrossEntropyLossGeneralize,
extract_target,
set_seed,
to_numpy,
)
from utils.helpers import (
SFFX_TOAGG,
BatchNormConv,
StoreVarGrad,
add_noise_to_param_,
batchnorms2convs_,
clip_perturbated_param_,
cont_tuple_to_tuple_cont,
dict_none_toNaN,
get_device,
get_exponential_decay_gamma,
get_float_value,
merge_dicts,
save_pattern,
set_requires_grad,
)
try:
from torch.utils.tensorboard import SummaryWriter
except ImportError:
pass
__all__ = ["eval_loglike", "eval_clf"]
FILE_SCORE = "score.csv"
FILE_CLF_REP = "clf_report.csv"
logger = logging.getLogger(__name__)
def eval_corr_gen(trainer, dataset, mode="train"):
"""
Evaluates a classifier for correlation with generalization using some of the best predictors
of generalization from each section of "FANTASTIC GENERALIZATION MEASURES AND WHERE TO FIND THEM.
Also does the usual classifier evaluation
"""
# measure usual classification (i.e. how well generalizes)
out_eval_clf = eval_clf(trainer, dataset)
if mode == "test":
# only do correlation measure if "train mode"
return out_eval_clf
trainer = clone_trainer(trainer)
logger.info(f"len(dataset)={len(dataset)}")
# Variance of gradients (for classifier and transformer)
logger.info("var_grad")
var_grad = get_var_grad(trainer, dataset)
logger.info(logger)
logger.info("d_H_Q_xCz")
# before freezing the net
d_H_Q_xCz = get_H_Q_xCz(
trainer, dataset, "d_H_Q_xCz", conditional="H_Q[X|Z]-H_Q[Y|Z]"
)
logger.info("H_Q_xCz")
# H_Q[X|Z]
H_Q_xCz = get_H_Q_xCz(trainer, dataset, "H_Q_xCz")
# H_Q+[X|Z]
logger.info("d_H_Q+_xCz")
d_H_Qp_xCz = get_H_Q_xCz(
trainer,
dataset,
"H_Q_xCz",
Q_zx=partial(
MLP, hidden_size=2048, n_hidden_layers=trainer.module_.clf.n_hidden_layers
),
)
# H_Q-[X|Z]
logger.info("d_H_Q-_xCz")
d_H_Qm_xCz = get_H_Q_xCz(
trainer,
dataset,
"H_Q_xCz",
Q_zx=partial(
MLP, hidden_size=2, n_hidden_layers=trainer.module_.clf.n_hidden_layers
),
)
# freezes all batchnorm layers by converting them to convolutions
trainer.module_.eval()
batchnorms2convs_(trainer.module_)
# Entropy of the logits
logger.info("entropy")
y_pred_proba = trainer.predict_proba(dataset)
y_pred_ent = scipy.stats.entropy(
y_pred_proba, axis=1, base=BASE_LOG).mean()
# Path Norm (for classifier and transformer)
logger.info("path_norm")
path_norm = get_path_norm(trainer, dataset)
# Sharpness magnitude => max (relative) change in weights that cause less than 1 diff in log like
logger.info("sharp_mag")
sharp_mag = get_sharp_mag(trainer, dataset)
return dict(
y_pred_ent=y_pred_ent,
path_norm=path_norm,
var_grad=var_grad,
sharp_mag=sharp_mag,
H_Q_xCz=H_Q_xCz,
d_H_Qp_xCz=d_H_Qp_xCz,
d_H_Qm_xCz=d_H_Qm_xCz,
d_H_Q_xCz=d_H_Q_xCz,
**out_eval_clf,
)
def eval_clf(trainer, dataset, **kwargs):
"""Evaluates a classifier on a dateset."""
y_pred_proba = trainer.predict_proba(dataset)
loglike = -log_loss(dataset.targets, y_pred_proba)
y_pred = y_pred_proba.argmax(-1)
accuracy = accuracy_score(dataset.targets, y_pred)
top5_acc = top_n_accuracy_score(dataset.targets, y_pred_proba, n=5)
return dict(accuracy=accuracy, top5_acc=top5_acc, loglike=loglike)
def eval_trnsf(trainer, dataset, **kwargs):
"""
Evaluates a transformer on a dateset by returning everything in history that starts with `valid_`.
Difference with `eval_clf` is that saves all temporary variables.
"""
trainer.check_data(dataset, None)
trainer.notify("on_epoch_begin", dataset_train=dataset,
dataset_valid=dataset)
trainer._single_epoch(dataset, training=False, epoch=0)
# don't call "on epoch end" because should not checkpoint (but still want scoring)
for _, cb in trainer.callbacks_:
# score everything on validation because you didn't train
if isinstance(cb, ScoringBase) and not cb.on_train:
cb.on_epoch_end(trainer, dataset_train=dataset,
dataset_valid=dataset)
return {
k.replace("valid_", ""): v
for k, v in trainer.history[-1].items()
if k.startswith("valid_")
and k not in ["valid_batch_count"]
and "_best" not in k
}
def eval_trainer_log(
trainer,
dataset,
csv_score_pattern,
chckpnt_dirnames,
hyperparameters,
evaluator=eval_clf,
tensorboard_dir=None,
is_append=False,
mode="test",
epoch="last",
file_score=FILE_SCORE,
file_clf_rep=FILE_CLF_REP,
**kwargs,
):
"""Evaluate a trainer and log it."""
to_log = evaluator(trainer, dataset, mode=mode, **kwargs)
# first save the header then actual
if not is_append:
save_pattern(chckpnt_dirnames, csv_score_pattern, file_score)
for metric_name, score in to_log.items():
save_pattern(
chckpnt_dirnames,
csv_score_pattern,
file_score,
formatting=dict(epoch=epoch, metric=metric_name,
mode=mode, score=score),
logger=logger,
)
if tensorboard_dir is not None:
with SummaryWriter(log_dir=tensorboard_dir + "hypopt/") as w:
w.add_hparams(
# tensorboard does not accept None
dict_none_toNaN(hyperparameters),
{f"hparam/{metric_name}": score},
)
def eval_loglike(trainer, dataset, seed=123, **kwargs):
"""Return the log likelihood for each image in order."""
set_seed(seed) # make sure same order and indices for context and target
trainer.module_.to(trainer.device)
trainer.criterion_.is_return_all = True
y_valid_is_ph = uses_placeholder_y(dataset)
all_losses = []
trainer.notify("on_epoch_begin", dataset_valid=dataset)
for data in trainer.get_iterator(dataset, training=False):
Xi, yi = unpack_data(data)
yi_res = yi if not y_valid_is_ph else None
trainer.notify("on_batch_begin", X=Xi, y=yi_res, training=False)
step = trainer.validation_step(Xi, yi, **kwargs)
trainer.notify("on_batch_end", X=Xi, y=yi_res, training=False, **step)
all_losses.append(-step["loss"]) # use log likelihood instead of NLL
trainer.criterion_.is_return_all = False
return torch.cat(all_losses, dim=0).detach().cpu().numpy()
# credits : https://github.com/scikit-learn/scikit-learn/pull/8234
def top_n_accuracy_score(y_true, y_pred, n=5, normalize=True):
"""top N Accuracy classification score.
For multiclass classification tasks, this metric returns the
number of times that the correct class was among the top N classes
predicted.
Parameters
----------
y_true : 1d array-like, or label indicator array / sparse matrix
Ground truth (correct) labels.
y_pred : array-like, where for each sample, each row represents the
likelihood of each possible label.
The number of columns must be at least as large as the set of possible
label values.
normalize : bool, optional (default=True)
If ``False``, return the number of top N correctly classified samples.
Otherwise, return the fraction of top N correctly classified samples.
Returns
-------
score : float
If ``normalize == True``, return the proportion of top N correctly
classified samples, (float), else it returns the number of top N
correctly classified samples (int.)
The best performance is 1 with ``normalize == True`` and the number
of samples with ``normalize == False``.
See also
--------
accuracy_score
Notes
-----
If n = 1, the result will be the same as the accuracy_score. If n is the
same as the number of classes, this score will be perfect and meaningless.
In cases where two or more classes are assigned equal likelihood, the
result may be incorrect if one of those classes falls at the threshold, as
one class must be chosen to be the nth class and the class chosen may not
be the correct one.
Examples
--------
>>> import numpy as np
>>> y_pred = np.array([[0.1, 0.3, 0.4, 0.2],
... [0.4, 0.3, 0.2, 0.1],
... [0.2, 0.3, 0.4, 0.1],
... [0.8, 0.1, 0.025, 0.075]])
>>> y_true = np.array([2, 2, 2, 1])
>>> top_n_accuracy_score(y_true, y_pred, n=1)
0.5
>>> top_n_accuracy_score(y_true, y_pred, n=2)
0.75
>>> top_n_accuracy_score(y_true, y_pred, n=3)
1.0
>>> top_n_accuracy_score(y_true, y_pred, n=2, normalize=False)
3
"""
num_obs, num_labels = y_pred.shape
idx = num_labels - n - 1
counter = 0
argsorted = np.argsort(y_pred, axis=1)
for i in range(num_obs):
if y_true[i] in argsorted[i, idx + 1:]:
counter += 1
if normalize:
return counter / num_obs
else:
return counter
def _load_something(
get_rows_cols_agg, pattern, base_dir="", mark_to_agg=lambda c: c + SFFX_TOAGG
):
"""Load the something from all the saved values .
Parameters
----------
get_rows_toaggreg : callable
function that takes files, raw_files as input and return all the rows as well as the name
of columns over which to aggregate.
pattern : str
Pattern of files to load. Needs to start with `tmp_results`.
base_dir : str, optional
Base directory to prepend to pattern.
aggregate : list of string, optional
Aggregation methods. If singleton will show the results as a single level. If empty list,
will not aggregate anything.
mark_to_agg : callable, optional
Function that marks columns to aggregate.
"""
raw_files = glob.glob(base_dir + pattern, recursive=True)
# rm results/ and /score.csv and add experiment_ name
files = [
"/".join(
col if i > 0 else f"experiment_{col}"
for i, col in enumerate(f[len(base_dir):].split("/")[1:-1])
)
for f in raw_files
]
rows, columns, toaggreg = get_rows_cols_agg(files, raw_files)
# the first column (experiment will be wrong if underscores in the experiments )
results = pd.DataFrame(rows, columns=columns)
results = results.apply(pd.to_numeric, errors="ignore")
results = results.apply(to_bool)
def fn_rename(col):
if col in toaggreg:
return mark_to_agg(col)
return col
return results.rename(columns=fn_rename)
def to_bool(s):
if s.dtypes != "object":
return s
return s.replace({"True": True, "False": False})
def load_results(
pattern="tmp_results/**/score.csv",
metrics=["test_acc", "test_loss"],
metric_column_name="{mode}_{metric}",
**kwargs,
):
"""Load the results from the folder.
Parameters
----------
pattern : str, optional
Pattern of files to load. Needs to start with `tmp_results`.
metrics : list of string, optional
Precomputed metrics to load. E.g. ["test_acc","test_loss","test_top5_acc","train_loss"].
metric_column_name : str, optional
Name of the column containing the metric.
"""
def get_rows_cols_agg(files, raw_files, metrics=metrics):
rows = []
for raw_file, file in zip(raw_files, files):
# hyperparameters
row = [file.split("/")[0][len("experiment") + 1:]]
# hyperparameters
row += [str_to_val(folder.split("_")[-1])
for folder in file.split("/")[1:]]
# metrics
score = pd.read_csv(raw_file)
# only keep numeric
score = score[pd.to_numeric(
score["{score}"], errors="coerce").notnull()]
score["{score}"] = pd.to_numeric(score["{score}"])
score = score.pivot_table(
columns=metric_column_name, values="{score}", index="{epoch}"
)
score = score.reindex(columns=metrics).reset_index()
for _, r in score.iterrows():
rows.append(row + list(r.values))
columns = (
["experiment"]
+ ["_".join(folder.split("_")[:-1])
for folder in files[0].split("/")][1:]
+ ["epochs"]
+ metrics
)
return rows, columns, metrics
results = _load_something(get_rows_cols_agg, pattern=pattern, **kwargs)
return results
def load_histories(
pattern="tmp_results/**/transformer/last_epoch_history.json", **kwargs
):
"""Load the history of a model (validation and train variables saved at every epoch).
Parameters
----------
pattern : str, optional
Pattern of files to load. Needs to start with `tmp_results`.
kwargs : list of string, optional
Additional arguments to `_load_something`.
"""
def is_plot(key, values):
"""Columns to plot from history."""
if not (key.startswith("train") or key.startswith("valid")):
return False
if len(set(values)) == 1:
return False
if not all(isinstance(v, float) for v in values):
return False
return True
def get_rows_cols_agg(files, raw_files):
to_plot_col = set()
rows = []
for raw_file, file in zip(raw_files, files):
const_row = dict()
const_row["experiment"] = file.split(
"/")[0][len("experiment") + 1:]
# hyperparameters
for col in file.split("/")[1:]:
key = "_".join(col.split("_")[:-1])
value = col.split("_")[-1]
const_row[key] = str_to_val(value)
history = History.from_file(raw_file)
# prepare for line plots
history_to_plot = {
key: history[:, key]
for key in history[0].keys()
if is_plot(key, history[:, key])
}
# Checking same number epoch
for i, (k, v) in enumerate(history_to_plot.items()):
if i == 0:
old_k = k
old_len = len(v)
if old_len != len(v):
raise ValueError(
f"Number of epochs not the same for (at least) {old_k} and {k}."
)
for epoch, history_per_epoch in enumerate(
cont_tuple_to_tuple_cont(history_to_plot)
):
row = const_row.copy()
row["epochs"] = epoch
for key, value in history_per_epoch.items():
row[key] = value
to_plot_col.add(key)
rows.append(row)
return rows, None, list(to_plot_col)
results = _load_something(get_rows_cols_agg, pattern=pattern, **kwargs)
return results
def str_to_val(s):
if s == "None":
return None
return s
def accuracy_filter_train(model, X, y, map_target_position, **kwargs):
"""
Helper function that computes the accuracy but only the training examples.
"""
target = to_numpy(extract_target(y, map_target_position))
try:
is_train = y[:, map_target_position["constant"]] == 1
target = target[is_train]
y_pred = model.predict_proba(X)[is_train]
out = accuracy_score(target, y_pred.argmax(-1), **kwargs)
except (IndexError, KeyError):
out = accuracy(model, X, target)
return out
def accuracy(model, X, y, **kwargs):
"""
Compute the accuracy score of a Sklearn Classifier on a given dataset.
"""
y_pred = model.predict_proba(X)
return accuracy_score(y, y_pred.argmax(-1), **kwargs)
def loglike(model, X, y, **kwargs):
"""Compute the loglikelihood (base e) score of a Sklearn Classifier on a given dataset."""
y_pred_proba = model.predict_proba(X)
return -log_loss(y, y_pred_proba, **kwargs)
def get_path_norm(trainer, dataset):
"""Compute the pathnorm as described in "FANTASTIC GENERALIZATION MEASURES AND WHERE TO FIND THEM".
I.e. squares all parameters, foreward pass all ones and then take sqrt of output."""
trainer = clone_trainer(trainer)
# use the mean instead of sampling to make sure that not negative
trainer.module_.transformer.is_use_mean = True
# square all parameters
with torch.no_grad():
for name, W in trainer.module_.named_parameters():
W.pow_(2)
all_ones = dataset[0][0].unsqueeze(0).fill_(1)
logits = trainer.forward(all_ones)[0]
sum_logits = logits.sum().item()
return sum_logits ** 0.5
def get_var_grad(trainer, dataset):
"""Compute the variance of the gradients."""
trainer = clone_trainer(trainer)
# compute also the gradients of the transformer
trainer.module_.is_freeze_transformer = False
# compute elementwise variance of parameters
trainer.callbacks_.append(("store_grad", StoreVarGrad()))
trainer.check_data(dataset, None)
trainer.notify("on_epoch_begin", dataset_train=dataset,
dataset_valid=dataset)
trainer._single_epoch(dataset, training=True, epoch=0)
last_epoch = trainer.history[-1]["epoch"]
# don't call "on epoch end" because should not checkpoint (but still want to compute variance)
for _, cb in trainer.callbacks_:
if isinstance(cb, StoreVarGrad):
cb.on_epoch_end(trainer, dataset_train=dataset,
dataset_valid=dataset)
var_grad = np.concatenate([v.flatten()
for v in cb.var_grads.values()])
return var_grad.mean() # take mean over all parameters
class NegCrossEntropyLoss(nn.CrossEntropyLoss):
def forward(self, input, target):
# select label from the targets
return -super().forward(input, target[0])
def get_sharp_mag(
trainer,
dataset,
sigma_min=0,
sigma_max=2,
target_deviation=0.1,
n_restart_perturbate=3,
max_binary_search=50,
is_relative=True,
):
"""
Compute the sharpness magnitude 1/alpha'^2 described in [1].
Notes
-----
- This is slightly different than [1] because the target deviation is on cross-entropy instead
of accuracy (as we don't care about accuracy in our paper).
Parameters
----------
trainer : skorch.NeuralNet
dataset : torch.utils.data.Dataset
sigma_min : float, optional
Minimum standard deviation of perturbation.
sigma_max : float, optional
Maximum standard deviation of perturbation.
n_adv_perturbate : int, optional
Number of steps to perform adversarial perturbation for.
n_restart_perturbate : int, optional
Number of times restarting the perturbation (different initialization for adv perturbate).
target_deviation : float, optional
Maximum difference of log likelihood allowed.
max_binary_search : int, optional
Maximum number of binary search tries.
References
----------
[1] Jiang, Yiding, et al. "Fantastic Generalization Measures and Where to Find Them."
arXiv preprint arXiv:1912.02178 (2019).
"""
trainer = clone_trainer(trainer)
acc = accuracy(trainer, dataset, dataset.targets)
# compute also the gradients of the transformer
trainer.module_.is_freeze_transformer = False
# reverses cross entropy to MAXIMIZE (adversarial)
trainer.criterion = NegCrossEntropyLoss
for bin_search in range(max_binary_search):
sigma_min, sigma_max = get_sharp_mag_interval(
trainer,
acc,
dataset,
sigma_min,
sigma_max,
target_deviation,
n_restart_perturbate,
is_relative,
)
if sigma_min > sigma_max or math.isclose(sigma_min, sigma_max, rel_tol=1e-2):
# if interval for binary search is very small stop
break
if bin_search == max_binary_search - 1:
logger.info(
f"Stopped early beacuase reached max_binary_search={max_binary_search}. [sigma_min,sigma_max]=[{sigma_min},{sigma_max}]"
)
return 1 / (sigma_max ** 2)
def get_sharp_mag_interval(
unperturbated_trainer,
unperturbated_acc,
dataset,
sigma_min,
sigma_max,
target_deviation,
n_restart_perturbate,
is_relative,
):
sigma_new = (sigma_min + sigma_max) / 2
worst_acc = math.inf
unperturbated_params = {
name: param.detach()
for name, param in unperturbated_trainer.module_.named_parameters()
}
for _ in range(n_restart_perturbate):
trainer = clone_trainer(unperturbated_trainer,
is_reinit_besides_param=True)
# add half of the possible noise to give some space for gradient ascent
add_noise_to_param_(
trainer.module_, sigma=sigma_new / 2, is_relative=is_relative
)
for i, data in enumerate(trainer.get_iterator(dataset, training=True)):
Xi, yi = unpack_data(data)
step = trainer.train_step(Xi, yi)
# clipping perturbation value of added parameters to |w_i * sigma| or |sigma|
clip_perturbated_param_(
trainer.module_,
unperturbated_params,
sigma_new,
is_relative=is_relative,
)
if not torch.isfinite(step["loss"]) or step["loss"].abs() > (
abs(unperturbated_acc) + 10 * target_deviation
):
# if loss is very large for one batch then no need to finish this loop
return sigma_min, sigma_new
curr_acc = accuracy(trainer, dataset, dataset.targets)
worst_acc = min(worst_acc, curr_acc)
deviation = abs(curr_acc - worst_acc)
if math.isclose(unperturbated_acc, worst_acc, rel_tol=1e-2):
# if not deviation is nearly zero can stop
return sigma_new, sigma_new
if deviation > target_deviation:
sigma_max = sigma_new
else:
sigma_min = sigma_new
return sigma_min, sigma_max
def get_H_Q_xCz(
trainer,
dataset,
select,
n_per_head=1,
batch_size=256,
lr=1e-2,
max_epochs=100,
Q_zx=None,
**kwargs,
):
trainer = clone_trainer(trainer) # ensure not changing (shouldn't)
trainer.module_.transformer.is_transform = False # DIB Loss expects pred of label
model = trainer.module_.transformer
z_dim = model.z_dim
def get_Q(*args, **kwargs):
Q = copy.deepcopy(trainer.module_.clf)
Q.reset_parameters() # shouldn't be needed
return Q
count_targets = dataset.count_targets()
n_per_target = {str(k): int(v) for k, v in count_targets.items()}
dib = partial(
DIBLoss,
Q_zx if Q_zx is not None else get_Q,
n_per_target,
n_per_head=n_per_head,
n_classes=dataset.n_classes,
z_dim=z_dim,
map_target_position=dataset.map_target_position,
ZYCriterion=partial(
CrossEntropyLossGeneralize,
map_target_position=dataset.map_target_position,
gamma=0,
),
**kwargs,
)
# making sure that training of the parameter of the criterion
NeuralNetTransformer._get_params_for_optimizer = partialmethod(
_get_params_for_optimizer, is_add_criterion=True
)
set_requires_grad(model, False)
net = NeuralNetTransformer(
module=model,
criterion=dib,
optimizer=torch.optim.Adam,
lr=lr,
max_epochs=max_epochs,
train_split=None,
batch_size=batch_size,
device=trainer.device,
iterator_valid__batch_size=batch_size * 2,
callbacks=[
LRScheduler(
torch.optim.lr_scheduler.ExponentialLR,
gamma=get_exponential_decay_gamma(100, max_epochs),
)
],
)
net.fit(dataset, y=None)
out = eval_trnsf(net, dataset)
return out[select]
class HQxCz:
def __init__(self, trainer, dataset):
trainer = clone_trainer(trainer) # ensure not changing (shouldn't)
self.Q_zy = copy.deepcopy(trainer.module_.clf)
trainer.module_ = trainer.module_.transformer
# using squeezing n_z_samples to work with forward call (working in determinstic setting )
trainer.module_.is_avg_trnsf = True
self.trainer = trainer
Z = trainer.forward(dataset, training=False, device=trainer.device)
targets = torch.stack([torch.tensor(y) for _, y in dataset])
self.trnsf_dataset = skorch.dataset.Dataset(
Z, [targets[:, i] for i in range(targets.size(1))]
) # compute transformed data onces
self.z_dim = self.trnsf_dataset[0][0].size(-1)
count_targets = dataset.count_targets()
self.n_per_target = {str(k): int(v) for k, v in count_targets.items()}
self.n_classes = dataset.n_classes
self.map_target_position = dataset.map_target_position
def __call__(
self, select, n_per_head=1, batch_size=256, lr=1e-2, max_epochs=100, **kwargs
):
def get_Q(*args, **kwargs):
Q = copy.deepcopy(self.Q_zy)
Q.reset_parameters() # shouldn't be needed
return Q
dib = partial(
DIBLossZX,
get_Q,
self.n_per_target,
n_per_head=n_per_head,
n_classes=self.n_classes,
z_dim=self.z_dim,
map_target_position=self.map_target_position,
ZYCriterion=partial(
CrossEntropyLossGeneralize,
map_target_position=self.map_target_position,
gamma=0,
),
**kwargs,
)
# making sure that training of the parameter of the criterion
NeuralNetTransformer._get_params_for_optimizer = partialmethod(
_get_params_for_optimizer, is_add_criterion=True
)
net = NeuralNetTransformer(
module=nn.Identity,
criterion=dib,
optimizer=torch.optim.Adam,
lr=lr,
max_epochs=max_epochs,
train_split=None,
batch_size=batch_size,
device=self.trainer.device,
iterator_valid__batch_size=batch_size * 2,
callbacks=[
LRScheduler(
torch.optim.lr_scheduler.ExponentialLR,
gamma=get_exponential_decay_gamma(100, max_epochs),
)
],
)
net.fit(self.trnsf_dataset, y=None)
out = eval_trnsf(net, self.trnsf_dataset)
return out[select]
|
decodable_information_bottleneck-main
|
utils/evaluate.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import collections
import copy
import glob
import logging
import math
import os
import random
import shutil
import types
from collections import ChainMap, defaultdict
from contextlib import contextmanager, suppress
from multiprocessing import Pool, cpu_count
import numpy as np
import omegaconf
import pandas as pd
import skorch
import torch
import torch.nn as nn
from omegaconf import OmegaConf
from skorch.callbacks import Callback
from skorch.callbacks.scoring import check_scoring
from skorch.dataset import get_len, unpack_data, uses_placeholder_y
from torch.optim.optimizer import Optimizer, required
from dib import UNLABELLED_CLASS
from dib.training.helpers import Checkpoint
from dib.training.trainer import _single_epoch
from dib.utils.helpers import *
SKLEARN_MODEL = "model.joblib"
SFFX_TOAGG = "_toagg"
logger = logging.getLogger(__name__)
def get_float_value(x):
"""Convert to float"""
if isinstance(x, torch.Tensor):
x = x.item()
elif not isinstance(x, float):
x = float(x)
return x
def add_noise_to_param_(module, sigma, is_relative=True):
"""Add uniform noise with standard deviation `sigma` to each weight."""
with torch.no_grad():
for param in module.parameters():
if is_relative:
unif = torch.distributions.uniform.Uniform(
0, param.abs() * sigma)
noise = unif.sample()
else:
unif = torch.distributions.uniform.Uniform(0, sigma)
noise = unif.sample(param.shape)
param.add_(noise)
def force_generalization(datasets):
"""Force the (anti)-generalization of a model by adding the test set to the training set. It
also adds a label `is_train` that says whether the example is from the training `is_train=1` or
testing `is_train=0` set.
Parameters
----------
datasets : dictionary of torch.utils.data.Dataset
Dictionary containing at least the `"train"` and `"test"` set.
Returns
-------
train_data : torch.utils.data.Dataset
"""
# make sure don't change the dataset for eval and clf
datasets = copy.deepcopy(datasets)
datasets["test"].set_const_target_(0)
datasets["train"].set_const_target_(1)
datasets["train"].append_(datasets["test"])
return datasets["train"]
def clip_perturbated_param_(
module, unperturbated_params, clip_factor, is_relative=True
):
"""
Element wise clipping of the absolute value of the difference in weight. `unperturbated_params`
needs to be a dictionary of unperturbated param. Use `is_relative` if clip factor should multipy
the unperturbated param.
"""
with torch.no_grad():
for name, param in module.named_parameters():
w = unperturbated_params[name]
# delta_i = (delta+w)_i - w_i
delta = param - w
max_abs_delta = clip_factor * w.abs() if is_relative else clip_factor
clipped_delta = torch.where(
delta.abs() > max_abs_delta, delta.sign() * max_abs_delta, delta
)
# inplace replace
param.fill_(0)
param.add_(w)
param.add_(clipped_delta)
def get_device(module):
"""return device of module."""
return next(module.parameters()).device
def batchnorms2convs_(module):
"""Converts all the batchnorms to frozen convolutions."""
for name, m in module.named_children():
if isinstance(m, nn.modules.batchnorm._BatchNorm):
module._modules[name] = BatchNormConv(m)
else:
batchnorms2convs_(module._modules[name])
class BatchNormConv(nn.Module):
"""Replace a batchnorm layer with a frozen convolution."""
def __init__(self, batchnorm):
super().__init__()
if isinstance(batchnorm, nn.BatchNorm2d):
conv = nn.Conv2d(
batchnorm.num_features,
batchnorm.num_features,
1,
groups=batchnorm.num_features,
)
elif isinstance(batchnorm, nn.BatchNorm1d):
conv = nn.Conv1d(
batchnorm.num_features,
batchnorm.num_features,
1,
groups=batchnorm.num_features,
)
conv.eval()
nn.init.ones_(conv.weight)
nn.init.zeros_(conv.bias)
conv.to(get_device(batchnorm))
self.bn = nn.utils.fusion.fuse_conv_bn_eval(conv, batchnorm)
def forward(self, x):
return self.bn(x)
def rm_clf_experiment(experiment):
"""Remove all the classifier files for an experiment."""
for f in glob.glob(f"tmp_results/{experiment}/**/clf_*/**", recursive=True):
try:
shutil.rmtree(f)
except FileNotFoundError:
pass
def invert_dict(d):
return {v: k for k, v in d.items()}
def flip_nested_dict(nested_dict):
"""Flip nested dictionary inside out."""
flipped = dict()
for key, subdict in nested_dict.items():
for k, v in subdict.items():
flipped[k] = flipped.get(k, dict())
flipped[k][key] = v
return flipped
# Credits https://stackoverflow.com/questions/17215400/python-format-string-unused-named-arguments/17215533#17215533
class PartialFormatMap(dict):
"""Dictionary used to do partial formatting of string in python.
E.g. `"{keep} {modify}".format_map(SafeDict(modify='done')) == '{keep} done'`
"""
def __missing__(self, key):
return "{" + key + "}"
# credits : https://gist.github.com/simon-weber/7853144
@contextmanager
def all_logging_disabled(highest_level=logging.CRITICAL):
"""
A context manager that will prevent any logging messages
triggered during the body from being processed.
:param highest_level: the maximum logging level in use.
This would only need to be changed if a custom level greater than CRITICAL
is defined.
"""
# two kind-of hacks here:
# * can't get the highest logging level in effect => delegate to the user
# * can't get the current module-level override => use an undocumented
# (but non-private!) interface
previous_level = logging.root.manager.disable
logging.disable(highest_level)
try:
yield
finally:
logging.disable(previous_level)
# credits : https://stackoverflow.com/questions/5543651/computing-standard-deviation-in-a-stream
class OnlineVariance:
"""
Welford's algorithm computes the sample variance incrementally.
"""
def __init__(self, iterable=None, ddof=1):
self.ddof, self.n, self.mean, self.M2 = ddof, 0, 0.0, 0.0
if iterable is not None:
for datum in iterable:
self.include(datum)
def include(self, datum):
self.n += 1
self.delta = datum - self.mean
self.mean += self.delta / self.n
self.M2 += self.delta * (datum - self.mean)
@property
def variance(self):
return self.M2 / (self.n - self.ddof)
@property
def std(self):
return np.sqrt(self.variance)
class StoreVarGrad(Callback):
"""Callback which applies a function on all gradients, stores the variance during each epoch."""
def __init__(self):
self.online_vars = dict()
self.n = 0
self.var_grads = dict()
def initialize(self):
self.online_vars = dict()
self.n = 0
self.var_grads = dict()
def on_grad_computed(self, net, **kwargs):
for name, param in net.module_.named_parameters():
if param.grad is not None:
if name not in self.online_vars:
self.online_vars[name] = OnlineVariance()
self.online_vars[name].include(
param.grad.cpu().detach().flatten().numpy()
)
def on_epoch_end(self, net, parent=None, **kwargs):
epoch = net.history[-1]["epoch"]
self.n += 1
self.var_grads = {k: v.variance for k, v in self.online_vars.items()}
self.online_vars = dict()
class StoreGrad(Callback):
"""Callback which applies a function on all gradients, stores the output at each epoch and
then takes an average across epochs."""
def __init__(self, fn=Identity()):
self.curr_epoch = dict()
self.prev_epochs = dict()
self.fn = fn
def initialize(self):
self.curr_epoch = dict()
self.prev_epoch = dict()
def on_grad_computed(self, net, **kwargs):
for name, param in net.module_.named_parameters():
if param.grad is not None:
if name not in self.curr_epoch:
self.curr_epoch[name] = OnlineVariance()
self.curr_epoch[name].include(param.grad.cpu().flatten())
def on_epoch_end(self, net, parent=None, **kwargs):
epoch = net.history[-1]["epoch"]
self.prev_epochs[epoch] = {
k: v.variance for k, v in self.curr_epoch.items()}
self.curr_epoch = dict()
class StopAtThreshold(skorch.callbacks.Callback):
"""Callback for stopping training when `monitor` reaches threshold."""
def __init__(
self, monitor="train_loss", threshold=0.01, lower_is_better=True, sink=print
):
self.monitor = monitor
self.threshold = threshold
self.sink = sink
self.lower_is_better = lower_is_better
def on_epoch_end(self, net, **kwargs):
current_score = net.history[-1, self.monitor]
if self._is_score_improved(current_score):
self._sink(
"Stopping since {} reached {}".format(
self.monitor, self.threshold),
verbose=net.verbose,
)
raise KeyboardInterrupt
def _is_score_improved(self, score):
if self.lower_is_better:
return score < self.threshold
return score > self.threshold
def _sink(self, text, verbose):
# We do not want to be affected by verbosity if sink is not print
if (self.sink is not print) or verbose:
self.sink(text)
class TensorBoard(skorch.callbacks.TensorBoard):
def on_epoch_end(self, net, parent=None, **kwargs):
epoch = net.history[-1]["epoch"]
for m in net.module_.modules():
try:
m.tensorboard(self.writer, epoch, mode="on_epoch_end")
except AttributeError:
pass
super().on_epoch_end(net, **kwargs) # call super last
if parent is not None and hasattr(parent.criterion_, "to_store"):
for k, v in parent.criterion_.to_store.items():
with suppress(NotImplementedError):
# pytorch raises NotImplementedError on wrong types
self.writer.add_scalar(
tag=f"Loss/partial/{k}",
scalar_value=v[0] / v[1],
global_step=epoch, # avg
)
parent.criterion_.to_store = dict()
def on_grad_computed(self, net, **kwargs):
epoch = net.history[-1]["epoch"]
for m in net.module_.modules():
if hasattr(m, "tensorboard") and callable(m.tensorboard):
try:
m.tensorboard(self.writer, epoch, mode="on_grad_computed")
except NotImplementedError: # if frozen
pass
def clean_end_run(clean_after_run, chckpnt_dirnames):
"""Clean the checkpoiting directories after running.
Parameters
----------
clean_after_run : ["training","all",None]
Cleans the directory. If "training" removes all the checkpoiting needed for training
(last epoch models and all the optimizer). If "all" also removes the best_epoch model.
chckpnt_dirnames : list of str
Directories where checkpoints were saved.
"""
for chckpnt_dirname in chckpnt_dirnames:
if clean_after_run == "all":
patterns = ["*.pt"]
elif clean_after_run == "training":
patterns = ["*_optimizer.pt"]
elif clean_after_run is None:
continue
else:
raise ValueError(f"Unkown chckpnt_dirnames={chckpnt_dirnames}")
for pattern in patterns:
for f in glob.glob(os.path.join(chckpnt_dirname, pattern)):
os.remove(f)
def hyperparam_to_path(hyperparameters):
"""Return a string of all hyperparameters that can be used as a path extension."""
return "/".join([f"{k}_{v}" for k, v in hyperparameters.items()])
def format_container(to_format, formatter, k=None):
"""Format a container of string.
Parameters
----------
to_format : str, list, dict, or omegaconf.Config
(list of) strings to fromat.
formatter : dict
Dict of keys to replace and values with which to replace.
"""
if isinstance(to_format, str):
out = to_format.format(**formatter)
elif to_format is None:
out = None
else:
if isinstance(to_format, omegaconf.Config):
to_format = OmegaConf.to_container(to_format, resolve=True)
if isinstance(to_format, list):
out = [
format_container(path, formatter, k=i)
for i, path in enumerate(to_format)
]
elif isinstance(to_format, dict):
out = {
k: format_container(path, formatter, k=k)
for k, path in to_format.items()
}
else:
raise ValueError(f"Unkown to_format={to_format}")
return out
# Change _scoring for computing validation only at certain epochs
def _scoring(self, net, X_test, y_test):
"""Resolve scoring and apply it to data. Use cached prediction
instead of running inference again, if available."""
scorer = check_scoring(net, self.scoring_)
if y_test is None:
return float(
"nan"
) # ! Only difference : make sure no issue if valid not computed
return scorer(net, X_test, y_test)
def _single_epoch_skipvalid(
self,
dataset,
training,
epoch,
save_epochs=(
list(range(10))
+ list(range(9, 100, 10))
+ list(range(99, 1000, 50))
+ list(range(999, 10000, 500))
),
**fit_params,
):
if not training and epoch not in save_epochs:
return
_single_epoch(self, dataset, training, epoch, **fit_params)
def get_checkpoint(chckpnt_dirname, monitor="valid_loss_best", **kwargs):
"""Return the correct checkpoint.
Parameters
----------
chckpnt_dirname : str
monitor : {"valid_loss_best", "valid_acc_best", "train_loss_best", "last"} or list of int or int
"*_best" saves the model with the best *. "last" saves the last model.
If list of int saves at each of these epochs. If int saves at a specific epoch (useful for
loading).
"""
if monitor == "last":
return Checkpoint(
dirname=chckpnt_dirname, monitor=None, fn_prefix="last_epoch_", **kwargs
)
elif isinstance(monitor, str):
return Checkpoint(
dirname=chckpnt_dirname, monitor=monitor, fn_prefix="best_", **kwargs
)
else:
def _monitor_epoch(net):
epoch = net.history[-1]["epoch"]
return epoch in monitor
if isinstance(monitor, int):
f_params = f"params_epoch{monitor}.pt"
f_optimizer = f"optimizer_epoch{monitor}.pt"
f_criterion = f"criterion_epoch{monitor}.pt"
monitor = [monitor]
else:
f_params = "params_epoch{last_epoch[epoch]}.pt"
f_optimizer = "optimizer_epoch{last_epoch[epoch]}.pt"
f_criterion = "criterion_epoch{last_epoch[epoch]}.pt"
return Checkpoint(
dirname=chckpnt_dirname,
f_params=f_params,
f_optimizer=f_optimizer,
f_criterion=f_criterion,
monitor=_monitor_epoch,
**kwargs,
)
def count_parameters(model):
"""Count the number of parameters in a model."""
return sum([p.numel() for p in model.parameters()])
def count_prune_parameters(model):
"""Count the number of parameters that were pruned out."""
return sum(torch.nn.utils.parameters_to_vector(model.buffers()) == 0)
def merge_dicts(*dicts):
"""Merge multiple dictionaries. If key is repeated, first appearance will be used."""
return dict(ChainMap(*dicts))
def rm_const_col(df):
"""Remove constant columns in a dataframe"""
# nan need specific dropping
df = df.dropna(axis=1, how="all")
return df.loc[:, (df != df.iloc[0]).any()].copy()
def update_prepending(to_update, new):
"""Update a dictionary with another. the difference with .update, is that it puts the new keys
before the old ones (prepending)."""
# makes sure don't update arguments
to_update = to_update.copy()
new = new.copy()
# updated with the new values appended
to_update.update(new)
# remove all the new values => just updated old values
to_update = {k: v for k, v in to_update.items() if k not in new}
# keep only values that ought to be prepended
new = {k: v for k, v in new.items() if k not in to_update}
# update the new dict with old one => new values are at the begining (prepended)
new.update(to_update)
return new
def aggregate_table(table, aggregate=["mean", "std"], match_to_agg=SFFX_TOAGG):
"""Aggregates all the results in all columns containing `match_to_agg`."""
table = table.copy()
toaggreg = [c for c in table.columns if "_toagg" in c]
#! Hacky way of dealing with NaN in groupby while waiting for https://github.com/pandas-dev/pandas/pull/30584
hacky_nan = -7878 # has to be something that will not appear anywhere else
groupby_idx = [c for c in table.columns if c not in (["run"] + toaggreg)]
table[groupby_idx] = table[groupby_idx].fillna(hacky_nan)
table = table.groupby(
[c for c in table.columns if c not in (["run"] + toaggreg)]
).agg(
merge_dicts({k: aggregate for k in toaggreg}, {"run": "count"})
) # make sure that add counts
table.columns = [
"_".join(col).strip().replace("_toagg", "") for col in table.columns.values
]
table = table.reset_index()
#! Reset the nan (careful when replacing due to float precision)
numeric_col = table.select_dtypes(np.number).columns
table[numeric_col] = table[numeric_col].mask(
np.isclose(table[numeric_col].values, hacky_nan)
)
# in case replacement was in object col
table = table.mask(table == hacky_nan)
return table
def append_sffx(l, sffx):
"""Append a suffix to a list of strings."""
return [el + sffx for el in l]
def save_pattern(folders, pattern, filename, formatting={}, logger=None):
"""Save the pattern (formatted) to file. If `formatting` is not empty then append."""
for i, folder in enumerate(folders):
file = os.path.join(folder, filename)
with open(file, "w" if len(formatting) == 0 else "a") as f:
if len(formatting) > 0:
pattern = pattern.format(**formatting)
f.write(pattern + "\n")
if logger is not None and i == 0:
logger.info(f"Saving {pattern} to {file.split('/')[-1]}")
def get_exponential_decay_gamma(scheduling_factor, max_epochs):
"""Return the exponential learning rate factor gamma.
Parameters
----------
scheduling_factor :
By how much to reduce learning rate during training.
max_epochs : int
Maximum number of epochs.
"""
return (1 / scheduling_factor) ** (1 / max_epochs)
def replace_None_with_all(df, column):
"""Duplicate rows with `None` in `columns` to have one for all unique values.
Parameters
----------
df : pd.DataFrame
Dataframe from which to replace the values.
column : str
Name of the column in which to search for None.
Return
------
df : pd.DataFrame
Dataframe with the replicated rows.
Examples
--------
>>> df = pd.DataFrame([["model1",2,0.01],["model1",3,0.1],["model2",5,None]], columns=["Model","N Layers","Col"])
>>> df
Model N Layers Col
0 model1 2 0.01
1 model1 3 0.10
2 model2 5 NaN
>>> replace_None_with_all(df, "Col")
Model N Layers Col
0 model1 2 0.01
1 model1 3 0.10
2 model2 5 0.01
3 model2 5 0.10
"""
to_replicate = df[df[column].isin([None])].copy()
df = df[~df[column].isin([None])]
replicated = []
for val in df[column].unique():
to_replicate[column] = val
replicated.append(to_replicate.copy())
df = pd.concat([df, *replicated], ignore_index=True)
return df
def dict_none_toNaN(d):
return {k: v if v is not None else float("nan") for k, v in d.items()}
class SetLR(torch.optim.lr_scheduler._LRScheduler):
"""Set the learning rate of each parameter group.
When last_epoch=-1, sets initial lr as lr.
Args:
optimizer (Optimizer): Wrapped optimizer.
lr_lambda (function or list): A function which computes the new lr
given an integer parameter epoch, the current lr and the base lr,
or a list of such functions, one for each group in optimizer.param_groups.
last_epoch (int): The index of last epoch. Default: -1.
Example:
>>> # Assuming optimizer has two groups.
>>> lmbda = lambda epoch, cur_lr, base_lr: 0.95
>>> scheduler = SetLR(optimizer, lr_lambda=lmbda)
>>> for epoch in range(100):
>>> train(...)
>>> validate(...)
>>> scheduler.step()
"""
def __init__(self, optimizer, lr_lambda, last_epoch=-1):
self.optimizer = optimizer
if not isinstance(lr_lambda, list) and not isinstance(lr_lambda, tuple):
self.lr_lambdas = [lr_lambda] * len(optimizer.param_groups)
else:
if len(lr_lambda) != len(optimizer.param_groups):
raise ValueError(
"Expected {} lr_lambdas, but got {}".format(
len(optimizer.param_groups), len(lr_lambda)
)
)
self.lr_lambdas = list(lr_lambda)
self.last_epoch = last_epoch
super().__init__(optimizer, last_epoch)
def state_dict(self):
"""Returns the state of the scheduler as a :class:`dict`.
It contains an entry for every variable in self.__dict__ which
is not the optimizer.
The learning rate lambda functions will only be saved if they are callable objects
and not if they are functions or lambdas.
"""
state_dict = {
key: value
for key, value in self.__dict__.items()
if key not in ("optimizer", "lr_lambdas")
}
state_dict["lr_lambdas"] = [None] * len(self.lr_lambdas)
for idx, fn in enumerate(self.lr_lambdas):
if not isinstance(fn, types.FunctionType):
state_dict["lr_lambdas"][idx] = fn.__dict__.copy()
return state_dict
def load_state_dict(self, state_dict):
"""Loads the schedulers state.
Arguments:
state_dict (dict): scheduler state. Should be an object returned
from a call to :meth:`state_dict`.
"""
lr_lambdas = state_dict.pop("lr_lambdas")
self.__dict__.update(state_dict)
for idx, fn in enumerate(lr_lambdas):
if fn is not None:
self.lr_lambdas[idx].__dict__.update(fn)
def get_lr(self):
if self.last_epoch > 0:
return [
lmbda(self.last_epoch, group["lr"], base_lr)
for base_lr, lmbda, group in zip(
self.base_lrs, self.lr_lambdas, self.optimizer.param_groups
)
]
else:
return [base_lr for base_lr in self.base_lrs]
|
decodable_information_bottleneck-main
|
utils/helpers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import logging
import sys
import warnings
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap, ListedColormap
from dib.utils.helpers import tmp_seed
__all__ = ["plot_2D_decision_boundary"]
logger = logging.getLogger(__name__)
def make_cmap(cmap_dflt, alpha=1):
if isinstance(cmap_dflt, list):
colors = cmap_dflt
else:
colors = cmap_dflt(np.linspace(0, 1, 256), alpha=alpha)
cm = LinearSegmentedColormap.from_list("colormap", colors)
cm.set_under(alpha=0)
cm.set_over(alpha=0)
return cm
def get_sequential_colors(n):
"""
Return a list of n sequential color maps, the extreme color associated
with it (or similar color) and a bright similar color.
"""
assert n <= 10
# for binary classification same as using plt.cm.RdBu
cmaps = [
make_cmap(plt.cm.Blues),
make_cmap(plt.cm.Reds),
make_cmap(plt.cm.Greens),
make_cmap(plt.cm.Purples),
make_cmap(["white", "xkcd:dark grey"]),
make_cmap(plt.cm.Oranges),
make_cmap(["white", "xkcd:olive"]),
make_cmap(["white", "xkcd:brown"]),
make_cmap(["white", "xkcd:dark turquoise"]),
make_cmap(["white", "xkcd:bordeaux"]),
]
extreme_colors = [
"xkcd:darkish blue",
"xkcd:darkish red",
"xkcd:darkish green",
"xkcd:indigo",
"xkcd:dark grey",
"xkcd:dark orange",
"xkcd:olive",
"xkcd:brown",
"xkcd:dark turquoise",
"xkcd:bordeaux",
]
bright_colors = [
"xkcd:bright blue",
"xkcd:bright red",
"xkcd:green",
"xkcd:bright purple",
"k",
"xkcd:bright orange",
"xkcd:bright olive",
"xkcd:golden brown",
"xkcd:bright turquoise",
"xkcd:purple red",
]
return cmaps[:n], extreme_colors[:n], bright_colors[:n]
def plot_2D_decision_boundary(
X,
y,
model,
title=None,
ax=None,
n_mesh=50,
is_only_wrong=False,
is_force_no_proba=False,
n_max_scatter=100,
scatter_unlabelled_kwargs={
"c": "whitesmoke",
"alpha": 0.4,
"linewidths": 0.5,
"s": 10,
"marker": "o",
},
scatter_labelled_kwargs={"linewidths": 0.7,
"s": 50, "marker": "o", "alpha": 0.7, },
seed=123,
delta=0.5,
test=None,
):
"""Plot the 2D decision boundaries of a sklearn classification model.
Parameters
----------
X: array-like
2D input data
y: array-like
Labels, with `-1` for unlabeled points. Currently works with max 10 classes.
model: sklearn.BaseEstimator
Trained model. If `None` plot the dataset only.
title: str, optional
Title to add.
ax: matplotlib.axes, optional
Axis on which to plot.
n_mesh: int, optional
Number of points in each axes of the mesh. Increase to increase the quality.
50 is a good valuen for nice quality, 10 is faster but still ok.
is_only_wrong : bool, optional
Whether to plot only the wrong data points for simplicity.
is_force_no_proba : bool, optional
Whether not to plot probabilistic decision boundaries even if could.
n_max_scatter : int, optional
Maximum number of points to plot.
seed : int, optional
Pseudorandom seed. E.g. for selecting which points to plot.
delta : float, optional
How much space to add on the side of each points.
test : tuple of array like, optional
(X_test, y_train). If given will plot some test datapoints. Will also plot n max scatter
of them. Still in dev.
"""
X = np.array(X)
y = np.array(y)
if ax is None:
F, ax = plt.subplots(1, 1, figsize=(7, 7))
x_min, x_max = X[:, 0].min(), X[:, 0].max()
y_min, y_max = X[:, 1].min(), X[:, 1].max()
if test is not None:
X_test = np.array(test[0])
y_test = np.array(test[1])
x_min = min(x_min, X_test[:, 0].min())
x_max = min(x_max, X_test[:, 0].max())
y_min = min(y_min, X_test[:, 1].min())
y_max = min(y_max, X_test[:, 1].max())
x_min, x_max = x_min - delta, x_max + delta
y_min, y_max = y_min - delta, y_max + delta
xx, yy = np.meshgrid(
np.linspace(x_min, x_max, num=n_mesh), np.linspace(
y_min, y_max, num=n_mesh)
)
cmaps, extreme_colors, bright_colors = get_sequential_colors(max(y) + 1)
if model is not None:
if is_force_no_proba or not hasattr(model, "predict_proba"):
y_hat = model.predict(
np.c_[xx.ravel(), yy.ravel()].astype("float32"))
contourf_kwargs = dict(alpha=1, antialiased=True)
cmaps = [ListedColormap(extreme_colors)]
y_hat = y_hat.reshape(xx.shape)
# contourf does not work well without proba
plt.pcolormesh(
xx, yy, y_hat, cmap=ListedColormap(extreme_colors), **contourf_kwargs
)
else:
y_hat = model.predict_proba(
np.c_[xx.ravel(), yy.ravel()].astype("float32"))
y_hat = y_hat.reshape(xx.shape + (-1,))
y_argmax = y_hat.argmax(-1)
vmin = y_hat.max(-1).min()
contourf_kwargs = dict(
vmin=vmin,
vmax=1,
extend="neither",
levels=y_hat.shape[-1],
antialiased=True,
)
for i in range(y_hat.shape[-1]):
mask_plot = y_argmax == i
y_i = y_hat[:, :, i]
y_i[~mask_plot] = np.nan # don't plot if not predicted
with warnings.catch_warnings():
# warnings because of nan
warnings.simplefilter("ignore")
ax.contourf(
xx, yy, y_hat[:, :, i], cmap=cmaps[i], **contourf_kwargs
)
args_scatter = [
n_max_scatter,
model,
is_only_wrong,
seed,
scatter_unlabelled_kwargs,
scatter_labelled_kwargs,
bright_colors,
]
ax = plot_scatter_data(X, y, ax, *args_scatter)
if test is not None:
scatter_labelled_kwargs["marker"] = "*"
ax = plot_scatter_data(test[0], test[1], ax, *args_scatter,)
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xticks(())
ax.set_yticks(())
if title is not None:
ax.set_title(title)
return ax
def plot_scatter_data(
X,
y,
ax,
n_max_scatter,
model,
is_only_wrong,
seed,
scatter_unlabelled_kwargs,
scatter_labelled_kwargs,
bright_colors,
):
if is_only_wrong:
y_pred = model.predict(X.astype("float32"))
wrong_pred = y_pred != np.array(y)
# randomly select n_max_scatter
mask_select = np.zeros_like(y).astype(bool)
mask_select[:n_max_scatter] = True
with tmp_seed(seed):
np.random.shuffle(mask_select)
for i in np.unique(y):
idx = y == i
if is_only_wrong:
idx = np.logical_and(idx, wrong_pred)
idx = np.logical_and(idx, mask_select)
if i == -1:
scatter_kwargs = scatter_unlabelled_kwargs
else:
scatter_kwargs = scatter_labelled_kwargs
scatter_kwargs["c"] = bright_colors[i]
ax.scatter(X[idx, 0], X[idx, 1], edgecolors="k", **scatter_kwargs)
return ax
|
decodable_information_bottleneck-main
|
utils/visualize/visualize_clf.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
from .visualize_clf import *
from .visualize_imgs import *
|
decodable_information_bottleneck-main
|
utils/visualize/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import numpy as np
# example : https://github.com/matplotlib/matplotlib/issues/7008
def kwargs_log_xscale(x_data, mode="equidistant", base=None):
"""Return arguments to set log_scale as one would wish. mode=["smooth","equidistant"]."""
# if constant diff don't use logscale
if base == 1 or np.diff(x_data).var() == 0:
return dict(value="linear")
# automatically compute base
if base is None:
# take avg multiplier between each consecutive elements as base i.e 2,8,32 would be 4
# but 0.1,1,10 would be 10
base = int((x_data[x_data > 0][1:] /
x_data[x_data > 0][:-1]).mean().round())
if (x_data <= 0).any():
min_nnz_x = np.abs(x_data[x_data != 0]).min()
if mode == "smooth":
linscalex = np.log(np.e) / np.log(base) * (1 - (1 / base))
elif mode == "equidistant":
linscalex = 1 - (1 / base)
else:
raise ValueError(f"Unkown mode={mode}")
return dict(
value="symlog",
linthreshx=min_nnz_x,
basex=base,
subsx=list(range(base)),
linscalex=linscalex,
)
else:
return dict(value="log", basex=base, subsx=list(range(base)))
|
decodable_information_bottleneck-main
|
utils/visualize/helpers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import os
import random
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
import seaborn as sns
import torch
from skorch.dataset import unpack_data
from torchvision.utils import make_grid
from dib.utils.helpers import prod, set_seed
__all__ = ["plot_dataset_samples_imgs"]
DFLT_FIGSIZE = (17, 9)
def remove_axis(ax, is_rm_ticks=True, is_rm_spines=True):
"""Remove all axis but not the labels."""
if is_rm_spines:
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.spines["left"].set_visible(False)
if is_rm_ticks:
ax.tick_params(bottom="off", left="off")
def plot_dataset_samples_imgs(
dataset, n_plots=4, figsize=DFLT_FIGSIZE, ax=None, pad_value=1, seed=123, title=None
):
"""Plot `n_samples` samples of the a datset."""
set_seed(seed)
if ax is None:
fig, ax = plt.subplots(figsize=figsize)
img_tensor = torch.stack(
[dataset[random.randint(0, len(dataset) - 1)][0] for i in range(n_plots)], dim=0
)
grid = make_grid(img_tensor, nrow=2, pad_value=pad_value)
ax.imshow(grid.permute(1, 2, 0).numpy())
ax.axis("off")
if title is not None:
ax.set_title(title, fontsize=18)
|
decodable_information_bottleneck-main
|
utils/visualize/visualize_imgs.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import glob
import logging
import os
import numpy as np
import torch
from PIL import Image
from torch.utils.data import Dataset
from torchvision import datasets, transforms
from dib.utils.helpers import to_numpy
from .base import BaseDataset
from .helpers import bw_to_color, get_masks_drop_features, overlay_save_datasets
COLOUR_BLACK = torch.tensor([0.0, 0.0, 0.0])
COLOUR_WHITE = torch.tensor([1.0, 1.0, 1.0])
COLOUR_BLUE = torch.tensor([0.0, 0.0, 1.0])
DATASETS_DICT = {
"mnist": "MNIST",
"cifar10": "CIFAR10",
"cifar100": "CIFAR100",
"bincifar100": "BinaryCIFAR100",
"binsvhn": "BinarySVHN",
"binmnist": "BinaryMNIST",
"coloredmnist": "ColoredMNIST",
"svhn": "SVHN",
"bincifar10mnist": "BinCifar10Mnist",
"cifar10mnist": "Cifar10Mnist",
"cifar10mnistshift": "Cifar10MnistShift",
"bincifar10mnistdep9": "BinCifar10MnistDep9",
"bincifar10mnistdep8": "BinCifar10MnistDep8",
"bincifar10mnistdep7": "BinCifar10MnistDep7",
"bincifar10mnistdep5": "BinCifar10MnistDep5",
"bincifar10mnistdep3": "BinCifar10MnistDep3",
"cifar10mnistdep9": "Cifar10MnistDep9",
"cifar10mnistdep8": "Cifar10MnistDep8",
"cifar10mnistdep7": "Cifar10MnistDep7",
"cifar10mnistdep5": "Cifar10MnistDep5",
"cifar10mnistdep3": "Cifar10MnistDep3",
}
DATASETS = list(DATASETS_DICT.keys())
logger = logging.getLogger(__name__)
# HELPERS
def get_Dataset(dataset):
"""Return the correct uninstantiated datasets."""
dataset = dataset.lower()
try:
# eval because stores name as string in order to put it at top of file
return eval(DATASETS_DICT[dataset])
except KeyError:
raise ValueError("Unkown dataset: {}".format(dataset))
def get_img_size(dataset):
"""Return the correct image size."""
return get_Dataset(dataset).shape
class ImgDataset(BaseDataset):
"""Image dataset wrapper that adds nice functionalitites.
Parameters
----------
is_augment : bool, optional
Whether to transform the training set (and thus validation).
split : {'train', 'test', ...}, optional
According dataset is selected.
translation : int or sequence of int, optional
Max translation of the image translate the images when using data augmentation.
is_flip : int, optional
Whether to apply horizontal flipping. Only applied if not a number
rotation : float or sequence of float, optional
Range of degrees to select from.
is_normalize : bool, optional
Whether to normalize the dataset.
target_transform : callable, optional
Transformation of the target.
"""
is_numbers = False
def __init__(
self,
*args,
is_augment=True,
split="train",
translation=4,
is_flip=True,
rotation=15,
is_normalize=True,
target_transform=None,
**kwargs,
):
super().__init__(*args, **kwargs)
self.is_augment = is_augment
self.split = split
self.is_drop_features = False # by default return all features
self.translation = translation
self.is_flip = is_flip and not self.is_numbers
self.rotation = rotation
self.is_normalize = is_normalize
self.target_transform = target_transform
if self.is_augment and self.split == "train":
self.transform = transforms.Compose(self.get_train_transforms())
else:
self.transform = transforms.Compose(self.get_test_transforms())
def get_train_transforms(self):
"""Return the training transformation."""
return [
transforms.Resize((self.shape[1], self.shape[2])),
# the following performs translation
transforms.RandomCrop(
(self.shape[1], self.shape[2]), padding=self.translation
),
# don't flip if working with numbers
transforms.RandomHorizontalFlip() if self.is_flip else torch.nn.Identity(),
transforms.RandomRotation(self.rotation),
transforms.ToTensor(),
transforms.Normalize(self.mean, self.std)
if self.is_normalize
else torch.nn.Identity(),
]
def get_test_transforms(self):
"""Return the testing transformation."""
return [
transforms.Resize((self.shape[1], self.shape[2])),
transforms.ToTensor(),
transforms.Normalize(self.mean, self.std)
if self.is_normalize
else torch.nn.Identity(),
]
def rm_transformations(self):
"""Completely remove transformation. Used to plot or compute mean and variance."""
self.transform = transforms.Compose([transforms.ToTensor()])
def make_test(self):
"""Make the data a test set."""
self.transform = transforms.Compose(self.get_test_transforms())
def drop_features_(self, drop_size):
"""Drop part of the features (pixels in images).
Note
----
- this function actually just precomputes the `self.to_drop` of values that should be droped
the dropping is in `__get_item__`.
Parameters
----------
drop_size : float or int or tuple, optional
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to
drop. If int, represents the number of datapoints to drop. If tuple, same as before
but give bounds (min and max). 0 means keep all.
"""
self.logger.info(f"drop_features_ {drop_size} features...")
assert not self.is_drop_features, "cannot drop multiple times the features"
self.is_drop_features = True
self.to_drop = get_masks_drop_features(
drop_size, [self.shape[1], self.shape[2]], len(self), seed=self.seed
)
def __getitem__(self, index):
if self.targets.ndim > 1:
multi_target = self.targets
# often datasets have code that can only deal with a single target
self.targets = multi_target[:, 0]
X, target = super().__getitem__(index)
self.targets = multi_target # set back multi targets
multi_target = (target,) + tuple(self.targets[index, 1:])
else:
X, target = super().__getitem__(index)
multi_target = (target,)
multi_target = self.add_index(multi_target, index)
if self.is_drop_features:
X[:, self.to_drop[index]] = float("nan")
return X, multi_target
# TORCHVISION DATASETS
class SVHN(ImgDataset, datasets.SVHN):
"""SVHN wrapper. Docs: `datasets.SVHN.`
Parameters
----------
kwargs:
Additional arguments to `ImgDataset`.
Examples
--------
>>> data = SVHN(split="train") #doctest:+ELLIPSIS
Using ...
>>> len(data)
73257
>>> len(data) == len(data.data) == len(data.targets)
True
>>> [type(i) for i in data[0]]
[<class 'torch.Tensor'>, <class 'int'>]
>>> from .helpers import get_mean_std
>>> mean, std = get_mean_std(data)
>>> (str(list(mean)) == str(data.mean)) and (str(list(std)) == str(data.std))
True
>>> train, valid = data.train_test_split(size=1000, is_stratify=True)
>>> len(valid)
1000
>>> data.drop_labels_(0.9)
>>> round(len([t for t in data.targets if t == -1]) / len(data), 1)
0.9
>>> data.balance_labels_()
>>> len(data)
131864
>>> data.drop_unlabelled_()
>>> len(data)
65932
>>> data.drop_features_(0.7)
>>> round((torch.isnan(data[0][0])).float().mean().item(), 1)
0.7
>>> data.set_test_transforms() # for replicability
>>> data[0][0][0] # showing image for one channel
tensor([[ nan, nan, 0.2850, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, 0.2652, nan, ..., nan, nan, nan],
...,
[ 0.1067, nan, 0.1860, ..., -0.4477, nan, nan],
[ nan, nan, nan, ..., nan, nan, nan],
[ nan, nan, nan, ..., nan, 0.1067, nan]])
>>> data[0][1]
1
>>> data.randomize_targets_()
>>> data[0][1]
8
"""
shape = (3, 32, 32)
missing_px_color = COLOUR_BLACK
n_classes = 10
n_train = 73257
mean = [0.43768448, 0.4437684, 0.4728041]
std = [0.19803017, 0.20101567, 0.19703583]
is_numbers = True
def __init__(self, **kwargs):
ImgDataset.__init__(self, **kwargs)
datasets.SVHN.__init__(
self,
self.root,
download=True,
split=self.split,
transform=self.transform,
target_transform=self.target_transform,
)
self.labels = to_numpy(self.labels)
if self.is_random_targets:
self.randomize_targets_()
@property
def targets(self):
# make compatible with CIFAR10 dataset
return self.labels
@targets.setter
def targets(self, values):
self.labels = values
class CIFAR10(ImgDataset, datasets.CIFAR10):
"""CIFAR10 wrapper. Docs: `datasets.CIFAR10.`
Parameters
----------
kwargs:
Additional arguments to `datasets.CIFAR10` and `ImgDataset`.
Examples
--------
See SVHN for more examples.
>>> data = CIFAR10(split="train") #doctest:+ELLIPSIS
Files ...
>>> from .helpers import get_mean_std
>>> mean, std = get_mean_std(data)
>>> list(std)
[0.24703279, 0.24348423, 0.26158753]
>>> (str(list(mean)) == str(data.mean)) and (str(list(std)) == str(data.std))
True
"""
shape = (3, 32, 32)
n_classes = 10
missing_px_color = COLOUR_BLACK
n_train = 50000
mean = [0.4914009, 0.48215896, 0.4465308]
std = [0.24703279, 0.24348423, 0.26158753]
def __init__(self, **kwargs):
ImgDataset.__init__(self, **kwargs)
datasets.CIFAR10.__init__(
self,
self.root,
download=True,
train=self.split == "train",
transform=self.transform,
target_transform=self.target_transform,
)
self.targets = to_numpy(self.targets)
if self.is_random_targets:
self.randomize_targets_()
class CIFAR100(ImgDataset, datasets.CIFAR100):
"""CIFAR100 wrapper. Docs: `datasets.CIFAR100.`
Parameters
----------
root : str, optional
Path to the dataset root. If `None` uses the default one.
split : {'train', 'test'}, optional
According dataset is selected.
kwargs:
Additional arguments to `datasets.CIFAR100` and `ImgDataset`.
Examples
--------
See SVHN for more examples.
>>> data = CIFAR100(split="train") #doctest:+ELLIPSIS
Files ...
>>> from .helpers import get_mean_std
>>> mean, std = get_mean_std(data)
>>> (str(list(mean)) == str(data.mean)) and (str(list(std)) == str(data.std))
True
"""
shape = (3, 32, 32)
n_classes = 100
n_train = 50000
missing_px_color = COLOUR_BLACK
mean = [0.5070754, 0.48655024, 0.44091907]
std = [0.26733398, 0.25643876, 0.2761503]
def __init__(self, **kwargs):
ImgDataset.__init__(self, **kwargs)
datasets.CIFAR100.__init__(
self,
self.root,
download=True,
train=self.split == "train",
transform=self.transform,
target_transform=self.target_transform,
)
self.targets = to_numpy(self.targets)
if self.is_random_targets:
self.randomize_targets_()
class MNIST(ImgDataset, datasets.MNIST):
"""MNIST wrapper. Docs: `datasets.MNIST.`
Parameters
----------
root : str, optional
Path to the dataset root. If `None` uses the default one.
split : {'train', 'test', "extra"}, optional
According dataset is selected.
kwargs:
Additional arguments to `datasets.MNIST` and `ImgDataset`.
Examples
--------
See SVHN for more examples.
>>> data = MNIST(split="train")
>>> from .helpers import get_mean_std
>>> mean, std = get_mean_std(data)
>>> (str(list(mean)) == str(data.mean)) and (str(list(std)) == str(data.std))
True
"""
shape = (1, 32, 32)
n_classes = 10
n_examples = 60000
missing_px_color = COLOUR_BLUE
mean = [0.13066062]
std = [0.30810776]
is_numbers = True
def __init__(self, **kwargs):
ImgDataset.__init__(self, **kwargs)
datasets.MNIST.__init__(
self,
self.root,
download=True,
train=self.split == "train",
transform=self.transform,
target_transform=self.target_transform,
)
self.targets = to_numpy(self.targets)
if self.is_random_targets:
self.randomize_targets_()
def append_(self, other):
"""Append a dataset to the current one."""
# mnist data is in torch format
self.data = torch.cat([self.data, other.data], dim=0)
self.targets = np.append(self.targets, other.targets, axis=0)
class ColoredMNIST(MNIST):
"""Colored binary MNIST where the color is a noisy predictor of the label. Binary label is larger
or smaller than 10.
Parameters
----------
root : str, optional
Path to the dataset root. If `None` uses the default one.
split : {'train', 'test', "extra"}, optional
According dataset is selected.
noise : float, optional
Probability of being wrong when only considering the color.
is_fixed : bool, optional
Whether to resample colors at every epoch though the noise.
is_wrong_test : bool, optional
Whether to use a test set where color is useless (wrong 100 %) of the time.
kwargs:
Additional arguments to `datasets.MNIST` and `ImgDataset`.
Examples
--------
>>> import pandas as pd
>>> import numpy as np
>>> n = 2000
>>> data = ColoredMNIST(split="train", noise=0.2)
>>> df = pd.DataFrame([(data[i][1][0], int(data[i][0][0].sum() == 0)) for i in range(n)], columns = ["lab","col"])
>>> out = df.groupby(["lab","col"]).size().values / n
>>> (np.array([0.37,0.07,0.07,0.37]) < out).all() and (out < np.array([0.43,0.13,0.13,0.43])).all()
True
>>> data = ColoredMNIST(split="test", noise=0.2, is_noisy_test=True)
>>> df = pd.DataFrame([(data[i][1][0], int(data[i][0][0].sum() == 0)) for i in range(n)], columns = ["lab","col"])
>>> out = df.groupby(["lab","col"]).size().values / n
>>> (np.array([0.00,0.47,0.47,0.00]) <= out).all() and (out < np.array([0.08,0.48,0.48,0.08])).all()
True
"""
shape = (3, 32, 32)
n_classes = 2
n_examples = 60000
def __init__(self, noise=0.1, is_fixed=True, is_noisy_test=True, **kwargs):
super().__init__(**kwargs)
self.targets = (self.targets < 5).astype(int)
self.is_fixed = is_fixed
self.noise = noise
self.is_noisy_test = is_noisy_test
if self.is_noisy_test and self.split == "test":
self.noise = 1
@property
def raw_folder(self):
# use mnist data
return os.path.join(self.root, "MNIST", "raw")
@property
def processed_folder(self):
# use mnist data
return os.path.join(self.root, "MNIST", "processed")
def __getitem__(self, index):
X, multi_target = super().__getitem__(index)
# by using the index for the seed ensures that same across epochs
seed = index if self.is_fixed else None
if multi_target[0] == 0:
X = bw_to_color(
X, rgb_proba=[1 - self.noise, self.noise, 0], seed=seed)
else:
X = bw_to_color(
X, rgb_proba=[self.noise, 1 - self.noise, 0], seed=seed)
return X, multi_target
# OTHER DATASETS
class BinaryCIFAR100(CIFAR100):
"""Like CIFAR100 but 2 class (odd or even)."""
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
class BinarySVHN(SVHN):
"""Like SVHN but 2 class (odd or even)."""
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
class BinaryMNIST(MNIST):
"""Like MNIST but 2 class (odd or even)."""
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
@property
def raw_folder(self):
# use mnist data
return os.path.join(self.root, "MNIST", "raw")
@property
def processed_folder(self):
# use mnist data
return os.path.join(self.root, "MNIST", "processed")
class OverlayedDatasetBase(ImgDataset, Dataset):
"""Overlays 2 other datasets.
Note
----
- Randomization of targets / stratification / ... Will still be done on the actual
targets instead of the distractor.
- only overlaying train and test and not other splits.
- The second value of the target will be the distractor. Index is always last.
- This a base class that should be inherited from. The only required changed are
adding class generic attributed such as Overlayed, Background, shift.
See Cifar10Mnist for an example.
Parameters.
----------
kwargs :
Additional arguments to ImgDataset.
Attributes
----------
Background : ImgDataset
Dataset to use as background.
Overlayed : ImgDataset
Dataset to overlay on the background. Currently the following assumptions are made:
- the overlaid images have to be at most as big as the background ones (i.e.
`height_bckgrnd <= height_bckgrnd` and `<= width_bckgrnd`).
- The overlayed images are also used as mask. This is especially good for black
and white images : whiter pixels (~1) are the ones to be overlayed. In the case
of colored image, this still hold but channel wise.
dependence : dict of float, optional
Whether to overlay in a way where there is dependencies between the background and the overlayed data.
Dictionary because can be different for each split.
"""
add_dist = True # whether to add the distractor to the target
named_dir = None
# Whether to randomly shift all overlayed images or to keep them on the bottom right.
is_shift = False
dependence = dict(train=0, test=0)
def __init__(self, **kwargs):
ImgDataset.__init__(self, **kwargs)
name = self.named_dir if self.named_dir is not None else type(
self).__name__
self.dir = os.path.join(self.root, name)
if not os.path.isdir(self.dir):
self.make_dataset()
self.data = np.load(os.path.join(self.dir, f"{self.split}_x.npy"))
self.targets = np.load(os.path.join(self.dir, f"{self.split}_y.npy"))
self.distractor = np.load(
os.path.join(self.dir, f"{self.split}_y_distractor.npy")
)
if self.is_random_targets:
self.randomize_targets_() # doesn't randomize distractors
def __len__(self):
return len(self.targets)
@property
def map_target_position(self):
"""
Return a dictionary that maps the type of target (e.g. "index") to its position in the
outputted target.
"""
map_target_position = super().map_target_position
if self.add_dist:
map_target_position["distractor"] = len(map_target_position)
return map_target_position
def add_distractor(self, y, index):
"""Append the distractor to the targets."""
if self.add_dist:
try:
y = tuple(y) + (self.distractor[index],)
except TypeError:
y = [y, self.distractor[index]]
return y
def _switch_distractor_target(self):
"""Switch the distractor and the target."""
self.targets, self.distractor = self.distractor, self.targets
def keep_indcs_(self, indcs):
super().keep_indcs_(indcs)
self.distractor = self.distractor[indcs]
def __getitem__(self, index):
img = self.data[index]
target = self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
# make sure it's a tuple
try:
target = tuple(target)
except TypeError:
target = (target,)
target = self.add_index(target, index)
target = self.add_distractor(target, index)
if self.is_drop_features:
img[:, self.to_drop[index]] = float("nan")
return img, target
def make_dataset(self):
logger.info("Overlaying the datasets...")
overlay_train = self.Overlayed()
bckgrnd_train = self.Background()
overlay_train.rm_transformations()
bckgrnd_train.rm_transformations()
overlay_test = self.Overlayed(split="test")
bckgrnd_test = self.Background(split="test")
overlay_test.rm_transformations()
bckgrnd_test.rm_transformations()
bckgrnd_datasets = (
[bckgrnd_train.data, bckgrnd_train.targets],
[bckgrnd_test.data, bckgrnd_test.targets],
)
overlay_datasets = (
[overlay_train.data, overlay_train.targets],
[overlay_test.data, overlay_test.targets],
)
overlay_save_datasets(
bckgrnd_datasets,
overlay_datasets,
folder=self.dir,
is_shift=self.is_shift,
dependence=self.dependence,
)
def append_(self, other):
super().append_(other)
self.distractor = np.append(self.distractor, other.distractor, axis=0)
class Cifar10Mnist(OverlayedDatasetBase):
shape = (3, 32, 32)
n_classes = 10
missing_px_color = COLOUR_BLACK
n_train = 50000
mean = [0.52897614, 0.5220055, 0.49050677]
std = [0.2650898, 0.263235, 0.28332546]
Overlayed = MNIST
Background = CIFAR10
is_shift = False
class Cifar10MnistShift(Cifar10Mnist):
is_shift = True
class BinCifar10Mnist(Cifar10Mnist):
"""Like Cifar10Mnist but 2 class (odd or even)."""
named_dir = "Cifar10Mnist" # same dataset as Cifar10Mnist => don't recreate
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
# Correlated overlayed
class Cifar10MnistDep9(Cifar10Mnist):
"""Cifar10mnist where the mnist is correlated with cifar10."""
dependence = dict(train=0.9, test=0)
class Cifar10MnistDep8(Cifar10Mnist):
"""Cifar10mnist where the mnist is correlated with cifar10."""
dependence = dict(train=0.8, test=0)
class Cifar10MnistDep7(Cifar10Mnist):
"""Cifar10mnist where the mnist is correlated with cifar10."""
dependence = dict(train=0.7, test=0)
class Cifar10MnistDep5(Cifar10Mnist):
"""Cifar10mnist where the mnist is correlated with cifar10."""
dependence = dict(train=0.5, test=0)
class Cifar10MnistDep3(Cifar10Mnist):
"""Cifar10mnist where the mnist is correlated with cifar10."""
dependence = dict(train=0.3, test=0)
class BinCifar10MnistDep9(Cifar10MnistDep9):
"""Like Cifar10Mnist but 2 class (odd or even)."""
named_dir = "Cifar10MnistDep9" # same dataset as Cifar10Mnist => don't recreate
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
class BinCifar10MnistDep8(Cifar10MnistDep8):
"""Like Cifar10Mnist but 2 class (odd or even)."""
named_dir = "Cifar10MnistDep8" # same dataset as Cifar10Mnist => don't recreate
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
class BinCifar10MnistDep7(Cifar10MnistDep7):
"""Like Cifar10Mnist but 2 class (odd or even)."""
named_dir = "Cifar10MnistDep7" # same dataset as Cifar10Mnist => don't recreate
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
class BinCifar10MnistDep5(Cifar10MnistDep5):
"""Like Cifar10Mnist but 2 class (odd or even)."""
named_dir = "Cifar10MnistDep5" # same dataset as Cifar10Mnist => don't recreate
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
class BinCifar10MnistDep3(Cifar10MnistDep3):
"""Like Cifar10Mnist but 2 class (odd or even)."""
named_dir = "Cifar10MnistDep3" # same dataset as Cifar10Mnist => don't recreate
n_classes = 2
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.targets = self.targets % 2
|
decodable_information_bottleneck-main
|
utils/data/imgs.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
def get_train_dev_test_datasets(dataset, data_type, valid_size=0.1, **kwargs):
"""Return the correct instantiated train, validation, test dataset
Parameters
----------
dataset : str
Name of the dataset to load.
data_type : {"imgs"}
Type of dataset.
valid_size : float or int, optional
Size of the validation set. If float, should be between 0.0 and 1.0 and represent the
proportion of the dataset. If int, represents the absolute number of valid samples.
0 if no validation.
Returns
-------
datasets : dictionary of torch.utils.data.Dataset
Dictionary of the `"train"`, `"valid"`, and `"valid"`.
"""
datasets = dict()
if data_type == "imgs":
from .imgs import get_Dataset
Dataset = get_Dataset(dataset)
dataset = Dataset(split="train", **kwargs)
if valid_size != 0:
datasets["train"], datasets["valid"] = dataset.train_test_split(size=valid_size)
else:
datasets["train"], datasets["valid"] = dataset, None
datasets["test"] = Dataset(split="test", **kwargs)
return datasets
|
decodable_information_bottleneck-main
|
utils/data/__init__.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import os
import numpy as np
import torch
from dib.utils.datasplit import RandomMasker
from dib.utils.helpers import tmp_seed, to_numpy
def get_masks_drop_features(drop_size, mask_shape, n_masks, n_batch=32, seed=123):
"""
Parameters
----------
drop_size : float or int or tuple, optional
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to
drop. If int, represents the number of datapoints to drop. If tuple, same as before
but give bounds (min and max). 0 means keep all.
mask_shape : tuple of int or callable
Shape of the mask for one example. If callable, it is given the current index.
n_masks : int, optional
Number of masks to return.
n_batch : int, optional
Size of the batches of masks => number fo concsecutive examples with the same abount of
kept features.
seed : int, optional
Random seed.
Returns
-------
to_drops : list of torch.BoolTensor
List of length n_masks where each element is a boolean tensor of shape `mask_shape` with
1s where features should be droped.
Examples
--------
>>> get_masks_drop_features(0.5, (10,), 1, n_batch=1)
[tensor([ True, False, False, False, True, True, False, True, True, False])]
"""
try:
mask_shape(0)
except TypeError:
def mask_shape(_, ret=mask_shape):
return ret
if drop_size == 0:
return [torch.zeros(1, *mask_shape(i)).bool() for i in n_batch]
with tmp_seed(seed):
try:
droper = RandomMasker(
min_nnz=drop_size[0], max_nnz=drop_size[1], is_batch_share=False
)
except TypeError:
droper = RandomMasker(
min_nnz=drop_size, max_nnz=drop_size, is_batch_share=False
)
to_drops = []
for i in range(0, n_masks, n_batch):
to_drop = droper(n_batch, mask_shape(i))
to_drops.extend(torch.unbind(to_drop.bool(), dim=0))
return to_drops
def get_mean_std(dataset):
"""Return the mean and std of a datset.
Examples
--------
>>> from .imgs import get_Dataset
>>> import numpy as np
>>> cifar10 = get_Dataset("cifar10")(split="test")
Files already downloaded and verified
>>> get_mean_std(cifar10)
(array([0.49421427, 0.4851322 , 0.45040992], dtype=float32), array([0.24665268, 0.24289216, 0.2615922 ], dtype=float32))
"""
dataset.rm_transformations()
data = torch.stack([el[0] for el in dataset], dim=0)
return np.mean(data.numpy(), axis=(0, 2, 3)), np.std(data.numpy(), axis=(0, 2, 3))
def overlay_save_datasets(
bckgrnd_datasets,
to_overlay_datasets,
folder="data/",
split_names=["train", "test"],
dependence=dict(train=0, test=0),
**kwargs
):
"""Overlay corresponding train and test datasetsand save the output to file.
Parameters
----------
bckgrnd_datasets : tuple of tuple of array like
Background datasets on which to overlay the others. The exterior tuple corresponds to the
split (train, test, ...), the interior tuple is imgs/label: `((train_imgs, train_labels), ...)`,
image arrays should be of shape [n_bckgrnd, height_bckgrnd, width_bckgrnd, ...] and
dtype=uint8. Labels should be shape=[n_imgs, ...] and dtype=*.
to_overlay_datasets : tuple of array like
Datasets to overlay. Same shape and form as the previous argument `bckgrnd_datasets`.
folder : str, optional
Folder to which to save the images.
split_names : list of str, optional
Names of all the splits, should be at least as long as len(bckgrnd_datasets).
dependence : dict of float, optional
Whether to overlay in a way where there is dependencies between the background and the overlayed data.
Dictionary because can be different for each split.
kwargs :
Additional arguments to `overlay_img`.
"""
is_missing_names = len(split_names) < len(bckgrnd_datasets)
if is_missing_names or len(bckgrnd_datasets) != len(to_overlay_datasets):
err = "Sizes don't agree `len(split_names)={}, len(bckgrnd_datasets)={}, len(to_overlay_datasets)={}`."
raise ValueError(
err.format(
len(split_names), len(bckgrnd_datasets), len(
to_overlay_datasets)
)
)
if not os.path.exists(folder):
os.makedirs(folder)
for i, (bckgrnd, to_overlay, name) in enumerate(
zip(bckgrnd_datasets, to_overlay_datasets, split_names)
):
if to_overlay[0] is not None and bckgrnd[0] is not None:
if dependence is not None and dependence[name] != 0:
out, idcs = overlay_img_dependencies(
bckgrnd[0],
to_overlay[0],
bckgrnd[1],
to_overlay[1],
dependence=dependence[name],
**kwargs
)
else:
# no dependecies between bckground and overlay
out, idcs = overlay_img(bckgrnd[0], to_overlay[0], **kwargs)
np.save(os.path.join(folder, name + "_x.npy"),
out, allow_pickle=False)
np.save(
os.path.join(folder, name + "_y.npy"),
to_numpy(bckgrnd[1]),
allow_pickle=False,
)
np.save(
os.path.join(folder, name + "_y_distractor.npy"),
to_numpy(to_overlay[1])[idcs],
allow_pickle=False,
)
def overlay_img_dependencies(
bckgrnd,
to_overlay,
bckgrnd_labels,
to_overlay_labels,
dependence=0.5,
seed=123,
**kwargs
):
"""Overlays an image `to_overlay` on a `bckgrnd` with dependencies between the labels.
Parameters
----------
bckgrnd : array like, shape=[n_bckgrnd, height_bckgrnd, width_bckgrnd, ...], dtype=uint8
Background images. Each image will have one random image from `to_overlay` overlayed on it.
to_overlay : array like, shape=[n_overlay, height_overlay, width_overlay, ...], dtype=uint8
Images to overlay. Currently the following assumptions are made:
- the overlaid images have to be at most as big as the background ones (i.e.
`height_bckgrnd <= height_bckgrnd` and `<= width_bckgrnd`).
- The overlayed images are also used as mask. This is especially good for black
and white images : whiter pixels (~1) are the ones to be overlayed. In the case
of colored image, this still hold but channel wise.
bckgrnd_labels : array like, shape=[n_bckgrnd]
Labels of the background images.
to_overlay_labels : array like, shape=[n_overlay]
Labels of the images to overlay. The number of unique labels need to be larger than the unique
labels of background images.
dependence : float, optional
Level of positive dependence in [0,1]. If 0 no dependence. If 1 then label of overlayed
is the same as label of background all the time.
seed : int, optional
Pseudo random seed.
kwargs :
Additional arguments to `overlay_img`.
"""
bckgrnd_labels = to_numpy(bckgrnd_labels)
to_overlay_labels = to_numpy(to_overlay_labels)
bckgrnd = to_numpy(bckgrnd)
to_overlay = to_numpy(to_overlay)
out_imgs = np.zeros_like(bckgrnd)
out_idcs = np.zeros_like(bckgrnd_labels)
for i in np.unique(bckgrnd_labels):
bckgrnd_i = bckgrnd[bckgrnd_labels == i]
to_overlay_i = to_overlay[to_overlay_labels == i]
n_bckgrnd_i = bckgrnd_i.shape[0]
n_overlay_i = to_overlay_i.shape[0]
with tmp_seed(seed):
n_dependent = int(dependence * n_bckgrnd_i)
idx_to_overlay_i = np.random.choice(
range(n_overlay_i), size=n_dependent)
to_overlay_i = to_overlay_i[idx_to_overlay_i]
out, idcs = overlay_img(
bckgrnd_i[:n_dependent], to_overlay_i, seed=seed, **kwargs
)
# indices in terms of the actual indices
idcs = idx_to_overlay_i[idcs]
idcs = np.flatnonzero(to_overlay_labels == i)[idcs]
if n_dependent < n_bckgrnd_i:
with tmp_seed(seed):
# sampling without dependency => from the entire set
n_independent = n_bckgrnd_i - n_dependent
idx_to_overlay_i = np.random.choice(
range(to_overlay.shape[0]), size=n_independent
)
out_indep, idcs_indep = overlay_img(
bckgrnd_i[n_dependent:],
to_overlay[idx_to_overlay_i],
seed=seed,
**kwargs
)
# indices in terms of the actual indices
idcs_indep = idx_to_overlay_i[idcs_indep]
out = np.concatenate([out, out_indep])
idcs = np.concatenate([idcs, idcs_indep])
# put it in order compared to initial order of background
out_imgs[bckgrnd_labels == i] = out
out_idcs[bckgrnd_labels == i] = idcs
return out_imgs, out_idcs
def overlay_img(bckgrnd, to_overlay, is_shift=False, seed=123):
"""Overlays an image with black background `to_overlay` on a `bckgrnd`
Parameters
----------
bckgrnd : array like, shape=[n_bckgrnd, height_bckgrnd, width_bckgrnd, ...], dtype=uint8
Background images. Each image will have one random image from `to_overlay` overlayed on it.
to_overlay : array like, shape=[n_overlay, height_overlay, width_overlay, ...], dtype=uint8
Images to overlay. Currently the following assumptions are made:
- the overlaid images have to be at most as big as the background ones (i.e.
`height_bckgrnd <= height_bckgrnd` and `<= width_bckgrnd`).
- The overlayed images are also used as mask. This is especially good for black
and white images : whiter pixels (~1) are the ones to be overlayed. In the case
of colored image, this still hold but channel wise.
is_shift : bool, optional
Whether to randomly shift all overlayed images or to keep them on the bottom right.
seed : int, optional
Pseudo random seed.
Return
------
imgs : np.array, shape=[n_bckgrnd, height, width, 3], dtype=uint8
Overlayed images.
selected : np.array, shape=[n_bckgrnd], dtype=int64
Indices of the selected overlayed images.
"""
bckgrnd = to_numpy(bckgrnd)
to_overlay = to_numpy(to_overlay)
with tmp_seed(seed):
n_bckgrnd = bckgrnd.shape[0]
n_overlay = to_overlay.shape[0]
selected = np.random.choice(np.arange(n_overlay), size=n_bckgrnd)
to_overlay = to_overlay[selected, ...]
bckgrnd = ensure_color(bckgrnd).astype(np.float32)
to_overlay = ensure_color(to_overlay).astype(np.float32)
over_shape = to_overlay.shape[1:]
bck_shape = bckgrnd.shape[1:]
def get_margin(i): return (bck_shape[i] - over_shape[i]) // 2
def get_max_shift(i): return get_margin(i) + over_shape[i] // 3
get_shift = (
lambda i: np.random.randint(-get_max_shift(i), get_max_shift(i))
if is_shift
else get_max_shift(i) // 2
)
resized_overlay = np.zeros((n_bckgrnd,) + bck_shape[:2] + over_shape[2:])
resized_overlay[
:,
get_margin(0): -get_margin(0) or None,
get_margin(1): -get_margin(1) or None,
] = to_overlay
for i in range(2): # shift x and y
resized_overlay = np.stack(
[np.roll(im, get_shift(i), axis=i) for im in resized_overlay]
)
mask = resized_overlay / 255
return (mask * resized_overlay + (1 - mask) * bckgrnd).astype(np.uint8), selected
def at_least_ndim(arr, ndim):
"""Ensures that a numpy array is at least `ndim`-dimensional."""
padded_shape = arr.shape + (1,) * (ndim - len(arr.shape))
return arr.reshape(padded_shape)
def ensure_color(imgs):
"""
Ensures that a batch of colored (3 channels) or black and white (1 channels) numpy uint 8 images
is colored (3 channels).
"""
imgs = at_least_ndim(imgs, 4)
if imgs.shape[-1] == 1:
imgs = np.repeat(imgs, 3, axis=-1)
return imgs
def bw_to_color(img, rgb_proba=[1, 0, 0], seed=None):
"""Transform black and white image to red green or blue with a given probability."""
with tmp_seed(seed):
channel_to_color = np.random.choice(3, size=1, p=rgb_proba)
frame = torch.zeros_like(img.expand(3, -1, -1))
frame[channel_to_color] = img
return frame
|
decodable_information_bottleneck-main
|
utils/data/helpers.py
|
"""
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import copy
import logging
import os
import random
import numpy as np
import torch
from sklearn.model_selection import train_test_split
from sklearn.utils import resample
from dib import UNLABELLED_CLASS
from dib.utils.helpers import tmp_seed
DIR = os.path.abspath(os.path.dirname(__file__))
class BaseDataset:
"""BaseDataset that should be inherited by the format-specific ones.
Parameters
----------
root : str, optional
Root to the data directory.
logger : logging.Logger, optional
Logger
is_return_index : bool, optional
Whether to return the index in addition to the labels.
is_random_targets : bool, optional
Whether to use random targets for the dataset.
seed : int, optional
Random seed.
"""
unlabelled_class = UNLABELLED_CLASS
def __init__(
self,
root=os.path.join(DIR, "../../../../data/"),
logger=logging.getLogger(__name__),
is_return_index=False,
is_random_targets=False,
seed=123,
):
self.seed = seed
self.logger = logger
self.root = root
self.is_return_index = is_return_index
self.is_random_targets = is_random_targets
self._is_return_constant = False
@property
def map_target_position(self):
"""
Return a dictionary that maps the type of target (e.g. "index") to its position in the
outputted target.
"""
target_names = {"target": 0}
if self._is_return_constant:
target_names["constant"] = len(target_names)
if self.is_return_index:
target_names["index"] = len(target_names)
return target_names
def randomize_targets_(self):
"""Randomize the targets in place"""
with tmp_seed(self.seed):
idcs = list(range(len(self.targets)))
random.shuffle(idcs)
self.targets = self.targets[idcs]
def rm_all_transformations_(self):
"""Completely remove transformation."""
pass
def make_test_(self):
"""Make the data a test set."""
pass
def append_(self, other):
"""Append a dataset to the current one."""
self.data = np.append(self.data, other.data, axis=0)
self.targets = np.append(self.targets, other.targets, axis=0)
def train_test_split(self, size=0.1, is_stratify=True, is_test_size=True):
"""Split the dataset into train and test (without data augmentation).
Parameters
----------
size : float or int, optional
If float, should be between 0.0 and 1.0 and represent the proportion of
the dataset to include in the test split. If int, represents the absolute
size of the test dataset.
is_stratify : bool, optional
Whether to stratify splits based on class label.
is_test_size : bool, optional
Whether size should be the test size or the training one.
Returns
-------
train : BaseDataset
Train dataset containing the complement of `test_size` examples.
test : BaseDataset
Test dataset containing `test_size` examples.
"""
idcs_all = list(range(len(self)))
stratify = self.targets if is_stratify else None
idcs_train, indcs_test = train_test_split(
idcs_all, stratify=stratify, test_size=size, random_state=self.seed
)
if not is_test_size:
indcs_test, idcs_train = idcs_train, indcs_test
train = self.clone()
train.keep_indcs_(idcs_train)
test = self.clone()
test.keep_indcs_(indcs_test)
test.make_test_()
return train, test
def drop_labels_(self, drop_size, is_stratify=True):
"""Drop part of the labels to make the dataset semisupervised.
Parameters
----------
drop_size : float or int or tuple, optional
If float, should be between 0.0 and 1.0 and represent the proportion of the labels to
drop. If int, represents the number of labels to drop. 0 means keep all.
is_stratify : bool, optional
Whether to stratify splits based on class label.
"""
if drop_size == 0:
return
self.logger.info(f"Dropping {drop_size} labels...")
idcs_all = list(range(len(self)))
stratify = self.targets if is_stratify else None
idcs_label, idcs_unlabel = train_test_split(
idcs_all, stratify=stratify, test_size=drop_size, random_state=self.seed
)
self.targets[idcs_unlabel] = self.unlabelled_class
def balance_labels_(self):
"""
Balances the number of labelled and unlabbeld data by updasmpling labeled. Only works if
number of labelled data is smaller than unlabelled.
"""
self.logger.info(f"Balancing the semi-supervised labels...")
idcs_unlab = [i for i, t in enumerate(
self.targets) if t == UNLABELLED_CLASS]
idcs_lab = [i for i, t in enumerate(
self.targets) if t != UNLABELLED_CLASS]
assert len(idcs_unlab) > len(idcs_lab)
resampled_idcs_lab = resample(
idcs_lab,
replace=True,
n_samples=len(idcs_unlab),
stratify=self.targets[idcs_lab],
random_state=self.seed,
)
self.keep_indcs_(idcs_unlab + resampled_idcs_lab)
def drop_unlabelled_(self):
"""Drop all the unlabelled examples."""
self.logger.info(f"Drop all the unlabelled examples...")
idcs_lab = [i for i, t in enumerate(
self.targets) if t != UNLABELLED_CLASS]
self.keep_indcs_(idcs_lab)
def get_subset(self, size, is_stratify=True):
"""Return a subset of `size` that does not share its memory.
Parameters
----------
size : float or int, optional
If float, should be between 0.0 and 1.0 and represent the proportion of
the dataset to include in the subset. If int, represents the absolute
size of the subset. If -1, return all.
is_stratify : bool, optional
Whether to stratify splits based on class label.
"""
if size == -1:
return self
subset, _ = self.train_test_split(
size=size, is_stratify=is_stratify, is_test_size=False
)
return subset
def clone(self):
"""Returns a deepcopy of the daatset."""
return copy.deepcopy(self)
def keep_indcs_(self, indcs):
"""Keep the given indices.
Parameters
----------
indcs : array-like int
Indices to keep. If the multiplicity of the indices is larger than 1 then will duplicate
the data.
"""
self.data = self.data[indcs]
self.targets = self.targets[indcs]
def drop_features_(self, drop_size):
"""Drop part of the features (e.g. pixels in images).
Parameters
----------
drop_size : float or int or tuple, optional
If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to
drop. If int, represents the number of datapoints to drop. If tuple, same as before
but give bounds (min and max). 1 means drop all.
"""
if drop_size == 0:
return
raise NotImplementedError(
"drop_features_ not implemented for current dataset")
def add_index(self, y, index):
"""Append the index to the targets (if needed)."""
if self.is_return_index:
y = tuple(y) + (index,)
return y
def set_const_target_(self, target):
"""Set a constant target `target` to all targets."""
if self.targets.ndim == 1:
self.targets = np.expand_dims(self.targets, 1)
self.targets = np.append(
self.targets, self.targets * 0 + target, axis=1)
self._is_return_constant = True
def sort_data_(self, by="targets"):
"""Sort the data by {"targets"}. E.g. the first |X|/|Y| examples will be from the same target."""
targets = self.targets
if self.targets.ndim > 1:
targets = targets[:, 0]
if by == "targets":
idcs = list(np.argsort(targets))
self.targets = self.targets[idcs]
self.data = self.data[idcs]
else:
raise ValueError(f"Unkown by={by}")
def count_targets(self):
"""Return a dictionary where the keys are the targets and values the number of each target."""
targets, counts = np.unique(self.targets, return_counts=True)
return {t: c for t, c in zip(targets, counts)}
|
decodable_information_bottleneck-main
|
utils/data/base.py
|
AVT-main
|
__init__.py
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""Launch script to run arguments stored in txt files."""
import argparse
import subprocess
import os
import socket
import glob
from omegaconf import OmegaConf
import inquirer
import pathlib
from hydra.core.override_parser.overrides_parser import OverridesParser
from hydra._internal.core_plugins.basic_sweeper import BasicSweeper
CODE_DIR = str(pathlib.Path(__file__).parent.resolve())
BASE_RUN_DIR = f'{CODE_DIR}/OUTPUTS'
def parse_args():
"""Parse arguments."""
parser = argparse.ArgumentParser()
parser.add_argument('-c',
'--cfg',
type=str,
required=True,
help='Overrides config file')
parser.add_argument('-l',
'--local',
action='store_true',
help='Run locally instead of launching to cluster')
parser.add_argument('-g',
'--debug',
action='store_true',
help='Run in debug mode: 1 GPU, when locally')
parser.add_argument('-t',
'--test',
action='store_true',
help='Run testing mode (will pick the last ckpt)')
parser.add_argument('-p',
'--partition',
type=str,
default=None,
help='Specify SLURM partition to run on')
parser.add_argument('--tb',
action='store_true',
help='Run tensorboard on this directory')
parser.add_argument('-f',
'--fl',
action='store_true',
help='View the folder (run a python server)')
parser.add_argument('-d',
'--delete',
action='store_true',
help='Delete the folder')
parser.add_argument('-k',
'--kill',
action='store_true',
help='Kill jobs running this config.')
parser.add_argument('--profile',
action='store_true',
help='Run with kernprof. Decorate fn with @profile')
parser.add_argument('--cls',
action='store_true',
help='Gen classification file and run that')
parser.add_argument('--run_id',
type=int,
default=None,
help='Run for this specific run_id, if known')
parser.add_argument('rest', nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.debug:
args.local = True
return args
def get_sweep_param_from_combinations(clis):
"""
Returns:
[(run_id, overrides_dict)]. The run_id can be None if unsure what hydra
would use.
"""
sweeper = BasicSweeper(max_batch_size=None)
parser = OverridesParser.create()
overrides = parser.parse_overrides(clis)
run_args = sweeper.split_arguments(overrides, max_batch_size=None)[0]
res = []
for i, run_arg in enumerate(run_args):
res.append((i, dict([el.split('=') for el in run_arg])))
return res
def get_sweep_param_from_runs(conf_path):
exp_path = os.path.join(BASE_RUN_DIR, conf_path)
run_dirs = glob.glob(os.path.join(exp_path, r'[0-9]*'))
if len(run_dirs) == 0:
return []
res = []
for run_dir in run_dirs:
run_id = int(os.path.basename(run_dir))
override_fpath = os.path.join(run_dir, '.hydra/overrides.yaml')
if not os.path.exists(override_fpath):
# Likely deleted, so run_dirs may not be useful..
# Happens when we delete the output folder, but the run folders
# don't get deleted, because I opened in toplog and the nfs
# files aren't deleted until I kill the toplog on that folder
return []
conf = OmegaConf.load(override_fpath)
res.append((run_id, dict([el.split('=') for el in conf])))
return res
def subselect_dict_keys_diff(run_id_param_dicts):
"""Select keys from the param_dicts that actually change between configs."""
key_vals = {}
for _, param_dict in run_id_param_dicts:
for key, val in param_dict.items():
if key not in key_vals:
key_vals[key] = []
key_vals[key].append(val)
keys_to_keep = [
key for key, vals in key_vals.items() if len(set(vals)) > 1
]
return [(el[0], {key: el[1][key]
for key in keys_to_keep}) for el in run_id_param_dicts]
def escape_str(input_str):
return f"'{input_str}'"
def choose_single_run(clis, fpath, run_id):
"""
clis are a list of flags provided in the config overrides file.
Args:
clis: List of clis from the txt file
run_id: If known which model to run locally, the run_id of that sweep
"""
run_id_param_dicts = get_sweep_param_from_combinations(clis)
if len(run_id_param_dicts) == 1:
final_run_id, param_dict = run_id_param_dicts[0]
assert run_id is None or run_id == final_run_id
elif run_id is not None:
final_run_id = run_id
param_dicts = [el[1] for el in run_id_param_dicts if el[0] == run_id]
assert len(param_dicts) == 1, 'run_id not found, or multiple found'
param_dict = param_dicts[0]
else:
# Show options to the user and let her pick
run_id_param_dicts_diff = subselect_dict_keys_diff(run_id_param_dicts)
print('Choose from: \n' +
'\n'.join([str(el) for el in run_id_param_dicts_diff]))
qst = [
inquirer.List(
'r',
message='Which sweep config to use?',
choices=range(len(run_id_param_dicts)),
carousel=True,
),
]
final_run_id, param_dict = run_id_param_dicts[inquirer.prompt(qst)
['r']]
return final_run_id, [f'{key}={val}' for key, val in param_dict.items()]
def read_file_into_cli(fpath, running_local=False, run_id=None):
"""Read cli from file into a string."""
res = []
with open(fpath, 'r') as fin:
for line in fin:
args = line.split('#')[0].strip()
if len(args) == 0:
continue
res.append(args)
if running_local:
final_run_id, res = choose_single_run(res, fpath, run_id)
else:
final_run_id = None # not local, launch all, so run_id is irrelevant
return final_run_id, res
def get_models_dir(dpath):
"""Go inside the dpath to get the model dir."""
runs = sorted([el for el in next(os.walk(dpath))[1] if el.isdigit()])
if len(runs) > 1:
# Ask which run to use
question = [
inquirer.List(
'run',
message='Which run to use?',
choices=runs,
),
]
answers = inquirer.prompt(question)
else:
answers = dict(run=runs[0])
return dpath + '/' + answers['run']
def is_port_in_use(port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
return s.connect_ex(('localhost', port)) == 0
def get_free_port():
# Make sure to forward these ports in et
potential_ports = range(30303, 30399)
for port in potential_ports:
if not is_port_in_use(port):
return port
raise ResourceWarning('No empty port found')
def num_gpus():
output = subprocess.run('nvidia-smi --query-gpu=name --format=csv,noheader'
'| wc -l', shell=True, capture_output=True)
return int(output.stdout.decode().strip())
def construct_cmd(args):
"""Construct the cmd as provided in args."""
if args.cfg:
assert args.cfg.startswith('expts'), 'Must be wrt this directory'
agent_folder = '{}/{}'.format(BASE_RUN_DIR,
args.cfg if args.cfg else 'default')
if args.kill:
slurm_ids = os.listdir(os.path.join(agent_folder, '.submitit/'))
shall = input("Kill %s (y/N) " % slurm_ids).lower() == 'y'
if shall:
return 'scancel {}'.format(' '.join(slurm_ids))
if args.tb: # Run tensorboard only
# Clear the cli and just run tensorboard
cli = ('cd {agent_folder}; tensorboard --logdir . --port {port} '
'--max_reload_threads 10 --window_title {name} ').format(
agent_folder=agent_folder,
port=get_free_port(),
name=args.cfg)
return cli
if args.fl: # Visualize the folder only
# Clear the cli and just run tensorboard
cli = 'cd {}; python -m http.server {}'.format(agent_folder,
get_free_port())
return cli
if args.delete:
cli = 'rm -r {f}/* {f}/.*'.format(f=agent_folder)
shall = input("Run %s (y/N) " % cli).lower() == 'y'
if shall:
return cli
return ''
# Else, it is the general train command
run_id, cli_stuff = read_file_into_cli(args.cfg,
running_local=args.local,
run_id=args.run_id)
cli_stuff = [escape_str(el) for el in cli_stuff]
cli_stuff = ' '.join(cli_stuff)
if args.debug:
if args.test:
# If args.test, then might be testing a model from other dir
agent_folder = os.path.join(agent_folder, str(run_id))
else:
agent_folder = os.path.join(agent_folder, 'local')
# Delete the sync file if it exists
clear_cmd = f'find {agent_folder} -iname sync_file_init -delete'
print(f'Clearing out the sync files using: {clear_cmd}')
subprocess.call(clear_cmd, shell=True)
cli = (
'export NCCL_SOCKET_IFNAME=; export GLOO_SOCKET_IFNAME=; '
' HYDRA_FULL_ERROR=1 '
' {} train_net.py hydra.run.dir={} ').format(
'kernprof -l ' if args.profile else 'python ', agent_folder)
cli += cli_stuff
if args.test:
cli += ' test_only=True '
if args.local:
cli += (' hydra.launcher.nodes=1 '
f' hydra.launcher.gpus_per_node={num_gpus()} '
' hydra/launcher=submitit_local ')
else:
cli += (' hydra.launcher.max_num_timeout=3 ')
if args.partition is not None and not args.local:
cli += f' +hydra.launcher.partition="{args.partition}" '
if args.debug:
cli += (' data_train.workers=0 data_eval.workers=0 ')
cli += ' ' + ' '.join(args.rest)
# This must go at the end, the other args must go before
if not args.debug:
cli += ' -m '
return cli
def main():
"""Main func."""
args = parse_args()
# if args.cls:
# args = gen_cls_override_file(args)
cmd = construct_cmd(args)
print('>> Running "{}"'.format(cmd))
subprocess.call(cmd, shell=True)
if __name__ == '__main__':
main()
|
AVT-main
|
launch.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""Main training entry."""
import os
import logging
import random
import subprocess
import torch
import hydra
from omegaconf import DictConfig, OmegaConf
import func
OmegaConf.register_new_resolver('minus', lambda x, y: x - y)
# Multiply and cast to integer
OmegaConf.register_new_resolver('times_int', lambda x, y: int(x * y))
@hydra.main(config_path='conf', config_name='config')
def main(cfg: DictConfig) -> None:
# Since future runs might corrupt the stored hydra config, copy it over
# for backup.
if not os.path.exists('.hydra.orig'):
subprocess.call('cp -r .hydra .hydra.orig', shell=True)
random.seed(cfg.seed)
torch.manual_seed(cfg.seed)
try:
print(subprocess.check_output('nvidia-smi'))
except subprocess.CalledProcessError:
print('Could not run nvidia-smi..')
# cudnn.deterministic = True # Makes it slow..
getattr(func, cfg.train.fn).main(cfg)
if __name__ == "__main__":
logging.basicConfig(format=('%(asctime)s %(levelname)-8s'
' {%(module)s:%(lineno)d} %(message)s'),
level=logging.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S')
torch.multiprocessing.set_start_method('spawn')
main() # pylint: disable=no-value-for-parameter # Uses hydra
|
AVT-main
|
train_net.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""The Epic Kitchens dataset loaders."""
from typing import List, Dict, Sequence, Tuple, Union
from datetime import datetime, date
from collections import OrderedDict
import pickle as pkl
import csv
import logging
from pathlib import Path
import lmdb
import pandas as pd
import numpy as np
from omegaconf import OmegaConf
import torch
import torch.nn as nn
from .base_video_dataset import BaseVideoDataset, RULSTM_TSN_FPS
from .reader_fns import Reader
EGTEA_VERSION = -1 # This class also supports EGTEA Gaze+
EPIC55_VERSION = 0.1
EPIC100_VERSION = 0.2
class EPICKitchens(BaseVideoDataset):
"""EPICKitchens dataloader."""
def __init__(
self,
annotation_path: Sequence[Path],
only_keep_persons: str = None,
only_keep_videos: Path = None,
action_labels_fpath: Path = None,
annotation_dir: Path = None,
rulstm_annotation_dir: Path = None,
_precomputed_metadata: Path = None,
version: float = EPIC55_VERSION,
**other_kwargs,
):
"""
Args:
label_type (str): The type of label to return
only_keep_persons (str): If None, ignore. Else, will only keep
videos of persons P<start> to P<end> (both included), where this
string is "<start>-<end>". This is used to create
the train_minus_val and val sets, as per
https://arxiv.org/abs/1806.06157
only_keep_videos (Path): Path to a file with list of videos to keep.
This was used to define the val set as used in anticipation
in https://arxiv.org/abs/1905.09035
action_labels_fpath (Path): Path to map the verb and noun labels to
actions. It was used in the anticipation paper, that defines
a set of actions and train for action prediction, as opposed
to verb and noun prediction.
annotation_dir: Where all the other annotations are typically stored
"""
self.version = version
df = pd.concat([self._load_df(el) for el in annotation_path])
df.reset_index(inplace=True, drop=True) # to combine all of them
df = self._subselect_df_by_videos(
self._subselect_df_by_person(df, only_keep_persons),
only_keep_videos)
# If no specific annotation_dir specified, use the parent dir of
# the first annot path
if annotation_dir is None:
self.annotation_dir = Path(annotation_path[0]).parent
else:
self.annotation_dir = Path(annotation_dir)
self.rulstm_annotation_dir = rulstm_annotation_dir
epic_postfix = ''
if self.version == EPIC100_VERSION:
epic_postfix = '_100'
if self.version != EGTEA_VERSION:
verb_classes = self._load_class_names(
self.annotation_dir / f'EPIC{epic_postfix}_verb_classes.csv')
noun_classes = self._load_class_names(
self.annotation_dir / f'EPIC{epic_postfix}_noun_classes.csv')
else:
verb_classes, noun_classes = [], []
# Create action classes
if action_labels_fpath is not None:
load_action_fn = self._load_action_classes
if self.version == EGTEA_VERSION:
load_action_fn = self._load_action_classes_egtea
action_classes, verb_noun_to_action = (
load_action_fn(action_labels_fpath))
else:
action_classes, verb_noun_to_action = self._gen_all_actions(
verb_classes, noun_classes)
# Add the action classes to the data frame
if ('action_class' not in df.columns
and {'noun_class', 'verb_class'}.issubset(df.columns)):
df.loc[:, 'action_class'] = df.loc[:, (
'verb_class', 'noun_class')].apply(
lambda row: (verb_noun_to_action[
(row.at['verb_class'], row.at['noun_class'])]
if (row.at['verb_class'], row.at['noun_class']
) in verb_noun_to_action else -1),
axis=1)
elif 'action_class' not in df.columns:
df.loc[:, 'action_class'] = -1
df.loc[:, 'verb_class'] = -1
df.loc[:, 'noun_class'] = -1
num_undefined_actions = len(df[df['action_class'] == -1].index)
if num_undefined_actions > 0:
logging.error(
'Did not found valid action label for %d/%d samples!',
num_undefined_actions, len(df))
assert _precomputed_metadata is None, 'Not supported yet'
other_kwargs['verb_classes'] = verb_classes
other_kwargs['noun_classes'] = noun_classes
other_kwargs['action_classes'] = action_classes
super().__init__(df, **other_kwargs)
# following is used in the notebooks for marginalization, so save it
self.verb_noun_to_action = verb_noun_to_action
logging.info('Created EPIC %s dataset with %d samples', self.version,
len(self))
@property
def primary_metric(self) -> str:
if self.version == EPIC100_VERSION:
# For EK100, we want to optimize for AR5
return 'final_acc/action/AR5'
return super().primary_metric
@property
def class_mappings(self) -> Dict[Tuple[str, str], torch.FloatTensor]:
num_verbs = len(self.verb_classes)
if num_verbs == 0:
num_verbs = len(
set([el[0] for el, _ in self.verb_noun_to_action.items()]))
num_nouns = len(self.noun_classes)
if num_nouns == 0:
num_nouns = len(
set([el[1] for el, _ in self.verb_noun_to_action.items()]))
num_actions = len(self.action_classes)
if num_actions == 0:
num_actions = len(
set([el for _, el in self.verb_noun_to_action.items()]))
verb_in_action = torch.zeros((num_actions, num_verbs),
dtype=torch.float)
noun_in_action = torch.zeros((num_actions, num_nouns),
dtype=torch.float)
for (verb, noun), action in self.verb_noun_to_action.items():
verb_in_action[action, verb] = 1.0
noun_in_action[action, noun] = 1.0
return {
('verb', 'action'): verb_in_action,
('noun', 'action'): noun_in_action
}
@property
def classes_manyshot(self) -> OrderedDict:
"""
In EPIC-55, the recall computation was done for "many shot" classes,
and not for all classes. So, for that version read the class names as
provided by RULSTM.
Function adapted from
https://github.com/fpv-iplab/rulstm/blob/57842b27d6264318be2cb0beb9e2f8c2819ad9bc/RULSTM/main.py#L386
"""
if self.version != EPIC55_VERSION:
return super().classes_manyshot
# read the list of many shot verbs
many_shot_verbs = {
el['verb']: el['verb_class']
for el in pd.read_csv(self.annotation_dir /
'EPIC_many_shot_verbs.csv').to_dict(
'records')
}
# read the list of many shot nouns
many_shot_nouns = {
el['noun']: el['noun_class']
for el in pd.read_csv(self.annotation_dir /
'EPIC_many_shot_nouns.csv').to_dict(
'records')
}
# create the list of many shot actions
# an action is "many shot" if at least one
# between the related verb and noun are many shot
many_shot_actions = {}
action_names = {val: key for key, val in self.action_classes.items()}
for (verb_id, noun_id), action_id in self.verb_noun_to_action.items():
if (verb_id in many_shot_verbs.values()) or (
noun_id in many_shot_nouns.values()):
many_shot_actions[action_names[action_id]] = action_id
return {
'verb': many_shot_verbs,
'noun': many_shot_nouns,
'action': many_shot_actions,
}
@staticmethod
def _load_action_classes(
action_labels_fpath: Path
) -> Tuple[Dict[str, int], Dict[Tuple[int, int], int]]:
"""
Given a CSV file with the actions (as from RULSTM paper), construct
the set of actions and mapping from verb/noun to action
Args:
action_labels_fpath: path to the file
Returns:
class_names: Dict of action class names
verb_noun_to_action: Mapping from verb/noun to action IDs
"""
class_names = {}
verb_noun_to_action = {}
with open(action_labels_fpath, 'r') as fin:
reader = csv.DictReader(fin, delimiter=',')
for lno, line in enumerate(reader):
class_names[line['action']] = lno
verb_noun_to_action[(int(line['verb']),
int(line['noun']))] = int(line['id'])
return class_names, verb_noun_to_action
@staticmethod
def _load_action_classes_egtea(
action_labels_fpath: Path
) -> Tuple[Dict[str, int], Dict[Tuple[int, int], int]]:
"""
Given a CSV file with the actions (as from RULSTM paper), construct
the set of actions and mapping from verb/noun to action
Args:
action_labels_fpath: path to the file
Returns:
class_names: Dict of action class names
verb_noun_to_action: Mapping from verb/noun to action IDs
"""
class_names = {}
verb_noun_to_action = {}
with open(action_labels_fpath, 'r') as fin:
reader = csv.DictReader(
fin,
delimiter=',',
# Assuming the order is verb/noun
# TODO check if that is correct
fieldnames=['id', 'verb_noun', 'action'])
for lno, line in enumerate(reader):
class_names[line['action']] = lno
verb, noun = [int(el) for el in line['verb_noun'].split('_')]
verb_noun_to_action[(verb, noun)] = int(line['id'])
return class_names, verb_noun_to_action
@staticmethod
def _gen_all_actions(
verb_classes: List[str], noun_classes: List[str]
) -> Tuple[Dict[str, int], Dict[Tuple[int, int], int]]:
"""
Given all possible verbs and nouns, construct all possible actions
Args:
verb_classes: All verbs
noun_classes: All nouns
Returns:
class_names: list of action class names
verb_noun_to_action: Mapping from verb/noun to action IDs
"""
class_names = {}
verb_noun_to_action = {}
action_id = 0
for verb_id, verb_cls in enumerate(verb_classes):
for noun_id, noun_cls in enumerate(noun_classes):
class_names[f'{verb_cls}:{noun_cls}'] = action_id
verb_noun_to_action[(verb_id, noun_id)] = action_id
action_id += 1
return class_names, verb_noun_to_action
def _load_class_names(self, annot_path: Path):
res = {}
with open(annot_path, 'r') as fin:
reader = csv.DictReader(fin, delimiter=',')
for lno, line in enumerate(reader):
res[line['class_key' if self.version ==
EPIC55_VERSION else 'key']] = lno
return res
def _load_df(self, annotation_path):
if annotation_path.endswith('.pkl'):
return self._init_df_orig(annotation_path)
elif annotation_path.endswith('.csv'):
# Else, it must be the RULSTM annotations (which are a
# little different, perhaps due to quantization into frames)
return self._init_df_rulstm(annotation_path)
else:
raise NotImplementedError(annotation_path)
def _init_df_gen_vidpath(self, df):
# generate video_path
if self.version == EGTEA_VERSION:
df.loc[:, 'video_path'] = df.apply(
lambda x: Path(x.video_id + '.mp4'),
axis=1,
)
else: # For the EPIC datasets
df.loc[:, 'video_path'] = df.apply(
lambda x: (Path(x.participant_id) / Path(x.video_id + '.MP4')),
axis=1,
)
return df
def _init_df_rulstm(self, annotation_path):
logging.info('Loading RULSTM EPIC csv annotations %s', annotation_path)
df = pd.read_csv(
annotation_path,
names=[
'uid',
'video_id',
'start_frame_30fps',
'end_frame_30fps',
'verb_class',
'noun_class',
'action_class',
],
index_col=0,
skipinitialspace=True,
dtype={
'uid': str, # In epic-100, this is a str
'video_id': str,
'start_frame_30fps': int,
'end_frame_30fps': int,
'verb_class': int,
'noun_class': int,
'action_class': int,
})
# Make a copy of the UID column, since that will be needed to gen
# output files
df.reset_index(drop=False, inplace=True)
# Convert the frame number to start and end
df.loc[:, 'start'] = df.loc[:, 'start_frame_30fps'].apply(
lambda x: x / RULSTM_TSN_FPS)
df.loc[:, 'end'] = df.loc[:, 'end_frame_30fps'].apply(
lambda x: x / RULSTM_TSN_FPS)
# Participant ID from video_id
df.loc[:, 'participant_id'] = df.loc[:, 'video_id'].apply(
lambda x: x.split('_')[0])
df = self._init_df_gen_vidpath(df)
df.reset_index(inplace=True, drop=True)
return df
def _init_df_orig(self, annotation_path):
"""
Loading the original EPIC Kitchens annotations
"""
def timestr_to_sec(s, fmt='%H:%M:%S.%f'):
timeobj = datetime.strptime(s, fmt).time()
td = datetime.combine(date.min, timeobj) - datetime.min
return td.total_seconds()
# Load the DF from annot path
logging.info('Loading original EPIC pkl annotations %s',
annotation_path)
with open(annotation_path, 'rb') as fin:
df = pkl.load(fin)
# Make a copy of the UID column, since that will be needed to gen
# output files
df.reset_index(drop=False, inplace=True)
# parse timestamps from the video
df.loc[:, 'start'] = df.start_timestamp.apply(timestr_to_sec)
df.loc[:, 'end'] = df.stop_timestamp.apply(timestr_to_sec)
# original annotations have text in weird format - fix that
if 'noun' in df.columns:
df.loc[:, 'noun'] = df.loc[:, 'noun'].apply(
lambda s: ' '.join(s.replace(':', ' ').split(sep=' ')[::-1]))
if 'verb' in df.columns:
df.loc[:, 'verb'] = df.loc[:, 'verb'].apply(
lambda s: ' '.join(s.replace('-', ' ').split(sep=' ')))
df = self._init_df_gen_vidpath(df)
df.reset_index(inplace=True, drop=True)
return df
@staticmethod
def _subselect_df_by_person(df, only_keep_persons):
if only_keep_persons is None:
return df
start, end = [int(el) for el in only_keep_persons.split('-')]
df = df.loc[df['participant_id'].isin(
['P{:02d}'.format(el) for el in range(start, end + 1)]), :]
df.reset_index(inplace=True, drop=True)
return df
@staticmethod
def _subselect_df_by_videos(df, videos_fpath):
if videos_fpath is None:
return df
with open(videos_fpath, 'r') as fin:
videos_to_keep = [el.strip() for el in fin.read().splitlines()]
df = df.loc[df['video_id'].isin(videos_to_keep), :]
df.reset_index(inplace=True, drop=True)
return df
class EpicRULSTMFeatsReader(Reader):
def __init__(self,
lmdb_path: Union[Path, List[Path]] = None,
read_type: str = 'exact_rulstm',
warn_if_using_closeby_frame: bool = True):
"""
Args:
feats_lmdb_path: LMDB path for RULSTM features. Must be
specified if using rulstm_tsn_feat input_type. Could be a
list, in which case it will concat all those features together.
read_type: [rulstm_exact/normal] This specifies what style of
feature reading for RULSTM features. Until Oct 22, I have been
exactly reading 11 frames at 0.25s, but that is not scalable to
learn language models, so making it more generic to read all
frames and let the base_video_dataset code figure how to
re-sample to get the relevant frames. Not making it default
to be able to repro older results.
"""
super().__init__()
if OmegaConf.get_type(lmdb_path) != list:
lmdb_path = [lmdb_path]
self.lmdb_envs = [
lmdb.open(el, readonly=True, lock=False) for el in lmdb_path
]
self.read_type = read_type
self.warn_if_using_closeby_frame = warn_if_using_closeby_frame
def forward(self, *args, **kwargs):
return self._read_rulstm_features(*args, **kwargs)
@staticmethod
def get_frame_rate(video_path: Path) -> float:
del video_path
return RULSTM_TSN_FPS
def read_representations(self, frames, env, frame_format):
"""Reads a set of representations, given their frame names and an LMDB
environment.
From https://github.com/fpv-iplab/rulstm/blob/96e38666fad7feafebbeeae94952dba24771e512/RULSTM/dataset.py#L10
"""
features = []
# for each frame
for frame_id in frames:
# read the current frame
with env.begin() as e:
# Need to search for a frame that has features stored,
# the exact frame may not have.
# To avoid looking at the future when training/testing,
# (important for anticipation), look only for previous to
# current position.
dd = None
search_radius = 0
for search_radius in range(10):
dd = e.get(
frame_format.format(
frame_id - search_radius).strip().encode('utf-8'))
if dd is not None:
break
if dd is not None and search_radius > 0:
if self.warn_if_using_closeby_frame:
logging.warning('Missing %s, but used %d instead',
frame_format.format(frame_id),
frame_id - search_radius)
if dd is None:
logging.error(
'Missing %s, Only specific frames are stored in lmdb :(',
frame_format.format(frame_id))
features.append(None)
else:
# convert to numpy array
data = np.frombuffer(dd, 'float32')
# append to list
features.append(data)
# For any frames we didn't find a feature, use a series of 0s
features_not_none = [el for el in features if el is not None]
assert len(features_not_none) > 0, (
f'No features found in {frame_format} - {frames}')
feature_not_none = features_not_none[0] # any
features = [
np.zeros_like(feature_not_none) if el is None else el
for el in features
]
# convert list to numpy array
features = np.array(features)
# Add singleton dimensions to make it look like a video, so
# rest of the code just works
features = features[:, np.newaxis, np.newaxis, :]
# Make it torch Tensor to be consistent
features = torch.as_tensor(features)
return features
def _read_rulstm_features(self,
video_path: Path,
start_sec: float,
end_sec: float,
fps: float,
df_row: pd.DataFrame,
pts_unit='sec'):
del pts_unit # Not supported here
if self.read_type == 'exact_rulstm':
# get frames every 0.25s between start and end frames
# 0.25 comes from their code, and they typically do 2.5s total
# observation time. 14 is the sequence length they use.
time_stamps = end_sec - np.arange(0.0, 0.25 * 11, 0.25)[::-1]
frames = np.floor(time_stamps * fps).astype(int)
elif self.read_type == 'normal':
# Read every single frame between the start and end, the
# base_video_dataset code will deal with how to sample into 4fps
# (i.e. 0.25s steps)
# Rather than first computing the timestamps, just compute the
# frame ID of the start and end, and do a arange .. that avoids
# any repeated frames due to quantization/floor
time_stamps = None
start_frame = np.floor(start_sec * fps)
end_frame = np.floor(end_sec * fps)
frames = np.arange(end_frame, start_frame, -1).astype(int)[::-1]
else:
raise NotImplementedError(f'Unknown {self.read_type}')
# If the frames go below 1, replace them with the lowest time pt
assert frames.max() >= 1, (
f'The dataset shouldnt have cases otherwise. {video_path} '
f'{start_sec} {end_sec} {df_row} {frames} {time_stamps} ')
frames[frames < 1] = frames[frames >= 1].min()
# Get the features
all_feats = []
for lmdb_env in self.lmdb_envs:
all_feats.append(
self.read_representations(
frames, lmdb_env,
Path(video_path).stem + '_frame_{:010d}.jpg'))
final_feat = torch.cat(all_feats, dim=-1)
# Must return rgb, audio, info; so padding with empty dicts for those
return final_feat, {}, {}
|
AVT-main
|
datasets/epic_kitchens.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""The base dataset loader."""
from typing import Tuple, Union, Sequence, Dict
import logging
from pathlib import Path
from collections import OrderedDict
import operator
from multiprocessing import Manager
import math
import h5py
import pandas as pd
import numpy as np
import torch
import torch.nn as nn
import torchvision
from omegaconf import OmegaConf
import hydra
from hydra.types import TargetConf
from common.utils import get_video_info, get_world_size, get_rank
SAMPLE_STRAT_CNTR = 'center_clip'
SAMPLE_STRAT_RAND = 'random_clip'
SAMPLE_STRAT_LAST = 'last_clip'
SAMPLE_STRAT_FIRST = 'first_clip'
FUTURE_PREFIX = 'future' # to specify future videos
# This is specific to EPIC kitchens
RULSTM_TSN_FPS = 30.0 # The frame rate the feats were stored by RULSTM
# This is important for some datasets, like Breakfast, where reading using the
# pyAV reader leads to jerky videos for some reason. This requires torchvision
# to be compiled from source, instructions in the top level README
torchvision.set_video_backend('video_reader')
def convert_to_anticipation(df: pd.DataFrame,
root_dir: Sequence[Path],
tau_a: float,
tau_o: float,
future_clip_ratios: Sequence[float] = (1.0, ),
drop_style='correct'
) -> Tuple[pd.DataFrame, pd.DataFrame]:
"""
Based on the definition in the original paper
https://arxiv.org/pdf/1804.02748.pdf, convert the start and end
video times to as used in anticipation.
tau_a (float): Anticipation time in seconds. By default -1, since
we train the model to do action recognition, in which case
the model sees a clip that finishes tau_a seconds before
the action to be anticipated starts. This is as per defn
in https://arxiv.org/pdf/1804.02748.pdf (pg 15)
tau_o (float): The amount of video to see before doing the
anticipation. In the original paper they used 1s
(https://arxiv.org/pdf/1804.02748.pdf), but in further ones
they use 3.5 (https://arxiv.org/pdf/1905.09035.pdf).
future_clip_ratios: A list of ratios (< 1.0) of tau_a, to define what clips
to set as the future clips. These will be used when returning future
clips. Ideally the labels should be adjusted to match this too, but
not doing that for now.
"""
del root_dir
if tau_a == -999:
# No anticipation, just simple recognition
# still add the orig_start and orig_end, future etc
# so the future prediction baseline can do the case where not future
# is predicted.
# This will ensure the future clip ends up being the same as current
tau_a = df.loc[:, 'start'] - df.loc[:, 'end']
tau_o = df.loc[:, 'end'] - df.loc[:, 'start']
logging.debug(
'Converting data to anticipation with tau_a=%s and '
'tau_o=%s.', tau_a, tau_o)
# Copy over the current start and end times
df.loc[:, 'orig_start'] = df.start
df.loc[:, 'orig_end'] = df.end
# Convert using tau_o and tau_a
df.loc[:, 'end'] = df.loc[:, 'start'] - tau_a
df.loc[:, 'start'] = df.loc[:, 'end'] - tau_o
# Add the future clips
for i, future_clip_ratio in enumerate(future_clip_ratios):
if future_clip_ratio == -999:
# A spl number to use the exact current clip as the future
df.loc[:, f'{FUTURE_PREFIX}_{i}_start'] = df.loc[:, 'start']
df.loc[:, f'{FUTURE_PREFIX}_{i}_end'] = df.loc[:, 'end']
elif future_clip_ratio > -10 and future_clip_ratio < 10:
eff_tau_a = tau_a * future_clip_ratio
df.loc[:, f'{FUTURE_PREFIX}_{i}_start'] = (df.loc[:, 'end'] +
eff_tau_a)
df.loc[:, f'{FUTURE_PREFIX}_{i}_end'] = (
df.loc[:, f'future_{i}_start'] + tau_o)
else:
raise ValueError(f'Seems out of bound {future_clip_ratio}')
# first frame seconds
f1_sec = 1 / RULSTM_TSN_FPS
old_df = df
if drop_style == 'correct':
# at least 1 frame
df = df[df.end >= f1_sec]
elif drop_style == 'full_context_in':
# All frames should be in
df = df[df.start >= f1_sec]
elif drop_style == 'action_banks':
# Based on their dataset_anticipation:__get_snippet_features()
df = df[df.end >= 2]
else:
raise NotImplementedError(f'Unknown style {drop_style}')
discarded_df = pd.concat([old_df, df]).drop_duplicates(subset=['uid'],
keep=False)
df.reset_index(inplace=True, drop=True)
return df, discarded_df
def break_segments_by_duration(duration, label, segment_len):
"""
Return a list of [(duration, label1, label2, ...), ...] such that each
duration is == segment_len if set.
Note label can be a scalar or vector (in case of multi-label cls)
"""
if not isinstance(label, list):
label = [label]
if segment_len is None:
return [[duration] + label], duration
nseg = int(round(duration / segment_len))
return [[segment_len] + label for _ in range(nseg)], nseg * segment_len
def dense_labels_to_segments(
dense_labels,
segment_start_time,
segment_end_time,
# -1 => get as many as possible
pred_steps=-1,
fixed_duration=None,
dummy_label=-1):
segments = []
for start, end, label in dense_labels:
if end < segment_start_time:
# Then this action is past, not relevant here
# should only happen for the pos-1 action being added
continue
if start > segment_end_time:
# This action starts after the segment, so leave this
continue
# should not look at anything beyond the segment end time
end = min(end, segment_end_time)
if start > segment_start_time:
# Add an empty slot of action, for the time where we don't know
# what happened. Setting the action itself to be -1, so the
# model can predict whatever and it won't be penalized
new_segments, duration_used = break_segments_by_duration(
start - segment_start_time, dummy_label, fixed_duration)
segments += new_segments
segment_start_time += duration_used
new_segments, duration_used = break_segments_by_duration(
end - segment_start_time, label, fixed_duration)
segments += new_segments
segment_start_time += duration_used
if fixed_duration is None:
assert segment_start_time == end
if pred_steps > 0 and len(segments) >= pred_steps:
break
if pred_steps > 0:
segments = segments[:pred_steps]
# Pad it with dummy intervals for batching, if lower
if not isinstance(dummy_label, list):
dummy_label = [dummy_label]
segments += [[-1] + dummy_label] * (pred_steps - len(segments))
return segments
def get_abs_path(root_dirs: Sequence[Path], fpath: Path):
"""
Combine the fpath with the first root_dir it exists in.
"""
res_fpath = None
for root_dir in root_dirs:
res_fpath = root_dir / fpath
if res_fpath.exists():
return res_fpath
logging.warning('Did not find any directory for %s [from %s]', fpath,
root_dirs)
return res_fpath # return the last one for now
def read_saved_results_uids(resfpath: Path):
if not resfpath.exists():
return set([])
with h5py.File(resfpath, 'r') as fin:
res = fin['uid'][()].tolist()
# For fast lookup when filtering (makes big difference)
return set([el.decode() for el in res])
def dense_clip_sampler(df: pd.DataFrame,
root_dir: Sequence[Path],
clip_len: Union[float, str] = 'mean_action_len',
stride: float = 1.0,
shard_per_worker: bool = False,
keep_orig_clips: bool = True,
featext_skip_done: bool = False):
"""
Add clips to the data frame sampling the videos densely from the video.
This function is also compatible with the convert_to_anticipation_fn
to extract features etc. The class label for those clips
is -1, it's mostly just used for SSL/feat ext.
Args:
stride (float): stride in seconds on how the clips are sampled.
shard_per_worker (bool): If true, create subset DF for this process
featext_skip_done (bool): Set this to true only when extracting
features. This will go through saved results files and check
what features have been stored and skip those from populating
into the dataset to the computed, hence continuing from what
has already been done.
"""
uniq_videos = sorted(list(df.video_path.unique()))
if shard_per_worker:
world_size = get_world_size()
rank = get_rank()
vids_per_shard = int(math.ceil(len(uniq_videos) / world_size))
uniq_videos = uniq_videos[(vids_per_shard * rank):min((
(rank + 1) * vids_per_shard), len(uniq_videos))]
skip_uids = []
if featext_skip_done:
# TODO replace with RESULTS_SAVE_DIR
skip_uids = read_saved_results_uids(Path(f'./results/{get_rank()}.h5'))
logging.info('Found %d done UIDs, skipping those', len(skip_uids))
if clip_len == 'mean_action_len':
clip_len = np.mean(df.end - df.start)
new_rows = []
total_possible_clips = 0
for vid_path in uniq_videos:
end_s = get_video_info(get_abs_path(root_dir, vid_path),
['len'])['len']
new_ends = np.arange(0, end_s, stride)
for new_end in new_ends:
total_possible_clips += 1
uid = f'{vid_path.stem}_{new_end}'
if uid in skip_uids:
continue
new_rows.append({
'participant_id': vid_path.stem.split('_')[0],
'narration': '',
'video_id': vid_path.stem,
'start': new_end - clip_len,
'end': new_end,
'verb_class': -1,
'noun_class': -1,
'action_class': -1,
'video_path': vid_path,
'uid': uid,
})
logging.info('Out of %d total potential clips, kept %d',
total_possible_clips, len(new_rows))
new_df = pd.DataFrame(new_rows)
if keep_orig_clips:
# Convert the uid to str since the new UIDs being added to the new DF
# are all strings
df.uid = df.uid.astype('str')
new_df = pd.concat([df, new_df])
new_df.reset_index(drop=True, inplace=True)
return new_df, pd.DataFrame([])
class BaseVideoDataset(torch.utils.data.Dataset):
"""Basic video dataset."""
def __init__(
self,
df,
root: Union[Sequence[Path], Path] = Path(''),
frames_per_clip: int = 32,
frame_rate: float = None,
subclips_options: Dict[str, float] = None,
load_seg_labels: bool = False,
load_long_term_future_labels: int = 0,
reader_fn: TargetConf = {
'_target_': 'datasets.reader_fns.DefaultReader'
},
transform: torchvision.transforms.Compose = None,
# verb, noun, action
label_type: Union[str, Sequence[str]] = 'verb',
return_future_clips_too: bool = False,
sample_strategy: str = SAMPLE_STRAT_RAND,
sample_strategy_future: str = SAMPLE_STRAT_FIRST,
conv_to_anticipate_fn: TargetConf = None,
conv_to_anticipate_fn_runtime: TargetConf = None,
process_df_before_read_fn: TargetConf = None,
sample_clips_densely: bool = False,
sample_clips_densely_fn: TargetConf = None,
random_seed: int = 42,
verb_classes: dict = {},
noun_classes: dict = {},
action_classes: dict = {},
repeat_data_times: float = 1.0,
dummy_label: Union[list, int] = -1,
class_balanced_sampling: bool = False,
return_unsampled_video: bool = False,
uid_subset: list = None):
"""
Args:
df: DataFrame of all the data (see a subclass for example/fmt).
Must be passed in through super() when init-ing the subclass
root: The path where all the videos are stored, will be
prepended to video path.
load_seg_labels: Set to true to load frame level segmentation
labels that can be jointly used to finetune the model for
classification as well.
load_long_term_future_labels: Set to the number of future labels
to also return, from where load_seg_labels stops. This is
used for long-term rollout visualization and getting GT for
those.
transform: The video transform function
return_future_clips_too: Set to true to also return future, actual
action clips along with the tau_o clips. This is used for SSL.
sample_strategy_future: Samplnig strategy used to return future
clips, if return_future_clips_too is set.
conv_to_anticipate_fn: The function that converts to anticipation.
conv_to_anticipate_fn_runtime: A similar fn as ^, but is applied
in the getitem function. Useful if don't want to do upfront,
for large datasets like HowTo.
sample_clips_densely: Add clips to the data frame sampling the
videos densely between the first and the last labeled clip.
The class label for those clips is -1, it's mostly just
used for SSL.
sample_clips_densely_fn: If this function is set, then no need
to set the sample_clip_densely to true. It will use this fn
to densify.
process_df_before_read_fn: A function that is applied to the
data frame[idx] before it's used for reading the video etc.
repeat_data: Set to number of times to repeat the data in the
DF. This is used if the epoch is too small, so can roll
through the data more than once during a single epoch. Also
helps if the preprocessing at read time effectively means
each data item corresponds to > 1 data items really through
random cropping etc.
class_balanced_sampling: If true, it will sample from the data
such that each class appears approximately equally -- so using
the distribution of labels, it will try to enforce unformity.
This is independent of adding loss weights based on how
often a class appears, which is done in train_eval_ops.
return_unsampled_video (bool): If true, return the video clip
before it was sub-sampled to match the FPS requirements.
So if experimenting at 1FPS, this will also return the
original frame rate clip that could be used for visualization.
MUST use batch size = 1 if using this, since it will return
different length videos which won't be batch-able.
uid_subset: Make a dataset keeping only those UIDs. This is useful
for visualization code when I just want to visualize on
specific clips.
"""
super().__init__()
# Based on https://github.com/pytorch/pytorch/issues/13246#issuecomment-612396143,
# trying to avoid mem leaks by wrapping lists and dicts in this
# manager class objects
manager = Manager()
self.root = root
# Convert to list if not already
if OmegaConf.get_type(self.root) != list:
self.root = [self.root]
self.root = [Path(el) for el in self.root]
self.subclips_options = subclips_options
self.load_seg_labels = load_seg_labels
self.load_long_term_future_labels = load_long_term_future_labels
# TODO: Move away from DataFrames... based on
# https://github.com/pytorch/pytorch/issues/5902#issuecomment-374611523
# it seems data frames are not ideal and cause memory leaks...
self.df = df # Data frame that will contain all info
# To be consistent with EPIC, add a uid column if not already present
if 'uid' not in self.df.columns:
self.df.loc[:, 'uid'] = range(1, len(self.df) + 1)
if sample_clips_densely or sample_clips_densely_fn:
if sample_clips_densely_fn is None:
# Use the default parameters. Keeping this sample_clips_densely
# param to be backward compatible.
sample_clips_densely_fn = {
'_target_':
'datasets.base_video_dataset.dense_clip_sampler',
}
self.df, _ = hydra.utils.call(sample_clips_densely_fn, self.df,
self.root)
assert not (conv_to_anticipate_fn and conv_to_anticipate_fn_runtime), (
'At max only one of these should be set.')
self.conv_to_anticipate_fn = conv_to_anticipate_fn
self.discarded_df = None
if conv_to_anticipate_fn is not None:
self.df, self.discarded_df = hydra.utils.call(
conv_to_anticipate_fn, self.df, self.root)
logging.info('Discarded %d elements in anticipate conversion',
len(self.discarded_df))
# this is an alternate implementation of ^, run in getitem,
# useful for large datasets like HowTo, but won't work for
# any dataset where you want to run testing
self.conv_to_anticipate_fn_runtime = conv_to_anticipate_fn_runtime
# This is used in the output files for EPIC submissions
self.challenge_type = 'action_recognition'
if conv_to_anticipate_fn or conv_to_anticipate_fn_runtime:
# If either of these are set, this must be an anticipation setup
self.challenge_type = 'action_anticipation'
self.repeat_data_times = repeat_data_times
self.process_df_before_read_fn = process_df_before_read_fn
self.frames_per_clip = frames_per_clip
self.frame_rate = frame_rate
self.reader_fn = hydra.utils.instantiate(reader_fn)
self.transform = transform
self.label_type = label_type
if OmegaConf.get_type(self.label_type) != list:
# Will use the first one for the balancing etc
self.label_type = [self.label_type]
self.verb_classes = manager.dict(verb_classes)
self.noun_classes = manager.dict(noun_classes)
self.action_classes = manager.dict(action_classes)
self.return_future_clips_too = return_future_clips_too
self.sample_strategy = sample_strategy
self.sample_strategy_future = sample_strategy_future
self.random_seed = random_seed
self.rng = np.random.default_rng(self.random_seed)
self.dummy_label = dummy_label
if isinstance(self.dummy_label, list):
self.dummy_label = manager.list(self.dummy_label)
# Precompute some commonly useful stats
self.classes_counts = manager.dict(self._compute_stats_cls_counts())
self.class_balanced_sampling = class_balanced_sampling
if self.class_balanced_sampling:
# sort the data frame by labels, to allow for the runtime
# remapping of idx
assert len(self.label_type) == 1, 'Not supported more yet'
self.df.sort_values(by=self.label_type[0] + '_class', inplace=True)
self.return_unsampled_video = return_unsampled_video
if self.return_unsampled_video:
logging.warning('Make sure using batch size = 1 since '
'return_unsampled_videos is set to True.')
# store the full DF so far in df_before_subset, since I will now keep a
# subset that may be used for testing etc. df_before_subset will be
# used to get intermediate labels for L_cls etc still (even during
# visualizations sometimes I want to show that)
self.df_before_subset = self.df
if uid_subset is not None:
# Select a subset in the order of the list
self.df = self.df.iloc[pd.Index(
self.df.uid).get_indexer(uid_subset)].reset_index(drop=True)
def _compute_stats_cls_counts(self):
"""
Compute some stats that are useful, like ratio of classes etc.
"""
all_classes_counts = {}
for tname, tclasses in self.classes.items():
col_name = tname + '_class'
if col_name not in self.df:
logging.warning('Didnt find %s column in %s', col_name,
self.df)
continue
lbls = np.array(self.df.loc[:, col_name].values)
# not removing the -1 labels, it's a dict so keep all of them.
classes_counts = {
cls_id: np.sum(lbls == cls_id)
for _, cls_id in [('', -1)] + tclasses.items()
}
assert sum(classes_counts.values()) == len(self.df)
all_classes_counts[tname] = classes_counts
logging.debug('Found %s classes counts', all_classes_counts)
return all_classes_counts
@property
def classes(self) -> OrderedDict:
return OrderedDict([(tname,
operator.attrgetter(tname + '_classes')(self))
for tname in self.label_type])
@property
def classes_manyshot(self) -> OrderedDict:
"""This is subset of classes that are labeled as "many shot".
These were used in EPIC-55 for computing recall numbers. By default
using all the classes.
"""
return self.classes
@property
def class_mappings(self) -> Dict[Tuple[str, str], torch.FloatTensor]:
return {}
@property
def primary_metric(self) -> str:
"""
The primary metric for this dataset. Datasets should override this
if top1 is not the metric to be used. This is the key to the dictionary
in the func/train.py when accuracies are computed. Some of these come
from the notebook utils.
"""
return 'final_acc/action/top1'
def _get_text(self, df_row, df_key='narration'):
if df_key in df_row:
text = df_row[df_key]
else:
text = ''
return text
def _get_label_from_df_row(self, df_row, tname):
col_name = tname + '_class'
if col_name not in df_row:
lbl = self.dummy_label
else:
lbl = df_row[col_name]
return lbl
def _get_labels(self, df_row) -> OrderedDict:
labels = OrderedDict()
for tname in self.label_type:
labels[tname] = self._get_label_from_df_row(df_row, tname)
return labels
@classmethod
def _sample(cls, video_path: Path, fps: float, start: float, end: float,
df_row: pd.DataFrame, frames_per_clip: int, frame_rate: float,
sample_strategy: str, reader_fn: nn.Module,
rng: np.random.Generator):
"""
Need this since VideoClip/RandomSampler etc are not quite compatible
with this dataset. So recreating that here. Gets the full clip and
crops out a fixed size region.
Args:
video_path: The path to read the video from
fps: What this video's natural FPS is.
start, end: floats of the start and end point in seconds
Returns:
video between start', end'; info of the video
"""
start = max(start, 0) # No way can read negative time anyway
end = max(end, 0) # No way can read negative time anyway
if fps <= 0:
logging.error('Found %f FPS video => likely empty [%s].', fps,
video_path)
fps = frame_rate # So code works, will anyway return black frames
req_fps = frame_rate
if req_fps is None:
req_fps = fps
nframes = int(fps * (end - start))
frames_to_ext = int(round(frames_per_clip * (fps / req_fps)))
# Find a point in the video and crop out
if sample_strategy == SAMPLE_STRAT_RAND:
start_frame = max(nframes - frames_to_ext, 0)
if start_frame > 0:
start_frame = rng.integers(start_frame)
elif sample_strategy == SAMPLE_STRAT_CNTR:
start_frame = max((nframes - frames_to_ext) // 2, 0)
elif sample_strategy == SAMPLE_STRAT_LAST:
start_frame = max(nframes - frames_to_ext, 0)
elif sample_strategy == SAMPLE_STRAT_FIRST:
start_frame = 0
else:
raise NotImplementedError(f'Unknown {sample_strategy}')
new_start = start + max(start_frame / fps, 0)
new_end = start + max((start_frame + frames_to_ext) / fps, 0)
# Do not bleed out.. since this function could be used for anticipation
# as well
new_end = max(min(end, new_end), 0)
# Start from the beginning of the video in case anticipation made it
# go even further back
new_start = min(max(new_start, 0), new_end)
args = [str(video_path), new_start, new_end, fps, df_row]
kwargs = dict(pts_unit='sec')
outputs = reader_fn(*args, **kwargs)
video, _, info = outputs
if new_start >= new_end:
video_frame_sec = new_start * torch.ones((video.size(0), ))
else:
video_frame_sec = torch.linspace(new_start, new_end, video.size(0))
assert video_frame_sec.size(0) == video.size(0)
# Subsample the video to the req_fps
if sample_strategy == SAMPLE_STRAT_LAST:
# From the back
frames_to_keep = range(
len(video))[::-max(int(round(fps / req_fps)), 1)][::-1]
else:
# Otherwise this is fine
frames_to_keep = range(len(video))[::max(int(round(fps /
req_fps)), 1)]
# Convert video to the required fps
video_without_fps_subsample = video
video = video[frames_to_keep]
video_frame_sec = video_frame_sec[frames_to_keep]
sampled_frames = torch.LongTensor(frames_to_keep)
info['video_fps'] = req_fps
# Ideally could have done the following operations only on the
# frames_to_keep and done the above slice after, but to avoid bugs
# and ensuring reproducibility (since earlier it was done separately),
# just doing on all separately
# Pad the video with the last frame, or crop out the extra frames
# so that it is consistent with the frames_per_clip
vid_t = video.size(0)
if video.ndim != 4 or (video.size(0) * video.size(1) * video.size(2) *
video.size(3)) == 0:
# Empty clip if any of the dims are 0, corrupted file likely
logging.warning('Generating empty clip...')
video = torch.zeros((frames_per_clip, 100, 100, 3),
dtype=torch.uint8)
video_frame_sec = -torch.ones((frames_per_clip, ))
sampled_frames = torch.range(0, frames_per_clip, dtype=torch.int64)
elif vid_t < frames_per_clip:
# # Repeat the video
# video_reqfps = torch.cat([video_reqfps] *
# int(math.ceil(frames_per_clip / vid_t)),
# dim=0)
# Pad the last frame..
if sample_strategy == SAMPLE_STRAT_LAST:
# Repeat the first frame
def padding_fn(T, npad):
return torch.cat([T[:1]] * npad + [T], dim=0)
else:
# Repeat the last frame
def padding_fn(T, npad):
return torch.cat([T] + [T[-1:]] * npad, dim=0)
npad = frames_per_clip - vid_t
logging.debug('Too few frames read, padding with %d frames', npad)
video = padding_fn(video, npad)
video_frame_sec = padding_fn(video_frame_sec, npad)
sampled_frames = padding_fn(sampled_frames, npad)
if sample_strategy == SAMPLE_STRAT_LAST:
video = video[-frames_per_clip:]
video_frame_sec = video_frame_sec[-frames_per_clip:]
sampled_frames = sampled_frames[-frames_per_clip:]
else:
video = video[:frames_per_clip]
video_frame_sec = video_frame_sec[:frames_per_clip]
sampled_frames = sampled_frames[:frames_per_clip]
# TODO(rgirdhar): Resample the audio in the same way too..
return (video, video_frame_sec, video_without_fps_subsample,
sampled_frames, info)
def _get_video(self, df_row):
# While we only need the absolute path for certain reader_fns, worth
# doing it for all since some might still need it to read fps etc.
video_path = get_abs_path(self.root, df_row['video_path'])
fps = self.reader_fn.get_frame_rate(video_path)
video_dict = {}
(video, video_frame_sec, video_without_fps_subsample,
frames_subsampled,
info) = self._sample(video_path, fps, df_row['start'], df_row['end'],
df_row, self.frames_per_clip, self.frame_rate,
self.sample_strategy, self.reader_fn, self.rng)
if 'audio_fps' not in info:
# somehow this is missing is some elts.. it causes issues with
# batching... anyway not using it so this is fine
info['audio_fps'] = 0
# Assuming no temporal transformation is done here (except moving the
# dimension around), so no need to change the video_frame_sec
video = self._apply_vid_transform(video)
video_dict['video'] = video
if self.return_unsampled_video:
video_without_fps_subsample = self._apply_vid_transform(
video_without_fps_subsample)
video_dict[
'video_without_fps_subsample'] = video_without_fps_subsample
video_dict['video_frames_subsampled'] = frames_subsampled
# Using video.size(-3) since at test there is a #crops dimension too
# in the front, so from back it will always work
assert video_frame_sec.size(0) == video.size(-3), (
'nothing should have changed temporally')
video_dict['video_frame_sec'] = video_frame_sec
video_dict['video_info'] = info
if self.return_future_clips_too:
assert 'orig_start' in df_row, 'Has to be anticipation data'
nfutures = len([
el for el in df_row.keys() if el.startswith(FUTURE_PREFIX)
]) // 2 # Since start and end for each
for future_id in range(nfutures):
video_future, _, _, _, _ = self._sample(
video_path, fps,
df_row[f'{FUTURE_PREFIX}_{future_id}_start'],
df_row[f'{FUTURE_PREFIX}_{future_id}_end'], df_row,
self.frames_per_clip, self.frame_rate,
self.sample_strategy_future, self.reader_fn, self.rng)
video_future = self._apply_vid_transform(video_future)
video_dict[f'{FUTURE_PREFIX}_{future_id}_video'] = video_future
video_dict['start'] = df_row['start']
video_dict['end'] = df_row['end']
return video_dict
def _get_subclips(self, video: torch.Tensor, num_frames: int, stride: int):
"""
Args:
video (C, T, *): The original read video
num_frames: Number of frames in each clip
stride: stride to use when getting clips
Returns:
video (num_subclips, C, num_frames, *)
"""
total_time = video.size(1)
subclips = []
for i in range(0, total_time, stride):
subclips.append(video[:, i:i + num_frames, ...])
return torch.stack(subclips)
def _get_vidseg_labels(self, df_row, video_frame_sec: torch.Tensor):
"""
Args:
video_frame_sec (#clips, T): The time point each frame in the video
comes from.
"""
this_video_df = self.df_before_subset[self.df_before_subset.video_path
== df_row.video_path]
assert video_frame_sec.ndim == 2
labels = OrderedDict()
for tname in self.label_type:
labels[tname] = -torch.ones_like(video_frame_sec, dtype=torch.long)
for clip_id in range(video_frame_sec.size(0)):
for t in range(video_frame_sec[clip_id].size(0)):
cur_t = video_frame_sec[clip_id][t].tolist()
matching_rows = this_video_df[
(this_video_df.orig_start <= cur_t)
& (this_video_df.orig_end >= cur_t)]
if len(matching_rows) == 0:
continue # Nothing labeled at this point
elif len(matching_rows) > 1:
# logging.warning(
# 'Found multiple labels for a given time. '
# 'Should not happen.. overlapping labels. '
# '%f %s %s', t, df_row, matching_rows)
# Apparently ^ happens often in epic100, so lets take the
# label closest to the center
closest_row = np.argmin(
np.abs(cur_t - np.array((
(matching_rows.orig_end -
matching_rows.orig_start) / 2.0).tolist())))
matching_row = matching_rows.iloc[closest_row]
else:
matching_row = matching_rows.iloc[0]
for tname in self.label_type:
labels[tname][clip_id][t] = self._get_label_from_df_row(
matching_row, tname)
return labels
def _apply_vid_transform(self, video):
# Only apply the transform to normal videos, not if features are
# being read
if video.nelement() == 0: # Placeholder
return video
if self.transform:
assert video.ndim == 4
if video.size(1) > 1 and video.size(2) > 1:
# Normal video with spatial dimension
video = self.transform(video)
else:
# Make sure the video is in the right permutation as expected
# Esp important when video is the RULSTM features
# TxHxWxC -> CxTxHxW
# No other transformation to be applied in this case
video = video.permute(3, 0, 1, 2)
return video
def addl_df_proc_for_dense(self, df_row):
"""
This function allows processing the DF row after it is passed through
the `process_df_before_read_fn` function, so it's like 2 layers of
processing. This is a function that a specific dataset can override.
Used by HowTo100M to convert narrations to classes
"""
return df_row
def __getitem__(self, idx):
idx = self._class_balance_data_idx(idx) # Must be run before repeat
idx = self._repeat_process_idx(idx)
df_row = self.df.loc[idx, :]
if self.conv_to_anticipate_fn_runtime is not None:
df_row = hydra.utils.call(self.conv_to_anticipate_fn_runtime,
df_row, self.df, self.root,
self.addl_df_proc_for_dense)
if df_row is None:
return None
if self.process_df_before_read_fn is not None:
df_row = hydra.utils.call(self.process_df_before_read_fn, df_row,
self.root, self.rng, self.label_type,
self.frames_per_clip, self.frame_rate,
self.sample_strategy, self.dummy_label)
if df_row is None:
return None
video_dict = self._get_video(df_row)
video = video_dict['video']
orig_video_shape = video.shape
if len(orig_video_shape) == 5:
# #ncrops, C, T, H, W -- flatten first 2 dims for subclips
video = video.flatten(0, 1)
# #ncrops * C, T, H, W -> #clips, #ncrops * C, T', H, W
video = self._get_subclips(video, **self.subclips_options)
if len(orig_video_shape) == 5:
# unflatten back
video = video.reshape((video.size(0), ) + orig_video_shape[:2] +
video.shape[-3:])
video_dict['video'] = video
video_dict['video_frame_sec'] = self._get_subclips(
video_dict['video_frame_sec'].unsqueeze(0),
# squeeze(1) because the 0th dim now will be the clips
**self.subclips_options).squeeze(1)
sentence = self._get_text(df_row) # Not used at the moment
label_idx = self._get_labels(df_row)
video_dict.update({
'idx':
idx,
'text':
sentence,
'target':
label_idx,
'audio': [], # TODO?
'orig_vid_len':
df_row.video_len if 'video_len' in df_row else -1,
'uid':
df_row.uid,
})
if self.load_seg_labels:
video_dict.update({
'target_subclips':
self._get_vidseg_labels(df_row, video_dict['video_frame_sec'])
})
if self.load_long_term_future_labels > 0:
# This is only really used for visualization for now
last_frame = video_dict['video_frame_sec'][-1].item()
gap_in_frames = (video_dict['video_frame_sec'][-1].item() -
video_dict['video_frame_sec'][-2].item())
video_dict.update({
'future_subclips':
self._get_vidseg_labels(
df_row,
torch.FloatTensor([
last_frame + gap_in_frames * i
for i in range(1, self.load_long_term_future_labels +
1)
]).reshape(-1, 1))
})
return video_dict
def _repeat_process_idx(self, idx):
"""
Depending on repeat_data_times, convert to the idx to actual idx.
"""
total_len = len(self.df)
scaled_idx = idx / self.repeat_data_times
if self.repeat_data_times < 1:
# Add some jitter, since it is being mapped to a bigger space
scaled_idx += self.rng.integers(int(1 / self.repeat_data_times))
scaled_idx = int(scaled_idx)
scaled_idx %= total_len
return scaled_idx
def _class_balance_data_idx(self, idx):
"""
If asked for balanced sampling based on labels, remap the idx to try to
follow a uniform distribution over the dataset, based on classes.
This must be run before repeating the df etc, since it assumes values
based on self.df (not repeated versions) (can be done, but this is
how it's currently implememented).
"""
if not self.class_balanced_sampling:
return idx
classes_counts = OrderedDict(self.classes_counts)
# if there is > 0 elements with -1, then keep it, else remove it
if classes_counts[-1] == 0:
del classes_counts[-1]
# By equal distribution, the idx should land in this class
# Counts sorted in the same way class IDs are sorted in the DF
cls_counts = [classes_counts[i] for i in sorted(classes_counts.keys())]
cls_cumsum = np.cumsum(cls_counts).tolist()
cls_firstelt = [0] + cls_cumsum[:-1]
share_per_class = max(cls_counts)
# effective idx, given that we would have replicated each class to have
# same number of elements
new_total_len = len(cls_counts) * share_per_class
old_total_len = sum(cls_counts)
# inflation_per_idx = (new_total_len - old_total_len) // len(old_total_len)
# Any random position in the scaled up indexing space
# eff_idx = (int(idx * (new_total_len / old_total_len)) +
# self.rng.integers(inflation_per_idx))
eff_idx = int(round(idx * ((new_total_len - 1) / (old_total_len - 1))))
assert eff_idx <= new_total_len
cls_idx = eff_idx // share_per_class
new_idx = self.rng.integers(cls_firstelt[cls_idx], cls_cumsum[cls_idx])
# Make sure it doesn't go over
new_idx = new_idx % len(self.df)
return new_idx
def __len__(self):
return int(len(self.df) * self.repeat_data_times)
|
AVT-main
|
datasets/base_video_dataset.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""Implementation of reader functions."""
import logging
from pathlib import Path
import torch
import torch.nn as nn
import torchvision
from common.utils import get_video_info
# An abstract class to keep track of all reader type classes
class Reader(nn.Module):
pass
class DefaultReader(Reader):
def forward(self, video_path, start, end, fps, df_row, **kwargs):
del df_row, fps # Not needed here
video_info = torchvision.io.read_video(video_path, start, end,
**kwargs)
# DEBUG see what is breaking
logging.debug('Read %s from %s', video_info[0].shape, video_path)
return video_info
@staticmethod
def get_frame_rate(video_path: Path) -> float:
return get_video_info(video_path, ['fps'])['fps']
class VideoAsLabelOnehotReader(Reader):
@staticmethod
def get_frame_rate(video_path: Path) -> float:
raise NotImplementedError('Not sure what it is here... TODO')
def forward(self,
video_path,
start,
end,
fps,
df_row,
pts_unit='sec',
num_classes=1000):
"""
Return the video as a 1-hot representation of the actual labels.
Args:
video_path
start: start time in sec
end: end time in sec
fps: frame rate of this video
df_row: The data frame row corresponding to this video. Includes
labels
num_classes: Total number of classes for the 1-hot representation.
Could just be a large number, should work too.
Returns:
video_feature of shape T x 1 x 1 x num_classes
"""
del pts_unit, video_path, start, fps
assert abs(end -
df_row['end']) < 0.1, 'For now just supporting last_clip'
labels = df_row['obs_action_class'][:, 1]
# Convert to 1-hot, TxC shape
feats = nn.functional.one_hot(torch.LongTensor(labels), num_classes)
return feats.unsqueeze(1).unsqueeze(1).float(), {}, {}
|
AVT-main
|
datasets/reader_fns.py
|
AVT-main
|
datasets/__init__.py
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""The Breakfast/50Salads dataset loader.
"""
from pathlib import Path
import logging
import pandas as pd
from tqdm import tqdm
import gzip
import numpy as np
import torch
import torch.nn as nn
import hydra
from hydra.types import TargetConf
from common.utils import get_video_info
from .base_video_dataset import BaseVideoDataset
from .reader_fns import Reader, DefaultReader
def load_mappings_file(fpath: Path) -> list:
"""
Read the mappings file shared by Abu farha (CVPR'18) to read the class
names.
"""
res = []
with open(fpath, 'r') as fin:
for line in fin:
res.append(line.rpartition(' ')[-1].strip())
# convert to dict
return dict(zip(res, range(len(res))))
def bundle_entry_to_video_fname_50salads(bundle_entry, root):
del root # not needed here
# remove the "rgb-"
video_id = bundle_entry.strip()[len('rgb-'):-(len('.txt'))]
video_fname = f'rgb-{video_id}.avi'
annot_fname = f'{video_id}-activityAnnotation.txt'
return video_fname, annot_fname
def read_orig_50salads_annotations(videos: list, root: Path,
action_classes: dict, annots_dir: Path,
timestamps_dir: Path):
all_segments = []
for video in videos:
video_fname, annot_fname = bundle_entry_to_video_fname_50salads(
video.strip(), root)
video_id = video.strip()[len('rgb-'):-(len('.txt'))]
ts_fpath = f'timestamps-{video_id}.txt'
frame_rate = get_video_info(Path(root) / video_fname, ['fps'])['fps']
frame_ts = []
# Read the "timestamp" of each frame
with open(Path(timestamps_dir) / ts_fpath, 'r') as fin:
for line in fin:
frame_ts.append(int(line.partition(' ')[0]))
first_start = len(frame_ts)
last_end = 0
with open(Path(annots_dir) / annot_fname, 'r') as fin:
for line in fin:
start_ts, end_ts, activity = line.split(' ')
act_pre, _, act_post = activity.strip().rpartition('_')
if not act_post in ['prep', 'core', 'post']:
# This is a coarse grained label, so ignore it
continue
label = action_classes[act_pre]
start = frame_ts.index(int(start_ts)) / frame_rate
first_start = min(first_start, start)
end = frame_ts.index(int(end_ts) + 1) / frame_rate
last_end = max(last_end, end)
all_segments.append((video_fname, start, end, label))
return all_segments
def bundle_entry_to_video_fname_breakfast(bundle_entry, root):
# remove the "rgb-"
person, camera, _, topic = bundle_entry.strip()[:-len('.txt')].split('_')
channels = ['']
if camera.startswith('stereo'):
channels = ['_ch0', '_ch1'] # ch0 is not always available
camera = 'stereo'
video_fname = f'{person}/{camera}/{person}_{topic}{{channel}}.avi'
annot_fname = f'{video_fname}.labels'
# Try both, if defined
for channel in channels:
if (Path(root) / annot_fname.format(channel=channel)).exists():
video_fname = video_fname.format(channel=channel)
annot_fname = annot_fname.format(channel=channel)
break
return video_fname, annot_fname
def read_orig_breakfast_annotations(videos: list, root: Path,
action_classes: dict):
all_segments = []
for video in videos:
video_fname, annot_fname = bundle_entry_to_video_fname_breakfast(
video.strip(), root)
# All videos are 15fps as says here:
# https://serre-lab.clps.brown.edu/resource/breakfast-actions-dataset/
# though can read out from the video if needed..
video_fps = 15
with open(Path(root) / annot_fname, 'r') as fin:
lines = [el.strip() for el in fin.readlines()]
# No longer removing SIL -- based on email conversation
# with Fadime, they keep everything, and drop any action segments
# they don't have any context for prediction; so the SIL from the
# beginning will be removed anyway.
# # Ignore the lead and end SIL (silence) segments as per
# # Sec 5.4 https://serre-lab.clps.brown.edu/wp-content/uploads/2014/05/paper_cameraReady-2.pdf (Unit recognition)
# if lines[0].endswith('SIL'):
# lines = lines[1:]
# if lines[-1].endswith('SIL'):
# lines = lines[:-1]
for line in lines:
start_end, activity = line.split(' ')
start, end = start_end.split('-')
if activity in action_classes:
label = action_classes[activity]
else:
logging.warning(
'Didnt find %s. Ignoring... Ideally '
'should merge with the next action, or '
'just use abu_farha annotations which '
'already does that.', activity)
continue
start = int(start) / video_fps
end = int(end) / video_fps
all_segments.append((video_fname, start, end, label))
return all_segments
def read_abu_farha_annotations(videos: list,
root: Path,
action_classes: dict,
annots_dir: Path,
bundle_entry_to_vname_fn: TargetConf,
frame_rate: int = None):
all_segments = []
for video in tqdm(videos, desc='Loading Abu Farha annots'):
video_fname, _ = hydra.utils.call(bundle_entry_to_vname_fn,
video.strip(), root)
if frame_rate is None:
frame_rate = get_video_info(Path(root) / video_fname,
['fps'])['fps']
with open(Path(annots_dir) / video.strip(), 'r') as fin:
cur_action = '' # dummy, will fire to insert action first
for lno, line in enumerate(fin):
if line == cur_action:
# Update the end time
# Using lno + 1 to avoid any gaps between the clips, which
# would lead to the -1 clips, making it harder for the
# model to learn
# Update the last added segment's end time point to this
# frame
all_segments[-1][-2] = (lno + 1) / frame_rate
continue
# Else a new action is starting, add to the segments
cur_action = line
label = action_classes[cur_action.strip()]
all_segments.append([
video,
video_fname,
lno / frame_rate, # start
(lno + 1) / frame_rate, # end
label,
])
return all_segments
def init_df(bundle_fpath: Path, annot_reader_fn: TargetConf, root: Path,
action_classes: dict):
with open(bundle_fpath, 'r') as fin:
videos = fin.readlines()
# Remove the "#bundle.txt" line from top
assert videos[0].startswith('#')
videos = videos[1:]
all_segments = hydra.utils.call(annot_reader_fn,
videos,
root,
action_classes,
_recursive_=False)
dataframe = pd.DataFrame(all_segments,
columns=[
'video_bundle_name', 'video_path', 'start',
'end', 'action_class'
])
dataframe = dataframe.astype(dtype={
'start': 'float16',
'end': 'float16',
'video_path': 'object',
})
return dataframe
class Breakfast50Salads(BaseVideoDataset):
"""Wrapper for Univ of Dundee 50Salads, or Bonn Breakfast dataset."""
def __init__(
self,
which: str, # specify which of BF or 50S
root: Path,
splits_dir: Path,
classes_fpath: Path,
is_train: bool = True,
fold: int = 1,
annot_reader_fn: TargetConf = None,
**kwargs):
bundle_fpath = (
Path(splits_dir) /
f'{"train" if is_train else "test"}.split{fold}.bundle')
self.which = which
if self.which == '50Salads':
assert 1 <= fold <= 5
elif self.which == 'Breakfast':
assert 1 <= fold <= 4
else:
raise NotImplementedError(f'Unknown type {which}')
action_classes = load_mappings_file(classes_fpath)
dataframe = init_df(bundle_fpath, annot_reader_fn, root,
action_classes)
kwargs['action_classes'] = action_classes
kwargs['label_type'] = 'action'
super().__init__(dataframe, root=root, **kwargs)
class FormatReader(nn.Module):
pass
class GZFormatReader(FormatReader):
def forward(self, path, start_frame, end_frame):
feats = []
with gzip.open(str(path).replace('.txt', '.gz'), 'r') as fin:
for lno, line in enumerate(fin):
if lno >= start_frame and lno <= end_frame:
feats.append(
[float(el) for el in line.strip().split(b' ')])
feats = torch.FloatTensor(feats)
return feats
class NPYFormatReader(FormatReader):
def forward(self, path, start_frame, end_frame):
feats = np.load(str(path).replace('.txt', '.npy'))
start_frame = max(start_frame, 0)
end_frame = min(end_frame, feats.shape[1])
feats_subset = feats[:, start_frame:(end_frame + 1)]
return torch.from_numpy(feats_subset.transpose())
class SenerFeatsReader(Reader):
def __init__(self, feat_dir: Path, format_reader: FormatReader):
super().__init__()
self.feat_dir = Path(feat_dir)
# No need to init the reader again, will be done recursively
self.format_reader = format_reader
def get_frame_rate(self, *args, **kwargs) -> float:
# Use the actual frame rate, since I guess that's what is used in the
# Abu Farha annotations, which is what the features here correspond
# to as well.
return DefaultReader.get_frame_rate(*args, **kwargs)
def forward(self,
video_path: Path,
start_sec: float,
end_sec: float,
fps: float,
df_row: pd.DataFrame,
pts_unit='sec'):
"""
Returns:
feats: (T, 1, 1, C) -- features shaped like a video
"""
del pts_unit, video_path # Not supported here
vidname = df_row['video_bundle_name'].strip()
start_frame = int(round(start_sec * fps - 1))
end_frame = int(round(end_sec * fps - 1))
feats = self.format_reader(self.feat_dir / vidname, start_frame,
end_frame)
return feats.unsqueeze(1).unsqueeze(1), {}, {}
|
AVT-main
|
datasets/breakfast_50salads.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
import os
import torch
from importlib import import_module
from tqdm import tqdm
import omegaconf
import hydra
from common import utils
__all__ = [
"get_dataset",
]
def get_dataset(dataset_cfg, data_cfg, transform, logger):
# If there is _precomputed_metadata file passed in, load that in
kwargs = {}
precomp_metadata_fpath = None
if '_precomputed_metadata_file' in dataset_cfg:
precomp_metadata_fpath = dataset_cfg._precomputed_metadata_file
# Remove from the config since otherwise can't init the obj
with omegaconf.open_dict(dataset_cfg):
del dataset_cfg['_precomputed_metadata_file']
if os.path.exists(precomp_metadata_fpath):
_precomputed_metadata = torch.load(precomp_metadata_fpath)
kwargs['_precomputed_metadata'] = _precomputed_metadata
kwargs['transform'] = transform
kwargs['frame_rate'] = data_cfg.frame_rate
kwargs['frames_per_clip'] = data_cfg.num_frames
# Have to call dict() here since relative interpolation somehow doesn't
# work once I get the subclips object
kwargs['subclips_options'] = dict(data_cfg.subclips)
kwargs['load_seg_labels'] = data_cfg.load_seg_labels
logger.info('Creating the dataset object...')
# Not recursive since many of the sub-instantiations would need positional
# arguments
_dataset = hydra.utils.instantiate(dataset_cfg,
_recursive_=False,
**kwargs)
try:
logger.info('Computing clips...')
_dataset.video_clips.compute_clips(data_cfg.num_frames,
1,
frame_rate=data_cfg.frame_rate)
logger.info('Done')
except AttributeError: # if video_clips not in _dataset
logger.warning('No video_clips present')
logger.info(f'Created dataset with {len(_dataset)} elts')
if precomp_metadata_fpath and not os.path.exists(precomp_metadata_fpath):
utils.save_on_master(_dataset.metadata, precomp_metadata_fpath)
return _dataset
|
AVT-main
|
datasets/data.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""
Implementation of the future features prediction models.
Input: (B, C)
Output: (B, C)
"""
import torch
import torch.nn as nn
import transformers
import logging
import hydra
from common.cluster import KmeansAssigner
class Identity(nn.Module):
"""Wrapper around the Identity fn to drop target_shape etc."""
def __init__(self, in_features):
super().__init__()
self.in_features = in_features
def forward(self, feats, target_shape=None):
del target_shape # not needed here
return feats, feats, {}, {}
@property
def output_dim(self):
return self.in_features
class MLP(nn.Module):
def __init__(self, in_features, num_layers=2):
super().__init__()
self.in_features = in_features
layers = [[nn.Linear(in_features, in_features),
nn.ReLU(inplace=True)] for _ in range(num_layers)]
# Flatten, remove the last ReLU, and create a sequential
self.model = nn.Sequential(
*([item for sublist in layers for item in sublist][:-1]))
def forward(self, feats, target_shape=None):
del target_shape
return feats, self.model(feats), {}, {}
@property
def output_dim(self):
return self.in_features
class AVTh(nn.Module):
"""AVT head architecture."""
def __init__(
self,
in_features: int,
output_len: int = -1,
output_len_eval: int = -1, # Same as output_len, used during eval
avg_last_n: int = -1,
inter_dim: int = 768,
future_pred_loss: hydra.types.TargetConf = None,
return_past_too: bool = False,
drop_last_n: int = 0,
quantize_before_rollout: bool = False,
# This is only relevant when in_features=1 and input is
# clustered, or if on the fly cluster assgn is requested
assign_to_centroids: str = None,
num_cluster_centers: int = 50000,
freeze_encoder_decoder: bool = False,
**kwargs):
super().__init__()
self.assign_to_centroids = assign_to_centroids
if self.assign_to_centroids:
# Since we will be assign the features
assert in_features != 1
self.assigner = KmeansAssigner(assign_to_centroids)
assert self.assigner.num_clusters == num_cluster_centers
if in_features == 1 or assign_to_centroids:
self.encoder = nn.Embedding(num_cluster_centers, inter_dim)
else:
self.encoder = nn.Linear(in_features, inter_dim, bias=False)
self.decoder = nn.Linear(inter_dim, in_features, bias=False)
# If encoder is an embedding, then tie up the weights
if isinstance(self.encoder, nn.Embedding):
self.decoder.weight = self.encoder.weight
if freeze_encoder_decoder:
self.encoder.weight.requires_grad = False
self.decoder.weight.requires_grad = False
# This already has the LayerNorm inside residual, as Naman suggested.
self.gpt_model = transformers.GPT2Model(
transformers.GPT2Config(n_embd=inter_dim,
vocab_size=in_features,
use_cache=True,
**kwargs))
# Not needed, encoder will take care of it.
del self.gpt_model.wte
self.output_len = output_len
self.output_len_eval = output_len_eval
self.avg_last_n = avg_last_n
self.inter_dim = inter_dim
self.in_features = in_features
if future_pred_loss is not None:
self.future_pred_loss = hydra.utils.instantiate(future_pred_loss,
reduction='none')
else:
self.future_pred_loss = None
self.return_past_too = return_past_too
self.drop_last_n = drop_last_n
# Set this, if want to quantize the prediction (using top-1) and
# re-encode, as opposed to using the soft predicted feature
self.quantize_before_rollout = quantize_before_rollout
def forward(self, feats, target_shape):
"""
Args:
feats: tensor of shape (B, T, C)
target_shape: shape of the output (B, T', n_output)
"""
addl_endpoints = {}
if feats.ndim == 2:
# add back the temporal dimension, which was likely mean pooled
feats = feats.unsqueeze(1)
# Decide the output len based on the target_shape
if len(target_shape) == 3:
output_len = target_shape[1]
elif self.training or self.output_len_eval < 0:
# If training mode or output_len for eval has not been set
output_len = self.output_len
else: # eval mode
output_len = self.output_len_eval
# Keep track
full_inp_feats = feats
if self.assign_to_centroids:
# Unsqueeze only to be compatible with the 1 channel inputs -- that
# will get squeezed out later
feats = self.assigner(feats).unsqueeze(-1)
# The time dimension in already in the middle -> B, T, C
# That's what huggingface version needs:
# (batch_size, sequence_length, hidden_size)
if self.in_features == 1 or self.assign_to_centroids:
# This is a quantized input, so cast it to long, and remove the
# last singleton dimension
assert feats.size(-1) == 1
feats = feats.squeeze(-1).long()
# Keep only the first N, this is used when the model is given
# input more frames than it should be using for prediction. The other
# future is used to incur loss during training, but shouldn't otherwise
# be used, so dropping those features
full_orig_feats = feats
inp_feats = full_inp_feats
if self.drop_last_n != 0:
logging.warning('This should be used very carefully, ideally only '
'for debugging. The padding can lead to some '
'frames from the actual clip to leak into the '
'past clip, even after dropping last n. So even '
'after dropping the model might end up seeing '
'frames that are beyond the tau_a.')
feats = feats[:, :-self.drop_last_n]
inp_feats = inp_feats[:, :-self.drop_last_n]
# Keep track
orig_feats_len = feats.size(1)
# Reduce the dimensionality, since not using the GPT encoding matrix,
# since I don't have a "token" representation
feats = self.encoder(feats)
orig_feats_encoded = feats
past = None
all_outputs = []
all_outputs_decoded = []
for output_id in range(output_len):
pred_so_far = sum([el.size(1) for el in all_outputs])
position_ids = torch.arange(pred_so_far,
pred_so_far + feats.size(1),
dtype=torch.long,
device=feats.device)
# The past output will encode the previous past AND the new input
# (you can check the output, it keeps increasing)
# Got this from
# https://huggingface.co/transformers/quickstart.html#using-the-past
outputs = self.gpt_model(inputs_embeds=feats,
past_key_values=past,
position_ids=position_ids)
last_hidden_state = outputs.last_hidden_state
past = outputs.past_key_values
all_outputs.append(last_hidden_state)
# For visualization later, if output_attentions was passed into gpt
if outputs.attentions is not None:
# dimensions will be (batch_size, nlayers, nheads, seqlen, seqlen)
addl_endpoints[f'gpt2_att_{output_id}'] = torch.stack(
outputs.attentions).transpose(0, 1)
# Map back to the original feature dimension
all_outputs_decoded.append(self.decoder(last_hidden_state))
# hidden_states[-1] or last_hidden_state is the embedding from the
# final layer. Not using logits (earlier was using the LMHead model
# that returned logits) since that is already decoded to vocab size
# and I want to have control over the weights of that final matrix
# Also, the input for the next would be encodings, so need to
# access the encodings directly
if self.quantize_before_rollout:
assert isinstance(self.encoder, nn.Embedding)
feats = self.encoder(
all_outputs_decoded[-1][:, -1:, :].argmax(dim=-1))
else:
feats = last_hidden_state[:, -1:, :]
all_outputs = torch.cat(all_outputs, dim=1)
all_outputs_decoded = torch.cat(all_outputs_decoded, dim=1)
# Compute a loss on future prediction (teacher forced)
losses = {}
if self.future_pred_loss is not None:
num_elts_for_loss = min(full_orig_feats.size(1),
all_outputs_decoded.size(1))
losses = {
'feat':
self.future_pred_loss(
all_outputs_decoded[:, :num_elts_for_loss - 1],
full_orig_feats[:, 1:num_elts_for_loss])
}
# Set all_output as the final output features, and prev as the
# structure to use to get the original features of past
if self.in_features == 1:
prev = orig_feats_encoded
# all_outputs contains the hidden states, the best we will get
# anyway, so that doesn't change
elif self.assign_to_centroids:
prev = inp_feats # For this, I have the orig feats, so use that
# For prediction, use the predicted cluster centers, but use
# features from the original kmeans, not what the embeddings
# that were learnt.. it didn't work with them
all_outputs = self.assigner(all_outputs_decoded.argmax(dim=-1))
else:
prev = inp_feats
all_outputs = all_outputs_decoded
# Return the actual predictions
if self.return_past_too:
# Pad in the GT past (no point using the predicted past when
# we have the actual past)
final = torch.cat((prev, all_outputs[:, orig_feats_len - 1:, :]),
dim=1)
elif output_len > 0:
final = all_outputs[:, -output_len:]
else:
final = all_outputs
if self.avg_last_n > 0:
final = torch.mean(final[:, -self.avg_last_n:, :], dim=1)
# compute the past feature.
assert prev.size(1) == orig_feats_len, (
'If not, need to figure how to deal')
# Now keep the old feature for the first one, and return the predicted
# features shifted by 1 for the rest -- which are as predicted by
# GPT
updated_past_feat = torch.cat(
[prev[:, :1, :], all_outputs[:, :(orig_feats_len - 1)]], dim=1)
return updated_past_feat, final, losses, addl_endpoints
@property
def output_dim(self):
if self.in_features == 1:
return self.inter_dim # since it will return encoded features
# else, it will decode it back to the original feat dimension
return self.in_features
|
AVT-main
|
models/future_prediction.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""
Implementation of the temporal aggregation algorithms.
Input: (B, C, T)
Output: (B, C)
"""
import math
import torch
import torch.nn as nn
import logging
import warnings
try:
from external.rulstm.RULSTM.models import RULSTM
except ImportError:
RULSTM = object
logging.warning('No RULSTM found.')
class Identity(nn.Identity):
def __init__(self, in_features):
super().__init__()
self.in_features = in_features
def forward(self, *args, **kwargs):
return super().forward(*args, **kwargs), {}
@property
def output_dim(self):
return self.in_features
class Mean(nn.Module):
def __init__(self, in_features):
super().__init__()
self.in_features = in_features
def forward(self, feats):
"""
feats: B, T, C dimensional input
"""
return torch.mean(feats, dim=1), {}
@property
def output_dim(self):
return self.in_features
class PositionalEncoding(nn.Module):
"""For now, just using simple pos encoding from language.
https://pytorch.org/tutorials/beginner/transformer_tutorial.html
"""
def __init__(self, d_model, dropout=0.1, max_len=5000):
super(PositionalEncoding, self).__init__()
self.dropout = nn.Dropout(p=dropout)
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(
torch.arange(0, d_model, 2).float() *
(-math.log(10000.0) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return self.dropout(x)
class Transformer(nn.Module):
""" Using a transformer encoder and simple decoder. """
def __init__(self,
in_features,
inter_rep=512,
nheads=8,
nlayers=6,
agg_style='mean',
cloze_loss_ratio=0.0,
cloze_loss_wt=0.0):
super().__init__()
self.in_features = in_features
self.inter_rep = inter_rep
self.downproject = nn.Linear(in_features, inter_rep)
layer = nn.TransformerEncoderLayer(d_model=inter_rep, nhead=nheads)
# Don't think I'll ever consider longer than 1000 features?
self.pos_encoder = PositionalEncoding(inter_rep, max_len=1000)
self.transformer_encoder = nn.TransformerEncoder(
layer, num_layers=nlayers, norm=nn.LayerNorm(inter_rep))
self.agg_style = agg_style
self.cloze_loss_ratio = cloze_loss_ratio
self.cloze_loss_wt = cloze_loss_wt
self.cloze_loss_fn = nn.MSELoss(reduction='none')
# The embedding for the [MASK] token
if self.cloze_loss_ratio > 0:
self.extra_embeddings = nn.Embedding(1, in_features)
def forward(self, feats):
"""
Args:
feats (B, T, C)
Returns:
aggregated features (B, C')
"""
# Convert to the format used by transformer: T, B, C
feats = feats.transpose(0, 1)
kwargs = {}
if self.training and self.cloze_loss_ratio > 0:
# Mask out certain positions, so when doing attention these
# positions will be ignored
key_padding_mask = torch.rand((feats.size(0), feats.size(1)),
device=feats.device)
# Get close_ratio amount as True, so those will be ignored
key_padding_mask = key_padding_mask <= self.cloze_loss_ratio
# Set the features to MASK embedding, for the ones that are masked
key_padding_mask_rep = key_padding_mask.unsqueeze(-1).expand(
-1, -1, feats.size(2))
# Set the masked elements to 0, and add the MASK embedding
replaced_feats = (
feats * (~key_padding_mask_rep) +
key_padding_mask_rep * self.extra_embeddings(
torch.tensor([0], dtype=torch.long,
device=feats.device)).unsqueeze(0))
feats = replaced_feats
# Transpose since the function takes in B, T
kwargs['src_key_padding_mask'] = key_padding_mask.t()
feats = self.pos_encoder(self.downproject(feats))
feats_encoded = self.transformer_encoder(feats, **kwargs)
aux_losses = {}
if self.training and self.cloze_loss_ratio > 0:
dist = self.cloze_loss_fn(feats_encoded, feats)
dist_masked_elts = self.cloze_loss_wt * torch.mean(
torch.mean(dist, dim=-1) * key_padding_mask)
aux_losses['tx_mlm'] = dist_masked_elts
if self.agg_style == 'mean':
res = torch.mean(feats_encoded, dim=[0])
elif self.agg_style == 'last':
res = feats_encoded[-1]
else:
raise NotImplementedError(f'Unknown agg style {self.agg_style}')
return res, aux_losses
@property
def output_dim(self):
return self.inter_rep
class RULSTMAggregation(RULSTM):
def __init__(self,
in_features: int,
intermediate_featdim: int = 1024,
dropout: float = 0.8,
num_pad_feats: int = 0):
"""
Args:
num_pad_feats (int): Pad the features with zero feats for this
many times on the time axis. This is because the unrolling
LSTM unrolls forward as many times as input, and since original
models were trained for 14 steps unrolling (upto 0.25s
before the action), and I usually test for 11 steps (1s before
action), need to pad 3 times to get the same output when
testing pre-trained models.
"""
super().__init__(1, in_features, intermediate_featdim, dropout)
# Remove the classifier, since the outside code will deal with that
self.classifier = nn.Sequential()
self.output_dim = intermediate_featdim
self.num_pad_feats = num_pad_feats
# Ignore warnings because it UserWarning: RNN module weights are not
# part of single contiguous chunk of memory. This means they need to be
# compacted at every call, possibly greatly increasing memory usage.
# To compact weights again call flatten_parameters().
# Not sure how to fix this, adding the flatten didn't really fix
# Happens only with DataParallel, not DDP
# Using https://github.com/pytorch/pytorch/issues/24155#issuecomment-604474511
# Just ignoring the warning
warnings.filterwarnings('ignore')
def forward(self, feats):
"""
Args:
feats (B, T, C)
Returns:
aggregated (B, C)
"""
if self.num_pad_feats > 0:
empty_feats = torch.zeros(
(feats.size(0), self.num_pad_feats, feats.size(-1)),
dtype=feats.dtype,
device=feats.device)
feats = torch.cat([feats, empty_feats], dim=1)
res = super().forward(feats)
# Return output corresponding to the last input frame. Note that in
# original RULSTM they do -4 since they predict 3 steps further into
# the anticipation time, whereas I stop when the anticipation time
# starts here.
# Subtract num_pad_feat as that would mean it predicted further into
# the future
return res[:, -1 - self.num_pad_feats, :], {}
|
AVT-main
|
models/temporal_aggregation.py
|
AVT-main
|
models/__init__.py
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""
Model architectures.
"""
import torch.nn as nn
from torchvision.models.video.resnet import (
BasicBlock,
Bottleneck,
R2Plus1dStem,
_video_resnet,
)
from pretrainedmodels import bninception
import timm
__all__ = [
'r2plus1d_34',
'r2plus1d_152',
'ir_csn_152',
'ip_csn_152',
'ip_csn_50',
'BNInceptionVideo',
]
class BasicStem_Pool(nn.Sequential):
def __init__(self):
super(BasicStem_Pool, self).__init__(
nn.Conv3d(
3,
64,
kernel_size=(3, 7, 7),
stride=(1, 2, 2),
padding=(1, 3, 3),
bias=False,
),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1, 3, 3),
stride=(1, 2, 2),
padding=(0, 1, 1)),
)
class Conv3DDepthwise(nn.Conv3d):
def __init__(self,
in_planes,
out_planes,
midplanes=None,
stride=1,
padding=1):
assert in_planes == out_planes
super(Conv3DDepthwise, self).__init__(
in_channels=in_planes,
out_channels=out_planes,
kernel_size=(3, 3, 3),
stride=stride,
padding=padding,
groups=in_planes,
bias=False,
)
@staticmethod
def get_downsample_stride(stride):
return (stride, stride, stride)
class IPConv3DDepthwise(nn.Sequential):
def __init__(self, in_planes, out_planes, midplanes, stride=1, padding=1):
assert in_planes == out_planes
super(IPConv3DDepthwise, self).__init__(
nn.Conv3d(in_planes, out_planes, kernel_size=1, bias=False),
nn.BatchNorm3d(out_planes),
# nn.ReLU(inplace=True),
Conv3DDepthwise(out_planes, out_planes, None, stride),
)
@staticmethod
def get_downsample_stride(stride):
return (stride, stride, stride)
class Conv2Plus1D(nn.Sequential):
def __init__(self, in_planes, out_planes, midplanes, stride=1, padding=1):
midplanes = (in_planes * out_planes * 3 * 3 *
3) // (in_planes * 3 * 3 + 3 * out_planes)
super(Conv2Plus1D, self).__init__(
nn.Conv3d(
in_planes,
midplanes,
kernel_size=(1, 3, 3),
stride=(1, stride, stride),
padding=(0, padding, padding),
bias=False,
),
nn.BatchNorm3d(midplanes),
nn.ReLU(inplace=True),
nn.Conv3d(
midplanes,
out_planes,
kernel_size=(3, 1, 1),
stride=(stride, 1, 1),
padding=(padding, 0, 0),
bias=False,
),
)
@staticmethod
def get_downsample_stride(stride):
return (stride, stride, stride)
def _set_bn_params(model, bn_eps=1e-3, bn_mom=0.1):
"""
Set the BN parameters to the defaults: Du's models were trained
with 1e-3 and 0.9 for eps and momentum resp.
Ref: https://github.com/facebookresearch/VMZ/blob/f4089e2164f67a98bc5bed4f97dc722bdbcd268e/lib/models/r3d_model.py#L208
"""
for module in model.modules():
if isinstance(module, nn.BatchNorm3d):
module.eps = bn_eps
module.momentum = bn_mom
def r2plus1d_34(pretrained=False,
progress=False,
bn_eps=1e-3,
bn_mom=0.1,
**kwargs):
model = _video_resnet("r2plus1d_34",
False,
False,
block=BasicBlock,
conv_makers=[Conv2Plus1D] * 4,
layers=[3, 4, 6, 3],
stem=R2Plus1dStem,
**kwargs)
_set_bn_params(model, bn_eps, bn_mom)
return model
def r2plus1d_152(pretrained=False,
progress=False,
bn_eps=1e-3,
bn_mom=0.1,
**kwargs):
model = _video_resnet("r2plus1d_152",
False,
False,
block=Bottleneck,
conv_makers=[Conv2Plus1D] * 4,
layers=[3, 8, 36, 3],
stem=R2Plus1dStem,
**kwargs)
_set_bn_params(model, bn_eps, bn_mom)
return model
def ir_csn_152(pretrained=False,
progress=False,
bn_eps=1e-3,
bn_mom=0.1,
**kwargs):
model = _video_resnet("ir_csn_152",
False,
False,
block=Bottleneck,
conv_makers=[Conv3DDepthwise] * 4,
layers=[3, 8, 36, 3],
stem=BasicStem_Pool,
**kwargs)
_set_bn_params(model, bn_eps, bn_mom)
return model
def ip_csn_152(pretrained=False,
progress=False,
bn_eps=1e-3,
bn_mom=0.1,
**kwargs):
model = _video_resnet("ip_csn_152",
False,
False,
block=Bottleneck,
conv_makers=[IPConv3DDepthwise] * 4,
layers=[3, 8, 36, 3],
stem=BasicStem_Pool,
**kwargs)
_set_bn_params(model, bn_eps, bn_mom)
return model
def ip_csn_50(pretrained=False,
progress=False,
bn_eps=0.3,
bn_mom=0.1,
**kwargs):
model = _video_resnet("ip_csn_50",
False,
False,
block=Bottleneck,
conv_makers=[IPConv3DDepthwise] * 4,
layers=[3, 8, 6, 3],
stem=BasicStem_Pool,
**kwargs)
_set_bn_params(model, bn_eps, bn_mom)
return model
def process_each_frame(model, video, *args, **kwargs):
"""
Pass in each frame separately
Args:
video (B, C, T, H, W)
Returns:
feats: (B, C', T, 1, 1)
"""
batch_size = video.size(0)
time_dim = video.size(2)
video_flat = video.transpose(1, 2).flatten(0, 1)
feats_flat = model(video_flat, *args, **kwargs)
return feats_flat.view((batch_size, time_dim) +
feats_flat.shape[1:]).transpose(
1, 2).unsqueeze(-1).unsqueeze(-1)
class FrameLevelModel(nn.Module):
"""Runs a frame level model on all the frames."""
def __init__(self, num_classes: int, model: nn.Module = None):
del num_classes
super().__init__()
self.model = model
def forward(self, video, *args, **kwargs):
return process_each_frame(self.model, video, *args, **kwargs)
class BNInceptionVideo(FrameLevelModel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = bninception(*args, **kwargs)
self.model.last_linear = nn.Identity()
self.model.global_pool = nn.AdaptiveAvgPool2d(1)
class TIMMModel(FrameLevelModel):
def __init__(self,
num_classes,
model_type='vit_base_patch16_224',
drop_cls=True):
super().__init__(num_classes)
model = timm.create_model(model_type,
num_classes=0 if drop_cls else num_classes)
self.model = model
|
AVT-main
|
models/video_classification.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
"""
The overall base model.
"""
from typing import Dict, Tuple
import operator
import torch
import torch.nn as nn
import hydra
from omegaconf import OmegaConf
CLS_MAP_PREFIX = 'cls_map_'
PAST_LOGITS_PREFIX = 'past_'
class BaseModel(nn.Module):
def __init__(self, model_cfg: OmegaConf, num_classes: Dict[str, int],
class_mappings: Dict[Tuple[str, str], torch.FloatTensor]):
super().__init__()
# Takes as input (B, T, H, W, C) -> (B, T', H', W', C')
_backbone_full = hydra.utils.instantiate(
model_cfg.backbone,
# Add dummy value for num_cls
# will be removed next anyway
num_classes=1)
if model_cfg.backbone_last_n_modules_to_drop > 0:
self.backbone = nn.Sequential()
for name, child in list(_backbone_full.named_children(
))[:-model_cfg.backbone_last_n_modules_to_drop]:
self.backbone.add_module(name, child)
else:
self.backbone = _backbone_full
# Map the (B, T', H', W', C') -> (B, T', H', W', C*)
# to the intermediate feature dimensions
# IMP: this is only used if C' != C*
if (model_cfg.backbone_last_n_modules_to_drop == 0
and 'output_dim' in dir(self.backbone)):
backbone_dim = self.backbone.output_dim
else:
backbone_dim = model_cfg.backbone_dim # TODO: Figure automatically
self.mapper_to_inter = None
if model_cfg.intermediate_featdim is None:
model_cfg.intermediate_featdim = backbone_dim
if backbone_dim != model_cfg.intermediate_featdim:
self.mapper_to_inter = nn.Linear(backbone_dim,
model_cfg.intermediate_featdim,
bias=False)
# Takes as input (B, T', H', W', C*) -> (B, C**)
self.temporal_aggregator = hydra.utils.instantiate(
model_cfg.temporal_aggregator,
in_features=model_cfg.intermediate_featdim)
self.reset_temp_agg_feat_dim = nn.Sequential()
temp_agg_output_dim = self.temporal_aggregator.output_dim
if model_cfg.same_temp_agg_dim and (temp_agg_output_dim !=
model_cfg.intermediate_featdim):
# Ideally want to maintain it so that the same project_mlp
# can be used for the temporally aggregated features, or the
# original features.
self.reset_temp_agg_feat_dim = nn.Linear(
temp_agg_output_dim, model_cfg.intermediate_featdim)
temp_agg_output_dim = model_cfg.intermediate_featdim
# Transforms the current features to future ones
# (B, C**) -> (B, C**)
self.future_predictor = hydra.utils.instantiate(
model_cfg.future_predictor,
in_features=temp_agg_output_dim,
_recursive_=False)
# Projection layer
self.project_mlp = nn.Sequential()
if model_cfg.project_dim_for_nce is not None:
self.project_mlp = nn.Sequential(
nn.Linear(temp_agg_output_dim, temp_agg_output_dim),
nn.ReLU(inplace=True),
nn.Linear(temp_agg_output_dim, model_cfg.project_dim_for_nce))
# 2nd round of temporal aggregation, if needed
self.temporal_aggregator_after_future_pred = hydra.utils.instantiate(
model_cfg.temporal_aggregator_after_future_pred,
self.future_predictor.output_dim)
# Dropout
self.dropout = nn.Dropout(model_cfg.dropout)
# Takes as input (B, C**) -> (B, num_classes)
cls_input_dim = self.temporal_aggregator_after_future_pred.output_dim
# Make a separate classifier for each output
self.classifiers = nn.ModuleDict()
self.num_classes = num_classes
for i, (cls_type, cls_dim) in enumerate(num_classes.items()):
if model_cfg.use_cls_mappings and i > 0:
# In this case, rely on the class mappings to generate the
# other predictions, rather than creating a new linear layer
break
self.classifiers.update({
cls_type:
hydra.utils.instantiate(model_cfg.classifier,
in_features=cls_input_dim,
out_features=cls_dim)
})
# Store the class mappings as buffers
for (src, dst), mapping in class_mappings.items():
self.register_buffer(f'{CLS_MAP_PREFIX}{src}_{dst}', mapping)
self.regression_head = None
if model_cfg.add_regression_head:
self.regression_head = nn.Linear(cls_input_dim, 1)
# Init weights, as per the video resnets
self._initialize_weights()
# Set he BN momentum and eps here, Du uses a different value and its imp
self._set_bn_params(model_cfg.bn.eps, model_cfg.bn.mom)
self.cfg = model_cfg
def _initialize_weights(self):
# Copied over from
# https://github.com/pytorch/vision/blob/75f5b57e680549d012b3fc01b356b2fb92658ea7/torchvision/models/video/resnet.py#L261
# Making sure all layers get init to good video defaults
for m in self.modules():
if isinstance(m, nn.Conv3d):
nn.init.kaiming_normal_(m.weight,
mode='fan_out',
nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm3d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
def _set_bn_params(self, bn_eps=1e-3, bn_mom=0.1):
"""
Set the BN parameters to the defaults: Du's models were trained
with 1e-3 and 0.9 for eps and momentum resp.
Ref: https://github.com/facebookresearch/VMZ/blob/f4089e2164f67a98bc5bed4f97dc722bdbcd268e/lib/models/r3d_model.py#L208
"""
for module in self.modules():
if isinstance(module, nn.BatchNorm3d):
module.eps = bn_eps
module.momentum = bn_mom
def forward_singlecrop(self, video, target_shape=None):
"""
Args:
video (torch.Tensor, Bx#clipsxCxTxHxW)
target_shape: The shape of the target. Some of these layers might
be able to use this information.
"""
outputs = {}
aux_losses = {}
batch_size = video.size(0)
num_clips = video.size(1)
# Fold the clips dimension into the batch for feature extraction, upto
# temporal aggregation
video = video.flatten(0, 1)
feats = self.backbone(video)
outputs['backbone'] = feats
# Spatial mean
feats = torch.mean(feats, [-1, -2])
# store temporal mean as well
outputs['backbone_mean'] = torch.mean(feats, [-1])
# If it's not sequential and can be applied here
if len(self.project_mlp) > 0 and (outputs['backbone_mean'].size(-1) ==
self.project_mlp[0].in_features):
outputs['backbone_mean_projected'] = self.project_mlp(
outputs['backbone_mean'])
# Move the time dimension inside: B,C,T -> B,T,C
feats = feats.permute((0, 2, 1))
# Map the feats to intermediate dimension, that rest of the code
# will operate on. Only if the original feature is not already
if feats.shape[-1] != self.cfg.intermediate_featdim:
assert self.mapper_to_inter is not None, (
f'The backbone feat does not match intermediate {feats.shape} '
f'and {self.cfg.intermediate_featdim}. Please set '
f'model.backbone_dim correctly.')
feats = self.mapper_to_inter(feats)
feats_agg, agg_losses = self.temporal_aggregator(feats)
aux_losses.update(agg_losses)
feats_agg = self.reset_temp_agg_feat_dim(feats_agg)
outputs['temp_agg'] = feats_agg
# For the contrastive loss, I need a projected version of this feature
outputs['temp_agg_projected'] = self.project_mlp(feats_agg)
# Now before future prediction, need to unfold the clips back out,
# and concat on the temporal dimension
if num_clips > 1:
assert (
(feats_agg.ndim == 2)
or (feats_agg.ndim == 3 and feats_agg.size(1) == 1)
), ('Should be using some temporal aggregation when using clips')
feats_agg = feats_agg.reshape((batch_size, num_clips) +
feats_agg.shape[1:])
if feats_agg.ndim == 4:
feats_agg = torch.flatten(feats_agg, 1, 2)
# now feats_agg back to 3D (B, T, F)
feats_past = feats_agg
# Now the future prediction, also it might update the past features
# like the GPT style models would
(feats_past, feats_future, future_losses,
endpoints) = self.future_predictor(feats_past, target_shape)
aux_losses.update(future_losses)
outputs.update(endpoints)
outputs['future'] = feats_future
outputs['past'] = feats_past
# Apply a classifier on the past features, might be supervising that
if self.cfg.classifier_on_past:
feats_past_drop = self.dropout(feats_past)
outputs.update(
self._apply_classifier(feats_past_drop,
outputs_prefix=PAST_LOGITS_PREFIX))
# For the contrastive loss, I need a projected version of this feature
outputs['future_projected'] = self.project_mlp(feats_agg)
# Aggregate again, if asked for
feats_future_agg, future_agg_losses = (
self.temporal_aggregator_after_future_pred(feats_future))
aux_losses.update(future_agg_losses)
outputs['future_agg'] = feats_future_agg
feats_future_agg_drop = self.dropout(feats_future_agg)
outputs.update(self._apply_classifier(feats_future_agg_drop))
if self.regression_head:
outputs['logits_regression'] = self.regression_head(
feats_future_agg_drop)
return outputs, aux_losses
def _apply_classifier(self, input_feat, outputs_prefix=''):
outputs = {}
for key in self.num_classes.keys():
if key in self.classifiers:
outputs[f'{outputs_prefix}logits/{key}'] = self.classifiers[
key](input_feat)
else:
# A mapping must exist, in order to compute this, and must
# have been computed already (so ordering in the config
# matters)
src_key = next(iter(self.classifiers.keys()))
src_tensor = outputs[f'{outputs_prefix}logits/{src_key}']
mapper = operator.attrgetter(
f'{CLS_MAP_PREFIX}{key}_{src_key}')(self)
outputs[f'{outputs_prefix}logits/{key}'] = torch.mm(
src_tensor, mapper)
return outputs
def forward(self, video, *args, **kwargs):
"""
Args: video (torch.Tensor)
Could be (B, #clips, C, T, H, W) or
(B, #clips, #crops, C, T, H, W)
Returns:
Final features
And any auxiliarly losses produced by the model
"""
if video.ndim == 6:
video_crops = [video]
elif video.ndim == 7 and video.size(2) == 1:
video_crops = [video.squeeze(2)]
elif video.ndim == 7:
video_crops = torch.unbind(video, dim=2)
else:
raise NotImplementedError('Unsupported size %s' % video.shape)
feats_losses = [
self.forward_singlecrop(el, *args, **kwargs) for el in video_crops
]
feats, losses = zip(*feats_losses)
# Convert to dict of lists
feats = {k: [dic[k] for dic in feats] for k in feats[0]}
losses = {k: [dic[k] for dic in losses] for k in losses[0]}
# Average over the crops
feats = {
k: torch.mean(torch.stack(el, dim=0), dim=0)
for k, el in feats.items()
}
losses = {
k: torch.mean(torch.stack(el, dim=0), dim=0)
for k, el in losses.items()
}
return feats, losses
|
AVT-main
|
models/base_model.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
import torch.nn as nn
class MLP(nn.Module):
def __init__(self, in_features, out_features, nlayers, **kwargs):
super().__init__()
layers = [[nn.Linear(in_features, in_features, **kwargs),
nn.ReLU()] for _ in range(nlayers - 1)]
# flatten out the pairs
layers = [item for sublist in layers for item in sublist]
layers.append(nn.Linear(in_features, out_features))
self.cls = nn.Sequential(*layers)
def forward(self, inp):
return self.cls(inp)
|
AVT-main
|
models/classifiers.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
import torch
import numbers
import random
from torchvision.transforms import (
RandomCrop,
RandomResizedCrop,
ColorJitter,
ToPILImage,
ToTensor,
)
__all__ = [
"RandomCropVideo",
"RandomResizedCropVideo",
"CenterCropVideo",
"NormalizeVideo",
"ToTensorVideo",
"RandomHorizontalFlipVideo",
"Resize",
"TemporalCenterCrop",
"ColorJitterVideo",
]
def _is_tensor_video_clip(clip):
if not torch.is_tensor(clip):
raise TypeError("clip should be Tensor. Got %s" % type(clip))
if not clip.ndimension() == 4:
raise ValueError("clip should be 4D. Got %dD" % clip.dim())
return True
def crop(clip, i, j, h, w):
"""
Args:
clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
"""
assert len(clip.size()) == 4, "clip should be a 4D tensor"
return clip[..., i:i + h, j:j + w]
def temporal_center_crop(clip, clip_len):
"""
Args:
clip (torch.tensor): Video clip to be
cropped along the temporal axis. Size is (C, T, H, W)
"""
assert len(clip.size()) == 4, "clip should be a 4D tensor"
assert clip.size(1) >= clip_len, "clip is shorter than the proposed lenght"
middle = int(clip.size(1) // 2)
start = middle - clip_len // 2
return clip[:, start:start + clip_len, ...]
def resize(clip, target_size, interpolation_mode):
"""
Args:
target_size can be a
integer: Which is the length of the smaller side
string: with format <min>-<max>: will randomly pick a size from
min and max (included) to be the smaller side
or tuple of either integers and/or string
"""
def _convert_size_to_integer(size_str):
if isinstance(size_str, int):
return size_str
size_min, size_max = [int(el) for el in size_str.split('-')]
return random.randint(size_min, size_max)
if isinstance(target_size, (list, tuple)):
target_size = (_convert_size_to_integer(target_size[0]),
_convert_size_to_integer(target_size[1]))
else:
target_size = _convert_size_to_integer(target_size)
if isinstance(target_size, int):
clip_h, clip_w = clip.shape[-2], clip.shape[-1]
scale_factor = target_size * 1.0 / min(clip_h, clip_w)
# Make sure the new sizes def follow the target_size, sometimes the
# ratio leads to a couple pixel below which can lead to errors
new_h = max(int(clip_h * scale_factor), target_size)
new_w = max(int(clip_w * scale_factor), target_size)
target_size = (new_h, new_w)
return torch.nn.functional.interpolate(clip,
size=target_size,
mode=interpolation_mode)
def resized_crop(clip, i, j, h, w, size, interpolation_mode="bilinear"):
"""
Do spatial cropping and resizing to the video clip
Args:
clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
i (int): i in (i,j) i.e coordinates of the upper left corner.
j (int): j in (i,j) i.e coordinates of the upper left corner.
h (int): Height of the cropped region.
w (int): Width of the cropped region.
size (tuple(int, int)): height and width of resized clip
Returns:
clip (torch.tensor): Resized and cropped clip. Size is (C, T, H, W)
"""
assert _is_tensor_video_clip(clip), "clip should be a 4D torch.tensor"
clip = crop(clip, i, j, h, w)
clip = resize(clip, size, interpolation_mode)
return clip
def center_crop(clip, crop_size):
assert _is_tensor_video_clip(clip), "clip should be a 4D torch.tensor"
h, w = clip.size(-2), clip.size(-1)
th, tw = crop_size
assert h >= th and w >= tw, "height and width must be >= than crop_size"
i = int(round((h - th) / 2.0))
j = int(round((w - tw) / 2.0))
return crop(clip, i, j, th, tw)
def to_tensor(clip):
"""
Convert tensor data type from uint8 to float, divide value by 255.0 and
permute the dimenions of clip tensor
Args:
clip (torch.tensor, dtype=torch.uint8): Size is (T, H, W, C)
Return:
clip (torch.tensor, dtype=torch.float): Size is (C, T, H, W)
"""
try:
_is_tensor_video_clip(clip)
except (TypeError, ValueError):
# Needed to add this since this happens when using Miao's transforms
clip = torch.as_tensor(clip)
if not clip.dtype == torch.uint8:
raise TypeError("clip tensor should have data type uint8. Got %s" %
str(clip.dtype))
return clip.float().permute(3, 0, 1, 2) / 255.0
def normalize(clip, mean, std, inplace=False):
"""
Args:
clip (torch.tensor): Video clip to be normalized. Size is (C, T, H, W)
mean (tuple): pixel RGB mean. Size is (3)
std (tuple): pixel standard deviation. Size is (3)
Returns:
normalized clip (torch.tensor): Size is (C, T, H, W)
"""
assert _is_tensor_video_clip(clip), "clip should be a 4D torch.tensor"
if not inplace:
clip = clip.clone()
mean = torch.as_tensor(mean, dtype=clip.dtype, device=clip.device)
std = torch.as_tensor(std, dtype=clip.dtype, device=clip.device)
clip.sub_(mean[:, None, None, None]).div_(std[:, None, None, None])
return clip
def hflip(clip):
"""
Args:
clip (torch.tensor): Video clip to be normalized. Size is (C, T, H, W)
Returns:
flipped clip (torch.tensor): Size is (C, T, H, W)
"""
assert _is_tensor_video_clip(clip), "clip should be a 4D torch.tensor"
return clip.flip((-1))
class RandomCropVideo(RandomCrop):
def __init__(self, size):
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
else:
self.size = size
def __call__(self, clip):
"""
Args:
clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
Returns:
torch.tensor: randomly cropped/resized video clip.
size is (C, T, OH, OW)
"""
i, j, h, w = self.get_params(clip, self.size)
return crop(clip, i, j, h, w)
def __repr__(self):
return self.__class__.__name__ + '(size={0})'.format(self.size)
class RandomResizedCropVideo(RandomResizedCrop):
def __init__(
self,
size,
scale=(0.08, 1.0),
ratio=(3.0 / 4.0, 4.0 / 3.0),
interpolation_mode="bilinear",
):
if isinstance(size, tuple):
assert len(size) == 2, "size should be tuple (height, width)"
self.size = size
else:
self.size = (size, size)
self.interpolation_mode = interpolation_mode
self.scale = scale
self.ratio = ratio
def __call__(self, clip):
"""
Args:
clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
Returns:
torch.tensor: randomly cropped/resized video clip.
size is (C, T, H, W)
"""
i, j, h, w = self.get_params(clip, self.scale, self.ratio)
return resized_crop(clip, i, j, h, w, self.size,
self.interpolation_mode)
def __repr__(self):
return self.__class__.__name__ + \
'(size={0}, interpolation_mode={1}, scale={2}, ratio={3})'.format(
self.size, self.interpolation_mode, self.scale, self.ratio
)
class CenterCropVideo(object):
def __init__(self, crop_size):
if isinstance(crop_size, numbers.Number):
self.crop_size = (int(crop_size), int(crop_size))
else:
self.crop_size = crop_size
def __call__(self, clip):
"""
Args:
clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
Returns:
torch.tensor: central cropping of video clip. Size is
(C, T, crop_size, crop_size)
"""
return center_crop(clip, self.crop_size)
def __repr__(self):
r = self.__class__.__name__ + '(crop_size={0})'.format(self.crop_size)
return r
def multi_crop(video, crop_size, num_crops, flips):
"""
Returns a list of video crops of crop_size, num_crops * 2 in length
(including flipped versions)
"""
assert _is_tensor_video_clip(video), "clip should be a 4D torch.tensor"
h, w = video.size(-2), video.size(-1)
th, tw = crop_size
assert h >= th and w >= tw, "height and width must be >= than crop_size"
if num_crops == 1:
# Center crop, as used in the CenterCrop function
pos = [(int(round((h - th) / 2.0)), int(round((w - tw) / 2.0)))]
elif num_crops == 3:
# top left, center, and bottom right
pos = [(0, 0), (int(round((h - th) / 2.0)), int(round(
(w - tw) / 2.0))), (h - th, w - tw)]
else:
raise NotImplementedError('Not supported')
cropped = [crop(video, i, j, th, tw) for i, j in pos]
if flips:
cropped += [hflip(el) for el in cropped]
return cropped
class MultiCropVideo(object):
def __init__(self, crop_size, num_crops, flips=False):
if isinstance(crop_size, numbers.Number):
self.crop_size = (int(crop_size), int(crop_size))
else:
self.crop_size = crop_size
self.num_crops = num_crops
self.flips = flips
def __call__(self, clip):
"""
Args:
clip (torch.tensor): Video clip to be cropped. Size is (C, T, H, W)
Returns:
torch.tensor: central cropping of video clip. Size is
(num_crops, C, T, crop_size, crop_size)
"""
return torch.stack(
multi_crop(clip, self.crop_size, self.num_crops, self.flips), 0)
def __repr__(self):
return (self.__class__.__name__ +
f'(crop_size={self.crop_size},num_crops={self.num_crops})')
class TemporalCenterCrop(object):
def __init__(self, clip_len):
self.clip_len = clip_len
def __call__(self, clip):
return temporal_center_crop(clip, self.clip_len)
class UnfoldClips(object):
def __init__(self, clip_len, overlap):
self.clip_len = clip_len
assert overlap > 0 and overlap <= 1
self.step = round(clip_len * overlap)
def __call__(self, clip):
if clip.size(1) < self.clip_len:
return clip.unfold(1, clip.size(1),
clip.size(1)).permute(1, 0, 4, 2, 3)
results = clip.unfold(1, self.clip_len,
self.clip_len).permute(1, 0, 4, 2, 3)
return results
class NormalizeVideo(object):
"""
Normalize the video clip by mean subtraction
and division by standard deviation
Args:
mean (3-tuple): pixel RGB mean
std (3-tuple): pixel RGB standard deviation
inplace (boolean): whether do in-place normalization
"""
def __init__(self, mean, std, inplace=False):
self.mean = mean
self.std = std
self.inplace = inplace
def __call__(self, clip):
"""
Args:
clip (torch.tensor): video clip to be
normalized. Size is (C, T, H, W)
"""
return normalize(clip, self.mean, self.std, self.inplace)
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1}, inplace={2})'.format(
self.mean, self.std, self.inplace)
class ToTensorVideo(object):
"""
Convert tensor data type from uint8 to float, divide value by 255.0 and
permute the dimenions of clip tensor
"""
def __init__(self):
pass
def __call__(self, clip):
"""
Args:
clip (torch.tensor, dtype=torch.uint8): Size is (T, H, W, C)
Return:
clip (torch.tensor, dtype=torch.float): Size is (C, T, H, W)
"""
return to_tensor(clip)
def __repr__(self):
return self.__class__.__name__
class RandomHorizontalFlipVideo(object):
"""
Flip the video clip along the horizonal direction with a given probability
Args:
p (float): probability of the clip being flipped. Default value is 0.5
"""
def __init__(self, p=0.5):
self.p = p
def __call__(self, clip):
"""
Args:
clip (torch.tensor): Size is (C, T, H, W)
Return:
clip (torch.tensor): Size is (C, T, H, W)
"""
if random.random() < self.p:
clip = hflip(clip)
return clip
def __repr__(self):
return self.__class__.__name__ + "(p={0})".format(self.p)
class ColorJitterVideo():
"""
Randomly add color jitter to video
Args:
Same as original ColorJitter
"""
def __init__(self, *args, **kwargs):
self.frame_color_jitter = ColorJitter(*args, **kwargs)
def __call__(self, clip):
"""
Args:
clip (torch.tensor): Size is (C, T, H, W)
Return:
clip (torch.tensor): Size is (C, T, H, W)
"""
assert _is_tensor_video_clip(clip), "clip should be a 4D torch.tensor"
# Stack the frames on height dimension
stacked_frames = clip.view((clip.size(0), -1, clip.size(-1)))
stacked_frames_pil = ToPILImage()(stacked_frames)
output_stacked_frames = ToTensor()(
self.frame_color_jitter(stacked_frames_pil))
return output_stacked_frames.view(clip.shape)
class Resize(object):
def __init__(self, size):
self.size = size
def __call__(self, vid):
return resize(vid, self.size, interpolation_mode="bilinear")
|
AVT-main
|
common/transforms.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
from collections import defaultdict, deque
import datetime
import time
import logging
import torch
import torch.distributed as dist
from common.utils import is_dist_avail_and_initialized, is_main_process
__all__ = [
'SmoothedValue', 'MetricLogger', 'get_default_loggers',
'get_default_loggers'
]
EPS = 0.000001
class SmoothedValue(object):
"""Track a series of values and provide access to smoothed values over a
window or the global series average.
"""
def __init__(self, window_size=20, fmt=None):
if fmt is None:
fmt = "{median:.4f} ({global_avg:.4f})"
self.deque = deque(maxlen=window_size)
self.total = 0.0
self.count = 0
self.fmt = fmt
self.ws = window_size
def reset(self):
self.__init__(window_size=self.ws, fmt=self.fmt)
def update(self, value, n=1):
self.deque.append(value)
self.count += n
self.total += value * n
def synchronize_between_processes(self):
"""
Warning: does not synchronize the deque!
"""
if not is_dist_avail_and_initialized():
return
t = torch.tensor([self.count, self.total],
dtype=torch.float64,
device='cuda')
dist.barrier()
dist.all_reduce(t)
t = t.tolist()
self.count = int(t[0])
self.total = t[1]
@property
def median(self):
d = torch.tensor(list(self.deque))
return d.median().item()
@property
def avg(self):
d = torch.tensor(list(self.deque), dtype=torch.float32)
return d.mean().item()
@property
def global_avg(self):
return self.total / (self.count + EPS)
@property
def max(self):
return max(self.deque)
@property
def value(self):
return self.deque[-1]
def __str__(self):
return self.fmt.format(median=self.median,
avg=self.avg,
global_avg=self.global_avg,
max=self.max,
value=self.value)
class MetricLogger(object):
def __init__(self,
delimiter="\t",
writer=None,
stat_set="train",
epoch=0,
logger=None):
self.meters = defaultdict(SmoothedValue)
self.delimiter = delimiter
self.metric_set = stat_set
self.epoch = epoch
self.logger = logger.info if logger is not None else logging.info
self.writer = writer
self.writer_step = 0
# Adding all logs from this to raw/ header, so I can plot other metrics
# cleanly
self.tbd_header = 'metric_logger/'
self.meters["iter_time"] = SmoothedValue(fmt='{avg:.4f}')
self.meters["data_time"] = SmoothedValue(fmt='{avg:.4f}')
def update(self, **kwargs):
for k, v in kwargs.items():
if isinstance(v, torch.Tensor):
v = v.item()
assert isinstance(v, (float, int))
self.meters[k].update(v)
def __getattr__(self, attr):
if attr in self.meters:
return self.meters[attr]
if attr in self.__dict__:
return self.__dict__[attr]
raise AttributeError("'{}' object has no attribute '{}'".format(
type(self).__name__, attr))
def __str__(self):
loss_str = []
for name, meter in self.meters.items():
loss_str.append("{}: {}".format(name, str(meter)))
return self.delimiter.join(loss_str)
def synchronize_between_processes(self):
for meter in self.meters.values():
meter.synchronize_between_processes()
def add_meter(self, name, meter):
self.meters[name] = meter
def reset_meters(self):
self.logger("Logging: resseting all meters")
for name, meter in self.meters.items():
meter.reset()
self.logger(
"Logging: resseting all meters done, updating epoch to %d".format(
self.epoch + 1))
self.epoch += 1
def log_every(self, iterable, print_freq, header=None):
i = 0
if not header:
header = ''
start_time = time.time()
end = time.time()
space_fmt = ':' + str(len(str(len(iterable)))) + 'd'
if torch.cuda.is_available():
log_msg = self.delimiter.join([
header, '[{0' + space_fmt + '}/{1}]', 'eta: {eta}', '{meters}',
'time: {time}', 'data: {data}', 'max mem: {memory:.0f}'
])
else:
log_msg = self.delimiter.join([
header, '[{0' + space_fmt + '}/{1}]', 'eta: {eta}', '{meters}',
'time: {time}', 'data: {data}'
])
MB = 1024.0 * 1024.0
for obj in iterable:
self.meters["data_time"].update(time.time() - end)
yield obj
self.meters["iter_time"].update(time.time() - end)
if i % print_freq == 0:
self._write_meters()
eta_seconds = self.meters["iter_time"].global_avg * (
len(iterable) - i)
eta_string = str(datetime.timedelta(seconds=int(eta_seconds)))
if torch.cuda.is_available():
self.logger(
log_msg.format(
i,
len(iterable),
eta=eta_string,
meters=str(self),
time=str(self.meters["iter_time"]),
data=str(self.meters["data_time"]),
memory=torch.cuda.max_memory_allocated() / MB))
else:
self.logger(
log_msg.format(i,
len(iterable),
eta=eta_string,
meters=str(self),
time=str(self.meters["iter_time"]),
data=str(self.meters["data_time"])))
i += 1
end = time.time()
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
self.logger('{} Total time: {}'.format(header, total_time_str))
self._write_epoch(total_time_str)
def _write_meters(self):
if self.writer is not None:
for name, meter in self.meters.items():
self.writer.add_scalar(
f"{self.tbd_header}iter/{self.metric_set}_{name}",
meter.avg, self.writer_step)
self.writer_step += 1
def _write_epoch(self, total_time_string):
if self.writer is not None:
for name, meter in self.meters.items():
self.writer.add_scalar(
f"{self.tbd_header}epoch/{self.metric_set}_{name}",
meter.avg, self.epoch)
self.writer.add_text(
f"{self.tbd_header}epoch/{self.metric_set}_totaltime",
total_time_string, self.epoch)
def setup_tbx(save_dir, SummaryWriter):
if not is_main_process():
return None
writer = SummaryWriter(save_dir)
return writer
def get_default_loggers(writer, epoch, logger):
stat_loggers = dict()
stat_loggers["train"] = MetricLogger(delimiter=" ",
writer=writer,
stat_set="train",
epoch=epoch,
logger=logger)
stat_loggers["train"].add_meter(
'lr', SmoothedValue(window_size=1, fmt='{value}'))
stat_loggers["train"].add_meter(
'clips/s', SmoothedValue(window_size=10, fmt='{value:.3f}'))
stat_loggers["val"] = MetricLogger(delimiter=" ",
writer=writer,
stat_set="val",
epoch=epoch,
logger=logger)
return stat_loggers
|
AVT-main
|
common/log.py
|
from .log import *
|
AVT-main
|
common/__init__.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
from __future__ import print_function
from typing import List, Dict
import errno
import os
from pathlib import Path
import logging
import submitit
import cv2
import torch
import torch.distributed as dist
def accuracy(output, target, topk=(1, )):
"""Computes the accuracy over the k top predictions
for the specified values of k
Args:
output (*, K) predictions
target (*, ) targets
"""
if torch.all(target < 0):
return [
torch.zeros([], device=output.device) for _ in range(len(topk))
]
with torch.no_grad():
# flatten the initial dimensions, to deal with 3D+ input
output = output.flatten(0, -2)
target = target.flatten()
# Now compute the accuracy
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target[None])
res = []
for k in topk:
correct_k = correct[:k].flatten().sum(dtype=torch.float32)
res.append(correct_k * (100.0 / batch_size))
return res
def mkdir(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def setup_for_distributed(is_master, logger):
"""
This function disables printing when not in master process
"""
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
force = kwargs.pop('force', False)
if is_master or force:
builtin_print(*args, **kwargs)
__builtin__.print = print
if not is_master:
# Don't print anything except FATAL
logger.setLevel(logging.ERROR)
logging.basicConfig(level=logging.ERROR)
else:
logger.setLevel(logging.INFO)
logging.basicConfig(level=logging.INFO)
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_world_size():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_rank():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def is_main_process():
return get_rank() == 0
def save_on_master(*args, **kwargs):
if is_main_process():
torch.save(*args, **kwargs)
def init_distributed_mode(logger, dist_backend='nccl'):
dist_info = dict(
distributed=False,
rank=0,
world_size=1,
gpu=0,
dist_backend=dist_backend,
dist_url=get_init_file(None).as_uri(),
)
# If launched using submitit, get the job_env and set using those
try:
job_env = submitit.JobEnvironment()
except RuntimeError:
job_env = None
if job_env is not None:
dist_info['rank'] = job_env.global_rank
dist_info['world_size'] = job_env.num_tasks
dist_info['gpu'] = job_env.local_rank
if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
dist_info['rank'] = int(os.environ["RANK"])
dist_info['world_size'] = int(os.environ['WORLD_SIZE'])
dist_info['gpu'] = int(os.environ['LOCAL_RANK'])
elif 'SLURM_PROCID' in os.environ:
dist_info['rank'] = int(os.environ['SLURM_PROCID'])
dist_info['gpu'] = dist_info['rank'] % torch.cuda.device_count()
elif 'rank' in dist_info:
pass
else:
print('Not using distributed mode')
dist_info['distributed'] = False
return dist_info
dist_info['distributed'] = True
torch.cuda.set_device(dist_info['gpu'])
dist_info['dist_backend'] = dist_backend
print('| distributed init (rank {}): {}'.format(dist_info['rank'],
dist_info['dist_url']),
flush=True)
torch.distributed.init_process_group(backend=dist_info['dist_backend'],
init_method=dist_info['dist_url'],
world_size=dist_info['world_size'],
rank=dist_info['rank'])
setup_for_distributed(dist_info['rank'] == 0, logger)
return dist_info
def get_shared_folder(name) -> Path:
# Since using hydra, which figures the out folder
return Path('./').absolute()
def get_init_file(name):
# Init file must not exist, but it's parent dir must exist.
os.makedirs(str(get_shared_folder(name)), exist_ok=True)
init_file = get_shared_folder(name) / 'sync_file_init'
return init_file
def gather_tensors_from_all(tensor: torch.Tensor) -> List[torch.Tensor]:
"""
Wrapper over torch.distributed.all_gather for performing
'gather' of 'tensor' over all processes in both distributed /
non-distributed scenarios.
"""
if tensor.ndim == 0:
# 0 dim tensors cannot be gathered. so unsqueeze
tensor = tensor.unsqueeze(0)
if is_dist_avail_and_initialized():
gathered_tensors = [
torch.zeros_like(tensor)
for _ in range(torch.distributed.get_world_size())
]
torch.distributed.all_gather(gathered_tensors, tensor)
else:
gathered_tensors = [tensor]
return gathered_tensors
def gather_from_all(tensor: torch.Tensor) -> torch.Tensor:
gathered_tensors = gather_tensors_from_all(tensor)
gathered_tensor = torch.cat(gathered_tensors, 0)
return gathered_tensor
def get_video_info(video_path: Path, props: List[str]) -> Dict[str, float]:
"""
Given the video, return the properties asked for
"""
output = {}
cam = cv2.VideoCapture(str(video_path))
if 'fps' in props:
output['fps'] = cam.get(cv2.CAP_PROP_FPS)
if 'len' in props:
fps = cam.get(cv2.CAP_PROP_FPS)
if fps <= 0:
output['len'] = 0
else:
output['len'] = (cam.get(cv2.CAP_PROP_FRAME_COUNT) / fps)
cam.release()
return output
|
AVT-main
|
common/utils.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
import torch
import torch.nn as nn
class KmeansAssigner(nn.Module):
def __init__(self, centroids_fpath, norm=False):
super().__init__()
# NxC dimension
# Not converting this to linear layer as then the weights get
# overwriten during random init, and these cluster centers are lost.
self.register_buffer('centroids',
torch.load(centroids_fpath)['weight'])
self.norm = norm
@property
def num_clusters(self):
return self.centroids.size(0)
@staticmethod
def feat2cluster(feats, centroids, norm):
"""
Compute index for the feats, w.r.t centroids.
Args:
feats *xC
centroids KxC
Returns:
assignments *
"""
feats_flat = feats.flatten(0, -2)
if norm:
feats_flat = nn.functional.normalize(feats_flat, dim=-1, p=2)
dists = torch.cdist(feats_flat.unsqueeze(0), centroids.unsqueeze(0))
assgns = torch.argmin(dists[0], dim=-1)
assgns = assgns.reshape(feats.shape[:-1])
return assgns
@staticmethod
def cluster2feat(idx, centroids):
"""
Get features for cluster ids
Args:
idx *
centroids KxC
Returns:
assignments *xC
"""
idx_flat = idx.reshape((-1, ))
feats = centroids[idx_flat, :]
return feats.reshape(list(idx.shape) + [feats.size(-1)])
def forward(self, inp):
"""
If inp is torch.float, then find the nearest assignments.
If torch.long, return the corresponding features.
"""
if inp.dtype == torch.long:
return self.cluster2feat(inp, self.centroids)
return self.feat2cluster(inp, self.centroids, self.norm)
|
AVT-main
|
common/cluster.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
from typing import Sequence
import torch
from bisect import bisect_right
class WarmupMultiStepLR(torch.optim.lr_scheduler._LRScheduler):
def __init__(
self,
optimizer: torch.optim.Optimizer,
milestone_epochs: Sequence[int],
gamma: float = 0.1,
warmup_factor: float = 1.0 / 3,
warmup_epochs: int = 5,
warmup_method: str = 'linear',
last_epoch: int = -1,
iters_per_epoch: int = None, # Must be set by calling code
world_size: int = None,
):
del world_size
if not milestone_epochs == sorted(milestone_epochs):
raise ValueError(
"Milestones should be a list of"
" increasing integers. Got {}",
milestone_epochs,
)
if warmup_method not in ("constant", "linear"):
raise ValueError(
"Only 'constant' or 'linear' warmup_method accepted"
"got {}".format(warmup_method))
self.milestones = [iters_per_epoch * m for m in milestone_epochs]
self.gamma = gamma
self.warmup_factor = warmup_factor
self.warmup_iters = max(warmup_epochs * iters_per_epoch, 1)
self.warmup_method = warmup_method
super(WarmupMultiStepLR, self).__init__(optimizer, last_epoch)
def get_lr(self):
warmup_factor = 1
if self.last_epoch < self.warmup_iters:
if self.warmup_method == "constant":
warmup_factor = self.warmup_factor
elif self.warmup_method == "linear":
alpha = float(self.last_epoch) / self.warmup_iters
warmup_factor = self.warmup_factor * (1 - alpha) + alpha
return [
base_lr * warmup_factor *
self.gamma**bisect_right(self.milestones, self.last_epoch)
for base_lr in self.base_lrs
]
class CosineLR(torch.optim.lr_scheduler.CosineAnnealingLR):
def __init__(self,
optimizer,
num_epochs,
iters_per_epoch=None,
world_size=None,
**kwargs):
kwargs['eta_min'] *= world_size
super().__init__(optimizer,
T_max=num_epochs * iters_per_epoch,
**kwargs)
def get_lr(self, *args, **kwargs):
if self.last_epoch < self.T_max:
return super().get_lr(*args, **kwargs)
else:
# Adding this if I train the model longer than the T_max set in
# this. Happens when I sweep over different amounts of warmup.
return [0.0 for _ in self.optimizer.param_groups]
class ReduceLROnPlateau(torch.optim.lr_scheduler.ReduceLROnPlateau):
def __init__(self,
optimizer,
iters_per_epoch=None,
world_size=None,
**kwargs):
del iters_per_epoch, world_size
super().__init__(optimizer, **kwargs)
class Warmup(torch.optim.lr_scheduler._LRScheduler):
"""Wrap the scheduler for warmup before it kicks in."""
def __init__(
self,
optimizer: torch.optim.Optimizer,
scheduler: torch.optim.lr_scheduler._LRScheduler,
init_lr_ratio: float = 0.0,
num_epochs: int = 5,
last_epoch: int = -1,
iters_per_epoch: int = None, # Must be set by calling code
world_size: int = None,
):
"""
Args:
init_lr_ratio (float in [0, 1]): Ratio of the original LR to start
from. If 0.1, it will start from 0.1 of the original LRs and go
upto 1.0 of the original LRs in the epochs. By def start from
0 up.
num_epochs (int): Num of epochs to take to warmup.
last_epoch (int): Which was the last epoch to init from (not really
used anymore since we store the state_dict when loading
scheduler from disk.)
"""
del world_size
self.base_scheduler = scheduler
self.warmup_iters = max(num_epochs * iters_per_epoch, 1)
if self.warmup_iters > 1:
self.init_lr_ratio = init_lr_ratio
else:
self.init_lr_ratio = 1.0 # Don't go from 0 to 1 in 1 iteration
super().__init__(optimizer, last_epoch)
def get_lr(self):
# Epoch is iters for me, since I step after each iteration
# (not after each epoch)
# Based on logic in step, this should only be called for the warmup
# iters. After that it should go to the base scheduler
assert self.last_epoch < self.warmup_iters # since it increments
return [
el * (self.init_lr_ratio + (1 - self.init_lr_ratio) *
(float(self.last_epoch) / self.warmup_iters))
for el in self.base_lrs
]
def step(self, *args, **kwargs):
if self.last_epoch < (self.warmup_iters - 1):
super().step(*args, **kwargs)
else:
self.base_scheduler.step(*args, **kwargs)
def state_dict(self):
"""Returns the state of the scheduler as a :class:`dict`.
It contains an entry for every variable in self.__dict__ which
is not the optimizer.
"""
base_sched_dict = self.base_scheduler.state_dict()
other_stuff = {
key: value
for key, value in self.__dict__.items() if key not in [
'base_scheduler', 'optimizer']
}
return {'base_sched_dict': base_sched_dict, 'other_stuff': other_stuff}
def load_state_dict(self, state_dict):
"""Loads the schedulers state.
Arguments:
state_dict (dict): scheduler state. Should be an object returned
from a call to :meth:`state_dict`.
"""
self.base_scheduler.__dict__.update(state_dict['base_sched_dict'])
self.__dict__.update(state_dict['other_stuff'])
|
AVT-main
|
common/scheduler.py
|
# Copyright (c) Facebook, Inc. and its affiliates.
import math
import torch
from torch.utils.data import Sampler
import torch.distributed as dist
import torchvision.datasets.video_utils
class DistributedSampler(Sampler):
"""
Extension of DistributedSampler, as discussed in
https://github.com/pytorch/pytorch/issues/23430
"""
def __init__(self, dataset, num_replicas=None, rank=None, shuffle=False):
if num_replicas is None:
if not dist.is_available():
raise RuntimeError("Requires distributed package to be available")
num_replicas = dist.get_world_size()
if rank is None:
if not dist.is_available():
raise RuntimeError("Requires distributed package to be available")
rank = dist.get_rank()
self.dataset = dataset
self.num_replicas = num_replicas
self.rank = rank
self.epoch = 0
self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas))
self.total_size = self.num_samples * self.num_replicas
self.shuffle = shuffle
def __iter__(self):
# deterministically shuffle based on epoch
g = torch.Generator()
g.manual_seed(self.epoch)
if self.shuffle:
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = list(range(len(self.dataset)))
# add extra samples to make it evenly divisible
indices += indices[:(self.total_size - len(indices))]
assert len(indices) == self.total_size
# subsample
indices = indices[self.rank:self.total_size:self.num_replicas]
assert len(indices) == self.num_samples
if isinstance(self.dataset, Sampler):
orig_indices = list(iter(self.dataset))
indices = [orig_indices[i] for i in indices]
return iter(indices)
def __len__(self):
return self.num_samples
def set_epoch(self, epoch):
self.epoch = epoch
class UniformClipSampler(torch.utils.data.Sampler):
"""
Samples at most `max_video_clips_per_video` clips for each video, equally spaced
Arguments:
video_clips (VideoClips): video clips to sample from
max_clips_per_video (int): maximum number of clips to be sampled per video
"""
def __init__(self, video_clips, max_clips_per_video):
if not isinstance(video_clips, torchvision.datasets.video_utils.VideoClips):
raise TypeError("Expected video_clips to be an instance of VideoClips, "
"got {}".format(type(video_clips)))
self.video_clips = video_clips
self.max_clips_per_video = max_clips_per_video
def __iter__(self):
idxs = []
s = 0
# select at most max_clips_per_video for each video, uniformly spaced
for c in self.video_clips.clips:
length = len(c)
step = max(length // self.max_clips_per_video, 1)
sampled = torch.arange(length)[::step] + s
s += length
idxs.append(sampled)
idxs = torch.cat(idxs).tolist()
return iter(idxs)
def __len__(self):
return sum(min(len(c), self.max_clips_per_video) for c in self.video_clips.clips)
class RandomClipSampler(torch.utils.data.Sampler):
"""
Samples at most `max_video_clips_per_video` clips for each video randomly
Arguments:
video_clips (VideoClips): video clips to sample from
max_clips_per_video (int): maximum number of clips to be sampled per video
"""
def __init__(self, video_clips, max_clips_per_video):
if not isinstance(video_clips, torchvision.datasets.video_utils.VideoClips):
raise TypeError("Expected video_clips to be an instance of VideoClips, "
"got {}".format(type(video_clips)))
self.video_clips = video_clips
self.max_clips_per_video = max_clips_per_video
def __iter__(self):
idxs = []
s = 0
# select at most max_clips_per_video for each video, randomly
for c in self.video_clips.clips:
length = len(c)
size = min(length, self.max_clips_per_video)
sampled = torch.randperm(length)[:size] + s
s += length
idxs.append(sampled)
idxs = torch.cat(idxs)
# shuffle all clips randomly
perm = torch.randperm(len(idxs))
idxs = idxs[perm].tolist()
return iter(idxs)
def __len__(self):
return sum(min(len(c), self.max_clips_per_video) for c in self.video_clips.clips)
|
AVT-main
|
common/sampler.py
|
AVT-main
|
external/__init__.py
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.