当前位置:   article > 正文

ModuleNotFoundError: No module named ‘ultralytics.utils‘_modulenotfounderror: no module named 'ultralytics

modulenotfounderror: no module named 'ultralytics

一、错误提示

二、原因分析

使用多个yolov5项目,或者yolo项目,相互调用时,下级推理程序会直接调用上级(即根目录下)的程序文件,例如:下级推理文件会直接调用上级模型项目中utils包中的plots.py,而不会调用同级目录下的utils包。当上级项目目录下的程序文件发生变化更改时,就会发生调用错误。

三、解决方法

    使用onnx的权重格式,代替.pt格式

1)使用export.py文件导出best.onnx文件

2)推理文件

  1. import argparse
  2. import os
  3. import platform
  4. import sys
  5. from pathlib import Path
  6. import torch
  7. from models.common import DetectMultiBackend
  8. from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadScreenshots, LoadStreams
  9. from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
  10. increment_path, non_max_suppression, print_args, scale_boxes, strip_optimizer, xyxy2xywh)
  11. from utils.plots import Annotator, colors, save_one_box
  12. from utils.torch_utils import select_device, smart_inference_mode
  13. import onnxruntime
  14. def my_letter_box(img,size=(640,640)):
  15. h,w,c = img.shape
  16. r = min(size[0]/h,size[1]/w)
  17. new_h,new_w = int(h*r),int(w*r)
  18. top = int((size[0]-new_h)/2)
  19. left = int((size[1]-new_w)/2)
  20. bottom = size[0]-new_h-top
  21. right = size[1]-new_w-left
  22. img_resize = cv2.resize(img,(new_w,new_h))
  23. img = cv2.copyMakeBorder(img_resize,top,bottom,left,right,borderType=cv2.BORDER_CONSTANT,value=(114,114,114))
  24. return img,r,left,top
  25. def xywh2xyxy(boxes):
  26. xywh =copy.deepcopy(boxes)
  27. xywh[:,0]=boxes[:,0]-boxes[:,2]/2
  28. xywh[:,1]=boxes[:,1]-boxes[:,3]/2
  29. xywh[:,2]=boxes[:,0]+boxes[:,2]/2
  30. xywh[:,3]=boxes[:,1]+boxes[:,3]/2
  31. return xywh
  32. def my_nms(boxes,iou_thresh):
  33. index = np.argsort(boxes[:,4])[::-1]
  34. keep = []
  35. while index.size >0:
  36. i = index[0]
  37. keep.append(i)
  38. x1=np.maximum(boxes[i,0],boxes[index[1:],0])
  39. y1=np.maximum(boxes[i,1],boxes[index[1:],1])
  40. x2=np.minimum(boxes[i,2],boxes[index[1:],2])
  41. y2=np.minimum(boxes[i,3],boxes[index[1:],3])
  42. w = np.maximum(0,x2-x1)
  43. h = np.maximum(0,y2-y1)
  44. inter_area = w*h
  45. union_area = (boxes[i,2]-boxes[i,0])*(boxes[i,3]-boxes[i,1])+(boxes[index[1:],2]-boxes[index[1:],0])*(boxes[index[1:],3]-boxes[index[1:],1])
  46. iou = inter_area/(union_area-inter_area)
  47. idx = np.where(iou<=iou_thresh)[0]
  48. index = index[idx+1]
  49. return keep
  50. def restore_box(boxes,r,left,top):
  51. boxes[:,[0,2,5,7,9,11]]-=left
  52. boxes[:,[1,3,6,8,10,12]]-=top
  53. boxes[:,[0,2,5,7,9,11]]/=r
  54. boxes[:,[1,3,6,8,10,12]]/=r
  55. return boxes
  56. def post_precessing(dets,r,left,top,conf_thresh=0.3,iou_thresh=0.5):
  57. choice = dets[:,:,4]>conf_thresh
  58. dets=dets[choice]
  59. dets[:,13:15]*=dets[:,4:5]
  60. box = dets[:,:4]
  61. boxes = xywh2xyxy(box)
  62. score= np.max(dets[:,13:15],axis=-1,keepdims=True)
  63. index = np.argmax(dets[:,13:15],axis=-1).reshape(-1,1)
  64. output = np.concatenate((boxes,score,dets[:,5:13],index),axis=1)
  65. reserve_=my_nms(output,iou_thresh)
  66. output=output[reserve_]
  67. output = restore_box(output,r,left,top)
  68. return output
  69. weight='./best.onnx'
  70. providers = ['CPUExecutionProvider']
  71. session = onnxruntime.InferenceSession(wheelweight, providers=providers)
  72. img,r,left,top=my_letter_box(img)
  73. img =img[:,:,::-1].transpose(2,0,1).copy().astype(np.float32)
  74. img=img.reshape(1,*img.shape)
  75. y_onnx = session.run([session.get_outputs()[0].name], {session.get_inputs()[0].name: img})[0]
  76. outputs = post_precessing(y_onnx,r,left,top)
  77. if len(outputs)>1:
  78. continue
  79. for output in outputs:
  80. output = output.tolist()
  81. rect=output[:4]
  82. cv2.rectangle(img,(int(rect[0]),int(rect[1])),(int(rect[2]),int(rect[3])),(0,255,255),thickness=3)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/642336
推荐阅读
相关标签
  

闽ICP备14008679号