-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add keras-in/keras-out to INC (#243)
Signed-off-by: Lv, Liang1 <[email protected]> Signed-off-by: chensuyue <[email protected]> Co-authored-by: Lv, Liang1 <[email protected]> Co-authored-by: chensuyue <[email protected]>
- Loading branch information
1 parent
a230726
commit 4fa7531
Showing
20 changed files
with
1,386 additions
and
66 deletions.
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,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 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 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.