-
Notifications
You must be signed in to change notification settings - Fork 281
add keras-in/keras-out to INC #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8f1f318
add keras-in/keras-out to INC
ClarkChin08 9c18029
clean the code
ClarkChin08 69911c1
fix the tune_cfg issue
ClarkChin08 8c6c743
fix the dataloader issue
ClarkChin08 a9f3dab
fix several issues
ClarkChin08 fa5e6ab
add ut case
lvliang-intel f3b89a1
Merge branch 'keras_in_out' of https://github.com/intel/neural-compre…
lvliang-intel 19b954c
add itex to requirements.txt
lvliang-intel 6b2de1c
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel 8b2c428
rebase master
lvliang-intel 3cb0873
remove the version check
ClarkChin08 1662e0b
change keras to tensorflow framework
ClarkChin08 9d5aaba
fix ut issue
lvliang-intel a97c86a
update ut itex binary with nightly master version
chensuyue 463403f
only enable keras adaptor for itex backend
lvliang-intel e18e526
Merge branch 'keras_in_out' of https://github.com/intel/neural-compre…
lvliang-intel 839b9a7
fix mnist copyright
lvliang-intel 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| Step-by-Step | ||
| ============ | ||
|
|
||
| This document list steps of reproducing Keras mnist model tuning results via Neural Compressor. | ||
| This example can run on Intel CPUs. | ||
|
|
||
| # Prerequisite | ||
|
|
||
| ### 1. Installation | ||
| Recommend python 3.6 or higher version. | ||
|
|
||
| ```shell | ||
| # Install Intel® Neural Compressor | ||
| pip install neural-compressor | ||
| ``` | ||
|
|
||
| ### 2. Install Tensorflow | ||
| ```shell | ||
| pip install tensorflow | ||
| ``` | ||
| > Note: Supported Tensorflow version > 2.10.0. | ||
|
|
||
| ### 3. Installation Dependency packages | ||
| ```shell | ||
| cd examples/keras/mnist/ | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| #### Quantizing the model on Intel CPU(Experimental) | ||
| Intel Extension for Tensorflow for Intel CPUs is experimental currently. It's not mandatory for quantizing the model on Intel CPUs. | ||
|
|
||
| ```shell | ||
| pip install --upgrade intel-extension-for-tensorflow[cpu] | ||
| ``` | ||
|
|
||
| # Run | ||
|
|
||
| ```shell | ||
| cd examples/keras/mnist/ | ||
| python mnist.py | ||
| ``` |
This file contains hidden or 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,107 @@ | ||
| #!/usr/bin/env python | ||
| # -*- coding: utf-8 -*- | ||
| # | ||
| # Copyright (c) 2022 Intel Corporation | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import tensorflow as tf | ||
| import numpy as np | ||
| from tensorflow import keras | ||
| from tensorflow.keras import layers | ||
| import time | ||
|
|
||
| num_classes = 10 | ||
|
|
||
| def build_dataset(): | ||
| # Load the data and split it between train and test sets | ||
| (x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data() | ||
|
|
||
| # Scale images to the [0, 1] range | ||
| x_train = x_train.astype("float32") / 255 | ||
| x_test = x_test.astype("float32") / 255 | ||
| # Make sure images have shape (28, 28, 1) | ||
| x_train = np.expand_dims(x_train, -1) | ||
| x_test = np.expand_dims(x_test, -1) | ||
|
|
||
| # convert class vectors to binary class matrices | ||
| y_train = keras.utils.to_categorical(y_train, num_classes) | ||
| y_test = keras.utils.to_categorical(y_test, num_classes) | ||
| return x_train, y_train, x_test, y_test | ||
|
|
||
| class Dataset(): | ||
| def __init__(self, ): | ||
| _, _ , self.inputs, self.labels = build_dataset() | ||
|
|
||
| def __getitem__(self, idx): | ||
| return self.inputs[idx], self.labels[idx] | ||
|
|
||
| def __len__(self): | ||
| assert len(self.inputs) == len(self.labels), 'inputs should have equal len with labels' | ||
| return len(self.inputs) | ||
|
|
||
| def build_model(x_train, y_train, x_test, y_test): | ||
| if os.path.exists('fp32_model'): | ||
| model = keras.models.load_model('fp32_model') | ||
| return model | ||
| # Model / data parameters | ||
| input_shape = (28, 28, 1) | ||
| model = keras.Sequential( | ||
| [ | ||
| keras.Input(shape=input_shape), | ||
| layers.Conv2D(32, kernel_size=(3, 3), activation="relu"), | ||
| layers.MaxPooling2D(pool_size=(2, 2)), | ||
| layers.Conv2D(64, kernel_size=(3, 3), activation="relu"), | ||
| layers.MaxPooling2D(pool_size=(2, 2)), | ||
| layers.Flatten(), | ||
| layers.Dropout(0.5), | ||
| layers.Dense(num_classes, activation="softmax"), | ||
| ] | ||
| ) | ||
|
|
||
| batch_size = 128 | ||
| epochs = 1 | ||
|
|
||
| model.compile(loss="categorical_crossentropy", optimizer="adam", | ||
| metrics=["accuracy"], run_eagerly=True) | ||
| model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_split=0.1) | ||
| model.summary() | ||
| if not os.path.exists('fp32_model'): | ||
| model.save('fp32_model') | ||
| return model | ||
|
|
||
| def eval_func(model): | ||
| x_train, y_train, x_test, y_test = build_dataset() | ||
| model.compile(metrics=["accuracy"], run_eagerly=False) | ||
| score = model.evaluate(x_test, y_test) | ||
| return score[1] | ||
|
|
||
| def main(): | ||
| x_train, y_train, x_test, y_test = build_dataset() | ||
| model = build_model(x_train, y_train, x_test, y_test) | ||
|
|
||
| from neural_compressor.quantization import fit | ||
| from neural_compressor.config import PostTrainingQuantConfig | ||
| from neural_compressor.utils.utility import set_random_seed | ||
| from neural_compressor.experimental import common | ||
| set_random_seed(9527) | ||
| config = PostTrainingQuantConfig(backend='itex') | ||
| quantized_model = fit(model, | ||
| conf=config, | ||
| calib_dataloader=common.DataLoader(Dataset(), batch_size=10), | ||
| eval_func=eval_func) | ||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
|
|
||
This file contains hidden or 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,3 @@ | ||
| tensorflow | ||
| neural-compressor | ||
| intel-extension-for-tensorflow[cpu] |
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.
Uh oh!
There was an error while loading. Please reload this page.