Changement mesure memoire + Tests solarize differentiable

This commit is contained in:
Harle, Antoine (Contracteur) 2020-02-10 14:36:12 -05:00
parent 6277e268c1
commit 7d5aa7c6fb
4 changed files with 102 additions and 66 deletions

View file

@ -83,77 +83,88 @@ if __name__ == "__main__":
### Benchmark ### ### Benchmark ###
''' '''
n_inner_iter = 1
dist_mix = [0.5]#[0.5, 1.0]
N_seq_TF= [3, 4]
mag_setup = [(True, True), (False, False)] #(FxSh, Independant)
for model_type in model_list.keys(): for model_type in model_list.keys():
for model_name in model_list[model_type]: for model_name in model_list[model_type]:
for run in range(nb_run): for run in range(nb_run):
torch.cuda.reset_max_memory_cached() #reset_peak_stats for n_tf in N_seq_TF:
t0 = time.perf_counter() for dist in dist_mix:
for m_setup in mag_setup:
model = getattr(model_type, model_name)(pretrained=False) torch.cuda.reset_max_memory_allocated() #reset_peak_stats
t0 = time.perf_counter()
model = Higher_model(model, model_name) #run_dist_dataugV3 model = getattr(model_type, model_name)(pretrained=False)
if n_inner_iter!=0:
aug_model = Augmented_model(
Data_augV5(TF_dict=tf_dict,
N_TF=n_tf,
mix_dist=dist,
fixed_prob=p_setup,
fixed_mag=m_setup[0],
shared_mag=m_setup[1]),
model).to(device)
else:
aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=n_tf), model).to(device)
print("{} on {} for {} epochs - {} inner_it".format(str(aug_model), device_name, epochs, n_inner_iter)) model = Higher_model(model, model_name) #run_dist_dataugV3
log= run_dist_dataugV3(model=aug_model, if n_inner_iter!=0:
epochs=epochs, aug_model = Augmented_model(
inner_it=n_inner_iter, Data_augV5(TF_dict=tf_dict,
dataug_epoch_start=dataug_epoch_start, N_TF=n_tf,
opt_param=optim_param, mix_dist=dist,
print_freq=epochs/4, fixed_prob=False,
unsup_loss=1, fixed_mag=m_setup[0],
hp_opt=False, shared_mag=m_setup[1]),
save_sample_freq=None) model).to(device)
else:
aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=n_tf), model).to(device)
exec_time=time.perf_counter() - t0 print("{} on {} for {} epochs - {} inner_it".format(str(aug_model), device_name, epochs, n_inner_iter))
max_cached = torch.cuda.max_memory_cached()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB log= run_dist_dataugV3(model=aug_model,
#### epochs=epochs,
print('-'*9) inner_it=n_inner_iter,
times = [x["time"] for x in log] dataug_epoch_start=dataug_epoch_start,
out = {"Accuracy": max([x["acc"] for x in log]), opt_param=optim_param,
"Time": (np.mean(times),np.std(times), exec_time), print_freq=epochs/4,
'Optimizer': optim_param, unsup_loss=1,
"Device": device_name, hp_opt=False,
"Memory": max_cached, save_sample_freq=None)
"Param_names": aug_model.TF_names(),
"Log": log}
print(str(aug_model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
filename = "{}-{} epochs (dataug:{})- {} in_it-{}".format(str(aug_model),epochs,dataug_epoch_start,n_inner_iter, run)
with open(res_folder+"log/%s.json" % filename, "w+") as f:
try:
json.dump(out, f, indent=True)
print('Log :\"',f.name, '\" saved !')
except:
print("Failed to save logs :",f.name)
print('Execution Time : %.00f '%(exec_time)) exec_time=time.perf_counter() - t0
print('-'*9) max_cached = torch.cuda.max_memory_allocated()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB
####
print('-'*9)
times = [x["time"] for x in log]
out = {"Accuracy": max([x["acc"] for x in log]),
"Time": (np.mean(times),np.std(times), exec_time),
'Optimizer': optim_param,
"Device": device_name,
"Memory": max_cached,
"Param_names": aug_model.TF_names(),
"Log": log}
print(str(aug_model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
filename = "{}-{} epochs (dataug:{})- {} in_it-{}".format(str(aug_model),epochs,dataug_epoch_start,n_inner_iter, run)
with open(res_folder+"log/%s.json" % filename, "w+") as f:
try:
json.dump(out, f, indent=True)
print('Log :\"',f.name, '\" saved !')
except:
print("Failed to save logs :",f.name)
print('Execution Time : %.00f '%(exec_time))
print('-'*9)
''' '''
### Benchmark - RandAugment ### ### Benchmark - RandAugment/Vanilla ###
#'''
for model_type in model_list.keys(): for model_type in model_list.keys():
for model_name in model_list[model_type]: for model_name in model_list[model_type]:
for run in range(nb_run): for run in range(nb_run):
torch.cuda.reset_max_memory_cached() #reset_peak_stats torch.cuda.reset_max_memory_allocated() #reset_peak_stats
t0 = time.perf_counter() t0 = time.perf_counter()
model = getattr(model_type, model_name)(pretrained=False).to(device) model = getattr(model_type, model_name)(pretrained=False).to(device)
print("RandAugment(N{}-M{})-{} on {} for {} epochs".format(rand_aug['N'],rand_aug['M'],model_name, device_name, epochs)) print("{} on {} for {} epochs".format(model_name, device_name, epochs))
#print("RandAugment(N{}-M{:.2f})-{} on {} for {} epochs".format(rand_aug['N'],rand_aug['M'],model_name, device_name, epochs))
log= train_classic(model=model, opt_param=optim_param, epochs=epochs, print_freq=epochs/4) log= train_classic(model=model, opt_param=optim_param, epochs=epochs, print_freq=epochs/4)
exec_time=time.perf_counter() - t0 exec_time=time.perf_counter() - t0
max_cached = torch.cuda.max_memory_cached()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB max_cached = torch.cuda.max_memory_allocated()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB
#### ####
print('-'*9) print('-'*9)
times = [x["time"] for x in log] times = [x["time"] for x in log]
@ -162,10 +173,12 @@ if __name__ == "__main__":
'Optimizer': optim_param, 'Optimizer': optim_param,
"Device": device_name, "Device": device_name,
"Memory": max_cached, "Memory": max_cached,
"Rand_Aug": rand_aug, #"Rand_Aug": rand_aug,
"Log": log} "Log": log}
print("RandAugment-",model_name,": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1]) print(model_name,": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
filename = "RandAugment(N{}-M{})-{}-{} epochs -{}".format(rand_aug['N'],rand_aug['M'],model_name,epochs, run) filename = "{} epochs -{}".format(model_name,epochs, run)
#print("RandAugment-",model_name,": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
#filename = "RandAugment(N{}-M{:.2f})-{}-{} epochs -{}".format(rand_aug['N'],rand_aug['M'],model_name,epochs, run)
with open(res_folder+"log/%s.json" % filename, "w+") as f: with open(res_folder+"log/%s.json" % filename, "w+") as f:
try: try:
json.dump(out, f, indent=True) json.dump(out, f, indent=True)
@ -177,14 +190,14 @@ if __name__ == "__main__":
print('Execution Time : %.00f '%(exec_time)) print('Execution Time : %.00f '%(exec_time))
print('-'*9) print('-'*9)
#'''
### HP Search ### ### HP Search ###
''' '''
from LeNet import * from LeNet import *
inner_its = [1] inner_its = [1]
dist_mix = [0.0, 0.5, 0.8, 1.0] dist_mix = [1.0]#[0.0, 0.5, 0.8, 1.0]
N_seq_TF= [3, 2, 4] N_seq_TF= [5, 6]
mag_setup = [(True,True), (False, False)] #(FxSh, Independant) mag_setup = [(True, True), (False, False)] #(FxSh, Independant)
#prob_setup = [True, False] #prob_setup = [True, False]
try: try:
@ -204,8 +217,8 @@ if __name__ == "__main__":
t0 = time.perf_counter() t0 = time.perf_counter()
#model = getattr(models.resnet, 'resnet18')(pretrained=False) model = getattr(models.resnet, 'resnet18')(pretrained=False)
model = LeNet(3,10) #model = LeNet(3,10)
model = Higher_model(model) #run_dist_dataugV3 model = Higher_model(model) #run_dist_dataugV3
aug_model = Augmented_model(Data_augV5(TF_dict=tf_dict, N_TF=n_tf, mix_dist=dist, fixed_prob=p_setup, fixed_mag=m_setup[0], shared_mag=m_setup[1]), model).to(device) aug_model = Augmented_model(Data_augV5(TF_dict=tf_dict, N_TF=n_tf, mix_dist=dist, fixed_prob=p_setup, fixed_mag=m_setup[0], shared_mag=m_setup[1]), model).to(device)
#aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=2), model).to(device) #aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=2), model).to(device)

View file

@ -39,14 +39,14 @@ transform_train = torchvision.transforms.Compose([
]) ])
## RandAugment ## ## RandAugment ##
from RandAugment import RandAugment #from RandAugment import RandAugment
# Add RandAugment with N, M(hyperparameter) # Add RandAugment with N, M(hyperparameter)
rand_aug={'N': 2, 'M': 1} #rand_aug={'N': 2, 'M': 1}
#rand_aug={'N': 2, 'M': 9./30} #RN-ImageNet #rand_aug={'N': 2, 'M': 9./30} #RN-ImageNet
#rand_aug={'N': 3, 'M': 5./30} #WRN-CIFAR10 #rand_aug={'N': 3, 'M': 5./30} #WRN-CIFAR10
#rand_aug={'N': 2, 'M': 14./30} #WRN-CIFAR100 #rand_aug={'N': 2, 'M': 14./30} #WRN-CIFAR100
#rand_aug={'N': 3, 'M': 7./30} #WRN-SVHN #rand_aug={'N': 3, 'M': 7./30} #WRN-SVHN
transform_train.transforms.insert(0, RandAugment(n=rand_aug['N'], m=rand_aug['M'])) #transform_train.transforms.insert(0, RandAugment(n=rand_aug['N'], m=rand_aug['M']))
### Classic Dataset ### ### Classic Dataset ###

View file

@ -146,7 +146,7 @@ if __name__ == "__main__":
#### Augmented Model #### #### Augmented Model ####
if 'aug_model' in tasks: if 'aug_model' in tasks:
torch.cuda.reset_max_memory_cached() #reset_peak_stats torch.cuda.reset_max_memory_allocated() #reset_peak_stats
t0 = time.perf_counter() t0 = time.perf_counter()
tf_dict = {k: TF.TF_dict[k] for k in tf_names} tf_dict = {k: TF.TF_dict[k] for k in tf_names}
@ -166,7 +166,7 @@ if __name__ == "__main__":
save_sample_freq=1) save_sample_freq=1)
exec_time=time.perf_counter() - t0 exec_time=time.perf_counter() - t0
max_cached = torch.cuda.max_memory_cached()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB max_cached = torch.cuda.max_memory_allocated()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB
#### ####
print('-'*9) print('-'*9)
times = [x["time"] for x in log] times = [x["time"] for x in log]
@ -178,7 +178,7 @@ if __name__ == "__main__":
"Param_names": aug_model.TF_names(), "Param_names": aug_model.TF_names(),
"Log": log} "Log": log}
print(str(aug_model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1]) print(str(aug_model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
filename = "{}-{} epochs (dataug:{})- {} in_it".format(str(aug_model),epochs,dataug_epoch_start,n_inner_iter)+"(CV0.1)" filename = "{}-{} epochs (dataug:{})- {} in_it".format(str(aug_model),epochs,dataug_epoch_start,n_inner_iter)
with open("../res/log/%s.json" % filename, "w+") as f: with open("../res/log/%s.json" % filename, "w+") as f:
try: try:
json.dump(out, f, indent=True) json.dump(out, f, indent=True)

View file

@ -346,10 +346,14 @@ def posterize(x, bits):
return float_image(x & mask) return float_image(x & mask)
import torch.nn.functional as F
def solarize(x, thresholds): def solarize(x, thresholds):
"""Invert all pixel values above a threshold. """Invert all pixel values above a threshold.
Be warry that the use of the inequality (x>tresholds) block the gradient propagation. Be warry that the use of the inequality (x>tresholds) block the gradient propagation.
TODO : Make differentiable.
Args: Args:
x (Tensor): Batch of images. x (Tensor): Batch of images.
thresholds (Tensor): All pixels above this level are inverted thresholds (Tensor): All pixels above this level are inverted
@ -386,6 +390,25 @@ def solarize(x, thresholds):
#x[mask]=inv_x #x[mask]=inv_x
#x=x.masked_scatter(mask, inv_x) #x=x.masked_scatter(mask, inv_x)
#Differentiable (/Thresholds) ?
#inv_x_bT= F.relu(x) - F.relu(x - thresholds)
#inv_x_aT= 1-x #Besoin thresholds
#print('-'*10)
#print(thresholds[0])
#print(x[0])
#print(inv_x_bT[0])
#print(inv_x_aT[0])
#x=torch.where(x>thresholds,inv_x_aT, inv_x_bT)
#print(torch.allclose(x, x+0.001, atol=1e-3))
#print(torch.allclose(x, sol_x, atol=1e-2))
#print(torch.eq(x,sol_x)[0])
#print(x[0])
#print(sol_x[0])
#'''
return x return x
def blend(x,y,alpha): def blend(x,y,alpha):