This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Compression for Tensorflow #2755
Merged
+414
−545
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
21b25b7
draft
a5c82fd
update wrapper
0670561
fix default layer names
515121f
refactor
0177ba4
remove old ut
949f52b
add base classes' doc
9389ff0
Merge branch 'master' into tf-compress
3e915d0
Merge remote-tracking branch 'ms/master' into tf-compress
d51a6c8
update doc
d3b2bfb
Merge remote-tracking branch 'lz/tf-compress' into tf-compress
9ea5503
fix doc
7a22fd9
fix lint
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import argparse | ||
|
||
import tensorflow as tf | ||
|
||
import nni.compression.tensorflow | ||
|
||
prune_config = { | ||
'level': { | ||
'dataset_name': 'mnist', | ||
'model_name': 'naive', | ||
'pruner_class': nni.compression.tensorflow.LevelPruner, | ||
'config_list': [{ | ||
'sparsity': 0.9, | ||
'op_types': ['default'], | ||
}] | ||
}, | ||
} | ||
|
||
|
||
def get_dataset(dataset_name='mnist'): | ||
assert dataset_name == 'mnist' | ||
|
||
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() | ||
x_train = x_train[..., tf.newaxis] / 255.0 | ||
x_test = x_test[..., tf.newaxis] / 255.0 | ||
return (x_train, y_train), (x_test, y_test) | ||
|
||
|
||
def create_model(model_name='naive'): | ||
assert model_name == 'naive' | ||
return tf.keras.Sequential([ | ||
tf.keras.layers.Conv2D(filters=20, kernel_size=5), | ||
tf.keras.layers.BatchNormalization(), | ||
tf.keras.layers.ReLU(), | ||
tf.keras.layers.MaxPool2D(pool_size=2), | ||
tf.keras.layers.Conv2D(filters=20, kernel_size=5), | ||
tf.keras.layers.BatchNormalization(), | ||
tf.keras.layers.ReLU(), | ||
tf.keras.layers.MaxPool2D(pool_size=2), | ||
tf.keras.layers.Flatten(), | ||
tf.keras.layers.Dense(units=500), | ||
tf.keras.layers.ReLU(), | ||
tf.keras.layers.Dense(units=10), | ||
tf.keras.layers.Softmax() | ||
]) | ||
|
||
|
||
def create_pruner(model, pruner_name): | ||
pruner_class = prune_config[pruner_name]['pruner_class'] | ||
config_list = prune_config[pruner_name]['config_list'] | ||
return pruner_class(model, config_list) | ||
|
||
|
||
def main(args): | ||
model_name = prune_config[args.pruner_name]['model_name'] | ||
dataset_name = prune_config[args.pruner_name]['dataset_name'] | ||
train_set, test_set = get_dataset(dataset_name) | ||
model = create_model(model_name) | ||
|
||
optimizer = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9, decay=1e-4) | ||
model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy']) | ||
|
||
print('start training') | ||
model.fit(train_set[0], train_set[1], batch_size=args.batch_size, epochs=args.pretrain_epochs, validation_data=test_set) | ||
|
||
print('start model pruning') | ||
optimizer_finetune = tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0.9, decay=1e-4) | ||
pruner = create_pruner(model, args.pruner_name) | ||
model = pruner.compress() | ||
model.compile(optimizer=optimizer_finetune, loss='sparse_categorical_crossentropy', metrics=['accuracy']) | ||
QuanluZhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model.fit(train_set[0], train_set[1], batch_size=args.batch_size, epochs=args.prune_epochs, validation_data=test_set) | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--pruner_name', type=str, default='level') | ||
parser.add_argument('--batch_size', type=int, default=256) | ||
parser.add_argument('--pretrain_epochs', type=int, default=10) | ||
parser.add_argument('--prune_epochs', type=int, default=10) | ||
|
||
args = parser.parse_args() | ||
main(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT license. | ||
|
||
from .compressor import LayerInfo, Compressor, Pruner, Quantizer | ||
from .builtin_pruners import * | ||
from .builtin_quantizers import * | ||
from .compressor import Compressor, Pruner | ||
from .pruning import * |
195 changes: 0 additions & 195 deletions
195
src/sdk/pynni/nni/compression/tensorflow/builtin_pruners.py
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we should keep Chinese doc unchanged, and let @JunweiSUN to update Chinese doc using crowdin