Skip to content

Commit f6b068f

Browse files
authored
Upgrade ImageNet Demo (#1233)
* Upgrade ImageNet Demo * Upgrade ImageNet Demo * Upgrade ImageNet Demo
1 parent a59d776 commit f6b068f

File tree

27 files changed

+660
-1702
lines changed

27 files changed

+660
-1702
lines changed

demo/imagenet_reader.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
np.random.seed(0)
1212

1313
DATA_DIM = 224
14+
RESIZE_DIM = 256
1415

1516
THREAD = 16
1617
BUF_SIZE = 10240
@@ -34,8 +35,8 @@ def crop_image(img, target_size, center):
3435
width, height = img.size
3536
size = target_size
3637
if center == True:
37-
w_start = (width - size) / 2
38-
h_start = (height - size) / 2
38+
w_start = (width - size) // 2
39+
h_start = (height - size) // 2
3940
else:
4041
w_start = np.random.randint(0, width - size + 1)
4142
h_start = np.random.randint(0, height - size + 1)
@@ -98,7 +99,7 @@ def random_color(img, lower=0.5, upper=1.5):
9899
return img
99100

100101

101-
def process_image(sample, mode, color_jitter, rotate):
102+
def process_image(sample, mode, color_jitter, rotate, crop_size, resize_size):
102103
img_path = sample[0]
103104

104105
try:
@@ -108,10 +109,10 @@ def process_image(sample, mode, color_jitter, rotate):
108109
return None
109110
if mode == 'train':
110111
if rotate: img = rotate_image(img)
111-
img = random_crop(img, DATA_DIM)
112+
img = random_crop(img, crop_size)
112113
else:
113-
img = resize_short(img, target_size=256)
114-
img = crop_image(img, target_size=DATA_DIM, center=True)
114+
img = resize_short(img, target_size=resize_size)
115+
img = crop_image(img, target_size=crop_size, center=True)
115116
if mode == 'train':
116117
if color_jitter:
117118
img = distort_color(img)
@@ -185,9 +186,15 @@ def test(data_dir=DATA_DIR):
185186

186187

187188
class ImageNetDataset(Dataset):
188-
def __init__(self, data_dir=DATA_DIR, mode='train'):
189+
def __init__(self,
190+
data_dir=DATA_DIR,
191+
mode='train',
192+
crop_size=DATA_DIM,
193+
resize_size=RESIZE_DIM):
189194
super(ImageNetDataset, self).__init__()
190195
self.data_dir = data_dir
196+
self.crop_size = crop_size
197+
self.resize_size = resize_size
191198
train_file_list = os.path.join(data_dir, 'train_list.txt')
192199
val_file_list = os.path.join(data_dir, 'val_list.txt')
193200
test_file_list = os.path.join(data_dir, 'test_list.txt')
@@ -211,21 +218,27 @@ def __getitem__(self, index):
211218
[data_path, sample[1]],
212219
mode='train',
213220
color_jitter=False,
214-
rotate=False)
221+
rotate=False,
222+
crop_size=self.crop_size,
223+
resize_size=self.resize_size)
215224
return data, np.array([label]).astype('int64')
216225
elif self.mode == 'val':
217226
data, label = process_image(
218227
[data_path, sample[1]],
219228
mode='val',
220229
color_jitter=False,
221-
rotate=False)
230+
rotate=False,
231+
crop_size=self.crop_size,
232+
resize_size=self.resize_size)
222233
return data, np.array([label]).astype('int64')
223234
elif self.mode == 'test':
224235
data = process_image(
225236
[data_path, sample[1]],
226237
mode='test',
227238
color_jitter=False,
228-
rotate=False)
239+
rotate=False,
240+
crop_size=self.crop_size,
241+
resize_size=self.resize_size)
229242
return data
230243

231244
def __len__(self):

example/auto_compression/image_classification/README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@
2424
| 模型 | 策略 | Top-1 Acc | GPU 耗时(ms) | ARM CPU 耗时(ms) |
2525
|:------:|:------:|:------:|:------:|:------:|
2626
| MobileNetV1 | Baseline | 70.90 | - | 33.15 |
27-
| MobileNetV1 | 量化+蒸馏 | 70.49 | - | 13.64 |
27+
| MobileNetV1 | 量化+蒸馏 | 70.57 | - | 13.64 |
2828
| ResNet50_vd | Baseline | 79.12 | 3.19 | - |
29-
| ResNet50_vd | 量化+蒸馏 | 78.55 | 0.92 | - |
29+
| ResNet50_vd | 量化+蒸馏 | 78.74 | 0.92 | - |
3030
| ShuffleNetV2_x1_0 | Baseline | 68.65 | - | 10.43 |
31-
| ShuffleNetV2_x1_0 | 量化+蒸馏 | 67.78 | - | 5.51 |
31+
| ShuffleNetV2_x1_0 | 量化+蒸馏 | 68.32 | - | 5.51 |
3232
| SqueezeNet1_0_infer | Baseline | 59.60 | - | 35.98 |
33-
| SqueezeNet1_0_infer | 量化+蒸馏 | 59.13 | - | 16.96 |
33+
| SqueezeNet1_0_infer | 量化+蒸馏 | 59.45 | - | 16.96 |
3434
| PPLCNetV2_base | Baseline | 76.86 | - | 36.50 |
3535
| PPLCNetV2_base | 量化+蒸馏 | 76.43 | - | 15.79 |
3636
| PPHGNet_tiny | Baseline | 79.59 | 2.82 | - |
37-
| PPHGNet_tiny | 量化+蒸馏 | 79.19 | 0.98 | - |
37+
| PPHGNet_tiny | 量化+蒸馏 | 79.20 | 0.98 | - |
38+
| InceptionV3 | Baseline | 79.14 | 4.79 | - |
39+
| InceptionV3 | 量化+蒸馏 | 78.32 | 1.47 | - |
3840
| EfficientNetB0 | Baseline | 77.02 | 1.95 | - |
39-
| EfficientNetB0 | 量化+蒸馏 | 73.61 | 1.44 | - |
41+
| EfficientNetB0 | 量化+蒸馏 | 75.39 | 1.44 | - |
4042
| GhostNet_x1_0 | Baseline | 74.02 | 2.93 | - |
41-
| GhostNet_x1_0 | 量化+蒸馏 | 71.11 | 1.03 | - |
42-
| InceptionV3 | Baseline | 79.14 | 4.79 | - |
43-
| InceptionV3 | 量化+蒸馏 | 73.16 | 1.47 | - |
43+
| GhostNet_x1_0 | 量化+蒸馏 | 72.62 | 1.03 | - |
4444
| MobileNetV3_large_x1_0 | Baseline | 75.32 | - | 16.62 |
45-
| MobileNetV3_large_x1_0 | 量化+蒸馏 | 68.84 | - | 9.85 |
45+
| MobileNetV3_large_x1_0 | 量化+蒸馏 | 70.93 | - | 9.85 |
4646

4747
- ARM CPU 测试环境:`SDM865(4xA77+4xA55)`
4848
- Nvidia GPU 测试环境:
@@ -119,7 +119,7 @@ python -m paddle.distributed.launch run.py --save_dir='./save_quant_mobilev1/' -
119119

120120
准备好inference模型后,使用以下命令进行预测:
121121
```shell
122-
python infer.py -c configs/infer.yaml
122+
python infer.py --config_path="configs/infer.yaml"
123123
```
124124

125125
在配置文件```configs/infer.yaml```中有以下字段用于配置预测参数:
@@ -134,7 +134,7 @@ python infer.py -c configs/infer.yaml
134134
- ```PostProcess.Topk.class_id_map_file```:数据集 label 的映射文件,默认为```./images/imagenet1k_label_list.txt```,该文件为 PaddleClas 所使用的 ImageNet 数据集 label 映射文件
135135

136136
注意:
137-
- 请注意模型的输入数据尺寸,部分模型需要修改参数```PreProcess.resize_short```, ```PreProcess.resize```
137+
- 请注意模型的输入数据尺寸,如InceptionV3输入尺寸为299,所以部分模型需要修改参数```PreProcess.resize_short```, ```PreProcess.resize```
138138
- 如果希望提升评测模型速度,使用 ```GPU``` 评测时,建议开启 ```TensorRT``` 加速预测,使用 ```CPU``` 评测时,建议开启 ```MKL-DNN``` 加速预测
139139
- 若使用 TesorRT 预测引擎,需安装 ```WITH_TRT=ON``` 的Paddle,下载地址:[Python预测库](https://paddleinference.paddlepaddle.org.cn/master/user_guides/download_lib.html#python)
140140

example/auto_compression/image_classification/configs/EfficientNetB0/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant

example/auto_compression/image_classification/configs/GhostNet_x1_0/qat_dis.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant
@@ -33,3 +33,4 @@ TrainConfig:
3333
optimizer:
3434
type: Momentum
3535
weight_decay: 0.00002
36+
origin_metric: 0.7402

example/auto_compression/image_classification/configs/InceptionV3/prune_dis.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Global:
44
model_filename: inference.pdmodel
55
params_filename: inference.pdiparams
66
batch_size: 32
7+
resize_size: 320
8+
crop_size: 299
79
data_dir: /ILSVRC2012
810

911
Distillation:

example/auto_compression/image_classification/configs/InceptionV3/qat_dis.yaml

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
Global:
22
input_name: x
3-
model_dir: InceptionV3_infer
3+
model_dir: save_quant_inception
44
model_filename: inference.pdmodel
55
params_filename: inference.pdiparams
66
batch_size: 32
7-
data_dir: /ILSVRC2012
7+
resize_size: 320
8+
img_size: 299
9+
data_dir: /workspace/dataset/ILSVRC2012
810

911
Distillation:
10-
alpha: 10.0
12+
alpha: 1.0
1113
loss: l2
1214
node:
1315
- softmax_1.tmp_0
1416
Quantization:
1517
is_full_quantize: false
16-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1719
weight_quantize_type: channel_wise_abs_max
1820
not_quant_pattern:
1921
- skip_quant
2022
quantize_op_types:
2123
- conv2d
2224
- depthwise_conv2d
2325
weight_bits: 8
26+
2427
TrainConfig:
2528
epochs: 1
2629
eval_iter: 500

example/auto_compression/image_classification/configs/MobileNetV1/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Quantization:
1616
activation_bits: 8
1717
is_full_quantize: false
1818
activation_quantize_type: moving_average_abs_max
19-
weight_quantize_type: abs_max
19+
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant
2222
quantize_op_types:

example/auto_compression/image_classification/configs/MobileNetV3_large_x1_0/qat_dis.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ Distillation:
1212
node:
1313
- softmax_0.tmp_0
1414
Quantization:
15+
use_pact: true
1516
activation_bits: 8
1617
is_full_quantize: false
17-
use_pact: true
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant
@@ -25,10 +25,10 @@ Quantization:
2525
weight_bits: 8
2626
TrainConfig:
2727
epochs: 1
28-
eval_iter: 2000
28+
eval_iter: 500
2929
learning_rate:
3030
type: CosineAnnealingDecay
31-
learning_rate: 0.0001
31+
learning_rate: 0.015
3232
optimizer_builder:
3333
optimizer:
3434
type: Momentum
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Global:
2+
input_name: x
3+
model_dir: PPHGNet_tiny_infer
4+
model_filename: inference.pdmodel
5+
params_filename: inference.pdiparams
6+
batch_size: 32
7+
data_dir: /ILSVRC2012
8+
9+
Distillation:
10+
alpha: 1.0
11+
loss: l2
12+
node:
13+
- softmax_1.tmp_0
14+
UnstructurePrune:
15+
prune_strategy: gmp
16+
prune_mode: ratio
17+
ratio: 0.75
18+
gmp_config:
19+
stable_iterations: 0
20+
pruning_iterations: 4500
21+
tunning_iterations: 4500
22+
resume_iteration: -1
23+
pruning_steps: 100
24+
initial_ratio: 0.15
25+
prune_params_type: conv1x1_only
26+
local_sparsity: True
27+
TrainConfig:
28+
epochs: 1
29+
eval_iter: 500
30+
learning_rate:
31+
type: CosineAnnealingDecay
32+
learning_rate: 0.015
33+
optimizer_builder:
34+
optimizer:
35+
type: Momentum
36+
weight_decay: 0.00002
37+
origin_metric: 0.7959
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Global:
2+
input_name: x
3+
model_dir: PPHGNet_tiny_infer
4+
model_filename: inference.pdmodel
5+
params_filename: inference.pdiparams
6+
batch_size: 32
7+
data_dir: /ILSVRC2012
8+
9+
Distillation:
10+
alpha: 1.0
11+
loss: l2
12+
node:
13+
- softmax_1.tmp_0
14+
Quantization:
15+
use_pact: true
16+
activation_bits: 8
17+
is_full_quantize: false
18+
activation_quantize_type: moving_average_abs_max
19+
weight_quantize_type: channel_wise_abs_max
20+
not_quant_pattern:
21+
- skip_quant
22+
quantize_op_types:
23+
- conv2d
24+
- depthwise_conv2d
25+
weight_bits: 8
26+
TrainConfig:
27+
epochs: 1
28+
eval_iter: 500
29+
learning_rate:
30+
type: CosineAnnealingDecay
31+
learning_rate: 0.015
32+
optimizer_builder:
33+
optimizer:
34+
type: Momentum
35+
weight_decay: 0.00002
36+
origin_metric: 0.7959

example/auto_compression/image_classification/configs/PPLCNetV2_base/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant

example/auto_compression/image_classification/configs/PPLCNet_x1_0/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant

example/auto_compression/image_classification/configs/ResNet50_vd/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant

example/auto_compression/image_classification/configs/ShuffleNetV2_x1_0/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant

example/auto_compression/image_classification/configs/SqueezeNet1_0/qat_dis.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ Distillation:
1111
loss: l2
1212
node:
1313
- softmax_0.tmp_0
14-
teacher_model_dir: SqueezeNet1_0_infer
15-
teacher_model_filename: inference.pdmodel
16-
teacher_params_filename: inference.pdiparams
1714
Quantization:
1815
activation_bits: 8
1916
is_full_quantize: false
20-
activation_quantize_type: range_abs_max
17+
activation_quantize_type: moving_average_abs_max
2118
weight_quantize_type: channel_wise_abs_max
2219
not_quant_pattern:
2320
- skip_quant

example/auto_compression/image_classification/configs/SwinTransformer_base_patch4_window7_224/qat_dis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Quantization:
1515
use_pact: true
1616
activation_bits: 8
1717
is_full_quantize: false
18-
activation_quantize_type: range_abs_max
18+
activation_quantize_type: moving_average_abs_max
1919
weight_quantize_type: channel_wise_abs_max
2020
not_quant_pattern:
2121
- skip_quant

0 commit comments

Comments
 (0)