Skip to content

Commit

Permalink
Fix api docs easeof use (PaddlePaddle#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
minghaoBD authored May 17, 2021
1 parent afb884f commit 022c5fb
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 59 deletions.
10 changes: 3 additions & 7 deletions demo/dygraph/unstructured_pruning/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def compress(args):
test_reader = None
if args.data == "imagenet":
import imagenet_reader as reader
val_dataset = reader.ImageNetDataset(data_dir='/data', mode='val')
val_dataset = reader.ImageNetDataset(mode='val')
class_dim = 1000
elif args.data == "cifar10":
normalize = T.Normalize(
Expand All @@ -47,13 +47,12 @@ def compress(args):

places = paddle.static.cuda_places(
) if args.use_gpu else paddle.static.cpu_places()
batch_size_per_card = int(args.batch_size / len(places))
valid_loader = paddle.io.DataLoader(
val_dataset,
places=places,
drop_last=False,
return_list=True,
batch_size=batch_size_per_card,
batch_size=args.batch_size,
shuffle=False,
use_shared_memory=True)

Expand All @@ -70,15 +69,12 @@ def test(epoch):
y_data = paddle.to_tensor(data[1])
if args.data == 'cifar10':
y_data = paddle.unsqueeze(y_data, 1)
end_time = time.time()

logits = model(x_data)
loss = F.cross_entropy(logits, y_data)
acc_top1 = paddle.metric.accuracy(logits, y_data, k=1)
acc_top5 = paddle.metric.accuracy(logits, y_data, k=5)

acc_top1_ns.append(acc_top1.numpy())
acc_top5_ns.append(acc_top5.numpy())
end_time = time.time()
if batch_id % args.log_period == 0:
_logger.info(
"Eval epoch[{}] batch[{}] - acc_top1: {}; acc_top5: {}; time: {}".
Expand Down
8 changes: 3 additions & 5 deletions demo/dygraph/unstructured_pruning/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
add_arg('batch_size', int, 64 * 4, "Minibatch size.")
add_arg('batch_size_for_validation', int, 64, "Minibatch size for validation.")
add_arg('use_gpu', bool, True, "Whether to use GPU or not.")
add_arg('lr', float, 0.1, "The learning rate used to fine-tune pruned model.")
add_arg('lr_strategy', str, "piecewise_decay", "The learning rate decay strategy.")
Expand Down Expand Up @@ -121,7 +122,7 @@ def compress(args):
places=place,
drop_last=False,
return_list=True,
batch_size=64,
batch_size=args.batch_size_for_validation,
shuffle=False,
use_shared_memory=True)
step_per_epoch = int(
Expand All @@ -146,15 +147,12 @@ def test(epoch):
y_data = paddle.to_tensor(data[1])
if args.data == 'cifar10':
y_data = paddle.unsqueeze(y_data, 1)
end_time = time.time()

logits = model(x_data)
loss = F.cross_entropy(logits, y_data)
acc_top1 = paddle.metric.accuracy(logits, y_data, k=1)
acc_top5 = paddle.metric.accuracy(logits, y_data, k=5)

acc_top1_ns.append(acc_top1.numpy())
acc_top5_ns.append(acc_top5.numpy())
end_time = time.time()
if batch_id % args.log_period == 0:
_logger.info(
"Eval epoch[{}] batch[{}] - acc_top1: {}; acc_top5: {}; time: {}".
Expand Down
14 changes: 4 additions & 10 deletions demo/unstructured_prune/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
parser = argparse.ArgumentParser(description=__doc__)
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
add_arg('batch_size', int, 64*12, "Minibatch size.")
add_arg('batch_size', int, 64, "Minibatch size.")
add_arg('use_gpu', bool, True, "Whether to use GPU or not.")
add_arg('model', str, "MobileNet", "The target model.")
add_arg('pruned_model', str, "models", "Whether to use pretrained model.")
Expand All @@ -44,8 +44,8 @@ def compress(args):
image_shape = "1,28,28"
elif args.data == "imagenet":
import imagenet_reader as reader
train_dataset = reader.ImageNetDataset(data_dir='/data', mode='train')
val_dataset = reader.ImageNetDataset(data_dir='/data', mode='val')
train_dataset = reader.ImageNetDataset(mode='train')
val_dataset = reader.ImageNetDataset(mode='val')
class_dim = 1000
image_shape = "3,224,224"
else:
Expand All @@ -71,7 +71,6 @@ def compress(args):
use_shared_memory=True,
batch_size=batch_size_per_card,
shuffle=False)
step_per_epoch = int(np.ceil(len(train_dataset) * 1. / args.batch_size))

# model definition
model = models.__dict__[args.model]()
Expand Down Expand Up @@ -103,12 +102,7 @@ def test(epoch, program):
for batch_id, data in enumerate(valid_loader):
start_time = time.time()
acc_top1_n, acc_top5_n = exe.run(
program,
feed={
"image": data[0].get('image'),
"label": data[0].get('label'),
},
fetch_list=[acc_top1.name, acc_top5.name])
program, feed=data, fetch_list=[acc_top1.name, acc_top5.name])
end_time = time.time()
if batch_id % args.log_period == 0:
_logger.info(
Expand Down
3 changes: 2 additions & 1 deletion demo/unstructured_prune/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
add_arg = functools.partial(add_arguments, argparser=parser)
# yapf: disable
add_arg('batch_size', int, 64 * 4, "Minibatch size.")
add_arg('batch_size_for_validation', int, 64, "Minibatch size for validation.")
add_arg('use_gpu', bool, True, "Whether to use GPU or not.")
add_arg('model', str, "MobileNet", "The target model.")
add_arg('pretrained_model', str, "../pretrained_model/MobileNetV1_pretrained", "Whether to use pretrained model.")
Expand Down Expand Up @@ -123,7 +124,7 @@ def compress(args):
drop_last=False,
return_list=False,
use_shared_memory=True,
batch_size=batch_size_per_card,
batch_size=args.batch_size_for_validation,
shuffle=False)
step_per_epoch = int(np.ceil(len(train_dataset) * 1. / args.batch_size))

Expand Down
58 changes: 44 additions & 14 deletions docs/zh_cn/api_cn/dygraph/pruners/unstructured_pruner.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ UnstructuredPruner
.. py:class:: paddleslim.UnstructuredPruner(model, mode, threshold=0.01, ratio=0.3, skip_params_func=None)
`源代码 <https://github.com/minghaoBD/PaddleSlim/blob/update_unstructured_pruning_docs/paddleslim/dygraph/prune/unstructured_pruner.py>`_
`源代码 <https://github.com/PaddlePaddle/PaddleSlim/blob/develop/paddleslim/dygraph/prune/unstructured_pruner.py>`_

对于神经网络中的参数进行非结构化稀疏。非结构化稀疏是指,根据某些衡量指标,将不重要的参数置0。其不按照固定结构剪裁(例如一个通道等),这是和结构化剪枝的主要区别。

Expand All @@ -23,11 +23,16 @@ UnstructuredPruner

**示例代码:**

此示例不能直接运行,因为需要定义和加载模型,详细用法请参考 `这里 <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/dygraph/unstructured_pruning>`_

.. code-block:: python
import paddle
from paddleslim import UnstructuredPruner
from paddle.vision.models import LeNet as net
import numpy as np
place = paddle.set_device('cpu')
model = net(num_classes=10)
pruner = UnstructuredPruner(model, mode='ratio', ratio=0.5)
..
Expand All @@ -38,13 +43,19 @@ UnstructuredPruner

**示例代码:**

此示例不能直接运行,因为需要定义和加载模型,详细用法请参考 `这里 <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/dygraph/unstructured_pruning>`_

.. code-block:: python
from paddleslim import UnstructuredPruner
from paddle.vision.models import LeNet as net
import numpy as np
place = paddle.set_device('cpu')
model = net(num_classes=10)
pruner = UnstructuredPruner(model, mode='ratio', ratio=0.5)
print(pruner.threshold)
pruner.step()
print(pruner.threshold) # 可以看出,这里的threshold和上面打印的不同,这是因为step函数根据设定的ratio更新了threshold数值,便于剪裁操作。
..
Expand All @@ -54,13 +65,23 @@ UnstructuredPruner

**示例代码:**

此示例不能直接运行,因为需要定义和加载模型,详细用法请参考 `这里 <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/dygraph/unstructured_pruning>`_

.. code-block:: python
from paddleslim import UnstructuredPruner
pruner = UnstructuredPruner(model, mode='ratio', ratio=0.5)
from paddle.vision.models import LeNet as net
import numpy as np
place = paddle.set_device('cpu')
model = net(num_classes=10)
pruner = UnstructuredPruner(model, mode='threshold', threshold=0.5)
density = UnstructuredPruner.total_sparse(model)
print(density)
model(paddle.to_tensor(
np.random.uniform(0, 1, [16, 1, 28, 28]), dtype='float32'))
pruner.update_params()
density = UnstructuredPruner.total_sparse(model)
print(density) # 可以看出,这里打印的模型稠密度与上述不同,这是因为update_params()函数置零了所有绝对值小于0.5的权重。
..
Expand All @@ -78,13 +99,17 @@ UnstructuredPruner

**示例代码:**

此示例不能直接运行,因为需要定义和加载模型,详细用法请参考 `这里 <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/dygraph/unstructured_pruning>`_

.. code-block:: python
from paddleslim import UnstructuredPruner
density = UnstructuredPruner.total_sparse(model)
from paddle.vision.models import LeNet as net
import numpy as np
place = paddle.set_device('cpu')
model = net(num_classes=10)
density = UnstructuredPruner.total_sparse(model)
print(density)
..
.. py:method:: paddleslim.UnstructuredPruner.summarize_weights(model, ratio=0.1)
Expand All @@ -102,12 +127,17 @@ UnstructuredPruner

**示例代码:**

此示例不能直接运行,因为需要定义和加载模型,详细用法请参考 `这里 <https://github.com/PaddlePaddle/PaddleSlim/tree/develop/demo/dygraph/unstructured_pruning>`_

.. code-block:: python
from paddleslim import UnstructuredPruner
from paddle.vision.models import LeNet as net
import numpy as np
place = paddle.set_device('cpu')
model = net(num_classes=10)
pruner = UnstructuredPruner(model, mode='ratio', ratio=0.5)
threshold = pruner.summarize_weights(model, ratio=0.1)
threshold = pruner.summarize_weights(model, 0.5)
print(threshold)
..
Loading

0 comments on commit 022c5fb

Please sign in to comment.