297 lines
7.6 KiB
Plaintext
297 lines
7.6 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Demo"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Set Up\n",
|
|
"prerequisites\n",
|
|
"- python 3.12\n",
|
|
"\n",
|
|
"``` shell\n",
|
|
"$ cd ./package/{platform}/\n",
|
|
"$ pip install KneronPLUS-2.3.0-py3-none-any.whl\n",
|
|
"$ pip install --force-reinstall KneronPLUS-2.3.0-py3-none-any.whl\n",
|
|
"$ pip install opencv-pythn\n",
|
|
"\n",
|
|
"```"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Connect KL520"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import cv2\n",
|
|
"import kp"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"22\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"device_descriptors = kp.core.scan_devices()\n",
|
|
"\n",
|
|
"if 0 < device_descriptors.device_descriptor_number:\n",
|
|
" usb_port_id = device_descriptors.device_descriptor_list[0].usb_port_id\n",
|
|
" print(usb_port_id)\n",
|
|
"else:\n",
|
|
" print('Error: no Kneron device connect.')\n",
|
|
" exit(0)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"device_group = kp.core.connect_devices(usb_port_ids=[22])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"kp.core.set_timeout(device_group=device_group,\n",
|
|
" milliseconds=5000)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Load firmware, model, and test image"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"SCPU_FW_PATH = '../../res/firmware/KL520/fw_scpu.bin'\n",
|
|
"NCPU_FW_PATH = '../../res/firmware/KL520/fw_ncpu.bin'\n",
|
|
"kp.core.load_firmware_from_file(device_group=device_group,\n",
|
|
" scpu_fw_path=SCPU_FW_PATH,\n",
|
|
" ncpu_fw_path=NCPU_FW_PATH)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"MODEL_FILE_PATH = './photo_scorer_520.nef'\n",
|
|
"model_nef_descriptor = kp.core.load_model_from_file(device_group=device_group,\n",
|
|
" file_path=MODEL_FILE_PATH)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### pre-processing"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"IMAGE_FILE_PATH = './test.jpg'\n",
|
|
"\n",
|
|
"img = cv2.imread(filename=IMAGE_FILE_PATH)\n",
|
|
"img_bgr565 = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2BGR565)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"generic_inference_input_descriptor = kp.GenericImageInferenceDescriptor(\n",
|
|
" model_id=model_nef_descriptor.models[0].id,\n",
|
|
" inference_number=0,\n",
|
|
" input_node_image_list=[\n",
|
|
" kp.GenericInputNodeImage(\n",
|
|
" image=img_bgr565,\n",
|
|
" image_format=kp.ImageFormat.KP_IMAGE_FORMAT_RGB565,\n",
|
|
" resize_mode=kp.ResizeMode.KP_RESIZE_ENABLE,\n",
|
|
" padding_mode=kp.PaddingMode.KP_PADDING_CORNER,\n",
|
|
" normalize_mode=kp.NormalizeMode.KP_NORMALIZE_KNERON\n",
|
|
" )\n",
|
|
" ]\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"{\n",
|
|
" \"header\": {\n",
|
|
" \"inference_number\": 0,\n",
|
|
" \"crop_number\": 0,\n",
|
|
" \"num_output_node\": 1,\n",
|
|
" \"product_id\": 256,\n",
|
|
" \"num_hw_pre_proc_info\": 1,\n",
|
|
" \"hw_pre_proc_info_list\": {\n",
|
|
" \"0\": {\n",
|
|
" \"img_width\": 1200,\n",
|
|
" \"img_height\": 800,\n",
|
|
" \"resized_img_width\": 224,\n",
|
|
" \"resized_img_height\": 149,\n",
|
|
" \"pad_top\": 0,\n",
|
|
" \"pad_bottom\": 75,\n",
|
|
" \"pad_left\": 0,\n",
|
|
" \"pad_right\": 0,\n",
|
|
" \"model_input_width\": 224,\n",
|
|
" \"model_input_height\": 224,\n",
|
|
" \"crop_area\": {\n",
|
|
" \"crop_box_index\": 0,\n",
|
|
" \"x\": 0,\n",
|
|
" \"y\": 0,\n",
|
|
" \"width\": 0,\n",
|
|
" \"height\": 0\n",
|
|
" }\n",
|
|
" }\n",
|
|
" }\n",
|
|
" },\n",
|
|
" \"raw_result\": {\n",
|
|
" \"buffer_size\": 388\n",
|
|
" }\n",
|
|
"}\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"kp.inference.generic_image_inference_send(device_group=device_group,\n",
|
|
" generic_inference_input_descriptor=generic_inference_input_descriptor)\n",
|
|
"generic_raw_result = kp.inference.generic_image_inference_receive(device_group=device_group)\n",
|
|
"print(generic_raw_result)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[{\n",
|
|
" \"width\": 1,\n",
|
|
" \"height\": 1,\n",
|
|
" \"channel\": 1,\n",
|
|
" \"channels_ordering\": \"ChannelOrdering.KP_CHANNEL_ORDERING_CHW\",\n",
|
|
" \"num_data\": 1,\n",
|
|
" \"ndarray\": [\n",
|
|
" \"[[[[0.71443015]]]]\"\n",
|
|
" ]\n",
|
|
"}]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"inf_node_output_list = []\n",
|
|
"\n",
|
|
"for node_idx in range(generic_raw_result.header.num_output_node):\n",
|
|
" inference_float_node_output = kp.inference.generic_inference_retrieve_float_node(node_idx=node_idx,\n",
|
|
" generic_raw_result=generic_raw_result,\n",
|
|
" channels_ordering=kp.ChannelOrdering.KP_CHANNEL_ORDERING_CHW)\n",
|
|
" inf_node_output_list.append(inference_float_node_output)\n",
|
|
"\n",
|
|
"print(inf_node_output_list)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### post-processing\n",
|
|
"Kneron PLUS python version doesn't support on-NPU post-processing, so this step is demonstrated here"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 23,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The score of this photo is 0.7144301533699036\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"nd_array = inf_node_output_list[0].ndarray\n",
|
|
"number = float(nd_array.flatten()[0])\n",
|
|
"\n",
|
|
"print(\"The score of this photo is\", number)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|