RandAugment

This commit is contained in:
Harle, Antoine (Contracteur) 2020-02-05 12:24:20 -05:00
parent 7221142a9a
commit 6277e268c1
3 changed files with 407 additions and 73 deletions

View file

@ -1,3 +1,6 @@
""" Script to run series of experiments.
"""
from dataug import *
#from utils import *
from train_utils import *
@ -13,14 +16,16 @@ optim_param={
},
'Inner':{
'optim': 'SGD',
'lr':1e-1, #1e-2 #1e-1 for ResNet
'lr':1e-2, #1e-2 #1e-1 for ResNet
'momentum':0.9, #0.9
}
}
res_folder="../res/benchmark/CIFAR10/"
epochs= 150
#res_folder="../res/HPsearch/"
epochs= 200
dataug_epoch_start=0
nb_run= 3
# Use available TF (see transformations.py)
tf_names = [
@ -80,60 +85,107 @@ if __name__ == "__main__":
'''
for model_type in model_list.keys():
for model_name in model_list[model_type]:
model = getattr(model_type, model_name)(pretrained=False)
for run in range(nb_run):
t0 = time.process_time()
torch.cuda.reset_max_memory_cached() #reset_peak_stats
t0 = time.perf_counter()
model = Higher_model(model) #run_dist_dataugV3
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)
model = getattr(model_type, model_name)(pretrained=False)
print("{} on {} for {} epochs - {} inner_it".format(str(aug_model), device_name, epochs, n_inner_iter))
log= run_dist_dataugV3(model=aug_model,
epochs=epochs,
inner_it=n_inner_iter,
dataug_epoch_start=dataug_epoch_start,
opt_param=optim_param,
print_freq=epochs/4,
unsup_loss=1,
hp_opt=False,
save_sample_freq=None)
model = Higher_model(model, model_name) #run_dist_dataugV3
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)
exec_time=time.process_time() - t0
####
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, "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/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("{} on {} for {} epochs - {} inner_it".format(str(aug_model), device_name, epochs, n_inner_iter))
log= run_dist_dataugV3(model=aug_model,
epochs=epochs,
inner_it=n_inner_iter,
dataug_epoch_start=dataug_epoch_start,
opt_param=optim_param,
print_freq=epochs/4,
unsup_loss=1,
hp_opt=False,
save_sample_freq=None)
print('Execution Time : %.00f '%(exec_time))
print('-'*9)
exec_time=time.perf_counter() - t0
max_cached = torch.cuda.max_memory_cached()/(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 ###
for model_type in model_list.keys():
for model_name in model_list[model_type]:
for run in range(nb_run):
torch.cuda.reset_max_memory_cached() #reset_peak_stats
t0 = time.perf_counter()
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))
log= train_classic(model=model, opt_param=optim_param, epochs=epochs, print_freq=epochs/4)
exec_time=time.perf_counter() - t0
max_cached = torch.cuda.max_memory_cached()/(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,
"Rand_Aug": rand_aug,
"Log": log}
print("RandAugment-",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)
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)
#plot_resV2(log, fig_name=res_folder+filename)
print('Execution Time : %.00f '%(exec_time))
print('-'*9)
### HP Search ###
'''
from LeNet import *
inner_its = [1]
dist_mix = [0.0, 0.5, 0.8, 1.0]
N_seq_TF= [2, 3, 4]
N_seq_TF= [3, 2, 4]
mag_setup = [(True,True), (False, False)] #(FxSh, Independant)
#prob_setup = [True, False]
nb_run= 3
try:
os.mkdir(res_folder)
@ -150,9 +202,10 @@ if __name__ == "__main__":
p_setup=False
for run in range(nb_run):
t0 = time.process_time()
t0 = time.perf_counter()
model = getattr(models.resnet, 'resnet18')(pretrained=False)
#model = getattr(models.resnet, 'resnet18')(pretrained=False)
model = LeNet(3,10)
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(RandAug(TF_dict=tf_dict, N_TF=2), model).to(device)
@ -168,7 +221,7 @@ if __name__ == "__main__":
hp_opt=False,
save_sample_freq=None)
exec_time=time.process_time() - t0
exec_time=time.perf_counter() - t0
####
print('-'*9)
times = [x["time"] for x in log]
@ -184,4 +237,4 @@ if __name__ == "__main__":
print('Execution Time : %.00f '%(exec_time))
print('-'*9)
#'''
'''