Skip to content

Commit 6738022

Browse files
committed
Initial commit
0 parents  commit 6738022

26 files changed

+52931
-0
lines changed

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/quantizednn.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
## Quantize CNN Model using PyTorch(python3.5)
2+
3+
Implement [Towards Effective Low-bitwidth Convolutional Neural Networks](https://arxiv.org/abs/1711.00205)
4+
5+
```
6+
@InProceedings{Zhuang_2018_CVPR,
7+
author = {Zhuang, Bohan and Shen, Chunhua and Tan, Mingkui and Liu, Lingqiao and Reid, Ian},
8+
title = {Towards Effective Low-Bitwidth Convolutional Neural Networks},
9+
booktitle = {The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
10+
month = {June},
11+
year = {2018}
12+
}
13+
```
14+
15+
### 下载和配置
16+
17+
```bash
18+
git clone https://github.com/nowgood/QuantizeCNNModel.git && cd QuantizeCNNModel
19+
pip install -r requirements.txt
20+
echo export PYTHONPATH=$PYTHONPATH:`pwd` >> ~/.bashrc
21+
source ~/.bashrc
22+
```
23+
24+
### 使用方法
25+
26+
使用如下命令查看函数使用方法
27+
28+
```
29+
python guided.py -h
30+
```
31+
32+
33+
34+
然后使用 tensorboard 查看训练过程
35+
36+
```
37+
# QuantizeCNNModel 目录下
38+
tensorboard --logdir model/xxx/
39+
```
40+
然后就可以在 `http:localhost:6006` 查看训练的损失值和精确度, 以及每个epoch的在验证集上的精确度
41+
42+
![top5](https://github.com/nowgood/QuantizeCNNModel/raw/master/data/WandA_lr0.01_scalar2.5.png)
43+
44+
### 训练方法
45+
46+
训练模式选择:
47+
48+
0: full precision training from scratch
49+
1: only quantize weight
50+
2. quantize activation using quantized weight to init model
51+
3. joint quantize weight and activation from pre-trained imageNet model
52+
4. guided quantize weight and activation from pre-trained imageNet model
53+
54+
55+
**单卡训练**
56+
57+
```
58+
python guided.py \
59+
--arch resnet18 \
60+
--mode 3 \
61+
--workers 16 \
62+
--epochs 35 \
63+
--checkpoint model/WandA_lr0.001_scalar2.5 \
64+
--lr 0.001 \
65+
--data /media/wangbin/8057840b-9a1e-48c9-aa84-d353a6ba1090/ImageNet_ILSVRC2012/ILSVRC2012 \
66+
> log/WandA_lr_0.001_scalar2.5_20180719.log 2>&1 &
67+
```
68+
69+
### 量化权重
70+
71+
**单机多卡训练**, 如: 使用 8 个GPU的后 4 个GPU来训练25个epoch
72+
73+
```
74+
CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py \
75+
--arch resnet18 \
76+
--mode 1 \
77+
--workers 16 \
78+
--epochs 25 \
79+
--batch_size 1024\
80+
--device_ids 0 1 2 3 \
81+
--lr 0.0001 \
82+
--checkpoint model/W_lr0.0001_epoch25 \
83+
--data /home/user/wangbin/datasets/ILSVRC2012 \
84+
|tee model/W_lr_1e-4_epoch25.log 2>&1
85+
```
86+
87+
### 使用量化权重的参数来初始化量化激活的网络
88+
89+
```bash
90+
CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py \
91+
--arch resnet18 \
92+
--mode 2 \
93+
--workers 16 \
94+
--epochs 35 \
95+
--batch_size 1024\
96+
--device_ids 0 1 2 3 \
97+
--lr 0.001 \
98+
--weight_quantized model/W_lr1e-4_epoch2/model_best.pth.tar \
99+
--checkpoint model/AafterW_lr1e-3_epoch35 \
100+
--data /home/user/wangbin/datasets/ILSVRC2012 \
101+
|tee model/AafterW_lr1e-3_epoch35.log 2>&1
102+
```
103+
104+
**resume**
105+
106+
```bash
107+
CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py \
108+
--arch resnet18 \
109+
--mode 2 \
110+
--workers 16 \
111+
--epochs 35 \
112+
--batch_size 1024\
113+
--device_ids 0 1 2 3 \
114+
--lr 0.001 \
115+
--resume model/AafterW_lr1e-3_epoch35/checkpoint.pth.tar \
116+
--weight_quantized model/W_lr1e-4_epoch2/model_best.pth.tar \
117+
--checkpoint model/AafterW_lr1e-3_epoch35 \
118+
--data /home/user/wangbin/datasets/ILSVRC2012 \
119+
|tee model/AafterW_lr1e-3_epoch35.log 2>&1
120+
```
121+
122+
### 同时量化权重和激活
123+
124+
```
125+
CUDA_VISIBLE_DEVICES=4,5,6,7 python guided.py \
126+
--mode 3
127+
--arch resnet18 \
128+
--workers 16 \
129+
--epochs 35 \
130+
--batch-size 800 \
131+
--pretrained \
132+
--device_ids 0 1 2 3 \
133+
--lr 0.01 \
134+
--data /home/user/wangbin/datasets/ILSVRC2012 \
135+
--checkpoint model/AandW_lr0.01_epoch35 \
136+
| tee AandW_lr0.01_epoch35.log 2>&1
137+
```
138+
139+
### 使用 guidance 信号来同时量化权重和激活
140+
141+
```bash
142+
CUDA_VISIBLE_DEVICES=0,1,2,3 python main.py \
143+
--arch resnet18
144+
--mode 4 \
145+
--workers 16 \
146+
--epochs 50 \
147+
--batch-size 512 \
148+
--pretrained \
149+
--device_ids 0 1 2 3 \
150+
--balance 2 \
151+
--lowlr 0.001 \
152+
--fulllr 0.001 \
153+
--data /home/user/wangbin/datasets/ILSVRC2012 \
154+
--checkpoint /home/user/wangbin/quantizednn/model/WandA_guided_balance2_lr1e-3_lr1e-3_epoch50 \
155+
| tee model/log.WandA_guided_balance2_lr1e-3_lr1e-3_epoch50 2>&1
156+
```

data/WandA_lr0.01_scalar2.5.png

74.2 KB
Loading

data/smurf.jpeg

6.26 KB
Loading

graffiti/README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
### Usage: [argparse](http://wiki.jikexueyuan.com/project/explore-python/Standard-Modules/argparse.html)
3+
4+
```
5+
每个参数解释如下:
6+
7+
name or flags - 选项字符串的名字或者列表,例如 foo 或者 -f, --foo。
8+
action - 命令行遇到参数时的动作,默认值是 store。
9+
store_const,表示赋值为const;
10+
append,将遇到的值存储成列表,也就是如果参数重复则会保存多个值;
11+
append_const,将参数规范中定义的一个值保存到一个列表;
12+
count,存储遇到的次数;此外,也可以继承 argparse.Action 自定义参数解析;
13+
nargs - 应该读取的命令行参数个数,可以是具体的数字,或者是?号,当不指定值时对于 Positional argument 使用 default,
14+
对于 Optional argument 使用 const;
15+
或者是 * 号,表示 0 或多个参数;
16+
或者是 + 号表示 1 或多个参数。
17+
const - action 和 nargs 所需要的常量值。
18+
default - 不指定参数时的默认值。
19+
type - 命令行参数应该被转换成的类型。
20+
choices - 参数可允许的值的一个容器。
21+
required - 可选参数是否可以省略 (仅针对可选参数)。
22+
help - 参数的帮助信息,当指定为 argparse.SUPPRESS 时表示不显示该参数的帮助信息.
23+
metavar - 在 usage 说明中的参数名称,对于必选参数默认就是参数名称,对于可选参数默认是全大写的参数名称.
24+
dest - 解析后的参数名称,默认情况下,对于可选参数选取最长的名称,中划线转换为下划线.
25+
```
26+
27+
### Usage imagenet.py
28+
29+
```
30+
usage: guided.py [-h] [--arch ARCH] [-j N] [--epochs N] [--start-epoch N] [-b N]
31+
[--lr LR] [--momentum M] [--weight-decay W] [--print-freq N]
32+
[--resume PATH] [-e] [--pretrained]
33+
DIR
34+
35+
PyTorch ImageNet Training
36+
37+
positional arguments:
38+
DIR path to dataset
39+
40+
optional arguments:
41+
-h, --help show this help message and exit
42+
--arch ARCH, -a ARCH model architecture: alexnet | resnet | resnet101 |
43+
resnet152 | resnet18 | resnet34 | resnet50 | vgg |
44+
vgg11 | vgg11_bn | vgg13 | vgg13_bn | vgg16 | vgg16_bn
45+
| vgg19 | vgg19_bn (default: resnet18)
46+
-j N, --workers N number of data loading workers (default: 4)
47+
--epochs N number of total epochs to run
48+
--start-epoch N manual epoch number (useful on restarts)
49+
-b N, --batch-size N mini-batch size (default: 256)
50+
--lr LR, --README.md-rate LR
51+
initial README.md rate
52+
--momentum M momentum
53+
--weight-decay W, --wd W
54+
weight decay (default: 1e-4)
55+
--print-freq N, -p N print frequency (default: 10)
56+
--resume PATH path to latest checkpoint (default: none)
57+
-e, --evaluate evaluate model on validation set
58+
--pretrained use pre-trained model
59+
60+
```
61+
62+
### use pretrained model to initialize your modified model
63+
64+
```
65+
model_dict = your_model.state_dict()
66+
67+
pretrained_model = models.__dict__[args.arch](pretrained=True)
68+
pretrained_dict = pretrained_model.state_dict()
69+
70+
# 将 pretrained_dict 里不属于 model_dict 的键剔除掉
71+
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
72+
73+
model_dict.update(pretrained_dict)
74+
your_model.load_state_dict(model_dict)
75+
```
76+
77+
### how to get nn.DataParallel model filter weight
78+
79+
```python
80+
low_prec_state_dict = low_prec_model.state_dict()
81+
full_prec_state_dict = full_prec_model.state_dict()
82+
low_prec_norm = low_prec_state_dict['module.layer4.1.conv1.weight'].norm(p=2) + low_prec_state_dict['module.layer4.1.conv2.weight'].norm(p=2)
83+
full_prec_norm = full_prec_state_dict['module.layer4.1.conv1.weight'].norm(p=2) + full_prec_state_dict['module.layer4.1.conv2.weight'].norm(p=2)
84+
85+
l2 = (low_prec_norm + full_prec_norm) * args.balance
86+
```
87+
88+
### torch.topk
89+
90+
```
91+
>>> x = torch.arange(1, 6)
92+
>>> x
93+
tensor([ 1., 2., 3., 4., 5.])
94+
>>> torch.topk(x, 3)
95+
(tensor([ 5., 4., 3.]), tensor([ 4, 3, 2]))
96+
```

0 commit comments

Comments
 (0)