mirror of
https://github.com/AntoineHX/smart_augmentation.git
synced 2025-05-04 04:00:46 +02:00
+controle mag reg + Tests fonction mag reg
This commit is contained in:
parent
fc0fb25148
commit
534e244307
6 changed files with 45 additions and 22 deletions
|
@ -29,11 +29,11 @@ optim_param={
|
||||||
|
|
||||||
res_folder="../res/benchmark/CIFAR10/"
|
res_folder="../res/benchmark/CIFAR10/"
|
||||||
#res_folder="../res/HPsearch/"
|
#res_folder="../res/HPsearch/"
|
||||||
epochs= 400
|
epochs= 200
|
||||||
dataug_epoch_start=0
|
dataug_epoch_start=0
|
||||||
nb_run= 1
|
nb_run= 1
|
||||||
|
|
||||||
tf_config='../config/base_tf_config.json'
|
tf_config='../config/wide_tf_config.json' #'../config/wide_tf_config.json'#'../config/base_tf_config.json'
|
||||||
TF_loader=TF_loader()
|
TF_loader=TF_loader()
|
||||||
tf_dict, tf_ignore_mag =TF_loader.load_TF_dict(tf_config)
|
tf_dict, tf_ignore_mag =TF_loader.load_TF_dict(tf_config)
|
||||||
|
|
||||||
|
@ -55,15 +55,16 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
### Benchmark ###
|
### Benchmark ###
|
||||||
#'''
|
#'''
|
||||||
n_inner_iter = 1#[0, 1]
|
inner_its = [3]
|
||||||
dist_mix = [0.5]
|
dist_mix = [0.5]
|
||||||
N_seq_TF= [3, 4]
|
N_seq_TF= [3]
|
||||||
mag_setup = [(False, False)] #[(True, True), (False, False)] #(FxSh, Independant)
|
mag_setup = [(False, False)] #[(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):
|
||||||
|
|
||||||
|
for n_inner_iter in inner_its:
|
||||||
for n_tf in N_seq_TF:
|
for n_tf in N_seq_TF:
|
||||||
for dist in dist_mix:
|
for dist in dist_mix:
|
||||||
for m_setup in mag_setup:
|
for m_setup in mag_setup:
|
||||||
|
|
|
@ -115,8 +115,17 @@ class Data_augV5(nn.Module): #Optimisation jointe (mag, proba)
|
||||||
if self._shared_mag :
|
if self._shared_mag :
|
||||||
self._reg_tgt = torch.tensor(TF.PARAMETER_MAX, dtype=torch.float) #Encourage amplitude max
|
self._reg_tgt = torch.tensor(TF.PARAMETER_MAX, dtype=torch.float) #Encourage amplitude max
|
||||||
else:
|
else:
|
||||||
self._reg_mask=[self._TF.index(t) for t in self._TF if t not in self._TF_ignore_mag]
|
TF_mag=[t for t in self._TF if t not in self._TF_ignore_mag] #TF w/ differentiable mag
|
||||||
|
self._reg_mask=[self._TF.index(t) for t in TF_mag]
|
||||||
self._reg_tgt=torch.full(size=(len(self._reg_mask),), fill_value=TF.PARAMETER_MAX) #Encourage amplitude max
|
self._reg_tgt=torch.full(size=(len(self._reg_mask),), fill_value=TF.PARAMETER_MAX) #Encourage amplitude max
|
||||||
|
|
||||||
|
#Prevent Identity
|
||||||
|
#print(TF.TF_identity)
|
||||||
|
#self._reg_tgt=torch.full(size=(len(self._reg_mask),), fill_value=0.0)
|
||||||
|
#for val in TF.TF_identity.keys():
|
||||||
|
# idx=[self._reg_mask.index(self._TF.index(t)) for t in TF_mag if t in TF.TF_identity[val]]
|
||||||
|
# self._reg_tgt[idx]=val
|
||||||
|
#print(TF_mag, self._reg_tgt)
|
||||||
|
|
||||||
def forward(self, x):
|
def forward(self, x):
|
||||||
""" Main method of the Data augmentation module.
|
""" Main method of the Data augmentation module.
|
||||||
|
@ -247,7 +256,8 @@ class Data_augV5(nn.Module): #Optimisation jointe (mag, proba)
|
||||||
else:
|
else:
|
||||||
#return reg_factor * F.l1_loss(self._params['mag'][self._reg_mask], target=self._reg_tgt, reduction='mean')
|
#return reg_factor * F.l1_loss(self._params['mag'][self._reg_mask], target=self._reg_tgt, reduction='mean')
|
||||||
mags = self._params['mag'] if self._params['mag'].shape==torch.Size([]) else self._params['mag'][self._reg_mask]
|
mags = self._params['mag'] if self._params['mag'].shape==torch.Size([]) else self._params['mag'][self._reg_mask]
|
||||||
max_mag_reg = reg_factor * F.mse_loss(mags, target=self._reg_tgt.to(mags.device), reduction='mean')
|
max_mag_reg = reg_factor * F.mse_loss(mags, target=self._reg_tgt.to(mags.device), reduction='mean') #Close to target ?
|
||||||
|
#max_mag_reg = - reg_factor * F.mse_loss(mags, target=self._reg_tgt.to(mags.device), reduction='mean') #Far from target ?
|
||||||
return max_mag_reg
|
return max_mag_reg
|
||||||
|
|
||||||
def train(self, mode=True):
|
def train(self, mode=True):
|
||||||
|
|
|
@ -10,14 +10,15 @@ if __name__ == "__main__":
|
||||||
#"res/brutus-tests/log/Aug_mod(Data_augV5(Uniform-14TFx3-MagFxSh)-LeNet)-150epochs(dataug:0)-10in_it-2.json",
|
#"res/brutus-tests/log/Aug_mod(Data_augV5(Uniform-14TFx3-MagFxSh)-LeNet)-150epochs(dataug:0)-10in_it-2.json",
|
||||||
#"res/log/Aug_mod(RandAugUDA(18TFx2-Mag1)-LeNet)-100 epochs (dataug:0)- 0 in_it.json",
|
#"res/log/Aug_mod(RandAugUDA(18TFx2-Mag1)-LeNet)-100 epochs (dataug:0)- 0 in_it.json",
|
||||||
]
|
]
|
||||||
files = ["../res/benchmark/CIFAR10/log/RandAugment(N%d-M%.2f)-%s-200 epochs -%s.json"%(3,0.17,'wide_resnet50_2', str(run)) for run in range(3)]
|
#files = ["../res/benchmark/CIFAR10/log/RandAugment(N%d-M%.2f)-%s-200 epochs -%s.json"%(3,0.17,'wide_resnet50_2', str(run)) for run in range(3)]
|
||||||
files = ["../res/benchmark/CIFAR100/log/Aug_mod(Data_augV5(Mix%.1f-14TFx%d-Mag)-%s)-200 epochs (dataug:0)- 1 in_it-%s.json"%(0.5,3,'wide_resnet50_2', str(run)) for run in range(3)]
|
#files = ["../res/benchmark/CIFAR10/log/Aug_mod(RandAug(14TFx%d-Mag%d)-%s)-200 epochs (dataug:0)- 0 in_it-%s.json"%(2,1,'resnet18', str(run)) for run in range(1)]
|
||||||
|
files = ["../res/benchmark/CIFAR10/log/Aug_mod(Data_augV5(Mix%.1f-14TFx%d-Mag)-%s)-200 epochs (dataug:0)- 3 in_it-%s.json"%(0.5,3,'resnet18', str(run)) for run in range(1)]
|
||||||
|
|
||||||
for idx, file in enumerate(files):
|
for idx, file in enumerate(files):
|
||||||
#legend+=str(idx)+'-'+file+'\n'
|
#legend+=str(idx)+'-'+file+'\n'
|
||||||
with open(file) as json_file:
|
with open(file) as json_file:
|
||||||
data = json.load(json_file)
|
data = json.load(json_file)
|
||||||
plot_resV2(data['Log'], fig_name=file.replace("/log","").replace(".json",""))#, param_names=data['Param_names'])
|
plot_resV2(data['Log'], fig_name=file.replace("/log","").replace(".json",""), param_names=data['Param_names'], f1=True)
|
||||||
#plot_TF_influence(data['Log'], param_names=data['Param_names'])
|
#plot_TF_influence(data['Log'], param_names=data['Param_names'])
|
||||||
#'''
|
#'''
|
||||||
## Loss , Acc, Proba = f(epoch) ##
|
## Loss , Acc, Proba = f(epoch) ##
|
||||||
|
|
|
@ -34,13 +34,15 @@ if __name__ == "__main__":
|
||||||
}
|
}
|
||||||
#Parameters
|
#Parameters
|
||||||
n_inner_iter = 1
|
n_inner_iter = 1
|
||||||
epochs = 2
|
epochs = 200
|
||||||
dataug_epoch_start=0
|
dataug_epoch_start=0
|
||||||
|
Nb_TF_seq=3
|
||||||
optim_param={
|
optim_param={
|
||||||
'Meta':{
|
'Meta':{
|
||||||
'optim':'Adam',
|
'optim':'Adam',
|
||||||
'lr':1e-2, #1e-2
|
'lr':1e-2, #1e-2
|
||||||
'epoch_start': 2, #0 / 2 (Resnet?)
|
'epoch_start': 2, #0 / 2 (Resnet?)
|
||||||
|
'reg_factor': 0.001,
|
||||||
},
|
},
|
||||||
'Inner':{
|
'Inner':{
|
||||||
'optim': 'SGD',
|
'optim': 'SGD',
|
||||||
|
@ -110,7 +112,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
#### Augmented Model ####
|
#### Augmented Model ####
|
||||||
if 'aug_model' in tasks:
|
if 'aug_model' in tasks:
|
||||||
tf_config='../config/base_tf_config.json'
|
tf_config='../config/invScale_wide_tf_config.json'#'../config/base_tf_config.json'
|
||||||
tf_dict, tf_ignore_mag =TF_loader.load_TF_dict(tf_config)
|
tf_dict, tf_ignore_mag =TF_loader.load_TF_dict(tf_config)
|
||||||
|
|
||||||
torch.cuda.reset_max_memory_allocated() #reset_peak_stats
|
torch.cuda.reset_max_memory_allocated() #reset_peak_stats
|
||||||
|
@ -118,15 +120,17 @@ if __name__ == "__main__":
|
||||||
t0 = time.perf_counter()
|
t0 = time.perf_counter()
|
||||||
|
|
||||||
model = Higher_model(model, model_name) #run_dist_dataugV3
|
model = Higher_model(model, model_name) #run_dist_dataugV3
|
||||||
aug_model = Augmented_model(
|
if n_inner_iter !=0:
|
||||||
Data_augV5(TF_dict=tf_dict,
|
aug_model = Augmented_model(
|
||||||
N_TF=1,
|
Data_augV5(TF_dict=tf_dict,
|
||||||
mix_dist=0.5,
|
N_TF=Nb_TF_seq,
|
||||||
fixed_prob=False,
|
mix_dist=0.5,
|
||||||
fixed_mag=False,
|
fixed_prob=False,
|
||||||
shared_mag=False,
|
fixed_mag=False,
|
||||||
TF_ignore_mag=tf_ignore_mag), model).to(device)
|
shared_mag=False,
|
||||||
#aug_model = Augmented_model(RandAug(TF_dict=tf_dict, N_TF=2), model).to(device)
|
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)
|
||||||
|
|
||||||
print("{} on {} for {} epochs - {} inner_it{}".format(str(aug_model), device_name, epochs, n_inner_iter, postfix))
|
print("{} on {} for {} epochs - {} inner_it{}".format(str(aug_model), device_name, epochs, n_inner_iter, postfix))
|
||||||
log= run_dist_dataugV3(model=aug_model,
|
log= run_dist_dataugV3(model=aug_model,
|
||||||
|
@ -134,7 +138,7 @@ if __name__ == "__main__":
|
||||||
inner_it=n_inner_iter,
|
inner_it=n_inner_iter,
|
||||||
dataug_epoch_start=dataug_epoch_start,
|
dataug_epoch_start=dataug_epoch_start,
|
||||||
opt_param=optim_param,
|
opt_param=optim_param,
|
||||||
print_freq=20,
|
print_freq=10,
|
||||||
unsup_loss=1,
|
unsup_loss=1,
|
||||||
hp_opt=False,
|
hp_opt=False,
|
||||||
save_sample_freq=None)
|
save_sample_freq=None)
|
||||||
|
|
|
@ -329,7 +329,7 @@ def run_dist_dataugV3(model, opt_param, epochs=1, inner_it=1, dataug_epoch_start
|
||||||
|
|
||||||
if(high_grad_track and i>0 and i%inner_it==0 and epoch>=opt_param['Meta']['epoch_start']): #Perform Meta step
|
if(high_grad_track and i>0 and i%inner_it==0 and epoch>=opt_param['Meta']['epoch_start']): #Perform Meta step
|
||||||
#print("meta")
|
#print("meta")
|
||||||
val_loss = compute_vaLoss(model=model, dl_it=dl_val_it, dl=dl_val) + model['data_aug'].reg_loss()
|
val_loss = compute_vaLoss(model=model, dl_it=dl_val_it, dl=dl_val) + model['data_aug'].reg_loss(opt_param['Meta']['reg_factor'])
|
||||||
#print_graph(val_loss) #to visualize computational graph
|
#print_graph(val_loss) #to visualize computational graph
|
||||||
val_loss.backward()
|
val_loss.backward()
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,13 @@ PARAMETER_MAX = 1
|
||||||
# What is the min 'level' a transform could be predicted
|
# What is the min 'level' a transform could be predicted
|
||||||
PARAMETER_MIN = 0.1
|
PARAMETER_MIN = 0.1
|
||||||
|
|
||||||
|
#Dict containing the value for wich TF are closer to identity
|
||||||
|
#TF_identity={
|
||||||
|
# PARAMETER_MAX:{'Solarize', 'Posterize'},
|
||||||
|
# PARAMETER_MAX/2:{'Contrast','Color','Brightness','Sharpness'},
|
||||||
|
# PARAMETER_MIN:{'Rotate','TranslateX','TranslateY','ShearX','ShearY'},
|
||||||
|
#}
|
||||||
|
|
||||||
class TF_loader(object):
|
class TF_loader(object):
|
||||||
""" Transformations builder.
|
""" Transformations builder.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue