2020-01-24 14:32:37 -05:00
|
|
|
""" Script to run experiment on smart augmentation.
|
|
|
|
|
|
|
|
"""
|
2020-01-31 16:43:10 -05:00
|
|
|
import sys
|
2019-11-08 11:28:06 -05:00
|
|
|
from dataug import *
|
2019-11-13 11:44:29 -05:00
|
|
|
#from utils import *
|
|
|
|
from train_utils import *
|
2020-02-19 11:54:54 -05:00
|
|
|
from transformations import TF_loader
|
2024-08-20 11:53:35 +02:00
|
|
|
# from arg_parser import *
|
2019-11-08 11:28:06 -05:00
|
|
|
|
2020-02-19 11:54:54 -05:00
|
|
|
TF_loader=TF_loader()
|
2020-01-24 14:32:37 -05:00
|
|
|
|
2020-01-30 11:21:25 -05:00
|
|
|
torch.backends.cudnn.benchmark = True #Faster if same input size #Not recommended for reproductibility
|
|
|
|
|
|
|
|
#Increase reproductibility
|
|
|
|
torch.manual_seed(0)
|
|
|
|
np.random.seed(0)
|
|
|
|
|
2019-11-08 11:28:06 -05:00
|
|
|
##########################################
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2024-08-20 11:53:35 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
print(args)
|
|
|
|
|
|
|
|
res_folder=args.res_folder
|
|
|
|
postfix=args.postfix
|
|
|
|
|
|
|
|
if args.dtype == 'FP32':
|
|
|
|
def_type=torch.float32
|
|
|
|
elif args.dtype == 'FP16':
|
|
|
|
# def_type=torch.float16 #Default : float32
|
|
|
|
def_type=torch.bfloat16
|
|
|
|
else:
|
|
|
|
raise Exception('dtype not supported :', args.dtype)
|
|
|
|
torch.set_default_dtype(def_type) #Default : float32
|
|
|
|
|
|
|
|
|
|
|
|
device = torch.device(args.device) #Select device to use
|
|
|
|
if device == torch.device('cpu'):
|
|
|
|
device_name = 'CPU'
|
|
|
|
else:
|
|
|
|
device_name = torch.cuda.get_device_name(device)
|
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
#Parameters
|
2024-08-20 11:53:35 +02:00
|
|
|
n_inner_iter = args.K
|
|
|
|
epochs = args.epochs
|
2020-01-21 13:53:07 -05:00
|
|
|
dataug_epoch_start=0
|
2024-08-20 11:53:35 +02:00
|
|
|
Nb_TF_seq= args.N
|
2019-12-09 13:49:57 -05:00
|
|
|
optim_param={
|
|
|
|
'Meta':{
|
|
|
|
'optim':'Adam',
|
2024-08-20 11:53:35 +02:00
|
|
|
'lr':args.mlr,
|
|
|
|
'epoch_start': args.meta_epoch_start, #0 / 2 (Resnet?)
|
|
|
|
'reg_factor': args.mag_reg,
|
|
|
|
'scheduler': None, #None, 'multiStep'
|
2019-12-09 13:49:57 -05:00
|
|
|
},
|
|
|
|
'Inner':{
|
|
|
|
'optim': 'SGD',
|
2024-08-20 11:53:35 +02:00
|
|
|
'lr':args.lr, #1e-2/1e-1 (ResNet)
|
|
|
|
'momentum':args.momentum, #0.9
|
|
|
|
'weight_decay':args.decay, #0.0005
|
|
|
|
'nesterov':args.nesterov, #False (True: Bad behavior w/ Data_aug)
|
|
|
|
'scheduler': args.scheduler, #None, 'cosine', 'multiStep', 'exponential'
|
|
|
|
'warmup':{
|
|
|
|
'multiplier': args.warmup, #2 #+ batch_size => + mutliplier #No warmup = 0
|
|
|
|
'epochs': 5
|
|
|
|
}
|
2019-12-09 13:49:57 -05:00
|
|
|
}
|
|
|
|
}
|
2019-11-08 11:28:06 -05:00
|
|
|
|
2024-08-20 11:53:35 +02:00
|
|
|
#Info params
|
|
|
|
F1=True
|
|
|
|
sample_save=None
|
|
|
|
print_f= epochs/4
|
|
|
|
|
|
|
|
#Load network
|
|
|
|
model, model_name= load_model(args.model, num_classes=len(dl_train.dataset.classes), pretrained=args.pretrained)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-11-08 11:28:06 -05:00
|
|
|
#### Classic ####
|
2024-08-20 11:53:35 +02:00
|
|
|
if not args.augment:
|
|
|
|
if device_name != 'CPU':
|
|
|
|
torch.cuda.reset_max_memory_allocated() #reset_peak_stats
|
|
|
|
torch.cuda.reset_max_memory_cached() #reset_peak_stats
|
2020-02-03 12:55:54 -05:00
|
|
|
t0 = time.perf_counter()
|
2020-02-12 13:43:44 -05:00
|
|
|
|
2019-12-06 14:13:28 -05:00
|
|
|
model = model.to(device)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2020-01-31 16:43:10 -05:00
|
|
|
|
2020-02-21 15:29:34 -05:00
|
|
|
print("{} on {} for {} epochs{}".format(model_name, device_name, epochs, postfix))
|
|
|
|
#print("RandAugment(N{}-M{:.2f})-{} on {} for {} epochs{}".format(rand_aug['N'],rand_aug['M'],model_name, device_name, epochs, postfix))
|
2024-08-20 11:53:35 +02:00
|
|
|
log= train_classic(model=model, opt_param=optim_param, epochs=epochs, print_freq=print_f)
|
2020-01-16 16:38:15 -05:00
|
|
|
#log= train_classic_higher(model=model, epochs=epochs)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2020-02-03 12:55:54 -05:00
|
|
|
exec_time=time.perf_counter() - t0
|
2024-08-20 11:53:35 +02:00
|
|
|
|
|
|
|
if device_name != 'CPU':
|
|
|
|
max_allocated = torch.cuda.max_memory_allocated()/(1024.0 * 1024.0)
|
|
|
|
max_cached = torch.cuda.max_memory_cached()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB
|
|
|
|
else:
|
|
|
|
max_allocated = 0.0
|
|
|
|
max_cached=0.0
|
2019-12-04 12:28:32 -05:00
|
|
|
####
|
|
|
|
print('-'*9)
|
|
|
|
times = [x["time"] for x in log]
|
2020-02-03 12:06:32 -05:00
|
|
|
out = {"Accuracy": max([x["acc"] for x in log]),
|
|
|
|
"Time": (np.mean(times),np.std(times), exec_time),
|
|
|
|
'Optimizer': optim_param['Inner'],
|
|
|
|
"Device": device_name,
|
2020-02-12 13:43:44 -05:00
|
|
|
"Memory": [max_allocated, max_cached],
|
|
|
|
#"Rand_Aug": rand_aug,
|
2020-02-03 12:06:32 -05:00
|
|
|
"Log": log}
|
2020-02-03 11:21:54 -05:00
|
|
|
print(model_name,": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
|
2020-02-21 15:29:34 -05:00
|
|
|
filename = "{}-{} epochs".format(model_name,epochs)+postfix
|
2020-02-12 13:43:44 -05:00
|
|
|
#print("RandAugment-",model_name,": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
|
2020-02-21 15:29:34 -05:00
|
|
|
#filename = "RandAugment(N{}-M{:.2f})-{}-{} epochs".format(rand_aug['N'],rand_aug['M'],model_name,epochs)+postfix
|
2024-08-20 11:53:35 +02:00
|
|
|
with open(res_folder+"log/%s.json" % filename, "w+") as f:
|
2020-02-03 11:21:54 -05:00
|
|
|
try:
|
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
|
|
|
except:
|
|
|
|
print("Failed to save logs :",f.name)
|
|
|
|
print(sys.exc_info()[1])
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2020-02-03 11:21:54 -05:00
|
|
|
try:
|
2024-08-20 11:53:35 +02:00
|
|
|
plot_resV2(log, fig_name=res_folder+filename, f1=F1)
|
2020-02-03 11:21:54 -05:00
|
|
|
except:
|
|
|
|
print("Failed to plot res")
|
|
|
|
print(sys.exc_info()[1])
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2020-02-03 12:55:54 -05:00
|
|
|
print('Execution Time (s): %.00f '%(exec_time))
|
2019-12-04 12:28:32 -05:00
|
|
|
print('-'*9)
|
2020-01-29 06:36:12 -05:00
|
|
|
|
|
|
|
#### Augmented Model ####
|
2024-08-20 11:53:35 +02:00
|
|
|
else:
|
|
|
|
# tf_config='../config/invScale_wide_tf_config.json'#'../config/invScale_wide_tf_config.json'#'../config/base_tf_config.json'
|
|
|
|
tf_dict, tf_ignore_mag =TF_loader.load_TF_dict(args.tf_config)
|
2020-02-14 13:57:17 -05:00
|
|
|
|
2024-08-20 11:53:35 +02:00
|
|
|
if device_name != 'CPU':
|
|
|
|
torch.cuda.reset_max_memory_allocated() #reset_peak_stats
|
|
|
|
torch.cuda.reset_max_memory_cached() #reset_peak_stats
|
2020-02-03 12:55:54 -05:00
|
|
|
t0 = time.perf_counter()
|
2020-01-29 06:36:12 -05:00
|
|
|
|
2020-01-31 16:43:10 -05:00
|
|
|
model = Higher_model(model, model_name) #run_dist_dataugV3
|
2024-08-20 11:53:35 +02:00
|
|
|
dataug_mod = 'Data_augV8' if args.learn_seq else 'Data_augV5'
|
2020-02-25 14:05:17 -05:00
|
|
|
if n_inner_iter !=0:
|
|
|
|
aug_model = Augmented_model(
|
2024-08-20 11:53:35 +02:00
|
|
|
globals()[dataug_mod](TF_dict=tf_dict,
|
2020-02-25 14:05:17 -05:00
|
|
|
N_TF=Nb_TF_seq,
|
2024-08-20 11:53:35 +02:00
|
|
|
temp=args.temp,
|
2020-02-25 14:05:17 -05:00
|
|
|
fixed_prob=False,
|
2024-08-20 11:53:35 +02:00
|
|
|
fixed_mag=args.fixed_mag,
|
|
|
|
shared_mag=args.shared_mag,
|
2020-02-25 14:05:17 -05:00
|
|
|
TF_ignore_mag=tf_ignore_mag), model).to(device)
|
|
|
|
else:
|
|
|
|
aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=Nb_TF_seq), model).to(device)
|
2020-01-29 06:36:12 -05:00
|
|
|
|
2020-02-21 15:29:34 -05:00
|
|
|
print("{} on {} for {} epochs - {} inner_it{}".format(str(aug_model), device_name, epochs, n_inner_iter, postfix))
|
2024-08-20 11:53:35 +02:00
|
|
|
log, aug_acc = run_dist_dataugV3(model=aug_model,
|
2020-01-29 06:36:12 -05:00
|
|
|
epochs=epochs,
|
|
|
|
inner_it=n_inner_iter,
|
|
|
|
dataug_epoch_start=dataug_epoch_start,
|
|
|
|
opt_param=optim_param,
|
|
|
|
unsup_loss=1,
|
2024-08-20 11:53:35 +02:00
|
|
|
augment_loss=args.augment_loss,
|
|
|
|
hp_opt=False, #False #['lr', 'momentum', 'weight_decay']
|
|
|
|
print_freq=print_f,
|
|
|
|
save_sample_freq=sample_save)
|
2020-01-29 06:36:12 -05:00
|
|
|
|
2020-02-03 12:55:54 -05:00
|
|
|
exec_time=time.perf_counter() - t0
|
2024-08-20 11:53:35 +02:00
|
|
|
if device_name != 'CPU':
|
|
|
|
max_allocated = torch.cuda.max_memory_allocated()/(1024.0 * 1024.0)
|
|
|
|
max_cached = torch.cuda.max_memory_cached()/(1024.0 * 1024.0) #torch.cuda.max_memory_reserved() #MB
|
|
|
|
else:
|
|
|
|
max_allocated = 0.0
|
|
|
|
max_cached = 0.0
|
2020-01-29 06:36:12 -05:00
|
|
|
####
|
|
|
|
print('-'*9)
|
|
|
|
times = [x["time"] for x in log]
|
2020-02-03 12:06:32 -05:00
|
|
|
out = {"Accuracy": max([x["acc"] for x in log]),
|
2024-08-20 11:53:35 +02:00
|
|
|
"Aug_Accuracy": [args.augment_loss, aug_acc],
|
2020-02-03 12:06:32 -05:00
|
|
|
"Time": (np.mean(times),np.std(times), exec_time),
|
|
|
|
'Optimizer': optim_param,
|
|
|
|
"Device": device_name,
|
2020-02-10 14:50:45 -05:00
|
|
|
"Memory": [max_allocated, max_cached],
|
2024-08-20 11:53:35 +02:00
|
|
|
"TF_config": args.tf_config,
|
2020-02-03 12:06:32 -05:00
|
|
|
"Param_names": aug_model.TF_names(),
|
|
|
|
"Log": log}
|
2024-08-20 11:53:35 +02:00
|
|
|
print(str(aug_model),": acc", out["Accuracy"], "/ aug_acc", out["Aug_Accuracy"][1] , "in:", out["Time"][0], "+/-", out["Time"][1])
|
|
|
|
filename = "{}-{}_epochs-{}_in_it-AL{}".format(str(aug_model),epochs,n_inner_iter,args.augment_loss)+postfix
|
|
|
|
with open(res_folder+"log/%s.json" % filename, "w+") as f:
|
2020-01-29 06:36:12 -05:00
|
|
|
try:
|
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
|
|
|
except:
|
|
|
|
print("Failed to save logs :",f.name)
|
2020-02-03 11:21:54 -05:00
|
|
|
print(sys.exc_info()[1])
|
2020-01-29 06:36:12 -05:00
|
|
|
try:
|
2024-08-20 11:53:35 +02:00
|
|
|
plot_resV2(log, fig_name=res_folder+filename, param_names=aug_model.TF_names(), f1=F1)
|
2020-01-29 06:36:12 -05:00
|
|
|
except:
|
|
|
|
print("Failed to plot res")
|
2020-02-03 11:21:54 -05:00
|
|
|
print(sys.exc_info()[1])
|
2020-01-29 06:36:12 -05:00
|
|
|
|
2020-02-03 12:55:54 -05:00
|
|
|
print('Execution Time (s): %.00f '%(exec_time))
|
2020-01-31 10:34:44 -05:00
|
|
|
print('-'*9)
|