def info_tutorial(): nn = NeuralNet() x_shape = dat.shape() test_gen, _ = dat.testset(batch_size=cfg.BATCH_SIZE, max_samples=cfg.TEST_SET_SIZE) nn.test(test_gen, print_it=True) nn.net.initialize_spatial_layers(x_shape, cfg.BATCH_SIZE, PATCH_SIZE) nn.summary(x_shape, print_it=True) nn.print_weights() print(nn.output_size(x_shape)) # Spatial Operations, defined one the net itself. Remember that after enabling a layer, ops are affected assert nn.net.num_spatial_layers() != 0 nn.net.print_spatial_status() # nn.train(epochs=1, set_size=5000, lr=0.1, batch_size=cfg.BATCH_SIZE) # Train to see fully disabled performance nn.net.print_ops_summary() nn.net.print_ops_summary( use_conv=True) # Count convlution operations instead of MAC print(nn.net.num_ops()) # (ops_saved, total_ops) # Given x, we generate all spatial layer requirement sizes: spat_sizes = nn.net.generate_spatial_sizes(x_shape) print(spat_sizes) p_spat_sizes = nn.net.generate_padded_spatial_sizes(x_shape, PATCH_SIZE) print(p_spat_sizes) # Generate a constant 1 value mask over all spatial nets print(nn.net.enabled_layers()) nn.net.fill_masks_to_val(1) print(nn.net.enabled_layers()) print(nn.net.disabled_layers()) nn.net.print_spatial_status( ) # Now all are enabled, seeing the mask was set nn.train(epochs=1, set_size=5000, lr=0.1, batch_size=cfg.BATCH_SIZE ) # Train to see all layers enabled performance nn.net.print_ops_summary() nn.net.print_ops_summary( use_conv=True) # Count convlution operations instead of MAC nn.net.reset_spatial() # Disables layers as well nn.net.print_ops_summary() nn.net.print_ops_summary(use_conv=True) # Turns on 3 ids and turns off all others chosen_victims = random.sample(range(nn.net.num_spatial_layers()), 4) nn.net.strict_mask_update(update_ids=chosen_victims[0:3], masks=[ torch.zeros(p_spat_sizes[chosen_victims[0]]), torch.zeros(p_spat_sizes[chosen_victims[1]]), torch.zeros(p_spat_sizes[chosen_victims[2]]) ]) # Turns on one additional id and *does not* turn off all others nn.net.lazy_mask_update( update_ids=[chosen_victims[3]], masks=[torch.zeros(p_spat_sizes[chosen_victims[3]])]) nn.net.print_spatial_status() # print(nn.net.enabled_layers()) nn.train(epochs=1, set_size=5000, lr=0.1, batch_size=cfg.BATCH_SIZE) # Run with 4 layers on nn.net.print_ops_summary() nn.net.print_ops_summary(use_conv=True)
def training(): # dat.data_summary(show_sample=False) nn = NeuralNet(resume=True) # Spatial layers are by default, disabled nn.summary(dat.shape()) nn.train(epochs=50, lr=0.01) test_gen, _ = dat.testset(batch_size=cfg.BATCH_SIZE, max_samples=cfg.TEST_SET_SIZE) test_loss, test_acc, count = nn.test(test_gen) print( f'==> Final testing results: test acc: {test_acc:.3f} with {count}, test loss: {test_loss:.3f}' )
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 test(): nn = NeuralNet(resume=True) test_gen, _ = dat.testset(batch_size=cfg.BATCH_SIZE, max_samples=cfg.TEST_SET_SIZE) test_loss, test_acc, count = nn.test(test_gen, print_it=True)