国产精品久久久aaaa,日日干夜夜操天天插,亚洲乱熟女香蕉一区二区三区少妇,99精品国产高清一区二区三区,国产成人精品一区二区色戒,久久久国产精品成人免费,亚洲精品毛片久久久久,99久久婷婷国产综合精品电影,国产一区二区三区任你鲁

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

如何在TensorFlow2里使用Keras API創建一個自定義CNN網絡?

電子工程師 ? 來源:XILINX技術社區 ? 作者:張超 ? 2021-04-15 11:36 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

概述

本示例工程中我們會在 TensorFlow2 下使用 Keras API 創建一個自定義 CNN 網絡,在 Vitis-AI 1.3 環境下編譯成 Xilinx DPU 上運行的模型文件,并在 Xilinx zynqMP 上部署運行。

該示例工程應在 Vitis-AI1.3 Docker,vitis-ai-tensorflow2conda 環境中運行。

有關Mnist手寫數字數據集,請參見http://yann.lecun.com/exdb/mnist/。

在該例程中我們會演示以下的步驟:

使用 TensorFlow2 訓練和評估小型自定義卷積神經網絡

對浮點模型進行量化 (quantization)

對量化模型進行評估

使用校準數據集對訓練后的模型進行微調 (finetuning)

編譯量化和微調的模型,以創建 .xmodel 文件,準備在 DPU 加速器 IP 上執行在 zynqMP 評估板上下載并運行該模型

代碼說明

可從下面的 github 頁面獲得源碼。

https://github.com/lobster1989/Mnist-classification-Vitis-AI-1.3-TensorFlow2

Python 及 Shell 腳本源碼說明如下:

load_data.py:下載 Mnist 數據集;

generate_images.py:從 Keras 在線數據集生成本地圖像文件;

train.py:創建和訓練用于 Mnist 分類的簡單 CNN 模型, 經過訓練的浮點模型將被保存到本地;

quantize.py:使用 VitisQuantizer 量化前一個步驟保存的浮點模型,量化后的模型將被保存到本地;

eval_quantized.py:評估量化模型的 accuracy;

finetune.py:利用 Vitis-AI 進行模型微調(僅作演示,后續編譯部署的為 quantize 步驟保存的模型);

compile_zcu102.sh/compile_zcu104.sh:啟動 vai_c_tensorflow2 命令以將量化或微調的模型編譯為供 DPU 運行的 .xmodel 文件。兩個文件分別對應目標為 zcu102 和 zcu104 開發板;

make_target_zcu102.sh/make_target_zcu104.sh:將.xmodel和圖像復制到 。/target_zcu10x 文件夾中,準備好將其復制到 zcu102/zcu104 評估板的 SD 卡中。

模型創建/量化/編譯

在運行此部分之前,我們應在 Vitis-AI docker 環境下激活 vitis-ai-tensorflow2anaconda 環境。有關更多詳細信息,請參閱最新版本的 UG1414。

(vitis-ai-tensorflow2) Vitis-AI /workspace/myproj/tf2-mnist-end-to-end 》 python train.py

Load Mnist dataset.。

Create custom cnn.。

Model: “mnist_customcnn_model”

_________________________________________________________________

Layer (type) Output Shape Param #

=================================================================

input_1 (InputLayer) [(None, 28, 28, 1)] 0

_________________________________________________________________

conv2d (Conv2D) (None, 26, 26, 32) 320

_________________________________________________________________

max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0

_________________________________________________________________

conv2d_1 (Conv2D) (None, 11, 11, 64) 18496

_________________________________________________________________

max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64) 0

_________________________________________________________________

conv2d_2 (Conv2D) (None, 3, 3, 64) 36928

_________________________________________________________________

flatten (Flatten) (None, 576) 0

_________________________________________________________________

dense (Dense) (None, 64) 36928

_________________________________________________________________

dense_1 (Dense) (None, 10) 650

=================================================================

Total params: 93,322

Trainable params: 93,322

Non-trainable params: 0

_________________________________________________________________

Fit on dataset.。

Epoch 1/10

782/782 [==============================] - 13s 16ms/step - loss: 0.1843 - accuracy: 0.9427 - val_loss: 0.0701 - val_accuracy: 0.9813

Epoch 2/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0529 - accuracy: 0.9835 - val_loss: 0.0543 - val_accuracy: 0.9855

Epoch 3/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0346 - accuracy: 0.9894 - val_loss: 0.0472 - val_accuracy: 0.9877

Epoch 4/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0252 - accuracy: 0.9929 - val_loss: 0.0463 - val_accuracy: 0.9878

Epoch 5/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0188 - accuracy: 0.9945 - val_loss: 0.0494 - val_accuracy: 0.9896

Epoch 6/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0147 - accuracy: 0.9956 - val_loss: 0.0513 - val_accuracy: 0.9901

Epoch 7/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0121 - accuracy: 0.9966 - val_loss: 0.0452 - val_accuracy: 0.9920

Epoch 8/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0096 - accuracy: 0.9973 - val_loss: 0.0542 - val_accuracy: 0.9906

Epoch 9/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0088 - accuracy: 0.9976 - val_loss: 0.0640 - val_accuracy: 0.9893

Epoch 10/10

782/782 [==============================] - 5s 7ms/step - loss: 0.0073 - accuracy: 0.9978 - val_loss: 0.0709 - val_accuracy: 0.9893

Save trained model to./models/float_model.h5.

Evaluate model on test dataset.。

157/157 [==============================] - 1s 3ms/step - loss: 0.0426 - accuracy: 0.9911

loss: 0.043

acc: 0.991

(vitis-ai-tensorflow2) Vitis-AI /workspace/myproj/tf2-mnist-end-to-end 》 python quantize.py

Load float model.。

Load Mnist dataset.。

Run quantization.。

[INFO] Start CrossLayerEqualization.。.

10/10 [==============================] - 0s 32ms/step

[INFO] CrossLayerEqualization Done.

[INFO] Start Quantize Calibration.。.

157/157 [==============================] - 12s 77ms/step

[INFO] Quantize Calibration Done.

[INFO] Start Generating Quantized Model.。.

[Warning] Skip quantize pos adjustment for layer quant_dense, its quantize pos is [i=None, w=8.0, b=9.0, o=3.0]

[INFO] Generating Quantized Model Done.

Saved quantized model as 。/models/quantized_model.h5

(vitis-ai-tensorflow2) Vitis-AI /workspace/myproj/tf2-mnist-end-to-end 》 python eval_quantized.py

Load quantized model.。

WARNINGNo training configuration found in the save file, so the model was *not* compiled. Compile it manually.

Load Mnist dataset.。

Compile model.。

Evaluate model on test Dataset

157/157 [==============================] - 4s 22ms/step - loss: 0.0417 - accuracy: 0.9913

loss: 0.042

acc: 0.991

(vitis-ai-tensorflow2) Vitis-AI /workspace/myproj/tf2-mnist-end-to-end 》 python finetune.py

Load float model.。

Load Mnist dataset.。

Create quantize training model.。

[INFO] Start CrossLayerEqualization.。.

10/10 [==============================] - 0s 33ms/step

[INFO] CrossLayerEqualization Done.

Compiling model.。

Fit on Dataset.。

Epoch 1/10

782/782 [==============================] - 48s 61ms/step - loss: 0.0077 - accuracy: 0.9978 - val_loss: 0.0738 - val_accuracy: 0.9882

Epoch 2/10

782/782 [==============================] - 39s 50ms/step - loss: 0.0062 - accuracy: 0.9980 - val_loss: 0.0845 - val_accuracy: 0.9888

Epoch 3/10

782/782 [==============================] - 40s 51ms/step - loss: 0.0058 - accuracy: 0.9983 - val_loss: 0.0810 - val_accuracy: 0.9885

Epoch 4/10

782/782 [==============================] - 40s 51ms/step - loss: 0.0061 - accuracy: 0.9982 - val_loss: 0.0744 - val_accuracy: 0.9902

Epoch 5/10

782/782 [==============================] - 40s 51ms/step - loss: 0.0048 - accuracy: 0.9984 - val_loss: 0.0834 - val_accuracy: 0.9911

Epoch 6/10

782/782 [==============================] - 39s 50ms/step - loss: 0.0047 - accuracy: 0.9986 - val_loss: 0.0807 - val_accuracy: 0.9893

Epoch 7/10

782/782 [==============================] - 39s 50ms/step - loss: 0.0039 - accuracy: 0.9987 - val_loss: 0.0894 - val_accuracy: 0.9903

Epoch 8/10

782/782 [==============================] - 39s 50ms/step - loss: 0.0034 - accuracy: 0.9989 - val_loss: 0.0863 - val_accuracy: 0.9904

Epoch 9/10

782/782 [==============================] - 39s 49ms/step - loss: 0.0042 - accuracy: 0.9989 - val_loss: 0.1043 - val_accuracy: 0.9893

Epoch 10/10

782/782 [==============================] - 39s 50ms/step - loss: 0.0044 - accuracy: 0.9986 - val_loss: 0.0994 - val_accuracy: 0.9908

Saved finetuned model as 。/models/finetuned_model.h5

Evaluate model on test Dataset.。

157/157 [==============================] - 1s 7ms/step - loss: 0.0675 - accuracy: 0.9920

loss: 0.068

acc: 0.992

(vitis-ai-tensorflow2) Vitis-AI /workspace/myproj/tf2-mnist-end-to-end 》 bash -x compile_zcu102.sh

+ ARCH=/opt/vitis_ai/compiler/arch/DPUCZDX8G/ZCU102/arch.json

+ OUTDIR=。/compiled_model

+ NET_NAME=customcnn

+ MODEL=。/models/finetuned_model.h5

+ echo -----------------------------------------

-----------------------------------------

+ echo ‘COMPILING MODEL FOR ZCU102.。’

COMPILING MODEL FOR ZCU102.。

+ echo -----------------------------------------

-----------------------------------------

+ compile

+ tee compile.log

+ vai_c_tensorflow2 --model 。/models/finetuned_model.h5 --arch /opt/vitis_ai/compiler/arch/DPUCZDX8G/ZCU102/arch.json --output_dir 。/compiled_model --net_name customcnn

/opt/vitis_ai/conda/envs/vitis-ai-tensorflow2/lib/python3.7/site-packages/xnnc/translator/tensorflow_translator.py H5pyDeprecationWarning: dataset.value has been deprecated. Use dataset[()] instead.

value = param.get(group).get(ds).value

[INFO] parse raw model :100%|██████████| 10/10 [00:00《00:00, 16871.70it/s]

[INFO] infer shape (NHWC) :100%|██████████| 26/26 [00:00《00:00, 2956.30it/s]

[INFO] generate xmodel :100%|██████████| 26/26 [00:00《00:00, 5561.60it/s]

[INFO] Namespace(inputs_shape=None, layout=‘NHWC’, model_files=[‘。/models/finetuned_model.h5’], model_type=‘tensorflow2’, out_filename=‘。/compiled_model/customcnn_org.xmodel’, proto=None)

[INFO] tensorflow2 model: models/finetuned_model.h5

[OPT] No optimization method available for xir-level optimization.

[INFO] generate xmodel: /workspace/myproj/tf2-mnist-end-to-end/compiled_model/customcnn_org.xmodel

[UNILOG][INFO] The compiler log will be dumped at “/tmp/vitis-ai-user/log/xcompiler-20210325-093926-3120”

[UNILOG][INFO] Target architecture: DPUCZDX8G_ISA0_B4096_MAX_BG2

[UNILOG][INFO] Compile mode: dpu

[UNILOG][INFO] Debug mode: function

[UNILOG][INFO] Target architecture: DPUCZDX8G_ISA0_B4096_MAX_BG2

[UNILOG][INFO] Graph name: mnist_customcnn_model, with op num: 42

[UNILOG][INFO] Begin to compile.。.

[UNILOG][INFO] Total device subgraph number 3, DPU subgraph number 1

[UNILOG][INFO] Compile done.

[UNILOG][INFO] The meta json is saved to “/workspace/myproj/tf2-mnist-end-to-end/。/compiled_model/meta.json”

[UNILOG][INFO] The compiled xmodel is saved to “/workspace/myproj/tf2-mnist-end-to-end/。/compiled_model/customcnn.xmodel”

[UNILOG][INFO] The compiled xmodel‘s md5sum is 764142e83d074ea9470b9eb9d0757f68, and been saved to “/workspace/myproj/tf2-mnist-end-to-end/。/compiled_model/md5sum.txt”

**************************************************

* VITIS_AI Compilation - Xilinx Inc.

**************************************************

+ echo -----------------------------------------

-----------------------------------------

+ echo ’MODEL COMPILED‘

MODEL COMPILED

+ echo -----------------------------------------

-----------------------------------------

(vitis-ai-tensorflow2) Vitis-AI /workspace/myproj/tf2-mnist-end-to-end 》 bash -x make_target_zcu102.sh

+ echo -----------------------------------------

-----------------------------------------

+ echo ’MAKE TARGET ZCU102 STARTED.。‘

MAKE TARGET ZCU102 STARTED.。

+ echo -----------------------------------------

-----------------------------------------

+ TARGET_ZCU102=。/target_zcu102

+ COMPILE_ZCU102=。/compiled_model

+ APP=。/application

+ NET_NAME=customcnn

+ rm -rf 。/target_zcu102

+ mkdir -p 。/target_zcu102/model_dir

+ cp 。/application/app_mt.py 。/target_zcu102

+ echo ’ Copied application to TARGET_ZCU102 folder‘

Copied application to TARGET_ZCU102 folder

+ cp 。/compiled_model/customcnn.xmodel 。/target_zcu102/model_dir/。

+ echo ’ Copied xmodel file(s) to TARGET_ZCU102 folder‘

Copied xmodel file(s) to TARGET_ZCU102 folder

+ mkdir -p 。/target_zcu102/images

+ python generate_images.py --dataset=mnist --image_dir=。/target_zcu102/images --image_format=jpg --max_images=10000

2021-03-25 0934.445257: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1

Command line options:

--dataset : mnist

--subset : test

--image_dir : 。/target_zcu102/images

--image_list :

--label_list :

--image_format : jpg

--max_images : 10000

+ echo ’ Copied images to TARGET_ZCU102 folder‘

Copied images to TARGET_ZCU102 folder

+ echo -----------------------------------------

-----------------------------------------

+ echo ’MAKE TARGET ZCU102 COMPLETED‘

MAKE TARGET ZCU102 COMPLETED

+ echo -----------------------------------------

-----------------------------------------

在 ZynqMP 評估板上運行

進行電路板設置。

之后,將 target_zcu10x 目錄中的所有文件復制到 SD 卡。從 SD 卡啟動 ZCU10x(下面實例運行的是 zcu104),運行 app_mt.py。結果顯示,實際運行模型的精度為 99.13%.

root@xilinx-zcu104-2020_2:~/target_zcu104# python3 app_mt.py --threads 4

Command line options:

--image_dir : images

--threads : 4

--model : model_dir/customcnn.xmodel

Pre-processing 10000 images.。.

Starting 4 threads.。.

Throughput=5622.61 fps, total frames = 10000, time=1.7785 seconds

Correct:9913, Wrong:87, Accuracy:0.9913

原文標題:開發者分享 | Vitis-AI 1.3/TensorFlow2 環境下使用自定義 CNN 模型進行 Mnist 分類

文章出處:【微信公眾號:XILINX技術社區】歡迎添加關注!文章轉載請注明出處。

責任編輯:haq

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • API
    API
    +關注

    關注

    2

    文章

    2338

    瀏覽量

    66675
  • 模型
    +關注

    關注

    1

    文章

    3730

    瀏覽量

    52043

原文標題:開發者分享 | Vitis-AI 1.3/TensorFlow2 環境下使用自定義 CNN 模型進行 Mnist 分類

文章出處:【微信號:gh_2d1c7e2d540e,微信公眾號:XILINX開發者社區】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    何在TensorFlow Lite Micro中添加自定義操作符(2

    reshape算子進行說明,如何將reshape算子注冊到解析器中,接下來介紹如果我們想自定義算子需要干些什么。
    的頭像 發表于 12-26 10:53 ?1003次閱讀

    何在TensorFlow Lite Micro中添加自定義操作符(1)

    來支持各種神經網絡層的計算。也就是說,我們不僅可以使用內嵌的算子運算,還可以自己注冊新的算子,更加的靈活。本期就將用兩期的文章以 `reshape.cpp` 為例,詳細說明如何在
    的頭像 發表于 12-26 10:34 ?4957次閱讀

    何在e203 SOC中添加自定義外設

    何在E203 中添加自定義的外設,困擾已久,以下是從別處借鑒而來的方法: 1、設計好自定義外設的RTL后,在其中添加ICB總線的握手協
    發表于 10-20 10:38

    構建自定義電商數據分析API

    ? 在電商業務中,數據是驅動決策的核心。隨著數據量的增長,企業需要實時、靈活的分析工具來監控銷售、用戶行為和庫存等指標。自定義電商數據分析API(應用程序接口)可以自動化數據提取和
    的頭像 發表于 07-17 14:44 ?572次閱讀
    構建<b class='flag-5'>自定義</b>電商數據分析<b class='flag-5'>API</b>

    KiCad 中的自定義規則(KiCon 演講)

    “ ?Seth Hillbrand 在 KiCon US 2025 上為大家介紹了 KiCad 的規則系統,并詳細講解了自定義規則的設計與實例。? ” ? 演講主要圍繞 加強 KiCad 中的自定義
    的頭像 發表于 06-16 11:17 ?2031次閱讀
    KiCad 中的<b class='flag-5'>自定義</b>規則(KiCon 演講)

    HarmonyOS應用自定義鍵盤解決方案

    自定義鍵盤是種替換系統默認鍵盤的解決方案,可實現鍵盤個性化交互。允許用戶結合業務需求與操作習慣,對按鍵布局進行可視化重構、設置多功能組合鍵位,使輸入更加便捷和舒適。在安全防護層面,自定義鍵盤可以
    的頭像 發表于 06-05 14:19 ?2318次閱讀

    MIMXRT1176如何在MBDT中正確實現自定義LUT?

    我正在嘗試創建自定義 LUT,但不確定如何進行。默認閃存類型為 ISSI IS25WP064A-JBLE,但我想使用 FM25V02A-GTR。我嘗試用我自己的 LUT 條目修改生成的文件
    發表于 04-08 07:56

    如何添加自定義單板

    在開發過程中,用戶有時需要創建自定義板配置。本節將通過實例講解用戶如何創建屬于自己的machine,下面以g
    的頭像 發表于 03-12 14:43 ?1326次閱讀

    e203在vivado硬件自定義指令識別為非法指令怎么解決?

    e203自定義指令硬件模塊設計,修改內核,綜合沒錯誤,軟件也修改工具鏈通過并產生verilog文件,但在vivado硬件自定義指令識別為非法指令怎么解決
    發表于 03-07 07:34

    使用OpenVINO? 2020.4.582將自定義TensorFlow 2模型轉換為中間表示 (IR)收到錯誤怎么解決?

    轉換自定義 TensorFlow 2 模型 mask_rcnn_inception_resnet_v2_1024x1024_coco17 要 IR 使用模型優化器命令: 注意上面的
    發表于 03-07 07:28

    將YOLOv4模型轉換為IR的說明,無法將模型轉換為TensorFlow2格式怎么解決?

    遵照 將 YOLOv4 模型轉換為 IR 的 說明,但無法將模型轉換為 TensorFlow2* 格式。 將 YOLOv4 darknet 轉換為 Keras 模型時,收到 TypeError: buffer is too small for requested arr
    發表于 03-07 07:14

    如何將Keras H5模型轉換為中間表示 (IR) 格式?

    自定義層 CustomLayer 的模型: import tensorflow as tf from custom_layer import CustomLayer model
    發表于 03-07 06:11

    使用Python API在OpenVINO?中創建了用于異步推理的自定義代碼,輸出張量的打印結果會重復,為什么?

    使用 Python* API 在 OpenVINO? 中創建了用于異步推理的自定義代碼。 遇到輸出張量的打印結果會重復的問題,即使輸入圖像不同。
    發表于 03-06 07:53

    為什么無法將TensorFlow自定義模型轉換為IR格式?

    TensorFlow* 自定義模型轉換為 IR 格式: mo --data_type FP16 --saved_model_dir--input_shape (1,150,150,3
    發表于 03-05 07:26

    為什么無法將自定義EfficientDet模型從TensorFlow 2轉換為中間表示(IR)?

    自定義 EfficientDet 模型從 TensorFlow* 2 轉換 為 IR 時遇到錯誤: [ ERROR ] Exception occurred during running replacer \"REPLACEM
    發表于 03-05 06:29