def generate_set(img, n, wl = None): """ Group metadata includes: - `ys` --- Label 1 or 0 indicating target presence or absence, shape: (n,) corresponding for first dim of images array. Arguments --------- - `img` --- Path to HDF5 archive of distilled detection dask. - `n` --- Number of images per category. - `wl` --- Whitelist of category names to include. If None, all categories will be used. """ four_task = det.DistilledDetectionTask( img, whitelist = wl) imgs, ys, _, _ = four_task.val_set(None, n) group_info = [ (c, {'ys': ys[c]}) for c in imgs] return group_info, lambda: imgs.values()
import importlib.util spec = importlib.util.spec_from_file_location("link_libs", "/Users/kaifox/projects/art_physio/code/script/link_libs_kfmbp.py") link_libs = importlib.util.module_from_spec(spec) spec.loader.exec_module(link_libs) import proc.detection_task as det from proc import video_gen import matplotlib.pyplot as plt import skimage.io import sys PATH = sys.argv[1] CAT = sys.argv[2] IMG = int(sys.argv[3]) OUT = sys.argv[4] task = det.DistilledDetectionTask(PATH) val_imgs, val_ys, _ = task.train_set(CAT, IMG) print(video_gen.to_numpy(val_imgs)[-1].shape) skimage.io.imsave(OUT, video_gen.to_numpy(val_imgs)[-1])
# ================================================= # # Unit Activations & Decision Gradients # # ================================================= # rf_fname = os.path.join(DATA_OUT, "rfs_noatt.csv") units, _ = lsq_fields.load_rf_csv(rf_fname) # Fit logistic regressions print("Fitting Regressions") iso_task = det.DistilledDetectionTask( ISO_PATH, whitelist = CAT_WHITELIST) _, _, regs, _, _ = det.fit_logregs( model, DECODERS, iso_task, train_size = TRAIN_N, shuffle = False) # Unit gradients with NO attention print("Unit effects without attention") four_task = det.DistilledDetectionTask( FOUR_PATH, whitelist = CAT_WHITELIST) val_imgs, val_ys, _ = four_task.val_set(None, TEST_N) ugrads, uacts = det.voxel_decision_grads( units, model, DECODERS, val_imgs, val_ys, regs) # Output data fname = os.path.join(DATA_OUT, '{}_noatt.csv') save_bhv_grads(fname.format('ugrads'), units, ugrads)
def main(): # Load input files if args.model is None: from proc import cornet model, _ = cornet.load_cornet("Z") else: spec = importlib.util.spec_from_file_location("model", args.model) model_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(model_module) model = model_module.get_model() if args.regs is not None: regs = det.load_logregs(args.regs) if args.nodata: four_task = det.FakeDetectionTask(args.cats, 224, 3) else: four_task = det.DistilledDetectionTask(args.comp_images_path, whitelist=args.cats) imgs, ys, _, img_ixs = four_task.val_set(None, args.test_n) if args.attn is not None: kws = atts.load_cfg(args.attn_cfg) att_mods = atts.load_model(args.attn, **kws) print("loading attention", args.attn) else: att_mods = {} print("no attention", args.attn) # Ensure model will be un on GPU if requested if args.cuda: model.cuda() # Don't treat categories separately # Setup outputs fn_outputs = h5py.File(args.output_path, 'w') # ====== Run behavior task ====== for c in four_task.cats: print("Category:", c) # Save true outputs so we don't have to load the dataset later ys_ds = fn_outputs.create_dataset(f'{c}_y', (len(imgs[c]), ), np.int8) ys_ds[...] = ys[c] ys_ds.attrs['cat_names'] = np.array(list(regs.keys())).astype('S') # record index of image in the dataset ix_ds = fn_outputs.create_dataset(f'{c}_ix', (len(imgs[c]), ), np.uint32) ix_ds[...] = img_ixs[c] # Create a place to store the eventual scores fn_ds = fn_outputs.create_dataset(f'{c}_fn', (len(imgs[c]), ), np.float32) batches = (np.arange(len(imgs[c])), ) if args.batch_size > 0: if len(imgs[c]) > args.batch_size: n_batch = np.ceil(len(imgs[c]) / args.batch_size) batches = np.array_split(batches[0], n_batch) # NOTE: code assumes we'll still see images in expected order for batch_n, batch_ix in enumerate(batches): print(f"Batch {batch_n+1} / {len(batches)}") run_batch(model, imgs[c], batch_ix, att_mods, fn_ds, regs[c]) cleanup() fn_outputs.close()
def main(): # Load input files if args.model is None: from proc import cornet model, _ = cornet.load_cornet("Z") else: spec = importlib.util.spec_from_file_location("model", args.model) model_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(model_module) model = model_module.get_model() units = lsq_fields.load_units_from_csv(args.unit_path) if args.regs is not None: regs = det.load_logregs(args.regs) else: regs = None if args.nodata: four_task = det.FakeDetectionTask(args.cats, 224, 3) else: four_task = det.DistilledDetectionTask(args.comp_images_path, whitelist=args.cats) imgs, ys, _ = four_task.val_set(None, args.test_n) if args.attn is not None: kws = atts.load_cfg(args.attn_cfg) att_mods = atts.load_model(args.attn, **kws) print("loading attention", args.attn) else: att_mods = {} print("no attention", args.attn) print(att_mods) # Ensure model will be un on GPU if requested if args.cuda: model.cuda() # Don't treat categories separately cat_names = list(imgs.keys()) cat_ids = np.concatenate( [np.repeat(i_cat, len(imgs[c])) for i_cat, c in enumerate(cat_names)]) cat_img_ixs = [ # Within-category image counter np.cumsum(cat_ids == i) - 1 for i in range(len(cat_names)) ] cat_ns = {c: len(imgs[c]) for c in imgs} ys = np.concatenate([a for a in ys.values()]) imgs = torch.cat([a for a in imgs.values()]) # Setup outputs # We have to wait to initialize the hdf5 arrays until we # know what size the wrt layers will be but can setup the # file pointer now. img_count = 0 if not args.no_back: outputs = h5py.File(args.output_path, 'w') else: outputs = None if args.regs is not None: fn_output_fname = os.path.join( os.path.dirname(args.output_path), 'fn_' + os.path.basename(args.output_path)) fn_outputs = h5py.File(fn_output_fname, 'w') # Save image metadata along with behavioral outputs ids_ds = fn_outputs.create_dataset('cat_ids', (cat_ids.size, ), np.int8) ids_ds[...] = cat_ids ids_ds.attrs['cat_names'] = np.array(cat_names).astype('S') ys_ds = fn_outputs.create_dataset('true_ys', (cat_ids.size, ), np.int8) ys_ds[...] = ys else: fn_outputs = None # Adjust batch size to processor if requested: if args.batch_size == int(args.batch_size) or not args.cuda: args.batch_size = int(args.batch_size) else: tot_mem = torch.cuda.get_device_properties(0).total_memory / (1024**3) args.batch_size = int(args.batch_size * tot_mem) print(f"[bp] tot_mem {tot_mem} batch_size {args.batch_size}") # ====== Run backprop ====== batches = (np.arange(len(imgs)), ) if args.batch_size > 0: if len(imgs) > args.batch_size: n_batch = np.ceil(len(imgs) / args.batch_size) batches = np.array_split(batches[0], n_batch) # NOTE: code assumes we'll still see images in expected order for batch_n, batch_ix in enumerate(batches): if args.verbose: print(f"Batch {batch_n+1} / {len(batches)}") run_batch(model, imgs, batch_ix, units, att_mods, fn_outputs, outputs, regs, cat_names, cat_ids, cat_img_ixs, cat_ns) cleanup() if not args.no_back: outputs.close() if args.regs is not None: fn_outputs.close()
def run_and_save_performance( model, units, att_gens, BETAS, ISO_PATH, CAT_WHITELIST, DECODERS, TRAIN_N, FOUR_PATH, TEST_N, UNIT_LAYERS, DATA_OUT, BATCH_SIZE, no_grads = False, do_noatt = False): # Fit logistic regressions print("Fitting Regressions") iso_task = det.DistilledDetectionTask( ISO_PATH, whitelist = CAT_WHITELIST) #iso_task = det.FakeDetectionTask(CAT_WHITELIST, 3, 224) _, _, regs, _, _ = det.fit_logregs( model, DECODERS, iso_task, train_size = TRAIN_N, shuffle = False) four_task = det.DistilledDetectionTask( FOUR_PATH, whitelist = CAT_WHITELIST) #four_task = det.FakeDetectionTask(CAT_WHITELIST, 3, 224) val_imgs, val_ys, _ = four_task.val_set(None, TEST_N) # Unit gradients with NO attention if do_noatt: print("Task with attention: [ None ]") noatt_results = det.voxel_decision_grads( units, model, DECODERS, val_imgs, val_ys, regs, no_grads = no_grads, batch_size = BATCH_SIZE) # Generate a table of results for later calls to add to df = exp.csv_template(units, four_task.cats, UNIT_LAYERS, TEST_N) exp.append_results(noatt_results, df, "noatt") df.to_csv( os.path.join(DATA_OUT, 'bhv_noatt.csv'), index = False, float_format = '%g') # Clean up del noatt_results, _; gc.collect() # Unit gradients WITH attention for (att_name, att_mod_gen), att_b in (iit.product(att_gens.items(), BETAS)): print(f"Task with attention: [ {att_name} : {att_b} ]") # Parse out the attention model into a dict of network mods # and an effective 'layer'. The requisite layer is mainly for # backwards compatibility. att_mod = att_mod_gen(att_b) # Set up data saving callback condition = f"a{att_name}_b{att_b}" df = exp.csv_template(units, four_task.cats, UNIT_LAYERS, TEST_N) data_cb = append_results_intermediate(df, condition) att_results = det.voxel_decision_grads( units, model, DECODERS, val_imgs, val_ys, regs, mods = att_mod, no_grads = no_grads, batch_size = BATCH_SIZE, batch_callback = data_cb) # Merge data with other conditions df.to_csv( os.path.join(DATA_OUT, f'bhv_{condition}.csv'), index = False, float_format = '%g') # Clean up del att_results; gc.collect()
parser.add_argument( "--cats", nargs='*', default=[], help='If given, a whitelist for categories to train regressors for.') parser.add_argument('--verbose', action="store_true", help='Run with extra progress output.') args = parser.parse_args() args.decoders = [eval('tuple(' + l + ')') for l in args.decoders] if len(args.cats) == 0: args.cats = None if args.model is None: model, _ = cornet.load_cornet("Z") else: spec = importlib.util.spec_from_file_location("model", args.model) model_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(model_module) model = model_module.get_model() iso_task = det.DistilledDetectionTask(args.iso_images_path, whitelist=args.cats) #iso_task = det.FakeDetectionTask(args.cats, 3, 224) _, _, regs, _, _ = det.fit_logregs(model, args.decoders, iso_task, train_size=args.train_n, shuffle=False, verbose=args.verbose) det.save_logregs(args.output_path, regs)