@@ -140,6 +140,13 @@ def set_requires_grad(module, value):
140140 ):
141141 prev_module = module2
142142
143+ if isinstance (module2 , nn .Linear ):
144+ if prev_module is not None :
145+ scale_factor = np .percentile (activations .cpu (), percentile )
146+ prev_module .weight *= prev_factor / scale_factor
147+ prev_module .bias /= scale_factor
148+ prev_factor = scale_factor
149+
143150 else :
144151 activations = all_activations [name ]
145152 if isinstance (module , nn .ReLU ):
@@ -188,20 +195,16 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
188195 )
189196
190197 elif isinstance (current , nn .Conv2d ):
191- input_height , input_width = prev .shape [2 ], prev .shape [3 ]
192- out_channels , output_height , output_width = (
193- current .out_channels ,
194- prev .shape [2 ],
195- prev .shape [3 ],
196- )
198+ input_height , input_width = prev .shape [1 ], prev .shape [2 ]
199+ out_channels = current .out_channels
197200
198201 width = (
199202 input_height - current .kernel_size [0 ] + 2 * current .padding [0 ]
200203 ) / current .stride [0 ] + 1
201204 height = (
202205 input_width - current .kernel_size [1 ] + 2 * current .padding [1 ]
203206 ) / current .stride [1 ] + 1
204- shape = (1 , out_channels , int (width ), int (height ))
207+ shape = (out_channels , int (width ), int (height ))
205208
206209 layer = node_type (
207210 shape = shape , reset = 0 , thresh = 1 , refrac = 0 , sum_input = last , ** kwargs
@@ -223,7 +226,7 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
223226 )
224227
225228 elif isinstance (current , nn .MaxPool2d ):
226- input_height , input_width = prev .shape [2 ], prev .shape [3 ]
229+ input_height , input_width = prev .shape [1 ], prev .shape [2 ]
227230 current .kernel_size = _pair (current .kernel_size )
228231 current .padding = _pair (current .padding )
229232 current .stride = _pair (current .stride )
@@ -234,7 +237,7 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
234237 height = (
235238 input_width - current .kernel_size [1 ] + 2 * current .padding [1 ]
236239 ) / current .stride [1 ] + 1
237- shape = (1 , prev .shape [1 ], int (width ), int (height ))
240+ shape = (prev .shape [0 ], int (width ), int (height ))
238241
239242 layer = PassThroughNodes (shape = shape )
240243 connection = topology .MaxPool2dConnection (
@@ -253,7 +256,6 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
253256 prev .shape [current .dims [0 ]],
254257 prev .shape [current .dims [1 ]],
255258 prev .shape [current .dims [2 ]],
256- prev .shape [current .dims [3 ]],
257259 ]
258260 )
259261
@@ -265,9 +267,8 @@ def _ann_to_snn_helper(prev, current, node_type, last=False, **kwargs):
265267 layer = PassThroughNodes (
266268 shape = [
267269 prev .shape [0 ],
268- prev .shape [1 ],
269- current .padding [0 ] + current .padding [1 ] + prev .shape [2 ],
270- current .padding [2 ] + current .padding [3 ] + prev .shape [3 ],
270+ current .padding [0 ] + current .padding [1 ] + prev .shape [1 ],
271+ current .padding [2 ] + current .padding [3 ] + prev .shape [2 ],
271272 ]
272273 )
273274
0 commit comments