Exemple #1
0
 def create_FR_after_retrain(self,
                             mode,
                             acc_loss,
                             retrain=True,
                             epochs=50,
                             lr=0.01):
     final_rec = self.create_FR_with_different_acc_loss(mode, acc_loss)
     if retrain:
         self.retrain_with_mask(final_rec, epochs=epochs, lr=lr)
     retrain_nn = NeuralNet(ckp_name_prefix=final_rec.get_retrain_prefix())
     retrain_nn.net.initialize_spatial_layers(dat.shape(), cfg.BATCH_SIZE,
                                              self.ps)
     retrain_nn.net.reset_spatial()
     retrain_nn.net.strict_mask_update(update_ids=list(
         range(len(final_rec.mask))),
                                       masks=final_rec.mask)
     if INNAS_COMP:
         test_acc = 100
         ops_saved = 100
         ops_total = 100
     else:
         _, test_acc, _ = retrain_nn.test(self.test_gen)
         ops_saved, ops_total = retrain_nn.net.num_ops()
     final_rec.retrain_update(test_acc, ops_saved, ops_total, epochs, lr)
     print(final_rec)
     save_to_file(final_rec, path=cfg.RESULTS_DIR)
def ops_saved_summery(net_name=cfg.NET.__name__,
                      dataset_name=cfg.DATA.name(),
                      mode=Mode.ALL_MODES,
                      ps='*',
                      ones_range=('*', '*'),
                      acc_loss='*',
                      gran_thresh='*',
                      init_acc='*',
                      batch_size=cfg.BATCH_SIZE,
                      max_samples=cfg.TEST_SET_SIZE):
    rec_finder = RecordFinder(net_name, dataset_name, ps, ones_range,
                              gran_thresh, acc_loss, init_acc)
    final_rec_fn = rec_finder.find_rec_filename(mode,
                                                RecordType.FINAL_RESULT_REC)
    if final_rec_fn is None:
        print('No Record found')
        return
    rec = load_from_file(final_rec_fn, '')
    print(rec)

    base_fn = 'ops_summery_' + rec.filename
    summery_fn_pkl = os.path.join(cfg.RESULTS_DIR, base_fn + '.pkl')
    if os.path.exists(summery_fn_pkl):
        arr = load_from_file(summery_fn_pkl, path='')
    else:
        nn = NeuralNet()
        data = Datasets.get(dataset_name, cfg.DATASET_DIR)
        nn.net.initialize_spatial_layers(data.shape(), cfg.BATCH_SIZE,
                                         rec.patch_size)
        test_gen, _ = data.testset(batch_size=batch_size,
                                   max_samples=max_samples)

        arr = [None] * len(rec.mask)
        for idx, layer in enumerate(rec.mask):
            nn.net.reset_spatial()
            print(
                f"----------------------------------------------------------------"
            )

            nn.net.strict_mask_update(update_ids=[idx], masks=[layer])
            _, test_acc, _ = nn.test(test_gen)
            ops_saved, ops_total = nn.net.num_ops()

            arr[idx] = (ops_saved, ops_total, test_acc)
            nn.net.print_ops_summary()

        print(
            f"----------------------------------------------------------------"
        )
        nn.net.reset_spatial()
        save_to_file(arr, use_default=False, path='', filename=summery_fn_pkl)

    out_path = os.path.join(cfg.RESULTS_DIR, base_fn + ".csv")
    with open(out_path, 'w', newline='') as f:
        csv.writer(f).writerow(['layer', 'ops_saved', 'ops_total'])
        for idx, r in enumerate(arr):
            csv.writer(f).writerow([idx, r[0], r[1]])

    return arr
Exemple #3
0
    def gen_first_lvl_results(self, mode):
        rec_filename = self.record_finder.find_rec_filename(
            mode, RecordType.FIRST_LVL_REC)
        if rec_filename is not None:
            rcs = load_from_file(rec_filename, path='')
            st_point = rcs.find_resume_point()
            if st_point is None:
                return rcs

        layers_layout = self.nn.net.generate_spatial_sizes(dat.shape())
        self._init_nn()

        if rec_filename is None:
            if self.input_patterns is None:
                rcs = Record(layers_layout, self.gran_thresh, True, mode,
                             self.init_acc, self.ps, self.ones_range)
            else:
                rcs = Record(layers_layout, self.gran_thresh, False, mode,
                             self.init_acc, self.input_patterns,
                             self.ones_range)
            st_point = [0] * 4

            if INNAS_COMP:
                rcs.filename = 'DEBUG_' + rcs.filename

        print('==> Result will be saved to ' +
              os.path.join(cfg.RESULTS_DIR, rcs.filename))
        save_counter = 0
        for layer, channel, patch, pattern_idx, mask in tqdm(
                mf.gen_masks_with_resume(self.ps,
                                         rcs.all_patterns,
                                         rcs.mode,
                                         rcs.gran_thresh,
                                         layers_layout,
                                         resume_params=st_point)):
            self.nn.net.strict_mask_update(update_ids=[layer],
                                           masks=[torch.from_numpy(mask)])

            if INNAS_COMP:
                test_acc = 100
                ops_saved = 100
                ops_total = 100
            else:
                _, test_acc, _ = self.nn.test(self.test_gen)
                ops_saved, ops_total = self.nn.net.num_ops()
                self.nn.net.reset_spatial()
            rcs.addRecord(ops_saved, ops_total, test_acc, layer, channel,
                          patch, pattern_idx)

            save_counter += 1
            if save_counter > cfg.SAVE_INTERVAL:
                save_to_file(rcs, True, cfg.RESULTS_DIR)
                save_counter = 0

        save_to_file(rcs, True, cfg.RESULTS_DIR)
        print('==> Result saved to ' +
              os.path.join(cfg.RESULTS_DIR, rcs.filename))
        return rcs
Exemple #4
0
 def save_state(self, test_acc, ops_saved, ops_total, to_file=False):
     self.resume_rec.add_algo_debug_rec(test_acc, ops_saved, ops_total)
     if test_acc >=  self.min_acc and ops_saved > self.resume_rec.curr_saved_ops:
         self.resume_rec.curr_best_acc = test_acc
         self.resume_rec.curr_saved_ops = ops_saved
         self.resume_rec.curr_tot_ops = ops_total
         self.resume_rec.curr_best_mask = [self.sp_list[l].clone() for l in range(len(self.sp_list))]
         self.resume_rec.curr_resume_index = self.resume_rec.resume_index.copy()
     if to_file:
         save_to_file(self.resume_rec, False, cfg.RESULTS_DIR, self.resume_param_filename)
         print(self.resume_param_filename)
Exemple #5
0
    def plot_ops_saved_accuracy_uniform_network(self):
        layers_layout = self.nn.net.generate_spatial_sizes(dat.shape())
        rcs = Record(layers_layout, self.gran_thresh, True, Mode.UNIFORM_LAYER,
                     self.init_acc, self.ps, self.ones_range)
        no_of_patterns = rcs.all_patterns.shape[2]
        ops_saved_array = [None] * no_of_patterns
        acc_array = [None] * no_of_patterns

        self._init_nn()
        for p_idx in range(no_of_patterns):
            sp_list = [None] * len(layers_layout)
            for layer, layer_mask in enumerate(
                    mf.base_line_mask(layers_layout,
                                      self.ps,
                                      pattern=rcs.all_patterns[:, :, p_idx])):
                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()
            self.nn.net.reset_ops()
            ops_saved_array[p_idx] = ops_saved / ops_total
            acc_array[p_idx] = test_acc

        plt.figure()
        plt.subplot(211)
        plt.plot(list(range(no_of_patterns)), ops_saved_array, 'o')
        plt.xlabel('pattern index')
        plt.ylabel('ops_saved [%]')
        plt.title(f'ops saved for uniform network, patch_size:{self.ps}')
        plt.subplot(212)
        plt.plot(list(range(no_of_patterns)), acc_array, 'o')
        plt.xlabel('pattern index')
        plt.ylabel('accuracy [%]')
        plt.title(f'accuracy for uniform network, patch_size:{self.ps}')

        data = [rcs.all_patterns, ops_saved_array, acc_array]
        save_to_file(
            data, False, cfg.RESULTS_DIR,
            'baseline_all_patterns_{cfg.NET.__name__}_{dat.name()}' +
            f'acc{self.init_acc}_ps{self.ps}_ones{self.ones_range[0]}x{self.ones_range[1]}_mg{self.gran_thresh}.pkl'
        )

        plt.savefig(
            f'{cfg.RESULTS_DIR}/baseline_all_patterns_{cfg.NET.__name__}_{dat.name()}'
            +
            f'acc{self.init_acc}_ps{self.ps}_ones{self.ones_range[0]}x{self.ones_range[1]}_mg{self.gran_thresh}.pdf'
        )

        return data
Exemple #6
0
    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)
Exemple #7
0
 def create_FR_with_different_acc_loss(self, mode, acc_loss):
     self.record_finder.max_acc_loss = '*'
     best_FR = None
     for lq_rec_fn in self.record_finder.find_all_recs_fns(
             mode, RecordType.lQ_RESUME):
         in_rec_fn = '_'.join(os.path.basename(lq_rec_fn).split('_')[2:])
         lq = LayerQuantizier(
             load_from_file(in_rec_fn, path=cfg.RESULTS_DIR), self.init_acc,
             self.max_acc_loss, self.ps, self.ones_range,
             self.get_total_ops(), lq_rec_fn)
         final_rec = lq.find_final_mask(acc_loss,
                                        nn=self.nn,
                                        test_gen=self.test_gen)
         if best_FR is None:
             best_FR = final_rec
         elif best_FR.ops_saved < final_rec.ops_saved:
             best_FR = final_rec
     print(best_FR)
     save_to_file(best_FR, True, cfg.RESULTS_DIR)
     print('==> result saved to ' + best_FR.filename)
     self.record_finder.max_acc_loss = self.max_acc_loss
     return best_FR
Exemple #8
0
 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
         
     
Exemple #9
0
 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
Exemple #10
0
 def save_state(self):
     save_to_file(self.output_rec, True, cfg.RESULTS_DIR)