def stability_experiment_dropoutprior( images_file='alexnet_val_2k_top1_correct.txt', advex_subdir='alexnet_val_2k_top1_correct/deepfool_oblivious/', attack_name='deepfool', nodrop_train=False): if nodrop_train: imgprior = get_default_prior(mode='dropout_nodrop_train1024') else: imgprior = get_default_prior(mode='dropout1024') optimizer = 'adam' learning_rate = 0.6 n_iterations = 10 log_freq = 1 imgprior.activate_dropout = False log_path = '../logs/adversarial_examples/alexnet_top1/{}/oblivious_dropoutprior_nodrop_train1024/lr06/'.format( attack_name) # noinspection PyTypeChecker stability_experiment(images_file=images_file, advex_subdir=advex_subdir, imgprior=imgprior, optimizer=optimizer, learning_rate=learning_rate, n_iterations=n_iterations, log_freq=log_freq, log_path=log_path)
def stability_experiment_fullprior_adaptive( images_file='alexnet_val_2k_top1_correct.txt', advex_subdir='alexnet_val_2k_top1_correct/deepfool_adaptive_full512/'): imgprior = get_default_prior(mode='full512') optimizer = 'adam' learning_rate = 0.6 n_iterations = 30 log_freq = 1 log_path = '../logs/adversarial_examples/alexnet_top1/deepfool/adaptive_fullprior512/lr06long/' # noinspection PyTypeChecker stability_experiment(images_file=images_file, advex_subdir=advex_subdir, imgprior=imgprior, optimizer=optimizer, learning_rate=learning_rate, n_iterations=n_iterations, log_freq=log_freq, log_path=log_path)
def c1l_prior_tranferable_stability_experiment( images_file='alexnet_val_2k_top1_correct.txt', advex_subdir='alexnet_val_2k_top1_correct/deepfool_adaptive_full512/', attack_name='deepfool', prior_mode='fullc1l6000'): imgprior = get_default_prior(mode=prior_mode) optimizer = 'adam' learning_rate = 0.6 n_iterations = 30 log_freq = 1 log_path = '../logs/adversarial_examples/alexnet_top1/{}/transfer_{}/lr06/'.format( attack_name, prior_mode) # noinspection PyTypeChecker stability_experiment(images_file=images_file, advex_subdir=advex_subdir, imgprior=imgprior, optimizer=optimizer, learning_rate=learning_rate, n_iterations=n_iterations, log_freq=log_freq, log_path=log_path)
def stability_experiment_fullprior( images_file='alexnet_val_2k_top1_correct.txt', advex_subdir='alexnet_val_2k_top1_correct/deepfool_oblivious/', attack_name='deepfool'): imgprior = get_default_prior(mode='full512') optimizer = 'adam' # learning_rate = 1e-0 # n_iterations = 100 # log_freq = list(range(1, 5)) + list(range(5, 50, 5)) + list(range(50, 101, 10)) learning_rate = 0.6 n_iterations = 30 log_freq = 1 log_path = '../logs/adversarial_examples/alexnet_top1/{}/oblivious_fullprior512/lr06long/'.format( attack_name) # noinspection PyTypeChecker stability_experiment(images_file=images_file, advex_subdir=advex_subdir, imgprior=imgprior, optimizer=optimizer, learning_rate=learning_rate, n_iterations=n_iterations, log_freq=log_freq, log_path=log_path)
# name=None, load_name=None, dir_name=None, load_tensor_names='image') # p = FoESeparablePrior('rgb_scaled:0', 1e-10, 'alexnet', [9, 9], 1.0, n_components=500, n_channels=3, # n_features_per_channel_white=9**2, # dim_multiplier=50, share_weights=False, channelwise_data=True, # dist='student', mean_mode='gc', sdev_mode='gc', whiten_mode='zca', # name=None, load_name=None, dir_name=None, load_tensor_names=None) # dropout_prior = get_default_prior('dropout1024', custom_weighting=1e-8) # img_prior2 = get_default_prior('full512logistic', custom_weighting=1e-2) layer = 5 jitter_t = 4 # 1:1, 2:2, 3,4,5:4, 6,7,8:8 weighting = '1e-4' make_mse = False restart_adam = False img_prior = get_default_prior('full512', custom_weighting=float(weighting)) if layer == 'softmax': split = SplitModule(name_to_split='softmax:0', img_slice_name='smx_img', rec_slice_name='smx_rec', name='Split_smx') mse = MSELoss(target='smx_img' + ':0', reconstruction='smx_rec' + ':0', name='MSE_smx') subdir = '8x8_gc_gc_student/{}/jitter_bound_plots/'.format(weighting) log_dir = '../logs/opt_inversion/alexnet/img_prior_comp/val153_check/' cutoff = None else: subdir = '8x8_gc_gc_student/{}/jitter_bound_plots/'.format(weighting) log_dir = '../logs/opt_inversion/alexnet/img_prior_comp/c{}l_to_img/'.format( layer)
def featmap_inv(match_layer, target_layer, image_name, prior_id, prior_weighting, make_mse=False, restart_adam=False, pre_image=True, do_plot=True, jitter_t=0, jitter_stop_point=3200, lr=1., bound_plots=True, custom_target=None): do_plot = do_plot if pre_image else False pre_img_subdir = 'pre_img' if pre_image else 'featmap' prior_subdir = 'no_prior' if make_mse else prior_id subdir = '{}/{}/'.format(prior_subdir, prior_weighting) log_dir = '../logs/opt_inversion/alexnet/featmap_rec/{}l_to_{}l/{}/'.format(match_layer, target_layer, pre_img_subdir) cutoff = 'conv{}/lin'.format(match_layer) if match_layer < 6 else None split_match, mse_match = lin_split_and_mse(match_layer, add_loss=True, mse_name='') modules = [split_match, mse_match] if pre_image: pre_featmap_name = 'input' split_target, mse_target = lin_split_and_mse(target_layer, add_loss=False, mse_name='MSE_{}l_reconstruction'.format(target_layer)) pre_mse = MSELoss(target='target_featmap/read:0', reconstruction='pre_featmap/read:0', name='MSE_pre_image') pre_mse.add_loss = False modules.extend([split_target, mse_target, pre_mse]) prior_target = custom_target or split_target.rec_slice_name + ':0' prior = get_default_prior(prior_id, custom_weighting=float(prior_weighting), custom_target=prior_target) else: pre_featmap_name = 'conv{}/lin'.format(target_layer) if target_layer < 6 \ else 'fc{}/lin'.format(target_layer) pre_mse = MSELoss(target='target_featmap/read:0', reconstruction='pre_featmap/read:0', name='MSE_{}l_reconstruction'.format(target_layer)) pre_mse.add_loss = False modules.extend([pre_mse]) prior_target = custom_target or 'pre_featmap/read:0' prior = get_default_prior(prior_id, custom_weighting=float(prior_weighting), custom_target=prior_target) if not make_mse: modules.append(prior) log_path = log_dir + subdir pre_featmap_init = np.load(log_dir + 'pure_mse/mats/rec_10000.npy') else: log_path = log_dir + 'pure_mse/' pre_featmap_init = None ni = NetInversion(modules, log_path, classifier='alexnet', summary_freq=10, print_freq=50, log_freq=500) if not os.path.exists(log_path): os.makedirs(log_path) copyfile('./foe_inv_featmap_script.py', log_path + 'script.py') target_image = '../data/selected/images_resized_227/{}.bmp'.format(image_name) first_n_iterations = 500 if restart_adam else 10000 ni.train_pre_featmap(target_image, n_iterations=first_n_iterations, grad_clip=10000., lr_lower_points=((1e+0, lr),), jitter_t=jitter_t, range_clip=False, bound_plots=bound_plots, optim_name='adam', save_as_plot=do_plot, jitter_stop_point=jitter_stop_point, pre_featmap_init=pre_featmap_init, ckpt_offset=0, pre_featmap_name=pre_featmap_name, classifier_cutoff=cutoff, featmap_names_to_plot=(), max_n_featmaps_to_plot=10) if restart_adam: pre_featmap_init = np.load(ni.log_path + 'mats/rec_500.npy') for mod in ni.modules: if isinstance(mod, LossModule): mod.reset() ni.train_pre_featmap(target_image, n_iterations=9500, grad_clip=10000., optim_name='adam', lr_lower_points=((1e+0, lr),), jitter_t=jitter_t, range_clip=False, bound_plots=bound_plots, pre_featmap_init=pre_featmap_init, ckpt_offset=500, jitter_stop_point=jitter_stop_point, pre_featmap_name=pre_featmap_name, classifier_cutoff=cutoff, featmap_names_to_plot=(), max_n_featmaps_to_plot=10, save_as_plot=do_plot)
def run_image_opt_inversions(classifier, prior_mode, layer_select=None, lr=1., pre_featmap_name='input', do_plot=True, mse_iterations=5000, opt_iterations=5000, jitterations=3200, summary_freq=50, print_freq=500, log_freq=500, grad_clip=10000., select_img=None, custom_images=None): _, img_hw, layer_names = classifier_stats(classifier) if layer_select is not None: layer_names = [n for n in layer_names if layer_select in n] tgt_paths = subset10_paths(classifier) layer_subdirs = [n.replace('/', '_') for n in layer_names] img_subdirs = ['val{}'.format(i) for i in selected_img_ids()] if select_img is not None: tgt_paths = tgt_paths[select_img:select_img+1] img_subdirs = img_subdirs[select_img:select_img+1] if custom_images is not None: tgt_paths, img_subdirs = custom_images log_path = '../logs/opt_inversion/{}/image_rec/'.format(classifier) print(layer_names) for idx, layer_subdir in enumerate(layer_subdirs): cutoff = None # layer_names[idx] if layer_names[idx].startswith('conv') else None jitter_t, weight = get_imagerec_jitter_and_prior_weight(classifier, layer_names[idx]) print('jitter', jitter_t, 'prior_weight', weight) for idy, img_subdir in enumerate(img_subdirs): target_image = tgt_paths[idy] exp_log_path = '{}{}/{}/'.format(log_path, layer_subdir, img_subdir) if not os.path.exists(log_path): os.makedirs(log_path) lr_lower_points = ((1e+0, lr),) print(layer_subdir) split = SplitModule(name_to_split=layer_names[idx] + ':0', img_slice_name=layer_subdir + '_img', rec_slice_name=layer_subdir + '_rec') feat_mse = MSELoss(target=layer_subdir + '_img:0', reconstruction=layer_subdir + '_rec:0', name='MSE_' + layer_subdir) img_mse = MSELoss(target='target_featmap/read:0', reconstruction='pre_featmap/read:0', name='MSE_Reconstruction') img_mse.add_loss = False modules = [split, feat_mse, img_mse] pure_mse_path = exp_log_path + 'pure_mse/' ni = NetInversion(modules, pure_mse_path, classifier=classifier, summary_freq=summary_freq, print_freq=print_freq, log_freq=log_freq) pre_featmap_init = None ni.train_pre_featmap(target_image, n_iterations=mse_iterations, grad_clip=grad_clip, lr_lower_points=lr_lower_points, jitter_t=jitter_t, range_clip=False, bound_plots=True, optim_name='adam', save_as_plot=do_plot, jitter_stop_point=jitterations, pre_featmap_init=pre_featmap_init, ckpt_offset=0, pre_featmap_name=pre_featmap_name, classifier_cutoff=cutoff, featmap_names_to_plot=(), max_n_featmaps_to_plot=10) for mod in modules: if isinstance(mod, LossModule): mod.reset() prior = get_default_prior(prior_mode, custom_weighting=weight, custom_target='pre_featmap:0') modules = [split, feat_mse, img_mse, prior] prior_path = exp_log_path + prior_mode + '/' ni = NetInversion(modules, prior_path, classifier=classifier, summary_freq=summary_freq, print_freq=print_freq, log_freq=log_freq) pre_featmap_init = np.load(pure_mse_path + '/mats/rec_{}.npy'.format(mse_iterations)) ni.train_pre_featmap(target_image, n_iterations=opt_iterations, grad_clip=grad_clip, lr_lower_points=lr_lower_points, jitter_t=jitter_t, range_clip=False, bound_plots=True, optim_name='adam', save_as_plot=do_plot, jitter_stop_point=mse_iterations + jitterations, pre_featmap_init=pre_featmap_init, ckpt_offset=mse_iterations, pre_featmap_name=pre_featmap_name, classifier_cutoff=cutoff, featmap_names_to_plot=(), max_n_featmaps_to_plot=10)