未验证 提交 3686ba3f 编写于 作者: W whs 提交者: GitHub

[PaddleSlim]Fix name conficts in distillation. (#2013)

* Fix name conficts in distillation.

* Download pretrain model in run_quant.sh
上级 c11f177a
......@@ -69,14 +69,9 @@ def compress(args):
momentum=0.9,
learning_rate=fluid.layers.piecewise_decay(
boundaries=[5000 * 30, 5000 * 60, 5000 * 90],
values=[0.01, 0.001, 0.0001, 0.00001]),
values=[0.1, 0.01, 0.001, 0.0001]),
regularization=fluid.regularizer.L2Decay(4e-5))
# opt = fluid.optimizer.Momentum(
# momentum=0.9,
# learning_rate=0.01,
# regularization=fluid.regularizer.L2Decay(4e-5))
place = fluid.CUDAPlace(0) if args.use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
......@@ -107,7 +102,10 @@ def compress(args):
with fluid.program_guard(teacher_program, startup_program):
img = teacher_program.global_block()._clone_variable(
image, force_persistable=False)
predict = teacher_model.net(img, class_dim=args.class_dim)
predict = teacher_model.net(img,
class_dim=args.class_dim,
conv1_name='res_conv1',
fc_name='res_fc')
exe.run(startup_program)
assert args.teacher_pretrained_model and os.path.exists(
args.teacher_pretrained_model
......
......@@ -26,7 +26,7 @@ class ResNet():
self.params = train_parameters
self.layers = layers
def net(self, input, class_dim=1000):
def net(self, input, class_dim=1000, conv1_name='conv1', fc_name=None):
layers = self.layers
supported_layers = [50, 101, 152]
assert layers in supported_layers, \
......@@ -48,7 +48,7 @@ class ResNet():
filter_size=7,
stride=2,
act='relu',
name="conv1")
name=conv1_name)
conv = fluid.layers.pool2d(
input=conv,
pool_size=3,
......@@ -77,6 +77,7 @@ class ResNet():
out = fluid.layers.fc(input=pool,
size=class_dim,
act='softmax',
name=fc_name,
param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv,
stdv)))
......
#!/usr/bin/env bash
# download pretrain model
root_url="http://paddle-imagenet-models-name.bj.bcebos.com"
MobileNetV1="MobileNetV1_pretrained.zip"
ResNet50="ResNet50_pretrained.zip"
pretrain_dir='../pretrain'
if [ ! -d ${pretrain_dir} ]; then
mkdir ${pretrain_dir}
fi
cd ${pretrain_dir}
if [ ! -f ${MobileNetV1} ]; then
wget ${root_url}/${MobileNetV1}
unzip ${MobileNetV1}
fi
if [ ! -f ${ResNet50} ]; then
wget ${root_url}/${ResNet50}
unzip ${ResNet50}
fi
cd -
export CUDA_VISIBLE_DEVICES=0,1,2,3
#MobileNet v1:
python quant.py \
--model=MobileNet \
--pretrained_fp32_model=../data/pretrain/MobileNetV1_pretrained \
--pretrained_fp32_model=${pretrain_dir}/MobileNetV1_pretrained \
--use_gpu=True \
--data_dir=../data/ILSVRC2012 \
--batch_size=256 \
......@@ -23,7 +48,7 @@ python quant.py \
#ResNet50:
#python quant.py \
# --model=ResNet50 \
# --pretrained_fp32_model=../data/pretrain/ResNet50_pretrained \
# --pretrained_fp32_model=${pretrain_dir}/ResNet50_pretrained \
# --use_gpu=True \
# --data_dir=../data/ILSVRC2012 \
# --batch_size=128 \
......
......@@ -26,13 +26,22 @@ cd -
# for distillation
#-----------------
export CUDA_VISIBLE_DEVICES=0
export CUDA_VISIBLE_DEVICES=0,1,2,3
# Fixing name conflicts in distillation
mv ResNet50_pretrained/conv1_weights ResNet50_pretrained/res_conv1_weights
mv ResNet50_pretrained/fc_0.w_0 ResNet50_pretrained/res_fc.w_0
mv ResNet50_pretrained/fc_0.b_0 ResNet50_pretrained/res_fc.b_0
python compress.py \
--model "MobileNet" \
--teacher_model "ResNet50" \
--teacher_pretrained_model ./pretrain/ResNet50_pretrained \
--compress_config ./configs/mobilenetv1_resnet50_distillation.yaml
mv ResNet50_pretrained/res_conv1_weights ResNet50_pretrained/conv1_weights
mv ResNet50_pretrained/res_fc.w_0 ResNet50_pretrained/fc_0.w_0
mv ResNet50_pretrained/res_fc.b_0 ResNet50_pretrained/fc_0.b_0
# for sensitivity filter pruning
#-------------------------------
......@@ -62,12 +71,21 @@ python compress.py \
# for distillation with quantization
#-----------------------------------
#export CUDA_VISIBLE_DEVICES=0
#
## Fixing name conflicts in distillation
#mv ResNet50_pretrained/conv1_weights ResNet50_pretrained/res_conv1_weights
#mv ResNet50_pretrained/fc_0.w_0 ResNet50_pretrained/res_fc.w_0
#mv ResNet50_pretrained/fc_0.b_0 ResNet50_pretrained/res_fc.b_0
#
#python compress.py \
#--model "MobileNet" \
#--teacher_model "ResNet50" \
#--teacher_pretrained_model ./data/pretrain/ResNet50_pretrained \
#--compress_config ./configs/quantization_dist.yaml
#
#mv ResNet50_pretrained/res_conv1_weights ResNet50_pretrained/conv1_weights
#mv ResNet50_pretrained/res_fc.w_0 ResNet50_pretrained/fc_0.w_0
#mv ResNet50_pretrained/res_fc.b_0 ResNet50_pretrained/fc_0.b_0
# for uniform filter pruning with quantization
#---------------------------------------------
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册