Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .azure-pipelines/scripts/ut/env_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ elif [[ "${tensorflow_version}" != "" ]]; then
pip install intel-tensorflow==${tensorflow_version}
fi

if [[ "${itex_version}" != "" ]]; then
if [[ "${itex_version}" == "nightly" ]]; then
pip install /tf_dataset/itex_binary/221209/intel_extension_for_tensorflow-1.1.0-cp38-cp38-linux_x86_64.whl
pip install /tf_dataset/itex_binary/221209/intel_extension_for_tensorflow_lib-1.1.0.0-cp38-cp38-linux_x86_64.whl
elif [[ "${itex_version}" != "" ]]; then
pip install --upgrade intel-extension-for-tensorflow[cpu]==${itex_version}
fi

Expand Down
2 changes: 1 addition & 1 deletion .azure-pipelines/scripts/ut/run_basic_itex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ python -c "import neural_compressor as nc;print(nc.version.__version__)"
echo "run basic itex"

echo "specify fwk version..."
export itex_version='1.0.0'
export itex_version='nightly'
export tensorflow_version='2.10.0-official'

echo "set up UT env..."
Expand Down
41 changes: 41 additions & 0 deletions examples/keras/mnist/README.md
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
```
107 changes: 107 additions & 0 deletions examples/keras/mnist/mnist.py
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()

3 changes: 3 additions & 0 deletions examples/keras/mnist/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tensorflow
neural-compressor
intel-extension-for-tensorflow[cpu]
Loading