fangwei123456 / spikingjelly

SpikingJelly is an open-source deep learning framework for Spiking Neural Network (SNN) based on PyTorch.
https://spikingjelly.readthedocs.io
Other
1.36k stars 239 forks source link

关于ann2snn去掉bias的问题 #153

Closed lovingpolar closed 2 years ago

lovingpolar commented 2 years ago

将cnn_mnist.py的网络修改成: self.network = nn.Sequential( nn.Conv2d(1, 32, 3, 1, bias=False), nn.BatchNorm2d(32, eps=1e-3), nn.ReLU(), nn.AvgPool2d(2, 2),

        nn.Conv2d(32, 32, 3, 1, bias=False),
        nn.BatchNorm2d(32, eps=1e-3),
        nn.ReLU(),
        nn.AvgPool2d(2, 2),

        nn.Conv2d(32, 32, 3, 1, bias=False),
        nn.BatchNorm2d(32, eps=1e-3),
        nn.ReLU(),
        nn.AvgPool2d(2, 2),

        nn.Flatten(),
        nn.Linear(32, 10, bias=False),
        nn.ReLU()
    )

会出现AttributeError: type object '_o2p_converter' has no attribute 'convert_matmul' 请问如何修改?

fangwei123456 commented 2 years ago

我试了一下,spikingjelly.clock_driven.ann2snn.examples.cnn_mnist中conv的bias全部设置成False,运行没有问题。

100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 15/15 [00:00<00:00, 20.58it/s]
Finished getting intermediate output statistics!

Normalizing model...

100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 15/15 [00:00<00:00, 9904.69it/s]
Finished normalizing model!
no user-defined conversion method found, use default
simulator log_dir: ./cnn_mnist/simulator
simulator is working on the normal mode, device: cuda:0
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 100/100 [00:21<00:00,  4.63it/s]
--------------------simulator summary--------------------
time elapsed: 21.585700082127005 (sec)
---------------------------------------------------------

下面是我的环境

(pytorch-env) wfang@Precision-5820-Tower-X-Series:~/tempdir$ conda list torch
# packages in environment at /home/wfang/anaconda3/envs/pytorch-env:
#
# Name                    Version                   Build  Channel
pytorch                   1.8.0           py3.9_cuda11.1_cudnn8.0.5_0    pytorch
torchaudio                0.8.0                      py39    pytorch
torchvision               0.9.0                py39_cu111    pytorch
(pytorch-env) wfang@Precision-5820-Tower-X-Series:~/tempdir$ conda list onnx
# packages in environment at /home/wfang/anaconda3/envs/pytorch-env:
#
# Name                    Version                   Build  Channel
onnx                      1.8.0                    pypi_0    pypi
onnxruntime               1.9.0                    pypi_0    pypi
onnxruntime-gpu           1.9.0                    pypi_0    pypi
(pytorch-env) wfang@Precision-5820-Tower-X-Series:~/tempdir$ conda list proto
# packages in environment at /home/wfang/anaconda3/envs/pytorch-env:
#
# Name                    Version                   Build  Channel
protobuf                  3.8.0                    pypi_0    pypi
lovingpolar commented 2 years ago

conv的bias全部设置成False,运行没有问题,可是fc层的bias如果设置成False就会报错了

fangwei123456 commented 2 years ago

我刚才试了一下也没有问题

lovingpolar commented 2 years ago

将生成的cnn_mnist.pkl文件delete再运行试试,这个代码不删除掉pkl文件重新运行的话,网络好像是不会重新训练的

fangwei123456 commented 2 years ago

发现错误了

no user-defined conversion method found, use default
Traceback (most recent call last):
  File "/home/wfang/tempdir/w1.py", line 219, in <module>
    main('./cnn_mnist')
  File "/home/wfang/tempdir/w1.py", line 194, in main
    snn = onnxparser.parse(ann, norm_data.to(parser_device))
  File "/home/wfang/spikingjelly/spikingjelly/clock_driven/ann2snn/__init__.py", line 108, in parse
    torch.save(model, os.path.join(self.config['log_dir'], "debug.pth"))
  File "/home/wfang/anaconda3/envs/pytorch-env/lib/python3.9/site-packages/torch/serialization.py", line 372, in save
    _save(obj, opened_zipfile, pickle_module, pickle_protocol)
  File "/home/wfang/anaconda3/envs/pytorch-env/lib/python3.9/site-packages/torch/serialization.py", line 476, in _save
    pickler.dump(obj)
AttributeError: Can't pickle local object '_o2p_converter.convert_matmul.<locals>.MatMul'

@DingJianhao

lovingpolar commented 2 years ago

感谢回复!麻烦您们了

lovingpolar commented 2 years ago

请问有解决办法了吗?

fangwei123456 commented 2 years ago

考虑到ONNX的bug太多, @DingJianhao 准备去掉ONNX重写了。应该需要一定的时间

DingJianhao commented 2 years ago

@lovingpolar 办法不是没有 就是最近期末了太忙了

lovingpolar commented 2 years ago

@DingJianhao请问bug解决了吗?或者说我应该怎么做才能在onnx的基础上去掉fc层的bias吗?

DingJianhao commented 2 years ago

@lovingpolar 更新了一下 如果你只用pytorch的话可以试试

lovingpolar commented 2 years ago

我用pip更新了一下spikingjelly和onnx,还是有报错,我还需要更新哪里吗

fangwei123456 commented 2 years ago

pip上的版本没有更新,直接从源代码安装吧

lovingpolar commented 2 years ago

是使用 git clone https://github.com/fangwei123456/spikingjelly.git吗?我在pycharm用这条指令装了但还是报错

fangwei123456 commented 2 years ago
git clone https://github.com/fangwei123456/spikingjelly.git
cd spikingjelly
python setup.py install
lovingpolar commented 2 years ago

现在又出现了AttributeError: Can't pickle local object '_o2p_converter.convert_matmul..MatMul'这个新错误

fangwei123456 commented 2 years ago

@DingJianhao 按理说去掉onnx后不应该出现这个错误吧

DingJianhao commented 2 years ago

AttributeError: Can't pickle local object '_o2p_converter.convert 这是原来留给开发用户自定义的提示,现在版本已经删除。现在可以直接git clone使用新的ann2snn(初步版本)

fangwei123456 commented 2 years ago

检查一下安装的框架git log,看是否为最新版

lovingpolar commented 2 years ago

安装新版本会出现 Installed d:\anaconda3\envs\pytorch113\lib\site-packages\spikingjelly-0.0.0.0.9-py3.9.egg Processing dependencies for spikingjelly==0.0.0.0.9 error: pyparsing 3.0.5 is installed but pyparsing!=3.0.5,>=2.0.2 is required by {'packaging'}

fangwei123456 commented 2 years ago

直接升级pyparsing到最新版试试

lovingpolar commented 2 years ago

您好! 我在尝试ANN上的VGG-16转换为SNN上的VGG-16,在转换之后发现,每一个计算层之前和之后分别加了1/s和s,请问我应该怎么做才能得到转换之后VGG-16的权重呢?(我们暂时只想得到最终的权重)

fangwei123456 commented 2 years ago

每一层及其之前的层的缩放系数累乘后再乘这一层的权重,应该就是最终的权重,参考这篇文章的2.2.2. Parameter Normalization

https://www.frontiersin.org/articles/10.3389/fnins.2017.00682/full

lovingpolar commented 2 years ago

十分感谢!