├── CIFAR10 ├── ANN_baseline │ ├── cifar10_resNet20_base_model.py │ └── cifar10_vgg16_base_model.py └── Hybrid_coding │ ├── cifar10_main_res20.py │ └── cifar10_main_vgg16.py ├── LICENSE ├── README.md ├── data ├── __pycache__ │ ├── autoaugment.cpython-39.pyc │ └── data_loader_cifar10.cpython-39.pyc ├── autoaugment.py └── data_loader_cifar10.py ├── distributed_utils ├── __pycache__ │ └── dist_helper.cpython-39.pyc └── dist_helper.py ├── models ├── TTFS_LIF.py ├── __pycache__ │ ├── TTFS_LIF.cpython-39.pyc │ ├── resnet20.cpython-39.pyc │ └── vgg16.cpython-39.pyc ├── resnet20.py └── vgg16.py └── utils ├── __pycache__ ├── classification.cpython-39.pyc ├── lib.cpython-39.pyc ├── modules.cpython-39.pyc └── utils.cpython-39.pyc ├── classification.py ├── lib.py ├── modules.py └── utils.py /CIFAR10/ANN_baseline/cifar10_resNet20_base_model.py: -------------------------------------------------------------------------------- 1 | import torch.backends.cudnn as cudnn 2 | import time 3 | import os 4 | import torch 5 | current_dir = os.path.dirname(os.path.dirname(os.getcwd())) 6 | os.environ['CUDA_VISIBLE_DEVICES'] = '0' # CUDA configuration 7 | 8 | from data.data_loader_cifar10 import build_data 9 | from models.resnet20 import ResNet20 10 | from utils.classification import training, testing 11 | from utils.lib import dump_json, set_seed 12 | 13 | set_seed(1111) 14 | 15 | # Load datasets 16 | home_dir = current_dir # relative path 17 | data_dir = '' # Data dir 18 | ckp_dir = os.path.join(home_dir, 'exp/cifar10/') 19 | 20 | batch_size = 128 21 | num_workers = 0 22 | 23 | train_loader, test_loader = build_data(dpath=data_dir, batch_size=batch_size, workers=num_workers, 24 | cutout=True, use_cifar10=True, auto_aug=True) 25 | 26 | if __name__ == '__main__': 27 | 28 | if torch.cuda.is_available(): 29 | device = 'cuda' 30 | print('GPU is available') 31 | else: 32 | device = 'cpu' 33 | print('GPU is not available') 34 | 35 | # Parameters 36 | num_epochs = 300 37 | global best_acc 38 | best_acc = 0 39 | test_acc_history = [] 40 | train_acc_history = [] 41 | 42 | # Models and training configuration 43 | model = ResNet20(num_class=10) 44 | print(model) 45 | model = model.to(device) 46 | cudnn.benchmark = True 47 | 48 | optimizer = torch.optim.SGD(model.parameters(), lr=1e-2, momentum=0.9, weight_decay=5e-4) 49 | criterion = torch.nn.CrossEntropyLoss() 50 | scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, eta_min=0, T_max=num_epochs) 51 | 52 | for epoch in range(num_epochs): 53 | since = time.time() 54 | 55 | # Training Stage 56 | model, acc_train, loss_train = training(model, train_loader, optimizer, criterion, device) 57 | 58 | # Testing Stage 59 | acc_test, loss_test = testing(model, test_loader, criterion, device) 60 | 61 | scheduler.step() 62 | 63 | # log results 64 | test_acc_history.append(acc_test) 65 | train_acc_history.append(acc_train) 66 | 67 | # Training Record 68 | time_elapsed = time.time() - since 69 | print('Epoch {:d} takes {:.0f}m {:.0f}s'.format(epoch+1, time_elapsed // 60, time_elapsed % 60)) 70 | print('Train Accuracy: {:4f}, Loss: {:4f}'.format(acc_train, loss_train)) 71 | print('Test Accuracy: {:4f}'.format(acc_test)) 72 | 73 | # Save Model 74 | if acc_test > best_acc: 75 | print("Saving the model.")\ 76 | 77 | if not os.path.isdir(ckp_dir+'checkpoint'): 78 | os.makedirs(ckp_dir+'checkpoint') 79 | 80 | state = { 81 | 'epoch': epoch, 82 | 'model_state_dict': model.state_dict(), 83 | 'optimizer_state_dict': optimizer.state_dict(), 84 | 'loss': loss_train, 85 | 'acc': acc_test, 86 | } 87 | torch.save(state, ckp_dir+'checkpoint/resnet20_addFC4096_wAvgPool_baseline.pth') 88 | best_acc = acc_test 89 | 90 | print('Best Test Accuracy: {:4f}'.format(best_acc)) 91 | training_record = { 92 | 'test_acc_history': test_acc_history, 93 | 'train_acc_history': train_acc_history, 94 | 'best_acc': best_acc, 95 | } 96 | dump_json(training_record, ckp_dir, 'resnet20_baseline_train_record') 97 | -------------------------------------------------------------------------------- /CIFAR10/ANN_baseline/cifar10_vgg16_base_model.py: -------------------------------------------------------------------------------- 1 | import torch.backends.cudnn as cudnn 2 | import time 3 | import os 4 | 5 | import torch.optim.lr_scheduler 6 | current_dir = os.path.dirname(os.path.dirname(os.getcwd())) 7 | os.environ['CUDA_VISIBLE_DEVICES'] = '0' # CUDA configuration 8 | 9 | from data.data_loader_cifar10 import build_data 10 | from models.vgg16 import VGG16 11 | from utils.classification import training, testing 12 | from utils.lib import dump_json, set_seed 13 | 14 | set_seed(1111) 15 | 16 | # Load datasets 17 | home_dir = current_dir # relative path 18 | data_dir = '' # Data dir 19 | ckp_dir = os.path.join(home_dir, 'exp/cifar10/') 20 | 21 | batch_size = 128 22 | num_workers = 0 23 | 24 | train_loader, test_loader = build_data(dpath=data_dir, batch_size=batch_size, workers=num_workers, 25 | cutout=True, use_cifar10=True, auto_aug=True) 26 | 27 | if __name__ == '__main__': 28 | if torch.cuda.is_available(): 29 | device = 'cuda' 30 | print('GPU is available') 31 | else: 32 | device = 'cpu' 33 | print('GPU is not available') 34 | 35 | # Parameters 36 | num_epochs = 300 37 | global best_acc 38 | best_acc = 0 39 | test_acc_history = [] 40 | train_acc_history = [] 41 | 42 | # Models and training configuration 43 | model = VGG16(num_class=10) 44 | model = model.to(device) 45 | cudnn.benchmark = True 46 | print(model) 47 | 48 | optimizer = torch.optim.SGD(model.parameters(), lr=1e-1, momentum=0.9, weight_decay=5e-4) 49 | criterion = torch.nn.CrossEntropyLoss() 50 | scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, eta_min=0, T_max=num_epochs) 51 | 52 | 53 | for epoch in range(num_epochs): 54 | since = time.time() 55 | 56 | # Training Stage 57 | model, acc_train, loss_train = training(model, train_loader, optimizer, criterion, device) 58 | 59 | # Testing Stage 60 | acc_test, loss_test = testing(model, test_loader, criterion, device) 61 | 62 | # log results 63 | test_acc_history.append(acc_test) 64 | train_acc_history.append(acc_train) 65 | 66 | scheduler.step() 67 | 68 | # Training Record 69 | time_elapsed = time.time() - since 70 | print('Epoch {:d} takes {:.0f}m {:.0f}s'.format(epoch+1, time_elapsed // 60, time_elapsed % 60)) 71 | print('Train Accuracy: {:4f}, Loss: {:4f}'.format(acc_train, loss_train)) 72 | print('Test Accuracy: {:4f}'.format(acc_test)) 73 | 74 | # Save Model 75 | if acc_test > best_acc: 76 | print("Saving the model.")\ 77 | 78 | if not os.path.isdir(ckp_dir+'checkpoint'): 79 | os.makedirs(ckp_dir+'checkpoint') 80 | 81 | state = { 82 | 'epoch': epoch, 83 | 'model_state_dict': model.state_dict(), 84 | 'optimizer_state_dict': optimizer.state_dict(), 85 | 'loss': loss_train, 86 | 'acc': acc_test, 87 | } 88 | torch.save(state, ckp_dir+'checkpoint/vgg16_relu_wAvgPool_baseline.pth') 89 | best_acc = acc_test 90 | 91 | training_record = { 92 | 'test_acc_history': test_acc_history, 93 | 'train_acc_history': train_acc_history, 94 | 'best_acc': best_acc, 95 | } 96 | dump_json(training_record, ckp_dir, 'vgg16_relu_baseline_train_record') 97 | -------------------------------------------------------------------------------- /CIFAR10/Hybrid_coding/cifar10_main_res20.py: -------------------------------------------------------------------------------- 1 | import time 2 | import os 3 | import argparse 4 | import sys 5 | current_dir = os.path.dirname(os.path.dirname(os.getcwd())) 6 | sys.path.append(current_dir) 7 | os.environ['CUDA_VISIBLE_DEVICES'] = '0' # CUDA configuration 8 | import copy 9 | import torch 10 | import torch.nn as nn 11 | 12 | from models.resnet20 import ResNet20 13 | from models.TTFS_LIF import TTFS_LIF_linear 14 | from data.data_loader_cifar10 import build_data 15 | from utils.classification import training_thNorm_with_T,testing_snn_Burst, testing, training_snn_TTFS,testing_snn_TTFS 16 | from utils.utils import search_fold_and_remove_bn, replace_activation_by_neuron, replace_IF_by_Burst_all, get_maximum_activation 17 | from utils.lib import dump_json, set_seed 18 | 19 | set_seed(1111) 20 | 21 | # Load datasets 22 | home_dir = current_dir # relative path 23 | data_dir = '' # Data dir 24 | ann_ckp_dir = os.path.join(home_dir, 'exp/cifar10/') 25 | snn_ckp_dir = os.path.join(home_dir, 'exp/cifar10/snn/') 26 | 27 | parser = argparse.ArgumentParser(description='PyTorch Cifar-10 Training') 28 | parser.add_argument('--Tencode', default=16, type=int, metavar='N', 29 | help='encoding time window size') 30 | parser.add_argument('--epochs', default=50, type=int, metavar='N', 31 | help='number of total epochs to run') 32 | parser.add_argument('-b', '--batch-size', default=128, type=int, 33 | metavar='N', 34 | help='mini-batch size (default: 256), this is the total ' 35 | 'batch size of all GPUs on the current node when ' 36 | 'using Data Parallel or Distributed Data Parallel') 37 | parser.add_argument('--lr1', default=1e-4, type=float, 38 | metavar='LR_S1', help='initial learning rate of LTL training', dest='lr1') 39 | parser.add_argument('--lr2', default=1e-5, type=float, 40 | metavar='LR_S2', help='initial learning rate of TTFS training', dest='lr2') 41 | parser.add_argument('--save_path', default='', type=str, metavar='PATH', 42 | help='path to save the training record (default: none)') 43 | parser.add_argument('--local_coefficient', default=1.0, type=float, 44 | help='Coefficient of Local Loss') 45 | parser.add_argument('--beta', default=1, type=float, 46 | metavar='beta', help='coefficient beta') 47 | parser.add_argument('--gamma', default=5, type=float, 48 | metavar='gamma', help='Maximum number of spikes per timestep in burst coding') 49 | parser.add_argument('--threshold', default=3, type=float, 50 | help='The potential threshold of the output layer (TTFS coding)') 51 | parser.add_argument('--ltl_resume', default=True, action='store_true', 52 | help='Resume from LTL finetuned model and start ttfs learning') 53 | 54 | 55 | if __name__ == '__main__': 56 | if torch.cuda.is_available(): 57 | device = 'cuda' 58 | print('GPU is available') 59 | else: 60 | device = 'cpu' 61 | print('GPU is not available') 62 | 63 | # Parameters 64 | args = parser.parse_args() 65 | Tencode = args.Tencode 66 | num_epochs = args.epochs 67 | lr_ltl = args.lr1 68 | lr_ttfs = args.lr2 69 | 70 | alpha = 2 # coefficient alpha 71 | beta = args.beta # coefficient beta 72 | 73 | best_test_acc = 0 74 | best_test_spktime = Tencode 75 | batch_size = args.batch_size 76 | coeff_local = [args.local_coefficient] * 13 # Local loss coefficient for each layer 77 | test_acc_history = [] 78 | train_acc_history = [] 79 | test_spktime_history = [] 80 | 81 | # Prepare dataloader 82 | train_loader, test_loader = build_data(dpath=data_dir, batch_size=batch_size, cutout=True, workers=0, use_cifar10=True, auto_aug=True) 83 | 84 | # Init ANN and load pre-trained model 85 | model = ResNet20(num_class=10) 86 | model = model.to(device) 87 | TTFS_model = TTFS_LIF_linear(4096, 10).to(device) 88 | checkpoint = torch.load(ann_ckp_dir + 'checkpoint/resnet20_addFC4096_wAvgPool_baseline.pth') 89 | model.load_state_dict(checkpoint['model_state_dict']) 90 | print('Accuracy of pre-trained model {}'.format(checkpoint['acc'])) 91 | search_fold_and_remove_bn(model) 92 | ann = copy.deepcopy(model) 93 | 94 | get_maximum_activation(train_loader, model=model, momentum=0.9, iters=20, mse=True, percentile=None, 95 | sim_length=Tencode, channel_wise=False) 96 | 97 | # Init SNN model with ANN weights 98 | model = replace_activation_by_neuron(model) 99 | model = replace_IF_by_Burst_all(model, gamma=args.gamma) 100 | snn = copy.deepcopy(model) 101 | print(snn) 102 | 103 | # Training configuration 104 | criterion_out = torch.nn.CrossEntropyLoss() 105 | criterion_local = nn.MSELoss() # Local loss function 106 | optimizer = torch.optim.Adam(snn.parameters(), lr=lr_ltl, weight_decay=0) 107 | scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[20, 30, 40, 50, 80, 90], gamma=0.5) 108 | 109 | # Testing ANN and SNN model 110 | acc_test, loss_test = testing(ann, test_loader, criterion_out, device) 111 | print('Accuracy of BN folded ANN model {}'.format(acc_test)) 112 | 113 | acc_test, spk, spk_cnt = testing_snn_Burst(snn, test_loader, device, Tencode) 114 | print('Accuracy of converted SNN model {}'.format(acc_test)) 115 | 116 | if not args.ltl_resume: 117 | """ 118 | Stage1: Hidden Layers Training -- LTL 119 | """ 120 | print('------ Stage 1 - Training Hidden Layers ------') 121 | 122 | for epoch in range(num_epochs): 123 | since = time.time() 124 | 125 | # Training Stage 126 | snn, acc_train, loss_train = training_thNorm_with_T(ann, snn, train_loader, optimizer, criterion_out, 127 | criterion_local, coeff_local, device, Tencode, args.gamma) 128 | scheduler.step() 129 | 130 | # Testing Stage 131 | acc_test, spk, spk_cnt = testing_snn_Burst(snn, test_loader, device, Tencode) 132 | 133 | # log results 134 | test_acc_history.append(acc_test[-1].item()) 135 | train_acc_history.append(acc_train) 136 | 137 | # Report Training Progress 138 | time_elapsed = time.time() - since 139 | print('Stage1, Epoch {:d} takes {:.0f}m {:.0f}s'.format(epoch + 1, time_elapsed // 60, time_elapsed % 60)) 140 | print('Train Accuracy: {:4f}, Loss: {:4f}'.format(acc_train, loss_train)) 141 | print('Test Accuracy: {}'.format(acc_test)) 142 | 143 | # Save Model 144 | if acc_test[-1] > best_test_acc: 145 | print("Saving the model.") 146 | 147 | if not os.path.isdir(snn_ckp_dir + 'checkpoint'): 148 | os.makedirs(snn_ckp_dir + 'checkpoint') 149 | 150 | state = { 151 | 'epoch': epoch, 152 | 'model_state_dict': snn.state_dict(), 153 | 'optimizer_state_dict': optimizer.state_dict(), 154 | 'loss': loss_train, 155 | 'acc': acc_test[-1], 156 | } 157 | torch.save(state, snn_ckp_dir + 'DBD_CIFAR10_res20.pth') 158 | best_test_acc = acc_test[-1].item() 159 | 160 | print('Best Test Accuracy: {:4f}'.format(best_test_acc)) 161 | 162 | training_record = { 163 | 'test_acc_history': test_acc_history, 164 | 'train_acc_history': train_acc_history, 165 | 'best_acc': best_test_acc, 166 | } 167 | dump_json(training_record, snn_ckp_dir + 'record', 'cifar10_res20_record_LTL.pth') 168 | else: 169 | LTL = torch.load(snn_ckp_dir + 'DBD_CIFAR10_res20.pth') 170 | snn.load_state_dict(LTL['model_state_dict']) 171 | print('Resume the LTL-finetuned Model') 172 | acc_test, spk, spk_cnt = testing_snn_Burst(snn, test_loader, device, Tencode) 173 | print('Stage1 Test Accuracy: {}'.format(acc_test)) 174 | 175 | 176 | """ 177 | Stage2: Output Layers Training -- TTFS 178 | """ 179 | print('------ Stage 2 - Training Output Layer ------') 180 | 181 | optimizer = torch.optim.SGD(TTFS_model.parameters(), lr=lr_ttfs, momentum=0.9, weight_decay=0) 182 | scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[0.3 * num_epochs, 0.5 * num_epochs, 0.8 * num_epochs],gamma=0.5) 183 | 184 | for epoch in range(num_epochs): 185 | 186 | # Training Stage 187 | since = time.time() 188 | TTFS_model, acc_train, loss_train = training_snn_TTFS(snn, TTFS_model, train_loader, optimizer, criterion_out, alpha, beta, device, Tencode, args.threshold) 189 | scheduler.step() 190 | 191 | # Testing Stage 192 | acc_test, avg_test_time, spk_count = testing_snn_TTFS(snn, TTFS_model, test_loader, device, Tencode, args.threshold) 193 | 194 | # Report Training Progress 195 | time_elapsed = time.time() - since 196 | print('Stage2, Epoch {:d} takes {:.0f}m {:.0f}s'.format(epoch + 1, time_elapsed // 60, time_elapsed % 60)) 197 | print('Train Accuracy: {:.4f}, Loss: {:.4f}'.format(acc_train, loss_train)) 198 | print('Test Accuracy: {:.4f}, Test spiking time: {:.4f}'.format(acc_test.item(),avg_test_time.item())) 199 | #print('Test spiking distribution: {}'.format(spk_count)) # print the spike distribution 200 | 201 | # log results 202 | test_acc_history.append(acc_test.item()) 203 | test_spktime_history.append(avg_test_time.item()) 204 | train_acc_history.append(acc_train) 205 | 206 | 207 | # Save Model 208 | if acc_test > best_test_acc: 209 | print("Saving the model.") 210 | torch.save(TTFS_model.state_dict(), snn_ckp_dir + 'DBT_CIFAR10_res20.pth') 211 | if not os.path.isdir(snn_ckp_dir + 'checkpoint'): 212 | os.makedirs(snn_ckp_dir + 'checkpoint') 213 | 214 | state = { 215 | 'epoch': epoch, 216 | 'model_state_dict': TTFS_model.state_dict(), 217 | 'optimizer_state_dict': optimizer.state_dict(), 218 | 'loss': loss_train, 219 | 'acc': acc_test, 220 | } 221 | best_test_acc = acc_test 222 | best_test_spktime = avg_test_time 223 | print('Accuracy: {:.4f}, Average spike time: {:.4f}, Best acc: {:.4f}, Best acc spike time: {:.4f}'.format(acc_test,avg_test_time,best_test_acc, best_test_spktime)) 224 | 225 | training_record = { 226 | 'test_acc_history': test_acc_history, 227 | 'test_spiketime_history': test_spktime_history, 228 | 'train_acc_history': train_acc_history, 229 | 'best_acc': best_test_acc.item(), 230 | } 231 | dump_json(training_record, snn_ckp_dir + 'record', 'cifar10_res20_record_TTFS.pth') 232 | -------------------------------------------------------------------------------- /CIFAR10/Hybrid_coding/cifar10_main_vgg16.py: -------------------------------------------------------------------------------- 1 | import time 2 | import os 3 | import argparse 4 | import sys 5 | current_dir = os.path.dirname(os.path.dirname(os.getcwd())) 6 | sys.path.append(current_dir) 7 | os.environ['CUDA_VISIBLE_DEVICES'] = '0' # CUDA configuration 8 | import copy 9 | import torch 10 | import torch.nn as nn 11 | 12 | from models.vgg16 import VGG16 13 | from models.TTFS_LIF import TTFS_LIF_linear 14 | from data.data_loader_cifar10 import build_data 15 | from utils.classification import training_thNorm_with_T,testing_snn_Burst, testing, training_snn_TTFS,testing_snn_TTFS 16 | from utils.utils import search_fold_and_remove_bn, replace_activation_by_neuron, replace_IF_by_Burst_all, get_maximum_activation 17 | from utils.lib import dump_json, set_seed 18 | 19 | set_seed(1111) 20 | 21 | # Load datasets 22 | home_dir = current_dir # relative path 23 | data_dir = '' # Data dir 24 | ann_ckp_dir = os.path.join(home_dir, 'exp/cifar10/') 25 | snn_ckp_dir = os.path.join(home_dir, 'exp/cifar10/snn/') 26 | 27 | parser = argparse.ArgumentParser(description='PyTorch Cifar-10 Training') 28 | parser.add_argument('--Tencode', default=16, type=int, metavar='N', 29 | help='encoding time window size') 30 | parser.add_argument('--epochs', default=50, type=int, metavar='N', 31 | help='number of total epochs to run') 32 | parser.add_argument('-b', '--batch-size', default=96, type=int, 33 | metavar='N', 34 | help='mini-batch size (default: 256), this is the total ' 35 | 'batch size of all GPUs on the current node when ' 36 | 'using Data Parallel or Distributed Data Parallel') 37 | parser.add_argument('--lr1', default=1e-4, type=float, 38 | metavar='LR_S1', help='initial learning rate of LTL training', dest='lr1') 39 | parser.add_argument('--lr2', default=1e-5, type=float, 40 | metavar='LR_S2', help='initial learning rate of TTFS training', dest='lr2') 41 | parser.add_argument('--save_path', default='', type=str, metavar='PATH', 42 | help='path to save the training record (default: none)') 43 | parser.add_argument('--local_coefficient', default=1.0, type=float, 44 | help='Coefficient of Local Loss') 45 | parser.add_argument('--beta', default=2, type=float, 46 | metavar='beta', help='coefficient beta') 47 | parser.add_argument('--gamma', default=5, type=float, 48 | metavar='gamma', help='Maximum number of spikes per timestep in burst coding') 49 | parser.add_argument('--threshold', default=3, type=float, 50 | help='The potential threshold of the output layer (TTFS coding)') 51 | parser.add_argument('--ltl_resume', default=True, action='store_true', 52 | help='Resume from LTL finetuned model and start ttfs learning') 53 | 54 | 55 | if __name__ == '__main__': 56 | if torch.cuda.is_available(): 57 | device = 'cuda' 58 | print('GPU is available') 59 | else: 60 | device = 'cpu' 61 | print('GPU is not available') 62 | 63 | # Parameters 64 | args = parser.parse_args() 65 | Tencode = args.Tencode 66 | num_epochs = args.epochs 67 | lr_ltl = args.lr1 68 | lr_ttfs = args.lr2 69 | 70 | alpha = 2 # coefficient alpha 71 | beta = args.beta # coefficient beta 72 | 73 | best_test_acc = 0 74 | best_test_spktime = Tencode 75 | batch_size = args.batch_size 76 | coeff_local = [args.local_coefficient] * 15 # Local loss coefficient for each layer 77 | test_acc_history = [] 78 | train_acc_history = [] 79 | test_spktime_history = [] 80 | 81 | # Prepare dataloader 82 | train_loader, test_loader = build_data(dpath=data_dir, batch_size=batch_size, cutout=True, workers=0, use_cifar10=True, auto_aug=True) 83 | 84 | # Init ANN and load pre-trained model 85 | model = VGG16(num_class=10) 86 | model = model.to(device) 87 | TTFS_model = TTFS_LIF_linear(4096, 10).to(device) 88 | checkpoint = torch.load(ann_ckp_dir + 'checkpoint/vgg16_relu_wAvgPool_baseline.pth') 89 | model.load_state_dict(checkpoint['model_state_dict']) 90 | print('Accuracy of pre-trained model {}'.format(checkpoint['acc'])) 91 | search_fold_and_remove_bn(model) 92 | ann = copy.deepcopy(model) 93 | 94 | get_maximum_activation(train_loader, model=model, momentum=0.9, iters=20, mse=True, percentile=None, 95 | sim_length=Tencode, channel_wise=False) 96 | 97 | # Init SNN model with ANN weights 98 | model = replace_activation_by_neuron(model) 99 | model = replace_IF_by_Burst_all(model, gamma=args.gamma) 100 | snn = copy.deepcopy(model) 101 | print(snn) 102 | 103 | # Training configuration 104 | criterion_out = torch.nn.CrossEntropyLoss() 105 | criterion_local = nn.MSELoss() # Local loss function 106 | optimizer = torch.optim.Adam(snn.parameters(), lr=lr_ltl, weight_decay=0) 107 | scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[20, 30, 40, 50, 80, 90], gamma=0.5) 108 | 109 | # Testing ANN and SNN model 110 | acc_test, loss_test = testing(ann, test_loader, criterion_out, device) 111 | print('Accuracy of BN folded ANN model {}'.format(acc_test)) 112 | 113 | acc_test, spk, spk_cnt = testing_snn_Burst(snn, test_loader, device, Tencode) 114 | print('Accuracy of converted SNN model {}'.format(acc_test)) 115 | 116 | if not args.ltl_resume: 117 | """ 118 | Stage1: Hidden Layers Training -- LTL 119 | """ 120 | print('------ Stage 1 - Training Hidden Layers ------') 121 | 122 | for epoch in range(num_epochs): 123 | since = time.time() 124 | 125 | # Training Stage 126 | snn, acc_train, loss_train = training_thNorm_with_T(ann, snn, train_loader, optimizer, criterion_out, 127 | criterion_local, coeff_local, device, Tencode, args.gamma) 128 | scheduler.step() 129 | 130 | # Testing Stage 131 | acc_test, spk, spk_cnt = testing_snn_Burst(snn, test_loader, device, Tencode) 132 | 133 | # log results 134 | test_acc_history.append(acc_test[-1].item()) 135 | train_acc_history.append(acc_train) 136 | 137 | # Report Training Progress 138 | time_elapsed = time.time() - since 139 | print('Stage1, Epoch {:d} takes {:.0f}m {:.0f}s'.format(epoch + 1, time_elapsed // 60, time_elapsed % 60)) 140 | print('Train Accuracy: {:4f}, Loss: {:4f}'.format(acc_train, loss_train)) 141 | print('Test Accuracy: {}'.format(acc_test)) 142 | 143 | # Save Model 144 | if acc_test[-1] > best_test_acc: 145 | print("Saving the model.") 146 | 147 | if not os.path.isdir(snn_ckp_dir + 'checkpoint'): 148 | os.makedirs(snn_ckp_dir + 'checkpoint') 149 | 150 | state = { 151 | 'epoch': epoch, 152 | 'model_state_dict': snn.state_dict(), 153 | 'optimizer_state_dict': optimizer.state_dict(), 154 | 'loss': loss_train, 155 | 'acc': acc_test[-1], 156 | } 157 | torch.save(state, snn_ckp_dir + 'DBD_CIFAR10_vgg16.pth') 158 | best_test_acc = acc_test[-1].item() 159 | 160 | print('Best Test Accuracy: {:4f}'.format(best_test_acc)) 161 | 162 | training_record = { 163 | 'test_acc_history': test_acc_history, 164 | 'train_acc_history': train_acc_history, 165 | 'best_acc': best_test_acc, 166 | } 167 | dump_json(training_record, snn_ckp_dir + 'record', 'cifar10_vgg16_record_LTL.pth') 168 | else: 169 | LTL = torch.load(snn_ckp_dir + 'DBD_CIFAR10_vgg16.pth') 170 | snn.load_state_dict(LTL['model_state_dict']) 171 | print('Resume the LTL-finetuned Model') 172 | acc_test, spk, spk_cnt = testing_snn_Burst(snn, test_loader, device, Tencode) 173 | print('Stage1 Test Accuracy: {}'.format(acc_test)) 174 | 175 | """ 176 | Stage2: Output Layers Training -- TTFS 177 | """ 178 | print('------ Stage 2 - Training Output Layer ------') 179 | 180 | optimizer = torch.optim.SGD(TTFS_model.parameters(), lr=lr_ttfs, momentum=0.9, weight_decay=0) 181 | scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[0.3 * num_epochs, 0.5 * num_epochs, 0.8 * num_epochs],gamma=0.5) 182 | 183 | for epoch in range(num_epochs): 184 | 185 | # Training Stage 186 | since = time.time() 187 | TTFS_model, acc_train, loss_train = training_snn_TTFS(snn, TTFS_model, train_loader, optimizer, criterion_out, alpha, beta, device, Tencode, args.threshold) 188 | scheduler.step() 189 | 190 | # Testing Stage 191 | acc_test, avg_test_time, spk_count = testing_snn_TTFS(snn, TTFS_model, test_loader, device, Tencode, args.threshold) 192 | 193 | # Report Training Progress 194 | time_elapsed = time.time() - since 195 | print('Stage2, Epoch {:d} takes {:.0f}m {:.0f}s'.format(epoch + 1, time_elapsed // 60, time_elapsed % 60)) 196 | print('Train Accuracy: {:.4f}, Loss: {:.4f}'.format(acc_train, loss_train)) 197 | print('Test Accuracy: {:.4f}, Test spiking time: {:.4f}'.format(acc_test.item(),avg_test_time.item())) 198 | #print('Test spiking distribution: {}'.format(spk_count)) # print the spike distribution 199 | 200 | # log results 201 | test_acc_history.append(acc_test.item()) 202 | test_spktime_history.append(avg_test_time.item()) 203 | train_acc_history.append(acc_train) 204 | 205 | 206 | # Save Model 207 | if acc_test > best_test_acc: 208 | print("Saving the model.") 209 | torch.save(TTFS_model.state_dict(), snn_ckp_dir + 'DBT_CIFAR10_vgg16.pth') 210 | if not os.path.isdir(snn_ckp_dir + 'checkpoint'): 211 | os.makedirs(snn_ckp_dir + 'checkpoint') 212 | 213 | state = { 214 | 'epoch': epoch, 215 | 'model_state_dict': TTFS_model.state_dict(), 216 | 'optimizer_state_dict': optimizer.state_dict(), 217 | 'loss': loss_train, 218 | 'acc': acc_test, 219 | } 220 | best_test_acc = acc_test 221 | best_test_spktime = avg_test_time 222 | print('Accuracy: {:.4f}, Average spike time: {:.4f}, Best acc: {:.4f}, Best acc spike time: {:.4f}'.format(acc_test,avg_test_time,best_test_acc, best_test_spktime)) 223 | 224 | training_record = { 225 | 'test_acc_history': test_acc_history, 226 | 'test_spiketime_history': test_spktime_history, 227 | 'train_acc_history': train_acc_history, 228 | 'best_acc': best_test_acc.item(), 229 | } 230 | dump_json(training_record, snn_ckp_dir + 'record', 'cifar10_vgg16_record_TTFS.pth') 231 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A Hybrid Neural Coding Approach for Pattern Recognition with Spiking Neural Networks 2 | This repository contains the implementation of the paper [A Hybrid Neural Coding Approach for Pattern Recognition with Spiking Neural Networks](https://ieeexplore.ieee.org/document/10347028/metrics#metrics) published at **IEEE Transactions on Pattern Analysis Machine Intelligence (TPAMI)**. 3 | 4 | ## Dataset 5 | CIFAR-10 6 | 7 | ## Dependencies 8 | - python 3.9.18 9 | - Pytorch 1.13.0 10 | - [Spikingjelly 0.0.0.12](https://github.com/fangwei123456/spikingjelly). 11 | 12 | ## Reducibility 13 | 14 | * Pre-train the ANN. 15 | ``` 16 | python ./CIFAR10/ANN_baseline/cifar10_vgg16_base_model.py 17 | python ./CIFAR10/ANN_baseline/cifar10_resNet20_base_model.py 18 | ``` 19 | * Training Hybrid Coding framework. 20 | ``` 21 | python ./CIFAR10/Hybrid_coding/cifar10_main_vgg16.py 22 | python ./CIFAR10/Hybrid_coding/cifar10_main_res20.py 23 | ``` 24 | 25 | ## Citation 26 | ```tex 27 | @article{chen2024hybrid, 28 | title={A hybrid neural coding approach for pattern recognition with spiking neural networks}, 29 | author={Chen, Xinyi and Yang, Qu and Wu, Jibin and Li, Haizhou and Tan, Kay Chen}, 30 | journal={IEEE Transactions on Pattern Analysis \& Machine Intelligence}, 31 | volume={46}, 32 | number={05}, 33 | pages={3064--3078}, 34 | year={2024}, 35 | publisher={IEEE Computer Society} 36 | } 37 | ``` 38 | -------------------------------------------------------------------------------- /data/__pycache__/autoaugment.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/data/__pycache__/autoaugment.cpython-39.pyc -------------------------------------------------------------------------------- /data/__pycache__/data_loader_cifar10.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/data/__pycache__/data_loader_cifar10.cpython-39.pyc -------------------------------------------------------------------------------- /data/autoaugment.py: -------------------------------------------------------------------------------- 1 | from PIL import Image, ImageEnhance, ImageOps 2 | import numpy as np 3 | import random 4 | 5 | import torch 6 | 7 | 8 | class Cutout(object): 9 | """Randomly mask out one or more patches from an image. 10 | Args: 11 | n_holes (int): Number of patches to cut out of each image. 12 | length (int): The length (in pixels) of each square patch. 13 | """ 14 | 15 | def __init__(self, n_holes, length): 16 | self.n_holes = n_holes 17 | self.length = length 18 | 19 | def __call__(self, img): 20 | """ 21 | Args: 22 | img (Tensor): Tensor image of size (C, H, W). 23 | Returns: 24 | Tensor: Image with n_holes of dimension length x length cut out of it. 25 | """ 26 | h = img.size(1) 27 | w = img.size(2) 28 | 29 | mask = np.ones((h, w), np.float32) 30 | 31 | for n in range(self.n_holes): 32 | y = np.random.randint(h) 33 | x = np.random.randint(w) 34 | 35 | y1 = np.clip(y - self.length // 2, 0, h) 36 | y2 = np.clip(y + self.length // 2, 0, h) 37 | x1 = np.clip(x - self.length // 2, 0, w) 38 | x2 = np.clip(x + self.length // 2, 0, w) 39 | 40 | mask[y1: y2, x1: x2] = 0. 41 | 42 | mask = torch.from_numpy(mask) 43 | mask = mask.expand_as(img) 44 | img = img * mask 45 | 46 | return img 47 | 48 | 49 | class ImageNetPolicy(object): 50 | """ Randomly choose one of the best 24 Sub-policies on ImageNet. 51 | 52 | Example: 53 | >>> policy = ImageNetPolicy() 54 | >>> transformed = policy(image) 55 | 56 | Example as a PyTorch Transform: 57 | >>> transform=transforms.Compose([ 58 | >>> transforms.Resize(256), 59 | >>> ImageNetPolicy(), 60 | >>> transforms.ToTensor()]) 61 | """ 62 | 63 | def __init__(self, fillcolor=(128, 128, 128)): 64 | self.policies = [ 65 | SubPolicy(0.4, "posterize", 8, 0.6, "rotate", 9, fillcolor), 66 | SubPolicy(0.6, "solarize", 5, 0.6, "autocontrast", 5, fillcolor), 67 | SubPolicy(0.8, "equalize", 8, 0.6, "equalize", 3, fillcolor), 68 | SubPolicy(0.6, "posterize", 7, 0.6, "posterize", 6, fillcolor), 69 | SubPolicy(0.4, "equalize", 7, 0.2, "solarize", 4, fillcolor), 70 | 71 | SubPolicy(0.4, "equalize", 4, 0.8, "rotate", 8, fillcolor), 72 | SubPolicy(0.6, "solarize", 3, 0.6, "equalize", 7, fillcolor), 73 | SubPolicy(0.8, "posterize", 5, 1.0, "equalize", 2, fillcolor), 74 | SubPolicy(0.2, "rotate", 3, 0.6, "solarize", 8, fillcolor), 75 | SubPolicy(0.6, "equalize", 8, 0.4, "posterize", 6, fillcolor), 76 | 77 | SubPolicy(0.8, "rotate", 8, 0.4, "color", 0, fillcolor), 78 | SubPolicy(0.4, "rotate", 9, 0.6, "equalize", 2, fillcolor), 79 | SubPolicy(0.0, "equalize", 7, 0.8, "equalize", 8, fillcolor), 80 | SubPolicy(0.6, "invert", 4, 1.0, "equalize", 8, fillcolor), 81 | SubPolicy(0.6, "color", 4, 1.0, "contrast", 8, fillcolor), 82 | 83 | SubPolicy(0.8, "rotate", 8, 1.0, "color", 2, fillcolor), 84 | SubPolicy(0.8, "color", 8, 0.8, "solarize", 7, fillcolor), 85 | SubPolicy(0.4, "sharpness", 7, 0.6, "invert", 8, fillcolor), 86 | SubPolicy(0.6, "shearX", 5, 1.0, "equalize", 9, fillcolor), 87 | SubPolicy(0.4, "color", 0, 0.6, "equalize", 3, fillcolor), 88 | 89 | SubPolicy(0.4, "equalize", 7, 0.2, "solarize", 4, fillcolor), 90 | SubPolicy(0.6, "solarize", 5, 0.6, "autocontrast", 5, fillcolor), 91 | SubPolicy(0.6, "invert", 4, 1.0, "equalize", 8, fillcolor), 92 | SubPolicy(0.6, "color", 4, 1.0, "contrast", 8, fillcolor) 93 | ] 94 | 95 | def __call__(self, img): 96 | policy_idx = random.randint(0, len(self.policies) - 1) 97 | return self.policies[policy_idx](img) 98 | 99 | def __repr__(self): 100 | return "AutoAugment ImageNet Policy" 101 | 102 | 103 | class CIFAR10Policy(object): 104 | """ Randomly choose one of the best 25 Sub-policies on CIFAR10. 105 | 106 | Example: 107 | >>> policy = CIFAR10Policy() 108 | >>> transformed = policy(image) 109 | 110 | Example as a PyTorch Transform: 111 | >>> transform=transforms.Compose([ 112 | >>> transforms.Resize(256), 113 | >>> CIFAR10Policy(), 114 | >>> transforms.ToTensor()]) 115 | """ 116 | 117 | def __init__(self, fillcolor=(128, 128, 128)): 118 | self.policies = [ 119 | SubPolicy(0.1, "invert", 7, 0.2, "contrast", 6, fillcolor), 120 | SubPolicy(0.7, "rotate", 2, 0.3, "translateX", 9, fillcolor), 121 | SubPolicy(0.8, "sharpness", 1, 0.9, "sharpness", 3, fillcolor), 122 | SubPolicy(0.5, "shearY", 8, 0.7, "translateY", 9, fillcolor), 123 | SubPolicy(0.5, "autocontrast", 8, 0.9, "equalize", 2, fillcolor), 124 | 125 | SubPolicy(0.2, "shearY", 7, 0.3, "posterize", 7, fillcolor), 126 | SubPolicy(0.4, "color", 3, 0.6, "brightness", 7, fillcolor), 127 | SubPolicy(0.3, "sharpness", 9, 0.7, "brightness", 9, fillcolor), 128 | SubPolicy(0.6, "equalize", 5, 0.5, "equalize", 1, fillcolor), 129 | SubPolicy(0.6, "contrast", 7, 0.6, "sharpness", 5, fillcolor), 130 | 131 | SubPolicy(0.7, "color", 7, 0.5, "translateX", 8, fillcolor), 132 | SubPolicy(0.3, "equalize", 7, 0.4, "autocontrast", 8, fillcolor), 133 | SubPolicy(0.4, "translateY", 3, 0.2, "sharpness", 6, fillcolor), 134 | SubPolicy(0.9, "brightness", 6, 0.2, "color", 8, fillcolor), 135 | SubPolicy(0.5, "solarize", 2, 0.0, "invert", 3, fillcolor), 136 | 137 | SubPolicy(0.2, "equalize", 0, 0.6, "autocontrast", 0, fillcolor), 138 | SubPolicy(0.2, "equalize", 8, 0.8, "equalize", 4, fillcolor), 139 | SubPolicy(0.9, "color", 9, 0.6, "equalize", 6, fillcolor), 140 | SubPolicy(0.8, "autocontrast", 4, 0.2, "solarize", 8, fillcolor), 141 | SubPolicy(0.1, "brightness", 3, 0.7, "color", 0, fillcolor), 142 | 143 | SubPolicy(0.4, "solarize", 5, 0.9, "autocontrast", 3, fillcolor), 144 | SubPolicy(0.9, "translateY", 9, 0.7, "translateY", 9, fillcolor), 145 | SubPolicy(0.9, "autocontrast", 2, 0.8, "solarize", 3, fillcolor), 146 | SubPolicy(0.8, "equalize", 8, 0.1, "invert", 3, fillcolor), 147 | SubPolicy(0.7, "translateY", 9, 0.9, "autocontrast", 1, fillcolor) 148 | ] 149 | 150 | def __call__(self, img): 151 | policy_idx = random.randint(0, len(self.policies) - 1) 152 | return self.policies[policy_idx](img) 153 | 154 | def __repr__(self): 155 | return "AutoAugment CIFAR10 Policy" 156 | 157 | 158 | class SVHNPolicy(object): 159 | """ Randomly choose one of the best 25 Sub-policies on SVHN. 160 | 161 | Example: 162 | >>> policy = SVHNPolicy() 163 | >>> transformed = policy(image) 164 | 165 | Example as a PyTorch Transform: 166 | >>> transform=transforms.Compose([ 167 | >>> transforms.Resize(256), 168 | >>> SVHNPolicy(), 169 | >>> transforms.ToTensor()]) 170 | """ 171 | 172 | def __init__(self, fillcolor=(128, 128, 128)): 173 | self.policies = [ 174 | SubPolicy(0.9, "shearX", 4, 0.2, "invert", 3, fillcolor), 175 | SubPolicy(0.9, "shearY", 8, 0.7, "invert", 5, fillcolor), 176 | SubPolicy(0.6, "equalize", 5, 0.6, "solarize", 6, fillcolor), 177 | SubPolicy(0.9, "invert", 3, 0.6, "equalize", 3, fillcolor), 178 | SubPolicy(0.6, "equalize", 1, 0.9, "rotate", 3, fillcolor), 179 | 180 | SubPolicy(0.9, "shearX", 4, 0.8, "autocontrast", 3, fillcolor), 181 | SubPolicy(0.9, "shearY", 8, 0.4, "invert", 5, fillcolor), 182 | SubPolicy(0.9, "shearY", 5, 0.2, "solarize", 6, fillcolor), 183 | SubPolicy(0.9, "invert", 6, 0.8, "autocontrast", 1, fillcolor), 184 | SubPolicy(0.6, "equalize", 3, 0.9, "rotate", 3, fillcolor), 185 | 186 | SubPolicy(0.9, "shearX", 4, 0.3, "solarize", 3, fillcolor), 187 | SubPolicy(0.8, "shearY", 8, 0.7, "invert", 4, fillcolor), 188 | SubPolicy(0.9, "equalize", 5, 0.6, "translateY", 6, fillcolor), 189 | SubPolicy(0.9, "invert", 4, 0.6, "equalize", 7, fillcolor), 190 | SubPolicy(0.3, "contrast", 3, 0.8, "rotate", 4, fillcolor), 191 | 192 | SubPolicy(0.8, "invert", 5, 0.0, "translateY", 2, fillcolor), 193 | SubPolicy(0.7, "shearY", 6, 0.4, "solarize", 8, fillcolor), 194 | SubPolicy(0.6, "invert", 4, 0.8, "rotate", 4, fillcolor), 195 | SubPolicy(0.3, "shearY", 7, 0.9, "translateX", 3, fillcolor), 196 | SubPolicy(0.1, "shearX", 6, 0.6, "invert", 5, fillcolor), 197 | 198 | SubPolicy(0.7, "solarize", 2, 0.6, "translateY", 7, fillcolor), 199 | SubPolicy(0.8, "shearY", 4, 0.8, "invert", 8, fillcolor), 200 | SubPolicy(0.7, "shearX", 9, 0.8, "translateY", 3, fillcolor), 201 | SubPolicy(0.8, "shearY", 5, 0.7, "autocontrast", 3, fillcolor), 202 | SubPolicy(0.7, "shearX", 2, 0.1, "invert", 5, fillcolor) 203 | ] 204 | 205 | def __call__(self, img): 206 | policy_idx = random.randint(0, len(self.policies) - 1) 207 | return self.policies[policy_idx](img) 208 | 209 | def __repr__(self): 210 | return "AutoAugment SVHN Policy" 211 | 212 | 213 | class SubPolicy(object): 214 | def __init__(self, p1, operation1, magnitude_idx1, p2, operation2, magnitude_idx2, fillcolor=(128, 128, 128)): 215 | ranges = { 216 | "shearX": np.linspace(0, 0.3, 10), 217 | "shearY": np.linspace(0, 0.3, 10), 218 | "translateX": np.linspace(0, 150 / 331, 10), 219 | "translateY": np.linspace(0, 150 / 331, 10), 220 | "rotate": np.linspace(0, 30, 10), 221 | "color": np.linspace(0.0, 0.9, 10), 222 | "posterize": np.round(np.linspace(8, 4, 10), 0).astype(np.int64), 223 | "solarize": np.linspace(256, 0, 10), 224 | "contrast": np.linspace(0.0, 0.9, 10), 225 | "sharpness": np.linspace(0.0, 0.9, 10), 226 | "brightness": np.linspace(0.0, 0.9, 10), 227 | "autocontrast": [0] * 10, 228 | "equalize": [0] * 10, 229 | "invert": [0] * 10 230 | } 231 | 232 | def rotate_with_fill(img, magnitude): 233 | rot = img.convert("RGBA").rotate(magnitude) 234 | return Image.composite(rot, Image.new("RGBA", rot.size, (128,) * 4), rot).convert(img.mode) 235 | 236 | func = { 237 | "shearX": lambda img, magnitude: img.transform( 238 | img.size, Image.AFFINE, (1, magnitude * 239 | random.choice([-1, 1]), 0, 0, 1, 0), 240 | Image.BICUBIC, fillcolor=fillcolor), 241 | "shearY": lambda img, magnitude: img.transform( 242 | img.size, Image.AFFINE, (1, 0, 0, magnitude * 243 | random.choice([-1, 1]), 1, 0), 244 | Image.BICUBIC, fillcolor=fillcolor), 245 | "translateX": lambda img, magnitude: img.transform( 246 | img.size, Image.AFFINE, (1, 0, magnitude * 247 | img.size[0] * random.choice([-1, 1]), 0, 1, 0), 248 | fillcolor=fillcolor), 249 | "translateY": lambda img, magnitude: img.transform( 250 | img.size, Image.AFFINE, (1, 0, 0, 0, 1, magnitude * 251 | img.size[1] * random.choice([-1, 1])), 252 | fillcolor=fillcolor), 253 | "rotate": lambda img, magnitude: rotate_with_fill(img, magnitude), 254 | # "rotate": lambda img, magnitude: img.rotate(magnitude * random.choice([-1, 1])), 255 | "color": lambda img, magnitude: ImageEnhance.Color(img).enhance(1 + magnitude * random.choice([-1, 1])), 256 | "posterize": lambda img, magnitude: ImageOps.posterize(img, magnitude), 257 | "solarize": lambda img, magnitude: ImageOps.solarize(img, magnitude), 258 | "contrast": lambda img, magnitude: ImageEnhance.Contrast(img).enhance( 259 | 1 + magnitude * random.choice([-1, 1])), 260 | "sharpness": lambda img, magnitude: ImageEnhance.Sharpness(img).enhance( 261 | 1 + magnitude * random.choice([-1, 1])), 262 | "brightness": lambda img, magnitude: ImageEnhance.Brightness(img).enhance( 263 | 1 + magnitude * random.choice([-1, 1])), 264 | "autocontrast": lambda img, magnitude: ImageOps.autocontrast(img), 265 | "equalize": lambda img, magnitude: ImageOps.equalize(img), 266 | "invert": lambda img, magnitude: ImageOps.invert(img) 267 | } 268 | 269 | # self.name = "{}_{:.2f}_and_{}_{:.2f}".format( 270 | # operation1, ranges[operation1][magnitude_idx1], 271 | # operation2, ranges[operation2][magnitude_idx2]) 272 | self.p1 = p1 273 | self.operation1 = func[operation1] 274 | self.magnitude1 = ranges[operation1][magnitude_idx1] 275 | self.p2 = p2 276 | self.operation2 = func[operation2] 277 | self.magnitude2 = ranges[operation2][magnitude_idx2] 278 | 279 | def __call__(self, img): 280 | if random.random() < self.p1: 281 | img = self.operation1(img, self.magnitude1) 282 | if random.random() < self.p2: 283 | img = self.operation2(img, self.magnitude2) 284 | return img 285 | -------------------------------------------------------------------------------- /data/data_loader_cifar10.py: -------------------------------------------------------------------------------- 1 | """ 2 | Create train, valid, test iterators for CIFAR-10 [1]. 3 | Easily extended to MNIST, CIFAR-100 and Imagenet. 4 | Extracted from: https://gist.github.com/kevinzakka/d33bf8d6c7f06a9d8c76d97 5 | """ 6 | 7 | import torch 8 | import numpy as np 9 | from torch.utils.data import DataLoader 10 | import torch.nn as nn 11 | import torchvision.transforms as transforms 12 | from torchvision.datasets import CIFAR10, CIFAR100 13 | from data.autoaugment import CIFAR10Policy, Cutout 14 | from torchvision import datasets 15 | from torchvision import transforms 16 | from torch.utils.data.sampler import SubsetRandomSampler 17 | 18 | def build_data(dpath: str, batch_size=128, cutout=False, workers=0, use_cifar10=False, auto_aug=False): 19 | 20 | aug = [transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip()] 21 | if auto_aug: 22 | aug.append(CIFAR10Policy()) 23 | 24 | aug.append(transforms.ToTensor()) 25 | 26 | if cutout: 27 | aug.append(Cutout(n_holes=1, length=16)) 28 | 29 | if use_cifar10: 30 | aug.append( 31 | transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))) 32 | transform_train = transforms.Compose(aug) 33 | transform_test = transforms.Compose([ 34 | transforms.ToTensor(), 35 | transforms.Normalize( 36 | (0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)), 37 | ]) 38 | train_dataset = CIFAR10(root=dpath, train=True, download=True, transform=transform_train) 39 | val_dataset = CIFAR10(root=dpath, train=False, download=True, transform=transform_test) 40 | 41 | else: 42 | aug.append( 43 | transforms.Normalize( 44 | (0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)), 45 | ) 46 | transform_train = transforms.Compose(aug) 47 | transform_test = transforms.Compose([ 48 | transforms.ToTensor(), 49 | transforms.Normalize( 50 | (0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)), 51 | ]) 52 | train_dataset = CIFAR100(root=dpath, train=True, download=True, transform=transform_train) 53 | val_dataset = CIFAR100(root=dpath, train=False, download=True, transform=transform_test) 54 | 55 | train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, 56 | num_workers=workers, pin_memory=True) 57 | 58 | val_loader = DataLoader(val_dataset, batch_size=batch_size, 59 | shuffle=False, num_workers=workers, pin_memory=True) 60 | 61 | return train_loader, val_loader 62 | -------------------------------------------------------------------------------- /distributed_utils/__pycache__/dist_helper.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/distributed_utils/__pycache__/dist_helper.cpython-39.pyc -------------------------------------------------------------------------------- /distributed_utils/dist_helper.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import distributed_utils as linklink 3 | 4 | 5 | class Distributed(object): 6 | """Decorator for Distributed tensor range""" 7 | def __init__(self, func): 8 | self._func = func 9 | 10 | def sync(data_min, data_max): 11 | linklink.allreduce(data_min, reduce_op=linklink.allreduceOp_t.Min) 12 | linklink.allreduce(data_max, reduce_op=linklink.allreduceOp_t.Max) 13 | return data_min, data_max 14 | 15 | self._sync = sync 16 | 17 | def __call__(self, args, **kwargs): 18 | return self._sync(*(self._func(args, **kwargs))) 19 | 20 | 21 | class AllReduce(torch.autograd.Function): 22 | @staticmethod 23 | def forward(ctx, input): 24 | output = torch.zeros_like(input) 25 | output.copy_(input) 26 | linklink.allreduce(output) 27 | return output 28 | 29 | @staticmethod 30 | def backward(ctx, grad_output): 31 | in_grad = torch.zeros_like(grad_output) 32 | in_grad.copy_(grad_output) 33 | linklink.allreduce(in_grad) 34 | return in_grad 35 | 36 | 37 | def allaverage(tensor): 38 | tensor.data /= linklink.get_world_size() 39 | linklink.allreduce(tensor.data) 40 | return tensor 41 | 42 | 43 | def allaverage_autograd(tensor): 44 | tensor /= linklink.get_world_size() 45 | tensor = AllReduce().apply(tensor) 46 | return tensor 47 | 48 | 49 | def allreduce(tensor): 50 | linklink.allreduce(tensor.data) 51 | -------------------------------------------------------------------------------- /models/TTFS_LIF.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | import math 5 | from typing import Callable 6 | from spikingjelly.clock_driven import surrogate 7 | import random 8 | 9 | class TTFS_LIF(nn.Module): 10 | """LIF neurons in output layer""" 11 | def __init__(self, in_features, out_features, tau=2.0, tau_s=2.0 / 4, v_threshold=1.0, surrogate_function: Callable = surrogate.Sigmoid()): 12 | 13 | super().__init__() 14 | self.tau = tau 15 | self.tau_s = tau_s 16 | #self.T = T 17 | self.v_threshold = v_threshold 18 | self.fc = nn.Linear(in_features, out_features, bias=False) 19 | 20 | # v0 normalize the maximum value of PSP kernel to Vth 21 | t_max = (tau * tau_s * math.log(tau / tau_s)) / (tau - tau_s) 22 | self.v0 = self.v_threshold / (math.exp(-t_max / tau) - math.exp(-t_max / tau_s)) 23 | self.surrogate_function = surrogate_function 24 | self.delta=1 25 | 26 | def forward(self, threshold, count_t: torch.Tensor): 27 | # The last hidden layer’s output 28 | spike_in = count_t # shape=[batch_size, out_features, T] 29 | 30 | # PSP kernel iteration 31 | psp1 = math.exp(-self.delta / self.tau) * spike_in 32 | psp2 = math.exp(-self.delta / self.tau_s) * spike_in 33 | for step in range(spike_in.size(2)): 34 | if step > 0: 35 | psp1[:, :, step] += psp1[:, :, step-1] * math.exp( -1 / self.tau) 36 | psp2[:, :, step] += psp2[:, :, step-1] * math.exp( -1 / self.tau_s) 37 | 38 | # Post synaptic membrane potential 39 | v_out = self.fc(self.v0 *(psp1-psp2).permute(0, 2, 1)).permute(0, 2, 1) 40 | 41 | # Spike generation 42 | if torch.is_tensor(threshold): 43 | spike = self.surrogate_function(v_out-threshold.unsqueeze(0).unsqueeze(0). 44 | repeat(v_out.size(0),v_out.size(1),1)) 45 | else: 46 | spike = self.surrogate_function(v_out - threshold) 47 | 48 | return v_out.permute(0, 2, 1), spike.permute(0, 2, 1) # [N, T, Class] 49 | 50 | 51 | class TTFS_LIF_linear(nn.Module): 52 | def __init__(self, input_dim, out_dim): 53 | # input shape [N, H, T] 54 | super().__init__() 55 | self.LIF = TTFS_LIF(input_dim, out_dim) 56 | 57 | def forward(self,threshold,count_t: torch.Tensor): 58 | return self.LIF(threshold,count_t) 59 | -------------------------------------------------------------------------------- /models/__pycache__/TTFS_LIF.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/models/__pycache__/TTFS_LIF.cpython-39.pyc -------------------------------------------------------------------------------- /models/__pycache__/resnet20.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/models/__pycache__/resnet20.cpython-39.pyc -------------------------------------------------------------------------------- /models/__pycache__/vgg16.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/models/__pycache__/vgg16.cpython-39.pyc -------------------------------------------------------------------------------- /models/resnet20.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | import math 5 | from spikingjelly.clock_driven import neuron, functional, layer 6 | 7 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 8 | layer_list = ['conv1', 'conv2', 'conv3', 'conv4', 'conv5', 'conv6', 'conv7', 'conv8', 9 | 'conv9', 'conv10', 'conv11', 'conv12', 'conv13', 'fc14', 'fc15', 'fc16'] 10 | 11 | 12 | 13 | def conv3x3(in_planes, out_planes, stride=1): 14 | " 3x3 convolution with padding" 15 | return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) 16 | 17 | class BasicBlock(nn.Module): 18 | expansion = 1 19 | 20 | def __init__(self, in_planes, planes, stride=1): 21 | super(BasicBlock, self).__init__() 22 | self.conv1 = nn.Sequential( 23 | nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False), 24 | nn.BatchNorm2d(planes), 25 | nn.ReLU(inplace=True)) 26 | self.conv2 = nn.Sequential( 27 | nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False), 28 | nn.BatchNorm2d(planes), 29 | nn.ReLU(inplace=True)) 30 | 31 | self.shortcut = nn.Sequential() 32 | if stride != 1 or in_planes != self.expansion * planes: 33 | self.shortcut = nn.Sequential( 34 | nn.Conv2d(in_planes, self.expansion * planes, kernel_size=1, stride=stride, bias=False), 35 | nn.BatchNorm2d(self.expansion * planes), 36 | nn.ReLU(inplace=True)) 37 | 38 | def forward(self, x, SNN = False): 39 | out1 = self.conv1(x) 40 | out2_input = out1 41 | out2 = self.conv2(out2_input) 42 | 43 | if len(self.shortcut) > 0: 44 | out3 = self.shortcut(x) 45 | else: 46 | out3 = x 47 | 48 | out = out2 + out3 49 | return out, [out1, out2, out3] 50 | 51 | class ResNet20(nn.Module): 52 | def __init__(self, num_class=10): 53 | super(ResNet20, self).__init__() 54 | self.inplanes = 64 55 | self.conv1 = nn.Sequential( 56 | nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False), 57 | nn.BatchNorm2d(64), 58 | nn.ReLU(inplace=True)) 59 | self.conv2 = nn.Sequential( 60 | nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1, bias=False), 61 | nn.BatchNorm2d(64), 62 | nn.ReLU(inplace=True)) 63 | self.conv3 = nn.Sequential( 64 | nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=False), 65 | nn.BatchNorm2d(64), 66 | nn.ReLU(inplace=True)) 67 | 68 | self.layer4 = BasicBlock(64, 64, stride=1) 69 | self.layer5 = BasicBlock(64, 64, stride=1) 70 | self.layer6 = BasicBlock(64, 128, stride=2) 71 | self.layer7 = BasicBlock(128, 128, stride=1) 72 | self.layer8 = BasicBlock(128, 256, stride=2) 73 | self.layer9 = BasicBlock(256, 256, stride=1) 74 | self.layer10 = BasicBlock(256, 512, stride=2) 75 | self.layer11 = BasicBlock(512, 512, stride=1) 76 | self.pool12 = nn.AvgPool2d(2, 2) 77 | if num_class == 200: 78 | self.fc13 = nn.Sequential(nn.Linear(512 * 2 * 2, 4096), 79 | nn.ReLU(inplace=True)) 80 | else: 81 | self.fc13 = nn.Sequential(nn.Linear(512 * 1 * 1, 4096), 82 | nn.ReLU(inplace=True)) 83 | self.linear = nn.Linear(4096, num_class, bias=False) 84 | 85 | self._initialize_weights() 86 | 87 | def _initialize_weights(self): 88 | for m in self.modules(): 89 | if isinstance(m, nn.Conv2d): 90 | n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels 91 | m.weight.data.normal_(0, math.sqrt(2. / n)) 92 | elif isinstance(m, nn.BatchNorm2d): 93 | m.weight.data.fill_(1) 94 | if m.bias is not None: 95 | m.bias.data.zero_() 96 | elif isinstance(m, nn.Linear): 97 | n = m.weight.size(1) 98 | m.weight.data.normal_(0, 1.0 / float(n)) 99 | if m.bias is not None: 100 | m.bias.data.zero_() 101 | 102 | def forward(self, x, SNN = False, TTFS=False): 103 | x1 = self.conv1(x) 104 | x2_input = x1.detach() if SNN else x1 105 | x2 = self.conv2(x2_input) 106 | x3_input = x2.detach() if SNN else x2 107 | x3 = self.conv3(x3_input) 108 | x4_input = x3.detach() if SNN else x3 109 | 110 | x4, x4_mid = self.layer4(x4_input, SNN) 111 | x5_input = x4.detach() if SNN else x4 112 | x5, x5_mid = self.layer5(x5_input, SNN) 113 | x6_input = x5.detach() if SNN else x5 114 | x6, x6_mid = self.layer6(x6_input, SNN) 115 | x7_input = x6.detach() if SNN else x6 116 | x7, x7_mid = self.layer7(x7_input, SNN) 117 | x8_input = x7.detach() if SNN else x7 118 | x8, x8_mid = self.layer8(x8_input, SNN) 119 | x9_input = x8.detach() if SNN else x8 120 | x9, x9_mid = self.layer9(x9_input, SNN) 121 | x10_input = x9.detach() if SNN else x9 122 | x10, x10_mid = self.layer10(x10_input, SNN) 123 | x11_input = x10.detach() if SNN else x10 124 | x11, x11_mid = self.layer11(x11_input, SNN) 125 | x12 = self.pool12(x11) 126 | x12 = x12.view(x12.size(0), -1) 127 | 128 | x13_input = x12.detach() if SNN else x12 129 | x13 = self.fc13(x13_input) 130 | x14_input = x13.detach() if SNN else x13 131 | if not TTFS: 132 | out = self.linear(x14_input) 133 | else: 134 | out=x14_input 135 | return (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13), out 136 | 137 | 138 | -------------------------------------------------------------------------------- /models/vgg16.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | from spikingjelly.clock_driven import neuron, functional, layer 5 | from utils.modules import MyFloor, ScaledNeuron, BurstNode 6 | 7 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 8 | layer_list = ['conv1', ['conv2','pool2'], 'conv3', ['conv4','pool4'], 'conv5', 'conv6', ['conv7','pool7'], 'conv8', 9 | 'conv9', ['conv10','pool10'], 'conv11', 'conv12', ['conv13','pool13'], 'fc14', 'fc15', 'fc16'] 10 | 11 | 12 | class VGG16(nn.Module): 13 | def __init__(self, num_class, dropout=0): 14 | super(VGG16, self).__init__() 15 | self.bias = True 16 | self.conv1 = nn.Sequential(nn.Conv2d(3, 64, 3, stride=1, padding=1, bias=self.bias), 17 | nn.BatchNorm2d(64), 18 | nn.ReLU(inplace=True)) 19 | 20 | self.conv2 = nn.Sequential(nn.Conv2d(64, 64, 3, stride=1, padding=1, bias=self.bias), 21 | nn.BatchNorm2d(64), 22 | nn.ReLU(inplace=True)) 23 | self.pool2 = nn.AvgPool2d(2, 2) 24 | 25 | self.conv3 = nn.Sequential(nn.Conv2d(64, 128, 3, stride=1, padding=1, bias=self.bias), 26 | nn.BatchNorm2d(128), 27 | nn.ReLU(inplace=True)) 28 | 29 | self.conv4 = nn.Sequential(nn.Conv2d(128, 128, 3, stride=1, padding=1, bias=self.bias), 30 | nn.BatchNorm2d(128), 31 | nn.ReLU(inplace=True)) 32 | self.pool4 = nn.AvgPool2d(2, 2) 33 | 34 | self.conv5 = nn.Sequential(nn.Conv2d(128, 256, 3, stride=1, padding=1, bias=self.bias), 35 | nn.BatchNorm2d(256), 36 | nn.ReLU(inplace=True)) 37 | 38 | self.conv6 = nn.Sequential(nn.Conv2d(256, 256, 3, stride=1, padding=1, bias=self.bias), 39 | nn.BatchNorm2d(256), 40 | nn.ReLU(inplace=True)) 41 | 42 | self.conv7 = nn.Sequential(nn.Conv2d(256, 256, 3, stride=1, padding=1, bias=self.bias), 43 | nn.BatchNorm2d(256), 44 | nn.ReLU(inplace=True)) 45 | self.pool7 = nn.AvgPool2d(2, 2) 46 | 47 | self.conv8 = nn.Sequential(nn.Conv2d(256, 512, 3, stride=1, padding=1, bias=self.bias), 48 | nn.BatchNorm2d(512), 49 | nn.ReLU(inplace=True)) 50 | 51 | self.conv9 = nn.Sequential(nn.Conv2d(512, 512, 3, stride=1, padding=1, bias=self.bias), 52 | nn.BatchNorm2d(512), 53 | nn.ReLU(inplace=True)) 54 | 55 | self.conv10 = nn.Sequential(nn.Conv2d(512, 512, 3, stride=1, padding=1, bias=self.bias), 56 | nn.BatchNorm2d(512), 57 | nn.ReLU(inplace=True)) 58 | self.pool10 = nn.AvgPool2d(2, 2) 59 | 60 | self.conv11 = nn.Sequential(nn.Conv2d(512, 512, 3, stride=1, padding=1, bias=self.bias), 61 | nn.BatchNorm2d(512), 62 | nn.ReLU(inplace=True)) 63 | 64 | self.conv12 = nn.Sequential(nn.Conv2d(512, 512, 3, stride=1, padding=1, bias=self.bias), 65 | nn.BatchNorm2d(512), 66 | nn.ReLU(inplace=True)) 67 | 68 | self.conv13 = nn.Sequential(nn.Conv2d(512, 512, 3, stride=1, padding=1, bias=self.bias), 69 | nn.BatchNorm2d(512), 70 | nn.ReLU(inplace=True)) 71 | self.pool13 = nn.AvgPool2d(2, 2) 72 | 73 | if num_class == 1000: 74 | self.fc14 = nn.Sequential(nn.Linear(7 * 7 * 512, 4096, bias=self.bias), 75 | nn.ReLU(inplace=True) ) 76 | elif num_class == 200: 77 | self.fc14 = nn.Sequential(nn.Linear(2 * 2 * 512, 4096, bias=self.bias), 78 | nn.ReLU(inplace=True) ) 79 | else: 80 | self.fc14 = nn.Sequential(nn.Linear(1 * 1 * 512, 4096, bias=self.bias), 81 | nn.ReLU(inplace=True) ) 82 | 83 | self.fc15 = nn.Sequential(nn.Linear(4096, 4096, bias=self.bias), 84 | nn.ReLU(inplace=True)) 85 | self.fc16 = nn.Sequential(nn.Linear(4096, num_class, bias=self.bias),) 86 | 87 | self._initialize_weights() 88 | 89 | def _initialize_weights(self): 90 | for m in self.modules(): 91 | if isinstance(m, nn.Conv2d): 92 | nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') 93 | elif isinstance(m, nn.BatchNorm2d): 94 | nn.init.constant_(m.weight, val=1) 95 | if m.bias is not None: 96 | nn.init.zeros_(m.bias) 97 | elif isinstance(m, nn.Linear): 98 | if m.bias is not None: 99 | nn.init.zeros_(m.bias) 100 | 101 | def forward(self, x, SNN = False, TTFS = False): 102 | # Conv Layer 103 | x1 = self.conv1(x) 104 | x2_input = x1.detach() if SNN else x1 105 | x2 = self.conv2(x2_input) 106 | x2 = self.pool2(x2) 107 | x3_input = x2.detach() if SNN else x2 108 | x3 = self.conv3(x3_input) 109 | x4_input = x3.detach() if SNN else x3 110 | x4 = self.conv4(x4_input) 111 | x4 = self.pool4(x4) 112 | x5_input = x4.detach() if SNN else x4 113 | x5 = self.conv5(x5_input) 114 | x6_input = x5.detach() if SNN else x5 115 | x6 = self.conv6(x6_input) 116 | x7_input = x6.detach() if SNN else x6 117 | x7 = self.conv7(x7_input) 118 | x7 = self.pool7(x7) 119 | x8_input = x7.detach() if SNN else x7 120 | x8 = self.conv8(x8_input) 121 | x9_input = x8.detach() if SNN else x8 122 | x9 = self.conv9(x9_input) 123 | x10_input = x9.detach() if SNN else x9 124 | x10 = self.conv10(x10_input) 125 | x10 = self.pool10(x10) 126 | x11_input = x10.detach() if SNN else x10 127 | x11 = self.conv11(x11_input) 128 | x12_input = x11.detach() if SNN else x11 129 | x12 = self.conv12(x12_input) 130 | x13_input = x12.detach() if SNN else x12 131 | x13 = self.conv13(x13_input) 132 | x13 = self.pool13(x13) 133 | 134 | # FC Layers 135 | x13 = x13.view(x13.size(0), -1) 136 | x14_input = x13.detach() if SNN else x13 137 | x14 = self.fc14(x14_input) 138 | x15_input = x14.detach() if SNN else x14 139 | x15 = self.fc15(x15_input) 140 | x16_input = x15.detach() if SNN else x15 141 | if not TTFS: 142 | out = self.fc16(x16_input) 143 | else: 144 | out = x16_input 145 | hidden_act = [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15] 146 | 147 | return hidden_act, out 148 | 149 | def shapes(self, x): 150 | hidden = [] 151 | for m in self.modules(): 152 | #print(m) 153 | if isinstance(m, nn.Conv2d) or isinstance(m, nn.BatchNorm2d) \ 154 | or isinstance(m, nn.Dropout) or isinstance(m, MyFloor) or isinstance(m, nn.Flatten) \ 155 | or isinstance(m, nn.MaxPool2d) or isinstance(m, nn.AvgPool2d) or isinstance(m, ScaledNeuron) : 156 | prev_x = x 157 | #print(x.size()) 158 | x = m(x) 159 | if isinstance(m, nn.Conv2d): 160 | hidden.append(prev_x) 161 | elif isinstance(m, nn.Linear): 162 | prev_x = x 163 | #print(x.size()) 164 | x = m(x.view(x.size(0), -1)) 165 | 166 | hidden.append(prev_x) 167 | return hidden, x -------------------------------------------------------------------------------- /utils/__pycache__/classification.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/utils/__pycache__/classification.cpython-39.pyc -------------------------------------------------------------------------------- /utils/__pycache__/lib.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/utils/__pycache__/lib.cpython-39.pyc -------------------------------------------------------------------------------- /utils/__pycache__/modules.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/utils/__pycache__/modules.cpython-39.pyc -------------------------------------------------------------------------------- /utils/__pycache__/utils.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xychen-comp/Hybrid-Coding-SNN/b7206abdf766ba0b73bf25fd5aec9bba7b0e52f7/utils/__pycache__/utils.cpython-39.pyc -------------------------------------------------------------------------------- /utils/classification.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from utils.utils import reset_net, get_neuron_threshold 3 | 4 | 5 | # Testing pre-trained ANN -- CIFAR 6 | def testing(model, testLoader, criterion, device): 7 | model.eval() # Put the model in test mode 8 | 9 | running_loss = 0.0 10 | correct = 0 11 | total = 0 12 | for data in testLoader: 13 | inputs, labels = data 14 | 15 | # Transfer to GPU 16 | inputs, labels = inputs.type(torch.FloatTensor).to(device), \ 17 | labels.type(torch.LongTensor).to(device) 18 | 19 | # forward pass 20 | _, y_pred = model.forward(inputs) 21 | loss = criterion(y_pred, labels) 22 | _, predicted = torch.max(y_pred.data, 1) 23 | total += labels.size(0) 24 | correct += (predicted == labels).sum().item() 25 | running_loss += loss.item() 26 | 27 | # calculate epoch statistics 28 | epoch_loss = running_loss / len(testLoader) 29 | acc = correct / total 30 | 31 | return acc, epoch_loss 32 | 33 | # Testing LTL-fine-tuned SNN -- CIFAR 34 | def testing_snn_Burst(snn, testLoader, device, T): 35 | tot = torch.zeros(T).to(device) 36 | spk = [0] * T 37 | spk_cnt = [0] * T 38 | length = 0 39 | model = snn.to(device) 40 | model.eval() 41 | # evaluate 42 | threshold = get_neuron_threshold(snn) 43 | with torch.no_grad(): 44 | for idx, (inputs, label) in enumerate(testLoader): 45 | spikes = 0 46 | length += len(label) 47 | inputs = inputs.to(device) 48 | label = label.to(device) 49 | for t in range(T): 50 | hidden, out = model(inputs, SNN=True) 51 | spikes += out 52 | tot[t] += (label==spikes.max(1)[1]).sum() 53 | spk[t] += (((hidden[-1] > 0).float()).sum()).item() 54 | spk_cnt[t] += (((hidden[-1]).float()).sum()).item() 55 | reset_net(model) 56 | 57 | spk_cnt = [(x / threshold[-1]) / length for x in spk_cnt] 58 | spk_cnt = ['%.2f' % n for n in spk_cnt] 59 | spk = [x/length for x in spk] 60 | return tot/length, spk, spk_cnt 61 | 62 | # TTFS learning testing -- CIFAR 63 | def testing_snn_TTFS(snn, ttfs_model, testloader, device, sim_len, threshold): 64 | correct = torch.zeros(sim_len).to(device) 65 | total = 0 66 | ttfs_model.eval() 67 | snn = snn.to(device) 68 | snn.eval() 69 | avg_spk_time = torch.zeros(sim_len).to(device) 70 | hidden_threshold = get_neuron_threshold(snn) 71 | out_count = torch.zeros(sim_len).to(device) 72 | 73 | for i_batch, (inputs, labels) in enumerate(testloader, 1): 74 | # Transfer to GPU 75 | inputs = inputs.type(torch.FloatTensor).to(device) 76 | labels = labels.type(torch.LongTensor).to(device) 77 | 78 | total += len(labels) 79 | spk_counts = [] 80 | end_symbol = torch.zeros(inputs.size(0),1,1,1).to(device) 81 | 82 | for t in range(sim_len): 83 | inputs = inputs * (1 - end_symbol) # Once the first output spike is detected for a sample, the simulation of this sample ends with inputting zero. 84 | 85 | # Get the last hidden layers output 86 | _, out = snn.forward(inputs, SNN=True, TTFS=True) # spk and mem size [N, Class] 87 | spk_count = out / hidden_threshold[-1] # burst count of the last hidden layer 88 | spk_counts.append(spk_count) 89 | count_t = torch.stack(spk_counts).permute(1, 2, 0) 90 | 91 | # TTFS 92 | V_out, spikes = ttfs_model(threshold, count_t) # [N, T, Class] 93 | 94 | # Get t_f 95 | t_idx = torch.arange(0, t + 1).unsqueeze(0).unsqueeze(2).repeat(spikes.size(0), 1, spikes.size(2)).to(device) # [N,t+1,10] 96 | timing = (t + 1 - t_idx) * spikes 97 | boolFire = spikes.sum(dim=1, keepdim=True) > 0 98 | t_f = torch.argmax(timing.float(), dim=1, keepdim=True) # [N,1,Class] 99 | t_last = torch.ones_like(t_f) * t 100 | t_f = torch.where(boolFire, t_f, t_last) # Once one neuron never fire, it will be considered as fire at the last timestep t 101 | t_f = torch.min(t_f, 2)[0].unsqueeze(2).repeat(1, 1, t_f.size(2)) 102 | 103 | V_t_f = torch.gather(V_out, 1, t_f).squeeze(1) 104 | correct[t] += (labels == V_t_f.argmax(dim=1)).sum().item() 105 | avg_spk_time[t] += t_f.float().sum() / t_f.size(2) 106 | end_symbol = (boolFire.sum(dim=2) > 0).view(-1,1,1,1).float() # The samples that detect the first end. 107 | 108 | reset_net(snn) 109 | 110 | # Distribution Analysis 111 | for t in range(sim_len): 112 | out_count[t] += (t_f[:,-1,-1] == t).float().sum().item() 113 | 114 | return correct.max()/total, (avg_spk_time[torch.argmax(correct)]/total) + 1, out_count/total 115 | 116 | 117 | 118 | # Training pre-trained ANN -- CIFAR 119 | def training(model, trainloader, optimizer, criterion, device): 120 | model.train() # Put the model in train mode 121 | 122 | running_loss = 0.0 123 | total = 0 124 | correct = 0 125 | #cnt =0 126 | for i_batch, (inputs, labels) in enumerate(trainloader, 1): 127 | 128 | # Transfer to GPU 129 | inputs, labels = inputs.type(torch.FloatTensor).to(device), \ 130 | labels.type(torch.LongTensor).to(device) 131 | 132 | # Model computation and weight update 133 | _, y_pred = model.forward(inputs) 134 | loss = criterion(y_pred, labels) 135 | _, predicted = torch.max(y_pred.data, dim=1) 136 | total += labels.size(0) 137 | correct += (predicted == labels).sum().item() 138 | 139 | optimizer.zero_grad() 140 | loss.backward(retain_graph=True) 141 | optimizer.step() 142 | running_loss += loss.item() 143 | 144 | epoch_loss = running_loss / i_batch 145 | acc_train = correct / total 146 | 147 | return model, acc_train, epoch_loss 148 | 149 | 150 | # LTL-fine-tuning SNN -- CIFAR 151 | def training_thNorm_with_T(ann, snn, trainloader, optimizer, criterion_out, criterion_local, coeff_local, device, T, gamma=5): 152 | """SNN fine-tune with threshold normalization""" 153 | snn.train() # Put the model in train mode 154 | ann.eval() 155 | 156 | running_loss = 0.0 157 | total = 0 158 | correct = 0 159 | 160 | for i_batch, (inputs, labels) in enumerate(trainloader, 1): 161 | # Transfer to GPU 162 | inputs, labels = inputs.type(torch.FloatTensor).to(device), \ 163 | labels.type(torch.LongTensor).to(device) 164 | 165 | # Model computation and weight update 166 | hiddenA, _ = ann.forward(inputs) 167 | y_pred = 0 168 | hiddenS = 0 169 | for t in range(T): 170 | out_ = snn.forward(inputs, SNN=True) 171 | hidden_cached = out_[0] 172 | y_pred += out_[1] 173 | if t == 0:# for the first step 174 | hiddenS = [x.clone() for x in hidden_cached] 175 | else: 176 | for iLayer in range(len(hidden_cached)): 177 | hiddenS[iLayer] += hidden_cached[iLayer] 178 | hiddenS = [x/T for x in hiddenS] 179 | y_pred = y_pred / T 180 | 181 | loss = criterion_out(y_pred, labels) 182 | # Compute local loss 183 | for (A, S, C) in zip(hiddenA, hiddenS, coeff_local): 184 | loss += C * criterion_local(S, A*gamma) 185 | 186 | _, predicted = torch.max(y_pred.data, dim=1) 187 | total += labels.size(0) 188 | correct += (predicted == labels).sum().item() 189 | 190 | optimizer.zero_grad() 191 | loss.backward(retain_graph=True) 192 | optimizer.step() 193 | running_loss += loss.item() 194 | reset_net(snn) 195 | 196 | epoch_loss = running_loss / i_batch 197 | acc_train = correct / total 198 | 199 | return snn, acc_train, epoch_loss 200 | 201 | 202 | # TTFS learning training -- CIFAR 203 | def training_snn_TTFS(snn, ttfs_model, trainloader, optimizer, loss_fn, alpha, beta, device, sim_len, threshold): 204 | snn.eval() # Put the model in test mode 205 | avg_spk_time = 0 206 | avg_spk_pro = 0 207 | running_loss = 0.0 208 | running_loss1 = 0.0 209 | running_loss2 = 0.0 210 | total = 0 211 | correct = 0 212 | hidden_threshold = get_neuron_threshold(snn) 213 | ttfs_model.train() 214 | 215 | for i_batch, (inputs, labels) in enumerate(trainloader, 1): 216 | # Transfer to GPU 217 | inputs = inputs.type(torch.FloatTensor).to(device) 218 | labels = labels.type(torch.LongTensor).to(device) 219 | 220 | label_ = torch.zeros(len(inputs), ttfs_model.LIF.fc.out_features).to(device) 221 | label_ = label_.scatter_(1, labels.view(-1, 1), 1) 222 | spk_counts = [] 223 | 224 | # Get the last hidden layers output 225 | for t in range(sim_len): 226 | _, out = snn.forward(inputs, SNN=True, TTFS=True) 227 | spk_count = out / hidden_threshold[-1] 228 | spk_counts.append(spk_count) 229 | 230 | count_t = torch.stack(spk_counts, dim=0).permute(1, 2, 0).detach() # [N, hid_dim, T]) 231 | reset_net(snn) 232 | 233 | # TTFS 234 | V_out, spikes = ttfs_model(threshold, count_t) # [N, T, Class] 235 | 236 | # Get t_f 237 | with torch.no_grad(): 238 | t_idx = torch.arange(0, sim_len).unsqueeze(0).unsqueeze(2).repeat(spikes.size(0), 1, spikes.size(2)).to(device) 239 | timing = (sim_len - t_idx) * spikes 240 | boolFire = spikes.sum(dim=1, keepdim=True) > 0 241 | t_f = torch.argmax(timing, dim=1, keepdim=True) # [N,1,Class] 242 | last = torch.ones_like(t_f) * (sim_len -1) 243 | t_f = torch.where(boolFire, t_f, last) # Once one neuron never fire, it will be considered as fire at the last timestep 244 | t_f = torch.min(t_f, 2)[0].unsqueeze(2).repeat(1, 1, t_f.size(2)) # [N,1,Class] 245 | 246 | V_t_f = torch.gather(V_out, 1, t_f).squeeze(1) # [N,10] 247 | 248 | # Computing loss 249 | LTD_mask = ((V_t_f >= threshold) * (1 - label_)).detach() 250 | theta = 1 - ((V_t_f.argmax(dim=1) == labels).sum() / (V_out[:,-1,:].argmax(dim=1) == labels).sum()).clamp(0,1).item() # Scaling factor 251 | 252 | loss1 = loss_fn(V_t_f, labels) 253 | loss2 = (theta * (V_t_f - threshold) * LTD_mask).sum() 254 | 255 | loss = alpha * loss1 + beta * loss2 256 | 257 | optimizer.zero_grad() 258 | loss.backward() 259 | optimizer.step() 260 | 261 | running_loss += loss.item() 262 | running_loss1 += loss1.item() 263 | running_loss2 += loss2.item() 264 | avg_spk_pro += boolFire.float().mean() 265 | avg_spk_time += t_f.float().sum() / t_f.size(2) 266 | correct += (V_t_f.argmax(dim=1) == labels).sum().item() 267 | total += labels.size(0) 268 | 269 | if i_batch % 100 == 0: 270 | print('Batch {}, Train Acc {:.4f}, Train Loss {:.4f}, Avg spike time {:.4f}, spike probability {:.4f}' 271 | .format(i_batch, (correct / total) * 100, running_loss / i_batch, (avg_spk_time / total) + 1, 272 | avg_spk_pro / i_batch)) 273 | 274 | epoch_loss = running_loss / i_batch 275 | epoch_loss1 = running_loss1 / i_batch 276 | epoch_loss2 = running_loss2 / i_batch 277 | print('Train loss: ', epoch_loss1, epoch_loss2) 278 | 279 | return ttfs_model, correct / total, epoch_loss 280 | 281 | -------------------------------------------------------------------------------- /utils/lib.py: -------------------------------------------------------------------------------- 1 | '''Some helper functions for PyTorch, including: 2 | - get_mean_and_std: calculate the mean and std value of dataset. 3 | - msr_init: net parameter initialization. 4 | - progress_bar: progress bar mimic xlua.progress. 5 | ''' 6 | import os 7 | import sys 8 | import time 9 | import math 10 | import json 11 | import logging 12 | 13 | import torch 14 | import torch.nn as nn 15 | import torch.nn.init as init 16 | import numpy as np 17 | import random 18 | from collections import OrderedDict 19 | from typing import Union 20 | 21 | from torch import Tensor 22 | 23 | A_list=[[16,15,14,13,12,11], #1 24 | [16,15,14,13,10, 9, 8, 7], #2 25 | [16,15,14,12,11,10, 6, 5], #3 26 | [16,15,13,12,11,9,8,4], #4 27 | [16,15,14,13,12,10,9,7,6,3],#5 28 | [16,15,14,13,11,10,8,5], #6 29 | [16,15,14,13,12,11,9,7], #7 30 | [16,14,12,10,8,6,4,2], #8 31 | [16,15,14,13,12,11,9,7], #9 32 | [16,15,14,13,11,10,8,5], #10 33 | [16,15,14,13,12,10,9,7,6,3], #11 34 | [16,15,13,12,11,9,8,4], #12 35 | [16,15,14,12,11,10,6,5], #13 36 | [16,15,14,13,10,9,8,7], #14 37 | [16,15,14,13,12,11], #15 38 | [16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], #16 39 | ] 40 | 41 | 42 | def freezeLayer(model, layer_list, layer_idx): 43 | ''' freeze the fine-tuned layer ''' 44 | if isinstance(layer_list[layer_idx], list): 45 | for layer_name in layer_list[layer_idx]: 46 | layer = eval('model.' + str(layer_name)) 47 | for param in layer.parameters(): 48 | param.requires_grad = False 49 | else: 50 | layer = eval('model.' + str(layer_list[layer_idx])) 51 | for param in layer.parameters(): 52 | param.requires_grad = False 53 | return model 54 | 55 | def set_seed(seed): 56 | torch.manual_seed(seed) 57 | torch.cuda.manual_seed(seed) 58 | torch.cuda.manual_seed_all(seed) # if you are using multi-GPU. 59 | np.random.seed(seed) # Numpy module. 60 | random.seed(seed) # Python random module. 61 | 62 | # torch.set_deterministic(True) 63 | # torch.backends.cudnn.enabled = False 64 | torch.backends.cudnn.benchmark = False 65 | torch.backends.cudnn.deterministic = True 66 | # os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' 67 | os.environ['PYTHONHASHSEED'] = str(seed) 68 | 69 | def Quantize_lvl(tensor, level=16): 70 | # print(min_float, max_float) 71 | min_float = tensor.min().item() 72 | max_float = tensor.max().item() 73 | tensor.clamp_(min_float, max_float) 74 | scale = (max_float- min_float)/level 75 | #min_float_adjusted = min_float + (-min_float)%scale 76 | tensor = (tensor - min_float).div(scale).round().mul(scale) + min_float 77 | return tensor 78 | 79 | def Quantize(tensor, numBits=8): 80 | # print(min_float, max_float) 81 | min_float = tensor.min().item() 82 | max_float = tensor.max().item() 83 | tensor.clamp_(min_float, max_float) 84 | scale = (max_float - min_float) / (2 ** numBits - 1) 85 | min_float_adjusted = min_float + (-min_float) % scale 86 | tensor = (tensor - min_float_adjusted).div(scale).round().mul(scale) + min_float_adjusted 87 | return tensor 88 | 89 | def get_SNN_layer_output(model, input, T, layer_list, iLayer): 90 | ''' get layer-wise output ''' 91 | # Get the layer 92 | if isinstance(layer_list[iLayer], list): # is layer contain pool 93 | for layer_name in layer_list[iLayer]: 94 | if layer_name.startswith('conv'): 95 | layer = eval('model.conv' + str(iLayer + 1)) 96 | elif layer_name.startswith('pool'): 97 | layer_pool = eval('model.pool' + str(iLayer + 1)) 98 | else: 99 | if layer_list[iLayer].startswith('conv'): 100 | layer = eval('model.conv' + str(iLayer + 1)) 101 | elif layer_list[iLayer].startswith('fc'): 102 | layer = eval('model.fc' + str(iLayer + 1)) 103 | else: 104 | print('No supported layers!') 105 | 106 | # Inference 107 | x = input/T if iLayer !=0 else input # Average to each T 108 | spk_cnt = [0] * T 109 | outputs =0 110 | for t in range(T): 111 | if isinstance(layer_list[iLayer], list): 112 | x = layer(x) 113 | output = layer_pool(x) 114 | else: 115 | output = layer(x) 116 | 117 | outputs += output 118 | spk_cnt[t] += (((output > 0).float()).sum()).item() 119 | #spk_cnt = [x / input.size(0) for x in spk_cnt] 120 | return outputs/T, spk_cnt 121 | 122 | 123 | def Channel_Norm(model, data_loader, device, HidLayer, p=99): 124 | """Perform channel normalization""" 125 | model.eval() # Put the model in test mode 126 | 127 | scale_layer_list = [[]] * HidLayer 128 | #cnt = 0 129 | for i_batch, (inputs, labels) in enumerate(data_loader, 1): 130 | #cnt += 1 131 | #if cnt > 50: 132 | # break 133 | 134 | # Transfer to GPU 135 | inputs = inputs.type(torch.FloatTensor).to(device) 136 | 137 | # forward pass to get channelwise activation values 138 | with torch.no_grad(): 139 | hiddenA, _ = model.forward(inputs) 140 | 141 | for iLayer, A in enumerate(hiddenA): 142 | if len(A.shape) > 3: # for Conv layer 143 | scale = percentile_ch(A, p) 144 | scale = scale.to(device) 145 | else: # for Linear layer 146 | scale = percentile(A, p) 147 | scale = torch.tensor(scale).to(device) 148 | 149 | if i_batch == 1: #init the list 150 | scale_layer_list[iLayer] = scale 151 | else: 152 | scale_layer_list[iLayer] = torch.add(scale_layer_list[iLayer], scale) 153 | 154 | scale_list = [x/i_batch for x in scale_layer_list] 155 | return scale_list 156 | 157 | 158 | def Channel_Norm_DDP(model, data_loader, args, p=99): 159 | """Perform channel normalization""" 160 | model.eval() # Put the model in test mode 161 | 162 | scale_layer_list = [[]] * 15 163 | for i_batch, (inputs, labels) in enumerate(data_loader, 1): 164 | # Transfer to GPU 165 | if args.gpu is not None: 166 | inputs = inputs.cuda(args.gpu, non_blocking=True) 167 | #inputs = inputs.type(torch.FloatTensor).to(args) 168 | 169 | # forward pass to get channelwise activation values 170 | with torch.no_grad(): 171 | hiddenA, _ = model.forward(inputs) 172 | 173 | for iLayer, A in enumerate(hiddenA): 174 | if len(A.shape) > 3: # for Conv layer 175 | scale = percentile_ch(A, p) 176 | scale = scale.cuda(args.gpu, non_blocking=True) 177 | else: # for Linear layer 178 | scale = percentile(A, p) 179 | scale = torch.tensor(scale).cuda(args.gpu, non_blocking=True) 180 | 181 | if i_batch == 1: #init the list 182 | scale_layer_list[iLayer] = scale 183 | else: 184 | scale_layer_list[iLayer] = torch.add(scale_layer_list[iLayer], scale) 185 | 186 | scale_list = [x/i_batch for x in scale_layer_list] 187 | return scale_list 188 | 189 | 190 | 191 | def weights_init(m, wInit, bInit): 192 | if isinstance(m, nn.Conv2d): 193 | m.bias.data = bInit 194 | m.weight.data = wInit 195 | elif isinstance(m, nn.Linear): 196 | m.bias.data = bInit 197 | m.weight.data = wInit 198 | 199 | 200 | def get_mean_and_std(dataset): 201 | '''Compute the mean and std value of dataset.''' 202 | dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=2) 203 | mean = torch.zeros(3) 204 | std = torch.zeros(3) 205 | print('==> Computing mean and std..') 206 | for inputs, targets in dataloader: 207 | for i in range(3): 208 | mean[i] += inputs[:, i, :, :].mean() 209 | std[i] += inputs[:, i, :, :].std() 210 | mean.div_(len(dataset)) 211 | std.div_(len(dataset)) 212 | return mean, std 213 | 214 | 215 | def init_params(net): 216 | '''Init layer parameters.''' 217 | for m in net.modules(): 218 | if isinstance(m, nn.Conv2d): 219 | init.kaiming_normal(m.weight, mode='fan_out') 220 | if m.bias: 221 | init.constant(m.bias, 0) 222 | elif isinstance(m, nn.BatchNorm2d): 223 | init.constant(m.weight, 1) 224 | init.constant(m.bias, 0) 225 | elif isinstance(m, nn.Linear): 226 | init.normal(m.weight, std=1e-3) 227 | if m.bias: 228 | init.constant(m.bias, 0) 229 | 230 | 231 | # _, term_width = os.popen('stty size', 'r').read().split() 232 | # term_width = int(term_width) 233 | 234 | TOTAL_BAR_LENGTH = 65. 235 | last_time = time.time() 236 | begin_time = last_time 237 | 238 | 239 | def progress_bar(current, total, msg=None): 240 | global last_time, begin_time 241 | if current == 0: 242 | begin_time = time.time() # Reset for new bar. 243 | 244 | cur_len = int(TOTAL_BAR_LENGTH * current / total) 245 | rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1 246 | 247 | sys.stdout.write(' [') 248 | for i in range(cur_len): 249 | sys.stdout.write('=') 250 | sys.stdout.write('>') 251 | for i in range(rest_len): 252 | sys.stdout.write('.') 253 | sys.stdout.write(']') 254 | 255 | cur_time = time.time() 256 | step_time = cur_time - last_time 257 | last_time = cur_time 258 | tot_time = cur_time - begin_time 259 | 260 | L = [] 261 | L.append(' Step: %s' % format_time(step_time)) 262 | L.append(' | Tot: %s' % format_time(tot_time)) 263 | if msg: 264 | L.append(' | ' + msg) 265 | 266 | msg = ''.join(L) 267 | sys.stdout.write(msg) 268 | for i in range(term_width - int(TOTAL_BAR_LENGTH) - len(msg) - 3): 269 | sys.stdout.write(' ') 270 | 271 | # Go back to the center of the bar. 272 | for i in range(term_width - int(TOTAL_BAR_LENGTH / 2) + 2): 273 | sys.stdout.write('\b') 274 | sys.stdout.write(' %d/%d ' % (current + 1, total)) 275 | 276 | if current < total - 1: 277 | sys.stdout.write('\r') 278 | else: 279 | sys.stdout.write('\n') 280 | sys.stdout.flush() 281 | 282 | 283 | def format_time(seconds): 284 | days = int(seconds / 3600 / 24) 285 | seconds = seconds - days * 3600 * 24 286 | hours = int(seconds / 3600) 287 | seconds = seconds - hours * 3600 288 | minutes = int(seconds / 60) 289 | seconds = seconds - minutes * 60 290 | secondsf = int(seconds) 291 | seconds = seconds - secondsf 292 | millis = int(seconds * 1000) 293 | 294 | f = '' 295 | i = 1 296 | if days > 0: 297 | f += str(days) + 'D' 298 | i += 1 299 | if hours > 0 and i <= 2: 300 | f += str(hours) + 'h' 301 | i += 1 302 | if minutes > 0 and i <= 2: 303 | f += str(minutes) + 'm' 304 | i += 1 305 | if secondsf > 0 and i <= 2: 306 | f += str(secondsf) + 's' 307 | i += 1 308 | if millis > 0 and i <= 2: 309 | f += str(millis) + 'ms' 310 | i += 1 311 | if f == '': 312 | f = '0ms' 313 | return f 314 | 315 | 316 | def state_dict_data_parallel(state_dict): 317 | """# remove 'module.' of for model trained with dataParallel """ 318 | 319 | new_state_dict = OrderedDict() 320 | 321 | for k, v in state_dict.items(): 322 | name = k[7:] # remove 'module.' 323 | new_state_dict[name] = v 324 | 325 | return new_state_dict 326 | 327 | 328 | class AverageMeter(object): 329 | """Computes and stores the average and current value""" 330 | 331 | def __init__(self, name, fmt=':f'): 332 | self.name = name 333 | self.fmt = fmt 334 | self.reset() 335 | 336 | def reset(self): 337 | self.val = 0 338 | self.avg = 0 339 | self.sum = 0 340 | self.count = 0 341 | 342 | def update(self, val, n=1): 343 | self.val = val 344 | self.sum += val * n 345 | self.count += n 346 | self.avg = self.sum / self.count 347 | 348 | def __str__(self): 349 | fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})' 350 | return fmtstr.format(**self.__dict__) 351 | 352 | 353 | class ProgressMeter(object): 354 | def __init__(self, num_batches, meters, prefix=""): 355 | self.batch_fmtstr = self._get_batch_fmtstr(num_batches) 356 | self.meters = meters 357 | self.prefix = prefix 358 | 359 | def display(self, batch): 360 | entries = [self.prefix + self.batch_fmtstr.format(batch)] 361 | entries += [str(meter) for meter in self.meters] 362 | print('\t'.join(entries)) 363 | 364 | def _get_batch_fmtstr(self, num_batches): 365 | num_digits = len(str(num_batches // 1)) 366 | fmt = '{:' + str(num_digits) + 'd}' 367 | return '[' + fmt + '/' + fmt.format(num_batches) + ']' 368 | 369 | 370 | def adjust_learning_rate(optimizer, epoch, args): 371 | """Sets the learning rate to the initial LR decayed by 10 every 30 epochs""" 372 | lr = args.lr * (0.1 ** (epoch // 30)) 373 | for param_group in optimizer.param_groups: 374 | param_group['lr'] = lr 375 | 376 | 377 | def accuracy(output, target, topk=(1,)): 378 | """Computes the accuracy over the k top predictions for the specified values of k""" 379 | with torch.no_grad(): 380 | maxk = max(topk) 381 | batch_size = target.size(0) 382 | 383 | _, pred = output.topk(maxk, 1, True, True) 384 | pred = pred.t() 385 | correct = pred.eq(target.view(1, -1).expand_as(pred)) 386 | 387 | res = [] 388 | for k in topk: 389 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 390 | res.append(correct_k.mul_(100.0 / batch_size)) 391 | return res 392 | 393 | @torch.no_grad() 394 | def concat_all_gather(tensor): 395 | """ 396 | Performs all_gather operation on the provided tensors. 397 | *** Warning ***: torch.distributed.all_gather has no gradient. 398 | """ 399 | tensors_gather = [torch.ones_like(tensor) 400 | for _ in range(torch.distributed.get_world_size())] 401 | torch.distributed.all_gather(tensors_gather, tensor, async_op=False) 402 | 403 | output = torch.cat(tensors_gather, dim=0) 404 | return output 405 | 406 | 407 | # def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'): 408 | # torch.save(state, filename) 409 | # if is_best: 410 | # shutil.copyfile(filename, 'model_best.pth.tar') 411 | 412 | def save_checkpoint(epoch, model, optimizer, ckp_dir, best=True): 413 | if not os.path.isdir(ckp_dir): 414 | os.mkdir(ckp_dir) 415 | 416 | state = { 417 | 'epoch': epoch, 418 | 'model_state_dict': model.state_dict(), 419 | 'optimizer_state_dict': optimizer.state_dict(), 420 | } 421 | torch.save(state, os.path.join(ckp_dir, "{0}.pt.tar".format("best" if best else "last"))) 422 | 423 | 424 | def dump_json(obj, fdir, name): 425 | """ 426 | Dump python object in json 427 | """ 428 | if fdir and not os.path.exists(fdir): 429 | os.makedirs(fdir) 430 | with open(os.path.join(fdir, name), "w") as f: 431 | json.dump(obj, f, indent=4, sort_keys=False) 432 | 433 | 434 | def load_json(fdir, name): 435 | """ 436 | Load json as python object 437 | """ 438 | path = os.path.join(fdir, name) 439 | if not os.path.exists(path): 440 | raise FileNotFoundError("Could not find json file: {}".format(path)) 441 | with open(path, "r") as f: 442 | obj = json.load(f) 443 | return obj 444 | 445 | 446 | def get_logger( 447 | name, 448 | format_str="%(asctime)s [%(pathname)s:%(lineno)s - %(levelname)s ] %(message)s", 449 | date_format="%Y-%m-%d %H:%M:%S", 450 | file=False): 451 | """ 452 | Get python logger instance 453 | """ 454 | logger = logging.getLogger(name) 455 | logger.setLevel(logging.INFO) 456 | # file or console 457 | handler = logging.StreamHandler() if not file else logging.FileHandler( 458 | name) 459 | handler.setLevel(logging.INFO) 460 | formatter = logging.Formatter(fmt=format_str, datefmt=date_format) 461 | handler.setFormatter(formatter) 462 | logger.addHandler(handler) 463 | 464 | return logger 465 | 466 | 467 | def percentile(t: torch.tensor, q: float) -> Union[int, float]: 468 | """ 469 | Return the ``q``-th percentile of the flattened input tensor's data. 470 | 471 | CAUTION: 472 | * Needs PyTorch >= 1.1.0, as ``torch.kthvalue()`` is used. 473 | * Values are not interpolated, which corresponds to 474 | ``numpy.percentile(..., interpolation="nearest")``. 475 | 476 | :param t: Input tensor. 477 | :param q: Percentile to compute, which must be between 0 and 100 inclusive. 478 | :return: Resulting value (scalar). 479 | """ 480 | # Note that ``kthvalue()`` works one-based, i.e. the first sorted value 481 | # indeed corresponds to k=1, not k=0! Use float(q) instead of q directly, 482 | # so that ``round()`` returns an integer, even if q is a np.float32. 483 | k = 1 + round(.01 * float(q) * (t.numel() - 1)) 484 | result = t.view(-1).kthvalue(k).values.item() 485 | return result 486 | 487 | 488 | def percentile_ch(t: torch.tensor, q: float) -> Tensor: 489 | """ 490 | Return the channel-wise ``q``-th percentile of the input tensor's data. 491 | """ 492 | result = [] 493 | for ch in range(t.size(1)): 494 | t_ch = t[:, ch, :, :] 495 | k_ch = 1 + round(.01 * float(q) * (t_ch.numel() - 1)) 496 | result_ch = t_ch.reshape(-1).kthvalue(k_ch).values.item() 497 | if result_ch == 0: 498 | result_ch = 1e-8 499 | result.append(result_ch) 500 | result = torch.tensor(result) 501 | return result 502 | 503 | 504 | def channel_norm(t: torch.tensor, q: float) -> Tensor: 505 | """ 506 | Return the channel-wise normalization of the input tensor's data. 507 | """ 508 | A_norm = [] 509 | for ch in range(t.size(1)): 510 | t_ch = t[:, ch, :, :] 511 | k_ch = 1 + round(.01 * float(q) * (t_ch.numel() - 1)) 512 | result_ch = t_ch.reshape(-1).kthvalue(k_ch).values.item() 513 | if result_ch == 0: 514 | result_ch = 1e-8 515 | t_norm = torch.clamp(t_ch / result_ch, min=0, max=1.0) 516 | A_norm.append(t_norm) 517 | A_norm = torch.stack(A_norm).permute(1, 0, 2, 3) 518 | return A_norm 519 | -------------------------------------------------------------------------------- /utils/modules.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | import torch.nn.functional as F 3 | import numpy as np 4 | import torch 5 | from torch.autograd import Function 6 | from spikingjelly.clock_driven import neuron 7 | 8 | class StraightThrough(nn.Module): 9 | def __init__(self, channel_num: int = 1): 10 | super().__init__() 11 | 12 | def forward(self, input): 13 | return input 14 | 15 | class ScaledNeuron(nn.Module): 16 | def __init__(self, scale=1.0, Burst=False, gamma=5): 17 | super(ScaledNeuron, self).__init__() 18 | self.scale = scale 19 | self.t = 0 20 | self.neuron = neuron.IFNode(v_threshold=1.0, v_reset=None) if not Burst else BurstNode(gamma=gamma) 21 | def forward(self, x): 22 | x = x / self.scale 23 | if self.t == 0: 24 | self.neuron(torch.ones_like(x)*0.0) 25 | x = self.neuron(x) 26 | self.t += 1 27 | return x * self.scale 28 | def reset(self): 29 | self.t = 0 30 | self.neuron.reset() 31 | 32 | class BurstNode(nn.Module): 33 | """Burst neurons in hidden layers""" 34 | def __init__(self, gamma): 35 | super(BurstNode, self).__init__() 36 | self.mem = 0 37 | self.spike = 0 38 | self.sum = 0 39 | self.threshold = 1.0 40 | self.summem = 0 41 | self.gamma = gamma 42 | 43 | def reset(self): 44 | self.mem = 0 45 | self.spike = 0 46 | 47 | def forward(self, x): 48 | self.mem = self.mem + x 49 | self.spike = myfloor((self.mem / self.threshold)).clamp(min=0, max=self.gamma) 50 | self.mem = self.mem - self.spike * self.threshold 51 | out = self.spike 52 | return out 53 | 54 | class MyFloor(nn.Module): 55 | def __init__(self, up=8., t=32): 56 | super().__init__() 57 | self.up = nn.Parameter(torch.tensor([up]), requires_grad=True) 58 | self.t = t 59 | 60 | def forward(self, x): 61 | x = x / self.up 62 | x = myfloor(x*self.t+0.5)/self.t 63 | x = torch.clamp(x, 0, 1) 64 | x = x * self.up 65 | return x 66 | 67 | class GradFloor(Function): 68 | @staticmethod 69 | def forward(ctx, input): 70 | return input.floor() 71 | 72 | @staticmethod 73 | def backward(ctx, grad_output): 74 | return grad_output 75 | 76 | class GradFloor_partial(Function): 77 | @staticmethod 78 | def forward(ctx, input, mask): 79 | out = input.floor() 80 | isSpike = (out > 0).float() 81 | out = torch.where(mask == 0, isSpike, out) 82 | ctx.save_for_backward(isSpike, mask) 83 | return out 84 | 85 | @staticmethod 86 | def backward(ctx, grad_output): 87 | #pydevd.settrace(suspend=False, trace_only_current_thread=True) # for debuger 88 | isSpike, mask = ctx.saved_tensors 89 | grad_output = torch.where(mask == 0, isSpike, grad_output) 90 | return grad_output, None 91 | 92 | myfloor = GradFloor.apply 93 | myfloor_partial = GradFloor_partial.apply 94 | -------------------------------------------------------------------------------- /utils/utils.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import numpy as np 4 | from typing import Callable, Tuple, List, Union, Dict, cast 5 | from distributed_utils.dist_helper import allaverage 6 | from utils.modules import ScaledNeuron, StraightThrough 7 | 8 | def isActivation(name): 9 | if 'relu' in name.lower(): 10 | return True 11 | return False 12 | 13 | def isSNNneuron(name): 14 | if 'scaledneuron' in name.lower(): 15 | return True 16 | return False 17 | 18 | def replace_activation_by_neuron(model): 19 | for name, module in model._modules.items(): 20 | if hasattr(module,"_modules"): 21 | model._modules[name] = replace_activation_by_neuron(module) 22 | if name.startswith('relu'): # for res BLK: 23 | model._modules[name] = ScaledNeuron(scale=model.residual_function[3].threshold.item()) 24 | else: 25 | if isActivation(module.__class__.__name__.lower()): 26 | if hasattr(model[0], "threshold"): 27 | model._modules[name] = ScaledNeuron(scale=model[0].threshold.item()) 28 | else: 29 | model._modules[name] = ScaledNeuron(scale=1.) 30 | return model 31 | 32 | def get_layer_shape(model, data, device): 33 | data = data.to(device) 34 | hidden = model.forward(data, SNN=True, isGetShape=True) 35 | return [x.squeeze(0) for x in hidden] 36 | 37 | def replace_IF_by_Burst_all(model,gamma): 38 | for name, module in model._modules.items(): 39 | if hasattr(module,"_modules"): 40 | model._modules[name] = replace_IF_by_Burst_all(module,gamma) 41 | if isSNNneuron(module.__class__.__name__.lower()): 42 | if hasattr(model[0], "threshold"): 43 | model._modules[name] = ScaledNeuron(scale=model[0].threshold.item(), Burst=True, gamma=gamma) 44 | else: 45 | model._modules[name] = ScaledNeuron(scale=1.0, Burst=True, gamma=gamma) 46 | return model 47 | 48 | def get_neuron_threshold(model): 49 | threshold = [] 50 | for name, module in model._modules.items(): 51 | if hasattr(module,"_modules") and len(module._modules) > 1: 52 | if name.startswith('layer'): 53 | threshold.append(module.conv2[0].threshold.item()) 54 | else: 55 | threshold.append(module[0].threshold.item()) 56 | return threshold 57 | 58 | def reset_net(model): 59 | for name, module in model._modules.items(): 60 | if hasattr(module,"_modules"): 61 | reset_net(module) 62 | if 'Neuron' in module.__class__.__name__: 63 | module.reset() 64 | return model 65 | 66 | def _fold_bn(conv_module, bn_module, avg=False): 67 | w = conv_module.weight.data 68 | y_mean = bn_module.running_mean 69 | y_var = bn_module.running_var 70 | safe_std = torch.sqrt(y_var + bn_module.eps) 71 | w_view = (conv_module.out_channels, 1, 1, 1) 72 | if bn_module.affine: 73 | weight = w * (bn_module.weight / safe_std).view(w_view) 74 | beta = bn_module.bias - bn_module.weight * y_mean / safe_std 75 | if conv_module.bias is not None: 76 | bias = bn_module.weight * conv_module.bias / safe_std + beta 77 | else: 78 | bias = beta 79 | else: 80 | weight = w / safe_std.view(w_view) 81 | beta = -y_mean / safe_std 82 | if conv_module.bias is not None: 83 | bias = conv_module.bias / safe_std + beta 84 | else: 85 | bias = beta 86 | return weight, bias 87 | 88 | def fold_bn_into_conv(conv_module, bn_module, avg=False): 89 | w, b = _fold_bn(conv_module, bn_module, avg) 90 | if conv_module.bias is None: 91 | conv_module.bias = nn.Parameter(b) 92 | else: 93 | conv_module.bias.data = b 94 | conv_module.weight.data = w 95 | # set bn running stats 96 | bn_module.running_mean = bn_module.bias.data 97 | bn_module.running_var = bn_module.weight.data ** 2 98 | 99 | def is_bn(m): 100 | return isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d) 101 | 102 | 103 | def is_absorbing(m): 104 | return (isinstance(m, nn.Conv2d)) or isinstance(m, nn.Linear) 105 | 106 | 107 | def search_fold_and_remove_bn(model): 108 | model.eval() 109 | prev = None 110 | for n, m in model.named_children(): 111 | if is_bn(m) and is_absorbing(prev): 112 | fold_bn_into_conv(prev, m) 113 | # set the bn module to straight through 114 | setattr(model, n, StraightThrough()) 115 | elif is_absorbing(m): 116 | prev = m 117 | else: 118 | prev = search_fold_and_remove_bn(m) 119 | return prev 120 | 121 | 122 | def regular_set(model, paras=([],[],[])): 123 | for n, module in model._modules.items(): 124 | if isActivation(module.__class__.__name__.lower()) and hasattr(module, "up"): 125 | for name, para in module.named_parameters(): 126 | paras[0].append(para) 127 | elif 'batchnorm' in module.__class__.__name__.lower(): 128 | for name, para in module.named_parameters(): 129 | paras[2].append(para) 130 | elif len(list(module.children())) > 0: 131 | paras = regular_set(module, paras) 132 | elif module.parameters() is not None: 133 | for name, para in module.named_parameters(): 134 | paras[1].append(para) 135 | return paras 136 | 137 | class LabelSmoothing(nn.Module): 138 | """ 139 | NLL loss with label smoothing. 140 | """ 141 | def __init__(self, smoothing=0.1): 142 | """ 143 | Constructor for the LabelSmoothing module. 144 | :param smoothing: label smoothing factor 145 | """ 146 | super(LabelSmoothing, self).__init__() 147 | self.confidence = 1.0 - smoothing 148 | self.smoothing = smoothing 149 | 150 | def forward(self, x, target): 151 | logprobs = torch.nn.functional.log_softmax(x, dim=-1) 152 | nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1)) 153 | nll_loss = nll_loss.squeeze(1) 154 | smooth_loss = -logprobs.mean(dim=-1) 155 | loss = self.confidence * nll_loss + self.smoothing * smooth_loss 156 | return loss.mean() 157 | 158 | 159 | # ------------------------- Max Activation --------------------------- 160 | 161 | class DataSaverHook: 162 | def __init__(self, momentum: Union[float, None] = 0.9, sim_length: int = 8, 163 | mse: bool = True, percentile: Union[float, None] = None, channel_wise: bool = False, 164 | dist_avg: bool = False): 165 | self.momentum = momentum 166 | self.max_act = None 167 | self.T = sim_length 168 | self.mse = mse 169 | self.percentile = percentile 170 | self.channel_wise = channel_wise 171 | self.dist_avg = dist_avg 172 | 173 | def __call__(self, module, input_batch, output_batch): 174 | def get_act_thresh(tensor): 175 | if self.mse: 176 | act_thresh = find_threshold_mse(output_batch, T=self.T, channel_wise=self.channel_wise) 177 | elif self.percentile is not None: 178 | assert 0. <= self.percentile <= 1.0 179 | act_thresh = quantile(output_batch, self.percentile) 180 | else: 181 | act_thresh = tensor.max() 182 | return act_thresh 183 | 184 | if self.max_act is None: 185 | self.max_act = get_act_thresh(output_batch) 186 | else: 187 | cur_max = get_act_thresh(output_batch) 188 | if self.momentum is None: 189 | self.max_act = self.max_act if self.max_act > cur_max else cur_max 190 | else: 191 | self.max_act = self.momentum * self.max_act + (1 - self.momentum) * cur_max 192 | if self.dist_avg: 193 | allaverage(self.max_act) 194 | module.threshold = self.max_act 195 | 196 | 197 | def quantile(tensor: torch.Tensor, p: float): 198 | try: 199 | return torch.quantile(tensor, p) 200 | except: 201 | tensor_np = tensor.cpu().detach().numpy() 202 | return torch.tensor(np.percentile(tensor_np, q=p*100)).type_as(tensor) 203 | 204 | 205 | def find_threshold_mse(tensor: torch.Tensor, T: int = 8, channel_wise: bool = True): 206 | """ 207 | This function use grid search to find the best suitable 208 | threshold value for snn. 209 | :param tensor: the output batch tensor, 210 | :param T: simulation length 211 | :param channel_wise: set threshold channel-wise 212 | :return: threshold with MMSE 213 | """ 214 | def clip_floor(tensor:torch.Tensor, T: int, Vth: Union[float, torch.Tensor]): 215 | snn_out = torch.clamp(tensor / Vth * T, min=0, max=T) 216 | return snn_out.floor() * Vth / T 217 | 218 | if channel_wise: 219 | num_channel =tensor.shape[1] 220 | best_Vth = torch.ones(num_channel).type_as(tensor) 221 | # determine the Vth channel-by-channel 222 | for i in range(num_channel): 223 | best_Vth[i] = find_threshold_mse(tensor[:, i], T, channel_wise=False) 224 | best_Vth = best_Vth.reshape(1, num_channel, 1, 1) if len(tensor.shape)==4 else best_Vth.reshape(1, num_channel) 225 | else: 226 | max_act = tensor.max() 227 | best_score = 1e5 228 | best_Vth = 0 229 | for i in range(95): 230 | new_Vth = max_act * (1.0 - (i * 0.01)) 231 | mse = lp_loss(tensor, clip_floor(tensor, T, new_Vth), p=2.0, reduction='other') 232 | if mse < best_score: 233 | best_Vth = new_Vth 234 | best_score = mse 235 | 236 | return best_Vth 237 | 238 | 239 | @torch.no_grad() 240 | def get_maximum_activation(train_loader: torch.utils.data.DataLoader, 241 | model, 242 | momentum: Union[float, None] = 0.9, 243 | iters: int = 20, 244 | sim_length: int = 8, 245 | mse: bool = True, percentile: Union[float, None] = None, 246 | channel_wise: bool = False, 247 | dist_avg: bool = False): 248 | """ 249 | This function store the maximum activation in each convolutional or FC layer. 250 | :param train_loader: Data loader of the training set 251 | :param model: target model 252 | :param momentum: if use momentum, the max activation will be EMA updated 253 | :param iters: number of iterations to calculate the max act 254 | :param sim_length: sim_length when computing the mse of SNN output 255 | :param mse: if Ture, use MMSE to find the V_th 256 | :param percentile: if mse = False and percentile is in [0,1], use percentile to find the V_th 257 | :param channel_wise: use channel-wise mse 258 | :param dist_avg: if True, then compute mean between distributed nodes 259 | :return: model with stored max activation buffer 260 | """ 261 | # do not use train mode here (avoid bn update) 262 | model.eval() 263 | device = next(model.parameters()).device 264 | hook_list = [] 265 | for m in model.modules(): 266 | if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear): 267 | hook_list += [m.register_forward_hook(DataSaverHook(momentum, sim_length, mse, percentile, channel_wise, 268 | dist_avg))] 269 | for i, (input, target) in enumerate(train_loader): 270 | input = input.to(device=device) 271 | _ = model(input) 272 | if i > iters: 273 | break 274 | for h in hook_list: 275 | h.remove() 276 | 277 | 278 | def lp_loss(pred, tgt, p=2.0, reduction='none'): 279 | if reduction == 'none': 280 | return (pred-tgt).abs().pow(p).sum(1).mean() 281 | elif reduction == 'channel_split': 282 | return (pred-tgt).abs().pow(p).sum((0,2,3)) 283 | else: 284 | return (pred-tgt).abs().pow(p).mean() 285 | --------------------------------------------------------------------------------