Skip to content

Commit

Permalink
#120 Update example code for the changed interface (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Only-bottle committed Jan 29, 2024
1 parent 8ce0805 commit 03c88e2
Show file tree
Hide file tree
Showing 21 changed files with 540 additions and 743 deletions.
18 changes: 11 additions & 7 deletions examples/compressor/automatic_compression.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
from netspresso.compressor import Compressor
from netspresso import NetsPresso


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"
compressor = Compressor(email=EMAIL, password=PASSWORD)

MODEL_NAME = "test_graphmodule_pt"
netspresso = NetsPresso(email=EMAIL, password=PASSWORD)

# 1. Declare compressor
compressor = netspresso.compressor()

# 2. Set variables for compression
INPUT_SHAPES = [{"batch": 1, "channel": 3, "dimension": [224, 224]}]
INPUT_MODEL_PATH = "./examples/sample_models/graphmodule.pt"
OUTPUT_MODEL_PATH = "./outputs/compressed/graphmodule_automatic_compression"
OUTPUT_DIR = "./outputs/compressed1/pytorch_automatic_compression_1"
COMPRESSION_RATIO = 0.5

# 3. Run automatic compression
compressed_model = compressor.automatic_compression(
model_name=MODEL_NAME,
input_shapes=INPUT_SHAPES,
input_path=INPUT_MODEL_PATH,
output_path=OUTPUT_MODEL_PATH,
input_model_path=INPUT_MODEL_PATH,
output_dir=OUTPUT_DIR,
compression_ratio=COMPRESSION_RATIO,
)
28 changes: 15 additions & 13 deletions examples/compressor/create_model.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
from loguru import logger

from netspresso.compressor import Compressor, Task, Framework
from netspresso import NetsPresso
from netspresso.enums import Framework


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"
compressor = Compressor(email=EMAIL, password=PASSWORD)

UPLOAD_MODEL_NAME = "test_h5"
TASK = Task.IMAGE_CLASSIFICATION
netspresso = NetsPresso(email=EMAIL, password=PASSWORD)

# 1. Declare compressor
compressor = netspresso.compressor()

# 2. Set variables for upload
UPLOAD_MODEL_NAME = "sample_h5"
FRAMEWORK = Framework.TENSORFLOW_KERAS
UPLOAD_MODEL_PATH = "./examples/sample_models/mobilenetv1.h5"
INPUT_MODEL_PATH = "./examples/sample_models/mobilenetv1.h5"
INPUT_SHAPES = [{"batch": 1, "channel": 3, "dimension": [32, 32]}]

# Upload Model
# 3. Upload model
model = compressor.upload_model(
model_name=UPLOAD_MODEL_NAME,
task=TASK,
framework=FRAMEWORK,
file_path=UPLOAD_MODEL_PATH,
input_model_path=INPUT_MODEL_PATH,
input_shapes=INPUT_SHAPES,
framework=FRAMEWORK,
)

# Get Model
# 4. Get Model
model = compressor.get_model(model_id=model.model_id)
logger.info(f"model id: {model.model_id}")
print(model)
45 changes: 0 additions & 45 deletions examples/compressor/delete_model.py

This file was deleted.

50 changes: 22 additions & 28 deletions examples/compressor/manual_compression.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,48 @@
from loguru import logger

from netspresso.compressor import (
Compressor,
CompressionMethod,
Policy,
LayerNorm,
GroupPolicy,
Options,
)
from netspresso import NetsPresso
from netspresso.enums import CompressionMethod, Policy, LayerNorm, GroupPolicy, Options


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"
compressor = Compressor(email=EMAIL, password=PASSWORD)

# Upload Model
UPLOAD_MODEL_NAME = "test_pt"
UPLOAD_MODEL_PATH = "./examples/sample_models/graphmodule.pt"
netspresso = NetsPresso(email=EMAIL, password=PASSWORD)

# 1. Declare compressor
compressor = netspresso.compressor()

# 2. Upload model
MODEL_NAME = "sample_graphmodule_pt"
INPUT_MODEL_PATH = "./examples/sample_models/graphmodule.pt"
INPUT_SHAPES = [{"batch": 1, "channel": 3, "dimension": [224, 224]}]
model = compressor.upload_model(
model_name=UPLOAD_MODEL_NAME,
file_path=UPLOAD_MODEL_PATH,
model_name=MODEL_NAME,
input_model_path=INPUT_MODEL_PATH,
input_shapes=INPUT_SHAPES,
)

# Select Compression Method
# 3. Select compression method
COMPRESSION_METHOD = CompressionMethod.PR_L2
OPTIONS = Options(
policy=Policy.AVERAGE,
layer_norm=LayerNorm.STANDARD_SCORE,
group_policy=GroupPolicy.AVERAGE,
reshape_channel_axis=-1,
)
compression_1 = compressor.select_compression_method(
compression_info = compressor.select_compression_method(
model_id=model.model_id,
compression_method=COMPRESSION_METHOD,
options=OPTIONS,
)
logger.info(f"compression method: {compression_1.compression_method}")
logger.info(f"available layers: {compression_1.available_layers}")
print(f"compression method: {compression_info.compression_method}")
print(f"available layers: {compression_info.available_layers}")

# Set Compression Params
for available_layer in compression_1.available_layers[:5]:
# 4. Set params for compression(ratio or rank)
for available_layer in compression_info.available_layers[:5]:
available_layer.values = [0.2]

# Compress Model
COMPRESSED_MODEL_NAME = "test_l2norm"
OUTPUT_PATH = "./outputs/compressed/graphmodule_manual"
# 5. Compress model
OUTPUT_DIR = "./outputs/compressed/graphmodule_manual"
compressed_model = compressor.compress_model(
compression=compression_1,
model_name=COMPRESSED_MODEL_NAME,
output_path=OUTPUT_PATH,
compression=compression_info,
output_dir=OUTPUT_DIR,
)
24 changes: 12 additions & 12 deletions examples/compressor/recommendation_compression.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
from netspresso.compressor import (
Compressor,
CompressionMethod,
RecommendationMethod,
)
from netspresso import NetsPresso
from netspresso.enums import CompressionMethod, RecommendationMethod


EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"
compressor = Compressor(email=EMAIL, password=PASSWORD)

# Upload Model
MODEL_NAME = "test_pt"
netspresso = NetsPresso(email=EMAIL, password=PASSWORD)

# 1. Declare compressor
compressor = netspresso.compressor()

# 2. Set variables for compression
INPUT_MODEL_PATH = "./examples/sample_models/graphmodule.pt"
OUTPUT_MODEL_PATH = "./outputs/compressed/graphmodule_recommend"
OUTPUT_DIR = "./outputs/compressed/graphmodule_recommend"
INPUT_SHAPES = [{"batch": 1, "channel": 3, "dimension": [224, 224]}]
COMPRESSION_METHOD = CompressionMethod.PR_L2
RECOMMENDATION_METHOD = RecommendationMethod.SLAMP
RECOMMENDATION_RATIO = 0.5

# 3. Run recommendation compression
compressed_model = compressor.recommendation_compression(
model_name=MODEL_NAME,
compression_method=COMPRESSION_METHOD,
recommendation_method=RECOMMENDATION_METHOD,
recommendation_ratio=RECOMMENDATION_RATIO,
input_path=INPUT_MODEL_PATH,
output_path=OUTPUT_MODEL_PATH,
input_model_path=INPUT_MODEL_PATH,
output_dir=OUTPUT_DIR,
input_shapes=INPUT_SHAPES,
)
54 changes: 54 additions & 0 deletions examples/converter & benchmarker/keras2tflite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName


###
# Available target frameworks for conversion with keras models
#
# Framework.TENSORFLOW_LITE
#

###
# Available devices for Framework.TENSORFLOW_LITE (target_framework)
#
# DeviceName.RASPBERRY_PI_4B
# DeviceName.RASPBERRY_PI_3B_PLUS
# DeviceName.RASPBERRY_PI_ZERO_W
# DeviceName.RASPBERRY_PI_ZERO_2W
#

EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"

netspresso = NetsPresso(email=EMAIL, password=PASSWORD)

# 1. Declare converter
converter = netspresso.converter()

# 2. Set variables for convert
INPUT_MODEL_PATH = "./examples/sample_models/mobilenetv1.h5"
OUTPUT_DIR = "./outputs/converted/TFLITE_RASPBERRY_PI_4B"
TARGET_FRAMEWORK = Framework.TENSORFLOW_LITE
TARGET_DEVICE_NAME = DeviceName.RASPBERRY_PI_4B

# 3. Run convert
conversion_task = converter.convert_model(
input_model_path=INPUT_MODEL_PATH,
output_dir=OUTPUT_DIR,
target_framework=TARGET_FRAMEWORK,
target_device_name=TARGET_DEVICE_NAME,
)
print(conversion_task)

# 4. Declare benchmarker
benchmarker = netspresso.benchmarker()

# 5. Set variables for benchmark
CONVERTED_MODEL_PATH = "./outputs/converted/TFLITE_RASPBERRY_PI_4B/TFLITE_RASPBERRY_PI_4B.tflite"

# 6. Run benchmark
benchmark_task = benchmarker.benchmark_model(
input_model_path=CONVERTED_MODEL_PATH,
target_device_name=TARGET_DEVICE_NAME,
)
print(f"model inference latency: {benchmark_task['result']['latency']} ms")
59 changes: 59 additions & 0 deletions examples/converter & benchmarker/keras2tflite_int8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from netspresso import NetsPresso
from netspresso.enums import Framework, DeviceName, DataType


# Available target frameworks for conversion with keras models
#
# Framework.TENSORFLOW_LITE
#

# Available devices for Framework.TENSORFLOW_LITE and DataType.INT8 (target_framework)
#
# DeviceName.RASPBERRY_PI_4B
# DeviceName.RASPBERRY_PI_3B_PLUS
# DeviceName.RASPBERRY_PI_ZERO_W
# DeviceName.RASPBERRY_PI_ZERO_2W
# DeviceName.ALIF_ENSEMBLE_E7_DEVKIT_GEN2
# DeviceName.RENESAS_RA8D1
#

EMAIL = "YOUR_EMAIL"
PASSWORD = "YOUR_PASSWORD"

netspresso = NetsPresso(email=EMAIL, password=PASSWORD)

# 1. Declare converter
converter = netspresso.converter()

# 2. Set variables for convert
INPUT_MODEL_PATH = "./examples/sample_models/mobilenetv1_cifar100_automatic.h5"
OUTPUT_DIR = "./outputs/converted/TFLITE_INT8_RENESAS_RA8D1"
TARGET_FRAMEWORK = Framework.TENSORFLOW_LITE
TARGET_DEVICE_NAME = DeviceName.RENESAS_RA8D1
TARGET_DATA_TYPE = DataType.INT8
DATASET_PATH = "./examples/sample_datasets/20x32x32x3.npy"

# 3. Run convert
conversion_task = converter.convert_model(
input_model_path=INPUT_MODEL_PATH,
output_dir=OUTPUT_DIR,
target_framework=TARGET_FRAMEWORK,
target_data_type=TARGET_DATA_TYPE,
target_device_name=TARGET_DEVICE_NAME,
dataset_path=DATASET_PATH,
)
print(conversion_task)

# 4. Declare benchmarker
benchmarker = netspresso.benchmarker()

# 5. Set variables for benchmark
CONVERTED_MODEL_PATH = "./outputs/converted/TFLITE_INT8_RENESAS_RA8D1/TFLITE_INT8_RENESAS_RA8D1.tflite"

# 6. Run benchmark
benchmark_task = benchmarker.benchmark_model(
input_model_path=CONVERTED_MODEL_PATH,
target_data_type=TARGET_DATA_TYPE,
target_device_name=TARGET_DEVICE_NAME,
)
print(f"model inference latency: {benchmark_task['result']['latency']} ms")
Loading

0 comments on commit 03c88e2

Please sign in to comment.