Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix keras
Signed-off-by: zehao-intel <[email protected]>
  • Loading branch information
zehao-intel committed Jul 15, 2024
commit b92543c8d682c516e2cf31eb54c6072b03676e02
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pip install -r requirements.txt

The pretrained model is provided by [Keras Applications](https://keras.io/api/applications/). prepare the model, Run as follow:
```
python prepare_model.py --output_model=/path/to/model
python prepare_model.py --output_model=./inception_v3_keras
```
`--output_model ` the model should be saved as SavedModel format or H5 format.

Expand All @@ -40,9 +40,9 @@ python prepare_model.py --output_model=/path/to/model
```shell
cd examples/3.x_api/tensorflow/keras/cv/
# convert validation subset
bash prepare_dataset.sh --output_dir=/inception_v3/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
bash prepare_dataset.sh --output_dir=./inception_v3/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
# convert train subset
bash prepare_dataset.sh --output_dir=/inception_v3/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
bash prepare_dataset.sh --output_dir=./inception_v3/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
cd inception_v3/quantization/ptq
```
> **Note**:
Expand All @@ -55,7 +55,7 @@ python prepare_model.py --output_model=/path/to/model

## Quantization
```shell
bash run_quant.sh --input_model=./inception_v3_keras/ --output_model=./result --dataset_location=/path/to/evaluation/dataset
bash run_quant.sh --input_model=./inception_v3_keras --output_model=./result --dataset_location=/path/to/evaluation/dataset
```

## Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pip install -r requirements.txt

The pretrained model is provided by [Keras Applications](https://keras.io/api/applications/). prepare the model, Run as follow:
```
python prepare_model.py --output_model=/path/to/model
python prepare_model.py --output_model=./mobilenet_v2_keras
```
`--output_model ` the model should be saved as SavedModel format or H5 format.

Expand All @@ -39,9 +39,9 @@ python prepare_model.py --output_model=/path/to/model
```shell
cd examples/3.x_api/tensorflow/keras/cv/
# convert validation subset
bash prepare_dataset.sh --output_dir=/mobilenet_v2/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
bash prepare_dataset.sh --output_dir=./mobilenet_v2/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
# convert train subset
bash prepare_dataset.sh --output_dir=/mobilenet_v2/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
bash prepare_dataset.sh --output_dir=./mobilenet_v2/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
cd mobilenet_v2/quantization/ptq
```
> **Note**:
Expand All @@ -54,7 +54,7 @@ python prepare_model.py --output_model=/path/to/model

## Quantization
```shell
bash run_quant.sh --input_model=./mobilenet_v2_keras/ --output_model=./result --dataset_location=/path/to/evaluation/dataset
bash run_quant.sh --input_model=./mobilenet_v2_keras --output_model=./result --dataset_location=/path/to/evaluation/dataset
```

## Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pip install -r requirements.txt

The pretrained model is provided by [Keras Applications](https://keras.io/api/applications/). prepare the model, Run as follow:
```
python prepare_model.py --output_model=/path/to/model
python prepare_model.py --output_model=./resnetv2_50_keras
```
`--output_model ` the model should be saved as SavedModel format or H5 format.

Expand All @@ -39,9 +39,9 @@ python prepare_model.py --output_model=/path/to/model
```shell
cd examples/3.x_api/tensorflow/keras/cv/
# convert validation subset
bash prepare_dataset.sh --output_dir=/resnetv2_50/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
bash prepare_dataset.sh --output_dir=./resnetv2_50/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/val/ --subset=validation
# convert train subset
bash prepare_dataset.sh --output_dir=/resnetv2_50/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
bash prepare_dataset.sh --output_dir=./resnetv2_50/quantization/ptq/data --raw_dir=/PATH/TO/img_raw/train/ --subset=train
cd resnetv2_50/quantization/ptq
```
> **Note**:
Expand All @@ -54,7 +54,7 @@ python prepare_model.py --output_model=/path/to/model

## Quantization
```shell
bash run_quant.sh --input_model=./resnetv2_50_keras/ --output_model=./result --dataset_location=/path/to/evaluation/dataset
bash run_quant.sh --input_model=./resnetv2_50_keras --output_model=./result --dataset_location=/path/to/evaluation/dataset
```

## Benchmark
Expand Down
27 changes: 14 additions & 13 deletions neural_compressor/tensorflow/algorithms/static_quant/keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,20 @@ def _fuse_bn_keras2(self, fuse_conv_bn, fp32_layers): # pragma: no cover
fuse_layers.append(layer)
else:
for bound_node in layer._inbound_nodes:
inbound_layer = bound_node.inbound_layers
if isinstance(inbound_layer, list) and len(inbound_layer) == 0:
continue
if inbound_layer in self.bn_weights.keys():
for bn_inbound_node in inbound_layer._inbound_nodes:
bn_inbound_layer = bn_inbound_node.inbound_layers
if bn_inbound_layer.name in self.conv_weights.keys():
new_bound_nodes.append(bn_inbound_node)
else:
if bound_node not in new_bound_nodes:
new_bound_nodes.append(bound_node)
else:
new_bound_nodes.append(bound_node)
inbound_layers = bound_node.inbound_layers
if not isinstance(inbound_layers, list):
inbound_layers = [inbound_layers]
for inbound_layer in inbound_layers:
if inbound_layer in self.bn_weights.keys():
for bn_inbound_node in inbound_layer._inbound_nodes:
bn_inbound_layer = bn_inbound_node.inbound_layers
if bn_inbound_layer.name in self.conv_weights.keys():
new_bound_nodes.append(bn_inbound_node)
else:
if bound_node not in new_bound_nodes:
new_bound_nodes.append(bound_node)
else:
new_bound_nodes.append(bound_node)

layer._inbound_nodes.clear()
for bound_node in new_bound_nodes:
Expand Down