2019-11-08 11:28:06 -05:00
|
|
|
from model import *
|
|
|
|
from dataug import *
|
2019-11-13 11:44:29 -05:00
|
|
|
#from utils import *
|
|
|
|
from train_utils import *
|
2019-11-08 11:28:06 -05:00
|
|
|
|
2019-11-11 14:33:40 -05:00
|
|
|
tf_names = [
|
|
|
|
## Geometric TF ##
|
2019-11-19 15:37:29 -05:00
|
|
|
'Identity',
|
2019-11-22 11:22:57 -05:00
|
|
|
'FlipUD',
|
2019-11-19 15:37:29 -05:00
|
|
|
'FlipLR',
|
2019-11-11 17:01:15 -05:00
|
|
|
'Rotate',
|
2019-11-22 11:22:57 -05:00
|
|
|
'TranslateX',
|
|
|
|
'TranslateY',
|
2019-11-11 17:01:15 -05:00
|
|
|
'ShearX',
|
|
|
|
'ShearY',
|
2019-11-11 14:33:40 -05:00
|
|
|
|
2019-11-22 11:22:57 -05:00
|
|
|
#'BRotate',
|
|
|
|
#'BTranslateX',
|
|
|
|
#'BTranslateY',
|
|
|
|
#'BShearX',
|
|
|
|
#'BShearY',
|
|
|
|
|
2019-11-11 14:33:40 -05:00
|
|
|
## Color TF (Expect image in the range of [0, 1]) ##
|
2019-11-11 17:01:15 -05:00
|
|
|
'Contrast',
|
|
|
|
'Color',
|
2019-11-11 14:33:40 -05:00
|
|
|
'Brightness',
|
2019-11-11 17:01:15 -05:00
|
|
|
'Sharpness',
|
|
|
|
'Posterize',
|
2019-11-22 11:22:57 -05:00
|
|
|
'Solarize', #=>Image entre [0,1] #Pas opti pour des batch
|
2019-11-11 14:33:40 -05:00
|
|
|
|
|
|
|
#Non fonctionnel
|
|
|
|
#'Auto_Contrast', #Pas opti pour des batch (Super lent)
|
|
|
|
#'Equalize',
|
|
|
|
]
|
|
|
|
|
2019-11-08 11:28:06 -05:00
|
|
|
device = torch.device('cuda')
|
|
|
|
|
|
|
|
if device == torch.device('cpu'):
|
|
|
|
device_name = 'CPU'
|
|
|
|
else:
|
|
|
|
device_name = torch.cuda.get_device_name(device)
|
|
|
|
|
|
|
|
##########################################
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2019-11-22 11:22:57 -05:00
|
|
|
n_inner_iter = 10
|
2019-11-21 12:29:17 -05:00
|
|
|
epochs = 100
|
2019-11-08 11:28:06 -05:00
|
|
|
dataug_epoch_start=0
|
|
|
|
|
|
|
|
#### Classic ####
|
|
|
|
'''
|
2019-11-21 12:29:17 -05:00
|
|
|
model = LeNet(3,10).to(device)
|
|
|
|
#model = WideResNet(num_classes=10, wrn_size=16).to(device)
|
2019-11-08 11:28:06 -05:00
|
|
|
#model = Augmented_model(Data_augV3(mix_dist=0.0), LeNet(3,10)).to(device)
|
|
|
|
#model.augment(mode=False)
|
|
|
|
|
|
|
|
print(str(model), 'on', device_name)
|
2019-11-11 14:33:40 -05:00
|
|
|
log= train_classic(model=model, epochs=epochs)
|
|
|
|
#log= train_classic_higher(model=model, epochs=epochs)
|
2019-11-08 11:28:06 -05:00
|
|
|
|
|
|
|
####
|
|
|
|
plot_res(log, fig_name="res/{}-{} epochs".format(str(model),epochs))
|
|
|
|
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)), "Device": device_name, "Log": log}
|
2019-11-19 15:37:29 -05:00
|
|
|
print(str(model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
|
2019-11-08 11:28:06 -05:00
|
|
|
with open("res/log/%s.json" % "{}-{} epochs".format(str(model),epochs), "w+") as f:
|
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
|
|
|
print('-'*9)
|
|
|
|
'''
|
|
|
|
#### Augmented Model ####
|
2019-11-20 16:06:27 -05:00
|
|
|
'''
|
2019-11-13 13:38:00 -05:00
|
|
|
t0 = time.process_time()
|
2019-11-11 14:33:40 -05:00
|
|
|
tf_dict = {k: TF.TF_dict[k] for k in tf_names}
|
|
|
|
#tf_dict = TF.TF_dict
|
2019-11-22 11:22:57 -05:00
|
|
|
aug_model = Augmented_model(Data_augV5(TF_dict=tf_dict, N_TF=1, mix_dist=0.0, fixed_mag=False, shared_mag=False), LeNet(3,10)).to(device)
|
2019-11-21 12:29:17 -05:00
|
|
|
#aug_model = Augmented_model(Data_augV5(TF_dict=tf_dict, N_TF=2, mix_dist=0.5, fixed_mag=True, shared_mag=True), WideResNet(num_classes=10, wrn_size=160)).to(device)
|
2019-11-08 11:28:06 -05:00
|
|
|
print(str(aug_model), 'on', device_name)
|
|
|
|
#run_simple_dataug(inner_it=n_inner_iter, epochs=epochs)
|
2019-11-22 11:22:57 -05:00
|
|
|
log= run_dist_dataugV2(model=aug_model, epochs=epochs, inner_it=n_inner_iter, dataug_epoch_start=dataug_epoch_start, print_freq=1, loss_patience=None)
|
2019-11-08 11:28:06 -05:00
|
|
|
|
|
|
|
####
|
|
|
|
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)), "Device": device_name, "Param_names": aug_model.TF_names(), "Log": log}
|
2019-11-19 15:37:29 -05:00
|
|
|
print(str(aug_model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
|
2019-11-20 16:06:27 -05:00
|
|
|
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:
|
2019-11-08 11:28:06 -05:00
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
2019-11-13 13:38:00 -05:00
|
|
|
|
2019-11-20 16:06:27 -05:00
|
|
|
plot_resV2(log, fig_name="res/"+filename, param_names=tf_names)
|
|
|
|
|
2019-11-19 15:37:29 -05:00
|
|
|
print('Execution Time : %.00f '%(time.process_time() - t0))
|
2019-11-08 11:28:06 -05:00
|
|
|
print('-'*9)
|
2019-11-14 21:42:00 -05:00
|
|
|
'''
|
2019-11-20 16:06:27 -05:00
|
|
|
#### TF tests ####
|
|
|
|
#'''
|
2019-11-22 11:22:57 -05:00
|
|
|
res_folder="res/brutus-tests/"
|
|
|
|
epochs= 150
|
|
|
|
inner_its = [0, 1, 10]
|
|
|
|
dist_mix = [0.0, 0.5, 1]
|
2019-11-12 18:14:10 -05:00
|
|
|
dataug_epoch_starts= [0]
|
2019-11-20 16:06:27 -05:00
|
|
|
tf_dict = {k: TF.TF_dict[k] for k in tf_names}
|
|
|
|
TF_nb = [len(tf_dict)] #range(10,len(TF.TF_dict)+1) #[len(TF.TF_dict)]
|
2019-11-22 11:22:57 -05:00
|
|
|
N_seq_TF= [1, 2, 3, 4]
|
|
|
|
mag_setup = [(True,True), (False,True), (False, False)]
|
|
|
|
nb_run= 3
|
2019-11-08 16:50:02 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.mkdir(res_folder)
|
|
|
|
os.mkdir(res_folder+"log/")
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
for n_inner_iter in inner_its:
|
|
|
|
for dataug_epoch_start in dataug_epoch_starts:
|
2019-11-12 18:14:10 -05:00
|
|
|
for n_tf in N_seq_TF:
|
2019-11-20 16:06:27 -05:00
|
|
|
for dist in dist_mix:
|
|
|
|
#for i in TF_nb:
|
|
|
|
for m_setup in mag_setup:
|
|
|
|
for run in range(nb_run):
|
|
|
|
#keys = list(TF.TF_dict.keys())[0:i]
|
|
|
|
#ntf_dict = {k: TF.TF_dict[k] for k in keys}
|
|
|
|
|
|
|
|
aug_model = Augmented_model(Data_augV5(TF_dict=tf_dict, N_TF=n_tf, mix_dist=dist, fixed_mag=m_setup[0], shared_mag=m_setup[1]), LeNet(3,10)).to(device)
|
|
|
|
print(str(aug_model), 'on', device_name)
|
|
|
|
#run_simple_dataug(inner_it=n_inner_iter, epochs=epochs)
|
|
|
|
log= run_dist_dataugV2(model=aug_model, epochs=epochs, inner_it=n_inner_iter, dataug_epoch_start=dataug_epoch_start, print_freq=20, loss_patience=None)
|
|
|
|
|
|
|
|
####
|
|
|
|
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)), "Device": device_name, "Param_names": aug_model.TF_names(), "Log": log}
|
|
|
|
print(str(aug_model),": acc", out["Accuracy"], "in (s?):", 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:
|
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
|
|
|
|
|
|
|
#plot_resV2(log, fig_name=res_folder+filename, param_names=tf_names)
|
|
|
|
print('-'*9)
|
|
|
|
|
|
|
|
#'''
|