forked from AdamDHines/VPRTempo-quant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VPRTempo_Train.py
289 lines (249 loc) · 10.5 KB
/
VPRTempo_Train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#MIT License
#Copyright (c) 2023 Adam Hines, Peter G Stratton, Michael Milford, Tobias Fischer
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
'''
Imports
'''
import os
import torch
import gc
import sys
sys.path.append('./src')
sys.path.append('./models')
sys.path.append('./output')
sys.path.append('./dataset')
import blitnet as bn
import numpy as np
import torch.nn as nn
import torchvision.transforms as transforms
from settings import configure, model_logger
from dataset import CustomImageDataset, ProcessImage
from torch.utils.data import DataLoader
from tqdm import tqdm
class VPRTempo(nn.Module):
def __init__(self):
super(VPRTempo, self).__init__()
# Configure the network
configure(self)
# Layer dict to keep track of layer names and their order
self.layer_dict = {}
self.layer_counter = 0
"""
Define trainable layers here
"""
self.add_layer(
'feature_layer',
dims=[self.input, self.feature],
thr_range=[0, 0.5],
fire_rate=[0.2, 0.9],
ip_rate=0.15,
stdp_rate=0.005,
p=[0.1, 0.5],
device=self.device
)
self.add_layer(
'output_layer',
dims=[self.feature, self.output],
ip_rate=0.15,
stdp_rate=0.005,
spk_force=True,
device=self.device
)
def add_layer(self, name, **kwargs):
"""
Dynamically add a layer with given name and keyword arguments.
:param name: Name of the layer to be added
:type name: str
:param kwargs: Hyperparameters for the layer
"""
# Check for layer name duplicates
if name in self.layer_dict:
raise ValueError(f"Layer with name {name} already exists.")
# Add a new SNNLayer with provided kwargs
setattr(self, name, bn.SNNLayer(**kwargs))
# Add layer name and index to the layer_dict
self.layer_dict[name] = self.layer_counter
self.layer_counter += 1
def model_logger(self):
"""
Log the model configuration to the console.
"""
model_logger(self)
def _anneal_learning_rate(self, layer, mod, itp, stdp):
"""
Anneal the learning rate for the current layer.
"""
if np.mod(mod, 100) == 0: # Modify learning rate every 100 timesteps
pt = pow(float(self.T - mod) / self.T, self.annl_pow)
layer.eta_ip = torch.mul(itp, pt) # Anneal intrinsic threshold plasticity learning rate
layer.eta_stdp = torch.mul(stdp, pt) # Anneal STDP learning rate
return layer
def train_model(self, train_loader, layer, prev_layers=None):
"""
Train a layer of the network model.
:param train_loader: Training data loader
:param layer: Layer to train
:param prev_layers: Previous layers to pass data through
"""
# Initialize the tqdm progress bar
pbar = tqdm(total=int(self.T),
desc="Training ",
position=0)
# Initialize the learning rates for each layer (used for annealment)
init_itp = layer.eta_ip.detach()
init_stdp = layer.eta_stdp.detach()
mod = 0 # Used to determine the learning rate annealment, resets at each epoch
# Run training for the specified number of epochs
for _ in range(self.epoch):
# Run training for the specified number of timesteps
for spikes, labels in train_loader:
spikes, labels = spikes.to(self.device), labels.to(self.device)
idx = labels / self.filter # Set output index for spike forcing
# Pass through previous layers if they exist
if prev_layers:
with torch.no_grad():
for prev_layer_name in prev_layers:
prev_layer = getattr(self, prev_layer_name) # Get the previous layer object
spikes = self.forward(spikes, prev_layer) # Pass spikes through the previous layer
spikes = bn.clamp_spikes(spikes, prev_layer) # Clamp spikes [0, 0.9]
else:
prev_layer = None
# Get the output spikes from the current layer
pre_spike = spikes.detach() # Previous layer spikes for STDP
spikes = self.forward(spikes, layer) # Current layer spikes
spikes_noclp = spikes.detach() # Used for inhibitory homeostasis
spikes = bn.clamp_spikes(spikes, layer) # Clamp spikes [0, 0.9]
# Calculate STDP
layer = bn.calc_stdp(pre_spike,spikes,spikes_noclp,layer, idx, prev_layer=prev_layer)
# Adjust learning rates
layer = self._anneal_learning_rate(layer, mod, init_itp, init_stdp)
# Update the annealing mod & progress bar
mod += 1
pbar.update(1)
# Close the tqdm progress bar
pbar.close()
# Free up memory
if self.device.type == "cuda":
torch.cuda.empty_cache()
gc.collect()
def forward(self, spikes, layer):
"""
Compute the forward pass of the model.
Parameters:
- spikes (Tensor): Input spikes.
Returns:
- Tensor: Output after processing.
"""
spikes = layer.exc(spikes) + layer.inh(spikes)
return spikes
def combine_weights(self, model):
for layer_name, _ in sorted(model.layer_dict.items(), key=lambda item: item[1]):
# Retrieve the layer object
layer = getattr(model, layer_name)
dims = layer.dims
# Define weight variable in layer
layer.w = nn.Linear(dims[0],dims[1],bias=False)
# Send to model device
layer.x.to(model.device)
# Replace weights with combined weights
layer.w.weight = nn.Parameter(layer.exc.weight + layer.inh.weight)
# Delete original weights
del layer.exc, layer.inh
return model
def save_model(self, model_out):
"""
Save the trained model to models output folder.
"""
torch.save(self.state_dict(), model_out)
def generate_model_name(model):
"""
Generate the model name based on its parameters.
"""
return ("VPRTempo" +
str(model.input) +
str(model.feature) +
str(model.output) +
str(model.number_modules) +
'.pth')
def check_pretrained_model(model_name):
"""
Check if a pre-trained model exists and prompt the user to retrain if desired.
"""
if os.path.exists(os.path.join('./models', model_name)):
prompt = "A network with these parameters exists, re-train network? (y/n):\n"
retrain = input(prompt).strip().lower()
return retrain == 'n'
return False
def train_new_model(model, model_name):
"""
Train a new model.
:param model: Model to train
:param model_name: Name of the model to save after training
:param qconfig: Quantization configuration
"""
# Initialize the image transforms and datasets
image_transform = transforms.Compose([
ProcessImage(model.dims, model.patches)
])
train_dataset = CustomImageDataset(annotations_file=model.dataset_file,
img_dirs=model.training_dirs,
transform=image_transform,
skip=model.filter,
max_samples=model.number_training_images,
test=False)
# Initialize the data loader
train_loader = DataLoader(train_dataset,
batch_size=1,
shuffle=True,
num_workers=8,
persistent_workers=True)
# Set the model to training mode and move to device
model.train()
# Keep track of trained layers to pass data through them
trained_layers = []
# Training each layer
for layer_name, _ in sorted(model.layer_dict.items(), key=lambda item: item[1]):
print(f"Training layer: {layer_name}")
# Retrieve the layer object
layer = getattr(model, layer_name)
# Train the layer
model.train_model(train_loader, layer, prev_layers=trained_layers)
# After training the current layer, add it to the list of trained layers
trained_layers.append(layer_name)
# Combine excitatory and inhibitory weights
model = model.combine_weights(model)
# Convert the model to a quantized model
model.eval()
# Save the model
model.save_model(os.path.join('./models', model_name))
if __name__ == "__main__":
# Set the number of threads for PyTorch
#torch.set_num_threads(8)
# Initialize the model
model = VPRTempo()
if model.quantize:
raise ValueError("Quantization enabled, please disable.")
# Initialize the logger
model.model_logger()
# Generate the model name
model_name = generate_model_name(model)
# Check if a pre-trained model exists
use_pretrained = check_pretrained_model(model_name)
# Train or run inference based on the user's input
if not use_pretrained:
train_new_model(model, model_name) # Training
model.logger.info('Training complete.')