2020-01-24 14:32:37 -05:00
|
|
|
""" Script to run experiment on smart augmentation.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
# Use available TF (see transformations.py)
|
2019-11-11 14:33:40 -05:00
|
|
|
tf_names = [
|
|
|
|
## Geometric TF ##
|
2019-12-02 06:37:19 -05:00
|
|
|
'Identity',
|
2020-01-13 10:59:32 -05:00
|
|
|
'FlipUD',
|
|
|
|
'FlipLR',
|
|
|
|
'Rotate',
|
|
|
|
'TranslateX',
|
|
|
|
'TranslateY',
|
|
|
|
'ShearX',
|
|
|
|
'ShearY',
|
2019-11-11 14:33:40 -05:00
|
|
|
|
2019-11-27 12:54:19 -05:00
|
|
|
## Color TF (Expect image in the range of [0, 1]) ##
|
2020-01-13 10:59:32 -05:00
|
|
|
'Contrast',
|
|
|
|
'Color',
|
|
|
|
'Brightness',
|
|
|
|
'Sharpness',
|
2020-01-15 16:55:03 -05:00
|
|
|
'Posterize',
|
|
|
|
'Solarize', #=>Image entre [0,1] #Pas opti pour des batch
|
2019-11-27 12:54:19 -05:00
|
|
|
|
|
|
|
#Color TF (Common mag scale)
|
|
|
|
#'+Contrast',
|
|
|
|
#'+Color',
|
|
|
|
#'+Brightness',
|
|
|
|
#'+Sharpness',
|
|
|
|
#'-Contrast',
|
|
|
|
#'-Color',
|
|
|
|
#'-Brightness',
|
|
|
|
#'-Sharpness',
|
|
|
|
#'=Posterize',
|
|
|
|
#'=Solarize',
|
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
## Bad Tranformations ##
|
|
|
|
# Bad Geometric TF #
|
2020-01-20 11:05:40 -05:00
|
|
|
#'BShearX',
|
|
|
|
#'BShearY',
|
|
|
|
#'BTranslateX-',
|
|
|
|
#'BTranslateX-',
|
|
|
|
#'BTranslateY',
|
|
|
|
#'BTranslateY-',
|
|
|
|
|
|
|
|
#'BadContrast',
|
|
|
|
#'BadBrightness',
|
|
|
|
|
|
|
|
#'Random',
|
|
|
|
#'RandBlend'
|
2020-01-24 14:32:37 -05:00
|
|
|
|
2019-11-11 14:33:40 -05:00
|
|
|
#Non fonctionnel
|
|
|
|
#'Auto_Contrast', #Pas opti pour des batch (Super lent)
|
|
|
|
#'Equalize',
|
|
|
|
]
|
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
|
|
|
|
device = torch.device('cuda') #Select device to use
|
2019-11-08 11:28:06 -05:00
|
|
|
|
|
|
|
if device == torch.device('cpu'):
|
|
|
|
device_name = 'CPU'
|
|
|
|
else:
|
|
|
|
device_name = torch.cuda.get_device_name(device)
|
|
|
|
|
|
|
|
##########################################
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
#Task to perform
|
2019-12-04 12:28:32 -05:00
|
|
|
tasks={
|
2020-01-10 13:21:34 -05:00
|
|
|
#'classic',
|
|
|
|
'aug_model'
|
2020-01-29 06:36:12 -05:00
|
|
|
#'aug_dataset', #Moved to old code
|
2019-12-04 12:28:32 -05:00
|
|
|
}
|
2020-01-24 14:32:37 -05:00
|
|
|
#Parameters
|
2020-01-13 18:02:36 -05:00
|
|
|
n_inner_iter = 1
|
2020-01-29 06:36:12 -05:00
|
|
|
epochs = 1
|
2020-01-21 13:53:07 -05:00
|
|
|
dataug_epoch_start=0
|
2019-12-09 13:49:57 -05:00
|
|
|
optim_param={
|
|
|
|
'Meta':{
|
|
|
|
'optim':'Adam',
|
|
|
|
'lr':1e-2, #1e-2
|
|
|
|
},
|
|
|
|
'Inner':{
|
|
|
|
'optim': 'SGD',
|
|
|
|
'lr':1e-2, #1e-2
|
|
|
|
'momentum':0.9, #0.9
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 11:28:06 -05:00
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
#Models
|
2020-01-16 16:38:15 -05:00
|
|
|
model = LeNet(3,10)
|
|
|
|
#model = ResNet(num_classes=10)
|
|
|
|
#Lents
|
2019-12-06 14:13:28 -05:00
|
|
|
#model = MobileNetV2(num_classes=10)
|
|
|
|
#model = WideResNet(num_classes=10, wrn_size=32)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-11-08 11:28:06 -05:00
|
|
|
#### Classic ####
|
2019-12-04 12:28:32 -05:00
|
|
|
if 'classic' in tasks:
|
|
|
|
t0 = time.process_time()
|
2019-12-06 14:13:28 -05:00
|
|
|
model = model.to(device)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-12-06 14:13:28 -05:00
|
|
|
print("{} on {} for {} epochs".format(str(model), device_name, epochs))
|
2020-01-16 16:38:15 -05:00
|
|
|
log= train_classic(model=model, opt_param=optim_param, epochs=epochs, print_freq=1)
|
|
|
|
#log= train_classic_higher(model=model, epochs=epochs)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-12-04 14:48:11 -05:00
|
|
|
exec_time=time.process_time() - t0
|
2019-12-04 12:28:32 -05:00
|
|
|
####
|
|
|
|
print('-'*9)
|
|
|
|
times = [x["time"] for x in log]
|
2019-12-09 13:49:57 -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, "Log": log}
|
2019-12-04 12:28:32 -05:00
|
|
|
print(str(model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
|
|
|
|
filename = "{}-{} epochs".format(str(model),epochs)
|
2020-01-24 14:32:37 -05:00
|
|
|
with open("../res/log/%s.json" % filename, "w+") as f:
|
2019-12-04 12:28:32 -05:00
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
|
|
|
|
2020-01-24 14:32:37 -05:00
|
|
|
plot_res(log, fig_name="../res/"+filename)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-12-04 14:48:11 -05:00
|
|
|
print('Execution Time : %.00f '%(exec_time))
|
2019-12-04 12:28:32 -05:00
|
|
|
print('-'*9)
|
2020-01-29 06:36:12 -05:00
|
|
|
|
|
|
|
#### Augmented Model ####
|
|
|
|
if 'aug_model' in tasks:
|
|
|
|
t0 = time.process_time()
|
|
|
|
|
|
|
|
tf_dict = {k: TF.TF_dict[k] for k in tf_names}
|
|
|
|
model = Higher_model(model) #run_dist_dataugV3
|
|
|
|
aug_model = Augmented_model(Data_augV5(TF_dict=tf_dict, N_TF=3, mix_dist=0.8, fixed_prob=False, fixed_mag=False, shared_mag=False), model).to(device)
|
|
|
|
#aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=2), model).to(device)
|
|
|
|
|
|
|
|
print("{} on {} for {} epochs - {} inner_it".format(str(aug_model), device_name, epochs, n_inner_iter))
|
|
|
|
log= run_simple_smartaug(model=aug_model, epochs=epochs, inner_it=n_inner_iter, opt_param=optim_param)
|
|
|
|
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=1,
|
|
|
|
unsup_loss=1,
|
|
|
|
hp_opt=False)
|
|
|
|
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
try:
|
|
|
|
plot_resV2(log, fig_name="../res/"+filename, param_names=aug_model.TF_names())
|
|
|
|
except:
|
|
|
|
print("Failed to plot res")
|
|
|
|
|
|
|
|
print('Execution Time : %.00f '%(exec_time))
|
|
|
|
print('-'*9)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
|
|
|
#### Augmented Dataset ####
|
2020-01-24 14:32:37 -05:00
|
|
|
'''
|
2019-12-04 12:28:32 -05:00
|
|
|
if 'aug_dataset' in tasks:
|
2019-12-04 12:58:11 -05:00
|
|
|
|
2019-12-04 12:28:32 -05:00
|
|
|
t0 = time.process_time()
|
2019-12-04 14:48:11 -05:00
|
|
|
|
2019-12-06 16:54:40 -05:00
|
|
|
#data_train_aug = AugmentedDataset("./data", train=True, download=download_data, transform=transform, subset=(0,int(len(data_train)/2)))
|
|
|
|
#data_train_aug.augement_data(aug_copy=30)
|
|
|
|
#print(data_train_aug)
|
|
|
|
#dl_train = torch.utils.data.DataLoader(data_train_aug, batch_size=BATCH_SIZE, shuffle=True)
|
|
|
|
|
|
|
|
#xs, ys = next(iter(dl_train))
|
|
|
|
#viz_sample_data(imgs=xs, labels=ys, fig_name='samples/data_sample_{}'.format(str(data_train_aug)))
|
|
|
|
|
|
|
|
#model = model.to(device)
|
|
|
|
|
|
|
|
#print("{} on {} for {} epochs".format(str(model), device_name, epochs))
|
|
|
|
#log= train_classic(model=model, epochs=epochs, print_freq=10)
|
|
|
|
##log= train_classic_higher(model=model, epochs=epochs)
|
|
|
|
|
|
|
|
data_train_aug = AugmentedDatasetV2("./data", train=True, download=download_data, transform=transform, subset=(0,int(len(data_train)/2)))
|
2019-12-09 13:49:57 -05:00
|
|
|
data_train_aug.augement_data(aug_copy=1)
|
2019-12-04 14:48:11 -05:00
|
|
|
print(data_train_aug)
|
2019-12-06 16:54:40 -05:00
|
|
|
unsup_ratio = 5
|
2020-01-10 13:21:34 -05:00
|
|
|
dl_unsup = torch.utils.data.DataLoader(data_train_aug, batch_size=BATCH_SIZE*unsup_ratio, shuffle=True, num_workers=num_workers, pin_memory=pin_memory)
|
2019-12-04 14:48:11 -05:00
|
|
|
|
2019-12-06 16:54:40 -05:00
|
|
|
unsup_xs, sup_xs, ys = next(iter(dl_unsup))
|
|
|
|
viz_sample_data(imgs=sup_xs, labels=ys, fig_name='samples/data_sample_{}'.format(str(data_train_aug)))
|
|
|
|
viz_sample_data(imgs=unsup_xs, labels=ys, fig_name='samples/data_sample_{}_unsup'.format(str(data_train_aug)))
|
2019-12-04 14:48:11 -05:00
|
|
|
|
2019-12-06 14:13:28 -05:00
|
|
|
model = model.to(device)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-12-06 14:13:28 -05:00
|
|
|
print("{} on {} for {} epochs".format(str(model), device_name, epochs))
|
2019-12-09 13:49:57 -05:00
|
|
|
log= train_UDA(model=model, dl_unsup=dl_unsup, epochs=epochs, opt_param=optim_param, print_freq=10)
|
2019-12-04 12:28:32 -05:00
|
|
|
|
2019-12-04 14:48:11 -05:00
|
|
|
exec_time=time.process_time() - t0
|
2019-12-04 12:28:32 -05:00
|
|
|
####
|
|
|
|
print('-'*9)
|
|
|
|
times = [x["time"] for x in log]
|
2020-01-13 10:59: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, "Param_names": data_train_aug._TF, "Log": log}
|
2019-12-04 12:28:32 -05:00
|
|
|
print(str(model),": acc", out["Accuracy"], "in:", out["Time"][0], "+/-", out["Time"][1])
|
|
|
|
filename = "{}-{}-{} epochs".format(str(data_train_aug),str(model),epochs)
|
|
|
|
with open("res/log/%s.json" % filename, "w+") as f:
|
|
|
|
json.dump(out, f, indent=True)
|
|
|
|
print('Log :\"',f.name, '\" saved !')
|
|
|
|
|
|
|
|
plot_res(log, fig_name="res/"+filename)
|
|
|
|
|
2019-12-04 14:48:11 -05:00
|
|
|
print('Execution Time : %.00f '%(exec_time))
|
2019-12-04 12:28:32 -05:00
|
|
|
print('-'*9)
|
2020-01-29 06:36:12 -05:00
|
|
|
'''
|