cat> list_models.py <<-'EOF'
from timm.models import create_model, is_model, list_models
model_names = list_models()
with open("models.txt","w") as f:for n in model_names:f.write(f"{n}\n")
EOF
python list_models.py
B.生成ONNX模型
cat> gen_onnx.py<<-'EOF'import timm
import sys
import torch
import os
from timm.utils.model import reparameterize_model
from timm.utils.onnx import onnx_exportONNX_MODEL ='model.onnx'if os.path.exists(ONNX_MODEL):os.remove(ONNX_MODEL)
model = timm.create_model(sys.argv[1],num_classes=2,in_chans=3,pretrained=False,exportable=True,)
model.eval()
input_size=model.default_cfg.get('input_size')
input_tensor = torch.ones((1,)+ input_size)
input_names =["input"]
output_names =["output"]
torch.onnx.export(model, input_tensor, ONNX_MODEL, verbose=False, input_names=input_names,output_names=output_names,opset_version=17,export_params=True)
EOF
C.生成RKNN模型
cat> gen_rknn.py<<-'EOF'import os
import urllib
import traceback
import time
import sys
import numpy as np
import cv2
from rknn.api import RKNNONNX_MODEL ='model.onnx'
RKNN_MODEL ='model.rknn'ifnot os.path.exists(ONNX_MODEL):exit(-1)if os.path.exists(RKNN_MODEL):os.remove(RKNN_MODEL)ifnot os.path.exists("./dataset.txt"):img=np.ones((224,224,3),dtype=np.int8)cv2.imwrite("img.jpg",img)withopen("./dataset.txt","w")as f:f.write("img.jpg")rknn = RKNN(verbose=False)
rknn.config(mean_values=[123.675,116.28,103.53], std_values=[58.82,58.82,58.82], target_platform='rk3588')
ret = rknn.load_onnx(model=ONNX_MODEL)if ret !=0:exit(ret)
ret = rknn.build(do_quantization=True, dataset='./dataset.txt')if ret !=0:exit(ret)
ret = rknn.export_rknn(RKNN_MODEL)if ret !=0:exit(ret)
rknn.release()if os.path.exists(RKNN_MODEL):print("BUILD SUCCESS")
EOF