下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
官网下载源码、模型和测试数据集:源代码:/ultralytics/ultralytics模型:/ultralytics/assets/releases/download/v0.0.0/yolov8m.pt测试数据:/s/1q8t6WUe2r9NavY-4jYOokA?pwd=mf4i安装依赖环境:cdultralyticspipinstall-rrequirements.txt-i/simplepythonsetup.pyinstall修改脚本(ultralytics/nn/tasks.py)defxywh2xyxy(x):
y=x.clone()
y[:,:,0]=x[:,:,0]-x[:,:,2]/2
#topleftx
y[:,:,1]=x[:,:,1]-x[:,:,3]/2
#toplefty
y[:,:,2]=x[:,:,0]+x[:,:,2]/2
#bottomrightx
y[:,:,3]=x[:,:,1]+x[:,:,3]/2
#bottomrighty
returnydefget_box(input_feature_np):
boxes_feat=input_feature_np[:,:,0:4]
boxes_xyxy=xywh2xyxy(boxes_feat)
returnboxes_xyxydefget_score(input_feature_np):
#confidence_feat=input_feature_np[:,:,4:5]
#classes_feat=input_feature_np[:,:,5:]
#score=confidence_feat*classes_feat
score=input_feature_np[:,:,4:]
returnscore
def_predict_once(self,x,profile=False,visualize=False):
"""
Performaforwardpassthroughthenetwork.
Args:
x(torch.Tensor):Theinputtensortothemodel.
profile(bool):
PrintthecomputationtimeofeachlayerifTrue,defaultstoFalse.
visualize(bool):SavethefeaturemapsofthemodelifTrue,defaultstoFalse
Returns:
(torch.Tensor):Thelastoutputofthemodel.
"""
y,dt=[],[]
#outputs
forminself.model:
ifm.f!=-1:
#ifnotfrompreviouslayer
x=y[m.f]ifisinstance(m.f,int)else[xifj==-1elsey[j]forjinm.f]
#fromearlierlayers
ifprofile:
self._profile_one_layer(m,x,dt)
x=m(x)
#run
y.append(xifm.iinself.saveelseNone)
#saveoutput
ifvisualize:
feature_visualization(x,m.type,m.i,save_dir=visualize)
xt=x.transpose(1,2)
bboxes=get_box(xt)
scores=get_score(xt)
returnbboxes,scores
returnx修改输出(ultralytics/engine/exporter.py)name添加导出onnx脚本importonnxfromonnximportGraphProto,TensorProtofromonnx.helperimportmake_tensor_value_info,make_attribute,make_node,make_modelfromultralyticsimportYOLOdefappend_nms(graph,unused_node=[]):
ngraph=GraphProto()
=
ngraph.input.extend([iforiingraph.inputifnotinunused_node])
ngraph.initializer.extend([iforiingraph.initializerifnotinunused_node])
ngraph.value_info.extend([iforiingraph.value_infoifnotinunused_node])
ngraph.node.extend([iforiingraph.nodeifnotinunused_node])
output_info=[iforiingraph.output]
ngraph.value_info.extend(output_info)
print(graph.output)
nms=make_node(
'DLNonMaxSuppression',
inputs=['bboxes','scores'],
outputs=['num_detections','nmsed_boxes','nmsed_scores','nmsed_classes']
)
nms.attribute.append(make_attribute('backgroundLabelId',-1))
nms.attribute.append(make_attribute('iouThreshold',0.45))
nms.attribute.append(make_attribute('isNormalized',False))
nms.attribute.append(make_attribute('keepTopK',200))
nms.attribute.append(make_attribute('numClasses',80))#
nms.attribute.append(make_attribute('shareLocation',True))
nms.attribute.append(make_attribute('scoreThreshold',0.25))
nms.attribute.append(make_attribute('topK',1000))
ngraph.node.append(nms)
num_detection=make_tensor_value_info('num_detections',TensorProto.INT32,[1,1])
nmsed_box=make_tensor_value_info('nmsed_boxes',TensorProto.FLOAT,[1,200,4])
nmsed_score=make_tensor_value_info('nmsed_scores',TensorProto.FLOAT,[1,200,1])
nmsed_class=make_tensor_value_info('nmsed_classes',TensorProto.FLOAT,[1,200,1])
ngraph.output.extend([num_detection,nmsed_box,nmsed_score,nmsed_class])
returnngraphdefexport_onnx():
#Loadamodel
model=YOLO("yolov8m.pt")
#loadapretrainedmodel(recommendedfortraining)
f=model.export(format="onnx")
#exportthemodeltoONNXformat
print('model.exportsuccess')
#Checks
model_onnx=onnx.load(f)
#loadonnxmodel
onnx.checker.check_model(model_onnx)
#checkonnxmodel
#Simplify
try:
importonnxsim
model_onnx,check=onnxsim.simplify(model_onnx)
assertcheck,'assertcheckfailed'
onnx.save(model_onnx,f)
exceptExceptionase:
print(f'simplifierfailure:{e}')
returnmodel_onnxmodel=export_onnx()model_attrs=dict(
ir_version=
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 车队安全培训总结反思
- 2026年消防安全及防火安全知识竞赛试题及答案
- 车间负责人安全培训讲话课件
- 2026年燃气安全知识竞赛试题及答案
- 车间级安全培训目的课件
- 车间级安全培训学时课件
- 2026年煤矿采煤机(掘进机)操作考试试题及答案
- 银行金融衍生品业务制度
- 2026年寄生虫及检验试题及答案
- 2026年电工考试题及答案
- 谷歌员工关系管理案例
- 班级互动小游戏-课件共30张课件-小学生主题班会版
- 物流企业仓储安全操作规程与培训教材
- 黄体酮破裂课件
- 中学学生教育惩戒规则实施方案(2025修订版)
- ISO 9001(DIS)-2026与ISO9001-2015英文标准对照版(编辑-2025年9月)
- 结算审计踏勘现场实施方案详细版
- 手机玻璃工厂年终总结报告
- 全国大学生职业规划大赛《信息与计算科学》专业生涯发展展示
- 急诊科护士年终总结汇报
- 瓦斯发电安全规程培训课件
评论
0/150
提交评论