Skip to content
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

Add German Traffic Sign Recognition CNN to finn-examples #81

Merged
merged 12 commits into from
Apr 21, 2024
Merged
125 changes: 125 additions & 0 deletions build/gtsrb/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Copyright (C) 2024, Advanced Micro Devices, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of FINN nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import finn.builder.build_dataflow as build
import finn.builder.build_dataflow_config as build_cfg
from finn.builder.build_dataflow_config import default_build_dataflow_steps
from qonnx.core.datatype import DataType
import os
import shutil
import numpy as np
from onnx import helper as oh

models = [
"cnv_1w1a_gtsrb",
]

# which platforms to build the networks for
zynq_platforms = ["Pynq-Z1"]
platforms_to_build = zynq_platforms


def custom_step_add_preproc(model, cfg):
# GTSRB data with raw uint8 pixels is divided by 255 prior to training
# reflect this in the inference graph so we can perform inference directly
# on raw uint8 data
in_name = model.graph.input[0].name
new_in_name = model.make_new_valueinfo_name()
new_param_name = model.make_new_valueinfo_name()
div_param = np.asarray(255.0, dtype=np.float32)
new_div = oh.make_node(
"Div",
[in_name, new_param_name],
[new_in_name],
name="PreprocDiv",
)
model.set_initializer(new_param_name, div_param)
model.graph.node.insert(0, new_div)
model.graph.node[1].input[0] = new_in_name
# set input dtype to uint8
model.set_tensor_datatype(in_name, DataType["UINT8"])
return model


custom_build_steps = [custom_step_add_preproc] + default_build_dataflow_steps


# determine which shell flow to use for a given platform
def platform_to_shell(platform):
if platform in zynq_platforms:
return build_cfg.ShellFlowType.VIVADO_ZYNQ
else:
raise Exception("Unknown platform, can't determine ShellFlowType")


# create a release dir, used for finn-examples release packaging
os.makedirs("release", exist_ok=True)

for platform_name in platforms_to_build:
shell_flow_type = platform_to_shell(platform_name)
vitis_platform = None
# for Zynq, use the board name as the release name
# e.g. ZCU104
release_platform_name = platform_name
platform_dir = "release/%s" % release_platform_name
os.makedirs(platform_dir, exist_ok=True)
for model_name in models:
# set up the build configuration for this model
cfg = build_cfg.DataflowBuildConfig(
output_dir="output_%s_%s" % (model_name, release_platform_name),
target_fps=3000,
synth_clk_period_ns=10.0,
board=platform_name,
steps=custom_build_steps,
folding_config_file="folding_config/cnv_gtsrb_folding_config.json",
shell_flow_type=shell_flow_type,
vitis_platform=vitis_platform,
generate_outputs=[
build_cfg.DataflowOutputType.ESTIMATE_REPORTS,
build_cfg.DataflowOutputType.STITCHED_IP,
build_cfg.DataflowOutputType.RTLSIM_PERFORMANCE,
build_cfg.DataflowOutputType.BITFILE,
],
save_intermediate_models=True,
default_swg_exception=True,
)
model_file = "models/%s.onnx" % model_name
# launch FINN compiler to build
build.build_dataflow_cfg(model_file, cfg)
# copy bitfiles into release dir if found
bitfile_gen_dir = cfg.output_dir + "/bitfile"
files_to_check_and_copy = [
"finn-accel.bit",
"finn-accel.hwh",
"finn-accel.xclbin",
]
for f in files_to_check_and_copy:
src_file = bitfile_gen_dir + "/" + f
dst_file = platform_dir + "/" + f.replace("finn-accel", model_name)
if os.path.isfile(src_file):
shutil.copy(src_file, dst_file)
78 changes: 78 additions & 0 deletions build/gtsrb/folding_config/cnv_gtsrb_folding_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"Defaults": {},
"Thresholding_rtl_0": {
"PE": 1
},
"ConvolutionInputGenerator_rtl_0": {
"SIMD": 3,
"ram_style": "distributed"
},
"MVAU_hls_0": {
"PE": 16,
"SIMD": 3,
"ram_style": "auto"
},
"ConvolutionInputGenerator_rtl_1": {
"SIMD": 32,
"ram_style": "distributed"
},
"MVAU_hls_1": {
"PE": 32,
"SIMD": 32,
"ram_style": "auto"
},
"ConvolutionInputGenerator_rtl_2": {
"SIMD": 32,
"ram_style": "distributed"
},
"MVAU_hls_2": {
"PE": 16,
"SIMD": 32,
"ram_style": "auto"
},
"ConvolutionInputGenerator_rtl_3": {
"SIMD": 32,
"ram_style": "distributed"
},
"MVAU_hls_3": {
"PE": 16,
"SIMD": 32,
"ram_style": "auto"
},
"ConvolutionInputGenerator_rtl_4": {
"SIMD": 32,
"ram_style": "distributed"
},
"MVAU_hls_4": {
"PE": 4,
"SIMD": 32,
"ram_style": "auto"
},
"ConvolutionInputGenerator_rtl_5": {
"SIMD": 32,
"ram_style": "distributed"
},
"MVAU_hls_5": {
"PE": 1,
"SIMD": 32,
"ram_style": "auto"
},
"MVAU_hls_6": {
"PE": 1,
"SIMD": 4,
"ram_style": "auto"
},
"MVAU_hls_7": {
"PE": 1,
"SIMD": 8,
"ram_style": "auto"
},
"MVAU_hls_8": {
"PE": 4,
"SIMD": 1,
"ram_style": "auto"
},
"LabelSelect_hls_0": {
"PE": 1
}
}
2 changes: 2 additions & 0 deletions build/gtsrb/models/download-model.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
wget https://github.com/fastmachinelearning/qonnx_model_zoo/raw/feature/gtsrb_cnv/models/GTSRB/Brevitas_CNV1W1A/cnv_1w1a_gtsrb.onnx
3 changes: 2 additions & 1 deletion ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pipeline {
"kws",
"mobilenet-v1",
"resnet50",
"vgg10-radioml"]
"vgg10-radioml",
"gtsrb"]
createParallelBuilds(buildList)
createReleaseArea(buildList)
}
Expand Down
20 changes: 18 additions & 2 deletions finn_examples/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@
"num_outputs": 1,
}

_gtsrb_cnv_io_shape_dict = {
"idt": DataType["UINT8"],
"odt": DataType["INT16"],
"ishape_normal": (1, 32, 32, 3),
"oshape_normal": (1, 44),
"ishape_folded": (1, 1, 32, 32, 3, 1),
"oshape_folded": (1, 11, 4),
"ishape_packed": (1, 1, 32, 32, 3, 1),
"oshape_packed": (1, 11, 8),
}

_bincop_cnv_io_shape_dict = {
"idt": [DataType["UINT8"]],
"odt": [DataType["UINT8"]],
Expand Down Expand Up @@ -352,7 +363,6 @@ def resnet50_w1a2_imagenet(target_platform=None):
runtime_weight_dir=runtime_weight_dir,
)


def vgg10_w4a4_radioml(target_platform=None):
target_platform = resolve_target_platform(target_platform)
driver_mode = get_driver_mode()
Expand All @@ -366,7 +376,6 @@ def vgg10_w4a4_radioml(target_platform=None):
fclk_mhz=fclk_mhz,
)


def mlp_w2a2_unsw_nb15(target_platform=None):
target_platform = resolve_target_platform(target_platform)
driver_mode = get_driver_mode()
Expand All @@ -376,3 +385,10 @@ def mlp_w2a2_unsw_nb15(target_platform=None):
return FINNExampleOverlay(
filename, driver_mode, _unsw_nb15_mlp_io_shape_dict, fclk_mhz=fclk_mhz
)

def cnv_w1a1_gtsrb(target_platform=None):
target_platform = resolve_target_platform(target_platform)
driver_mode = get_driver_mode()
model_name = "cnv-gtsrb-w1a1"
filename = find_bitfile(model_name, target_platform)
return FINNExampleOverlay(filename, driver_mode, _gtsrb_cnv_io_shape_dict)
Loading