def prepare_opts(): opts = Options() opts.n_examples = int(1e3) opts.n_epoch = int(5e2) opts.rnn_type = 'tanh' # no gains from relu at a short distance opts.folder = opts.rnn_type opts.network_state_size = 100 opts.max_angle_change = 90 opts.batch_size = 50 opts.label_units = 'rad' # rad, deg - rad is imprecise, but deg is useless opts.direction = 'current' # current, home # home seems to work better, but it really should work both ways opts.separate_losses = False # useless - don't do this opts.zero_start = False # helps with 2-leg (obv) opts.angle_format = 'trig' opts.output_format = 'cartesian' # polar with gap can follow existence of turns, but is very imprecise # works well in cartesian opts.velocity = True # either form works opts.stopping_prob = .3 opts.network_loss = True if opts.network_loss: opts.name = opts.rnn_type + '_awloss' else: opts.name = opts.rnn_type + '_noloss' opts.step_gap = 3 opts.use_gap = False # use of gap improves performance for polar coords, not needed in cartesian if opts.use_gap: opts.name += '_gap' # opts.name += '_{}n'.format(opts.network_state_size) print(opts.name) return opts
def show_mask_norms(mask_folder, model_name = 'gtsrb', out_png=False): options = Options() options.model_name = model_name options = justify_options_for_model(options, model_name) options.data_subset = 'validation' options.batch_size = 1 options.num_epochs = 1 options.net_mode = 'backdoor_def' options.load_mode = 'all' options.fix_level = 'all' options.build_level = 'mask_only' options.selected_training_labels = None options.gen_ori_label = False ld_paths = dict() root_folder = mask_folder print(root_folder) dirs = os.listdir(root_folder) for d in dirs: tt = d.split('_')[0] if len(tt) == 0: continue try: tgt_id = int(tt) except: continue ld_paths[tgt_id] = get_last_checkpoint_in_folder(os.path.join(root_folder,d)) print(ld_paths) model, dataset, input_list, feed_list, out_op, aux_out_op = get_output(options, model_name=model_name) model.add_backbone_saver() mask_abs = dict() config = tf.ConfigProto() config.gpu_options.allow_growth = True import cv2 init_op = tf.global_variables_initializer() local_var_init_op = tf.local_variables_initializer() table_init_ops = tf.tables_initializer() # iterator_initilizor in here with tf.Session(config=config) as sess: sess.run(init_op) sess.run(local_var_init_op) sess.run(table_init_ops) for k, v in ld_paths.items(): print(v) model.load_backbone_model(sess, v) pattern, mask = sess.run([out_op, aux_out_op]) pattern = (pattern[0]+1)/2 mask = mask[0] mask_abs[k] = np.sum(np.abs(mask)) if out_png: show_name = '%d_pattern.png'%k out_pattern = pattern*255 cv2.imwrite(show_name, out_pattern.astype(np.uint8)) show_name = '%d_mask.png'%k out_mask = mask*255 cv2.imwrite(show_name, out_mask.astype(np.uint8)) show_name = '%d_color.png'%k out_color = pattern*mask*255 cv2.imwrite(show_name, out_color.astype(np.uint8)) #cv2.imshow(show_name,out_pattern) #cv2.waitKey() #break out_norms = np.zeros([len(mask_abs),2]) z = 0 for k,v in mask_abs.items(): out_norms[z][0] = k out_norms[z][1] = v z = z+1 print('===Results===') np.save('out_norms.npy', out_norms) print('write norms to out_norms.npy') #return vs = list(mask_abs.values()) import statistics me = statistics.median(vs) abvs = abs(vs - me) mad = statistics.median(abvs) rvs = abvs / (mad * 1.4826) print(mask_abs) print(rvs) x_arr = [i for i in range(len(mask_abs))] import matplotlib.pyplot as plt plt.figure() plt.boxplot(rvs) plt.show()
def pipeline(): opts = Options() # get all present dirs opts = get_model_ix(opts) # opts.trial_time = {'no_lick': .5, 'sample': .5, 'delay': 1.5, 'test': .5, 'response': 1} opts.trial_time = { 'no_lick': .5, 'sample': .5, 'delay': 0, 'test': .5, 'response': .5 } opts.fixation = True opts.mask = True opts = get_loss_times(opts) opts.activation_fn = 'relu' opts.decay = True opts.epoch = 100 # 100-200 is enough # opts.epoch = 10 opts.batch_size = 20 opts.multilayer = False opts.layer_size = [80, 80] opts.noise = False opts.noise_intensity = .01 opts.noise_density = .5 opts.learning_rate = .001 opts.rnn_size = 100 opts.weight_alpha = .1 opts.activity_alpha = .1 run0 = opts run1 = copy.deepcopy(opts) run1.trial_time['delay'] = .5 run1 = get_loss_times(run1) run1.load_checkpoint = True run2 = copy.deepcopy(run1) run2.trial_time['delay'] = 1 run2 = get_loss_times(run2) run3 = copy.deepcopy(run1) run3.trial_time['delay'] = 1.5 run3 = get_loss_times(run3) for run in [run0, run1, run2, run3]: train.train(run) # opts.EI_in = False # opts.EI_h = False # opts.EI_out = False # opts.proportion_ex = .8 # train.eval(opts) # run_multiple(opts) return opts