def patch3x3(patterns_idx): ones_range = (patterns_idx, patterns_idx + 1) gran = 10 eval_baseline_and_runtimes(3, ones_range, gran, patterns_idx=patterns_idx) acc_loss = [10] run_all_acc_loss_possibilities(3, ones_range, gran, Mode.UNIFORM_LAYER, acc_loss_opts=acc_loss, patterns_idx=patterns_idx) run_all_acc_loss_possibilities(3, ones_range, gran, Mode.UNIFORM_FILTERS, acc_loss_opts=acc_loss, patterns_idx=patterns_idx) run_all_acc_loss_possibilities(3, ones_range, gran, Mode.UNIFORM_PATCH, acc_loss_opts=acc_loss, patterns_idx=patterns_idx) plotting.plot_ops_saved_vs_max_acc_loss(cfg.NET.__name__, dat.name(), 3, ones_range, gran, acc_loss, 93.5)
def __init__(self, resume=True, ckp_name_prefix=None): # Decide on device: if torch.cuda.is_available(): # print('CUDA FOUND!') self.device = torch.device('cuda') cudnn.benchmark = True if torch.cuda.device_count() > 1: raise NotImplementedError # This line enables multiple GPUs, but changes the layer names a bit # self.net = torch.nn.DataParallel(self.net) # Useful if you have multiple GPUs - does not hurt otherwise else: self.device = torch.device('cpu') # torch.set_num_threads(4) # Presuming 4 cores print('WARNING: Found no valid GPU device - Running on CPU') # Build Model: print(f'==> Building model {net.__name__} on the dataset {dat.name()}') self.net = net(self.device, dat.num_classes(), dat.input_channels(), dat.shape()) print(f'==> Detected family model of {self.net.family_name()}') if resume: print( f'==> Resuming from checkpoint via sorting method: {cfg.RESUME_METHOD}' ) assert os.path.isdir( cfg.CHECKPOINT_DIR), 'Error: no checkpoint directory found!' ck_file = self.__class__.resume_methods[cfg.RESUME_METHOD]( self.net.family_name(), ckp_name_prefix=ckp_name_prefix) if ck_file is None: print( f'-E- Found no valid checkpoints for {net.__name__} on {dat.name()}' ) self.best_val_acc = 0 self.start_epoch = 0 else: checkpoint = torch.load(ck_file, map_location=self.device) self._load_checkpoint(checkpoint['net']) self.best_val_acc = checkpoint['acc'] self.start_epoch = checkpoint['epoch'] assert (dat.name() == checkpoint['dataset']) print( f'==> Loaded model with val-acc of {self.best_val_acc:.3f}' ) else: self.best_val_acc = 0 self.start_epoch = 0 self.net = self.net.to(self.device) # Build SGD Algorithm: self.criterion = torch.nn.CrossEntropyLoss() self.train_gen, self.val_gen, self.classes = (None, None, None) self.optimizer = None
def main_plot_ops_saved_vs_max_acc_loss(ps, ones_range, gran_th, title=None): init_acc = get_init_acc(ps, ones_range, gran_th) run_all_acc_loss_possibilities(ps, ones_range, gran_th, Mode.UNIFORM_LAYER) run_all_acc_loss_possibilities(ps, ones_range, gran_th, Mode.UNIFORM_FILTERS) run_all_acc_loss_possibilities(ps, ones_range, gran_th, Mode.UNIFORM_PATCH) plotting.plot_ops_saved_vs_max_acc_loss(cfg.NET.__name__, dat.name(), ps, ones_range, gran_th, ACC_LOSS_OPTS, init_acc, title=title) run_all_acc_loss_possibilities(ps, ones_range, gran_th, Mode.MAX_GRANULARITY) plotting.plot_ops_saved_vs_max_acc_loss(cfg.NET.__name__, dat.name(), ps, ones_range, gran_th, ACC_LOSS_OPTS, init_acc)
def main_plot_ops_saved_vs_ones(mode): ps = 3 ones_possibilities = range(3, 8) init_acc = get_init_acc(ps, (ones_possibilities[0], ones_possibilities[1]), GRANULARITY_TH) run_all_ones_possibilities(ps, ones_possibilities, GRANULARITY_TH, ACC_LOSS) plotting.plot_ops_saved_vs_ones(cfg.NET.__name__, dat.name(), ps, ones_possibilities, GRANULARITY_TH, ACC_LOSS, init_acc, mode)
def _checkpoint(self, val_acc, epoch, ckp_name_prefix=None): # Decide on whether to checkpoint or not: save_it = val_acc > self.best_val_acc if save_it and cfg.DONT_SAVE_REDUNDANT: # target = os.path.join(cfg.CHECKPOINT_DIR, f'{self.net.family_name()}_{dat.name()}_*_ckpt.t7') # checkpoints = [os.path.basename(f) for f in glob.glob(target)] # if ckp_name_prefix is not None: # target = os.path.join(cfg.CHECKPOINT_DIR, f'{self.net.family_name()}_{dat.name()}_*_ckpt_{ckp_name_prefix}.t7') # checkpoints += [os.path.basename(f) for f in glob.glob(target)] if ckp_name_prefix is None: target = os.path.join( cfg.CHECKPOINT_DIR, f'{self.net.family_name()}_{dat.name()}_*_ckpt.t7') else: target = os.path.join( cfg.CHECKPOINT_DIR, f'{self.net.family_name()}_{dat.name()}_*_ckpt_{ckp_name_prefix}.t7' ) checkpoints = [os.path.basename(f) for f in glob.glob(target)] if checkpoints: best_cp_val_acc = max([ float( f.replace(f'{self.net.family_name()}_{dat.name()}', '').split('_')[1]) for f in checkpoints ]) if best_cp_val_acc >= val_acc: save_it = False print( f'\nResuming without save - Found valid checkpoint with higher val_acc: {best_cp_val_acc}' ) # Do checkpoint val_acc = round(val_acc, 3) # Don't allow too long a number if save_it: print( f'\nBeat val_acc record of {self.best_val_acc} with {val_acc} - Saving checkpoint' ) state = { 'net': self.net.state_dict(), 'dataset': dat.name(), 'acc': val_acc, 'epoch': epoch, } if not os.path.isdir(cfg.CHECKPOINT_DIR): os.mkdir(cfg.CHECKPOINT_DIR) if ckp_name_prefix is None: cp_name = f'{self.net.family_name()}_{dat.name()}_{val_acc}_ckpt.t7' else: cp_name = f'{self.net.family_name()}_{dat.name()}_{val_acc}_ckpt_{ckp_name_prefix}.t7' torch.save(state, os.path.join(cfg.CHECKPOINT_DIR, cp_name)) self.best_val_acc = val_acc
def main_1x3_ones(): acc_loss = [3.5] eval_baseline_and_runtimes(2, (1, 3), 10) run_all_acc_loss_possibilities(2, (1, 3), 10, Mode.UNIFORM_LAYER, acc_loss_opts=acc_loss) run_all_acc_loss_possibilities(2, (1, 3), 10, Mode.UNIFORM_PATCH, acc_loss_opts=acc_loss) run_all_acc_loss_possibilities(2, (1, 3), 10, Mode.UNIFORM_FILTERS, acc_loss_opts=acc_loss) plotting.plot_ops_saved_vs_max_acc_loss(cfg.NET.__name__, dat.name(), 2, (1, 3), 10, acc_loss, 93.5) run_all_acc_loss_possibilities(2, (1, 3), 10, Mode.MAX_GRANULARITY, acc_loss_opts=acc_loss) plotting.plot_ops_saved_vs_max_acc_loss(cfg.NET.__name__, dat.name(), 2, (1, 3), 10, acc_loss, 93.5)
def _save_final_rec(self): self.resume_rec.save_csv(self.resume_param_filename) save_to_file(self.resume_rec, False, cfg.RESULTS_DIR, self.resume_param_filename) if self.resume_rec.curr_tot_ops == 0: print(f'==> No suitable Option was found for min accuracy of {self.min_acc}') return f_rec = FinalResultRc(self.min_acc + self.max_acc_loss, self.resume_rec.curr_best_acc, self.resume_rec.curr_saved_ops, self.resume_rec.curr_tot_ops, self.mode, self.resume_rec.curr_best_mask, self.patch_size, self.max_acc_loss, self.ones_range, cfg.NET.__name__, dat.name(), self.layers_layout) save_to_file(f_rec,True,cfg.RESULTS_DIR) print('==> result saved to ' + f_rec.filename) self.resume_rec.mark_finished(mark_all=True) save_to_file(self.resume_rec, False, cfg.RESULTS_DIR, self.resume_param_filename) return f_rec
def base_line_result(self): layers_layout = self.nn.net.generate_spatial_sizes(dat.shape()) self._init_nn() sp_list = [None] * len(layers_layout) for layer, layer_mask in enumerate( mf.base_line_mask(layers_layout, self.ps)): sp_list[layer] = torch.from_numpy(layer_mask) self.nn.net.strict_mask_update(update_ids=list( range(len(layers_layout))), masks=sp_list) _, test_acc, _ = self.nn.test(self.test_gen) ops_saved, ops_total = self.nn.net.num_ops() bl_rec = BaselineResultRc(self.init_acc, test_acc, ops_saved, ops_total, self.ps, cfg.NET.__name__, dat.name()) print(bl_rec) save_to_file(bl_rec, True, cfg.RESULTS_DIR)
def __init__(self, patch_size, ones_range, gran_thresh, max_acc_loss, init_acc=None, test_size=cfg.TEST_SET_SIZE, patterns_idx=None): self.ps = patch_size self.max_acc_loss = max_acc_loss self.gran_thresh = gran_thresh if patterns_idx is None: self.ones_range = ones_range self.input_patterns = None else: patterns_rec = load_from_file( f'all_patterns_ps{self.ps}_cluster{patterns_idx}.pkl', path=cfg.RESULTS_DIR) self.ones_range = (patterns_rec[1], patterns_rec[1] + 1) self.input_patterns = patterns_rec[2] self.full_net_run_time = None self.total_ops = None self.nn = NeuralNet() self.nn.net.initialize_spatial_layers(dat.shape(), cfg.BATCH_SIZE, self.ps) self.test_gen, _ = dat.testset(batch_size=cfg.BATCH_SIZE, max_samples=cfg.TEST_SET_SIZE) self.test_set_size = cfg.TEST_SET_SIZE if INNAS_COMP: init_acc = DEBUG_INIT_ACC if init_acc is None: _, test_acc, correct = self.nn.test(self.test_gen) print(f'==> Asserted test-acc of: {test_acc} [{correct}]\n ') self.init_acc = test_acc # TODO - Fix initialize bug else: self.init_acc = init_acc self.record_finder = RecordFinder(cfg.NET.__name__, dat.name(), patch_size, ones_range, gran_thresh, max_acc_loss, self.init_acc)
def main_ones(ones_range, acc_loss=None): eval_baseline_and_runtimes(2, ones_range, 10) if acc_loss is None: acc_loss = [1, 3.5, 5] run_all_acc_loss_possibilities(2, ones_range, 10, Mode.UNIFORM_LAYER, acc_loss_opts=acc_loss) run_all_acc_loss_possibilities(2, ones_range, 10, Mode.UNIFORM_PATCH, acc_loss_opts=acc_loss) run_all_acc_loss_possibilities(2, ones_range, 10, Mode.UNIFORM_FILTERS, acc_loss_opts=acc_loss) plotting.plot_ops_saved_vs_max_acc_loss(cfg.NET.__name__, dat.name(), 2, ones_range, 10, acc_loss, 93.5)
def find_final_mask(self, max_acc_loss, nn=None, test_gen=None, should_save=False): init_acc = self.max_acc_loss + self.min_acc new_min_acc = init_acc - max_acc_loss if not self.resume_rec.is_finised(): print('Simulation not finished!') if nn is not None and test_gen is not None: self.simulate(nn, test_gen) final_mask_indexes, best_ops_saved, best_acc = self.resume_rec.find_best_mask(new_min_acc) if final_mask_indexes is None: print(f'==> No suitable Option was found for min accuracy of {new_min_acc}') return None self.sp_list = [None] * len(final_mask_indexes) for l_idx, p_idx in enumerate(final_mask_indexes): self._update_layer( l_idx, p_idx) f_rec = FinalResultRc(init_acc, best_acc, best_ops_saved, self.resume_rec.curr_tot_ops, self.mode, self.sp_list, self.patch_size, max_acc_loss, self.ones_range, cfg.NET.__name__, dat.name(), self.layers_layout) if should_save: save_to_file(f_rec,True,cfg.RESULTS_DIR) print('==> result saved to ' + f_rec.filename) return f_rec